@x12i/ai-gateway 7.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +4259 -0
- package/config.defaults.json +31 -0
- package/dist/activity-manager.d.ts +206 -0
- package/dist/activity-manager.js +1051 -0
- package/dist/config/activity-tracking-config.d.ts +11 -0
- package/dist/config/activity-tracking-config.js +15 -0
- package/dist/config.defaults.json +31 -0
- package/dist/content-normalizer/content-normalizer.d.ts +46 -0
- package/dist/content-normalizer/content-normalizer.js +393 -0
- package/dist/content-normalizer/index.d.ts +7 -0
- package/dist/content-normalizer/index.js +6 -0
- package/dist/content-normalizer/types.d.ts +33 -0
- package/dist/content-normalizer/types.js +4 -0
- package/dist/defaults/instructions-blocks.json +61 -0
- package/dist/defaults/model-config.json +16 -0
- package/dist/defaults/template-rendering.json +6 -0
- package/dist/flex-md-loader.d.ts +109 -0
- package/dist/flex-md-loader.js +940 -0
- package/dist/gateway-config.d.ts +49 -0
- package/dist/gateway-config.js +292 -0
- package/dist/gateway-conversion.d.ts +29 -0
- package/dist/gateway-conversion.js +174 -0
- package/dist/gateway-instructions.d.ts +30 -0
- package/dist/gateway-instructions.js +62 -0
- package/dist/gateway-memory.d.ts +51 -0
- package/dist/gateway-memory.js +207 -0
- package/dist/gateway-messages.d.ts +23 -0
- package/dist/gateway-messages.js +83 -0
- package/dist/gateway-meta.d.ts +25 -0
- package/dist/gateway-meta.js +87 -0
- package/dist/gateway-provider-auto-register.d.ts +17 -0
- package/dist/gateway-provider-auto-register.js +159 -0
- package/dist/gateway-provider.d.ts +54 -0
- package/dist/gateway-provider.js +202 -0
- package/dist/gateway-rate-limiter-constants.d.ts +16 -0
- package/dist/gateway-rate-limiter-constants.js +16 -0
- package/dist/gateway-rate-limiter.d.ts +56 -0
- package/dist/gateway-rate-limiter.js +107 -0
- package/dist/gateway-retry.d.ts +49 -0
- package/dist/gateway-retry.js +204 -0
- package/dist/gateway-utils.d.ts +21 -0
- package/dist/gateway-utils.js +181 -0
- package/dist/gateway-validation.d.ts +13 -0
- package/dist/gateway-validation.js +50 -0
- package/dist/gateway.d.ts +39 -0
- package/dist/gateway.js +430 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +55 -0
- package/dist/instruction-errors.d.ts +16 -0
- package/dist/instruction-errors.js +29 -0
- package/dist/instruction-optimizer.d.ts +113 -0
- package/dist/instruction-optimizer.js +293 -0
- package/dist/instructions-parser.d.ts +31 -0
- package/dist/instructions-parser.js +56 -0
- package/dist/logger-factory.d.ts +17 -0
- package/dist/logger-factory.js +42 -0
- package/dist/message-builder.d.ts +41 -0
- package/dist/message-builder.js +522 -0
- package/dist/object-types-library-integration.d.ts +22 -0
- package/dist/object-types-library-integration.js +27 -0
- package/dist/object-types-library.d.ts +351 -0
- package/dist/object-types-library.js +210 -0
- package/dist/output-auditor.d.ts +44 -0
- package/dist/output-auditor.js +49 -0
- package/dist/request-report-generator.d.ts +60 -0
- package/dist/request-report-generator.js +169 -0
- package/dist/response-analyzer/format-type-detector.d.ts +35 -0
- package/dist/response-analyzer/format-type-detector.js +115 -0
- package/dist/response-analyzer/index.d.ts +9 -0
- package/dist/response-analyzer/index.js +8 -0
- package/dist/response-analyzer/object-type-detector.d.ts +42 -0
- package/dist/response-analyzer/object-type-detector.js +95 -0
- package/dist/response-analyzer/response-analyzer.d.ts +38 -0
- package/dist/response-analyzer/response-analyzer.js +97 -0
- package/dist/response-analyzer/types.d.ts +97 -0
- package/dist/response-analyzer/types.js +4 -0
- package/dist/response-fallback-fixer.d.ts +11 -0
- package/dist/response-fallback-fixer.js +123 -0
- package/dist/runtime-objects.d.ts +52 -0
- package/dist/runtime-objects.js +46 -0
- package/dist/template-parser.d.ts +58 -0
- package/dist/template-parser.js +99 -0
- package/dist/template-render-merge.d.ts +9 -0
- package/dist/template-render-merge.js +40 -0
- package/dist/troubleshooting-helper.d.ts +123 -0
- package/dist/troubleshooting-helper.js +596 -0
- package/dist/types.d.ts +1173 -0
- package/dist/types.js +6 -0
- package/dist/usage-tracker.d.ts +78 -0
- package/dist/usage-tracker.js +79 -0
- package/dist-cjs/activity-manager.cjs +1056 -0
- package/dist-cjs/activity-manager.d.ts +206 -0
- package/dist-cjs/config/activity-tracking-config.cjs +18 -0
- package/dist-cjs/config/activity-tracking-config.d.ts +11 -0
- package/dist-cjs/config.defaults.json +31 -0
- package/dist-cjs/content-normalizer/content-normalizer.cjs +398 -0
- package/dist-cjs/content-normalizer/content-normalizer.d.ts +46 -0
- package/dist-cjs/content-normalizer/index.cjs +12 -0
- package/dist-cjs/content-normalizer/index.d.ts +7 -0
- package/dist-cjs/content-normalizer/types.cjs +5 -0
- package/dist-cjs/content-normalizer/types.d.ts +33 -0
- package/dist-cjs/defaults/instructions-blocks.json +61 -0
- package/dist-cjs/defaults/model-config.json +16 -0
- package/dist-cjs/defaults/template-rendering.json +6 -0
- package/dist-cjs/flex-md-loader.cjs +986 -0
- package/dist-cjs/flex-md-loader.d.ts +109 -0
- package/dist-cjs/gateway-config.cjs +331 -0
- package/dist-cjs/gateway-config.d.ts +49 -0
- package/dist-cjs/gateway-conversion.cjs +212 -0
- package/dist-cjs/gateway-conversion.d.ts +29 -0
- package/dist-cjs/gateway-instructions.cjs +67 -0
- package/dist-cjs/gateway-instructions.d.ts +30 -0
- package/dist-cjs/gateway-memory.cjs +211 -0
- package/dist-cjs/gateway-memory.d.ts +51 -0
- package/dist-cjs/gateway-messages.cjs +86 -0
- package/dist-cjs/gateway-messages.d.ts +23 -0
- package/dist-cjs/gateway-meta.cjs +90 -0
- package/dist-cjs/gateway-meta.d.ts +25 -0
- package/dist-cjs/gateway-provider-auto-register.cjs +195 -0
- package/dist-cjs/gateway-provider-auto-register.d.ts +17 -0
- package/dist-cjs/gateway-provider.cjs +214 -0
- package/dist-cjs/gateway-provider.d.ts +54 -0
- package/dist-cjs/gateway-rate-limiter-constants.cjs +19 -0
- package/dist-cjs/gateway-rate-limiter-constants.d.ts +16 -0
- package/dist-cjs/gateway-rate-limiter.cjs +111 -0
- package/dist-cjs/gateway-rate-limiter.d.ts +56 -0
- package/dist-cjs/gateway-retry.cjs +212 -0
- package/dist-cjs/gateway-retry.d.ts +49 -0
- package/dist-cjs/gateway-utils.cjs +219 -0
- package/dist-cjs/gateway-utils.d.ts +21 -0
- package/dist-cjs/gateway-validation.cjs +54 -0
- package/dist-cjs/gateway-validation.d.ts +13 -0
- package/dist-cjs/gateway.cjs +434 -0
- package/dist-cjs/gateway.d.ts +39 -0
- package/dist-cjs/index.cjs +108 -0
- package/dist-cjs/index.d.ts +36 -0
- package/dist-cjs/instruction-errors.cjs +34 -0
- package/dist-cjs/instruction-errors.d.ts +16 -0
- package/dist-cjs/instruction-optimizer.cjs +299 -0
- package/dist-cjs/instruction-optimizer.d.ts +113 -0
- package/dist-cjs/instructions-parser.cjs +61 -0
- package/dist-cjs/instructions-parser.d.ts +31 -0
- package/dist-cjs/logger-factory.cjs +45 -0
- package/dist-cjs/logger-factory.d.ts +17 -0
- package/dist-cjs/message-builder.cjs +558 -0
- package/dist-cjs/message-builder.d.ts +41 -0
- package/dist-cjs/object-types-library-integration.cjs +32 -0
- package/dist-cjs/object-types-library-integration.d.ts +22 -0
- package/dist-cjs/object-types-library.cjs +215 -0
- package/dist-cjs/object-types-library.d.ts +351 -0
- package/dist-cjs/output-auditor.cjs +52 -0
- package/dist-cjs/output-auditor.d.ts +44 -0
- package/dist-cjs/request-report-generator.cjs +172 -0
- package/dist-cjs/request-report-generator.d.ts +60 -0
- package/dist-cjs/response-analyzer/format-type-detector.cjs +119 -0
- package/dist-cjs/response-analyzer/format-type-detector.d.ts +35 -0
- package/dist-cjs/response-analyzer/index.cjs +14 -0
- package/dist-cjs/response-analyzer/index.d.ts +9 -0
- package/dist-cjs/response-analyzer/object-type-detector.cjs +99 -0
- package/dist-cjs/response-analyzer/object-type-detector.d.ts +42 -0
- package/dist-cjs/response-analyzer/response-analyzer.cjs +101 -0
- package/dist-cjs/response-analyzer/response-analyzer.d.ts +38 -0
- package/dist-cjs/response-analyzer/types.cjs +5 -0
- package/dist-cjs/response-analyzer/types.d.ts +97 -0
- package/dist-cjs/response-fallback-fixer.cjs +126 -0
- package/dist-cjs/response-fallback-fixer.d.ts +11 -0
- package/dist-cjs/runtime-objects.cjs +52 -0
- package/dist-cjs/runtime-objects.d.ts +52 -0
- package/dist-cjs/template-parser.cjs +136 -0
- package/dist-cjs/template-parser.d.ts +58 -0
- package/dist-cjs/template-render-merge.cjs +43 -0
- package/dist-cjs/template-render-merge.d.ts +9 -0
- package/dist-cjs/troubleshooting-helper.cjs +611 -0
- package/dist-cjs/troubleshooting-helper.d.ts +123 -0
- package/dist-cjs/types.cjs +7 -0
- package/dist-cjs/types.d.ts +1173 -0
- package/dist-cjs/usage-tracker.cjs +83 -0
- package/dist-cjs/usage-tracker.d.ts +78 -0
- package/package.json +91 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gateway Conversion Module
|
|
4
|
+
* Handles structured text conversion and two-step conversion
|
|
5
|
+
*
|
|
6
|
+
* Text mode now uses flex-md format exclusively.
|
|
7
|
+
* Two-step conversion parses flex-md directly to JSON without additional LLM call.
|
|
8
|
+
*/
|
|
9
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
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");
|
|
47
|
+
/**
|
|
48
|
+
* Get the current directory - works in both ESM and CJS
|
|
49
|
+
* In CJS: uses __dirname
|
|
50
|
+
* In ESM: uses import.meta.url (accessed via eval to avoid TS errors in CJS build)
|
|
51
|
+
*/
|
|
52
|
+
async function getCurrentDir() {
|
|
53
|
+
if (typeof __dirname !== 'undefined') {
|
|
54
|
+
return __dirname;
|
|
55
|
+
}
|
|
56
|
+
// ESM: use import.meta.url
|
|
57
|
+
// Use Function constructor to access import.meta without TypeScript errors
|
|
58
|
+
try {
|
|
59
|
+
const getMetaUrl = new Function('return import.meta.url');
|
|
60
|
+
const metaUrl = getMetaUrl();
|
|
61
|
+
if (metaUrl) {
|
|
62
|
+
const url = await Promise.resolve().then(() => __importStar(require('url')));
|
|
63
|
+
const path = await Promise.resolve().then(() => __importStar(require('path')));
|
|
64
|
+
return path.dirname(url.fileURLToPath(metaUrl));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
// Fall through to process.cwd()
|
|
69
|
+
}
|
|
70
|
+
// Fallback
|
|
71
|
+
return process.cwd();
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Invoke structured text mode (first step of two-step conversion)
|
|
75
|
+
*/
|
|
76
|
+
async function invokeStructuredText(request, gateway) {
|
|
77
|
+
// Create a new request with structured-text mode
|
|
78
|
+
const structuredRequest = {
|
|
79
|
+
...request,
|
|
80
|
+
jobId: `${request.jobId}-step1`
|
|
81
|
+
};
|
|
82
|
+
// Make the call (this will recursively call invoke, but with structured-text mode)
|
|
83
|
+
return await gateway.invoke(structuredRequest);
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Get default conversion instructions for two-step mode
|
|
87
|
+
*
|
|
88
|
+
* NOTE: This function is no longer used in two-step conversion.
|
|
89
|
+
* Two-step mode now parses flex-md directly using the flex-md package.
|
|
90
|
+
* This function is kept for backward compatibility only.
|
|
91
|
+
*/
|
|
92
|
+
function getDefaultConversionInstructions() {
|
|
93
|
+
return `You are a data converter. Your task is to convert structured text into valid JSON.
|
|
94
|
+
|
|
95
|
+
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.
|
|
96
|
+
|
|
97
|
+
Your response must be ONLY a valid JSON object with no other text. The JSON should preserve all information from the structured text in a structured format.
|
|
98
|
+
|
|
99
|
+
CRITICAL RULES:
|
|
100
|
+
- Your ENTIRE response must be parseable JSON
|
|
101
|
+
- Do NOT write conversational text outside the JSON
|
|
102
|
+
- Response must START with { and END with }
|
|
103
|
+
- Preserve all information from the structured text
|
|
104
|
+
- Use appropriate JSON data types (strings, numbers, arrays, objects)
|
|
105
|
+
- If the structured text contains a story or narrative, structure it appropriately in JSON
|
|
106
|
+
- Extract key elements and organize them logically`;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Convert flex-md structured text to JSON (second step of two-step conversion)
|
|
110
|
+
*
|
|
111
|
+
* This function now parses flex-md directly using the flex-md package,
|
|
112
|
+
* eliminating the need for an additional LLM call.
|
|
113
|
+
*/
|
|
114
|
+
async function convertStructuredToJson(structuredText, originalRequest, config, gateway, logger) {
|
|
115
|
+
logger.debug('Two-step conversion: Parsing flex-md text directly using flex-md SDK 3.0.0', {
|
|
116
|
+
aiRequestId: originalRequest.aiRequestId,
|
|
117
|
+
structuredTextLength: structuredText.length,
|
|
118
|
+
usingFlexMd: true
|
|
119
|
+
});
|
|
120
|
+
try {
|
|
121
|
+
// Use flex-md 3.0.0 to extract and parse JSON
|
|
122
|
+
const flexMdResult = await (0, flex_md_loader_js_1.extractJsonFromFlexMd)(structuredText);
|
|
123
|
+
if (!flexMdResult || !flexMdResult.json) {
|
|
124
|
+
throw new Error('flex-md SDK could not extract JSON from structured text');
|
|
125
|
+
}
|
|
126
|
+
const parsedJson = flexMdResult.json;
|
|
127
|
+
// Create response structure matching EnhancedLLMResponse
|
|
128
|
+
const conversionResponse = {
|
|
129
|
+
content: structuredText, // Keep original flex-md text
|
|
130
|
+
rawText: structuredText,
|
|
131
|
+
parsedContent: parsedJson, // Parsed JSON from flex-md
|
|
132
|
+
requestId: originalRequest.aiRequestId,
|
|
133
|
+
provider: 'conversion',
|
|
134
|
+
rawResponse: structuredText,
|
|
135
|
+
metadata: {
|
|
136
|
+
aiRequestId: originalRequest.aiRequestId,
|
|
137
|
+
agentId: originalRequest.agentId,
|
|
138
|
+
latencyMs: 0, // No LLM call, so no latency
|
|
139
|
+
tokens: {
|
|
140
|
+
prompt: 0,
|
|
141
|
+
completion: 0,
|
|
142
|
+
total: 0
|
|
143
|
+
},
|
|
144
|
+
model: 'flex-md-parser', // Indicate we used flex-md parser
|
|
145
|
+
provider: 'flex-md',
|
|
146
|
+
isTwoStepConversion: true,
|
|
147
|
+
structuredTextStep: 'second',
|
|
148
|
+
contentType: 'object', // Parsed JSON is an object
|
|
149
|
+
conversionMethod: 'flex-md-direct-parse'
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
logger.info('Two-step conversion completed using flex-md parser', {
|
|
153
|
+
aiRequestId: originalRequest.aiRequestId,
|
|
154
|
+
parsedContentType: typeof parsedJson,
|
|
155
|
+
isObject: typeof parsedJson === 'object' && parsedJson !== null
|
|
156
|
+
});
|
|
157
|
+
return conversionResponse;
|
|
158
|
+
}
|
|
159
|
+
catch (parseError) {
|
|
160
|
+
const error = parseError instanceof Error ? parseError : new Error(String(parseError));
|
|
161
|
+
logger.error('Two-step conversion failed: flex-md parse error', {
|
|
162
|
+
jobId: originalRequest.jobId,
|
|
163
|
+
error: error.message,
|
|
164
|
+
errorName: error.name,
|
|
165
|
+
structuredTextPreview: structuredText.substring(0, 200)
|
|
166
|
+
});
|
|
167
|
+
// Create error report for flex-md parsing issue
|
|
168
|
+
let flexMdVersion = 'unknown';
|
|
169
|
+
try {
|
|
170
|
+
// Use type assertion to allow dynamic import of package.json
|
|
171
|
+
const flexMdPkg = await Promise.resolve(`${'flex-md/package.json'}`).then(s => __importStar(require(s)));
|
|
172
|
+
flexMdVersion = flexMdPkg?.version || flexMdPkg?.default?.version || 'unknown';
|
|
173
|
+
}
|
|
174
|
+
catch {
|
|
175
|
+
// Package.json not accessible
|
|
176
|
+
}
|
|
177
|
+
const report = {
|
|
178
|
+
package: 'flex-md',
|
|
179
|
+
version: flexMdVersion,
|
|
180
|
+
issue: 'parse function threw error',
|
|
181
|
+
error: error.message,
|
|
182
|
+
errorName: error.name,
|
|
183
|
+
stack: error.stack,
|
|
184
|
+
timestamp: new Date().toISOString(),
|
|
185
|
+
request: {
|
|
186
|
+
jobId: originalRequest.jobId,
|
|
187
|
+
agentId: originalRequest.agentId
|
|
188
|
+
},
|
|
189
|
+
inputPreview: structuredText.substring(0, 500),
|
|
190
|
+
inputLength: structuredText.length,
|
|
191
|
+
environment: {
|
|
192
|
+
nodeVersion: process.version,
|
|
193
|
+
platform: process.platform
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
// Write report to docs directory
|
|
197
|
+
try {
|
|
198
|
+
const fs = await Promise.resolve().then(() => __importStar(require('fs')));
|
|
199
|
+
const path = await Promise.resolve().then(() => __importStar(require('path')));
|
|
200
|
+
const currentDir = await getCurrentDir();
|
|
201
|
+
const reportPath = path.join(currentDir, '../../docs/FLEX_MD_PACKAGE_ISSUE_REPORT.json');
|
|
202
|
+
fs.writeFileSync(reportPath, JSON.stringify(report, null, 2));
|
|
203
|
+
logger.error('flex-md parsing error report written', { reportPath });
|
|
204
|
+
}
|
|
205
|
+
catch (writeError) {
|
|
206
|
+
logger.error('Failed to write flex-md parsing error report', {
|
|
207
|
+
error: writeError instanceof Error ? writeError.message : String(writeError)
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
throw new Error(`flex-md parsing failed: ${error.message}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway Conversion Module
|
|
3
|
+
* Handles structured text conversion and two-step conversion
|
|
4
|
+
*
|
|
5
|
+
* Text mode now uses flex-md format exclusively.
|
|
6
|
+
* Two-step conversion parses flex-md directly to JSON without additional LLM call.
|
|
7
|
+
*/
|
|
8
|
+
import type { AIRequest, EnhancedLLMResponse, GatewayConfig } from './types.js';
|
|
9
|
+
import type { Logxer } from '@x12i/logxer';
|
|
10
|
+
import type { AIGateway } from './gateway.js';
|
|
11
|
+
/**
|
|
12
|
+
* Invoke structured text mode (first step of two-step conversion)
|
|
13
|
+
*/
|
|
14
|
+
export declare function invokeStructuredText(request: AIRequest, gateway: AIGateway): Promise<EnhancedLLMResponse<string>>;
|
|
15
|
+
/**
|
|
16
|
+
* Get default conversion instructions for two-step mode
|
|
17
|
+
*
|
|
18
|
+
* NOTE: This function is no longer used in two-step conversion.
|
|
19
|
+
* Two-step mode now parses flex-md directly using the flex-md package.
|
|
20
|
+
* This function is kept for backward compatibility only.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getDefaultConversionInstructions(): string;
|
|
23
|
+
/**
|
|
24
|
+
* Convert flex-md structured text to JSON (second step of two-step conversion)
|
|
25
|
+
*
|
|
26
|
+
* This function now parses flex-md directly using the flex-md package,
|
|
27
|
+
* eliminating the need for an additional LLM call.
|
|
28
|
+
*/
|
|
29
|
+
export declare function convertStructuredToJson(structuredText: string, originalRequest: AIRequest, config: GatewayConfig, gateway: AIGateway, logger: Logxer): Promise<EnhancedLLMResponse>;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gateway Instructions Module
|
|
4
|
+
* Handles instructions block resolution from inline config only (no content registry).
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.resolveNestedInstructionsBlock = resolveNestedInstructionsBlock;
|
|
8
|
+
exports.resolveInstructionsBlock = resolveInstructionsBlock;
|
|
9
|
+
exports.getPreParsedInstructions = getPreParsedInstructions;
|
|
10
|
+
const instruction_errors_js_1 = require("./instruction-errors.cjs");
|
|
11
|
+
function getNestedString(blocks, dotPath) {
|
|
12
|
+
const parts = dotPath.split('.');
|
|
13
|
+
let cur = blocks;
|
|
14
|
+
for (const p of parts) {
|
|
15
|
+
if (cur == null || typeof cur !== 'object')
|
|
16
|
+
return undefined;
|
|
17
|
+
cur = cur[p];
|
|
18
|
+
}
|
|
19
|
+
return typeof cur === 'string' ? cur : undefined;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Resolves nested instructionsBlocks (e.g., "input.inputRecognitionRule")
|
|
23
|
+
* Supports dot notation for nested object access in defaultInstructionsBlocks,
|
|
24
|
+
* then flat keys in merged overrides.
|
|
25
|
+
*/
|
|
26
|
+
async function resolveNestedInstructionsBlock(blockPath, _agentId, _taskTypeId, context) {
|
|
27
|
+
return resolveInstructionsBlock(blockPath, _agentId, _taskTypeId, context);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Resolves instructionsBlocks from config overrides, request overrides, nested defaults, or flat defaults.
|
|
31
|
+
*/
|
|
32
|
+
async function resolveInstructionsBlock(blockName, agentId, taskTypeId, context) {
|
|
33
|
+
const { config, logger, defaultInstructionsBlocks, instructionsBlockOverrides, requestInstructionsBlocks } = context;
|
|
34
|
+
logger.verbose('Resolving instructionsBlock', {
|
|
35
|
+
blockName,
|
|
36
|
+
agentId,
|
|
37
|
+
taskTypeId,
|
|
38
|
+
hasConfigOverride: !!config.instructionsBlocks?.[blockName]
|
|
39
|
+
});
|
|
40
|
+
const configOverride = config.instructionsBlocks?.[blockName];
|
|
41
|
+
if (typeof configOverride === 'string' && configOverride.trim()) {
|
|
42
|
+
logger.debug('Using per-call config override for instructionsBlock', { blockName, agentId });
|
|
43
|
+
return configOverride;
|
|
44
|
+
}
|
|
45
|
+
const fromRequest = requestInstructionsBlocks?.[blockName];
|
|
46
|
+
if (typeof fromRequest === 'string' && fromRequest.trim()) {
|
|
47
|
+
logger.debug('Using request.config.instructionsBlocks for instructionsBlock', { blockName, agentId });
|
|
48
|
+
return fromRequest;
|
|
49
|
+
}
|
|
50
|
+
const fromGateway = instructionsBlockOverrides[blockName];
|
|
51
|
+
if (typeof fromGateway === 'string' && fromGateway.trim()) {
|
|
52
|
+
logger.debug('Using gateway instructionsBlocks override for instructionsBlock', { blockName, agentId });
|
|
53
|
+
return fromGateway;
|
|
54
|
+
}
|
|
55
|
+
const nested = getNestedString(defaultInstructionsBlocks, blockName);
|
|
56
|
+
if (nested?.trim()) {
|
|
57
|
+
logger.debug('Using nested defaultInstructionsBlocks for instructionsBlock', { blockName, agentId });
|
|
58
|
+
return nested;
|
|
59
|
+
}
|
|
60
|
+
throw new instruction_errors_js_1.InstructionNotFoundError(blockName, 'instructions-blocks', `InstructionsBlock "${blockName}" not found. Provide it via packaged defaults, gateway instructionsBlocks, or request.config.instructionsBlocks.`, blockName);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Pre-parse instructions string for stable hashing (taskTypeId). No external resolution.
|
|
64
|
+
*/
|
|
65
|
+
function getPreParsedInstructions(instructions) {
|
|
66
|
+
return instructions ?? '';
|
|
67
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway Instructions Module
|
|
3
|
+
* Handles instructions block resolution from inline config only (no content registry).
|
|
4
|
+
*/
|
|
5
|
+
import type { Logxer } from '@x12i/logxer';
|
|
6
|
+
export interface InstructionsContext {
|
|
7
|
+
defaultInstructionsBlocks: Record<string, any>;
|
|
8
|
+
/** Flat overrides from gateway `instructionsBlocks` (merged at init). */
|
|
9
|
+
instructionsBlockOverrides: Record<string, string>;
|
|
10
|
+
/** Per-request flat overrides from `request.config.instructionsBlocks`. */
|
|
11
|
+
requestInstructionsBlocks?: Record<string, string>;
|
|
12
|
+
config: {
|
|
13
|
+
instructionsBlocks?: Record<string, any>;
|
|
14
|
+
};
|
|
15
|
+
logger: Logxer;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Resolves nested instructionsBlocks (e.g., "input.inputRecognitionRule")
|
|
19
|
+
* Supports dot notation for nested object access in defaultInstructionsBlocks,
|
|
20
|
+
* then flat keys in merged overrides.
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveNestedInstructionsBlock(blockPath: string, _agentId: string, _taskTypeId: string | undefined, context: InstructionsContext): Promise<string>;
|
|
23
|
+
/**
|
|
24
|
+
* Resolves instructionsBlocks from config overrides, request overrides, nested defaults, or flat defaults.
|
|
25
|
+
*/
|
|
26
|
+
export declare function resolveInstructionsBlock(blockName: string, agentId: string, taskTypeId: string | undefined, context: InstructionsContext): Promise<string>;
|
|
27
|
+
/**
|
|
28
|
+
* Pre-parse instructions string for stable hashing (taskTypeId). No external resolution.
|
|
29
|
+
*/
|
|
30
|
+
export declare function getPreParsedInstructions(instructions: string | undefined): string;
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Gateway Memory Module
|
|
4
|
+
* Handles working memory and template parameter resolution
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.buildWorkingMemory = buildWorkingMemory;
|
|
8
|
+
exports.resolveTemplateParams = resolveTemplateParams;
|
|
9
|
+
// Type guard to check if request is AIRequest (has primaryObjectType)
|
|
10
|
+
function isAIRequest(request) {
|
|
11
|
+
return 'primaryObjectType' in request &&
|
|
12
|
+
typeof request.primaryObjectType === 'object' &&
|
|
13
|
+
request.primaryObjectType !== null &&
|
|
14
|
+
typeof request.primaryObjectType.type === 'string';
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Builds workingMemory structure from request fields, enriching with metadata
|
|
18
|
+
* Merges existing workingMemory (from request or memory component) with request metadata
|
|
19
|
+
*
|
|
20
|
+
* Implements tiered token resolution:
|
|
21
|
+
* - Tier 1 (highest): templateTokens (handled in resolveTemplateParams, merged into shortTermMemory)
|
|
22
|
+
* - Tier 2: workingMemory (this method) - checks existing workingMemory first
|
|
23
|
+
* - Tier 3: derived from request fields or other memories (fallback)
|
|
24
|
+
*/
|
|
25
|
+
function buildWorkingMemory(request, existingWorkingMemory, otherMemories) {
|
|
26
|
+
// Start with existing workingMemory if provided, otherwise empty object
|
|
27
|
+
const workingMemory = existingWorkingMemory && typeof existingWorkingMemory === 'object'
|
|
28
|
+
? { ...existingWorkingMemory }
|
|
29
|
+
: request.workingMemory && typeof request.workingMemory === 'object'
|
|
30
|
+
? { ...request.workingMemory }
|
|
31
|
+
: {};
|
|
32
|
+
// Ensure required job and task fields exist (required by Rendrix)
|
|
33
|
+
if (!workingMemory.job) {
|
|
34
|
+
workingMemory.job = {};
|
|
35
|
+
}
|
|
36
|
+
if (!workingMemory.task) {
|
|
37
|
+
workingMemory.task = {};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Token Resolution with Tiered Fallback
|
|
41
|
+
* Tier 1: templateTokens (handled in resolveTemplateParams, merged into shortTermMemory)
|
|
42
|
+
* Tier 2: workingMemory (check existing workingMemory first)
|
|
43
|
+
* Tier 3: derive from request fields or other memories
|
|
44
|
+
*/
|
|
45
|
+
// Resolve {{taskDescription}} token
|
|
46
|
+
// Tier 2: Check workingMemory first
|
|
47
|
+
// Tier 3: Fallback to request.instructions or derive from task.objective
|
|
48
|
+
if (workingMemory.taskDescription === undefined) {
|
|
49
|
+
// Tier 3: Derive from request.instructions (if available)
|
|
50
|
+
if (request.instructions) {
|
|
51
|
+
workingMemory.taskDescription = request.instructions;
|
|
52
|
+
}
|
|
53
|
+
else if (workingMemory.task?.objective) {
|
|
54
|
+
// Fallback to task.objective if instructions not available
|
|
55
|
+
workingMemory.taskDescription = workingMemory.task.objective;
|
|
56
|
+
}
|
|
57
|
+
else if (otherMemories) {
|
|
58
|
+
// Tier 3: Check other memories (experienceMemory, knowledgeMemory)
|
|
59
|
+
// Note: shortTermMemory is tier 1, so we skip it here
|
|
60
|
+
const fromExp = otherMemories.experienceMemory?.['taskDescription'];
|
|
61
|
+
const fromKnow = otherMemories.knowledgeMemory?.['taskDescription'];
|
|
62
|
+
if (fromExp !== undefined) {
|
|
63
|
+
workingMemory.taskDescription = fromExp;
|
|
64
|
+
}
|
|
65
|
+
else if (fromKnow !== undefined) {
|
|
66
|
+
workingMemory.taskDescription = fromKnow;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
// Resolve {{input}} token
|
|
71
|
+
// Input must come from workingMemory.input (request.input has been removed)
|
|
72
|
+
// If not in workingMemory, check other memories as fallback
|
|
73
|
+
if (workingMemory.input === undefined && otherMemories) {
|
|
74
|
+
const fromExp = otherMemories.experienceMemory?.['input'];
|
|
75
|
+
const fromKnow = otherMemories.knowledgeMemory?.['input'];
|
|
76
|
+
if (fromExp !== undefined) {
|
|
77
|
+
workingMemory.input = fromExp;
|
|
78
|
+
}
|
|
79
|
+
else if (fromKnow !== undefined) {
|
|
80
|
+
workingMemory.input = fromKnow;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
// Resolve {{role}} token
|
|
84
|
+
// Tier 2: Check workingMemory first
|
|
85
|
+
// Tier 3: Derive from request.agentType or message role context
|
|
86
|
+
if (workingMemory.role === undefined) {
|
|
87
|
+
// Tier 3: Derive from request.agentType or default to context
|
|
88
|
+
if (request.agentType) {
|
|
89
|
+
workingMemory.role = request.agentType;
|
|
90
|
+
}
|
|
91
|
+
else if (isAIRequest(request)) {
|
|
92
|
+
workingMemory.role = 'ai-reasoner';
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
workingMemory.role = 'chat';
|
|
96
|
+
}
|
|
97
|
+
// Tier 3: Check other memories (can override derivation)
|
|
98
|
+
if (otherMemories) {
|
|
99
|
+
const fromExp = otherMemories.experienceMemory?.['role'];
|
|
100
|
+
const fromKnow = otherMemories.knowledgeMemory?.['role'];
|
|
101
|
+
if (fromExp !== undefined) {
|
|
102
|
+
workingMemory.role = fromExp;
|
|
103
|
+
}
|
|
104
|
+
else if (fromKnow !== undefined) {
|
|
105
|
+
workingMemory.role = fromKnow;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
// Populate job fields from request if not already present in existing memory
|
|
110
|
+
if (!workingMemory.job.objective && request.instructions) {
|
|
111
|
+
workingMemory.job.objective = request.instructions;
|
|
112
|
+
}
|
|
113
|
+
if (!workingMemory.job.context && request.context) {
|
|
114
|
+
workingMemory.job.context = request.context;
|
|
115
|
+
}
|
|
116
|
+
// Input field has been removed - data should come from workingMemory.input
|
|
117
|
+
if (!workingMemory.job.narrative && request.prompt) {
|
|
118
|
+
workingMemory.job.narrative = request.prompt;
|
|
119
|
+
}
|
|
120
|
+
if (!workingMemory.job.id && request.jobId) {
|
|
121
|
+
workingMemory.job.id = request.jobId;
|
|
122
|
+
}
|
|
123
|
+
// Populate task fields from request if not already present in existing memory
|
|
124
|
+
if (!workingMemory.task.objective && request.instructions) {
|
|
125
|
+
workingMemory.task.objective = request.instructions;
|
|
126
|
+
}
|
|
127
|
+
if (!workingMemory.task.context && request.context) {
|
|
128
|
+
workingMemory.task.context = request.context;
|
|
129
|
+
}
|
|
130
|
+
// Input field has been removed - data should come from workingMemory.input
|
|
131
|
+
if (!workingMemory.task.id && request.taskId) {
|
|
132
|
+
workingMemory.task.id = request.taskId;
|
|
133
|
+
}
|
|
134
|
+
// Add entity information if not present
|
|
135
|
+
if (!workingMemory.entity && request.agentId) {
|
|
136
|
+
workingMemory.entity = {
|
|
137
|
+
name: request.agentId,
|
|
138
|
+
type: request.agentType || (isAIRequest(request) ? 'ai-reasoner' : 'chat')
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
return workingMemory;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Resolves template parameters with smart fallback logic
|
|
145
|
+
* Priority: Request args (tier 1) -> Memory component (tier 2) -> Gateway config (tier 3) -> defaults
|
|
146
|
+
*/
|
|
147
|
+
async function resolveTemplateParams(request, config, logger) {
|
|
148
|
+
// Tier 1: Request args (highest priority)
|
|
149
|
+
let workingMemory = request.workingMemory;
|
|
150
|
+
// Tier 2: Memory component integration (if available)
|
|
151
|
+
// When memoryManager is configured, it handles fetching, merging, enriching, and updating
|
|
152
|
+
if (config.memoryManager && config.memoryResolution) {
|
|
153
|
+
try {
|
|
154
|
+
// Build memory context from request
|
|
155
|
+
// Note: Spread request first, then add providedMemories to avoid duplicate properties
|
|
156
|
+
const memoryContext = {
|
|
157
|
+
...request,
|
|
158
|
+
providedMemories: {
|
|
159
|
+
workingMemory
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
// Determine which memories need to be fetched
|
|
163
|
+
const memoryResolution = config.memoryResolution;
|
|
164
|
+
const needsWorkingMemory = memoryResolution.fetchWorkingMemory !== false && !workingMemory;
|
|
165
|
+
// Call memoryManager.resolveMemories() which handles:
|
|
166
|
+
// 1. Fetching missing memories from storage
|
|
167
|
+
// 2. Merging input memories with fetched memories
|
|
168
|
+
// 3. Enriching memories if configured
|
|
169
|
+
// 4. Updating existing memories in storage if updateExistingMemories is true
|
|
170
|
+
// 5. Returning final merged memory
|
|
171
|
+
const memoryManager = config.memoryManager;
|
|
172
|
+
if (memoryManager && typeof memoryManager.resolveMemories === 'function') {
|
|
173
|
+
const resolved = await memoryManager.resolveMemories(memoryContext, {
|
|
174
|
+
fetchWorkingMemory: needsWorkingMemory,
|
|
175
|
+
updateExistingMemories: memoryResolution.updateExistingMemories ?? false,
|
|
176
|
+
updateScope: memoryResolution.updateScope ?? ['workingMemory'],
|
|
177
|
+
enrichment: memoryResolution.enrichment ?? { experience: 'none', knowledge: 'none' },
|
|
178
|
+
resolutionStrategy: memoryResolution.resolutionStrategy ?? 'fetch-on-demand'
|
|
179
|
+
});
|
|
180
|
+
// Use resolved memories (already merged from input + fetched, enriched if configured)
|
|
181
|
+
// Input memories take precedence, but resolved memories fill in missing pieces
|
|
182
|
+
workingMemory = resolved.workingMemory || workingMemory;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
// If memory component fails, fall back to request-only mode
|
|
187
|
+
logger?.warn('Memory component resolution failed, using request-only mode', {
|
|
188
|
+
error: error instanceof Error ? error.message : String(error),
|
|
189
|
+
jobId: request.jobId
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Build proper workingMemory structure (merge with request fields if needed)
|
|
194
|
+
// This implements tiered token resolution: tier 2 (workingMemory) and tier 3 (derive from request fields)
|
|
195
|
+
const finalWorkingMemory = buildWorkingMemory(request, workingMemory);
|
|
196
|
+
// Merge templateTokens (tier 1 - highest priority) into shortTermMemory AFTER memory resolution
|
|
197
|
+
// This ensures templateTokens override everything (workingMemory and other memories)
|
|
198
|
+
// Rendrix priority: shortTermMemory > workingMemory > experienceMemory > knowledgeMemory
|
|
199
|
+
if (request.templateTokens && Object.keys(request.templateTokens).length > 0) {
|
|
200
|
+
logger?.debug('Merged templateTokens into shortTermMemory (tier 1 - highest priority)', {
|
|
201
|
+
jobId: request.jobId,
|
|
202
|
+
tokenKeys: Object.keys(request.templateTokens)
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
// Note: taskConfig removed - Rendrix 3.0.0+ no longer accepts it
|
|
206
|
+
// taskConfig is deprecated and no longer used
|
|
207
|
+
return {
|
|
208
|
+
workingMemory: finalWorkingMemory
|
|
209
|
+
// taskConfig removed - Rendrix 3.0.0+ no longer uses it
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Gateway Memory Module
|
|
3
|
+
* Handles working memory and template parameter resolution
|
|
4
|
+
*/
|
|
5
|
+
import type { ChatRequest, AIRequest, GatewayConfig } from './types.js';
|
|
6
|
+
import type { Logxer } from '@x12i/logxer';
|
|
7
|
+
type Request = ChatRequest | AIRequest;
|
|
8
|
+
/**
|
|
9
|
+
* Builds workingMemory structure from request fields, enriching with metadata
|
|
10
|
+
* Merges existing workingMemory (from request or memory component) with request metadata
|
|
11
|
+
*
|
|
12
|
+
* Implements tiered token resolution:
|
|
13
|
+
* - Tier 1 (highest): templateTokens (handled in resolveTemplateParams, merged into shortTermMemory)
|
|
14
|
+
* - Tier 2: workingMemory (this method) - checks existing workingMemory first
|
|
15
|
+
* - Tier 3: derived from request fields or other memories (fallback)
|
|
16
|
+
*/
|
|
17
|
+
export declare function buildWorkingMemory(request: Request, existingWorkingMemory?: unknown, otherMemories?: {
|
|
18
|
+
shortTermMemory?: {
|
|
19
|
+
[key: string]: any;
|
|
20
|
+
};
|
|
21
|
+
experienceMemory?: {
|
|
22
|
+
[key: string]: any;
|
|
23
|
+
};
|
|
24
|
+
knowledgeMemory?: {
|
|
25
|
+
[key: string]: any;
|
|
26
|
+
};
|
|
27
|
+
}): unknown;
|
|
28
|
+
/**
|
|
29
|
+
* Resolves template parameters with smart fallback logic
|
|
30
|
+
* Priority: Request args (tier 1) -> Memory component (tier 2) -> Gateway config (tier 3) -> defaults
|
|
31
|
+
*/
|
|
32
|
+
export declare function resolveTemplateParams(request: Request, config: GatewayConfig, logger: Logxer): Promise<{
|
|
33
|
+
workingMemory: unknown;
|
|
34
|
+
shortTermMemory?: {
|
|
35
|
+
[key: string]: string | number | boolean | object | object[];
|
|
36
|
+
};
|
|
37
|
+
experienceMemory?: {
|
|
38
|
+
knowHows?: object | object[] | string[] | string;
|
|
39
|
+
narratives?: object | object[] | string[] | string;
|
|
40
|
+
experiences?: object | object[] | string[] | string;
|
|
41
|
+
insights?: object | object[] | string[] | string;
|
|
42
|
+
learnings?: object | object[] | string[] | string;
|
|
43
|
+
lessons?: object | object[] | string[] | string;
|
|
44
|
+
frameworks?: object | object[] | string[] | string;
|
|
45
|
+
tools?: object | object[] | string[] | string;
|
|
46
|
+
};
|
|
47
|
+
knowledgeMemory?: {
|
|
48
|
+
[key: string]: string | number | boolean | object | object[];
|
|
49
|
+
};
|
|
50
|
+
}>;
|
|
51
|
+
export {};
|