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: add colored console logs for all API calls and agent events

Connor4bd11c0ff4c4
diff --git a/frontend/app/composables/useApi.ts b/frontend/app/composables/useApi.ts
index 86c6794..a52ba55 100644
--- a/frontend/app/composables/useApi.ts
+++ b/frontend/app/composables/useApi.ts
@@ -3,10 +3,29 @@ const BASE = '/api/v1'
async function req<T = any>(method: string, path: string, body?: any): Promise<T> {
const opts: RequestInit = { method, headers: { 'Content-Type': 'application/json' } }
if (body) opts.body = JSON.stringify(body)
- const resp = await fetch(`${BASE}${path}`, opts)
- const json = await resp.json()
- if (!resp.ok || (json.code && json.code >= 400)) throw new Error(json.message || `${resp.status}`)
- return json.data ?? json
+
+ const start = performance.now()
+ console.log(`%c[API] %c${method} %c${path}`, 'color:#888', 'color:#4fc3f7;font-weight:bold', 'color:#ccc', body || '')
+
+ try {
+ const resp = await fetch(`${BASE}${path}`, opts)
+ const json = await resp.json()
+ const ms = Math.round(performance.now() - start)
+
+ if (!resp.ok || (json.code && json.code >= 400)) {
+ console.log(`%c[API] %c${method} ${path} %c${resp.status} %c${ms}ms`, 'color:#888', 'color:#ef5350', 'color:#ef5350;font-weight:bold', 'color:#888', json.message || '')
+ throw new Error(json.message || `${resp.status}`)
+ }
+
+ console.log(`%c[API] %c${method} ${path} %c${resp.status} %c${ms}ms`, 'color:#888', 'color:#66bb6a', 'color:#66bb6a;font-weight:bold', 'color:#888')
+ return json.data ?? json
+ } catch (err: any) {
+ if (!err.message?.match(/^\d{3}$/)) {
+ const ms = Math.round(performance.now() - start)
+ console.log(`%c[API] %c${method} ${path} %cERROR %c${ms}ms`, 'color:#888', 'color:#ef5350', 'color:#ef5350;font-weight:bold', 'color:#888', err.message)
+ }
+ throw err
+ }
}
export const api = {
@@ -58,11 +77,17 @@ export const aiConfigAPI = {
}
export async function* streamAgent(type: string, msg: string, dramaId: number, episodeId: number) {
+ console.log(`%c[Agent] %cSTART %c${type}`, 'color:#888', 'color:#ce93d8;font-weight:bold', 'color:#ccc', `drama=${dramaId} episode=${episodeId}`)
+
const r = await fetch(`${BASE}/agent/${type}/chat`, {
method: 'POST', headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ message: msg, drama_id: dramaId, episode_id: episodeId }),
})
- if (!r.ok || !r.body) throw new Error('Agent failed')
+ if (!r.ok || !r.body) {
+ console.log(`%c[Agent] %cFAILED %c${type} %c${r.status}`, 'color:#888', 'color:#ef5350', 'color:#ccc', 'color:#ef5350')
+ throw new Error('Agent failed')
+ }
+
const reader = r.body.getReader(), dec = new TextDecoder()
let buf = ''
while (true) {
@@ -71,7 +96,16 @@ export async function* streamAgent(type: string, msg: string, dramaId: number, e
buf += dec.decode(value, { stream: true })
const lines = buf.split('\n'); buf = lines.pop() || ''
for (const l of lines) {
- if (l.startsWith('data: ')) try { yield JSON.parse(l.slice(6)) } catch {}
+ if (l.startsWith('data: ')) {
+ try {
+ const event = JSON.parse(l.slice(6))
+ if (event.type === 'tool_call') console.log(`%c[Agent] %cCALL %c${event.tool_name}`, 'color:#888', 'color:#ffb74d', 'color:#fff', event.data ? JSON.parse(event.data) : '')
+ else if (event.type === 'tool_result') console.log(`%c[Agent] %cRESULT %c${event.tool_name}`, 'color:#888', 'color:#66bb6a', 'color:#ccc', event.data?.slice?.(0, 100) || '')
+ else if (event.type === 'done') console.log(`%c[Agent] %cDONE %c${type}`, 'color:#888', 'color:#66bb6a;font-weight:bold', 'color:#ccc')
+ else if (event.type === 'error') console.log(`%c[Agent] %cERROR %c${type}`, 'color:#888', 'color:#ef5350;font-weight:bold', 'color:#ccc', event.data)
+ yield event
+ } catch {}
+ }
}
}
}