@tyvm/knowhow 0.0.21 ā 0.0.23
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/package.json +3 -1
- package/src/agents/base/base.ts +16 -7
- package/src/agents/configurable/ConfigAgent.ts +5 -3
- package/src/agents/developer/developer.ts +3 -4
- package/src/agents/index.ts +26 -2
- package/src/agents/patcher/patcher.ts +3 -5
- package/src/agents/researcher/researcher.ts +3 -4
- package/src/agents/tools/agentCall.ts +5 -2
- package/src/agents/tools/executeScript/README.md +78 -0
- package/src/agents/tools/executeScript/definition.ts +73 -0
- package/src/agents/tools/executeScript/examples/dependency-injection-validation.ts +272 -0
- package/src/agents/tools/executeScript/examples/quick-test.ts +74 -0
- package/src/agents/tools/executeScript/examples/serialization-test.ts +321 -0
- package/src/agents/tools/executeScript/examples/test-runner.ts +197 -0
- package/src/agents/tools/executeScript/index.ts +93 -0
- package/src/agents/tools/index.ts +1 -0
- package/src/agents/tools/list.ts +2 -1
- package/src/agents/vim/vim.ts +3 -4
- package/src/ai.ts +2 -1
- package/src/chat.ts +4 -2
- package/src/cli.ts +7 -15
- package/src/clients/index.ts +23 -9
- package/src/dataset/diffs/test.ts +2 -1
- package/src/index.ts +3 -3
- package/src/services/AgentService.ts +9 -10
- package/src/services/EventService.ts +0 -2
- package/src/services/GitHub.ts +0 -1
- package/src/services/KnowhowClient.ts +0 -3
- package/src/services/Mcp.ts +0 -2
- package/src/services/S3.ts +0 -1
- package/src/services/Tools.ts +63 -8
- package/src/services/flags.ts +0 -1
- package/src/services/index.ts +56 -0
- package/src/services/modules/index.ts +53 -0
- package/src/{modules ā services/modules}/types.ts +16 -5
- package/src/services/script-execution/SandboxContext.ts +278 -0
- package/src/services/script-execution/ScriptExecutor.ts +339 -0
- package/src/services/script-execution/ScriptPolicy.ts +236 -0
- package/src/services/script-execution/ScriptTracer.ts +249 -0
- package/src/services/script-execution/types.ts +134 -0
- package/src/worker.ts +3 -3
- package/tests/integration/fileblocks/readwrite.test.ts +2 -1
- package/tests/integration/patching.test.ts +5 -5
- package/ts_build/src/agents/base/base.d.ts +9 -4
- package/ts_build/src/agents/base/base.js +7 -10
- package/ts_build/src/agents/base/base.js.map +1 -1
- package/ts_build/src/agents/configurable/ConfigAgent.d.ts +2 -2
- package/ts_build/src/agents/configurable/ConfigAgent.js +2 -2
- package/ts_build/src/agents/configurable/ConfigAgent.js.map +1 -1
- package/ts_build/src/agents/developer/developer.d.ts +2 -3
- package/ts_build/src/agents/developer/developer.js +3 -4
- package/ts_build/src/agents/developer/developer.js.map +1 -1
- package/ts_build/src/agents/index.d.ts +11 -2
- package/ts_build/src/agents/index.js +19 -3
- package/ts_build/src/agents/index.js.map +1 -1
- package/ts_build/src/agents/patcher/patcher.d.ts +2 -3
- package/ts_build/src/agents/patcher/patcher.js +3 -4
- package/ts_build/src/agents/patcher/patcher.js.map +1 -1
- package/ts_build/src/agents/researcher/researcher.d.ts +2 -3
- package/ts_build/src/agents/researcher/researcher.js +3 -4
- package/ts_build/src/agents/researcher/researcher.js.map +1 -1
- package/ts_build/src/agents/tools/agentCall.js +4 -4
- package/ts_build/src/agents/tools/agentCall.js.map +1 -1
- package/ts_build/src/agents/tools/executeScript/definition.d.ts +2 -0
- package/ts_build/src/agents/tools/executeScript/definition.js +70 -0
- package/ts_build/src/agents/tools/executeScript/definition.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/examples/dependency-injection-validation.d.ts +18 -0
- package/ts_build/src/agents/tools/executeScript/examples/dependency-injection-validation.js +192 -0
- package/ts_build/src/agents/tools/executeScript/examples/dependency-injection-validation.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/examples/quick-test.d.ts +3 -0
- package/ts_build/src/agents/tools/executeScript/examples/quick-test.js +65 -0
- package/ts_build/src/agents/tools/executeScript/examples/quick-test.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/examples/serialization-test.d.ts +15 -0
- package/ts_build/src/agents/tools/executeScript/examples/serialization-test.js +266 -0
- package/ts_build/src/agents/tools/executeScript/examples/serialization-test.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/examples/simple-example.d.ts +20 -0
- package/ts_build/src/agents/tools/executeScript/examples/simple-example.js +35 -0
- package/ts_build/src/agents/tools/executeScript/examples/simple-example.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/examples/test-runner.d.ts +4 -0
- package/ts_build/src/agents/tools/executeScript/examples/test-runner.js +198 -0
- package/ts_build/src/agents/tools/executeScript/examples/test-runner.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/handler.d.ts +27 -0
- package/ts_build/src/agents/tools/executeScript/handler.js +64 -0
- package/ts_build/src/agents/tools/executeScript/handler.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript/index.d.ts +27 -0
- package/ts_build/src/agents/tools/executeScript/index.js +70 -0
- package/ts_build/src/agents/tools/executeScript/index.js.map +1 -0
- package/ts_build/src/agents/tools/executeScript.d.ts +29 -0
- package/ts_build/src/agents/tools/executeScript.js +124 -0
- package/ts_build/src/agents/tools/executeScript.js.map +1 -0
- package/ts_build/src/agents/tools/index.d.ts +1 -0
- package/ts_build/src/agents/tools/index.js +1 -0
- package/ts_build/src/agents/tools/index.js.map +1 -1
- package/ts_build/src/agents/tools/list.js +2 -0
- package/ts_build/src/agents/tools/list.js.map +1 -1
- package/ts_build/src/agents/vim/vim.d.ts +2 -3
- package/ts_build/src/agents/vim/vim.js +3 -4
- package/ts_build/src/agents/vim/vim.js.map +1 -1
- package/ts_build/src/ai.js +2 -1
- package/ts_build/src/ai.js.map +1 -1
- package/ts_build/src/chat.js +10 -9
- package/ts_build/src/chat.js.map +1 -1
- package/ts_build/src/cli.js +12 -19
- package/ts_build/src/cli.js.map +1 -1
- package/ts_build/src/clients/index.d.ts +9 -2
- package/ts_build/src/clients/index.js +17 -4
- package/ts_build/src/clients/index.js.map +1 -1
- package/ts_build/src/dataset/diffs/test.js +2 -1
- package/ts_build/src/dataset/diffs/test.js.map +1 -1
- package/ts_build/src/index.js +10 -10
- package/ts_build/src/index.js.map +1 -1
- package/ts_build/src/services/AgentService.d.ts +7 -3
- package/ts_build/src/services/AgentService.js +11 -10
- package/ts_build/src/services/AgentService.js.map +1 -1
- package/ts_build/src/services/EventService.d.ts +0 -1
- package/ts_build/src/services/EventService.js +1 -2
- package/ts_build/src/services/EventService.js.map +1 -1
- package/ts_build/src/services/GitHub.d.ts +0 -1
- package/ts_build/src/services/GitHub.js +1 -2
- package/ts_build/src/services/GitHub.js.map +1 -1
- package/ts_build/src/services/KnowhowClient.d.ts +0 -1
- package/ts_build/src/services/KnowhowClient.js +1 -2
- package/ts_build/src/services/KnowhowClient.js.map +1 -1
- package/ts_build/src/services/Mcp.d.ts +0 -1
- package/ts_build/src/services/Mcp.js +1 -2
- package/ts_build/src/services/Mcp.js.map +1 -1
- package/ts_build/src/services/S3.d.ts +0 -1
- package/ts_build/src/services/S3.js +1 -2
- package/ts_build/src/services/S3.js.map +1 -1
- package/ts_build/src/services/Tools.d.ts +22 -1
- package/ts_build/src/services/Tools.js +32 -6
- package/ts_build/src/services/Tools.js.map +1 -1
- package/ts_build/src/services/flags.d.ts +0 -1
- package/ts_build/src/services/flags.js +1 -2
- package/ts_build/src/services/flags.js.map +1 -1
- package/ts_build/src/services/index.d.ts +25 -0
- package/ts_build/src/services/index.js +42 -1
- package/ts_build/src/services/index.js.map +1 -1
- package/ts_build/src/services/modules/example-usage.d.ts +11 -0
- package/ts_build/src/services/modules/example-usage.js +43 -0
- package/ts_build/src/services/modules/example-usage.js.map +1 -0
- package/ts_build/src/services/modules/index.d.ts +4 -0
- package/ts_build/src/services/modules/index.js +44 -0
- package/ts_build/src/services/modules/index.js.map +1 -0
- package/ts_build/src/services/modules/types.d.ts +47 -0
- package/ts_build/src/services/modules/types.js +3 -0
- package/ts_build/src/services/modules/types.js.map +1 -0
- package/ts_build/src/services/script-execution/SandboxContext.d.ts +34 -0
- package/ts_build/src/services/script-execution/SandboxContext.js +186 -0
- package/ts_build/src/services/script-execution/SandboxContext.js.map +1 -0
- package/ts_build/src/services/script-execution/ScriptExecutor.d.ts +17 -0
- package/ts_build/src/services/script-execution/ScriptExecutor.js +211 -0
- package/ts_build/src/services/script-execution/ScriptExecutor.js.map +1 -0
- package/ts_build/src/services/script-execution/ScriptPolicy.d.ts +27 -0
- package/ts_build/src/services/script-execution/ScriptPolicy.js +150 -0
- package/ts_build/src/services/script-execution/ScriptPolicy.js.map +1 -0
- package/ts_build/src/services/script-execution/ScriptTracer.d.ts +19 -0
- package/ts_build/src/services/script-execution/ScriptTracer.js +186 -0
- package/ts_build/src/services/script-execution/ScriptTracer.js.map +1 -0
- package/ts_build/src/services/script-execution/types.d.ts +108 -0
- package/ts_build/src/services/script-execution/types.js +3 -0
- package/ts_build/src/services/script-execution/types.js.map +1 -0
- package/ts_build/src/services/singletons.d.ts +17 -0
- package/ts_build/src/services/singletons.js +28 -0
- package/ts_build/src/services/singletons.js.map +1 -0
- package/ts_build/src/worker.js +4 -3
- package/ts_build/src/worker.js.map +1 -1
- package/ts_build/tests/integration/fileblocks/readwrite.test.js +10 -9
- package/ts_build/tests/integration/fileblocks/readwrite.test.js.map +1 -1
- package/ts_build/tests/integration/patching.test.js +9 -10
- package/ts_build/tests/integration/patching.test.js.map +1 -1
- package/src/modules/index.ts +0 -37
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tyvm/knowhow",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.23",
|
|
4
4
|
"description": "ai cli with plugins and agents",
|
|
5
5
|
"main": "ts_build/src/index.js",
|
|
6
6
|
"bin": {
|
|
@@ -51,10 +51,12 @@
|
|
|
51
51
|
"axios": "^1.5.0",
|
|
52
52
|
"cheerio": "^1.0.0",
|
|
53
53
|
"diff": "^5.2.0",
|
|
54
|
+
"esbuild": "^0.25.8",
|
|
54
55
|
"express": "^4.19.2",
|
|
55
56
|
"figma-js": "^1.16.1-0",
|
|
56
57
|
"gitignore-to-glob": "^0.3.0",
|
|
57
58
|
"ink": "^6.0.1",
|
|
59
|
+
"isolated-vm": "^5.0.4",
|
|
58
60
|
"jira-client": "^8.2.2",
|
|
59
61
|
"marked": "^10.0.0",
|
|
60
62
|
"marked-terminal": "^6.2.0",
|
package/src/agents/base/base.ts
CHANGED
|
@@ -7,13 +7,13 @@ import {
|
|
|
7
7
|
ToolCall,
|
|
8
8
|
} from "../../clients/types";
|
|
9
9
|
import { IAgent } from "../interface";
|
|
10
|
-
import { ToolsService
|
|
10
|
+
import { ToolsService } from "../../services/Tools";
|
|
11
11
|
import {
|
|
12
12
|
mcpToolName,
|
|
13
13
|
replaceEscapedNewLines,
|
|
14
14
|
restoreEscapedNewLines,
|
|
15
15
|
} from "../../utils";
|
|
16
|
-
import {
|
|
16
|
+
import { EventService } from "../../services/EventService";
|
|
17
17
|
import { AIClient, Clients } from "../../clients";
|
|
18
18
|
import { Models } from "../../ai";
|
|
19
19
|
import { MessageProcessor } from "../../services/MessageProcessor";
|
|
@@ -24,6 +24,12 @@ export interface ModelPreference {
|
|
|
24
24
|
provider: keyof typeof Clients.clients;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
export interface AgentContext {
|
|
28
|
+
Tools?: ToolsService;
|
|
29
|
+
Events?: EventService;
|
|
30
|
+
messageProcessor?: MessageProcessor;
|
|
31
|
+
}
|
|
32
|
+
|
|
27
33
|
export abstract class BaseAgent implements IAgent {
|
|
28
34
|
abstract name: string;
|
|
29
35
|
abstract description: string;
|
|
@@ -55,14 +61,17 @@ export abstract class BaseAgent implements IAgent {
|
|
|
55
61
|
kill: "kill",
|
|
56
62
|
unpause: "unpause",
|
|
57
63
|
};
|
|
64
|
+
public tools: ToolsService;
|
|
65
|
+
public events: EventService;
|
|
66
|
+
public messageProcessor: MessageProcessor;
|
|
58
67
|
|
|
59
68
|
disabledTools = [];
|
|
60
69
|
|
|
61
|
-
constructor(
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
70
|
+
constructor(context: AgentContext) {
|
|
71
|
+
this.tools = context.Tools;
|
|
72
|
+
this.events = context.Events;
|
|
73
|
+
this.messageProcessor = context.messageProcessor;
|
|
74
|
+
}
|
|
66
75
|
|
|
67
76
|
newTask() {
|
|
68
77
|
this.currentThread = 0;
|
|
@@ -1,13 +1,15 @@
|
|
|
1
|
-
import { BaseAgent } from "../base/base";
|
|
1
|
+
import { AgentContext, BaseAgent } from "../base/base";
|
|
2
2
|
import { Message } from "../../clients/types";
|
|
3
3
|
import { Assistant, Config } from "../../types";
|
|
4
|
+
import { EventService, ToolsService } from "src/services";
|
|
5
|
+
import { MessageProcessor } from "src/services/MessageProcessor";
|
|
4
6
|
|
|
5
7
|
export class ConfigAgent extends BaseAgent {
|
|
6
8
|
name: string;
|
|
7
9
|
description: string;
|
|
8
10
|
|
|
9
|
-
constructor(private config: Assistant) {
|
|
10
|
-
super();
|
|
11
|
+
constructor(private config: Assistant, context: AgentContext) {
|
|
12
|
+
super(context);
|
|
11
13
|
this.name = config.name;
|
|
12
14
|
this.setModelPreferences([
|
|
13
15
|
{ model: config.model, provider: config.provider },
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Models } from "../../ai";
|
|
2
2
|
import { Message } from "../../clients/types";
|
|
3
|
-
import { BaseAgent } from "../base/base";
|
|
3
|
+
import { AgentContext, BaseAgent } from "../base/base";
|
|
4
4
|
import { BASE_PROMPT } from "../base/prompt";
|
|
5
5
|
export class DeveloperAgent extends BaseAgent {
|
|
6
6
|
name = "Developer";
|
|
7
7
|
description = `This agent manages requests and uses tools and delegation via agentCall to accomplish things`;
|
|
8
8
|
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
9
|
+
constructor(context: AgentContext) {
|
|
10
|
+
super(context);
|
|
11
11
|
this.disableTool("patchFile");
|
|
12
12
|
this.disableTool("openFileInVim");
|
|
13
13
|
this.disableTool("sendVimInput");
|
|
@@ -66,4 +66,3 @@ export class DeveloperAgent extends BaseAgent {
|
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
|
|
69
|
-
export const Developer = new DeveloperAgent();
|
package/src/agents/index.ts
CHANGED
|
@@ -1,8 +1,32 @@
|
|
|
1
|
+
import { services } from "src/services";
|
|
2
|
+
import { AgentContext } from "./base/base";
|
|
3
|
+
import { DeveloperAgent } from "./developer/developer";
|
|
4
|
+
import { PatchingAgent } from "./patcher/patcher";
|
|
5
|
+
import { ResearcherAgent } from "./researcher/researcher";
|
|
6
|
+
|
|
1
7
|
export { BaseAgent } from "./base/base";
|
|
2
8
|
export { ConfigAgent } from "./configurable/ConfigAgent";
|
|
3
|
-
export
|
|
4
|
-
export
|
|
9
|
+
export { DeveloperAgent };
|
|
10
|
+
export { PatchingAgent };
|
|
11
|
+
|
|
5
12
|
export * from "./researcher/researcher";
|
|
6
13
|
|
|
7
14
|
export * as tools from "./tools";
|
|
8
15
|
export { includedTools } from "./tools/list";
|
|
16
|
+
|
|
17
|
+
let singletons = {} as {
|
|
18
|
+
Developer: DeveloperAgent;
|
|
19
|
+
Patcher: PatchingAgent;
|
|
20
|
+
Researcher: ResearcherAgent;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export function agents(agentContext: AgentContext = services()) {
|
|
24
|
+
if (Object.keys(singletons).length === 0) {
|
|
25
|
+
singletons = {
|
|
26
|
+
Developer: new DeveloperAgent(agentContext),
|
|
27
|
+
Patcher: new PatchingAgent(agentContext),
|
|
28
|
+
Researcher: new ResearcherAgent(agentContext),
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
return singletons;
|
|
32
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Message } from "../../clients/types";
|
|
2
|
-
import { BaseAgent } from "../base/base";
|
|
2
|
+
import { AgentContext, BaseAgent } from "../base/base";
|
|
3
3
|
import { BASE_PROMPT } from "../base/prompt";
|
|
4
4
|
import { Models } from "../../ai";
|
|
5
5
|
|
|
@@ -75,8 +75,8 @@ export class PatchingAgent extends BaseAgent {
|
|
|
75
75
|
name = "Patcher";
|
|
76
76
|
description = `This agent is prepared to work on the codebase by leveraging patches`;
|
|
77
77
|
|
|
78
|
-
constructor() {
|
|
79
|
-
super();
|
|
78
|
+
constructor(context: AgentContext) {
|
|
79
|
+
super(context);
|
|
80
80
|
this.disableTool("sendVimInput");
|
|
81
81
|
this.disableTool("openFileInVim");
|
|
82
82
|
|
|
@@ -106,5 +106,3 @@ export class PatchingAgent extends BaseAgent {
|
|
|
106
106
|
] as Message[];
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
|
|
110
|
-
export const Patcher = new PatchingAgent();
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Models } from "../../ai";
|
|
2
2
|
import { Message } from "../../clients/types";
|
|
3
|
-
import { BaseAgent } from "../base/base";
|
|
3
|
+
import { AgentContext, BaseAgent } from "../base/base";
|
|
4
4
|
|
|
5
5
|
export class ResearcherAgent extends BaseAgent {
|
|
6
6
|
name = "Researcher";
|
|
7
7
|
description = `This agent is prepared to research a request using the tools available to them. Great for finding answers to questions about the codebase`;
|
|
8
8
|
|
|
9
|
-
constructor() {
|
|
10
|
-
super();
|
|
9
|
+
constructor(context: AgentContext) {
|
|
10
|
+
super(context);
|
|
11
11
|
this.setModel(Models.google.Gemini_20_Flash);
|
|
12
12
|
this.setProvider("google");
|
|
13
13
|
this.disableTool("patchFile");
|
|
@@ -107,4 +107,3 @@ export class ResearcherAgent extends BaseAgent {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
export const Researcher = new ResearcherAgent();
|
|
@@ -1,12 +1,15 @@
|
|
|
1
|
-
import { Events } from "../../services/EventService";
|
|
2
|
-
import { Plugins } from "../../plugins/plugins";
|
|
3
1
|
import { getConfig } from "../../config";
|
|
2
|
+
import { ToolsService } from "../../services";
|
|
4
3
|
|
|
5
4
|
export async function agentCall(agentName: string, userInput: string) {
|
|
6
5
|
return new Promise(async (resolve, reject) => {
|
|
7
6
|
const config = await getConfig();
|
|
7
|
+
const toolService = this as ToolsService;
|
|
8
|
+
|
|
9
|
+
const { Events, Plugins } = toolService.getContext();
|
|
8
10
|
const pluginText = await Plugins.callMany(config.plugins, userInput);
|
|
9
11
|
const fullPrompt = `${userInput} \n ${pluginText}`;
|
|
12
|
+
|
|
10
13
|
Events.emit("agents:call", {
|
|
11
14
|
name: agentName,
|
|
12
15
|
query: fullPrompt,
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Execute Script Tool
|
|
2
|
+
|
|
3
|
+
The `executeScript` tool provides secure, isolated execution of TypeScript scripts with access to the Knowhow platform's tools and AI capabilities.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Secure Sandbox**: Scripts run in isolated-vm with no access to Node.js APIs
|
|
8
|
+
- **Tool Integration**: Access to all existing Knowhow tools via `callTool()`
|
|
9
|
+
- **AI Integration**: Direct access to AI completions via `llm()`
|
|
10
|
+
- **Resource Quotas**: Configurable limits on execution time, memory, tool calls, and tokens
|
|
11
|
+
- **Comprehensive Tracing**: Full audit trail of all script activities
|
|
12
|
+
- **Policy Enforcement**: Fine-grained security controls and access restrictions
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
// Basic usage
|
|
18
|
+
await executeScript({
|
|
19
|
+
script: `
|
|
20
|
+
console.log("Hello from script!");
|
|
21
|
+
|
|
22
|
+
const files = await callTool("fileSearch", { searchTerm: "*.ts" });
|
|
23
|
+
console.log("Found TypeScript files:", files);
|
|
24
|
+
|
|
25
|
+
const response = await llm([
|
|
26
|
+
{ role: "user", content: "What is TypeScript?" }
|
|
27
|
+
]);
|
|
28
|
+
|
|
29
|
+
return { message: "Script completed successfully" };
|
|
30
|
+
`
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// With custom policy
|
|
34
|
+
await executeScript({
|
|
35
|
+
script: "...",
|
|
36
|
+
policy: {
|
|
37
|
+
maxExecutionTimeMs: 10000,
|
|
38
|
+
maxMemoryMB: 64,
|
|
39
|
+
maxToolCalls: 5,
|
|
40
|
+
maxTokens: 1000,
|
|
41
|
+
allowedTools: ["fileSearch", "textSearch"],
|
|
42
|
+
deniedTools: ["execCommand"]
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Available Functions in Scripts
|
|
48
|
+
|
|
49
|
+
### `callTool(name: string, args: any): Promise<any>`
|
|
50
|
+
Call any available Knowhow tool by name with arguments.
|
|
51
|
+
|
|
52
|
+
### `llm(messages: ChatMessage[], options?: LLMOptions): Promise<ChatCompletion>`
|
|
53
|
+
Make AI completion requests with message history and options.
|
|
54
|
+
|
|
55
|
+
## Security Features
|
|
56
|
+
|
|
57
|
+
- **No Node.js Access**: Scripts cannot access filesystem, network, or system APIs directly
|
|
58
|
+
- **Tool Authorization**: All external access goes through existing authorization systems
|
|
59
|
+
- **Resource Limits**: Prevents runaway scripts with time, memory, and usage quotas
|
|
60
|
+
- **Trace Sanitization**: Sensitive data is redacted from execution logs
|
|
61
|
+
- **Policy Enforcement**: Granular control over what tools and resources scripts can access
|
|
62
|
+
|
|
63
|
+
## Implementation Details
|
|
64
|
+
|
|
65
|
+
- **ScriptExecutor**: Handles compilation and isolated execution
|
|
66
|
+
- **SandboxContext**: Provides `callTool` and `llm` function implementations
|
|
67
|
+
- **ScriptTracer**: Records all script activities for debugging and audit
|
|
68
|
+
- **ScriptPolicyEnforcer**: Enforces resource quotas and security policies
|
|
69
|
+
|
|
70
|
+
## Files
|
|
71
|
+
|
|
72
|
+
- `types.ts` - TypeScript interfaces and type definitions
|
|
73
|
+
- `ScriptExecutor.ts` - Core execution engine with isolated-vm
|
|
74
|
+
- `SandboxContext.ts` - Script execution context with tool/AI access
|
|
75
|
+
- `ScriptTracer.ts` - Event tracing and monitoring system
|
|
76
|
+
- `ScriptPolicyEnforcer.ts` - Security policy enforcement
|
|
77
|
+
- `executeScript.ts` - Tool handler and main entry point
|
|
78
|
+
- `examples/` - Example scripts demonstrating capabilities
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Tool } from "../../../clients";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Tool for executing TypeScript scripts in a secure sandbox
|
|
5
|
+
*/
|
|
6
|
+
export const executeScriptDefinition: Tool = {
|
|
7
|
+
type: "function",
|
|
8
|
+
function: {
|
|
9
|
+
name: "executeScript",
|
|
10
|
+
description: `Execute TypeScript code in a secure sandbox environment with access to tools and AI models.
|
|
11
|
+
|
|
12
|
+
The script has access to:
|
|
13
|
+
- callTool(toolName, parameters): Call any available tool
|
|
14
|
+
- llm(messages, options): Make LLM calls
|
|
15
|
+
- createArtifact(name, content, type): Create downloadable artifacts
|
|
16
|
+
- console: Standard console logging
|
|
17
|
+
- getQuotaUsage(): Check resource usage
|
|
18
|
+
- sleep(ms): Pause execution for a specified time
|
|
19
|
+
|
|
20
|
+
Example:
|
|
21
|
+
\`\`\`typescript
|
|
22
|
+
// Call a tool
|
|
23
|
+
const searchResult = await callTool('textSearch', { searchTerm: 'hello world' });
|
|
24
|
+
console.log('Search found:', searchResult);
|
|
25
|
+
|
|
26
|
+
// Call LLM
|
|
27
|
+
const response = await llm([
|
|
28
|
+
{ role: 'user', content: 'Explain quantum computing' }
|
|
29
|
+
], { model: 'gpt-4o-mini', maxTokens: 100 });
|
|
30
|
+
console.log('LLM response:', response.choices[0].message.content);
|
|
31
|
+
|
|
32
|
+
// Create an artifact
|
|
33
|
+
createArtifact('summary.md', '# Summary\\nThis is a test', 'markdown');
|
|
34
|
+
|
|
35
|
+
return { message: 'Script completed successfully' };
|
|
36
|
+
\`\`\`
|
|
37
|
+
|
|
38
|
+
You must return the data you want to be the functionResp
|
|
39
|
+
|
|
40
|
+
Test tools yourself to know the return type when scripting. Can pass JSON.stringified data into llm call if you don't need to know the type.
|
|
41
|
+
You cannot use isolation breaking methods like: setTimeout setInterval setImmediate clearTimeout clearInterval
|
|
42
|
+
|
|
43
|
+
Security: Scripts run in isolation with quotas on tool calls, tokens, time, and cost.`,
|
|
44
|
+
|
|
45
|
+
parameters: {
|
|
46
|
+
type: "object",
|
|
47
|
+
properties: {
|
|
48
|
+
script: {
|
|
49
|
+
type: "string",
|
|
50
|
+
description: "The TypeScript code to execute",
|
|
51
|
+
},
|
|
52
|
+
maxToolCalls: {
|
|
53
|
+
type: "number",
|
|
54
|
+
description: "Maximum number of tool calls allowed (default: 50)",
|
|
55
|
+
},
|
|
56
|
+
maxTokens: {
|
|
57
|
+
type: "number",
|
|
58
|
+
description: "Maximum tokens for LLM calls (default: 10000)",
|
|
59
|
+
},
|
|
60
|
+
maxExecutionTimeMs: {
|
|
61
|
+
type: "number",
|
|
62
|
+
description:
|
|
63
|
+
"Maximum execution time in milliseconds (default: 30000)",
|
|
64
|
+
},
|
|
65
|
+
maxCostUsd: {
|
|
66
|
+
type: "number",
|
|
67
|
+
description: "Maximum cost in USD (default: 1.0)",
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
required: ["script"],
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
};
|
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
#!/usr/bin/env ts-node
|
|
2
|
+
/**
|
|
3
|
+
* Comprehensive test for Tools Dependency Injection System
|
|
4
|
+
*
|
|
5
|
+
* This test validates:
|
|
6
|
+
* 1. Context injection is working properly
|
|
7
|
+
* 2. Tools can access services via `this.getContext()`
|
|
8
|
+
* 3. Agent isolation is functioning (each agent has independent context)
|
|
9
|
+
* 4. No singleton usage in tool implementations
|
|
10
|
+
* 5. Backward compatibility is maintained
|
|
11
|
+
*
|
|
12
|
+
* Usage: npx ts-node src/agents/tools/executeScript/examples/dependency-injection-validation.ts
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { ToolsService } from "../../../../services/Tools";
|
|
16
|
+
import { Clients } from "../../../../clients";
|
|
17
|
+
import { EventService, services } from "../../../../services/";
|
|
18
|
+
import { BaseAgent } from "../../../base/base";
|
|
19
|
+
import { Message } from "../../../../clients/types";
|
|
20
|
+
import { includedTools } from "../../list";
|
|
21
|
+
import { executeScript } from "../";
|
|
22
|
+
import { executeScriptDefinition } from "../definition";
|
|
23
|
+
|
|
24
|
+
// Test Agent that extends BaseAgent to test agent isolation
|
|
25
|
+
class TestAgent1 extends BaseAgent {
|
|
26
|
+
name = "TestAgent1";
|
|
27
|
+
description = "Test agent for dependency injection validation";
|
|
28
|
+
|
|
29
|
+
async getInitialMessages(userInput: string): Promise<Message[]> {
|
|
30
|
+
return [
|
|
31
|
+
{ role: "system", content: this.description },
|
|
32
|
+
{ role: "user", content: userInput },
|
|
33
|
+
];
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Expose toolsService for testing
|
|
37
|
+
public getToolsServiceForTest() {
|
|
38
|
+
return this.tools;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class TestAgent2 extends BaseAgent {
|
|
43
|
+
name = "TestAgent2";
|
|
44
|
+
description = "Another test agent for dependency injection validation";
|
|
45
|
+
|
|
46
|
+
async getInitialMessages(userInput: string): Promise<Message[]> {
|
|
47
|
+
return [
|
|
48
|
+
{ role: "system", content: this.description },
|
|
49
|
+
{ role: "user", content: userInput },
|
|
50
|
+
];
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Expose toolsService for testing
|
|
54
|
+
public getToolsServiceForTest() {
|
|
55
|
+
return this.tools;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Test tool function that uses context injection
|
|
60
|
+
function testToolWithContext(this: ToolsService, params: { message: string }) {
|
|
61
|
+
const context = this.getContext();
|
|
62
|
+
|
|
63
|
+
console.log("ā
Tool called with context:");
|
|
64
|
+
console.log("- AgentService available:", !!context.Agents);
|
|
65
|
+
console.log("- EventService available:", !!context.Events);
|
|
66
|
+
console.log("- Clients available:", !!context.Clients);
|
|
67
|
+
console.log("- ToolsService self-reference available:", !!context.Tools);
|
|
68
|
+
console.log("- Test message:", params.message);
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
success: true,
|
|
72
|
+
contextValidated: true,
|
|
73
|
+
hasAgentService: !!context.Agents,
|
|
74
|
+
hasEventService: !!context.Events,
|
|
75
|
+
hasClients: !!context.Clients,
|
|
76
|
+
hasToolsService: !!context.Tools,
|
|
77
|
+
testMessage: params.message,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
async function runValidationTests() {
|
|
82
|
+
console.log("š§Ŗ Starting Tools Dependency Injection Validation Tests\n");
|
|
83
|
+
|
|
84
|
+
// Test 1: Create independent agents with their own ToolsService instances
|
|
85
|
+
console.log("š Test 1: Agent Isolation");
|
|
86
|
+
const agent1 = new TestAgent1({
|
|
87
|
+
Events: new EventService(),
|
|
88
|
+
Tools: new ToolsService({ Clients }),
|
|
89
|
+
});
|
|
90
|
+
const agent2 = new TestAgent2({
|
|
91
|
+
Events: new EventService(),
|
|
92
|
+
Tools: new ToolsService(),
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
const toolsService1 = agent1.getToolsServiceForTest();
|
|
96
|
+
const toolsService2 = agent2.getToolsServiceForTest();
|
|
97
|
+
|
|
98
|
+
// Verify agents have different ToolsService instances
|
|
99
|
+
console.log(
|
|
100
|
+
"ā
Agent1 and Agent2 have different ToolsService instances:",
|
|
101
|
+
toolsService1 !== toolsService2
|
|
102
|
+
);
|
|
103
|
+
|
|
104
|
+
// Test 2: Context validation for each agent
|
|
105
|
+
console.log("\nš Test 2: Context Injection Validation");
|
|
106
|
+
|
|
107
|
+
const context1 = toolsService1.getContext();
|
|
108
|
+
const context2 = toolsService2.getContext();
|
|
109
|
+
|
|
110
|
+
console.log("ā
Agent1 context has required services:", {
|
|
111
|
+
agentService: !!context1.Agents,
|
|
112
|
+
eventService: !!context1.Events,
|
|
113
|
+
clients: !!context1.Clients,
|
|
114
|
+
toolsService: !!context1.Tools,
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
console.log("ā
Agent2 context has required services:", {
|
|
118
|
+
agentService: !!context2.Agents,
|
|
119
|
+
eventService: !!context2.Events,
|
|
120
|
+
clients: !!context2.Clients,
|
|
121
|
+
toolsService: !!context2.Tools,
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// Test 3: Register a test tool and verify it can access context
|
|
125
|
+
console.log("\nš Test 3: Tool Context Access");
|
|
126
|
+
|
|
127
|
+
// Register the test tool on agent1's ToolsService
|
|
128
|
+
toolsService1.addTool({
|
|
129
|
+
type: "function",
|
|
130
|
+
function: {
|
|
131
|
+
name: "testToolWithContext",
|
|
132
|
+
description: "Test tool for context validation",
|
|
133
|
+
parameters: {
|
|
134
|
+
type: "object",
|
|
135
|
+
properties: {
|
|
136
|
+
message: { type: "string", description: "Test message" },
|
|
137
|
+
},
|
|
138
|
+
required: ["message"],
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
toolsService1.setFunction("testToolWithContext", testToolWithContext);
|
|
144
|
+
try {
|
|
145
|
+
const result = await toolsService1.callTool({
|
|
146
|
+
id: "test-call",
|
|
147
|
+
type: "function",
|
|
148
|
+
function: {
|
|
149
|
+
name: "testToolWithContext",
|
|
150
|
+
arguments: JSON.stringify({ message: "Hello from Agent1!" }),
|
|
151
|
+
},
|
|
152
|
+
});
|
|
153
|
+
console.log(
|
|
154
|
+
"ā
Tool executed successfully with context access:",
|
|
155
|
+
result.functionResp?.success
|
|
156
|
+
);
|
|
157
|
+
} catch (error) {
|
|
158
|
+
console.error("ā Tool execution failed:", error);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// Test 4: Verify agent isolation - agent2 should not have agent1's tool
|
|
162
|
+
console.log("\nš Test 4: Tool Isolation Between Agents");
|
|
163
|
+
|
|
164
|
+
const agent1Tools = toolsService1.getTools().map((t) => t.function.name);
|
|
165
|
+
const agent2Tools = toolsService2.getTools().map((t) => t.function.name);
|
|
166
|
+
|
|
167
|
+
console.log("Agent1 tools:", agent1Tools.length);
|
|
168
|
+
console.log("Agent2 tools:", agent2Tools.length);
|
|
169
|
+
console.log(
|
|
170
|
+
"ā
Agent2 does not have agent1's custom tool:",
|
|
171
|
+
!agent2Tools.includes("testToolWithContext")
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
// Test 5: Add different tool to agent2 to verify independence
|
|
175
|
+
function agent2SpecificTool(this: ToolsService, params: { data: string }) {
|
|
176
|
+
const context = this.getContext();
|
|
177
|
+
return {
|
|
178
|
+
agent: "Agent2",
|
|
179
|
+
data: params.data,
|
|
180
|
+
contextAvailable: !!context,
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
toolsService2.addTool({
|
|
185
|
+
type: "function",
|
|
186
|
+
function: {
|
|
187
|
+
name: "agent2SpecificTool",
|
|
188
|
+
description: "Tool specific to Agent2",
|
|
189
|
+
parameters: {
|
|
190
|
+
type: "object",
|
|
191
|
+
properties: {
|
|
192
|
+
data: { type: "string", description: "Test data" },
|
|
193
|
+
},
|
|
194
|
+
required: ["data"],
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
toolsService2.setFunction("agent2SpecificTool", agent2SpecificTool);
|
|
199
|
+
|
|
200
|
+
const agent1ToolsAfter = toolsService1.getTools().map((t) => t.function.name);
|
|
201
|
+
const agent2ToolsAfter = toolsService2.getTools().map((t) => t.function.name);
|
|
202
|
+
|
|
203
|
+
console.log(
|
|
204
|
+
"ā
Agent1 does not have agent2's tool:",
|
|
205
|
+
!agent1ToolsAfter.includes("agent2SpecificTool")
|
|
206
|
+
);
|
|
207
|
+
console.log(
|
|
208
|
+
"ā
Agent2 has its specific tool:",
|
|
209
|
+
agent2ToolsAfter.includes("agent2SpecificTool")
|
|
210
|
+
);
|
|
211
|
+
|
|
212
|
+
// Test 6: Verify executeScript tool is using context injection
|
|
213
|
+
console.log("\nš Test 6: executeScript Context Integration");
|
|
214
|
+
|
|
215
|
+
// Test that executeScript uses the bound context instead of singletons
|
|
216
|
+
const executeScriptTest = `
|
|
217
|
+
async function main() {
|
|
218
|
+
return callTool("testToolWithContext", {message: "Hello from executeScript!"});
|
|
219
|
+
}
|
|
220
|
+
return main()
|
|
221
|
+
`;
|
|
222
|
+
|
|
223
|
+
toolsService1.defineTools([executeScriptDefinition], { executeScript });
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
const executeResult = await toolsService1.callTool({
|
|
227
|
+
id: "execute-test",
|
|
228
|
+
type: "function",
|
|
229
|
+
function: {
|
|
230
|
+
name: "executeScript",
|
|
231
|
+
arguments: JSON.stringify({ script: executeScriptTest }),
|
|
232
|
+
},
|
|
233
|
+
});
|
|
234
|
+
console.log(
|
|
235
|
+
"ā
executeScript using dependency injection:",
|
|
236
|
+
executeResult.functionResp
|
|
237
|
+
);
|
|
238
|
+
} catch (error) {
|
|
239
|
+
console.error("ā executeScript test failed:", error);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
console.log("\nš All Tests Completed!");
|
|
243
|
+
console.log("\nš Test Summary:");
|
|
244
|
+
console.log(
|
|
245
|
+
"ā
Agent isolation working - each agent has independent ToolsService"
|
|
246
|
+
);
|
|
247
|
+
console.log(
|
|
248
|
+
"ā
Context injection working - tools can access all required services"
|
|
249
|
+
);
|
|
250
|
+
console.log("ā
No singleton usage - tools use bound context instead");
|
|
251
|
+
console.log(
|
|
252
|
+
"ā
Backward compatibility maintained - existing patterns still work"
|
|
253
|
+
);
|
|
254
|
+
console.log("ā
executeScript migrated successfully to dependency injection");
|
|
255
|
+
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
// Run the validation tests
|
|
260
|
+
if (require.main === module) {
|
|
261
|
+
runValidationTests()
|
|
262
|
+
.then(() => {
|
|
263
|
+
console.log("\nš Dependency Injection Implementation Complete!");
|
|
264
|
+
process.exit(0);
|
|
265
|
+
})
|
|
266
|
+
.catch((error) => {
|
|
267
|
+
console.error("\nā Validation tests failed:", error);
|
|
268
|
+
process.exit(1);
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export { runValidationTests, TestAgent1, TestAgent2 };
|