@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.
Files changed (66) hide show
  1. package/dist/esm/adapters/chat-completions-text.d.ts +116 -0
  2. package/dist/esm/adapters/chat-completions-text.js +967 -0
  3. package/dist/esm/adapters/chat-completions-text.js.map +1 -0
  4. package/dist/esm/adapters/chat-completions-tool-converter.d.ts +35 -0
  5. package/dist/esm/adapters/chat-completions-tool-converter.js +42 -0
  6. package/dist/esm/adapters/chat-completions-tool-converter.js.map +1 -0
  7. package/dist/esm/adapters/responses-text.d.ts +135 -0
  8. package/dist/esm/adapters/responses-text.js +1324 -0
  9. package/dist/esm/adapters/responses-text.js.map +1 -0
  10. package/dist/esm/adapters/responses-tool-converter.d.ts +42 -0
  11. package/dist/esm/adapters/responses-tool-converter.js +38 -0
  12. package/dist/esm/adapters/responses-tool-converter.js.map +1 -0
  13. package/dist/esm/index.d.ts +7 -0
  14. package/dist/esm/index.js +59 -0
  15. package/dist/esm/index.js.map +1 -0
  16. package/dist/esm/tools/apply-patch-tool.d.ts +16 -0
  17. package/dist/esm/tools/apply-patch-tool.js +17 -0
  18. package/dist/esm/tools/apply-patch-tool.js.map +1 -0
  19. package/dist/esm/tools/code-interpreter-tool.d.ts +17 -0
  20. package/dist/esm/tools/code-interpreter-tool.js +22 -0
  21. package/dist/esm/tools/code-interpreter-tool.js.map +1 -0
  22. package/dist/esm/tools/computer-use-tool.d.ts +16 -0
  23. package/dist/esm/tools/computer-use-tool.js +23 -0
  24. package/dist/esm/tools/computer-use-tool.js.map +1 -0
  25. package/dist/esm/tools/custom-tool.d.ts +13 -0
  26. package/dist/esm/tools/custom-tool.js +25 -0
  27. package/dist/esm/tools/custom-tool.js.map +1 -0
  28. package/dist/esm/tools/file-search-tool.d.ts +18 -0
  29. package/dist/esm/tools/file-search-tool.js +35 -0
  30. package/dist/esm/tools/file-search-tool.js.map +1 -0
  31. package/dist/esm/tools/function-tool.d.ts +23 -0
  32. package/dist/esm/tools/function-tool.js +33 -0
  33. package/dist/esm/tools/function-tool.js.map +1 -0
  34. package/dist/esm/tools/image-generation-tool.d.ts +23 -0
  35. package/dist/esm/tools/image-generation-tool.js +28 -0
  36. package/dist/esm/tools/image-generation-tool.js.map +1 -0
  37. package/dist/esm/tools/index.d.ts +27 -0
  38. package/dist/esm/tools/local-shell-tool.d.ts +17 -0
  39. package/dist/esm/tools/local-shell-tool.js +17 -0
  40. package/dist/esm/tools/local-shell-tool.js.map +1 -0
  41. package/dist/esm/tools/mcp-tool.d.ts +18 -0
  42. package/dist/esm/tools/mcp-tool.js +31 -0
  43. package/dist/esm/tools/mcp-tool.js.map +1 -0
  44. package/dist/esm/tools/shell-tool.d.ts +26 -0
  45. package/dist/esm/tools/shell-tool.js +25 -0
  46. package/dist/esm/tools/shell-tool.js.map +1 -0
  47. package/dist/esm/tools/tool-choice.d.ts +17 -0
  48. package/dist/esm/tools/tool-converter.d.ts +6 -0
  49. package/dist/esm/tools/tool-converter.js +61 -0
  50. package/dist/esm/tools/tool-converter.js.map +1 -0
  51. package/dist/esm/tools/web-search-preview-tool.d.ts +19 -0
  52. package/dist/esm/tools/web-search-preview-tool.js +19 -0
  53. package/dist/esm/tools/web-search-preview-tool.js.map +1 -0
  54. package/dist/esm/tools/web-search-tool.d.ts +20 -0
  55. package/dist/esm/tools/web-search-tool.js +19 -0
  56. package/dist/esm/tools/web-search-tool.js.map +1 -0
  57. package/dist/esm/usage.d.ts +34 -0
  58. package/dist/esm/usage.js +83 -0
  59. package/dist/esm/usage.js.map +1 -0
  60. package/dist/esm/utils/request-options.d.ts +14 -0
  61. package/dist/esm/utils/request-options.js +11 -0
  62. package/dist/esm/utils/request-options.js.map +1 -0
  63. package/dist/esm/utils/schema-converter.d.ts +39 -0
  64. package/dist/esm/utils/schema-converter.js +145 -0
  65. package/dist/esm/utils/schema-converter.js.map +1 -0
  66. 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
+ }