@namzu/openai 1.0.3 → 1.1.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/CHANGELOG.md +138 -0
- package/dist/__tests__/document-input.test.d.ts +2 -0
- package/dist/__tests__/document-input.test.d.ts.map +1 -0
- package/dist/__tests__/document-input.test.js +92 -0
- package/dist/__tests__/document-input.test.js.map +1 -0
- package/dist/__tests__/openai.test.js +74 -3
- package/dist/__tests__/openai.test.js.map +1 -1
- package/dist/__tests__/reasoning-params.test.d.ts +2 -0
- package/dist/__tests__/reasoning-params.test.d.ts.map +1 -0
- package/dist/__tests__/reasoning-params.test.js +61 -0
- package/dist/__tests__/reasoning-params.test.js.map +1 -0
- package/dist/__tests__/strict-schema.test.d.ts +2 -0
- package/dist/__tests__/strict-schema.test.d.ts.map +1 -0
- package/dist/__tests__/strict-schema.test.js +85 -0
- package/dist/__tests__/strict-schema.test.js.map +1 -0
- package/dist/client.d.ts +28 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +75 -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 +3 -3
- package/src/__tests__/document-input.test.ts +125 -0
- package/src/__tests__/openai.test.ts +77 -3
- package/src/__tests__/reasoning-params.test.ts +76 -0
- package/src/__tests__/strict-schema.test.ts +110 -0
- package/src/client.ts +84 -7
- package/src/strict-schema.ts +0 -0
package/src/client.ts
CHANGED
|
@@ -8,6 +8,7 @@ import type {
|
|
|
8
8
|
TokenUsage,
|
|
9
9
|
ToolChoice,
|
|
10
10
|
} from '@namzu/sdk'
|
|
11
|
+
import { toolResultToText } from '@namzu/sdk'
|
|
11
12
|
import {
|
|
12
13
|
ProviderRequestError,
|
|
13
14
|
isCallerAbortError,
|
|
@@ -33,6 +34,7 @@ export const OPENAI_CAPABILITIES: ProviderCapabilities = {
|
|
|
33
34
|
supportsStreaming: true,
|
|
34
35
|
supportsFunctionCalling: true,
|
|
35
36
|
supportsVision: true,
|
|
37
|
+
supportsDocuments: true,
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
type OpenAIFinishReason = 'stop' | 'length' | 'tool_calls' | 'content_filter' | 'function_call'
|
|
@@ -90,6 +92,47 @@ function formatToolChoice(tc: ToolChoice | undefined): ChatCompletionToolChoiceO
|
|
|
90
92
|
return undefined
|
|
91
93
|
}
|
|
92
94
|
|
|
95
|
+
/**
|
|
96
|
+
* Refuse an extended-thinking request this driver does not implement.
|
|
97
|
+
*
|
|
98
|
+
* The parameter was accepted and dropped, so a caller who had asked for
|
|
99
|
+
* reasoning got an ordinary completion: no thinking, no reasoning blocks,
|
|
100
|
+
* no error — and the empty reasoning array reads as "the model did not
|
|
101
|
+
* reason" rather than "nobody asked it to". Turning it OFF is honoured as
|
|
102
|
+
* a no-op, because that is the state the driver is already in.
|
|
103
|
+
*/
|
|
104
|
+
export function assertThinkingSupported(params: {
|
|
105
|
+
thinking?: { type: 'enabled' | 'disabled' }
|
|
106
|
+
}): void {
|
|
107
|
+
if (params.thinking?.type !== 'enabled') return
|
|
108
|
+
throw new Error(
|
|
109
|
+
'This provider driver does not implement extended thinking, and silently ' +
|
|
110
|
+
'ignoring the request would return an ordinary completion with an empty ' +
|
|
111
|
+
'reasoning list. Drop `thinking`, or use a driver that implements it.',
|
|
112
|
+
)
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Refuse a document whose citations this wire cannot return.
|
|
117
|
+
*
|
|
118
|
+
* Citations are the difference between an answer you trust and one you
|
|
119
|
+
* verify, and this request format has no mechanism to carry them back.
|
|
120
|
+
* Sending the document anyway would answer the question and quietly drop
|
|
121
|
+
* the checkability the caller specifically asked for — the caller would
|
|
122
|
+
* see prose, no error, and an empty `citations` array they might read as
|
|
123
|
+
* "the model cited nothing" rather than "nobody asked".
|
|
124
|
+
*/
|
|
125
|
+
function assertCitationsSupported(
|
|
126
|
+
attachment: { citations?: boolean; name?: string },
|
|
127
|
+
index: number,
|
|
128
|
+
): void {
|
|
129
|
+
if (!attachment.citations) return
|
|
130
|
+
const which = attachment.name ?? `attachment ${index}`
|
|
131
|
+
throw new Error(
|
|
132
|
+
`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.`,
|
|
133
|
+
)
|
|
134
|
+
}
|
|
135
|
+
|
|
93
136
|
export function toOpenAIMessages(
|
|
94
137
|
messages: ChatCompletionParams['messages'],
|
|
95
138
|
): ChatCompletionMessageParam[] {
|
|
@@ -98,16 +141,32 @@ export function toOpenAIMessages(
|
|
|
98
141
|
return { role: 'system', content: msg.content }
|
|
99
142
|
}
|
|
100
143
|
if (msg.role === 'user') {
|
|
101
|
-
// User message with
|
|
102
|
-
//
|
|
103
|
-
//
|
|
104
|
-
//
|
|
144
|
+
// User message with attachments → multimodal content parts, text
|
|
145
|
+
// first. Images become an `image_url` part carrying a base64 data
|
|
146
|
+
// URI; documents become a `file` part. Plain text-only user
|
|
147
|
+
// messages keep the string form.
|
|
105
148
|
if (msg.attachments && msg.attachments.length > 0) {
|
|
106
149
|
const parts: ChatCompletionContentPart[] = []
|
|
107
150
|
if (msg.content.length > 0) {
|
|
108
151
|
parts.push({ type: 'text', text: msg.content })
|
|
109
152
|
}
|
|
110
|
-
for (const att of msg.attachments) {
|
|
153
|
+
for (const [index, att] of msg.attachments.entries()) {
|
|
154
|
+
if (att.type === 'document') {
|
|
155
|
+
// A document is not an image with a different media
|
|
156
|
+
// type. Every attachment used to become an `image_url`,
|
|
157
|
+
// so a PDF went up as `data:application/pdf;base64,...`
|
|
158
|
+
// inside an image part — while the capability set
|
|
159
|
+
// claimed documents were supported.
|
|
160
|
+
assertCitationsSupported(att, index)
|
|
161
|
+
parts.push({
|
|
162
|
+
type: 'file',
|
|
163
|
+
file: {
|
|
164
|
+
file_data: `data:${att.mediaType};base64,${att.data}`,
|
|
165
|
+
...(att.name ? { filename: att.name } : {}),
|
|
166
|
+
},
|
|
167
|
+
} as ChatCompletionContentPart)
|
|
168
|
+
continue
|
|
169
|
+
}
|
|
111
170
|
parts.push({
|
|
112
171
|
type: 'image_url',
|
|
113
172
|
image_url: { url: `data:${att.mediaType};base64,${att.data}` },
|
|
@@ -120,7 +179,10 @@ export function toOpenAIMessages(
|
|
|
120
179
|
if (msg.role === 'tool') {
|
|
121
180
|
return {
|
|
122
181
|
role: 'tool',
|
|
123
|
-
content
|
|
182
|
+
// Tool messages are text-only on this wire, so a content block
|
|
183
|
+
// degrades to an honest placeholder rather than having its
|
|
184
|
+
// base64 payload dumped as text.
|
|
185
|
+
content: toolResultToText(msg.content),
|
|
124
186
|
tool_call_id: msg.toolCallId,
|
|
125
187
|
}
|
|
126
188
|
}
|
|
@@ -143,14 +205,28 @@ export function toOpenAIMessages(
|
|
|
143
205
|
})
|
|
144
206
|
}
|
|
145
207
|
|
|
146
|
-
|
|
208
|
+
/**
|
|
209
|
+
* Tool schemas, with constrained generation where the caller asked for it.
|
|
210
|
+
*
|
|
211
|
+
* `enforceToolInputSchema` names the tools whose schema should be enforced
|
|
212
|
+
* rather than suggested. Both sibling drivers consumed it; this one dropped
|
|
213
|
+
* it on the floor, so a caller who had asked for a guaranteed-valid tool
|
|
214
|
+
* input got a best-effort one — and found out from a repair attempt rather
|
|
215
|
+
* than from an error.
|
|
216
|
+
*
|
|
217
|
+
* This wire is the one the flag maps onto most directly: it takes `strict`
|
|
218
|
+
* on the function itself.
|
|
219
|
+
*/
|
|
220
|
+
export function toOpenAITools(params: ChatCompletionParams): ChatCompletionTool[] | undefined {
|
|
147
221
|
if (!params.tools || params.tools.length === 0) return undefined
|
|
222
|
+
const enforced = new Set(params.enforceToolInputSchema ?? [])
|
|
148
223
|
return params.tools.map((t) => ({
|
|
149
224
|
type: 'function' as const,
|
|
150
225
|
function: {
|
|
151
226
|
name: t.function.name,
|
|
152
227
|
description: t.function.description ?? '',
|
|
153
228
|
parameters: (t.function.parameters ?? {}) as Record<string, unknown>,
|
|
229
|
+
...(enforced.has(t.function.name) ? { strict: true } : {}),
|
|
154
230
|
},
|
|
155
231
|
}))
|
|
156
232
|
}
|
|
@@ -193,6 +269,7 @@ export class OpenAIProvider implements LLMProvider {
|
|
|
193
269
|
|
|
194
270
|
async *chatStream(params: ChatCompletionParams): AsyncIterable<StreamChunk> {
|
|
195
271
|
const model = this.resolveModel(params)
|
|
272
|
+
assertThinkingSupported(params)
|
|
196
273
|
|
|
197
274
|
let stream: Awaited<ReturnType<typeof this.client.chat.completions.create>>
|
|
198
275
|
try {
|
|
File without changes
|