@mariozechner/pi-ai 0.5.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.
- package/README.md +231 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/models.d.ts +72 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.generated.d.ts +2890 -0
- package/dist/models.generated.d.ts.map +1 -0
- package/dist/models.generated.js +2889 -0
- package/dist/models.generated.js.map +1 -0
- package/dist/models.js +71 -0
- package/dist/models.js.map +1 -0
- package/dist/providers/anthropic.d.ts +23 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +312 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/google.d.ts +20 -0
- package/dist/providers/google.d.ts.map +1 -0
- package/dist/providers/google.js +337 -0
- package/dist/providers/google.js.map +1 -0
- package/dist/providers/openai-completions.d.ts +21 -0
- package/dist/providers/openai-completions.d.ts.map +1 -0
- package/dist/providers/openai-completions.js +304 -0
- package/dist/providers/openai-completions.js.map +1 -0
- package/dist/providers/openai-responses.d.ts +16 -0
- package/dist/providers/openai-responses.d.ts.map +1 -0
- package/dist/providers/openai-responses.js +261 -0
- package/dist/providers/openai-responses.js.map +1 -0
- package/dist/types.d.ts +119 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +50 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
import { calculateCost } from "../models.js";
|
|
3
|
+
export class OpenAICompletionsLLM {
|
|
4
|
+
client;
|
|
5
|
+
modelInfo;
|
|
6
|
+
constructor(model, apiKey) {
|
|
7
|
+
if (!apiKey) {
|
|
8
|
+
if (!process.env.OPENAI_API_KEY) {
|
|
9
|
+
throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");
|
|
10
|
+
}
|
|
11
|
+
apiKey = process.env.OPENAI_API_KEY;
|
|
12
|
+
}
|
|
13
|
+
this.client = new OpenAI({ apiKey, baseURL: model.baseUrl });
|
|
14
|
+
this.modelInfo = model;
|
|
15
|
+
}
|
|
16
|
+
getModel() {
|
|
17
|
+
return this.modelInfo;
|
|
18
|
+
}
|
|
19
|
+
async complete(request, options) {
|
|
20
|
+
try {
|
|
21
|
+
const messages = this.convertMessages(request.messages, request.systemPrompt);
|
|
22
|
+
const params = {
|
|
23
|
+
model: this.modelInfo.id,
|
|
24
|
+
messages,
|
|
25
|
+
stream: true,
|
|
26
|
+
stream_options: { include_usage: true },
|
|
27
|
+
};
|
|
28
|
+
// Cerebras/xAI dont like the "store" field
|
|
29
|
+
if (!this.modelInfo.baseUrl?.includes("cerebras.ai") && !this.modelInfo.baseUrl?.includes("api.x.ai")) {
|
|
30
|
+
params.store = false;
|
|
31
|
+
}
|
|
32
|
+
if (options?.maxTokens) {
|
|
33
|
+
params.max_completion_tokens = options?.maxTokens;
|
|
34
|
+
}
|
|
35
|
+
if (options?.temperature !== undefined) {
|
|
36
|
+
params.temperature = options?.temperature;
|
|
37
|
+
}
|
|
38
|
+
if (request.tools) {
|
|
39
|
+
params.tools = this.convertTools(request.tools);
|
|
40
|
+
}
|
|
41
|
+
if (options?.toolChoice) {
|
|
42
|
+
params.tool_choice = options.toolChoice;
|
|
43
|
+
}
|
|
44
|
+
if (options?.reasoningEffort &&
|
|
45
|
+
this.modelInfo.reasoning &&
|
|
46
|
+
!this.modelInfo.id.toLowerCase().includes("grok")) {
|
|
47
|
+
params.reasoning_effort = options.reasoningEffort;
|
|
48
|
+
}
|
|
49
|
+
const stream = await this.client.chat.completions.create(params, {
|
|
50
|
+
signal: options?.signal,
|
|
51
|
+
});
|
|
52
|
+
let content = "";
|
|
53
|
+
let reasoningContent = "";
|
|
54
|
+
let reasoningField = null;
|
|
55
|
+
const parsedToolCalls = [];
|
|
56
|
+
let usage = {
|
|
57
|
+
input: 0,
|
|
58
|
+
output: 0,
|
|
59
|
+
cacheRead: 0,
|
|
60
|
+
cacheWrite: 0,
|
|
61
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
62
|
+
};
|
|
63
|
+
let finishReason = null;
|
|
64
|
+
let blockType = null;
|
|
65
|
+
for await (const chunk of stream) {
|
|
66
|
+
if (chunk.usage) {
|
|
67
|
+
usage = {
|
|
68
|
+
input: chunk.usage.prompt_tokens || 0,
|
|
69
|
+
output: (chunk.usage.completion_tokens || 0) +
|
|
70
|
+
(chunk.usage.completion_tokens_details?.reasoning_tokens || 0),
|
|
71
|
+
cacheRead: chunk.usage.prompt_tokens_details?.cached_tokens || 0,
|
|
72
|
+
cacheWrite: 0,
|
|
73
|
+
cost: {
|
|
74
|
+
input: 0,
|
|
75
|
+
output: 0,
|
|
76
|
+
cacheRead: 0,
|
|
77
|
+
cacheWrite: 0,
|
|
78
|
+
total: 0,
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
const choice = chunk.choices[0];
|
|
83
|
+
if (!choice)
|
|
84
|
+
continue;
|
|
85
|
+
if (choice.delta) {
|
|
86
|
+
// Handle text content
|
|
87
|
+
if (choice.delta.content !== null &&
|
|
88
|
+
choice.delta.content !== undefined &&
|
|
89
|
+
choice.delta.content.length > 0) {
|
|
90
|
+
if (blockType === "thinking") {
|
|
91
|
+
options?.onThinking?.("", true);
|
|
92
|
+
blockType = null;
|
|
93
|
+
}
|
|
94
|
+
content += choice.delta.content;
|
|
95
|
+
options?.onText?.(choice.delta.content, false);
|
|
96
|
+
blockType = "text";
|
|
97
|
+
}
|
|
98
|
+
// Handle reasoning_content field
|
|
99
|
+
if (choice.delta.reasoning_content !== null &&
|
|
100
|
+
choice.delta.reasoning_content !== undefined) {
|
|
101
|
+
if (blockType === "text") {
|
|
102
|
+
options?.onText?.("", true);
|
|
103
|
+
blockType = null;
|
|
104
|
+
}
|
|
105
|
+
reasoningContent += choice.delta.reasoning_content;
|
|
106
|
+
reasoningField = "reasoning_content";
|
|
107
|
+
options?.onThinking?.(choice.delta.reasoning_content, false);
|
|
108
|
+
blockType = "thinking";
|
|
109
|
+
}
|
|
110
|
+
// Handle reasoning field
|
|
111
|
+
if (choice.delta.reasoning !== null && choice.delta.reasoning !== undefined) {
|
|
112
|
+
if (blockType === "text") {
|
|
113
|
+
options?.onText?.("", true);
|
|
114
|
+
blockType = null;
|
|
115
|
+
}
|
|
116
|
+
reasoningContent += choice.delta.reasoning;
|
|
117
|
+
reasoningField = "reasoning";
|
|
118
|
+
options?.onThinking?.(choice.delta.reasoning, false);
|
|
119
|
+
blockType = "thinking";
|
|
120
|
+
}
|
|
121
|
+
// Handle tool calls
|
|
122
|
+
if (choice?.delta?.tool_calls) {
|
|
123
|
+
if (blockType === "text") {
|
|
124
|
+
options?.onText?.("", true);
|
|
125
|
+
blockType = null;
|
|
126
|
+
}
|
|
127
|
+
if (blockType === "thinking") {
|
|
128
|
+
options?.onThinking?.("", true);
|
|
129
|
+
blockType = null;
|
|
130
|
+
}
|
|
131
|
+
for (const toolCall of choice.delta.tool_calls) {
|
|
132
|
+
if (parsedToolCalls.length === 0 ||
|
|
133
|
+
(toolCall.id !== undefined && parsedToolCalls[parsedToolCalls.length - 1].id !== toolCall.id)) {
|
|
134
|
+
parsedToolCalls.push({
|
|
135
|
+
id: toolCall.id || "",
|
|
136
|
+
name: toolCall.function?.name || "",
|
|
137
|
+
arguments: "",
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const current = parsedToolCalls[parsedToolCalls.length - 1];
|
|
141
|
+
if (toolCall.id)
|
|
142
|
+
current.id = toolCall.id;
|
|
143
|
+
if (toolCall.function?.name)
|
|
144
|
+
current.name = toolCall.function.name;
|
|
145
|
+
if (toolCall.function?.arguments) {
|
|
146
|
+
current.arguments += toolCall.function.arguments;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
// Capture finish reason
|
|
152
|
+
if (choice.finish_reason) {
|
|
153
|
+
if (blockType === "text") {
|
|
154
|
+
options?.onText?.("", true);
|
|
155
|
+
blockType = null;
|
|
156
|
+
}
|
|
157
|
+
if (blockType === "thinking") {
|
|
158
|
+
options?.onThinking?.("", true);
|
|
159
|
+
blockType = null;
|
|
160
|
+
}
|
|
161
|
+
finishReason = choice.finish_reason;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Convert tool calls map to array
|
|
165
|
+
const toolCalls = parsedToolCalls.map((tc) => ({
|
|
166
|
+
id: tc.id,
|
|
167
|
+
name: tc.name,
|
|
168
|
+
arguments: JSON.parse(tc.arguments),
|
|
169
|
+
}));
|
|
170
|
+
// Calculate cost
|
|
171
|
+
calculateCost(this.modelInfo, usage);
|
|
172
|
+
return {
|
|
173
|
+
role: "assistant",
|
|
174
|
+
content: content || undefined,
|
|
175
|
+
thinking: reasoningContent || undefined,
|
|
176
|
+
thinkingSignature: reasoningField || undefined,
|
|
177
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
178
|
+
provider: this.modelInfo.provider,
|
|
179
|
+
model: this.modelInfo.id,
|
|
180
|
+
usage,
|
|
181
|
+
stopReason: this.mapStopReason(finishReason),
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
catch (error) {
|
|
185
|
+
return {
|
|
186
|
+
role: "assistant",
|
|
187
|
+
provider: this.modelInfo.provider,
|
|
188
|
+
model: this.modelInfo.id,
|
|
189
|
+
usage: {
|
|
190
|
+
input: 0,
|
|
191
|
+
output: 0,
|
|
192
|
+
cacheRead: 0,
|
|
193
|
+
cacheWrite: 0,
|
|
194
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
195
|
+
},
|
|
196
|
+
stopReason: "error",
|
|
197
|
+
error: error instanceof Error ? error.message : String(error),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
convertMessages(messages, systemPrompt) {
|
|
202
|
+
const params = [];
|
|
203
|
+
// Add system prompt if provided
|
|
204
|
+
if (systemPrompt) {
|
|
205
|
+
// Cerebras/xAi don't like the "developer" role
|
|
206
|
+
const useDeveloperRole = this.modelInfo.reasoning &&
|
|
207
|
+
!this.modelInfo.baseUrl?.includes("cerebras.ai") &&
|
|
208
|
+
!this.modelInfo.baseUrl?.includes("api.x.ai");
|
|
209
|
+
const role = useDeveloperRole ? "developer" : "system";
|
|
210
|
+
params.push({ role: role, content: systemPrompt });
|
|
211
|
+
}
|
|
212
|
+
// Convert messages
|
|
213
|
+
for (const msg of messages) {
|
|
214
|
+
if (msg.role === "user") {
|
|
215
|
+
// Handle both string and array content
|
|
216
|
+
if (typeof msg.content === "string") {
|
|
217
|
+
params.push({
|
|
218
|
+
role: "user",
|
|
219
|
+
content: msg.content,
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
// Convert array content to OpenAI format
|
|
224
|
+
const content = msg.content.map((item) => {
|
|
225
|
+
if (item.type === "text") {
|
|
226
|
+
return {
|
|
227
|
+
type: "text",
|
|
228
|
+
text: item.text,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
else {
|
|
232
|
+
// Image content - OpenAI uses data URLs
|
|
233
|
+
return {
|
|
234
|
+
type: "image_url",
|
|
235
|
+
image_url: {
|
|
236
|
+
url: `data:${item.mimeType};base64,${item.data}`,
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
params.push({
|
|
242
|
+
role: "user",
|
|
243
|
+
content,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if (msg.role === "assistant") {
|
|
248
|
+
const assistantMsg = {
|
|
249
|
+
role: "assistant",
|
|
250
|
+
content: msg.content || null,
|
|
251
|
+
};
|
|
252
|
+
// LLama.cpp server + gpt-oss
|
|
253
|
+
if (msg.thinking && msg.thinkingSignature && msg.thinkingSignature.length > 0) {
|
|
254
|
+
assistantMsg[msg.thinkingSignature] = msg.thinking;
|
|
255
|
+
}
|
|
256
|
+
if (msg.toolCalls) {
|
|
257
|
+
assistantMsg.tool_calls = msg.toolCalls.map((tc) => ({
|
|
258
|
+
id: tc.id,
|
|
259
|
+
type: "function",
|
|
260
|
+
function: {
|
|
261
|
+
name: tc.name,
|
|
262
|
+
arguments: JSON.stringify(tc.arguments),
|
|
263
|
+
},
|
|
264
|
+
}));
|
|
265
|
+
}
|
|
266
|
+
params.push(assistantMsg);
|
|
267
|
+
}
|
|
268
|
+
else if (msg.role === "toolResult") {
|
|
269
|
+
params.push({
|
|
270
|
+
role: "tool",
|
|
271
|
+
content: msg.content,
|
|
272
|
+
tool_call_id: msg.toolCallId,
|
|
273
|
+
});
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return params;
|
|
277
|
+
}
|
|
278
|
+
convertTools(tools) {
|
|
279
|
+
return tools.map((tool) => ({
|
|
280
|
+
type: "function",
|
|
281
|
+
function: {
|
|
282
|
+
name: tool.name,
|
|
283
|
+
description: tool.description,
|
|
284
|
+
parameters: tool.parameters,
|
|
285
|
+
},
|
|
286
|
+
}));
|
|
287
|
+
}
|
|
288
|
+
mapStopReason(reason) {
|
|
289
|
+
switch (reason) {
|
|
290
|
+
case "stop":
|
|
291
|
+
return "stop";
|
|
292
|
+
case "length":
|
|
293
|
+
return "length";
|
|
294
|
+
case "function_call":
|
|
295
|
+
case "tool_calls":
|
|
296
|
+
return "toolUse";
|
|
297
|
+
case "content_filter":
|
|
298
|
+
return "safety";
|
|
299
|
+
default:
|
|
300
|
+
return "stop";
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
//# sourceMappingURL=openai-completions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-completions.js","sourceRoot":"","sources":["../../src/providers/openai-completions.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,Q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AC;YACrB,KAAK,YAAY;gBAChB,OAAO,SAAS,CAAC;YAClB,KAAK,gBAAgB;gBACpB,OAAO,QAAQ,CAAC;YACjB;gBACC,OAAO,MAAM,CAAC;QAChB,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { AssistantMessage, Context, LLM, LLMOptions, Model } from "../types.js";
|
|
2
|
+
export interface OpenAIResponsesLLMOptions extends LLMOptions {
|
|
3
|
+
reasoningEffort?: "minimal" | "low" | "medium" | "high";
|
|
4
|
+
reasoningSummary?: "auto" | "detailed" | "concise" | null;
|
|
5
|
+
}
|
|
6
|
+
export declare class OpenAIResponsesLLM implements LLM<OpenAIResponsesLLMOptions> {
|
|
7
|
+
private client;
|
|
8
|
+
private modelInfo;
|
|
9
|
+
constructor(model: Model, apiKey?: string);
|
|
10
|
+
getModel(): Model;
|
|
11
|
+
complete(request: Context, options?: OpenAIResponsesLLMOptions): Promise<AssistantMessage>;
|
|
12
|
+
private convertToInput;
|
|
13
|
+
private convertTools;
|
|
14
|
+
private mapStopReason;
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=openai-responses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-responses.d.ts","sourceRoot":"","sources":["../../src/providers/openai-responses.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EACX,gBAAgB,EAChB,OAAO,EACP,GAAG,EACH,UAAU,EAEV,KAAK,EAKL,MAAM,aAAa,CAAC;AAErB,MAAM,WAAW,yBAA0B,SAAQ,UAAU;IAC5D,eAAe,CAAC,EAAE,SAAS,GAAG,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAC;IACxD,gBAAgB,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;CAC1D;AAED,qBAAa,kBAAmB,YAAW,GAAG,CAAC,yBAAyB,CAAC;IACxE,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,SAAS,CAAQ;gBAEb,KAAK,EAAE,KAAK,EAAE,MAAM,CAAC,EAAE,MAAM;IAazC,QAAQ,IAAI,KAAK;IAIX,QAAQ,CAAC,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,yBAAyB,GAAG,OAAO,CAAC,gBAAgB,CAAC;IAkJhG,OAAO,CAAC,cAAc;IAkFtB,OAAO,CAAC,YAAY;IAUpB,OAAO,CAAC,aAAa;CAarB"}
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
export class OpenAIResponsesLLM {
|
|
3
|
+
client;
|
|
4
|
+
modelInfo;
|
|
5
|
+
constructor(model, apiKey) {
|
|
6
|
+
if (!apiKey) {
|
|
7
|
+
if (!process.env.OPENAI_API_KEY) {
|
|
8
|
+
throw new Error("OpenAI API key is required. Set OPENAI_API_KEY environment variable or pass it as an argument.");
|
|
9
|
+
}
|
|
10
|
+
apiKey = process.env.OPENAI_API_KEY;
|
|
11
|
+
}
|
|
12
|
+
this.client = new OpenAI({ apiKey, baseURL: model.baseUrl });
|
|
13
|
+
this.modelInfo = model;
|
|
14
|
+
}
|
|
15
|
+
getModel() {
|
|
16
|
+
return this.modelInfo;
|
|
17
|
+
}
|
|
18
|
+
async complete(request, options) {
|
|
19
|
+
try {
|
|
20
|
+
const input = this.convertToInput(request.messages, request.systemPrompt);
|
|
21
|
+
const params = {
|
|
22
|
+
model: this.modelInfo.id,
|
|
23
|
+
input,
|
|
24
|
+
stream: true,
|
|
25
|
+
};
|
|
26
|
+
if (options?.maxTokens) {
|
|
27
|
+
params.max_output_tokens = options?.maxTokens;
|
|
28
|
+
}
|
|
29
|
+
if (options?.temperature !== undefined) {
|
|
30
|
+
params.temperature = options?.temperature;
|
|
31
|
+
}
|
|
32
|
+
if (request.tools) {
|
|
33
|
+
params.tools = this.convertTools(request.tools);
|
|
34
|
+
}
|
|
35
|
+
// Add reasoning options for models that support it
|
|
36
|
+
if (this.modelInfo?.reasoning && (options?.reasoningEffort || options?.reasoningSummary)) {
|
|
37
|
+
params.reasoning = {
|
|
38
|
+
effort: options?.reasoningEffort || "medium",
|
|
39
|
+
summary: options?.reasoningSummary || "auto",
|
|
40
|
+
};
|
|
41
|
+
params.include = ["reasoning.encrypted_content"];
|
|
42
|
+
}
|
|
43
|
+
const stream = await this.client.responses.create(params, {
|
|
44
|
+
signal: options?.signal,
|
|
45
|
+
});
|
|
46
|
+
let content = "";
|
|
47
|
+
let thinking = "";
|
|
48
|
+
const toolCalls = [];
|
|
49
|
+
const reasoningItems = [];
|
|
50
|
+
let usage = {
|
|
51
|
+
input: 0,
|
|
52
|
+
output: 0,
|
|
53
|
+
cacheRead: 0,
|
|
54
|
+
cacheWrite: 0,
|
|
55
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
56
|
+
};
|
|
57
|
+
let stopReason = "stop";
|
|
58
|
+
for await (const event of stream) {
|
|
59
|
+
// Handle reasoning summary for models that support it
|
|
60
|
+
if (event.type === "response.reasoning_summary_text.delta") {
|
|
61
|
+
const delta = event.delta;
|
|
62
|
+
thinking += delta;
|
|
63
|
+
options?.onThinking?.(delta, false);
|
|
64
|
+
}
|
|
65
|
+
else if (event.type === "response.reasoning_summary_text.done") {
|
|
66
|
+
if (event.text) {
|
|
67
|
+
thinking = event.text;
|
|
68
|
+
}
|
|
69
|
+
options?.onThinking?.("", true);
|
|
70
|
+
}
|
|
71
|
+
// Handle main text output
|
|
72
|
+
else if (event.type === "response.output_text.delta") {
|
|
73
|
+
const delta = event.delta;
|
|
74
|
+
content += delta;
|
|
75
|
+
options?.onText?.(delta, false);
|
|
76
|
+
}
|
|
77
|
+
else if (event.type === "response.output_text.done") {
|
|
78
|
+
if (event.text) {
|
|
79
|
+
content = event.text;
|
|
80
|
+
}
|
|
81
|
+
options?.onText?.("", true);
|
|
82
|
+
}
|
|
83
|
+
// Handle function calls
|
|
84
|
+
else if (event.type === "response.output_item.done") {
|
|
85
|
+
const item = event.item;
|
|
86
|
+
if (item?.type === "function_call") {
|
|
87
|
+
toolCalls.push({
|
|
88
|
+
id: item.call_id + "|" + item.id,
|
|
89
|
+
name: item.name,
|
|
90
|
+
arguments: JSON.parse(item.arguments),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (item.type === "reasoning") {
|
|
94
|
+
reasoningItems.push(item);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
// Handle completion
|
|
98
|
+
else if (event.type === "response.completed") {
|
|
99
|
+
const response = event.response;
|
|
100
|
+
if (response?.usage) {
|
|
101
|
+
usage = {
|
|
102
|
+
input: response.usage.input_tokens || 0,
|
|
103
|
+
output: response.usage.output_tokens || 0,
|
|
104
|
+
cacheRead: response.usage.input_tokens_details?.cached_tokens || 0,
|
|
105
|
+
cacheWrite: 0,
|
|
106
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
// Map status to stop reason
|
|
110
|
+
stopReason = this.mapStopReason(response?.status);
|
|
111
|
+
if (toolCalls.length > 0 && stopReason === "stop") {
|
|
112
|
+
stopReason = "toolUse";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Handle errors
|
|
116
|
+
else if (event.type === "error") {
|
|
117
|
+
return {
|
|
118
|
+
role: "assistant",
|
|
119
|
+
provider: this.modelInfo.provider,
|
|
120
|
+
model: this.modelInfo.id,
|
|
121
|
+
usage,
|
|
122
|
+
stopReason: "error",
|
|
123
|
+
error: `Code ${event.code}: ${event.message}` || "Unknown error",
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
role: "assistant",
|
|
129
|
+
content: content || undefined,
|
|
130
|
+
thinking: thinking || undefined,
|
|
131
|
+
thinkingSignature: JSON.stringify(reasoningItems) || undefined,
|
|
132
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : undefined,
|
|
133
|
+
provider: this.modelInfo.provider,
|
|
134
|
+
model: this.modelInfo.id,
|
|
135
|
+
usage,
|
|
136
|
+
stopReason,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
return {
|
|
141
|
+
role: "assistant",
|
|
142
|
+
provider: this.modelInfo.provider,
|
|
143
|
+
model: this.modelInfo.id,
|
|
144
|
+
usage: {
|
|
145
|
+
input: 0,
|
|
146
|
+
output: 0,
|
|
147
|
+
cacheRead: 0,
|
|
148
|
+
cacheWrite: 0,
|
|
149
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
150
|
+
},
|
|
151
|
+
stopReason: "error",
|
|
152
|
+
error: error instanceof Error ? error.message : String(error),
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
convertToInput(messages, systemPrompt) {
|
|
157
|
+
const input = [];
|
|
158
|
+
// Add system prompt if provided
|
|
159
|
+
if (systemPrompt) {
|
|
160
|
+
const role = this.modelInfo?.reasoning ? "developer" : "system";
|
|
161
|
+
input.push({
|
|
162
|
+
role,
|
|
163
|
+
content: systemPrompt,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
// Convert messages
|
|
167
|
+
for (const msg of messages) {
|
|
168
|
+
if (msg.role === "user") {
|
|
169
|
+
// Handle both string and array content
|
|
170
|
+
if (typeof msg.content === "string") {
|
|
171
|
+
input.push({
|
|
172
|
+
role: "user",
|
|
173
|
+
content: [{ type: "input_text", text: msg.content }],
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
else {
|
|
177
|
+
// Convert array content to OpenAI Responses format
|
|
178
|
+
const content = msg.content.map((item) => {
|
|
179
|
+
if (item.type === "text") {
|
|
180
|
+
return {
|
|
181
|
+
type: "input_text",
|
|
182
|
+
text: item.text,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
// Image content - OpenAI Responses uses data URLs
|
|
187
|
+
return {
|
|
188
|
+
type: "input_image",
|
|
189
|
+
detail: "auto",
|
|
190
|
+
image_url: `data:${item.mimeType};base64,${item.data}`,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
input.push({
|
|
195
|
+
role: "user",
|
|
196
|
+
content,
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else if (msg.role === "assistant") {
|
|
201
|
+
// Assistant messages - add both content and tool calls to output
|
|
202
|
+
const output = [];
|
|
203
|
+
if (msg.thinkingSignature) {
|
|
204
|
+
output.push(...JSON.parse(msg.thinkingSignature));
|
|
205
|
+
}
|
|
206
|
+
if (msg.toolCalls) {
|
|
207
|
+
for (const toolCall of msg.toolCalls) {
|
|
208
|
+
output.push({
|
|
209
|
+
type: "function_call",
|
|
210
|
+
id: toolCall.id.split("|")[1], // Extract original ID
|
|
211
|
+
call_id: toolCall.id.split("|")[0], // Extract call session ID
|
|
212
|
+
name: toolCall.name,
|
|
213
|
+
arguments: JSON.stringify(toolCall.arguments),
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (msg.content) {
|
|
218
|
+
output.push({
|
|
219
|
+
type: "message",
|
|
220
|
+
role: "assistant",
|
|
221
|
+
content: [{ type: "input_text", text: msg.content }],
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
// Add all output items to input
|
|
225
|
+
input.push(...output);
|
|
226
|
+
}
|
|
227
|
+
else if (msg.role === "toolResult") {
|
|
228
|
+
// Tool results are sent as function_call_output
|
|
229
|
+
input.push({
|
|
230
|
+
type: "function_call_output",
|
|
231
|
+
call_id: msg.toolCallId.split("|")[0], // Extract call session ID
|
|
232
|
+
output: msg.content,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return input;
|
|
237
|
+
}
|
|
238
|
+
convertTools(tools) {
|
|
239
|
+
return tools.map((tool) => ({
|
|
240
|
+
type: "function",
|
|
241
|
+
name: tool.name,
|
|
242
|
+
description: tool.description,
|
|
243
|
+
parameters: tool.parameters,
|
|
244
|
+
strict: null,
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
mapStopReason(status) {
|
|
248
|
+
switch (status) {
|
|
249
|
+
case "completed":
|
|
250
|
+
return "stop";
|
|
251
|
+
case "incomplete":
|
|
252
|
+
return "length";
|
|
253
|
+
case "failed":
|
|
254
|
+
case "cancelled":
|
|
255
|
+
return "error";
|
|
256
|
+
default:
|
|
257
|
+
return "stop";
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
//# sourceMappingURL=openai-responses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-responses.js","sourceRoot":"","sources":["../../src/providers/openai-responses.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AA4B5B,MAAM,OAAO,kBAAkB;IACtB,MAAM,CAAS;IACf,SAAS,CAAQ;IAEzB,YAAY,KAAY,EAAE,MAAe;QACxC,IAAI,CAAC,MAAM,EAAE,CAAC;YACb,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,cAAc,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CACd,gGAAgG,CAChG,CAAC;YACH,CAAC;YACD,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC;QACrC,CAAC;QACD,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,CAAC,OAAO,EAAE,CAAC,CAAC;QAC7D,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;IACxB,CAAC;IAED,QAAQ;QACP,OAAO,IAAI,CAAC,SAAS,CAAC;IACvB,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,OAAgB,EAAE,OAAmC;QACnE,IAAI,CAAC;YACJ,MAAM,KAAK,GAAG,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,QAAQ,EAAE,OAAO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