@x12i/ai-gateway 9.2.0 → 9.3.4
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 +105 -13
- package/dist/activity-manager.d.ts +1 -0
- package/dist/activity-manager.js +123 -26
- package/dist/ai-tools-client.d.ts +20 -0
- package/dist/ai-tools-client.js +91 -0
- package/dist/gateway-config.d.ts +2 -0
- package/dist/gateway-config.js +2 -1
- package/dist/gateway-mode.d.ts +40 -0
- package/dist/gateway-mode.js +75 -0
- package/dist/gateway-utils.d.ts +28 -1
- package/dist/gateway-utils.js +137 -12
- package/dist/gateway.d.ts +3 -0
- package/dist/gateway.js +34 -6
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -1
- package/dist/types.d.ts +21 -0
- package/dist-cjs/activity-manager.cjs +137 -45
- package/dist-cjs/activity-manager.d.ts +1 -0
- package/dist-cjs/ai-tools-client.cjs +91 -0
- package/dist-cjs/ai-tools-client.d.ts +20 -0
- package/dist-cjs/config/activity-tracking-config.cjs +1 -4
- package/dist-cjs/content-normalizer/content-normalizer.cjs +3 -8
- package/dist-cjs/content-normalizer/index.cjs +1 -7
- package/dist-cjs/content-normalizer/types.cjs +1 -2
- package/dist-cjs/flex-md-loader.cjs +20 -67
- package/dist-cjs/gateway-config.cjs +25 -63
- package/dist-cjs/gateway-config.d.ts +2 -0
- package/dist-cjs/gateway-conversion.cjs +10 -48
- package/dist-cjs/gateway-instructions.cjs +5 -10
- package/dist-cjs/gateway-log-meta.cjs +9 -14
- package/dist-cjs/gateway-memory.cjs +2 -6
- package/dist-cjs/gateway-messages.cjs +3 -6
- package/dist-cjs/gateway-meta.cjs +1 -4
- package/dist-cjs/gateway-mode.cjs +75 -0
- package/dist-cjs/gateway-mode.d.ts +40 -0
- package/dist-cjs/gateway-provider-auto-register.cjs +2 -38
- package/dist-cjs/gateway-provider.cjs +10 -22
- package/dist-cjs/gateway-rate-limiter-constants.cjs +2 -5
- package/dist-cjs/gateway-rate-limiter.cjs +5 -9
- package/dist-cjs/gateway-retry.cjs +6 -14
- package/dist-cjs/gateway-utils.cjs +160 -89
- package/dist-cjs/gateway-utils.d.ts +28 -1
- package/dist-cjs/gateway-validation.cjs +2 -6
- package/dist-cjs/gateway.cjs +91 -67
- package/dist-cjs/gateway.d.ts +3 -0
- package/dist-cjs/index.cjs +22 -98
- package/dist-cjs/index.d.ts +3 -1
- package/dist-cjs/instruction-errors.cjs +2 -7
- package/dist-cjs/instruction-optimizer.cjs +4 -10
- package/dist-cjs/instructions-parser.cjs +5 -10
- package/dist-cjs/logger-factory.cjs +3 -6
- package/dist-cjs/memory-path-resolution.cjs +8 -18
- package/dist-cjs/message-builder.cjs +11 -47
- package/dist-cjs/object-types-library-integration.cjs +3 -8
- package/dist-cjs/object-types-library.cjs +5 -10
- package/dist-cjs/output-auditor.cjs +1 -4
- package/dist-cjs/output-contract-normalizer.cjs +9 -14
- package/dist-cjs/request-report-generator.cjs +1 -4
- package/dist-cjs/response-analyzer/format-type-detector.cjs +1 -5
- package/dist-cjs/response-analyzer/index.cjs +3 -9
- package/dist-cjs/response-analyzer/object-type-detector.cjs +1 -5
- package/dist-cjs/response-analyzer/response-analyzer.cjs +6 -10
- package/dist-cjs/response-analyzer/types.cjs +1 -2
- package/dist-cjs/response-fallback-fixer.cjs +1 -4
- package/dist-cjs/runtime-objects.cjs +7 -13
- package/dist-cjs/template-parser.cjs +5 -42
- package/dist-cjs/template-render-merge.cjs +2 -6
- package/dist-cjs/troubleshooting-helper.cjs +13 -28
- package/dist-cjs/types.cjs +1 -2
- package/dist-cjs/types.d.ts +21 -0
- package/dist-cjs/usage-tracker.cjs +3 -7
- package/package.json +11 -5
|
@@ -1,15 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Instruction Optimizer Feature
|
|
4
3
|
*
|
|
5
4
|
* Uses AI to analyze and fix poorly-written instructions.
|
|
6
5
|
* This is a meta-feature that uses the AI Gateway (via router) to improve AI instructions.
|
|
7
6
|
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.optimizeInstructions = optimizeInstructions;
|
|
10
|
-
exports.getFixedInstructions = getFixedInstructions;
|
|
11
|
-
exports.getChangesSummary = getChangesSummary;
|
|
12
|
-
exports.validateTokenPreservation = validateTokenPreservation;
|
|
13
7
|
/**
|
|
14
8
|
* The meta-instructions used to fix other instructions
|
|
15
9
|
* Loaded from instructions-audit.md
|
|
@@ -111,7 +105,7 @@ If the instructions are already well-written, explain that in the analysis and m
|
|
|
111
105
|
* @param options - Optimization options
|
|
112
106
|
* @returns Optimization result with fixed instructions and analysis
|
|
113
107
|
*/
|
|
114
|
-
async function optimizeInstructions(gateway, originalInstructions, options) {
|
|
108
|
+
export async function optimizeInstructions(gateway, originalInstructions, options) {
|
|
115
109
|
// Get internal system action config (instruction optimization)
|
|
116
110
|
const internalConfig = gateway.config?.internalSystemActions?.instructionOptimization;
|
|
117
111
|
const defaultEngine = gateway.config?.defaultEngine || 'openai';
|
|
@@ -257,7 +251,7 @@ async function optimizeInstructions(gateway, originalInstructions, options) {
|
|
|
257
251
|
* @param result - Optimization result
|
|
258
252
|
* @returns Just the complete fixed instructions text
|
|
259
253
|
*/
|
|
260
|
-
function getFixedInstructions(result) {
|
|
254
|
+
export function getFixedInstructions(result) {
|
|
261
255
|
return result.fixed_instructions.complete_text;
|
|
262
256
|
}
|
|
263
257
|
/**
|
|
@@ -266,7 +260,7 @@ function getFixedInstructions(result) {
|
|
|
266
260
|
* @param result - Optimization result
|
|
267
261
|
* @returns Human-readable summary of changes
|
|
268
262
|
*/
|
|
269
|
-
function getChangesSummary(result) {
|
|
263
|
+
export function getChangesSummary(result) {
|
|
270
264
|
const summary = [
|
|
271
265
|
`Quality improved from ${result.quality_improvements.before_score}/10 to ${result.quality_improvements.after_score}/10`,
|
|
272
266
|
`\n${result.quality_improvements.summary}`,
|
|
@@ -286,7 +280,7 @@ function getChangesSummary(result) {
|
|
|
286
280
|
* @param result - Optimization result
|
|
287
281
|
* @returns Object with validation status and any missing tokens
|
|
288
282
|
*/
|
|
289
|
-
function validateTokenPreservation(originalInstructions, result) {
|
|
283
|
+
export function validateTokenPreservation(originalInstructions, result) {
|
|
290
284
|
// Extract tokens from original (matches {{anything}})
|
|
291
285
|
const tokenRegex = /\{\{([^}]+)\}\}/g;
|
|
292
286
|
const originalTokens = new Set();
|
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Instructions Parser Module
|
|
4
3
|
*
|
|
5
4
|
* Handles template rendering for instruction content using Rendrix (@x12i/rendrix).
|
|
6
5
|
* Separated from instruction resolution to keep concerns clean.
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
exports.renderInstructionTemplate = renderInstructionTemplate;
|
|
10
|
-
exports.hasTemplateVariables = hasTemplateVariables;
|
|
11
|
-
exports.extractTemplateVariables = extractTemplateVariables;
|
|
12
|
-
const template_parser_js_1 = require("./template-parser.cjs");
|
|
7
|
+
import { parseTemplate } from './template-parser.js';
|
|
13
8
|
/**
|
|
14
9
|
* Renders instruction templates with variables using Rendrix (@x12i/rendrix)
|
|
15
10
|
*/
|
|
16
|
-
async function renderInstructionTemplate(template, variables = {}, templateRenderOptions, logger) {
|
|
11
|
+
export async function renderInstructionTemplate(template, variables = {}, templateRenderOptions, logger) {
|
|
17
12
|
const startTime = Date.now();
|
|
18
13
|
// Extract memory contexts for Rendrix
|
|
19
14
|
// parseTemplate expects: (template, workingMemory, shortTermMemory?, experienceMemory?, knowledgeMemory?)
|
|
@@ -23,7 +18,7 @@ async function renderInstructionTemplate(template, variables = {}, templateRende
|
|
|
23
18
|
const knowledgeMemory = variables.knowledgeMemory || {};
|
|
24
19
|
// Render template using Rendrix
|
|
25
20
|
// Pass memory contexts as separate parameters, not as a combined object
|
|
26
|
-
const renderedText = await
|
|
21
|
+
const renderedText = await parseTemplate(template, workingMemory, undefined, // taskConfig removed - no longer used
|
|
27
22
|
shortTermMemory, experienceMemory, knowledgeMemory, templateRenderOptions, logger);
|
|
28
23
|
// Combine all memory contexts for return value (for metadata)
|
|
29
24
|
const combinedVariables = {
|
|
@@ -43,14 +38,14 @@ async function renderInstructionTemplate(template, variables = {}, templateRende
|
|
|
43
38
|
/**
|
|
44
39
|
* Checks if a template contains variables that need rendering
|
|
45
40
|
*/
|
|
46
|
-
function hasTemplateVariables(template) {
|
|
41
|
+
export function hasTemplateVariables(template) {
|
|
47
42
|
// Check for double-brace variables like {{variable}}
|
|
48
43
|
return /\{\{[^}]+\}\}/g.test(template);
|
|
49
44
|
}
|
|
50
45
|
/**
|
|
51
46
|
* Extracts variable names from a template
|
|
52
47
|
*/
|
|
53
|
-
function extractTemplateVariables(template) {
|
|
48
|
+
export function extractTemplateVariables(template) {
|
|
54
49
|
const matches = template.match(/\{\{([^}]+)\}\}/g);
|
|
55
50
|
if (!matches)
|
|
56
51
|
return [];
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Logger Factory
|
|
4
3
|
*
|
|
5
4
|
* Creates and configures logxer instances for the gateway
|
|
6
5
|
*/
|
|
7
|
-
|
|
8
|
-
exports.createGatewayLogger = createGatewayLogger;
|
|
9
|
-
const logxer_1 = require("@x12i/logxer");
|
|
6
|
+
import { createLogxer } from '@x12i/logxer';
|
|
10
7
|
function resolveDefaultRuntimeIdentity(packageName) {
|
|
11
8
|
const service = process.env.AI_GATEWAY_LOG_SERVICE ?? packageName ?? 'AI_GATEWAY';
|
|
12
9
|
if (!service)
|
|
@@ -23,14 +20,14 @@ function resolveDefaultRuntimeIdentity(packageName) {
|
|
|
23
20
|
* @param config - Logger configuration
|
|
24
21
|
* @returns Configured logger or no-op logger if logging is disabled
|
|
25
22
|
*/
|
|
26
|
-
function createGatewayLogger(config) {
|
|
23
|
+
export function createGatewayLogger(config) {
|
|
27
24
|
if (config.customLogger) {
|
|
28
25
|
return config.customLogger;
|
|
29
26
|
}
|
|
30
27
|
if (!config.enableLogging) {
|
|
31
28
|
return createNoOpLogger();
|
|
32
29
|
}
|
|
33
|
-
return
|
|
30
|
+
return createLogxer({
|
|
34
31
|
packageName: config.packageName || 'AI_GATEWAY',
|
|
35
32
|
envPrefix: config.packageName || 'AI_GATEWAY',
|
|
36
33
|
debugNamespace: 'ai-gateway'
|
|
@@ -1,25 +1,15 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Dual memory roots `input` (merged MAIN payload) and `inputs` (caller / graph-entry bag).
|
|
4
3
|
* Aligned with @exellix/graph-engine ≥ 5.5.0 resolution rules for template / smart-input paths.
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
exports.GATEWAY_DUAL_MEMORY_ROOTS = void 0;
|
|
8
|
-
exports.parseLooseJsonObject = parseLooseJsonObject;
|
|
9
|
-
exports.extractCallerInputsBag = extractCallerInputsBag;
|
|
10
|
-
exports.coalesceMergedInputBucket = coalesceMergedInputBucket;
|
|
11
|
-
exports.resolveGatewayMemoryPathValue = resolveGatewayMemoryPathValue;
|
|
12
|
-
exports.buildMemoryResolutionRootFromWorkingMemory = buildMemoryResolutionRootFromWorkingMemory;
|
|
13
|
-
exports.prepareWorkingMemoryForTemplateRender = prepareWorkingMemoryForTemplateRender;
|
|
14
|
-
exports.mapSmartInputPathsInputsToInput = mapSmartInputPathsInputsToInput;
|
|
15
|
-
exports.GATEWAY_DUAL_MEMORY_ROOTS = ['input', 'inputs'];
|
|
5
|
+
export const GATEWAY_DUAL_MEMORY_ROOTS = ['input', 'inputs'];
|
|
16
6
|
function isPlainObject(value) {
|
|
17
7
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
18
8
|
}
|
|
19
9
|
/**
|
|
20
10
|
* Parse JSON object shapes from strings (matches graph-engine `parseLooseJsonObject`).
|
|
21
11
|
*/
|
|
22
|
-
function parseLooseJsonObject(value) {
|
|
12
|
+
export function parseLooseJsonObject(value) {
|
|
23
13
|
if (isPlainObject(value))
|
|
24
14
|
return value;
|
|
25
15
|
if (typeof value !== 'string')
|
|
@@ -36,14 +26,14 @@ function parseLooseJsonObject(value) {
|
|
|
36
26
|
}
|
|
37
27
|
}
|
|
38
28
|
/** Caller / graph-entry bag from `workingMemory.inputs`. */
|
|
39
|
-
function extractCallerInputsBag(workingMemory) {
|
|
29
|
+
export function extractCallerInputsBag(workingMemory) {
|
|
40
30
|
if (!isPlainObject(workingMemory))
|
|
41
31
|
return undefined;
|
|
42
32
|
const inputs = workingMemory.inputs;
|
|
43
33
|
return isPlainObject(inputs) ? inputs : undefined;
|
|
44
34
|
}
|
|
45
35
|
/** Merged MAIN bucket from `workingMemory.input` (object or parsed JSON string). */
|
|
46
|
-
function coalesceMergedInputBucket(workingMemory) {
|
|
36
|
+
export function coalesceMergedInputBucket(workingMemory) {
|
|
47
37
|
if (!isPlainObject(workingMemory))
|
|
48
38
|
return undefined;
|
|
49
39
|
const raw = workingMemory.input;
|
|
@@ -107,7 +97,7 @@ function mergeBucketViews(primary, fallback) {
|
|
|
107
97
|
* - `input` / `input.*` → merged `input` first, then caller `inputs`
|
|
108
98
|
* - other paths → direct lookup on working memory
|
|
109
99
|
*/
|
|
110
|
-
function resolveGatewayMemoryPathValue(workingMemory, path) {
|
|
100
|
+
export function resolveGatewayMemoryPathValue(workingMemory, path) {
|
|
111
101
|
const trimmed = path.trim();
|
|
112
102
|
if (!trimmed)
|
|
113
103
|
return undefined;
|
|
@@ -146,7 +136,7 @@ function resolveGatewayMemoryPathValue(workingMemory, path) {
|
|
|
146
136
|
* Working-memory view for Rendrix / smart-input: preserves all keys but overlays
|
|
147
137
|
* `input` and `inputs` with dual-root merge views (does not rewrite authored paths).
|
|
148
138
|
*/
|
|
149
|
-
function buildMemoryResolutionRootFromWorkingMemory(workingMemory) {
|
|
139
|
+
export function buildMemoryResolutionRootFromWorkingMemory(workingMemory) {
|
|
150
140
|
if (!isPlainObject(workingMemory))
|
|
151
141
|
return {};
|
|
152
142
|
const inputsBag = extractCallerInputsBag(workingMemory);
|
|
@@ -161,7 +151,7 @@ function buildMemoryResolutionRootFromWorkingMemory(workingMemory) {
|
|
|
161
151
|
* When WM carries `input` and/or `inputs`, return a resolution root for template rendering.
|
|
162
152
|
* Otherwise returns the original reference unchanged.
|
|
163
153
|
*/
|
|
164
|
-
function prepareWorkingMemoryForTemplateRender(workingMemory) {
|
|
154
|
+
export function prepareWorkingMemoryForTemplateRender(workingMemory) {
|
|
165
155
|
if (!isPlainObject(workingMemory))
|
|
166
156
|
return workingMemory;
|
|
167
157
|
if (workingMemory.input === undefined && workingMemory.inputs === undefined) {
|
|
@@ -170,7 +160,7 @@ function prepareWorkingMemoryForTemplateRender(workingMemory) {
|
|
|
170
160
|
return buildMemoryResolutionRootFromWorkingMemory(workingMemory);
|
|
171
161
|
}
|
|
172
162
|
/** Optional migration: rewrite `inputs.*` smart-input / memory paths to `input.*`. */
|
|
173
|
-
function mapSmartInputPathsInputsToInput(paths) {
|
|
163
|
+
export function mapSmartInputPathsInputsToInput(paths) {
|
|
174
164
|
return paths.map((p) => {
|
|
175
165
|
const t = p.trim();
|
|
176
166
|
if (t === 'inputs')
|
|
@@ -1,47 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Message Builder
|
|
4
3
|
* Direct message construction without request-builder package dependency
|
|
5
4
|
* Handles flex-md format exclusively
|
|
6
5
|
*/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
11
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
12
|
-
}
|
|
13
|
-
Object.defineProperty(o, k2, desc);
|
|
14
|
-
}) : (function(o, m, k, k2) {
|
|
15
|
-
if (k2 === undefined) k2 = k;
|
|
16
|
-
o[k2] = m[k];
|
|
17
|
-
}));
|
|
18
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
19
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
20
|
-
}) : function(o, v) {
|
|
21
|
-
o["default"] = v;
|
|
22
|
-
});
|
|
23
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
24
|
-
var ownKeys = function(o) {
|
|
25
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
26
|
-
var ar = [];
|
|
27
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
28
|
-
return ar;
|
|
29
|
-
};
|
|
30
|
-
return ownKeys(o);
|
|
31
|
-
};
|
|
32
|
-
return function (mod) {
|
|
33
|
-
if (mod && mod.__esModule) return mod;
|
|
34
|
-
var result = {};
|
|
35
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
36
|
-
__setModuleDefault(result, mod);
|
|
37
|
-
return result;
|
|
38
|
-
};
|
|
39
|
-
})();
|
|
40
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
|
-
exports.buildMessages = buildMessages;
|
|
42
|
-
const template_parser_js_1 = require("./template-parser.cjs");
|
|
43
|
-
const template_render_merge_js_1 = require("./template-render-merge.cjs");
|
|
44
|
-
const gateway_instructions_js_1 = require("./gateway-instructions.cjs");
|
|
6
|
+
import { parseTemplate } from './template-parser.js';
|
|
7
|
+
import { mergeGatewayAndRequestTemplateRenderOptions } from './template-render-merge.js';
|
|
8
|
+
import { resolveNestedInstructionsBlock } from './gateway-instructions.js';
|
|
45
9
|
// Type guard
|
|
46
10
|
// AIRequest is distinguished by having primaryObjectType or objectTypes
|
|
47
11
|
// ChatRequest does not have these fields
|
|
@@ -95,7 +59,7 @@ async function buildInputRecognitionRules(request, config, options) {
|
|
|
95
59
|
}
|
|
96
60
|
// If direct access didn't work, try merged inline overrides / nested defaults
|
|
97
61
|
if (!rule) {
|
|
98
|
-
rule = await
|
|
62
|
+
rule = await resolveNestedInstructionsBlock(rulePath, request.agentId || '', request.taskTypeId, blockContext);
|
|
99
63
|
}
|
|
100
64
|
if (rule && typeof rule === 'string' && rule.trim() !== '') {
|
|
101
65
|
rules.push(rule);
|
|
@@ -157,7 +121,7 @@ async function buildReinforcementRules(request, config, options) {
|
|
|
157
121
|
}
|
|
158
122
|
// If direct access didn't work, try merged inline overrides / nested defaults
|
|
159
123
|
if (!rule) {
|
|
160
|
-
rule = await
|
|
124
|
+
rule = await resolveNestedInstructionsBlock(rulePath, request.agentId || '', request.taskTypeId, blockContext);
|
|
161
125
|
}
|
|
162
126
|
if (rule && typeof rule === 'string' && rule.trim() !== '') {
|
|
163
127
|
rules.push(rule);
|
|
@@ -238,7 +202,7 @@ async function buildUserMessage(request, config, shortTermMemory, experienceMemo
|
|
|
238
202
|
});
|
|
239
203
|
throw err;
|
|
240
204
|
}
|
|
241
|
-
const parsedPrompt = await
|
|
205
|
+
const parsedPrompt = await parseTemplate(request.prompt, request.workingMemory, undefined, shortTermMemory, experienceMemory, knowledgeMemory, templateRenderOptions, logger);
|
|
242
206
|
if (!parsedPrompt || parsedPrompt.trim() === '') {
|
|
243
207
|
const workingMemoryObj = request.workingMemory;
|
|
244
208
|
const err = new Error(`Prompt template rendered to empty string. This may indicate missing template variables or empty template content.`);
|
|
@@ -354,7 +318,7 @@ async function hasFlexMdContract(instructionsText, complianceLevel = 'L0') {
|
|
|
354
318
|
}
|
|
355
319
|
try {
|
|
356
320
|
// Try to use flex-md SDK to validate compliance
|
|
357
|
-
const { loadFlexMd } = await
|
|
321
|
+
const { loadFlexMd } = await import('./flex-md-loader.js');
|
|
358
322
|
const flexMd = await loadFlexMd();
|
|
359
323
|
// Check if validateMarkdownAgainstOfs is available
|
|
360
324
|
if (flexMd.validateMarkdownAgainstOfs && typeof flexMd.validateMarkdownAgainstOfs === 'function') {
|
|
@@ -404,7 +368,7 @@ async function hasFlexMdContract(instructionsText, complianceLevel = 'L0') {
|
|
|
404
368
|
/**
|
|
405
369
|
* Main function to build messages
|
|
406
370
|
*/
|
|
407
|
-
async function buildMessages(request, config, options = {}) {
|
|
371
|
+
export async function buildMessages(request, config, options = {}) {
|
|
408
372
|
const { useSystemContextFallback = true, includeInputRecognition = true, includeReinforcement = true, parsedSnapshot } = options;
|
|
409
373
|
const { logger } = config;
|
|
410
374
|
const messages = [];
|
|
@@ -415,7 +379,7 @@ async function buildMessages(request, config, options = {}) {
|
|
|
415
379
|
const shortTermMemory = options.shortTermMemory;
|
|
416
380
|
const experienceMemory = options.experienceMemory;
|
|
417
381
|
const knowledgeMemory = options.knowledgeMemory;
|
|
418
|
-
const templateRenderOptions =
|
|
382
|
+
const templateRenderOptions = mergeGatewayAndRequestTemplateRenderOptions(config.templateRendering, request);
|
|
419
383
|
if (request.instructions) {
|
|
420
384
|
if (typeof request.instructions === 'string') {
|
|
421
385
|
logger.info('Using instructions as template text', {
|
|
@@ -436,7 +400,7 @@ async function buildMessages(request, config, options = {}) {
|
|
|
436
400
|
// Step 2: Parse instructions template with full memory context
|
|
437
401
|
// Rendrix handles token resolution, so we just parse directly
|
|
438
402
|
if (instructionsText) {
|
|
439
|
-
instructionsText = await
|
|
403
|
+
instructionsText = await parseTemplate(instructionsText, request.workingMemory, undefined, // taskConfig removed - no longer used
|
|
440
404
|
shortTermMemory, experienceMemory, knowledgeMemory, templateRenderOptions, logger);
|
|
441
405
|
}
|
|
442
406
|
// Step 4: Add input recognition rules
|
|
@@ -485,7 +449,7 @@ async function buildMessages(request, config, options = {}) {
|
|
|
485
449
|
// Step 7: Add context (if provided) with full memory context
|
|
486
450
|
if (request.context) {
|
|
487
451
|
const contextText = typeof request.context === 'string' ? request.context : JSON.stringify(request.context);
|
|
488
|
-
const parsedContext = await
|
|
452
|
+
const parsedContext = await parseTemplate(contextText, request.workingMemory, undefined, // taskConfig removed - no longer used
|
|
489
453
|
shortTermMemory, experienceMemory, knowledgeMemory, templateRenderOptions, logger);
|
|
490
454
|
if (parsedContext && parsedContext.trim() !== '') {
|
|
491
455
|
messages.push({
|
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Object Types Library Integration - REMOVED
|
|
4
3
|
*
|
|
5
4
|
* The @x12i/outputs-library dependency has been removed.
|
|
6
5
|
* This file now provides stub functions for backward compatibility.
|
|
7
6
|
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.initializeObjectTypesLibrary = initializeObjectTypesLibrary;
|
|
10
|
-
exports.getObjectTypesLibrary = getObjectTypesLibrary;
|
|
11
|
-
exports.resetObjectTypesLibrary = resetObjectTypesLibrary;
|
|
12
7
|
/**
|
|
13
8
|
* Initialize ObjectTypesLibrary singleton - STUB
|
|
14
9
|
* Since ObjectTypesLibrary is removed, this function does nothing
|
|
15
10
|
*/
|
|
16
|
-
async function initializeObjectTypesLibrary(config, logger) {
|
|
11
|
+
export async function initializeObjectTypesLibrary(config, logger) {
|
|
17
12
|
logger.info('ObjectTypesLibrary dependency removed - using simplified object type handling');
|
|
18
13
|
return null;
|
|
19
14
|
}
|
|
@@ -21,12 +16,12 @@ async function initializeObjectTypesLibrary(config, logger) {
|
|
|
21
16
|
* Get the current ObjectTypesLibrary instance - STUB
|
|
22
17
|
* Always returns null since the library is removed
|
|
23
18
|
*/
|
|
24
|
-
function getObjectTypesLibrary() {
|
|
19
|
+
export function getObjectTypesLibrary() {
|
|
25
20
|
return null;
|
|
26
21
|
}
|
|
27
22
|
/**
|
|
28
23
|
* Reset the library instance (for testing) - STUB
|
|
29
24
|
*/
|
|
30
|
-
function resetObjectTypesLibrary() {
|
|
25
|
+
export function resetObjectTypesLibrary() {
|
|
31
26
|
// Nothing to reset since library is removed
|
|
32
27
|
}
|
|
@@ -1,15 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Object Types Library
|
|
4
3
|
*
|
|
5
4
|
* Standard object type definitions for AI Gateway structured outputs.
|
|
6
5
|
* Each agent type has corresponding object types with JSON schemas.
|
|
7
6
|
*/
|
|
8
|
-
|
|
9
|
-
exports.OBJECT_TYPES_LIBRARY = void 0;
|
|
10
|
-
exports.getObjectType = getObjectType;
|
|
11
|
-
exports.getObjectTypesForAgent = getObjectTypesForAgent;
|
|
12
|
-
exports.OBJECT_TYPES_LIBRARY = {
|
|
7
|
+
export const OBJECT_TYPES_LIBRARY = {
|
|
13
8
|
/**
|
|
14
9
|
* AI Reasoner - General reasoning and analysis
|
|
15
10
|
*/
|
|
@@ -194,13 +189,13 @@ exports.OBJECT_TYPES_LIBRARY = {
|
|
|
194
189
|
/**
|
|
195
190
|
* Get object type definition by name
|
|
196
191
|
*/
|
|
197
|
-
function getObjectType(typeName) {
|
|
198
|
-
return
|
|
192
|
+
export function getObjectType(typeName) {
|
|
193
|
+
return OBJECT_TYPES_LIBRARY[typeName];
|
|
199
194
|
}
|
|
200
195
|
/**
|
|
201
196
|
* Get all object types for a specific agent type
|
|
202
197
|
*/
|
|
203
|
-
function getObjectTypesForAgent(agentType) {
|
|
198
|
+
export function getObjectTypesForAgent(agentType) {
|
|
204
199
|
const mapping = {
|
|
205
200
|
'ai-reasoner': ['reasoning-result', 'classification', 'question-answer'],
|
|
206
201
|
'ai-auditor': ['audit-report'],
|
|
@@ -211,5 +206,5 @@ function getObjectTypesForAgent(agentType) {
|
|
|
211
206
|
'chat': [] // Chat doesn't use structured outputs
|
|
212
207
|
};
|
|
213
208
|
const typeNames = mapping[agentType] || [];
|
|
214
|
-
return typeNames.map(name =>
|
|
209
|
+
return typeNames.map(name => OBJECT_TYPES_LIBRARY[name]);
|
|
215
210
|
}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Output Structure Auditor
|
|
4
3
|
*
|
|
5
4
|
* Audits the structure of parsed output against the expected schema.
|
|
6
5
|
* Provides detailed analysis of what fields are present, missing, or extra.
|
|
7
6
|
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.auditOutputStructure = auditOutputStructure;
|
|
10
7
|
/**
|
|
11
8
|
* Audits the structure of parsed output against the expected schema
|
|
12
9
|
*
|
|
@@ -14,7 +11,7 @@ exports.auditOutputStructure = auditOutputStructure;
|
|
|
14
11
|
* @param schema - The JSON schema to compare against
|
|
15
12
|
* @returns Audit result with detailed structure analysis
|
|
16
13
|
*/
|
|
17
|
-
function auditOutputStructure(parsedOutput, schema) {
|
|
14
|
+
export function auditOutputStructure(parsedOutput, schema) {
|
|
18
15
|
// Only audit if we have both parsedOutput and schema
|
|
19
16
|
if (!parsedOutput || !schema) {
|
|
20
17
|
return undefined;
|
|
@@ -1,14 +1,9 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Normalizes invoke responses into `output.parsed` when an explicit output contract is forwarded.
|
|
4
3
|
* Does not infer contracts from other request fields — that is upstream (graph-engine / ai-tasks).
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
exports.resolveOutputContractFieldKeys = resolveOutputContractFieldKeys;
|
|
9
|
-
exports.enrichParsedContentForOutputContract = enrichParsedContentForOutputContract;
|
|
10
|
-
const flex_md_loader_js_1 = require("./flex-md-loader.cjs");
|
|
11
|
-
const memory_path_resolution_js_1 = require("./memory-path-resolution.cjs");
|
|
5
|
+
import { parseMarkdownSectionsFromContent } from './flex-md-loader.js';
|
|
6
|
+
import { coalesceMergedInputBucket, extractCallerInputsBag, parseLooseJsonObject } from './memory-path-resolution.js';
|
|
12
7
|
function isPlainObject(value) {
|
|
13
8
|
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
14
9
|
}
|
|
@@ -26,7 +21,7 @@ function hasMeaningfulContractValue(value) {
|
|
|
26
21
|
/**
|
|
27
22
|
* Maps an explicit contract spec to field keys. No inference from descriptions or other request fields.
|
|
28
23
|
*/
|
|
29
|
-
function contractSpecToFieldKeys(contract) {
|
|
24
|
+
export function contractSpecToFieldKeys(contract) {
|
|
30
25
|
if (contract == null)
|
|
31
26
|
return [];
|
|
32
27
|
if (Array.isArray(contract)) {
|
|
@@ -44,14 +39,14 @@ function contractSpecToFieldKeys(contract) {
|
|
|
44
39
|
function readExplicitOutputContractFromWorkingMemory(workingMemory) {
|
|
45
40
|
if (!isPlainObject(workingMemory))
|
|
46
41
|
return undefined;
|
|
47
|
-
const inputs =
|
|
42
|
+
const inputs = extractCallerInputsBag(workingMemory);
|
|
48
43
|
if (inputs?.outputContract !== undefined)
|
|
49
44
|
return inputs.outputContract;
|
|
50
|
-
const input =
|
|
45
|
+
const input = coalesceMergedInputBucket(workingMemory);
|
|
51
46
|
if (isPlainObject(input) && input.outputContract !== undefined) {
|
|
52
47
|
return input.outputContract;
|
|
53
48
|
}
|
|
54
|
-
const loose =
|
|
49
|
+
const loose = parseLooseJsonObject(workingMemory.input);
|
|
55
50
|
if (loose?.outputContract !== undefined)
|
|
56
51
|
return loose.outputContract;
|
|
57
52
|
return undefined;
|
|
@@ -59,7 +54,7 @@ function readExplicitOutputContractFromWorkingMemory(workingMemory) {
|
|
|
59
54
|
/**
|
|
60
55
|
* Resolves field keys only from explicit `outputContract` on the request or graph-forwarded inputs.
|
|
61
56
|
*/
|
|
62
|
-
function resolveOutputContractFieldKeys(request) {
|
|
57
|
+
export function resolveOutputContractFieldKeys(request) {
|
|
63
58
|
if (request == null || typeof request !== 'object')
|
|
64
59
|
return undefined;
|
|
65
60
|
const r = request;
|
|
@@ -100,7 +95,7 @@ function pickAliasValue(source, key) {
|
|
|
100
95
|
/**
|
|
101
96
|
* Fills missing contract keys from markdown sections after flex-md. Only runs when explicit contract keys were supplied.
|
|
102
97
|
*/
|
|
103
|
-
async function enrichParsedContentForOutputContract(parsed, rawContent, contractKeys, logger) {
|
|
98
|
+
export async function enrichParsedContentForOutputContract(parsed, rawContent, contractKeys, logger) {
|
|
104
99
|
const base = asParsedRecord(parsed);
|
|
105
100
|
if (!contractKeys?.length)
|
|
106
101
|
return base;
|
|
@@ -114,7 +109,7 @@ async function enrichParsedContentForOutputContract(parsed, rawContent, contract
|
|
|
114
109
|
: '';
|
|
115
110
|
if (!content.trim())
|
|
116
111
|
return base;
|
|
117
|
-
const fromMarkdown =
|
|
112
|
+
const fromMarkdown = parseMarkdownSectionsFromContent(content, logger);
|
|
118
113
|
const merged = { ...base };
|
|
119
114
|
for (const key of missing) {
|
|
120
115
|
const value = pickAliasValue(fromMarkdown, key);
|
|
@@ -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
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
9
|
-
|
|
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
|
|
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
|
|
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
|
|
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;
|