@genesislcap/foundation-ai 15.91.0 → 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.
Files changed (35) hide show
  1. package/README.md +26 -103
  2. package/dist/dts/ai-provider.d.ts +5 -3
  3. package/dist/dts/ai-provider.d.ts.map +1 -1
  4. package/dist/dts/index.d.ts +7 -4
  5. package/dist/dts/index.d.ts.map +1 -1
  6. package/dist/dts/providers/openai-provider.d.ts +8 -7
  7. package/dist/dts/providers/openai-provider.d.ts.map +1 -1
  8. package/dist/dts/transports/budget-exhausted-error.d.ts +5 -7
  9. package/dist/dts/transports/budget-exhausted-error.d.ts.map +1 -1
  10. package/dist/dts/transports/context-overflow-error.d.ts.map +1 -1
  11. package/dist/dts/transports/openai-transport.d.ts +119 -0
  12. package/dist/dts/transports/openai-transport.d.ts.map +1 -0
  13. package/dist/dts/types/chat.types.d.ts +12 -0
  14. package/dist/dts/types/chat.types.d.ts.map +1 -1
  15. package/dist/dts/types/config.types.d.ts +44 -16
  16. package/dist/dts/types/config.types.d.ts.map +1 -1
  17. package/dist/dts/utils/token-cost.d.ts +65 -1
  18. package/dist/dts/utils/token-cost.d.ts.map +1 -1
  19. package/dist/dts/utils/tool-schema.d.ts +38 -0
  20. package/dist/dts/utils/tool-schema.d.ts.map +1 -1
  21. package/dist/esm/ai-provider.js +21 -14
  22. package/dist/esm/index.js +4 -2
  23. package/dist/esm/providers/openai-provider.js +15 -9
  24. package/dist/esm/transports/budget-exhausted-error.js +6 -7
  25. package/dist/esm/transports/context-overflow-error.js +2 -0
  26. package/dist/esm/transports/openai-transport.js +752 -0
  27. package/dist/esm/types/config.types.js +12 -1
  28. package/dist/esm/utils/token-cost.js +89 -0
  29. package/dist/esm/utils/tool-schema.js +108 -0
  30. package/dist/foundation-ai.api.json +1745 -137
  31. package/dist/foundation-ai.d.ts +270 -28
  32. package/package.json +9 -9
  33. package/dist/dts/transports/server-openai-transport.d.ts +0 -23
  34. package/dist/dts/transports/server-openai-transport.d.ts.map +0 -1
  35. package/dist/esm/transports/server-openai-transport.js +0 -94
@@ -18,11 +18,20 @@ export const SUPPORTED_ANTHROPIC_MODEL_IDS = [
18
18
  'claude-sonnet-4-6',
19
19
  'claude-haiku-4-5-20251001',
20
20
  ];
