@townco/agent 0.1.19 → 0.1.20
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/http.js +189 -173
- package/dist/definition/index.d.ts +3 -0
- package/dist/definition/index.js +11 -1
- package/dist/dev-agent/index.d.ts +2 -0
- package/dist/dev-agent/index.js +18 -0
- package/dist/index.js +18 -12
- package/dist/runner/agent-runner.d.ts +4 -1
- package/dist/runner/langchain/index.d.ts +2 -1
- package/dist/runner/langchain/index.js +27 -8
- package/dist/runner/langchain/tools/filesystem.d.ts +66 -0
- package/dist/runner/langchain/tools/filesystem.js +261 -0
- package/dist/runner/tools.d.ts +5 -2
- package/dist/runner/tools.js +11 -1
- package/dist/templates/index.d.ts +3 -0
- package/dist/templates/index.js +11 -2
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.js +1 -0
- package/dist/utils/logger.d.ts +39 -0
- package/dist/utils/logger.js +175 -0
- package/index.ts +6 -1
- package/package.json +10 -5
- package/templates/index.ts +14 -3
package/dist/acp-server/http.js
CHANGED
|
@@ -5,184 +5,200 @@ import { Hono } from "hono";
|
|
|
5
5
|
import { cors } from "hono/cors";
|
|
6
6
|
import { streamSSE } from "hono/streaming";
|
|
7
7
|
import { makeRunnerFromDefinition } from "../runner";
|
|
8
|
+
import { createLogger } from "../utils/logger.js";
|
|
8
9
|
import { AgentAcpAdapter } from "./adapter";
|
|
9
|
-
|
|
10
|
+
const logger = createLogger("agent");
|
|
10
11
|
// Use PGlite in-memory database for LISTEN/NOTIFY
|
|
11
12
|
const pg = new PGlite();
|
|
12
13
|
// Helper to create safe channel names from untrusted IDs
|
|
13
14
|
function safeChannelName(prefix, id) {
|
|
14
|
-
|
|
15
|
-
|
|
15
|
+
const hash = createHash("sha256").update(id).digest("hex").slice(0, 16);
|
|
16
|
+
return `${prefix}_${hash}`;
|
|
16
17
|
}
|
|
17
18
|
export function makeHttpTransport(agent) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
19
|
+
const inbound = new TransformStream();
|
|
20
|
+
const outbound = new TransformStream();
|
|
21
|
+
const bridge = acp.ndJsonStream(outbound.writable, inbound.readable);
|
|
22
|
+
const agentRunner = "definition" in agent ? agent : makeRunnerFromDefinition(agent);
|
|
23
|
+
new acp.AgentSideConnection((conn) => new AgentAcpAdapter(agentRunner, conn), bridge);
|
|
24
|
+
const app = new Hono();
|
|
25
|
+
const decoder = new TextDecoder();
|
|
26
|
+
const encoder = new TextEncoder();
|
|
27
|
+
(async () => {
|
|
28
|
+
const reader = outbound.readable.getReader();
|
|
29
|
+
let buf = "";
|
|
30
|
+
for (;;) {
|
|
31
|
+
const { value, done } = await reader.read();
|
|
32
|
+
if (done)
|
|
33
|
+
break;
|
|
34
|
+
buf += decoder.decode(value, { stream: true });
|
|
35
|
+
for (let nl = buf.indexOf("\n"); nl !== -1; nl = buf.indexOf("\n")) {
|
|
36
|
+
const line = buf.slice(0, nl).trim();
|
|
37
|
+
buf = buf.slice(nl + 1);
|
|
38
|
+
if (!line)
|
|
39
|
+
continue;
|
|
40
|
+
let rawMsg;
|
|
41
|
+
try {
|
|
42
|
+
rawMsg = JSON.parse(line);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
// ignore malformed lines
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
// Validate the agent message with Zod schema
|
|
49
|
+
const parseResult = acp.agentOutgoingMessageSchema.safeParse(rawMsg);
|
|
50
|
+
if (!parseResult.success) {
|
|
51
|
+
logger.error("Invalid agent message", {
|
|
52
|
+
issues: parseResult.error.issues,
|
|
53
|
+
});
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
//const msg = parseResult.data;
|
|
57
|
+
if (rawMsg == null || typeof rawMsg !== "object") {
|
|
58
|
+
logger.warn("Malformed message, cannot route", { message: rawMsg });
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
if ("id" in rawMsg &&
|
|
62
|
+
typeof rawMsg.id === "string" &&
|
|
63
|
+
rawMsg.id != null) {
|
|
64
|
+
// This is a response to a request - send to response-specific channel
|
|
65
|
+
const channel = safeChannelName("response", rawMsg.id);
|
|
66
|
+
const payload = JSON.stringify(rawMsg).replace(/'/g, "''");
|
|
67
|
+
await pg.query(`NOTIFY ${channel}, '${payload}'`);
|
|
68
|
+
}
|
|
69
|
+
else if ("params" in rawMsg &&
|
|
70
|
+
rawMsg.params != null &&
|
|
71
|
+
typeof rawMsg.params === "object" &&
|
|
72
|
+
"sessionId" in rawMsg.params &&
|
|
73
|
+
typeof rawMsg.params.sessionId === "string") {
|
|
74
|
+
// Other messages (notifications, requests from agent) go to
|
|
75
|
+
// session-specific channel
|
|
76
|
+
const sessionId = rawMsg.params.sessionId;
|
|
77
|
+
const channel = safeChannelName("notifications", sessionId);
|
|
78
|
+
const payload = JSON.stringify(rawMsg).replace(/'/g, "''");
|
|
79
|
+
await pg.query(`NOTIFY ${channel}, '${payload}'`);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
logger.warn("Message without sessionId, cannot route", {
|
|
83
|
+
message: rawMsg,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
})();
|
|
89
|
+
// TODO: fix this for production
|
|
90
|
+
app.use("*", cors({
|
|
91
|
+
origin: "*",
|
|
92
|
+
allowHeaders: ["content-type", "authorization", "x-session-id"],
|
|
93
|
+
allowMethods: ["GET", "POST", "OPTIONS"],
|
|
94
|
+
}));
|
|
95
|
+
app.get("/health", (c) => c.json({ ok: true }));
|
|
96
|
+
app.get("/events", (c) => {
|
|
97
|
+
const sessionId = c.req.header("X-Session-ID");
|
|
98
|
+
if (!sessionId) {
|
|
99
|
+
logger.warn("GET /events - Missing X-Session-ID header");
|
|
100
|
+
return c.json({ error: "X-Session-ID header required" }, 401);
|
|
101
|
+
}
|
|
102
|
+
logger.debug("GET /events - SSE connection opened", { sessionId });
|
|
103
|
+
return streamSSE(c, async (stream) => {
|
|
104
|
+
await stream.writeSSE({ event: "ping", data: "{}" });
|
|
105
|
+
const hb = setInterval(() => {
|
|
106
|
+
// Heartbeat to keep proxies from terminating idle connections
|
|
107
|
+
void stream.writeSSE({ event: "ping", data: "{}" });
|
|
108
|
+
}, 1000);
|
|
109
|
+
const channel = safeChannelName("notifications", sessionId);
|
|
110
|
+
const unsub = await pg.listen(channel, async (payload) => {
|
|
111
|
+
const json = JSON.parse(payload);
|
|
112
|
+
logger.trace("Sending SSE message", { sessionId, channel });
|
|
113
|
+
await stream.writeSSE({
|
|
114
|
+
event: "message",
|
|
115
|
+
data: JSON.stringify(json),
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
// Clean up when the client disconnects
|
|
119
|
+
stream.onAbort(() => {
|
|
120
|
+
logger.debug("GET /events - SSE connection closed", { sessionId });
|
|
121
|
+
clearInterval(hb);
|
|
122
|
+
unsub();
|
|
123
|
+
});
|
|
124
|
+
// Keep the connection open indefinitely
|
|
125
|
+
await stream.sleep(1000 * 60 * 60 * 24);
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
app.post("/rpc", async (c) => {
|
|
129
|
+
// Get and validate the request body
|
|
130
|
+
const rawBody = await c.req.json();
|
|
131
|
+
// Validate using Zod schema
|
|
132
|
+
const parseResult = acp.clientOutgoingMessageSchema.safeParse(rawBody);
|
|
133
|
+
if (!parseResult.success) {
|
|
134
|
+
logger.warn("POST /rpc - Invalid ACP message", {
|
|
135
|
+
issues: parseResult.error.issues,
|
|
136
|
+
});
|
|
137
|
+
return c.json({
|
|
138
|
+
error: "Invalid ACP message",
|
|
139
|
+
details: parseResult.error.issues,
|
|
140
|
+
}, 400);
|
|
141
|
+
}
|
|
142
|
+
const body = parseResult.data;
|
|
143
|
+
// Ensure the request has an id
|
|
144
|
+
const id = "id" in body ? body.id : null;
|
|
145
|
+
const isRequest = id != null;
|
|
146
|
+
const method = "method" in body ? body.method : "notification";
|
|
147
|
+
logger.debug("POST /rpc - Received request", { method, id, isRequest });
|
|
148
|
+
if (isRequest) {
|
|
149
|
+
// For requests, set up a listener before sending the request
|
|
150
|
+
const responseChannel = safeChannelName("response", String(id));
|
|
151
|
+
let responseResolver;
|
|
152
|
+
const responsePromise = new Promise((resolve) => {
|
|
153
|
+
responseResolver = resolve;
|
|
154
|
+
});
|
|
155
|
+
const unsub = await pg.listen(responseChannel, (payload) => {
|
|
156
|
+
const rawResponse = JSON.parse(payload);
|
|
157
|
+
responseResolver(rawResponse);
|
|
158
|
+
});
|
|
159
|
+
// Write NDJSON line into the ACP inbound stream
|
|
160
|
+
const writer = inbound.writable.getWriter();
|
|
161
|
+
await writer.write(encoder.encode(`${JSON.stringify(body)}\n`));
|
|
162
|
+
writer.releaseLock();
|
|
163
|
+
// Wait for response with 30 second timeout
|
|
164
|
+
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Request timeout")), 30000));
|
|
165
|
+
try {
|
|
166
|
+
const response = await Promise.race([responsePromise, timeoutPromise]);
|
|
167
|
+
logger.debug("POST /rpc - Response received", { id });
|
|
168
|
+
return c.json(response);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
logger.error("POST /rpc - Request timeout", {
|
|
172
|
+
id,
|
|
173
|
+
error: error instanceof Error ? error.message : String(error),
|
|
174
|
+
});
|
|
175
|
+
throw error;
|
|
176
|
+
}
|
|
177
|
+
finally {
|
|
178
|
+
// Clean up the listener whether we got a response or timed out
|
|
179
|
+
unsub();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
// For notifications, just send and return success
|
|
184
|
+
const writer = inbound.writable.getWriter();
|
|
185
|
+
await writer.write(encoder.encode(`${JSON.stringify(body)}\n`));
|
|
186
|
+
writer.releaseLock();
|
|
187
|
+
logger.debug("POST /rpc - Notification sent", { method });
|
|
188
|
+
return c.json({
|
|
189
|
+
success: true,
|
|
190
|
+
message: "Notification sent to agent",
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
const port = Number.parseInt(process.env.PORT || "3100", 10);
|
|
195
|
+
logger.info("Starting HTTP server", { port });
|
|
196
|
+
Bun.serve({
|
|
197
|
+
fetch: app.fetch,
|
|
198
|
+
port,
|
|
199
|
+
});
|
|
200
|
+
logger.info("HTTP server listening", {
|
|
201
|
+
url: `http://localhost:${port}`,
|
|
202
|
+
port,
|
|
203
|
+
});
|
|
188
204
|
}
|
|
@@ -19,6 +19,9 @@ export declare const AgentDefinitionSchema: z.ZodObject<{
|
|
|
19
19
|
tools: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodObject<{
|
|
20
20
|
type: z.ZodLiteral<"custom">;
|
|
21
21
|
modulePath: z.ZodString;
|
|
22
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
23
|
+
type: z.ZodLiteral<"filesystem">;
|
|
24
|
+
working_directory: z.ZodOptional<z.ZodString>;
|
|
22
25
|
}, z.core.$strip>]>>>;
|
|
23
26
|
mcps: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
24
27
|
name: z.ZodString;
|
package/dist/definition/index.js
CHANGED
|
@@ -29,8 +29,18 @@ const CustomToolSchema = z.object({
|
|
|
29
29
|
type: z.literal("custom"),
|
|
30
30
|
modulePath: z.string(),
|
|
31
31
|
});
|
|
32
|
+
/** Filesystem tool configuration schema. */
|
|
33
|
+
const FilesystemToolSchema = z.object({
|
|
34
|
+
type: z.literal("filesystem"),
|
|
35
|
+
/** If omitted, defaults to process.cwd() at runtime */
|
|
36
|
+
working_directory: z.string().optional(),
|
|
37
|
+
});
|
|
32
38
|
/** Tool schema - can be a string (built-in tool) or custom tool object. */
|
|
33
|
-
const ToolSchema = z.union([
|
|
39
|
+
const ToolSchema = z.union([
|
|
40
|
+
z.string(),
|
|
41
|
+
CustomToolSchema,
|
|
42
|
+
FilesystemToolSchema,
|
|
43
|
+
]);
|
|
34
44
|
/** Agent definition schema. */
|
|
35
45
|
export const AgentDefinitionSchema = z.object({
|
|
36
46
|
systemPrompt: z.string().nullable(),
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { makeHttpTransport, makeStdioTransport } from "../acp-server/index";
|
|
5
|
+
// Load agent definition from JSON file
|
|
6
|
+
const configPath = join(import.meta.dir, "agent.json");
|
|
7
|
+
const agent = JSON.parse(readFileSync(configPath, "utf-8"));
|
|
8
|
+
const transport = process.argv[2] || "stdio";
|
|
9
|
+
if (transport === "http") {
|
|
10
|
+
makeHttpTransport(agent);
|
|
11
|
+
}
|
|
12
|
+
else if (transport === "stdio") {
|
|
13
|
+
makeStdioTransport(agent);
|
|
14
|
+
}
|
|
15
|
+
else {
|
|
16
|
+
console.error(`Invalid transport: ${transport}`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,19 +1,25 @@
|
|
|
1
1
|
import { makeHttpTransport, makeStdioTransport } from "./acp-server";
|
|
2
|
-
|
|
3
2
|
const exampleAgent = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
model: "claude-sonnet-4-5-20250929",
|
|
4
|
+
systemPrompt: "You are a helpful assistant.",
|
|
5
|
+
tools: [
|
|
6
|
+
"todo_write",
|
|
7
|
+
"get_weather",
|
|
8
|
+
"web_search",
|
|
9
|
+
{ type: "filesystem", working_directory: "/Users/michael/code/town" },
|
|
10
|
+
],
|
|
11
|
+
mcps: [],
|
|
8
12
|
};
|
|
9
13
|
// Parse transport type from command line argument
|
|
10
14
|
const transport = process.argv[2] || "stdio";
|
|
11
15
|
if (transport === "http") {
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
makeHttpTransport(exampleAgent);
|
|
17
|
+
}
|
|
18
|
+
else if (transport === "stdio") {
|
|
19
|
+
makeStdioTransport(exampleAgent);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
console.error(`Invalid transport: ${transport}`);
|
|
23
|
+
console.error("Usage: bun run index.ts [stdio|http]");
|
|
24
|
+
process.exit(1);
|
|
19
25
|
}
|
|
@@ -3,9 +3,12 @@ import { z } from "zod";
|
|
|
3
3
|
export declare const zAgentRunnerParams: z.ZodObject<{
|
|
4
4
|
systemPrompt: z.ZodNullable<z.ZodString>;
|
|
5
5
|
model: z.ZodString;
|
|
6
|
-
tools: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodLiteral<"todo_write">, z.ZodLiteral<"get_weather">, z.ZodLiteral<"web_search">]>, z.ZodObject<{
|
|
6
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodUnion<readonly [z.ZodLiteral<"todo_write">, z.ZodLiteral<"get_weather">, z.ZodLiteral<"web_search">, z.ZodLiteral<"filesystem">]>, z.ZodObject<{
|
|
7
7
|
type: z.ZodLiteral<"custom">;
|
|
8
8
|
modulePath: z.ZodString;
|
|
9
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
10
|
+
type: z.ZodLiteral<"filesystem">;
|
|
11
|
+
working_directory: z.ZodOptional<z.ZodString>;
|
|
9
12
|
}, z.core.$strip>]>>>;
|
|
10
13
|
mcps: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
11
14
|
name: z.ZodString;
|
|
@@ -5,8 +5,9 @@ import type { BuiltInToolType } from "../tools";
|
|
|
5
5
|
type LangchainTool = DynamicStructuredTool | Tool;
|
|
6
6
|
/** Lazily-loaded langchain tools */
|
|
7
7
|
type LazyLangchainTool = MakeLazy<LangchainTool>;
|
|
8
|
+
type LazyLangchainTools = () => readonly LangchainTool[];
|
|
8
9
|
type MakeLazy<T> = T extends LangchainTool ? () => T : never;
|
|
9
|
-
export declare const TOOL_REGISTRY: Record<BuiltInToolType, LangchainTool | LazyLangchainTool>;
|
|
10
|
+
export declare const TOOL_REGISTRY: Record<BuiltInToolType, LangchainTool | LazyLangchainTool | LazyLangchainTools>;
|
|
10
11
|
export declare class LangchainAgent implements AgentRunner {
|
|
11
12
|
definition: CreateAgentRunnerParams;
|
|
12
13
|
constructor(params: CreateAgentRunnerParams);
|
|
@@ -2,6 +2,7 @@ import { MultiServerMCPClient } from "@langchain/mcp-adapters";
|
|
|
2
2
|
import { AIMessageChunk, createAgent, ToolMessage, tool, } from "langchain";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { loadCustomToolModule } from "../tool-loader";
|
|
5
|
+
import { makeFilesystemTools } from "./tools/filesystem";
|
|
5
6
|
import { todoItemSchema, todoWrite } from "./tools/todo";
|
|
6
7
|
import { makeWebSearchTool } from "./tools/web_search";
|
|
7
8
|
const getWeather = tool(({ city }) => `It's always sunny in ${city}!`, {
|
|
@@ -15,6 +16,7 @@ export const TOOL_REGISTRY = {
|
|
|
15
16
|
todo_write: todoWrite,
|
|
16
17
|
get_weather: getWeather,
|
|
17
18
|
web_search: makeWebSearchTool,
|
|
19
|
+
filesystem: () => makeFilesystemTools(process.cwd()),
|
|
18
20
|
};
|
|
19
21
|
// ============================================================================
|
|
20
22
|
// Custom tool loading
|
|
@@ -47,6 +49,7 @@ export class LangchainAgent {
|
|
|
47
49
|
const todoWriteToolCallIds = new Set();
|
|
48
50
|
// --------------------------------------------------------------------------
|
|
49
51
|
// Resolve tools: built-ins (string) + custom ({ type: "custom", modulePath })
|
|
52
|
+
// + filesystem ({ type: "filesystem", working_directory? })
|
|
50
53
|
// --------------------------------------------------------------------------
|
|
51
54
|
const enabledTools = [];
|
|
52
55
|
const toolDefs = this.definition.tools ?? [];
|
|
@@ -56,13 +59,18 @@ export class LangchainAgent {
|
|
|
56
59
|
if (typeof t === "string") {
|
|
57
60
|
builtInNames.push(t);
|
|
58
61
|
}
|
|
59
|
-
else if (t &&
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
62
|
+
else if (t && typeof t === "object" && "type" in t) {
|
|
63
|
+
const type = t.type;
|
|
64
|
+
if (type === "custom" &&
|
|
65
|
+
"modulePath" in t &&
|
|
66
|
+
typeof t.modulePath === "string") {
|
|
67
|
+
customToolPaths.push(t.modulePath);
|
|
68
|
+
}
|
|
69
|
+
else if (type === "filesystem") {
|
|
70
|
+
const wd = t.working_directory ??
|
|
71
|
+
process.cwd();
|
|
72
|
+
enabledTools.push(...makeFilesystemTools(wd));
|
|
73
|
+
}
|
|
66
74
|
}
|
|
67
75
|
}
|
|
68
76
|
// Built-in tools from registry
|
|
@@ -71,7 +79,18 @@ export class LangchainAgent {
|
|
|
71
79
|
if (!entry) {
|
|
72
80
|
throw new Error(`Unknown built-in tool "${name}"`);
|
|
73
81
|
}
|
|
74
|
-
|
|
82
|
+
if (typeof entry === "function") {
|
|
83
|
+
const result = entry();
|
|
84
|
+
if (Array.isArray(result)) {
|
|
85
|
+
enabledTools.push(...result);
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
enabledTools.push(result);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
enabledTools.push(entry);
|
|
93
|
+
}
|
|
75
94
|
}
|
|
76
95
|
// Custom tools loaded from modulePaths
|
|
77
96
|
if (customToolPaths.length > 0) {
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare function makeFilesystemTools(workingDirectory: string): readonly [import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
3
|
+
pattern: z.ZodString;
|
|
4
|
+
path: z.ZodOptional<z.ZodString>;
|
|
5
|
+
glob: z.ZodOptional<z.ZodString>;
|
|
6
|
+
output_mode: z.ZodOptional<z.ZodEnum<{
|
|
7
|
+
content: "content";
|
|
8
|
+
files_with_matches: "files_with_matches";
|
|
9
|
+
count: "count";
|
|
10
|
+
}>>;
|
|
11
|
+
"-B": z.ZodOptional<z.ZodNumber>;
|
|
12
|
+
"-A": z.ZodOptional<z.ZodNumber>;
|
|
13
|
+
"-C": z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
"-n": z.ZodOptional<z.ZodBoolean>;
|
|
15
|
+
"-i": z.ZodOptional<z.ZodBoolean>;
|
|
16
|
+
type: z.ZodOptional<z.ZodString>;
|
|
17
|
+
head_limit: z.ZodOptional<z.ZodNumber>;
|
|
18
|
+
multiline: z.ZodOptional<z.ZodBoolean>;
|
|
19
|
+
}, z.core.$strip>, {
|
|
20
|
+
pattern: string;
|
|
21
|
+
path?: string | undefined;
|
|
22
|
+
glob?: string | undefined;
|
|
23
|
+
output_mode?: "content" | "files_with_matches" | "count" | undefined;
|
|
24
|
+
"-B"?: number | undefined;
|
|
25
|
+
"-A"?: number | undefined;
|
|
26
|
+
"-C"?: number | undefined;
|
|
27
|
+
"-n"?: boolean | undefined;
|
|
28
|
+
"-i"?: boolean | undefined;
|
|
29
|
+
type?: string | undefined;
|
|
30
|
+
head_limit?: number | undefined;
|
|
31
|
+
multiline?: boolean | undefined;
|
|
32
|
+
}, {
|
|
33
|
+
pattern: string;
|
|
34
|
+
path?: string | undefined;
|
|
35
|
+
glob?: string | undefined;
|
|
36
|
+
output_mode?: "content" | "files_with_matches" | "count" | undefined;
|
|
37
|
+
"-B"?: number | undefined;
|
|
38
|
+
"-A"?: number | undefined;
|
|
39
|
+
"-C"?: number | undefined;
|
|
40
|
+
"-n"?: boolean | undefined;
|
|
41
|
+
"-i"?: boolean | undefined;
|
|
42
|
+
type?: string | undefined;
|
|
43
|
+
head_limit?: number | undefined;
|
|
44
|
+
multiline?: boolean | undefined;
|
|
45
|
+
}, unknown>, import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
46
|
+
file_path: z.ZodString;
|
|
47
|
+
offset: z.ZodOptional<z.ZodNumber>;
|
|
48
|
+
limit: z.ZodOptional<z.ZodNumber>;
|
|
49
|
+
}, z.core.$strip>, {
|
|
50
|
+
file_path: string;
|
|
51
|
+
offset?: number | undefined;
|
|
52
|
+
limit?: number | undefined;
|
|
53
|
+
}, {
|
|
54
|
+
file_path: string;
|
|
55
|
+
offset?: number | undefined;
|
|
56
|
+
limit?: number | undefined;
|
|
57
|
+
}, unknown>, import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
58
|
+
file_path: z.ZodString;
|
|
59
|
+
content: z.ZodString;
|
|
60
|
+
}, z.core.$strip>, {
|
|
61
|
+
file_path: string;
|
|
62
|
+
content: string;
|
|
63
|
+
}, {
|
|
64
|
+
file_path: string;
|
|
65
|
+
content: string;
|
|
66
|
+
}, unknown>];
|