@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.
- package/README.md +45 -0
- package/dist/activity-manager.d.ts +1 -0
- package/dist/activity-manager.js +7 -0
- package/dist/ai-tools-client.d.ts +20 -0
- package/dist/ai-tools-client.js +91 -0
- package/dist/flex-md-loader.d.ts +5 -0
- package/dist/flex-md-loader.js +16 -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 +57 -1
- package/dist/gateway-utils.js +181 -12
- package/dist/gateway.d.ts +3 -0
- package/dist/gateway.js +47 -15
- package/dist/index.d.ts +6 -1
- package/dist/index.js +3 -1
- package/dist/output-contract-normalizer.d.ts +21 -0
- package/dist/output-contract-normalizer.js +121 -0
- package/dist/types.d.ts +35 -0
- package/dist-cjs/activity-manager.cjs +21 -19
- 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 +35 -65
- package/dist-cjs/flex-md-loader.d.ts +5 -0
- 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 +201 -83
- package/dist-cjs/gateway-utils.d.ts +57 -1
- package/dist-cjs/gateway-validation.cjs +2 -6
- package/dist-cjs/gateway.cjs +100 -72
- package/dist-cjs/gateway.d.ts +3 -0
- package/dist-cjs/index.cjs +22 -91
- package/dist-cjs/index.d.ts +6 -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 +121 -0
- package/dist-cjs/output-contract-normalizer.d.ts +21 -0
- 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 +35 -0
- package/dist-cjs/usage-tracker.cjs +3 -7
- package/package.json +11 -5
|
@@ -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),
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Meta Operations Module
|
|
4
3
|
* Handles meta operations like instruction optimization and testing
|
|
5
4
|
*/
|
|
6
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
-
exports.testInstructions = testInstructions;
|
|
8
5
|
/**
|
|
9
6
|
* Test instructions by running them and analyzing the response
|
|
10
7
|
*/
|
|
11
|
-
async function testInstructions(instructions, testInput, expectedSchema, options, config, gateway, logger) {
|
|
8
|
+
export async function testInstructions(instructions, testInput, expectedSchema, options, config, gateway, logger) {
|
|
12
9
|
// Get internal system action config (instruction audit)
|
|
13
10
|
const internalConfig = config.internalSystemActions?.instructionAudit;
|
|
14
11
|
const defaultEngine = config.defaultEngine || 'openai';
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway operational mode (prod vs dev/debug) and default model resolution.
|
|
3
|
+
*/
|
|
4
|
+
import { gatewayLogDebug, withActivityIdentity } from './gateway-log-meta.js';
|
|
5
|
+
export const CODE_DEFAULT_MODEL = 'gpt-5-nano';
|
|
6
|
+
/**
|
|
7
|
+
* Operational mode: `GatewayConfig.mode` overrides `process.env.mode` / `MODE`.
|
|
8
|
+
* Only `prod` allows silent default-model substitution; all other values are strict.
|
|
9
|
+
*/
|
|
10
|
+
export function getGatewayOperationalMode(config) {
|
|
11
|
+
if (config?.mode) {
|
|
12
|
+
return config.mode;
|
|
13
|
+
}
|
|
14
|
+
const raw = (process.env.mode ?? process.env.MODE ?? '').toLowerCase();
|
|
15
|
+
if (raw === 'prod')
|
|
16
|
+
return 'prod';
|
|
17
|
+
if (raw === 'dev')
|
|
18
|
+
return 'dev';
|
|
19
|
+
return 'debug';
|
|
20
|
+
}
|
|
21
|
+
export function isProdGatewayMode(mode) {
|
|
22
|
+
return mode === 'prod';
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Parse `provider/model` or bare model id (OpenRouter ids may contain multiple slashes).
|
|
26
|
+
*/
|
|
27
|
+
export function parseModelProviderSpec(spec) {
|
|
28
|
+
const trimmed = spec.trim();
|
|
29
|
+
if (!trimmed) {
|
|
30
|
+
return { model: CODE_DEFAULT_MODEL };
|
|
31
|
+
}
|
|
32
|
+
const slash = trimmed.indexOf('/');
|
|
33
|
+
if (slash === -1) {
|
|
34
|
+
return { model: trimmed };
|
|
35
|
+
}
|
|
36
|
+
const first = trimmed.slice(0, slash);
|
|
37
|
+
const rest = trimmed.slice(slash + 1);
|
|
38
|
+
if (rest.includes('/') && (first === 'openrouter' || first === 'open-router')) {
|
|
39
|
+
return { provider: 'openrouter', model: trimmed };
|
|
40
|
+
}
|
|
41
|
+
return { provider: first, model: rest };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Default model priority: AI_GATEWAY_DEFAULT_MODEL → model-config.json → code constant.
|
|
45
|
+
*/
|
|
46
|
+
export function resolveGatewayDefaultModel(defaultModelConfig, gatewayDefaultEngine) {
|
|
47
|
+
const envSpec = process.env.AI_GATEWAY_DEFAULT_MODEL?.trim();
|
|
48
|
+
if (envSpec) {
|
|
49
|
+
const parsed = parseModelProviderSpec(envSpec);
|
|
50
|
+
return { model: parsed.model, provider: parsed.provider, source: 'env' };
|
|
51
|
+
}
|
|
52
|
+
const jsonModel = typeof defaultModelConfig?.defaultModel === 'string' ? defaultModelConfig.defaultModel : undefined;
|
|
53
|
+
if (jsonModel) {
|
|
54
|
+
const parsed = parseModelProviderSpec(jsonModel);
|
|
55
|
+
const jsonEngine = typeof defaultModelConfig?.defaultEngine === 'string'
|
|
56
|
+
? defaultModelConfig.defaultEngine
|
|
57
|
+
: gatewayDefaultEngine;
|
|
58
|
+
return {
|
|
59
|
+
model: parsed.model,
|
|
60
|
+
provider: parsed.provider ?? jsonEngine,
|
|
61
|
+
source: 'model-config.json'
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
model: CODE_DEFAULT_MODEL,
|
|
66
|
+
provider: gatewayDefaultEngine,
|
|
67
|
+
source: 'code'
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
export function warnDefaultModelSubstitution(logger, identity, details) {
|
|
71
|
+
logger.warn('Gateway substituted default model for request', withActivityIdentity(identity, {
|
|
72
|
+
...details,
|
|
73
|
+
debugKind: gatewayLogDebug.anomaly
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway operational mode (prod vs dev/debug) and default model resolution.
|
|
3
|
+
*/
|
|
4
|
+
import type { Logxer } from '@x12i/logxer';
|
|
5
|
+
import type { ActivityIdentity, GatewayConfig } from './types.js';
|
|
6
|
+
export type GatewayOperationalMode = 'prod' | 'debug' | 'dev';
|
|
7
|
+
export type GatewayDefaultModelSource = 'env' | 'model-config.json' | 'code';
|
|
8
|
+
export type DefaultModelSubstitutionReason = 'no_model_provided' | 'model_resolution_failed' | 'ai_tools_unavailable';
|
|
9
|
+
export declare const CODE_DEFAULT_MODEL = "gpt-5-nano";
|
|
10
|
+
export type ResolvedGatewayDefault = {
|
|
11
|
+
model: string;
|
|
12
|
+
provider?: string;
|
|
13
|
+
source: GatewayDefaultModelSource;
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* Operational mode: `GatewayConfig.mode` overrides `process.env.mode` / `MODE`.
|
|
17
|
+
* Only `prod` allows silent default-model substitution; all other values are strict.
|
|
18
|
+
*/
|
|
19
|
+
export declare function getGatewayOperationalMode(config?: Pick<GatewayConfig, 'mode'>): GatewayOperationalMode;
|
|
20
|
+
export declare function isProdGatewayMode(mode: GatewayOperationalMode): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Parse `provider/model` or bare model id (OpenRouter ids may contain multiple slashes).
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseModelProviderSpec(spec: string): {
|
|
25
|
+
provider?: string;
|
|
26
|
+
model: string;
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Default model priority: AI_GATEWAY_DEFAULT_MODEL → model-config.json → code constant.
|
|
30
|
+
*/
|
|
31
|
+
export declare function resolveGatewayDefaultModel(defaultModelConfig?: Record<string, unknown>, gatewayDefaultEngine?: string): ResolvedGatewayDefault;
|
|
32
|
+
export declare function warnDefaultModelSubstitution(logger: Logxer, identity: Partial<ActivityIdentity> | undefined, details: {
|
|
33
|
+
reason: DefaultModelSubstitutionReason;
|
|
34
|
+
mode: GatewayOperationalMode;
|
|
35
|
+
defaultSource: GatewayDefaultModelSource;
|
|
36
|
+
defaultProvider?: string;
|
|
37
|
+
defaultModel: string;
|
|
38
|
+
originalProvider?: string;
|
|
39
|
+
originalModel?: string;
|
|
40
|
+
}): void;
|
|
@@ -1,43 +1,7 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
/**
|
|
3
2
|
* Gateway Provider Auto-Registration Module
|
|
4
3
|
* Automatically registers providers based on environment variables
|
|
5
4
|
*/
|
|
6
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
-
if (k2 === undefined) k2 = k;
|
|
8
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
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.autoRegisterProviders = autoRegisterProviders;
|
|
41
5
|
const PROVIDER_CONFIGS = [
|
|
42
6
|
{
|
|
43
7
|
name: 'openai',
|
|
@@ -90,7 +54,7 @@ const PROVIDER_CONFIGS = [
|
|
|
90
54
|
* @param logger - Logger instance
|
|
91
55
|
* @returns Array of provider names that were successfully registered
|
|
92
56
|
*/
|
|
93
|
-
async function autoRegisterProviders(router, logger) {
|
|
57
|
+
export async function autoRegisterProviders(router, logger) {
|
|
94
58
|
const registeredProviders = [];
|
|
95
59
|
logger.debug('Auto-registering providers from environment variables');
|
|
96
60
|
for (const providerConfig of PROVIDER_CONFIGS) {
|
|
@@ -107,7 +71,7 @@ async function autoRegisterProviders(router, logger) {
|
|
|
107
71
|
// Use dynamic import() for ES module compatibility
|
|
108
72
|
let ProviderClass;
|
|
109
73
|
try {
|
|
110
|
-
const providerModule = await
|
|
74
|
+
const providerModule = await import(providerConfig.packageName);
|
|
111
75
|
ProviderClass = providerModule[providerConfig.className] || providerModule.default;
|
|
112
76
|
if (!ProviderClass) {
|
|
113
77
|
logger.debug(`Provider class ${providerConfig.className} not found in ${providerConfig.packageName}`);
|