21
+ /** @beta */
22
+ export const SUPPORTED_OPENAI_MODEL_IDS = [
23
+ 'gpt-6-astra',
24
+ 'gpt-6.1-sol',
25
+ 'gpt-6.1-terra',
26
+ 'gpt-6-sol',
27
+ 'gpt-6-luna',
28
+ ];
21
29
  /**
22
30
  * Which vendor a model id belongs to, or `undefined` when no supported provider claims it.
23
31
  *
24
32
  * @remarks
25
- * Derived from {@link SUPPORTED_ANTHROPIC_MODEL_IDS} and {@link SUPPORTED_GEMINI_MODEL_IDS},
33
+ * Derived from {@link SUPPORTED_ANTHROPIC_MODEL_IDS}, {@link SUPPORTED_GEMINI_MODEL_IDS} and
34
+ * {@link SUPPORTED_OPENAI_MODEL_IDS},
26
35
  * so it cannot drift from them the way a hand-maintained map does — adding a model to an
27
36
  * allowlist is enough to teach this too.
28
37
  *
@@ -48,5 +57,7 @@ export function vendorOfModel(modelId) {
48
57
  return 'anthropic';
49
58
  if (SUPPORTED_GEMINI_MODEL_IDS.includes(modelId))
50
59
  return 'gemini';
60
+ if (SUPPORTED_OPENAI_MODEL_IDS.includes(modelId))
61
+ return 'openai';
51
62
  return undefined;
52
63
  }
@@ -298,3 +298,92 @@ export function geminiTokenCost(model, usage, at = new Date()) {
298
298
  },
299
299
  };
300
300
  }
301
+ // ---------------------------------------------------------------------------
302
+ // OpenAI
303
+ // ---------------------------------------------------------------------------
304
+ /**
305
+ * Standard tier pricing per million tokens, for prompts at or below
306
+ * {@link OPENAI_LONG_CONTEXT_THRESHOLD} — https://developers.openai.com/api/docs/pricing
307
+ * (read 2026-11-03).
308
+ *
309
+ * `cachedPerMillion` is stated per model rather than derived from one multiplier, because OpenAI's
310
+ * read discount is not uniform: 0.1x on most models, 0.05x on the 6.1 models, and 0.1x again on the
311
+ * older `gpt-6-sol` whose input rate matches 6.1 Sol's.
312
+ */
313
+ const OPENAI_PRICING = {
314
+ 'gpt-6-astra': { promptPerMillion: 10, cachedPerMillion: 1, candidatePerMillion: 50 },
315
+ 'gpt-6.1-sol': { promptPerMillion: 2, cachedPerMillion: 0.1, candidatePerMillion: 10 },
316
+ 'gpt-6.1-terra': { promptPerMillion: 1, cachedPerMillion: 0.05, candidatePerMillion: 5 },
317
+ 'gpt-6-sol': { promptPerMillion: 2, cachedPerMillion: 0.2, candidatePerMillion: 10 },
318
+ 'gpt-6-luna': { promptPerMillion: 0.1, cachedPerMillion: 0.01, candidatePerMillion: 0.5 },
319
+ };
320
+ /**
321
+ * Above this many input tokens the WHOLE request bills at the long-context tier — input and cached
322
+ * input at {@link OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER}, output at
323
+ * {@link OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER}. Like Gemini's tier it is a property of the request,
324
+ * not a marginal rate, and it is chosen on the whole prompt: uncached, cached and written tokens.
325
+ *
326
+ * @beta
327
+ */
328
+ export const OPENAI_LONG_CONTEXT_THRESHOLD = 272000;
329
+ /** @beta */
330
+ export const OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER = 2;
331
+ /** @beta */
332
+ export const OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER = 1.5;
333
+ /**
334
+ * Cache-write multipliers on the base input rate, by TTL —
335
+ * https://developers.openai.com/api/docs/guides/prompt-caching. These are the WHOLE rate for a
336
+ * written token, not a premium added to it, which is the same shape as Anthropic's.
337
+ *
338
+ * @beta
339
+ */
340
+ export const OPENAI_CACHE_WRITE_30M_MULTIPLIER = 1.25;
341
+ /** @beta */
342
+ export const OPENAI_CACHE_WRITE_12H_MULTIPLIER = 2.5;
343
+ /**
344
+ * The rates a request on `model` with `promptTokens` of input is billed at. `promptTokens` is the
345
+ * whole prompt; pass 0 when only the standard tier is wanted.
346
+ *
347
+ * @beta
348
+ */
349
+ export function openaiRatesFor(model, promptTokens) {
350
+ const rate = OPENAI_PRICING[model];
351
+ if (promptTokens <= OPENAI_LONG_CONTEXT_THRESHOLD) {
352
+ return rate;
353
+ }
354
+ return {
355
+ promptPerMillion: rate.promptPerMillion * OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER,
356
+ cachedPerMillion: rate.cachedPerMillion * OPENAI_LONG_CONTEXT_INPUT_MULTIPLIER,
357
+ candidatePerMillion: rate.candidatePerMillion * OPENAI_LONG_CONTEXT_OUTPUT_MULTIPLIER,
358
+ };
359
+ }
360
+ /**
361
+ * Cost one OpenAI request.
362
+ *
363
+ * @beta
364
+ */
365
+ export function openaiTokenCost(model, usage) {
366
+ const { promptPerMillion, cachedPerMillion, candidatePerMillion } = openaiRatesFor(model, usage.inputTokens);
367
+ const m = TOKENS_PER_MILLION;
368
+ const uncachedTokens = Math.max(0, usage.inputTokens - usage.cacheReadTokens - usage.cacheWriteTokens);
369
+ const write12hTokens = Math.min(usage.cacheWrite12hTokens, usage.cacheWriteTokens);
370
+ const write30mTokens = Math.max(0, usage.cacheWriteTokens - write12hTokens);
371
+ const promptCost = (uncachedTokens / m) * promptPerMillion;
372
+ const cacheReadCost = (usage.cacheReadTokens / m) * cachedPerMillion;
373
+ const cacheWriteCost = (write30mTokens / m) * promptPerMillion * OPENAI_CACHE_WRITE_30M_MULTIPLIER +
374
+ (write12hTokens / m) * promptPerMillion * OPENAI_CACHE_WRITE_12H_MULTIPLIER;
375
+ const candidateCost = (usage.outputTokens / m) * candidatePerMillion;
376
+ // Same netting as Anthropic's: reads save against the full input rate, writes cost over it.
377
+ const cacheReadFull = (usage.cacheReadTokens / m) * promptPerMillion;
378
+ const cacheWriteFull = (usage.cacheWriteTokens / m) * promptPerMillion;
379
+ return {
380
+ costUsd: promptCost + cacheReadCost + cacheWriteCost + candidateCost,
381
+ savedUsd: cacheReadFull - cacheReadCost + (cacheWriteFull - cacheWriteCost),
382
+ breakdown: {
383
+ promptUsd: promptCost,
384
+ cacheReadUsd: cacheReadCost,
385
+ cacheWriteUsd: cacheWriteCost,
386
+ candidateUsd: candidateCost,
387
+ },
388
+ };
389
+ }
@@ -331,3 +331,111 @@ export function toGeminiSchema(parameters) {
331
331
  });
