fix: call Qwen API directly for streaming, not through Next.js route

- Fixed generateAIAssistStream to call Qwen API directly
- Uses getEffectiveEndpoint() and getRequestHeaders() for proper auth
- Added better error logging for debugging
- Fixed listModels to use getAvailableModels()
- This fixes the 'Stream request failed' error
This commit is contained in:
Gemini AI
2025-12-29 01:00:18 +04:00
Unverified
parent f9cbfb0184
commit b21ef566b7

View File

@@ -1016,7 +1016,6 @@ Perform analysis based on provided instructions.`,
model?: string model?: string
): Promise<APIResponse<void>> { ): Promise<APIResponse<void>> {
try { try {
// ... existing prompt logic ...
const systemPrompt = `You are "AI Assist". const systemPrompt = `You are "AI Assist".
Your goal is to provide intelligent support with a "Canvas" experience. Your goal is to provide intelligent support with a "Canvas" experience.
@@ -1040,21 +1039,21 @@ Perform analysis based on provided instructions.`,
})) }))
]; ];
const endpoint = "/tools/promptarch/api/qwen/chat"; // Get authorization headers
const headers: Record<string, string> = { const headers = await this.getRequestHeaders();
"Content-Type": "application/json", const endpoint = this.getEffectiveEndpoint();
}; const url = endpoint.endsWith("/chat/completions")
? endpoint
: `${endpoint}/chat/completions`;
const tokenInfo = this.getTokenInfo(); console.log("[QwenOAuth] Stream request:", { url, model: model || this.getAvailableModels()[0], hasAuth: !!headers.Authorization });
if (tokenInfo?.accessToken) {
headers["Authorization"] = `Bearer ${tokenInfo.accessToken}`;
} else if (this.apiKey) {
headers["Authorization"] = `Bearer ${this.apiKey}`;
}
const response = await fetch(endpoint, { const response = await fetch(url, {
method: "POST", method: "POST",
headers, headers: {
"Content-Type": "application/json",
...headers,
},
signal: options.signal, signal: options.signal,
body: JSON.stringify({ body: JSON.stringify({
model: model || this.getAvailableModels()[0], model: model || this.getAvailableModels()[0],
@@ -1064,11 +1063,13 @@ Perform analysis based on provided instructions.`,
}); });
if (!response.ok) { if (!response.ok) {
throw new Error("Stream request failed"); const errorText = await response.text();
console.error("[QwenOAuth] Stream request failed:", response.status, errorText);
throw new Error(`Stream request failed (${response.status}): ${errorText.slice(0, 200)}`);
} }
const reader = response.body?.getReader(); const reader = response.body?.getReader();
if (!reader) throw new Error("No reader"); if (!reader) throw new Error("No reader available");
const decoder = new TextDecoder(); const decoder = new TextDecoder();
let buffer = ""; let buffer = "";
@@ -1103,15 +1104,13 @@ Perform analysis based on provided instructions.`,
return { success: true, data: undefined }; return { success: true, data: undefined };
} catch (error) { } catch (error) {
console.error("[QwenOAuth] Stream error:", error);
return { success: false, error: error instanceof Error ? error.message : "Stream failed" }; return { success: false, error: error instanceof Error ? error.message : "Stream failed" };
} }
} }
async listModels(): Promise<APIResponse<string[]>> { async listModels(): Promise<APIResponse<string[]>> {
const models = [ return { success: true, data: this.getAvailableModels() };
"coder-model",
];
return { success: true, data: models };
} }
getAvailableModels(): string[] { getAvailableModels(): string[] {