@codebolt/agent 5.0.7 → 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.
Files changed (160) hide show
  1. package/dist/processor-pieces/base/basePreToolCallProcessor.js +5 -2
  2. package/dist/processor-pieces/messageModifiers/argumentProcessorModifier.d.ts +1 -1
  3. package/dist/processor-pieces/messageModifiers/argumentProcessorModifier.js +2 -2
  4. package/dist/processor-pieces/messageModifiers/atFileProcessorModifier.js +24 -34
  5. package/dist/processor-pieces/messageModifiers/chatRecordingModifier.d.ts +1 -1
  6. package/dist/processor-pieces/messageModifiers/chatRecordingModifier.js +2 -2
  7. package/dist/processor-pieces/messageModifiers/coreSystemPromptModifier.d.ts +1 -1
  8. package/dist/processor-pieces/messageModifiers/coreSystemPromptModifier.js +2 -2
  9. package/dist/processor-pieces/messageModifiers/directoryContextModifier.d.ts +1 -1
  10. package/dist/processor-pieces/messageModifiers/directoryContextModifier.js +8 -3
  11. package/dist/processor-pieces/messageModifiers/environmentContextModifier.d.ts +1 -1
  12. package/dist/processor-pieces/messageModifiers/environmentContextModifier.js +1 -1
  13. package/dist/processor-pieces/messageModifiers/ideContextModifier.js +114 -17
  14. package/dist/processor-pieces/messageModifiers/memoryImportModifier.d.ts +1 -1
  15. package/dist/processor-pieces/messageModifiers/memoryImportModifier.js +13 -5
  16. package/dist/processor-pieces/messageModifiers/toolInjectionModifier.d.ts +5 -0
  17. package/dist/processor-pieces/messageModifiers/toolInjectionModifier.js +4 -0
  18. package/dist/processor-pieces/postInferenceProcessors/loopDetectionModifier.js +24 -8
  19. package/dist/processor-pieces/postToolCallProcessors/conversationCompactorModifier.d.ts +202 -5
  20. package/dist/processor-pieces/postToolCallProcessors/conversationCompactorModifier.js +826 -19
  21. package/dist/processor-pieces/postToolCallProcessors/index.d.ts +1 -1
  22. package/dist/processor-pieces/postToolCallProcessors/index.js +2 -1
  23. package/dist/processor-pieces/postToolCallProcessors/shellProcessorModifier.js +7 -4
  24. package/dist/processor-pieces/preInferenceProcessors/chatCompressionModifier.d.ts +1 -1
  25. package/dist/processor-pieces/preInferenceProcessors/chatCompressionModifier.js +12 -16
  26. package/dist/processor-pieces/pretoolCallProcessors/toolParameterModifier.d.ts +2 -3
  27. package/dist/processor-pieces/pretoolCallProcessors/toolParameterModifier.js +1 -2
  28. package/dist/processor-pieces/utils/messageModifierHelper.js +5 -2
  29. package/dist/types/InternalTypes.d.ts +8 -6
  30. package/dist/unified/agent/agent.js +1 -2
  31. package/dist/unified/agent/codeboltAgent.d.ts +23 -2
  32. package/dist/unified/agent/codeboltAgent.js +55 -12
  33. package/dist/unified/agent/tools.d.ts +7 -11
  34. package/dist/unified/agent/tools.js +12 -3
  35. package/dist/unified/agent/workflow.js +38 -20
  36. package/dist/unified/agent/workflowSteps.d.ts +1 -1
  37. package/dist/unified/agent/workflowSteps.js +58 -23
  38. package/dist/unified/base/agentStep.js +20 -1
  39. package/dist/unified/base/initialPromptGenerator.js +3 -1
  40. package/dist/unified/base/responseExecutor.d.ts +3 -0
  41. package/dist/unified/base/responseExecutor.js +47 -21
  42. package/dist/unified/index.d.ts +1 -0
  43. package/dist/unified/index.js +4 -1
  44. package/dist/unified/services/LoopDetectionService.d.ts +44 -0
  45. package/dist/unified/services/LoopDetectionService.js +79 -0
  46. package/dist/unified/utils/utils.d.ts +20 -1
  47. package/dist/unified/utils/utils.js +8 -4
  48. package/package.json +5 -5
  49. package/dist/agent.d.ts +0 -86
  50. package/dist/agent.js +0 -324
  51. package/dist/builderpattern/agent.d.ts +0 -86
  52. package/dist/builderpattern/agent.js +0 -324
  53. package/dist/builderpattern/followupquestionbuilder.d.ts +0 -75
  54. package/dist/builderpattern/followupquestionbuilder.js +0 -197
  55. package/dist/builderpattern/index.d.ts +0 -14
  56. package/dist/builderpattern/index.js +0 -23
  57. package/dist/builderpattern/llmoutputhandler.d.ts +0 -102
  58. package/dist/builderpattern/llmoutputhandler.js +0 -452
  59. package/dist/builderpattern/promptbuilder.d.ts +0 -382
  60. package/dist/builderpattern/promptbuilder.js +0 -805
  61. package/dist/builderpattern/systemprompt.d.ts +0 -20
  62. package/dist/builderpattern/systemprompt.js +0 -48
  63. package/dist/builderpattern/taskInstruction.d.ts +0 -37
  64. package/dist/builderpattern/taskInstruction.js +0 -57
  65. package/dist/builderpattern/usermessage.d.ts +0 -70
  66. package/dist/builderpattern/usermessage.js +0 -123
  67. package/dist/composablepattern/agent.d.ts +0 -169
  68. package/dist/composablepattern/agent.js +0 -598
  69. package/dist/composablepattern/codebolt-storage.d.ts +0 -67
  70. package/dist/composablepattern/codebolt-storage.js +0 -320
  71. package/dist/composablepattern/document.d.ts +0 -149
  72. package/dist/composablepattern/document.js +0 -405
  73. package/dist/composablepattern/examples/codebolt-integration-example.d.ts +0 -12
  74. package/dist/composablepattern/examples/codebolt-integration-example.js +0 -218
  75. package/dist/composablepattern/examples/codebolt-storage-example.d.ts +0 -40
  76. package/dist/composablepattern/examples/codebolt-storage-example.js +0 -223
  77. package/dist/composablepattern/examples/custom-steps-example.d.ts +0 -20
  78. package/dist/composablepattern/examples/custom-steps-example.js +0 -455
  79. package/dist/composablepattern/examples/document-agent.d.ts +0 -17
  80. package/dist/composablepattern/examples/document-agent.js +0 -107
  81. package/dist/composablepattern/examples/simple-codebolt-integration.d.ts +0 -8
  82. package/dist/composablepattern/examples/simple-codebolt-integration.js +0 -164
  83. package/dist/composablepattern/examples/weather-agent.d.ts +0 -18
  84. package/dist/composablepattern/examples/weather-agent.js +0 -86
  85. package/dist/composablepattern/examples/workflow-example.d.ts +0 -12
  86. package/dist/composablepattern/examples/workflow-example.js +0 -463
  87. package/dist/composablepattern/index.d.ts +0 -63
  88. package/dist/composablepattern/index.js +0 -115
  89. package/dist/composablepattern/memory.d.ts +0 -89
  90. package/dist/composablepattern/memory.js +0 -141
  91. package/dist/composablepattern/tool.d.ts +0 -84
  92. package/dist/composablepattern/tool.js +0 -260
  93. package/dist/composablepattern/types.d.ts +0 -194
  94. package/dist/composablepattern/types.js +0 -6
  95. package/dist/composablepattern/user-context.d.ts +0 -214
  96. package/dist/composablepattern/user-context.js +0 -275
  97. package/dist/composablepattern/workflow.d.ts +0 -388
  98. package/dist/composablepattern/workflow.js +0 -562
  99. package/dist/followupquestionbuilder.d.ts +0 -75
  100. package/dist/followupquestionbuilder.js +0 -197
  101. package/dist/index.d.ts +0 -14
  102. package/dist/index.js +0 -23
  103. package/dist/llmoutputhandler.d.ts +0 -102
  104. package/dist/llmoutputhandler.js +0 -452
  105. package/dist/processor/agent/agentStep.d.ts +0 -49
  106. package/dist/processor/agent/agentStep.js +0 -241
  107. package/dist/processor/agent/toolExecutor.d.ts +0 -19
  108. package/dist/processor/agent/toolExecutor.js +0 -90
  109. package/dist/processor/index.d.ts +0 -7
  110. package/dist/processor/index.js +0 -36
  111. package/dist/processor/messageModifiers/baseMessageModifier.d.ts +0 -20
  112. package/dist/processor/messageModifiers/baseMessageModifier.js +0 -55
  113. package/dist/processor/processors/baseProcessor.d.ts +0 -14
  114. package/dist/processor/processors/baseProcessor.js +0 -29
  115. package/dist/processor/tools/baseTool.d.ts +0 -10
  116. package/dist/processor/tools/baseTool.js +0 -20
  117. package/dist/processor/tools/toolList.d.ts +0 -9
  118. package/dist/processor/tools/toolList.js +0 -22
  119. package/dist/processor/types/interfaces.d.ts +0 -94
  120. package/dist/processor/types/interfaces.js +0 -2
  121. package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.d.ts +0 -22
  122. package/dist/processor-pieces/messageModifiers/addToolsListMessageModifier.js +0 -144
  123. package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.d.ts +0 -15
  124. package/dist/processor-pieces/messageModifiers/baseContextMessageModifier.js +0 -75
  125. package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.d.ts +0 -11
  126. package/dist/processor-pieces/messageModifiers/baseSystemInstructionMessageModifier.js +0 -46
  127. package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.d.ts +0 -12
  128. package/dist/processor-pieces/messageModifiers/handleUrlMessageModifier.js +0 -60
  129. package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.d.ts +0 -18
  130. package/dist/processor-pieces/messageModifiers/imageAttachmentMessageModifier.js +0 -222
  131. package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.d.ts +0 -22
  132. package/dist/processor-pieces/messageModifiers/workingDirectoryMessageModifier.js +0 -194
  133. package/dist/processor-pieces/processors/advancedLoopDetectionProcessor.d.ts +0 -41
  134. package/dist/processor-pieces/processors/advancedLoopDetectionProcessor.js +0 -197
  135. package/dist/processor-pieces/processors/chatCompressionProcessor.d.ts +0 -26
  136. package/dist/processor-pieces/processors/chatCompressionProcessor.js +0 -90
  137. package/dist/processor-pieces/processors/chatRecordingProcessor.d.ts +0 -81
  138. package/dist/processor-pieces/processors/chatRecordingProcessor.js +0 -329
  139. package/dist/processor-pieces/processors/contextManagementProcessor.d.ts +0 -40
  140. package/dist/processor-pieces/processors/contextManagementProcessor.js +0 -305
  141. package/dist/processor-pieces/processors/loopDetectionProcessor.d.ts +0 -30
  142. package/dist/processor-pieces/processors/loopDetectionProcessor.js +0 -137
  143. package/dist/processor-pieces/processors/responseValidationProcessor.d.ts +0 -30
  144. package/dist/processor-pieces/processors/responseValidationProcessor.js +0 -228
  145. package/dist/processor-pieces/processors/telemetryProcessor.d.ts +0 -99
  146. package/dist/processor-pieces/processors/telemetryProcessor.js +0 -311
  147. package/dist/processor-pieces/processors/tokenManagementProcessor.d.ts +0 -37
  148. package/dist/processor-pieces/processors/tokenManagementProcessor.js +0 -178
  149. package/dist/processor-pieces/processors/toolExecutionProcessor.d.ts +0 -43
  150. package/dist/processor-pieces/processors/toolExecutionProcessor.js +0 -207
  151. package/dist/processor-pieces/tools/fileTools.d.ts +0 -26
  152. package/dist/processor-pieces/tools/fileTools.js +0 -162
  153. package/dist/promptbuilder.d.ts +0 -382
  154. package/dist/promptbuilder.js +0 -805
  155. package/dist/systemprompt.d.ts +0 -20
  156. package/dist/systemprompt.js +0 -48
  157. package/dist/taskInstruction.d.ts +0 -37
  158. package/dist/taskInstruction.js +0 -57
  159. package/dist/usermessage.d.ts +0 -70
  160. package/dist/usermessage.js +0 -123
