@townco/agent 0.1.52 ā 0.1.54
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/acp-server/adapter.d.ts +18 -0
- package/dist/acp-server/adapter.js +258 -19
- package/dist/acp-server/http.js +39 -1
- package/dist/acp-server/session-storage.d.ts +18 -1
- package/dist/acp-server/session-storage.js +25 -0
- package/dist/definition/index.d.ts +2 -2
- package/dist/definition/index.js +1 -0
- package/dist/runner/agent-runner.d.ts +11 -2
- package/dist/runner/langchain/index.d.ts +0 -1
- package/dist/runner/langchain/index.js +265 -64
- package/dist/runner/langchain/tools/generate_image.d.ts +28 -0
- package/dist/runner/langchain/tools/generate_image.js +135 -0
- package/dist/runner/langchain/tools/subagent.d.ts +6 -1
- package/dist/runner/langchain/tools/subagent.js +12 -2
- package/dist/runner/tools.d.ts +19 -2
- package/dist/runner/tools.js +9 -0
- package/dist/telemetry/index.js +7 -1
- package/dist/templates/index.d.ts +3 -0
- package/dist/templates/index.js +26 -4
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/context-size-calculator.d.ts +9 -4
- package/dist/utils/context-size-calculator.js +23 -6
- package/dist/utils/tool-overhead-calculator.d.ts +30 -0
- package/dist/utils/tool-overhead-calculator.js +54 -0
- package/package.json +7 -6
- package/templates/index.ts +36 -5
- package/dist/check-jaeger.d.ts +0 -5
- package/dist/check-jaeger.js +0 -82
- package/dist/run-subagents.d.ts +0 -9
- package/dist/run-subagents.js +0 -110
- package/dist/runner/langchain/custom-stream-types.d.ts +0 -36
- package/dist/runner/langchain/custom-stream-types.js +0 -23
- package/dist/runner/langchain/tools/bash.d.ts +0 -14
- package/dist/runner/langchain/tools/bash.js +0 -135
- package/dist/scaffold/link-local.d.ts +0 -1
- package/dist/scaffold/link-local.js +0 -54
- package/dist/test-telemetry.d.ts +0 -5
- package/dist/test-telemetry.js +0 -88
- package/dist/utils/logger.d.ts +0 -39
- package/dist/utils/logger.js +0 -175
|
@@ -5,6 +5,8 @@
|
|
|
5
5
|
import type { SessionMessage } from "../acp-server/session-storage.js";
|
|
6
6
|
export interface ContextSize {
|
|
7
7
|
systemPromptTokens: number;
|
|
8
|
+
toolOverheadTokens?: number;
|
|
9
|
+
mcpOverheadTokens?: number;
|
|
8
10
|
userMessagesTokens: number;
|
|
9
11
|
assistantMessagesTokens: number;
|
|
10
12
|
toolInputTokens: number;
|
|
@@ -22,8 +24,11 @@ export interface ContextSize {
|
|
|
22
24
|
* all messages, and all tool results
|
|
23
25
|
* - We pass this as `llmReportedTokens` for comparison with our estimate
|
|
24
26
|
* - This helps us validate the accuracy of our tokenizer estimates
|
|
25
|
-
*
|
|
26
|
-
*
|
|
27
|
+
*
|
|
28
|
+
* @param messages - Resolved messages from context entry
|
|
29
|
+
* @param systemPrompt - Base system prompt (without TODO instructions)
|
|
30
|
+
* @param llmReportedTokens - From API usage_metadata.input_tokens
|
|
31
|
+
* @param toolOverheadTokens - Pre-calculated tool overhead (built-in/custom/filesystem tool definitions + TODO instructions)
|
|
32
|
+
* @param mcpOverheadTokens - Pre-calculated MCP tool overhead (MCP tool definitions)
|
|
27
33
|
*/
|
|
28
|
-
export declare function calculateContextSize(messages: SessionMessage[],
|
|
29
|
-
systemPrompt?: string, llmReportedTokens?: number): ContextSize;
|
|
34
|
+
export declare function calculateContextSize(messages: SessionMessage[], systemPrompt?: string, llmReportedTokens?: number, toolOverheadTokens?: number, mcpOverheadTokens?: number): ContextSize;
|
|
@@ -37,12 +37,17 @@ function countContentBlock(block) {
|
|
|
37
37
|
* all messages, and all tool results
|
|
38
38
|
* - We pass this as `llmReportedTokens` for comparison with our estimate
|
|
39
39
|
* - This helps us validate the accuracy of our tokenizer estimates
|
|
40
|
-
*
|
|
41
|
-
*
|
|
40
|
+
*
|
|
41
|
+
* @param messages - Resolved messages from context entry
|
|
42
|
+
* @param systemPrompt - Base system prompt (without TODO instructions)
|
|
43
|
+
* @param llmReportedTokens - From API usage_metadata.input_tokens
|
|
44
|
+
* @param toolOverheadTokens - Pre-calculated tool overhead (built-in/custom/filesystem tool definitions + TODO instructions)
|
|
45
|
+
* @param mcpOverheadTokens - Pre-calculated MCP tool overhead (MCP tool definitions)
|
|
42
46
|
*/
|
|
43
|
-
export function calculateContextSize(messages,
|
|
44
|
-
systemPrompt, llmReportedTokens) {
|
|
47
|
+
export function calculateContextSize(messages, systemPrompt, llmReportedTokens, toolOverheadTokens, mcpOverheadTokens) {
|
|
45
48
|
const systemPromptTokens = systemPrompt ? countTokens(systemPrompt) : 0;
|
|
49
|
+
const toolOverheadTokensEstimate = toolOverheadTokens ?? 0;
|
|
50
|
+
const mcpOverheadTokensEstimate = mcpOverheadTokens ?? 0;
|
|
46
51
|
let userMessagesTokens = 0;
|
|
47
52
|
let assistantMessagesTokens = 0;
|
|
48
53
|
let toolInputTokens = 0;
|
|
@@ -62,17 +67,29 @@ systemPrompt, llmReportedTokens) {
|
|
|
62
67
|
}
|
|
63
68
|
}
|
|
64
69
|
}
|
|
65
|
-
|
|
70
|
+
const result = {
|
|
66
71
|
systemPromptTokens,
|
|
67
72
|
userMessagesTokens,
|
|
68
73
|
assistantMessagesTokens,
|
|
69
74
|
toolInputTokens,
|
|
70
75
|
toolResultsTokens,
|
|
71
76
|
totalEstimated: systemPromptTokens +
|
|
77
|
+
toolOverheadTokensEstimate +
|
|
78
|
+
mcpOverheadTokensEstimate +
|
|
72
79
|
userMessagesTokens +
|
|
73
80
|
assistantMessagesTokens +
|
|
74
81
|
toolInputTokens +
|
|
75
82
|
toolResultsTokens,
|
|
76
|
-
llmReportedInputTokens: llmReportedTokens,
|
|
77
83
|
};
|
|
84
|
+
// Only include optional fields if they have values
|
|
85
|
+
if (toolOverheadTokensEstimate > 0) {
|
|
86
|
+
result.toolOverheadTokens = toolOverheadTokensEstimate;
|
|
87
|
+
}
|
|
88
|
+
if (mcpOverheadTokensEstimate > 0) {
|
|
89
|
+
result.mcpOverheadTokens = mcpOverheadTokensEstimate;
|
|
90
|
+
}
|
|
91
|
+
if (llmReportedTokens !== undefined) {
|
|
92
|
+
result.llmReportedInputTokens = llmReportedTokens;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
78
95
|
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool overhead calculation utilities
|
|
3
|
+
* Estimates token overhead from tool definitions sent to LLM APIs
|
|
4
|
+
*/
|
|
5
|
+
export interface ToolMetadata {
|
|
6
|
+
name: string;
|
|
7
|
+
description: string;
|
|
8
|
+
schema: Record<string, unknown>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Estimate tokens for a single tool definition
|
|
12
|
+
* LLMs receive tools as JSON with name, description, and parameters schema
|
|
13
|
+
*
|
|
14
|
+
* Note: Different LLM providers (Anthropic, OpenAI, Gemini) serialize tools
|
|
15
|
+
* slightly differently. This is a rough approximation based on the general format.
|
|
16
|
+
*/
|
|
17
|
+
export declare function estimateToolDefinitionTokens(tool: ToolMetadata): number;
|
|
18
|
+
/**
|
|
19
|
+
* Estimate total tokens for all tool definitions
|
|
20
|
+
*/
|
|
21
|
+
export declare function estimateAllToolsOverhead(tools: ToolMetadata[]): number;
|
|
22
|
+
/**
|
|
23
|
+
* Extract metadata from LangChain tools
|
|
24
|
+
* LangChain tools have .name, .description, and .schema properties
|
|
25
|
+
*/
|
|
26
|
+
export declare function extractToolMetadata(langchainTool: {
|
|
27
|
+
name: string;
|
|
28
|
+
description?: string;
|
|
29
|
+
schema?: unknown;
|
|
30
|
+
}): ToolMetadata;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tool overhead calculation utilities
|
|
3
|
+
* Estimates token overhead from tool definitions sent to LLM APIs
|
|
4
|
+
*/
|
|
5
|
+
import { countTokens } from "./token-counter.js";
|
|
6
|
+
/**
|
|
7
|
+
* Estimate tokens for a single tool definition
|
|
8
|
+
* LLMs receive tools as JSON with name, description, and parameters schema
|
|
9
|
+
*
|
|
10
|
+
* Note: Different LLM providers (Anthropic, OpenAI, Gemini) serialize tools
|
|
11
|
+
* slightly differently. This is a rough approximation based on the general format.
|
|
12
|
+
*/
|
|
13
|
+
export function estimateToolDefinitionTokens(tool) {
|
|
14
|
+
// Rough serialization of how tools are sent to APIs:
|
|
15
|
+
// {"name": "tool_name", "description": "...", "input_schema": {...}}
|
|
16
|
+
const approximateJson = JSON.stringify({
|
|
17
|
+
name: tool.name,
|
|
18
|
+
description: tool.description,
|
|
19
|
+
input_schema: tool.schema,
|
|
20
|
+
});
|
|
21
|
+
return countTokens(approximateJson);
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Estimate total tokens for all tool definitions
|
|
25
|
+
*/
|
|
26
|
+
export function estimateAllToolsOverhead(tools) {
|
|
27
|
+
return tools.reduce((total, tool) => total + estimateToolDefinitionTokens(tool), 0);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Extract metadata from LangChain tools
|
|
31
|
+
* LangChain tools have .name, .description, and .schema properties
|
|
32
|
+
*/
|
|
33
|
+
export function extractToolMetadata(langchainTool) {
|
|
34
|
+
// LangChain tools may have Zod schemas - serialize them to JSON
|
|
35
|
+
let schemaObject = {};
|
|
36
|
+
if (langchainTool.schema) {
|
|
37
|
+
// If it's a Zod schema, it might have a _def property
|
|
38
|
+
if (typeof langchainTool.schema === "object" &&
|
|
39
|
+
langchainTool.schema !== null) {
|
|
40
|
+
// Try to serialize to JSON, fallback to empty object
|
|
41
|
+
try {
|
|
42
|
+
schemaObject = JSON.parse(JSON.stringify(langchainTool.schema));
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
schemaObject = {};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
name: langchainTool.name,
|
|
51
|
+
description: langchainTool.description || "",
|
|
52
|
+
schema: schemaObject,
|
|
53
|
+
};
|
|
54
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@townco/agent",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.54",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"module": "index.ts",
|
|
6
6
|
"files": [
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"@agentclientprotocol/sdk": "^0.5.1",
|
|
56
56
|
"@anthropic-ai/sandbox-runtime": "^0.0.2",
|
|
57
57
|
"@anthropic-ai/sdk": "^0.70.0",
|
|
58
|
+
"@google/genai": "^0.14.1",
|
|
58
59
|
"@anthropic-ai/tokenizer": "^0.0.4",
|
|
59
60
|
"@electric-sql/pglite": "^0.2.15",
|
|
60
61
|
"@langchain/anthropic": "1.0.1",
|
|
@@ -73,11 +74,11 @@
|
|
|
73
74
|
"@opentelemetry/sdk-trace-base": "^1.28.0",
|
|
74
75
|
"@opentelemetry/sdk-trace-node": "^1.28.0",
|
|
75
76
|
"@opentelemetry/semantic-conventions": "^1.28.0",
|
|
76
|
-
"@townco/core": "0.0.
|
|
77
|
-
"@townco/gui-template": "0.1.
|
|
78
|
-
"@townco/tsconfig": "0.1.
|
|
79
|
-
"@townco/tui-template": "0.1.
|
|
80
|
-
"@townco/ui": "0.1.
|
|
77
|
+
"@townco/core": "0.0.27",
|
|
78
|
+
"@townco/gui-template": "0.1.46",
|
|
79
|
+
"@townco/tsconfig": "0.1.46",
|
|
80
|
+
"@townco/tui-template": "0.1.46",
|
|
81
|
+
"@townco/ui": "0.1.49",
|
|
81
82
|
"exa-js": "^2.0.0",
|
|
82
83
|
"hono": "^4.10.4",
|
|
83
84
|
"langchain": "^1.0.3",
|
package/templates/index.ts
CHANGED
|
@@ -4,6 +4,9 @@ import type { AgentDefinition } from "../definition";
|
|
|
4
4
|
export interface TemplateVars {
|
|
5
5
|
name: string;
|
|
6
6
|
model: string;
|
|
7
|
+
displayName?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
suggestedPrompts?: string[];
|
|
7
10
|
tools: Array<
|
|
8
11
|
| string
|
|
9
12
|
| { type: "custom"; modulePath: string }
|
|
@@ -45,7 +48,7 @@ export function getTemplateVars(
|
|
|
45
48
|
definition: AgentDefinition,
|
|
46
49
|
): TemplateVars {
|
|
47
50
|
const tools = definition.tools ?? [];
|
|
48
|
-
|
|
51
|
+
const result: TemplateVars = {
|
|
49
52
|
name,
|
|
50
53
|
model: definition.model,
|
|
51
54
|
tools,
|
|
@@ -55,6 +58,18 @@ export function getTemplateVars(
|
|
|
55
58
|
),
|
|
56
59
|
hooks: definition.hooks as TemplateVars["hooks"],
|
|
57
60
|
};
|
|
61
|
+
|
|
62
|
+
if (definition.displayName) {
|
|
63
|
+
result.displayName = definition.displayName;
|
|
64
|
+
}
|
|
65
|
+
if (definition.description) {
|
|
66
|
+
result.description = definition.description;
|
|
67
|
+
}
|
|
68
|
+
if (definition.suggestedPrompts) {
|
|
69
|
+
result.suggestedPrompts = definition.suggestedPrompts;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return result;
|
|
58
73
|
}
|
|
59
74
|
|
|
60
75
|
export function generatePackageJson(vars: TemplateVars): string {
|
|
@@ -94,12 +109,28 @@ export function generatePackageJson(vars: TemplateVars): string {
|
|
|
94
109
|
}
|
|
95
110
|
|
|
96
111
|
export async function generateIndexTs(vars: TemplateVars): Promise<string> {
|
|
97
|
-
|
|
112
|
+
// Build agent definition with fields in a logical order
|
|
113
|
+
const agentDef: Record<string, unknown> = {
|
|
98
114
|
model: vars.model,
|
|
99
|
-
systemPrompt: vars.systemPrompt,
|
|
100
|
-
tools: vars.tools,
|
|
101
|
-
hooks: vars.hooks,
|
|
102
115
|
};
|
|
116
|
+
|
|
117
|
+
if (vars.displayName) {
|
|
118
|
+
agentDef.displayName = vars.displayName;
|
|
119
|
+
}
|
|
120
|
+
if (vars.description) {
|
|
121
|
+
agentDef.description = vars.description;
|
|
122
|
+
}
|
|
123
|
+
if (vars.suggestedPrompts) {
|
|
124
|
+
agentDef.suggestedPrompts = vars.suggestedPrompts;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
agentDef.systemPrompt = vars.systemPrompt;
|
|
128
|
+
agentDef.tools = vars.tools;
|
|
129
|
+
|
|
130
|
+
if (vars.hooks) {
|
|
131
|
+
agentDef.hooks = vars.hooks;
|
|
132
|
+
}
|
|
133
|
+
|
|
103
134
|
return prettier.format(
|
|
104
135
|
`import { makeHttpTransport, makeStdioTransport } from "@townco/agent/acp-server";
|
|
105
136
|
import type { AgentDefinition } from "@townco/agent/definition";
|
package/dist/check-jaeger.d.ts
DELETED
package/dist/check-jaeger.js
DELETED
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Quick script to verify Jaeger connectivity
|
|
3
|
-
* Run with: bun check-jaeger.ts
|
|
4
|
-
*/
|
|
5
|
-
export {}; // Make this a module for top-level await
|
|
6
|
-
console.log("š Checking Jaeger connectivity...\n");
|
|
7
|
-
// Check if Jaeger collector is accepting OTLP on gRPC port
|
|
8
|
-
const checkPort = async (port, protocol) => {
|
|
9
|
-
try {
|
|
10
|
-
const net = await import("node:net");
|
|
11
|
-
const socket = new net.Socket();
|
|
12
|
-
return new Promise((resolve) => {
|
|
13
|
-
socket.setTimeout(2000);
|
|
14
|
-
socket.on("connect", () => {
|
|
15
|
-
console.log(`ā
${protocol} port ${port} is accepting connections`);
|
|
16
|
-
socket.destroy();
|
|
17
|
-
resolve(true);
|
|
18
|
-
});
|
|
19
|
-
socket.on("timeout", () => {
|
|
20
|
-
console.log(`ā ${protocol} port ${port} timed out`);
|
|
21
|
-
socket.destroy();
|
|
22
|
-
resolve(false);
|
|
23
|
-
});
|
|
24
|
-
socket.on("error", (error) => {
|
|
25
|
-
console.log(`ā ${protocol} port ${port} not reachable: ${error.message}`);
|
|
26
|
-
resolve(false);
|
|
27
|
-
});
|
|
28
|
-
socket.connect(port, "localhost");
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
32
|
-
console.log(`ā Error checking ${protocol} port ${port}:`, error);
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
// Check Jaeger ports
|
|
37
|
-
console.log("Checking Jaeger ports:");
|
|
38
|
-
console.log("āāāāāāāāāāāāāāāāāāāāāāāāāāāāā");
|
|
39
|
-
await checkPort(4317, "OTLP gRPC"); // Collector OTLP gRPC
|
|
40
|
-
await checkPort(4318, "OTLP HTTP"); // Collector OTLP HTTP
|
|
41
|
-
await checkPort(16686, "UI "); // Query UI
|
|
42
|
-
console.log("\nš Checking Docker container...\n");
|
|
43
|
-
// Check if Jaeger container is running
|
|
44
|
-
try {
|
|
45
|
-
const { execSync } = await import("node:child_process");
|
|
46
|
-
const output = execSync("docker ps --filter name=jaeger --format '{{.Status}}'", {
|
|
47
|
-
encoding: "utf-8",
|
|
48
|
-
}).trim();
|
|
49
|
-
if (output) {
|
|
50
|
-
console.log(`ā
Jaeger container is running: ${output}`);
|
|
51
|
-
// Check container logs for OTLP
|
|
52
|
-
try {
|
|
53
|
-
const logs = execSync("docker logs jaeger 2>&1 | tail -20", {
|
|
54
|
-
encoding: "utf-8",
|
|
55
|
-
});
|
|
56
|
-
console.log("\nš Recent container logs:");
|
|
57
|
-
console.log("āāāāāāāāāāāāāāāāāāāāāāāāāāāāā");
|
|
58
|
-
console.log(logs);
|
|
59
|
-
}
|
|
60
|
-
catch (e) {
|
|
61
|
-
console.log("ā ļø Could not fetch container logs");
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
console.log("ā No Jaeger container running with name 'jaeger'");
|
|
66
|
-
console.log("\nš” Start Jaeger with:");
|
|
67
|
-
console.log("docker run -d --name jaeger \\");
|
|
68
|
-
console.log(" -e COLLECTOR_OTLP_ENABLED=true \\");
|
|
69
|
-
console.log(" -p 16686:16686 \\");
|
|
70
|
-
console.log(" -p 4317:4317 \\");
|
|
71
|
-
console.log(" -p 4318:4318 \\");
|
|
72
|
-
console.log(" jaegertracing/all-in-one:latest");
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
catch (error) {
|
|
76
|
-
console.log("ā ļø Could not check Docker container:", error);
|
|
77
|
-
}
|
|
78
|
-
console.log("\nšÆ Next steps:");
|
|
79
|
-
console.log("āāāāāāāāāāāāāāāāāāāāāāāāāāāāā");
|
|
80
|
-
console.log("1. Make sure ports above show ā
");
|
|
81
|
-
console.log("2. Run the test: ENABLE_TELEMETRY=true bun test-telemetry.ts");
|
|
82
|
-
console.log("3. Check UI: http://localhost:16686");
|
package/dist/run-subagents.d.ts
DELETED
package/dist/run-subagents.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bun
|
|
2
|
-
/**
|
|
3
|
-
* Helper script to test makeSubagentsTool
|
|
4
|
-
*
|
|
5
|
-
* Usage:
|
|
6
|
-
* bun packages/agent/run-subagents.ts
|
|
7
|
-
* bun packages/agent/run-subagents.ts "your custom query here"
|
|
8
|
-
*/
|
|
9
|
-
import { makeSubagentsTool } from "./utils";
|
|
10
|
-
import { makeRunnerFromDefinition } from "./runner";
|
|
11
|
-
// Default query if none provided
|
|
12
|
-
const defaultQuery = "Can you search for news about artificial intelligence?";
|
|
13
|
-
const userQuery = process.argv[2] || defaultQuery;
|
|
14
|
-
console.log("š§Ŗ Testing makeSubagentsTool\n");
|
|
15
|
-
console.log("Query:", userQuery);
|
|
16
|
-
console.log("ā".repeat(60));
|
|
17
|
-
// Create a simple subagent definition
|
|
18
|
-
const simpleSubagent = {
|
|
19
|
-
model: "claude-sonnet-4-5-20250929",
|
|
20
|
-
systemPrompt: "You are a helpful research assistant. Provide concise, informative responses.",
|
|
21
|
-
tools: [
|
|
22
|
-
"web_search",
|
|
23
|
-
"todo_write",
|
|
24
|
-
],
|
|
25
|
-
mcps: [],
|
|
26
|
-
};
|
|
27
|
-
// Create the coordinator agent with subagent tool
|
|
28
|
-
const coordinatorAgent = {
|
|
29
|
-
model: "claude-sonnet-4-5-20250929",
|
|
30
|
-
systemPrompt: `You are a coordinator agent that delegates tasks to specialized subagents.
|
|
31
|
-
When you receive a query, evaluate if it needs to be delegated to a subagent.
|
|
32
|
-
If so, use the Task tool with the appropriate subagent.`,
|
|
33
|
-
tools: [
|
|
34
|
-
makeSubagentsTool([
|
|
35
|
-
{
|
|
36
|
-
agentName: "researcher",
|
|
37
|
-
description: "Use this agent to research topics, search the web, and gather information",
|
|
38
|
-
path: import.meta.filename, // Points to itself as a simple test
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
agentName: "simple",
|
|
42
|
-
description: "Use this for general tasks that don't require specialized tools",
|
|
43
|
-
path: import.meta.filename,
|
|
44
|
-
},
|
|
45
|
-
]),
|
|
46
|
-
],
|
|
47
|
-
mcps: [],
|
|
48
|
-
};
|
|
49
|
-
// Run the test
|
|
50
|
-
async function runTest() {
|
|
51
|
-
console.log("\nš Creating runner...");
|
|
52
|
-
const runner = makeRunnerFromDefinition(coordinatorAgent);
|
|
53
|
-
console.log("š Running query...\n");
|
|
54
|
-
try {
|
|
55
|
-
const stream = runner.invoke({
|
|
56
|
-
prompt: [
|
|
57
|
-
{
|
|
58
|
-
type: "text",
|
|
59
|
-
text: userQuery,
|
|
60
|
-
},
|
|
61
|
-
],
|
|
62
|
-
sessionId: "test-session",
|
|
63
|
-
messageId: "msg-1",
|
|
64
|
-
});
|
|
65
|
-
console.log("š Stream output:");
|
|
66
|
-
console.log("ā".repeat(60));
|
|
67
|
-
let messageCount = 0;
|
|
68
|
-
let toolCallCount = 0;
|
|
69
|
-
let lastTextContent = "";
|
|
70
|
-
for await (const chunk of stream) {
|
|
71
|
-
messageCount++;
|
|
72
|
-
// Pretty print different types of chunks
|
|
73
|
-
if (chunk.type === "agent_message_chunk") {
|
|
74
|
-
const content = chunk.content;
|
|
75
|
-
if (content.type === "text" && content.text) {
|
|
76
|
-
process.stdout.write(content.text);
|
|
77
|
-
lastTextContent += content.text;
|
|
78
|
-
}
|
|
79
|
-
else if (content.type === "tool_use") {
|
|
80
|
-
toolCallCount++;
|
|
81
|
-
console.log(`\n\nš§ Tool Call #${toolCallCount}: ${content.name}`);
|
|
82
|
-
console.log(" Input:", JSON.stringify(content.input, null, 2).split('\n').join('\n '));
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
else if (chunk.type === "tool_result") {
|
|
86
|
-
console.log("\n\nā
Tool Result:");
|
|
87
|
-
console.log(" ", JSON.stringify(chunk, null, 2).split('\n').join('\n '));
|
|
88
|
-
}
|
|
89
|
-
else {
|
|
90
|
-
// Print other chunk types for debugging
|
|
91
|
-
console.log("\n\nš¦ Chunk:", JSON.stringify(chunk, null, 2));
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
console.log("\n\n" + "ā".repeat(60));
|
|
95
|
-
console.log("⨠Test completed!");
|
|
96
|
-
console.log(` Total chunks: ${messageCount}`);
|
|
97
|
-
console.log(` Tool calls: ${toolCallCount}`);
|
|
98
|
-
console.log("ā".repeat(60));
|
|
99
|
-
}
|
|
100
|
-
catch (error) {
|
|
101
|
-
console.error("\nā Error during test:");
|
|
102
|
-
console.error(error);
|
|
103
|
-
process.exit(1);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
// Run the test
|
|
107
|
-
runTest().catch((error) => {
|
|
108
|
-
console.error("Fatal error:", error);
|
|
109
|
-
process.exit(1);
|
|
110
|
-
});
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
/**
|
|
3
|
-
* Custom stream events emitted by subagent tools via config.writer
|
|
4
|
-
*/
|
|
5
|
-
export declare const SubagentToolCallEventSchema: z.ZodObject<{
|
|
6
|
-
type: z.ZodLiteral<"tool_call">;
|
|
7
|
-
toolName: z.ZodString;
|
|
8
|
-
}, z.core.$strip>;
|
|
9
|
-
export declare const SubagentMessageEventSchema: z.ZodObject<{
|
|
10
|
-
type: z.ZodLiteral<"message">;
|
|
11
|
-
text: z.ZodString;
|
|
12
|
-
}, z.core.$strip>;
|
|
13
|
-
export declare const SubagentEventSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
14
|
-
type: z.ZodLiteral<"tool_call">;
|
|
15
|
-
toolName: z.ZodString;
|
|
16
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
17
|
-
type: z.ZodLiteral<"message">;
|
|
18
|
-
text: z.ZodString;
|
|
19
|
-
}, z.core.$strip>], "type">;
|
|
20
|
-
export type SubagentToolCallEvent = z.infer<typeof SubagentToolCallEventSchema>;
|
|
21
|
-
export type SubagentMessageEvent = z.infer<typeof SubagentMessageEventSchema>;
|
|
22
|
-
export type SubagentEvent = z.infer<typeof SubagentEventSchema>;
|
|
23
|
-
/**
|
|
24
|
-
* Wrapper for subagent events that includes the parent tool call ID
|
|
25
|
-
*/
|
|
26
|
-
export declare const CustomStreamChunkSchema: z.ZodObject<{
|
|
27
|
-
parentToolCallId: z.ZodString;
|
|
28
|
-
event: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
29
|
-
type: z.ZodLiteral<"tool_call">;
|
|
30
|
-
toolName: z.ZodString;
|
|
31
|
-
}, z.core.$strip>, z.ZodObject<{
|
|
32
|
-
type: z.ZodLiteral<"message">;
|
|
33
|
-
text: z.ZodString;
|
|
34
|
-
}, z.core.$strip>], "type">;
|
|
35
|
-
}, z.core.$strip>;
|
|
36
|
-
export type CustomStreamChunk = z.infer<typeof CustomStreamChunkSchema>;
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
/**
|
|
3
|
-
* Custom stream events emitted by subagent tools via config.writer
|
|
4
|
-
*/
|
|
5
|
-
export const SubagentToolCallEventSchema = z.object({
|
|
6
|
-
type: z.literal("tool_call"),
|
|
7
|
-
toolName: z.string(),
|
|
8
|
-
});
|
|
9
|
-
export const SubagentMessageEventSchema = z.object({
|
|
10
|
-
type: z.literal("message"),
|
|
11
|
-
text: z.string(),
|
|
12
|
-
});
|
|
13
|
-
export const SubagentEventSchema = z.discriminatedUnion("type", [
|
|
14
|
-
SubagentToolCallEventSchema,
|
|
15
|
-
SubagentMessageEventSchema,
|
|
16
|
-
]);
|
|
17
|
-
/**
|
|
18
|
-
* Wrapper for subagent events that includes the parent tool call ID
|
|
19
|
-
*/
|
|
20
|
-
export const CustomStreamChunkSchema = z.object({
|
|
21
|
-
parentToolCallId: z.string(),
|
|
22
|
-
event: SubagentEventSchema,
|
|
23
|
-
});
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { z } from "zod";
|
|
2
|
-
export declare function makeBashTool(workingDirectory?: string): import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
3
|
-
command: z.ZodString;
|
|
4
|
-
timeout: z.ZodOptional<z.ZodNumber>;
|
|
5
|
-
description: z.ZodOptional<z.ZodString>;
|
|
6
|
-
}, z.core.$strip>, {
|
|
7
|
-
command: string;
|
|
8
|
-
timeout?: number | undefined;
|
|
9
|
-
description?: string | undefined;
|
|
10
|
-
}, {
|
|
11
|
-
command: string;
|
|
12
|
-
timeout?: number | undefined;
|
|
13
|
-
description?: string | undefined;
|
|
14
|
-
}, unknown>;
|