@aparte/provider-openai-compat 0.2.0-alpha.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 aparté
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,21 @@
1
+ # @aparte/provider-openai-compat
2
+
3
+ **One** adapter for every OpenAI-compatible `/chat/completions` endpoint — OpenAI, Mistral,
4
+ OpenRouter, Z.ai, Groq, Together, LM Studio, Ollama (`/v1`) and friends all speak the same
5
+ wire format, so they share a single, **zero-dependency** format adapter. Vendors differ only
6
+ by data (base URL, auth header, branding), passed as config or picked from `presets`.
7
+
8
+ ```ts
9
+ import { createOpenAICompatProvider, presets } from '@aparte/provider-openai-compat';
10
+ import { AparteConfig } from '@aparte/core';
11
+
12
+ AparteConfig.registerAIProvider(createOpenAICompatProvider(presets.OPENROUTER));
13
+ // …or any compat endpoint, no preset needed:
14
+ AparteConfig.registerAIProvider(createOpenAICompatProvider({ id: 'groq', baseURL: 'https://api.groq.com/openai/v1' }));
15
+ ```
16
+
17
+ `@aparte/core` is a **peer dependency**. For vendors outside the OpenAI-compat family
18
+ (Anthropic, Gemini, …) use [`@aparte/provider-ai-sdk`](../ai-sdk) instead.
19
+
20
+ > Part of the [aparté](https://github.com/apartejs/aparte) monorepo. ESM-only.
21
+ > See the **Providers** guide in the docs for the full usage.
@@ -0,0 +1,81 @@
1
+ /**
2
+ * @aparte/provider-openai-compat — ONE adapter for every OpenAI-compatible
3
+ * chat-completions endpoint.
4
+ *
5
+ * The OpenAI `/chat/completions` wire format is the de-facto industry standard:
6
+ * OpenAI, Mistral, OpenRouter, Z.ai, Groq, Together, LM Studio, Ollama (`/v1`)
7
+ * and many more all speak it. This package is the single, zero-dependency
8
+ * format adapter for that family — vendors differ only by DATA (base URL, auth
9
+ * header, branding), which you pass as config (or pick from `presets`).
10
+ *
11
+ * It replaces the per-vendor `@aparte/provider-{openai,mistral,zai,openrouter,
12
+ * lmstudio,ollama}` packages, whose adapter bodies were byte-identical copies
13
+ * (drift even produced real bugs: LM Studio dropped `max_tokens`, Z.ai dropped
14
+ * `seed` — both fixed here by construction, there is only one body now).
15
+ *
16
+ * Model lists are CONSUMER data: pass `models` statically, or rely on the
17
+ * generic `GET {baseURL}/models` fetcher (part of the compat standard). For
18
+ * vendors outside this family (Anthropic, Gemini, …) use the AI-SDK bridge
19
+ * provider instead — this package deliberately covers ONE format.
20
+ */
21
+ import type { AparteAIProvider, AparteAIModel, AparteAIProviderConfigSchema, AparteStreamEvent } from '@aparte/core';
22
+ /** Config for one OpenAI-compatible endpoint. Everything but `id`/`baseURL` is branding/data. */
23
+ export interface OpenAICompatProviderOptions {
24
+ /** Provider id used across aparté (key resolution, model picker, events). */
25
+ id: string;
26
+ /** Endpoint base, e.g. `https://api.openai.com/v1` or `http://localhost:11434/v1`. */
27
+ baseURL: string;
28
+ /** Display name (defaults to `id`). */
29
+ name?: string;
30
+ /** Brand icon (SVG string / data URI / icon-provider key). */
31
+ icon?: string;
32
+ /** Brand color. */
33
+ color?: string;
34
+ /** Short tag line. */
35
+ description?: string;
36
+ /** Where the user gets a key. */
37
+ helpUrl?: string;
38
+ /** Whether the vendor offers free models. */
39
+ hasFreeModels?: boolean;
40
+ /**
41
+ * Local server (LM Studio, Ollama…): key optional, and the generic
42
+ * `/models` fetch runs even without a key.
43
+ */
44
+ isLocal?: boolean;
45
+ /** Static model list (consumer data). Defaults to `[]` — use `fetchModels`. */
46
+ models?: AparteAIModel[];
47
+ /**
48
+ * Extra headers sent on every request (chat + model fetch), e.g.
49
+ * OpenRouter's attribution headers `HTTP-Referer` / `X-Title`.
50
+ */
51
+ extraHeaders?: Record<string, string>;
52
+ /** Override the default apiKey+endpoint settings schema. */
53
+ configSchema?: AparteAIProviderConfigSchema;
54
+ }
55
+ /**
56
+ * Build an `AparteAIProvider` (full format-adapter surface) for one
57
+ * OpenAI-compatible endpoint. Register it like any provider:
58
+ *
59
+ * ```ts
60
+ * import { createOpenAICompatProvider, presets } from '@aparte/provider-openai-compat';
61
+ * AparteConfig.registerAIProvider(createOpenAICompatProvider(presets.OPENROUTER));
62
+ * // or any compat endpoint, no preset needed:
63
+ * AparteConfig.registerAIProvider(createOpenAICompatProvider({ id: 'groq', baseURL: 'https://api.groq.com/openai/v1' }));
64
+ * ```
65
+ */
66
+ export declare function createOpenAICompatProvider(opts: OpenAICompatProviderOptions): AparteAIProvider;
67
+ /**
68
+ * OpenAI-compatible SSE stream parser — this package's own copy of core's
69
+ * `parseOpenAIStream` (the parser follows the format adapter; core keeps only
70
+ * the aparté-native NDJSON parser).
71
+ *
72
+ * Handles:
73
+ * - `delta.content` → text event
74
+ * - `delta.reasoning_content` → thinking event (Qwen3, DeepSeek R1, …)
75
+ * - `delta.tool_calls` → accumulate → tool_use on finish_reason='tool_calls'
76
+ * - usage-only chunk + [DONE] → done{usage}
77
+ */
78
+ export declare function parseOpenAICompatStream(stream: ReadableStream<Uint8Array>): ReadableStream<AparteStreamEvent>;
79
+ export * from './presets.js';
80
+ export type { AparteAIProvider, AparteAIModel } from '@aparte/core';
81
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,KAAK,EACR,gBAAgB,EAChB,aAAa,EACb,4BAA4B,EAM5B,iBAAiB,EAEpB,MAAM,cAAc,CAAC;AAKtB,iGAAiG;AACjG,MAAM,WAAW,2BAA2B;IACxC,6EAA6E;IAC7E,EAAE,EAAE,MAAM,CAAC;IACX,sFAAsF;IACtF,OAAO,EAAE,MAAM,CAAC;IAChB,uCAAuC;IACvC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,8DAA8D;IAC9D,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,mBAAmB;IACnB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,sBAAsB;IACtB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,iCAAiC;IACjC,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,6CAA6C;IAC7C,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,+EAA+E;IAC/E,MAAM,CAAC,EAAE,aAAa,EAAE,CAAC;IACzB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACtC,4DAA4D;IAC5D,YAAY,CAAC,EAAE,4BAA4B,CAAC;CAC/C;AA8DD;;;;;;;;;;GAUG;AACH,wBAAgB,0BAA0B,CAAC,IAAI,EAAE,2BAA2B,GAAG,gBAAgB,CA2F9F;AAID;;;;;;;;;;GAUG;AACH,wBAAgB,uBAAuB,CACnC,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACnC,cAAc,CAAC,iBAAiB,CAAC,CAmHnC;AAED,cAAc,cAAc,CAAC;AAC7B,YAAY,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,297 @@
1
+ import { contentToText } from "@aparte/core";
2
+ const OPENAI = {
3
+ id: "openai",
4
+ baseURL: "https://api.openai.com/v1",
5
+ name: "OpenAI",
6
+ icon: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2a10 10 0 1 0 10 10A10 10 0 0 0 12 2zm0 18a8 8 0 1 1 8-8 8 8 0 0 1-8 8z"/><path d="M12 6v6l4 2"/></svg>`,
7
+ color: "#10a37f",
8
+ description: "GPT-4o, GPT-4 Turbo, and the O1 reasoning models",
9
+ helpUrl: "https://platform.openai.com/api-keys"
10
+ };
11
+ const MISTRAL = {
12
+ id: "mistral",
13
+ baseURL: "https://api.mistral.ai/v1",
14
+ name: "Mistral AI",
15
+ icon: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2l4 22-4-8-4 8 4-22z"/></svg>`,
16
+ color: "#fd7e14",
17
+ description: "European AI: Mistral Large, Mixtral, Codestral",
18
+ helpUrl: "https://console.mistral.ai/api-keys/"
19
+ };
20
+ const ZAI = {
21
+ id: "zai",
22
+ baseURL: "https://open.bigmodel.cn/api/paas/v4",
23
+ name: "Z.ai (Zhipu)",
24
+ icon: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/></svg>`,
25
+ color: "#6366f1",
26
+ description: "Access Zhipu AI's free GLM models",
27
+ hasFreeModels: true,
28
+ helpUrl: "https://open.bigmodel.cn"
29
+ };
30
+ const OPENROUTER = {
31
+ id: "openrouter",
32
+ baseURL: "https://openrouter.ai/api/v1",
33
+ name: "OpenRouter",
34
+ icon: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><circle cx="12" cy="12" r="10"/><path d="M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20"/><path d="M2 12h20"/></svg>`,
35
+ color: "#000000",
36
+ description: "Access 100+ AI models through a single API",
37
+ hasFreeModels: true,
38
+ helpUrl: "https://openrouter.ai/keys",
39
+ // OpenRouter attribution headers (sent on chat + model fetch).
40
+ extraHeaders: {
41
+ "HTTP-Referer": typeof window !== "undefined" ? window.location.origin : "",
42
+ "X-Title": "aparté"
43
+ }
44
+ };
45
+ const LMSTUDIO = {
46
+ id: "lmstudio",
47
+ baseURL: "http://localhost:1234/v1",
48
+ name: "LM Studio",
49
+ icon: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><rect x="4" y="4" width="16" height="16" rx="2"/><path d="M9 9h6v6H9z"/><path d="M9 1h1v3h-1zM14 1h1v3h-1zM9 20h1v3h-1zM14 20h1v3h-1zM20 9h3v1h-3zM20 14h3v1h-3zM1 9h3v1H1zM1 14h3v1H1z"/></svg>`,
50
+ color: "#444444",
51
+ description: "Run LLMs locally with the LM Studio app",
52
+ hasFreeModels: true,
53
+ isLocal: true,
54
+ helpUrl: "https://lmstudio.ai/"
55
+ };
56
+ const OLLAMA = {
57
+ id: "ollama",
58
+ baseURL: "http://localhost:11434/v1",
59
+ name: "Ollama",
60
+ icon: `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M12 2a10 10 0 0 1 10 10 10 10 0 0 1-10 10 10 10 0 0 1-10-10 10 10 0 0 1 10-10zM12 11l4 2-4 2-4-2 4-2zM12 6v5M12 15v3"/></svg>`,
61
+ color: "#57534e",
62
+ description: "Run LLMs locally — free and fully private",
63
+ hasFreeModels: true,
64
+ isLocal: true,
65
+ helpUrl: "https://ollama.com/"
66
+ };
67
+ const presets = { OPENAI, MISTRAL, ZAI, OPENROUTER, LMSTUDIO, OLLAMA };
68
+ function toOpenAIContent(content) {
69
+ if (typeof content === "string") return content;
70
+ return content.map((p) => {
71
+ if (p.type === "text") return { type: "text", text: p.text };
72
+ if (p.type === "image") return { type: "image_url", image_url: { url: p.image } };
73
+ return { type: "text", text: "" };
74
+ });
75
+ }
76
+ function toOpenAIMessages(messages) {
77
+ return messages.map((msg) => {
78
+ if (msg.role === "tool_call") {
79
+ return {
80
+ role: "assistant",
81
+ content: msg.precedingText ?? null,
82
+ tool_calls: (msg.toolCalls ?? []).map((tc) => ({
83
+ id: tc.id,
84
+ type: "function",
85
+ function: { name: tc.name, arguments: JSON.stringify(tc.input) }
86
+ }))
87
+ };
88
+ }
89
+ if (msg.role === "tool_result") {
90
+ return { role: "tool", tool_call_id: msg.toolCallId, content: contentToText(msg.content) };
91
+ }
92
+ return { role: msg.role, content: toOpenAIContent(msg.content) };
93
+ });
94
+ }
95
+ function toOpenAITools(tools) {
96
+ return tools.map((t) => ({
97
+ type: "function",
98
+ function: { name: t.name, description: t.description, parameters: t.inputSchema }
99
+ }));
100
+ }
101
+ function bearer(key) {
102
+ return key.startsWith("Bearer ") ? key : `Bearer ${key}`;
103
+ }
104
+ const DEFAULT_CONFIG_SCHEMA = (opts) => ({
105
+ fields: opts.isLocal ? [
106
+ { id: "endpoint", type: "url", label: "Server", defaultValue: opts.baseURL, required: true },
107
+ { id: "apiKey", type: "password", label: "API Key / Token (optional)", placeholder: "Bearer ...", isAdvanced: true }
108
+ ] : [
109
+ { id: "apiKey", type: "password", label: "API Key", placeholder: "sk-...", required: true },
110
+ { id: "endpoint", type: "url", label: "Custom endpoint", placeholder: opts.baseURL, isAdvanced: true }
111
+ ]
112
+ });
113
+ function createOpenAICompatProvider(opts) {
114
+ const displayName = opts.name ?? opts.id;
115
+ return {
116
+ id: opts.id,
117
+ getMetadata() {
118
+ return {
119
+ id: opts.id,
120
+ name: displayName,
121
+ icon: opts.icon,
122
+ color: opts.color,
123
+ description: opts.description,
124
+ helpUrl: opts.helpUrl,
125
+ hasFreeModels: opts.hasFreeModels,
126
+ isLocal: opts.isLocal,
127
+ configSchema: opts.configSchema ?? DEFAULT_CONFIG_SCHEMA(opts)
128
+ };
129
+ },
130
+ getModels() {
131
+ return opts.models ?? [];
132
+ },
133
+ /**
134
+ * Generic `GET {baseURL}/models` — part of the compat standard. Cloud
135
+ * endpoints need a key (returns `[]` without one); local servers fetch
136
+ * keyless. Vendor-specific niceties (pricing, name prettifying) are
137
+ * consumer concerns: pass `models` yourself for anything fancier.
138
+ */
139
+ async fetchModels(config) {
140
+ const apiKey = typeof config === "string" ? config : config?.["apiKey"];
141
+ const endpoint = (typeof config === "object" ? config?.["endpoint"] : null) || opts.baseURL;
142
+ if (!apiKey && !opts.isLocal) return [];
143
+ try {
144
+ const headers = { ...opts.extraHeaders };
145
+ if (apiKey) headers["Authorization"] = bearer(apiKey);
146
+ const response = await fetch(`${endpoint}/models`, { headers });
147
+ if (!response.ok) throw new Error(`HTTP ${response.status}`);
148
+ const data = await response.json();
149
+ return (data.data ?? []).map((m) => ({
150
+ id: m.id,
151
+ name: m.name || m.id,
152
+ contextWindow: m.context_length,
153
+ capabilities: ["streaming"]
154
+ }));
155
+ } catch (error) {
156
+ console.error(`[${displayName}] Failed to fetch models:`, error);
157
+ return [];
158
+ }
159
+ },
160
+ // ── Format-adapter surface (transport ⊥ format) ──────────────────────
161
+ // The vendor concern only: request shape + stream parsing. Auth and
162
+ // network are the transport's job (DirectTransport / BackendTransport).
163
+ defaultEndpoint: opts.baseURL,
164
+ buildRequest(request) {
165
+ const body = {
166
+ model: request.modelId,
167
+ messages: toOpenAIMessages(request.messages),
168
+ temperature: request.temperature,
169
+ max_tokens: request.maxTokens,
170
+ stream: request.stream ?? true,
171
+ ...request.stream ?? true ? { stream_options: { include_usage: true } } : {},
172
+ ...request.seed !== void 0 ? { seed: request.seed } : {}
173
+ };
174
+ if (request.tools?.length) {
175
+ body["tools"] = toOpenAITools(request.tools);
176
+ body["tool_choice"] = "auto";
177
+ }
178
+ return {
179
+ path: "/chat/completions",
180
+ body,
181
+ ...opts.extraHeaders ? { headers: opts.extraHeaders } : {}
182
+ };
183
+ },
184
+ authHeaders(key) {
185
+ return { Authorization: bearer(key) };
186
+ },
187
+ parseStream(body) {
188
+ return parseOpenAICompatStream(body);
189
+ },
190
+ parseText(json) {
191
+ return json?.choices?.[0]?.message?.content || "";
192
+ }
193
+ };
194
+ }
195
+ function parseOpenAICompatStream(stream) {
196
+ const decoder = new TextDecoder();
197
+ let buffer = "";
198
+ const toolCallsById = {};
199
+ let capturedUsage;
200
+ let reader = null;
201
+ return new ReadableStream({
202
+ async start(controller) {
203
+ reader = stream.getReader();
204
+ try {
205
+ while (true) {
206
+ const { done, value } = await reader.read();
207
+ if (done) break;
208
+ buffer += decoder.decode(value, { stream: true });
209
+ const lines = buffer.split("\n");
210
+ buffer = lines.pop() ?? "";
211
+ for (const line of lines) {
212
+ const trimmed = line.trim();
213
+ if (!trimmed.startsWith("data:")) continue;
214
+ const raw = trimmed.slice(5).trim();
215
+ if (raw === "[DONE]") {
216
+ controller.enqueue({ type: "done", usage: capturedUsage });
217
+ return;
218
+ }
219
+ try {
220
+ const json = JSON.parse(raw);
221
+ if (json.usage) {
222
+ capturedUsage = {
223
+ inputTokens: json.usage.prompt_tokens ?? 0,
224
+ outputTokens: json.usage.completion_tokens ?? 0,
225
+ totalTokens: json.usage.total_tokens,
226
+ cacheReadTokens: json.usage.prompt_tokens_details?.cached_tokens
227
+ };
228
+ }
229
+ const choice = json.choices?.[0];
230
+ if (!choice) continue;
231
+ const delta = choice.delta;
232
+ if (delta) {
233
+ if (delta.reasoning_content) {
234
+ controller.enqueue({ type: "thinking", delta: delta.reasoning_content });
235
+ }
236
+ if (delta.content) {
237
+ controller.enqueue({ type: "text", delta: delta.content });
238
+ }
239
+ if (delta.tool_calls) {
240
+ for (const tc of delta.tool_calls) {
241
+ const idx = tc.index ?? 0;
242
+ if (!toolCallsById[idx]) {
243
+ toolCallsById[idx] = { id: tc.id ?? "", name: tc.function?.name ?? "", args: "" };
244
+ }
245
+ if (tc.id) toolCallsById[idx].id = tc.id;
246
+ if (tc.function?.name) toolCallsById[idx].name = tc.function.name;
247
+ if (tc.function?.arguments) toolCallsById[idx].args += tc.function.arguments;
248
+ }
249
+ }
250
+ }
251
+ if (choice.finish_reason === "tool_calls") {
252
+ for (const entry of Object.values(toolCallsById)) {
253
+ let input = {};
254
+ try {
255
+ input = JSON.parse(entry.args);
256
+ } catch {
257
+ console.warn(
258
+ `[openai-compat] Tool "${entry.name}" returned malformed arguments JSON; passing empty input. Raw:`,
259
+ entry.args
260
+ );
261
+ }
262
+ const toolCall = { id: entry.id, name: entry.name, input };
263
+ controller.enqueue({ type: "tool_use", ...toolCall });
264
+ }
265
+ controller.enqueue({ type: "done", usage: capturedUsage });
266
+ return;
267
+ }
268
+ } catch {
269
+ console.warn("[openai-compat] Skipped an unparseable SSE data line:", raw);
270
+ }
271
+ }
272
+ }
273
+ controller.enqueue({ type: "done", usage: capturedUsage });
274
+ } catch (err) {
275
+ controller.enqueue({ type: "error", message: err?.message ?? "Stream error" });
276
+ } finally {
277
+ reader?.releaseLock();
278
+ reader = null;
279
+ controller.close();
280
+ }
281
+ },
282
+ // Consumer cancelled (e.g. user hit "stop"): cancel the underlying reader so
283
+ // the vendor response body stops being drained to its natural end instead of
284
+ // silently finishing the whole SSE stream in the background.
285
+ cancel(reason) {
286
+ const r = reader;
287
+ reader = null;
288
+ return r?.cancel(reason);
289
+ }
290
+ });
291
+ }
292
+ export {
293
+ createOpenAICompatProvider,
294
+ parseOpenAICompatStream,
295
+ presets
296
+ };
297
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../src/presets.ts","../src/index.ts"],"sourcesContent":["/**\n * presets.ts — vendor DATA for well-known OpenAI-compatible endpoints.\n *\n * A preset is nothing but an `OpenAICompatProviderOptions` literal: base URL +\n * branding (icon/color/helpUrl) carried over from the retired per-vendor\n * packages. No code varies per vendor — that is the whole point.\n *\n * ```ts\n * AparteConfig.registerAIProvider(createOpenAICompatProvider(presets.MISTRAL));\n * ```\n *\n * Local servers (LM Studio, Ollama) are served through their OpenAI-compat\n * `/v1` endpoints — same format, `isLocal` just relaxes the key requirement.\n */\n\nimport type { OpenAICompatProviderOptions } from './index.js';\n\nconst OPENAI: OpenAICompatProviderOptions = {\n id: 'openai',\n baseURL: 'https://api.openai.com/v1',\n name: 'OpenAI',\n icon: `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><path d=\"M12 2a10 10 0 1 0 10 10A10 10 0 0 0 12 2zm0 18a8 8 0 1 1 8-8 8 8 0 0 1-8 8z\"/><path d=\"M12 6v6l4 2\"/></svg>`,\n color: '#10a37f',\n description: 'GPT-4o, GPT-4 Turbo, and the O1 reasoning models',\n helpUrl: 'https://platform.openai.com/api-keys',\n};\n\nconst MISTRAL: OpenAICompatProviderOptions = {\n id: 'mistral',\n baseURL: 'https://api.mistral.ai/v1',\n name: 'Mistral AI',\n icon: `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><path d=\"M12 2l4 22-4-8-4 8 4-22z\"/></svg>`,\n color: '#fd7e14',\n description: 'European AI: Mistral Large, Mixtral, Codestral',\n helpUrl: 'https://console.mistral.ai/api-keys/',\n};\n\nconst ZAI: OpenAICompatProviderOptions = {\n id: 'zai',\n baseURL: 'https://open.bigmodel.cn/api/paas/v4',\n name: 'Z.ai (Zhipu)',\n icon: `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><path d=\"M13 2L3 14h9l-1 8 10-12h-9l1-8z\"/></svg>`,\n color: '#6366f1',\n description: \"Access Zhipu AI's free GLM models\",\n hasFreeModels: true,\n helpUrl: 'https://open.bigmodel.cn',\n};\n\nconst OPENROUTER: OpenAICompatProviderOptions = {\n id: 'openrouter',\n baseURL: 'https://openrouter.ai/api/v1',\n name: 'OpenRouter',\n icon: `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><circle cx=\"12\" cy=\"12\" r=\"10\"/><path d=\"M12 2a14.5 14.5 0 0 0 0 20 14.5 14.5 0 0 0 0-20\"/><path d=\"M2 12h20\"/></svg>`,\n color: '#000000',\n description: 'Access 100+ AI models through a single API',\n hasFreeModels: true,\n helpUrl: 'https://openrouter.ai/keys',\n // OpenRouter attribution headers (sent on chat + model fetch).\n extraHeaders: {\n 'HTTP-Referer': typeof window !== 'undefined' ? window.location.origin : '',\n 'X-Title': 'aparté',\n },\n};\n\nconst LMSTUDIO: OpenAICompatProviderOptions = {\n id: 'lmstudio',\n baseURL: 'http://localhost:1234/v1',\n name: 'LM Studio',\n icon: `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><rect x=\"4\" y=\"4\" width=\"16\" height=\"16\" rx=\"2\"/><path d=\"M9 9h6v6H9z\"/><path d=\"M9 1h1v3h-1zM14 1h1v3h-1zM9 20h1v3h-1zM14 20h1v3h-1zM20 9h3v1h-3zM20 14h3v1h-3zM1 9h3v1H1zM1 14h3v1H1z\"/></svg>`,\n color: '#444444',\n description: 'Run LLMs locally with the LM Studio app',\n hasFreeModels: true,\n isLocal: true,\n helpUrl: 'https://lmstudio.ai/',\n};\n\n// Ollama through its OpenAI-compat endpoint (`/v1`), NOT its native `/api/chat`.\n// Same chat-completions format as everyone else; the native-protocol niceties\n// (inline base64 images, Ollama-shaped tool calls, keep_alive) don't apply —\n// see the README's Ollama section for the behavioural delta.\nconst OLLAMA: OpenAICompatProviderOptions = {\n id: 'ollama',\n baseURL: 'http://localhost:11434/v1',\n name: 'Ollama',\n icon: `<svg viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" stroke-width=\"2\"><path d=\"M12 2a10 10 0 0 1 10 10 10 10 0 0 1-10 10 10 10 0 0 1-10-10 10 10 0 0 1 10-10zM12 11l4 2-4 2-4-2 4-2zM12 6v5M12 15v3\"/></svg>`,\n color: '#57534e',\n description: 'Run LLMs locally — free and fully private',\n hasFreeModels: true,\n isLocal: true,\n helpUrl: 'https://ollama.com/',\n};\n\n/** Vendor presets for {@link createOpenAICompatProvider}. Pure data. */\nexport const presets = { OPENAI, MISTRAL, ZAI, OPENROUTER, LMSTUDIO, OLLAMA } as const;\n","/**\n * @aparte/provider-openai-compat — ONE adapter for every OpenAI-compatible\n * chat-completions endpoint.\n *\n * The OpenAI `/chat/completions` wire format is the de-facto industry standard:\n * OpenAI, Mistral, OpenRouter, Z.ai, Groq, Together, LM Studio, Ollama (`/v1`)\n * and many more all speak it. This package is the single, zero-dependency\n * format adapter for that family — vendors differ only by DATA (base URL, auth\n * header, branding), which you pass as config (or pick from `presets`).\n *\n * It replaces the per-vendor `@aparte/provider-{openai,mistral,zai,openrouter,\n * lmstudio,ollama}` packages, whose adapter bodies were byte-identical copies\n * (drift even produced real bugs: LM Studio dropped `max_tokens`, Z.ai dropped\n * `seed` — both fixed here by construction, there is only one body now).\n *\n * Model lists are CONSUMER data: pass `models` statically, or rely on the\n * generic `GET {baseURL}/models` fetcher (part of the compat standard). For\n * vendors outside this family (Anthropic, Gemini, …) use the AI-SDK bridge\n * provider instead — this package deliberately covers ONE format.\n */\n\nimport type {\n AparteAIProvider,\n AparteAIModel,\n AparteAIProviderConfigSchema,\n AparteChatRequest,\n AparteChatMessage,\n AparteTool,\n AparteToolCall,\n AparteContentPart,\n AparteStreamEvent,\n AparteUsage,\n} from '@aparte/core';\nimport { contentToText } from '@aparte/core';\n\n// ─── Options ─────────────────────────────────────────────────────────────────\n\n/** Config for one OpenAI-compatible endpoint. Everything but `id`/`baseURL` is branding/data. */\nexport interface OpenAICompatProviderOptions {\n /** Provider id used across aparté (key resolution, model picker, events). */\n id: string;\n /** Endpoint base, e.g. `https://api.openai.com/v1` or `http://localhost:11434/v1`. */\n baseURL: string;\n /** Display name (defaults to `id`). */\n name?: string;\n /** Brand icon (SVG string / data URI / icon-provider key). */\n icon?: string;\n /** Brand color. */\n color?: string;\n /** Short tag line. */\n description?: string;\n /** Where the user gets a key. */\n helpUrl?: string;\n /** Whether the vendor offers free models. */\n hasFreeModels?: boolean;\n /**\n * Local server (LM Studio, Ollama…): key optional, and the generic\n * `/models` fetch runs even without a key.\n */\n isLocal?: boolean;\n /** Static model list (consumer data). Defaults to `[]` — use `fetchModels`. */\n models?: AparteAIModel[];\n /**\n * Extra headers sent on every request (chat + model fetch), e.g.\n * OpenRouter's attribution headers `HTTP-Referer` / `X-Title`.\n */\n extraHeaders?: Record<string, string>;\n /** Override the default apiKey+endpoint settings schema. */\n configSchema?: AparteAIProviderConfigSchema;\n}\n\n// ─── Message / tool shaping (the format half) ────────────────────────────────\n\n/** Content → OpenAI multipart array when images are present. */\nfunction toOpenAIContent(content: string | AparteContentPart[]): unknown {\n if (typeof content === 'string') return content;\n return content.map(p => {\n if (p.type === 'text') return { type: 'text', text: p.text };\n if (p.type === 'image') return { type: 'image_url', image_url: { url: p.image } };\n return { type: 'text', text: '' }; // AparteFilePart — no inline-file support in the compat format\n });\n}\n\n/** AparteChatMessage[] → OpenAI messages (incl. the tool_call / tool_result envelope). */\nfunction toOpenAIMessages(messages: AparteChatMessage[]): unknown[] {\n return messages.map(msg => {\n if (msg.role === 'tool_call') {\n return {\n role: 'assistant',\n content: msg.precedingText ?? null,\n tool_calls: (msg.toolCalls ?? []).map(tc => ({\n id: tc.id,\n type: 'function',\n function: { name: tc.name, arguments: JSON.stringify(tc.input) },\n })),\n };\n }\n if (msg.role === 'tool_result') {\n return { role: 'tool', tool_call_id: msg.toolCallId, content: contentToText(msg.content) };\n }\n return { role: msg.role, content: toOpenAIContent(msg.content) };\n });\n}\n\n/** AparteTool[] → OpenAI function-tool declarations. */\nfunction toOpenAITools(tools: AparteTool[]): unknown[] {\n return tools.map(t => ({\n type: 'function',\n function: { name: t.name, description: t.description, parameters: t.inputSchema },\n }));\n}\n\n/** Accept both raw keys and user-pasted `Bearer xxx` values (local servers). */\nfunction bearer(key: string): string {\n return key.startsWith('Bearer ') ? key : `Bearer ${key}`;\n}\n\nconst DEFAULT_CONFIG_SCHEMA = (opts: OpenAICompatProviderOptions): AparteAIProviderConfigSchema => ({\n fields: opts.isLocal\n ? [\n { id: 'endpoint', type: 'url', label: 'Server', defaultValue: opts.baseURL, required: true },\n { id: 'apiKey', type: 'password', label: 'API Key / Token (optional)', placeholder: 'Bearer ...', isAdvanced: true },\n ]\n : [\n { id: 'apiKey', type: 'password', label: 'API Key', placeholder: 'sk-...', required: true },\n { id: 'endpoint', type: 'url', label: 'Custom endpoint', placeholder: opts.baseURL, isAdvanced: true },\n ],\n});\n\n// ─── The factory ─────────────────────────────────────────────────────────────\n\n/**\n * Build an `AparteAIProvider` (full format-adapter surface) for one\n * OpenAI-compatible endpoint. Register it like any provider:\n *\n * ```ts\n * import { createOpenAICompatProvider, presets } from '@aparte/provider-openai-compat';\n * AparteConfig.registerAIProvider(createOpenAICompatProvider(presets.OPENROUTER));\n * // or any compat endpoint, no preset needed:\n * AparteConfig.registerAIProvider(createOpenAICompatProvider({ id: 'groq', baseURL: 'https://api.groq.com/openai/v1' }));\n * ```\n */\nexport function createOpenAICompatProvider(opts: OpenAICompatProviderOptions): AparteAIProvider {\n const displayName = opts.name ?? opts.id;\n\n return {\n id: opts.id,\n\n getMetadata() {\n return {\n id: opts.id,\n name: displayName,\n icon: opts.icon,\n color: opts.color,\n description: opts.description,\n helpUrl: opts.helpUrl,\n hasFreeModels: opts.hasFreeModels,\n isLocal: opts.isLocal,\n configSchema: opts.configSchema ?? DEFAULT_CONFIG_SCHEMA(opts),\n };\n },\n\n getModels(): AparteAIModel[] {\n return opts.models ?? [];\n },\n\n /**\n * Generic `GET {baseURL}/models` — part of the compat standard. Cloud\n * endpoints need a key (returns `[]` without one); local servers fetch\n * keyless. Vendor-specific niceties (pricing, name prettifying) are\n * consumer concerns: pass `models` yourself for anything fancier.\n */\n async fetchModels(config?: string | Record<string, string>): Promise<AparteAIModel[]> {\n const apiKey = typeof config === 'string' ? config : config?.['apiKey'];\n const endpoint = (typeof config === 'object' ? config?.['endpoint'] : null) || opts.baseURL;\n if (!apiKey && !opts.isLocal) return [];\n\n try {\n const headers: Record<string, string> = { ...opts.extraHeaders };\n if (apiKey) headers['Authorization'] = bearer(apiKey);\n const response = await fetch(`${endpoint}/models`, { headers });\n if (!response.ok) throw new Error(`HTTP ${response.status}`);\n const data = await response.json() as { data?: Array<{ id: string; name?: string; context_length?: number }> };\n return (data.data ?? []).map(m => ({\n id: m.id,\n name: m.name || m.id,\n contextWindow: m.context_length,\n capabilities: ['streaming'],\n }));\n } catch (error) {\n console.error(`[${displayName}] Failed to fetch models:`, error);\n return [];\n }\n },\n\n // ── Format-adapter surface (transport ⊥ format) ──────────────────────\n // The vendor concern only: request shape + stream parsing. Auth and\n // network are the transport's job (DirectTransport / BackendTransport).\n defaultEndpoint: opts.baseURL,\n\n buildRequest(request: AparteChatRequest) {\n const body: Record<string, unknown> = {\n model: request.modelId,\n messages: toOpenAIMessages(request.messages),\n temperature: request.temperature,\n max_tokens: request.maxTokens,\n stream: request.stream ?? true,\n ...((request.stream ?? true) ? { stream_options: { include_usage: true } } : {}),\n ...(request.seed !== undefined ? { seed: request.seed } : {}),\n };\n if (request.tools?.length) {\n body['tools'] = toOpenAITools(request.tools);\n body['tool_choice'] = 'auto';\n }\n return {\n path: '/chat/completions',\n body,\n ...(opts.extraHeaders ? { headers: opts.extraHeaders } : {}),\n };\n },\n\n authHeaders(key: string) {\n return { Authorization: bearer(key) };\n },\n\n parseStream(body: ReadableStream<Uint8Array>) {\n return parseOpenAICompatStream(body);\n },\n\n parseText(json: unknown): string {\n return (json as { choices?: Array<{ message?: { content?: string } }> })?.choices?.[0]?.message?.content || '';\n },\n };\n}\n\n// ─── SSE stream parser (ported from @aparte/core parseOpenAIStream) ──────────\n\n/**\n * OpenAI-compatible SSE stream parser — this package's own copy of core's\n * `parseOpenAIStream` (the parser follows the format adapter; core keeps only\n * the aparté-native NDJSON parser).\n *\n * Handles:\n * - `delta.content` → text event\n * - `delta.reasoning_content` → thinking event (Qwen3, DeepSeek R1, …)\n * - `delta.tool_calls` → accumulate → tool_use on finish_reason='tool_calls'\n * - usage-only chunk + [DONE] → done{usage}\n */\nexport function parseOpenAICompatStream(\n stream: ReadableStream<Uint8Array>,\n): ReadableStream<AparteStreamEvent> {\n const decoder = new TextDecoder();\n let buffer = '';\n\n // Tool call accumulation state (keyed by index)\n const toolCallsById: Record<number, { id: string; name: string; args: string }> = {};\n let capturedUsage: AparteUsage | undefined;\n let reader: ReadableStreamDefaultReader<Uint8Array> | null = null;\n\n return new ReadableStream<AparteStreamEvent>({\n async start(controller) {\n reader = stream.getReader();\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n const lines = buffer.split('\\n');\n buffer = lines.pop() ?? '';\n\n for (const line of lines) {\n const trimmed = line.trim();\n if (!trimmed.startsWith('data:')) continue;\n const raw = trimmed.slice(5).trim();\n if (raw === '[DONE]') {\n controller.enqueue({ type: 'done', usage: capturedUsage });\n return;\n }\n try {\n const json = JSON.parse(raw);\n\n // Capture usage from the usage-only chunk (choices: [])\n if (json.usage) {\n capturedUsage = {\n inputTokens: json.usage.prompt_tokens ?? 0,\n outputTokens: json.usage.completion_tokens ?? 0,\n totalTokens: json.usage.total_tokens,\n cacheReadTokens: json.usage.prompt_tokens_details?.cached_tokens,\n };\n }\n\n const choice = json.choices?.[0];\n if (!choice) continue;\n\n const delta = choice.delta;\n if (delta) {\n if (delta.reasoning_content) {\n controller.enqueue({ type: 'thinking', delta: delta.reasoning_content });\n }\n if (delta.content) {\n controller.enqueue({ type: 'text', delta: delta.content });\n }\n if (delta.tool_calls) {\n for (const tc of delta.tool_calls) {\n const idx: number = tc.index ?? 0;\n if (!toolCallsById[idx]) {\n toolCallsById[idx] = { id: tc.id ?? '', name: tc.function?.name ?? '', args: '' };\n }\n if (tc.id) toolCallsById[idx].id = tc.id;\n if (tc.function?.name) toolCallsById[idx].name = tc.function.name;\n if (tc.function?.arguments) toolCallsById[idx].args += tc.function.arguments;\n }\n }\n }\n\n // Emit tool_use events when the turn is done\n if (choice.finish_reason === 'tool_calls') {\n for (const entry of Object.values(toolCallsById)) {\n let input: Record<string, unknown> = {};\n // finish_reason is 'tool_calls' → the model considers the call\n // COMPLETE, so a parse failure here is malformed tool-call JSON\n // (a real small-model failure), not partial streaming. Surface it\n // instead of silently handing the tool `{}`.\n try {\n input = JSON.parse(entry.args);\n } catch {\n console.warn(\n `[openai-compat] Tool \"${entry.name}\" returned malformed arguments JSON; ` +\n `passing empty input. Raw:`, entry.args,\n );\n }\n const toolCall: AparteToolCall = { id: entry.id, name: entry.name, input };\n controller.enqueue({ type: 'tool_use', ...toolCall });\n }\n controller.enqueue({ type: 'done', usage: capturedUsage });\n return;\n }\n } catch {\n // Every line here is a complete, newline-terminated SSE\n // line (see buffer split above), so a parse failure is an\n // unexpected/malformed server payload, not partial JSON —\n // log a breadcrumb instead of dropping it silently.\n console.warn('[openai-compat] Skipped an unparseable SSE data line:', raw);\n }\n }\n }\n controller.enqueue({ type: 'done', usage: capturedUsage });\n } catch (err: unknown) {\n controller.enqueue({ type: 'error', message: (err as Error | undefined)?.message ?? 'Stream error' });\n } finally {\n reader?.releaseLock();\n reader = null;\n controller.close();\n }\n },\n // Consumer cancelled (e.g. user hit \"stop\"): cancel the underlying reader so\n // the vendor response body stops being drained to its natural end instead of\n // silently finishing the whole SSE stream in the background.\n cancel(reason) {\n const r = reader;\n reader = null;\n return r?.cancel(reason);\n },\n });\n}\n\nexport * from './presets.js';\nexport type { AparteAIProvider, AparteAIModel } from '@aparte/core';\n"],"names":[],"mappings":";AAiBA,MAAM,SAAsC;AAAA,EACxC,IAAI;AAAA,EACJ,SAAS;AAAA,EACT,MAAM;AAAA,EACN,MAAM;AAAA,EACN,OAAO;AAAA,EACP,aAAa;AAAA,EACb,SAAS;AACb;AAEA,MAAM,UAAuC;AAAA,EACzC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,EAAA,CACH;AACL;"}
@@ -0,0 +1,25 @@
1
+ /**
2
+ * presets.ts — vendor DATA for well-known OpenAI-compatible endpoints.
3
+ *
4
+ * A preset is nothing but an `OpenAICompatProviderOptions` literal: base URL +
5
+ * branding (icon/color/helpUrl) carried over from the retired per-vendor
6
+ * packages. No code varies per vendor — that is the whole point.
7
+ *
8
+ * ```ts
9
+ * AparteConfig.registerAIProvider(createOpenAICompatProvider(presets.MISTRAL));
10
+ * ```
11
+ *
12
+ * Local servers (LM Studio, Ollama) are served through their OpenAI-compat
13
+ * `/v1` endpoints — same format, `isLocal` just relaxes the key requirement.
14
+ */
15
+ import type { OpenAICompatProviderOptions } from './index.js';
16
+ /** Vendor presets for {@link createOpenAICompatProvider}. Pure data. */
17
+ export declare const presets: {
18
+ readonly OPENAI: OpenAICompatProviderOptions;
19
+ readonly MISTRAL: OpenAICompatProviderOptions;
20
+ readonly ZAI: OpenAICompatProviderOptions;
21
+ readonly OPENROUTER: OpenAICompatProviderOptions;
22
+ readonly LMSTUDIO: OpenAICompatProviderOptions;
23
+ readonly OLLAMA: OpenAICompatProviderOptions;
24
+ };
25
+ //# sourceMappingURL=presets.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"presets.d.ts","sourceRoot":"","sources":["../src/presets.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,KAAK,EAAE,2BAA2B,EAAE,MAAM,YAAY,CAAC;AA6E9D,wEAAwE;AACxE,eAAO,MAAM,OAAO;;;;;;;CAAkE,CAAC"}
package/package.json ADDED
@@ -0,0 +1,63 @@
1
+ {
2
+ "name": "@aparte/provider-openai-compat",
3
+ "version": "0.2.0-alpha.0",
4
+ "description": "One OpenAI-compatible chat-completions adapter for any /v1 endpoint — OpenAI, Mistral, OpenRouter, Z.ai, Groq, LM Studio, Ollama and friends, via presets or plain config.",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.js",
9
+ "types": "./dist/index.d.ts",
10
+ "exports": {
11
+ ".": {
12
+ "@aparte-workspace/source": "./src/index.ts",
13
+ "types": "./dist/index.d.ts",
14
+ "import": "./dist/index.js",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "README.md",
21
+ "LICENSE"
22
+ ],
23
+ "engines": {
24
+ "node": ">=18"
25
+ },
26
+ "peerDependencies": {
27
+ "@aparte/core": "0.2.0-alpha.0"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^22.0.0",
31
+ "typescript": "^5.4.0",
32
+ "vite": "^6.0.0",
33
+ "vite-plugin-dts": "^4.5.4",
34
+ "@aparte/core": "0.2.0-alpha.0"
35
+ },
36
+ "keywords": [
37
+ "ai",
38
+ "provider",
39
+ "openai",
40
+ "openai-compatible",
41
+ "chat-completions",
42
+ "llm",
43
+ "ollama",
44
+ "lmstudio"
45
+ ],
46
+ "license": "MIT",
47
+ "repository": {
48
+ "type": "git",
49
+ "url": "git+https://github.com/apartejs/aparte.git",
50
+ "directory": "packages/providers/ai/openai-compat"
51
+ },
52
+ "bugs": {
53
+ "url": "https://github.com/apartejs/aparte/issues"
54
+ },
55
+ "scripts": {
56
+ "dev": "vite",
57
+ "build": "vite build && tsc -b --emitDeclarationOnly --force",
58
+ "preview": "vite preview",
59
+ "test": "vitest",
60
+ "test:run": "vitest run",
61
+ "test:coverage": "vitest run --coverage"
62
+ }
63
+ }