|
| 1 | +import type { SessionMessageState } from "@/utils"; |
| 2 | +import { log } from "@/utils"; |
| 3 | + |
| 4 | +const SILICONFLOW_API_URL = "https://api.siliconflow.cn/v1/chat/completions"; |
| 5 | +const SILICONFLOW_MODEL = "Qwen/Qwen2.5-7B-Instruct"; |
| 6 | +const SILICONFLOW_API_KEY = "sk-avkivvbgozinofsnrfuazmsfhiuxlyimewadbfmvghilfkax"; |
| 7 | +const REQUEST_TIMEOUT_MS = 6000; |
| 8 | +const MAX_TITLE_LENGTH = 64; |
| 9 | + |
| 10 | +const inFlight = new Set<string>(); |
| 11 | + |
| 12 | +type SiliconFlowResponse = { |
| 13 | + choices?: Array<{ |
| 14 | + message?: { |
| 15 | + content?: string; |
| 16 | + }; |
| 17 | + }>; |
| 18 | +}; |
| 19 | + |
| 20 | +function normalizeTitle(value: string): string | null { |
| 21 | + const title = value |
| 22 | + .replace(/^[`"'\s]+|[`"'\s]+$/g, "") |
| 23 | + .replace(/\s+/g, " ") |
| 24 | + .trim(); |
| 25 | + if (!title) return null; |
| 26 | + if (title.length <= MAX_TITLE_LENGTH) return title; |
| 27 | + return title.slice(0, MAX_TITLE_LENGTH).trim(); |
| 28 | +} |
| 29 | + |
| 30 | +async function generateTitleFromPrompt(prompt: string): Promise<string | null> { |
| 31 | + const controller = new AbortController(); |
| 32 | + const timeout = setTimeout(() => controller.abort(), REQUEST_TIMEOUT_MS); |
| 33 | + try { |
| 34 | + const response = await fetch(SILICONFLOW_API_URL, { |
| 35 | + method: "POST", |
| 36 | + headers: { |
| 37 | + "Content-Type": "application/json", |
| 38 | + Authorization: `Bearer ${SILICONFLOW_API_KEY}`, |
| 39 | + }, |
| 40 | + body: JSON.stringify({ |
| 41 | + model: SILICONFLOW_MODEL, |
| 42 | + temperature: 0.2, |
| 43 | + max_tokens: 24, |
| 44 | + messages: [ |
| 45 | + { |
| 46 | + role: "system", |
| 47 | + content: [ |
| 48 | + "You generate concise chat session titles.", |
| 49 | + "Rules:", |
| 50 | + "- Return title text only.", |
| 51 | + "- No quotes.", |
| 52 | + "- Keep it under 12 words.", |
| 53 | + ].join("\n"), |
| 54 | + }, |
| 55 | + { |
| 56 | + role: "user", |
| 57 | + content: prompt, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }), |
| 61 | + signal: controller.signal, |
| 62 | + }); |
| 63 | + |
| 64 | + if (!response.ok) { |
| 65 | + log.debug("SiliconFlow title generation failed", { status: response.status }); |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + const data = await response.json() as SiliconFlowResponse; |
| 70 | + const content = data.choices?.[0]?.message?.content; |
| 71 | + if (typeof content !== "string") return null; |
| 72 | + return normalizeTitle(content); |
| 73 | + } catch (error) { |
| 74 | + log.debug("SiliconFlow title generation error", { error: String(error) }); |
| 75 | + return null; |
| 76 | + } finally { |
| 77 | + clearTimeout(timeout); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +function createTitleOnlyState(title: string, startedAt: number): SessionMessageState { |
| 82 | + return { |
| 83 | + sessionTitle: title, |
| 84 | + currentText: "", |
| 85 | + tools: [], |
| 86 | + todos: [], |
| 87 | + startedAt, |
| 88 | + }; |
| 89 | +} |
| 90 | + |
| 91 | +export async function maybeGenerateSessionTitle(params: { |
| 92 | + prompt: string; |
| 93 | + stateKey: string; |
| 94 | + liveParsedState: Map<string, SessionMessageState>; |
| 95 | + startedAt: number; |
| 96 | +}): Promise<void> { |
| 97 | + const { prompt, stateKey, liveParsedState, startedAt } = params; |
| 98 | + if (!prompt.trim()) return; |
| 99 | + if (inFlight.has(stateKey)) return; |
| 100 | + |
| 101 | + const existingState = liveParsedState.get(stateKey); |
| 102 | + if (existingState?.sessionTitle) return; |
| 103 | + |
| 104 | + inFlight.add(stateKey); |
| 105 | + try { |
| 106 | + const title = await generateTitleFromPrompt(prompt); |
| 107 | + if (!title) return; |
| 108 | + |
| 109 | + const nextState = liveParsedState.get(stateKey); |
| 110 | + if (nextState) { |
| 111 | + if (!nextState.sessionTitle) { |
| 112 | + nextState.sessionTitle = title; |
| 113 | + } |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + liveParsedState.set(stateKey, createTitleOnlyState(title, startedAt)); |
| 118 | + } finally { |
| 119 | + inFlight.delete(stateKey); |
| 120 | + } |
| 121 | +} |
0 commit comments