@codebolt/codeboltjs 2.0.13 → 2.0.15

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 (125) hide show
  1. package/dist/agentlib/agent.js +3 -7
  2. package/dist/agentlib/followupquestionbuilder.d.ts +75 -0
  3. package/dist/agentlib/followupquestionbuilder.js +193 -0
  4. package/dist/agentlib/llmoutputhandler.d.ts +102 -0
  5. package/dist/agentlib/llmoutputhandler.js +451 -0
  6. package/dist/agentlib/promptbuilder.d.ts +171 -17
  7. package/dist/agentlib/promptbuilder.js +367 -49
  8. package/dist/agentlib/systemprompt.js +3 -3
  9. package/dist/agentlib/usermessage.d.ts +1 -1
  10. package/dist/agentlib/usermessage.js +5 -1
  11. package/dist/core/Codebolt.d.ts +52 -20
  12. package/dist/core/Codebolt.js +43 -1
  13. package/dist/core/websocket.js +2 -1
  14. package/dist/modules/llm.d.ts +23 -3
  15. package/dist/modules/llm.js +20 -3
  16. package/dist/modules/project.d.ts +3 -3
  17. package/dist/modules/project.js +1 -1
  18. package/dist/modules/state.d.ts +5 -3
  19. package/dist/modules/state.js +2 -0
  20. package/dist/modules/task.d.ts +126 -11
  21. package/dist/modules/task.js +262 -20
  22. package/dist/notificationfunctions/agent.d.ts +48 -0
  23. package/dist/notificationfunctions/agent.js +115 -0
  24. package/dist/notificationfunctions/browser.d.ts +60 -0
  25. package/dist/notificationfunctions/browser.js +145 -0
  26. package/dist/notificationfunctions/chat.d.ts +53 -0
  27. package/dist/notificationfunctions/chat.js +125 -0
  28. package/dist/notificationfunctions/codeutils.d.ts +60 -0
  29. package/dist/notificationfunctions/codeutils.js +145 -0
  30. package/dist/notificationfunctions/crawler.d.ts +59 -0
  31. package/dist/notificationfunctions/crawler.js +159 -0
  32. package/dist/notificationfunctions/dbmemory.d.ts +50 -0
  33. package/dist/notificationfunctions/dbmemory.js +132 -0
  34. package/dist/notificationfunctions/fs.d.ts +125 -0
  35. package/dist/notificationfunctions/fs.js +472 -0
  36. package/dist/notificationfunctions/git.d.ts +174 -0
  37. package/dist/notificationfunctions/git.js +521 -0
  38. package/dist/notificationfunctions/history.d.ts +38 -0
  39. package/dist/notificationfunctions/history.js +97 -0
  40. package/dist/notificationfunctions/index.d.ts +79 -0
  41. package/dist/notificationfunctions/index.js +102 -0
  42. package/dist/notificationfunctions/llm.d.ts +39 -0
  43. package/dist/notificationfunctions/llm.js +99 -0
  44. package/dist/notificationfunctions/mcp.d.ts +67 -0
  45. package/dist/notificationfunctions/mcp.js +192 -0
  46. package/dist/notificationfunctions/search.d.ts +50 -0
  47. package/dist/notificationfunctions/search.js +146 -0
  48. package/dist/notificationfunctions/system.d.ts +25 -0
  49. package/dist/notificationfunctions/system.js +59 -0
  50. package/dist/notificationfunctions/terminal.d.ts +26 -0
  51. package/dist/notificationfunctions/terminal.js +64 -0
  52. package/dist/notificationfunctions/todo.d.ts +63 -0
  53. package/dist/notificationfunctions/todo.js +164 -0
  54. package/dist/notificationfunctions/utils.d.ts +81 -0
  55. package/dist/notificationfunctions/utils.js +177 -0
  56. package/dist/types/InternalTypes.d.ts +1 -0
  57. package/dist/types/commonTypes.d.ts +26 -3
  58. package/dist/types/index.d.ts +2 -2
  59. package/dist/types/libFunctionTypes.d.ts +80 -3
  60. package/dist/types/notificationFunctions/agent.d.ts +8 -0
  61. package/dist/types/notificationFunctions/agent.js +2 -0
  62. package/dist/types/notificationFunctions/browser.d.ts +10 -0
  63. package/dist/types/notificationFunctions/browser.js +2 -0
  64. package/dist/types/notificationFunctions/chat.d.ts +10 -0
  65. package/dist/types/notificationFunctions/chat.js +2 -0
  66. package/dist/types/notificationFunctions/codeutils.d.ts +10 -0
  67. package/dist/types/notificationFunctions/codeutils.js +2 -0
  68. package/dist/types/notificationFunctions/crawler.d.ts +10 -0
  69. package/dist/types/notificationFunctions/crawler.js +2 -0
  70. package/dist/types/notificationFunctions/dbmemory.d.ts +9 -0
  71. package/dist/types/notificationFunctions/dbmemory.js +2 -0
  72. package/dist/types/notificationFunctions/fs.d.ts +27 -0
  73. package/dist/types/notificationFunctions/fs.js +2 -0
  74. package/dist/types/notificationFunctions/git.d.ts +29 -0
  75. package/dist/types/notificationFunctions/git.js +2 -0
  76. package/dist/types/notificationFunctions/history.d.ts +10 -0
  77. package/dist/types/notificationFunctions/history.js +2 -0
  78. package/dist/types/notificationFunctions/index.d.ts +0 -0
  79. package/dist/types/notificationFunctions/index.js +1 -0
  80. package/dist/types/notificationFunctions/llm.d.ts +10 -0
  81. package/dist/types/notificationFunctions/llm.js +2 -0
  82. package/dist/types/notificationFunctions/mcp.d.ts +16 -0
  83. package/dist/types/notificationFunctions/mcp.js +2 -0
  84. package/dist/types/notificationFunctions/search.d.ts +11 -0
  85. package/dist/types/notificationFunctions/search.js +2 -0
  86. package/dist/types/notificationFunctions/system.d.ts +7 -0
  87. package/dist/types/notificationFunctions/system.js +2 -0
  88. package/dist/types/notificationFunctions/terminal.d.ts +7 -0
  89. package/dist/types/notificationFunctions/terminal.js +2 -0
  90. package/dist/types/notificationFunctions/todo.d.ts +11 -0
  91. package/dist/types/notificationFunctions/todo.js +2 -0
  92. package/dist/types/notifications/agent.d.ts +31 -0
  93. package/dist/types/notifications/agent.js +3 -0
  94. package/dist/types/notifications/browser.d.ts +53 -0
  95. package/dist/types/notifications/browser.js +3 -0
  96. package/dist/types/notifications/chat.d.ts +37 -0
  97. package/dist/types/notifications/chat.js +3 -0
  98. package/dist/types/notifications/codeutils.d.ts +55 -0
  99. package/dist/types/notifications/codeutils.js +3 -0
  100. package/dist/types/notifications/crawler.d.ts +55 -0
  101. package/dist/types/notifications/crawler.js +3 -0
  102. package/dist/types/notifications/dbmemory.d.ts +31 -0
  103. package/dist/types/notifications/dbmemory.js +3 -0
  104. package/dist/types/notifications/fs.d.ts +180 -0
  105. package/dist/types/notifications/fs.js +3 -0
  106. package/dist/types/notifications/git.d.ts +205 -0
  107. package/dist/types/notifications/git.js +3 -0
  108. package/dist/types/notifications/history.d.ts +32 -0
  109. package/dist/types/notifications/history.js +3 -0
  110. package/dist/types/notifications/llm.d.ts +45 -0
  111. package/dist/types/notifications/llm.js +2 -0
  112. package/dist/types/notifications/mcp.d.ts +63 -0
  113. package/dist/types/notifications/mcp.js +3 -0
  114. package/dist/types/notifications/search.d.ts +47 -0
  115. package/dist/types/notifications/search.js +3 -0
  116. package/dist/types/notifications/system.d.ts +20 -0
  117. package/dist/types/notifications/system.js +2 -0
  118. package/dist/types/notifications/terminal.d.ts +17 -0
  119. package/dist/types/notifications/terminal.js +2 -0
  120. package/dist/types/notifications/todo.d.ts +60 -0
  121. package/dist/types/notifications/todo.js +3 -0
  122. package/dist/types/socketMessageTypes.d.ts +3 -0
  123. package/dist/utils.d.ts +3 -0
  124. package/dist/utils.js +7 -1
  125. package/package.json +1 -1
