@x12i/ai-gateway 9.1.6 → 9.3.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.
Files changed (78) hide show
  1. package/README.md +45 -0
  2. package/dist/activity-manager.d.ts +1 -0
  3. package/dist/activity-manager.js +7 -0
  4. package/dist/ai-tools-client.d.ts +20 -0
  5. package/dist/ai-tools-client.js +91 -0
  6. package/dist/flex-md-loader.d.ts +5 -0
  7. package/dist/flex-md-loader.js +16 -0
  8. package/dist/gateway-config.d.ts +2 -0
  9. package/dist/gateway-config.js +2 -1
  10. package/dist/gateway-mode.d.ts +40 -0
  11. package/dist/gateway-mode.js +75 -0
  12. package/dist/gateway-utils.d.ts +57 -1
  13. package/dist/gateway-utils.js +181 -12
  14. package/dist/gateway.d.ts +3 -0
  15. package/dist/gateway.js +47 -15
  16. package/dist/index.d.ts +6 -1
  17. package/dist/index.js +3 -1
  18. package/dist/output-contract-normalizer.d.ts +21 -0
  19. package/dist/output-contract-normalizer.js +121 -0
  20. package/dist/types.d.ts +35 -0
  21. package/dist-cjs/activity-manager.cjs +21 -19
  22. package/dist-cjs/activity-manager.d.ts +1 -0
  23. package/dist-cjs/ai-tools-client.cjs +91 -0
  24. package/dist-cjs/ai-tools-client.d.ts +20 -0
  25. package/dist-cjs/config/activity-tracking-config.cjs +1 -4
  26. package/dist-cjs/content-normalizer/content-normalizer.cjs +3 -8
  27. package/dist-cjs/content-normalizer/index.cjs +1 -7
  28. package/dist-cjs/content-normalizer/types.cjs +1 -2
  29. package/dist-cjs/flex-md-loader.cjs +35 -65
  30. package/dist-cjs/flex-md-loader.d.ts +5 -0
  31. package/dist-cjs/gateway-config.cjs +25 -63
  32. package/dist-cjs/gateway-config.d.ts +2 -0
  33. package/dist-cjs/gateway-conversion.cjs +10 -48
  34. package/dist-cjs/gateway-instructions.cjs +5 -10
  35. package/dist-cjs/gateway-log-meta.cjs +9 -14
  36. package/dist-cjs/gateway-memory.cjs +2 -6
  37. package/dist-cjs/gateway-messages.cjs +3 -6
  38. package/dist-cjs/gateway-meta.cjs +1 -4
  39. package/dist-cjs/gateway-mode.cjs +75 -0
  40. package/dist-cjs/gateway-mode.d.ts +40 -0
  41. package/dist-cjs/gateway-provider-auto-register.cjs +2 -38
  42. package/dist-cjs/gateway-provider.cjs +10 -22
  43. package/dist-cjs/gateway-rate-limiter-constants.cjs +2 -5
  44. package/dist-cjs/gateway-rate-limiter.cjs +5 -9
  45. package/dist-cjs/gateway-retry.cjs +6 -14
  46. package/dist-cjs/gateway-utils.cjs +201 -83
  47. package/dist-cjs/gateway-utils.d.ts +57 -1
  48. package/dist-cjs/gateway-validation.cjs +2 -6
  49. package/dist-cjs/gateway.cjs +100 -72
  50. package/dist-cjs/gateway.d.ts +3 -0
  51. package/dist-cjs/index.cjs +22 -91
  52. package/dist-cjs/index.d.ts +6 -1
  53. package/dist-cjs/instruction-errors.cjs +2 -7
  54. package/dist-cjs/instruction-optimizer.cjs +4 -10
  55. package/dist-cjs/instructions-parser.cjs +5 -10
  56. package/dist-cjs/logger-factory.cjs +3 -6
  57. package/dist-cjs/memory-path-resolution.cjs +8 -18
  58. package/dist-cjs/message-builder.cjs +11 -47
  59. package/dist-cjs/object-types-library-integration.cjs +3 -8
  60. package/dist-cjs/object-types-library.cjs +5 -10
  61. package/dist-cjs/output-auditor.cjs +1 -4
  62. package/dist-cjs/output-contract-normalizer.cjs +121 -0
  63. package/dist-cjs/output-contract-normalizer.d.ts +21 -0
  64. package/dist-cjs/request-report-generator.cjs +1 -4
  65. package/dist-cjs/response-analyzer/format-type-detector.cjs +1 -5
  66. package/dist-cjs/response-analyzer/index.cjs +3 -9
  67. package/dist-cjs/response-analyzer/object-type-detector.cjs +1 -5
  68. package/dist-cjs/response-analyzer/response-analyzer.cjs +6 -10
  69. package/dist-cjs/response-analyzer/types.cjs +1 -2
  70. package/dist-cjs/response-fallback-fixer.cjs +1 -4
  71. package/dist-cjs/runtime-objects.cjs +7 -13
  72. package/dist-cjs/template-parser.cjs +5 -42
  73. package/dist-cjs/template-render-merge.cjs +2 -6
  74. package/dist-cjs/troubleshooting-helper.cjs +13 -28
  75. package/dist-cjs/types.cjs +1 -2
  76. package/dist-cjs/types.d.ts +35 -0
  77. package/dist-cjs/usage-tracker.cjs +3 -7
  78. package/package.json +11 -5
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Normalizes invoke responses into `output.parsed` when an explicit output contract is forwarded.
3
+ * Does not infer contracts from other request fields — that is upstream (graph-engine / ai-tasks).
4
+ */
5
+ import { parseMarkdownSectionsFromContent } from './flex-md-loader.js';
6
+ import { coalesceMergedInputBucket, extractCallerInputsBag, parseLooseJsonObject } from './memory-path-resolution.js';
7
+ function isPlainObject(value) {
8
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
9
+ }
10
+ function hasMeaningfulContractValue(value) {
11
+ if (value === undefined || value === null)
12
+ return false;
13
+ if (typeof value === 'string')
14
+ return value.trim().length > 0;
15
+ if (Array.isArray(value))
16
+ return value.length > 0;
17
+ if (typeof value === 'object')
18
+ return Object.keys(value).length > 0;
19
+ return true;
20
+ }
21
+ /**
22
+ * Maps an explicit contract spec to field keys. No inference from descriptions or other request fields.
23
+ */
24
+ export function contractSpecToFieldKeys(contract) {
25
+ if (contract == null)
26
+ return [];
27
+ if (Array.isArray(contract)) {
28
+ return contract.filter((k) => typeof k === 'string' && k.trim().length > 0);
29
+ }
30
+ if (!isPlainObject(contract))
31
+ return [];
32
+ const properties = contract.properties;
33
+ if (isPlainObject(properties)) {
34
+ return Object.keys(properties);
35
+ }
36
+ return [];
37
+ }
38
+ /** Graph-engine path: `workingMemory.inputs.outputContract` or merged `input.outputContract`. */
39
+ function readExplicitOutputContractFromWorkingMemory(workingMemory) {
40
+ if (!isPlainObject(workingMemory))
41
+ return undefined;
42
+ const inputs = extractCallerInputsBag(workingMemory);
43
+ if (inputs?.outputContract !== undefined)
44
+ return inputs.outputContract;
45
+ const input = coalesceMergedInputBucket(workingMemory);
46
+ if (isPlainObject(input) && input.outputContract !== undefined) {
47
+ return input.outputContract;
48
+ }
49
+ const loose = parseLooseJsonObject(workingMemory.input);
50
+ if (loose?.outputContract !== undefined)
51
+ return loose.outputContract;
52
+ return undefined;
53
+ }
54
+ /**
55
+ * Resolves field keys only from explicit `outputContract` on the request or graph-forwarded inputs.
56
+ */
57
+ export function resolveOutputContractFieldKeys(request) {
58
+ if (request == null || typeof request !== 'object')
59
+ return undefined;
60
+ const r = request;
61
+ const candidates = [
62
+ r.outputContract,
63
+ readExplicitOutputContractFromWorkingMemory(r.workingMemory)
64
+ ];
65
+ for (const candidate of candidates) {
66
+ const keys = contractSpecToFieldKeys(candidate);
67
+ if (keys.length > 0)
68
+ return keys;
69
+ }
70
+ return undefined;
71
+ }
72
+ function asParsedRecord(parsed) {
73
+ if (!isPlainObject(parsed))
74
+ return {};
75
+ return { ...parsed };
76
+ }
77
+ function pickAliasValue(source, key) {
78
+ if (hasMeaningfulContractValue(source[key]))
79
+ return source[key];
80
+ const spaced = key.replace(/([A-Z])/g, ' $1').replace(/^./, (c) => c.toUpperCase());
81
+ const title = spaced.charAt(0).toUpperCase() + spaced.slice(1);
82
+ const aliases = [
83
+ key,
84
+ key.toLowerCase(),
85
+ title,
86
+ title.replace(/\s+/g, ' '),
87
+ key.replace(/([A-Z])/g, '_$1').toLowerCase()
88
+ ];
89
+ for (const alias of aliases) {
90
+ if (hasMeaningfulContractValue(source[alias]))
91
+ return source[alias];
92
+ }
93
+ return undefined;
94
+ }
95
+ /**
96
+ * Fills missing contract keys from markdown sections after flex-md. Only runs when explicit contract keys were supplied.
97
+ */
98
+ export async function enrichParsedContentForOutputContract(parsed, rawContent, contractKeys, logger) {
99
+ const base = asParsedRecord(parsed);
100
+ if (!contractKeys?.length)
101
+ return base;
102
+ const missing = contractKeys.filter((k) => !hasMeaningfulContractValue(base[k]));
103
+ if (missing.length === 0)
104
+ return base;
105
+ const content = typeof rawContent === 'string' && rawContent.trim().length > 0
106
+ ? rawContent
107
+ : typeof base.rawText === 'string'
108
+ ? base.rawText
109
+ : '';
110
+ if (!content.trim())
111
+ return base;
112
+ const fromMarkdown = parseMarkdownSectionsFromContent(content, logger);
113
+ const merged = { ...base };
114
+ for (const key of missing) {
115
+ const value = pickAliasValue(fromMarkdown, key);
116
+ if (hasMeaningfulContractValue(value)) {
117
+ merged[key] = value;
118
+ }
119
+ }
120
+ return merged;
121
+ }
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Normalizes invoke responses into `output.parsed` when an explicit output contract is forwarded.
3
+ * Does not infer contracts from other request fields — that is upstream (graph-engine / ai-tasks).
4
+ */
5
+ import type { Logxer } from '@x12i/logxer';
6
+ /** Explicit contract: field names or JSON-schema-style `properties` only. */
7
+ export type OutputContractSpec = string[] | {
8
+ properties: Record<string, unknown>;
9
+ };
10
+ /**
11
+ * Maps an explicit contract spec to field keys. No inference from descriptions or other request fields.
12
+ */
13
+ export declare function contractSpecToFieldKeys(contract: unknown): string[];
14
+ /**
15
+ * Resolves field keys only from explicit `outputContract` on the request or graph-forwarded inputs.
16
+ */
17
+ export declare function resolveOutputContractFieldKeys(request: unknown): string[] | undefined;
18
+ /**
19
+ * Fills missing contract keys from markdown sections after flex-md. Only runs when explicit contract keys were supplied.
20
+ */
21
+ export declare function enrichParsedContentForOutputContract(parsed: unknown, rawContent: string, contractKeys: string[] | undefined, logger?: Logxer): Promise<Record<string, unknown>>;
@@ -1,11 +1,8 @@
1
- "use strict";
2
1
  /**
3
2
  * Request Report Generator
4
3
  *
5
4
  * Generates comprehensive request reports with validation, examples, and structured text information
6
5
  */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.generateRequestReport = generateRequestReport;
