-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai.ts
More file actions
190 lines (181 loc) · 6.14 KB
/
Copy pathopenai.ts
File metadata and controls
190 lines (181 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import type Anthropic from "@anthropic-ai/sdk";
import OpenAI from "openai";
import { proxyAwareFetch } from "../proxy-bootstrap.js";
import { withStreamRetry } from "./stream-retry.js";
import type { StoredMessage } from "../types.js";
import type { Provider, ProviderResult, ProviderRunOpts } from "./types.js";
export class OpenAIProvider implements Provider {
readonly name = "openai";
private client: OpenAI;
constructor(opts: { apiKey?: string; baseURL?: string } = {}) {
this.client = new OpenAI({
apiKey: opts.apiKey,
baseURL: opts.baseURL,
fetch: proxyAwareFetch,
});
}
async runTurn(opts: ProviderRunOpts): Promise<ProviderResult> {
const messages = anthropicToOpenAI(opts.systemPrompt, opts.messages);
const tools = opts.tools.map((t) => ({
type: "function" as const,
function: {
name: t.name,
description: t.description,
parameters: t.inputSchema,
},
}));
// Per-attempt state lives inside the retry closure so it resets cleanly on
// a transient empty-stream retry (see withStreamRetry).
return withStreamRetry({ signal: opts.signal }, async (markEmitted) => {
const stream = await this.client.chat.completions.create(
{
model: opts.model,
messages,
tools: tools.length ? tools : undefined,
max_tokens: opts.maxTokens ?? 16_000,
stream: true,
stream_options: { include_usage: true },
},
// Per-request options; `signal` aborts the in-flight HTTP stream
// (the SDK then throws APIUserAbortError).
{ signal: opts.signal },
);
let text = "";
const toolCalls = new Map<number, { id: string; name: string; args: string }>();
let finish = "stop";
let inputTokens = 0;
let outputTokens = 0;
let cacheReadTokens = 0;
for await (const chunk of stream) {
const choice = chunk.choices[0];
if (choice?.delta?.content) {
markEmitted();
text += choice.delta.content;
opts.handlers?.onTextDelta?.(choice.delta.content);
}
if (choice?.delta?.tool_calls) {
for (const tc of choice.delta.tool_calls) {
const idx = tc.index;
if (!toolCalls.has(idx)) {
toolCalls.set(idx, { id: "", name: "", args: "" });
}
const acc = toolCalls.get(idx)!;
if (tc.id) acc.id = tc.id;
if (tc.function?.name) acc.name = tc.function.name;
if (tc.function?.arguments) acc.args += tc.function.arguments;
}
}
if (choice?.finish_reason) finish = choice.finish_reason;
if (chunk.usage) {
inputTokens = chunk.usage.prompt_tokens ?? 0;
outputTokens = chunk.usage.completion_tokens ?? 0;
const details = chunk.usage.prompt_tokens_details;
cacheReadTokens = details?.cached_tokens ?? 0;
}
}
const content: Anthropic.ContentBlock[] = [];
if (text) {
content.push({ type: "text", text, citations: null });
}
for (const tc of toolCalls.values()) {
let parsed: unknown;
try {
parsed = tc.args ? JSON.parse(tc.args) : {};
} catch {
parsed = { _raw: tc.args };
}
content.push({
type: "tool_use",
id: tc.id || `call_${tc.name}_${Math.random().toString(36).slice(2)}`,
name: tc.name,
input: parsed,
} as Anthropic.ToolUseBlock);
}
return {
content,
stopReason: finish === "tool_calls" ? "tool_use" : "end_turn",
usage: {
inputTokens,
outputTokens,
cacheReadTokens,
cacheWriteTokens: 0,
},
};
});
}
}
function anthropicToOpenAI(
systemPrompt: string,
messages: StoredMessage[],
): OpenAI.Chat.Completions.ChatCompletionMessageParam[] {
const out: OpenAI.Chat.Completions.ChatCompletionMessageParam[] = [
{ role: "system", content: systemPrompt },
];
for (const msg of messages) {
if (msg.role === "user") {
const content = msg.content;
if (typeof content === "string") {
out.push({ role: "user", content });
continue;
}
const textBlocks: string[] = [];
const toolResults: { id: string; content: string; isError: boolean }[] = [];
for (const block of content) {
if (block.type === "text") {
textBlocks.push(block.text);
} else if (block.type === "tool_result") {
const text =
typeof block.content === "string"
? block.content
: Array.isArray(block.content)
? block.content.map((b) => (b.type === "text" ? b.text : "")).join("\n")
: "";
toolResults.push({
id: block.tool_use_id,
content: text,
isError: block.is_error ?? false,
});
}
}
if (textBlocks.length) {
out.push({ role: "user", content: textBlocks.join("\n") });
}
for (const tr of toolResults) {
out.push({
role: "tool",
tool_call_id: tr.id,
content: tr.isError ? `[error] ${tr.content}` : tr.content,
});
}
} else {
const content = msg.content;
if (typeof content === "string") {
out.push({ role: "assistant", content });
continue;
}
const textParts: string[] = [];
const toolCalls: OpenAI.Chat.Completions.ChatCompletionMessageToolCall[] = [];
for (const block of content) {
if (block.type === "text") {
textParts.push(block.text);
} else if (block.type === "tool_use") {
toolCalls.push({
id: block.id,
type: "function",
function: {
name: block.name,
arguments: JSON.stringify(block.input ?? {}),
},
});
}
}
const assistant: OpenAI.Chat.Completions.ChatCompletionAssistantMessageParam = {
role: "assistant",
content: textParts.join("\n") || null,
};
if (toolCalls.length) assistant.tool_calls = toolCalls;
out.push(assistant);
}
}
return out;
}