@@ -40,7 +40,7 @@ class Agent {
40
40
  let mentaionedMCPSTool = await task.userMessage.getMentionedMcpsTools();
41
41
  this.tools = [
42
42
  ...this.tools,
43
- ...(mentaionedMCPSTool || []),
43
+ ...mentaionedMCPSTool || [],
44
44
  ];
45
45
  let mentionedAgents = await task.userMessage.getMentionedAgents();
46
46
  // Transform agents into tool format
@@ -123,9 +123,7 @@ class Agent {
123
123
  }
124
124
  }
125
125
  else {
126
- console.log("Executing tool: ", toolName, toolInput);
127
126
  const [didUserReject, result] = await this.executeTool(toolName, toolInput);
128
- console.log("Tool result: ", result);
129
127
  // toolResults.push(this.getToolResult(toolUseId, result));
130
128
  let toolResult = this.getToolResult(toolUseId, result);
131
129
  toolResults.push({
@@ -164,7 +162,7 @@ class Agent {
164
162
  toolResults.push({
165
163
  role: "tool",
166
164
  tool_call_id: tool.id,
167
- content: String(error),
165
+ content: `please provide valid json string for tool.function.arguments String(error)`,
168
166
  });
169
167
  }
170
168
  }
@@ -261,12 +259,11 @@ class Agent {
261
259
  */
262
260
  async executeTool(toolName, toolInput) {
263
261
  //codebolttools--readfile
264
- console.log("Executing tool: ", toolName, toolInput);
265
262
  const [toolboxName, actualToolName] = toolName.split('--');
266
263
  console.log("Toolbox name: ", toolboxName, "Actual tool name: ", actualToolName);
267
264
  const { data } = await mcp_1.default.executeTool(toolboxName, actualToolName, toolInput);
268
265
  console.log("Tool result: ", data);
269
- return [false, data];
266
+ return data;
270
267
  }
271
268
  /**
272
269
  * Starts a sub-agent to handle a specific task.
@@ -302,7 +299,6 @@ class Agent {
302
299
  let userMessage = undefined;
303
300
  try {
304
301
  let parsed = JSON.parse(content);
305
- console.log("Parsed Content: ", parsed);
306
302
  if (parsed.payload && parsed.payload.content) {
307
303
  content = `The browser action has been executed. The screenshot have been captured for your analysis. The tool response is provided in the next user message`;
308
304
  // this.apiConversationHistory.push()
@@ -0,0 +1,75 @@
1
+ import type { OpenAIMessage, OpenAITool, ToolResult, CodeboltAPI } from "../types/libFunctionTypes";
2
+ /**
3
+ * Builds follow-up prompts for continuing conversations with tool results.
4
+ * Manages conversation history and summarization when conversations get too long.
5
+ */
6
+ declare class FollowUpPromptBuilder {
7
+ /** Previous conversation messages */
8
+ private previousConversation;
9
+ /** Tool results to add to the conversation */
10
+ private toolResults;
11
+ /** Available tools for the conversation */
12
+ private tools;
13
+ /** The last LLM response, if available */
14
+ private llmResponse?;
15
+ /** Maximum conversation length before summarization */
16
+ private maxConversationLength;
17
+ /** Whether to force summarization */
18
+ private forceSummarization;
19
+ /** Codebolt API instance */
20
+ private codebolt?;
21
+ /**
22
+ * Creates a new FollowUpQuestionBuilder instance.
23
+ *
24
+ * @param codebolt - Optional codebolt API instance
25
+ */
26
+ constructor(codebolt?: CodeboltAPI);
27
+ /**
28
+ * Adds the previous conversation to the builder.
29
+ *
30
+ * @param previousPrompt - The previous prompt object containing messages and tools
31
+ * @returns The FollowUpQuestionBuilder instance for chaining
32
+ */
33
+ addPreviousConversation(previousPrompt: {
34
+ messages: OpenAIMessage[];
35
+ tools: OpenAITool[];
36
+ tool_choice?: string;
37
+ }, llmResponse: {
38
+ completion: any;
39
+ }): this;
40
+ addLLMResponseToConverstaion(llmResponse: {
41
+ completion: any;
42
+ }): this;
43
+ /**
44
+ * Adds tool execution results to the conversation.
45
+ *
46
+ * @param toolResults - Array of tool execution results
47
+ * @returns The FollowUpQuestionBuilder instance for chaining
48
+ */
49
+ addToolResult(toolResults: ToolResult[]): this;
50
+ /**
51
+ * Checks if the conversation is too long and sets up summarization with custom max length.
52
+ *
53
+ * @param maxLength - Maximum number of messages before summarization
54
+ * @returns The FollowUpQuestionBuilder instance for chaining
55
+ */
56
+ checkAndSummarizeConversationIfLong(maxLength: number): this;
57
+ /**
58
+ * Performs conversation summarization if needed.
59
+ *
60
+ * @returns Promise that resolves to the summarized messages
61
+ */
62
+ private performSummarization;
63
+ /**
64
+ * Builds the follow-up conversation prompt with tool results.
65
+ *
66
+ * @returns Promise that resolves to the conversation prompt object
67
+ */
68
+ build(): Promise<{
69
+ messages: OpenAIMessage[];
70
+ tools: OpenAITool[];
71
+ tool_choice: "auto";
72
+ full: boolean;
73
+ }>;
74
+ }
75
+ export { FollowUpPromptBuilder };
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FollowUpPromptBuilder = void 0;
4
+ const history_1 = require("../modules/history");
5
+ /**
6
+ * Builds follow-up prompts for continuing conversations with tool results.
7
+ * Manages conversation history and summarization when conversations get too long.
8
+ */
9
+ class FollowUpPromptBuilder {
10
+ /**
11
+ * Creates a new FollowUpQuestionBuilder instance.
12
+ *
13
+ * @param codebolt - Optional codebolt API instance
14
+ */
15
+ constructor(codebolt) {
16
+ /** Previous conversation messages */
17
+ this.previousConversation = [];
18
+ /** Tool results to add to the conversation */
19
+ this.toolResults = [];
20
+ /** Available tools for the conversation */
21
+ this.tools = [];
22
+ /** Maximum conversation length before summarization */
23
+ this.maxConversationLength = 50;
24
+ /** Whether to force summarization */
25
+ this.forceSummarization = false;
26
+ this.codebolt = codebolt;
27
+ }
28
+ /**
29
+ * Adds the previous conversation to the builder.
30
+ *
31
+ * @param previousPrompt - The previous prompt object containing messages and tools
32
+ * @returns The FollowUpQuestionBuilder instance for chaining
33
+ */
34
+ addPreviousConversation(previousPrompt, llmResponse) {
35
+ this.previousConversation = [...previousPrompt.messages];
36
+ this.tools = [...previousPrompt.tools];
37
+ try {
38
+ // Resolve the response if it's a promise
39
+ const resolvedResponse = llmResponse;
40
+ if (!resolvedResponse || !resolvedResponse.completion) {
41
+ console.warn("Invalid LLM response provided");
42
+ return this;
43
+ }
44
+ const completion = resolvedResponse.completion;
45
+ let assistantMessage = null;
46
+ // Handle different response formats
47
+ if (completion.choices && completion.choices.length > 0) {
48
+ // OpenAI-style response with choices
49
+ const choice = completion.choices[0];
50
+ if (choice.message) {
51
+ assistantMessage = {
52
+ role: "assistant",
53
+ content: choice.message.content || "",
54
+ tool_calls: choice.message.tool_calls || undefined
55
+ };
56
+ }
57
+ }
58
+ else if (completion.content) {
59
+ // Direct content response
60
+ assistantMessage = {
61
+ role: "assistant",
62
+ content: completion.content
63
+ };
64
+ }
65
+ else if (completion.message) {
66
+ // Message format response
67
+ assistantMessage = {
68
+ role: "assistant",
69
+ content: completion.message.content || "",
70
+ tool_calls: completion.message.tool_calls || undefined
71
+ };
72
+ }
73
+ // Add the assistant message to conversation history
74
+ if (assistantMessage) {
75
+ this.previousConversation.push(assistantMessage);
76
+ }
77
+ else {
78
+ // Fallback for cases where no valid message is found
79
+ this.previousConversation.push({
80
+ role: "assistant",
81
+ content: "I apologize, but I was unable to provide a proper response."
82
+ });
83
+ }
84
+ }
85
+ catch (error) {
86
+ console.error("Error adding LLM response to conversation:", error);
87
+ // Add error message to conversation history
88
+ this.previousConversation.push({
89
+ role: "assistant",
90
+ content: "An error occurred while processing my response."
91
+ });
92
+ }
93
+ return this;
94
+ }
95
+ addLLMResponseToConverstaion(llmResponse) {
96
+ return this;
97
+ }
98
+ /**
99
+ * Adds tool execution results to the conversation.
100
+ *
101
+ * @param toolResults - Array of tool execution results
102
+ * @returns The FollowUpQuestionBuilder instance for chaining
103
+ */
104
+ addToolResult(toolResults) {
105
+ toolResults.forEach(toolResult => {
106
+ this.previousConversation.push(toolResult);
107
+ });
108
+ if (!toolResults.length) {
109
+ this.previousConversation.push({
110
+ role: "user",
111
+ content: [{
112
+ type: "text",
113
+ text: "If you have completed the user's task, use the attempt_completion tool. If you require additional information from the user, use the ask_followup_question tool. Otherwise, if you have not completed the task and do not need additional information, then proceed with the next step of the task. (This is an automated message, so do not respond to it conversationally.)"
114
+ }]
115
+ });
116
+ }
117
+ return this;
118
+ }
119
+ /**
120
+ * Checks if the conversation is too long and sets up summarization with custom max length.
121
+ *
122
+ * @param maxLength - Maximum number of messages before summarization
123
+ * @returns The FollowUpQuestionBuilder instance for chaining
124
+ */
125
+ checkAndSummarizeConversationIfLong(maxLength) {
126
+ this.maxConversationLength = maxLength;
127
+ this.forceSummarization = this.previousConversation.length > maxLength;
128
+ return this;
129
+ }
130
+ /**
131
+ * Performs conversation summarization if needed.
132
+ *
133
+ * @returns Promise that resolves to the summarized messages
134
+ */
135
+ async performSummarization() {
136
+ const shouldSummarize = this.forceSummarization ||
137
+ this.previousConversation.length > this.maxConversationLength;
138
+ if (!shouldSummarize) {
139
+ return this.previousConversation;
140
+ }
141
+ try {
142
+ console.log("Summarizing conversation due to length:", this.previousConversation.length);
143
+ // Convert OpenAI messages to the format expected by chatSummary
144
+ const messagesToSummarize = this.previousConversation.map(msg => ({
145
+ role: msg.role,
146
+ content: typeof msg.content === 'string' ? msg.content :
147
+ Array.isArray(msg.content) ? msg.content.map(c => c.text).join(' ') :
148
+ String(msg.content)
149
+ }));
150
+ // Use the chat summary service to summarize the conversation
151
+ const summaryResponse = await history_1.chatSummary.summarize(messagesToSummarize, Math.floor(this.maxConversationLength / 2));
152
+ if (summaryResponse.payload || summaryResponse.summary) {
153
+ const summaryText = summaryResponse.payload || summaryResponse.summary || '';
154
+ // Keep the system message if it exists, and replace the rest with summary
155
+ const systemMessage = this.previousConversation.find(msg => msg.role === 'system');
156
+ const summarizedMessages = [];
157
+ if (systemMessage) {
158
+ summarizedMessages.push(systemMessage);
159
+ }
160
+ // Add the summary as a system message
161
+ summarizedMessages.push({
162
+ role: 'system',
163
+ content: `Previous conversation summary: ${summaryText}`
164
+ });
165
+ // Keep the last few messages for context
166
+ const recentMessages = this.previousConversation.slice(-5);
167
+ summarizedMessages.push(...recentMessages);
168
+ return summarizedMessages;
169
+ }
170
+ }
171
+ catch (error) {
172
+ console.error("Error summarizing conversation:", error);
173
+ }
174
+ // If summarization fails, just return the original conversation
175
+ return this.previousConversation;
176
+ }
177
+ /**
178
+ * Builds the follow-up conversation prompt with tool results.
179
+ *
180
+ * @returns Promise that resolves to the conversation prompt object
181
+ */
182
+ async build() {
183
+ // Perform summarization if needed
184
+ let messages = this.previousConversation;
185
+ return {
186
+ messages,
187
+ tools: this.tools,
188
+ full: true,
189
+ tool_choice: "auto"
190
+ };
191
+ }
192
+ }
193
+ exports.FollowUpPromptBuilder = FollowUpPromptBuilder;
@@ -0,0 +1,102 @@
1
+ import type { OpenAIMessage, ToolResult, CodeboltAPI } from "../types/libFunctionTypes";
2
+ /**
3
+ * Handles LLM output processing, tool execution, and completion detection.
4
+ * This class processes LLM responses and manages the conversation flow.
5
+ */
6
+ declare class LLMOutputHandler {
7
+ /** The LLM response to process */
8
+ private llmResponse;
9
+ /** Whether the task has been completed */
10
+ private completed;
11
+ /** Tool results from execution */
12
+ private toolResults;
13
+ /** Next user message to be added to conversation */
14
+ private nextUserMessage;
15
+ /** Codebolt API instance */
16
+ private codebolt?;
17
+ /** Whether tools have been executed */
18
+ private toolsExecuted;
19
+ /**
20
+ * Creates a new LLMOutputHandler instance.
21
+ *
22
+ * @param llmResponse - The LLM response object
23
+ * @param codebolt - Optional codebolt API instance
24
+ */
25
+ constructor(llmResponse: any, codebolt?: CodeboltAPI);
26
+ /**
27
+ * Checks if the task has been completed.
28
+ *
29
+ * @returns True if the task is completed, false otherwise
30
+ */
31
+ isCompleted(): boolean;
32
+ /**
33
+ * Gets the tool execution results.
34
+ *
35
+ * @returns Array of tool results
36
+ */
37
+ getToolResults(): ToolResult[];
38
+ /**
39
+ * Gets the next user message to be added to conversation.
40
+ *
41
+ * @returns The next user message or null
42
+ */
43
+ getNextUserMessage(): OpenAIMessage | null;
44
+ /**
45
+ * Sends the assistant's message to the user interface.
46
+ *
47
+ * @returns Promise that resolves when the message is sent
48
+ */
49
+ sendMessageToUser(): Promise<void>;
50
+ /**
51
+ * Executes all tool calls found in the LLM response.
52
+ *
53
+ * @returns Promise that resolves when all tools are executed
54
+ */
55
+ runTools(): Promise<ToolResult[]>;
56
+ /**
57
+ * Extracts tool calls from the LLM response.
58
+ *
59
+ * @returns Array of tool calls or null if none found
60
+ */
61
+ private getToolCallsFromResponse;
62
+ /**
63
+ * Extracts tool details from a tool call object.
64
+ *
65
+ * @param tool - The tool call object from the LLM response
66
+ * @returns ToolDetails object with name, input, and ID
67
+ */
68
+ private getToolDetail;
69
+ /**
70
+ * Executes a tool with given name and input.
71
+ *
72
+ * @param toolName - The name of the tool to execute
73
+ * @param toolInput - The input parameters for the tool
74
+ * @returns Promise with tuple [userRejected, result]
75
+ */
76
+ private executeTool;
77
+ /**
78
+ * Creates a tool result object from the tool execution response.
79
+ *
80
+ * @param tool_call_id - The ID of the tool call
81
+ * @param content - The content returned by the tool
82
+ * @returns ToolResult object
83
+ */
84
+ private getToolResult;
85
+ /**
86
+ * Checks if the response has tool calls.
87
+ *
88
+ * @returns True if the response contains tool calls
89
+ */
90
+ hasToolCalls(): boolean;
91
+ /**
92
+ * Gets the assistant's message content from the response.
93
+ *
94
+ * @returns The assistant's message content or null
95
+ */
96
+ getAssistantMessage(): string | null;
97
+ /**
98
+ * Resets the handler state for reuse.
99
+ */
100
+ reset(): void;
101
+ }
102
+ export { LLMOutputHandler };