@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,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* @x12i/flex-md Loader
|
|
4
3
|
*
|
|
@@ -14,55 +13,9 @@
|
|
|
14
13
|
* - Native output format support: Extracts and uses flex-md native output formats from instructions
|
|
15
14
|
* based on compliance level when no explicit format is provided
|
|
16
15
|
*/
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
-
}
|
|
23
|
-
Object.defineProperty(o, k2, desc);
|
|
24
|
-
}) : (function(o, m, k, k2) {
|
|
25
|
-
if (k2 === undefined) k2 = k;
|
|
26
|
-
o[k2] = m[k];
|
|
27
|
-
}));
|
|
28
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
29
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
-
}) : function(o, v) {
|
|
31
|
-
o["default"] = v;
|
|
32
|
-
});
|
|
33
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
34
|
-
var ownKeys = function(o) {
|
|
35
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
36
|
-
var ar = [];
|
|
37
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
38
|
-
return ar;
|
|
39
|
-
};
|
|
40
|
-
return ownKeys(o);
|
|
41
|
-
};
|
|
42
|
-
return function (mod) {
|
|
43
|
-
if (mod && mod.__esModule) return mod;
|
|
44
|
-
var result = {};
|
|
45
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
46
|
-
__setModuleDefault(result, mod);
|
|
47
|
-
return result;
|
|
48
|
-
};
|
|
49
|
-
})();
|
|
50
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
exports.loadFlexMd = loadFlexMd;
|
|
52
|
-
exports.extractJsonFromFlexMd = extractJsonFromFlexMd;
|
|
53
|
-
exports.parseMarkdownSectionsFromContent = parseMarkdownSectionsFromContent;
|
|
54
|
-
exports.isFlexMdAvailable = isFlexMdAvailable;
|
|
55
|
-
exports.getModelMaxTokensFromFlexMd = getModelMaxTokensFromFlexMd;
|
|
56
|
-
exports.getFlexMdModule = getFlexMdModule;
|
|
57
|
-
exports.buildDynamicInstructions = buildDynamicInstructions;
|
|
58
|
-
exports.enrichInstructionsWithFlexMd = enrichInstructionsWithFlexMd;
|
|
59
|
-
exports.getStrictnessDefaults = getStrictnessDefaults;
|
|
60
|
-
exports.enforceFlexMdFormat = enforceFlexMdFormat;
|
|
61
|
-
exports.hasFlexMdContract = hasFlexMdContract;
|
|
62
|
-
exports.detectComplianceLevel = detectComplianceLevel;
|
|
63
|
-
const path = __importStar(require("path"));
|
|
64
|
-
const url_1 = require("url");
|
|
65
|
-
const logxer_1 = require("@x12i/logxer");
|
|
16
|
+
import * as path from 'path';
|
|
17
|
+
import { pathToFileURL } from 'url';
|
|
18
|
+
import { DebugLogAbstract } from '@x12i/logxer';
|
|
66
19
|
let flexMdModule = null;
|
|
67
20
|
let flexMdLoadError = null;
|
|
68
21
|
let flexMdLoading = null;
|
|
@@ -79,7 +32,7 @@ async function importFlexMdModule() {
|
|
|
79
32
|
const { createRequire } = await importModule('module');
|
|
80
33
|
const requireFn = typeof require === 'function' ? require : createRequire(path.join(process.cwd(), 'noop.cjs'));
|
|
81
34
|
const cjsEntryPath = requireFn.resolve('@x12i/flex-md');
|
|
82
|
-
const cjsEntryUrl =
|
|
35
|
+
const cjsEntryUrl = pathToFileURL(cjsEntryPath).href;
|
|
83
36
|
try {
|
|
84
37
|
return await importModule(cjsEntryUrl);
|
|
85
38
|
}
|
|
@@ -97,7 +50,7 @@ async function importFlexMdModule() {
|
|
|
97
50
|
* Load flex-md module (ES Module) asynchronously
|
|
98
51
|
* Caches the result to avoid multiple imports
|
|
99
52
|
*/
|
|
100
|
-
async function loadFlexMd() {
|
|
53
|
+
export async function loadFlexMd() {
|
|
101
54
|
// Return cached module if available
|
|
102
55
|
if (flexMdModule) {
|
|
103
56
|
return flexMdModule;
|
|
@@ -376,7 +329,7 @@ async function extractFlexMdOutputFormatFromInstructions(instructions, complianc
|
|
|
376
329
|
* - Other flex-md options
|
|
377
330
|
* @returns Parsed JSON object and method used, or null if parsing fails
|
|
378
331
|
*/
|
|
379
|
-
async function extractJsonFromFlexMd(content, logger) {
|
|
332
|
+
export async function extractJsonFromFlexMd(content, logger) {
|
|
380
333
|
// ARCHITECTURE: ai-gateway layer only handles unstructured parsing
|
|
381
334
|
// Schema-driven parsing is handled at skills layer with nx-md-parser
|
|
382
335
|
// No schema parameter - this layer doesn't know about schemas
|
|
@@ -406,8 +359,8 @@ async function extractJsonFromFlexMd(content, logger) {
|
|
|
406
359
|
if (result && typeof result === 'object' && result !== null) {
|
|
407
360
|
const json = result.json || result.data || result.result || result;
|
|
408
361
|
if (json && typeof json === 'object' && json !== null) {
|
|
409
|
-
logger?.debug('extractJsonFromFlexMd: using extractFromMarkdown', { debugKind:
|
|
410
|
-
logger?.debug('extractJsonFromFlexMd: parsed json', { json, debugKind:
|
|
362
|
+
logger?.debug('extractJsonFromFlexMd: using extractFromMarkdown', { debugKind: DebugLogAbstract.TRACE });
|
|
363
|
+
logger?.debug('extractJsonFromFlexMd: parsed json', { json, debugKind: DebugLogAbstract.STATE });
|
|
411
364
|
return { json, method: 'extractFromMarkdown' };
|
|
412
365
|
}
|
|
413
366
|
}
|
|
@@ -531,11 +484,11 @@ function fallbackMarkdownParser(content, logger) {
|
|
|
531
484
|
result[key] = parseMarkdownList(currentContent);
|
|
532
485
|
detectedHeaders.push(key);
|
|
533
486
|
}
|
|
534
|
-
logger?.debug('Fallback parser detected headers', { detectedHeaders, debugKind:
|
|
487
|
+
logger?.debug('Fallback parser detected headers', { detectedHeaders, debugKind: DebugLogAbstract.STATE });
|
|
535
488
|
const expectedHeaders = ['shortAnswer', 'fullAnswer', 'assumptions', 'unknowns', 'evidence'];
|
|
536
489
|
const missingHeaders = expectedHeaders.filter(h => !detectedHeaders.includes(h));
|
|
537
490
|
if (missingHeaders.length > 0) {
|
|
538
|
-
logger?.debug('Fallback parser missing expected headers', { missingHeaders, debugKind:
|
|
491
|
+
logger?.debug('Fallback parser missing expected headers', { missingHeaders, debugKind: DebugLogAbstract.STATE });
|
|
539
492
|
}
|
|
540
493
|
// If we extracted some structure, return it
|
|
541
494
|
if (Object.keys(result).length > 0) {
|
|
@@ -554,7 +507,7 @@ function fallbackMarkdownParser(content, logger) {
|
|
|
554
507
|
* Section-based markdown → camelCase field map (e.g. `### Short Answer` → `shortAnswer`).
|
|
555
508
|
* Used when output contracts need structured `parsed` fields but flex-md returned only `rawText`.
|
|
556
509
|
*/
|
|
557
|
-
function parseMarkdownSectionsFromContent(content, logger) {
|
|
510
|
+
export function parseMarkdownSectionsFromContent(content, logger) {
|
|
558
511
|
const parsed = fallbackMarkdownParser(content, logger);
|
|
559
512
|
const json = parsed.json;
|
|
560
513
|
if (json != null && typeof json === 'object' && !Array.isArray(json)) {
|
|
@@ -597,7 +550,7 @@ function extractJsonFallback(content) {
|
|
|
597
550
|
/**
|
|
598
551
|
* Check if flex-md module is available
|
|
599
552
|
*/
|
|
600
|
-
function isFlexMdAvailable() {
|
|
553
|
+
export function isFlexMdAvailable() {
|
|
601
554
|
return flexMdModule !== null;
|
|
602
555
|
}
|
|
603
556
|
/**
|
|
@@ -608,7 +561,7 @@ function isFlexMdAvailable() {
|
|
|
608
561
|
* @param model - Model name (e.g., 'gpt-4', 'claude-3-opus')
|
|
609
562
|
* @returns maxTokens if found, null otherwise
|
|
610
563
|
*/
|
|
611
|
-
async function getModelMaxTokensFromFlexMd(provider, model) {
|
|
564
|
+
export async function getModelMaxTokensFromFlexMd(provider, model) {
|
|
612
565
|
try {
|
|
613
566
|
const flexMd = await loadFlexMd();
|
|
614
567
|
if (!flexMd || typeof flexMd !== 'object') {
|
|
@@ -687,7 +640,7 @@ async function getModelMaxTokensFromFlexMd(provider, model) {
|
|
|
687
640
|
/**
|
|
688
641
|
* Get flex-md module directly (must call loadFlexMd() first)
|
|
689
642
|
*/
|
|
690
|
-
function getFlexMdModule() {
|
|
643
|
+
export function getFlexMdModule() {
|
|
691
644
|
if (!flexMdModule) {
|
|
692
645
|
throw new Error('flex-md module not loaded. Call loadFlexMd() first.');
|
|
693
646
|
}
|
|
@@ -754,7 +707,7 @@ function generateFormatInstructionsFromSchema(schema, options) {
|
|
|
754
707
|
parts.push('\n\nIMPORTANT: Return your answer as JSON inside the markdown fenced block. The JSON must match the schema above exactly.');
|
|
755
708
|
return parts.join('\n');
|
|
756
709
|
}
|
|
757
|
-
async function buildDynamicInstructions(schemaObject, options) {
|
|
710
|
+
export async function buildDynamicInstructions(schemaObject, options) {
|
|
758
711
|
try {
|
|
759
712
|
const flexMd = await loadFlexMd();
|
|
760
713
|
// Check if dynamic instruction feature is available (flex-md 3.1+)
|
|
@@ -786,7 +739,7 @@ async function buildDynamicInstructions(schemaObject, options) {
|
|
|
786
739
|
* @param strictnessLevel - Compliance level ('L0' | 'L1' | 'L2' | 'L3')
|
|
787
740
|
* @returns Enriched instructions with flex-md guidance
|
|
788
741
|
*/
|
|
789
|
-
async function enrichInstructionsWithFlexMd(instructions, strictnessLevel = 'L0') {
|
|
742
|
+
export async function enrichInstructionsWithFlexMd(instructions, strictnessLevel = 'L0') {
|
|
790
743
|
try {
|
|
791
744
|
const flexMd = await loadFlexMd();
|
|
792
745
|
// Use native flex-md function if available (flex-md 3.2+)
|
|
@@ -814,7 +767,7 @@ async function enrichInstructionsWithFlexMd(instructions, strictnessLevel = 'L0'
|
|
|
814
767
|
* @param strictnessLevel - Compliance level ('L0' | 'L1' | 'L2' | 'L3')
|
|
815
768
|
* @returns Configuration object with level-specific defaults
|
|
816
769
|
*/
|
|
817
|
-
async function getStrictnessDefaults(strictnessLevel = 'L0') {
|
|
770
|
+
export async function getStrictnessDefaults(strictnessLevel = 'L0') {
|
|
818
771
|
try {
|
|
819
772
|
const flexMd = await loadFlexMd();
|
|
820
773
|
if (flexMd.strictnessDefaults && typeof flexMd.strictnessDefaults === 'function') {
|
|
@@ -835,7 +788,7 @@ async function getStrictnessDefaults(strictnessLevel = 'L0') {
|
|
|
835
788
|
* @param strictnessLevel - Compliance level ('L0' | 'L1' | 'L2' | 'L3')
|
|
836
789
|
* @returns Instructions with flex-md enforcement rules
|
|
837
790
|
*/
|
|
838
|
-
async function enforceFlexMdFormat(instructions, outputFormatSpec, strictnessLevel = 'L0') {
|
|
791
|
+
export async function enforceFlexMdFormat(instructions, outputFormatSpec, strictnessLevel = 'L0') {
|
|
839
792
|
try {
|
|
840
793
|
const flexMd = await loadFlexMd();
|
|
841
794
|
if (flexMd.enforceFlexMd && typeof flexMd.enforceFlexMd === 'function') {
|
|
@@ -861,7 +814,7 @@ async function enforceFlexMdFormat(instructions, outputFormatSpec, strictnessLev
|
|
|
861
814
|
* @param complianceLevel - Required compliance level
|
|
862
815
|
* @returns true if instructions meet the compliance level
|
|
863
816
|
*/
|
|
864
|
-
async function hasFlexMdContract(instructions, complianceLevel = 'L0') {
|
|
817
|
+
export async function hasFlexMdContract(instructions, complianceLevel = 'L0') {
|
|
865
818
|
if (!instructions || instructions.trim() === '') {
|
|
866
819
|
return false;
|
|
867
820
|
}
|
|
@@ -929,7 +882,7 @@ async function hasFlexMdContract(instructions, complianceLevel = 'L0') {
|
|
|
929
882
|
* @param instructions - Instructions text to check
|
|
930
883
|
* @returns The highest compliance level met ('L0' | 'L1' | 'L2' | 'L3' | null if none)
|
|
931
884
|
*/
|
|
932
|
-
async function detectComplianceLevel(instructions) {
|
|
885
|
+
export async function detectComplianceLevel(instructions) {
|
|
933
886
|
if (!instructions || instructions.trim() === '') {
|
|
934
887
|
return null;
|
|
935
888
|
}
|
|
@@ -1,49 +1,10 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Configuration Module
|
|
4
3
|
* Handles configuration loading and setup
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
-
}
|
|
12
|
-
Object.defineProperty(o, k2, desc);
|
|
13
|
-
}) : (function(o, m, k, k2) {
|
|
14
|
-
if (k2 === undefined) k2 = k;
|
|
15
|
-
o[k2] = m[k];
|
|
16
|
-
}));
|
|
17
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
-
}) : function(o, v) {
|
|
20
|
-
o["default"] = v;
|
|
21
|
-
});
|
|
22
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
23
|
-
var ownKeys = function(o) {
|
|
24
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
25
|
-
var ar = [];
|
|
26
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
27
|
-
return ar;
|
|
28
|
-
};
|
|
29
|
-
return ownKeys(o);
|
|
30
|
-
};
|
|
31
|
-
return function (mod) {
|
|
32
|
-
if (mod && mod.__esModule) return mod;
|
|
33
|
-
var result = {};
|
|
34
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
35
|
-
__setModuleDefault(result, mod);
|
|
36
|
-
return result;
|
|
37
|
-
};
|
|
38
|
-
})();
|
|
39
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
-
exports.loadConfig = loadConfig;
|
|
41
|
-
exports.getFlexMdMinComplianceLevel = getFlexMdMinComplianceLevel;
|
|
42
|
-
exports.setupRequestInterceptor = setupRequestInterceptor;
|
|
43
|
-
exports.initializeGatewayComponents = initializeGatewayComponents;
|
|
44
|
-
const fs = __importStar(require("fs"));
|
|
45
|
-
const path = __importStar(require("path"));
|
|
46
|
-
const url_1 = require("url");
|
|
5
|
+
import * as fs from 'fs';
|
|
6
|
+
import * as path from 'path';
|
|
7
|
+
import { fileURLToPath } from 'url';
|
|
47
8
|
/** Resolve current module directory across ESM/CJS builds. */
|
|
48
9
|
function getModuleDir() {
|
|
49
10
|
if (typeof __dirname !== 'undefined') {
|
|
@@ -53,7 +14,7 @@ function getModuleDir() {
|
|
|
53
14
|
const getMetaUrl = new Function('return import.meta.url');
|
|
54
15
|
const metaUrl = getMetaUrl();
|
|
55
16
|
if (metaUrl && metaUrl.startsWith('file:')) {
|
|
56
|
-
return path.dirname(
|
|
17
|
+
return path.dirname(fileURLToPath(metaUrl));
|
|
57
18
|
}
|
|
58
19
|
}
|
|
59
20
|
catch {
|
|
@@ -84,18 +45,18 @@ function getDefaultsDir() {
|
|
|
84
45
|
// Keep existing behavior as a last fallback.
|
|
85
46
|
return path.join(cwd, 'dist');
|
|
86
47
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
48
|
+
import { LLMProviderRouter } from '@x12i/ai-providers-router';
|
|
49
|
+
import { createGatewayLogger } from './logger-factory.js';
|
|
50
|
+
import { ActivityManager } from './activity-manager.js';
|
|
51
|
+
import { UsageTracker } from './usage-tracker.js';
|
|
52
|
+
import { mergeTemplateRenderOptions } from './template-render-merge.js';
|
|
53
|
+
import { GatewayRateLimiter } from './gateway-rate-limiter.js';
|
|
54
|
+
import { DEFAULT_RATE_LIMIT_MIN_INTERVAL_MS, DEFAULT_RATE_LIMIT_ENABLED } from './gateway-rate-limiter-constants.js';
|
|
94
55
|
/**
|
|
95
56
|
* Loads configuration from JSON files (model config and instructionsBlocks).
|
|
96
57
|
* Pass a {@link Logxer} instance so load diagnostics go through logxer (not console).
|
|
97
58
|
*/
|
|
98
|
-
function loadConfig(logger) {
|
|
59
|
+
export function loadConfig(logger) {
|
|
99
60
|
const defaultModelConfig = {};
|
|
100
61
|
const defaultInstructionsBlocks = {};
|
|
101
62
|
let defaultTemplateRendering;
|
|
@@ -150,7 +111,7 @@ function loadConfig(logger) {
|
|
|
150
111
|
* Gets the minimum flex-md compliance level from environment variable
|
|
151
112
|
* Defaults to 'L0' if not set or invalid
|
|
152
113
|
*/
|
|
153
|
-
function getFlexMdMinComplianceLevel() {
|
|
114
|
+
export function getFlexMdMinComplianceLevel() {
|
|
154
115
|
const envValue = process.env.FLEX_MD_MIN_COMPLIANCE_LEVEL;
|
|
155
116
|
if (envValue === 'L0' || envValue === 'L1' || envValue === 'L2' || envValue === 'L3') {
|
|
156
117
|
return envValue;
|
|
@@ -160,7 +121,7 @@ function getFlexMdMinComplianceLevel() {
|
|
|
160
121
|
/**
|
|
161
122
|
* Sets up request interceptor for jobId propagation and config cleanup
|
|
162
123
|
*/
|
|
163
|
-
function setupRequestInterceptor(router, logger) {
|
|
124
|
+
export function setupRequestInterceptor(router, logger) {
|
|
164
125
|
logger.debug('Setting up request interceptor for jobId propagation and config cleanup');
|
|
165
126
|
router.addRequestInterceptor(async (request, provider) => {
|
|
166
127
|
const identityJobId = request?.identity?.jobId;
|
|
@@ -194,9 +155,9 @@ function setupRequestInterceptor(router, logger) {
|
|
|
194
155
|
/**
|
|
195
156
|
* Initializes gateway components
|
|
196
157
|
*/
|
|
197
|
-
function initializeGatewayComponents(config) {
|
|
158
|
+
export function initializeGatewayComponents(config) {
|
|
198
159
|
// Initialize logger FIRST (before other components that might need it)
|
|
199
|
-
const logger =
|
|
160
|
+
const logger = createGatewayLogger({
|
|
200
161
|
enableLogging: config.enableLogging ?? true,
|
|
201
162
|
packageName: config.packageName,
|
|
202
163
|
customLogger: config.logger
|
|
@@ -242,7 +203,7 @@ function initializeGatewayComponents(config) {
|
|
|
242
203
|
routerConfig.openrouter = { apiKey: openRouterKey };
|
|
243
204
|
routerConfig.defaultMode = 'openrouter';
|
|
244
205
|
}
|
|
245
|
-
const router = new
|
|
206
|
+
const router = new LLMProviderRouter(routerConfig);
|
|
246
207
|
// Set up BETWEEN-CALLS rate limiting as a request interceptor (applies to all provider calls)
|
|
247
208
|
// This ensures rate limiting works even when router is used directly without gateway
|
|
248
209
|
// Hidden in the flow - automatic and transparent
|
|
@@ -252,14 +213,14 @@ function initializeGatewayComponents(config) {
|
|
|
252
213
|
const rateLimitConfig = config.rateLimit;
|
|
253
214
|
// Get defaults from JSON config, fallback to constants
|
|
254
215
|
const jsonRateLimitConfig = defaultModelConfig.rateLimit || {};
|
|
255
|
-
const rateLimitEnabled = rateLimitConfig?.enabled ?? jsonRateLimitConfig.enabled ??
|
|
216
|
+
const rateLimitEnabled = rateLimitConfig?.enabled ?? jsonRateLimitConfig.enabled ?? DEFAULT_RATE_LIMIT_ENABLED;
|
|
256
217
|
if (rateLimitEnabled) {
|
|
257
218
|
// Priority: explicit config > JSON defaults > constants
|
|
258
219
|
const defaultMinIntervalMs = rateLimitConfig?.defaultMinIntervalMs
|
|
259
220
|
?? jsonRateLimitConfig.defaultMinIntervalMs
|
|
260
|
-
??
|
|
221
|
+
?? DEFAULT_RATE_LIMIT_MIN_INTERVAL_MS;
|
|
261
222
|
const providerIntervals = rateLimitConfig?.providerIntervals;
|
|
262
|
-
const rateLimiter = new
|
|
223
|
+
const rateLimiter = new GatewayRateLimiter(defaultMinIntervalMs, providerIntervals, logger);
|
|
263
224
|
// Add request interceptor for BETWEEN-CALLS rate limiting (hidden in the flow)
|
|
264
225
|
router.addRequestInterceptor(async (request, provider) => {
|
|
265
226
|
// Get provider name
|
|
@@ -295,18 +256,18 @@ function initializeGatewayComponents(config) {
|
|
|
295
256
|
logger.debug('Rate limiting disabled');
|
|
296
257
|
}
|
|
297
258
|
// Initialize usage tracking
|
|
298
|
-
const usageTracker = new
|
|
259
|
+
const usageTracker = new UsageTracker({
|
|
299
260
|
enableUsageTracking: config.enableUsageTracking ?? true,
|
|
300
261
|
usageTier: config.usageTier,
|
|
301
262
|
logger
|
|
302
263
|
});
|
|
303
264
|
// Initialize activity tracking
|
|
304
|
-
const activityManager = new
|
|
265
|
+
const activityManager = new ActivityManager({
|
|
305
266
|
enableActivityTracking: config.enableActivityTracking ?? true,
|
|
306
267
|
customTracker: config.activityTracker,
|
|
307
268
|
logger
|
|
308
269
|
});
|
|
309
|
-
const templateRendering =
|
|
270
|
+
const templateRendering = mergeTemplateRenderOptions(defaultTemplateRendering, config.templateRendering);
|
|
310
271
|
const instructionsBlockOverrides = {
|
|
311
272
|
...(config.instructionsBlocks ?? {})
|
|
312
273
|
};
|
|
@@ -322,6 +283,7 @@ function initializeGatewayComponents(config) {
|
|
|
322
283
|
router,
|
|
323
284
|
activityManager,
|
|
324
285
|
usageTracker,
|
|
325
|
-
messageBuilderConfig
|
|
286
|
+
messageBuilderConfig,
|
|
287
|
+
defaultModelConfig
|
|
326
288
|
};
|
|
327
289
|
}
|
|
@@ -19,6 +19,7 @@ export interface GatewayConfigContext {
|
|
|
19
19
|
usageTracker: UsageTracker;
|
|
20
20
|
messageBuilderConfig: MessageBuilderConfig;
|
|
21
21
|
}
|
|
22
|
+
export type InitializedGatewayComponents = ReturnType<typeof initializeGatewayComponents>;
|
|
22
23
|
/**
|
|
23
24
|
* Loads configuration from JSON files (model config and instructionsBlocks).
|
|
24
25
|
* Pass a {@link Logxer} instance so load diagnostics go through logxer (not console).
|
|
@@ -46,4 +47,5 @@ export declare function initializeGatewayComponents(config: GatewayConfig): {
|
|
|
46
47
|
activityManager: ActivityManager;
|
|
47
48
|
usageTracker: UsageTracker;
|
|
48
49
|
messageBuilderConfig: MessageBuilderConfig;
|
|
50
|
+
defaultModelConfig: Record<string, unknown>;
|
|
49
51
|
};
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Conversion Module
|
|
4
3
|
* Handles structured text conversion and two-step conversion
|
|
@@ -6,44 +5,7 @@
|
|
|
6
5
|
* Text mode now uses flex-md format exclusively.
|
|
7
6
|
* Two-step conversion parses flex-md directly to JSON without additional LLM call.
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
12
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
13
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
14
|
-
}
|
|
15
|
-
Object.defineProperty(o, k2, desc);
|
|
16
|
-
}) : (function(o, m, k, k2) {
|
|
17
|
-
if (k2 === undefined) k2 = k;
|
|
18
|
-
o[k2] = m[k];
|
|
19
|
-
}));
|
|
20
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
21
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
22
|
-
}) : function(o, v) {
|
|
23
|
-
o["default"] = v;
|
|
24
|
-
});
|
|
25
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
26
|
-
var ownKeys = function(o) {
|
|
27
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
28
|
-
var ar = [];
|
|
29
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
30
|
-
return ar;
|
|
31
|
-
};
|
|
32
|
-
return ownKeys(o);
|
|
33
|
-
};
|
|
34
|
-
return function (mod) {
|
|
35
|
-
if (mod && mod.__esModule) return mod;
|
|
36
|
-
var result = {};
|
|
37
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
38
|
-
__setModuleDefault(result, mod);
|
|
39
|
-
return result;
|
|
40
|
-
};
|
|
41
|
-
})();
|
|
42
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
-
exports.invokeStructuredText = invokeStructuredText;
|
|
44
|
-
exports.getDefaultConversionInstructions = getDefaultConversionInstructions;
|
|
45
|
-
exports.convertStructuredToJson = convertStructuredToJson;
|
|
46
|
-
const flex_md_loader_js_1 = require("./flex-md-loader.cjs");
|
|
8
|
+
import { extractJsonFromFlexMd } from './flex-md-loader.js';
|
|
47
9
|
/**
|
|
48
10
|
* Get the current directory - works in both ESM and CJS
|
|
49
11
|
* In CJS: uses __dirname
|
|
@@ -59,8 +21,8 @@ async function getCurrentDir() {
|
|
|
59
21
|
const getMetaUrl = new Function('return import.meta.url');
|
|
60
22
|
const metaUrl = getMetaUrl();
|
|
61
23
|
if (metaUrl) {
|
|
62
|
-
const url = await
|
|
63
|
-
const path = await
|
|
24
|
+
const url = await import('url');
|
|
25
|
+
const path = await import('path');
|
|
64
26
|
return path.dirname(url.fileURLToPath(metaUrl));
|
|
65
27
|
}
|
|
66
28
|
}
|
|
@@ -73,7 +35,7 @@ async function getCurrentDir() {
|
|
|
73
35
|
/**
|
|
74
36
|
* Invoke structured text mode (first step of two-step conversion)
|
|
75
37
|
*/
|
|
76
|
-
async function invokeStructuredText(request, gateway) {
|
|
38
|
+
export async function invokeStructuredText(request, gateway) {
|
|
77
39
|
// Create a new request with structured-text mode
|
|
78
40
|
const structuredRequest = {
|
|
79
41
|
...request,
|
|
@@ -89,7 +51,7 @@ async function invokeStructuredText(request, gateway) {
|
|
|
89
51
|
* Two-step mode now parses flex-md directly using the flex-md package.
|
|
90
52
|
* This function is kept for backward compatibility only.
|
|
91
53
|
*/
|
|
92
|
-
function getDefaultConversionInstructions() {
|
|
54
|
+
export function getDefaultConversionInstructions() {
|
|
93
55
|
return `You are a data converter. Your task is to convert structured text into valid JSON.
|
|
94
56
|
|
|
95
57
|
You will receive structured text output from an AI assistant. Convert it into a valid JSON object that captures all the information from the structured text.
|
|
@@ -111,7 +73,7 @@ CRITICAL RULES:
|
|
|
111
73
|
* This function now parses flex-md directly using the flex-md package,
|
|
112
74
|
* eliminating the need for an additional LLM call.
|
|
113
75
|
*/
|
|
114
|
-
async function convertStructuredToJson(structuredText, originalRequest, config, gateway, logger) {
|
|
76
|
+
export async function convertStructuredToJson(structuredText, originalRequest, config, gateway, logger) {
|
|
115
77
|
logger.debug('Two-step conversion: Parsing flex-md text directly using flex-md SDK 3.0.0', {
|
|
116
78
|
aiRequestId: originalRequest.aiRequestId,
|
|
117
79
|
structuredTextLength: structuredText.length,
|
|
@@ -119,7 +81,7 @@ async function convertStructuredToJson(structuredText, originalRequest, config,
|
|
|
119
81
|
});
|
|
120
82
|
try {
|
|
121
83
|
// Use flex-md 3.0.0 to extract and parse JSON
|
|
122
|
-
const flexMdResult = await
|
|
84
|
+
const flexMdResult = await extractJsonFromFlexMd(structuredText, logger);
|
|
123
85
|
if (!flexMdResult || !flexMdResult.json) {
|
|
124
86
|
throw new Error('flex-md SDK could not extract JSON from structured text');
|
|
125
87
|
}
|
|
@@ -168,7 +130,7 @@ async function convertStructuredToJson(structuredText, originalRequest, config,
|
|
|
168
130
|
let flexMdVersion = 'unknown';
|
|
169
131
|
try {
|
|
170
132
|
// Use type assertion to allow dynamic import of package.json
|
|
171
|
-
const flexMdPkg = await
|
|
133
|
+
const flexMdPkg = await import('flex-md/package.json');
|
|
172
134
|
flexMdVersion = flexMdPkg?.version || flexMdPkg?.default?.version || 'unknown';
|
|
173
135
|
}
|
|
174
136
|
catch {
|
|
@@ -195,8 +157,8 @@ async function convertStructuredToJson(structuredText, originalRequest, config,
|
|
|
195
157
|
};
|
|
196
158
|
// Write report to docs directory
|
|
197
159
|
try {
|
|
198
|
-
const fs = await
|
|
199
|
-
const path = await
|
|
160
|
+
const fs = await import('fs');
|
|
161
|
+
const path = await import('path');
|
|
200
162
|
const currentDir = await getCurrentDir();
|
|
201
163
|
const reportPath = path.join(currentDir, '../../docs/FLEX_MD_PACKAGE_ISSUE_REPORT.json');
|
|
202
164
|
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
|
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Instructions Module
|
|
4
3
|
* Handles instructions block resolution from inline config only (no content registry).
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
exports.resolveNestedInstructionsBlock = resolveNestedInstructionsBlock;
|
|
8
|
-
exports.resolveInstructionsBlock = resolveInstructionsBlock;
|
|
9
|
-
exports.getPreParsedInstructions = getPreParsedInstructions;
|
|
10
|
-
const instruction_errors_js_1 = require("./instruction-errors.cjs");
|
|
5
|
+
import { InstructionNotFoundError } from './instruction-errors.js';
|
|
11
6
|
function getNestedString(blocks, dotPath) {
|
|
12
7
|
const parts = dotPath.split('.');
|
|
13
8
|
let cur = blocks;
|
|
@@ -23,13 +18,13 @@ function getNestedString(blocks, dotPath) {
|
|
|
23
18
|
* Supports dot notation for nested object access in defaultInstructionsBlocks,
|
|
24
19
|
* then flat keys in merged overrides.
|
|
25
20
|
*/
|
|
26
|
-
async function resolveNestedInstructionsBlock(blockPath, _agentId, _taskTypeId, context) {
|
|
21
|
+
export async function resolveNestedInstructionsBlock(blockPath, _agentId, _taskTypeId, context) {
|
|
27
22
|
return resolveInstructionsBlock(blockPath, _agentId, _taskTypeId, context);
|
|
28
23
|
}
|
|
29
24
|
/**
|
|
30
25
|
* Resolves instructionsBlocks from config overrides, request overrides, nested defaults, or flat defaults.
|
|
31
26
|
*/
|
|
32
|
-
async function resolveInstructionsBlock(blockName, agentId, taskTypeId, context) {
|
|
27
|
+
export async function resolveInstructionsBlock(blockName, agentId, taskTypeId, context) {
|
|
33
28
|
const { config, logger, defaultInstructionsBlocks, instructionsBlockOverrides, requestInstructionsBlocks } = context;
|
|
34
29
|
logger.verbose('Resolving instructionsBlock', {
|
|
35
30
|
blockName,
|
|
@@ -57,11 +52,11 @@ async function resolveInstructionsBlock(blockName, agentId, taskTypeId, context)
|
|
|
57
52
|
logger.debug('Using nested defaultInstructionsBlocks for instructionsBlock', { blockName, agentId });
|
|
58
53
|
return nested;
|
|
59
54
|
}
|
|
60
|
-
throw new
|
|
55
|
+
throw new InstructionNotFoundError(blockName, 'instructions-blocks', `InstructionsBlock "${blockName}" not found. Provide it via packaged defaults, gateway instructionsBlocks, or request.config.instructionsBlocks.`, blockName);
|
|
61
56
|
}
|
|
62
57
|
/**
|
|
63
58
|
* Pre-parse instructions string for stable hashing (taskTypeId). No external resolution.
|
|
64
59
|
*/
|
|
65
|
-
function getPreParsedInstructions(instructions) {
|
|
60
|
+
export function getPreParsedInstructions(instructions) {
|
|
66
61
|
return instructions ?? '';
|
|
67
62
|
}
|
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.gatewayLogDebug = void 0;
|
|
4
|
-
exports.activityIdentityToLogMeta = activityIdentityToLogMeta;
|
|
5
|
-
exports.withActivityIdentity = withActivityIdentity;
|
|
6
|
-
const logxer_1 = require("@x12i/logxer");
|
|
1
|
+
import { DebugLogAbstract } from '@x12i/logxer';
|
|
7
2
|
/** Subset of identity forwarded on every structured log line (plus full envelope under `identity`). */
|
|
8
|
-
function activityIdentityToLogMeta(identity) {
|
|
3
|
+
export function activityIdentityToLogMeta(identity) {
|
|
9
4
|
if (!identity) {
|
|
10
5
|
return {};
|
|
11
6
|
}
|
|
@@ -25,17 +20,17 @@ function activityIdentityToLogMeta(identity) {
|
|
|
25
20
|
};
|
|
26
21
|
return meta;
|
|
27
22
|
}
|
|
28
|
-
function withActivityIdentity(identity, data, debugKind) {
|
|
23
|
+
export function withActivityIdentity(identity, data, debugKind) {
|
|
29
24
|
return {
|
|
30
25
|
...activityIdentityToLogMeta(identity),
|
|
31
26
|
...(data || {}),
|
|
32
27
|
...(debugKind !== undefined ? { debugKind } : {})
|
|
33
28
|
};
|
|
34
29
|
}
|
|
35
|
-
|
|
36
|
-
anomaly:
|
|
37
|
-
trace:
|
|
38
|
-
intent:
|
|
39
|
-
event:
|
|
40
|
-
state:
|
|
30
|
+
export const gatewayLogDebug = {
|
|
31
|
+
anomaly: DebugLogAbstract.ANOMALY,
|
|
32
|
+
trace: DebugLogAbstract.TRACE,
|
|
33
|
+
intent: DebugLogAbstract.INTENT,
|
|
34
|
+
event: DebugLogAbstract.EVENT,
|
|
35
|
+
state: DebugLogAbstract.STATE
|
|
41
36
|
};
|
|
@@ -1,11 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Memory Module
|
|
4
3
|
* Handles working memory and template parameter resolution
|
|
5
4
|
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.buildWorkingMemory = buildWorkingMemory;
|
|
8
|
-
exports.resolveTemplateParams = resolveTemplateParams;
|
|
9
5
|
// Type guard to check if request is AIRequest (has primaryObjectType)
|
|
10
6
|
function isAIRequest(request) {
|
|
11
7
|
return 'primaryObjectType' in request &&
|
|
@@ -21,7 +17,7 @@ function isAIRequest(request) {
|
|
|
21
17
|
* - Tier 2: workingMemory (this method) - checks existing workingMemory first
|
|
22
18
|
* - Tier 3: derived from request fields or other memories (fallback)
|
|
23
19
|
*/
|
|
24
|
-
function buildWorkingMemory(request, existingWorkingMemory, otherMemories) {
|
|
20
|
+
export function buildWorkingMemory(request, existingWorkingMemory, otherMemories) {
|
|
25
21
|
// Start with existing workingMemory if provided, otherwise empty object
|
|
26
22
|
const workingMemory = existingWorkingMemory && typeof existingWorkingMemory === 'object'
|
|
27
23
|
? { ...existingWorkingMemory }
|
|
@@ -142,7 +138,7 @@ function buildWorkingMemory(request, existingWorkingMemory, otherMemories) {
|
|
|
142
138
|
* Resolves template parameters with smart fallback logic
|
|
143
139
|
* Priority: request.workingMemory -> memoryManager resolution -> buildWorkingMemory merge
|
|
144
140
|
*/
|
|
145
|
-
async function resolveTemplateParams(request, config, logger) {
|
|
141
|
+
export async function resolveTemplateParams(request, config, logger) {
|
|
146
142
|
// Tier 1: Request args (highest priority)
|
|
147
143
|
let workingMemory = request.workingMemory;
|
|
148
144
|
// Tier 2: Memory component integration (if available)
|
|
@@ -1,11 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Messages Module
|
|
4
3
|
* Handles message construction using direct message builder
|
|
5
4
|
*/
|
|
6
|
-
|
|
7
|
-
exports.constructMessages = constructMessages;
|
|
8
|
-
const message_builder_js_1 = require("./message-builder.cjs");
|
|
5
|
+
import { buildMessages } from './message-builder.js';
|
|
9
6
|
// Type guard to check if request is AIRequest
|
|
10
7
|
// AIRequest is distinguished by having primaryObjectType, objectTypes, or other AIRequest-specific fields
|
|
11
8
|
function isAIRequest(request) {
|
|
@@ -25,7 +22,7 @@ function isAIRequest(request) {
|
|
|
25
22
|
* - Flex-md format specification
|
|
26
23
|
* - Message assembly
|
|
27
24
|
*/
|
|
28
|
-
async function constructMessages(request, config, logger, parsedSnapshot) {
|
|
25
|
+
export async function constructMessages(request, config, logger, parsedSnapshot) {
|
|
29
26
|
logger.verbose('Constructing messages from request', {
|
|
30
27
|
jobId: request.identity.jobId,
|
|
31
28
|
agentId: request.agentId,
|
|
@@ -73,7 +70,7 @@ async function constructMessages(request, config, logger, parsedSnapshot) {
|
|
|
73
70
|
// Update request with final instructions (for message builder)
|
|
74
71
|
const requestWithExamples = { ...request, instructions: finalInstructions };
|
|
75
72
|
// Build messages using direct message builder
|
|
76
|
-
const result = await
|
|
73
|
+
const result = await buildMessages(requestWithExamples, config, {
|
|
77
74
|
useSystemContextFallback: true,
|
|
78
75
|
includeInputRecognition: isAIRequest(request),
|
|
79
76
|
includeReinforcement: isAIRequest(request),
|