@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
package/dist/index.js
CHANGED
|
@@ -7,310 +7,317 @@ var __export = (target, all) => {
|
|
|
7
7
|
// src/model-provider/index.ts
|
|
8
8
|
var model_provider_exports = {};
|
|
9
9
|
__export(model_provider_exports, {
|
|
10
|
-
Deepseek: () => deepseek_default
|
|
10
|
+
Deepseek: () => deepseek_default,
|
|
11
|
+
OpenAICompatible: () => openai_compatible_default
|
|
11
12
|
});
|
|
12
13
|
|
|
13
|
-
// src/model-provider/
|
|
14
|
+
// src/model-provider/openai-compatible/index.ts
|
|
14
15
|
import { Component } from "@astack-tech/core";
|
|
15
16
|
import OpenAI from "openai";
|
|
16
|
-
var
|
|
17
|
+
var DEFAULT_BASE_URL = "https://api.openai.com/v1";
|
|
18
|
+
var DEFAULT_NAME = "The model API";
|
|
19
|
+
var DEFAULT_TEMPERATURE = 0.7;
|
|
20
|
+
var FALLBACK_TEXT = "API call failed";
|
|
21
|
+
function isRecord(value) {
|
|
22
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
23
|
+
}
|
|
24
|
+
function isOptionalString(value) {
|
|
25
|
+
return value === void 0 || typeof value === "string";
|
|
26
|
+
}
|
|
27
|
+
function isCount(value) {
|
|
28
|
+
return typeof value === "number" && Number.isFinite(value);
|
|
29
|
+
}
|
|
30
|
+
function isTokenUsage(value) {
|
|
31
|
+
return isRecord(value) && isCount(value["completion_tokens"]) && isCount(value["prompt_tokens"]) && isCount(value["total_tokens"]) && (value["prompt_cache_hit_tokens"] === void 0 || isCount(value["prompt_cache_hit_tokens"])) && (value["prompt_cache_miss_tokens"] === void 0 || isCount(value["prompt_cache_miss_tokens"]));
|
|
32
|
+
}
|
|
33
|
+
function isMessage(value) {
|
|
34
|
+
return isRecord(value) && typeof value["role"] === "string" && isOptionalString(value["content"]) && (value["tool_calls"] === void 0 || Array.isArray(value["tool_calls"])) && isOptionalString(value["tool_call_id"]) && (value["usage"] === void 0 || isTokenUsage(value["usage"]));
|
|
35
|
+
}
|
|
36
|
+
function isMessageList(value) {
|
|
37
|
+
return Array.isArray(value) && value.every((item) => isMessage(item));
|
|
38
|
+
}
|
|
39
|
+
function toApiTool(tool) {
|
|
40
|
+
const raw = isRecord(tool) ? tool : {};
|
|
41
|
+
const definition = raw["function"];
|
|
42
|
+
if (raw["type"] === "function" && isRecord(definition)) {
|
|
43
|
+
const { name: name2, description: description2, parameters: parameters2, strict } = definition;
|
|
44
|
+
return {
|
|
45
|
+
type: "function",
|
|
46
|
+
function: {
|
|
47
|
+
name: typeof name2 === "string" ? name2 : "",
|
|
48
|
+
...typeof description2 === "string" ? { description: description2 } : {},
|
|
49
|
+
...isRecord(parameters2) ? { parameters: parameters2 } : {},
|
|
50
|
+
...typeof strict === "boolean" ? { strict } : {}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
const { name, description, parameters } = raw;
|
|
55
|
+
return {
|
|
56
|
+
type: "function",
|
|
57
|
+
function: {
|
|
58
|
+
name: typeof name === "string" ? name : "",
|
|
59
|
+
description: typeof description === "string" ? description : "",
|
|
60
|
+
parameters: isRecord(parameters) ? parameters : {}
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
function parseArguments(raw) {
|
|
65
|
+
if (raw.trim().length === 0) {
|
|
66
|
+
return {};
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
const parsed = JSON.parse(raw);
|
|
70
|
+
return isRecord(parsed) ? parsed : {};
|
|
71
|
+
} catch {
|
|
72
|
+
return {};
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function mapUsage(usage) {
|
|
76
|
+
return {
|
|
77
|
+
completion_tokens: usage.completion_tokens ?? 0,
|
|
78
|
+
prompt_tokens: usage.prompt_tokens ?? 0,
|
|
79
|
+
total_tokens: usage.total_tokens ?? 0,
|
|
80
|
+
...usage.prompt_cache_hit_tokens === void 0 ? {} : { prompt_cache_hit_tokens: usage.prompt_cache_hit_tokens },
|
|
81
|
+
...usage.prompt_cache_miss_tokens === void 0 ? {} : { prompt_cache_miss_tokens: usage.prompt_cache_miss_tokens }
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
var OpenAICompatible = class extends Component {
|
|
85
|
+
client;
|
|
86
|
+
model;
|
|
87
|
+
temperature;
|
|
88
|
+
maxTokens;
|
|
89
|
+
topP;
|
|
90
|
+
systemPrompt;
|
|
91
|
+
tools;
|
|
92
|
+
verbose;
|
|
93
|
+
name;
|
|
17
94
|
constructor(config) {
|
|
18
95
|
super({});
|
|
19
96
|
this.client = new OpenAI({
|
|
20
97
|
apiKey: config.apiKey,
|
|
21
|
-
baseURL: config.baseURL
|
|
98
|
+
baseURL: config.baseURL ?? DEFAULT_BASE_URL,
|
|
22
99
|
dangerouslyAllowBrowser: config.dangerouslyAllowBrowser ?? false
|
|
23
100
|
});
|
|
24
|
-
this.model = config.model
|
|
25
|
-
this.
|
|
101
|
+
this.model = config.model;
|
|
102
|
+
this.name = config.name ?? DEFAULT_NAME;
|
|
103
|
+
this.temperature = config.temperature ?? DEFAULT_TEMPERATURE;
|
|
26
104
|
this.maxTokens = config.maxTokens;
|
|
27
105
|
this.topP = config.topP;
|
|
28
106
|
this.systemPrompt = config.systemPrompt;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
type: "function",
|
|
32
|
-
function: {
|
|
33
|
-
name: tool.name,
|
|
34
|
-
description: tool.description,
|
|
35
|
-
parameters: tool.parameters || {}
|
|
36
|
-
}
|
|
37
|
-
}));
|
|
38
|
-
} else {
|
|
39
|
-
this.tools = config.tools;
|
|
40
|
-
}
|
|
107
|
+
this.verbose = config.verbose ?? false;
|
|
108
|
+
this.tools = config.rawTools !== void 0 && config.rawTools.length > 0 ? config.rawTools.map(toApiTool) : config.tools;
|
|
41
109
|
Component.Port.I("prompt").attach(this);
|
|
42
110
|
Component.Port.I("messages").attach(this);
|
|
43
111
|
Component.Port.O("completion").attach(this);
|
|
44
112
|
Component.Port.O("message").attach(this);
|
|
45
113
|
}
|
|
46
114
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @returns
|
|
115
|
+
* Generates a completion for a single prompt.
|
|
116
|
+
*
|
|
117
|
+
* @returns The text answer, or the JSON-encoded assistant message when it contains tool calls.
|
|
50
118
|
*/
|
|
51
119
|
async generateCompletion(prompt) {
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
messages.
|
|
55
|
-
|
|
56
|
-
content: this.systemPrompt
|
|
57
|
-
});
|
|
58
|
-
}
|
|
59
|
-
messages.push({
|
|
60
|
-
role: "user",
|
|
61
|
-
content: prompt
|
|
120
|
+
const response = await this.client.chat.completions.create({
|
|
121
|
+
...this.baseRequest(),
|
|
122
|
+
messages: [...this.systemMessages(), { role: "user", content: prompt }],
|
|
123
|
+
...this.toolsParam(this.tools)
|
|
62
124
|
});
|
|
63
|
-
const
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
temperature: this.temperature,
|
|
67
|
-
max_tokens: this.maxTokens,
|
|
68
|
-
top_p: this.topP
|
|
69
|
-
};
|
|
70
|
-
if (this.tools && this.tools.length > 0) {
|
|
71
|
-
requestParams.tools = this.tools;
|
|
72
|
-
}
|
|
73
|
-
const response = await this.client.chat.completions.create(requestParams);
|
|
74
|
-
if (response.choices[0].message.tool_calls && response.choices[0].message.tool_calls.length > 0) {
|
|
75
|
-
return JSON.stringify(response.choices[0].message);
|
|
125
|
+
const message = this.firstMessage(response);
|
|
126
|
+
if (message.tool_calls !== void 0 && message.tool_calls.length > 0) {
|
|
127
|
+
return JSON.stringify(message);
|
|
76
128
|
}
|
|
77
|
-
return
|
|
129
|
+
return message.content ?? "";
|
|
78
130
|
}
|
|
79
131
|
/**
|
|
80
|
-
*
|
|
81
|
-
* @param messages 对话消息数组
|
|
82
|
-
* @param options 可选的调用选项,包含临时工具列表
|
|
83
|
-
* @returns 生成的流式响应消息异步生成器
|
|
132
|
+
* Sends a conversation and returns the assistant reply.
|
|
84
133
|
*/
|
|
85
|
-
async
|
|
86
|
-
const
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
134
|
+
async chatCompletion(messages, options) {
|
|
135
|
+
const response = await this.client.chat.completions.create({
|
|
136
|
+
...this.baseRequest(),
|
|
137
|
+
messages: this.formatMessages(messages),
|
|
138
|
+
...this.toolsParam(options?.temporaryTools ?? this.tools)
|
|
139
|
+
});
|
|
140
|
+
const message = this.firstMessage(response);
|
|
141
|
+
const result = { role: "assistant", content: message.content ?? "" };
|
|
142
|
+
if (response.usage !== void 0) {
|
|
143
|
+
result.usage = mapUsage(response.usage);
|
|
92
144
|
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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;
|
|
145
|
+
if (message.tool_calls !== void 0 && message.tool_calls.length > 0) {
|
|
146
|
+
result.tool_calls = message.tool_calls.map((toolCall) => {
|
|
147
|
+
if (toolCall.type !== "function") {
|
|
148
|
+
throw new Error(`Unsupported tool call type: ${toolCall.type}`);
|
|
120
149
|
}
|
|
150
|
+
const rawArguments = toolCall.function.arguments;
|
|
121
151
|
return {
|
|
152
|
+
id: toolCall.id,
|
|
122
153
|
type: "function",
|
|
123
154
|
function: {
|
|
124
|
-
name:
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
155
|
+
name: toolCall.function.name,
|
|
156
|
+
arguments: rawArguments.length > 0 ? rawArguments : "{}"
|
|
157
|
+
},
|
|
158
|
+
tool_name: toolCall.function.name,
|
|
159
|
+
arguments: parseArguments(rawArguments)
|
|
128
160
|
};
|
|
129
161
|
});
|
|
130
|
-
requestParams.tools = formattedTools;
|
|
131
162
|
}
|
|
132
|
-
|
|
163
|
+
return result;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Streams the assistant reply. Each chunk holds a text delta, tool-call deltas, or usage.
|
|
167
|
+
*/
|
|
168
|
+
async *streamChatCompletion(messages, options) {
|
|
169
|
+
const stream = await this.client.chat.completions.create({
|
|
170
|
+
...this.baseRequest(),
|
|
171
|
+
messages: this.formatMessages(messages),
|
|
172
|
+
...this.toolsParam(options?.temporaryTools ?? this.tools),
|
|
173
|
+
stream: true
|
|
174
|
+
});
|
|
133
175
|
for await (const chunk of stream) {
|
|
134
176
|
const delta = chunk.choices[0]?.delta;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
content: delta.content
|
|
139
|
-
};
|
|
177
|
+
const text = delta?.content ?? "";
|
|
178
|
+
if (text !== "") {
|
|
179
|
+
yield { role: "assistant", content: text };
|
|
140
180
|
}
|
|
141
181
|
if (delta?.tool_calls) {
|
|
142
182
|
yield {
|
|
143
183
|
role: "assistant",
|
|
144
184
|
content: "",
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
})
|
|
185
|
+
tool_calls: delta.tool_calls.map((toolCall) => {
|
|
186
|
+
const name = toolCall.function?.name ?? "";
|
|
187
|
+
const args = toolCall.function?.arguments ?? "";
|
|
188
|
+
return {
|
|
189
|
+
id: toolCall.id ?? "",
|
|
190
|
+
type: "function",
|
|
191
|
+
function: { name, arguments: args },
|
|
192
|
+
tool_name: name,
|
|
193
|
+
arguments: args
|
|
194
|
+
};
|
|
195
|
+
})
|
|
156
196
|
};
|
|
157
197
|
}
|
|
158
198
|
if (chunk.usage) {
|
|
159
|
-
yield {
|
|
160
|
-
role: "assistant",
|
|
161
|
-
content: "",
|
|
162
|
-
usage: {
|
|
163
|
-
completion_tokens: chunk.usage.completion_tokens || 0,
|
|
164
|
-
prompt_tokens: chunk.usage.prompt_tokens || 0,
|
|
165
|
-
prompt_cache_hit_tokens: chunk.usage.prompt_cache_hit_tokens,
|
|
166
|
-
prompt_cache_miss_tokens: chunk.usage.prompt_cache_miss_tokens,
|
|
167
|
-
total_tokens: chunk.usage.total_tokens || 0
|
|
168
|
-
}
|
|
169
|
-
};
|
|
199
|
+
yield { role: "assistant", content: "", usage: mapUsage(chunk.usage) };
|
|
170
200
|
}
|
|
171
201
|
}
|
|
172
202
|
}
|
|
173
203
|
/**
|
|
174
|
-
*
|
|
175
|
-
*
|
|
176
|
-
* @param
|
|
177
|
-
* @returns 生成的响应消息
|
|
204
|
+
* Runs the model outside a pipeline.
|
|
205
|
+
*
|
|
206
|
+
* @param input - A prompt string or a list of messages.
|
|
178
207
|
*/
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
208
|
+
run(input) {
|
|
209
|
+
return typeof input === "string" ? this.generateCompletion(input) : this.chatCompletion(input);
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Connects the `prompt` and `messages` input ports to the model.
|
|
213
|
+
* API errors produce a fallback value so the pipeline keeps flowing. Input of the wrong
|
|
214
|
+
* type fails the pipeline run with a `TypeError` before the API is called.
|
|
215
|
+
*/
|
|
216
|
+
_transform($i, $o) {
|
|
217
|
+
$i("prompt").receive((prompt) => {
|
|
218
|
+
if (typeof prompt !== "string") {
|
|
219
|
+
throw new TypeError(`${this.name}: the prompt port expects a string.`);
|
|
220
|
+
}
|
|
221
|
+
return this.generateCompletion(prompt).then(
|
|
222
|
+
(completion) => {
|
|
223
|
+
$o("completion").send(completion);
|
|
224
|
+
},
|
|
225
|
+
(error) => {
|
|
226
|
+
this.logError(error);
|
|
227
|
+
$o("completion").send(FALLBACK_TEXT);
|
|
195
228
|
}
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
)
|
|
199
|
-
|
|
229
|
+
);
|
|
230
|
+
});
|
|
231
|
+
$i("messages").receive((messages) => {
|
|
232
|
+
if (!isMessageList(messages)) {
|
|
233
|
+
throw new TypeError(`${this.name}: the messages port expects an array of messages.`);
|
|
234
|
+
}
|
|
235
|
+
return this.chatCompletion(messages).then(
|
|
236
|
+
(message) => {
|
|
237
|
+
$o("message").send(message);
|
|
238
|
+
},
|
|
239
|
+
(error) => {
|
|
240
|
+
this.logError(error);
|
|
241
|
+
$o("message").send({ role: "assistant", content: FALLBACK_TEXT });
|
|
242
|
+
}
|
|
243
|
+
);
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
baseRequest() {
|
|
247
|
+
return {
|
|
200
248
|
model: this.model,
|
|
201
|
-
messages: formattedMessages,
|
|
202
249
|
temperature: this.temperature,
|
|
203
|
-
max_tokens: this.maxTokens,
|
|
204
|
-
top_p: this.topP
|
|
250
|
+
...this.maxTokens === void 0 ? {} : { max_tokens: this.maxTokens },
|
|
251
|
+
...this.topP === void 0 ? {} : { top_p: this.topP }
|
|
205
252
|
};
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
return {
|
|
214
|
-
type: "function",
|
|
215
|
-
function: {
|
|
216
|
-
name: typedTool.name,
|
|
217
|
-
description: typedTool.description,
|
|
218
|
-
parameters: typedTool.parameters || {}
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
});
|
|
222
|
-
requestParams.tools = formattedTools;
|
|
223
|
-
}
|
|
224
|
-
const response = await this.client.chat.completions.create(requestParams);
|
|
225
|
-
const responseMessage = response.choices[0].message;
|
|
226
|
-
const result = {
|
|
227
|
-
role: "assistant",
|
|
228
|
-
content: responseMessage.content || ""
|
|
229
|
-
};
|
|
230
|
-
if (response.usage) {
|
|
231
|
-
const usage = response.usage;
|
|
232
|
-
result.usage = {
|
|
233
|
-
completion_tokens: usage.completion_tokens || 0,
|
|
234
|
-
prompt_tokens: usage.prompt_tokens || 0,
|
|
235
|
-
prompt_cache_hit_tokens: usage.prompt_cache_hit_tokens,
|
|
236
|
-
prompt_cache_miss_tokens: usage.prompt_cache_miss_tokens,
|
|
237
|
-
total_tokens: usage.total_tokens || 0
|
|
238
|
-
};
|
|
239
|
-
}
|
|
240
|
-
if (responseMessage.tool_calls && responseMessage.tool_calls.length > 0) {
|
|
241
|
-
result.tool_calls = responseMessage.tool_calls.map((toolCall) => {
|
|
242
|
-
if (toolCall.type !== "function" || !("function" in toolCall)) {
|
|
243
|
-
throw new Error(`Unsupported tool call type: ${toolCall.type}`);
|
|
244
|
-
}
|
|
245
|
-
let args;
|
|
246
|
-
const rawArgs = toolCall.function.arguments;
|
|
247
|
-
if (typeof rawArgs === "string") {
|
|
248
|
-
const raw = rawArgs;
|
|
249
|
-
try {
|
|
250
|
-
args = raw && raw.trim().length > 0 ? JSON.parse(raw) : {};
|
|
251
|
-
} catch {
|
|
252
|
-
args = {};
|
|
253
|
-
}
|
|
254
|
-
} else if (rawArgs && typeof rawArgs === "object") {
|
|
255
|
-
args = rawArgs;
|
|
256
|
-
} else {
|
|
257
|
-
args = {};
|
|
258
|
-
}
|
|
259
|
-
return {
|
|
260
|
-
id: toolCall.id,
|
|
261
|
-
// 满足 Agent 组件期望的格式
|
|
262
|
-
function: {
|
|
263
|
-
name: toolCall.function.name,
|
|
264
|
-
arguments: toolCall.function.arguments || "{}"
|
|
265
|
-
},
|
|
266
|
-
type: "function",
|
|
267
|
-
// 满足内部 ToolCall 接口
|
|
268
|
-
tool_name: toolCall.function.name,
|
|
269
|
-
arguments: args
|
|
270
|
-
};
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
return result;
|
|
253
|
+
}
|
|
254
|
+
toolsParam(tools) {
|
|
255
|
+
return tools !== void 0 && tools.length > 0 ? { tools: tools.map(toApiTool) } : {};
|
|
256
|
+
}
|
|
257
|
+
systemMessages() {
|
|
258
|
+
return this.systemPrompt === void 0 || this.systemPrompt === "" ? [] : [{ role: "system", content: this.systemPrompt }];
|
|
274
259
|
}
|
|
275
260
|
/**
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
261
|
+
* Converts input messages to API messages. Assistant `tool_calls` are not forwarded:
|
|
262
|
+
* agents send tool results back as user messages, and the API rejects assistant tool
|
|
263
|
+
* calls that are not followed by tool messages.
|
|
279
264
|
*/
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
return this.generateCompletion(input);
|
|
283
|
-
} else {
|
|
284
|
-
return this.chatCompletion(input);
|
|
285
|
-
}
|
|
265
|
+
formatMessages(messages) {
|
|
266
|
+
return [...this.systemMessages(), ...messages.map((message) => this.toApiMessage(message))];
|
|
286
267
|
}
|
|
287
268
|
/**
|
|
288
|
-
*
|
|
289
|
-
*
|
|
290
|
-
* @
|
|
269
|
+
* Converts one input message to its API shape.
|
|
270
|
+
*
|
|
271
|
+
* @throws Error when the role is not one of `MessageRole`, or a `tool` message has no
|
|
272
|
+
* `tool_call_id`. The API would reject both.
|
|
291
273
|
*/
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
274
|
+
toApiMessage(message) {
|
|
275
|
+
const content = message.content ?? "";
|
|
276
|
+
switch (message.role) {
|
|
277
|
+
case "system":
|
|
278
|
+
return { role: "system", content };
|
|
279
|
+
case "user":
|
|
280
|
+
return { role: "user", content };
|
|
281
|
+
case "assistant":
|
|
282
|
+
return message.content === void 0 ? { role: "assistant" } : { role: "assistant", content: message.content };
|
|
283
|
+
case "tool":
|
|
284
|
+
if (message.tool_call_id === void 0 || message.tool_call_id === "") {
|
|
285
|
+
throw new Error(
|
|
286
|
+
`${this.name}: a tool message needs the tool_call_id of the call it answers.`
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
return { role: "tool", content, tool_call_id: message.tool_call_id };
|
|
290
|
+
default:
|
|
291
|
+
throw new Error(
|
|
292
|
+
`${this.name}: unsupported message role '${message.role}'. Use system, user, assistant or tool.`
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
firstMessage(response) {
|
|
297
|
+
const choice = response.choices[0];
|
|
298
|
+
if (choice === void 0) {
|
|
299
|
+
throw new Error(`${this.name} returned no choices.`);
|
|
300
|
+
}
|
|
301
|
+
return choice.message;
|
|
302
|
+
}
|
|
303
|
+
logError(error) {
|
|
304
|
+
if (this.verbose) {
|
|
305
|
+
console.error(`[${this.name}] API call failed:`, error);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
var openai_compatible_default = OpenAICompatible;
|
|
310
|
+
|
|
311
|
+
// src/model-provider/deepseek/index.ts
|
|
312
|
+
var DEFAULT_BASE_URL2 = "https://api.deepseek.com/v1";
|
|
313
|
+
var DEFAULT_MODEL = "deepseek-chat";
|
|
314
|
+
var Deepseek = class extends openai_compatible_default {
|
|
315
|
+
constructor(config) {
|
|
316
|
+
super({
|
|
317
|
+
...config,
|
|
318
|
+
model: config.model ?? DEFAULT_MODEL,
|
|
319
|
+
baseURL: config.baseURL ?? DEFAULT_BASE_URL2,
|
|
320
|
+
name: "DeepSeek"
|
|
314
321
|
});
|
|
315
322
|
}
|
|
316
323
|
};
|