@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/dist/client.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { toolResultToText } from '@namzu/sdk';
|
|
1
2
|
import { ProviderRequestError, isCallerAbortError, isProviderRequestError, providerVendorError, } from '@namzu/sdk';
|
|
2
3
|
import OpenAI from 'openai';
|
|
3
4
|
/**
|
|
@@ -10,6 +11,7 @@ export const OPENAI_CAPABILITIES = {
|
|
|
10
11
|
supportsStreaming: true,
|
|
11
12
|
supportsFunctionCalling: true,
|
|
12
13
|
supportsVision: true,
|
|
14
|
+
supportsDocuments: true,
|
|
13
15
|
};
|
|
14
16
|
function mapFinishReason(reason) {
|
|
15
17
|
switch (reason) {
|
|
@@ -54,22 +56,70 @@ function formatToolChoice(tc) {
|
|
|
54
56
|
}
|
|
55
57
|
return undefined;
|
|
56
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Refuse an extended-thinking request this driver does not implement.
|
|
61
|
+
*
|
|
62
|
+
* The parameter was accepted and dropped, so a caller who had asked for
|
|
63
|
+
* reasoning got an ordinary completion: no thinking, no reasoning blocks,
|
|
64
|
+
* no error — and the empty reasoning array reads as "the model did not
|
|
65
|
+
* reason" rather than "nobody asked it to". Turning it OFF is honoured as
|
|
66
|
+
* a no-op, because that is the state the driver is already in.
|
|
67
|
+
*/
|
|
68
|
+
export function assertThinkingSupported(params) {
|
|
69
|
+
if (params.thinking?.type !== 'enabled')
|
|
70
|
+
return;
|
|
71
|
+
throw new Error('This provider driver does not implement extended thinking, and silently ' +
|
|
72
|
+
'ignoring the request would return an ordinary completion with an empty ' +
|
|
73
|
+
'reasoning list. Drop `thinking`, or use a driver that implements it.');
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Refuse a document whose citations this wire cannot return.
|
|
77
|
+
*
|
|
78
|
+
* Citations are the difference between an answer you trust and one you
|
|
79
|
+
* verify, and this request format has no mechanism to carry them back.
|
|
80
|
+
* Sending the document anyway would answer the question and quietly drop
|
|
81
|
+
* the checkability the caller specifically asked for — the caller would
|
|
82
|
+
* see prose, no error, and an empty `citations` array they might read as
|
|
83
|
+
* "the model cited nothing" rather than "nobody asked".
|
|
84
|
+
*/
|
|
85
|
+
function assertCitationsSupported(attachment, index) {
|
|
86
|
+
if (!attachment.citations)
|
|
87
|
+
return;
|
|
88
|
+
const which = attachment.name ?? `attachment ${index}`;
|
|
89
|
+
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.`);
|
|
90
|
+
}
|
|
57
91
|
export function toOpenAIMessages(messages) {
|
|
58
92
|
return messages.map((msg) => {
|
|
59
93
|
if (msg.role === 'system') {
|
|
60
94
|
return { role: 'system', content: msg.content };
|
|
61
95
|
}
|
|
62
96
|
if (msg.role === 'user') {
|
|
63
|
-
// User message with
|
|
64
|
-
//
|
|
65
|
-
//
|
|
66
|
-
//
|
|
97
|
+
// User message with attachments → multimodal content parts, text
|
|
98
|
+
// first. Images become an `image_url` part carrying a base64 data
|
|
99
|
+
// URI; documents become a `file` part. Plain text-only user
|
|
100
|
+
// messages keep the string form.
|
|
67
101
|
if (msg.attachments && msg.attachments.length > 0) {
|
|
68
102
|
const parts = [];
|
|
69
103
|
if (msg.content.length > 0) {
|
|
70
104
|
parts.push({ type: 'text', text: msg.content });
|
|
71
105
|
}
|
|
72
|
-
for (const att of msg.attachments) {
|
|
106
|
+
for (const [index, att] of msg.attachments.entries()) {
|
|
107
|
+
if (att.type === 'document') {
|
|
108
|
+
// A document is not an image with a different media
|
|
109
|
+
// type. Every attachment used to become an `image_url`,
|
|
110
|
+
// so a PDF went up as `data:application/pdf;base64,...`
|
|
111
|
+
// inside an image part — while the capability set
|
|
112
|
+
// claimed documents were supported.
|
|
113
|
+
assertCitationsSupported(att, index);
|
|
114
|
+
parts.push({
|
|
115
|
+
type: 'file',
|
|
116
|
+
file: {
|
|
117
|
+
file_data: `data:${att.mediaType};base64,${att.data}`,
|
|
118
|
+
...(att.name ? { filename: att.name } : {}),
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
continue;
|
|
122
|
+
}
|
|
73
123
|
parts.push({
|
|
74
124
|
type: 'image_url',
|
|
75
125
|
image_url: { url: `data:${att.mediaType};base64,${att.data}` },
|
|
@@ -82,7 +132,10 @@ export function toOpenAIMessages(messages) {
|
|
|
82
132
|
if (msg.role === 'tool') {
|
|
83
133
|
return {
|
|
84
134
|
role: 'tool',
|
|
85
|
-
content
|
|
135
|
+
// Tool messages are text-only on this wire, so a content block
|
|
136
|
+
// degrades to an honest placeholder rather than having its
|
|
137
|
+
// base64 payload dumped as text.
|
|
138
|
+
content: toolResultToText(msg.content),
|
|
86
139
|
tool_call_id: msg.toolCallId,
|
|
87
140
|
};
|
|
88
141
|
}
|
|
@@ -105,15 +158,29 @@ export function toOpenAIMessages(messages) {
|
|
|
105
158
|
return assistant;
|
|
106
159
|
});
|
|
107
160
|
}
|
|
108
|
-
|
|
161
|
+
/**
|
|
162
|
+
* Tool schemas, with constrained generation where the caller asked for it.
|
|
163
|
+
*
|
|
164
|
+
* `enforceToolInputSchema` names the tools whose schema should be enforced
|
|
165
|
+
* rather than suggested. Both sibling drivers consumed it; this one dropped
|
|
166
|
+
* it on the floor, so a caller who had asked for a guaranteed-valid tool
|
|
167
|
+
* input got a best-effort one — and found out from a repair attempt rather
|
|
168
|
+
* than from an error.
|
|
169
|
+
*
|
|
170
|
+
* This wire is the one the flag maps onto most directly: it takes `strict`
|
|
171
|
+
* on the function itself.
|
|
172
|
+
*/
|
|
173
|
+
export function toOpenAITools(params) {
|
|
109
174
|
if (!params.tools || params.tools.length === 0)
|
|
110
175
|
return undefined;
|
|
176
|
+
const enforced = new Set(params.enforceToolInputSchema ?? []);
|
|
111
177
|
return params.tools.map((t) => ({
|
|
112
178
|
type: 'function',
|
|
113
179
|
function: {
|
|
114
180
|
name: t.function.name,
|
|
115
181
|
description: t.function.description ?? '',
|
|
116
182
|
parameters: (t.function.parameters ?? {}),
|
|
183
|
+
...(enforced.has(t.function.name) ? { strict: true } : {}),
|
|
117
184
|
},
|
|
118
185
|
}));
|
|
119
186
|
}
|
|
@@ -152,6 +219,7 @@ export class OpenAIProvider {
|
|
|
152
219
|
}
|
|
153
220
|
async *chatStream(params) {
|
|
154
221
|
const model = this.resolveModel(params);
|
|
222
|
+
assertThinkingSupported(params);
|
|
155
223
|
let stream;
|
|
156
224
|
try {
|
|
157
225
|
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,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,KAAK,eAAe;YACnB,OAAO,YAAY,CAAA;QACpB,KAAK,gBAAgB;YACpB,OAAO,gBAAgB,CAAA;QACxB;YACC,OAAO,MAAM,CAAA;IACf,CAAC;AACF,CAAC;AASD,SAAS,UAAU,CAAC,GAA2B;IAC9C,IAAI,CAAC,GAAG,EAAE,CAAC;QACV,OAAO;YACN,YAAY,EAAE,CAAC;YACf,gBAAgB,EAAE,CAAC;YAC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|
|
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.0
|
|
3
|
+
"version": "1.1.0",
|
|
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",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"@biomejs/biome": "^1.9.4",
|
|
44
44
|
"@types/node": "^22.19.17",
|
|
45
45
|
"typescript": "^5.5.0",
|
|
46
|
-
"vitest": "^2.
|
|
47
|
-
"@namzu/sdk": "^
|
|
46
|
+
"vitest": "^3.2.6",
|
|
47
|
+
"@namzu/sdk": "^3.0.0"
|
|
48
48
|
},
|
|
49
49
|
"scripts": {
|
|
50
50
|
"build": "tsc --build",
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import type { ChatCompletionParams } from '@namzu/sdk'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { OPENAI_CAPABILITIES, toOpenAIMessages } from '../client.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* This driver declared `supportsDocuments: true` and had no document
|
|
8
|
+
* branch: every attachment became an `image_url` part, so a PDF went up as
|
|
9
|
+
* `data:application/pdf;base64,...` inside an image. The capability was a
|
|
10
|
+
* claim nothing in the mapper could honour.
|
|
11
|
+
*
|
|
12
|
+
* Citations are the other half, and this wire has no way to return them.
|
|
13
|
+
* Sending the document anyway would answer the question and drop the
|
|
14
|
+
* checkability the caller asked for — silently, since an empty citation
|
|
15
|
+
* list reads as "the model cited nothing" rather than "nobody asked". So a
|
|
16
|
+
* document requesting citations is refused here rather than degraded.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
const PDF = 'JVBERi0xLjQKJSVFT0Y='
|
|
20
|
+
|
|
21
|
+
function partsFor(attachments: unknown[], content = 'summarize this') {
|
|
22
|
+
const [message] = toOpenAIMessages([
|
|
23
|
+
{ role: 'user', content, attachments },
|
|
24
|
+
] as unknown as ChatCompletionParams['messages'])
|
|
25
|
+
return message?.content as unknown as Record<string, unknown>[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe('a document attachment reaches the wire as a file', () => {
|
|
29
|
+
it('sends a file part, not an image part', async () => {
|
|
30
|
+
const parts = partsFor([{ type: 'document', data: PDF, mediaType: 'application/pdf' }])
|
|
31
|
+
|
|
32
|
+
const file = parts.find((p) => p.type === 'file')
|
|
33
|
+
expect(file?.file).toMatchObject({ file_data: `data:application/pdf;base64,${PDF}` })
|
|
34
|
+
// The bug in one assertion: it used to be an image part.
|
|
35
|
+
expect(parts.some((p) => p.type === 'image_url')).toBe(false)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
it('keeps the prose ahead of the file', () => {
|
|
39
|
+
const parts = partsFor([{ type: 'document', data: PDF, mediaType: 'application/pdf' }])
|
|
40
|
+
|
|
41
|
+
expect(parts[0]).toEqual({ type: 'text', text: 'summarize this' })
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
it('carries the filename when one was given', () => {
|
|
45
|
+
const parts = partsFor([
|
|
46
|
+
{ type: 'document', data: PDF, mediaType: 'application/pdf', name: 'lease.pdf' },
|
|
47
|
+
])
|
|
48
|
+
|
|
49
|
+
expect((parts.find((p) => p.type === 'file')?.file as { filename?: string }).filename).toBe(
|
|
50
|
+
'lease.pdf',
|
|
51
|
+
)
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
it('omits the filename when none was given', () => {
|
|
55
|
+
const parts = partsFor([{ type: 'document', data: PDF, mediaType: 'application/pdf' }])
|
|
56
|
+
|
|
57
|
+
expect(parts.find((p) => p.type === 'file')?.file).not.toHaveProperty('filename')
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('still maps an image attachment as an image', () => {
|
|
61
|
+
const parts = partsFor([{ type: 'image', data: 'AAAA', mediaType: 'image/png' }])
|
|
62
|
+
|
|
63
|
+
expect(parts.find((p) => p.type === 'image_url')?.image_url).toEqual({
|
|
64
|
+
url: 'data:image/png;base64,AAAA',
|
|
65
|
+
})
|
|
66
|
+
expect(parts.some((p) => p.type === 'file')).toBe(false)
|
|
67
|
+
})
|
|
68
|
+
|
|
69
|
+
it('carries both kinds in one message, in the order given', () => {
|
|
70
|
+
const parts = partsFor([
|
|
71
|
+
{ type: 'document', data: PDF, mediaType: 'application/pdf' },
|
|
72
|
+
{ type: 'image', data: 'AAAA', mediaType: 'image/png' },
|
|
73
|
+
])
|
|
74
|
+
|
|
75
|
+
expect(parts.map((p) => p.type)).toEqual(['text', 'file', 'image_url'])
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
it('leaves a plain text message as a string', () => {
|
|
79
|
+
const [message] = toOpenAIMessages([
|
|
80
|
+
{ role: 'user', content: 'no attachments here' },
|
|
81
|
+
] as ChatCompletionParams['messages'])
|
|
82
|
+
|
|
83
|
+
expect(message?.content).toBe('no attachments here')
|
|
84
|
+
})
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
describe('a document asking for citations is refused, not answered without them', () => {
|
|
88
|
+
it('throws rather than sending it', () => {
|
|
89
|
+
expect(() =>
|
|
90
|
+
partsFor([{ type: 'document', data: PDF, mediaType: 'application/pdf', citations: true }]),
|
|
91
|
+
).toThrow(/citations/i)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('names the document, so the caller knows which one to change', () => {
|
|
95
|
+
expect(() =>
|
|
96
|
+
partsFor([
|
|
97
|
+
{
|
|
98
|
+
type: 'document',
|
|
99
|
+
data: PDF,
|
|
100
|
+
mediaType: 'application/pdf',
|
|
101
|
+
name: 'lease.pdf',
|
|
102
|
+
citations: true,
|
|
103
|
+
},
|
|
104
|
+
]),
|
|
105
|
+
).toThrow(/lease\.pdf/)
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
it('says what to do instead', () => {
|
|
109
|
+
expect(() =>
|
|
110
|
+
partsFor([{ type: 'document', data: PDF, mediaType: 'application/pdf', citations: true }]),
|
|
111
|
+
).toThrow(/Drop `citations`/)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('accepts the same document without the request', () => {
|
|
115
|
+
expect(() =>
|
|
116
|
+
partsFor([{ type: 'document', data: PDF, mediaType: 'application/pdf', citations: false }]),
|
|
117
|
+
).not.toThrow()
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
describe('the capability claim matches the mapper', () => {
|
|
122
|
+
it('declares document support, which is now true', () => {
|
|
123
|
+
expect(OPENAI_CAPABILITIES.supportsDocuments).toBe(true)
|
|
124
|
+
})
|
|
125
|
+
})
|
|
@@ -47,6 +47,7 @@ describe('@namzu/openai', () => {
|
|
|
47
47
|
supportsStreaming: true,
|
|
48
48
|
supportsFunctionCalling: true,
|
|
49
49
|
supportsVision: true,
|
|
50
|
+
supportsDocuments: true,
|
|
50
51
|
})
|
|
51
52
|
})
|
|
52
53
|
|
|
@@ -97,8 +98,14 @@ describe('@namzu/openai', () => {
|
|
|
97
98
|
role: 'user',
|
|
98
99
|
content: [
|
|
99
100
|
{ type: 'text', text: 'what is in this image?' },
|
|
100
|
-
{
|
|
101
|
-
|
|
101
|
+
{
|
|
102
|
+
type: 'image_url',
|
|
103
|
+
image_url: { url: 'data:image/png;base64,aGVsbG8=' },
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
type: 'image_url',
|
|
107
|
+
image_url: { url: 'data:image/jpeg;base64,d29ybGQ=' },
|
|
108
|
+
},
|
|
102
109
|
],
|
|
103
110
|
})
|
|
104
111
|
})
|
|
@@ -115,7 +122,12 @@ describe('@namzu/openai', () => {
|
|
|
115
122
|
const [mapped] = toOpenAIMessages(messages)
|
|
116
123
|
expect(mapped).toEqual({
|
|
117
124
|
role: 'user',
|
|
118
|
-
content: [
|
|
125
|
+
content: [
|
|
126
|
+
{
|
|
127
|
+
type: 'image_url',
|
|
128
|
+
image_url: { url: 'data:image/webp;base64,aGVsbG8=' },
|
|
129
|
+
},
|
|
130
|
+
],
|
|
119
131
|
})
|
|
120
132
|
})
|
|
121
133
|
|
|
@@ -124,4 +136,66 @@ describe('@namzu/openai', () => {
|
|
|
124
136
|
expect(toOpenAIMessages(messages)).toEqual([{ role: 'user', content: 'hello' }])
|
|
125
137
|
})
|
|
126
138
|
})
|
|
139
|
+
|
|
140
|
+
it('keeps the Namzu enforcement hint out of the OpenAI SDK request', async () => {
|
|
141
|
+
const provider = new OpenAIProvider({ apiKey: 'test-key' })
|
|
142
|
+
let requestBody: Record<string, unknown> | undefined
|
|
143
|
+
;(provider as unknown as { client: unknown }).client = {
|
|
144
|
+
chat: {
|
|
145
|
+
completions: {
|
|
146
|
+
create: async (body: Record<string, unknown>) => {
|
|
147
|
+
requestBody = body
|
|
148
|
+
return (async function* () {
|
|
149
|
+
yield {
|
|
150
|
+
id: 'msg_done',
|
|
151
|
+
choices: [{ index: 0, delta: {}, finish_reason: 'stop' }],
|
|
152
|
+
usage: {
|
|
153
|
+
prompt_tokens: 1,
|
|
154
|
+
completion_tokens: 0,
|
|
155
|
+
total_tokens: 1,
|
|
156
|
+
},
|
|
157
|
+
}
|
|
158
|
+
})()
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
},
|
|
162
|
+
}
|
|
163
|
+
const tools = [
|
|
164
|
+
{
|
|
165
|
+
type: 'function' as const,
|
|
166
|
+
function: {
|
|
167
|
+
name: 'edit',
|
|
168
|
+
description: 'Edit',
|
|
169
|
+
parameters: { type: 'object' },
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
]
|
|
173
|
+
|
|
174
|
+
for await (const _chunk of provider.chatStream({
|
|
175
|
+
model: 'gpt-5',
|
|
176
|
+
messages: [{ role: 'user', content: 'edit' }],
|
|
177
|
+
tools,
|
|
178
|
+
enforceToolInputSchema: ['edit'],
|
|
179
|
+
})) {
|
|
180
|
+
// Drain the test stream.
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// The hint is not a wire field, so it must never appear verbatim —
|
|
184
|
+
// that is what "keeps it out of the request" means.
|
|
185
|
+
expect(requestBody).not.toHaveProperty('enforceToolInputSchema')
|
|
186
|
+
// It is consumed, not ignored. This used to assert the tools went
|
|
187
|
+
// through untouched, which read as "the hint is kept out" and
|
|
188
|
+
// actually pinned "the hint does nothing".
|
|
189
|
+
expect(requestBody?.tools).toEqual([
|
|
190
|
+
{
|
|
191
|
+
type: 'function',
|
|
192
|
+
function: {
|
|
193
|
+
name: 'edit',
|
|
194
|
+
description: 'Edit',
|
|
195
|
+
parameters: { type: 'object' },
|
|
196
|
+
strict: true,
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
])
|
|
200
|
+
})
|
|
127
201
|
})
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { OPENAI_CAPABILITIES, assertThinkingSupported } from '../client.js'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This driver does not implement extended thinking, and used to accept the
|
|
7
|
+
* request anyway.
|
|
8
|
+
*
|
|
9
|
+
* So a caller who asked for reasoning got an ordinary completion: no
|
|
10
|
+
* thinking, no reasoning blocks, and no error. The empty reasoning list
|
|
11
|
+
* that came back reads as "the model did not reason" rather than "nobody
|
|
12
|
+
* asked it to" — which is the failure mode, not the missing feature. Same
|
|
13
|
+
* call as the citations one in this driver: refuse what cannot be
|
|
14
|
+
* delivered rather than deliver something else quietly.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
describe('an extended-thinking request is refused, not dropped', () => {
|
|
18
|
+
it('throws when thinking is asked for', () => {
|
|
19
|
+
expect(() => assertThinkingSupported({ thinking: { type: 'enabled' } })).toThrow(
|
|
20
|
+
/extended thinking/i,
|
|
21
|
+
)
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
it('says what to do instead', () => {
|
|
25
|
+
expect(() => assertThinkingSupported({ thinking: { type: 'enabled' } })).toThrow(
|
|
26
|
+
/Drop `thinking`/,
|
|
27
|
+
)
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
it('explains why silence would be worse than an error', () => {
|
|
31
|
+
// The message has to name the symptom the caller would otherwise
|
|
32
|
+
// see, or they will read the empty list as an answer.
|
|
33
|
+
expect(() => assertThinkingSupported({ thinking: { type: 'enabled' } })).toThrow(
|
|
34
|
+
/empty reasoning list/,
|
|
35
|
+
)
|
|
36
|
+
})
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
describe('asking for it to be off is honoured as a no-op', () => {
|
|
40
|
+
it('accepts an explicit disable', () => {
|
|
41
|
+
// Off is the state this driver is already in, so refusing here would
|
|
42
|
+
// reject a request it is in fact satisfying.
|
|
43
|
+
expect(() => assertThinkingSupported({ thinking: { type: 'disabled' } })).not.toThrow()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
it('accepts a call that says nothing about thinking', () => {
|
|
47
|
+
expect(() => assertThinkingSupported({})).not.toThrow()
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
describe('the declared capabilities stay honest', () => {
|
|
52
|
+
it('claims the tools and streaming it does implement', () => {
|
|
53
|
+
expect(OPENAI_CAPABILITIES.supportsTools).toBe(true)
|
|
54
|
+
expect(OPENAI_CAPABILITIES.supportsStreaming).toBe(true)
|
|
55
|
+
expect(OPENAI_CAPABILITIES.supportsFunctionCalling).toBe(true)
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
it('claims the vision and documents it now maps', () => {
|
|
59
|
+
expect(OPENAI_CAPABILITIES.supportsVision).toBe(true)
|
|
60
|
+
expect(OPENAI_CAPABILITIES.supportsDocuments).toBe(true)
|
|
61
|
+
})
|
|
62
|
+
|
|
63
|
+
it('answers every flag the negotiation reads', () => {
|
|
64
|
+
// An absent flag defaults to permissive, so a missing one is not a
|
|
65
|
+
// neutral omission — it is a claim.
|
|
66
|
+
for (const flag of [
|
|
67
|
+
'supportsTools',
|
|
68
|
+
'supportsStreaming',
|
|
69
|
+
'supportsFunctionCalling',
|
|
70
|
+
'supportsVision',
|
|
71
|
+
'supportsDocuments',
|
|
72
|
+
] as const) {
|
|
73
|
+
expect(typeof OPENAI_CAPABILITIES[flag]).toBe('boolean')
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
})
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import type { ChatCompletionParams } from '@namzu/sdk'
|
|
2
|
+
import { describe, expect, it } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import { toOpenAITools } from '../client.js'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* `enforceToolInputSchema` names the tools whose model-facing schema should
|
|
8
|
+
* be enforced by constrained generation rather than merely suggested.
|
|
9
|
+
*
|
|
10
|
+
* Both sibling drivers consumed it. This one dropped it on the floor, so a
|
|
11
|
+
* caller who had asked for a guaranteed-valid tool input silently got a
|
|
12
|
+
* best-effort one, and learned about it from a repair attempt rather than
|
|
13
|
+
* from an error. This is the wire the flag maps onto most directly — it
|
|
14
|
+
* takes the flag on the function itself.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const TOOLS = [
|
|
18
|
+
{
|
|
19
|
+
type: 'function' as const,
|
|
20
|
+
function: {
|
|
21
|
+
name: 'read',
|
|
22
|
+
description: 'read a file',
|
|
23
|
+
parameters: { type: 'object', properties: { path: { type: 'string' } } },
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
type: 'function' as const,
|
|
28
|
+
function: { name: 'write', description: 'write a file', parameters: { type: 'object' } },
|
|
29
|
+
},
|
|
30
|
+
]
|
|
31
|
+
|
|
32
|
+
type FunctionTool = {
|
|
33
|
+
function: { name: string; description?: string; parameters?: unknown; strict?: boolean }
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function toolsFor(enforceToolInputSchema?: readonly string[]): FunctionTool[] | undefined {
|
|
37
|
+
return toOpenAITools({
|
|
38
|
+
model: 'm',
|
|
39
|
+
messages: [],
|
|
40
|
+
tools: TOOLS,
|
|
41
|
+
...(enforceToolInputSchema ? { enforceToolInputSchema } : {}),
|
|
42
|
+
} as ChatCompletionParams) as unknown as FunctionTool[] | undefined
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
describe('the schema of a named tool is enforced, not suggested', () => {
|
|
46
|
+
it('marks the tool the caller named', () => {
|
|
47
|
+
const tools = toolsFor(['read'])
|
|
48
|
+
|
|
49
|
+
expect(tools?.find((t) => t.function.name === 'read')?.function.strict).toBe(true)
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
it('leaves every other tool alone', () => {
|
|
53
|
+
const tools = toolsFor(['read'])
|
|
54
|
+
|
|
55
|
+
// Enforcement is not free and not always possible; marking a tool
|
|
56
|
+
// nobody asked about changes behaviour the caller did not ask to
|
|
57
|
+
// change.
|
|
58
|
+
expect(tools?.find((t) => t.function.name === 'write')?.function).not.toHaveProperty('strict')
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('marks several when several were named', () => {
|
|
62
|
+
const tools = toolsFor(['read', 'write'])
|
|
63
|
+
|
|
64
|
+
expect(tools?.every((t) => t.function.strict === true)).toBe(true)
|
|
65
|
+
})
|
|
66
|
+
|
|
67
|
+
it('marks nothing when the hint is absent', () => {
|
|
68
|
+
expect(toolsFor()?.some((t) => 'strict' in t.function)).toBe(false)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
it('marks nothing when the hint names no tool', () => {
|
|
72
|
+
expect(toolsFor([])?.some((t) => 'strict' in t.function)).toBe(false)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('ignores a name that matches no registered tool', () => {
|
|
76
|
+
const tools = toolsFor(['nonexistent'])
|
|
77
|
+
|
|
78
|
+
expect(tools).toHaveLength(2)
|
|
79
|
+
expect(tools?.some((t) => 'strict' in t.function)).toBe(false)
|
|
80
|
+
})
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
describe('the rest of the tool schema is untouched', () => {
|
|
84
|
+
it('keeps the name, description and parameters', () => {
|
|
85
|
+
const read = toolsFor(['read'])?.find((t) => t.function.name === 'read')
|
|
86
|
+
|
|
87
|
+
expect(read?.function.description).toBe('read a file')
|
|
88
|
+
expect(read?.function.parameters).toMatchObject({
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: { path: { type: 'string' } },
|
|
91
|
+
})
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
it('substitutes an empty object for missing parameters', () => {
|
|
95
|
+
const tools = toOpenAITools({
|
|
96
|
+
model: 'm',
|
|
97
|
+
messages: [],
|
|
98
|
+
tools: [{ type: 'function', function: { name: 'ping' } }],
|
|
99
|
+
} as unknown as ChatCompletionParams) as unknown as FunctionTool[]
|
|
100
|
+
|
|
101
|
+
expect(tools?.[0]?.function.parameters).toEqual({})
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('sends no tools at all when there are none', () => {
|
|
105
|
+
expect(toOpenAITools({ model: 'm', messages: [] } as ChatCompletionParams)).toBeUndefined()
|
|
106
|
+
expect(
|
|
107
|
+
toOpenAITools({ model: 'm', messages: [], tools: [] } as ChatCompletionParams),
|
|
108
|
+
).toBeUndefined()
|
|
109
|
+
})
|
|
110
|
+
})
|