"use client"; import { ApiPath, HUAWEI_BASE_URL, Huawei } from "@/app/constant"; import { useAccessStore, useAppConfig, useChatStore, usePluginStore, ChatMessageTool, } from "@/app/store"; import { ChatOptions, getHeaders, LLMApi, LLMModel, MultimodalContent, SpeechOptions, } from "../api"; import { getClientConfig } from "@/app/config/client"; import { getTimeoutMSByModel } from "@/app/utils"; import { streamWithThink } from "@/app/utils/chat"; import { fetch } from "@/app/utils/stream"; interface RequestPayloadForHuawei { messages: { role: "system" | "user" | "assistant"; content: string | MultimodalContent[]; }[]; stream?: boolean; model: string; temperature: number; presence_penalty: number; frequency_penalty: number; top_p: number; max_tokens?: number; } export class HuaweiApi implements LLMApi { path(path: string): string { const accessStore = useAccessStore.getState(); let baseUrl = ""; if (accessStore.useCustomConfig) { baseUrl = accessStore.huaweiUrl; } if (baseUrl.length === 0) { const isApp = !!getClientConfig()?.isApp; baseUrl = isApp ? HUAWEI_BASE_URL : ApiPath.Huawei; } if (baseUrl.endsWith("/")) { baseUrl = baseUrl.slice(0, baseUrl.length - 1); } if (!baseUrl.startsWith("http") && !baseUrl.startsWith(ApiPath.Huawei)) { baseUrl = "https://" + baseUrl; } console.log("[Proxy Endpoint] ", baseUrl, path); return [baseUrl, path].join("/"); } extractMessage(res: any) { return res.choices?.at(0)?.message?.content ?? ""; } speech(options: SpeechOptions): Promise { throw new Error("Method not implemented."); } async chat(options: ChatOptions) { const messages = options.messages.map((v) => ({ role: v.role, content: v.content, })); const modelConfig = { ...useAppConfig.getState().modelConfig, ...useChatStore.getState().currentSession().mask.modelConfig, ...{ model: options.config.model, }, }; const requestPayload: RequestPayloadForHuawei = { messages, stream: options.config.stream, model: modelConfig.model, temperature: modelConfig.temperature, presence_penalty: modelConfig.presence_penalty, frequency_penalty: modelConfig.frequency_penalty, top_p: modelConfig.top_p, }; const shouldStream = !!options.config.stream; const controller = new AbortController(); options.onController?.(controller); try { const chatPath = this.path(Huawei.ChatPath); const chatPayload = { method: "POST", body: JSON.stringify(requestPayload), signal: controller.signal, headers: getHeaders(), }; const requestTimeoutId = setTimeout( () => controller.abort(), getTimeoutMSByModel(options.config.model), ); if (shouldStream) { const [tools, funcs] = usePluginStore .getState() .getAsTools( useChatStore.getState().currentSession().mask?.plugin || [], ); return streamWithThink( chatPath, requestPayload, getHeaders(), tools as any[], funcs, controller, // parseSSE (text: string, runTools: ChatMessageTool[]) => { const json = JSON.parse(text); const choices = json.choices as Array<{ delta: { content: string; tool_calls: ChatMessageTool[]; }; }>; const tool_calls = choices[0]?.delta?.tool_calls; if (tool_calls?.length > 0) { const index = tool_calls[0]?.index; const id = tool_calls[0]?.id; const args = tool_calls[0]?.function?.arguments; if (id) { runTools.push({ id, type: tool_calls[0]?.type, function: { name: tool_calls[0]?.function?.name as string, arguments: args, }, }); } else { // @ts-ignore runTools[index]["function"]["arguments"] += args; } } return { isThinking: false, content: choices[0]?.delta?.content || "", }; }, // processToolMessage ( payload: RequestPayloadForHuawei, toolCallMessage: any, toolCallResult: any[], ) => { payload?.messages?.splice( payload?.messages?.length, 0, toolCallMessage, ...toolCallResult, ); }, options, ); } else { const res = await fetch(chatPath, chatPayload); clearTimeout(requestTimeoutId); const resJson = await res.json(); const message = this.extractMessage(resJson); options.onFinish(message, res); } } catch (e) { console.log("[Request] failed to make a chat request", e); options.onError?.(e as Error); } } async usage() { return { used: 0, total: 0, }; } async models(): Promise { return []; } }