@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,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Request Report Generator
|
|
4
|
+
*
|
|
5
|
+
* Generates comprehensive request reports with validation, examples, and structured text information
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.generateRequestReport = generateRequestReport;
|
|
9
|
+
/**
|
|
10
|
+
* Extract validation rules from markdown
|
|
11
|
+
*/
|
|
12
|
+
function extractValidationRules(markdown) {
|
|
13
|
+
if (!markdown)
|
|
14
|
+
return [];
|
|
15
|
+
const rules = [];
|
|
16
|
+
// Look for validation rules sections
|
|
17
|
+
const rulesMatch = markdown.match(/## Validation Rules?\n([\s\S]*?)(?=\n##|$)/i);
|
|
18
|
+
if (rulesMatch) {
|
|
19
|
+
const rulesText = rulesMatch[1];
|
|
20
|
+
// Extract list items
|
|
21
|
+
const ruleItems = rulesText.match(/[-*]\s+(.+)/g);
|
|
22
|
+
if (ruleItems) {
|
|
23
|
+
ruleItems.forEach(item => {
|
|
24
|
+
rules.push({ rule: item.replace(/[-*]\s+/, '').trim() });
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return rules;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Extract validation code from markdown
|
|
32
|
+
*/
|
|
33
|
+
function extractValidationCode(markdown) {
|
|
34
|
+
if (!markdown)
|
|
35
|
+
return undefined;
|
|
36
|
+
// Look for TypeScript code blocks
|
|
37
|
+
const codeBlockMatch = markdown.match(/```typescript\n([\s\S]*?)\n```/);
|
|
38
|
+
if (codeBlockMatch) {
|
|
39
|
+
return codeBlockMatch[1];
|
|
40
|
+
}
|
|
41
|
+
// Also try JavaScript
|
|
42
|
+
const jsCodeBlockMatch = markdown.match(/```javascript\n([\s\S]*?)\n```/);
|
|
43
|
+
if (jsCodeBlockMatch) {
|
|
44
|
+
return jsCodeBlockMatch[1];
|
|
45
|
+
}
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Extract checklist items from markdown
|
|
50
|
+
*/
|
|
51
|
+
function extractChecklist(markdown) {
|
|
52
|
+
if (!markdown)
|
|
53
|
+
return [];
|
|
54
|
+
const checklist = [];
|
|
55
|
+
// Look for checklist section
|
|
56
|
+
const checklistMatch = markdown.match(/## Manual Review Checklist\n([\s\S]*?)(?=\n##|$)/i);
|
|
57
|
+
if (checklistMatch) {
|
|
58
|
+
const checklistText = checklistMatch[1];
|
|
59
|
+
// Extract checklist items (both checked and unchecked)
|
|
60
|
+
const items = checklistText.match(/- \[[ x]\] (.+)/g);
|
|
61
|
+
if (items) {
|
|
62
|
+
items.forEach(item => {
|
|
63
|
+
checklist.push(item.replace(/- \[[ x]\] /, '').trim());
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return checklist;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Extract schema fields from simple format schema
|
|
71
|
+
*/
|
|
72
|
+
function extractSchemaFields(schema) {
|
|
73
|
+
const fields = [];
|
|
74
|
+
for (const [key, value] of Object.entries(schema)) {
|
|
75
|
+
if (typeof value === 'string') {
|
|
76
|
+
// Simple format: "field: '<type> Description'"
|
|
77
|
+
const match = value.match(/^<(\w+)>(.*)$/);
|
|
78
|
+
if (match) {
|
|
79
|
+
fields.push({
|
|
80
|
+
name: key,
|
|
81
|
+
type: match[1],
|
|
82
|
+
required: true, // Assume required unless marked optional
|
|
83
|
+
description: match[2].trim() || undefined
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else if (Array.isArray(value)) {
|
|
87
|
+
// Array field: "field: ['<type> Description']"
|
|
88
|
+
fields.push({
|
|
89
|
+
name: key,
|
|
90
|
+
type: 'array',
|
|
91
|
+
required: true,
|
|
92
|
+
description: 'Array field'
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
else if (typeof value === 'object' && value !== null) {
|
|
96
|
+
// Nested object
|
|
97
|
+
fields.push({
|
|
98
|
+
name: key,
|
|
99
|
+
type: 'object',
|
|
100
|
+
required: true,
|
|
101
|
+
description: 'Nested object'
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else if (typeof value === 'object' && value !== null) {
|
|
106
|
+
// JSON Schema format
|
|
107
|
+
if (Array.isArray(value)) {
|
|
108
|
+
fields.push({
|
|
109
|
+
name: key,
|
|
110
|
+
type: 'array',
|
|
111
|
+
required: true
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
fields.push({
|
|
116
|
+
name: key,
|
|
117
|
+
type: 'object',
|
|
118
|
+
required: true
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return fields;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Find matching standard type for custom schema
|
|
127
|
+
*/
|
|
128
|
+
async function findMatchingStandardType(schema, library // ObjectTypesLibrary removed
|
|
129
|
+
) {
|
|
130
|
+
// Since ObjectTypesLibrary is removed, we can't match against standard types
|
|
131
|
+
// Return null to indicate no matching standard type found
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Compare two schemas for structural similarity
|
|
136
|
+
*/
|
|
137
|
+
function schemasMatch(schema1, schema2) {
|
|
138
|
+
// Simple structure comparison - compare field names
|
|
139
|
+
const keys1 = Object.keys(schema1).sort();
|
|
140
|
+
const keys2 = Object.keys(schema2).sort();
|
|
141
|
+
// Must have same number of fields
|
|
142
|
+
if (keys1.length !== keys2.length) {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
// All field names must match
|
|
146
|
+
return JSON.stringify(keys1) === JSON.stringify(keys2);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Generate comprehensive request report
|
|
150
|
+
*/
|
|
151
|
+
async function generateRequestReport(request, library, // ObjectTypesLibrary removed
|
|
152
|
+
logger) {
|
|
153
|
+
const report = {
|
|
154
|
+
requestId: request.jobId || `req-${Date.now()}`,
|
|
155
|
+
timestamp: Date.now(),
|
|
156
|
+
validation: {},
|
|
157
|
+
examples: { count: 0 },
|
|
158
|
+
structuredText: { available: false },
|
|
159
|
+
schema: {
|
|
160
|
+
format: 'simple-format',
|
|
161
|
+
fields: []
|
|
162
|
+
},
|
|
163
|
+
integrationHints: {
|
|
164
|
+
useStandardType: false,
|
|
165
|
+
validationAvailable: false,
|
|
166
|
+
structuredTextAvailable: false,
|
|
167
|
+
examplesAvailable: false
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
// Always use flex-md parsing - no object type processing needed
|
|
171
|
+
return report;
|
|
172
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Request Report Generator
|
|
3
|
+
*
|
|
4
|
+
* Generates comprehensive request reports with validation, examples, and structured text information
|
|
5
|
+
*/
|
|
6
|
+
import type { AIRequest } from './types.js';
|
|
7
|
+
import type { Logxer } from '@x12i/logxer';
|
|
8
|
+
export interface RequestReport {
|
|
9
|
+
requestId: string;
|
|
10
|
+
timestamp: number;
|
|
11
|
+
validation: {
|
|
12
|
+
instructions?: string;
|
|
13
|
+
rules?: any[];
|
|
14
|
+
automatedCode?: string;
|
|
15
|
+
checklist?: string[];
|
|
16
|
+
};
|
|
17
|
+
examples: {
|
|
18
|
+
count: number;
|
|
19
|
+
normal?: number;
|
|
20
|
+
edge?: number;
|
|
21
|
+
error?: number;
|
|
22
|
+
samples?: Array<{
|
|
23
|
+
input: string;
|
|
24
|
+
output: string;
|
|
25
|
+
metadata?: Record<string, unknown>;
|
|
26
|
+
}>;
|
|
27
|
+
};
|
|
28
|
+
structuredText: {
|
|
29
|
+
available: boolean;
|
|
30
|
+
instructions?: string;
|
|
31
|
+
template?: string;
|
|
32
|
+
guidelines?: string[];
|
|
33
|
+
};
|
|
34
|
+
schema: {
|
|
35
|
+
format: 'simple-format' | 'json-schema';
|
|
36
|
+
fields: Array<{
|
|
37
|
+
name: string;
|
|
38
|
+
type: string;
|
|
39
|
+
required: boolean;
|
|
40
|
+
description?: string;
|
|
41
|
+
}>;
|
|
42
|
+
};
|
|
43
|
+
metadata?: {
|
|
44
|
+
category?: string;
|
|
45
|
+
tags?: string[];
|
|
46
|
+
version?: string;
|
|
47
|
+
};
|
|
48
|
+
integrationHints: {
|
|
49
|
+
useStandardType: boolean;
|
|
50
|
+
recommendedType?: string;
|
|
51
|
+
validationAvailable: boolean;
|
|
52
|
+
structuredTextAvailable: boolean;
|
|
53
|
+
examplesAvailable: boolean;
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Generate comprehensive request report
|
|
58
|
+
*/
|
|
59
|
+
export declare function generateRequestReport(request: AIRequest, library: any, // ObjectTypesLibrary removed
|
|
60
|
+
logger: Logxer): Promise<RequestReport>;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Format Type Detector
|
|
4
|
+
*
|
|
5
|
+
* Detects text format types by analyzing text characteristics:
|
|
6
|
+
* - Sentence length and count
|
|
7
|
+
* - Paragraph count
|
|
8
|
+
* - Bullet points / numbered lists
|
|
9
|
+
* - Technical terms
|
|
10
|
+
* - Word/character count
|
|
11
|
+
* - Structure patterns
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.FormatTypeDetector = void 0;
|
|
15
|
+
/**
|
|
16
|
+
* Detects format type of text content
|
|
17
|
+
*/
|
|
18
|
+
class FormatTypeDetector {
|
|
19
|
+
/**
|
|
20
|
+
* Detects format type from text
|
|
21
|
+
*
|
|
22
|
+
* @param text - Text content to analyze
|
|
23
|
+
* @returns Detected format type with confidence
|
|
24
|
+
*/
|
|
25
|
+
detect(text) {
|
|
26
|
+
if (!text || typeof text !== 'string' || text.trim().length === 0) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const trimmed = text.trim();
|
|
30
|
+
const characteristics = this.analyzeCharacteristics(trimmed);
|
|
31
|
+
// Apply heuristics to determine format type
|
|
32
|
+
return this.applyHeuristics(characteristics);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Analyzes text characteristics
|
|
36
|
+
*/
|
|
37
|
+
analyzeCharacteristics(text) {
|
|
38
|
+
const sentences = text.split(/[.!?]+/).filter(s => s.trim().length > 0);
|
|
39
|
+
const paragraphs = text.split(/\n\s*\n/).filter(p => p.trim().length > 0);
|
|
40
|
+
const words = text.split(/\s+/).filter(w => w.length > 0);
|
|
41
|
+
// Detect bullet points (•, -, *, etc.)
|
|
42
|
+
const bulletPattern = /^[\s]*[•\-\*\+]\s+/gm;
|
|
43
|
+
const bullets = text.match(bulletPattern) || [];
|
|
44
|
+
// Detect numbered lists (1., 2., etc.)
|
|
45
|
+
const numberedPattern = /^[\s]*\d+[\.\)]\s+/gm;
|
|
46
|
+
const numbered = text.match(numberedPattern) || [];
|
|
47
|
+
// Simple technical term detection (words with common technical suffixes/patterns)
|
|
48
|
+
const technicalPattern = /\b(api|sdk|json|xml|http|https|sql|regex|async|await|function|class|interface|type|config|param|return)\b/gi;
|
|
49
|
+
const hasTechnicalTerms = technicalPattern.test(text);
|
|
50
|
+
const totalSentenceLength = sentences.reduce((sum, s) => sum + s.length, 0);
|
|
51
|
+
const avgSentenceLength = sentences.length > 0 ? totalSentenceLength / sentences.length : 0;
|
|
52
|
+
return {
|
|
53
|
+
sentenceCount: sentences.length,
|
|
54
|
+
avgSentenceLength,
|
|
55
|
+
paragraphCount: paragraphs.length,
|
|
56
|
+
wordCount: words.length,
|
|
57
|
+
charCount: text.length,
|
|
58
|
+
hasBullets: bullets.length > 0,
|
|
59
|
+
hasNumbered: numbered.length > 0,
|
|
60
|
+
bulletCount: bullets.length,
|
|
61
|
+
numberedCount: numbered.length,
|
|
62
|
+
hasTechnicalTerms
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Applies heuristics to determine format type
|
|
67
|
+
*/
|
|
68
|
+
applyHeuristics(characteristics) {
|
|
69
|
+
const { sentenceCount, avgSentenceLength, paragraphCount, wordCount, hasBullets, hasNumbered, bulletCount, numberedCount, hasTechnicalTerms } = characteristics;
|
|
70
|
+
// Bulleted list format
|
|
71
|
+
if (hasBullets && bulletCount >= 2) {
|
|
72
|
+
return {
|
|
73
|
+
formatType: 'bulleted',
|
|
74
|
+
confidence: Math.min(0.9, 0.6 + (bulletCount * 0.05))
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
// Numbered list format
|
|
78
|
+
if (hasNumbered && numberedCount >= 2) {
|
|
79
|
+
return {
|
|
80
|
+
formatType: 'numbered',
|
|
81
|
+
confidence: Math.min(0.9, 0.6 + (numberedCount * 0.05))
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// Concise format: short sentences, low word count, few paragraphs
|
|
85
|
+
if (avgSentenceLength < 50 && wordCount < 50 && paragraphCount <= 1) {
|
|
86
|
+
return {
|
|
87
|
+
formatType: 'concise',
|
|
88
|
+
confidence: 0.8
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
// Explanatory format: multiple paragraphs, longer sentences
|
|
92
|
+
if (paragraphCount >= 2 && avgSentenceLength > 80) {
|
|
93
|
+
return {
|
|
94
|
+
formatType: 'explanatory',
|
|
95
|
+
confidence: 0.75
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
// Technical format: technical terms + longer sentences
|
|
99
|
+
if (hasTechnicalTerms && avgSentenceLength > 60) {
|
|
100
|
+
return {
|
|
101
|
+
formatType: 'technical',
|
|
102
|
+
confidence: 0.7
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// Narrative format: multiple sentences, moderate length
|
|
106
|
+
if (sentenceCount >= 3 && avgSentenceLength >= 40 && avgSentenceLength <= 100) {
|
|
107
|
+
return {
|
|
108
|
+
formatType: 'narrative',
|
|
109
|
+
confidence: 0.65
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
// Default: plain text
|
|
113
|
+
return {
|
|
114
|
+
formatType: 'plain',
|
|
115
|
+
confidence: 0.5
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
exports.FormatTypeDetector = FormatTypeDetector;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Format Type Detector
|
|
3
|
+
*
|
|
4
|
+
* Detects text format types by analyzing text characteristics:
|
|
5
|
+
* - Sentence length and count
|
|
6
|
+
* - Paragraph count
|
|
7
|
+
* - Bullet points / numbered lists
|
|
8
|
+
* - Technical terms
|
|
9
|
+
* - Word/character count
|
|
10
|
+
* - Structure patterns
|
|
11
|
+
*/
|
|
12
|
+
export interface DetectedFormatType {
|
|
13
|
+
formatType: string;
|
|
14
|
+
confidence: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Detects format type of text content
|
|
18
|
+
*/
|
|
19
|
+
export declare class FormatTypeDetector {
|
|
20
|
+
/**
|
|
21
|
+
* Detects format type from text
|
|
22
|
+
*
|
|
23
|
+
* @param text - Text content to analyze
|
|
24
|
+
* @returns Detected format type with confidence
|
|
25
|
+
*/
|
|
26
|
+
detect(text: string): DetectedFormatType | null;
|
|
27
|
+
/**
|
|
28
|
+
* Analyzes text characteristics
|
|
29
|
+
*/
|
|
30
|
+
private analyzeCharacteristics;
|
|
31
|
+
/**
|
|
32
|
+
* Applies heuristics to determine format type
|
|
33
|
+
*/
|
|
34
|
+
private applyHeuristics;
|
|
35
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Response Analyzer Module
|
|
4
|
+
*
|
|
5
|
+
* Exports all public APIs for response analysis
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.FormatTypeDetector = exports.ObjectTypeDetector = exports.ResponseAnalyzer = void 0;
|
|
9
|
+
var response_analyzer_js_1 = require("./response-analyzer.cjs");
|
|
10
|
+
Object.defineProperty(exports, "ResponseAnalyzer", { enumerable: true, get: function () { return response_analyzer_js_1.ResponseAnalyzer; } });
|
|
11
|
+
var object_type_detector_js_1 = require("./object-type-detector.cjs");
|
|
12
|
+
Object.defineProperty(exports, "ObjectTypeDetector", { enumerable: true, get: function () { return object_type_detector_js_1.ObjectTypeDetector; } });
|
|
13
|
+
var format_type_detector_js_1 = require("./format-type-detector.cjs");
|
|
14
|
+
Object.defineProperty(exports, "FormatTypeDetector", { enumerable: true, get: function () { return format_type_detector_js_1.FormatTypeDetector; } });
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Response Analyzer Module
|
|
3
|
+
*
|
|
4
|
+
* Exports all public APIs for response analysis
|
|
5
|
+
*/
|
|
6
|
+
export { ResponseAnalyzer } from './response-analyzer.js';
|
|
7
|
+
export { ObjectTypeDetector } from './object-type-detector.js';
|
|
8
|
+
export { FormatTypeDetector } from './format-type-detector.js';
|
|
9
|
+
export type { ObjectTypeDefinition, ResponseAnalyzerConfig, MatchedObjectType, DetectedFormatType, AnalysisResult } from './types.js';
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Object Type Detector
|
|
4
|
+
*
|
|
5
|
+
* Matches objects against predefined type definitions based on properties.
|
|
6
|
+
* Uses a scoring algorithm to determine the best match.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ObjectTypeDetector = void 0;
|
|
10
|
+
/**
|
|
11
|
+
* Detects object types by matching against definitions
|
|
12
|
+
*/
|
|
13
|
+
class ObjectTypeDetector {
|
|
14
|
+
definitions;
|
|
15
|
+
minThreshold;
|
|
16
|
+
constructor(config) {
|
|
17
|
+
this.definitions = config.objectTypeDefinitions;
|
|
18
|
+
this.minThreshold = config.minConfidenceThreshold;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Detects object types in a parsed content object
|
|
22
|
+
*
|
|
23
|
+
* @param parsedContent - Parsed content (object, array, or null)
|
|
24
|
+
* @returns Array of matched types sorted by confidence
|
|
25
|
+
*/
|
|
26
|
+
detect(parsedContent) {
|
|
27
|
+
// Only process objects (not arrays or primitives)
|
|
28
|
+
if (!parsedContent || typeof parsedContent !== 'object' || Array.isArray(parsedContent)) {
|
|
29
|
+
return [];
|
|
30
|
+
}
|
|
31
|
+
const matches = [];
|
|
32
|
+
for (const definition of this.definitions) {
|
|
33
|
+
const confidence = this.calculateMatchConfidence(parsedContent, definition);
|
|
34
|
+
if (confidence >= this.minThreshold) {
|
|
35
|
+
matches.push({
|
|
36
|
+
type: definition,
|
|
37
|
+
confidence
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// Sort by confidence (desc), then priority (desc)
|
|
42
|
+
matches.sort((a, b) => {
|
|
43
|
+
if (a.confidence !== b.confidence) {
|
|
44
|
+
return b.confidence - a.confidence;
|
|
45
|
+
}
|
|
46
|
+
const priorityA = a.type.priority || 1;
|
|
47
|
+
const priorityB = b.type.priority || 1;
|
|
48
|
+
return priorityB - priorityA;
|
|
49
|
+
});
|
|
50
|
+
return matches;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Calculates match confidence for an object against a type definition
|
|
54
|
+
*
|
|
55
|
+
* Algorithm:
|
|
56
|
+
* 1. Count required properties present
|
|
57
|
+
* 2. Count optional properties present
|
|
58
|
+
* 3. Calculate base confidence: (required + optional) / total
|
|
59
|
+
* 4. Boost if all required present: +0.2
|
|
60
|
+
* 5. Boost for optional properties: +0.1 * (optional/total)
|
|
61
|
+
* 6. Apply priority weight: confidence * (1 + (priority-1) * 0.1)
|
|
62
|
+
*
|
|
63
|
+
* @param obj - Object to match
|
|
64
|
+
* @param definition - Type definition to match against
|
|
65
|
+
* @returns Confidence score (0-1)
|
|
66
|
+
*/
|
|
67
|
+
calculateMatchConfidence(obj, definition) {
|
|
68
|
+
const required = definition.requiredProperties || [];
|
|
69
|
+
const optional = definition.optionalProperties || [];
|
|
70
|
+
const totalProperties = required.length + optional.length;
|
|
71
|
+
if (totalProperties === 0) {
|
|
72
|
+
return 0; // No properties to match
|
|
73
|
+
}
|
|
74
|
+
// Count required properties present
|
|
75
|
+
const requiredPresent = required.filter((prop) => obj.hasOwnProperty(prop) && obj[prop] != null).length;
|
|
76
|
+
// Count optional properties present
|
|
77
|
+
const optionalPresent = optional.filter((prop) => obj.hasOwnProperty(prop) && obj[prop] != null).length;
|
|
78
|
+
// Base confidence: proportion of properties found
|
|
79
|
+
let confidence = (requiredPresent + optionalPresent) / totalProperties;
|
|
80
|
+
// Boost if all required properties are present
|
|
81
|
+
if (requiredPresent === required.length && required.length > 0) {
|
|
82
|
+
confidence += 0.2;
|
|
83
|
+
}
|
|
84
|
+
// Boost for optional properties (bonus for having extras)
|
|
85
|
+
if (optional.length > 0) {
|
|
86
|
+
confidence += 0.1 * (optionalPresent / optional.length);
|
|
87
|
+
}
|
|
88
|
+
// Cap at 1.0
|
|
89
|
+
confidence = Math.min(confidence, 1.0);
|
|
90
|
+
// Apply priority weight
|
|
91
|
+
const priority = definition.priority || 1;
|
|
92
|
+
if (priority > 1) {
|
|
93
|
+
confidence = confidence * (1 + (priority - 1) * 0.1);
|
|
94
|
+
confidence = Math.min(confidence, 1.0); // Cap again
|
|
95
|
+
}
|
|
96
|
+
return confidence;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
exports.ObjectTypeDetector = ObjectTypeDetector;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object Type Detector
|
|
3
|
+
*
|
|
4
|
+
* Matches objects against predefined type definitions based on properties.
|
|
5
|
+
* Uses a scoring algorithm to determine the best match.
|
|
6
|
+
*/
|
|
7
|
+
import type { ObjectTypeDefinition, MatchedObjectType } from './types.js';
|
|
8
|
+
export interface ObjectTypeDetectorConfig {
|
|
9
|
+
objectTypeDefinitions: ObjectTypeDefinition[];
|
|
10
|
+
minConfidenceThreshold: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Detects object types by matching against definitions
|
|
14
|
+
*/
|
|
15
|
+
export declare class ObjectTypeDetector {
|
|
16
|
+
private definitions;
|
|
17
|
+
private minThreshold;
|
|
18
|
+
constructor(config: ObjectTypeDetectorConfig);
|
|
19
|
+
/**
|
|
20
|
+
* Detects object types in a parsed content object
|
|
21
|
+
*
|
|
22
|
+
* @param parsedContent - Parsed content (object, array, or null)
|
|
23
|
+
* @returns Array of matched types sorted by confidence
|
|
24
|
+
*/
|
|
25
|
+
detect(parsedContent: any): MatchedObjectType[];
|
|
26
|
+
/**
|
|
27
|
+
* Calculates match confidence for an object against a type definition
|
|
28
|
+
*
|
|
29
|
+
* Algorithm:
|
|
30
|
+
* 1. Count required properties present
|
|
31
|
+
* 2. Count optional properties present
|
|
32
|
+
* 3. Calculate base confidence: (required + optional) / total
|
|
33
|
+
* 4. Boost if all required present: +0.2
|
|
34
|
+
* 5. Boost for optional properties: +0.1 * (optional/total)
|
|
35
|
+
* 6. Apply priority weight: confidence * (1 + (priority-1) * 0.1)
|
|
36
|
+
*
|
|
37
|
+
* @param obj - Object to match
|
|
38
|
+
* @param definition - Type definition to match against
|
|
39
|
+
* @returns Confidence score (0-1)
|
|
40
|
+
*/
|
|
41
|
+
private calculateMatchConfidence;
|
|
42
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Response Analyzer
|
|
4
|
+
*
|
|
5
|
+
* Main class that orchestrates object type and format type detection.
|
|
6
|
+
* Analyzes normalized responses and provides enhanced metadata.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.ResponseAnalyzer = void 0;
|
|
10
|
+
const object_type_detector_js_1 = require("./object-type-detector.cjs");
|
|
11
|
+
const format_type_detector_js_1 = require("./format-type-detector.cjs");
|
|
12
|
+
/**
|
|
13
|
+
* Analyzes AI responses to detect object types and format types
|
|
14
|
+
*/
|
|
15
|
+
class ResponseAnalyzer {
|
|
16
|
+
objectDetector;
|
|
17
|
+
formatDetector;
|
|
18
|
+
enableFormatDetection;
|
|
19
|
+
constructor(config = {}) {
|
|
20
|
+
this.enableFormatDetection = config.enableFormatDetection !== false;
|
|
21
|
+
// Initialize object type detector if definitions provided
|
|
22
|
+
if (config.objectTypeDefinitions && config.objectTypeDefinitions.length > 0) {
|
|
23
|
+
this.objectDetector = new object_type_detector_js_1.ObjectTypeDetector({
|
|
24
|
+
objectTypeDefinitions: config.objectTypeDefinitions,
|
|
25
|
+
minConfidenceThreshold: config.minConfidenceThreshold || 0.5
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
// Initialize format type detector
|
|
29
|
+
this.formatDetector = new format_type_detector_js_1.FormatTypeDetector();
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Analyzes a response and returns enhanced metadata
|
|
33
|
+
*
|
|
34
|
+
* @param rawText - Raw text content from response
|
|
35
|
+
* @param parsedContent - Parsed content (object, array, or null)
|
|
36
|
+
* @returns Analysis result with detected types
|
|
37
|
+
*/
|
|
38
|
+
analyze(rawText, parsedContent) {
|
|
39
|
+
// Determine content type
|
|
40
|
+
const contentType = this.determineContentType(rawText, parsedContent);
|
|
41
|
+
const result = {
|
|
42
|
+
contentType
|
|
43
|
+
};
|
|
44
|
+
// Detect object types if content is an object
|
|
45
|
+
if (contentType === 'object' && this.objectDetector && parsedContent) {
|
|
46
|
+
const objectTypes = this.objectDetector.detect(parsedContent);
|
|
47
|
+
if (objectTypes.length > 0) {
|
|
48
|
+
result.bestObjectType = objectTypes[0];
|
|
49
|
+
result.allObjectTypes = objectTypes;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// Detect format type if content is text and format detection is enabled
|
|
53
|
+
if (contentType === 'text' && this.enableFormatDetection && rawText) {
|
|
54
|
+
const formatType = this.formatDetector.detect(rawText);
|
|
55
|
+
if (formatType) {
|
|
56
|
+
result.formatType = formatType;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return result;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Determines the content type of the response
|
|
63
|
+
*
|
|
64
|
+
* @param rawText - Raw text content
|
|
65
|
+
* @param parsedContent - Parsed content
|
|
66
|
+
* @returns Content type classification
|
|
67
|
+
*/
|
|
68
|
+
determineContentType(rawText, parsedContent) {
|
|
69
|
+
// Empty if no content at all
|
|
70
|
+
if (!rawText && (parsedContent == null || parsedContent === '')) {
|
|
71
|
+
return 'empty';
|
|
72
|
+
}
|
|
73
|
+
// Object if parsed content is a non-array object
|
|
74
|
+
if (parsedContent != null && typeof parsedContent === 'object' && !Array.isArray(parsedContent)) {
|
|
75
|
+
return 'object';
|
|
76
|
+
}
|
|
77
|
+
// Text if we have raw text (or parsed content is a string/array)
|
|
78
|
+
if (rawText && rawText.trim().length > 0) {
|
|
79
|
+
return 'text';
|
|
80
|
+
}
|
|
81
|
+
// Default to empty
|
|
82
|
+
return 'empty';
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Updates object type definitions
|
|
86
|
+
*
|
|
87
|
+
* @param definitions - New object type definitions
|
|
88
|
+
*/
|
|
89
|
+
updateObjectTypeDefinitions(definitions) {
|
|
90
|
+
if (definitions.length > 0) {
|
|
91
|
+
this.objectDetector = new object_type_detector_js_1.ObjectTypeDetector({
|
|
92
|
+
objectTypeDefinitions: definitions,
|
|
93
|
+
minConfidenceThreshold: 0.5
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
this.objectDetector = undefined;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.ResponseAnalyzer = ResponseAnalyzer;
|