9
6
  /**
10
7
  * Extract validation rules from markdown
11
8
  */
@@ -148,7 +145,7 @@ function schemasMatch(schema1, schema2) {
148
145
  /**
149
146
  * Generate comprehensive request report
150
147
  */
151
- async function generateRequestReport(request, library, // ObjectTypesLibrary removed
148
+ export async function generateRequestReport(request, library, // ObjectTypesLibrary removed
152
149
  logger) {
153
150
  const report = {
154
151
  requestId: request.identity.jobId || request.aiRequestId,
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /**
3
2
  * Format Type Detector
4
3
  *
@@ -10,12 +9,10 @@
10
9
  * - Word/character count
11
10
  * - Structure patterns
12
11
  */
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.FormatTypeDetector = void 0;
15
12
  /**
16
13
  * Detects format type of text content
17
14
  */
18
- class FormatTypeDetector {
15
+ export class FormatTypeDetector {
19
16
  /**
20
17
  * Detects format type from text
21
18
  *
@@ -116,4 +113,3 @@ class FormatTypeDetector {
116
113
  };
117
114
  }
118
115
  }
119
- exports.FormatTypeDetector = FormatTypeDetector;
@@ -1,14 +1,8 @@
1
- "use strict";
2
1
  /**
3
2
  * Response Analyzer Module
4
3
  *
5
4
  * Exports all public APIs for response analysis
6
5
  */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.FormatTypeDetector = exports.ObjectTypeDetector = exports.ResponseAnalyzer = void 0;
9
- var response_analyzer_js_1 = require("./response-analyzer.cjs");
10
- Object.defineProperty(exports, "ResponseAnalyzer", { enumerable: true, get: function () { return response_analyzer_js_1.ResponseAnalyzer; } });
11
- var object_type_detector_js_1 = require("./object-type-detector.cjs");
12
- Object.defineProperty(exports, "ObjectTypeDetector", { enumerable: true, get: function () { return object_type_detector_js_1.ObjectTypeDetector; } });
13
- var format_type_detector_js_1 = require("./format-type-detector.cjs");
14
- Object.defineProperty(exports, "FormatTypeDetector", { enumerable: true, get: function () { return format_type_detector_js_1.FormatTypeDetector; } });
6
+ export { ResponseAnalyzer } from './response-analyzer.js';
7
+ export { ObjectTypeDetector } from './object-type-detector.js';
8
+ export { FormatTypeDetector } from './format-type-detector.js';
@@ -1,16 +1,13 @@
1
- "use strict";
2
1
  /**
3
2
  * Object Type Detector
4
3
  *
5
4
  * Matches objects against predefined type definitions based on properties.
6
5
  * Uses a scoring algorithm to determine the best match.
7
6
  */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.ObjectTypeDetector = void 0;
10
7
  /**
11
8
  * Detects object types by matching against definitions
12
9
  */
13
- class ObjectTypeDetector {
10
+ export class ObjectTypeDetector {
14
11
  definitions;
15
12
  minThreshold;
16
13
  constructor(config) {
@@ -96,4 +93,3 @@ class ObjectTypeDetector {
96
93
  return confidence;
97
94
  }
98
95
  }
99
- exports.ObjectTypeDetector = ObjectTypeDetector;
@@ -1,18 +1,15 @@
1
- "use strict";
2
1
  /**
3
2
  * Response Analyzer
4
3
  *
5
4
  * Main class that orchestrates object type and format type detection.
6
5
  * Analyzes normalized responses and provides enhanced metadata.
7
6
  */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.ResponseAnalyzer = void 0;
10
- const object_type_detector_js_1 = require("./object-type-detector.cjs");
11
- const format_type_detector_js_1 = require("./format-type-detector.cjs");
7
+ import { ObjectTypeDetector } from './object-type-detector.js';
8
+ import { FormatTypeDetector } from './format-type-detector.js';
12
9
  /**
13
10
  * Analyzes AI responses to detect object types and format types
14
11
  */
15
- class ResponseAnalyzer {
12
+ export class ResponseAnalyzer {
16
13
  objectDetector;
17
14
  formatDetector;
18
15
  enableFormatDetection;
@@ -20,13 +17,13 @@ class ResponseAnalyzer {
20
17
  this.enableFormatDetection = config.enableFormatDetection !== false;
21
18
  // Initialize object type detector if definitions provided
22
19
  if (config.objectTypeDefinitions && config.objectTypeDefinitions.length > 0) {
23
- this.objectDetector = new object_type_detector_js_1.ObjectTypeDetector({
20
+ this.objectDetector = new ObjectTypeDetector({
24
21
  objectTypeDefinitions: config.objectTypeDefinitions,
25
22
  minConfidenceThreshold: config.minConfidenceThreshold || 0.5
26
23
  });
27
24
  }
28
25
  // Initialize format type detector
29
- this.formatDetector = new format_type_detector_js_1.FormatTypeDetector();
26
+ this.formatDetector = new FormatTypeDetector();
30
27
  }
31
28
  /**
32
29
  * Analyzes a response and returns enhanced metadata
@@ -88,7 +85,7 @@ class ResponseAnalyzer {
88
85
  */
89
86
  updateObjectTypeDefinitions(definitions) {
90
87
  if (definitions.length > 0) {
91
- this.objectDetector = new object_type_detector_js_1.ObjectTypeDetector({
88
+ this.objectDetector = new ObjectTypeDetector({
92
89
  objectTypeDefinitions: definitions,
93
90
  minConfidenceThreshold: 0.5
94
91
  });
@@ -98,4 +95,3 @@ class ResponseAnalyzer {
98
95
  }
99
96
  }
100
97
  }
101
- exports.ResponseAnalyzer = ResponseAnalyzer;
@@ -1,5 +1,4 @@
1
- "use strict";
2
1
  /**
3
2
  * Type definitions for Response Analyzer
4
3
  */
5
- Object.defineProperty(exports, "__esModule", { value: true });
4
+ export {};
@@ -1,6 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.tryFixGatewayResponseToJsonText = tryFixGatewayResponseToJsonText;
4
1
  function tryParseJson(text) {
5
2
  try {
6
3
  return JSON.parse(text);
@@ -79,7 +76,7 @@ function stripToFirstJsonLike(text) {
79
76
  const stripped = s.slice(start).trim();
80
77
  return stripped.length > 0 ? stripped : undefined;
81
78
  }
82
- function tryFixGatewayResponseToJsonText(raw) {
79
+ export function tryFixGatewayResponseToJsonText(raw) {
83
80
  const warnings = [];
84
81
  const input = raw ?? '';
85
82
  if (!input || !input.trim()) {
@@ -1,17 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.runtimeObjects = void 0;
4
- exports.setGatewayLastJobId = setGatewayLastJobId;
5
- exports.setGatewayRuntimeClients = setGatewayRuntimeClients;
6
- exports.getRuntimeObservabilityMode = getRuntimeObservabilityMode;
7
- exports.runtimeObjects = createRuntimeObjectsForMode();
8
- function setGatewayLastJobId(jobId) {
1
+ export let runtimeObjects = createRuntimeObjectsForMode();
2
+ export function setGatewayLastJobId(jobId) {
9
3
  const objects = ensureRuntimeObjectsForCurrentMode();
10
4
  if (!objects)
11
5
  return;
12
6
  objects.lastJobId = jobId;
13
7
  }
14
- function setGatewayRuntimeClients(input) {
8
+ export function setGatewayRuntimeClients(input) {
15
9
  const objects = ensureRuntimeObjectsForCurrentMode();
16
10
  if (!objects)
17
11
  return;
@@ -19,17 +13,17 @@ function setGatewayRuntimeClients(input) {
19
13
  objects.logxerClient = input.logger ? asLogxerQueryableClient(input.logger) : undefined;
20
14
  objects.packagesRuntimeObjects = [];
21
15
  }
22
- function getRuntimeObservabilityMode() {
16
+ export function getRuntimeObservabilityMode() {
23
17
  const rawMode = process.env.mode ?? process.env.MODE;
24
18
  return rawMode === 'prod' ? 'prod' : 'debug';
25
19
  }
26
20
  function ensureRuntimeObjectsForCurrentMode() {
27
21
  if (getRuntimeObservabilityMode() === 'prod') {
28
- exports.runtimeObjects = undefined;
22
+ runtimeObjects = undefined;
29
23
  return undefined;
30
24
  }
31
- exports.runtimeObjects ??= createRuntimeObjects();
32
- return exports.runtimeObjects;
25
+ runtimeObjects ??= createRuntimeObjects();
26
+ return runtimeObjects;
33
27
  }
34
28
  function createRuntimeObjectsForMode() {
35
29
  return getRuntimeObservabilityMode() === 'debug' ? createRuntimeObjects() : undefined;
@@ -1,47 +1,10 @@
1
- "use strict";
2
1
  /**
3
2
  * Template Parser
4
3
  *
5
4
  * Uses @x12i/rendrix (v4+) to parse templates with workingMemory.
6
5
  * TemplateResolutionError from the parser is rethrown; other errors fall back to the raw template.
7
6
  */
8
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
9
- if (k2 === undefined) k2 = k;
10
- var desc = Object.getOwnPropertyDescriptor(m, k);
11
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
12
- desc = { enumerable: true, get: function() { return m[k]; } };
13
- }
14
- Object.defineProperty(o, k2, desc);
15
- }) : (function(o, m, k, k2) {
16
- if (k2 === undefined) k2 = k;
17
- o[k2] = m[k];
18
- }));
19
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
20
- Object.defineProperty(o, "default", { enumerable: true, value: v });
21
- }) : function(o, v) {
22
- o["default"] = v;
23
- });
24
- var __importStar = (this && this.__importStar) || (function () {
25
- var ownKeys = function(o) {
26
- ownKeys = Object.getOwnPropertyNames || function (o) {
27
- var ar = [];
28
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
29
- return ar;
30
- };
31
- return ownKeys(o);
32
- };
33
- return function (mod) {
34
- if (mod && mod.__esModule) return mod;
35
- var result = {};
36
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
37
- __setModuleDefault(result, mod);
38
- return result;
39
- };
40
- })();
41
- Object.defineProperty(exports, "__esModule", { value: true });
42
- exports.parseTemplate = parseTemplate;
43
- exports.isParserAvailable = isParserAvailable;
44
- const memory_path_resolution_js_1 = require("./memory-path-resolution.cjs");
7
+ import { prepareWorkingMemoryForTemplateRender } from './memory-path-resolution.js';
45
8
  let rendrixModule = null;
46
9
  let parserLoadPromise = null;
47
10
  async function loadRendrix() {
@@ -52,7 +15,7 @@ async function loadRendrix() {
52
15
  }
53
16
  parserLoadPromise = (async () => {
54
17
  try {
55
- rendrixModule = await Promise.resolve().then(() => __importStar(require('@x12i/rendrix')));
18
+ rendrixModule = await import('@x12i/rendrix');
56
19
  }
57
20
  catch {
58
21
  rendrixModule = null;
@@ -77,7 +40,7 @@ async function loadRendrix() {
77
40
  * @returns Parsed template string
78
41
  * @throws {Error} When the parser throws TemplateResolutionError (v4 MUST path missing after merge)
79
42
  */
80
- async function parseTemplate(template, workingMemory, taskConfig, shortTermMemory, experienceMemory, knowledgeMemory, templateRenderOptions, logger) {
43
+ export async function parseTemplate(template, workingMemory, taskConfig, shortTermMemory, experienceMemory, knowledgeMemory, templateRenderOptions, logger) {
81
44
  // If no template or empty, return as-is
82
45
  if (!template || typeof template !== 'string') {
83
46
  return template || '';
@@ -86,7 +49,7 @@ async function parseTemplate(template, workingMemory, taskConfig, shortTermMemor
86
49
  if (!workingMemory) {
87
50
  return template;
88
51
  }
89
- const wmForRender = (0, memory_path_resolution_js_1.prepareWorkingMemoryForTemplateRender)(workingMemory);
52
+ const wmForRender = prepareWorkingMemoryForTemplateRender(workingMemory);
90
53
  await loadRendrix();
91
54
  if (!rendrixModule) {
92
55
  return template;
@@ -134,6 +97,6 @@ async function parseTemplate(template, workingMemory, taskConfig, shortTermMemor
134
97
  /**
135
98
  * Checks if Rendrix (@x12i/rendrix) is available
136
99
  */
137
- function isParserAvailable() {
100
+ export function isParserAvailable() {
138
101
  return rendrixModule !== null;
139
102
  }
@@ -1,10 +1,6 @@
1
- "use strict";
2
1
  /**
3
2
  * Merge @x12i/rendrix TemplateRenderOptions from gateway defaults and per-request overrides.
4
3
  */
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.mergeTemplateRenderOptions = mergeTemplateRenderOptions;
7
- exports.mergeGatewayAndRequestTemplateRenderOptions = mergeGatewayAndRequestTemplateRenderOptions;
8
4
  function hasMeaningfulOptions(o) {
9
5
  if (o.templateId !== undefined)
10
6
  return true;
@@ -25,7 +21,7 @@ function hasMeaningfulOptions(o) {
25
21
  * Deep-merge template render options. Request/gateway overrides win per field.
26
22
  * For `subPathSearch`, `roots` on the override replace the base list when present.
27
23
  */
28
- function mergeTemplateRenderOptions(base, override) {
24
+ export function mergeTemplateRenderOptions(base, override) {
29
25
  if (!base && !override)
30
26
  return undefined;
31
27
  if (!base) {
@@ -50,7 +46,7 @@ function mergeTemplateRenderOptions(base, override) {
50
46
  * Gateway default `templateRendering` + optional request shorthand (`smartInput`, `smartInputRenderOptions`) + `templateRenderOptions`.
51
47
  * Same merge order as {@link buildMessages}.
52
48
  */
53
- function mergeGatewayAndRequestTemplateRenderOptions(gatewayTemplateRendering, request) {
49
+ export function mergeGatewayAndRequestTemplateRenderOptions(gatewayTemplateRendering, request) {
54
50
  const requestSmartTemplateOpts = request.smartInput !== undefined || request.smartInputRenderOptions !== undefined
55
51
  ? {
56
52
  ...(request.smartInput !== undefined ? { smartInput: request.smartInput } : {}),
@@ -1,24 +1,9 @@
1
- "use strict";
2
1
  /**
3
2
  * Troubleshooting Helper Toolbox
4
3
  *
5
4
  * Utility functions to diagnose and test AI Gateway issues
6
5
  * Based on TROUBLESHOOTING.md guide
7
6
  */
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.convertSchemaToSimpleFormat = convertSchemaToSimpleFormat;
10
- exports.validateAIRequest = validateAIRequest;
11
- exports.validateJSON = validateJSON;
12
- exports.extractJSON = extractJSON;
13
- exports.validateResponse = validateResponse;
14
- exports.diagnoseRequest = diagnoseRequest;
15
- exports.diagnoseResponse = diagnoseResponse;
16
- exports.supportsJSONMode = supportsJSONMode;
17
- exports.createTestAIRequest = createTestAIRequest;
18
- exports.createValidationTestCases = createValidationTestCases;
19
- exports.runValidationTests = runValidationTests;
20
- exports.formatDiagnostic = formatDiagnostic;
21
- exports.assertValidAIRequest = assertValidAIRequest;
22
7
  /**
23
8
  * Converts JSON Schema to simple, human-readable format for LLM instructions
24
9
  *
@@ -45,7 +30,7 @@ exports.assertValidAIRequest = assertValidAIRequest;
45
30
  * @param schema - Schema object (JSON Schema or simple format)
46
31
  * @returns Schema in simple format
47
32
  */
48
- function convertSchemaToSimpleFormat(schema) {
33
+ export function convertSchemaToSimpleFormat(schema) {
49
34
  if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
50
35
  return {};
51
36
  }
@@ -188,7 +173,7 @@ function validateSingleObjectType(objType, context, index) {
188
173
  * Validates AIRequest structure
189
174
  */
190
175
  const GATEWAY_ACTION_TYPES_DIAG = ['skill', 'preSkill', 'postSkill'];
191
- function validateAIRequest(request) {
176
+ export function validateAIRequest(request) {
192
177
  const errors = [];
193
178
  const warnings = [];
194
179
  const identityJobId = request.identity && typeof request.identity === 'object' ? request.identity.jobId : undefined;
@@ -230,7 +215,7 @@ function validateAIRequest(request) {
230
215
  /**
231
216
  * Validates JSON string
232
217
  */
233
- function validateJSON(jsonString) {
218
+ export function validateJSON(jsonString) {
234
219
  const errors = [];
235
220
  const warnings = [];
236
221
  if (!jsonString || typeof jsonString !== 'string') {
@@ -265,7 +250,7 @@ function validateJSON(jsonString) {
265
250
  /**
266
251
  * Extracts JSON from text (handles markdown code blocks, etc.)
267
252
  */
268
- function extractJSON(text) {
253
+ export function extractJSON(text) {
269
254
  if (!text || typeof text !== 'string') {
270
255
  return { json: null, method: 'none' };
271
256
  }
@@ -306,7 +291,7 @@ function extractJSON(text) {
306
291
  /**
307
292
  * Validates response structure
308
293
  */
309
- function validateResponse(response) {
294
+ export function validateResponse(response) {
310
295
  const errors = [];
311
296
  const warnings = [];
312
297
  if (!response.content) {
@@ -350,7 +335,7 @@ function validateResponse(response) {
350
335
  /**
351
336
  * Collects comprehensive diagnostic information
352
337
  */
353
- function diagnoseRequest(request) {
338
+ export function diagnoseRequest(request) {
354
339
  // AIRequest is distinguished by having primaryObjectType, objectTypes, or other AIRequest-specific fields
355
340
  const isAIRequest = 'primaryObjectType' in request ||
356
341
  ('objectTypes' in request && Array.isArray(request.objectTypes)) ||
@@ -374,7 +359,7 @@ function diagnoseRequest(request) {
374
359
  /**
375
360
  * Diagnoses response issues
376
361
  */
377
- function diagnoseResponse(response) {
362
+ export function diagnoseResponse(response) {
378
363
  if (!response) {
379
364
  return {
380
365
  hasContent: false,
@@ -409,7 +394,7 @@ function diagnoseResponse(response) {
409
394
  /**
410
395
  * Tests if a provider/model combination supports JSON mode
411
396
  */
412
- function supportsJSONMode(provider, model) {
397
+ export function supportsJSONMode(provider, model) {
413
398
  if (!provider || !model) {
414
399
  return { supported: false, notes: 'Provider and model must be specified' };
415
400
  }
@@ -457,7 +442,7 @@ function supportsJSONMode(provider, model) {
457
442
  /**
458
443
  * Creates a test AIRequest with minimal valid structure
459
444
  */
460
- function createTestAIRequest(overrides) {
445
+ export function createTestAIRequest(overrides) {
461
446
  const aiRequestId = 'test-ai-req-' + Date.now();
462
447
  const base = {
463
448
  aiRequestId,
@@ -496,7 +481,7 @@ function createTestAIRequest(overrides) {
496
481
  /**
497
482
  * Creates test cases for validation
498
483
  */
499
- function createValidationTestCases() {
484
+ export function createValidationTestCases() {
500
485
  const sampleIdentity = (jobId, agentId) => ({
501
486
  sessionId: `session-${jobId}`,
502
487
  instance: { instanceId: agentId, type: 'test' },
@@ -631,7 +616,7 @@ function createValidationTestCases() {
631
616
  /**
632
617
  * Runs all validation tests and returns results
633
618
  */
634
- function runValidationTests() {
619
+ export function runValidationTests() {
635
620
  const testCases = createValidationTestCases();
636
621
  const results = testCases.map(testCase => {
637
622
  const validation = validateAIRequest(testCase.request);
@@ -650,7 +635,7 @@ function runValidationTests() {
650
635
  /**
651
636
  * Formats diagnostic information for display
652
637
  */
653
- function formatDiagnostic(diagnostic) {
638
+ export function formatDiagnostic(diagnostic) {
654
639
  const lines = [];
655
640
  lines.push('=== Request Diagnostics ===');
656
641
  lines.push(`Job ID: ${diagnostic.request.hasJobId ? '✓' : '✗'}`);
@@ -679,7 +664,7 @@ function formatDiagnostic(diagnostic) {
679
664
  /**
680
665
  * Quick validation helper - throws if invalid
681
666
  */
682
- function assertValidAIRequest(request, logger) {
667
+ export function assertValidAIRequest(request, logger) {
683
668
  const validation = validateAIRequest(request);
684
669
  if (!validation.valid) {
685
670
  throw new Error(`AIRequest validation failed:\n${validation.errors.join('\n')}`);
@@ -1,7 +1,6 @@
1
- "use strict";
2
1
  /**
3
2
  * Type definitions for AI Gateway
4
3
  *
5
4
  * Extends router types with gateway-specific enhancements
6
5
  */
7
- Object.defineProperty(exports, "__esModule", { value: true });
6
+ export {};
@@ -340,6 +340,27 @@ export interface GatewayConfig extends Omit<RouterConfig, 'defaultEngine' | 'log
340
340
  openRouter?: {
341
341
  enabled?: boolean;
342
342
  };
343
+ /**
344
+ * Operational mode override (`process.env.mode` / `MODE` when omitted).
345
+ * - `prod`: unresolved models fall back to {@link AI_GATEWAY_DEFAULT_MODEL} / packaged default (with Logxer warn).
346
+ * - `dev` / `debug`: unresolved models throw {@link ModelResolutionError} from `@x12i/ai-tools`.
347
+ */
348
+ mode?: 'dev' | 'debug' | 'prod';
349
+ /**
350
+ * @x12i/ai-tools integration: catalog model resolution (request) and cost calculation (response).
351
+ */
352
+ aiTools?: {
353
+ /** @default true */
354
+ enabled?: boolean;
355
+ /** Inject Catalox; otherwise `createCataloxFromEnv()` from `@x12i/catalox/firebase`. */
356
+ catalox?: import('@x12i/catalox').Catalox;
357
+ cacheTtlMs?: number;
358
+ /** @default true */
359
+ resolveModels?: boolean;
360
+ /** @default true */
361
+ calculateCost?: boolean;
362
+ costIncludeBreakdown?: boolean;
363
+ };
343
364
  /**
344
365
  * InstructionsBlocks overrides
345
366
  * Key: block name, Value: block content
@@ -701,6 +722,14 @@ interface BaseLLMRequest extends Omit<LLMRequest, 'messages' | 'input' | 'reques
701
722
  * attach heavy diagnostic objects or raw provider payloads.
702
723
  */
703
724
  diagnostics?: DiagnosticsOptions;
725
+ /**
726
+ * Explicit output contract from graph-engine / ai-tasks: field names or `{ properties }`.
727
+ * Also accepted on `workingMemory.inputs.outputContract`. When absent, the gateway does not
728
+ * infer contract fields from other request shapes (`expectedSchema`, config, etc.).
729
+ */
730
+ outputContract?: string[] | {
731
+ properties: Record<string, unknown>;
732
+ };
704
733
  }
705
734
  /**
706
735
  * Chat request for conversational use cases
@@ -932,6 +961,12 @@ export interface EnhancedLLMResponse<TContent = unknown> extends Omit<AIResponse
932
961
  * Cost in USD (if available)
933
962
  */
934
963
  cost?: number;
964
+ /**
965
+ * Billing state for Run Analysis / Activix when usage is recorded.
966
+ * - `priced`: {@link cost} / {@link costUsd} is a finite number from the router
967
+ * - `unpriced`: usage exists but no price table / adapter cost was returned
968
+ */
969
+ costStatus?: 'priced' | 'unpriced';
935
970
  /**
936
971
  * Cost in USD (preferred stable key when the router exposes it).
937
972
  * When both are present, costUsd should mirror cost.