|
|
@@ -19,6 +19,7 @@ import {
|
|
|
persistBookSession,
|
|
|
appendBookSessionMessage,
|
|
|
runAgentSession,
|
|
|
+ buildAgentSystemPrompt,
|
|
|
resolveServicePreset,
|
|
|
resolveServiceModel,
|
|
|
loadSecrets,
|
|
|
@@ -812,7 +813,13 @@ export function createStudioServer(initialConfig: ProjectConfig, root: string) {
|
|
|
return c.json({ ok: false, error: "连接成功但未返回可用模型" }, 400);
|
|
|
}
|
|
|
|
|
|
- const sampleModel = models[0]?.id;
|
|
|
+ const rawConfig = await loadRawConfig(root).catch(() => ({} as Record<string, unknown>));
|
|
|
+ const preferredModel = typeof (rawConfig.llm as Record<string, unknown> | undefined)?.defaultModel === "string"
|
|
|
+ ? String((rawConfig.llm as Record<string, unknown>).defaultModel)
|
|
|
+ : undefined;
|
|
|
+ const sampleModel = models.find((m) => m.id === preferredModel)?.id
|
|
|
+ ?? models.find((m) => m.id === "gpt-5.4")?.id
|
|
|
+ ?? models[0]?.id;
|
|
|
if (sampleModel) {
|
|
|
const client = createLLMClient({
|
|
|
provider: service === "anthropic" ? "anthropic" : "openai",
|
|
|
@@ -1310,6 +1317,41 @@ export function createStudioServer(initialConfig: ProjectConfig, root: string) {
|
|
|
});
|
|
|
}
|
|
|
if (!result.responseText) {
|
|
|
+ try {
|
|
|
+ const fallbackClient = createLLMClient({
|
|
|
+ ...config.llm,
|
|
|
+ service: configuredEntry?.service ?? reqService ?? config.llm.service,
|
|
|
+ model: reqModel ?? config.llm.model,
|
|
|
+ apiKey: agentApiKey ?? config.llm.apiKey,
|
|
|
+ baseUrl: configuredEntry?.baseUrl ?? config.llm.baseUrl,
|
|
|
+ ...(configuredEntry?.apiFormat ? { apiFormat: configuredEntry.apiFormat } : {}),
|
|
|
+ ...(configuredEntry?.stream !== undefined ? { stream: configuredEntry.stream } : {}),
|
|
|
+ } as ProjectConfig["llm"]);
|
|
|
+ const fallback = await chatCompletion(
|
|
|
+ fallbackClient,
|
|
|
+ reqModel ?? config.llm.model,
|
|
|
+ [
|
|
|
+ { role: "system", content: buildAgentSystemPrompt(activeBookId ?? null, config.language ?? "zh") },
|
|
|
+ { role: "user", content: instruction },
|
|
|
+ ],
|
|
|
+ { maxTokens: 256 },
|
|
|
+ );
|
|
|
+ if (fallback.content?.trim()) {
|
|
|
+ bookSession = appendBookSessionMessage(bookSession, {
|
|
|
+ role: "assistant",
|
|
|
+ content: fallback.content,
|
|
|
+ timestamp: Date.now() + 1,
|
|
|
+ });
|
|
|
+ await persistBookSession(root, bookSession);
|
|
|
+ return c.json({
|
|
|
+ response: fallback.content,
|
|
|
+ session: { sessionId: bookSession.sessionId },
|
|
|
+ });
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ // fall through to probe-based diagnosis below
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
const probeClient = createLLMClient({
|
|
|
...config.llm,
|