Skip to content

Commit 933b770

Browse files
new release
1 parent 897adf1 commit 933b770

File tree

4 files changed

+143
-12
lines changed

4 files changed

+143
-12
lines changed

modules/agentlib/agent.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ declare class Agent {
77
private maxRun;
88
private systemPrompt;
99
private userMessage;
10+
private nextUserMessage;
1011
constructor(tools: any, systemPrompt: SystemPrompt, maxRun?: number, subAgents?: any[]);
1112
run(task: TaskInstruction, successCondition?: () => boolean): Promise<{
1213
success: boolean;

modules/agentlib/agent.js

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,18 @@ class Agent {
7575
console.log("got sub agent resonse result", agentResponse);
7676
const [didUserReject, result] = [false, "tool result is successful"];
7777
console.log("got sub agent resonse result", didUserReject, result);
78-
toolResults.push(this.getToolResult(toolUseId, result));
78+
let toolResult = this.getToolResult(toolUseId, result);
79+
toolResults.push({
80+
role: "tool",
81+
tool_call_id: toolResult.tool_call_id,
82+
content: toolResult.content,
83+
});
84+
if (toolResult.userMessage) {
85+
this.nextUserMessage = {
86+
role: "user",
87+
content: toolResult.userMessage
88+
};
89+
}
7990
if (didUserReject) {
8091
userRejectedToolUse = true;
8192
}
@@ -84,15 +95,38 @@ class Agent {
8495
console.log("calling tool with params", toolName, toolInput);
8596
const [didUserReject, result] = await this.executeTool(toolName, toolInput);
8697
console.log("tool result", result);
87-
toolResults.push(this.getToolResult(toolUseId, result));
98+
// toolResults.push(this.getToolResult(toolUseId, result));
99+
let toolResult = this.getToolResult(toolUseId, result);
100+
toolResults.push({
101+
role: "tool",
102+
tool_call_id: toolResult.tool_call_id,
103+
content: toolResult.content,
104+
});
105+
if (toolResult.userMessage) {
106+
this.nextUserMessage = {
107+
role: "user",
108+
content: toolResult.userMessage
109+
};
110+
}
88111
if (didUserReject) {
89112
userRejectedToolUse = true;
90113
}
91114
}
92115
}
93116
}
94117
else {
95-
toolResults.push(this.getToolResult(toolUseId, "Skipping tool execution due to previous tool user rejection."));
118+
let toolResult = this.getToolResult(toolUseId, "Skipping tool execution due to previous tool user rejection.");
119+
toolResults.push({
120+
role: "tool",
121+
tool_call_id: toolResult.tool_call_id,
122+
content: toolResult.content,
123+
});
124+
if (toolResult.userMessage) {
125+
this.nextUserMessage = {
126+
role: "user",
127+
content: toolResult.userMessage
128+
};
129+
}
96130
}
97131
}
98132
}
@@ -102,9 +136,23 @@ class Agent {
102136
completed = true;
103137
result = "The user is satisfied with the result.";
104138
}
105-
toolResults.push(this.getToolResult(taskCompletedBlock.id, result));
139+
let toolResult = this.getToolResult(taskCompletedBlock.id, result);
140+
toolResults.push({
141+
role: "tool",
142+
tool_call_id: toolResult.tool_call_id,
143+
content: toolResult.content,
144+
});
145+
if (toolResult.userMessage) {
146+
this.nextUserMessage = {
147+
role: "user",
148+
content: toolResult.userMessage
149+
};
150+
}
106151
}
107152
this.apiConversationHistory.push(...toolResults);
153+
if (this.nextUserMessage) {
154+
this.apiConversationHistory.push(this.nextUserMessage);
155+
}
108156
let nextUserMessage = toolResults;
109157
if (toolResults.length === 0) {
110158
nextUserMessage = [{
@@ -175,10 +223,22 @@ class Agent {
175223
};
176224
}
177225
getToolResult(tool_call_id, content) {
226+
let userMessage = undefined;
227+
try {
228+
let parsed = JSON.parse(content);
229+
if (parsed.payload && parsed.payload.content) {
230+
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`;
231+
// this.apiConversationHistory.push()
232+
userMessage = parsed.payload.content;
233+
}
234+
}
235+
catch (error) {
236+
}
178237
return {
179238
role: "tool",
180239
tool_call_id,
181240
content,
241+
userMessage
182242
};
183243
}
184244
// Placeholder for error fallback method

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codebolt/codeboltjs",
3-
"version": "1.1.92",
3+
"version": "1.1.93",
44
"description": "",
55
"keywords": [],
66
"author": "",

src/modules/agentlib/agent.ts

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ interface ToolResult {
1616
role: 'tool';
1717
tool_call_id: string;
1818
content: any;
19+
userMessage?: any;
1920
}
2021

2122
interface ToolDetails {
@@ -31,6 +32,7 @@ class Agent {
3132
private maxRun: number;
3233
private systemPrompt: SystemPrompt;
3334
private userMessage: Message[];
35+
private nextUserMessage:any;
3436

3537

3638
constructor(tools: any = [], systemPrompt: SystemPrompt, maxRun: number = 0, subAgents: any[] = []) {
@@ -47,6 +49,7 @@ class Agent {
4749
this.tools = this.tools.concat(subAgents.map(subagent => ({
4850
...subagent
4951
})));
52+
5053

5154

5255
}
@@ -114,26 +117,64 @@ class Agent {
114117
console.log("got sub agent resonse result", agentResponse);
115118
const [didUserReject, result] = [false, "tool result is successful"];
116119
console.log("got sub agent resonse result", didUserReject, result);
117-
118-
toolResults.push(this.getToolResult(toolUseId, result));
120+
let toolResult = this.getToolResult(toolUseId, result)
121+
toolResults.push({
122+
role: "tool",
123+
tool_call_id: toolResult.tool_call_id,
124+
content: toolResult.content,
125+
126+
});
127+
if (toolResult.userMessage) {
128+
this.nextUserMessage = {
129+
role: "user",
130+
content: toolResult.userMessage
131+
}
132+
}
119133
if (didUserReject) {
120134
userRejectedToolUse = true;
121135
}
136+
122137
}
123138
else {
124139
console.log("calling tool with params", toolName, toolInput);
125140
const [didUserReject, result] = await this.executeTool(toolName, toolInput);
126141
console.log("tool result", result);
127-
toolResults.push(this.getToolResult(toolUseId, result));
128-
142+
// toolResults.push(this.getToolResult(toolUseId, result));
143+
let toolResult = this.getToolResult(toolUseId, result)
144+
toolResults.push({
145+
role: "tool",
146+
tool_call_id: toolResult.tool_call_id,
147+
content: toolResult.content,
148+
149+
});
150+
if (toolResult.userMessage) {
151+
this.nextUserMessage = {
152+
role: "user",
153+
content: toolResult.userMessage
154+
}
155+
}
129156
if (didUserReject) {
130157
userRejectedToolUse = true;
131158
}
132159
}
133160

134161
}
135162
} else {
136-
toolResults.push(this.getToolResult(toolUseId, "Skipping tool execution due to previous tool user rejection."));
163+
let toolResult = this.getToolResult(toolUseId, "Skipping tool execution due to previous tool user rejection.")
164+
165+
toolResults.push({
166+
role: "tool",
167+
tool_call_id: toolResult.tool_call_id,
168+
content: toolResult.content,
169+
170+
});
171+
if (toolResult.userMessage) {
172+
this.nextUserMessage = {
173+
role: "user",
174+
content: toolResult.userMessage
175+
}
176+
}
177+
137178
}
138179
}
139180
}
@@ -148,10 +189,26 @@ class Agent {
148189
completed = true;
149190
result = "The user is satisfied with the result.";
150191
}
151-
toolResults.push(this.getToolResult(taskCompletedBlock.id, result));
192+
let toolResult = this.getToolResult(taskCompletedBlock.id, result)
193+
toolResults.push({
194+
role: "tool",
195+
tool_call_id: toolResult.tool_call_id,
196+
content: toolResult.content,
197+
198+
});
199+
if (toolResult.userMessage) {
200+
this.nextUserMessage = {
201+
role: "user",
202+
content: toolResult.userMessage
203+
}
204+
}
205+
152206
}
153207

154208
this.apiConversationHistory.push(...toolResults);
209+
if (this.nextUserMessage) {
210+
this.apiConversationHistory.push(this.nextUserMessage);
211+
}
155212
let nextUserMessage: Message[] = toolResults;
156213

157214
if (toolResults.length === 0) {
@@ -228,11 +285,24 @@ class Agent {
228285
};
229286
}
230287

231-
private getToolResult(tool_call_id: string, content: any): ToolResult {
288+
private getToolResult(tool_call_id: string, content: string): ToolResult {
289+
let userMessage=undefined
290+
try {
291+
let parsed = JSON.parse(content);
292+
293+
if (parsed.payload && parsed.payload.content) {
294+
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`
295+
// this.apiConversationHistory.push()
296+
userMessage = parsed.payload.content
297+
}
298+
} catch (error) {
299+
300+
}
232301
return {
233302
role: "tool",
234303
tool_call_id,
235304
content,
305+
userMessage
236306
};
237307
}
238308

0 commit comments

Comments
 (0)