@townco/agent 0.1.0
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/README.md +15 -0
- package/dist/acp-server/adapter.d.ts +15 -0
- package/dist/acp-server/adapter.js +76 -0
- package/dist/acp-server/cli.d.ts +3 -0
- package/dist/acp-server/cli.js +11 -0
- package/dist/acp-server/http.d.ts +3 -0
- package/dist/acp-server/http.js +178 -0
- package/dist/acp-server/index.d.ts +2 -0
- package/dist/acp-server/index.js +2 -0
- package/dist/bin.d.ts +2 -0
- package/dist/bin.js +3 -0
- package/dist/definition/index.d.ts +30 -0
- package/dist/definition/index.js +33 -0
- package/dist/definition/mcp.d.ts +0 -0
- package/dist/definition/mcp.js +1 -0
- package/dist/definition/tools/todo.d.ts +33 -0
- package/dist/definition/tools/todo.js +77 -0
- package/dist/definition/tools/web_search.d.ts +4 -0
- package/dist/definition/tools/web_search.js +23 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.js +20 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +20 -0
- package/dist/runner/agent-runner.d.ts +24 -0
- package/dist/runner/agent-runner.js +9 -0
- package/dist/runner/index.d.ts +4 -0
- package/dist/runner/index.js +18 -0
- package/dist/runner/langchain/index.d.ts +15 -0
- package/dist/runner/langchain/index.js +227 -0
- package/dist/runner/langchain/tools/todo.d.ts +33 -0
- package/dist/runner/langchain/tools/todo.js +77 -0
- package/dist/runner/langchain/tools/web_search.d.ts +4 -0
- package/dist/runner/langchain/tools/web_search.js +23 -0
- package/dist/runner/tools.d.ts +3 -0
- package/dist/runner/tools.js +6 -0
- package/dist/scaffold/bundle.d.ts +4 -0
- package/dist/scaffold/bundle.js +28 -0
- package/dist/scaffold/copy-gui.d.ts +4 -0
- package/dist/scaffold/copy-gui.js +210 -0
- package/dist/scaffold/index.d.ts +16 -0
- package/dist/scaffold/index.js +98 -0
- package/dist/storage/index.d.ts +24 -0
- package/dist/storage/index.js +58 -0
- package/dist/templates/index.d.ts +17 -0
- package/dist/templates/index.js +173 -0
- package/dist/test-script.d.ts +1 -0
- package/dist/test-script.js +17 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +72 -0
- package/templates/index.ts +203 -0
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# town-agent
|
|
2
|
+
|
|
3
|
+
To install dependencies:
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
bun install
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
To run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run index.ts
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
This project was created using `bun init` in bun v1.3.1. [Bun](https://bun.com) is a fast all-in-one JavaScript runtime.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import * as acp from "@agentclientprotocol/sdk";
|
|
2
|
+
import type { AgentRunner } from "../runner";
|
|
3
|
+
/** Adapts an Agent to speak the ACP protocol */
|
|
4
|
+
export declare class AgentAcpAdapter implements acp.Agent {
|
|
5
|
+
private connection;
|
|
6
|
+
private sessions;
|
|
7
|
+
private agent;
|
|
8
|
+
constructor(agent: AgentRunner, connection: acp.AgentSideConnection);
|
|
9
|
+
initialize(_params: acp.InitializeRequest): Promise<acp.InitializeResponse>;
|
|
10
|
+
newSession(_params: acp.NewSessionRequest): Promise<acp.NewSessionResponse>;
|
|
11
|
+
authenticate(_params: acp.AuthenticateRequest): Promise<acp.AuthenticateResponse | undefined>;
|
|
12
|
+
setSessionMode(_params: acp.SetSessionModeRequest): Promise<acp.SetSessionModeResponse>;
|
|
13
|
+
prompt(params: acp.PromptRequest): Promise<acp.PromptResponse>;
|
|
14
|
+
cancel(params: acp.CancelNotification): Promise<void>;
|
|
15
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import * as acp from "@agentclientprotocol/sdk";
|
|
2
|
+
/** Adapts an Agent to speak the ACP protocol */
|
|
3
|
+
export class AgentAcpAdapter {
|
|
4
|
+
connection;
|
|
5
|
+
sessions;
|
|
6
|
+
agent;
|
|
7
|
+
constructor(agent, connection) {
|
|
8
|
+
this.connection = connection;
|
|
9
|
+
this.sessions = new Map();
|
|
10
|
+
this.agent = agent;
|
|
11
|
+
}
|
|
12
|
+
async initialize(_params) {
|
|
13
|
+
return {
|
|
14
|
+
protocolVersion: acp.PROTOCOL_VERSION,
|
|
15
|
+
agentCapabilities: {
|
|
16
|
+
loadSession: false,
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
async newSession(_params) {
|
|
21
|
+
const sessionId = Math.random().toString(36).substring(2);
|
|
22
|
+
this.sessions.set(sessionId, {
|
|
23
|
+
pendingPrompt: null,
|
|
24
|
+
messages: [],
|
|
25
|
+
});
|
|
26
|
+
return {
|
|
27
|
+
sessionId,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
async authenticate(_params) {
|
|
31
|
+
// No auth needed - return empty response
|
|
32
|
+
return {};
|
|
33
|
+
}
|
|
34
|
+
async setSessionMode(_params) {
|
|
35
|
+
// Session mode changes are no-op for us (not related to coding)
|
|
36
|
+
return {};
|
|
37
|
+
}
|
|
38
|
+
async prompt(params) {
|
|
39
|
+
let session = this.sessions.get(params.sessionId);
|
|
40
|
+
// If session not found (e.g., after server restart), create a new one
|
|
41
|
+
if (!session) {
|
|
42
|
+
console.log(`Session ${params.sessionId} not found, creating new session`);
|
|
43
|
+
session = {
|
|
44
|
+
pendingPrompt: null,
|
|
45
|
+
messages: [],
|
|
46
|
+
};
|
|
47
|
+
this.sessions.set(params.sessionId, session);
|
|
48
|
+
}
|
|
49
|
+
session.pendingPrompt?.abort();
|
|
50
|
+
session.pendingPrompt = new AbortController();
|
|
51
|
+
try {
|
|
52
|
+
for await (const msg of this.agent.invoke({
|
|
53
|
+
prompt: params.prompt,
|
|
54
|
+
sessionId: params.sessionId,
|
|
55
|
+
})) {
|
|
56
|
+
this.connection.sessionUpdate({
|
|
57
|
+
sessionId: params.sessionId,
|
|
58
|
+
update: msg,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
catch (err) {
|
|
63
|
+
if (session.pendingPrompt.signal.aborted) {
|
|
64
|
+
return { stopReason: "cancelled" };
|
|
65
|
+
}
|
|
66
|
+
throw err;
|
|
67
|
+
}
|
|
68
|
+
session.pendingPrompt = null;
|
|
69
|
+
return {
|
|
70
|
+
stopReason: "end_turn",
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
async cancel(params) {
|
|
74
|
+
this.sessions.get(params.sessionId)?.pendingPrompt?.abort();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Readable, Writable } from "node:stream";
|
|
2
|
+
import * as acp from "@agentclientprotocol/sdk";
|
|
3
|
+
import { makeRunnerFromDefinition } from "../runner";
|
|
4
|
+
import { AgentAcpAdapter } from "./adapter";
|
|
5
|
+
export function makeStdioTransport(agent) {
|
|
6
|
+
const agentRunner = "definition" in agent ? agent : makeRunnerFromDefinition(agent);
|
|
7
|
+
const input = Writable.toWeb(process.stdout);
|
|
8
|
+
const output = Readable.toWeb(process.stdin);
|
|
9
|
+
const stream = acp.ndJsonStream(input, output);
|
|
10
|
+
new acp.AgentSideConnection((conn) => new AgentAcpAdapter(agentRunner, conn), stream);
|
|
11
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import * as acp from "@agentclientprotocol/sdk";
|
|
3
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
4
|
+
import { Hono } from "hono";
|
|
5
|
+
import { cors } from "hono/cors";
|
|
6
|
+
import { streamSSE } from "hono/streaming";
|
|
7
|
+
import { makeRunnerFromDefinition } from "../runner";
|
|
8
|
+
import { AgentAcpAdapter } from "./adapter";
|
|
9
|
+
// Use PGlite in-memory database for LISTEN/NOTIFY
|
|
10
|
+
const pg = new PGlite();
|
|
11
|
+
// Helper to create safe channel names from untrusted IDs
|
|
12
|
+
function safeChannelName(prefix, id) {
|
|
13
|
+
const hash = createHash("sha256").update(id).digest("hex").slice(0, 16);
|
|
14
|
+
return `${prefix}_${hash}`;
|
|
15
|
+
}
|
|
16
|
+
export function makeHttpTransport(agent) {
|
|
17
|
+
const inbound = new TransformStream();
|
|
18
|
+
const outbound = new TransformStream();
|
|
19
|
+
const bridge = acp.ndJsonStream(outbound.writable, inbound.readable);
|
|
20
|
+
const agentRunner = "definition" in agent ? agent : makeRunnerFromDefinition(agent);
|
|
21
|
+
new acp.AgentSideConnection((conn) => new AgentAcpAdapter(agentRunner, conn), bridge);
|
|
22
|
+
const app = new Hono();
|
|
23
|
+
const decoder = new TextDecoder();
|
|
24
|
+
const encoder = new TextEncoder();
|
|
25
|
+
(async () => {
|
|
26
|
+
const reader = outbound.readable.getReader();
|
|
27
|
+
let buf = "";
|
|
28
|
+
for (;;) {
|
|
29
|
+
const { value, done } = await reader.read();
|
|
30
|
+
if (done)
|
|
31
|
+
break;
|
|
32
|
+
buf += decoder.decode(value, { stream: true });
|
|
33
|
+
for (let nl = buf.indexOf("\n"); nl !== -1; nl = buf.indexOf("\n")) {
|
|
34
|
+
const line = buf.slice(0, nl).trim();
|
|
35
|
+
buf = buf.slice(nl + 1);
|
|
36
|
+
if (!line)
|
|
37
|
+
continue;
|
|
38
|
+
let rawMsg;
|
|
39
|
+
try {
|
|
40
|
+
rawMsg = JSON.parse(line);
|
|
41
|
+
}
|
|
42
|
+
catch {
|
|
43
|
+
// ignore malformed lines
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
// Validate the agent message with Zod schema
|
|
47
|
+
const parseResult = acp.agentOutgoingMessageSchema.safeParse(rawMsg);
|
|
48
|
+
if (!parseResult.success) {
|
|
49
|
+
console.error("Invalid agent message:", parseResult.error.issues);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
//const msg = parseResult.data;
|
|
53
|
+
if (rawMsg == null || typeof rawMsg !== "object") {
|
|
54
|
+
console.warn("Malformed message, cannot route:", rawMsg);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if ("id" in rawMsg &&
|
|
58
|
+
typeof rawMsg.id === "string" &&
|
|
59
|
+
rawMsg.id != null) {
|
|
60
|
+
// This is a response to a request - send to response-specific channel
|
|
61
|
+
const channel = safeChannelName("response", rawMsg.id);
|
|
62
|
+
const payload = JSON.stringify(rawMsg).replace(/'/g, "''");
|
|
63
|
+
await pg.query(`NOTIFY ${channel}, '${payload}'`);
|
|
64
|
+
}
|
|
65
|
+
else if ("params" in rawMsg &&
|
|
66
|
+
rawMsg.params != null &&
|
|
67
|
+
typeof rawMsg.params === "object" &&
|
|
68
|
+
"sessionId" in rawMsg.params &&
|
|
69
|
+
typeof rawMsg.params.sessionId === "string") {
|
|
70
|
+
// Other messages (notifications, requests from agent) go to
|
|
71
|
+
// session-specific channel
|
|
72
|
+
const sessionId = rawMsg.params.sessionId;
|
|
73
|
+
const channel = safeChannelName("notifications", sessionId);
|
|
74
|
+
const payload = JSON.stringify(rawMsg).replace(/'/g, "''");
|
|
75
|
+
await pg.query(`NOTIFY ${channel}, '${payload}'`);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
console.warn("Message without sessionId, cannot route:", rawMsg);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
// TODO: fix this for production
|
|
84
|
+
app.use("*", cors({
|
|
85
|
+
origin: "*",
|
|
86
|
+
allowHeaders: ["content-type", "authorization", "x-session-id"],
|
|
87
|
+
allowMethods: ["GET", "POST", "OPTIONS"],
|
|
88
|
+
}));
|
|
89
|
+
app.get("/health", (c) => c.json({ ok: true }));
|
|
90
|
+
app.get("/events", (c) => {
|
|
91
|
+
const sessionId = c.req.header("X-Session-ID");
|
|
92
|
+
if (!sessionId) {
|
|
93
|
+
return c.json({ error: "X-Session-ID header required" }, 401);
|
|
94
|
+
}
|
|
95
|
+
return streamSSE(c, async (stream) => {
|
|
96
|
+
await stream.writeSSE({ event: "ping", data: "{}" });
|
|
97
|
+
const hb = setInterval(() => {
|
|
98
|
+
// Heartbeat to keep proxies from terminating idle connections
|
|
99
|
+
void stream.writeSSE({ event: "ping", data: "{}" });
|
|
100
|
+
}, 1000);
|
|
101
|
+
const channel = safeChannelName("notifications", sessionId);
|
|
102
|
+
const unsub = await pg.listen(channel, async (payload) => {
|
|
103
|
+
const json = JSON.parse(payload);
|
|
104
|
+
await stream.writeSSE({
|
|
105
|
+
event: "message",
|
|
106
|
+
data: JSON.stringify(json),
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
// Clean up when the client disconnects
|
|
110
|
+
stream.onAbort(() => {
|
|
111
|
+
console.log("/events connection closed");
|
|
112
|
+
clearInterval(hb);
|
|
113
|
+
unsub();
|
|
114
|
+
});
|
|
115
|
+
// Keep the connection open indefinitely
|
|
116
|
+
await stream.sleep(1000 * 60 * 60 * 24);
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
app.post("/rpc", async (c) => {
|
|
120
|
+
// Get and validate the request body
|
|
121
|
+
const rawBody = await c.req.json();
|
|
122
|
+
// Validate using Zod schema
|
|
123
|
+
const parseResult = acp.clientOutgoingMessageSchema.safeParse(rawBody);
|
|
124
|
+
if (!parseResult.success) {
|
|
125
|
+
return c.json({
|
|
126
|
+
error: "Invalid ACP message",
|
|
127
|
+
details: parseResult.error.issues,
|
|
128
|
+
}, 400);
|
|
129
|
+
}
|
|
130
|
+
const body = parseResult.data;
|
|
131
|
+
// Ensure the request has an id
|
|
132
|
+
const id = "id" in body ? body.id : null;
|
|
133
|
+
const isRequest = id != null;
|
|
134
|
+
if (isRequest) {
|
|
135
|
+
// For requests, set up a listener before sending the request
|
|
136
|
+
const responseChannel = safeChannelName("response", String(id));
|
|
137
|
+
let responseResolver;
|
|
138
|
+
const responsePromise = new Promise((resolve) => {
|
|
139
|
+
responseResolver = resolve;
|
|
140
|
+
});
|
|
141
|
+
const unsub = await pg.listen(responseChannel, (payload) => {
|
|
142
|
+
const rawResponse = JSON.parse(payload);
|
|
143
|
+
responseResolver(rawResponse);
|
|
144
|
+
});
|
|
145
|
+
// Write NDJSON line into the ACP inbound stream
|
|
146
|
+
const writer = inbound.writable.getWriter();
|
|
147
|
+
await writer.write(encoder.encode(`${JSON.stringify(body)}\n`));
|
|
148
|
+
writer.releaseLock();
|
|
149
|
+
// Wait for response with 30 second timeout
|
|
150
|
+
const timeoutPromise = new Promise((_, reject) => setTimeout(() => reject(new Error("Request timeout")), 30000));
|
|
151
|
+
try {
|
|
152
|
+
const response = await Promise.race([responsePromise, timeoutPromise]);
|
|
153
|
+
return c.json(response);
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
// Clean up the listener whether we got a response or timed out
|
|
157
|
+
unsub();
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
// For notifications, just send and return success
|
|
162
|
+
const writer = inbound.writable.getWriter();
|
|
163
|
+
await writer.write(encoder.encode(`${JSON.stringify(body)}\n`));
|
|
164
|
+
writer.releaseLock();
|
|
165
|
+
return c.json({
|
|
166
|
+
success: true,
|
|
167
|
+
message: "Notification sent to agent",
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
const port = Number.parseInt(process.env.PORT || "3100", 10);
|
|
172
|
+
console.log(`Starting HTTP server on port ${port}...`);
|
|
173
|
+
Bun.serve({
|
|
174
|
+
fetch: app.fetch,
|
|
175
|
+
port,
|
|
176
|
+
});
|
|
177
|
+
console.log(`HTTP server listening on http://localhost:${port}`);
|
|
178
|
+
}
|
package/dist/bin.d.ts
ADDED
package/dist/bin.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export type AgentDefinition = z.infer<typeof AgentDefinitionSchema>;
|
|
3
|
+
/** MCP configuration types. */
|
|
4
|
+
export declare const McpConfigSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
5
|
+
name: z.ZodString;
|
|
6
|
+
transport: z.ZodLiteral<"stdio">;
|
|
7
|
+
command: z.ZodString;
|
|
8
|
+
args: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
9
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
10
|
+
name: z.ZodString;
|
|
11
|
+
transport: z.ZodLiteral<"http">;
|
|
12
|
+
url: z.ZodString;
|
|
13
|
+
}, z.core.$strip>]>;
|
|
14
|
+
/** Agent definition schema. */
|
|
15
|
+
export declare const AgentDefinitionSchema: z.ZodObject<{
|
|
16
|
+
systemPrompt: z.ZodNullable<z.ZodString>;
|
|
17
|
+
model: z.ZodString;
|
|
18
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
19
|
+
mcps: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
20
|
+
name: z.ZodString;
|
|
21
|
+
transport: z.ZodLiteral<"stdio">;
|
|
22
|
+
command: z.ZodString;
|
|
23
|
+
args: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
24
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
25
|
+
name: z.ZodString;
|
|
26
|
+
transport: z.ZodLiteral<"http">;
|
|
27
|
+
url: z.ZodString;
|
|
28
|
+
}, z.core.$strip>]>>>;
|
|
29
|
+
harnessImplementation: z.ZodOptional<z.ZodLiteral<"langchain">>;
|
|
30
|
+
}, z.core.$strip>;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
const McpBaseConfigSchema = z.object({
|
|
3
|
+
name: z.string(),
|
|
4
|
+
});
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for a stdio-transport MCP.
|
|
7
|
+
*
|
|
8
|
+
* This is only part of `StdioServerParameters` from
|
|
9
|
+
* @modelcontextprotocol/sdk/client/stdio.js.
|
|
10
|
+
*/
|
|
11
|
+
const McpStdioConfigSchema = McpBaseConfigSchema.extend({
|
|
12
|
+
transport: z.literal("stdio"),
|
|
13
|
+
command: z.string(),
|
|
14
|
+
args: z.array(z.string()).optional(),
|
|
15
|
+
});
|
|
16
|
+
/** Configuration for an Streaming HTTP MCP. */
|
|
17
|
+
const McpStreamableHttpConfigSchema = McpBaseConfigSchema.extend({
|
|
18
|
+
transport: z.literal("http"),
|
|
19
|
+
url: z.string(),
|
|
20
|
+
});
|
|
21
|
+
/** MCP configuration types. */
|
|
22
|
+
export const McpConfigSchema = z.union([
|
|
23
|
+
McpStdioConfigSchema,
|
|
24
|
+
McpStreamableHttpConfigSchema,
|
|
25
|
+
]);
|
|
26
|
+
/** Agent definition schema. */
|
|
27
|
+
export const AgentDefinitionSchema = z.object({
|
|
28
|
+
systemPrompt: z.string().nullable(),
|
|
29
|
+
model: z.string(),
|
|
30
|
+
tools: z.array(z.string()).optional(),
|
|
31
|
+
mcps: z.array(McpConfigSchema).optional(),
|
|
32
|
+
harnessImplementation: z.literal("langchain").optional(),
|
|
33
|
+
});
|
|
File without changes
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const todoItemSchema: z.ZodObject<{
|
|
3
|
+
content: z.ZodString;
|
|
4
|
+
status: z.ZodEnum<{
|
|
5
|
+
pending: "pending";
|
|
6
|
+
in_progress: "in_progress";
|
|
7
|
+
completed: "completed";
|
|
8
|
+
}>;
|
|
9
|
+
activeForm: z.ZodString;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
export declare const todoWrite: import("langchain").DynamicStructuredTool<z.ZodObject<{
|
|
12
|
+
todos: z.ZodArray<z.ZodObject<{
|
|
13
|
+
content: z.ZodString;
|
|
14
|
+
status: z.ZodEnum<{
|
|
15
|
+
pending: "pending";
|
|
16
|
+
in_progress: "in_progress";
|
|
17
|
+
completed: "completed";
|
|
18
|
+
}>;
|
|
19
|
+
activeForm: z.ZodString;
|
|
20
|
+
}, z.core.$strip>>;
|
|
21
|
+
}, z.core.$strip>, {
|
|
22
|
+
todos: {
|
|
23
|
+
content: string;
|
|
24
|
+
status: "pending" | "in_progress" | "completed";
|
|
25
|
+
activeForm: string;
|
|
26
|
+
}[];
|
|
27
|
+
}, {
|
|
28
|
+
todos: {
|
|
29
|
+
content: string;
|
|
30
|
+
status: "pending" | "in_progress" | "completed";
|
|
31
|
+
activeForm: string;
|
|
32
|
+
}[];
|
|
33
|
+
}, string>;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { tool } from "langchain";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export const todoItemSchema = z.object({
|
|
4
|
+
content: z.string().min(1),
|
|
5
|
+
status: z.enum(["pending", "in_progress", "completed"]),
|
|
6
|
+
activeForm: z.string().min(1),
|
|
7
|
+
});
|
|
8
|
+
export const todoWrite = tool(({ todos }) => {
|
|
9
|
+
// Simple implementation that confirms the todos were written
|
|
10
|
+
return `Successfully updated todo list with ${todos.length} items`;
|
|
11
|
+
}, {
|
|
12
|
+
name: "todo_write",
|
|
13
|
+
description: `Use this tool to create and manage a structured task list for your current coding session. This helps you track progress, organize complex tasks, and demonstrate thoroughness to the user.
|
|
14
|
+
It also helps the user understand the progress of the task and overall progress of their requests.
|
|
15
|
+
|
|
16
|
+
## When to Use This Tool
|
|
17
|
+
Use this tool proactively in these scenarios:
|
|
18
|
+
|
|
19
|
+
1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
|
|
20
|
+
2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
|
|
21
|
+
3. User explicitly requests todo list - When the user directly asks you to use the todo list
|
|
22
|
+
4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
|
|
23
|
+
5. After receiving new instructions - Immediately capture user requirements as todos
|
|
24
|
+
6. When you start working on a task - Mark it as in_progress BEFORE beginning work. Ideally you should only have one todo as in_progress at a time
|
|
25
|
+
7. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation
|
|
26
|
+
|
|
27
|
+
## When NOT to Use This Tool
|
|
28
|
+
|
|
29
|
+
Skip using this tool when:
|
|
30
|
+
1. There is only a single, straightforward task
|
|
31
|
+
2. The task is trivial and tracking it provides no organizational benefit
|
|
32
|
+
3. The task can be completed in less than 3 trivial steps
|
|
33
|
+
4. The task is purely conversational or informational
|
|
34
|
+
|
|
35
|
+
NOTE that you should not use this tool if there is only one trivial task to do. In this case you are better off just doing the task directly.
|
|
36
|
+
|
|
37
|
+
## Task States and Management
|
|
38
|
+
|
|
39
|
+
1. **Task States**: Use these states to track progress:
|
|
40
|
+
- pending: Task not yet started
|
|
41
|
+
- in_progress: Currently working on (limit to ONE task at a time)
|
|
42
|
+
- completed: Task finished successfully
|
|
43
|
+
|
|
44
|
+
**IMPORTANT**: Task descriptions must have two forms:
|
|
45
|
+
- content: The imperative form describing what needs to be done (e.g., "Run tests", "Build the project")
|
|
46
|
+
- activeForm: The present continuous form shown during execution (e.g., "Running tests", "Building the project")
|
|
47
|
+
|
|
48
|
+
2. **Task Management**:
|
|
49
|
+
- Update task status in real-time as you work
|
|
50
|
+
- Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
|
|
51
|
+
- Exactly ONE task must be in_progress at any time (not less, not more)
|
|
52
|
+
- Complete current tasks before starting new ones
|
|
53
|
+
- Remove tasks that are no longer relevant from the list entirely
|
|
54
|
+
|
|
55
|
+
3. **Task Completion Requirements**:
|
|
56
|
+
- ONLY mark a task as completed when you have FULLY accomplished it
|
|
57
|
+
- If you encounter errors, blockers, or cannot finish, keep the task as in_progress
|
|
58
|
+
- When blocked, create a new task describing what needs to be resolved
|
|
59
|
+
- Never mark a task as completed if:
|
|
60
|
+
- Tests are failing
|
|
61
|
+
- Implementation is partial
|
|
62
|
+
- You encountered unresolved errors
|
|
63
|
+
- You couldn't find necessary files or dependencies
|
|
64
|
+
|
|
65
|
+
4. **Task Breakdown**:
|
|
66
|
+
- Create specific, actionable items
|
|
67
|
+
- Break complex tasks into smaller, manageable steps
|
|
68
|
+
- Use clear, descriptive task names
|
|
69
|
+
- Always provide both forms:
|
|
70
|
+
- content: "Fix authentication bug"
|
|
71
|
+
- activeForm: "Fixing authentication bug"
|
|
72
|
+
|
|
73
|
+
When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.`,
|
|
74
|
+
schema: z.object({
|
|
75
|
+
todos: z.array(todoItemSchema),
|
|
76
|
+
}),
|
|
77
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ExaSearchResults } from "@langchain/exa";
|
|
2
|
+
import Exa from "exa-js";
|
|
3
|
+
let _webSearchInstance = null;
|
|
4
|
+
export function makeWebSearchTool() {
|
|
5
|
+
if (_webSearchInstance) {
|
|
6
|
+
return _webSearchInstance;
|
|
7
|
+
}
|
|
8
|
+
const apiKey = process.env.EXA_API_KEY;
|
|
9
|
+
if (!apiKey) {
|
|
10
|
+
throw new Error("EXA_API_KEY environment variable is required to use the web_search tool. " +
|
|
11
|
+
"Please set it to your Exa API key from https://exa.ai");
|
|
12
|
+
}
|
|
13
|
+
const client = new Exa(apiKey);
|
|
14
|
+
_webSearchInstance = new ExaSearchResults({
|
|
15
|
+
client,
|
|
16
|
+
searchArgs: {
|
|
17
|
+
numResults: 5,
|
|
18
|
+
type: "auto",
|
|
19
|
+
text: true,
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
return _webSearchInstance;
|
|
23
|
+
}
|
package/dist/example.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
import { makeHttpTransport, makeStdioTransport } from "./acp-server/index.js";
|
|
3
|
+
const exampleAgent = {
|
|
4
|
+
model: "claude-sonnet-4-5-20250929",
|
|
5
|
+
systemPrompt: "You are a helpful assistant.",
|
|
6
|
+
tools: ["todo_write", "get_weather", "web_search"],
|
|
7
|
+
};
|
|
8
|
+
// Parse transport type from command line argument
|
|
9
|
+
const transport = process.argv[2] || "stdio";
|
|
10
|
+
if (transport === "http") {
|
|
11
|
+
makeHttpTransport(exampleAgent);
|
|
12
|
+
}
|
|
13
|
+
else if (transport === "stdio") {
|
|
14
|
+
makeStdioTransport(exampleAgent);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.error(`Invalid transport: ${transport}`);
|
|
18
|
+
console.error("Usage: bun run example.ts [stdio|http]");
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { makeHttpTransport, makeStdioTransport } from "./acp-server";
|
|
2
|
+
const exampleAgent = {
|
|
3
|
+
model: "claude-sonnet-4-5-20250929",
|
|
4
|
+
systemPrompt: "You are a helpful assistant.",
|
|
5
|
+
tools: ["todo_write", "get_weather", "web_search"],
|
|
6
|
+
mcps: [],
|
|
7
|
+
};
|
|
8
|
+
// Parse transport type from command line argument
|
|
9
|
+
const transport = process.argv[2] || "stdio";
|
|
10
|
+
if (transport === "http") {
|
|
11
|
+
makeHttpTransport(exampleAgent);
|
|
12
|
+
}
|
|
13
|
+
else if (transport === "stdio") {
|
|
14
|
+
makeStdioTransport(exampleAgent);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
console.error(`Invalid transport: ${transport}`);
|
|
18
|
+
console.error("Usage: bun run index.ts [stdio|http]");
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { PromptRequest, PromptResponse, SessionNotification } from "@agentclientprotocol/sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
export declare const zAgentRunnerParams: z.ZodObject<{
|
|
4
|
+
systemPrompt: z.ZodNullable<z.ZodString>;
|
|
5
|
+
model: z.ZodString;
|
|
6
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodLiteral<"todo_write">, z.ZodLiteral<"get_weather">, z.ZodLiteral<"web_search">]>>>;
|
|
7
|
+
mcps: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
|
|
8
|
+
name: z.ZodString;
|
|
9
|
+
transport: z.ZodLiteral<"stdio">;
|
|
10
|
+
command: z.ZodString;
|
|
11
|
+
args: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
12
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
13
|
+
name: z.ZodString;
|
|
14
|
+
transport: z.ZodLiteral<"http">;
|
|
15
|
+
url: z.ZodString;
|
|
16
|
+
}, z.core.$strip>]>>>;
|
|
17
|
+
}, z.core.$strip>;
|
|
18
|
+
export type CreateAgentRunnerParams = z.infer<typeof zAgentRunnerParams>;
|
|
19
|
+
export type InvokeRequest = Omit<PromptRequest, "_meta">;
|
|
20
|
+
/** Describes an object that can run an agent definition */
|
|
21
|
+
export interface AgentRunner {
|
|
22
|
+
definition: CreateAgentRunnerParams;
|
|
23
|
+
invoke(req: InvokeRequest): AsyncGenerator<SessionNotification["update"], PromptResponse, undefined>;
|
|
24
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { McpConfigSchema } from "../definition";
|
|
3
|
+
import { zToolType } from "./tools";
|
|
4
|
+
export const zAgentRunnerParams = z.object({
|
|
5
|
+
systemPrompt: z.string().nullable(),
|
|
6
|
+
model: z.string(),
|
|
7
|
+
tools: z.array(zToolType).optional(),
|
|
8
|
+
mcps: z.array(McpConfigSchema).optional(),
|
|
9
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { zAgentRunnerParams } from "./agent-runner";
|
|
2
|
+
import { LangchainAgent } from "./langchain";
|
|
3
|
+
export const makeRunnerFromDefinition = (definition) => {
|
|
4
|
+
const agentRunnerParams = zAgentRunnerParams.safeParse(definition);
|
|
5
|
+
if (!agentRunnerParams.success) {
|
|
6
|
+
throw new Error(`Invalid agent definition: ${agentRunnerParams.error.message}`);
|
|
7
|
+
}
|
|
8
|
+
switch (definition.harnessImplementation) {
|
|
9
|
+
case undefined:
|
|
10
|
+
case "langchain": {
|
|
11
|
+
return new LangchainAgent(agentRunnerParams.data);
|
|
12
|
+
}
|
|
13
|
+
default: {
|
|
14
|
+
const _exhaustiveCheck = definition.harnessImplementation;
|
|
15
|
+
throw new Error(`Unsupported harness implementation: ${definition.harnessImplementation}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
};
|