Skip to content

🎬 火宝短剧 - 基于AI的一站式短剧生成平台 《一句话生成完整短剧,从剧本到成片全自动化》 Huobao Drama - An AI-Powered End-to-End Short Drama Generator "One Sentence to Complete Drama: Fully Automated from Script to Final Video"

TypeScript

feat: create agent types and SSE streaming API client

Connorff6e625734ec
diff --git a/web/src/api/agent.ts b/web/src/api/agent.ts
new file mode 100644
index 0000000..2ae1a3e
--- /dev/null
+++ b/web/src/api/agent.ts
@@ -0,0 +1,40 @@
+import type { AgentChatRequest, AgentSSEEvent } from '@/types/agent'
+
+const BASE = import.meta.env.VITE_API_PREFIX || '/api/v1'
+
+export const agentAPI = {
+ async *streamChat(agentType: string, data: AgentChatRequest): AsyncGenerator<AgentSSEEvent> {
+ const response = await fetch(`${BASE}/agent/${agentType}/chat`, {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(data),
+ })
+
+ if (!response.ok) throw new Error(`Agent request failed: ${response.status}`)
+ if (!response.body) throw new Error('No response body')
+
+ const reader = response.body.getReader()
+ const decoder = new TextDecoder()
+ let buffer = ''
+
+ while (true) {
+ const { done, value } = await reader.read()
+ if (done) break
+
+ buffer += decoder.decode(value, { stream: true })
+ const lines = buffer.split('\n')
+ buffer = lines.pop() || ''
+
+ for (const line of lines) {
+ if (line.startsWith('data: ')) {
+ try {
+ const event: AgentSSEEvent = JSON.parse(line.slice(6))
+ yield event
+ } catch {
+ // skip malformed lines
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/web/src/types/agent.ts b/web/src/types/agent.ts
new file mode 100644
index 0000000..ec3d151
--- /dev/null
+++ b/web/src/types/agent.ts
@@ -0,0 +1,27 @@
+export interface AgentChatRequest {
+ message: string
+ drama_id?: number
+ episode_id?: number
+}
+
+export interface AgentSSEEvent {
+ type: 'tool_call' | 'tool_result' | 'content' | 'done' | 'error'
+ data: string
+ tool_name?: string
+}
+
+export type AgentType =
+ | 'script_rewriter'
+ | 'style_analyzer'
+ | 'extractor'
+ | 'voice_assigner'
+ | 'storyboard_breaker'
+ | 'prompt_generator'
+
+export interface AgentMessage {
+ role: 'user' | 'assistant' | 'system'
+ content: string
+ agentType?: AgentType
+ toolCalls?: { name: string; result: string }[]
+ timestamp: number
+}