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: comprehensive agent logging

Connor4a00b6bb999c
diff --git a/backend/src/routes/agent.ts b/backend/src/routes/agent.ts
index 1a1e91d..9531239 100644
--- a/backend/src/routes/agent.ts
+++ b/backend/src/routes/agent.ts
@@ -1,11 +1,23 @@
/**
- * Agent SSE 聊天路由
+ * Agent SSE 聊天路由 — 完整日志版
*/
import { Hono } from 'hono'
import { streamSSE } from 'hono/streaming'
import { createAgent, validAgentTypes } from '../agents/index.js'
import { success, badRequest } from '../utils/response.js'
+const C = {
+ r: '\x1b[0m', dim: '\x1b[2m', bold: '\x1b[1m',
+ cyan: '\x1b[36m', green: '\x1b[32m', yellow: '\x1b[33m',
+ red: '\x1b[31m', magenta: '\x1b[35m', blue: '\x1b[34m',
+}
+
+function ts() { return new Date().toLocaleTimeString('zh-CN', { hour12: false }) }
+
+function log(tag: string, color: string, ...args: any[]) {
+ console.log(`${C.dim}${ts()}${C.r} ${color}[${tag}]${C.r}`, ...args)
+}
+
const app = new Hono()
// POST /agent/:type/chat — SSE 流式 Agent 对话
@@ -18,57 +30,96 @@ app.post('/:type/chat', async (c) => {
const body = await c.req.json()
const { message, drama_id, episode_id } = body
+ log('Agent', C.cyan, `START ${C.bold}${agentType}${C.r} | drama=${drama_id} episode=${episode_id}`)
+ log('Agent', C.cyan, `message: "${message}"`)
+
if (!episode_id || !drama_id) {
+ log('Agent', C.red, 'MISSING drama_id or episode_id')
return badRequest(c, 'drama_id and episode_id are required')
}
- // 每次请求创建新 agent(工具已注入 episodeId/dramaId)
const agent = createAgent(agentType, episode_id, drama_id)
- if (!agent) return badRequest(c, 'Agent not found')
+ if (!agent) {
+ log('Agent', C.red, `UNKNOWN agent type: ${agentType}`)
+ return badRequest(c, 'Agent not found')
+ }
- console.log(`[Agent] ${agentType} | drama=${drama_id} episode=${episode_id} | "${message}"`)
+ const startTime = performance.now()
return streamSSE(c, async (stream) => {
try {
+ log('Agent', C.blue, 'calling LLM stream...')
const result = await agent.stream([
{ role: 'user', content: message },
])
+ let chunkCount = 0
+ let textLength = 0
+ let toolCallCount = 0
+ let toolResultCount = 0
+
for await (const chunk of result.fullStream) {
- if (chunk.type === 'text-delta') {
- await stream.writeSSE({
- data: JSON.stringify({ type: 'content', data: chunk.textDelta }),
- })
- } else if (chunk.type === 'tool-call') {
- console.log(`[Agent] tool_call: ${chunk.toolName}(${JSON.stringify(chunk.args).slice(0, 200)})`)
- await stream.writeSSE({
- data: JSON.stringify({
- type: 'tool_call',
- data: JSON.stringify(chunk.args),
- tool_name: chunk.toolName,
- }),
- })
- } else if (chunk.type === 'tool-result') {
- const resultStr = chunk.result != null
- ? (typeof chunk.result === 'string' ? chunk.result : JSON.stringify(chunk.result))
- : ''
- console.log(`[Agent] tool_result: ${chunk.toolName} → ${resultStr.slice(0, 100)}`)
- await stream.writeSSE({
- data: JSON.stringify({
- type: 'tool_result',
- data: resultStr.length > 2000 ? resultStr.slice(0, 2000) + '...[truncated]' : resultStr,
- tool_name: chunk.toolName,
- }),
- })
+ chunkCount++
+
+ switch (chunk.type) {
+ case 'text-delta':
+ textLength += (chunk.textDelta || '').length
+ await stream.writeSSE({
+ data: JSON.stringify({ type: 'content', data: chunk.textDelta }),
+ })
+ break
+
+ case 'tool-call':
+ toolCallCount++
+ const argsStr = chunk.args != null ? JSON.stringify(chunk.args) : '{}'
+ log('Tool', C.magenta, `CALL ${C.bold}${chunk.toolName}${C.r}(${argsStr.slice(0, 300)})`)
+ await stream.writeSSE({
+ data: JSON.stringify({
+ type: 'tool_call',
+ data: argsStr,
+ tool_name: chunk.toolName,
+ }),
+ })
+ break
+
+ case 'tool-result':
+ toolResultCount++
+ const resultStr = chunk.result != null
+ ? (typeof chunk.result === 'string' ? chunk.result : JSON.stringify(chunk.result))
+ : '(empty)'
+ log('Tool', C.green, `RESULT ${C.bold}${chunk.toolName}${C.r} → ${resultStr.slice(0, 200)}`)
+ await stream.writeSSE({
+ data: JSON.stringify({
+ type: 'tool_result',
+ data: resultStr.length > 2000 ? resultStr.slice(0, 2000) + '...[truncated]' : resultStr,
+ tool_name: chunk.toolName,
+ }),
+ })
+ break
+
+ case 'error':
+ log('Agent', C.red, `STREAM ERROR:`, (chunk as any).error || chunk)
+ break
+
+ default:
+ // Log unknown chunk types for debugging
+ if (!['step-start', 'step-finish', 'finish'].includes(chunk.type)) {
+ log('Agent', C.dim, `chunk: ${chunk.type}`)
+ }
+ break
}
}
- console.log(`[Agent] ${agentType} done`)
+ const elapsed = ((performance.now() - startTime) / 1000).toFixed(1)
+ log('Agent', C.green, `DONE ${C.bold}${agentType}${C.r} | ${elapsed}s | chunks=${chunkCount} text=${textLength}chars tools=${toolCallCount}→${toolResultCount}`)
+
await stream.writeSSE({
data: JSON.stringify({ type: 'done', data: '' }),
})
} catch (err: any) {
- console.error(`[Agent] ${agentType} error:`, err.message)
+ const elapsed = ((performance.now() - startTime) / 1000).toFixed(1)
+ log('Agent', C.red, `ERROR ${C.bold}${agentType}${C.r} | ${elapsed}s`)
+ console.error(err.stack || err)
await stream.writeSSE({
data: JSON.stringify({ type: 'error', data: err.message || 'Agent execution failed' }),
})