@tanstack/openai-base 0.9.3 → 0.9.4
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/dist/esm/adapters/chat-completions-text.d.ts +116 -0
- package/dist/esm/adapters/chat-completions-text.js +967 -0
- package/dist/esm/adapters/chat-completions-text.js.map +1 -0
- package/dist/esm/adapters/chat-completions-tool-converter.d.ts +35 -0
- package/dist/esm/adapters/chat-completions-tool-converter.js +42 -0
- package/dist/esm/adapters/chat-completions-tool-converter.js.map +1 -0
- package/dist/esm/adapters/responses-text.d.ts +135 -0
- package/dist/esm/adapters/responses-text.js +1324 -0
- package/dist/esm/adapters/responses-text.js.map +1 -0
- package/dist/esm/adapters/responses-tool-converter.d.ts +42 -0
- package/dist/esm/adapters/responses-tool-converter.js +38 -0
- package/dist/esm/adapters/responses-tool-converter.js.map +1 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +59 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/tools/apply-patch-tool.d.ts +16 -0
- package/dist/esm/tools/apply-patch-tool.js +17 -0
- package/dist/esm/tools/apply-patch-tool.js.map +1 -0
- package/dist/esm/tools/code-interpreter-tool.d.ts +17 -0
- package/dist/esm/tools/code-interpreter-tool.js +22 -0
- package/dist/esm/tools/code-interpreter-tool.js.map +1 -0
- package/dist/esm/tools/computer-use-tool.d.ts +16 -0
- package/dist/esm/tools/computer-use-tool.js +23 -0
- package/dist/esm/tools/computer-use-tool.js.map +1 -0
- package/dist/esm/tools/custom-tool.d.ts +13 -0
- package/dist/esm/tools/custom-tool.js +25 -0
- package/dist/esm/tools/custom-tool.js.map +1 -0
- package/dist/esm/tools/file-search-tool.d.ts +18 -0
- package/dist/esm/tools/file-search-tool.js +35 -0
- package/dist/esm/tools/file-search-tool.js.map +1 -0
- package/dist/esm/tools/function-tool.d.ts +23 -0
- package/dist/esm/tools/function-tool.js +33 -0
- package/dist/esm/tools/function-tool.js.map +1 -0
- package/dist/esm/tools/image-generation-tool.d.ts +23 -0
- package/dist/esm/tools/image-generation-tool.js +28 -0
- package/dist/esm/tools/image-generation-tool.js.map +1 -0
- package/dist/esm/tools/index.d.ts +27 -0
- package/dist/esm/tools/local-shell-tool.d.ts +17 -0
- package/dist/esm/tools/local-shell-tool.js +17 -0
- package/dist/esm/tools/local-shell-tool.js.map +1 -0
- package/dist/esm/tools/mcp-tool.d.ts +18 -0
- package/dist/esm/tools/mcp-tool.js +31 -0
- package/dist/esm/tools/mcp-tool.js.map +1 -0
- package/dist/esm/tools/shell-tool.d.ts +26 -0
- package/dist/esm/tools/shell-tool.js +25 -0
- package/dist/esm/tools/shell-tool.js.map +1 -0
- package/dist/esm/tools/tool-choice.d.ts +17 -0
- package/dist/esm/tools/tool-converter.d.ts +6 -0
- package/dist/esm/tools/tool-converter.js +61 -0
- package/dist/esm/tools/tool-converter.js.map +1 -0
- package/dist/esm/tools/web-search-preview-tool.d.ts +19 -0
- package/dist/esm/tools/web-search-preview-tool.js +19 -0
- package/dist/esm/tools/web-search-preview-tool.js.map +1 -0
- package/dist/esm/tools/web-search-tool.d.ts +20 -0
- package/dist/esm/tools/web-search-tool.js +19 -0
- package/dist/esm/tools/web-search-tool.js.map +1 -0
- package/dist/esm/usage.d.ts +34 -0
- package/dist/esm/usage.js +83 -0
- package/dist/esm/usage.js.map +1 -0
- package/dist/esm/utils/request-options.d.ts +14 -0
- package/dist/esm/utils/request-options.js +11 -0
- package/dist/esm/utils/request-options.js.map +1 -0
- package/dist/esm/utils/schema-converter.d.ts +39 -0
- package/dist/esm/utils/schema-converter.js +145 -0
- package/dist/esm/utils/schema-converter.js.map +1 -0
- package/package.json +4 -4
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { BaseTextAdapter, StructuredOutputOptions, StructuredOutputResult } from '@tanstack/ai/adapters';
|
|
2
|
+
import { default as OpenAI } from 'openai';
|
|
3
|
+
import { ChatCompletionChunk, ChatCompletionContentPart, ChatCompletionCreateParamsStreaming, ChatCompletionMessageParam } from 'openai/resources/chat/completions/completions';
|
|
4
|
+
import { ContentPart, DefaultMessageMetadataByModality, Modality, ModelMessage, StreamChunk, TextOptions } from '@tanstack/ai';
|
|
5
|
+
/**
|
|
6
|
+
* Shared implementation of the OpenAI Chat Completions API. Holds the
|
|
7
|
+
* stream-accumulator + AG-UI lifecycle logic and calls the OpenAI SDK
|
|
8
|
+
* directly. Subclasses (ai-openai, ai-grok, ai-groq) construct an OpenAI
|
|
9
|
+
* client with their provider-specific `baseURL` / headers and pass it in.
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class OpenAIBaseChatCompletionsTextAdapter<TModel extends string, TProviderOptions extends Record<string, unknown> = Record<string, unknown>, TInputModalities extends ReadonlyArray<Modality> = ReadonlyArray<Modality>, TMessageMetadata extends DefaultMessageMetadataByModality = DefaultMessageMetadataByModality, TToolCapabilities extends ReadonlyArray<string> = ReadonlyArray<string>> extends BaseTextAdapter<TModel, TProviderOptions, TInputModalities, TMessageMetadata, TToolCapabilities> {
|
|
12
|
+
readonly kind: "text";
|
|
13
|
+
readonly name: string;
|
|
14
|
+
protected client: OpenAI;
|
|
15
|
+
constructor(model: TModel, name: string, client: OpenAI);
|
|
16
|
+
chatStream(options: TextOptions<TProviderOptions>): AsyncIterable<StreamChunk>;
|
|
17
|
+
/**
|
|
18
|
+
* Generate structured output using the provider's JSON Schema response format.
|
|
19
|
+
* Uses stream: false to get the complete response in one call.
|
|
20
|
+
*
|
|
21
|
+
* OpenAI-compatible APIs have strict requirements for structured output:
|
|
22
|
+
* - All properties must be in the `required` array
|
|
23
|
+
* - Optional fields should have null added to their type union
|
|
24
|
+
* - additionalProperties must be false for all objects
|
|
25
|
+
*
|
|
26
|
+
* The outputSchema is already JSON Schema (converted in the ai layer).
|
|
27
|
+
* We apply provider-specific transformations for structured output compatibility.
|
|
28
|
+
*/
|
|
29
|
+
structuredOutput(options: StructuredOutputOptions<TProviderOptions>): Promise<StructuredOutputResult<unknown>>;
|
|
30
|
+
/**
|
|
31
|
+
* Stream structured output. Single Chat Completions request with
|
|
32
|
+
* `response_format: json_schema` + `stream: true`. Emits the standard
|
|
33
|
+
* AG-UI lifecycle (`RUN_STARTED` → `REASONING_*?` → `TEXT_MESSAGE_*`
|
|
34
|
+
* carrying raw JSON deltas → terminal `CUSTOM 'structured-output.complete'`
|
|
35
|
+
* → `RUN_FINISHED`). Subclasses use the same SDK-call / reasoning /
|
|
36
|
+
* structured-output-transform hooks as `chatStream` / `structuredOutput` —
|
|
37
|
+
* no per-subclass override should be needed.
|
|
38
|
+
*/
|
|
39
|
+
structuredOutputStream(options: StructuredOutputOptions<TProviderOptions>): AsyncIterable<StreamChunk>;
|
|
40
|
+
/**
|
|
41
|
+
* Cross-SDK abort detection for `structuredOutputStream`. Default duck-types
|
|
42
|
+
* on `name === 'APIUserAbortError'` (OpenAI SDK), `code === 'ERR_CANCELED'`,
|
|
43
|
+
* and standard `AbortError`s. Subclasses with proprietary error types (e.g.
|
|
44
|
+
* `@openrouter/sdk`'s `RequestAbortedError`) override to extend the check.
|
|
45
|
+
*/
|
|
46
|
+
protected isAbortError(error: unknown): boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Applies provider-specific transformations for structured output compatibility.
|
|
49
|
+
* Override this in subclasses to handle provider-specific quirks.
|
|
50
|
+
*/
|
|
51
|
+
protected makeStructuredOutputCompatible(schema: Record<string, any>, originalRequired?: Array<string>): Record<string, any>;
|
|
52
|
+
/**
|
|
53
|
+
* Extract reasoning content from a stream chunk. Default returns
|
|
54
|
+
* `undefined` because the OpenAI Chat Completions chunk shape doesn't
|
|
55
|
+
* carry reasoning. The chunk param is typed `unknown` so an override can
|
|
56
|
+
* narrow to its own SDK chunk type without an `as` dance — the base only
|
|
57
|
+
* passes through `processStreamChunks`'s structurally-iterated chunk.
|
|
58
|
+
*/
|
|
59
|
+
protected extractReasoning(_chunk: unknown): {
|
|
60
|
+
text: string;
|
|
61
|
+
} | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Final shaping pass applied to parsed structured-output JSON before it is
|
|
64
|
+
* returned to the caller. Default is a passthrough.
|
|
65
|
+
*
|
|
66
|
+
* Provider `null`s are no longer stripped here: strict-mode null-widening is
|
|
67
|
+
* now undone precisely by the engine (`undoNullWidening`, driven by the
|
|
68
|
+
* schema's null-widening map) the moment the result is captured, so a blind
|
|
69
|
+
* `transformNullsToUndefined` at the adapter would only destroy genuine
|
|
70
|
+
* `.nullable()` nulls. Subclasses may still override to remap or reshape the
|
|
71
|
+
* provider's structured output.
|
|
72
|
+
*/
|
|
73
|
+
protected transformStructuredOutput(parsed: unknown): unknown;
|
|
74
|
+
/**
|
|
75
|
+
* Processes streamed chunks from the Chat Completions API and yields AG-UI events.
|
|
76
|
+
* Override this in subclasses to handle provider-specific stream behavior.
|
|
77
|
+
*/
|
|
78
|
+
protected processStreamChunks(stream: AsyncIterable<ChatCompletionChunk>, options: TextOptions, aguiState: {
|
|
79
|
+
runId: string;
|
|
80
|
+
threadId: string;
|
|
81
|
+
messageId: string;
|
|
82
|
+
hasEmittedRunStarted: boolean;
|
|
83
|
+
}): AsyncIterable<StreamChunk>;
|
|
84
|
+
/**
|
|
85
|
+
* Maps common TextOptions to Chat Completions API request format.
|
|
86
|
+
* Override this in subclasses to add provider-specific options.
|
|
87
|
+
*/
|
|
88
|
+
protected mapOptionsToRequest(options: TextOptions): ChatCompletionCreateParamsStreaming;
|
|
89
|
+
/**
|
|
90
|
+
* Modern OpenAI-compatible Chat Completions APIs support `tools` and
|
|
91
|
+
* `response_format: json_schema` together in a single streaming request
|
|
92
|
+
* (per issue #605). Subclasses can override — Groq, for instance, must
|
|
93
|
+
* return `false` because its API rejects schema + tools + stream with a
|
|
94
|
+
* 400.
|
|
95
|
+
*/
|
|
96
|
+
supportsCombinedToolsAndSchema(): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Converts a single ModelMessage to the Chat Completions API message format.
|
|
99
|
+
* Override this in subclasses to handle provider-specific message formats.
|
|
100
|
+
*/
|
|
101
|
+
protected convertMessage(message: ModelMessage): ChatCompletionMessageParam;
|
|
102
|
+
/**
|
|
103
|
+
* Converts a single ContentPart to the Chat Completions API content part format.
|
|
104
|
+
* Override this in subclasses to handle additional content types or provider-specific metadata.
|
|
105
|
+
*/
|
|
106
|
+
protected convertContentPart(part: ContentPart): ChatCompletionContentPart | null;
|
|
107
|
+
/**
|
|
108
|
+
* Normalizes message content to an array of ContentPart.
|
|
109
|
+
* Handles backward compatibility with string content.
|
|
110
|
+
*/
|
|
111
|
+
protected normalizeContent(content: string | null | Array<ContentPart>): Array<ContentPart>;
|
|
112
|
+
/**
|
|
113
|
+
* Extracts text content from a content value that may be string, null, or ContentPart array.
|
|
114
|
+
*/
|
|
115
|
+
protected extractTextContent(content: string | null | Array<ContentPart>): string;
|
|
116
|
+
}
|