@emabuild/ai 0.1.1 → 0.9.0
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/LICENSE +21 -0
- package/README.md +139 -13
- package/dist/cli.js +45 -7
- package/dist/cli.js.map +1 -1
- package/dist/handler.d.ts +3 -2
- package/dist/handler.d.ts.map +1 -1
- package/dist/handler.js +4 -1
- package/dist/handler.js.map +1 -1
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9 -2
- package/dist/index.js.map +1 -1
- package/dist/loop.d.ts +26 -0
- package/dist/loop.d.ts.map +1 -0
- package/dist/loop.js +215 -0
- package/dist/loop.js.map +1 -0
- package/dist/providers/anthropic.d.ts +58 -0
- package/dist/providers/anthropic.d.ts.map +1 -0
- package/dist/providers/anthropic.js +217 -0
- package/dist/providers/anthropic.js.map +1 -0
- package/dist/providers/errors.d.ts +12 -0
- package/dist/providers/errors.d.ts.map +1 -0
- package/dist/providers/errors.js +39 -0
- package/dist/providers/errors.js.map +1 -0
- package/dist/providers/model-call.d.ts +17 -0
- package/dist/providers/model-call.d.ts.map +1 -0
- package/dist/providers/model-call.js +117 -0
- package/dist/providers/model-call.js.map +1 -0
- package/dist/providers/openai-format.d.ts +125 -0
- package/dist/providers/openai-format.d.ts.map +1 -0
- package/dist/providers/openai-format.js +214 -0
- package/dist/providers/openai-format.js.map +1 -0
- package/dist/providers/types.d.ts +193 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +22 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/run.d.ts +40 -38
- package/dist/run.d.ts.map +1 -1
- package/dist/run.js +44 -323
- package/dist/run.js.map +1 -1
- package/dist/usage.d.ts +7 -1
- package/dist/usage.d.ts.map +1 -1
- package/dist/usage.js +35 -6
- package/dist/usage.js.map +1 -1
- package/package.json +24 -8
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module providers/model-call
|
|
3
|
+
*
|
|
4
|
+
* Wraps a plain `callModel(request) => response` function as a model adapter:
|
|
5
|
+
* the loop keeps the conversation in the neutral format, hands it out each
|
|
6
|
+
* turn, and turns the answer into the same events as a streaming provider.
|
|
7
|
+
*/
|
|
8
|
+
import { describeAnyError } from './errors.js';
|
|
9
|
+
const isAbort = (err) => (err instanceof Error && (err.name === 'AbortError' || err.name === 'APIUserAbortError'))
|
|
10
|
+
|| (typeof DOMException !== 'undefined' && err instanceof DOMException && err.name === 'AbortError');
|
|
11
|
+
/** Ids are needed to pair tool results with calls; models / gateways sometimes omit them */
|
|
12
|
+
function withIds(calls, turn) {
|
|
13
|
+
return (calls ?? []).map((c, i) => ({
|
|
14
|
+
id: c.id || `call_${turn}_${i}`,
|
|
15
|
+
name: c.name,
|
|
16
|
+
arguments: typeof c.arguments === 'string' ? c.arguments : JSON.stringify(c.arguments ?? {}),
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
const isAsyncIterable = (v) => !!v && typeof v[Symbol.asyncIterator] === 'function';
|
|
20
|
+
/** Forward a streamed answer as adapter events, recording the assistant turn */
|
|
21
|
+
async function* streamed(events, conv, turn, fallbackModel) {
|
|
22
|
+
let text = '';
|
|
23
|
+
const calls = [];
|
|
24
|
+
let stop;
|
|
25
|
+
let usage;
|
|
26
|
+
let model = fallbackModel;
|
|
27
|
+
try {
|
|
28
|
+
for await (const ev of events) {
|
|
29
|
+
if (ev.type === 'text') {
|
|
30
|
+
text += ev.text;
|
|
31
|
+
if (ev.text.trim())
|
|
32
|
+
yield { type: 'text', text: ev.text.trim() };
|
|
33
|
+
}
|
|
34
|
+
else if (ev.type === 'status') {
|
|
35
|
+
if (ev.text.trim())
|
|
36
|
+
yield { type: 'status', text: ev.text.trim() };
|
|
37
|
+
}
|
|
38
|
+
else if (ev.type === 'toolCall') {
|
|
39
|
+
const raw = ev.call;
|
|
40
|
+
const call = {
|
|
41
|
+
id: raw.id || `call_${turn}_${calls.length}`,
|
|
42
|
+
name: raw.name,
|
|
43
|
+
arguments: typeof raw.arguments === 'string' ? raw.arguments : JSON.stringify(raw.arguments ?? {}),
|
|
44
|
+
};
|
|
45
|
+
calls.push(call);
|
|
46
|
+
yield { type: 'toolStart', id: call.id, name: call.name };
|
|
47
|
+
yield { type: 'toolCall', call };
|
|
48
|
+
}
|
|
49
|
+
else if (ev.type === 'usage') {
|
|
50
|
+
usage = ev.usage;
|
|
51
|
+
if (ev.model)
|
|
52
|
+
model = ev.model;
|
|
53
|
+
}
|
|
54
|
+
else if (ev.type === 'stop') {
|
|
55
|
+
stop = ev.reason;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
finally {
|
|
60
|
+
// What arrived is the assistant turn (also when the stream fails half way)
|
|
61
|
+
conv.last = { text, toolCalls: calls };
|
|
62
|
+
}
|
|
63
|
+
yield { type: 'usage', model, usage };
|
|
64
|
+
yield { type: 'stop', reason: stop ?? (calls.length ? 'tool_calls' : 'end'), toolCallIds: calls.map((c) => c.id) };
|
|
65
|
+
}
|
|
66
|
+
/** An adapter that asks `callModel` for every turn */
|
|
67
|
+
export function adapterFromModelCall(callModel, model = 'custom') {
|
|
68
|
+
let turn = 0;
|
|
69
|
+
return {
|
|
70
|
+
model,
|
|
71
|
+
createConversation(userPrompt) {
|
|
72
|
+
return { messages: [{ role: 'user', content: userPrompt }] };
|
|
73
|
+
},
|
|
74
|
+
async *streamTurn(conv, settings) {
|
|
75
|
+
const answer = callModel({
|
|
76
|
+
system: settings.system.map((p) => p.text).join('\n\n'),
|
|
77
|
+
messages: conv.messages.map((m) => ({ ...m })),
|
|
78
|
+
tools: settings.tools,
|
|
79
|
+
...(settings.maxTokens !== undefined ? { maxTokens: settings.maxTokens } : {}),
|
|
80
|
+
effort: settings.effort,
|
|
81
|
+
signal: settings.signal,
|
|
82
|
+
});
|
|
83
|
+
if (isAsyncIterable(answer)) {
|
|
84
|
+
yield* streamed(answer, conv, turn++, model);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
const response = await answer;
|
|
88
|
+
const calls = withIds(response?.toolCalls, turn++);
|
|
89
|
+
conv.last = { ...response, toolCalls: calls };
|
|
90
|
+
const text = response?.text?.trim();
|
|
91
|
+
if (text)
|
|
92
|
+
yield { type: 'text', text };
|
|
93
|
+
for (const call of calls) {
|
|
94
|
+
yield { type: 'toolStart', id: call.id, name: call.name };
|
|
95
|
+
yield { type: 'toolCall', call };
|
|
96
|
+
}
|
|
97
|
+
yield { type: 'usage', model: response?.model || model, usage: response?.usage };
|
|
98
|
+
yield { type: 'stop', reason: response?.stop ?? (calls.length ? 'tool_calls' : 'end'), toolCallIds: calls.map((c) => c.id) };
|
|
99
|
+
},
|
|
100
|
+
commitAssistantTurn(conv) {
|
|
101
|
+
const last = conv.last;
|
|
102
|
+
if (!last)
|
|
103
|
+
return;
|
|
104
|
+
conv.messages.push({ role: 'assistant', content: last.text ?? '', ...(last.toolCalls?.length ? { toolCalls: last.toolCalls } : {}) });
|
|
105
|
+
conv.last = undefined;
|
|
106
|
+
},
|
|
107
|
+
addToolResults(conv, results) {
|
|
108
|
+
for (const r of results)
|
|
109
|
+
conv.messages.push({ role: 'tool', toolCallId: r.id, content: r.content, ...(r.isError ? { isError: true } : {}) });
|
|
110
|
+
},
|
|
111
|
+
describeError: (err) => (isAbort(err) ? undefined : describeAnyError(err)),
|
|
112
|
+
// Errors from your callModel are reported to the editor, not retried
|
|
113
|
+
isProviderError: () => true,
|
|
114
|
+
isAbortError: isAbort,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=model-call.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-call.js","sourceRoot":"","sources":["../../src/providers/model-call.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAS/C,MAAM,OAAO,GAAG,CAAC,GAAY,EAAE,EAAE,CAC/B,CAAC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C,gBAAgB,CAAC,GAAG,CAAC,CAAC;QAC1E,qEAAqE;QACrE,eAAe,EAAE,GAAG,EAAE,CAAC,IAAI;QAC3B,YAAY,EAAE,OAAO;KACtB,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module providers/openai-format
|
|
3
|
+
*
|
|
4
|
+
* Plain data conversions between the library's `callModel` contract and the
|
|
5
|
+
* OpenAI Chat Completions format, used by most providers (OpenAI, Azure
|
|
6
|
+
* OpenAI, Cerebras, Groq, local servers). No SDK involved:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* callModel: async (req) => {
|
|
10
|
+
* const completion = await openai.chat.completions.create({ model: 'gpt-4o', ...toOpenAIChat(req) }, { signal: req.signal });
|
|
11
|
+
* return fromOpenAIChat(completion);
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* {@link openAICompatibleModel} does the same with `fetch`, for servers you
|
|
16
|
+
* reach by URL and API key. {@link fromOpenAIChatStream} handles a streamed
|
|
17
|
+
* response (tool calls reach the editor while the model is still writing).
|
|
18
|
+
*/
|
|
19
|
+
import type { ModelCall, ModelRequest, ModelResponse, ModelStreamEvent } from './types.js';
|
|
20
|
+
/** Chat Completions request body parts: `messages` and `tools` (add `model` and anything else you need) */
|
|
21
|
+
export interface OpenAIChatParams {
|
|
22
|
+
messages: Array<Record<string, unknown>>;
|
|
23
|
+
tools: Array<{
|
|
24
|
+
type: 'function';
|
|
25
|
+
function: {
|
|
26
|
+
name: string;
|
|
27
|
+
description: string;
|
|
28
|
+
parameters: Record<string, unknown>;
|
|
29
|
+
};
|
|
30
|
+
}>;
|
|
31
|
+
tool_choice: 'auto';
|
|
32
|
+
}
|
|
33
|
+
/** Turn a model request into Chat Completions `messages` + `tools` */
|
|
34
|
+
export declare function toOpenAIChat(request: ModelRequest): OpenAIChatParams;
|
|
35
|
+
/** The subset of a Chat Completions response we read */
|
|
36
|
+
export interface OpenAIChatCompletion {
|
|
37
|
+
model?: string;
|
|
38
|
+
choices?: Array<{
|
|
39
|
+
finish_reason?: string | null;
|
|
40
|
+
message?: {
|
|
41
|
+
content?: string | null;
|
|
42
|
+
refusal?: string | null;
|
|
43
|
+
tool_calls?: Array<{
|
|
44
|
+
id?: string;
|
|
45
|
+
type?: string;
|
|
46
|
+
function?: {
|
|
47
|
+
name?: string;
|
|
48
|
+
arguments?: string;
|
|
49
|
+
};
|
|
50
|
+
}> | null;
|
|
51
|
+
};
|
|
52
|
+
}>;
|
|
53
|
+
usage?: {
|
|
54
|
+
prompt_tokens?: number;
|
|
55
|
+
completion_tokens?: number;
|
|
56
|
+
prompt_tokens_details?: {
|
|
57
|
+
cached_tokens?: number;
|
|
58
|
+
} | null;
|
|
59
|
+
} | null;
|
|
60
|
+
}
|
|
61
|
+
/** Turn a Chat Completions response into the model answer `callModel` returns */
|
|
62
|
+
export declare function fromOpenAIChat(completion: OpenAIChatCompletion): ModelResponse;
|
|
63
|
+
/** The subset of a streamed Chat Completions chunk we read */
|
|
64
|
+
export interface OpenAIChatChunk {
|
|
65
|
+
model?: string;
|
|
66
|
+
choices?: Array<{
|
|
67
|
+
finish_reason?: string | null;
|
|
68
|
+
delta?: {
|
|
69
|
+
content?: string | null;
|
|
70
|
+
refusal?: string | null;
|
|
71
|
+
tool_calls?: Array<{
|
|
72
|
+
index?: number;
|
|
73
|
+
id?: string;
|
|
74
|
+
type?: string;
|
|
75
|
+
function?: {
|
|
76
|
+
name?: string;
|
|
77
|
+
arguments?: string;
|
|
78
|
+
};
|
|
79
|
+
}> | null;
|
|
80
|
+
};
|
|
81
|
+
}>;
|
|
82
|
+
usage?: OpenAIChatCompletion['usage'];
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Turn a streamed Chat Completions response (`stream: true`, ideally with
|
|
86
|
+
* `stream_options: { include_usage: true }`) into `callModel` stream events.
|
|
87
|
+
* Tool-call arguments arrive in pieces per `index`: a call is sent as soon as
|
|
88
|
+
* the next one starts (so the editor applies it while the model keeps
|
|
89
|
+
* writing), the last ones when the stream ends.
|
|
90
|
+
*/
|
|
91
|
+
export declare function fromOpenAIChatStream(chunks: AsyncIterable<OpenAIChatChunk>): AsyncGenerator<ModelStreamEvent>;
|
|
92
|
+
/** Settings of {@link openAICompatibleModel} */
|
|
93
|
+
export interface OpenAICompatibleOptions {
|
|
94
|
+
/** Model id, e.g. "gpt-4o", "gpt-oss-120b" */
|
|
95
|
+
model: string;
|
|
96
|
+
/** API key, sent as `Authorization: Bearer …` (read it from your config, never hard-code it) */
|
|
97
|
+
apiKey?: string;
|
|
98
|
+
/** API base URL (default "https://api.openai.com/v1"; Cerebras "https://api.cerebras.ai/v1", Groq "https://api.groq.com/openai/v1") */
|
|
99
|
+
baseURL?: string;
|
|
100
|
+
/** Extra headers (e.g. Azure `api-key`) */
|
|
101
|
+
headers?: Record<string, string>;
|
|
102
|
+
/** Extra body fields (e.g. `temperature`, `reasoning_effort`) */
|
|
103
|
+
body?: Record<string, unknown>;
|
|
104
|
+
/** Name of the output-cap field when `maxTokens` is set (default "max_completion_tokens") */
|
|
105
|
+
maxTokensParam?: 'max_tokens' | 'max_completion_tokens';
|
|
106
|
+
/** Custom fetch (tests, proxies) */
|
|
107
|
+
fetch?: typeof fetch;
|
|
108
|
+
/**
|
|
109
|
+
* Stream the answer (SSE): each tool call reaches the editor as soon as it
|
|
110
|
+
* is complete, instead of at the end of the turn. Default false.
|
|
111
|
+
*/
|
|
112
|
+
stream?: boolean;
|
|
113
|
+
}
|
|
114
|
+
/** HTTP error from an OpenAI-compatible server (its `status` maps to the usual messages) */
|
|
115
|
+
export declare class OpenAICompatibleError extends Error {
|
|
116
|
+
readonly status: number;
|
|
117
|
+
constructor(status: number, message: string);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* A `callModel` for any OpenAI-compatible Chat Completions endpoint, using
|
|
121
|
+
* `fetch` (no SDK). One request per turn; with `stream: true` the answer is
|
|
122
|
+
* read as Server-Sent Events and tool calls reach the editor as they complete.
|
|
123
|
+
*/
|
|
124
|
+
export declare function openAICompatibleModel(options: OpenAICompatibleOptions): ModelCall;
|
|
125
|
+
//# sourceMappingURL=openai-format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-format.d.ts","sourceRoot":"","sources":["../../src/providers/openai-format.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAwB,MAAM,YAAY,CAAC;AAGjH,2GAA2G;AAC3G,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,EAAE,KAAK,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC,CAAC;IACzC,KAAK,EAAE,KAAK,CAAC;QAAE,IAAI,EAAE,UAAU,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAC;YAAC,WAAW,EAAE,MAAM,CAAC;YAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;SAAE,CAAA;KAAE,CAAC,CAAC;IACzH,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,sEAAsE;AACtE,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,gBAAgB,CAqBpE;AAID,wDAAwD;AACxD,MAAM,WAAW,oBAAoB;IACnC,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,KAAK,CAAC;QACd,aAAa,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;QAC9B,OAAO,CAAC,EAAE;YACR,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;YACxB,OAAO,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;YACxB,UAAU,CAAC,EAAE,KAAK,CAAC;gBAAE,EAAE,CAAC,EAAE,MAAM,CAAC;gBAAC,IAAI,C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C,GAAG,cAAc,CAAC,gBAAgB,CAAC,CA8CpH;AA2BD,gDAAgD;AAChD,MAAM,WAAW,uBAAuB;IACtC,8CAA8C;IAC9C,KAAK,EAAE,MAAM,CAAC;IACd,gGAAgG;IAChG,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,uIAAuI;IACvI,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,2CAA2C;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,iEAAiE;IACjE,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAC/B,6FAA6F;IAC7F,cAAc,CAAC,EAAE,YAAY,GAAG,uBAAuB,CAAC;IACxD,oCAAoC;IACpC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAC;CAClB;AAED,4FAA4F;AAC5F,qBAAa,qBAAsB,SAAQ,KAAK;IAClC,QAAQ,CAAC,MAAM,EAAE,MAAM;gBAAd,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM;CAIrD;AAED;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,OAAO,EAAE,uBAAuB,GAAG,SAAS,CAmCjF"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module providers/openai-format
|
|
3
|
+
*
|
|
4
|
+
* Plain data conversions between the library's `callModel` contract and the
|
|
5
|
+
* OpenAI Chat Completions format, used by most providers (OpenAI, Azure
|
|
6
|
+
* OpenAI, Cerebras, Groq, local servers). No SDK involved:
|
|
7
|
+
*
|
|
8
|
+
* ```ts
|
|
9
|
+
* callModel: async (req) => {
|
|
10
|
+
* const completion = await openai.chat.completions.create({ model: 'gpt-4o', ...toOpenAIChat(req) }, { signal: req.signal });
|
|
11
|
+
* return fromOpenAIChat(completion);
|
|
12
|
+
* }
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* {@link openAICompatibleModel} does the same with `fetch`, for servers you
|
|
16
|
+
* reach by URL and API key. {@link fromOpenAIChatStream} handles a streamed
|
|
17
|
+
* response (tool calls reach the editor while the model is still writing).
|
|
18
|
+
*/
|
|
19
|
+
import { ProviderSetupError } from './types.js';
|
|
20
|
+
/** Turn a model request into Chat Completions `messages` + `tools` */
|
|
21
|
+
export function toOpenAIChat(request) {
|
|
22
|
+
const messages = [{ role: 'system', content: request.system }];
|
|
23
|
+
for (const m of request.messages) {
|
|
24
|
+
if (m.role === 'user')
|
|
25
|
+
messages.push({ role: 'user', content: m.content });
|
|
26
|
+
else if (m.role === 'tool')
|
|
27
|
+
messages.push({ role: 'tool', tool_call_id: m.toolCallId, content: m.content });
|
|
28
|
+
else {
|
|
29
|
+
messages.push({
|
|
30
|
+
role: 'assistant',
|
|
31
|
+
content: m.content || null,
|
|
32
|
+
...(m.toolCalls?.length
|
|
33
|
+
? { tool_calls: m.toolCalls.map((c) => ({ id: c.id, type: 'function', function: { name: c.name, arguments: c.arguments } })) }
|
|
34
|
+
: {}),
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
messages,
|
|
40
|
+
// Not "strict": the operation schema uses anyOf / const / open objects
|
|
41
|
+
tools: request.tools.map((t) => ({ type: 'function', function: { name: t.name, description: t.description, parameters: t.inputSchema } })),
|
|
42
|
+
tool_choice: 'auto',
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
const FINISH = { stop: 'end', tool_calls: 'tool_calls', function_call: 'tool_calls', length: 'max_tokens', content_filter: 'refusal' };
|
|
46
|
+
/** Turn a Chat Completions response into the model answer `callModel` returns */
|
|
47
|
+
export function fromOpenAIChat(completion) {
|
|
48
|
+
const choice = completion.choices?.[0];
|
|
49
|
+
const msg = choice?.message;
|
|
50
|
+
const toolCalls = (msg?.tool_calls ?? [])
|
|
51
|
+
.filter((c) => c.function?.name)
|
|
52
|
+
.map((c, i) => ({ id: c.id || `call_${i}`, name: c.function.name, arguments: c.function?.arguments ?? '{}' }));
|
|
53
|
+
const refused = !!msg?.refusal && !toolCalls.length;
|
|
54
|
+
const u = completion.usage;
|
|
55
|
+
const cached = u?.prompt_tokens_details?.cached_tokens ?? 0;
|
|
56
|
+
return {
|
|
57
|
+
...(msg?.content ? { text: msg.content } : {}),
|
|
58
|
+
toolCalls,
|
|
59
|
+
stop: refused ? 'refusal' : FINISH[choice?.finish_reason ?? ''] ?? (toolCalls.length ? 'tool_calls' : 'end'),
|
|
60
|
+
...(u ? { usage: { input: Math.max(0, (u.prompt_tokens ?? 0) - cached), output: u.completion_tokens ?? 0, cacheRead: cached } } : {}),
|
|
61
|
+
...(completion.model ? { model: completion.model } : {}),
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Turn a streamed Chat Completions response (`stream: true`, ideally with
|
|
66
|
+
* `stream_options: { include_usage: true }`) into `callModel` stream events.
|
|
67
|
+
* Tool-call arguments arrive in pieces per `index`: a call is sent as soon as
|
|
68
|
+
* the next one starts (so the editor applies it while the model keeps
|
|
69
|
+
* writing), the last ones when the stream ends.
|
|
70
|
+
*/
|
|
71
|
+
export async function* fromOpenAIChatStream(chunks) {
|
|
72
|
+
const calls = new Map();
|
|
73
|
+
let sent = -1;
|
|
74
|
+
let text = '';
|
|
75
|
+
let refusal = '';
|
|
76
|
+
let finish;
|
|
77
|
+
let usage;
|
|
78
|
+
let model;
|
|
79
|
+
const flushUpTo = function* (last) {
|
|
80
|
+
for (let i = sent + 1; i <= last; i++) {
|
|
81
|
+
const c = calls.get(i);
|
|
82
|
+
if (c?.name)
|
|
83
|
+
yield { type: 'toolCall', call: { id: c.id || `call_${i}`, name: c.name, arguments: c.args || '{}' } };
|
|
84
|
+
sent = i;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
for await (const chunk of chunks) {
|
|
88
|
+
if (chunk.model)
|
|
89
|
+
model = chunk.model;
|
|
90
|
+
if (chunk.usage)
|
|
91
|
+
usage = chunk.usage;
|
|
92
|
+
const choice = chunk.choices?.[0];
|
|
93
|
+
if (!choice)
|
|
94
|
+
continue;
|
|
95
|
+
const d = choice.delta;
|
|
96
|
+
if (d?.content)
|
|
97
|
+
text += d.content;
|
|
98
|
+
if (d?.refusal)
|
|
99
|
+
refusal += d.refusal;
|
|
100
|
+
for (const tc of d?.tool_calls ?? []) {
|
|
101
|
+
const index = tc.index ?? 0;
|
|
102
|
+
// A new call started: the previous ones are complete
|
|
103
|
+
if (!calls.has(index) && index > sent + 1)
|
|
104
|
+
yield* flushUpTo(index - 1);
|
|
105
|
+
const cur = calls.get(index) ?? { id: '', name: '', args: '' };
|
|
106
|
+
if (tc.id)
|
|
107
|
+
cur.id = tc.id;
|
|
108
|
+
if (tc.function?.name)
|
|
109
|
+
cur.name += tc.function.name;
|
|
110
|
+
if (tc.function?.arguments)
|
|
111
|
+
cur.args += tc.function.arguments;
|
|
112
|
+
calls.set(index, cur);
|
|
113
|
+
}
|
|
114
|
+
if (choice.finish_reason)
|
|
115
|
+
finish = choice.finish_reason;
|
|
116
|
+
}
|
|
117
|
+
if (text.trim())
|
|
118
|
+
yield { type: 'text', text };
|
|
119
|
+
yield* flushUpTo(Math.max(-1, ...calls.keys()));
|
|
120
|
+
if (usage) {
|
|
121
|
+
const cached = usage.prompt_tokens_details?.cached_tokens ?? 0;
|
|
122
|
+
yield { type: 'usage', usage: { input: Math.max(0, (usage.prompt_tokens ?? 0) - cached), output: usage.completion_tokens ?? 0, cacheRead: cached }, ...(model ? { model } : {}) };
|
|
123
|
+
}
|
|
124
|
+
const reason = refusal && !calls.size ? 'refusal' : FINISH[finish ?? ''];
|
|
125
|
+
if (reason)
|
|
126
|
+
yield { type: 'stop', reason };
|
|
127
|
+
}
|
|
128
|
+
/** Read a Server-Sent Events body as JSON payloads (OpenAI-style: `data: {...}`, ends with `[DONE]`) */
|
|
129
|
+
async function* readSse(body) {
|
|
130
|
+
const reader = body.getReader();
|
|
131
|
+
const decoder = new TextDecoder();
|
|
132
|
+
let buffer = '';
|
|
133
|
+
try {
|
|
134
|
+
for (;;) {
|
|
135
|
+
const { value, done } = await reader.read();
|
|
136
|
+
if (done)
|
|
137
|
+
break;
|
|
138
|
+
buffer += decoder.decode(value, { stream: true });
|
|
139
|
+
let nl;
|
|
140
|
+
while ((nl = buffer.indexOf('\n')) >= 0) {
|
|
141
|
+
const line = buffer.slice(0, nl).trim();
|
|
142
|
+
buffer = buffer.slice(nl + 1);
|
|
143
|
+
if (!line.startsWith('data:'))
|
|
144
|
+
continue;
|
|
145
|
+
const data = line.slice(5).trim();
|
|
146
|
+
if (data === '[DONE]')
|
|
147
|
+
return;
|
|
148
|
+
try {
|
|
149
|
+
yield JSON.parse(data);
|
|
150
|
+
}
|
|
151
|
+
catch { /* keep-alive / partial line */ }
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
reader.releaseLock();
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
/** HTTP error from an OpenAI-compatible server (its `status` maps to the usual messages) */
|
|
160
|
+
export class OpenAICompatibleError extends Error {
|
|
161
|
+
constructor(status, message) {
|
|
162
|
+
super(message);
|
|
163
|
+
this.status = status;
|
|
164
|
+
this.name = 'OpenAICompatibleError';
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* A `callModel` for any OpenAI-compatible Chat Completions endpoint, using
|
|
169
|
+
* `fetch` (no SDK). One request per turn; with `stream: true` the answer is
|
|
170
|
+
* read as Server-Sent Events and tool calls reach the editor as they complete.
|
|
171
|
+
*/
|
|
172
|
+
export function openAICompatibleModel(options) {
|
|
173
|
+
const base = (options.baseURL ?? 'https://api.openai.com/v1').replace(/\/+$/, '');
|
|
174
|
+
const doFetch = options.fetch ?? globalThis.fetch;
|
|
175
|
+
if (!doFetch)
|
|
176
|
+
throw new ProviderSetupError('`fetch` is not available: use Node 18+ or pass `fetch`.');
|
|
177
|
+
const send = async (req, stream) => {
|
|
178
|
+
const body = { model: options.model, ...toOpenAIChat(req), ...(options.body ?? {}) };
|
|
179
|
+
if (req.maxTokens !== undefined)
|
|
180
|
+
body[options.maxTokensParam ?? 'max_completion_tokens'] = req.maxTokens;
|
|
181
|
+
if (stream)
|
|
182
|
+
Object.assign(body, { stream: true, stream_options: { include_usage: true } });
|
|
183
|
+
const res = await doFetch(`${base}/chat/completions`, {
|
|
184
|
+
method: 'POST',
|
|
185
|
+
headers: {
|
|
186
|
+
'content-type': 'application/json',
|
|
187
|
+
...(options.apiKey ? { authorization: `Bearer ${options.apiKey}` } : {}),
|
|
188
|
+
...(options.headers ?? {}),
|
|
189
|
+
},
|
|
190
|
+
body: JSON.stringify(body),
|
|
191
|
+
signal: req.signal,
|
|
192
|
+
});
|
|
193
|
+
if (!res.ok) {
|
|
194
|
+
// Only the provider's error message is kept (never request headers / keys)
|
|
195
|
+
let detail = res.statusText;
|
|
196
|
+
try {
|
|
197
|
+
const j = await res.json();
|
|
198
|
+
detail = typeof j.error === 'string' ? j.error : j.error?.message ?? detail;
|
|
199
|
+
}
|
|
200
|
+
catch { /* not JSON */ }
|
|
201
|
+
throw new OpenAICompatibleError(res.status, detail || `HTTP ${res.status}`);
|
|
202
|
+
}
|
|
203
|
+
return res;
|
|
204
|
+
};
|
|
205
|
+
if (!options.stream)
|
|
206
|
+
return async (req) => fromOpenAIChat(await (await send(req, false)).json());
|
|
207
|
+
return (req) => (async function* () {
|
|
208
|
+
const res = await send(req, true);
|
|
209
|
+
if (!res.body)
|
|
210
|
+
throw new OpenAICompatibleError(res.status, 'Empty streaming response');
|
|
211
|
+
yield* fromOpenAIChatStream(readSse(res.body));
|
|
212
|
+
})();
|
|
213
|
+
}
|
|
214
|
+
//# sourceMappingURL=openai-format.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-format.js","sourceRoot":"","sources":["../../src/providers/openai-format.ts"],"names":[],"mappings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