@astack-tech/integrations 0.1.1-beta.2 → 0.1.1-beta.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/index.cjs +97 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +97 -2
- package/dist/index.js.map +1 -1
- package/dist/model-provider/index.cjs +97 -2
- package/dist/model-provider/index.cjs.map +1 -1
- package/dist/model-provider/index.d.cts +14 -0
- package/dist/model-provider/index.d.ts +14 -0
- package/dist/model-provider/index.js +97 -2
- package/dist/model-provider/index.js.map +1 -1
- package/package.json +1 -1
- package/src/model-provider/deepseek/index.ts +141 -4
package/LICENSE
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -48,7 +48,8 @@ var Deepseek = class extends import_core.Component {
|
|
|
48
48
|
super({});
|
|
49
49
|
this.client = new import_openai.default({
|
|
50
50
|
apiKey: config.apiKey,
|
|
51
|
-
baseURL: config.baseURL || "https://api.deepseek.com/v1"
|
|
51
|
+
baseURL: config.baseURL || "https://api.deepseek.com/v1",
|
|
52
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false
|
|
52
53
|
});
|
|
53
54
|
this.model = config.model || "deepseek-chat";
|
|
54
55
|
this.temperature = config.temperature ?? 0.7;
|
|
@@ -105,6 +106,87 @@ var Deepseek = class extends import_core.Component {
|
|
|
105
106
|
}
|
|
106
107
|
return response.choices[0].message.content || "";
|
|
107
108
|
}
|
|
109
|
+
/**
|
|
110
|
+
* 流式处理对话消息
|
|
111
|
+
* @param messages 对话消息数组
|
|
112
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
113
|
+
* @returns 生成的流式响应消息异步生成器
|
|
114
|
+
*/
|
|
115
|
+
async *streamChatCompletion(messages, options) {
|
|
116
|
+
const formattedMessages = [];
|
|
117
|
+
if (this.systemPrompt) {
|
|
118
|
+
formattedMessages.push({
|
|
119
|
+
role: "system",
|
|
120
|
+
content: this.systemPrompt
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
formattedMessages.push(
|
|
124
|
+
...messages.map((msg) => {
|
|
125
|
+
const formattedMsg = {
|
|
126
|
+
role: msg.role,
|
|
127
|
+
content: msg.content
|
|
128
|
+
};
|
|
129
|
+
if (msg.role === "tool" && msg.tool_call_id) {
|
|
130
|
+
formattedMsg.tool_call_id = msg.tool_call_id;
|
|
131
|
+
}
|
|
132
|
+
return formattedMsg;
|
|
133
|
+
})
|
|
134
|
+
);
|
|
135
|
+
const requestParams = {
|
|
136
|
+
model: this.model,
|
|
137
|
+
messages: formattedMessages,
|
|
138
|
+
temperature: this.temperature,
|
|
139
|
+
max_tokens: this.maxTokens,
|
|
140
|
+
top_p: this.topP,
|
|
141
|
+
stream: true
|
|
142
|
+
// 开启流式模式
|
|
143
|
+
};
|
|
144
|
+
const toolsToUse = options?.temporaryTools || this.tools;
|
|
145
|
+
if (toolsToUse && toolsToUse.length > 0) {
|
|
146
|
+
const formattedTools = toolsToUse.map((tool) => {
|
|
147
|
+
const typedTool = tool;
|
|
148
|
+
if (typedTool.type === "function") {
|
|
149
|
+
return tool;
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
type: "function",
|
|
153
|
+
function: {
|
|
154
|
+
name: typedTool.name,
|
|
155
|
+
description: typedTool.description,
|
|
156
|
+
parameters: typedTool.parameters || {}
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
requestParams.tools = formattedTools;
|
|
161
|
+
}
|
|
162
|
+
const stream = await this.client.chat.completions.create(requestParams);
|
|
163
|
+
for await (const chunk of stream) {
|
|
164
|
+
const delta = chunk.choices[0]?.delta;
|
|
165
|
+
if (delta?.content) {
|
|
166
|
+
yield {
|
|
167
|
+
role: "assistant",
|
|
168
|
+
content: delta.content
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
if (delta?.tool_calls) {
|
|
172
|
+
yield {
|
|
173
|
+
role: "assistant",
|
|
174
|
+
content: "",
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
+
tool_calls: delta.tool_calls.map((toolCall) => ({
|
|
177
|
+
id: toolCall.id || "",
|
|
178
|
+
function: {
|
|
179
|
+
name: toolCall.function?.name || "",
|
|
180
|
+
arguments: toolCall.function?.arguments || ""
|
|
181
|
+
},
|
|
182
|
+
type: "function",
|
|
183
|
+
tool_name: toolCall.function?.name || "",
|
|
184
|
+
arguments: toolCall.function?.arguments || ""
|
|
185
|
+
}))
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
108
190
|
/**
|
|
109
191
|
* 处理对话消息
|
|
110
192
|
* @param messages 对话消息数组
|
|
@@ -164,7 +246,20 @@ var Deepseek = class extends import_core.Component {
|
|
|
164
246
|
};
|
|
165
247
|
if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {
|
|
166
248
|
result.tool_calls = responseMessage.tool_calls.map((toolCall) => {
|
|
167
|
-
|
|
249
|
+
let args;
|
|
250
|
+
const rawArgs = toolCall.function.arguments;
|
|
251
|
+
if (typeof rawArgs === "string") {
|
|
252
|
+
const raw = rawArgs;
|
|
253
|
+
try {
|
|
254
|
+
args = raw && raw.trim().length > 0 ? JSON.parse(raw) : {};
|
|
255
|
+
} catch {
|
|
256
|
+
args = {};
|
|
257
|
+
}
|
|
258
|
+
} else if (rawArgs && typeof rawArgs === "object") {
|
|
259
|
+
args = rawArgs;
|
|
260
|
+
} else {
|
|
261
|
+
args = {};
|
|
262
|
+
}
|
|
168
263
|
return {
|
|
169
264
|
id: toolCall.id,
|
|
170
265
|
// 满足 Agent 组件期望的格式
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/model-provider/index.ts","../src/model-provider/deepseek/index.ts"],"sourcesContent":["/**\n * Astack 集成包\n *\n * 提供与各种外部服务和平台的集成组件\n */\n\n// 导出模型提供者组件\nexport * as ModelProvider from './model-provider';\n","export {\n default as Deepseek,\n type Message as DeepseekMessage,\n type ToolCall as DeepseekToolCall,\n type DeepseekConfig,\n type MessageRole as DeepseekMessageRole,\n} from './deepseek';\n","import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n const args =\n typeof toolCall.function.arguments === 'string'\n ? JSON.parse(toolCall.function.arguments || '{}')\n : toolCall.function.arguments || {};\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出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AAW,IAAS,IAAS;AAE3B,OAAG,QAAQ,EAAE,QAAQ,OAAO,WAAmB;AAC7C,UAAI;AACF,cAAM,aAAa,MAAM,KAAK,mBAAmB,MAAM;AACvD,WAAG,YAAY,EAAE,KAAK,UAAU;AAAA,MAClC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,YAAY,EAAE,KAAK,8BAAU;AAAA,MAClC;AAAA,IACF,CAAC;AAGD,OAAG,UAAU,EAAE,QAAQ,OAAO,aAAwB;AACpD,UAAI;AACF,cAAM,kBAAkB,MAAM,KAAK,eAAe,QAAQ;AAC1D,WAAG,SAAS,EAAE,KAAK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":["OpenAI"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/model-provider/index.ts","../src/model-provider/deepseek/index.ts"],"sourcesContent":["/**\n * Astack 集成包\n *\n * 提供与各种外部服务和平台的集成组件\n */\n\n// 导出模型提供者组件\nexport * as ModelProvider from './model-provider';\n","export {\n default as Deepseek,\n type Message as DeepseekMessage,\n type ToolCall as DeepseekToolCall,\n type DeepseekConfig,\n type MessageRole as DeepseekMessageRole,\n} from './deepseek';\n","import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n\n /**\n * dangerouslyAllowBrowser\n * 是否允许在浏览器等非安全环境内运行\n */\n dangerouslyAllowBrowser?: boolean;\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false,\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 流式处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的流式响应消息异步生成器\n */\n async *streamChatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): AsyncGenerator<Partial<Message>> {\n // 转换消息格式 (复用现有逻辑)\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n stream: true, // 开启流式模式\n };\n\n // 决定使用哪些工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n const formattedTools = toolsToUse.map(tool => {\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n if (typedTool.type === 'function') {\n return tool;\n }\n\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用流式 API\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const stream = (await this.client.chat.completions.create(requestParams)) as any;\n\n // 处理流式响应\n for await (const chunk of stream) {\n const delta = chunk.choices[0]?.delta;\n\n if (delta?.content) {\n // 流式文本内容\n yield {\n role: 'assistant',\n content: delta.content,\n };\n }\n\n if (delta?.tool_calls) {\n // 流式工具调用(如果需要)\n yield {\n role: 'assistant',\n content: '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n tool_calls: delta.tool_calls.map((toolCall: any) => ({\n id: toolCall.id || '',\n function: {\n name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n },\n type: 'function',\n tool_name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n })),\n };\n }\n }\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n let args: Record<string, unknown>;\n\n const rawArgs = toolCall.function.arguments;\n\n if (typeof rawArgs === 'string') {\n const raw = rawArgs;\n try {\n args = raw && raw.trim().length > 0 ? (JSON.parse(raw) as Record<string, unknown>) : {};\n } catch {\n args = {};\n }\n } else if (rawArgs && typeof rawArgs === 'object') {\n args = rawArgs as Record<string, unknown>;\n } else {\n args = {};\n }\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0B;AAC1B,oBAAmB;AAiInB,IAAM,WAAN,cAAuB,sBAAU;AAAA,EAY/B,YAAY,QAAwB;AAClC,UAAM,CAAC,CAAC;AAGR,SAAK,SAAS,IAAI,cAAAA,QAAO;AAAA,MACvB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO,WAAW;AAAA,MAC3B,yBAAyB,OAAO,2BAA2B;AAAA,IAC7D,CAAC;AAGD,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,cAAc,OAAO,eAAe;AACzC,SAAK,YAAY,OAAO;AACxB,SAAK,OAAO,OAAO;AACnB,SAAK,eAAe,OAAO;AAG3B,QAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAK,QAAQ,OAAO,SAAS,IAAI,WAAS;AAAA,QACxC,MAAM;AAAA,QACN,UAAU;AAAA,UACR,MAAM,KAAK;AAAA,UACX,aAAa,KAAK;AAAA,UAClB,YAAY,KAAK,cAAc,CAAC;AAAA,QAClC;AAAA,MACF,EAAE;AAAA,IACJ,OAAO;AAEL,WAAK,QAAQ,OAAO;AAAA,IACtB;AAGA,0BAAU,KAAK,EAAE,QAAQ,EAAE,OAAO,IAAI;AACtC,0BAAU,KAAK,EAAE,UAAU,EAAE,OAAO,IAAI;AACxC,0BAAU,KAAK,EAAE,YAAY,EAAE,OAAO,IAAI;AAC1C,0BAAU,KAAK,EAAE,SAAS,EAAE,OAAO,IAAI;AAAA,EACzC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,mBAAmB,QAAiC;AACxD,UAAM,WAA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;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":["OpenAI"]}
|
package/dist/index.js
CHANGED
|
@@ -18,7 +18,8 @@ var Deepseek = class extends Component {
|
|
|
18
18
|
super({});
|
|
19
19
|
this.client = new OpenAI({
|
|
20
20
|
apiKey: config.apiKey,
|
|
21
|
-
baseURL: config.baseURL || "https://api.deepseek.com/v1"
|
|
21
|
+
baseURL: config.baseURL || "https://api.deepseek.com/v1",
|
|
22
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false
|
|
22
23
|
});
|
|
23
24
|
this.model = config.model || "deepseek-chat";
|
|
24
25
|
this.temperature = config.temperature ?? 0.7;
|
|
@@ -75,6 +76,87 @@ var Deepseek = class extends Component {
|
|
|
75
76
|
}
|
|
76
77
|
return response.choices[0].message.content || "";
|
|
77
78
|
}
|
|
79
|
+
/**
|
|
80
|
+
* 流式处理对话消息
|
|
81
|
+
* @param messages 对话消息数组
|
|
82
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
83
|
+
* @returns 生成的流式响应消息异步生成器
|
|
84
|
+
*/
|
|
85
|
+
async *streamChatCompletion(messages, options) {
|
|
86
|
+
const formattedMessages = [];
|
|
87
|
+
if (this.systemPrompt) {
|
|
88
|
+
formattedMessages.push({
|
|
89
|
+
role: "system",
|
|
90
|
+
content: this.systemPrompt
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
formattedMessages.push(
|
|
94
|
+
...messages.map((msg) => {
|
|
95
|
+
const formattedMsg = {
|
|
96
|
+
role: msg.role,
|
|
97
|
+
content: msg.content
|
|
98
|
+
};
|
|
99
|
+
if (msg.role === "tool" && msg.tool_call_id) {
|
|
100
|
+
formattedMsg.tool_call_id = msg.tool_call_id;
|
|
101
|
+
}
|
|
102
|
+
return formattedMsg;
|
|
103
|
+
})
|
|
104
|
+
);
|
|
105
|
+
const requestParams = {
|
|
106
|
+
model: this.model,
|
|
107
|
+
messages: formattedMessages,
|
|
108
|
+
temperature: this.temperature,
|
|
109
|
+
max_tokens: this.maxTokens,
|
|
110
|
+
top_p: this.topP,
|
|
111
|
+
stream: true
|
|
112
|
+
// 开启流式模式
|
|
113
|
+
};
|
|
114
|
+
const toolsToUse = options?.temporaryTools || this.tools;
|
|
115
|
+
if (toolsToUse && toolsToUse.length > 0) {
|
|
116
|
+
const formattedTools = toolsToUse.map((tool) => {
|
|
117
|
+
const typedTool = tool;
|
|
118
|
+
if (typedTool.type === "function") {
|
|
119
|
+
return tool;
|
|
120
|
+
}
|
|
121
|
+
return {
|
|
122
|
+
type: "function",
|
|
123
|
+
function: {
|
|
124
|
+
name: typedTool.name,
|
|
125
|
+
description: typedTool.description,
|
|
126
|
+
parameters: typedTool.parameters || {}
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
requestParams.tools = formattedTools;
|
|
131
|
+
}
|
|
132
|
+
const stream = await this.client.chat.completions.create(requestParams);
|
|
133
|
+
for await (const chunk of stream) {
|
|
134
|
+
const delta = chunk.choices[0]?.delta;
|
|
135
|
+
if (delta?.content) {
|
|
136
|
+
yield {
|
|
137
|
+
role: "assistant",
|
|
138
|
+
content: delta.content
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
if (delta?.tool_calls) {
|
|
142
|
+
yield {
|
|
143
|
+
role: "assistant",
|
|
144
|
+
content: "",
|
|
145
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
146
|
+
tool_calls: delta.tool_calls.map((toolCall) => ({
|
|
147
|
+
id: toolCall.id || "",
|
|
148
|
+
function: {
|
|
149
|
+
name: toolCall.function?.name || "",
|
|
150
|
+
arguments: toolCall.function?.arguments || ""
|
|
151
|
+
},
|
|
152
|
+
type: "function",
|
|
153
|
+
tool_name: toolCall.function?.name || "",
|
|
154
|
+
arguments: toolCall.function?.arguments || ""
|
|
155
|
+
}))
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
78
160
|
/**
|
|
79
161
|
* 处理对话消息
|
|
80
162
|
* @param messages 对话消息数组
|
|
@@ -134,7 +216,20 @@ var Deepseek = class extends Component {
|
|
|
134
216
|
};
|
|
135
217
|
if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {
|
|
136
218
|
result.tool_calls = responseMessage.tool_calls.map((toolCall) => {
|
|
137
|
-
|
|
219
|
+
let args;
|
|
220
|
+
const rawArgs = toolCall.function.arguments;
|
|
221
|
+
if (typeof rawArgs === "string") {
|
|
222
|
+
const raw = rawArgs;
|
|
223
|
+
try {
|
|
224
|
+
args = raw && raw.trim().length > 0 ? JSON.parse(raw) : {};
|
|
225
|
+
} catch {
|
|
226
|
+
args = {};
|
|
227
|
+
}
|
|
228
|
+
} else if (rawArgs && typeof rawArgs === "object") {
|
|
229
|
+
args = rawArgs;
|
|
230
|
+
} else {
|
|
231
|
+
args = {};
|
|
232
|
+
}
|
|
138
233
|
return {
|
|
139
234
|
id: toolCall.id,
|
|
140
235
|
// 满足 Agent 组件期望的格式
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/model-provider/index.ts","../src/model-provider/deepseek/index.ts"],"sourcesContent":["export {\n default as Deepseek,\n type Message as DeepseekMessage,\n type ToolCall as DeepseekToolCall,\n type DeepseekConfig,\n type MessageRole as DeepseekMessageRole,\n} from './deepseek';\n","import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n const args =\n typeof toolCall.function.arguments === 'string'\n ? JSON.parse(toolCall.function.arguments || '{}')\n : toolCall.function.arguments || {};\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";;;;;;;AAAA;AAAA;AAAA;AAAA;;;ACAA,SAAS,iBAAiB;AAC1B,OAAO,YAAY;AA2HnB,IAAM,WAAN,cAAuB,UAAU;AAAA,EAY/B,YAAY,QAAwB;AAClC,UAAM,CAAC,CAAC;AAGR,SAAK,SAAS,IAAI,OAAO;AAAA,MACvB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO,WAAW;AAAA,IAC7B,CAAC;AAGD,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,cAAc,OAAO,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AK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/model-provider/index.ts","../src/model-provider/deepseek/index.ts"],"sourcesContent":["export {\n default as Deepseek,\n type Message as DeepseekMessage,\n type ToolCall as DeepseekToolCall,\n type DeepseekConfig,\n type MessageRole as DeepseekMessageRole,\n} from './deepseek';\n","import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n\n /**\n * dangerouslyAllowBrowser\n * 是否允许在浏览器等非安全环境内运行\n */\n dangerouslyAllowBrowser?: boolean;\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false,\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 流式处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的流式响应消息异步生成器\n */\n async *streamChatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): AsyncGenerator<Partial<Message>> {\n // 转换消息格式 (复用现有逻辑)\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n stream: true, // 开启流式模式\n };\n\n // 决定使用哪些工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n const formattedTools = toolsToUse.map(tool => {\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n if (typedTool.type === 'function') {\n return tool;\n }\n\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用流式 API\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const stream = (await this.client.chat.completions.create(requestParams)) as any;\n\n // 处理流式响应\n for await (const chunk of stream) {\n const delta = chunk.choices[0]?.delta;\n\n if (delta?.content) {\n // 流式文本内容\n yield {\n role: 'assistant',\n content: delta.content,\n };\n }\n\n if (delta?.tool_calls) {\n // 流式工具调用(如果需要)\n yield {\n role: 'assistant',\n content: '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n tool_calls: delta.tool_calls.map((toolCall: any) => ({\n id: toolCall.id || '',\n function: {\n name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n },\n type: 'function',\n tool_name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n })),\n };\n }\n }\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n let args: Record<string, unknown>;\n\n const rawArgs = toolCall.function.arguments;\n\n if (typeof rawArgs === 'string') {\n const raw = rawArgs;\n try {\n args = raw && raw.trim().length > 0 ? (JSON.parse(raw) as Record<string, unknown>) : {};\n } catch {\n args = {};\n }\n } else if (rawArgs && typeof rawArgs === 'object') {\n args = rawArgs as Record<string, unknown>;\n } else {\n args = {};\n }\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";;;;;;;AAAA;AAAA;AAAA;AAAA;;;ACAA,SAAS,iBAAiB;AAC1B,OAAO,YAAY;AAiInB,IAAM,WAAN,cAAuB,UAAU;AAAA,EAY/B,YAAY,QAAwB;AAClC,UAAM,CAAC,CAAC;AAGR,SAAK,SAAS,IAAI,OAAO;AAAA,MACvB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO,WAAW;AAAA,MAC3B,yBAAyB,OAAO,2BAA2B;AAAA,IAC7D,CAAC;AAGD,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,cAAc,OAAO,eAAe;AAC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AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,OAAsD;AAC9D,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAO,KAAK,mBAAmB,KAAK;AAAA,IACtC,OAAO;AACL,aAAO,KAAK,eAAe,KAAK;AAAA,IAClC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,WAAW,IAAS,IAAS;AAE3B,OAAG,QAAQ,EAAE,QAAQ,OAAO,WAAmB;AAC7C,UAAI;AACF,cAAM,aAAa,MAAM,KAAK,mBAAmB,MAAM;AACvD,WAAG,YAAY,EAAE,KAAK,UAAU;AAAA,MAClC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,YAAY,EAAE,KAAK,8BAAU;AAAA,MAClC;AAAA,IACF,CAAC;AAGD,OAAG,UAAU,EAAE,QAAQ,OAAO,aAAwB;AACpD,UAAI;AACF,cAAM,kBAAkB,MAAM,KAAK,eAAe,QAAQ;AAC1D,WAAG,SAAS,EAAE,KAAK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":[]}
|
|
@@ -42,7 +42,8 @@ var Deepseek = class extends import_core.Component {
|
|
|
42
42
|
super({});
|
|
43
43
|
this.client = new import_openai.default({
|
|
44
44
|
apiKey: config.apiKey,
|
|
45
|
-
baseURL: config.baseURL || "https://api.deepseek.com/v1"
|
|
45
|
+
baseURL: config.baseURL || "https://api.deepseek.com/v1",
|
|
46
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false
|
|
46
47
|
});
|
|
47
48
|
this.model = config.model || "deepseek-chat";
|
|
48
49
|
this.temperature = config.temperature ?? 0.7;
|
|
@@ -99,6 +100,87 @@ var Deepseek = class extends import_core.Component {
|
|
|
99
100
|
}
|
|
100
101
|
return response.choices[0].message.content || "";
|
|
101
102
|
}
|
|
103
|
+
/**
|
|
104
|
+
* 流式处理对话消息
|
|
105
|
+
* @param messages 对话消息数组
|
|
106
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
107
|
+
* @returns 生成的流式响应消息异步生成器
|
|
108
|
+
*/
|
|
109
|
+
async *streamChatCompletion(messages, options) {
|
|
110
|
+
const formattedMessages = [];
|
|
111
|
+
if (this.systemPrompt) {
|
|
112
|
+
formattedMessages.push({
|
|
113
|
+
role: "system",
|
|
114
|
+
content: this.systemPrompt
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
formattedMessages.push(
|
|
118
|
+
...messages.map((msg) => {
|
|
119
|
+
const formattedMsg = {
|
|
120
|
+
role: msg.role,
|
|
121
|
+
content: msg.content
|
|
122
|
+
};
|
|
123
|
+
if (msg.role === "tool" && msg.tool_call_id) {
|
|
124
|
+
formattedMsg.tool_call_id = msg.tool_call_id;
|
|
125
|
+
}
|
|
126
|
+
return formattedMsg;
|
|
127
|
+
})
|
|
128
|
+
);
|
|
129
|
+
const requestParams = {
|
|
130
|
+
model: this.model,
|
|
131
|
+
messages: formattedMessages,
|
|
132
|
+
temperature: this.temperature,
|
|
133
|
+
max_tokens: this.maxTokens,
|
|
134
|
+
top_p: this.topP,
|
|
135
|
+
stream: true
|
|
136
|
+
// 开启流式模式
|
|
137
|
+
};
|
|
138
|
+
const toolsToUse = options?.temporaryTools || this.tools;
|
|
139
|
+
if (toolsToUse && toolsToUse.length > 0) {
|
|
140
|
+
const formattedTools = toolsToUse.map((tool) => {
|
|
141
|
+
const typedTool = tool;
|
|
142
|
+
if (typedTool.type === "function") {
|
|
143
|
+
return tool;
|
|
144
|
+
}
|
|
145
|
+
return {
|
|
146
|
+
type: "function",
|
|
147
|
+
function: {
|
|
148
|
+
name: typedTool.name,
|
|
149
|
+
description: typedTool.description,
|
|
150
|
+
parameters: typedTool.parameters || {}
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
requestParams.tools = formattedTools;
|
|
155
|
+
}
|
|
156
|
+
const stream = await this.client.chat.completions.create(requestParams);
|
|
157
|
+
for await (const chunk of stream) {
|
|
158
|
+
const delta = chunk.choices[0]?.delta;
|
|
159
|
+
if (delta?.content) {
|
|
160
|
+
yield {
|
|
161
|
+
role: "assistant",
|
|
162
|
+
content: delta.content
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
if (delta?.tool_calls) {
|
|
166
|
+
yield {
|
|
167
|
+
role: "assistant",
|
|
168
|
+
content: "",
|
|
169
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
170
|
+
tool_calls: delta.tool_calls.map((toolCall) => ({
|
|
171
|
+
id: toolCall.id || "",
|
|
172
|
+
function: {
|
|
173
|
+
name: toolCall.function?.name || "",
|
|
174
|
+
arguments: toolCall.function?.arguments || ""
|
|
175
|
+
},
|
|
176
|
+
type: "function",
|
|
177
|
+
tool_name: toolCall.function?.name || "",
|
|
178
|
+
arguments: toolCall.function?.arguments || ""
|
|
179
|
+
}))
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
102
184
|
/**
|
|
103
185
|
* 处理对话消息
|
|
104
186
|
* @param messages 对话消息数组
|
|
@@ -158,7 +240,20 @@ var Deepseek = class extends import_core.Component {
|
|
|
158
240
|
};
|
|
159
241
|
if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {
|
|
160
242
|
result.tool_calls = responseMessage.tool_calls.map((toolCall) => {
|
|
161
|
-
|
|
243
|
+
let args;
|
|
244
|
+
const rawArgs = toolCall.function.arguments;
|
|
245
|
+
if (typeof rawArgs === "string") {
|
|
246
|
+
const raw = rawArgs;
|
|
247
|
+
try {
|
|
248
|
+
args = raw && raw.trim().length > 0 ? JSON.parse(raw) : {};
|
|
249
|
+
} catch {
|
|
250
|
+
args = {};
|
|
251
|
+
}
|
|
252
|
+
} else if (rawArgs && typeof rawArgs === "object") {
|
|
253
|
+
args = rawArgs;
|
|
254
|
+
} else {
|
|
255
|
+
args = {};
|
|
256
|
+
}
|
|
162
257
|
return {
|
|
163
258
|
id: toolCall.id,
|
|
164
259
|
// 满足 Agent 组件期望的格式
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/model-provider/index.ts","../../src/model-provider/deepseek/index.ts"],"sourcesContent":["export {\n default as Deepseek,\n type Message as DeepseekMessage,\n type ToolCall as DeepseekToolCall,\n type DeepseekConfig,\n type MessageRole as DeepseekMessageRole,\n} from './deepseek';\n","import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n const args =\n typeof toolCall.function.arguments === 'string'\n ? JSON.parse(toolCall.function.arguments || '{}')\n : toolCall.function.arguments || {};\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0B;AAC1B,oBAAmB;AA2HnB,IAAM,WAAN,cAAuB,sBAAU;AAAA,EAY/B,YAAY,QAAwB;AAClC,UAAM,CAAC,CAAC;AAGR,SAAK,SAAS,IAAI,cAAAA,QAAO;AAAA,MACvB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO,WAAW;AAAA,IAC7B,CAAC;AAGD,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,cAAc,OAAO,eAAe;AACzC,SAAK,YAAY,OAAO;AACxB,SAAK,OAAO,OAAO;AACnB,SAAK,eAAe,OAAO;AAG3B,QAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAK,QAAQ,OAAO,SAAS,IAAI,WAAS;AAAA,QACxC,MAAM;AAAA,QACN,UAAU;AAAA,UACR,MAAM,KAAK;AAAA,UACX,aAAa,KAAK;AAAA,UAClB,YAAY,KAAK,cAAc,CAAC;AAAA,QAClC;AAAA,MACF,EAAE;AAAA,IACJ,OAAO;AAEL,WAAK,QAAQ,OAAO;AAAA,IACtB;AAGA,0BAAU,KAAK,EAAE,QAAQ,EAAE,OAAO,IAAI;AACtC,0BAAU,KAAK,EAAE,UAAU,EAAE,OAAO,IAAI;AACxC,0BAAU,KAAK,EAAE,YAAY,EAAE,OAAO,IAAI;AAC1C,0BAAU,KAAK,EAAE,SAAS,EAAE,OAAO,IAAI;AAA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,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,OAAsD;AAC9D,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAO,KAAK,mBAAmB,KAAK;AAAA,IACtC,OAAO;AACL,aAAO,KAAK,eAAe,KAAK;AAAA,IAClC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,WAAW,IAAS,IAAS;AAE3B,OAAG,QAAQ,EAAE,QAAQ,OAAO,WAAmB;AAC7C,UAAI;AACF,cAAM,aAAa,MAAM,KAAK,mBAAmB,MAAM;AACvD,WAAG,YAAY,EAAE,KAAK,UAAU;AAAA,MAClC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,YAAY,EAAE,KAAK,8BAAU;AAAA,MAClC;AAAA,IACF,CAAC;AAGD,OAAG,UAAU,EAAE,QAAQ,OAAO,aAAwB;AACpD,UAAI;AACF,cAAM,kBAAkB,MAAM,KAAK,eAAe,QAAQ;AAC1D,WAAG,SAAS,EAAE,KAAK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":["OpenAI"]}
|
|
1
|
+
{"version":3,"sources":["../../src/model-provider/index.ts","../../src/model-provider/deepseek/index.ts"],"sourcesContent":["export {\n default as Deepseek,\n type Message as DeepseekMessage,\n type ToolCall as DeepseekToolCall,\n type DeepseekConfig,\n type MessageRole as DeepseekMessageRole,\n} from './deepseek';\n","import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n\n /**\n * dangerouslyAllowBrowser\n * 是否允许在浏览器等非安全环境内运行\n */\n dangerouslyAllowBrowser?: boolean;\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false,\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 流式处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的流式响应消息异步生成器\n */\n async *streamChatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): AsyncGenerator<Partial<Message>> {\n // 转换消息格式 (复用现有逻辑)\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n stream: true, // 开启流式模式\n };\n\n // 决定使用哪些工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n const formattedTools = toolsToUse.map(tool => {\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n if (typedTool.type === 'function') {\n return tool;\n }\n\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用流式 API\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const stream = (await this.client.chat.completions.create(requestParams)) as any;\n\n // 处理流式响应\n for await (const chunk of stream) {\n const delta = chunk.choices[0]?.delta;\n\n if (delta?.content) {\n // 流式文本内容\n yield {\n role: 'assistant',\n content: delta.content,\n };\n }\n\n if (delta?.tool_calls) {\n // 流式工具调用(如果需要)\n yield {\n role: 'assistant',\n content: '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n tool_calls: delta.tool_calls.map((toolCall: any) => ({\n id: toolCall.id || '',\n function: {\n name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n },\n type: 'function',\n tool_name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n })),\n };\n }\n }\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n let args: Record<string, unknown>;\n\n const rawArgs = toolCall.function.arguments;\n\n if (typeof rawArgs === 'string') {\n const raw = rawArgs;\n try {\n args = raw && raw.trim().length > 0 ? (JSON.parse(raw) as Record<string, unknown>) : {};\n } catch {\n args = {};\n }\n } else if (rawArgs && typeof rawArgs === 'object') {\n args = rawArgs as Record<string, unknown>;\n } else {\n args = {};\n }\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0B;AAC1B,oBAAmB;AAiInB,IAAM,WAAN,cAAuB,sBAAU;AAAA,EAY/B,YAAY,QAAwB;AAClC,UAAM,CAAC,CAAC;AAGR,SAAK,SAAS,IAAI,cAAAA,QAAO;AAAA,MACvB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO,WAAW;AAAA,MAC3B,yBAAyB,OAAO,2BAA2B;AAAA,IAC7D,CAAC;AAGD,SAAK,QAAQ,OAAO,SAAS;AAC7B,SAAK,cAAc,OAAO,eAAe;AACzC,SAAK,YAAY,OAAO;AACxB,SAAK,OAAO,OAAO;AACnB,SAAK,eAAe,OAAO;AAG3B,QAAI,OAAO,YAAY,OAAO,SAAS,SAAS,GAAG;AAEjD,WAAK,QAAQ,OAAO,SAAS,IAAI,WAAS;AAAA,QACxC,MAAM;AAAA,QACN,UAAU;AAAA,UACR,MAAM,KAAK;AAAA,UACX,aAAa,KAAK;AAAA,UAClB,YAAY,KAAK,cAAc,CAAC;AAAA,QAClC;AAAA,MACF,EAAE;AAAA,IACJ,OAAO;AAEL,WAAK,QAAQ,OAAO;AAAA,IACtB;AAGA,0BAAU,KAAK,EAAE,QAAQ,EAAE,OAAO,IAAI;AACtC,0BAAU,KAAK,EAAE,UAAU,EAAE,OAAO,IAAI;AACxC,0BAAU,KAAK,EAAE,YAAY,EAAE,OAAO,IAAI;AAC1C,0BAAU,K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,UAAU,EAAE,QAAQ,OAAO,aAAwB;AACpD,UAAI;AACF,cAAM,kBAAkB,MAAM,KAAK,eAAe,QAAQ;AAC1D,WAAG,SAAS,EAAE,KAAK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":["OpenAI"]}
|
|
@@ -48,6 +48,11 @@ interface DeepseekConfig {
|
|
|
48
48
|
* 使用 OpenAI 兼容的工具定义格式
|
|
49
49
|
*/
|
|
50
50
|
tools?: unknown[];
|
|
51
|
+
/**
|
|
52
|
+
* dangerouslyAllowBrowser
|
|
53
|
+
* 是否允许在浏览器等非安全环境内运行
|
|
54
|
+
*/
|
|
55
|
+
dangerouslyAllowBrowser?: boolean;
|
|
51
56
|
}
|
|
52
57
|
/**
|
|
53
58
|
* 消息角色类型
|
|
@@ -119,6 +124,15 @@ declare class Deepseek extends Component {
|
|
|
119
124
|
* @returns 生成的文本
|
|
120
125
|
*/
|
|
121
126
|
generateCompletion(prompt: string): Promise<string>;
|
|
127
|
+
/**
|
|
128
|
+
* 流式处理对话消息
|
|
129
|
+
* @param messages 对话消息数组
|
|
130
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
131
|
+
* @returns 生成的流式响应消息异步生成器
|
|
132
|
+
*/
|
|
133
|
+
streamChatCompletion(messages: Message[], options?: {
|
|
134
|
+
temporaryTools?: unknown[];
|
|
135
|
+
}): AsyncGenerator<Partial<Message>>;
|
|
122
136
|
/**
|
|
123
137
|
* 处理对话消息
|
|
124
138
|
* @param messages 对话消息数组
|
|
@@ -48,6 +48,11 @@ interface DeepseekConfig {
|
|
|
48
48
|
* 使用 OpenAI 兼容的工具定义格式
|
|
49
49
|
*/
|
|
50
50
|
tools?: unknown[];
|
|
51
|
+
/**
|
|
52
|
+
* dangerouslyAllowBrowser
|
|
53
|
+
* 是否允许在浏览器等非安全环境内运行
|
|
54
|
+
*/
|
|
55
|
+
dangerouslyAllowBrowser?: boolean;
|
|
51
56
|
}
|
|
52
57
|
/**
|
|
53
58
|
* 消息角色类型
|
|
@@ -119,6 +124,15 @@ declare class Deepseek extends Component {
|
|
|
119
124
|
* @returns 生成的文本
|
|
120
125
|
*/
|
|
121
126
|
generateCompletion(prompt: string): Promise<string>;
|
|
127
|
+
/**
|
|
128
|
+
* 流式处理对话消息
|
|
129
|
+
* @param messages 对话消息数组
|
|
130
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
131
|
+
* @returns 生成的流式响应消息异步生成器
|
|
132
|
+
*/
|
|
133
|
+
streamChatCompletion(messages: Message[], options?: {
|
|
134
|
+
temporaryTools?: unknown[];
|
|
135
|
+
}): AsyncGenerator<Partial<Message>>;
|
|
122
136
|
/**
|
|
123
137
|
* 处理对话消息
|
|
124
138
|
* @param messages 对话消息数组
|
|
@@ -6,7 +6,8 @@ var Deepseek = class extends Component {
|
|
|
6
6
|
super({});
|
|
7
7
|
this.client = new OpenAI({
|
|
8
8
|
apiKey: config.apiKey,
|
|
9
|
-
baseURL: config.baseURL || "https://api.deepseek.com/v1"
|
|
9
|
+
baseURL: config.baseURL || "https://api.deepseek.com/v1",
|
|
10
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false
|
|
10
11
|
});
|
|
11
12
|
this.model = config.model || "deepseek-chat";
|
|
12
13
|
this.temperature = config.temperature ?? 0.7;
|
|
@@ -63,6 +64,87 @@ var Deepseek = class extends Component {
|
|
|
63
64
|
}
|
|
64
65
|
return response.choices[0].message.content || "";
|
|
65
66
|
}
|
|
67
|
+
/**
|
|
68
|
+
* 流式处理对话消息
|
|
69
|
+
* @param messages 对话消息数组
|
|
70
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
71
|
+
* @returns 生成的流式响应消息异步生成器
|
|
72
|
+
*/
|
|
73
|
+
async *streamChatCompletion(messages, options) {
|
|
74
|
+
const formattedMessages = [];
|
|
75
|
+
if (this.systemPrompt) {
|
|
76
|
+
formattedMessages.push({
|
|
77
|
+
role: "system",
|
|
78
|
+
content: this.systemPrompt
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
formattedMessages.push(
|
|
82
|
+
...messages.map((msg) => {
|
|
83
|
+
const formattedMsg = {
|
|
84
|
+
role: msg.role,
|
|
85
|
+
content: msg.content
|
|
86
|
+
};
|
|
87
|
+
if (msg.role === "tool" && msg.tool_call_id) {
|
|
88
|
+
formattedMsg.tool_call_id = msg.tool_call_id;
|
|
89
|
+
}
|
|
90
|
+
return formattedMsg;
|
|
91
|
+
})
|
|
92
|
+
);
|
|
93
|
+
const requestParams = {
|
|
94
|
+
model: this.model,
|
|
95
|
+
messages: formattedMessages,
|
|
96
|
+
temperature: this.temperature,
|
|
97
|
+
max_tokens: this.maxTokens,
|
|
98
|
+
top_p: this.topP,
|
|
99
|
+
stream: true
|
|
100
|
+
// 开启流式模式
|
|
101
|
+
};
|
|
102
|
+
const toolsToUse = options?.temporaryTools || this.tools;
|
|
103
|
+
if (toolsToUse && toolsToUse.length > 0) {
|
|
104
|
+
const formattedTools = toolsToUse.map((tool) => {
|
|
105
|
+
const typedTool = tool;
|
|
106
|
+
if (typedTool.type === "function") {
|
|
107
|
+
return tool;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
type: "function",
|
|
111
|
+
function: {
|
|
112
|
+
name: typedTool.name,
|
|
113
|
+
description: typedTool.description,
|
|
114
|
+
parameters: typedTool.parameters || {}
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
requestParams.tools = formattedTools;
|
|
119
|
+
}
|
|
120
|
+
const stream = await this.client.chat.completions.create(requestParams);
|
|
121
|
+
for await (const chunk of stream) {
|
|
122
|
+
const delta = chunk.choices[0]?.delta;
|
|
123
|
+
if (delta?.content) {
|
|
124
|
+
yield {
|
|
125
|
+
role: "assistant",
|
|
126
|
+
content: delta.content
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
if (delta?.tool_calls) {
|
|
130
|
+
yield {
|
|
131
|
+
role: "assistant",
|
|
132
|
+
content: "",
|
|
133
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
134
|
+
tool_calls: delta.tool_calls.map((toolCall) => ({
|
|
135
|
+
id: toolCall.id || "",
|
|
136
|
+
function: {
|
|
137
|
+
name: toolCall.function?.name || "",
|
|
138
|
+
arguments: toolCall.function?.arguments || ""
|
|
139
|
+
},
|
|
140
|
+
type: "function",
|
|
141
|
+
tool_name: toolCall.function?.name || "",
|
|
142
|
+
arguments: toolCall.function?.arguments || ""
|
|
143
|
+
}))
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
66
148
|
/**
|
|
67
149
|
* 处理对话消息
|
|
68
150
|
* @param messages 对话消息数组
|
|
@@ -122,7 +204,20 @@ var Deepseek = class extends Component {
|
|
|
122
204
|
};
|
|
123
205
|
if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {
|
|
124
206
|
result.tool_calls = responseMessage.tool_calls.map((toolCall) => {
|
|
125
|
-
|
|
207
|
+
let args;
|
|
208
|
+
const rawArgs = toolCall.function.arguments;
|
|
209
|
+
if (typeof rawArgs === "string") {
|
|
210
|
+
const raw = rawArgs;
|
|
211
|
+
try {
|
|
212
|
+
args = raw && raw.trim().length > 0 ? JSON.parse(raw) : {};
|
|
213
|
+
} catch {
|
|
214
|
+
args = {};
|
|
215
|
+
}
|
|
216
|
+
} else if (rawArgs && typeof rawArgs === "object") {
|
|
217
|
+
args = rawArgs;
|
|
218
|
+
} else {
|
|
219
|
+
args = {};
|
|
220
|
+
}
|
|
126
221
|
return {
|
|
127
222
|
id: toolCall.id,
|
|
128
223
|
// 满足 Agent 组件期望的格式
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/model-provider/deepseek/index.ts"],"sourcesContent":["import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n const args =\n typeof toolCall.function.arguments === 'string'\n ? JSON.parse(toolCall.function.arguments || '{}')\n : toolCall.function.arguments || {};\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,YAAY;AA2HnB,IAAM,WAAN,cAAuB,UAAU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M,0CAAsB,KAAK;AACzC,WAAG,YAAY,EAAE,KAAK,8BAAU;AAAA,MAClC;AAAA,IACF,CAAC;AAGD,OAAG,UAAU,EAAE,QAAQ,OAAO,aAAwB;AACpD,UAAI;AACF,cAAM,kBAAkB,MAAM,KAAK,eAAe,QAAQ;AAC1D,WAAG,SAAS,EAAE,KAAK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../../src/model-provider/deepseek/index.ts"],"sourcesContent":["import { Component } from '@astack-tech/core';\nimport OpenAI from 'openai';\n\n/**\n * Deepseek 模型提供者配置\n */\nexport interface DeepseekConfig {\n /**\n * API 密钥\n */\n apiKey: string;\n\n /**\n * 使用的模型名称\n * 默认为 'deepseek-chat'\n */\n model?: string;\n\n /**\n * 自定义 API 基础 URL\n * 默认为 Deepseek 官方 API 地址\n */\n baseURL?: string;\n\n /**\n * 温度参数,控制输出的随机性\n */\n temperature?: number;\n\n /**\n * 最大生成的 token 数\n */\n maxTokens?: number;\n\n /**\n * 采样概率\n */\n topP?: number;\n\n /**\n * 系统提示消息\n */\n systemPrompt?: string;\n\n /**\n * 工具实例数组,直接传递工具实例\n * 每个工具应具有 name、description 和 parameters 属性\n */\n rawTools?: Array<{\n name: string;\n description: string;\n parameters?: Record<string, unknown>;\n }>;\n\n /**\n * API 格式的工具定义数组,与 rawTools 二选一\n * 使用 OpenAI 兼容的工具定义格式\n */\n tools?: unknown[];\n\n /**\n * dangerouslyAllowBrowser\n * 是否允许在浏览器等非安全环境内运行\n */\n dangerouslyAllowBrowser?: boolean;\n}\n\n/**\n * 消息角色类型\n */\nexport type MessageRole = 'system' | 'user' | 'assistant' | 'tool';\n\n/**\n * 工具调用\n */\nexport interface ToolCall {\n /**\n * 工具调用 ID\n */\n id?: string;\n\n /**\n * 工具名称\n */\n tool_name: string;\n\n /**\n * 工具调用参数\n */\n arguments: Record<string, unknown>;\n}\n\n/**\n * 对话消息类型\n */\nexport interface Message {\n /**\n * 消息角色\n */\n role: MessageRole;\n\n /**\n * 消息内容\n */\n content?: string;\n\n /**\n * 工具调用\n */\n tool_calls?: ToolCall[];\n\n /**\n * 工具调用 ID,用于工具响应消息\n */\n tool_call_id?: string;\n}\n\n/**\n * Deepseek 组件\n *\n * 使用 OpenAI 兼容接口调用 Deepseek 模型\n *\n * 输入:\n * - prompt: 文本提示\n * - messages: 对话消息数组\n *\n * 输出:\n * - completion: 生成的文本完成\n * - message: 生成的响应消息\n */\nclass Deepseek extends Component {\n // OpenAI 客户端实例\n private client: OpenAI;\n\n // 配置选项\n private model: string;\n private temperature: number;\n private maxTokens?: number;\n private topP?: number;\n private systemPrompt?: string;\n private tools?: unknown[];\n\n constructor(config: DeepseekConfig) {\n super({});\n\n // 配置客户端\n this.client = new OpenAI({\n apiKey: config.apiKey,\n baseURL: config.baseURL || 'https://api.deepseek.com/v1',\n dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false,\n });\n\n // 保存基本配置\n this.model = config.model || 'deepseek-chat';\n this.temperature = config.temperature ?? 0.7;\n this.maxTokens = config.maxTokens;\n this.topP = config.topP;\n this.systemPrompt = config.systemPrompt;\n\n // 处理工具配置,优先使用 rawTools\n if (config.rawTools && config.rawTools.length > 0) {\n // 将工具实例转换为 API 格式\n this.tools = config.rawTools.map(tool => ({\n type: 'function',\n function: {\n name: tool.name,\n description: tool.description,\n parameters: tool.parameters || {},\n },\n }));\n } else {\n // 使用直接提供的 API 格式工具\n this.tools = config.tools;\n }\n\n // 重命名端口\n Component.Port.I('prompt').attach(this);\n Component.Port.I('messages').attach(this);\n Component.Port.O('completion').attach(this);\n Component.Port.O('message').attach(this);\n }\n\n /**\n * 生成文本完成\n * @param prompt 提示文本\n * @returns 生成的文本\n */\n async generateCompletion(prompt: string): Promise<string> {\n const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n messages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提示\n messages.push({\n role: 'user',\n content: prompt,\n });\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 如果有工具定义,添加到请求中\n if (this.tools && this.tools.length > 0) {\n requestParams.tools = this.tools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 处理工具调用结果\n if (\n response.choices[0].message.tool_calls &&\n response.choices[0].message.tool_calls.length > 0\n ) {\n return JSON.stringify(response.choices[0].message);\n }\n\n return response.choices[0].message.content || '';\n }\n\n /**\n * 流式处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的流式响应消息异步生成器\n */\n async *streamChatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): AsyncGenerator<Partial<Message>> {\n // 转换消息格式 (复用现有逻辑)\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n stream: true, // 开启流式模式\n };\n\n // 决定使用哪些工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n const formattedTools = toolsToUse.map(tool => {\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n if (typedTool.type === 'function') {\n return tool;\n }\n\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用流式 API\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const stream = (await this.client.chat.completions.create(requestParams)) as any;\n\n // 处理流式响应\n for await (const chunk of stream) {\n const delta = chunk.choices[0]?.delta;\n\n if (delta?.content) {\n // 流式文本内容\n yield {\n role: 'assistant',\n content: delta.content,\n };\n }\n\n if (delta?.tool_calls) {\n // 流式工具调用(如果需要)\n yield {\n role: 'assistant',\n content: '',\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n tool_calls: delta.tool_calls.map((toolCall: any) => ({\n id: toolCall.id || '',\n function: {\n name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n },\n type: 'function',\n tool_name: toolCall.function?.name || '',\n arguments: toolCall.function?.arguments || '',\n })),\n };\n }\n }\n }\n\n /**\n * 处理对话消息\n * @param messages 对话消息数组\n * @param options 可选的调用选项,包含临时工具列表\n * @returns 生成的响应消息\n */\n async chatCompletion(\n messages: Message[],\n options?: { temporaryTools?: unknown[] }\n ): Promise<Message> {\n // 转换消息格式\n const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];\n\n // 添加系统提示\n if (this.systemPrompt) {\n formattedMessages.push({\n role: 'system',\n content: this.systemPrompt,\n });\n }\n\n // 添加用户提供的消息\n formattedMessages.push(\n ...messages.map(msg => {\n // 基本消息结构\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const formattedMsg: any = {\n role: msg.role,\n content: msg.content,\n };\n\n // 如果有工具调用 ID,添加到消息中\n if (msg.role === 'tool' && msg.tool_call_id) {\n formattedMsg.tool_call_id = msg.tool_call_id;\n }\n\n return formattedMsg;\n })\n );\n\n // 创建请求参数\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const requestParams: any = {\n model: this.model,\n messages: formattedMessages,\n temperature: this.temperature,\n max_tokens: this.maxTokens,\n top_p: this.topP,\n };\n\n // 决定使用哪些工具\n // 如果提供了临时工具,优先使用临时工具\n const toolsToUse = options?.temporaryTools || this.tools;\n\n // 如果有工具定义,添加到请求中\n if (toolsToUse && toolsToUse.length > 0) {\n // 检查工具格式并转换,确保符合 Deepseek API 要求\n const formattedTools = toolsToUse.map(tool => {\n // 使用类型断言来处理 unknown 类型\n type ToolType = {\n type?: string;\n name?: string;\n description?: string;\n parameters?: Record<string, unknown>;\n };\n\n const typedTool = tool as ToolType;\n\n // 如果工具已经是 API 格式,直接返回\n if (typedTool.type === 'function') {\n return tool;\n }\n\n // 如果是简单工具实例,转换为 API 格式\n return {\n type: 'function',\n function: {\n name: typedTool.name,\n description: typedTool.description,\n parameters: typedTool.parameters || {},\n },\n };\n });\n\n requestParams.tools = formattedTools;\n }\n\n // 调用 API\n const response = await this.client.chat.completions.create(requestParams);\n\n // 解析响应\n const responseMessage = response.choices[0].message;\n\n // 转换为内部消息格式\n const result: Message = {\n role: 'assistant',\n content: responseMessage.content || '',\n };\n\n // 如果有工具调用,添加到结果中\n if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {\n // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段\n // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段\n result.tool_calls = responseMessage.tool_calls.map(toolCall => {\n // 解析参数,确保是对象形式\n let args: Record<string, unknown>;\n\n const rawArgs = toolCall.function.arguments;\n\n if (typeof rawArgs === 'string') {\n const raw = rawArgs;\n try {\n args = raw && raw.trim().length > 0 ? (JSON.parse(raw) as Record<string, unknown>) : {};\n } catch {\n args = {};\n }\n } else if (rawArgs && typeof rawArgs === 'object') {\n args = rawArgs as Record<string, unknown>;\n } else {\n args = {};\n }\n\n return {\n id: toolCall.id,\n // 满足 Agent 组件期望的格式\n function: {\n name: toolCall.function.name,\n arguments: toolCall.function.arguments || '{}',\n },\n type: 'function',\n // 满足内部 ToolCall 接口\n tool_name: toolCall.function.name,\n arguments: args,\n };\n });\n }\n\n return result;\n }\n\n /**\n * 在独立模式下运行组件\n * @param input 输入参数,可以是提示文本或对话消息数组\n * @returns 生成的文本或响应消息\n */\n async run(input: string | Message[]): Promise<string | Message> {\n if (typeof input === 'string') {\n return this.generateCompletion(input);\n } else {\n return this.chatCompletion(input);\n }\n }\n\n /**\n * 在流水线中运行组件\n * @param $i 输入端口\n * @param $o 输出端口\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n _transform($i: any, $o: any) {\n // 处理文本提示输入\n $i('prompt').receive(async (prompt: string) => {\n try {\n const completion = await this.generateCompletion(prompt);\n $o('completion').send(completion);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('completion').send('API 调用错误');\n }\n });\n\n // 处理对话消息输入\n $i('messages').receive(async (messages: Message[]) => {\n try {\n const responseMessage = await this.chatCompletion(messages);\n $o('message').send(responseMessage);\n } catch (error) {\n console.error('Deepseek API 调用错误:', error);\n $o('message').send({\n role: 'assistant',\n content: 'API 调用错误',\n });\n }\n });\n }\n}\n\n// 默认导出组件\nexport default Deepseek;\n"],"mappings":";AAAA,SAAS,iBAAiB;AAC1B,OAAO,YAAY;AAiInB,IAAM,WAAN,cAAuB,UAAU;AAAA,EAY/B,YAAY,QAAwB;AAClC,UAAM,CAAC,CAAC;AAGR,SAAK,SAAS,IAAI,OAAO;AAAA,MACvB,QAAQ,OAAO;AAAA,MACf,SAAS,OAAO,WAAW;AAAA,MAC3B,yBAAyB,OAAO,2BAA2B;AAAA,IAC7D,CAAC;AAGD,SAAK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F,CAAC;AAAA,IACH;AAEA,WAAO;AAAA,EACT;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,IAAI,OAAsD;AAC9D,QAAI,OAAO,UAAU,UAAU;AAC7B,aAAO,KAAK,mBAAmB,KAAK;AAAA,IACtC,OAAO;AACL,aAAO,KAAK,eAAe,KAAK;AAAA,IAClC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,WAAW,IAAS,IAAS;AAE3B,OAAG,QAAQ,EAAE,QAAQ,OAAO,WAAmB;AAC7C,UAAI;AACF,cAAM,aAAa,MAAM,KAAK,mBAAmB,MAAM;AACvD,WAAG,YAAY,EAAE,KAAK,UAAU;AAAA,MAClC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,YAAY,EAAE,KAAK,8BAAU;AAAA,MAClC;AAAA,IACF,CAAC;AAGD,OAAG,UAAU,EAAE,QAAQ,OAAO,aAAwB;AACpD,UAAI;AACF,cAAM,kBAAkB,MAAM,KAAK,eAAe,QAAQ;AAC1D,WAAG,SAAS,EAAE,KAAK,eAAe;AAAA,MACpC,SAAS,OAAO;AACd,gBAAQ,MAAM,0CAAsB,KAAK;AACzC,WAAG,SAAS,EAAE,KAAK;AAAA,UACjB,MAAM;AAAA,UACN,SAAS;AAAA,QACX,CAAC;AAAA,MACH;AAAA,IACF,CAAC;AAAA,EACH;AACF;AAGA,IAAO,mBAAQ;","names":[]}
|
package/package.json
CHANGED
|
@@ -57,6 +57,12 @@ export interface DeepseekConfig {
|
|
|
57
57
|
* 使用 OpenAI 兼容的工具定义格式
|
|
58
58
|
*/
|
|
59
59
|
tools?: unknown[];
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* dangerouslyAllowBrowser
|
|
63
|
+
* 是否允许在浏览器等非安全环境内运行
|
|
64
|
+
*/
|
|
65
|
+
dangerouslyAllowBrowser?: boolean;
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
/**
|
|
@@ -141,6 +147,7 @@ class Deepseek extends Component {
|
|
|
141
147
|
this.client = new OpenAI({
|
|
142
148
|
apiKey: config.apiKey,
|
|
143
149
|
baseURL: config.baseURL || 'https://api.deepseek.com/v1',
|
|
150
|
+
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false,
|
|
144
151
|
});
|
|
145
152
|
|
|
146
153
|
// 保存基本配置
|
|
@@ -224,6 +231,124 @@ class Deepseek extends Component {
|
|
|
224
231
|
return response.choices[0].message.content || '';
|
|
225
232
|
}
|
|
226
233
|
|
|
234
|
+
/**
|
|
235
|
+
* 流式处理对话消息
|
|
236
|
+
* @param messages 对话消息数组
|
|
237
|
+
* @param options 可选的调用选项,包含临时工具列表
|
|
238
|
+
* @returns 生成的流式响应消息异步生成器
|
|
239
|
+
*/
|
|
240
|
+
async *streamChatCompletion(
|
|
241
|
+
messages: Message[],
|
|
242
|
+
options?: { temporaryTools?: unknown[] }
|
|
243
|
+
): AsyncGenerator<Partial<Message>> {
|
|
244
|
+
// 转换消息格式 (复用现有逻辑)
|
|
245
|
+
const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];
|
|
246
|
+
|
|
247
|
+
// 添加系统提示
|
|
248
|
+
if (this.systemPrompt) {
|
|
249
|
+
formattedMessages.push({
|
|
250
|
+
role: 'system',
|
|
251
|
+
content: this.systemPrompt,
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// 添加用户提供的消息
|
|
256
|
+
formattedMessages.push(
|
|
257
|
+
...messages.map(msg => {
|
|
258
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
259
|
+
const formattedMsg: any = {
|
|
260
|
+
role: msg.role,
|
|
261
|
+
content: msg.content,
|
|
262
|
+
};
|
|
263
|
+
|
|
264
|
+
if (msg.role === 'tool' && msg.tool_call_id) {
|
|
265
|
+
formattedMsg.tool_call_id = msg.tool_call_id;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return formattedMsg;
|
|
269
|
+
})
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
// 创建请求参数
|
|
273
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
274
|
+
const requestParams: any = {
|
|
275
|
+
model: this.model,
|
|
276
|
+
messages: formattedMessages,
|
|
277
|
+
temperature: this.temperature,
|
|
278
|
+
max_tokens: this.maxTokens,
|
|
279
|
+
top_p: this.topP,
|
|
280
|
+
stream: true, // 开启流式模式
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// 决定使用哪些工具
|
|
284
|
+
const toolsToUse = options?.temporaryTools || this.tools;
|
|
285
|
+
|
|
286
|
+
// 如果有工具定义,添加到请求中
|
|
287
|
+
if (toolsToUse && toolsToUse.length > 0) {
|
|
288
|
+
const formattedTools = toolsToUse.map(tool => {
|
|
289
|
+
type ToolType = {
|
|
290
|
+
type?: string;
|
|
291
|
+
name?: string;
|
|
292
|
+
description?: string;
|
|
293
|
+
parameters?: Record<string, unknown>;
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const typedTool = tool as ToolType;
|
|
297
|
+
|
|
298
|
+
if (typedTool.type === 'function') {
|
|
299
|
+
return tool;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
type: 'function',
|
|
304
|
+
function: {
|
|
305
|
+
name: typedTool.name,
|
|
306
|
+
description: typedTool.description,
|
|
307
|
+
parameters: typedTool.parameters || {},
|
|
308
|
+
},
|
|
309
|
+
};
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
requestParams.tools = formattedTools;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// 调用流式 API
|
|
316
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
317
|
+
const stream = (await this.client.chat.completions.create(requestParams)) as any;
|
|
318
|
+
|
|
319
|
+
// 处理流式响应
|
|
320
|
+
for await (const chunk of stream) {
|
|
321
|
+
const delta = chunk.choices[0]?.delta;
|
|
322
|
+
|
|
323
|
+
if (delta?.content) {
|
|
324
|
+
// 流式文本内容
|
|
325
|
+
yield {
|
|
326
|
+
role: 'assistant',
|
|
327
|
+
content: delta.content,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
if (delta?.tool_calls) {
|
|
332
|
+
// 流式工具调用(如果需要)
|
|
333
|
+
yield {
|
|
334
|
+
role: 'assistant',
|
|
335
|
+
content: '',
|
|
336
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
337
|
+
tool_calls: delta.tool_calls.map((toolCall: any) => ({
|
|
338
|
+
id: toolCall.id || '',
|
|
339
|
+
function: {
|
|
340
|
+
name: toolCall.function?.name || '',
|
|
341
|
+
arguments: toolCall.function?.arguments || '',
|
|
342
|
+
},
|
|
343
|
+
type: 'function',
|
|
344
|
+
tool_name: toolCall.function?.name || '',
|
|
345
|
+
arguments: toolCall.function?.arguments || '',
|
|
346
|
+
})),
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
|
|
227
352
|
/**
|
|
228
353
|
* 处理对话消息
|
|
229
354
|
* @param messages 对话消息数组
|
|
@@ -329,10 +454,22 @@ class Deepseek extends Component {
|
|
|
329
454
|
// 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段
|
|
330
455
|
result.tool_calls = responseMessage.tool_calls.map(toolCall => {
|
|
331
456
|
// 解析参数,确保是对象形式
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
457
|
+
let args: Record<string, unknown>;
|
|
458
|
+
|
|
459
|
+
const rawArgs = toolCall.function.arguments;
|
|
460
|
+
|
|
461
|
+
if (typeof rawArgs === 'string') {
|
|
462
|
+
const raw = rawArgs;
|
|
463
|
+
try {
|
|
464
|
+
args = raw && raw.trim().length > 0 ? (JSON.parse(raw) as Record<string, unknown>) : {};
|
|
465
|
+
} catch {
|
|
466
|
+
args = {};
|
|
467
|
+
}
|
|
468
|
+
} else if (rawArgs && typeof rawArgs === 'object') {
|
|
469
|
+
args = rawArgs as Record<string, unknown>;
|
|
470
|
+
} else {
|
|
471
|
+
args = {};
|
|
472
|
+
}
|
|
336
473
|
|
|
337
474
|
return {
|
|
338
475
|
id: toolCall.id,
|