@vinhnt-sdk/provider-openai-compatible 0.1.3

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 Nguyen Thanh Vinh
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,30 @@
1
+ # @vinhnt-sdk/provider-openai-compatible
2
+
3
+ OpenAI-compatible model provider for VNT Agent — implemented with **raw `fetch`**, zero AI SDK.
4
+
5
+ Implements the [`ModelProvider`](https://github.com/vinhnt-develop/vinhnt-sdk) contract using the OpenAI Chat Completion API
6
+ (`/chat/completions`), including SSE streaming.
7
+
8
+ ## Features
9
+
10
+ - `buildRequest(modelRequest, { stream })` → OpenAI Chat Completion body
11
+ - `createSSEStream(body, signal)` → parse `data:` + `[DONE]` + final usage chunk
12
+ - Tool-call delta assembly → `ModelStreamEvent`
13
+ - Retry/backoff on retryable HTTP statuses + `Retry-After` header
14
+ - Upstream failures map to `UpstreamError` with `ERR_UPSTREAM_<STATUS>` codes
15
+ - Conversion functions (`fromOpenAIMessage`, `fromOpenAIResponse`, `fromOpenAIStreamChunk`, …) imported from schema types
16
+
17
+ ## Usage
18
+
19
+ ```ts
20
+ import { OpenAICompatibleProvider } from "@vinhnt-sdk/provider-openai-compatible";
21
+
22
+ const provider = new OpenAICompatibleProvider({
23
+ baseUrl: "https://api.openai.com/v1",
24
+ apiKey: process.env.OPENAI_API_KEY,
25
+ defaultModel: "gpt-4o",
26
+ retry: { maxRetries: 3 },
27
+ });
28
+ ```
29
+
30
+ Use it anywhere a `ModelProvider` is accepted (kernel `model`, model registry, agent profiles).
@@ -0,0 +1,72 @@
1
+ /**
2
+ * Builder for OpenAI Chat Completions request bodies from a vinhnt-sdk
3
+ * `ModelRequest` — raw fetch shape, zero AI SDK.
4
+ */
5
+ import type { ModelRequest, OpenAIMessage } from "@vinhnt-sdk/schema";
6
+ /** OpenAI Chat Completions request body (as POSTed to /chat/completions). */
7
+ export interface OpenAICompatibleRequestBody {
8
+ readonly model: string;
9
+ readonly messages: readonly OpenAIMessage[];
10
+ stream?: boolean;
11
+ stream_options?: {
12
+ readonly include_usage?: boolean;
13
+ };
14
+ tools?: readonly {
15
+ readonly type: "function";
16
+ readonly function: {
17
+ readonly name: string;
18
+ readonly description: string;
19
+ readonly parameters?: unknown;
20
+ readonly strict?: boolean;
21
+ };
22
+ }[];
23
+ tool_choice?: "auto" | "required" | "none" | {
24
+ readonly type: "function";
25
+ readonly function: {
26
+ readonly name: string;
27
+ };
28
+ };
29
+ parallel_tool_calls?: boolean;
30
+ max_tokens?: number;
31
+ max_completion_tokens?: number;
32
+ temperature?: number;
33
+ top_p?: number;
34
+ stop?: string | readonly string[];
35
+ presence_penalty?: number;
36
+ frequency_penalty?: number;
37
+ logit_bias?: Record<string, number>;
38
+ seed?: number;
39
+ user?: string;
40
+ logprobs?: boolean;
41
+ top_logprobs?: number;
42
+ reasoning_effort?: string;
43
+ response_format?: {
44
+ readonly type: "json_object";
45
+ } | {
46
+ readonly type: "json_schema";
47
+ readonly json_schema: {
48
+ readonly name: string;
49
+ readonly schema: unknown;
50
+ readonly strict?: boolean;
51
+ };
52
+ };
53
+ }
54
+ /** Build options. */
55
+ export interface BuildRequestOptions {
56
+ /** Model identifier sent in the body. Defaults to the provider's `model`. */
57
+ readonly model?: string;
58
+ /** Request a streaming response (`stream: true` + `stream_options.include_usage`). */
59
+ readonly stream?: boolean;
60
+ /** Track request/response token usage in streamed responses. */
61
+ readonly includeUsage?: boolean;
62
+ }
63
+ /**
64
+ * Build an OpenAI Chat Completions request body from a vinhnt-sdk ModelRequest.
65
+ *
66
+ * Maps: messages, system (top-level), tools, tool_choice, parallel_tool_calls,
67
+ * max_completion_tokens/max_tokens, temperature/top_p/stop, penalties,
68
+ * logit_bias, seed, user, logprobs, reasoning_effort, response_format, and
69
+ * (when streaming) `stream: true` + `stream_options.include_usage`.
70
+ */
71
+ export declare function buildRequest(request: ModelRequest, opts?: BuildRequestOptions): OpenAICompatibleRequestBody;
72
+ //# sourceMappingURL=build-request.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-request.d.ts","sourceRoot":"","sources":["../src/build-request.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAEV,YAAY,EAIZ,aAAa,EACd,MAAM,oBAAoB,CAAC;AAG5B,6EAA6E;AAC7E,MAAM,WAAW,2BAA2B;IAC1C,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,QAAQ,EAAE,SAAS,aAAa,EAAE,CAAC;IAC5C,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,cAAc,CAAC,EAAE;QAAE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;KAAE,CAAC;IACtD,KAAK,CAAC,EAAE,SAAS;QACf,QAAQ,CAAC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I,EAAE,MAAM,CAAC;YAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC;YAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;SAAE,CAAA;KAAE,CAAC;CAC5I;AAED,qBAAqB;AACrB,MAAM,WAAW,mBAAmB;IAClC,6EAA6E;IAC7E,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,sFAAsF;IACtF,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;IAC1B,gEAAgE;IAChE,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;CACjC;AAgDD;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,mBAAmB,GAAG,2BAA2B,CAkD3G"}
@@ -0,0 +1,116 @@
1
+ /**
2
+ * Builder for OpenAI Chat Completions request bodies from a vinhnt-sdk
3
+ * `ModelRequest` — raw fetch shape, zero AI SDK.
4
+ */
5
+ import { toOpenAIMessage } from "./convert.js";
6
+ function toToolChoice(choice) {
7
+ if (choice === undefined)
8
+ return undefined;
9
+ if (typeof choice === "string")
10
+ return choice;
11
+ return { type: "function", function: { name: choice.name } };
12
+ }
13
+ function toResponseFormat(format) {
14
+ if (format === undefined)
15
+ return undefined;
16
+ if (format.type === "json_object")
17
+ return { type: "json_object" };
18
+ return {
19
+ type: "json_schema",
20
+ json_schema: {
21
+ name: format.jsonSchema.name,
22
+ schema: format.jsonSchema.schema,
23
+ ...(format.jsonSchema.strict !== undefined ? { strict: format.jsonSchema.strict } : {}),
24
+ },
25
+ };
26
+ }
27
+ function toOpenAITool(tool) {
28
+ // Prefer the ready-made OpenAI tool shape when the tool exposes one
29
+ // (ToolDefinitionLike already carries `type`/`function` passthrough).
30
+ const fn = tool.function;
31
+ if (fn) {
32
+ return {
33
+ type: "function",
34
+ function: {
35
+ name: fn.name,
36
+ description: fn.description,
37
+ ...(fn.parameters !== undefined ? { parameters: fn.parameters } : {}),
38
+ ...(fn.strict !== undefined ? { strict: fn.strict } : {}),
39
+ },
40
+ };
41
+ }
42
+ return {
43
+ type: "function",
44
+ function: {
45
+ name: tool.name ?? tool.id,
46
+ description: tool.description,
47
+ ...(tool.inputSchema !== undefined ? { parameters: tool.inputSchema } : {}),
48
+ },
49
+ };
50
+ }
51
+ /**
52
+ * Build an OpenAI Chat Completions request body from a vinhnt-sdk ModelRequest.
53
+ *
54
+ * Maps: messages, system (top-level), tools, tool_choice, parallel_tool_calls,
55
+ * max_completion_tokens/max_tokens, temperature/top_p/stop, penalties,
56
+ * logit_bias, seed, user, logprobs, reasoning_effort, response_format, and
57
+ * (when streaming) `stream: true` + `stream_options.include_usage`.
58
+ */
59
+ export function buildRequest(request, opts) {
60
+ const messages = request.messages.map((m) => toOpenAIMessage(m));
61
+ if (request.system) {
62
+ messages.unshift({ role: "system", content: request.system });
63
+ }
64
+ const body = {
65
+ model: opts?.model ?? "",
66
+ messages,
67
+ };
68
+ if (opts?.stream) {
69
+ body.stream = true;
70
+ if (opts.includeUsage) {
71
+ body.stream_options = { include_usage: true };
72
+ }
73
+ }
74
+ if (request.tools && request.tools.length > 0) {
75
+ body.tools = request.tools.map(toOpenAITool);
76
+ }
77
+ const toolChoice = toToolChoice(request.toolChoice);
78
+ if (toolChoice !== undefined)
79
+ body.tool_choice = toolChoice;
80
+ if (request.parallelToolCalls !== undefined)
81
+ body.parallel_tool_calls = request.parallelToolCalls;
82
+ if (request.maxCompletionTokens !== undefined) {
83
+ body.max_completion_tokens = request.maxCompletionTokens;
84
+ }
85
+ else if (request.maxTokens !== undefined) {
86
+ body.max_tokens = request.maxTokens;
87
+ }
88
+ if (request.temperature !== undefined)
89
+ body.temperature = request.temperature;
90
+ if (request.topP !== undefined)
91
+ body.top_p = request.topP;
92
+ if (request.stopSequences !== undefined && request.stopSequences.length > 0) {
93
+ body.stop = request.stopSequences.length === 1 ? request.stopSequences[0] : request.stopSequences;
94
+ }
95
+ if (request.presencePenalty !== undefined)
96
+ body.presence_penalty = request.presencePenalty;
97
+ if (request.frequencyPenalty !== undefined)
98
+ body.frequency_penalty = request.frequencyPenalty;
99
+ if (request.logitBias)
100
+ body.logit_bias = request.logitBias;
101
+ if (request.seed !== undefined)
102
+ body.seed = request.seed;
103
+ if (request.user !== undefined)
104
+ body.user = request.user;
105
+ if (request.logprobs !== undefined)
106
+ body.logprobs = request.logprobs;
107
+ if (request.topLogprobs !== undefined)
108
+ body.top_logprobs = request.topLogprobs;
109
+ if (request.reasoningEffort !== undefined)
110
+ body.reasoning_effort = request.reasoningEffort;
111
+ const responseFormat = toResponseFormat(request.responseFormat);
112
+ if (responseFormat !== undefined)
113
+ body.response_format = responseFormat;
114
+ return body;
115
+ }
116
+ //# sourceMappingURL=build-request.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build-request.js","sourceRoot":"","sources":["../src/build-request.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAUH,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AA+C/C,SAAS,YAAY,CAAC,MAA8B;IAClD,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,OAAO,MAAM,KAAK,QAAQ;QAAE,OAAO,MAAM,CAAC;IAC9C,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;AAC/D,CAAC;AAED,SAAS,gBAAgB,CAAC,MAAkC;IAC1D,IAAI,MAAM,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC3C,IAAI,MAAM,CAAC,IAAI,KAAK,aAAa;QAAE,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,CAAC;IAClE,OAAO;QACL,IAAI,EAAE,aAAa;QACnB,WAAW,EAAE;YACX,IAAI,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI;YAC5B,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,MA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AC;IAE3F,MAAM,cAAc,GAAG,gBAAgB,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC;IAChE,IAAI,cAAc,KAAK,SAAS;QAAE,IAAI,CAAC,eAAe,GAAG,cAAc,CAAC;IAExE,OAAO,IAAI,CAAC;AACd,CAAC"}
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Conversion functions between OpenAI Chat Completion wire format and
3
+ * vinhnt-sdk types. The wire-format *types* stay in
4
+ * `@vinhnt-sdk/schema` (schema keeps the interface-only contract);
5
+ * the logic lives here in the provider package.
6
+ */
7
+ import type { ChatMessage, ModelResponse, OpenAIMessage, OpenAIResponse, OpenAIStreamChunk, Logprobs, OpenAIErrorResponse } from "@vinhnt-sdk/schema";
8
+ import type { VntError } from "@vinhnt-sdk/schema";
9
+ /**
10
+ * Convert OpenAI Chat Completion message to vinhnt-sdk ChatMessage.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * const vntMsg = fromOpenAIMessage({
15
+ * role: "user",
16
+ * content: [{ type: "text", text: "Hello" }],
17
+ * });
18
+ * ```
19
+ */
20
+ export declare function fromOpenAIMessage(msg: OpenAIMessage): ChatMessage;
21
+ /**
22
+ * Convert vinhnt-sdk ChatMessage to OpenAI Chat Completion message.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * const openaiMsg = toOpenAIMessage({
27
+ * role: "user",
28
+ * content: "Hello",
29
+ * });
30
+ * ```
31
+ */
32
+ export declare function toOpenAIMessage(msg: ChatMessage): OpenAIMessage;
33
+ /**
34
+ * Convert OpenAI Chat Completion response to vinhnt-sdk ModelResponse.
35
+ *
36
+ * @example
37
+ * ```ts
38
+ * const response = await fetch("https://api.openai.com/v1/chat/completions", ...);
39
+ * const data = await response.json();
40
+ * const vntResponse = fromOpenAIResponse(data);
41
+ * ```
42
+ */
43
+ export declare function fromOpenAIResponse(res: OpenAIResponse): ModelResponse;
44
+ /**
45
+ * Convert vinhnt-sdk ModelResponse to OpenAI Chat Completion response format.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const openaiRes = toOpenAIResponse({
50
+ * content: "Hello!",
51
+ * finishReason: "stop",
52
+ * usage: { promptTokens: 10, completionTokens: 5, totalTokens: 15 },
53
+ * });
54
+ * ```
55
+ */
56
+ export declare function toOpenAIResponse(res: ModelResponse): OpenAIResponse;
57
+ /** Individual event extracted from a streaming chunk. */
58
+ export interface StreamChunkEvent {
59
+ readonly type: string;
60
+ readonly content?: string;
61
+ readonly id?: string;
62
+ readonly name?: string;
63
+ readonly args?: Record<string, unknown>;
64
+ readonly logprobs?: Logprobs;
65
+ readonly reason?: string;
66
+ readonly inputTokens?: number;
67
+ readonly outputTokens?: number;
68
+ readonly reasoningTokens?: number;
69
+ }
70
+ /**
71
+ * Convert an OpenAI streaming chunk into an array of individual events.
72
+ *
73
+ * Each chunk can contain multiple choices, each with its own deltas. This
74
+ * function extracts each chunk's individual deltas. Use
75
+ * `toModelStreamEvents` (or the provider's `stream()`) for assembled output.
76
+ *
77
+ * @example
78
+ * ```ts
79
+ * for await (const chunk of stream) {
80
+ * const events = fromOpenAIStreamChunk(chunk);
81
+ * for (const event of events) { /* handle event *\/ }
82
+ * }
83
+ * ```
84
+ */
85
+ export declare function fromOpenAIStreamChunk(chunk: OpenAIStreamChunk): StreamChunkEvent[];
86
+ /**
87
+ * Convert Anthropic Messages API message to vinhnt-sdk ChatMessage.
88
+ *
89
+ * Note: Anthropic uses top-level `system` parameter, not role in messages.
90
+ */
91
+ export declare function fromAnthropicMessage(msg: {
92
+ role: "user" | "assistant";
93
+ content: string | Array<{
94
+ type: string;
95
+ text?: string;
96
+ }>;
97
+ }): ChatMessage;
98
+ /**
99
+ * Convert OpenAI error response to vinhnt-sdk VntError.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * const error = fromOpenAIError({
104
+ * error: {
105
+ * message: "Rate limit exceeded",
106
+ * type: "rate_limit_error",
107
+ * param: null,
108
+ * code: "rate_limit_exceeded",
109
+ * },
110
+ * });
111
+ * // error instanceof RateLimitError === true
112
+ * ```
113
+ */
114
+ export declare function fromOpenAIError(err: OpenAIErrorResponse): VntError;
115
+ //# sourceMappingURL=convert.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"convert.d.ts","sourceRoot":"","sources":["../src/convert.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,KAAK,EACV,WAAW,EAEX,aAAa,EAGb,aAAa,EAGb,cAAc,EACd,iBAAiB,EACjB,QAAQ,EACR,mBAAmB,EACpB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EACV,QAAQ,EACT,MAAM,oBAAoB,CAAC;AAW5B;;;;;;;;;;GAUG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,aAAa,GAAG,WAAW,CAkDjE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,eAAe,CAAC,GAAG,EAAE,WAAW,GAAG,aAAa,CA4B/D;AAID;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,cAAc,GAAG,aAAa,CAwCrE;AAED;;;;;;;;;;;GAWG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,aAAa,GAAG,cAAc,CA6CnE;AAID,yDAAyD;AACzD,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,EAAE,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACxC,QAAQ,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC;IAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,YAAY,CAAC,E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