@@ -1,194 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.WorkingDirectoryMessageModifier = void 0;
40
- const processor_1 = require("../../processor");
41
- const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
42
- const path = __importStar(require("path"));
43
- class WorkingDirectoryMessageModifier extends processor_1.BaseMessageModifier {
44
- constructor(options = {}) {
45
- super({ context: options });
46
- this.showFolderStructureSummary = options.showFolderStructureSummary || false;
47
- this.listFiles = options.listFiles || false;
48
- this.listFilesDepth = options.listFilesDepth || 2;
49
- this.listFilesLimit = options.listFilesLimit || 200;
50
- this.listFilesIgnoreFromGitignore = options.listFilesIgnoreFromGitignore || true;
51
- this.listFilesIgnore = options.listFilesIgnore || ['node_modules', '.git', 'dist', 'build'];
52
- }
53
- async modify(input) {
54
- try {
55
- const { originalRequest, createdMessage, context } = input;
56
- const contextParts = [];
57
- // Get project information
58
- try {
59
- const projectPathResponse = await codeboltjs_1.default.project.getProjectPath();
60
- const projectPath = typeof projectPathResponse === 'string'
61
- ? projectPathResponse
62
- : (projectPathResponse === null || projectPathResponse === void 0 ? void 0 : projectPathResponse.path) || String(projectPathResponse) || '';
63
- contextParts.push(`Project Directory: ${projectPath}`);
64
- if (this.showFolderStructureSummary) {
65
- const summary = await this.generateFolderStructureSummary(projectPath);
66
- contextParts.push(`Folder Structure Summary:\n${summary}`);
67
- }
68
- if (this.listFiles) {
69
- const filesList = await this.generateFilesList(projectPath);
70
- contextParts.push(`Files List:\n${filesList}`);
71
- }
72
- }
73
- catch (error) {
74
- console.error('Error getting project information:', error);
75
- contextParts.push('Working Directory: Unable to access project information');
76
- }
77
- // Create context message if we have any context parts
78
- if (contextParts.length > 0) {
79
- const contextMessage = {
80
- role: 'system',
81
- content: contextParts.join('\n\n'),
82
- name: 'working-directory-context'
83
- };
84
- return {
85
- messages: [contextMessage, ...createdMessage.messages],
86
- metadata: {
87
- ...createdMessage.metadata,
88
- workingDirectoryContextAdded: true,
89
- contextParts: contextParts.length
90
- }
91
- };
92
- }
93
- return createdMessage;
94
- }
95
- catch (error) {
96
- console.error('Error in WorkingDirectoryMessageModifier:', error);
97
- throw error;
98
- }
99
- }
100
- async generateFolderStructureSummary(projectPath) {
101
- try {
102
- // Use CodeBolt's directory listing with recursive option
103
- const response = await codeboltjs_1.default.fs.listDirectory({
104
- path: projectPath,
105
- show_hidden: false,
106
- detailed: false,
107
- limit: 200,
108
- notifyUser: false
109
- });
110
- const directories = new Set();
111
- // Extract directories from the response
112
- if (response && response.success) {
113
- let entries;
114
- if (typeof response.result === 'string') {
115
- // If result is a string, we'll need to parse it or work with what we have
116
- return response.result.split('\n').slice(0, 20).join('\n');
117
- }
118
- else if (response.result && response.result.entries) {
119
- entries = response.result.entries;
120
- }
121
- else if (response.entries) {
122
- entries = response.entries;
123
- }
124
- if (entries) {
125
- entries.forEach((entry) => {
126
- if (entry.type === 'directory' && !this.shouldIgnore(entry.path || entry.name)) {
127
- directories.add(entry.path || entry.name);
128
- }
129
- // Also get parent directories of files
130
- const dir = path.dirname(entry.path || entry.name);
131
- if (dir !== '.' && !this.shouldIgnore(dir)) {
132
- directories.add(dir);
133
- }
134
- });
135
- }
136
- }
137
- const sortedDirs = Array.from(directories).sort();
138
- return sortedDirs.length > 0
139
- ? sortedDirs.slice(0, 20).join('\n')
140
- : 'No accessible directories found';
141
- }
142
- catch (error) {
143
- return 'Unable to generate folder structure summary';
144
- }
145
- }
146
- async generateFilesList(projectPath) {
147
- try {
148
- // Use CodeBolt's directory listing to get files
149
- const response = await codeboltjs_1.default.fs.listDirectory({
150
- path: projectPath,
151
- show_hidden: false,
152
- detailed: false,
153
- limit: this.listFilesLimit,
154
- notifyUser: false
155
- });
156
- const files = [];
157
- // Extract files from the response
158
- if (response && response.success) {
159
- let entries;
160
- if (typeof response.result === 'string') {
161
- // If result is a string, split and filter for files
162
- return response.result.split('\n')
163
- .filter((line) => !this.shouldIgnore(line))
164
- .slice(0, this.listFilesLimit)
165
- .join('\n');
166
- }
167
- else if (response.result && response.result.entries) {
168
- entries = response.result.entries;
169
- }
170
- else if (response.entries) {
171
- entries = response.entries;
172
- }
173
- if (entries) {
174
- entries.forEach((entry) => {
175
- if (entry.type === 'file' && !this.shouldIgnore(entry.path || entry.name)) {
176
- files.push(entry.path || entry.name);
177
- }
178
- });
179
- }
180
- }
181
- const filteredFiles = files.slice(0, this.listFilesLimit);
182
- return filteredFiles.length > 0
183
- ? filteredFiles.join('\n')
184
- : 'No accessible files found';
185
- }
186
- catch (error) {
187
- return 'Unable to generate files list';
188
- }
189
- }
190
- shouldIgnore(filePath) {
191
- return this.listFilesIgnore.some(ignore => filePath.includes(ignore) || path.basename(filePath) === ignore);
192
- }
193
- }
194
- exports.WorkingDirectoryMessageModifier = WorkingDirectoryMessageModifier;
@@ -1,41 +0,0 @@
1
- import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
2
- export interface AdvancedLoopDetectionProcessorOptions {
3
- toolCallThreshold?: number;
4
- contentLoopThreshold?: number;
5
- contentChunkSize?: number;
6
- maxHistoryLength?: number;
7
- enableLLMDetection?: boolean;
8
- llmCheckAfterTurns?: number;
9
- defaultLLMCheckInterval?: number;
10
- }
11
- export declare class AdvancedLoopDetectionProcessor extends BaseProcessor {
12
- private readonly toolCallThreshold;
13
- private readonly contentLoopThreshold;
14
- private readonly contentChunkSize;
15
- private readonly maxHistoryLength;
16
- private readonly enableLLMDetection;
17
- private readonly llmCheckAfterTurns;
18
- private readonly defaultLLMCheckInterval;
19
- private lastToolCallKey;
20
- private toolCallRepetitionCount;
21
- private streamContentHistory;
22
- private contentStats;
23
- private lastContentIndex;
24
- private loopDetected;
25
- private inCodeBlock;
26
- private turnsInCurrentPrompt;
27
- private llmCheckInterval;
28
- private lastCheckTurn;
29
- constructor(options?: AdvancedLoopDetectionProcessorOptions);
30
- processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
31
- private getToolCallKey;
32
- private checkToolCallLoop;
33
- private checkContentLoop;
34
- private truncateAndUpdate;
35
- private analyzeContentChunksForLoop;
36
- private hasMoreChunksToProcess;
37
- private isLoopDetectedForChunk;
38
- private isActualContentMatch;
39
- private resetContentTracking;
40
- reset(): void;
41
- }
@@ -1,197 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AdvancedLoopDetectionProcessor = void 0;
4
- const processor_1 = require("../../processor");
5
- const crypto_1 = require("crypto");
6
- class AdvancedLoopDetectionProcessor extends processor_1.BaseProcessor {
7
- constructor(options = {}) {
8
- super();
9
- // Tool call tracking
10
- this.lastToolCallKey = null;
11
- this.toolCallRepetitionCount = 0;
12
- // Content streaming tracking
13
- this.streamContentHistory = '';
14
- this.contentStats = new Map();
15
- this.lastContentIndex = 0;
16
- this.loopDetected = false;
17
- this.inCodeBlock = false;
18
- // Turn tracking
19
- this.turnsInCurrentPrompt = 0;
20
- this.lastCheckTurn = 0;
21
- this.toolCallThreshold = options.toolCallThreshold || 5;
22
- this.contentLoopThreshold = options.contentLoopThreshold || 10;
23
- this.contentChunkSize = options.contentChunkSize || 50;
24
- this.maxHistoryLength = options.maxHistoryLength || 1000;
25
- this.enableLLMDetection = options.enableLLMDetection || false;
26
- this.llmCheckAfterTurns = options.llmCheckAfterTurns || 30;
27
- this.defaultLLMCheckInterval = options.defaultLLMCheckInterval || 3;
28
- this.llmCheckInterval = this.defaultLLMCheckInterval;
29
- }
30
- async processInput(input) {
31
- const { message, context } = input;
32
- // Increment turn counter
33
- this.turnsInCurrentPrompt++;
34
- // Check for tool call loops in the last message
35
- const lastMessage = message.messages[message.messages.length - 1];
36
- if (lastMessage.tool_calls && lastMessage.tool_calls.length > 0) {
37
- for (const toolCall of lastMessage.tool_calls) {
38
- const isLoop = this.checkToolCallLoop({
39
- name: toolCall.function.name,
40
- args: typeof toolCall.function.arguments === 'string'
41
- ? JSON.parse(toolCall.function.arguments)
42
- : toolCall.function.arguments
43
- });
44
- if (isLoop) {
45
- this.loopDetected = true;
46
- return [this.createEvent('LoopDetected', {
47
- type: 'tool_call_loop',
48
- toolCall: toolCall.function.name,
49
- repetitionCount: this.toolCallRepetitionCount,
50
- threshold: this.toolCallThreshold
51
- })];
52
- }
53
- }
54
- }
55
- // Check for content loops
56
- if (lastMessage.content && typeof lastMessage.content === 'string') {
57
- const isContentLoop = this.checkContentLoop(lastMessage.content);
58
- if (isContentLoop) {
59
- this.loopDetected = true;
60
- return [this.createEvent('LoopDetected', {
61
- type: 'content_loop',
62
- contentLength: this.streamContentHistory.length,
63
- threshold: this.contentLoopThreshold
64
- })];
65
- }
66
- }
67
- // LLM-based loop detection (if enabled)
68
- if (this.enableLLMDetection &&
69
- this.turnsInCurrentPrompt >= this.llmCheckAfterTurns &&
70
- this.turnsInCurrentPrompt - this.lastCheckTurn >= this.llmCheckInterval) {
71
- this.lastCheckTurn = this.turnsInCurrentPrompt;
72
- // For now, just log that LLM check would happen
73
- // In a real implementation, this would call an LLM to analyze the conversation
74
- console.log('[AdvancedLoopDetection] LLM-based loop check would be performed here');
75
- }
76
- return [this.createEvent('LoopCheckCompleted', {
77
- loopDetected: this.loopDetected,
78
- turnsInPrompt: this.turnsInCurrentPrompt,
79
- toolCallCount: this.toolCallRepetitionCount
80
- })];
81
- }
82
- getToolCallKey(toolCall) {
83
- const argsString = JSON.stringify(toolCall.args);
84
- const keyString = `${toolCall.name}:${argsString}`;
85
- return (0, crypto_1.createHash)('sha256').update(keyString).digest('hex');
86
- }
87
- checkToolCallLoop(toolCall) {
88
- const key = this.getToolCallKey(toolCall);
89
- if (this.lastToolCallKey === key) {
90
- this.toolCallRepetitionCount++;
91
- }
92
- else {
93
- this.lastToolCallKey = key;
94
- this.toolCallRepetitionCount = 1;
95
- }
96
- return this.toolCallRepetitionCount >= this.toolCallThreshold;
97
- }
98
- checkContentLoop(content) {
99
- var _a;
100
- // Different content elements can often contain repetitive syntax that is not indicative of a loop.
101
- // To avoid false positives, we detect when we encounter different content types and
102
- // reset tracking to avoid analyzing content that spans across different element boundaries.
103
- const numFences = ((_a = content.match(/```/g)) !== null && _a !== void 0 ? _a : []).length;
104
- const hasTable = /(^|\n)\s*(\|.*\||[|+-]{3,})/.test(content);
105
- const hasListItem = /(^|\n)\s*[*-+]\s/.test(content) || /(^|\n)\s*\d+\.\s/.test(content);
106
- const hasHeading = /(^|\n)#+\s/.test(content);
107
- const hasBlockquote = /(^|\n)>\s/.test(content);
108
- if (numFences || hasTable || hasListItem || hasHeading || hasBlockquote) {
109
- // Reset tracking when different content elements are detected
110
- this.resetContentTracking();
111
- }
112
- const wasInCodeBlock = this.inCodeBlock;
113
- this.inCodeBlock = numFences % 2 === 0 ? this.inCodeBlock : !this.inCodeBlock;
114
- if (wasInCodeBlock || this.inCodeBlock) {
115
- return false;
116
- }
117
- this.streamContentHistory += content;
118
- this.truncateAndUpdate();
119
- return this.analyzeContentChunksForLoop();
120
- }
121
- truncateAndUpdate() {
122
- if (this.streamContentHistory.length <= this.maxHistoryLength) {
123
- return;
124
- }
125
- const truncationAmount = this.streamContentHistory.length - this.maxHistoryLength;
126
- this.streamContentHistory = this.streamContentHistory.slice(truncationAmount);
127
- this.lastContentIndex = Math.max(0, this.lastContentIndex - truncationAmount);
128
- // Update all stored chunk indices to account for the truncation
129
- for (const [hash, oldIndices] of this.contentStats.entries()) {
130
- const adjustedIndices = oldIndices
131
- .map((index) => index - truncationAmount)
132
- .filter((index) => index >= 0);
133
- if (adjustedIndices.length > 0) {
134
- this.contentStats.set(hash, adjustedIndices);
135
- }
136
- else {
137
- this.contentStats.delete(hash);
138
- }
139
- }
140
- }
141
- analyzeContentChunksForLoop() {
142
- while (this.hasMoreChunksToProcess()) {
143
- const currentChunk = this.streamContentHistory.substring(this.lastContentIndex, this.lastContentIndex + this.contentChunkSize);
144
- const chunkHash = (0, crypto_1.createHash)('sha256').update(currentChunk).digest('hex');
145
- if (this.isLoopDetectedForChunk(currentChunk, chunkHash)) {
146
- return true;
147
- }
148
- this.lastContentIndex++;
149
- }
150
- return false;
151
- }
152
- hasMoreChunksToProcess() {
153
- return this.lastContentIndex + this.contentChunkSize <= this.streamContentHistory.length;
154
- }
155
- isLoopDetectedForChunk(chunk, hash) {
156
- const existingIndices = this.contentStats.get(hash);
157
- if (!existingIndices) {
158
- this.contentStats.set(hash, [this.lastContentIndex]);
159
- return false;
160
- }
161
- if (!this.isActualContentMatch(chunk, existingIndices[0])) {
162
- return false;
163
- }
164
- existingIndices.push(this.lastContentIndex);
165
- if (existingIndices.length < this.contentLoopThreshold) {
166
- return false;
167
- }
168
- // Analyze the most recent occurrences to see if they're clustered closely together
169
- const recentIndices = existingIndices.slice(-this.contentLoopThreshold);
170
- const totalDistance = recentIndices[recentIndices.length - 1] - recentIndices[0];
171
- const averageDistance = totalDistance / (this.contentLoopThreshold - 1);
172
- const maxAllowedDistance = this.contentChunkSize * 1.5;
173
- return averageDistance <= maxAllowedDistance;
174
- }
175
- isActualContentMatch(currentChunk, originalIndex) {
176
- const originalChunk = this.streamContentHistory.substring(originalIndex, originalIndex + this.contentChunkSize);
177
- return originalChunk === currentChunk;
178
- }
179
- resetContentTracking(resetHistory = true) {
180
- if (resetHistory) {
181
- this.streamContentHistory = '';
182
- }
183
- this.contentStats.clear();
184
- this.lastContentIndex = 0;
185
- }
186
- reset() {
187
- this.lastToolCallKey = null;
188
- this.toolCallRepetitionCount = 0;
189
- this.resetContentTracking();
190
- this.turnsInCurrentPrompt = 0;
191
- this.llmCheckInterval = this.defaultLLMCheckInterval;
192
- this.lastCheckTurn = 0;
193
- this.loopDetected = false;
194
- this.inCodeBlock = false;
195
- }
196
- }
197
- exports.AdvancedLoopDetectionProcessor = AdvancedLoopDetectionProcessor;
@@ -1,26 +0,0 @@
1
- import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
2
- export interface ChatCompressionInfo {
3
- originalTokenCount: number;
4
- newTokenCount: number;
5
- compressionRatio: number;
6
- threshold: number;
7
- }
8
- export interface ChatCompressionProcessorOptions {
9
- compressionThreshold?: number;
10
- compressionPreserveThreshold?: number;
11
- tokenLimit?: number;
12
- enableCompression?: boolean;
13
- }
14
- export declare class ChatCompressionProcessor extends BaseProcessor {
15
- private readonly compressionThreshold;
16
- private readonly compressionPreserveThreshold;
17
- private readonly tokenLimit;
18
- private readonly enableCompression;
19
- constructor(options?: ChatCompressionProcessorOptions);
20
- processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
21
- private estimateTokenCount;
22
- setCompressionThreshold(threshold: number): void;
23
- setTokenLimit(limit: number): void;
24
- enableCompressionForSession(): void;
25
- disableCompressionForSession(): void;
26
- }
@@ -1,90 +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.ChatCompressionProcessor = void 0;
7
- const processor_1 = require("../../processor");
8
- const codeboltjs_1 = __importDefault(require("@codebolt/codeboltjs"));
9
- class ChatCompressionProcessor extends processor_1.BaseProcessor {
10
- constructor(options = {}) {
11
- super(options);
12
- this.compressionThreshold = options.compressionThreshold || 0.7;
13
- this.compressionPreserveThreshold = options.compressionPreserveThreshold || 0.3;
14
- this.tokenLimit = options.tokenLimit || 128000;
15
- this.enableCompression = options.enableCompression !== false;
16
- }
17
- async processInput(input) {
18
- try {
19
- const { message, context } = input;
20
- if (!this.enableCompression) {
21
- return [this.createEvent('ChatCompressionDisabled', {
22
- reason: 'Compression is disabled for this session'
23
- })];
24
- }
25
- // Get chat history from CodeBolt
26
- const chatHistory = await codeboltjs_1.default.chat.getChatHistory();
27
- if (chatHistory.length === 0) {
28
- return [this.createEvent('NoChatHistory', {
29
- reason: 'No chat history available for compression'
30
- })];
31
- }
32
- // Estimate token count (simple heuristic: 4 chars per token)
33
- const estimatedTokens = this.estimateTokenCount(chatHistory);
34
- const threshold = this.compressionThreshold * this.tokenLimit;
35
- console.log(`[Compression] Estimated tokens: ${estimatedTokens}, threshold: ${threshold}`);
36
- if (estimatedTokens < threshold) {
37
- return [this.createEvent('ChatCompressionSkipped', {
38
- estimatedTokens,
39
- threshold,
40
- reason: 'Below compression threshold'
41
- })];
42
- }
43
- // Perform compression using CodeBolt's chatSummary
44
- const compressionResult = await codeboltjs_1.default.chatSummary.summarizeAll();
45
- if (compressionResult.success && compressionResult.data) {
46
- const newTokenCount = this.estimateTokenCount(compressionResult.data);
47
- const compressionInfo = {
48
- originalTokenCount: estimatedTokens,
49
- newTokenCount,
50
- compressionRatio: newTokenCount / estimatedTokens,
51
- threshold: this.compressionThreshold
52
- };
53
- // Send notification about compression
54
- await codeboltjs_1.default.chat.sendNotificationEvent(`Chat compressed: ${estimatedTokens} → ${newTokenCount} tokens`, 'debug');
55
- return [this.createEvent('ChatCompressed', compressionInfo)];
56
- }
57
- else {
58
- return [this.createEvent('ChatCompressionFailed', {
59
- error: compressionResult.message || 'Unknown compression error',
60
- originalTokenCount: estimatedTokens
61
- })];
62
- }
63
- }
64
- catch (error) {
65
- console.error('Error in ChatCompressionProcessor:', error);
66
- return [this.createEvent('ChatCompressionError', {
67
- error: error instanceof Error ? error.message : String(error)
68
- })];
69
- }
70
- }
71
- estimateTokenCount(data) {
72
- // Simple heuristic: 4 characters per token
73
- const totalContent = JSON.stringify(data);
74
- return Math.ceil(totalContent.length / 4);
75
- }
76
- // Public methods for external control
77
- setCompressionThreshold(threshold) {
78
- this.compressionThreshold = Math.max(0.1, Math.min(0.9, threshold));
79
- }
80
- setTokenLimit(limit) {
81
- this.tokenLimit = Math.max(1000, limit);
82
- }
83
- enableCompressionForSession() {
84
- this.enableCompression = true;
85
- }
86
- disableCompressionForSession() {
87
- this.enableCompression = false;
88
- }
89
- }
90
- exports.ChatCompressionProcessor = ChatCompressionProcessor;
@@ -1,81 +0,0 @@
1
- import { BaseProcessor, ProcessorInput, ProcessorOutput } from '../../processor';
2
- export interface ChatRecordingProcessorOptions {
3
- enableTokenTracking?: boolean;
4
- exportFormat?: 'json' | 'txt' | 'csv';
5
- outputDirectory?: string;
6
- maxFileSize?: number;
7
- enableCompression?: boolean;
8
- includeMetadata?: boolean;
9
- autoExport?: boolean;
10
- exportInterval?: number;
11
- }
12
- export interface TokensSummary {
13
- input: number;
14
- output: number;
15
- cached: number;
16
- thoughts?: number;
17
- tool?: number;
18
- total: number;
19
- }
20
- export interface MessageRecord {
21
- id: string;
22
- timestamp: string;
23
- role: string;
24
- content: string;
25
- metadata?: Record<string, any>;
26
- tokens?: TokensSummary;
27
- toolCalls?: ToolCallRecord[];
28
- }
29
- export interface ToolCallRecord {
30
- id: string;
31
- name: string;
32
- args: Record<string, unknown>;
33
- result?: any;
34
- status: 'pending' | 'success' | 'error' | 'cancelled';
35
- timestamp: string;
36
- executionTime?: number;
37
- }
38
- export interface ConversationSession {
39
- sessionId: string;
40
- startTime: string;
41
- endTime?: string;
42
- messages: MessageRecord[];
43
- totalTokens: TokensSummary;
44
- toolCallCount: number;
45
- metadata: Record<string, any>;
46
- }
47
- export declare class ChatRecordingProcessor extends BaseProcessor {
48
- private readonly enableTokenTracking;
49
- private readonly exportFormat;
50
- private readonly outputDirectory;
51
- private readonly maxFileSize;
52
- private readonly enableCompression;
53
- private readonly includeMetadata;
54
- private readonly autoExport;
55
- private readonly exportInterval;
56
- private currentSession;
57
- private messageCount;
58
- private sessions;
59
- constructor(options?: ChatRecordingProcessorOptions);
60
- processInput(input: ProcessorInput): Promise<ProcessorOutput[]>;
61
- private initializeSession;
62
- private createMessageRecord;
63
- private extractContent;
64
- private extractToolCalls;
65
- private estimateTokens;
66
- private updateSessionTotals;
67
- private generateSessionId;
68
- private generateMessageId;
69
- private generateToolCallId;
70
- exportSession(session: ConversationSession): Promise<string>;
71
- private ensureDirectoryExists;
72
- private formatAsText;
73
- private formatAsCSV;
74
- finalizeSession(): Promise<string | null>;
75
- getCurrentSession(): ConversationSession | null;
76
- getSessionStats(): {
77
- currentSessionMessages: number;
78
- totalSessions: number;
79
- totalMessages: number;
80
- };
81
- }