@codebolt/agent 5.0.8 → 5.0.9
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/dist/unified/base/responseExecutor.js +10 -6
- package/package.json +5 -5
- package/dist/agent.d.ts +0 -86
- package/dist/agent.js +0 -324
- package/dist/builderpattern/agent.d.ts +0 -86
- package/dist/builderpattern/agent.js +0 -324
- package/dist/builderpattern/followupquestionbuilder.d.ts +0 -75
- package/dist/builderpattern/followupquestionbuilder.js +0 -197
- package/dist/builderpattern/index.d.ts +0 -14
- package/dist/builderpattern/index.js +0 -23
- package/dist/builderpattern/llmoutputhandler.d.ts +0 -102
- package/dist/builderpattern/llmoutputhandler.js +0 -452
- package/dist/builderpattern/promptbuilder.d.ts +0 -382
- package/dist/builderpattern/promptbuilder.js +0 -805
- package/dist/builderpattern/systemprompt.d.ts +0 -20
- package/dist/builderpattern/systemprompt.js +0 -48
- package/dist/builderpattern/taskInstruction.d.ts +0 -37
- package/dist/builderpattern/taskInstruction.js +0 -57
- package/dist/builderpattern/usermessage.d.ts +0 -70
- package/dist/builderpattern/usermessage.js +0 -123
- package/dist/composablepattern/agent.d.ts +0 -169
- package/dist/composablepattern/agent.js +0 -598
- package/dist/composablepattern/codebolt-storage.d.ts +0 -67
- package/dist/composablepattern/codebolt-storage.js +0 -320
- package/dist/composablepattern/document.d.ts +0 -149
- package/dist/composablepattern/document.js +0 -405
- package/dist/composablepattern/examples/codebolt-integration-example.d.ts +0 -12
- package/dist/composablepattern/examples/codebolt-integration-example.js +0 -218
- package/dist/composablepattern/examples/codebolt-storage-example.d.ts +0 -40
- package/dist/composablepattern/examples/codebolt-storage-example.js +0 -223
- package/dist/composablepattern/examples/custom-steps-example.d.ts +0 -20
- package/dist/composablepattern/examples/custom-steps-example.js +0 -455
- package/dist/composablepattern/examples/document-agent.d.ts +0 -17
- package/dist/composablepattern/examples/document-agent.js +0 -107
- package/dist/composablepattern/examples/simple-codebolt-integration.d.ts +0 -8
- package/dist/composablepattern/examples/simple-codebolt-integration.js +0 -164
- package/dist/composablepattern/examples/weather-agent.d.ts +0 -18
- package/dist/composablepattern/examples/weather-agent.js +0 -86
- package/dist/composablepattern/examples/workflow-example.d.ts +0 -12
- package/dist/composablepattern/examples/workflow-example.js +0 -463
- package/dist/composablepattern/index.d.ts +0 -63
- package/dist/composablepattern/index.js +0 -115
- package/dist/composablepattern/memory.d.ts +0 -89
- package/dist/composablepattern/memory.js +0 -141
- package/dist/composablepattern/tool.d.ts +0 -84
- package/dist/composablepattern/tool.js +0 -260
- package/dist/composablepattern/types.d.ts +0 -194
- package/dist/composablepattern/types.js +0 -6
- package/dist/composablepattern/user-context.d.ts +0 -214
- package/dist/composablepattern/user-context.js +0 -275
- package/dist/composablepattern/workflow.d.ts +0 -388
- package/dist/composablepattern/workflow.js +0 -562
- package/dist/followupquestionbuilder.d.ts +0 -75
- package/dist/followupquestionbuilder.js +0 -197
- package/dist/index.d.ts +0 -14
- package/dist/index.js +0 -23
- package/dist/llmoutputhandler.d.ts +0 -102
- package/dist/llmoutputhandler.js +0 -452
- package/dist/processor/agent/agentStep.d.ts +0 -49
- package/dist/processor/agent/agentStep.js +0 -241
- package/dist/processor/agent/toolExecutor.d.ts +0 -19
- package/dist/processor/agent/toolExecutor.js +0 -90
- package/dist/processor/index.d.ts +0 -7
- package/dist/processor/index.js +0 -36
- package/dist/processor/messageModifiers/baseMessageModifier.d.ts +0 -20
- package/dist/processor/messageModifiers/baseMessageModifier.js +0 -55
- package/dist/processor/processors/baseProcessor.d.ts +0 -14
- package/dist/processor/processors/baseProcessor.js +0 -29
- package/dist/processor/tools/baseTool.d.ts +0 -10
- package/dist/processor/tools/baseTool.js +0 -20
- package/dist/processor/tools/toolList.d.ts +0 -9
- package/dist/processor/tools/toolList.js +0 -22
- package/dist/processor/types/interfaces.d.ts +0 -94
- package/dist/processor/types/interfaces.js +0 -2
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.d.ts +0 -22
- package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.js +0 -144
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.d.ts +0 -15
- package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.js +0 -75
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.d.ts +0 -11
- package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.js +0 -46
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.d.ts +0 -12
- package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.js +0 -60
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.d.ts +0 -18
- package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.js +0 -222
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.d.ts +0 -22
- package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.js +0 -194
- package/dist/processor-pieces/processors/advancedLoopDetectionProcessor.d.ts +0 -41
- package/dist/processor-pieces/processors/advancedLoopDetectionProcessor.js +0 -197
- package/dist/processor-pieces/processors/chatCompressionProcessor.d.ts +0 -26
- package/dist/processor-pieces/processors/chatCompressionProcessor.js +0 -90
- package/dist/processor-pieces/processors/chatRecordingProcessor.d.ts +0 -81
- package/dist/processor-pieces/processors/chatRecordingProcessor.js +0 -329
- package/dist/processor-pieces/processors/contextManagementProcessor.d.ts +0 -40
- package/dist/processor-pieces/processors/contextManagementProcessor.js +0 -305
- package/dist/processor-pieces/processors/loopDetectionProcessor.d.ts +0 -30
- package/dist/processor-pieces/processors/loopDetectionProcessor.js +0 -137
- package/dist/processor-pieces/processors/responseValidationProcessor.d.ts +0 -30
- package/dist/processor-pieces/processors/responseValidationProcessor.js +0 -228
- package/dist/processor-pieces/processors/telemetryProcessor.d.ts +0 -99
- package/dist/processor-pieces/processors/telemetryProcessor.js +0 -311
- package/dist/processor-pieces/processors/tokenManagementProcessor.d.ts +0 -37
- package/dist/processor-pieces/processors/tokenManagementProcessor.js +0 -178
- package/dist/processor-pieces/processors/toolExecutionProcessor.d.ts +0 -43
- package/dist/processor-pieces/processors/toolExecutionProcessor.js +0 -207
- package/dist/processor-pieces/tools/fileTools.d.ts +0 -26
- package/dist/processor-pieces/tools/fileTools.js +0 -162
- package/dist/promptbuilder.d.ts +0 -382
- package/dist/promptbuilder.js +0 -805
- package/dist/systemprompt.d.ts +0 -20
- package/dist/systemprompt.js +0 -48
- package/dist/taskInstruction.d.ts +0 -37
- package/dist/taskInstruction.js +0 -57
- package/dist/usermessage.d.ts +0 -70
- package/dist/usermessage.js +0 -123
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
|
|
2
|
-
export interface LoopDetectionInfo {
|
|
3
|
-
promptId: string;
|
|
4
|
-
loopCount: number;
|
|
5
|
-
maxLoops: number;
|
|
6
|
-
recentMessages: any[];
|
|
7
|
-
isLoop: boolean;
|
|
8
|
-
}
|
|
9
|
-
export interface LoopDetectionProcessorOptions {
|
|
10
|
-
maxLoops?: number;
|
|
11
|
-
messageThreshold?: number;
|
|
12
|
-
enableLoopDetection?: boolean;
|
|
13
|
-
}
|
|
14
|
-
export declare class LoopDetectionProcessor extends BaseProcessor {
|
|
15
|
-
private readonly maxLoops;
|
|
16
|
-
private readonly messageThreshold;
|
|
17
|
-
private readonly enableLoopDetection;
|
|
18
|
-
private lastPromptId;
|
|
19
|
-
private loopCount;
|
|
20
|
-
constructor(options?: LoopDetectionProcessorOptions);
|
|
21
|
-
processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
|
|
22
|
-
private generatePromptId;
|
|
23
|
-
private resetLoopDetector;
|
|
24
|
-
private checkLoopDetection;
|
|
25
|
-
resetSession(): void;
|
|
26
|
-
getCurrentLoopCount(): number;
|
|
27
|
-
setMaxLoops(maxLoops: number): void;
|
|
28
|
-
enableLoopDetectionForSession(): void;
|
|
29
|
-
disableLoopDetectionForSession(): void;
|
|
30
|
-
}
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.LoopDetectionProcessor = void 0;
|
|
7
|
-
const processor_1 = require("../../processor");
|
|
8
|
-
const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
|
|
9
|
-
class LoopDetectionProcessor extends processor_1.BaseProcessor {
|
|
10
|
-
constructor(options = {}) {
|
|
11
|
-
super(options);
|
|
12
|
-
this.lastPromptId = null;
|
|
13
|
-
this.loopCount = 0;
|
|
14
|
-
this.maxLoops = options.maxLoops || 3;
|
|
15
|
-
this.messageThreshold = options.messageThreshold || 6;
|
|
16
|
-
this.enableLoopDetection = options.enableLoopDetection !== false;
|
|
17
|
-
}
|
|
18
|
-
async processInput(input) {
|
|
19
|
-
try {
|
|
20
|
-
const { message, context } = input;
|
|
21
|
-
if (!this.enableLoopDetection) {
|
|
22
|
-
return [this.createEvent('LoopDetectionDisabled', {
|
|
23
|
-
reason: 'Loop detection is disabled for this session'
|
|
24
|
-
})];
|
|
25
|
-
}
|
|
26
|
-
// Get prompt ID from context or generate one
|
|
27
|
-
const promptId = (context === null || context === void 0 ? void 0 : context.promptId) || this.generatePromptId();
|
|
28
|
-
// Reset loop detector if new prompt
|
|
29
|
-
if (this.lastPromptId !== promptId) {
|
|
30
|
-
await this.resetLoopDetector(promptId);
|
|
31
|
-
this.lastPromptId = promptId;
|
|
32
|
-
}
|
|
33
|
-
else {
|
|
34
|
-
this.loopCount++;
|
|
35
|
-
}
|
|
36
|
-
// Check for conversation loops using CodeBolt state
|
|
37
|
-
const loopDetected = await this.checkLoopDetection();
|
|
38
|
-
if (loopDetected) {
|
|
39
|
-
// Send notification about loop detection
|
|
40
|
-
await codeboltjs_1.default.chat.sendNotificationEvent('Loop detected in conversation', 'debug');
|
|
41
|
-
return [this.createEvent('LoopDetected', {
|
|
42
|
-
promptId,
|
|
43
|
-
loopCount: this.loopCount,
|
|
44
|
-
maxLoops: this.maxLoops
|
|
45
|
-
})];
|
|
46
|
-
}
|
|
47
|
-
// Check if max loops reached
|
|
48
|
-
if (this.loopCount > this.maxLoops) {
|
|
49
|
-
return [this.createEvent('MaxLoopsReached', {
|
|
50
|
-
promptId,
|
|
51
|
-
loopCount: this.loopCount,
|
|
52
|
-
maxLoops: this.maxLoops
|
|
53
|
-
})];
|
|
54
|
-
}
|
|
55
|
-
// Continue processing
|
|
56
|
-
return [this.createEvent('LoopDetectionPassed', {
|
|
57
|
-
promptId,
|
|
58
|
-
loopCount: this.loopCount,
|
|
59
|
-
maxLoops: this.maxLoops
|
|
60
|
-
})];
|
|
61
|
-
}
|
|
62
|
-
catch (error) {
|
|
63
|
-
console.error('Error in LoopDetectionProcessor:', error);
|
|
64
|
-
return [this.createEvent('LoopDetectionError', {
|
|
65
|
-
error: error instanceof Error ? error.message : String(error)
|
|
66
|
-
})];
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
generatePromptId() {
|
|
70
|
-
return `prompt-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
71
|
-
}
|
|
72
|
-
async resetLoopDetector(promptId) {
|
|
73
|
-
this.loopCount = 0;
|
|
74
|
-
this.setContext('currentPromptId', promptId);
|
|
75
|
-
this.setContext('loopDetectorReset', Date.now().toString());
|
|
76
|
-
// Update CodeBolt agent state
|
|
77
|
-
try {
|
|
78
|
-
await codeboltjs_1.default.cbstate.addToAgentState('lastPromptId', promptId);
|
|
79
|
-
await codeboltjs_1.default.cbstate.addToAgentState('loopDetectorReset', Date.now().toString());
|
|
80
|
-
}
|
|
81
|
-
catch (error) {
|
|
82
|
-
console.warn('Failed to update CodeBolt agent state:', error);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
async checkLoopDetection() {
|
|
86
|
-
var _a;
|
|
87
|
-
try {
|
|
88
|
-
// Get recent messages from CodeBolt state
|
|
89
|
-
const agentState = await codeboltjs_1.default.cbstate.getAgentState();
|
|
90
|
-
const recentMessages = ((_a = agentState.data) === null || _a === void 0 ? void 0 : _a.recentMessages) || [];
|
|
91
|
-
// Simple loop detection: check for repeated patterns
|
|
92
|
-
if (recentMessages.length >= this.messageThreshold) {
|
|
93
|
-
const lastThree = recentMessages.slice(-3);
|
|
94
|
-
const previousThree = recentMessages.slice(-6, -3);
|
|
95
|
-
if (lastThree.length === 3 && previousThree.length === 3) {
|
|
96
|
-
const isLoop = JSON.stringify(lastThree) === JSON.stringify(previousThree);
|
|
97
|
-
if (isLoop) {
|
|
98
|
-
console.log('[Loop Detection] Loop detected in conversation');
|
|
99
|
-
return true;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
return false;
|
|
104
|
-
}
|
|
105
|
-
catch (error) {
|
|
106
|
-
console.warn('Failed to check loop detection:', error);
|
|
107
|
-
return false;
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
// Public methods for external control
|
|
111
|
-
resetSession() {
|
|
112
|
-
this.loopCount = 0;
|
|
113
|
-
this.lastPromptId = null;
|
|
114
|
-
this.clearContext();
|
|
115
|
-
// Clear CodeBolt agent state
|
|
116
|
-
try {
|
|
117
|
-
codeboltjs_1.default.cbstate.addToAgentState('sessionTurnCount', '0');
|
|
118
|
-
codeboltjs_1.default.cbstate.addToAgentState('lastPromptId', '');
|
|
119
|
-
}
|
|
120
|
-
catch (error) {
|
|
121
|
-
console.warn('Failed to clear CodeBolt agent state:', error);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
getCurrentLoopCount() {
|
|
125
|
-
return this.loopCount;
|
|
126
|
-
}
|
|
127
|
-
setMaxLoops(maxLoops) {
|
|
128
|
-
this.maxLoops = Math.max(1, maxLoops);
|
|
129
|
-
}
|
|
130
|
-
enableLoopDetectionForSession() {
|
|
131
|
-
this.enableLoopDetection = true;
|
|
132
|
-
}
|
|
133
|
-
disableLoopDetectionForSession() {
|
|
134
|
-
this.enableLoopDetection = false;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
exports.LoopDetectionProcessor = LoopDetectionProcessor;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
|
|
2
|
-
export interface ResponseValidationProcessorOptions {
|
|
3
|
-
validateToolCalls?: boolean;
|
|
4
|
-
validateContent?: boolean;
|
|
5
|
-
validateStructure?: boolean;
|
|
6
|
-
allowEmptyContent?: boolean;
|
|
7
|
-
maxContentLength?: number;
|
|
8
|
-
requiredFields?: string[];
|
|
9
|
-
}
|
|
10
|
-
export interface ValidationResult {
|
|
11
|
-
isValid: boolean;
|
|
12
|
-
errors: string[];
|
|
13
|
-
warnings: string[];
|
|
14
|
-
fixes: string[];
|
|
15
|
-
}
|
|
16
|
-
export declare class ResponseValidationProcessor extends BaseProcessor {
|
|
17
|
-
private readonly enableToolCallValidation;
|
|
18
|
-
private readonly validateContent;
|
|
19
|
-
private readonly validateStructure;
|
|
20
|
-
private readonly allowEmptyContent;
|
|
21
|
-
private readonly maxContentLength;
|
|
22
|
-
private readonly requiredFields;
|
|
23
|
-
constructor(options?: ResponseValidationProcessorOptions);
|
|
24
|
-
processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
|
|
25
|
-
private validateMessage;
|
|
26
|
-
private validateMessageStructure;
|
|
27
|
-
private validateMessageContent;
|
|
28
|
-
private validateToolCallsInMessage;
|
|
29
|
-
getValidationSummary(validationResult: ValidationResult): string;
|
|
30
|
-
}
|
|
@@ -1,228 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ResponseValidationProcessor = void 0;
|
|
4
|
-
const processor_1 = require("../../processor");
|
|
5
|
-
class ResponseValidationProcessor extends processor_1.BaseProcessor {
|
|
6
|
-
constructor(options = {}) {
|
|
7
|
-
super();
|
|
8
|
-
this.enableToolCallValidation = options.validateToolCalls !== false;
|
|
9
|
-
this.validateContent = options.validateContent !== false;
|
|
10
|
-
this.validateStructure = options.validateStructure !== false;
|
|
11
|
-
this.allowEmptyContent = options.allowEmptyContent || false;
|
|
12
|
-
this.maxContentLength = options.maxContentLength || 100000;
|
|
13
|
-
this.requiredFields = options.requiredFields || ['role'];
|
|
14
|
-
}
|
|
15
|
-
async processInput(input) {
|
|
16
|
-
var _a;
|
|
17
|
-
const { message } = input;
|
|
18
|
-
const validationResult = this.validateMessage(message);
|
|
19
|
-
const results = [
|
|
20
|
-
this.createEvent('ValidationCompleted', {
|
|
21
|
-
result: validationResult,
|
|
22
|
-
messageId: ((_a = message.metadata) === null || _a === void 0 ? void 0 : _a.id) || 'unknown'
|
|
23
|
-
})
|
|
24
|
-
];
|
|
25
|
-
if (!validationResult.isValid) {
|
|
26
|
-
results.push(this.createEvent('ValidationFailed', {
|
|
27
|
-
errors: validationResult.errors,
|
|
28
|
-
warnings: validationResult.warnings,
|
|
29
|
-
fixes: validationResult.fixes
|
|
30
|
-
}));
|
|
31
|
-
}
|
|
32
|
-
if (validationResult.warnings.length > 0) {
|
|
33
|
-
results.push(this.createEvent('ValidationWarnings', {
|
|
34
|
-
warnings: validationResult.warnings
|
|
35
|
-
}));
|
|
36
|
-
}
|
|
37
|
-
return results;
|
|
38
|
-
}
|
|
39
|
-
validateMessage(message) {
|
|
40
|
-
const errors = [];
|
|
41
|
-
const warnings = [];
|
|
42
|
-
const fixes = [];
|
|
43
|
-
// Structure validation
|
|
44
|
-
if (this.validateStructure) {
|
|
45
|
-
const structureValidation = this.validateMessageStructure(message);
|
|
46
|
-
errors.push(...structureValidation.errors);
|
|
47
|
-
warnings.push(...structureValidation.warnings);
|
|
48
|
-
fixes.push(...structureValidation.fixes);
|
|
49
|
-
}
|
|
50
|
-
// Content validation
|
|
51
|
-
if (this.validateContent && message.messages) {
|
|
52
|
-
for (let i = 0; i < message.messages.length; i++) {
|
|
53
|
-
const msg = message.messages[i];
|
|
54
|
-
const contentValidation = this.validateMessageContent(msg, i);
|
|
55
|
-
errors.push(...contentValidation.errors);
|
|
56
|
-
warnings.push(...contentValidation.warnings);
|
|
57
|
-
fixes.push(...contentValidation.fixes);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
// Tool calls validation
|
|
61
|
-
if (this.enableToolCallValidation && message.messages) {
|
|
62
|
-
for (let i = 0; i < message.messages.length; i++) {
|
|
63
|
-
const msg = message.messages[i];
|
|
64
|
-
if (msg.tool_calls) {
|
|
65
|
-
const toolValidation = this.validateToolCallsInMessage(msg.tool_calls, i);
|
|
66
|
-
errors.push(...toolValidation.errors);
|
|
67
|
-
warnings.push(...toolValidation.warnings);
|
|
68
|
-
fixes.push(...toolValidation.fixes);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return {
|
|
73
|
-
isValid: errors.length === 0,
|
|
74
|
-
errors,
|
|
75
|
-
warnings,
|
|
76
|
-
fixes
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
validateMessageStructure(message) {
|
|
80
|
-
const errors = [];
|
|
81
|
-
const warnings = [];
|
|
82
|
-
const fixes = [];
|
|
83
|
-
// Check if message exists
|
|
84
|
-
if (!message) {
|
|
85
|
-
errors.push('Message is null or undefined');
|
|
86
|
-
fixes.push('Provide a valid message object');
|
|
87
|
-
return { isValid: false, errors, warnings, fixes };
|
|
88
|
-
}
|
|
89
|
-
// Check if messages array exists
|
|
90
|
-
if (!message.messages) {
|
|
91
|
-
errors.push('Message object missing required "messages" array');
|
|
92
|
-
fixes.push('Add a "messages" array to the message object');
|
|
93
|
-
}
|
|
94
|
-
else if (!Array.isArray(message.messages)) {
|
|
95
|
-
errors.push('Messages field must be an array');
|
|
96
|
-
fixes.push('Convert messages field to an array');
|
|
97
|
-
}
|
|
98
|
-
else if (message.messages.length === 0) {
|
|
99
|
-
warnings.push('Messages array is empty');
|
|
100
|
-
}
|
|
101
|
-
// Check metadata structure
|
|
102
|
-
if (message.metadata && typeof message.metadata !== 'object') {
|
|
103
|
-
warnings.push('Metadata should be an object');
|
|
104
|
-
fixes.push('Convert metadata to an object or remove it');
|
|
105
|
-
}
|
|
106
|
-
return { isValid: errors.length === 0, errors, warnings, fixes };
|
|
107
|
-
}
|
|
108
|
-
validateMessageContent(msg, index) {
|
|
109
|
-
const errors = [];
|
|
110
|
-
const warnings = [];
|
|
111
|
-
const fixes = [];
|
|
112
|
-
// Check required fields
|
|
113
|
-
for (const field of this.requiredFields) {
|
|
114
|
-
if (!(field in msg)) {
|
|
115
|
-
errors.push(`Message at index ${index} missing required field: ${field}`);
|
|
116
|
-
fixes.push(`Add ${field} field to message at index ${index}`);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
// Validate role
|
|
120
|
-
if (msg.role) {
|
|
121
|
-
const validRoles = ['user', 'assistant', 'system', 'tool'];
|
|
122
|
-
if (!validRoles.includes(msg.role)) {
|
|
123
|
-
errors.push(`Invalid role "${msg.role}" at index ${index}. Must be one of: ${validRoles.join(', ')}`);
|
|
124
|
-
fixes.push(`Change role to one of: ${validRoles.join(', ')}`);
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
// Validate content
|
|
128
|
-
if (msg.content !== undefined) {
|
|
129
|
-
if (typeof msg.content === 'string') {
|
|
130
|
-
// String content validation
|
|
131
|
-
if (!this.allowEmptyContent && msg.content.trim() === '') {
|
|
132
|
-
warnings.push(`Empty content in message at index ${index}`);
|
|
133
|
-
fixes.push('Provide meaningful content or remove the message');
|
|
134
|
-
}
|
|
135
|
-
if (msg.content.length > this.maxContentLength) {
|
|
136
|
-
warnings.push(`Content too long at index ${index}: ${msg.content.length} chars (max: ${this.maxContentLength})`);
|
|
137
|
-
fixes.push('Truncate or compress the content');
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
else if (Array.isArray(msg.content)) {
|
|
141
|
-
// Array content validation (for multimodal messages)
|
|
142
|
-
for (let partIndex = 0; partIndex < msg.content.length; partIndex++) {
|
|
143
|
-
const part = msg.content[partIndex];
|
|
144
|
-
if (!part || (typeof part !== 'object' && typeof part !== 'string')) {
|
|
145
|
-
errors.push(`Invalid content part at message ${index}, part ${partIndex}`);
|
|
146
|
-
fixes.push('Ensure all content parts are valid objects or strings');
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
else if (typeof msg.content !== 'object') {
|
|
151
|
-
warnings.push(`Unexpected content type at index ${index}: ${typeof msg.content}`);
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
// Validate name field
|
|
155
|
-
if (msg.name && typeof msg.name !== 'string') {
|
|
156
|
-
warnings.push(`Name field should be a string at index ${index}`);
|
|
157
|
-
fixes.push('Convert name field to string');
|
|
158
|
-
}
|
|
159
|
-
return { isValid: errors.length === 0, errors, warnings, fixes };
|
|
160
|
-
}
|
|
161
|
-
validateToolCallsInMessage(toolCalls, messageIndex) {
|
|
162
|
-
const errors = [];
|
|
163
|
-
const warnings = [];
|
|
164
|
-
const fixes = [];
|
|
165
|
-
if (!Array.isArray(toolCalls)) {
|
|
166
|
-
errors.push(`Tool calls must be an array at message index ${messageIndex}`);
|
|
167
|
-
fixes.push('Convert tool_calls to an array');
|
|
168
|
-
return { isValid: false, errors, warnings, fixes };
|
|
169
|
-
}
|
|
170
|
-
for (let i = 0; i < toolCalls.length; i++) {
|
|
171
|
-
const toolCall = toolCalls[i];
|
|
172
|
-
// Check required tool call fields
|
|
173
|
-
if (!toolCall.id) {
|
|
174
|
-
errors.push(`Tool call at message ${messageIndex}, call ${i} missing required "id" field`);
|
|
175
|
-
fixes.push('Add unique id to tool call');
|
|
176
|
-
}
|
|
177
|
-
if (!toolCall.function) {
|
|
178
|
-
errors.push(`Tool call at message ${messageIndex}, call ${i} missing required "function" field`);
|
|
179
|
-
fixes.push('Add function object to tool call');
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
// Validate function object
|
|
183
|
-
if (!toolCall.function.name) {
|
|
184
|
-
errors.push(`Tool call function at message ${messageIndex}, call ${i} missing "name" field`);
|
|
185
|
-
fixes.push('Add function name to tool call');
|
|
186
|
-
}
|
|
187
|
-
if (toolCall.function.arguments === undefined) {
|
|
188
|
-
warnings.push(`Tool call function at message ${messageIndex}, call ${i} missing "arguments" field`);
|
|
189
|
-
fixes.push('Add arguments field (can be empty object)');
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
// Validate arguments
|
|
193
|
-
if (typeof toolCall.function.arguments === 'string') {
|
|
194
|
-
try {
|
|
195
|
-
JSON.parse(toolCall.function.arguments);
|
|
196
|
-
}
|
|
197
|
-
catch (e) {
|
|
198
|
-
errors.push(`Invalid JSON in tool call arguments at message ${messageIndex}, call ${i}`);
|
|
199
|
-
fixes.push('Ensure arguments are valid JSON string or object');
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
else if (typeof toolCall.function.arguments !== 'object') {
|
|
203
|
-
warnings.push(`Tool call arguments should be object or JSON string at message ${messageIndex}, call ${i}`);
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
return { isValid: errors.length === 0, errors, warnings, fixes };
|
|
209
|
-
}
|
|
210
|
-
// Utility method to get validation summary
|
|
211
|
-
getValidationSummary(validationResult) {
|
|
212
|
-
const summary = [];
|
|
213
|
-
if (validationResult.isValid) {
|
|
214
|
-
summary.push('✅ Validation passed');
|
|
215
|
-
}
|
|
216
|
-
else {
|
|
217
|
-
summary.push('❌ Validation failed');
|
|
218
|
-
}
|
|
219
|
-
if (validationResult.errors.length > 0) {
|
|
220
|
-
summary.push(`${validationResult.errors.length} error(s)`);
|
|
221
|
-
}
|
|
222
|
-
if (validationResult.warnings.length > 0) {
|
|
223
|
-
summary.push(`${validationResult.warnings.length} warning(s)`);
|
|
224
|
-
}
|
|
225
|
-
return summary.join(' - ');
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
exports.ResponseValidationProcessor = ResponseValidationProcessor;
|
|
@@ -1,99 +0,0 @@
|
|
|
1
|
-
import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
|
|
2
|
-
export interface TelemetryProcessorOptions {
|
|
3
|
-
enablePerformanceTracking?: boolean;
|
|
4
|
-
enableErrorTracking?: boolean;
|
|
5
|
-
enableUsageTracking?: boolean;
|
|
6
|
-
enableTokenTracking?: boolean;
|
|
7
|
-
sampleRate?: number;
|
|
8
|
-
batchSize?: number;
|
|
9
|
-
flushInterval?: number;
|
|
10
|
-
enableLocalStorage?: boolean;
|
|
11
|
-
exportPath?: string;
|
|
12
|
-
}
|
|
13
|
-
export interface PerformanceMetric {
|
|
14
|
-
operation: string;
|
|
15
|
-
startTime: number;
|
|
16
|
-
endTime: number;
|
|
17
|
-
duration: number;
|
|
18
|
-
success: boolean;
|
|
19
|
-
metadata?: Record<string, any>;
|
|
20
|
-
}
|
|
21
|
-
export interface ErrorMetric {
|
|
22
|
-
type: string;
|
|
23
|
-
message: string;
|
|
24
|
-
stack?: string;
|
|
25
|
-
timestamp: number;
|
|
26
|
-
context?: Record<string, any>;
|
|
27
|
-
}
|
|
28
|
-
export interface UsageMetric {
|
|
29
|
-
feature: string;
|
|
30
|
-
count: number;
|
|
31
|
-
timestamp: number;
|
|
32
|
-
userId?: string;
|
|
33
|
-
sessionId?: string;
|
|
34
|
-
}
|
|
35
|
-
export interface TokenMetric {
|
|
36
|
-
model: string;
|
|
37
|
-
inputTokens: number;
|
|
38
|
-
outputTokens: number;
|
|
39
|
-
cachedTokens: number;
|
|
40
|
-
totalTokens: number;
|
|
41
|
-
timestamp: number;
|
|
42
|
-
cost?: number;
|
|
43
|
-
}
|
|
44
|
-
export interface TelemetryData {
|
|
45
|
-
sessionId: string;
|
|
46
|
-
timestamp: number;
|
|
47
|
-
performance: PerformanceMetric[];
|
|
48
|
-
errors: ErrorMetric[];
|
|
49
|
-
usage: UsageMetric[];
|
|
50
|
-
tokens: TokenMetric[];
|
|
51
|
-
metadata: Record<string, any>;
|
|
52
|
-
}
|
|
53
|
-
export declare class TelemetryProcessor extends BaseProcessor {
|
|
54
|
-
private readonly enablePerformanceTracking;
|
|
55
|
-
private readonly enableErrorTracking;
|
|
56
|
-
private readonly enableUsageTracking;
|
|
57
|
-
private readonly enableTokenTracking;
|
|
58
|
-
private readonly sampleRate;
|
|
59
|
-
private readonly batchSize;
|
|
60
|
-
private readonly flushInterval;
|
|
61
|
-
private readonly enableLocalStorage;
|
|
62
|
-
private readonly exportPath;
|
|
63
|
-
private performanceMetrics;
|
|
64
|
-
private errorMetrics;
|
|
65
|
-
private usageMetrics;
|
|
66
|
-
private tokenMetrics;
|
|
67
|
-
private sessionId;
|
|
68
|
-
private startTime;
|
|
69
|
-
private operationTimers;
|
|
70
|
-
private flushTimer?;
|
|
71
|
-
constructor(options?: TelemetryProcessorOptions);
|
|
72
|
-
processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
|
|
73
|
-
startOperation(operation: string): void;
|
|
74
|
-
endOperation(operation: string, success: boolean, metadata?: Record<string, any>): void;
|
|
75
|
-
trackError(type: string, message: string, stack?: string, context?: Record<string, any>): void;
|
|
76
|
-
trackUsage(feature: string, userId?: string, sessionId?: string): void;
|
|
77
|
-
trackTokens(model: string, inputTokens: number, outputTokens: number, cachedTokens?: number, cost?: number): void;
|
|
78
|
-
flushMetrics(): Promise<void>;
|
|
79
|
-
private saveToFile;
|
|
80
|
-
private clearMetrics;
|
|
81
|
-
private getTotalMetricsCount;
|
|
82
|
-
private generateSessionId;
|
|
83
|
-
getMetricsSummary(): {
|
|
84
|
-
sessionId: string;
|
|
85
|
-
sessionDuration: number;
|
|
86
|
-
performance: number;
|
|
87
|
-
errors: number;
|
|
88
|
-
usage: number;
|
|
89
|
-
tokens: number;
|
|
90
|
-
total: number;
|
|
91
|
-
};
|
|
92
|
-
getPerformanceStats(): {
|
|
93
|
-
averageDuration: number;
|
|
94
|
-
successRate: number;
|
|
95
|
-
slowestOperation: PerformanceMetric | null;
|
|
96
|
-
fastestOperation: PerformanceMetric | null;
|
|
97
|
-
};
|
|
98
|
-
destroy(): Promise<void>;
|
|
99
|
-
}
|