@arcote.tech/arc-ai-openai 0.4.6

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/package.json ADDED
@@ -0,0 +1,19 @@
1
+ {
2
+ "name": "@arcote.tech/arc-ai-openai",
3
+ "type": "module",
4
+ "version": "0.4.6",
5
+ "private": false,
6
+ "description": "OpenAI adapter for Arc AI framework",
7
+ "main": "./src/index.ts",
8
+ "types": "./src/index.ts",
9
+ "scripts": {
10
+ "type-check": "tsc --noEmit"
11
+ },
12
+ "peerDependencies": {
13
+ "@arcote.tech/arc-ai": "^0.4.6",
14
+ "typescript": "^5.0.0"
15
+ },
16
+ "devDependencies": {
17
+ "@types/bun": "latest"
18
+ }
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,286 @@
1
+ import type {
2
+ LLMProvider,
3
+ CompletionRequest,
4
+ CompletionResult,
5
+ StreamChunk,
6
+ ToolCall,
7
+ TokenUsage,
8
+ FinishReason,
9
+ } from "@arcote.tech/arc-ai";
10
+
11
+ // ─── Config ──────────────────────────────────────────────────────
12
+
13
+ export interface OpenAIConfig {
14
+ apiKey: string;
15
+ baseUrl?: string;
16
+ defaultModel?: string;
17
+ }
18
+
19
+ // ─── Adapter ─────────────────────────────────────────────────────
20
+
21
+ export function openai(config: OpenAIConfig): LLMProvider {
22
+ const baseUrl = config.baseUrl ?? "https://api.openai.com/v1";
23
+
24
+ function translateTools(
25
+ tools: CompletionRequest["tools"],
26
+ ): unknown[] | undefined {
27
+ if (!tools || tools.length === 0) return undefined;
28
+ return tools.map((t) => ({
29
+ type: "function",
30
+ function: {
31
+ name: t.name,
32
+ description: t.description,
33
+ parameters: t.parameters,
34
+ },
35
+ }));
36
+ }
37
+
38
+ function parseUsage(raw: any): TokenUsage {
39
+ const usage = raw.usage ?? {};
40
+ return {
41
+ inputTokens: usage.prompt_tokens ?? 0,
42
+ outputTokens: usage.completion_tokens ?? 0,
43
+ totalTokens: usage.total_tokens ?? 0,
44
+ cachedTokens: usage.prompt_tokens_details?.cached_tokens ?? 0,
45
+ reasoningTokens:
46
+ usage.completion_tokens_details?.reasoning_tokens ?? 0,
47
+ };
48
+ }
49
+
50
+ function extractToolCalls(choice: any): ToolCall[] {
51
+ const toolCalls = choice.message?.tool_calls ?? [];
52
+ return toolCalls.map((tc: any) => ({
53
+ id: tc.id,
54
+ name: tc.function.name,
55
+ arguments: JSON.parse(tc.function.arguments),
56
+ }));
57
+ }
58
+
59
+ function mapFinishReason(reason: string): FinishReason {
60
+ switch (reason) {
61
+ case "stop":
62
+ return "stop";
63
+ case "tool_calls":
64
+ return "tool_call";
65
+ case "length":
66
+ return "max_tokens";
67
+ default:
68
+ return "stop";
69
+ }
70
+ }
71
+
72
+ async function complete(request: CompletionRequest): Promise<CompletionResult> {
73
+ const body: Record<string, unknown> = {
74
+ model: request.model,
75
+ messages: request.messages.map((m) => ({
76
+ role: m.role,
77
+ content: m.content,
78
+ ...(m.name ? { name: m.name } : {}),
79
+ ...(m.toolCallId ? { tool_call_id: m.toolCallId } : {}),
80
+ })),
81
+ temperature: request.temperature,
82
+ max_tokens: request.maxTokens,
83
+ };
84
+
85
+ const tools = translateTools(request.tools);
86
+ if (tools) body.tools = tools;
87
+
88
+ if (request.webSearch) {
89
+ body.tools = [
90
+ ...(tools ?? []),
91
+ { type: "web_search_preview" },
92
+ ];
93
+ }
94
+
95
+ const response = await fetch(`${baseUrl}/chat/completions`, {
96
+ method: "POST",
97
+ headers: {
98
+ "Content-Type": "application/json",
99
+ Authorization: `Bearer ${config.apiKey}`,
100
+ },
101
+ body: JSON.stringify(body),
102
+ });
103
+
104
+ if (!response.ok) {
105
+ const error = await response.text();
106
+ throw new Error(`OpenAI API error ${response.status}: ${error}`);
107
+ }
108
+
109
+ const data = await response.json() as any;
110
+ const choice = data.choices[0];
111
+
112
+ return {
113
+ content: choice.message?.content ?? "",
114
+ toolCalls: extractToolCalls(choice),
115
+ usage: parseUsage(data),
116
+ finishReason: mapFinishReason(choice.finish_reason),
117
+ };
118
+ }
119
+
120
+ async function streamComplete(
121
+ request: CompletionRequest,
122
+ onChunk: (chunk: StreamChunk) => void,
123
+ ): Promise<CompletionResult> {
124
+ const body: Record<string, unknown> = {
125
+ model: request.model,
126
+ messages: request.messages.map((m) => ({
127
+ role: m.role,
128
+ content: m.content,
129
+ ...(m.name ? { name: m.name } : {}),
130
+ ...(m.toolCallId ? { tool_call_id: m.toolCallId } : {}),
131
+ })),
132
+ temperature: request.temperature,
133
+ max_tokens: request.maxTokens,
134
+ stream: true,
135
+ stream_options: { include_usage: true },
136
+ };
137
+
138
+ const tools = translateTools(request.tools);
139
+ if (tools) body.tools = tools;
140
+
141
+ if (request.webSearch) {
142
+ body.tools = [
143
+ ...(tools ?? []),
144
+ { type: "web_search_preview" },
145
+ ];
146
+ }
147
+
148
+ const response = await fetch(`${baseUrl}/chat/completions`, {
149
+ method: "POST",
150
+ headers: {
151
+ "Content-Type": "application/json",
152
+ Authorization: `Bearer ${config.apiKey}`,
153
+ },
154
+ body: JSON.stringify(body),
155
+ });
156
+
157
+ if (!response.ok) {
158
+ const error = await response.text();
159
+ throw new Error(`OpenAI API error ${response.status}: ${error}`);
160
+ }
161
+
162
+ let content = "";
163
+ let finishReason: FinishReason = "stop";
164
+ let usage: TokenUsage = {
165
+ inputTokens: 0,
166
+ outputTokens: 0,
167
+ totalTokens: 0,
168
+ cachedTokens: 0,
169
+ reasoningTokens: 0,
170
+ };
171
+ const toolCallBuffers = new Map<
172
+ number,
173
+ { id: string; name: string; arguments: string }
174
+ >();
175
+ const completedToolCalls: ToolCall[] = [];
176
+
177
+ const reader = response.body!.getReader();
178
+ const decoder = new TextDecoder();
179
+ let buffer = "";
180
+
181
+ while (true) {
182
+ const { done, value } = await reader.read();
183
+ if (done) break;
184
+
185
+ buffer += decoder.decode(value, { stream: true });
186
+ const lines = buffer.split("\n");
187
+ buffer = lines.pop()!;
188
+
189
+ for (const line of lines) {
190
+ if (!line.startsWith("data: ")) continue;
191
+ const data = line.slice(6).trim();
192
+ if (data === "[DONE]") continue;
193
+
194
+ try {
195
+ const parsed = JSON.parse(data);
196
+
197
+ // Usage-only chunk (last chunk)
198
+ if (parsed.usage && !parsed.choices?.length) {
199
+ usage = parseUsage(parsed);
200
+ onChunk({ type: "usage_update", usage });
201
+ continue;
202
+ }
203
+
204
+ const delta = parsed.choices?.[0]?.delta;
205
+ if (!delta) continue;
206
+
207
+ // Content delta
208
+ if (delta.content) {
209
+ content += delta.content;
210
+ onChunk({ type: "content_delta", content: delta.content });
211
+ }
212
+
213
+ // Tool call chunks (arguments arrive fragmented)
214
+ if (delta.tool_calls) {
215
+ for (const tc of delta.tool_calls) {
216
+ const idx = tc.index;
217
+ if (tc.id) {
218
+ // First chunk for this tool call
219
+ toolCallBuffers.set(idx, {
220
+ id: tc.id,
221
+ name: tc.function?.name ?? "",
222
+ arguments: tc.function?.arguments ?? "",
223
+ });
224
+ onChunk({
225
+ type: "tool_call_start",
226
+ toolCall: { id: tc.id, name: tc.function?.name ?? "", arguments: {} },
227
+ });
228
+ } else {
229
+ // Continuation chunk
230
+ const buf = toolCallBuffers.get(idx);
231
+ if (buf && tc.function?.arguments) {
232
+ buf.arguments += tc.function.arguments;
233
+ onChunk({
234
+ type: "tool_call_delta",
235
+ content: tc.function.arguments,
236
+ });
237
+ }
238
+ }
239
+ }
240
+ }
241
+
242
+ // Finish reason
243
+ const fr = parsed.choices?.[0]?.finish_reason;
244
+ if (fr) {
245
+ finishReason = mapFinishReason(fr);
246
+
247
+ // Finalize tool calls
248
+ if (fr === "tool_calls") {
249
+ for (const buf of toolCallBuffers.values()) {
250
+ try {
251
+ completedToolCalls.push({
252
+ id: buf.id,
253
+ name: buf.name,
254
+ arguments: JSON.parse(buf.arguments),
255
+ });
256
+ } catch {
257
+ completedToolCalls.push({
258
+ id: buf.id,
259
+ name: buf.name,
260
+ arguments: {},
261
+ });
262
+ }
263
+ }
264
+ }
265
+ }
266
+ } catch {
267
+ // Skip malformed JSON
268
+ }
269
+ }
270
+ }
271
+
272
+ return {
273
+ content,
274
+ toolCalls: completedToolCalls,
275
+ usage,
276
+ finishReason,
277
+ };
278
+ }
279
+
280
+ return {
281
+ name: "openai",
282
+ models: ["gpt-4o", "gpt-4o-mini", "o3", "o3-mini", "gpt-4.1", "gpt-4.1-mini", "gpt-4.1-nano"],
283
+ complete,
284
+ streamComplete,
285
+ };
286
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "../../../../../tsconfig.json",
3
+ "include": ["src/**/*"]
4
+ }