@codebolt/codeboltjs 1.1.89 → 1.1.91
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/.github/workflows/publish-to-npm.yml +1 -1
- package/index.d.ts +6 -3
- package/index.js +1 -1
- package/modules/agent.d.ts +26 -1
- package/modules/agent.js +65 -4
- package/modules/agentlib/agent.d.ts +23 -0
- package/modules/agentlib/agent.js +187 -0
- package/modules/agentlib/systemprompt.d.ts +20 -0
- package/modules/agentlib/systemprompt.js +48 -0
- package/modules/agentlib/taskInstruction.d.ts +22 -0
- package/modules/agentlib/taskInstruction.js +37 -0
- package/modules/agentlib/usermessage.d.ts +22 -0
- package/modules/agentlib/usermessage.js +70 -0
- package/modules/chat.d.ts +11 -1
- package/modules/chat.js +52 -12
- package/modules/fs.d.ts +1 -1
- package/modules/toolBox.d.ts +262 -0
- package/modules/toolBox.js +720 -0
- package/modules/websocket.d.ts +1 -1
- package/modules/websocket.js +13 -11
- package/package.json +28 -4
- package/src/index.ts +6 -2
- package/src/modules/agent.ts +70 -4
- package/src/modules/agentlib/agent.ts +243 -0
- package/src/modules/agentlib/package-lock.json +282 -0
- package/src/modules/agentlib/package.json +6 -0
- package/src/modules/agentlib/systemprompt.ts +55 -0
- package/src/modules/agentlib/taskInstruction.ts +66 -0
- package/src/modules/agentlib/usermessage.ts +97 -0
- package/src/modules/chat.ts +56 -15
- package/src/modules/fs.ts +1 -1
- package/src/modules/toolBox.ts +1164 -0
- package/src/modules/websocket.ts +20 -13
- package/src/utils.ts +5 -0
- package/utils.d.ts +5 -0
- package/utils.js +13 -0
package/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ declare class Codebolt {
|
|
|
25
25
|
updateFile: (filename: string, filePath: string, newContent: string) => Promise<import("@codebolt/types").UpdateFileResponse>;
|
|
26
26
|
deleteFile: (filename: string, filePath: string) => Promise<import("@codebolt/types").DeleteFileResponse>;
|
|
27
27
|
deleteFolder: (foldername: string, folderpath: string) => Promise<import("@codebolt/types").DeleteFolderResponse>;
|
|
28
|
-
listFile: (folderPath: string, isRecursive?: boolean) => Promise<
|
|
28
|
+
listFile: (folderPath: string, isRecursive?: boolean) => Promise<any>;
|
|
29
29
|
listCodeDefinitionNames: (path: string) => Promise<{
|
|
30
30
|
success: boolean;
|
|
31
31
|
result: any;
|
|
@@ -74,6 +74,7 @@ declare class Codebolt {
|
|
|
74
74
|
};
|
|
75
75
|
chat: {
|
|
76
76
|
getChatHistory: () => Promise<import("@codebolt/types").ChatMessage[]>;
|
|
77
|
+
setRequestHandler: (handler: (request: any, response: (data: any) => void) => void | Promise<void>) => void;
|
|
77
78
|
onActionMessage: () => {
|
|
78
79
|
[EventEmitter.captureRejectionSymbol]?<K>(error: Error, event: string | symbol, ...args: any[]): void;
|
|
79
80
|
addListener<K_1>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
@@ -91,7 +92,7 @@ declare class Codebolt {
|
|
|
91
92
|
prependListener<K_10>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
92
93
|
prependOnceListener<K_11>(eventName: string | symbol, listener: (...args: any[]) => void): any;
|
|
93
94
|
eventNames(): (string | symbol)[];
|
|
94
|
-
}
|
|
95
|
+
};
|
|
95
96
|
sendMessage: (message: string, payload: any) => void;
|
|
96
97
|
waitforReply: (message: string) => Promise<import("@codebolt/types").UserMessage>;
|
|
97
98
|
processStarted: () => {
|
|
@@ -244,8 +245,10 @@ declare class Codebolt {
|
|
|
244
245
|
getEnabledMCPS: () => Promise<any>;
|
|
245
246
|
};
|
|
246
247
|
AGENT: {
|
|
247
|
-
|
|
248
|
+
findAgent: (task: string, maxResult: number | undefined, agents: never[] | undefined, agentLocaltion: import("./modules/agent").AgentLocation | undefined, getFrom: import("./modules/agent").FilterUsing.USE_VECTOR_DB) => Promise<any>;
|
|
248
249
|
startAgent: (agentId: string, task: string) => Promise<any>;
|
|
250
|
+
getAgentsList: (type?: import("./modules/agent").Agents) => Promise<any>;
|
|
251
|
+
getAgentsDetail: (agentList?: never[]) => Promise<any>;
|
|
249
252
|
};
|
|
250
253
|
}
|
|
251
254
|
declare const _default: Codebolt;
|
package/index.js
CHANGED
|
@@ -64,6 +64,7 @@ class Codebolt {
|
|
|
64
64
|
this.chatSummary = history_1.chatSummary;
|
|
65
65
|
this.MCP = mcp_1.default;
|
|
66
66
|
this.AGENT = agent_1.default;
|
|
67
|
+
websocket_1.default.initializeWebSocket();
|
|
67
68
|
this.websocket = websocket_1.default.getWebsocket;
|
|
68
69
|
}
|
|
69
70
|
/**
|
|
@@ -91,4 +92,3 @@ class Codebolt {
|
|
|
91
92
|
}
|
|
92
93
|
}
|
|
93
94
|
exports.default = new Codebolt();
|
|
94
|
-
// module.exports = new Codebolt();
|
package/modules/agent.d.ts
CHANGED
|
@@ -1,15 +1,40 @@
|
|
|
1
|
+
export declare enum AgentLocation {
|
|
2
|
+
ALL = "all",
|
|
3
|
+
LOCAL_ONLY = "local_only",
|
|
4
|
+
REMOTE_ONLY = "remote_only"
|
|
5
|
+
}
|
|
6
|
+
export declare enum Agents {
|
|
7
|
+
LOCAL = "local",
|
|
8
|
+
ALL = "all",
|
|
9
|
+
DOWNLOADED = "downloaded"
|
|
10
|
+
}
|
|
11
|
+
export declare enum FilterUsing {
|
|
12
|
+
USE_AI = "use_ai",
|
|
13
|
+
USE_VECTOR_DB = "use_vector_db",
|
|
14
|
+
USE_BOTH = "use_both"
|
|
15
|
+
}
|
|
1
16
|
declare const codeboltAgent: {
|
|
2
17
|
/**
|
|
3
18
|
* Retrieves an agent based on the specified task.
|
|
4
19
|
* @param {string} task - The task for which an agent is needed.
|
|
5
20
|
* @returns {Promise<AgentResponse>} A promise that resolves with the agent details.
|
|
6
21
|
*/
|
|
7
|
-
|
|
22
|
+
findAgent: (task: string, maxResult: number | undefined, agents: never[] | undefined, agentLocaltion: AgentLocation | undefined, getFrom: FilterUsing.USE_VECTOR_DB) => Promise<any>;
|
|
8
23
|
/**
|
|
9
24
|
* Starts an agent for the specified task.
|
|
10
25
|
* @param {string} task - The task for which the agent should be started.
|
|
11
26
|
* @returns {Promise<void>} A promise that resolves when the agent has been successfully started.
|
|
12
27
|
*/
|
|
13
28
|
startAgent: (agentId: string, task: string) => Promise<any>;
|
|
29
|
+
/**
|
|
30
|
+
* Lists all available agents.
|
|
31
|
+
* @returns {Promise<any>} A promise that resolves with the list of agents.
|
|
32
|
+
*/
|
|
33
|
+
getAgentsList: (type?: Agents) => Promise<any>;
|
|
34
|
+
/**
|
|
35
|
+
* Lists all available agents.
|
|
36
|
+
* @returns {Promise<any>} A promise that resolves with the list of agents.
|
|
37
|
+
*/
|
|
38
|
+
getAgentsDetail: (agentList?: never[]) => Promise<any>;
|
|
14
39
|
};
|
|
15
40
|
export default codeboltAgent;
|
package/modules/agent.js
CHANGED
|
@@ -3,23 +3,46 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FilterUsing = exports.Agents = exports.AgentLocation = void 0;
|
|
6
7
|
const websocket_1 = __importDefault(require("./websocket"));
|
|
8
|
+
var AgentLocation;
|
|
9
|
+
(function (AgentLocation) {
|
|
10
|
+
AgentLocation["ALL"] = "all";
|
|
11
|
+
AgentLocation["LOCAL_ONLY"] = "local_only";
|
|
12
|
+
AgentLocation["REMOTE_ONLY"] = "remote_only";
|
|
13
|
+
})(AgentLocation || (exports.AgentLocation = AgentLocation = {}));
|
|
14
|
+
var Agents;
|
|
15
|
+
(function (Agents) {
|
|
16
|
+
Agents["LOCAL"] = "local";
|
|
17
|
+
Agents["ALL"] = "all";
|
|
18
|
+
Agents["DOWNLOADED"] = "downloaded";
|
|
19
|
+
})(Agents || (exports.Agents = Agents = {}));
|
|
20
|
+
var FilterUsing;
|
|
21
|
+
(function (FilterUsing) {
|
|
22
|
+
FilterUsing["USE_AI"] = "use_ai";
|
|
23
|
+
FilterUsing["USE_VECTOR_DB"] = "use_vector_db";
|
|
24
|
+
FilterUsing["USE_BOTH"] = "use_both";
|
|
25
|
+
})(FilterUsing || (exports.FilterUsing = FilterUsing = {}));
|
|
7
26
|
const codeboltAgent = {
|
|
8
27
|
/**
|
|
9
28
|
* Retrieves an agent based on the specified task.
|
|
10
29
|
* @param {string} task - The task for which an agent is needed.
|
|
11
30
|
* @returns {Promise<AgentResponse>} A promise that resolves with the agent details.
|
|
12
31
|
*/
|
|
13
|
-
|
|
32
|
+
findAgent: (task, maxResult = 1, agents = [], agentLocaltion = AgentLocation.ALL, getFrom) => {
|
|
14
33
|
return new Promise((resolve, reject) => {
|
|
15
34
|
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
16
35
|
"type": "agentEvent",
|
|
17
|
-
"action": "
|
|
18
|
-
"task": task
|
|
36
|
+
"action": "findAgent",
|
|
37
|
+
"task": task,
|
|
38
|
+
"agents": agents, // for filter in vector db
|
|
39
|
+
"maxResult": maxResult,
|
|
40
|
+
"location": agentLocaltion,
|
|
41
|
+
"getFrom": getFrom
|
|
19
42
|
}));
|
|
20
43
|
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
21
44
|
const response = JSON.parse(data);
|
|
22
|
-
if (response.type === "
|
|
45
|
+
if (response.type === "findAgentByTaskResponse") {
|
|
23
46
|
resolve(response); // Resolve the Promise with the agent details
|
|
24
47
|
}
|
|
25
48
|
});
|
|
@@ -45,6 +68,44 @@ const codeboltAgent = {
|
|
|
45
68
|
}
|
|
46
69
|
});
|
|
47
70
|
});
|
|
71
|
+
},
|
|
72
|
+
/**
|
|
73
|
+
* Lists all available agents.
|
|
74
|
+
* @returns {Promise<any>} A promise that resolves with the list of agents.
|
|
75
|
+
*/
|
|
76
|
+
getAgentsList: (type = Agents.DOWNLOADED) => {
|
|
77
|
+
return new Promise((resolve, reject) => {
|
|
78
|
+
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
79
|
+
"type": "agentEvent",
|
|
80
|
+
"action": "listAgents",
|
|
81
|
+
"agentType": type,
|
|
82
|
+
}));
|
|
83
|
+
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
84
|
+
const response = JSON.parse(data);
|
|
85
|
+
if (response.type === "listAgentsResponse") {
|
|
86
|
+
resolve(response); // Resolve the Promise with the list of agents
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
},
|
|
91
|
+
/**
|
|
92
|
+
* Lists all available agents.
|
|
93
|
+
* @returns {Promise<any>} A promise that resolves with the list of agents.
|
|
94
|
+
*/
|
|
95
|
+
getAgentsDetail: (agentList = []) => {
|
|
96
|
+
return new Promise((resolve, reject) => {
|
|
97
|
+
websocket_1.default.getWebsocket.send(JSON.stringify({
|
|
98
|
+
"type": "agentEvent",
|
|
99
|
+
"action": "agentsDetail",
|
|
100
|
+
"agentList": agentList
|
|
101
|
+
}));
|
|
102
|
+
websocket_1.default.getWebsocket.on('message', (data) => {
|
|
103
|
+
const response = JSON.parse(data);
|
|
104
|
+
if (response.type === "listAgentsResponse") {
|
|
105
|
+
resolve(response); // Resolve the Promise with the list of agents
|
|
106
|
+
}
|
|
107
|
+
});
|
|
108
|
+
});
|
|
48
109
|
}
|
|
49
110
|
};
|
|
50
111
|
exports.default = codeboltAgent;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { SystemPrompt } from "./systemprompt";
|
|
2
|
+
import { TaskInstruction } from "./taskInstruction";
|
|
3
|
+
declare class Agent {
|
|
4
|
+
private tools;
|
|
5
|
+
private subAgents;
|
|
6
|
+
private apiConversationHistory;
|
|
7
|
+
private maxRun;
|
|
8
|
+
private systemPrompt;
|
|
9
|
+
private userMessage;
|
|
10
|
+
constructor(tools: any, systemPrompt: SystemPrompt, maxRun?: number, subAgents?: any[]);
|
|
11
|
+
run(task: TaskInstruction, successCondition?: () => boolean): Promise<{
|
|
12
|
+
success: boolean;
|
|
13
|
+
error: string | null;
|
|
14
|
+
message: string | null;
|
|
15
|
+
}>;
|
|
16
|
+
private attemptLlmRequest;
|
|
17
|
+
private executeTool;
|
|
18
|
+
private startSubAgent;
|
|
19
|
+
private getToolDetail;
|
|
20
|
+
private getToolResult;
|
|
21
|
+
private attemptApiRequest;
|
|
22
|
+
}
|
|
23
|
+
export { Agent };
|
|
@@ -0,0 +1,187 @@
|
|
|
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.Agent = void 0;
|
|
7
|
+
const chat_1 = __importDefault(require("./../chat"));
|
|
8
|
+
const mcp_1 = __importDefault(require("./../mcp"));
|
|
9
|
+
const llm_1 = __importDefault(require("./../llm"));
|
|
10
|
+
const agent_1 = __importDefault(require("./../agent"));
|
|
11
|
+
class Agent {
|
|
12
|
+
constructor(tools = [], systemPrompt, maxRun = 0, subAgents = []) {
|
|
13
|
+
this.tools = tools;
|
|
14
|
+
this.userMessage = [];
|
|
15
|
+
this.apiConversationHistory = [];
|
|
16
|
+
this.maxRun = maxRun;
|
|
17
|
+
this.systemPrompt = systemPrompt;
|
|
18
|
+
this.subAgents = subAgents;
|
|
19
|
+
this.subAgents = subAgents.map(subagent => {
|
|
20
|
+
subagent.function.name = `subagent--${subagent.function.name}`;
|
|
21
|
+
return subagent;
|
|
22
|
+
});
|
|
23
|
+
this.tools = this.tools.concat(subAgents.map(subagent => ({
|
|
24
|
+
...subagent
|
|
25
|
+
})));
|
|
26
|
+
}
|
|
27
|
+
async run(task, successCondition = () => true) {
|
|
28
|
+
var _a, _b;
|
|
29
|
+
let mentaionedMCPSTool = await task.userMessage.getMentionedMcpsTools();
|
|
30
|
+
this.tools = [
|
|
31
|
+
...this.tools,
|
|
32
|
+
...mentaionedMCPSTool
|
|
33
|
+
];
|
|
34
|
+
let completed = false;
|
|
35
|
+
let userMessages = await task.toPrompt();
|
|
36
|
+
this.apiConversationHistory.push({ role: "user", content: userMessages });
|
|
37
|
+
let runcomplete = 0;
|
|
38
|
+
while (!completed && (runcomplete <= this.maxRun || this.maxRun === 0)) {
|
|
39
|
+
try {
|
|
40
|
+
runcomplete++;
|
|
41
|
+
const response = await this.attemptLlmRequest(this.apiConversationHistory, this.tools);
|
|
42
|
+
let isMessagePresentinReply = false;
|
|
43
|
+
for (const contentBlock of response.choices) {
|
|
44
|
+
if (contentBlock.message) {
|
|
45
|
+
isMessagePresentinReply = true;
|
|
46
|
+
this.apiConversationHistory.push(contentBlock.message);
|
|
47
|
+
if (contentBlock.message.content != null) {
|
|
48
|
+
await chat_1.default.sendMessage(contentBlock.message.content, {});
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (!isMessagePresentinReply) {
|
|
53
|
+
this.apiConversationHistory.push({
|
|
54
|
+
role: "assistant",
|
|
55
|
+
content: [{ type: "text", text: "Failure: I did not provide a response." }],
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
let toolResults = [];
|
|
60
|
+
let taskCompletedBlock;
|
|
61
|
+
let userRejectedToolUse = false;
|
|
62
|
+
const contentBlock = response.choices[0];
|
|
63
|
+
if ((_a = contentBlock.message) === null || _a === void 0 ? void 0 : _a.tool_calls) {
|
|
64
|
+
for (const tool of contentBlock.message.tool_calls) {
|
|
65
|
+
const { toolInput, toolName, toolUseId } = this.getToolDetail(tool);
|
|
66
|
+
if (!userRejectedToolUse) {
|
|
67
|
+
if (toolName.includes("attempt_completion")) {
|
|
68
|
+
taskCompletedBlock = tool;
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
let [serverName] = toolName.replace('--', ':').split(':');
|
|
72
|
+
if (serverName == 'subagent') {
|
|
73
|
+
console.log("calling agent with params", toolName, toolInput);
|
|
74
|
+
const agentResponse = await agent_1.default.startAgent(toolName.replace("subagent--", ''), toolInput.task);
|
|
75
|
+
console.log("got sub agent resonse result", agentResponse);
|
|
76
|
+
const [didUserReject, result] = [false, "tool result is successful"];
|
|
77
|
+
console.log("got sub agent resonse result", didUserReject, result);
|
|
78
|
+
toolResults.push(this.getToolResult(toolUseId, result));
|
|
79
|
+
if (didUserReject) {
|
|
80
|
+
userRejectedToolUse = true;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
console.log("calling tool with params", toolName, toolInput);
|
|
85
|
+
const [didUserReject, result] = await this.executeTool(toolName, toolInput);
|
|
86
|
+
console.log("tool result", result);
|
|
87
|
+
toolResults.push(this.getToolResult(toolUseId, result));
|
|
88
|
+
if (didUserReject) {
|
|
89
|
+
userRejectedToolUse = true;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
toolResults.push(this.getToolResult(toolUseId, "Skipping tool execution due to previous tool user rejection."));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (taskCompletedBlock) {
|
|
100
|
+
let [_, result] = await this.executeTool(taskCompletedBlock.function.name, JSON.parse(taskCompletedBlock.function.arguments || "{}"));
|
|
101
|
+
if (result === "") {
|
|
102
|
+
completed = true;
|
|
103
|
+
result = "The user is satisfied with the result.";
|
|
104
|
+
}
|
|
105
|
+
toolResults.push(this.getToolResult(taskCompletedBlock.id, result));
|
|
106
|
+
}
|
|
107
|
+
this.apiConversationHistory.push(...toolResults);
|
|
108
|
+
let nextUserMessage = toolResults;
|
|
109
|
+
if (toolResults.length === 0) {
|
|
110
|
+
nextUserMessage = [{
|
|
111
|
+
role: "user",
|
|
112
|
+
content: [{
|
|
113
|
+
type: "text",
|
|
114
|
+
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.)"
|
|
115
|
+
}]
|
|
116
|
+
}];
|
|
117
|
+
if (nextUserMessage) {
|
|
118
|
+
this.apiConversationHistory.push(nextUserMessage[0]);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
console.error("Error in agent tool call:", error);
|
|
124
|
+
return { success: false, error: error instanceof Error ? error.message : String(error), message: null };
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
console.error("Error in agent tool call:", error);
|
|
129
|
+
return { success: false, error: error instanceof Error ? error.message : String(error), message: null };
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
success: completed,
|
|
134
|
+
error: null,
|
|
135
|
+
message: ((_b = this.apiConversationHistory
|
|
136
|
+
.filter(msg => msg.role === 'assistant')
|
|
137
|
+
.pop()) === null || _b === void 0 ? void 0 : _b.content) || ''
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
async attemptLlmRequest(apiConversationHistory, tools) {
|
|
141
|
+
try {
|
|
142
|
+
let systemPrompt = await this.systemPrompt.toPromptText();
|
|
143
|
+
const aiMessages = [
|
|
144
|
+
{ role: "system", content: systemPrompt },
|
|
145
|
+
...apiConversationHistory,
|
|
146
|
+
];
|
|
147
|
+
const createParams = {
|
|
148
|
+
full: true,
|
|
149
|
+
messages: aiMessages,
|
|
150
|
+
tools: tools,
|
|
151
|
+
tool_choice: "auto",
|
|
152
|
+
};
|
|
153
|
+
//@ts-ignore
|
|
154
|
+
const { completion } = await llm_1.default.inference(createParams);
|
|
155
|
+
return completion;
|
|
156
|
+
}
|
|
157
|
+
catch (error) {
|
|
158
|
+
console.log(error);
|
|
159
|
+
return this.attemptApiRequest();
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
async executeTool(toolName, toolInput) {
|
|
163
|
+
return mcp_1.default.executeTool(toolName, toolInput);
|
|
164
|
+
}
|
|
165
|
+
async startSubAgent(agentName, params) {
|
|
166
|
+
return agent_1.default.startAgent(agentName, params.task);
|
|
167
|
+
}
|
|
168
|
+
getToolDetail(tool) {
|
|
169
|
+
return {
|
|
170
|
+
toolName: tool.function.name,
|
|
171
|
+
toolInput: JSON.parse(tool.function.arguments || "{}"),
|
|
172
|
+
toolUseId: tool.id
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
getToolResult(tool_call_id, content) {
|
|
176
|
+
return {
|
|
177
|
+
role: "tool",
|
|
178
|
+
tool_call_id,
|
|
179
|
+
content,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
// Placeholder for error fallback method
|
|
183
|
+
attemptApiRequest() {
|
|
184
|
+
throw new Error("API request fallback not implemented");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports.Agent = Agent;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SystemPrompt class for loading and managing system prompts from YAML files
|
|
3
|
+
*/
|
|
4
|
+
declare class SystemPrompt {
|
|
5
|
+
private filepath;
|
|
6
|
+
private key;
|
|
7
|
+
/**
|
|
8
|
+
* Creates a SystemPrompt instance
|
|
9
|
+
* @param {string} filepath - Path to the YAML file containing prompts
|
|
10
|
+
* @param {string} key - Key identifier for the specific prompt
|
|
11
|
+
*/
|
|
12
|
+
constructor(filepath?: string, key?: string);
|
|
13
|
+
/**
|
|
14
|
+
* Loads and returns the prompt text
|
|
15
|
+
* @returns {string} The prompt text
|
|
16
|
+
* @throws {Error} If file cannot be read or parsed
|
|
17
|
+
*/
|
|
18
|
+
toPromptText(): string;
|
|
19
|
+
}
|
|
20
|
+
export { SystemPrompt };
|
|
@@ -0,0 +1,48 @@
|
|
|
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.SystemPrompt = void 0;
|
|
7
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
8
|
+
const fs_1 = __importDefault(require("fs"));
|
|
9
|
+
const path_1 = __importDefault(require("path"));
|
|
10
|
+
/**
|
|
11
|
+
* SystemPrompt class for loading and managing system prompts from YAML files
|
|
12
|
+
*/
|
|
13
|
+
class SystemPrompt {
|
|
14
|
+
/**
|
|
15
|
+
* Creates a SystemPrompt instance
|
|
16
|
+
* @param {string} filepath - Path to the YAML file containing prompts
|
|
17
|
+
* @param {string} key - Key identifier for the specific prompt
|
|
18
|
+
*/
|
|
19
|
+
constructor(filepath = "", key = "") {
|
|
20
|
+
this.filepath = filepath;
|
|
21
|
+
this.key = key;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Loads and returns the prompt text
|
|
25
|
+
* @returns {string} The prompt text
|
|
26
|
+
* @throws {Error} If file cannot be read or parsed
|
|
27
|
+
*/
|
|
28
|
+
toPromptText() {
|
|
29
|
+
var _a;
|
|
30
|
+
try {
|
|
31
|
+
const absolutePath = path_1.default.resolve(this.filepath);
|
|
32
|
+
const fileContents = fs_1.default.readFileSync(absolutePath, 'utf8');
|
|
33
|
+
const data = js_yaml_1.default.load(fileContents);
|
|
34
|
+
if (!data || typeof data !== 'object') {
|
|
35
|
+
throw new Error('Invalid YAML structure');
|
|
36
|
+
}
|
|
37
|
+
if (!((_a = data[this.key]) === null || _a === void 0 ? void 0 : _a.prompt)) {
|
|
38
|
+
throw new Error(`Prompt not found for key: ${this.key}`);
|
|
39
|
+
}
|
|
40
|
+
return data[this.key].prompt;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`SystemPrompt Error: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
44
|
+
throw error; // Re-throw to allow caller handling
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
exports.SystemPrompt = SystemPrompt;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { UserMessage, UserMessageContent } from "./usermessage";
|
|
2
|
+
interface Tools {
|
|
3
|
+
[key: string]: {
|
|
4
|
+
description: string;
|
|
5
|
+
usage: string;
|
|
6
|
+
example?: string;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
interface UserMessages {
|
|
10
|
+
type: string;
|
|
11
|
+
text: string;
|
|
12
|
+
}
|
|
13
|
+
declare class TaskInstruction {
|
|
14
|
+
tools: Tools;
|
|
15
|
+
userMessages: UserMessageContent[];
|
|
16
|
+
userMessage: UserMessage;
|
|
17
|
+
filepath: string;
|
|
18
|
+
refsection: string;
|
|
19
|
+
constructor(tools: Tools | undefined, userMessage: UserMessage, filepath?: string, refsection?: string);
|
|
20
|
+
toPrompt(): Promise<UserMessages[]>;
|
|
21
|
+
}
|
|
22
|
+
export { TaskInstruction };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TaskInstruction = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Encapsulates task instructions and their related metadata.
|
|
6
|
+
* Handles loading and processing of task instructions from YAML files.
|
|
7
|
+
*/
|
|
8
|
+
const yaml = require('js-yaml');
|
|
9
|
+
const fs = require('fs');
|
|
10
|
+
const path = require('path');
|
|
11
|
+
class TaskInstruction {
|
|
12
|
+
constructor(tools = {}, userMessage, filepath = "", refsection = "") {
|
|
13
|
+
this.userMessages = [];
|
|
14
|
+
this.tools = tools;
|
|
15
|
+
this.userMessage = userMessage;
|
|
16
|
+
this.filepath = filepath;
|
|
17
|
+
this.refsection = refsection;
|
|
18
|
+
}
|
|
19
|
+
async toPrompt() {
|
|
20
|
+
try {
|
|
21
|
+
this.userMessages = await this.userMessage.toPrompt();
|
|
22
|
+
const fileContents = fs.readFileSync(path.resolve(this.filepath), 'utf8');
|
|
23
|
+
const data = yaml.load(fileContents);
|
|
24
|
+
const task = data[this.refsection];
|
|
25
|
+
this.userMessages.push({
|
|
26
|
+
type: "text",
|
|
27
|
+
text: `Task Description: ${task.description}\nExpected Output: ${task.expected_output}`
|
|
28
|
+
});
|
|
29
|
+
return this.userMessages;
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
console.error(`Error processing task instruction: ${error}`);
|
|
33
|
+
throw error;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
exports.TaskInstruction = TaskInstruction;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface Message {
|
|
2
|
+
userMessage: string;
|
|
3
|
+
mentionedFiles?: string[];
|
|
4
|
+
mentionedMCPs: string[];
|
|
5
|
+
}
|
|
6
|
+
export interface UserMessageContent {
|
|
7
|
+
type: string;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
declare class UserMessage {
|
|
11
|
+
message: Message;
|
|
12
|
+
promptOverride: boolean;
|
|
13
|
+
userMessages: UserMessageContent[];
|
|
14
|
+
mentaionedMCPS: string[];
|
|
15
|
+
constructor(message: Message, promptOverride?: boolean);
|
|
16
|
+
getFiles(): void;
|
|
17
|
+
toPrompt(bAttachFiles?: boolean, bAttachImages?: boolean, bAttachEnvironment?: boolean): Promise<UserMessageContent[]>;
|
|
18
|
+
getMentionedMcps(): string[];
|
|
19
|
+
getMentionedMcpsTools(): Promise<any>;
|
|
20
|
+
private getEnvironmentDetail;
|
|
21
|
+
}
|
|
22
|
+
export { UserMessage };
|
|
@@ -0,0 +1,70 @@
|
|
|
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.UserMessage = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("./../fs"));
|
|
8
|
+
const project_1 = __importDefault(require("./../project"));
|
|
9
|
+
const mcp_1 = __importDefault(require("./../mcp"));
|
|
10
|
+
class UserMessage {
|
|
11
|
+
constructor(message, promptOverride = false) {
|
|
12
|
+
this.getEnvironmentDetail = async (cwd) => {
|
|
13
|
+
let details = "";
|
|
14
|
+
const { success, result } = await fs_1.default.listFile(cwd, true);
|
|
15
|
+
details += `\n\n# Current Working Directory (${cwd}) Files\n${result}
|
|
16
|
+
? "\n(Note: Only top-level contents shown for Desktop by default. Use list_files to explore further if necessary.)"
|
|
17
|
+
: ""
|
|
18
|
+
}`;
|
|
19
|
+
return `<environment_details>\n${details.trim()}\n</environment_details>`;
|
|
20
|
+
};
|
|
21
|
+
this.message = message;
|
|
22
|
+
this.promptOverride = promptOverride;
|
|
23
|
+
this.userMessages = [];
|
|
24
|
+
this.mentaionedMCPS = message.mentionedMCPs;
|
|
25
|
+
}
|
|
26
|
+
getFiles() {
|
|
27
|
+
// Implementation to be added
|
|
28
|
+
}
|
|
29
|
+
async toPrompt(bAttachFiles = true, bAttachImages = true, bAttachEnvironment = true) {
|
|
30
|
+
var _a;
|
|
31
|
+
if (bAttachFiles) {
|
|
32
|
+
if (this.promptOverride) {
|
|
33
|
+
// Use a rendering engine
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
let finalPrompt = `
|
|
37
|
+
The user has sent the following query:
|
|
38
|
+
${this.message.userMessage}.
|
|
39
|
+
`;
|
|
40
|
+
if ((_a = this.message.mentionedFiles) === null || _a === void 0 ? void 0 : _a.length) {
|
|
41
|
+
finalPrompt += `The Attached files are:`;
|
|
42
|
+
for (const file of this.message.mentionedFiles) {
|
|
43
|
+
let filedata = await fs_1.default.readFile(file);
|
|
44
|
+
finalPrompt += `File Name: ${file}, File Path: ${file}, Filedata: ${filedata}`;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
this.userMessages.push({ type: "text", text: finalPrompt });
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (bAttachEnvironment) {
|
|
51
|
+
let { projectPath } = await project_1.default.getProjectPath();
|
|
52
|
+
const environmentDetail = await this.getEnvironmentDetail(projectPath);
|
|
53
|
+
this.userMessages.push({ type: "text", text: environmentDetail });
|
|
54
|
+
}
|
|
55
|
+
return this.userMessages;
|
|
56
|
+
}
|
|
57
|
+
getMentionedMcps() {
|
|
58
|
+
return this.message.mentionedMCPs || [];
|
|
59
|
+
}
|
|
60
|
+
async getMentionedMcpsTools() {
|
|
61
|
+
if (this.mentaionedMCPS.length > 0) {
|
|
62
|
+
let tools = await mcp_1.default.getMcpTools(this.mentaionedMCPS);
|
|
63
|
+
return tools;
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.UserMessage = UserMessage;
|
package/modules/chat.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
3
|
import { ChatMessage, UserMessage } from '@codebolt/types';
|
|
4
|
+
type RequestHandler = (request: any, response: (data: any) => void) => Promise<void> | void;
|
|
4
5
|
/**
|
|
5
6
|
* CustomEventEmitter class that extends the Node.js EventEmitter class.
|
|
6
7
|
*/
|
|
@@ -15,11 +16,20 @@ declare const cbchat: {
|
|
|
15
16
|
* @returns {Promise<ChatMessage[]>} A promise that resolves with an array of ChatMessage objects representing the chat history.
|
|
16
17
|
*/
|
|
17
18
|
getChatHistory: () => Promise<ChatMessage[]>;
|
|
19
|
+
/**
|
|
20
|
+
* Sets a global request handler for all incoming messages
|
|
21
|
+
* @param handler The async handler function
|
|
22
|
+
*/
|
|
23
|
+
setRequestHandler: (handler: RequestHandler) => void;
|
|
18
24
|
/**
|
|
19
25
|
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
|
|
20
26
|
* @returns {EventEmitter} The event emitter used for emitting custom events.
|
|
21
27
|
*/
|
|
22
|
-
|
|
28
|
+
/**
|
|
29
|
+
* Sets up a listener for incoming WebSocket messages and emits a custom event when a message is received.
|
|
30
|
+
* @returns {EventEmitter} The event emitter used for emitting custom events.
|
|
31
|
+
*/
|
|
32
|
+
onActionMessage: () => CustomEventEmitter;
|
|
23
33
|
/**
|
|
24
34
|
* Sends a message through the WebSocket connection.
|
|
25
35
|
* @param {string} message - The message to be sent.
|