332
332
  return translated;
333
333
  }
334
+ // ── OpenAI: strict structured outputs (GENC-1829) ────────────────────────────
335
+ /**
336
+ * Keywords OpenAI's strict mode does not support and which cannot be dropped without changing what
337
+ * the schema admits — left in place so the request fails loudly, the same choice as
338
+ * {@link UNDROPPABLE_KEYWORDS}. `oneOf` is listed because the docs name `anyOf` as supported and say
339
+ * nothing of it; treat it as unsupported until measured.
340
+ * https://developers.openai.com/api/docs/guides/structured-outputs
341
+ */
342
+ const OPENAI_UNDROPPABLE_KEYWORDS = ['allOf', 'not', 'if', 'then', 'else', 'oneOf'];
343
+ /** Make a property schema also admit `null`, in whichever spelling it already uses. */
344
+ function withNull(node) {
345
+ if (typeof node.type === 'string') {
346
+ const out = Object.assign(Object.assign({}, node), { type: node.type === 'null' ? 'null' : [node.type, 'null'] });
347
+ if (Array.isArray(node.enum) && !node.enum.includes(null))
348
+ out.enum = [...node.enum, null];
349
+ return out;
350
+ }
351
+ if (Array.isArray(node.type)) {
352
+ if (node.type.includes('null'))
353
+ return node;
354
+ const out = Object.assign(Object.assign({}, node), { type: [...node.type, 'null'] });
355
+ if (Array.isArray(node.enum) && !node.enum.includes(null))
356
+ out.enum = [...node.enum, null];
357
+ return out;
358
+ }
359
+ if (Array.isArray(node.anyOf)) {
360
+ return node.anyOf.some((b) => isPlainObject(b) && b.type === 'null')
361
+ ? node
362
+ : Object.assign(Object.assign({}, node), { anyOf: [...node.anyOf, { type: 'null' }] });
363
+ }
364
+ // No type at all (a bare `$ref`, an enum-only node): wrap rather than guess a type for it.
365
+ return { anyOf: [node, { type: 'null' }] };
366
+ }
367
+ /**
368
+ * Adapt a JSON Schema for OpenAI strict mode (`strict: true` on a function tool, or a
369
+ * `text.format` json_schema).
370
+ *
371
+ * Strict mode needs three things the canonical dialect does not:
372
+ *
373
+ * 1. **Every property listed in `required`.** A property the caller left optional is made
374
+ * required-but-nullable, which is the documented way to express "may be absent" — so the
375
+ * meaning survives, but the SHAPE of the arguments changes (absent becomes `null`), and that is
376
+ * reported in {@link OpenAIStrictSchema.madeNullable}.
377
+ * 2. **`additionalProperties: false` on every object**, as with Anthropic enforcement.
378
+ * 3. **No `allOf` / `not` / conditionals**, which are left in place so the request is rejected
379
+ * rather than silently enforcing a weaker schema.
380
+ *
381
+ * Numeric bounds, `pattern`, `format`, `minItems`/`maxItems`, `anyOf`, `$defs`/`$ref` are all
382
+ * supported by strict mode and pass through untouched.
383
+ */
384
+ export function toOpenAIStrictSchema(parameters) {
385
+ const madeNullable = [];
386
+ const overridden = [];
387
+ const undroppable = [];
388
+ const schema = mapSchema(parameters, (node, path) => {
389
+ for (const keyword of OPENAI_UNDROPPABLE_KEYWORDS) {
390
+ if (keyword in node)
391
+ undroppable.push(`${path}/${keyword}`);
392
+ }
393
+ const isObjectNode = node.type === 'object' ||
394
+ (Array.isArray(node.type) && node.type.includes('object')) ||
395
+ isPlainObject(node.properties);
396
+ if (!isObjectNode)
397
+ return node;
398
+ if (node.additionalProperties !== false) {
399
+ if (node.additionalProperties !== undefined)
400
+ overridden.push(`${path}/additionalProperties`);
401
+ node.additionalProperties = false;
402
+ }
403
+ if (isPlainObject(node.properties)) {
404
+ const names = Object.keys(node.properties);
405
+ const required = new Set(Array.isArray(node.required) ? node.required : []);
406
+ const properties = node.properties;
407
+ for (const name of names) {
408
+ if (required.has(name))
409
+ continue;
410
+ properties[name] = isPlainObject(properties[name])
411
+ ? withNull(properties[name])
412
+ : { type: 'null' };
413
+ madeNullable.push(`${path}/properties/${name}`);
414
+ }
415
+ node.required = names;
416
+ }
417
+ return node;
418
+ });
419
+ return { schema, madeNullable, overridden, undroppable };
420
+ }
421
+ /**
422
+ * {@link toOpenAIStrictSchema} for transport use: logs what changed and returns only the schema. A
423
+ * verb, for the same reason as {@link enforceAnthropicToolSchema}.
424
+ */
425
+ export function enforceOpenAIToolSchema(parameters, toolName) {
426
+ const { schema, madeNullable, overridden, undroppable } = toOpenAIStrictSchema(parameters);
427
+ if (madeNullable.length) {
428
+ logger.debug(`OpenAITransport: tool "${toolName}" is strict, which has no optional properties; these are ` +
429
+ `now required and nullable, so an absent value arrives as null: ${madeNullable.join(', ')}`);
430
+ }
431
+ if (overridden.length) {
432
+ logger.warn(`OpenAITransport: tool "${toolName}" is strict, which requires closed objects; ` +
433
+ `\`additionalProperties\` was set to false at: ${overridden.join(', ')}.`);
434
+ }
435
+ if (undroppable.length) {
436
+ logger.warn(`OpenAITransport: tool "${toolName}" uses schema keywords strict mode cannot express ` +
437
+ `(${undroppable.join(', ')}). They are left in place, so the request will be rejected. ` +
438
+ `Rewrite them or drop enforceSchema for this tool.`);
439
+ }
440
+ return schema;
441
+ }