@genesislcap/foundation-ai 15.90.4 → 15.92.0-HT-305.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/README.md +26 -103
- package/dist/dts/ai-provider.d.ts +5 -3
- package/dist/dts/ai-provider.d.ts.map +1 -1
- package/dist/dts/index.d.ts +7 -4
- package/dist/dts/index.d.ts.map +1 -1
- package/dist/dts/providers/openai-provider.d.ts +8 -7
- package/dist/dts/providers/openai-provider.d.ts.map +1 -1
- package/dist/dts/transports/budget-exhausted-error.d.ts +5 -7
- package/dist/dts/transports/budget-exhausted-error.d.ts.map +1 -1
- package/dist/dts/transports/context-overflow-error.d.ts.map +1 -1
- package/dist/dts/transports/openai-transport.d.ts +119 -0
- package/dist/dts/transports/openai-transport.d.ts.map +1 -0
- package/dist/dts/types/chat.types.d.ts +12 -0
- package/dist/dts/types/chat.types.d.ts.map +1 -1
- package/dist/dts/types/config.types.d.ts +44 -16
- package/dist/dts/types/config.types.d.ts.map +1 -1
- package/dist/dts/utils/token-cost.d.ts +65 -1
- package/dist/dts/utils/token-cost.d.ts.map +1 -1
- package/dist/dts/utils/tool-schema.d.ts +38 -0
- package/dist/dts/utils/tool-schema.d.ts.map +1 -1
- package/dist/esm/ai-provider.js +21 -14
- package/dist/esm/index.js +4 -2
- package/dist/esm/providers/openai-provider.js +15 -9
- package/dist/esm/transports/budget-exhausted-error.js +6 -7
- package/dist/esm/transports/context-overflow-error.js +2 -0
- package/dist/esm/transports/openai-transport.js +752 -0
- package/dist/esm/types/config.types.js +12 -1
- package/dist/esm/utils/token-cost.js +89 -0
- package/dist/esm/utils/tool-schema.js +108 -0
- package/dist/foundation-ai.api.json +1745 -137
- package/dist/foundation-ai.d.ts +270 -28
- package/package.json +9 -9
- package/dist/dts/transports/server-openai-transport.d.ts +0 -23
- package/dist/dts/transports/server-openai-transport.d.ts.map +0 -1
- package/dist/esm/transports/server-openai-transport.js +0 -94
|
@@ -0,0 +1,752 @@
|
|
|
1
|
+
import { __awaiter, __rest } from "tslib";
|
|
2
|
+
import { SUPPORTED_OPENAI_MODEL_IDS, } from '../types';
|
|
3
|
+
import { normalizeImageMime, stripImageDataUrl } from '../utils/image-mime';
|
|
4
|
+
import { logger } from '../utils/logger';
|
|
5
|
+
import { scaleTemperature } from '../utils/temperature';
|
|
6
|
+
import { openaiTokenCost } from '../utils/token-cost';
|
|
7
|
+
import { enforceOpenAIToolSchema, toOpenAIStrictSchema } from '../utils/tool-schema';
|
|
8
|
+
import { ResponseTruncatedError } from './anthropic-transport';
|
|
9
|
+
import { VENDOR_LABELS } from './budget-exhausted-error';
|
|
10
|
+
import { postWithRetry } from './post-with-retry';
|
|
11
|
+
const OPENAI_DIRECT_URL = 'https://api.openai.com/v1/responses';
|
|
12
|
+
const DEFAULT_MODEL = 'gpt-6-luna';
|
|
13
|
+
const DEFAULT_TIMEOUT = 300000; // same wall clock as the Gemini/Anthropic transports
|
|
14
|
+
const DEFAULT_STALL_TIMEOUT = 30000; // inactivity window on the framed proxy path; pings arrive every 5s
|
|
15
|
+
/**
|
|
16
|
+
* Maximum input context window (tokens) per supported OpenAI model. Astra's window is 1.05M like the
|
|
17
|
+
* rest, but its documented maximum INPUT is lower because the output reservation is carved out of it.
|
|
18
|
+
* Source: https://platform.openai.com/docs/models
|
|
19
|
+
*/
|
|
20
|
+
const OPENAI_CONTEXT_LIMITS = {
|
|
21
|
+
'gpt-6-astra': 922000,
|
|
22
|
+
'gpt-6.1-sol': 1050000,
|
|
23
|
+
'gpt-6.1-terra': 1050000,
|
|
24
|
+
'gpt-6-sol': 1050000,
|
|
25
|
+
'gpt-6-luna': 1050000,
|
|
26
|
+
};
|
|
27
|
+
/** OpenAI's native default + ceiling temperature — https://platform.openai.com/docs/api-reference/responses */
|
|
28
|
+
const OPENAI_DEFAULT_TEMPERATURE = 1;
|
|
29
|
+
const OPENAI_MAX_TEMPERATURE = 2;
|
|
30
|
+
/**
|
|
31
|
+
* Tiers that accept `reasoning.effort: 'none'`. Everything else floors at `low` and returns a 400
|
|
32
|
+
* for `none`, so `thinkingPolicy: 'off'` on those tiers is clamped (with a one-time warning) rather
|
|
33
|
+
* than forwarded — the same "a clamp costs money, a guess costs a 400" rule the Gemini twin follows.
|
|
34
|
+
* Source: https://platform.openai.com/docs/guides/reasoning
|
|
35
|
+
*/
|
|
36
|
+
const OPENAI_EFFORT_NONE_TIERS = ['gpt-6.1-terra', 'gpt-6-luna'];
|
|
37
|
+
/**
|
|
38
|
+
* Graded policy → `reasoning.effort`. Exhaustive so a new level must be mapped here or the build
|
|
39
|
+
* fails. OpenAI has no `minimal`, so it clamps UP to `low` — the nearer neighbour, and the
|
|
40
|
+
* direction that shows up in the bill rather than as a quietly worse answer.
|
|
41
|
+
*/
|
|
42
|
+
const OPENAI_EFFORT = {
|
|
43
|
+
minimal: 'low',
|
|
44
|
+
low: 'low',
|
|
45
|
+
medium: 'medium',
|
|
46
|
+
high: 'high',
|
|
47
|
+
max: 'max',
|
|
48
|
+
};
|
|
49
|
+
/**
|
|
50
|
+
* The effort to send for a policy, or `undefined` to omit the field and take the model's default.
|
|
51
|
+
* `'auto'` omits it too: every model here already decides its own depth by default, so there is no
|
|
52
|
+
* opt-in to send.
|
|
53
|
+
*/
|
|
54
|
+
export function openaiEffortFor(model, policy) {
|
|
55
|
+
if (policy === undefined || policy === 'auto')
|
|
56
|
+
return undefined;
|
|
57
|
+
if (policy === 'off')
|
|
58
|
+
return OPENAI_EFFORT_NONE_TIERS.includes(model) ? 'none' : 'low';
|
|
59
|
+
return OPENAI_EFFORT[policy];
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Map a provider-agnostic {@link ChatToolChoice} to Responses' `tool_choice`. Returns undefined for
|
|
63
|
+
* the default (`'auto'`) so the request omits the field.
|
|
64
|
+
*/
|
|
65
|
+
function toOpenAIToolChoice(choice) {
|
|
66
|
+
if (!choice || choice === 'auto')
|
|
67
|
+
return undefined;
|
|
68
|
+
if (choice === 'required' || choice === 'none')
|
|
69
|
+
return choice;
|
|
70
|
+
return { type: 'function', name: choice.tool };
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A function tool in Responses' shape. `strict` is ALWAYS sent: omitted, Responses tries to
|
|
74
|
+
* normalise the schema to strict on its own, which silently changes what an optional field means.
|
|
75
|
+
* `enforceSchema` tools get the strict transform (every property required, optional ones nullable);
|
|
76
|
+
* the rest are sent as written with `strict: false`.
|
|
77
|
+
*/
|
|
78
|
+
function toOpenAITool(tool) {
|
|
79
|
+
const parameters = tool.enforceSchema
|
|
80
|
+
? enforceOpenAIToolSchema(tool.parameters, tool.name)
|
|
81
|
+
: tool.parameters;
|
|
82
|
+
return {
|
|
83
|
+
type: 'function',
|
|
84
|
+
name: tool.name,
|
|
85
|
+
description: tool.description,
|
|
86
|
+
parameters,
|
|
87
|
+
strict: !!tool.enforceSchema,
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
function assertSupportedOpenAIModel(model) {
|
|
91
|
+
if (!SUPPORTED_OPENAI_MODEL_IDS.includes(model)) {
|
|
92
|
+
throw new Error(`OpenAITransport: unsupported model "${model}". Use one of: ${SUPPORTED_OPENAI_MODEL_IDS.join(', ')}.`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
/** Per-model warnings emitted on construction, so the dear tiers are never picked by accident. */
|
|
96
|
+
const OPENAI_MODEL_WARNINGS = {
|
|
97
|
+
'gpt-6-astra': 'OpenAITransport: using gpt-6-astra — flagship model at $10/$50 per MTok; reserve for tasks where the cheaper tiers are insufficient.',
|
|
98
|
+
'gpt-6.1-sol': 'OpenAITransport: using gpt-6.1-sol — materially higher cost than terra/luna, and it cannot turn reasoning off (thinkingPolicy "off" runs at effort "low").',
|
|
99
|
+
'gpt-6-sol': 'OpenAITransport: using gpt-6-sol — superseded by gpt-6.1-sol at the same price, and it cannot turn reasoning off.',
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Key under which this transport stashes its round-trip state on `providerMetadata`: on the first
|
|
103
|
+
* tool call of a tool turn, on the message of a text-only one — never both. Private to
|
|
104
|
+
* OpenAITransport.
|
|
105
|
+
*/
|
|
106
|
+
const OPENAI_PROVIDER_KEY = 'openai';
|
|
107
|
+
const CACHE_BREAKPOINT = Object.freeze({ mode: 'explicit' });
|
|
108
|
+
/**
|
|
109
|
+
* How many history items carry a breakpoint. OpenAI looks a cached prefix up ONLY at the breakpoints
|
|
110
|
+
* in the request itself — the first 2 and the latest 50 — with no lookback from them, unlike
|
|
111
|
+
* Anthropic. 49 leaves the developer prompt's one inside that latest-50 window.
|
|
112
|
+
*/
|
|
113
|
+
const MAX_HISTORY_BREAKPOINTS = 49;
|
|
114
|
+
/** The items of a Responses `output`, sorted into what `fromResponse` builds a message from. */
|
|
115
|
+
function collectOutput(output) {
|
|
116
|
+
var _a, _b, _c, _d;
|
|
117
|
+
const reasoning = [];
|
|
118
|
+
const summaryParts = [];
|
|
119
|
+
const textParts = [];
|
|
120
|
+
const refusals = [];
|
|
121
|
+
const toolCalls = [];
|
|
122
|
+
let phase;
|
|
123
|
+
for (const item of output) {
|
|
124
|
+
if (item.type === 'reasoning') {
|
|
125
|
+
reasoning.push(item);
|
|
126
|
+
for (const s of ((_a = item.summary) !== null && _a !== void 0 ? _a : [])) {
|
|
127
|
+
if (s.text)
|
|
128
|
+
summaryParts.push(s.text);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else if (item.type === 'message') {
|
|
132
|
+
// The answer's phase wins over a preamble's: it is the one a replay must not mistake.
|
|
133
|
+
if (typeof item.phase === 'string' && phase !== 'final_answer')
|
|
134
|
+
phase = item.phase;
|
|
135
|
+
for (const part of ((_b = item.content) !== null && _b !== void 0 ? _b : [])) {
|
|
136
|
+
if (part.type === 'output_text' && typeof part.text === 'string')
|
|
137
|
+
textParts.push(part.text);
|
|
138
|
+
if (part.type === 'refusal' && typeof part.refusal === 'string')
|
|
139
|
+
refusals.push(part.refusal);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else if (item.type === 'function_call') {
|
|
143
|
+
toolCalls.push({
|
|
144
|
+
id: String((_d = (_c = item.call_id) !== null && _c !== void 0 ? _c : item.id) !== null && _d !== void 0 ? _d : crypto.randomUUID()),
|
|
145
|
+
name: String(item.name),
|
|
146
|
+
args: parseArguments(item.arguments),
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
return { reasoning, summaryParts, textParts, refusals, toolCalls, phase };
|
|
151
|
+
}
|
|
152
|
+
function imagePart(att) {
|
|
153
|
+
const part = {
|
|
154
|
+
type: 'input_image',
|
|
155
|
+
image_url: `data:${normalizeImageMime(att.mimeType)};base64,${stripImageDataUrl(att.data)}`,
|
|
156
|
+
};
|
|
157
|
+
if (att.detail)
|
|
158
|
+
part.detail = att.detail === 'medium' ? 'auto' : att.detail;
|
|
159
|
+
return part;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* User content parts: images first, then the text, then text attachments — the same order the
|
|
163
|
+
* Gemini/Anthropic twins use, so a turn with no images has the same shape on every vendor.
|
|
164
|
+
*/
|
|
165
|
+
function userContentParts(text, atts) {
|
|
166
|
+
const parts = [];
|
|
167
|
+
for (const att of atts) {
|
|
168
|
+
if (att.kind === 'image')
|
|
169
|
+
parts.push(imagePart(att));
|
|
170
|
+
}
|
|
171
|
+
if (text)
|
|
172
|
+
parts.push({ type: 'input_text', text });
|
|
173
|
+
for (const att of atts) {
|
|
174
|
+
if (att.kind !== 'image')
|
|
175
|
+
parts.push({ type: 'input_text', text: `[File: ${att.name}]\n${att.content}` });
|
|
176
|
+
}
|
|
177
|
+
return parts;
|
|
178
|
+
}
|
|
179
|
+
function turnStateOf(msg) {
|
|
180
|
+
var _a, _b, _c, _d, _e;
|
|
181
|
+
return ((_b = (_a = msg.providerMetadata) === null || _a === void 0 ? void 0 : _a[OPENAI_PROVIDER_KEY]) !== null && _b !== void 0 ? _b : (_e = (_d = (_c = msg.toolCalls) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.providerMetadata) === null || _e === void 0 ? void 0 : _e[OPENAI_PROVIDER_KEY]);
|
|
182
|
+
}
|
|
183
|
+
/** Drop server-assigned fields a stateless (`store: false`) replay must not reference. */
|
|
184
|
+
function replayableReasoning(item) {
|
|
185
|
+
const { status: _status } = item, rest = __rest(item, ["status"]);
|
|
186
|
+
return rest;
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Transport for OpenAI's Responses API. Calls it directly when `apiKey` is provided, otherwise
|
|
190
|
+
* posts to a server-proxy endpoint (ai-service `/api/openai/chat`) that adds the key, meters spend
|
|
191
|
+
* and frames the stream.
|
|
192
|
+
*
|
|
193
|
+
* Stateless by design: `store: false`, no `previous_response_id`. Everything a later turn needs
|
|
194
|
+
* (encrypted reasoning, `phase`) rides on the persisted `ChatMessage`s, so sessions survive restore
|
|
195
|
+
* and vendor switches the same way they do on the other transports.
|
|
196
|
+
*
|
|
197
|
+
* Implements `AITransport` (structured prompt) and `ChatTransport` (multi-turn chat).
|
|
198
|
+
*
|
|
199
|
+
* @beta
|
|
200
|
+
*/
|
|
201
|
+
export class OpenAITransport {
|
|
202
|
+
constructor(config = {}) {
|
|
203
|
+
var _a, _b, _c, _d;
|
|
204
|
+
this.lifetimeCostUsd = 0;
|
|
205
|
+
/** Net of write premiums, so it can go negative on a cold cache — same contract as Anthropic. */
|
|
206
|
+
this.lifetimeSavingsUsd = 0;
|
|
207
|
+
/**
|
|
208
|
+
* The clamped policies already warned about, so each warns once rather than on every turn of a
|
|
209
|
+
* tool loop. Per policy: a `minimal` warning must not silence the costlier `off` one.
|
|
210
|
+
*/
|
|
211
|
+
this.warnedThinkingClamped = new Set();
|
|
212
|
+
const model = (_a = config.model) !== null && _a !== void 0 ? _a : DEFAULT_MODEL;
|
|
213
|
+
assertSupportedOpenAIModel(model);
|
|
214
|
+
this.model = model;
|
|
215
|
+
const warning = OPENAI_MODEL_WARNINGS[model];
|
|
216
|
+
if (warning) {
|
|
217
|
+
logger.warn(warning);
|
|
218
|
+
}
|
|
219
|
+
this.timeout = (_b = config.timeout) !== null && _b !== void 0 ? _b : DEFAULT_TIMEOUT;
|
|
220
|
+
this.stallTimeout = (_c = config.stallTimeout) !== null && _c !== void 0 ? _c : DEFAULT_STALL_TIMEOUT;
|
|
221
|
+
this.apiKey = config.apiKey;
|
|
222
|
+
this.serverEndpoint = config.serverEndpoint;
|
|
223
|
+
this.maxTokens = config.maxTokens;
|
|
224
|
+
this.reasoningSummary = (_d = config.reasoningSummary) !== null && _d !== void 0 ? _d : 'none';
|
|
225
|
+
}
|
|
226
|
+
getConfig() {
|
|
227
|
+
return {
|
|
228
|
+
provider: 'openai',
|
|
229
|
+
model: this.model,
|
|
230
|
+
contextLimit: OPENAI_CONTEXT_LIMITS[this.model],
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
getLifetimeCost() {
|
|
234
|
+
return this.lifetimeCostUsd;
|
|
235
|
+
}
|
|
236
|
+
getLifetimeSavings() {
|
|
237
|
+
return this.lifetimeSavingsUsd;
|
|
238
|
+
}
|
|
239
|
+
resetLifetimeCost() {
|
|
240
|
+
this.lifetimeCostUsd = 0;
|
|
241
|
+
this.lifetimeSavingsUsd = 0;
|
|
242
|
+
}
|
|
243
|
+
// ── AITransport (structured prompt) ────────────────────────────────────
|
|
244
|
+
sendStructuredPrompt(options) {
|
|
245
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
246
|
+
const { systemPrompt, userPrompt, responseSchema } = options;
|
|
247
|
+
const message = yield this.sendChatMessage([], userPrompt, {
|
|
248
|
+
systemPrompt,
|
|
249
|
+
responseSchema,
|
|
250
|
+
});
|
|
251
|
+
return message.content;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
// ── ChatTransport (multi-turn chat) ────────────────────────────────────
|
|
255
|
+
sendChatMessage(history, userMessage, options) {
|
|
256
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
257
|
+
const body = this.buildRequestBody(history, userMessage, options);
|
|
258
|
+
const response = yield this.post(body, options === null || options === void 0 ? void 0 : options.signal);
|
|
259
|
+
const message = this.fromResponse(response, body.max_output_tokens);
|
|
260
|
+
this.restoreOptionalFields(message, options);
|
|
261
|
+
return message;
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
/** Undo strict mode's absent-becomes-null, in tool arguments and in a `responseSchema` answer. */
|
|
265
|
+
restoreOptionalFields(message, options) {
|
|
266
|
+
var _a;
|
|
267
|
+
const pointersByTool = strictNullPointersByTool(options === null || options === void 0 ? void 0 : options.tools);
|
|
268
|
+
for (const call of (_a = message.toolCalls) !== null && _a !== void 0 ? _a : []) {
|
|
269
|
+
const pointers = pointersByTool.get(call.name);
|
|
270
|
+
if (pointers)
|
|
271
|
+
stripStrictNulls(call.args, pointers);
|
|
272
|
+
}
|
|
273
|
+
if (!(options === null || options === void 0 ? void 0 : options.responseSchema) || !message.content)
|
|
274
|
+
return;
|
|
275
|
+
const { madeNullable } = toOpenAIStrictSchema(options.responseSchema);
|
|
276
|
+
if (!madeNullable.length)
|
|
277
|
+
return;
|
|
278
|
+
try {
|
|
279
|
+
const parsed = JSON.parse(message.content);
|
|
280
|
+
stripStrictNulls(parsed, madeNullable);
|
|
281
|
+
message.content = JSON.stringify(parsed);
|
|
282
|
+
}
|
|
283
|
+
catch (_b) {
|
|
284
|
+
// Not JSON: leave it for the caller's own parse to report.
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* The Responses request body for a turn. Public for tests and for hosts that want to inspect the
|
|
289
|
+
* exact wire shape; not part of the stable contract.
|
|
290
|
+
*
|
|
291
|
+
* @internal
|
|
292
|
+
*/
|
|
293
|
+
buildRequestBody(history, userMessage, options) {
|
|
294
|
+
var _a, _b;
|
|
295
|
+
const scope = (options === null || options === void 0 ? void 0 : options.cachePolicy) && options.cachePolicy.scope !== 'default'
|
|
296
|
+
? options.cachePolicy.scope
|
|
297
|
+
: undefined;
|
|
298
|
+
const input = [];
|
|
299
|
+
// The system prompt goes in as a developer MESSAGE, not top-level `instructions`: a cache
|
|
300
|
+
// breakpoint is not allowed in `instructions`, and the tools/prompt scopes need one here.
|
|
301
|
+
if (options === null || options === void 0 ? void 0 : options.systemPrompt) {
|
|
302
|
+
const part = { type: 'input_text', text: options.systemPrompt };
|
|
303
|
+
if (scope)
|
|
304
|
+
part.prompt_cache_breakpoint = CACHE_BREAKPOINT;
|
|
305
|
+
input.push({ type: 'message', role: 'developer', content: [part] });
|
|
306
|
+
}
|
|
307
|
+
input.push(...this.toInputItems(history, userMessage, options === null || options === void 0 ? void 0 : options.attachments));
|
|
308
|
+
if (scope === 'history')
|
|
309
|
+
this.markHistoryBreakpoints(input);
|
|
310
|
+
// Never-stored tail context goes AFTER the breakpoint so it never busts the cached prefix.
|
|
311
|
+
if (options === null || options === void 0 ? void 0 : options.tailContext) {
|
|
312
|
+
input.push({
|
|
313
|
+
type: 'message',
|
|
314
|
+
role: 'user',
|
|
315
|
+
content: [{ type: 'input_text', text: options.tailContext }],
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
const body = {
|
|
319
|
+
model: this.model,
|
|
320
|
+
input,
|
|
321
|
+
store: false,
|
|
322
|
+
// Encrypted reasoning is what makes a stateless tool loop keep its chain of thought.
|
|
323
|
+
include: ['reasoning.encrypted_content'],
|
|
324
|
+
};
|
|
325
|
+
if (scope) {
|
|
326
|
+
// 30m is the only TTL every model here takes, and 12h costs 2.5x to write — so the Anthropic
|
|
327
|
+
// `1h` maps to 30m too rather than to the dearer tier.
|
|
328
|
+
body.prompt_cache_options = { mode: 'explicit', ttl: '30m' };
|
|
329
|
+
}
|
|
330
|
+
if ((_a = options === null || options === void 0 ? void 0 : options.tools) === null || _a === void 0 ? void 0 : _a.length) {
|
|
331
|
+
body.tools = options.tools.map(toOpenAITool);
|
|
332
|
+
const toolChoice = toOpenAIToolChoice(options.toolChoice);
|
|
333
|
+
if (toolChoice)
|
|
334
|
+
body.tool_choice = toolChoice;
|
|
335
|
+
}
|
|
336
|
+
this.warnIfThinkingClamped(options === null || options === void 0 ? void 0 : options.thinkingPolicy);
|
|
337
|
+
const effort = openaiEffortFor(this.model, options === null || options === void 0 ? void 0 : options.thinkingPolicy);
|
|
338
|
+
if (effort) {
|
|
339
|
+
body.reasoning =
|
|
340
|
+
effort === 'none' || this.reasoningSummary === 'none'
|
|
341
|
+
? { effort }
|
|
342
|
+
: { effort, summary: this.reasoningSummary };
|
|
343
|
+
}
|
|
344
|
+
else if (this.reasoningSummary !== 'none') {
|
|
345
|
+
body.reasoning = { summary: this.reasoningSummary };
|
|
346
|
+
}
|
|
347
|
+
// Sampling knobs are rejected while the model reasons, so temperature only rides along when
|
|
348
|
+
// reasoning is explicitly off.
|
|
349
|
+
if ((options === null || options === void 0 ? void 0 : options.temperature) != null && effort === 'none') {
|
|
350
|
+
body.temperature = scaleTemperature(options.temperature, {
|
|
351
|
+
defaultTemp: OPENAI_DEFAULT_TEMPERATURE,
|
|
352
|
+
maxTemp: OPENAI_MAX_TEMPERATURE,
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
const maxTokens = (_b = options === null || options === void 0 ? void 0 : options.maxTokens) !== null && _b !== void 0 ? _b : this.maxTokens;
|
|
356
|
+
if (maxTokens != null)
|
|
357
|
+
body.max_output_tokens = maxTokens;
|
|
358
|
+
if (options === null || options === void 0 ? void 0 : options.responseSchema) {
|
|
359
|
+
body.text = {
|
|
360
|
+
format: {
|
|
361
|
+
type: 'json_schema',
|
|
362
|
+
name: 'response',
|
|
363
|
+
schema: toOpenAIStrictSchema(options.responseSchema).schema,
|
|
364
|
+
strict: true,
|
|
365
|
+
},
|
|
366
|
+
};
|
|
367
|
+
}
|
|
368
|
+
return body;
|
|
369
|
+
}
|
|
370
|
+
// ── Private helpers ────────────────────────────────────────────────────
|
|
371
|
+
warnIfThinkingClamped(policy) {
|
|
372
|
+
if (!policy || this.warnedThinkingClamped.has(policy))
|
|
373
|
+
return;
|
|
374
|
+
if (policy === 'off' && !OPENAI_EFFORT_NONE_TIERS.includes(this.model)) {
|
|
375
|
+
this.warnedThinkingClamped.add(policy);
|
|
376
|
+
logger.warn(`OpenAITransport: thinkingPolicy 'off' sent as effort 'low' — ${this.model} does not accept effort 'none'. Reasoning tokens are billed as output; use gpt-6.1-terra or gpt-6-luna if you need them gone.`);
|
|
377
|
+
}
|
|
378
|
+
else if (policy === 'minimal') {
|
|
379
|
+
this.warnedThinkingClamped.add(policy);
|
|
380
|
+
logger.warn(`OpenAITransport: thinkingPolicy 'minimal' sent as effort 'low' — OpenAI has no 'minimal' effort.`);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
toInputItems(history, userMessage, attachments) {
|
|
384
|
+
var _a, _b, _c, _d, _e;
|
|
385
|
+
const items = [];
|
|
386
|
+
// Reasoning items are replayed once per turn even when the driver split one model response
|
|
387
|
+
// into several stored messages that each carry a copy of the state.
|
|
388
|
+
const replayedReasoning = new Set();
|
|
389
|
+
for (const msg of history) {
|
|
390
|
+
if (msg.role === 'system' ||
|
|
391
|
+
msg.role === 'system-event' ||
|
|
392
|
+
msg.role === 'synthetic-user' ||
|
|
393
|
+
msg.role === 'compacted-summary' ||
|
|
394
|
+
msg.category === 'reasoning' ||
|
|
395
|
+
msg.category === 'narration' ||
|
|
396
|
+
// Driver-authored prose no model produced — see ChatMessage.excludeFromProvider.
|
|
397
|
+
msg.excludeFromProvider ||
|
|
398
|
+
msg.thinking // legacy pre-split sessions
|
|
399
|
+
)
|
|
400
|
+
continue;
|
|
401
|
+
if (msg.toolResult) {
|
|
402
|
+
items.push(this.toFunctionCallOutput(msg));
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
405
|
+
if (msg.role === 'user') {
|
|
406
|
+
const content = userContentParts(msg.content, (_a = msg.attachments) !== null && _a !== void 0 ? _a : []);
|
|
407
|
+
if (content.length)
|
|
408
|
+
items.push({ type: 'message', role: 'user', content });
|
|
409
|
+
continue;
|
|
410
|
+
}
|
|
411
|
+
// Assistant turn: reasoning first (only to the model that produced it — another model
|
|
412
|
+
// cannot decrypt it), then any text, then the function calls.
|
|
413
|
+
const state = turnStateOf(msg);
|
|
414
|
+
if ((state === null || state === void 0 ? void 0 : state.model) === this.model) {
|
|
415
|
+
for (const r of (_b = state.reasoning) !== null && _b !== void 0 ? _b : []) {
|
|
416
|
+
const key = (_c = r.id) !== null && _c !== void 0 ? _c : r.encrypted_content;
|
|
417
|
+
if (replayedReasoning.has(key))
|
|
418
|
+
continue;
|
|
419
|
+
replayedReasoning.add(key);
|
|
420
|
+
items.push(replayableReasoning(r));
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
if (msg.content) {
|
|
424
|
+
const item = {
|
|
425
|
+
type: 'message',
|
|
426
|
+
role: 'assistant',
|
|
427
|
+
content: [{ type: 'output_text', text: msg.content }],
|
|
428
|
+
};
|
|
429
|
+
if (state === null || state === void 0 ? void 0 : state.phase)
|
|
430
|
+
item.phase = state.phase;
|
|
431
|
+
items.push(item);
|
|
432
|
+
}
|
|
433
|
+
for (const tc of (_d = msg.toolCalls) !== null && _d !== void 0 ? _d : []) {
|
|
434
|
+
items.push({
|
|
435
|
+
type: 'function_call',
|
|
436
|
+
call_id: tc.id,
|
|
437
|
+
name: tc.name,
|
|
438
|
+
arguments: JSON.stringify((_e = tc.args) !== null && _e !== void 0 ? _e : {}),
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const content = userContentParts(userMessage, attachments !== null && attachments !== void 0 ? attachments : []);
|
|
443
|
+
if (content.length)
|
|
444
|
+
items.push({ type: 'message', role: 'user', content });
|
|
445
|
+
return items;
|
|
446
|
+
}
|
|
447
|
+
/**
|
|
448
|
+
* A tool result, always as content parts — never the plain-string form. A cache hit needs the
|
|
449
|
+
* whole rendered prefix to match, and a breakpoint can only sit on a part, so a string output
|
|
450
|
+
* that became an array when it carried one would render differently on the next turn.
|
|
451
|
+
*/
|
|
452
|
+
toFunctionCallOutput(msg) {
|
|
453
|
+
var _a;
|
|
454
|
+
const result = msg.toolResult;
|
|
455
|
+
const images = (_a = result.attachments) !== null && _a !== void 0 ? _a : [];
|
|
456
|
+
return {
|
|
457
|
+
type: 'function_call_output',
|
|
458
|
+
call_id: result.toolCallId,
|
|
459
|
+
output: [{ type: 'input_text', text: result.content }, ...images.map(imagePart)],
|
|
460
|
+
};
|
|
461
|
+
}
|
|
462
|
+
/**
|
|
463
|
+
* Put a history-scope breakpoint on every stored item that can carry one — a user message's last
|
|
464
|
+
* text part or a tool result's — up to the latest `MAX_HISTORY_BREAKPOINTS`.
|
|
465
|
+
*
|
|
466
|
+
* One breakpoint on the last item (Anthropic's shape) never hits on OpenAI: lookup checks only the
|
|
467
|
+
* breakpoints in this request, and the last item moves every turn, so the previous turn's boundary
|
|
468
|
+
* was unmarked and every turn rewrote the whole history to cache. Marking each boundary keeps the
|
|
469
|
+
* previous turn's in the request. Assistant items cannot carry one.
|
|
470
|
+
*/
|
|
471
|
+
markHistoryBreakpoints(input) {
|
|
472
|
+
let marked = 0;
|
|
473
|
+
for (let i = input.length - 1; i >= 0 && marked < MAX_HISTORY_BREAKPOINTS; i -= 1) {
|
|
474
|
+
const item = input[i];
|
|
475
|
+
if (item.role === 'developer')
|
|
476
|
+
continue;
|
|
477
|
+
const key = item.type === 'function_call_output'
|
|
478
|
+
? 'output'
|
|
479
|
+
: item.type === 'message' && item.role !== 'assistant'
|
|
480
|
+
? 'content'
|
|
481
|
+
: undefined;
|
|
482
|
+
if (!key)
|
|
483
|
+
continue;
|
|
484
|
+
const parts = [...item[key]];
|
|
485
|
+
let textIndex = -1;
|
|
486
|
+
for (let j = parts.length - 1; j >= 0; j -= 1) {
|
|
487
|
+
if (parts[j].type === 'input_text') {
|
|
488
|
+
textIndex = j;
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
if (textIndex < 0)
|
|
493
|
+
continue;
|
|
494
|
+
parts[textIndex] = Object.assign(Object.assign({}, parts[textIndex]), { prompt_cache_breakpoint: CACHE_BREAKPOINT });
|
|
495
|
+
input[i] = Object.assign(Object.assign({}, item), { [key]: parts });
|
|
496
|
+
marked += 1;
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
usageFromResponse(usage) {
|
|
500
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r;
|
|
501
|
+
if (!usage)
|
|
502
|
+
return {};
|
|
503
|
+
const u = usage;
|
|
504
|
+
const cacheReadTokens = (_b = (_a = u.input_tokens_details) === null || _a === void 0 ? void 0 : _a.cached_tokens) !== null && _b !== void 0 ? _b : 0;
|
|
505
|
+
const cacheWriteTokens = (_d = (_c = u.input_tokens_details) === null || _c === void 0 ? void 0 : _c.cache_write_tokens) !== null && _d !== void 0 ? _d : 0;
|
|
506
|
+
const { costUsd, savedUsd } = openaiTokenCost(this.model, {
|
|
507
|
+
inputTokens: (_e = u.input_tokens) !== null && _e !== void 0 ? _e : 0,
|
|
508
|
+
outputTokens: (_f = u.output_tokens) !== null && _f !== void 0 ? _f : 0,
|
|
509
|
+
cacheReadTokens,
|
|
510
|
+
cacheWriteTokens,
|
|
511
|
+
cacheWrite12hTokens: (_j = (_h = (_g = u.input_tokens_details) === null || _g === void 0 ? void 0 : _g.cache_write_tokens_details) === null || _h === void 0 ? void 0 : _h.ttl_12h_tokens) !== null && _j !== void 0 ? _j : 0,
|
|
512
|
+
});
|
|
513
|
+
this.lifetimeCostUsd += costUsd;
|
|
514
|
+
this.lifetimeSavingsUsd += savedUsd;
|
|
515
|
+
const dp = OpenAITransport.COST_DECIMAL_PLACES;
|
|
516
|
+
console.log(`--- OpenAI Token Usage (${this.model}) ---`);
|
|
517
|
+
console.log(`Input Tokens: ${(_k = u.input_tokens) !== null && _k !== void 0 ? _k : 0} (${cacheReadTokens} cached, ${cacheWriteTokens} written)`);
|
|
518
|
+
console.log(`Output Tokens: ${(_l = u.output_tokens) !== null && _l !== void 0 ? _l : 0} (incl. ${(_o = (_m = u.output_tokens_details) === null || _m === void 0 ? void 0 : _m.reasoning_tokens) !== null && _o !== void 0 ? _o : 0} reasoning)`);
|
|
519
|
+
console.log(`Total Cost: $${costUsd.toFixed(dp)}`);
|
|
520
|
+
console.log(`Cache Saved: $${savedUsd.toFixed(dp)} (lifetime $${this.lifetimeSavingsUsd.toFixed(dp)})`);
|
|
521
|
+
console.log(`Lifetime Cost: $${this.lifetimeCostUsd.toFixed(dp)}`);
|
|
522
|
+
console.log('--------------------------');
|
|
523
|
+
return {
|
|
524
|
+
// Responses' input_tokens already INCLUDES cached and written tokens, matching what
|
|
525
|
+
// ChatMessage.inputTokens means on Gemini (the whole prompt).
|
|
526
|
+
inputTokens: u.input_tokens,
|
|
527
|
+
// Reasoning tokens are already inside output_tokens — no folding needed (unlike Gemini).
|
|
528
|
+
outputTokens: u.output_tokens,
|
|
529
|
+
thoughtsTokens: (_p = u.output_tokens_details) === null || _p === void 0 ? void 0 : _p.reasoning_tokens,
|
|
530
|
+
cacheReadTokens: (_q = u.input_tokens_details) === null || _q === void 0 ? void 0 : _q.cached_tokens,
|
|
531
|
+
cacheWriteTokens: (_r = u.input_tokens_details) === null || _r === void 0 ? void 0 : _r.cache_write_tokens,
|
|
532
|
+
cost: costUsd,
|
|
533
|
+
};
|
|
534
|
+
}
|
|
535
|
+
fromResponse(response, effectiveMaxTokens) {
|
|
536
|
+
var _a, _b;
|
|
537
|
+
const usage = this.usageFromResponse(response.usage);
|
|
538
|
+
const output = ((_a = response.output) !== null && _a !== void 0 ? _a : []);
|
|
539
|
+
const { reasoning, summaryParts, textParts, refusals, toolCalls, phase } = collectOutput(output);
|
|
540
|
+
const state = { model: this.model };
|
|
541
|
+
if (reasoning.length)
|
|
542
|
+
state.reasoning = reasoning;
|
|
543
|
+
if (phase)
|
|
544
|
+
state.phase = phase;
|
|
545
|
+
// ONE copy of the state, which carries the encrypted reasoning: on the first tool call when there
|
|
546
|
+
// is one, otherwise on the message. On both, every split the driver makes of a tool turn
|
|
547
|
+
// (reasoning, narration, the call itself) would persist the blobs again.
|
|
548
|
+
const providerMetadata = { [OPENAI_PROVIDER_KEY]: state };
|
|
549
|
+
if (toolCalls.length)
|
|
550
|
+
toolCalls[0].providerMetadata = providerMetadata;
|
|
551
|
+
const narration = textParts.join('');
|
|
552
|
+
const summary = summaryParts.join('\n\n');
|
|
553
|
+
const base = Object.assign({ role: 'assistant', content: narration, reasoning: summary || undefined }, (toolCalls.length ? { toolCalls } : { providerMetadata }));
|
|
554
|
+
const status = response.status;
|
|
555
|
+
if (status === 'failed')
|
|
556
|
+
throw failedResponseError(this.model, response.error);
|
|
557
|
+
const incompleteReason = (_b = response.incomplete_details) === null || _b === void 0 ? void 0 : _b.reason;
|
|
558
|
+
const finishReason = refusals.length && !narration && !toolCalls.length
|
|
559
|
+
? 'refusal'
|
|
560
|
+
: status === 'incomplete'
|
|
561
|
+
? (incompleteReason !== null && incompleteReason !== void 0 ? incompleteReason : 'incomplete')
|
|
562
|
+
: status;
|
|
563
|
+
// A refusal keeps content EMPTY, as on Anthropic and Gemini: the driver recognises a refusal as an
|
|
564
|
+
// empty response with finishReason 'refusal', and ends the turn without retrying. Refusal text in
|
|
565
|
+
// `content` would read as a normal answer and be persisted as one.
|
|
566
|
+
if (finishReason === 'refusal') {
|
|
567
|
+
logger.warn('OpenAITransport: the model refused', {
|
|
568
|
+
model: this.model,
|
|
569
|
+
refusal: refusals.join('\n'),
|
|
570
|
+
});
|
|
571
|
+
}
|
|
572
|
+
// Cut off by the content filter: the partial text is not an answer and a partial tool call's
|
|
573
|
+
// arguments cannot be trusted, so neither is kept. The driver sees a blank response.
|
|
574
|
+
if (status === 'incomplete' && incompleteReason === 'content_filter') {
|
|
575
|
+
base.content = '';
|
|
576
|
+
delete base.toolCalls;
|
|
577
|
+
logger.warn('OpenAITransport: response stopped by the content filter; partial output dropped', {
|
|
578
|
+
model: this.model,
|
|
579
|
+
outputTypes: output.map((o) => o.type),
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
if (status === 'incomplete' && incompleteReason === 'max_output_tokens') {
|
|
583
|
+
// Same contract as the other transports: partial ANSWER text is kept and flagged; a cut-off
|
|
584
|
+
// tool call (untrustworthy args) or nothing at all raises the typed, non-retryable error.
|
|
585
|
+
if (toolCalls.length || !narration.trim()) {
|
|
586
|
+
throw new ResponseTruncatedError(this.model, effectiveMaxTokens, usage.outputTokens, toolCalls.map((tc) => tc.name));
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
if (!toolCalls.length &&
|
|
590
|
+
!narration &&
|
|
591
|
+
!summary &&
|
|
592
|
+
finishReason !== 'refusal' &&
|
|
593
|
+
finishReason !== 'content_filter') {
|
|
594
|
+
logger.warn('OpenAITransport: blank response', {
|
|
595
|
+
model: this.model,
|
|
596
|
+
status,
|
|
597
|
+
incompleteReason,
|
|
598
|
+
outputTypes: output.map((o) => o.type),
|
|
599
|
+
usage: response.usage,
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
if (usage.inputTokens != null)
|
|
603
|
+
base.inputTokens = usage.inputTokens;
|
|
604
|
+
if (usage.outputTokens != null)
|
|
605
|
+
base.outputTokens = usage.outputTokens;
|
|
606
|
+
if (usage.cacheReadTokens != null)
|
|
607
|
+
base.cacheReadTokens = usage.cacheReadTokens;
|
|
608
|
+
if (usage.cacheWriteTokens != null)
|
|
609
|
+
base.cacheWriteTokens = usage.cacheWriteTokens;
|
|
610
|
+
if (usage.cost != null)
|
|
611
|
+
base.cost = usage.cost;
|
|
612
|
+
base.model = this.model;
|
|
613
|
+
if (finishReason != null || usage.thoughtsTokens != null) {
|
|
614
|
+
base.responseMeta = {
|
|
615
|
+
finishReason,
|
|
616
|
+
thoughtsTokens: usage.thoughtsTokens,
|
|
617
|
+
parts: {
|
|
618
|
+
functionCall: toolCalls.length,
|
|
619
|
+
thought: summaryParts.length,
|
|
620
|
+
text: textParts.length,
|
|
621
|
+
},
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
return base;
|
|
625
|
+
}
|
|
626
|
+
buildEndpoint(body) {
|
|
627
|
+
if (this.apiKey) {
|
|
628
|
+
return {
|
|
629
|
+
url: OPENAI_DIRECT_URL,
|
|
630
|
+
payload: body,
|
|
631
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.apiKey}` },
|
|
632
|
+
credentials: 'omit',
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
if (this.serverEndpoint) {
|
|
636
|
+
return {
|
|
637
|
+
url: this.serverEndpoint,
|
|
638
|
+
payload: body,
|
|
639
|
+
// Opt in to the proxy's NDJSON heartbeat framing.
|
|
640
|
+
headers: { 'Content-Type': 'application/json', Accept: 'application/x-ndjson' },
|
|
641
|
+
credentials: 'include',
|
|
642
|
+
};
|
|
643
|
+
}
|
|
644
|
+
throw new Error('OpenAITransport: no API key or server endpoint configured. ' +
|
|
645
|
+
'Pass apiKey or serverEndpoint in OpenAIAIConfig.');
|
|
646
|
+
}
|
|
647
|
+
post(body, signal) {
|
|
648
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
649
|
+
const { url, payload, headers, credentials } = this.buildEndpoint(body);
|
|
650
|
+
return (yield postWithRetry({
|
|
651
|
+
url,
|
|
652
|
+
headers,
|
|
653
|
+
body: payload,
|
|
654
|
+
credentials,
|
|
655
|
+
vendorLabel: VENDOR_LABELS.openai,
|
|
656
|
+
retryableStatuses: OpenAITransport.RETRYABLE_STATUSES,
|
|
657
|
+
timeout: this.timeout,
|
|
658
|
+
stallTimeout: this.stallTimeout,
|
|
659
|
+
backoffBaseMs: OpenAITransport.BACKOFF_BASE_MS,
|
|
660
|
+
signal,
|
|
661
|
+
}));
|
|
662
|
+
});
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
OpenAITransport.COST_DECIMAL_PLACES = 6;
|
|
666
|
+
/**
|
|
667
|
+
* 429 is retried only once the proxy has ruled out an account wall — `insufficient_quota` and the
|
|
668
|
+
* spend-limit codes arrive as `PROVIDER_REFUSED`, which `postWithRetry` never retries. 500/502/503
|
|
669
|
+
* are OpenAI's documented transient faults (`server_error`, `server_is_overloaded`).
|
|
670
|
+
*/
|
|
671
|
+
OpenAITransport.RATE_LIMIT_STATUS = 429;
|
|
672
|
+
OpenAITransport.SERVER_ERROR_STATUS = 500;
|
|
673
|
+
OpenAITransport.BAD_GATEWAY_STATUS = 502;
|
|
674
|
+
OpenAITransport.SERVICE_UNAVAILABLE_STATUS = 503;
|
|
675
|
+
OpenAITransport.RETRYABLE_STATUSES = [
|
|
676
|
+
OpenAITransport.RATE_LIMIT_STATUS,
|
|
677
|
+
OpenAITransport.SERVER_ERROR_STATUS,
|
|
678
|
+
OpenAITransport.BAD_GATEWAY_STATUS,
|
|
679
|
+
OpenAITransport.SERVICE_UNAVAILABLE_STATUS,
|
|
680
|
+
];
|
|
681
|
+
OpenAITransport.BACKOFF_BASE_MS = 1000;
|
|
682
|
+
/**
|
|
683
|
+
* A `status: 'failed'` response arrives as HTTP 200, so `postWithRetry` never sees it. Raised rather
|
|
684
|
+
* than returned, or the turn becomes a blank message that the driver retries as an empty response.
|
|
685
|
+
*/
|
|
686
|
+
function failedResponseError(model, error) {
|
|
687
|
+
const { code, message } = (error !== null && error !== void 0 ? error : {});
|
|
688
|
+
return new Error(`OpenAITransport: ${model} response failed${code ? ` (${code})` : ''}: ${message !== null && message !== void 0 ? message : 'no error detail'}`);
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Strict mode has no optional properties, so the model sends `null` for every one it leaves out
|
|
692
|
+
* (`OpenAIStrictSchema.madeNullable`). Delete those nulls so a handler sees the arguments its
|
|
693
|
+
* own schema describes: an absent key, not `null`. `pointers` are the transform's JSON Pointers;
|
|
694
|
+
* `/items` applies to every element of an array. A pointer through anything else (`anyOf`, `$defs`)
|
|
695
|
+
* is not followed, and its null is left in place.
|
|
696
|
+
*/
|
|
697
|
+
function stripStrictNulls(value, pointers) {
|
|
698
|
+
for (const pointer of pointers) {
|
|
699
|
+
deleteNullAt(value, pointer.split('/').slice(1));
|
|
700
|
+
}
|
|
701
|
+
}
|
|
702
|
+
function deleteNullAt(value, segments) {
|
|
703
|
+
if (!value || typeof value !== 'object')
|
|
704
|
+
return;
|
|
705
|
+
const [head, ...rest] = segments;
|
|
706
|
+
if (head === 'items' && Array.isArray(value)) {
|
|
707
|
+
for (const element of value)
|
|
708
|
+
deleteNullAt(element, rest);
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
if (head !== 'properties' || !rest.length || Array.isArray(value))
|
|
712
|
+
return;
|
|
713
|
+
const [name, ...below] = rest;
|
|
714
|
+
const record = value;
|
|
715
|
+
if (!below.length) {
|
|
716
|
+
if (record[name] === null)
|
|
717
|
+
delete record[name];
|
|
718
|
+
return;
|
|
719
|
+
}
|
|
720
|
+
deleteNullAt(record[name], below);
|
|
721
|
+
}
|
|
722
|
+
/** The strict-mode null pointers of each `enforceSchema` tool, by tool name. */
|
|
723
|
+
function strictNullPointersByTool(tools) {
|
|
724
|
+
const byTool = new Map();
|
|
725
|
+
for (const tool of tools !== null && tools !== void 0 ? tools : []) {
|
|
726
|
+
if (!tool.enforceSchema)
|
|
727
|
+
continue;
|
|
728
|
+
const { madeNullable } = toOpenAIStrictSchema(tool.parameters);
|
|
729
|
+
if (madeNullable.length)
|
|
730
|
+
byTool.set(tool.name, madeNullable);
|
|
731
|
+
}
|
|
732
|
+
return byTool;
|
|
733
|
+
}
|
|
734
|
+
/**
|
|
735
|
+
* Function-call arguments arrive as a JSON string. A string that does not parse (a model emitting
|
|
736
|
+
* prose into a non-strict tool) becomes `{}` with a warning, rather than failing the whole turn —
|
|
737
|
+
* the tool's own validation then reports the missing arguments to the model.
|
|
738
|
+
*/
|
|
739
|
+
function parseArguments(raw) {
|
|
740
|
+
if (raw && typeof raw === 'object')
|
|
741
|
+
return raw;
|
|
742
|
+
if (typeof raw !== 'string' || !raw.trim())
|
|
743
|
+
return {};
|
|
744
|
+
try {
|
|
745
|
+
const parsed = JSON.parse(raw);
|
|
746
|
+
return parsed && typeof parsed === 'object' ? parsed : {};
|
|
747
|
+
}
|
|
748
|
+
catch (_a) {
|
|
749
|
+
logger.warn('OpenAITransport: function_call arguments are not valid JSON', { raw });
|
|
750
|
+
return {};
|
|
751
|
+
}
|
|
752
|
+
}
|