@codebolt/codeboltjs 2.0.16 → 2.0.17
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/core/Codebolt.d.ts +1 -1
- package/dist/modules/mcp.d.ts +1 -2
- package/dist/modules/mcp.js +2 -0
- package/dist/utils.d.ts +0 -7
- package/dist/utils.js +1 -15
- package/package.json +1 -1
- package/dist/agentlib/agent.d.ts +0 -86
- package/dist/agentlib/agent.js +0 -326
- package/dist/agentlib/followupquestionbuilder.d.ts +0 -75
- package/dist/agentlib/followupquestionbuilder.js +0 -193
- package/dist/agentlib/llmoutputhandler.d.ts +0 -102
- package/dist/agentlib/llmoutputhandler.js +0 -451
- package/dist/agentlib/promptbuilder.d.ts +0 -382
- package/dist/agentlib/promptbuilder.js +0 -805
- package/dist/agentlib/systemprompt.d.ts +0 -20
- package/dist/agentlib/systemprompt.js +0 -48
- package/dist/agentlib/taskInstruction.d.ts +0 -37
- package/dist/agentlib/taskInstruction.js +0 -57
- package/dist/agentlib/usermessage.d.ts +0 -70
- package/dist/agentlib/usermessage.js +0 -124
|
@@ -1,193 +0,0 @@
|
|
|
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;
|
|
@@ -1,102 +0,0 @@
|
|
|
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 };
|