(messages: any[], refreshToken: string, model = MODEL_NAME, refConvId = "", retryCount = 0, tools?: any[])
| 409 | } |
| 410 | |
| 411 | export async function createCompletionStream(messages: any[], refreshToken: string, model = MODEL_NAME, refConvId = "", retryCount = 0, tools?: any[]): Promise<ReadableStream> { |
| 412 | return (async () => { |
| 413 | let processedMessages = convertToolMessages(messages); |
| 414 | processedMessages = injectToolsPrompt(processedMessages, tools || []); |
| 415 | const refFileUrls = extractRefFileUrls(processedMessages); |
| 416 | const refs = refFileUrls.length ? await Promise.all(refFileUrls.map((fileUrl) => uploadFile(fileUrl, refreshToken))) : []; |
| 417 | if (!/[0-9a-zA-Z]{24}/.test(refConvId)) refConvId = ""; |
| 418 | let assistantId = /^[a-z0-9]{24,}$/.test(model) ? model : DEFAULT_ASSISTANT_ID; |
| 419 | let chatMode = ''; |
| 420 | if (model.includes('think') || model.includes('zero')) { chatMode = 'zero'; } |
| 421 | if (model.includes('deepresearch')) { chatMode = 'deep_research'; } |
| 422 | const token = await acquireToken(refreshToken); |
| 423 | const sign = await generateSign(); |
| 424 | const response = await glmPostStream( |
| 425 | "https://chatglm.cn/chatglm/backend-api/assistant/stream", |
| 426 | { |
| 427 | assistant_id: assistantId, |
| 428 | conversation_id: refConvId, |
| 429 | project_id: "", |
| 430 | chat_type: "user_chat", |
| 431 | messages: messagesPrepare(processedMessages, refs, !!refConvId), |
| 432 | meta_data: { |
| 433 | channel: "", |
| 434 | chat_mode: chatMode || undefined, |
| 435 | draft_id: "", |
| 436 | if_plus_model: true, |
| 437 | input_question_type: "xxxx", |
| 438 | is_networking: true, |
| 439 | is_test: false, |
| 440 | platform: "pc", |
| 441 | quote_log_id: "", |
| 442 | cogview: { rm_label_watermark: false } |
| 443 | }, |
| 444 | }, |
| 445 | { |
| 446 | Authorization: `Bearer ${token}`, |
| 447 | Referer: assistantId == DEFAULT_ASSISTANT_ID ? "https://chatglm.cn/main/alltoolsdetail" : `https://chatglm.cn/main/gdetail/${assistantId}`, |
| 448 | "X-Device-Id": uuid(false), |
| 449 | "X-Request-Id": uuid(false), |
| 450 | "X-Sign": sign.sign, |
| 451 | "X-Timestamp": sign.timestamp, |
| 452 | "X-Nonce": sign.nonce, |
| 453 | ...getHeaders(), |
| 454 | } |
| 455 | ); |
| 456 | const contentType = response.headers.get("content-type") || ""; |
| 457 | if (!contentType.includes("text/event-stream")) { |
| 458 | const errText = await response.text(); |
| 459 | console.error("Invalid response Content-Type:", contentType, errText); |
| 460 | const encoder = new TextEncoder(); |
| 461 | return new ReadableStream({ |
| 462 | start(controller) { |
| 463 | controller.enqueue(encoder.encode(`data: ${JSON.stringify({ |
| 464 | id: "", model: MODEL_NAME, object: "chat.completion.chunk", |
| 465 | choices: [{ index: 0, delta: { role: "assistant", content: "服务暂时不可用,第三方响应错误" }, finish_reason: "stop" }], |
| 466 | usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, |
| 467 | created: unixTimestamp(), |
| 468 | })}\n\n`)); |
no test coverage detected