@astack-tech/integrations 0.1.1-beta.6 → 0.2.0-beta.11
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/CHANGELOG.md +71 -0
- package/README.md +98 -1
- package/dist/index.cjs +252 -245
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +252 -245
- package/dist/index.js.map +1 -1
- package/dist/model-provider/index.cjs +254 -246
- package/dist/model-provider/index.cjs.map +1 -1
- package/dist/model-provider/index.d.cts +226 -146
- package/dist/model-provider/index.d.ts +226 -146
- package/dist/model-provider/index.js +252 -245
- package/dist/model-provider/index.js.map +1 -1
- package/package.json +47 -26
- package/src/index.ts +5 -3
- package/src/model-provider/deepseek/index.ts +35 -596
- package/src/model-provider/index.ts +23 -2
- package/src/model-provider/openai-compatible/index.ts +573 -0
|
@@ -1,195 +1,275 @@
|
|
|
1
|
-
import { Component } from '@astack-tech/core';
|
|
1
|
+
import { Component, Port } from '@astack-tech/core';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
4
|
+
* Options for a provider that speaks the OpenAI chat completions API.
|
|
5
5
|
*/
|
|
6
|
-
interface
|
|
7
|
-
/**
|
|
8
|
-
* API 密钥
|
|
9
|
-
*/
|
|
6
|
+
interface OpenAICompatibleConfig {
|
|
7
|
+
/** API key of the provider. */
|
|
10
8
|
apiKey: string;
|
|
9
|
+
/** Model name, for example `gpt-4o-mini`, `qwen-plus` or `llama3.1`. */
|
|
10
|
+
model: string;
|
|
11
|
+
/** API base URL. Defaults to `https://api.openai.com/v1`. */
|
|
12
|
+
baseURL?: string | undefined;
|
|
13
|
+
/** Provider name used in errors and logs. Defaults to `The model API`. */
|
|
14
|
+
name?: string | undefined;
|
|
15
|
+
/** Sampling temperature. Defaults to `0.7`. */
|
|
16
|
+
temperature?: number | undefined;
|
|
17
|
+
/** Maximum number of generated tokens. */
|
|
18
|
+
maxTokens?: number | undefined;
|
|
19
|
+
/** Nucleus sampling probability. */
|
|
20
|
+
topP?: number | undefined;
|
|
21
|
+
/** System prompt added before every request. */
|
|
22
|
+
systemPrompt?: string | undefined;
|
|
11
23
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
*/
|
|
15
|
-
model?: string;
|
|
16
|
-
/**
|
|
17
|
-
* 自定义 API 基础 URL
|
|
18
|
-
* 默认为 Deepseek 官方 API 地址
|
|
19
|
-
*/
|
|
20
|
-
baseURL?: string;
|
|
21
|
-
/**
|
|
22
|
-
* 温度参数,控制输出的随机性
|
|
24
|
+
* Tools given as `{ name, description, parameters }` objects.
|
|
25
|
+
* When this list is not empty, it takes precedence over `tools`.
|
|
23
26
|
*/
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
name: string;
|
|
43
|
-
description: string;
|
|
44
|
-
parameters?: Record<string, unknown>;
|
|
45
|
-
}>;
|
|
46
|
-
/**
|
|
47
|
-
* API 格式的工具定义数组,与 rawTools 二选一
|
|
48
|
-
* 使用 OpenAI 兼容的工具定义格式
|
|
49
|
-
*/
|
|
50
|
-
tools?: unknown[];
|
|
51
|
-
/**
|
|
52
|
-
* dangerouslyAllowBrowser
|
|
53
|
-
* 是否允许在浏览器等非安全环境内运行
|
|
54
|
-
*/
|
|
55
|
-
dangerouslyAllowBrowser?: boolean;
|
|
27
|
+
rawTools?: RawTool[] | undefined;
|
|
28
|
+
/** Tools in the OpenAI-compatible `{ type: 'function', function }` format. */
|
|
29
|
+
tools?: unknown[] | undefined;
|
|
30
|
+
/** Allows the OpenAI client to run in a browser. Defaults to `false`. */
|
|
31
|
+
dangerouslyAllowBrowser?: boolean | undefined;
|
|
32
|
+
/** Logs API errors to the console. Defaults to `false`. */
|
|
33
|
+
verbose?: boolean | undefined;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Tool description accepted by `rawTools` and `temporaryTools`.
|
|
37
|
+
*/
|
|
38
|
+
interface RawTool {
|
|
39
|
+
/** Unique tool name. */
|
|
40
|
+
name: string;
|
|
41
|
+
/** Description that tells the model when to use the tool. */
|
|
42
|
+
description: string;
|
|
43
|
+
/** JSON Schema of the arguments. Defaults to `{}`. */
|
|
44
|
+
parameters?: Record<string, unknown> | undefined;
|
|
56
45
|
}
|
|
57
46
|
/**
|
|
58
|
-
*
|
|
47
|
+
* Role of a chat message.
|
|
59
48
|
*/
|
|
60
49
|
type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
61
50
|
/**
|
|
62
|
-
*
|
|
51
|
+
* Tool call in the internal format.
|
|
63
52
|
*/
|
|
64
53
|
interface ToolCall {
|
|
65
|
-
/**
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
id?: string;
|
|
69
|
-
/**
|
|
70
|
-
* 工具名称
|
|
71
|
-
*/
|
|
54
|
+
/** Identifier assigned by the API. */
|
|
55
|
+
id?: string | undefined;
|
|
56
|
+
/** Name of the tool. */
|
|
72
57
|
tool_name: string;
|
|
73
|
-
/**
|
|
74
|
-
* 工具调用参数
|
|
75
|
-
*/
|
|
58
|
+
/** Parsed arguments. */
|
|
76
59
|
arguments: Record<string, unknown>;
|
|
77
60
|
}
|
|
78
61
|
/**
|
|
79
|
-
*
|
|
62
|
+
* Tool call returned by `chatCompletion`. It carries both the OpenAI shape
|
|
63
|
+
* (`type`, `function`) and the internal shape (`tool_name`, `arguments`).
|
|
64
|
+
*/
|
|
65
|
+
interface ResponseToolCall {
|
|
66
|
+
/** Identifier assigned by the API. A `tool` message answers the call with it. */
|
|
67
|
+
id: string;
|
|
68
|
+
/** Always `function`. */
|
|
69
|
+
type: 'function';
|
|
70
|
+
/** The call in the OpenAI shape: function name and raw JSON arguments. An empty argument string is sent as `{}`. */
|
|
71
|
+
function: {
|
|
72
|
+
/** Name of the tool to call. */
|
|
73
|
+
name: string;
|
|
74
|
+
/** Arguments as JSON text. */
|
|
75
|
+
arguments: string;
|
|
76
|
+
};
|
|
77
|
+
/** Name of the tool, same as `function.name`. */
|
|
78
|
+
tool_name: string;
|
|
79
|
+
/** Parsed arguments. JSON that is not an object becomes `{}`. */
|
|
80
|
+
arguments: Record<string, unknown>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Partial tool call emitted while streaming. `arguments` holds the raw JSON fragment.
|
|
84
|
+
*/
|
|
85
|
+
interface StreamToolCall {
|
|
86
|
+
/** Identifier assigned by the API. Only the first delta of a call carries it; later deltas have `''`. */
|
|
87
|
+
id: string;
|
|
88
|
+
/** Always `function`. */
|
|
89
|
+
type: 'function';
|
|
90
|
+
/** The delta in the OpenAI shape. `arguments` holds only this delta's JSON fragment. */
|
|
91
|
+
function: {
|
|
92
|
+
/** Name of the tool to call. */
|
|
93
|
+
name: string;
|
|
94
|
+
/** Arguments as JSON text. */
|
|
95
|
+
arguments: string;
|
|
96
|
+
};
|
|
97
|
+
/** Name of the tool when this delta carries it, otherwise `''`. */
|
|
98
|
+
tool_name: string;
|
|
99
|
+
/** Raw JSON fragment of this delta. Concatenate the fragments of a call to get its arguments. */
|
|
100
|
+
arguments: string;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Token usage reported by the API.
|
|
80
104
|
*/
|
|
81
105
|
interface TokenUsage {
|
|
82
|
-
/**
|
|
83
|
-
* 完成部分的 token 数量
|
|
84
|
-
*/
|
|
106
|
+
/** Tokens in the generated reply. */
|
|
85
107
|
completion_tokens: number;
|
|
86
|
-
/**
|
|
87
|
-
* 提示部分的 token 数量
|
|
88
|
-
*/
|
|
108
|
+
/** Tokens in the request. */
|
|
89
109
|
prompt_tokens: number;
|
|
90
|
-
/**
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* 提示缓存未命中的 token 数量(可选)
|
|
96
|
-
*/
|
|
97
|
-
prompt_cache_miss_tokens?: number;
|
|
98
|
-
/**
|
|
99
|
-
* 总 token 数量
|
|
100
|
-
*/
|
|
110
|
+
/** Prompt tokens served from the provider's context cache (reported by DeepSeek). */
|
|
111
|
+
prompt_cache_hit_tokens?: number | undefined;
|
|
112
|
+
/** Prompt tokens not served from the cache. */
|
|
113
|
+
prompt_cache_miss_tokens?: number | undefined;
|
|
114
|
+
/** Sum of prompt and completion tokens. */
|
|
101
115
|
total_tokens: number;
|
|
102
116
|
}
|
|
103
117
|
/**
|
|
104
|
-
*
|
|
118
|
+
* Chat message accepted as input. It matches the agent message contract in
|
|
119
|
+
* `@astack-tech/components`, so agent histories can be passed directly.
|
|
105
120
|
*/
|
|
106
121
|
interface Message {
|
|
107
|
-
/**
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
|
|
117
|
-
*/
|
|
118
|
-
tool_calls?: ToolCall[];
|
|
119
|
-
/**
|
|
120
|
-
* 工具调用 ID,用于工具响应消息
|
|
121
|
-
*/
|
|
122
|
-
tool_call_id?: string;
|
|
123
|
-
/**
|
|
124
|
-
* Token 使用统计(可选)
|
|
125
|
-
*/
|
|
126
|
-
usage?: TokenUsage;
|
|
122
|
+
/** One of `system`, `user`, `assistant` or `tool` (see `MessageRole`). */
|
|
123
|
+
role: string;
|
|
124
|
+
/** Text content. Omitted content is sent as no content. */
|
|
125
|
+
content?: string | undefined;
|
|
126
|
+
/** Accepted for compatibility. Assistant tool calls are not forwarded to the API. */
|
|
127
|
+
tool_calls?: readonly unknown[] | undefined;
|
|
128
|
+
/** Identifier of the call that a `tool` message answers. */
|
|
129
|
+
tool_call_id?: string | undefined;
|
|
130
|
+
/** Ignored on input. Present so agent histories can be passed unchanged. */
|
|
131
|
+
usage?: TokenUsage | undefined;
|
|
127
132
|
}
|
|
128
133
|
/**
|
|
129
|
-
*
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
134
|
+
* Assistant message returned by `chatCompletion`.
|
|
135
|
+
*/
|
|
136
|
+
interface AssistantMessage {
|
|
137
|
+
/** Always `assistant`. */
|
|
138
|
+
role: 'assistant';
|
|
139
|
+
/** Reply text. Empty when the model only calls tools. */
|
|
140
|
+
content: string;
|
|
141
|
+
/** Tool calls requested by the model, if any. */
|
|
142
|
+
tool_calls?: ResponseToolCall[] | undefined;
|
|
143
|
+
/** Token usage of this request, when the API reports it. */
|
|
144
|
+
usage?: TokenUsage | undefined;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Chunk yielded by `streamChatCompletion`.
|
|
148
|
+
*/
|
|
149
|
+
interface StreamChunk {
|
|
150
|
+
/** Always `assistant`. */
|
|
151
|
+
role: 'assistant';
|
|
152
|
+
/** Text delta. Empty in chunks that carry tool calls or usage. */
|
|
153
|
+
content: string;
|
|
154
|
+
/** Tool-call deltas in this chunk. */
|
|
155
|
+
tool_calls?: StreamToolCall[] | undefined;
|
|
156
|
+
/** Token usage. Sent once, in the final chunk, when the API reports it. */
|
|
157
|
+
usage?: TokenUsage | undefined;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Per-call options.
|
|
161
|
+
*/
|
|
162
|
+
interface ChatCompletionOptions {
|
|
163
|
+
/** Tools for this call only. They replace the configured tools. */
|
|
164
|
+
temporaryTools?: unknown[] | undefined;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Model provider for any API that speaks the OpenAI chat completions protocol:
|
|
168
|
+
* OpenAI itself, and the compatible endpoints of DeepSeek, Qwen, Moonshot, Ollama,
|
|
169
|
+
* vLLM and others. Set `baseURL` and `model` for the provider you use.
|
|
136
170
|
*
|
|
137
|
-
*
|
|
138
|
-
*
|
|
139
|
-
*
|
|
171
|
+
* Ports:
|
|
172
|
+
* - `prompt` (in): a text prompt. The answer is sent on `completion`.
|
|
173
|
+
* - `messages` (in): a list of chat messages. The reply is sent on `message`.
|
|
140
174
|
*/
|
|
141
|
-
declare class
|
|
142
|
-
private client;
|
|
143
|
-
private model;
|
|
144
|
-
private temperature;
|
|
145
|
-
private maxTokens
|
|
146
|
-
private topP
|
|
147
|
-
private systemPrompt
|
|
148
|
-
private tools
|
|
149
|
-
|
|
175
|
+
declare class OpenAICompatible extends Component {
|
|
176
|
+
private readonly client;
|
|
177
|
+
private readonly model;
|
|
178
|
+
private readonly temperature;
|
|
179
|
+
private readonly maxTokens;
|
|
180
|
+
private readonly topP;
|
|
181
|
+
private readonly systemPrompt;
|
|
182
|
+
private readonly tools;
|
|
183
|
+
private readonly verbose;
|
|
184
|
+
private readonly name;
|
|
185
|
+
constructor(config: OpenAICompatibleConfig);
|
|
150
186
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* @returns
|
|
187
|
+
* Generates a completion for a single prompt.
|
|
188
|
+
*
|
|
189
|
+
* @returns The text answer, or the JSON-encoded assistant message when it contains tool calls.
|
|
154
190
|
*/
|
|
155
191
|
generateCompletion(prompt: string): Promise<string>;
|
|
156
192
|
/**
|
|
157
|
-
*
|
|
158
|
-
* @param messages 对话消息数组
|
|
159
|
-
* @param options 可选的调用选项,包含临时工具列表
|
|
160
|
-
* @returns 生成的流式响应消息异步生成器
|
|
193
|
+
* Sends a conversation and returns the assistant reply.
|
|
161
194
|
*/
|
|
162
|
-
|
|
163
|
-
temporaryTools?: unknown[];
|
|
164
|
-
}): AsyncGenerator<Partial<Message>>;
|
|
195
|
+
chatCompletion(messages: Message[], options?: ChatCompletionOptions): Promise<AssistantMessage>;
|
|
165
196
|
/**
|
|
166
|
-
*
|
|
167
|
-
* @param messages 对话消息数组
|
|
168
|
-
* @param options 可选的调用选项,包含临时工具列表
|
|
169
|
-
* @returns 生成的响应消息
|
|
197
|
+
* Streams the assistant reply. Each chunk holds a text delta, tool-call deltas, or usage.
|
|
170
198
|
*/
|
|
171
|
-
|
|
172
|
-
temporaryTools?: unknown[];
|
|
173
|
-
}): Promise<Message>;
|
|
199
|
+
streamChatCompletion(messages: Message[], options?: ChatCompletionOptions): AsyncGenerator<StreamChunk>;
|
|
174
200
|
/**
|
|
175
|
-
*
|
|
176
|
-
*
|
|
177
|
-
* @
|
|
201
|
+
* Runs the model outside a pipeline.
|
|
202
|
+
*
|
|
203
|
+
* @param input - A prompt string or a list of messages.
|
|
178
204
|
*/
|
|
179
|
-
run(input: string | Message[]): Promise<string |
|
|
205
|
+
run(input: string | Message[]): Promise<string | AssistantMessage>;
|
|
180
206
|
/**
|
|
181
|
-
*
|
|
182
|
-
*
|
|
183
|
-
*
|
|
207
|
+
* Connects the `prompt` and `messages` input ports to the model.
|
|
208
|
+
* API errors produce a fallback value so the pipeline keeps flowing. Input of the wrong
|
|
209
|
+
* type fails the pipeline run with a `TypeError` before the API is called.
|
|
184
210
|
*/
|
|
185
|
-
_transform($i:
|
|
211
|
+
_transform($i: Port, $o: Port): void;
|
|
212
|
+
private baseRequest;
|
|
213
|
+
private toolsParam;
|
|
214
|
+
private systemMessages;
|
|
215
|
+
/**
|
|
216
|
+
* Converts input messages to API messages. Assistant `tool_calls` are not forwarded:
|
|
217
|
+
* agents send tool results back as user messages, and the API rejects assistant tool
|
|
218
|
+
* calls that are not followed by tool messages.
|
|
219
|
+
*/
|
|
220
|
+
private formatMessages;
|
|
221
|
+
/**
|
|
222
|
+
* Converts one input message to its API shape.
|
|
223
|
+
*
|
|
224
|
+
* @throws Error when the role is not one of `MessageRole`, or a `tool` message has no
|
|
225
|
+
* `tool_call_id`. The API would reject both.
|
|
226
|
+
*/
|
|
227
|
+
private toApiMessage;
|
|
228
|
+
private firstMessage;
|
|
229
|
+
private logError;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* Options for the DeepSeek model provider. They are the options of
|
|
234
|
+
* `OpenAICompatible`, with DeepSeek's endpoint and model as defaults.
|
|
235
|
+
*/
|
|
236
|
+
interface DeepseekConfig extends Omit<OpenAICompatibleConfig, 'model' | 'name'> {
|
|
237
|
+
/** Model name. Defaults to `deepseek-chat`. */
|
|
238
|
+
model?: string | undefined;
|
|
239
|
+
/** API base URL. Defaults to `https://api.deepseek.com/v1`. */
|
|
240
|
+
baseURL?: string | undefined;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* DeepSeek model provider: `OpenAICompatible` set up for the DeepSeek API.
|
|
244
|
+
*
|
|
245
|
+
* Ports:
|
|
246
|
+
* - `prompt` (in): a text prompt. The answer is sent on `completion`.
|
|
247
|
+
* - `messages` (in): a list of chat messages. The reply is sent on `message`.
|
|
248
|
+
*/
|
|
249
|
+
declare class Deepseek extends OpenAICompatible {
|
|
250
|
+
constructor(config: DeepseekConfig);
|
|
186
251
|
}
|
|
187
252
|
|
|
253
|
+
type index_AssistantMessage = AssistantMessage;
|
|
254
|
+
type index_ChatCompletionOptions = ChatCompletionOptions;
|
|
188
255
|
type index_Deepseek = Deepseek;
|
|
189
256
|
declare const index_Deepseek: typeof Deepseek;
|
|
190
257
|
type index_DeepseekConfig = DeepseekConfig;
|
|
258
|
+
type index_Message = Message;
|
|
259
|
+
type index_MessageRole = MessageRole;
|
|
260
|
+
type index_OpenAICompatible = OpenAICompatible;
|
|
261
|
+
declare const index_OpenAICompatible: typeof OpenAICompatible;
|
|
262
|
+
type index_OpenAICompatibleConfig = OpenAICompatibleConfig;
|
|
263
|
+
type index_RawTool = RawTool;
|
|
264
|
+
type index_ResponseToolCall = ResponseToolCall;
|
|
265
|
+
type index_StreamChunk = StreamChunk;
|
|
266
|
+
type index_StreamToolCall = StreamToolCall;
|
|
267
|
+
type index_TokenUsage = TokenUsage;
|
|
268
|
+
type index_ToolCall = ToolCall;
|
|
191
269
|
declare namespace index {
|
|
192
|
-
export { index_Deepseek as Deepseek,
|
|
270
|
+
export { index_Deepseek as Deepseek, index_OpenAICompatible as OpenAICompatible };
|
|
271
|
+
export type { index_AssistantMessage as AssistantMessage, index_ChatCompletionOptions as ChatCompletionOptions, AssistantMessage as DeepseekAssistantMessage, ChatCompletionOptions as DeepseekChatCompletionOptions, index_DeepseekConfig as DeepseekConfig, Message as DeepseekMessage, MessageRole as DeepseekMessageRole, RawTool as DeepseekRawTool, ResponseToolCall as DeepseekResponseToolCall, StreamChunk as DeepseekStreamChunk, StreamToolCall as DeepseekStreamToolCall, TokenUsage as DeepseekTokenUsage, ToolCall as DeepseekToolCall, index_Message as Message, index_MessageRole as MessageRole, index_OpenAICompatibleConfig as OpenAICompatibleConfig, index_RawTool as RawTool, index_ResponseToolCall as ResponseToolCall, index_StreamChunk as StreamChunk, index_StreamToolCall as StreamToolCall, index_TokenUsage as TokenUsage, index_ToolCall as ToolCall };
|
|
193
272
|
}
|
|
194
273
|
|
|
195
|
-
export { Deepseek,
|
|
274
|
+
export { Deepseek, OpenAICompatible, index as i };
|
|
275
|
+
export type { AssistantMessage, ChatCompletionOptions, AssistantMessage as DeepseekAssistantMessage, ChatCompletionOptions as DeepseekChatCompletionOptions, DeepseekConfig, Message as DeepseekMessage, MessageRole as DeepseekMessageRole, RawTool as DeepseekRawTool, ResponseToolCall as DeepseekResponseToolCall, StreamChunk as DeepseekStreamChunk, StreamToolCall as DeepseekStreamToolCall, TokenUsage as DeepseekTokenUsage, ToolCall as DeepseekToolCall, Message, MessageRole, OpenAICompatibleConfig, RawTool, ResponseToolCall, StreamChunk, StreamToolCall, TokenUsage, ToolCall };
|