@namzu/openai 1.0.3 → 1.1.1
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/CHANGELOG.md +167 -0
- package/dist/client.d.ts +28 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +77 -7
- package/dist/client.js.map +1 -1
- package/dist/strict-schema.d.ts +2 -0
- package/dist/strict-schema.d.ts.map +1 -0
- package/dist/strict-schema.js +2 -0
- package/dist/strict-schema.js.map +1 -0
- package/package.json +13 -3
- package/src/client.ts +86 -7
- package/src/strict-schema.ts +0 -0
- package/dist/__tests__/error-taxonomy.test.d.ts +0 -14
- package/dist/__tests__/error-taxonomy.test.d.ts.map +0 -1
- package/dist/__tests__/error-taxonomy.test.js +0 -203
- package/dist/__tests__/error-taxonomy.test.js.map +0 -1
- package/dist/__tests__/openai.test.d.ts +0 -2
- package/dist/__tests__/openai.test.d.ts.map +0 -1
- package/dist/__tests__/openai.test.js +0 -111
- package/dist/__tests__/openai.test.js.map +0 -1
- package/src/__tests__/error-taxonomy.test.ts +0 -234
- package/src/__tests__/openai.test.ts +0 -127
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,172 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.1.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 48d9d67: Published tarballs no longer contain test files.
|
|
8
|
+
|
|
9
|
+
`files: ["dist", "src", ...]` reads as "the build output and the sources" and
|
|
10
|
+
means "everything the compiler emitted and everything in the tree", so every
|
|
11
|
+
compiled test, its declaration, and both source maps shipped to the registry —
|
|
12
|
+
and for the twelve packages that also ship `src`, the raw test sources went with
|
|
13
|
+
them.
|
|
14
|
+
|
|
15
|
+
Measured on the versions currently published:
|
|
16
|
+
|
|
17
|
+
| package | files | of which tests | unpacked |
|
|
18
|
+
| ------------ | ----------- | -------------- | ------------------ |
|
|
19
|
+
| `@namzu/sdk` | 3879 → 2239 | 1640 (42%) | 12.73 MB → 6.81 MB |
|
|
20
|
+
| `@namzu/cli` | 462 → 282 | 180 (39%) | 1.21 MB → 0.73 MB |
|
|
21
|
+
|
|
22
|
+
Nothing you can import changes. Every package restricts `exports` to `"."`, so
|
|
23
|
+
Node refused a deep subpath into those files already — they were weight in the
|
|
24
|
+
tarball and nothing else. Hence `patch`: there is no consumer-visible surface
|
|
25
|
+
here, only less to download.
|
|
26
|
+
|
|
27
|
+
The exclusions are at the packaging layer, not the compiler. Adding `exclude`
|
|
28
|
+
to `tsconfig.json` would have kept tests out of `dist` and also dropped them
|
|
29
|
+
from `tsc --noEmit`, silently ending type-checking of the entire test suite —
|
|
30
|
+
trading a packaging defect for a much worse one.
|
|
31
|
+
|
|
32
|
+
## 1.1.0
|
|
33
|
+
|
|
34
|
+
### Minor Changes
|
|
35
|
+
|
|
36
|
+
- 935b8f3: Documents and citations were designed in the SDK and never built in the drivers.
|
|
37
|
+
|
|
38
|
+
`DocumentAttachment` and `Citation` existed in `@namzu/sdk` with a page of documentation about why native document handling is worth having — page structure, the provider's own extraction, and the ability to say which passage an answer rests on. The stream chunk carried a citation slot, the run's stream aggregator collected them onto the assistant message, and the iteration attached them to the turn. Both these drivers declared `supportsDocuments: true`.
|
|
39
|
+
|
|
40
|
+
Neither had a document branch. Every attachment was mapped as an image — a PDF went up as an image block with `media_type: application/pdf` on one wire and as `data:application/pdf;base64,…` inside an `image_url` part on the other, shapes the APIs reject. And only the mock provider ever emitted a citation, so in a real run the slot was always empty: an answer about a contract arrived as prose, and checking it meant reading the contract again.
|
|
41
|
+
|
|
42
|
+
- **`@namzu/anthropic`** now sends a native document block carrying the media type, the optional title, and — only when the attachment asked for them, because they cost tokens — citations. It parses citation deltas back onto the stream, keeping the location as the discriminated union the SDK defines: a provider that segments by character offset has no page number, and a citation whose location cannot be named is dropped rather than given an invented one. A citation that looks checkable and is not is worse than none.
|
|
43
|
+
- **`@namzu/openai`** now sends a document as a `file` content part with its filename. That wire has no way to return citations, so a document that asks for them is refused with a message naming the document and what to do instead — answering without them would drop the checkability the caller asked for, and an empty citation list reads as "the model cited nothing" rather than "nobody asked".
|
|
44
|
+
|
|
45
|
+
The drivers that declare `supportsDocuments: false` were already honest: none of them map attachments at all, and the runtime warns (or fails under `strictCapabilities`) before the request is built.
|
|
46
|
+
|
|
47
|
+
- 935b8f3: Four drivers dumped tool-result payloads into the prompt, and one ignored the strict-schema hint.
|
|
48
|
+
|
|
49
|
+
**A tool result carrying an image was `JSON.stringify`d** on four drivers, so a screenshot reached the model as a wall of quoted base64. The model paid for every character, could read none of them, and — worse — saw a serialized object where a picture should be, with nothing saying anything had been withheld. The SDK's `toolResultToText` exists for exactly this and produces a named placeholder reporting the media type and the size. All four now use it: `@namzu/bedrock`, both dialects of `@namzu/http`, and `@namzu/lmstudio`.
|
|
50
|
+
|
|
51
|
+
**`enforceToolInputSchema` was ignored by `@namzu/openai`.** It names the tools whose model-facing schema should be enforced by constrained generation rather than merely suggested; both sibling drivers consumed it. A caller who had asked for a guaranteed-valid tool input silently got a best-effort one and learned about it from a repair attempt. This is the wire the flag maps onto most directly — it takes the flag on the function itself. The existing test asserting the tools went through untouched read as "the hint is kept out of the request" and actually pinned "the hint does nothing"; it now asserts the hint is consumed and still never appears verbatim.
|
|
52
|
+
|
|
53
|
+
**An extended-thinking request is refused by `@namzu/openai` rather than dropped.** The parameter was accepted and ignored, so a caller who asked for reasoning got an ordinary completion with an empty reasoning list — which reads as "the model did not reason" rather than "nobody asked it to". Turning thinking off stays a no-op, since that is the state the driver is already in.
|
|
54
|
+
|
|
55
|
+
Eleven previously empty test files now cover these drivers, including the capability claims themselves: every driver that declares no vision, documents or tools is pinned against drift in both directions, because the runtime warns or fails on those flags before a request is built, and a flag flipped ahead of its mapping is as wrong as a mapping written without the flag.
|
|
56
|
+
|
|
57
|
+
- 935b8f3: Tool arguments can be made valid by construction
|
|
58
|
+
|
|
59
|
+
namzu has a whole repair path for arguments that do not match a tool's schema — a repair hook, a bounded retry, a model-visible error. This wire format offers a mode where the endpoint constrains decoding to the schema, so invalid arguments cannot be emitted at all, which is strictly better than repairing them well.
|
|
60
|
+
|
|
61
|
+
`strictTools: true` turns it on. Off by default because it is a real trade: strict decoding requires every property to be required, so the driver rewrites each schema — objects close, every property joins `required`, and one that was optional widens to accept `null` so "leave it out" stays expressible. An optional argument therefore becomes one the model must pass explicitly as null, and that change to what the model is told belongs to the tool's author rather than the driver.
|
|
62
|
+
|
|
63
|
+
The rewrite is not separable from the flag: the endpoint rejects strict mode on a schema that has not been closed for it, so sending one without the other would turn a correctness feature into a 400.
|
|
64
|
+
|
|
65
|
+
- 935b8f3: A user message can carry a document
|
|
66
|
+
|
|
67
|
+
Documents existed in the type system only in the tool-result direction, and both first-party drivers mapped images only on the input side. So "here is the contract, answer questions about it" — a mainstream workload — was reachable only by having a tool read the file and stringify it. That loses the provider's native document handling (page structure, built-in OCR, citations) and pays the text cost instead.
|
|
68
|
+
|
|
69
|
+
`UserMessage.attachments` is now `MessageAttachment[]`: an image or a document. The discriminant is optional and stays optional — an attachment without one is an image, which is what every attachment was before, so no existing caller changes.
|
|
70
|
+
|
|
71
|
+
`supportsDocuments` sits beside `supportsVision` in the driver capability declaration, and the runtime checks it the same way: a document sent to a driver that declares `false` warns before the request, or throws under `strictCapabilities`, instead of letting the model answer about a file it never saw. The two are counted separately because they are separate wire shapes and a driver can map one without the other.
|
|
72
|
+
|
|
73
|
+
The two first-party drivers map documents natively. The remaining five map images only and now say so; a document reaching them degrades to a named placeholder that says which kind was dropped, rather than one that calls a document an image.
|
|
74
|
+
|
|
75
|
+
### Patch Changes
|
|
76
|
+
|
|
77
|
+
- 935b8f3: Three driver defects found by auditing every provider against the same
|
|
78
|
+
contract checklist rather than one at a time.
|
|
79
|
+
|
|
80
|
+
**anthropic — a thinking block never reported its close.** `openReasoning`
|
|
81
|
+
was declared, read by the `content_block_stop` branch, and never added to:
|
|
82
|
+
three references in the whole file. The set was permanently empty, so the
|
|
83
|
+
close branch could not match and `reasoning: { done: true }` never reached
|
|
84
|
+
the consumer. A host that opens a thinking card on the first reasoning
|
|
85
|
+
delta left it spinning for the rest of the run. Stored blocks were
|
|
86
|
+
unaffected, so replay always looked correct — only the live stream was
|
|
87
|
+
broken, which is why it survived. This is the default driver.
|
|
88
|
+
|
|
89
|
+
**openai — every request to a reasoning-family model failed on turn one.**
|
|
90
|
+
`temperature` and `max_tokens` were sent unconditionally, and those models
|
|
91
|
+
reject both, requiring `max_completion_tokens`. The rejection is a 400,
|
|
92
|
+
which classifies as `invalid_request` and is not retryable, so the run
|
|
93
|
+
died immediately whenever a token cap was set — which the runtime always
|
|
94
|
+
does. Model family is now detected by id prefix, conservatively: an
|
|
95
|
+
unknown model keeps the standard parameters, because a false positive
|
|
96
|
+
silently strips `temperature` from a model that honours it while a false
|
|
97
|
+
negative produces a clear error naming the parameter.
|
|
98
|
+
|
|
99
|
+
**http — every streamed turn reported zero tokens.** The body builder never
|
|
100
|
+
requested usage on a streamed response, so a conforming endpoint sent none
|
|
101
|
+
and the (complete) parsing had nothing to parse. Cost read as free, and any
|
|
102
|
+
budget or compaction threshold keyed on usage never fired however large the
|
|
103
|
+
thread grew.
|
|
104
|
+
|
|
105
|
+
- 935b8f3: Widen the message model to content blocks: multimodal tool results, `is_error`,
|
|
106
|
+
and reasoning replay.
|
|
107
|
+
|
|
108
|
+
`ToolMessage.content` was `string` and `AssistantMessage` had no slot for
|
|
109
|
+
reasoning, so three separate things died at the provider boundary. Doing them
|
|
110
|
+
as one migration is deliberate — all three need the same widening, and every
|
|
111
|
+
stored transcript, checkpoint and `messages.json` is written in the narrow
|
|
112
|
+
shape, so the cost only grows.
|
|
113
|
+
|
|
114
|
+
**Tool results can carry non-text content.** `ToolResultContent` is
|
|
115
|
+
`string | ToolResultBlock[]`, where a block is text, image or document. String
|
|
116
|
+
stays first-class: the common case is unchanged and every existing tool and
|
|
117
|
+
driver compiles untouched. `@namzu/computer-use`'s `screenshot` returned
|
|
118
|
+
~400 KB–2.7 MB of base64 **as text** — roughly 100k–670k tokens of characters
|
|
119
|
+
no model can decode — so computer use was effectively non-functional; it now
|
|
120
|
+
returns an image block with a short textual description. MCP `image` and
|
|
121
|
+
inline `resource` blocks are passed through instead of being filtered out.
|
|
122
|
+
|
|
123
|
+
**Failures are marked on the wire.** The executor computed `isError`, routed it
|
|
124
|
+
to the SSE bridge, the A2A bridge and the TUI, then dropped it at the provider
|
|
125
|
+
boundary — so the model's trained tool-failure recovery never fired. The
|
|
126
|
+
Anthropic driver now sends `is_error: true`, and the value survives the
|
|
127
|
+
executor's result tuple, which previously narrowed to `{toolCallId, output}`
|
|
128
|
+
before the message was built.
|
|
129
|
+
|
|
130
|
+
**Reasoning is representable and replayed verbatim.** `AssistantMessage.reasoning`
|
|
131
|
+
holds opaque `ReasoningBlock`s (thinking / redacted, with signature or encrypted
|
|
132
|
+
payload). The Anthropic driver used to rebuild every assistant turn as
|
|
133
|
+
`[text?, ...tool_use]` — precisely the pattern the verbatim-echo contract
|
|
134
|
+
prohibits when a `tool_result` follows — and now emits stored reasoning blocks
|
|
135
|
+
first, signature intact.
|
|
136
|
+
|
|
137
|
+
Drivers that cannot express non-text tool results (`@namzu/openai`,
|
|
138
|
+
`@namzu/ollama`) degrade through `toolResultToText`, which renders an explicit
|
|
139
|
+
`[image: …]` placeholder rather than dumping base64 or silently dropping it.
|
|
140
|
+
|
|
141
|
+
This is the outbound half. The Anthropic driver does not yet parse thinking
|
|
142
|
+
blocks out of the stream and `ChatCompletionParams` has no `thinking` field,
|
|
143
|
+
so `reasoning` is populated only when a caller supplies it.
|
|
144
|
+
|
|
145
|
+
- 935b8f3: A turn that asked for tools no longer ends because the provider said it
|
|
146
|
+
didn't.
|
|
147
|
+
|
|
148
|
+
The iteration loop ended the turn on `finishReason === 'stop'` **before**
|
|
149
|
+
looking at whether the model had asked for tools. Endpoints on the OpenAI
|
|
150
|
+
wire shape — gateways and local servers especially — routinely report `stop`
|
|
151
|
+
on the same response that carries a populated `tool_calls`, and three of
|
|
152
|
+
this repo's drivers passed that value straight through.
|
|
153
|
+
|
|
154
|
+
The damage was total and silent: every requested call skipped, an assistant
|
|
155
|
+
turn left carrying `tool_use` blocks nothing ever answered, and the run
|
|
156
|
+
settling as though it had finished the work it never started.
|
|
157
|
+
|
|
158
|
+
- **The runtime now treats tool calls as the fact and the finish reason as
|
|
159
|
+
the summary.** When they disagree, the calls win. This is the load-bearing
|
|
160
|
+
fix: it protects every driver, including ones this repo does not ship.
|
|
161
|
+
- **The three drivers that cast the reason raw now report it honestly** —
|
|
162
|
+
a stream that produced a tool call reports `tool_calls`, whatever the
|
|
163
|
+
endpoint called it. Defence in depth, and it makes the reported reason
|
|
164
|
+
true for anyone else reading it.
|
|
165
|
+
|
|
166
|
+
The existing suite could not catch this: the scripted mock reports
|
|
167
|
+
`tool_calls` whenever it emits one, which is what an honest provider does
|
|
168
|
+
and therefore never the case that breaks.
|
|
169
|
+
|
|
3
170
|
## 1.0.3
|
|
4
171
|
|
|
5
172
|
### Patch Changes
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ChatCompletionParams, LLMProvider, ModelInfo, ProviderCapabilities, StreamChunk } from '@namzu/sdk';
|
|
2
|
-
import type { ChatCompletionMessageParam } from 'openai/resources/chat/completions';
|
|
2
|
+
import type { ChatCompletionMessageParam, ChatCompletionTool } from 'openai/resources/chat/completions';
|
|
3
3
|
import type { OpenAIConfig } from './types.js';
|
|
4
4
|
/**
|
|
5
5
|
* Full capability set — this driver maps tools (`toOpenAITools`), streams
|
|
@@ -7,7 +7,34 @@ import type { OpenAIConfig } from './types.js';
|
|
|
7
7
|
* content parts with base64 data URIs (`toOpenAIMessages`).
|
|
8
8
|
*/
|
|
9
9
|
export declare const OPENAI_CAPABILITIES: ProviderCapabilities;
|
|
10
|
+
/**
|
|
11
|
+
* Refuse an extended-thinking request this driver does not implement.
|
|
12
|
+
*
|
|
13
|
+
* The parameter was accepted and dropped, so a caller who had asked for
|
|
14
|
+
* reasoning got an ordinary completion: no thinking, no reasoning blocks,
|
|
15
|
+
* no error — and the empty reasoning array reads as "the model did not
|
|
16
|
+
* reason" rather than "nobody asked it to". Turning it OFF is honoured as
|
|
17
|
+
* a no-op, because that is the state the driver is already in.
|
|
18
|
+
*/
|
|
19
|
+
export declare function assertThinkingSupported(params: {
|
|
20
|
+
thinking?: {
|
|
21
|
+
type: 'adaptive' | 'enabled' | 'disabled';
|
|
22
|
+
};
|
|
23
|
+
}): void;
|
|
10
24
|
export declare function toOpenAIMessages(messages: ChatCompletionParams['messages']): ChatCompletionMessageParam[];
|
|
25
|
+
/**
|
|
26
|
+
* Tool schemas, with constrained generation where the caller asked for it.
|
|
27
|
+
*
|
|
28
|
+
* `enforceToolInputSchema` names the tools whose schema should be enforced
|
|
29
|
+
* rather than suggested. Both sibling drivers consumed it; this one dropped
|
|
30
|
+
* it on the floor, so a caller who had asked for a guaranteed-valid tool
|
|
31
|
+
* input got a best-effort one — and found out from a repair attempt rather
|
|
32
|
+
* than from an error.
|
|
33
|
+
*
|
|
34
|
+
* This wire is the one the flag maps onto most directly: it takes `strict`
|
|
35
|
+
* on the function itself.
|
|
36
|
+
*/
|
|
37
|
+
export declare function toOpenAITools(params: ChatCompletionParams): ChatCompletionTool[] | undefined;
|
|
11
38
|
export declare class OpenAIProvider implements LLMProvider {
|
|
12
39
|
readonly id = "openai";
|
|
13
40
|
readonly name = "OpenAI";
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,oBAAoB,EAEpB,WAAW,EACX,SAAS,EACT,oBAAoB,EACpB,WAAW,EAGX,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,o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CallerAbortError, isProviderRequestError, providerVendorError, } from '@namzu/sdk';
|
|
2
4
|
import OpenAI from 'openai';
|
|
3
5
|
/**
|
|
@@ -10,6 +12,7 @@ export const OPENAI_CAPABILITIES = {
|
|
|
10
12
|
supportsStreaming: true,
|
|
11
13
|
supportsFunctionCalling: true,
|
|
12
14
|
supportsVision: true,
|
|
15
|
+
supportsDocuments: true,
|
|
13
16
|
};
|
|
14
17
|
function mapFinishReason(reason) {
|
|
15
18
|
switch (reason) {
|
|
@@ -54,22 +57,71 @@ function formatToolChoice(tc) {
|
|
|
54
57
|
}
|
|
55
58
|
return undefined;
|
|
56
59
|
}
|
|
60
|
+
/**
|
|
61
|
+
* Refuse an extended-thinking request this driver does not implement.
|
|
62
|
+
*
|
|
63
|
+
* The parameter was accepted and dropped, so a caller who had asked for
|
|
64
|
+
* reasoning got an ordinary completion: no thinking, no reasoning blocks,
|
|
65
|
+
* no error — and the empty reasoning array reads as "the model did not
|
|
66
|
+
* reason" rather than "nobody asked it to". Turning it OFF is honoured as
|
|
67
|
+
* a no-op, because that is the state the driver is already in.
|
|
68
|
+
*/
|
|
69
|
+
export function assertThinkingSupported(params) {
|
|
70
|
+
// Delegates now. The rule was decided here and then applied only here,
|
|
71
|
+
// while five sibling drivers went on dropping the field silently — so it
|
|
72
|
+
// moved to the SDK where a new driver inherits it instead of re-deciding
|
|
73
|
+
// it. Kept as a named export because it is one, and removing it would
|
|
74
|
+
// break a caller for no gain.
|
|
75
|
+
assertThinkingUnsupported('OpenAIProvider', params);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Refuse a document whose citations this wire cannot return.
|
|
79
|
+
*
|
|
80
|
+
* Citations are the difference between an answer you trust and one you
|
|
81
|
+
* verify, and this request format has no mechanism to carry them back.
|
|
82
|
+
* Sending the document anyway would answer the question and quietly drop
|
|
83
|
+
* the checkability the caller specifically asked for — the caller would
|
|
84
|
+
* see prose, no error, and an empty `citations` array they might read as
|
|
85
|
+
* "the model cited nothing" rather than "nobody asked".
|
|
86
|
+
*/
|
|
87
|
+
function assertCitationsSupported(attachment, index) {
|
|
88
|
+
if (!attachment.citations)
|
|
89
|
+
return;
|
|
90
|
+
const which = attachment.name ?? `attachment ${index}`;
|
|
91
|
+
throw new Error(`This provider cannot return citations, and ${which} asked for them. Drop \`citations\` to send the document without them, or use a provider whose capabilities include citation support.`);
|
|
92
|
+
}
|
|
57
93
|
export function toOpenAIMessages(messages) {
|
|
58
94
|
return messages.map((msg) => {
|
|
59
95
|
if (msg.role === 'system') {
|
|
60
96
|
return { role: 'system', content: msg.content };
|
|
61
97
|
}
|
|
62
98
|
if (msg.role === 'user') {
|
|
63
|
-
// User message with
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
//
|
|
99
|
+
// User message with attachments → multimodal content parts, text
|
|
100
|
+
// first. Images become an `image_url` part carrying a base64 data
|
|
101
|
+
// URI; documents become a `file` part. Plain text-only user
|
|
102
|
+
// messages keep the string form.
|
|
67
103
|
if (msg.attachments && msg.attachments.length > 0) {
|
|
68
104
|
const parts = [];
|
|
69
105
|
if (msg.content.length > 0) {
|
|
70
106
|
parts.push({ type: 'text', text: msg.content });
|
|
71
107
|
}
|
|
72
|
-
for (const att of msg.attachments) {
|
|
108
|
+
for (const [index, att] of msg.attachments.entries()) {
|
|
109
|
+
if (att.type === 'document') {
|
|
110
|
+
// A document is not an image with a different media
|
|
111
|
+
// type. Every attachment used to become an `image_url`,
|
|
112
|
+
// so a PDF went up as `data:application/pdf;base64,...`
|
|
113
|
+
// inside an image part — while the capability set
|
|
114
|
+
// claimed documents were supported.
|
|
115
|
+
assertCitationsSupported(att, index);
|
|
116
|
+
parts.push({
|
|
117
|
+
type: 'file',
|
|
118
|
+
file: {
|
|
119
|
+
file_data: `data:${att.mediaType};base64,${att.data}`,
|
|
120
|
+
...(att.name ? { filename: att.name } : {}),
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
73
125
|
parts.push({
|
|
74
126
|
type: 'image_url',
|
|
75
127
|
image_url: { url: `data:${att.mediaType};base64,${att.data}` },
|
|
@@ -82,7 +134,10 @@ export function toOpenAIMessages(messages) {
|
|
|
82
134
|
if (msg.role === 'tool') {
|
|
83
135
|
return {
|
|
84
136
|
role: 'tool',
|
|
85
|
-
content
|
|
137
|
+
// Tool messages are text-only on this wire, so a content block
|
|
138
|
+
// degrades to an honest placeholder rather than having its
|
|
139
|
+
// base64 payload dumped as text.
|
|
140
|
+
content: toolResultToText(msg.content),
|
|
86
141
|
tool_call_id: msg.toolCallId,
|
|
87
142
|
};
|
|
88
143
|
}
|
|
@@ -105,15 +160,29 @@ export function toOpenAIMessages(messages) {
|
|
|
105
160
|
return assistant;
|
|
106
161
|
});
|
|
107
162
|
}
|
|
108
|
-
|
|
163
|
+
/**
|
|
164
|
+
* Tool schemas, with constrained generation where the caller asked for it.
|
|
165
|
+
*
|
|
166
|
+
* `enforceToolInputSchema` names the tools whose schema should be enforced
|
|
167
|
+
* rather than suggested. Both sibling drivers consumed it; this one dropped
|
|
168
|
+
* it on the floor, so a caller who had asked for a guaranteed-valid tool
|
|
169
|
+
* input got a best-effort one — and found out from a repair attempt rather
|
|
170
|
+
* than from an error.
|
|
171
|
+
*
|
|
172
|
+
* This wire is the one the flag maps onto most directly: it takes `strict`
|
|
173
|
+
* on the function itself.
|
|
174
|
+
*/
|
|
175
|
+
export function toOpenAITools(params) {
|
|
109
176
|
if (!params.tools || params.tools.length === 0)
|
|
110
177
|
return undefined;
|
|
178
|
+
const enforced = new Set(params.enforceToolInputSchema ?? []);
|
|
111
179
|
return params.tools.map((t) => ({
|
|
112
180
|
type: 'function',
|
|
113
181
|
function: {
|
|
114
182
|
name: t.function.name,
|
|
115
183
|
description: t.function.description ?? '',
|
|
116
184
|
parameters: (t.function.parameters ?? {}),
|
|
185
|
+
...(enforced.has(t.function.name) ? { strict: true } : {}),
|
|
117
186
|
},
|
|
118
187
|
}));
|
|
119
188
|
}
|
|
@@ -152,6 +221,7 @@ export class OpenAIProvider {
|
|
|
152
221
|
}
|
|
153
222
|
async *chatStream(params) {
|
|
154
223
|
const model = this.resolveModel(params);
|
|
224
|
+
assertThinkingSupported(params);
|
|
155
225
|
let stream;
|
|
156
226
|
try {
|
|
157
227
|
stream = await this.client.chat.completions.create({
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAUA,OAAO,EACN,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,GACnB,MAAM,YAAY,CAAA;AACnB,OAAO,MAAM,MAAM,QAAQ,CAAA;AAS3B;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAyB;IACxD,aAAa,EAAE,IAAI;IACnB,iBAAiB,EAAE,IAAI;IACvB,uBAAuB,EAAE,IAAI;IAC7B,cAAc,EAAE,IAAI;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAA;AAC7C,OAAO,EACN,oBAAoB,EACpB,kBAAkB,EAClB,sBAAsB,EACtB,mBAAmB,GACnB,MAAM,YAAY,CAAA;AACnB,OAAO,MAAM,MAAM,QAAQ,CAAA;AAS3B;;;;GAIG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAyB;IACxD,aAAa,EAAE,IAAI;IACnB,iBAAiB,EAAE,IAAI;IACvB,uBAAuB,EAAE,IAAI;IAC7B,cAAc,EAAE,IAAI;IACpB,iBAAiB,EAAE,IAAI;CACvB,CAAA;AAMD,SAAS,eAAe,CAAC,MAA6C;IACrE,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,QAAQ;YACZ,OAAO,QAAQ,CAAA;QAChB,KAAK,YAAY,CAAC;QAClB,KA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AC;IAED,KAAK,CAAC,WAAW;QAChB,IAAI,CAAC;YACJ,MAAM,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,CAAA;YAC/B,OAAO,IAAI,CAAA;QACZ,CAAC;QAAC,MAAM,CAAC;YACR,OAAO,KAAK,CAAA;QACb,CAAC;IACF,CAAC;CACD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict-schema.d.ts","sourceRoot":"","sources":["../src/strict-schema.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strict-schema.js","sourceRoot":"","sources":["../src/strict-schema.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@namzu/openai",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "OpenAI LLM provider for @namzu/sdk via the official `openai` npm SDK. Chat Completions API with streaming + tool-use.",
|
|
5
5
|
"license": "FSL-1.1-MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -23,7 +23,17 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
|
+
"!dist/**/*.test.*",
|
|
27
|
+
"!dist/**/*.proc-test.*",
|
|
28
|
+
"!dist/**/__tests__",
|
|
29
|
+
"!dist/**/__fixtures__",
|
|
30
|
+
"!dist/**/test-setup.*",
|
|
26
31
|
"src",
|
|
32
|
+
"!src/**/*.test.*",
|
|
33
|
+
"!src/**/*.proc-test.*",
|
|
34
|
+
"!src/**/__tests__",
|
|
35
|
+
"!src/**/__fixtures__",
|
|
36
|
+
"!src/**/test-setup.*",
|
|
27
37
|
"LICENSE.md",
|
|
28
38
|
"README.md",
|
|
29
39
|
"CHANGELOG.md"
|
|
@@ -43,8 +53,8 @@
|
|
|
43
53
|
"@biomejs/biome": "^1.9.4",
|
|
44
54
|
"@types/node": "^22.19.17",
|
|
45
55
|
"typescript": "^5.5.0",
|
|
46
|
-
"vitest": "^2.
|
|
47
|
-
"@namzu/sdk": "^
|
|
56
|
+
"vitest": "^3.2.6",
|
|
57
|
+
"@namzu/sdk": "^15.0.0"
|
|
48
58
|
},
|
|
49
59
|
"scripts": {
|
|
50
60
|
"build": "tsc --build",
|
package/src/client.ts
CHANGED
|
@@ -8,6 +8,9 @@ import type {
|
|
|
8
8
|
TokenUsage,
|
|
9
9
|
ToolChoice,
|
|
10
10
|
} from '@namzu/sdk'
|
|
11
|
+
import { assertThinkingUnsupported } from '@namzu/sdk'
|
|
12
|
+
import type { ThinkingConfig } from '@namzu/sdk'
|
|
13
|
+
import { toolResultToText } from '@namzu/sdk'
|
|
11
14
|
import {
|
|
12
15
|
ProviderRequestError,
|
|
13
16
|
isCallerAbortError,
|
|
@@ -33,6 +36,7 @@ export const OPENAI_CAPABILITIES: ProviderCapabilities = {
|
|
|
33
36
|
supportsStreaming: true,
|
|
34
37
|
supportsFunctionCalling: true,
|
|
35
38
|
supportsVision: true,
|
|
39
|
+
supportsDocuments: true,
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
type OpenAIFinishReason = 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call'
|
|
@@ -90,6 +94,47 @@ function formatToolChoice(tc: ToolChoice | undefined): ChatCompletionToolChoiceO
|
|
|
90
94
|
return undefined
|
|
91
95
|
}
|
|
92
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Refuse an extended-thinking request this driver does not implement.
|
|
99
|
+
*
|
|
100
|
+
* The parameter was accepted and dropped, so a caller who had asked for
|
|
101
|
+
* reasoning got an ordinary completion: no thinking, no reasoning blocks,
|
|
102
|
+
* no error — and the empty reasoning array reads as "the model did not
|
|
103
|
+
* reason" rather than "nobody asked it to". Turning it OFF is honoured as
|
|
104
|
+
* a no-op, because that is the state the driver is already in.
|
|
105
|
+
*/
|
|
106
|
+
export function assertThinkingSupported(params: {
|
|
107
|
+
thinking?: { type: 'adaptive' | 'enabled' | 'disabled' }
|
|
108
|
+
}): void {
|
|
109
|
+
// Delegates now. The rule was decided here and then applied only here,
|
|
110
|
+
// while five sibling drivers went on dropping the field silently — so it
|
|
111
|
+
// moved to the SDK where a new driver inherits it instead of re-deciding
|
|
112
|
+
// it. Kept as a named export because it is one, and removing it would
|
|
113
|
+
// break a caller for no gain.
|
|
114
|
+
assertThinkingUnsupported('OpenAIProvider', params as { thinking?: ThinkingConfig })
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Refuse a document whose citations this wire cannot return.
|
|
119
|
+
*
|
|
120
|
+
* Citations are the difference between an answer you trust and one you
|
|
121
|
+
* verify, and this request format has no mechanism to carry them back.
|
|
122
|
+
* Sending the document anyway would answer the question and quietly drop
|
|
123
|
+
* the checkability the caller specifically asked for — the caller would
|
|
124
|
+
* see prose, no error, and an empty `citations` array they might read as
|
|
125
|
+
* "the model cited nothing" rather than "nobody asked".
|
|
126
|
+
*/
|
|
127
|
+
function assertCitationsSupported(
|
|
128
|
+
attachment: { citations?: boolean; name?: string },
|
|
129
|
+
index: number,
|
|
130
|
+
): void {
|
|
131
|
+
if (!attachment.citations) return
|
|
132
|
+
const which = attachment.name ?? `attachment ${index}`
|
|
133
|
+
throw new Error(
|
|
134
|
+
`This provider cannot return citations, and ${which} asked for them. Drop \`citations\` to send the document without them, or use a provider whose capabilities include citation support.`,
|
|
135
|
+
)
|
|
136
|
+
}
|
|
137
|
+
|
|
93
138
|
export function toOpenAIMessages(
|
|
94
139
|
messages: ChatCompletionParams['messages'],
|
|
95
140
|
): ChatCompletionMessageParam[] {
|
|
@@ -98,16 +143,32 @@ export function toOpenAIMessages(
|
|
|
98
143
|
return { role: 'system', content: msg.content }
|
|
99
144
|
}
|
|
100
145
|
if (msg.role === 'user') {
|
|
101
|
-
// User message with
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
//
|
|
146
|
+
// User message with attachments → multimodal content parts, text
|
|
147
|
+
// first. Images become an `image_url` part carrying a base64 data
|
|
148
|
+
// URI; documents become a `file` part. Plain text-only user
|
|
149
|
+
// messages keep the string form.
|
|
105
150
|
if (msg.attachments && msg.attachments.length > 0) {
|
|
106
151
|
const parts: ChatCompletionContentPart[] = []
|
|
107
152
|
if (msg.content.length > 0) {
|
|
108
153
|
parts.push({ type: 'text', text: msg.content })
|
|
109
154
|
}
|
|
110
|
-
for (const att of msg.attachments) {
|
|
155
|
+
for (const [index, att] of msg.attachments.entries()) {
|
|
156
|
+
if (att.type === 'document') {
|
|
157
|
+
// A document is not an image with a different media
|
|
158
|
+
// type. Every attachment used to become an `image_url`,
|
|
159
|
+
// so a PDF went up as `data:application/pdf;base64,...`
|
|
160
|
+
// inside an image part — while the capability set
|
|
161
|
+
// claimed documents were supported.
|
|
162
|
+
assertCitationsSupported(att, index)
|
|
163
|
+
parts.push({
|
|
164
|
+
type: 'file',
|
|
165
|
+
file: {
|
|
166
|
+
file_data: `data:${att.mediaType};base64,${att.data}`,
|
|
167
|
+
...(att.name ? { filename: att.name } : {}),
|
|
168
|
+
},
|
|
169
|
+
} as ChatCompletionContentPart)
|
|
170
|
+
continue
|
|
171
|
+
}
|
|
111
172
|
parts.push({
|
|
112
173
|
type: 'image_url',
|
|
113
174
|
image_url: { url: `data:${att.mediaType};base64,${att.data}` },
|
|
@@ -120,7 +181,10 @@ export function toOpenAIMessages(
|
|
|
120
181
|
if (msg.role === 'tool') {
|
|
121
182
|
return {
|
|
122
183
|
role: 'tool',
|
|
123
|
-
content
|
|
184
|
+
// Tool messages are text-only on this wire, so a content block
|
|
185
|
+
// degrades to an honest placeholder rather than having its
|
|
186
|
+
// base64 payload dumped as text.
|
|
187
|
+
content: toolResultToText(msg.content),
|
|
124
188
|
tool_call_id: msg.toolCallId,
|
|
125
189
|
}
|
|
126
190
|
}
|
|
@@ -143,14 +207,28 @@ export function toOpenAIMessages(
|
|
|
143
207
|
})
|
|
144
208
|
}
|
|
145
209
|
|
|
146
|
-
|
|
210
|
+
/**
|
|
211
|
+
* Tool schemas, with constrained generation where the caller asked for it.
|
|
212
|
+
*
|
|
213
|
+
* `enforceToolInputSchema` names the tools whose schema should be enforced
|
|
214
|
+
* rather than suggested. Both sibling drivers consumed it; this one dropped
|
|
215
|
+
* it on the floor, so a caller who had asked for a guaranteed-valid tool
|
|
216
|
+
* input got a best-effort one — and found out from a repair attempt rather
|
|
217
|
+
* than from an error.
|
|
218
|
+
*
|
|
219
|
+
* This wire is the one the flag maps onto most directly: it takes `strict`
|
|
220
|
+
* on the function itself.
|
|
221
|
+
*/
|
|
222
|
+
export function toOpenAITools(params: ChatCompletionParams): ChatCompletionTool[] | undefined {
|
|
147
223
|
if (!params.tools || params.tools.length === 0) return undefined
|
|
224
|
+
const enforced = new Set(params.enforceToolInputSchema ?? [])
|
|
148
225
|
return params.tools.map((t) => ({
|
|
149
226
|
type: 'function' as const,
|
|
150
227
|
function: {
|
|
151
228
|
name: t.function.name,
|
|
152
229
|
description: t.function.description ?? '',
|
|
153
230
|
parameters: (t.function.parameters ?? {}) as Record<string, unknown>,
|
|
231
|
+
...(enforced.has(t.function.name) ? { strict: true } : {}),
|
|
154
232
|
},
|
|
155
233
|
}))
|
|
156
234
|
}
|
|
@@ -193,6 +271,7 @@ export class OpenAIProvider implements LLMProvider {
|
|
|
193
271
|
|
|
194
272
|
async *chatStream(params: ChatCompletionParams): AsyncIterable<StreamChunk> {
|
|
195
273
|
const model = this.resolveModel(params)
|
|
274
|
+
assertThinkingSupported(params)
|
|
196
275
|
|
|
197
276
|
let stream: Awaited<ReturnType<typeof this.client.chat.completions.create>>
|
|
198
277
|
try {
|
|
File without changes
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Provider error taxonomy — OpenAI driver.
|
|
3
|
-
*
|
|
4
|
-
* Like the Anthropic driver, this one lets the vendor SDK's own error type
|
|
5
|
-
* escape verbatim (`RateLimitError`, `BadRequestError`, …). Those carry a
|
|
6
|
-
* status but are vendor classes, so no caller can classify them without
|
|
7
|
-
* importing `openai` — and their `message` interpolates the response body.
|
|
8
|
-
*
|
|
9
|
-
* Transport seam: `OpenAIConfig.baseURL` — a loopback server answering
|
|
10
|
-
* with a scripted status/headers/body, so the REAL vendor client runs
|
|
11
|
-
* (including its internal retries, which this group must not disturb).
|
|
12
|
-
*/
|
|
13
|
-
export {};
|
|
14
|
-
//# sourceMappingURL=error-taxonomy.test.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error-taxonomy.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/error-taxonomy.test.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG"}
|