@townco/agent 0.1.31 → 0.1.33
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 +4 -1
- package/dist/acp-server/adapter.js +96 -4
- package/dist/acp-server/cli.d.ts +3 -1
- package/dist/acp-server/cli.js +9 -5
- package/dist/acp-server/http.d.ts +1 -1
- package/dist/acp-server/http.js +14 -3
- package/dist/acp-server/session-storage.d.ts +71 -0
- package/dist/acp-server/session-storage.js +155 -0
- package/dist/bin.js +0 -0
- package/dist/definition/mcp.d.ts +0 -0
- package/dist/definition/mcp.js +0 -0
- package/dist/definition/tools/todo.d.ts +49 -0
- package/dist/definition/tools/todo.js +80 -0
- package/dist/definition/tools/web_search.d.ts +4 -0
- package/dist/definition/tools/web_search.js +26 -0
- package/dist/dev-agent/index.d.ts +2 -0
- package/dist/dev-agent/index.js +18 -0
- package/dist/example.d.ts +2 -0
- package/dist/example.js +19 -0
- package/dist/index.js +5 -1
- package/dist/runner/agent-runner.d.ts +6 -0
- package/dist/runner/index.d.ts +3 -1
- package/dist/runner/index.js +18 -14
- package/dist/runner/langchain/index.js +41 -11
- package/dist/runner/langchain/model-factory.d.ts +20 -0
- package/dist/runner/langchain/model-factory.js +113 -0
- package/dist/templates/index.js +9 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/logger.d.ts +39 -0
- package/dist/utils/logger.js +175 -0
- package/index.ts +6 -1
- package/package.json +8 -6
- package/templates/index.ts +9 -1
|
@@ -16,9 +16,12 @@ export declare class AgentAcpAdapter implements acp.Agent {
|
|
|
16
16
|
private connection;
|
|
17
17
|
private sessions;
|
|
18
18
|
private agent;
|
|
19
|
-
|
|
19
|
+
private storage;
|
|
20
|
+
private noSession;
|
|
21
|
+
constructor(agent: AgentRunner, connection: acp.AgentSideConnection, agentDir?: string, agentName?: string);
|
|
20
22
|
initialize(_params: acp.InitializeRequest): Promise<acp.InitializeResponse>;
|
|
21
23
|
newSession(params: acp.NewSessionRequest): Promise<acp.NewSessionResponse>;
|
|
24
|
+
loadSession(params: acp.LoadSessionRequest): Promise<acp.LoadSessionResponse>;
|
|
22
25
|
authenticate(_params: acp.AuthenticateRequest): Promise<acp.AuthenticateResponse | undefined>;
|
|
23
26
|
setSessionMode(_params: acp.SetSessionModeRequest): Promise<acp.SetSessionModeResponse>;
|
|
24
27
|
prompt(params: acp.PromptRequest): Promise<acp.PromptResponse>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as acp from "@agentclientprotocol/sdk";
|
|
2
|
+
import { SessionStorage } from "./session-storage.js";
|
|
2
3
|
/**
|
|
3
4
|
* ACP extension key for subagent mode indicator
|
|
4
5
|
* Following ACP extensibility pattern with namespaced key
|
|
@@ -9,16 +10,30 @@ export class AgentAcpAdapter {
|
|
|
9
10
|
connection;
|
|
10
11
|
sessions;
|
|
11
12
|
agent;
|
|
12
|
-
|
|
13
|
+
storage;
|
|
14
|
+
noSession;
|
|
15
|
+
constructor(agent, connection, agentDir, agentName) {
|
|
13
16
|
this.connection = connection;
|
|
14
17
|
this.sessions = new Map();
|
|
15
18
|
this.agent = agent;
|
|
19
|
+
this.noSession = process.env.TOWN_NO_SESSION === "true";
|
|
20
|
+
this.storage =
|
|
21
|
+
agentDir && agentName && !this.noSession
|
|
22
|
+
? new SessionStorage(agentDir, agentName)
|
|
23
|
+
: null;
|
|
24
|
+
console.log("[adapter] Initialized with:", {
|
|
25
|
+
agentDir,
|
|
26
|
+
agentName,
|
|
27
|
+
noSession: this.noSession,
|
|
28
|
+
hasStorage: this.storage !== null,
|
|
29
|
+
sessionStoragePath: this.storage ? `${agentDir}/.sessions` : null,
|
|
30
|
+
});
|
|
16
31
|
}
|
|
17
32
|
async initialize(_params) {
|
|
18
33
|
return {
|
|
19
34
|
protocolVersion: acp.PROTOCOL_VERSION,
|
|
20
35
|
agentCapabilities: {
|
|
21
|
-
loadSession:
|
|
36
|
+
loadSession: !this.noSession && this.storage !== null,
|
|
22
37
|
},
|
|
23
38
|
};
|
|
24
39
|
}
|
|
@@ -33,6 +48,37 @@ export class AgentAcpAdapter {
|
|
|
33
48
|
sessionId,
|
|
34
49
|
};
|
|
35
50
|
}
|
|
51
|
+
async loadSession(params) {
|
|
52
|
+
if (!this.storage) {
|
|
53
|
+
throw new Error("Session storage is not configured");
|
|
54
|
+
}
|
|
55
|
+
const storedSession = await this.storage.loadSession(params.sessionId);
|
|
56
|
+
if (!storedSession) {
|
|
57
|
+
throw new Error(`Session ${params.sessionId} not found`);
|
|
58
|
+
}
|
|
59
|
+
// Restore session in active sessions map
|
|
60
|
+
this.sessions.set(params.sessionId, {
|
|
61
|
+
pendingPrompt: null,
|
|
62
|
+
messages: storedSession.messages,
|
|
63
|
+
requestParams: { cwd: process.cwd(), mcpServers: [] },
|
|
64
|
+
});
|
|
65
|
+
// Replay conversation history to client
|
|
66
|
+
console.log(`[adapter] Replaying ${storedSession.messages.length} messages for session ${params.sessionId}`);
|
|
67
|
+
for (const msg of storedSession.messages) {
|
|
68
|
+
console.log(`[adapter] Replaying message: ${msg.role} - ${msg.content.substring(0, 50)}...`);
|
|
69
|
+
this.connection.sessionUpdate({
|
|
70
|
+
sessionId: params.sessionId,
|
|
71
|
+
update: {
|
|
72
|
+
sessionUpdate: msg.role === "user" ? "user_message_chunk" : "agent_message_chunk",
|
|
73
|
+
content: {
|
|
74
|
+
type: "text",
|
|
75
|
+
text: msg.content,
|
|
76
|
+
},
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
36
82
|
async authenticate(_params) {
|
|
37
83
|
// No auth needed - return empty response
|
|
38
84
|
return {};
|
|
@@ -57,25 +103,52 @@ export class AgentAcpAdapter {
|
|
|
57
103
|
session.pendingPrompt = new AbortController();
|
|
58
104
|
// Generate a unique messageId for this assistant response
|
|
59
105
|
const messageId = Math.random().toString(36).substring(2);
|
|
106
|
+
// Extract and store the user message
|
|
107
|
+
const userMessageContent = params.prompt
|
|
108
|
+
.filter((p) => p.type === "text")
|
|
109
|
+
.map((p) => p.text)
|
|
110
|
+
.join("\n");
|
|
111
|
+
// Only store messages if session persistence is enabled
|
|
112
|
+
if (!this.noSession) {
|
|
113
|
+
const userMessage = {
|
|
114
|
+
role: "user",
|
|
115
|
+
content: userMessageContent,
|
|
116
|
+
timestamp: new Date().toISOString(),
|
|
117
|
+
};
|
|
118
|
+
session.messages.push(userMessage);
|
|
119
|
+
}
|
|
120
|
+
// Accumulate assistant response content
|
|
121
|
+
let assistantContent = "";
|
|
60
122
|
try {
|
|
61
123
|
const invokeParams = {
|
|
62
124
|
prompt: params.prompt,
|
|
63
125
|
sessionId: params.sessionId,
|
|
64
126
|
messageId,
|
|
127
|
+
// Only pass session history if session persistence is enabled
|
|
128
|
+
sessionMessages: this.noSession ? [] : session.messages,
|
|
65
129
|
};
|
|
66
130
|
// Only add sessionMeta if it's defined
|
|
67
131
|
if (session.requestParams._meta) {
|
|
68
132
|
invokeParams.sessionMeta = session.requestParams._meta;
|
|
69
133
|
}
|
|
70
134
|
for await (const msg of this.agent.invoke(invokeParams)) {
|
|
71
|
-
//
|
|
135
|
+
// Accumulate assistant content from message chunks
|
|
72
136
|
if ("sessionUpdate" in msg &&
|
|
73
137
|
msg.sessionUpdate === "agent_message_chunk") {
|
|
138
|
+
if ("content" in msg &&
|
|
139
|
+
msg.content &&
|
|
140
|
+
typeof msg.content === "object") {
|
|
141
|
+
const content = msg.content;
|
|
142
|
+
if (content.type === "text" && typeof content.text === "string") {
|
|
143
|
+
assistantContent += content.text;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
// Debug: log if this chunk has tokenUsage in _meta
|
|
74
147
|
if ("_meta" in msg &&
|
|
75
148
|
msg._meta &&
|
|
76
149
|
typeof msg._meta === "object" &&
|
|
77
150
|
"tokenUsage" in msg._meta) {
|
|
78
|
-
console.
|
|
151
|
+
console.log("DEBUG adapter: sending agent_message_chunk with tokenUsage in _meta:", JSON.stringify(msg._meta.tokenUsage));
|
|
79
152
|
}
|
|
80
153
|
}
|
|
81
154
|
// The agent may emit extended types (like tool_output) that aren't in ACP SDK yet
|
|
@@ -92,6 +165,25 @@ export class AgentAcpAdapter {
|
|
|
92
165
|
}
|
|
93
166
|
throw err;
|
|
94
167
|
}
|
|
168
|
+
// Store the complete assistant response in session messages
|
|
169
|
+
// Only store if session persistence is enabled
|
|
170
|
+
if (!this.noSession && assistantContent.length > 0) {
|
|
171
|
+
const assistantMessage = {
|
|
172
|
+
role: "assistant",
|
|
173
|
+
content: assistantContent,
|
|
174
|
+
timestamp: new Date().toISOString(),
|
|
175
|
+
};
|
|
176
|
+
session.messages.push(assistantMessage);
|
|
177
|
+
}
|
|
178
|
+
// Save session to disk if storage is configured and session persistence is enabled
|
|
179
|
+
if (!this.noSession && this.storage) {
|
|
180
|
+
try {
|
|
181
|
+
await this.storage.saveSession(params.sessionId, session.messages);
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error(`Failed to save session ${params.sessionId}:`, error instanceof Error ? error.message : String(error));
|
|
185
|
+
}
|
|
186
|
+
}
|
|
95
187
|
session.pendingPrompt = null;
|
|
96
188
|
return {
|
|
97
189
|
stopReason: "end_turn",
|
package/dist/acp-server/cli.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
import type { AgentDefinition } from "../definition";
|
|
2
2
|
import { type AgentRunner } from "../runner";
|
|
3
|
-
export declare function makeStdioTransport(
|
|
3
|
+
export declare function makeStdioTransport(
|
|
4
|
+
agent: AgentRunner | AgentDefinition,
|
|
5
|
+
): void;
|
package/dist/acp-server/cli.js
CHANGED
|
@@ -3,9 +3,13 @@ import * as acp from "@agentclientprotocol/sdk";
|
|
|
3
3
|
import { makeRunnerFromDefinition } from "../runner";
|
|
4
4
|
import { AgentAcpAdapter } from "./adapter";
|
|
5
5
|
export function makeStdioTransport(agent) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
const agentRunner =
|
|
7
|
+
"definition" in agent ? agent : makeRunnerFromDefinition(agent);
|
|
8
|
+
const input = Writable.toWeb(process.stdout);
|
|
9
|
+
const output = Readable.toWeb(process.stdin);
|
|
10
|
+
const stream = acp.ndJsonStream(input, output);
|
|
11
|
+
new acp.AgentSideConnection(
|
|
12
|
+
(conn) => new AgentAcpAdapter(agentRunner, conn),
|
|
13
|
+
stream,
|
|
14
|
+
);
|
|
11
15
|
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { AgentDefinition } from "../definition";
|
|
2
2
|
import { type AgentRunner } from "../runner";
|
|
3
|
-
export declare function makeHttpTransport(agent: AgentRunner | AgentDefinition): void;
|
|
3
|
+
export declare function makeHttpTransport(agent: AgentRunner | AgentDefinition, agentDir?: string, agentName?: string): void;
|
package/dist/acp-server/http.js
CHANGED
|
@@ -47,12 +47,12 @@ function safeChannelName(prefix, id) {
|
|
|
47
47
|
const hash = createHash("sha256").update(id).digest("hex").slice(0, 16);
|
|
48
48
|
return `${prefix}_${hash}`;
|
|
49
49
|
}
|
|
50
|
-
export function makeHttpTransport(agent) {
|
|
50
|
+
export function makeHttpTransport(agent, agentDir, agentName) {
|
|
51
51
|
const inbound = new TransformStream();
|
|
52
52
|
const outbound = new TransformStream();
|
|
53
53
|
const bridge = acp.ndJsonStream(outbound.writable, inbound.readable);
|
|
54
54
|
const agentRunner = "definition" in agent ? agent : makeRunnerFromDefinition(agent);
|
|
55
|
-
new acp.AgentSideConnection((conn) => new AgentAcpAdapter(agentRunner, conn), bridge);
|
|
55
|
+
new acp.AgentSideConnection((conn) => new AgentAcpAdapter(agentRunner, conn, agentDir, agentName), bridge);
|
|
56
56
|
const app = new Hono();
|
|
57
57
|
// Track active SSE streams by sessionId for direct output delivery
|
|
58
58
|
const sseStreams = new Map();
|
|
@@ -330,12 +330,23 @@ export function makeHttpTransport(agent) {
|
|
|
330
330
|
details: parseResult.error.issues,
|
|
331
331
|
}, 400);
|
|
332
332
|
}
|
|
333
|
-
|
|
333
|
+
// Use rawBody instead of parseResult.data to preserve all fields
|
|
334
|
+
// The Zod validation above ensures the structure is valid, but we don't
|
|
335
|
+
// want it to strip fields due to union matching issues
|
|
336
|
+
const body = rawBody;
|
|
334
337
|
// Ensure the request has an id
|
|
335
338
|
const id = "id" in body ? body.id : null;
|
|
336
339
|
const isRequest = id != null;
|
|
337
340
|
const method = "method" in body ? body.method : "notification";
|
|
338
341
|
logger.debug("POST /rpc - Received request", { method, id, isRequest });
|
|
342
|
+
// Debug log for session/load requests
|
|
343
|
+
if (method === "session/load") {
|
|
344
|
+
logger.debug("POST /rpc - session/load request details", {
|
|
345
|
+
rawBody: JSON.stringify(rawBody),
|
|
346
|
+
parsedBody: JSON.stringify(body),
|
|
347
|
+
params: "params" in body ? body.params : "NO PARAMS",
|
|
348
|
+
});
|
|
349
|
+
}
|
|
339
350
|
if (isRequest) {
|
|
340
351
|
// For requests, set up a listener before sending the request
|
|
341
352
|
const responseChannel = safeChannelName("response", String(id));
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Session message format stored in files
|
|
3
|
+
*/
|
|
4
|
+
export interface SessionMessage {
|
|
5
|
+
role: "user" | "assistant";
|
|
6
|
+
content: string;
|
|
7
|
+
timestamp: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Session metadata
|
|
11
|
+
*/
|
|
12
|
+
export interface SessionMetadata {
|
|
13
|
+
createdAt: string;
|
|
14
|
+
updatedAt: string;
|
|
15
|
+
agentName: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Complete session data stored in JSON files
|
|
19
|
+
*/
|
|
20
|
+
export interface StoredSession {
|
|
21
|
+
sessionId: string;
|
|
22
|
+
messages: SessionMessage[];
|
|
23
|
+
metadata: SessionMetadata;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* File-based session storage
|
|
27
|
+
* Stores sessions in agents/<agent-name>/.sessions/<session_id>.json
|
|
28
|
+
*/
|
|
29
|
+
export declare class SessionStorage {
|
|
30
|
+
private sessionsDir;
|
|
31
|
+
private agentName;
|
|
32
|
+
/**
|
|
33
|
+
* Create a new SessionStorage instance
|
|
34
|
+
* @param agentDir - Path to the agent directory (e.g., agents/my-agent)
|
|
35
|
+
* @param agentName - Name of the agent (used in metadata)
|
|
36
|
+
*/
|
|
37
|
+
constructor(agentDir: string, agentName: string);
|
|
38
|
+
/**
|
|
39
|
+
* Ensure the .sessions directory exists
|
|
40
|
+
*/
|
|
41
|
+
private ensureSessionsDir;
|
|
42
|
+
/**
|
|
43
|
+
* Get the file path for a session
|
|
44
|
+
*/
|
|
45
|
+
private getSessionPath;
|
|
46
|
+
/**
|
|
47
|
+
* Save a session to disk
|
|
48
|
+
* Uses atomic write (write to temp file, then rename)
|
|
49
|
+
*/
|
|
50
|
+
saveSession(sessionId: string, messages: SessionMessage[]): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Load a session from disk
|
|
53
|
+
*/
|
|
54
|
+
loadSession(sessionId: string): Promise<StoredSession | null>;
|
|
55
|
+
/**
|
|
56
|
+
* Synchronous session loading (for internal use)
|
|
57
|
+
*/
|
|
58
|
+
private loadSessionSync;
|
|
59
|
+
/**
|
|
60
|
+
* Check if a session exists
|
|
61
|
+
*/
|
|
62
|
+
sessionExists(sessionId: string): boolean;
|
|
63
|
+
/**
|
|
64
|
+
* Delete a session
|
|
65
|
+
*/
|
|
66
|
+
deleteSession(sessionId: string): Promise<boolean>;
|
|
67
|
+
/**
|
|
68
|
+
* List all session IDs
|
|
69
|
+
*/
|
|
70
|
+
listSessions(): Promise<string[]>;
|
|
71
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
/**
|
|
5
|
+
* Zod schema for validating session files
|
|
6
|
+
*/
|
|
7
|
+
const sessionMessageSchema = z.object({
|
|
8
|
+
role: z.enum(["user", "assistant"]),
|
|
9
|
+
content: z.string(),
|
|
10
|
+
timestamp: z.string(),
|
|
11
|
+
});
|
|
12
|
+
const sessionMetadataSchema = z.object({
|
|
13
|
+
createdAt: z.string(),
|
|
14
|
+
updatedAt: z.string(),
|
|
15
|
+
agentName: z.string(),
|
|
16
|
+
});
|
|
17
|
+
const storedSessionSchema = z.object({
|
|
18
|
+
sessionId: z.string(),
|
|
19
|
+
messages: z.array(sessionMessageSchema),
|
|
20
|
+
metadata: sessionMetadataSchema,
|
|
21
|
+
});
|
|
22
|
+
/**
|
|
23
|
+
* File-based session storage
|
|
24
|
+
* Stores sessions in agents/<agent-name>/.sessions/<session_id>.json
|
|
25
|
+
*/
|
|
26
|
+
export class SessionStorage {
|
|
27
|
+
sessionsDir;
|
|
28
|
+
agentName;
|
|
29
|
+
/**
|
|
30
|
+
* Create a new SessionStorage instance
|
|
31
|
+
* @param agentDir - Path to the agent directory (e.g., agents/my-agent)
|
|
32
|
+
* @param agentName - Name of the agent (used in metadata)
|
|
33
|
+
*/
|
|
34
|
+
constructor(agentDir, agentName) {
|
|
35
|
+
this.sessionsDir = join(agentDir, ".sessions");
|
|
36
|
+
this.agentName = agentName;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Ensure the .sessions directory exists
|
|
40
|
+
*/
|
|
41
|
+
ensureSessionsDir() {
|
|
42
|
+
if (!existsSync(this.sessionsDir)) {
|
|
43
|
+
mkdirSync(this.sessionsDir, { recursive: true });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get the file path for a session
|
|
48
|
+
*/
|
|
49
|
+
getSessionPath(sessionId) {
|
|
50
|
+
return join(this.sessionsDir, `${sessionId}.json`);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Save a session to disk
|
|
54
|
+
* Uses atomic write (write to temp file, then rename)
|
|
55
|
+
*/
|
|
56
|
+
async saveSession(sessionId, messages) {
|
|
57
|
+
this.ensureSessionsDir();
|
|
58
|
+
const sessionPath = this.getSessionPath(sessionId);
|
|
59
|
+
const tempPath = `${sessionPath}.tmp`;
|
|
60
|
+
const now = new Date().toISOString();
|
|
61
|
+
const existingSession = this.sessionExists(sessionId)
|
|
62
|
+
? this.loadSessionSync(sessionId)
|
|
63
|
+
: null;
|
|
64
|
+
const session = {
|
|
65
|
+
sessionId,
|
|
66
|
+
messages,
|
|
67
|
+
metadata: {
|
|
68
|
+
createdAt: existingSession?.metadata.createdAt || now,
|
|
69
|
+
updatedAt: now,
|
|
70
|
+
agentName: this.agentName,
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
try {
|
|
74
|
+
// Write to temp file
|
|
75
|
+
writeFileSync(tempPath, JSON.stringify(session, null, 2), "utf-8");
|
|
76
|
+
// Atomic rename
|
|
77
|
+
if (existsSync(sessionPath)) {
|
|
78
|
+
unlinkSync(sessionPath);
|
|
79
|
+
}
|
|
80
|
+
writeFileSync(sessionPath, readFileSync(tempPath, "utf-8"), "utf-8");
|
|
81
|
+
unlinkSync(tempPath);
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
// Clean up temp file on error
|
|
85
|
+
if (existsSync(tempPath)) {
|
|
86
|
+
unlinkSync(tempPath);
|
|
87
|
+
}
|
|
88
|
+
throw new Error(`Failed to save session ${sessionId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Load a session from disk
|
|
93
|
+
*/
|
|
94
|
+
async loadSession(sessionId) {
|
|
95
|
+
return this.loadSessionSync(sessionId);
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Synchronous session loading (for internal use)
|
|
99
|
+
*/
|
|
100
|
+
loadSessionSync(sessionId) {
|
|
101
|
+
const sessionPath = this.getSessionPath(sessionId);
|
|
102
|
+
if (!existsSync(sessionPath)) {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
try {
|
|
106
|
+
const content = readFileSync(sessionPath, "utf-8");
|
|
107
|
+
const parsed = JSON.parse(content);
|
|
108
|
+
// Validate with zod
|
|
109
|
+
const validated = storedSessionSchema.parse(parsed);
|
|
110
|
+
return validated;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
throw new Error(`Failed to load session ${sessionId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Check if a session exists
|
|
118
|
+
*/
|
|
119
|
+
sessionExists(sessionId) {
|
|
120
|
+
return existsSync(this.getSessionPath(sessionId));
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Delete a session
|
|
124
|
+
*/
|
|
125
|
+
async deleteSession(sessionId) {
|
|
126
|
+
const sessionPath = this.getSessionPath(sessionId);
|
|
127
|
+
if (!existsSync(sessionPath)) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
try {
|
|
131
|
+
unlinkSync(sessionPath);
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
catch (error) {
|
|
135
|
+
throw new Error(`Failed to delete session ${sessionId}: ${error instanceof Error ? error.message : String(error)}`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* List all session IDs
|
|
140
|
+
*/
|
|
141
|
+
async listSessions() {
|
|
142
|
+
if (!existsSync(this.sessionsDir)) {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const files = readdirSync(this.sessionsDir);
|
|
147
|
+
return files
|
|
148
|
+
.filter((file) => file.endsWith(".json") && !file.endsWith(".tmp"))
|
|
149
|
+
.map((file) => file.replace(".json", ""));
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
throw new Error(`Failed to list sessions: ${error instanceof Error ? error.message : String(error)}`);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
package/dist/bin.js
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const todoItemSchema: z.ZodObject<
|
|
3
|
+
{
|
|
4
|
+
content: z.ZodString;
|
|
5
|
+
status: z.ZodEnum<{
|
|
6
|
+
pending: "pending";
|
|
7
|
+
in_progress: "in_progress";
|
|
8
|
+
completed: "completed";
|
|
9
|
+
}>;
|
|
10
|
+
activeForm: z.ZodString;
|
|
11
|
+
},
|
|
12
|
+
z.core.$strip
|
|
13
|
+
>;
|
|
14
|
+
export declare const todoWrite: import("langchain").DynamicStructuredTool<
|
|
15
|
+
z.ZodObject<
|
|
16
|
+
{
|
|
17
|
+
todos: z.ZodArray<
|
|
18
|
+
z.ZodObject<
|
|
19
|
+
{
|
|
20
|
+
content: z.ZodString;
|
|
21
|
+
status: z.ZodEnum<{
|
|
22
|
+
pending: "pending";
|
|
23
|
+
in_progress: "in_progress";
|
|
24
|
+
completed: "completed";
|
|
25
|
+
}>;
|
|
26
|
+
activeForm: z.ZodString;
|
|
27
|
+
},
|
|
28
|
+
z.core.$strip
|
|
29
|
+
>
|
|
30
|
+
>;
|
|
31
|
+
},
|
|
32
|
+
z.core.$strip
|
|
33
|
+
>,
|
|
34
|
+
{
|
|
35
|
+
todos: {
|
|
36
|
+
content: string;
|
|
37
|
+
status: "pending" | "in_progress" | "completed";
|
|
38
|
+
activeForm: string;
|
|
39
|
+
}[];
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
todos: {
|
|
43
|
+
content: string;
|
|
44
|
+
status: "pending" | "in_progress" | "completed";
|
|
45
|
+
activeForm: string;
|
|
46
|
+
}[];
|
|
47
|
+
},
|
|
48
|
+
string
|
|
49
|
+
>;
|
|
@@ -0,0 +1,80 @@
|
|
|
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(
|
|
9
|
+
({ todos }) => {
|
|
10
|
+
// Simple implementation that confirms the todos were written
|
|
11
|
+
return `Successfully updated todo list with ${todos.length} items`;
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
name: "todo_write",
|
|
15
|
+
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.
|
|
16
|
+
It also helps the user understand the progress of the task and overall progress of their requests.
|
|
17
|
+
|
|
18
|
+
## When to Use This Tool
|
|
19
|
+
Use this tool proactively in these scenarios:
|
|
20
|
+
|
|
21
|
+
1. Complex multi-step tasks - When a task requires 3 or more distinct steps or actions
|
|
22
|
+
2. Non-trivial and complex tasks - Tasks that require careful planning or multiple operations
|
|
23
|
+
3. User explicitly requests todo list - When the user directly asks you to use the todo list
|
|
24
|
+
4. User provides multiple tasks - When users provide a list of things to be done (numbered or comma-separated)
|
|
25
|
+
5. After receiving new instructions - Immediately capture user requirements as todos
|
|
26
|
+
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
|
|
27
|
+
7. After completing a task - Mark it as completed and add any new follow-up tasks discovered during implementation
|
|
28
|
+
|
|
29
|
+
## When NOT to Use This Tool
|
|
30
|
+
|
|
31
|
+
Skip using this tool when:
|
|
32
|
+
1. There is only a single, straightforward task
|
|
33
|
+
2. The task is trivial and tracking it provides no organizational benefit
|
|
34
|
+
3. The task can be completed in less than 3 trivial steps
|
|
35
|
+
4. The task is purely conversational or informational
|
|
36
|
+
|
|
37
|
+
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.
|
|
38
|
+
|
|
39
|
+
## Task States and Management
|
|
40
|
+
|
|
41
|
+
1. **Task States**: Use these states to track progress:
|
|
42
|
+
- pending: Task not yet started
|
|
43
|
+
- in_progress: Currently working on (limit to ONE task at a time)
|
|
44
|
+
- completed: Task finished successfully
|
|
45
|
+
|
|
46
|
+
**IMPORTANT**: Task descriptions must have two forms:
|
|
47
|
+
- content: The imperative form describing what needs to be done (e.g., "Run tests", "Build the project")
|
|
48
|
+
- activeForm: The present continuous form shown during execution (e.g., "Running tests", "Building the project")
|
|
49
|
+
|
|
50
|
+
2. **Task Management**:
|
|
51
|
+
- Update task status in real-time as you work
|
|
52
|
+
- Mark tasks complete IMMEDIATELY after finishing (don't batch completions)
|
|
53
|
+
- Exactly ONE task must be in_progress at any time (not less, not more)
|
|
54
|
+
- Complete current tasks before starting new ones
|
|
55
|
+
- Remove tasks that are no longer relevant from the list entirely
|
|
56
|
+
|
|
57
|
+
3. **Task Completion Requirements**:
|
|
58
|
+
- ONLY mark a task as completed when you have FULLY accomplished it
|
|
59
|
+
- If you encounter errors, blockers, or cannot finish, keep the task as in_progress
|
|
60
|
+
- When blocked, create a new task describing what needs to be resolved
|
|
61
|
+
- Never mark a task as completed if:
|
|
62
|
+
- Tests are failing
|
|
63
|
+
- Implementation is partial
|
|
64
|
+
- You encountered unresolved errors
|
|
65
|
+
- You couldn't find necessary files or dependencies
|
|
66
|
+
|
|
67
|
+
4. **Task Breakdown**:
|
|
68
|
+
- Create specific, actionable items
|
|
69
|
+
- Break complex tasks into smaller, manageable steps
|
|
70
|
+
- Use clear, descriptive task names
|
|
71
|
+
- Always provide both forms:
|
|
72
|
+
- content: "Fix authentication bug"
|
|
73
|
+
- activeForm: "Fixing authentication bug"
|
|
74
|
+
|
|
75
|
+
When in doubt, use this tool. Being proactive with task management demonstrates attentiveness and ensures you complete all requirements successfully.`,
|
|
76
|
+
schema: z.object({
|
|
77
|
+
todos: z.array(todoItemSchema),
|
|
78
|
+
}),
|
|
79
|
+
},
|
|
80
|
+
);
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ExaSearchResults } from "@langchain/exa";
|
|
2
|
+
import Exa from "exa-js";
|
|
3
|
+
|
|
4
|
+
let _webSearchInstance = null;
|
|
5
|
+
export function makeWebSearchTool() {
|
|
6
|
+
if (_webSearchInstance) {
|
|
7
|
+
return _webSearchInstance;
|
|
8
|
+
}
|
|
9
|
+
const apiKey = process.env.EXA_API_KEY;
|
|
10
|
+
if (!apiKey) {
|
|
11
|
+
throw new Error(
|
|
12
|
+
"EXA_API_KEY environment variable is required to use the web_search tool. " +
|
|
13
|
+
"Please set it to your Exa API key from https://exa.ai",
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
const client = new Exa(apiKey);
|
|
17
|
+
_webSearchInstance = new ExaSearchResults({
|
|
18
|
+
client,
|
|
19
|
+
searchArgs: {
|
|
20
|
+
numResults: 5,
|
|
21
|
+
type: "auto",
|
|
22
|
+
text: true,
|
|
23
|
+
},
|
|
24
|
+
});
|
|
25
|
+
return _webSearchInstance;
|
|
26
|
+
}
|
|
@@ -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
|
+
}
|