mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
feat: agent
This commit is contained in:
parent
8f3a09f85a
commit
f835bb0774
5 changed files with 70 additions and 27 deletions
|
|
@ -111,5 +111,5 @@ async def delete_agent(
|
||||||
agent_id=agent_id,
|
agent_id=agent_id,
|
||||||
owner_id=current_user_id
|
owner_id=current_user_id
|
||||||
)
|
)
|
||||||
return success_response(data=None, message="Agent deleted successfully")
|
return success_response(data={}, message="Agent deleted successfully")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ class Settings(BaseSettings):
|
||||||
AWS_REGION: str = "us-east-1"
|
AWS_REGION: str = "us-east-1"
|
||||||
S3_BUCKET_NAME: str
|
S3_BUCKET_NAME: str
|
||||||
S3_ENDPOINT_URL: str | None = None
|
S3_ENDPOINT_URL: str | None = None
|
||||||
|
S3_PUBLIC_BASE_URL: str | None = None
|
||||||
|
|
||||||
# Security
|
# Security
|
||||||
SECRET_KEY: str
|
SECRET_KEY: str
|
||||||
|
|
|
||||||
46
main/live-agent-api/env.example
Normal file
46
main/live-agent-api/env.example
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
# Copy this file to .env and fill in your actual values
|
||||||
|
|
||||||
|
# Database Configuration
|
||||||
|
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/live_agent
|
||||||
|
|
||||||
|
# AWS S3 / MinIO Configuration
|
||||||
|
# S3-compatible storage credentials
|
||||||
|
AWS_ACCESS_KEY_ID=minioadmin
|
||||||
|
AWS_SECRET_ACCESS_KEY=minioadmin
|
||||||
|
|
||||||
|
# AWS Region (use default for MinIO)
|
||||||
|
AWS_REGION=us-east-1
|
||||||
|
|
||||||
|
# S3 Bucket Name
|
||||||
|
S3_BUCKET_NAME=test-bucket
|
||||||
|
|
||||||
|
# S3 Endpoint URL
|
||||||
|
# For MinIO local development (outside Docker): http://localhost:9000
|
||||||
|
# For MinIO inside Docker: http://minio:9000
|
||||||
|
# For AWS S3: leave empty or comment out
|
||||||
|
S3_ENDPOINT_URL=http://localhost:9000
|
||||||
|
|
||||||
|
# S3 Public Base URL
|
||||||
|
# This is the URL prefix used to generate public file URLs
|
||||||
|
# For local MinIO: http://localhost:9000
|
||||||
|
# For production S3: https://s3.amazonaws.com or your CloudFront URL
|
||||||
|
S3_PUBLIC_BASE_URL=http://localhost:9000
|
||||||
|
|
||||||
|
# Security Configuration
|
||||||
|
# Secret key for JWT token signing and other security features
|
||||||
|
# Generate with: python -c "import secrets; print(secrets.token_urlsafe(32))"
|
||||||
|
SECRET_KEY=your-secret-key-here-change-in-production
|
||||||
|
|
||||||
|
# Token expiration time in days
|
||||||
|
TOKEN_EXPIRE_DAYS=7
|
||||||
|
|
||||||
|
# Server Configuration
|
||||||
|
HOST=0.0.0.0
|
||||||
|
PORT=8080
|
||||||
|
|
||||||
|
# Debug mode (set to False in production)
|
||||||
|
DEBUG=True
|
||||||
|
|
||||||
|
# External Services
|
||||||
|
FISH_API_KEY=your-fish-audio-api-key-here
|
||||||
|
|
||||||
|
|
@ -1,57 +1,48 @@
|
||||||
"""
|
"""
|
||||||
S3 infrastructure - AWS S3 connection and session management
|
S3 infrastructure - AWS S3 connection and session management
|
||||||
"""
|
"""
|
||||||
from typing import AsyncGenerator
|
|
||||||
import aioboto3
|
import aioboto3
|
||||||
from types import SimpleNamespace
|
|
||||||
|
|
||||||
from config import settings
|
from config import settings
|
||||||
|
|
||||||
|
|
||||||
s3_session = aioboto3.Session()
|
s3_session = aioboto3.Session()
|
||||||
|
_s3_client = None
|
||||||
|
|
||||||
|
|
||||||
async def get_s3() -> AsyncGenerator[SimpleNamespace, None]:
|
async def get_s3() :
|
||||||
"""
|
"""
|
||||||
Dependency to get S3 client
|
Dependency to get S3 client
|
||||||
|
|
||||||
Usage:
|
|
||||||
@router.post("/upload")
|
|
||||||
async def upload(s3 = Depends(get_s3)):
|
|
||||||
await s3.put_object(...)
|
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
S3 client with async context management
|
S3 client
|
||||||
"""
|
"""
|
||||||
async with s3_session.client(
|
if _s3_client is None:
|
||||||
's3',
|
raise RuntimeError("S3 client not initialized. Call init_s3() first.")
|
||||||
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
return _s3_client
|
||||||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
|
||||||
region_name=settings.AWS_REGION,
|
|
||||||
endpoint_url=settings.S3_ENDPOINT_URL
|
|
||||||
) as client:
|
|
||||||
yield client
|
|
||||||
|
|
||||||
|
|
||||||
async def init_s3():
|
async def init_s3():
|
||||||
"""
|
"""
|
||||||
Initialize S3 connection and verify bucket access
|
Initialize S3 connection and verify bucket access
|
||||||
(called in lifespan startup)
|
(called in lifespan startup)
|
||||||
"""
|
"""
|
||||||
|
global _s3_client
|
||||||
try:
|
try:
|
||||||
async with s3_session.client(
|
_s3_client = await s3_session.client(
|
||||||
's3',
|
's3',
|
||||||
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
aws_access_key_id=settings.AWS_ACCESS_KEY_ID,
|
||||||
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
aws_secret_access_key=settings.AWS_SECRET_ACCESS_KEY,
|
||||||
region_name=settings.AWS_REGION,
|
region_name=settings.AWS_REGION,
|
||||||
endpoint_url=settings.S3_ENDPOINT_URL
|
endpoint_url=settings.S3_ENDPOINT_URL
|
||||||
) as client:
|
).__aenter__()
|
||||||
|
|
||||||
# Verify bucket exists and accessible
|
# Verify bucket exists and accessible
|
||||||
await client.head_bucket(Bucket=settings.S3_BUCKET_NAME)
|
await _s3_client.head_bucket(Bucket=settings.S3_BUCKET_NAME)
|
||||||
print(f"S3 connection verified: bucket '{settings.S3_BUCKET_NAME}'")
|
print(f"S3 connection verified: bucket '{settings.S3_BUCKET_NAME}'")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Warning: S3 connection verification failed: {e}")
|
print(f"Warning: S3 connection verification failed: {e}")
|
||||||
print("S3 operations may not work properly")
|
print("S3 operations may not work properly")
|
||||||
|
raise
|
||||||
|
|
||||||
|
|
||||||
async def close_s3():
|
async def close_s3():
|
||||||
|
|
@ -61,5 +52,9 @@ async def close_s3():
|
||||||
Note: aioboto3 handles cleanup automatically via context managers,
|
Note: aioboto3 handles cleanup automatically via context managers,
|
||||||
but this function is provided for symmetry with database layer
|
but this function is provided for symmetry with database layer
|
||||||
"""
|
"""
|
||||||
|
global _s3_client
|
||||||
|
if _s3_client:
|
||||||
|
await _s3_client.__aexit__(None, None, None)
|
||||||
|
_s3_client = None
|
||||||
print("S3 connections closed")
|
print("S3 connections closed")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -68,13 +68,14 @@ class FileRepository:
|
||||||
Bucket=settings.S3_BUCKET_NAME,
|
Bucket=settings.S3_BUCKET_NAME,
|
||||||
Key=s3_key,
|
Key=s3_key,
|
||||||
Body=content,
|
Body=content,
|
||||||
ContentType=file.content_type or 'application/octet-stream'
|
ContentType=file.content_type or 'application/octet-stream',
|
||||||
)
|
)
|
||||||
|
|
||||||
# Generate public URL
|
# Generate public URL
|
||||||
url = f"https://{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/{s3_key}"
|
url = f"{settings.S3_PUBLIC_BASE_URL}/{settings.S3_BUCKET_NAME}/{s3_key}"
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
async def delete(s3, url: str) -> bool:
|
async def delete(s3, url: str) -> bool:
|
||||||
"""
|
"""
|
||||||
|
|
@ -89,7 +90,7 @@ class FileRepository:
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Extract S3 key from URL
|
# Extract S3 key from URL
|
||||||
key = url.split(f"{settings.S3_BUCKET_NAME}.s3.{settings.AWS_REGION}.amazonaws.com/")[-1]
|
key = url.split(f"{settings.S3_PUBLIC_BASE_URL}/{settings.S3_BUCKET_NAME}/")[-1]
|
||||||
|
|
||||||
await s3.delete_object(
|
await s3.delete_object(
|
||||||
Bucket=settings.S3_BUCKET_NAME,
|
Bucket=settings.S3_BUCKET_NAME,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue