@astack-tech/integrations 0.1.1-beta.6 → 0.2.0-beta.10

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.
@@ -1,610 +1,49 @@
1
- import { Component } from '@astack-tech/core';
2
- import OpenAI from 'openai';
1
+ import OpenAICompatible from '../openai-compatible';
2
+ import type { OpenAICompatibleConfig } from '../openai-compatible';
3
+
4
+ export type {
5
+ AssistantMessage,
6
+ ChatCompletionOptions,
7
+ Message,
8
+ MessageRole,
9
+ RawTool,
10
+ ResponseToolCall,
11
+ StreamChunk,
12
+ StreamToolCall,
13
+ TokenUsage,
14
+ ToolCall,
15
+ } from '../openai-compatible';
16
+
17
+ const DEFAULT_BASE_URL = 'https://api.deepseek.com/v1';
18
+ const DEFAULT_MODEL = 'deepseek-chat';
3
19
 
4
20
  /**
5
- * Deepseek 模型提供者配置
21
+ * Options for the DeepSeek model provider. They are the options of
22
+ * `OpenAICompatible`, with DeepSeek's endpoint and model as defaults.
6
23
  */
7
- export interface DeepseekConfig {
8
- /**
9
- * API 密钥
10
- */
11
- apiKey: string;
12
-
13
- /**
14
- * 使用的模型名称
15
- * 默认为 'deepseek-chat'
16
- */
17
- model?: string;
18
-
19
- /**
20
- * 自定义 API 基础 URL
21
- * 默认为 Deepseek 官方 API 地址
22
- */
23
- baseURL?: string;
24
-
25
- /**
26
- * 温度参数,控制输出的随机性
27
- */
28
- temperature?: number;
29
-
30
- /**
31
- * 最大生成的 token 数
32
- */
33
- maxTokens?: number;
34
-
35
- /**
36
- * 采样概率
37
- */
38
- topP?: number;
39
-
40
- /**
41
- * 系统提示消息
42
- */
43
- systemPrompt?: string;
44
-
45
- /**
46
- * 工具实例数组,直接传递工具实例
47
- * 每个工具应具有 name、description 和 parameters 属性
48
- */
49
- rawTools?: Array<{
50
- name: string;
51
- description: string;
52
- parameters?: Record<string, unknown>;
53
- }>;
54
-
55
- /**
56
- * API 格式的工具定义数组,与 rawTools 二选一
57
- * 使用 OpenAI 兼容的工具定义格式
58
- */
59
- tools?: unknown[];
60
-
61
- /**
62
- * dangerouslyAllowBrowser
63
- * 是否允许在浏览器等非安全环境内运行
64
- */
65
- dangerouslyAllowBrowser?: boolean;
66
- }
67
-
68
- /**
69
- * 消息角色类型
70
- */
71
- export type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
72
-
73
- /**
74
- * 工具调用
75
- */
76
- export interface ToolCall {
77
- /**
78
- * 工具调用 ID
79
- */
80
- id?: string;
81
-
82
- /**
83
- * 工具名称
84
- */
85
- tool_name: string;
86
-
87
- /**
88
- * 工具调用参数
89
- */
90
- arguments: Record<string, unknown>;
91
- }
92
-
93
- /**
94
- * Token 使用统计
95
- */
96
- export interface TokenUsage {
97
- /**
98
- * 完成部分的 token 数量
99
- */
100
- completion_tokens: number;
101
-
102
- /**
103
- * 提示部分的 token 数量
104
- */
105
- prompt_tokens: number;
106
-
107
- /**
108
- * 提示缓存命中的 token 数量(可选)
109
- */
110
- prompt_cache_hit_tokens?: number;
111
-
112
- /**
113
- * 提示缓存未命中的 token 数量(可选)
114
- */
115
- prompt_cache_miss_tokens?: number;
116
-
117
- /**
118
- * 总 token 数量
119
- */
120
- total_tokens: number;
24
+ export interface DeepseekConfig extends Omit<OpenAICompatibleConfig, 'model' | 'name'> {
25
+ /** Model name. Defaults to `deepseek-chat`. */
26
+ model?: string | undefined;
27
+ /** API base URL. Defaults to `https://api.deepseek.com/v1`. */
28
+ baseURL?: string | undefined;
121
29
  }
122
30
 
123
31
  /**
124
- * 对话消息类型
125
- */
126
- export interface Message {
127
- /**
128
- * 消息角色
129
- */
130
- role: MessageRole;
131
-
132
- /**
133
- * 消息内容
134
- */
135
- content?: string;
136
-
137
- /**
138
- * 工具调用
139
- */
140
- tool_calls?: ToolCall[];
141
-
142
- /**
143
- * 工具调用 ID,用于工具响应消息
144
- */
145
- tool_call_id?: string;
146
-
147
- /**
148
- * Token 使用统计(可选)
149
- */
150
- usage?: TokenUsage;
151
- }
152
-
153
- /**
154
- * Deepseek 组件
155
- *
156
- * 使用 OpenAI 兼容接口调用 Deepseek 模型
32
+ * DeepSeek model provider: `OpenAICompatible` set up for the DeepSeek API.
157
33
  *
158
- * 输入:
159
- * - prompt: 文本提示
160
- * - messages: 对话消息数组
161
- *
162
- * 输出:
163
- * - completion: 生成的文本完成
164
- * - message: 生成的响应消息
34
+ * Ports:
35
+ * - `prompt` (in): a text prompt. The answer is sent on `completion`.
36
+ * - `messages` (in): a list of chat messages. The reply is sent on `message`.
165
37
  */
166
- class Deepseek extends Component {
167
- // OpenAI 客户端实例
168
- private client: OpenAI;
169
-
170
- // 配置选项
171
- private model: string;
172
- private temperature: number;
173
- private maxTokens?: number;
174
- private topP?: number;
175
- private systemPrompt?: string;
176
- private tools?: unknown[];
177
-
38
+ class Deepseek extends OpenAICompatible {
178
39
  constructor(config: DeepseekConfig) {
179
- super({});
180
-
181
- // 配置客户端
182
- this.client = new OpenAI({
183
- apiKey: config.apiKey,
184
- baseURL: config.baseURL || 'https://api.deepseek.com/v1',
185
- dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false,
186
- });
187
-
188
- // 保存基本配置
189
- this.model = config.model || 'deepseek-chat';
190
- this.temperature = config.temperature ?? 0.7;
191
- this.maxTokens = config.maxTokens;
192
- this.topP = config.topP;
193
- this.systemPrompt = config.systemPrompt;
194
-
195
- // 处理工具配置,优先使用 rawTools
196
- if (config.rawTools && config.rawTools.length > 0) {
197
- // 将工具实例转换为 API 格式
198
- this.tools = config.rawTools.map(tool => ({
199
- type: 'function',
200
- function: {
201
- name: tool.name,
202
- description: tool.description,
203
- parameters: tool.parameters || {},
204
- },
205
- }));
206
- } else {
207
- // 使用直接提供的 API 格式工具
208
- this.tools = config.tools;
209
- }
210
-
211
- // 重命名端口
212
- Component.Port.I('prompt').attach(this);
213
- Component.Port.I('messages').attach(this);
214
- Component.Port.O('completion').attach(this);
215
- Component.Port.O('message').attach(this);
216
- }
217
-
218
- /**
219
- * 生成文本完成
220
- * @param prompt 提示文本
221
- * @returns 生成的文本
222
- */
223
- async generateCompletion(prompt: string): Promise<string> {
224
- const messages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];
225
-
226
- // 添加系统提示
227
- if (this.systemPrompt) {
228
- messages.push({
229
- role: 'system',
230
- content: this.systemPrompt,
231
- });
232
- }
233
-
234
- // 添加用户提示
235
- messages.push({
236
- role: 'user',
237
- content: prompt,
238
- });
239
-
240
- // 创建请求参数
241
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
242
- const requestParams: any = {
243
- model: this.model,
244
- messages,
245
- temperature: this.temperature,
246
- max_tokens: this.maxTokens,
247
- top_p: this.topP,
248
- };
249
-
250
- // 如果有工具定义,添加到请求中
251
- if (this.tools && this.tools.length > 0) {
252
- requestParams.tools = this.tools;
253
- }
254
-
255
- // 调用 API
256
- const response = await this.client.chat.completions.create(requestParams);
257
-
258
- // 处理工具调用结果
259
- if (
260
- response.choices[0].message.tool_calls &&
261
- response.choices[0].message.tool_calls.length > 0
262
- ) {
263
- return JSON.stringify(response.choices[0].message);
264
- }
265
-
266
- return response.choices[0].message.content || '';
267
- }
268
-
269
- /**
270
- * 流式处理对话消息
271
- * @param messages 对话消息数组
272
- * @param options 可选的调用选项,包含临时工具列表
273
- * @returns 生成的流式响应消息异步生成器
274
- */
275
- async *streamChatCompletion(
276
- messages: Message[],
277
- options?: { temporaryTools?: unknown[] }
278
- ): AsyncGenerator<Partial<Message>> {
279
- // 转换消息格式 (复用现有逻辑)
280
- const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];
281
-
282
- // 添加系统提示
283
- if (this.systemPrompt) {
284
- formattedMessages.push({
285
- role: 'system',
286
- content: this.systemPrompt,
287
- });
288
- }
289
-
290
- // 添加用户提供的消息
291
- formattedMessages.push(
292
- ...messages.map(msg => {
293
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
294
- const formattedMsg: any = {
295
- role: msg.role,
296
- content: msg.content,
297
- };
298
-
299
- if (msg.role === 'tool' && msg.tool_call_id) {
300
- formattedMsg.tool_call_id = msg.tool_call_id;
301
- }
302
-
303
- return formattedMsg;
304
- })
305
- );
306
-
307
- // 创建请求参数
308
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
309
- const requestParams: any = {
310
- model: this.model,
311
- messages: formattedMessages,
312
- temperature: this.temperature,
313
- max_tokens: this.maxTokens,
314
- top_p: this.topP,
315
- stream: true, // 开启流式模式
316
- };
317
-
318
- // 决定使用哪些工具
319
- const toolsToUse = options?.temporaryTools || this.tools;
320
-
321
- // 如果有工具定义,添加到请求中
322
- if (toolsToUse && toolsToUse.length > 0) {
323
- const formattedTools = toolsToUse.map(tool => {
324
- type ToolType = {
325
- type?: string;
326
- name?: string;
327
- description?: string;
328
- parameters?: Record<string, unknown>;
329
- };
330
-
331
- const typedTool = tool as ToolType;
332
-
333
- if (typedTool.type === 'function') {
334
- return tool;
335
- }
336
-
337
- return {
338
- type: 'function',
339
- function: {
340
- name: typedTool.name,
341
- description: typedTool.description,
342
- parameters: typedTool.parameters || {},
343
- },
344
- };
345
- });
346
-
347
- requestParams.tools = formattedTools;
348
- }
349
-
350
- // 调用流式 API
351
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
352
- const stream = (await this.client.chat.completions.create(requestParams)) as any;
353
-
354
- // 处理流式响应
355
- for await (const chunk of stream) {
356
- const delta = chunk.choices[0]?.delta;
357
-
358
- if (delta?.content) {
359
- // 流式文本内容
360
- yield {
361
- role: 'assistant',
362
- content: delta.content,
363
- };
364
- }
365
-
366
- if (delta?.tool_calls) {
367
- // 流式工具调用(如果需要)
368
- yield {
369
- role: 'assistant',
370
- content: '',
371
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
372
- tool_calls: delta.tool_calls.map((toolCall: any) => ({
373
- id: toolCall.id || '',
374
- function: {
375
- name: toolCall.function?.name || '',
376
- arguments: toolCall.function?.arguments || '',
377
- },
378
- type: 'function',
379
- tool_name: toolCall.function?.name || '',
380
- arguments: toolCall.function?.arguments || '',
381
- })),
382
- };
383
- }
384
-
385
- // 提取并返回 token 使用统计信息
386
- if (chunk.usage) {
387
- yield {
388
- role: 'assistant',
389
- content: '',
390
- usage: {
391
- completion_tokens: chunk.usage.completion_tokens || 0,
392
- prompt_tokens: chunk.usage.prompt_tokens || 0,
393
- prompt_cache_hit_tokens: chunk.usage.prompt_cache_hit_tokens,
394
- prompt_cache_miss_tokens: chunk.usage.prompt_cache_miss_tokens,
395
- total_tokens: chunk.usage.total_tokens || 0,
396
- },
397
- };
398
- }
399
- }
400
- }
401
-
402
- /**
403
- * 处理对话消息
404
- * @param messages 对话消息数组
405
- * @param options 可选的调用选项,包含临时工具列表
406
- * @returns 生成的响应消息
407
- */
408
- async chatCompletion(
409
- messages: Message[],
410
- options?: { temporaryTools?: unknown[] }
411
- ): Promise<Message> {
412
- // 转换消息格式
413
- const formattedMessages: Array<OpenAI.Chat.ChatCompletionMessageParam> = [];
414
-
415
- // 添加系统提示
416
- if (this.systemPrompt) {
417
- formattedMessages.push({
418
- role: 'system',
419
- content: this.systemPrompt,
420
- });
421
- }
422
-
423
- // 添加用户提供的消息
424
- formattedMessages.push(
425
- ...messages.map(msg => {
426
- // 基本消息结构
427
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
428
- const formattedMsg: any = {
429
- role: msg.role,
430
- content: msg.content,
431
- };
432
-
433
- // 如果有工具调用 ID,添加到消息中
434
- if (msg.role === 'tool' && msg.tool_call_id) {
435
- formattedMsg.tool_call_id = msg.tool_call_id;
436
- }
437
-
438
- return formattedMsg;
439
- })
440
- );
441
-
442
- // 创建请求参数
443
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
444
- const requestParams: any = {
445
- model: this.model,
446
- messages: formattedMessages,
447
- temperature: this.temperature,
448
- max_tokens: this.maxTokens,
449
- top_p: this.topP,
450
- };
451
-
452
- // 决定使用哪些工具
453
- // 如果提供了临时工具,优先使用临时工具
454
- const toolsToUse = options?.temporaryTools || this.tools;
455
-
456
- // 如果有工具定义,添加到请求中
457
- if (toolsToUse && toolsToUse.length > 0) {
458
- // 检查工具格式并转换,确保符合 Deepseek API 要求
459
- const formattedTools = toolsToUse.map(tool => {
460
- // 使用类型断言来处理 unknown 类型
461
- type ToolType = {
462
- type?: string;
463
- name?: string;
464
- description?: string;
465
- parameters?: Record<string, unknown>;
466
- };
467
-
468
- const typedTool = tool as ToolType;
469
-
470
- // 如果工具已经是 API 格式,直接返回
471
- if (typedTool.type === 'function') {
472
- return tool;
473
- }
474
-
475
- // 如果是简单工具实例,转换为 API 格式
476
- return {
477
- type: 'function',
478
- function: {
479
- name: typedTool.name,
480
- description: typedTool.description,
481
- parameters: typedTool.parameters || {},
482
- },
483
- };
484
- });
485
-
486
- requestParams.tools = formattedTools;
487
- }
488
-
489
- // 调用 API
490
- const response = await this.client.chat.completions.create(requestParams);
491
-
492
- // 解析响应
493
- const responseMessage = response.choices[0].message;
494
-
495
- // 转换为内部消息格式
496
- const result: Message = {
497
- role: 'assistant',
498
- content: responseMessage.content || '',
499
- };
500
-
501
- // 提取并添加 token 使用统计信息
502
- if (response.usage) {
503
- const usage = response.usage as typeof response.usage & {
504
- prompt_cache_hit_tokens?: number;
505
- prompt_cache_miss_tokens?: number;
506
- };
507
- result.usage = {
508
- completion_tokens: usage.completion_tokens || 0,
509
- prompt_tokens: usage.prompt_tokens || 0,
510
- prompt_cache_hit_tokens: usage.prompt_cache_hit_tokens,
511
- prompt_cache_miss_tokens: usage.prompt_cache_miss_tokens,
512
- total_tokens: usage.total_tokens || 0,
513
- };
514
- }
515
-
516
- // 如果有工具调用,添加到结果中
517
- if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {
518
- // 为了兼容 Agent 组件期望的格式,保留 function 和 type 字段
519
- // 同时为了满足内部 ToolCall 接口,也添加 tool_name 和 arguments 字段
520
- result.tool_calls = responseMessage.tool_calls.map(toolCall => {
521
- // Type guard: only handle function tool calls (not custom tool calls)
522
- if (toolCall.type !== 'function' || !('function' in toolCall)) {
523
- throw new Error(`Unsupported tool call type: ${toolCall.type}`);
524
- }
525
-
526
- // 解析参数,确保是对象形式
527
- let args: Record<string, unknown>;
528
-
529
- const rawArgs = toolCall.function.arguments;
530
-
531
- if (typeof rawArgs === 'string') {
532
- const raw = rawArgs;
533
- try {
534
- args = raw && raw.trim().length > 0 ? (JSON.parse(raw) as Record<string, unknown>) : {};
535
- } catch {
536
- args = {};
537
- }
538
- } else if (rawArgs && typeof rawArgs === 'object') {
539
- args = rawArgs as Record<string, unknown>;
540
- } else {
541
- args = {};
542
- }
543
-
544
- return {
545
- id: toolCall.id,
546
- // 满足 Agent 组件期望的格式
547
- function: {
548
- name: toolCall.function.name,
549
- arguments: toolCall.function.arguments || '{}',
550
- },
551
- type: 'function',
552
- // 满足内部 ToolCall 接口
553
- tool_name: toolCall.function.name,
554
- arguments: args,
555
- };
556
- });
557
- }
558
-
559
- return result;
560
- }
561
-
562
- /**
563
- * 在独立模式下运行组件
564
- * @param input 输入参数,可以是提示文本或对话消息数组
565
- * @returns 生成的文本或响应消息
566
- */
567
- async run(input: string | Message[]): Promise<string | Message> {
568
- if (typeof input === 'string') {
569
- return this.generateCompletion(input);
570
- } else {
571
- return this.chatCompletion(input);
572
- }
573
- }
574
-
575
- /**
576
- * 在流水线中运行组件
577
- * @param $i 输入端口
578
- * @param $o 输出端口
579
- */
580
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
581
- _transform($i: any, $o: any) {
582
- // 处理文本提示输入
583
- $i('prompt').receive(async (prompt: string) => {
584
- try {
585
- const completion = await this.generateCompletion(prompt);
586
- $o('completion').send(completion);
587
- } catch (error) {
588
- console.error('Deepseek API 调用错误:', error);
589
- $o('completion').send('API 调用错误');
590
- }
591
- });
592
-
593
- // 处理对话消息输入
594
- $i('messages').receive(async (messages: Message[]) => {
595
- try {
596
- const responseMessage = await this.chatCompletion(messages);
597
- $o('message').send(responseMessage);
598
- } catch (error) {
599
- console.error('Deepseek API 调用错误:', error);
600
- $o('message').send({
601
- role: 'assistant',
602
- content: 'API 调用错误',
603
- });
604
- }
40
+ super({
41
+ ...config,
42
+ model: config.model ?? DEFAULT_MODEL,
43
+ baseURL: config.baseURL ?? DEFAULT_BASE_URL,
44
+ name: 'DeepSeek',
605
45
  });
606
46
  }
607
47
  }
608
48
 
609
- // 默认导出组件
610
49
  export default Deepseek;
@@ -1,7 +1,28 @@
1
1
  export {
2
2
  default as Deepseek,
3
- type Message as DeepseekMessage,
4
- type ToolCall as DeepseekToolCall,
3
+ type AssistantMessage as DeepseekAssistantMessage,
4
+ type ChatCompletionOptions as DeepseekChatCompletionOptions,
5
5
  type DeepseekConfig,
6
+ type Message as DeepseekMessage,
6
7
  type MessageRole as DeepseekMessageRole,
8
+ type RawTool as DeepseekRawTool,
9
+ type ResponseToolCall as DeepseekResponseToolCall,
10
+ type StreamChunk as DeepseekStreamChunk,
11
+ type StreamToolCall as DeepseekStreamToolCall,
12
+ type TokenUsage as DeepseekTokenUsage,
13
+ type ToolCall as DeepseekToolCall,
7
14
  } from './deepseek';
15
+ export {
16
+ default as OpenAICompatible,
17
+ type AssistantMessage,
18
+ type ChatCompletionOptions,
19
+ type Message,
20
+ type MessageRole,
21
+ type OpenAICompatibleConfig,
22
+ type RawTool,
23
+ type ResponseToolCall,
24
+ type StreamChunk,
25
+ type StreamToolCall,
26
+ type TokenUsage,
27
+ type ToolCall,
28
+ } from './openai-compatible';