@townco/agent 0.1.19 → 0.1.21

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.
@@ -1,77 +1,81 @@
1
1
  import * as acp from "@agentclientprotocol/sdk";
2
2
  /** Adapts an Agent to speak the ACP protocol */
3
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(
43
- `Session ${params.sessionId} not found, creating new session`,
44
- );
45
- session = {
46
- pendingPrompt: null,
47
- messages: [],
48
- };
49
- this.sessions.set(params.sessionId, session);
50
- }
51
- session.pendingPrompt?.abort();
52
- session.pendingPrompt = new AbortController();
53
- try {
54
- for await (const msg of this.agent.invoke({
55
- prompt: params.prompt,
56
- sessionId: params.sessionId,
57
- })) {
58
- this.connection.sessionUpdate({
59
- sessionId: params.sessionId,
60
- update: msg,
61
- });
62
- }
63
- } catch (err) {
64
- if (session.pendingPrompt.signal.aborted) {
65
- return { stopReason: "cancelled" };
66
- }
67
- throw err;
68
- }
69
- session.pendingPrompt = null;
70
- return {
71
- stopReason: "end_turn",
72
- };
73
- }
74
- async cancel(params) {
75
- this.sessions.get(params.sessionId)?.pendingPrompt?.abort();
76
- }
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
+ // Generate a unique messageId for this assistant response
52
+ const messageId = Math.random().toString(36).substring(2);
53
+ try {
54
+ for await (const msg of this.agent.invoke({
55
+ prompt: params.prompt,
56
+ sessionId: params.sessionId,
57
+ messageId,
58
+ })) {
59
+ // The agent may emit extended types (like tool_output) that aren't in ACP SDK yet
60
+ // The http transport will handle routing these appropriately
61
+ this.connection.sessionUpdate({
62
+ sessionId: params.sessionId,
63
+ update: msg,
64
+ });
65
+ }
66
+ }
67
+ catch (err) {
68
+ if (session.pendingPrompt.signal.aborted) {
69
+ return { stopReason: "cancelled" };
70
+ }
71
+ throw err;
72
+ }
73
+ session.pendingPrompt = null;
74
+ return {
75
+ stopReason: "end_turn",
76
+ };
77
+ }
78
+ async cancel(params) {
79
+ this.sessions.get(params.sessionId)?.pendingPrompt?.abort();
80
+ }
77
81
  }