@townco/ui 0.1.6 → 0.1.8

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.
Files changed (255) hide show
  1. package/README.md +11 -11
  2. package/dist/core/hooks/index.d.ts +1 -0
  3. package/dist/core/hooks/index.js +1 -0
  4. package/dist/core/hooks/use-chat-input.d.ts +17 -17
  5. package/dist/core/hooks/use-chat-input.js +55 -64
  6. package/dist/core/hooks/use-chat-messages.d.ts +11 -11
  7. package/dist/core/hooks/use-chat-messages.js +114 -121
  8. package/dist/core/hooks/use-chat-session.d.ts +5 -5
  9. package/dist/core/hooks/use-chat-session.js +80 -78
  10. package/dist/core/hooks/use-media-query.d.ts +5 -5
  11. package/dist/core/hooks/use-media-query.js +42 -38
  12. package/dist/core/index.d.ts +1 -1
  13. package/dist/core/index.js +1 -1
  14. package/dist/core/schemas/chat.d.ts +56 -83
  15. package/dist/core/schemas/chat.js +25 -27
  16. package/dist/core/store/chat-store.d.ts +22 -28
  17. package/dist/core/store/chat-store.js +50 -59
  18. package/dist/gui/components/Button.d.ts +7 -23
  19. package/dist/gui/components/Button.js +27 -40
  20. package/dist/gui/components/Card.d.ts +7 -26
  21. package/dist/gui/components/Card.js +8 -54
  22. package/dist/gui/components/ChatHeader.d.ts +31 -58
  23. package/dist/gui/components/ChatHeader.js +68 -171
  24. package/dist/gui/components/ChatInput.d.ts +54 -58
  25. package/dist/gui/components/ChatInput.js +207 -194
  26. package/dist/gui/components/ChatInputCommandMenu.d.ts +20 -0
  27. package/dist/gui/components/ChatInputCommandMenu.js +62 -0
  28. package/dist/gui/components/ChatLayout.d.ts +41 -71
  29. package/dist/gui/components/ChatLayout.js +87 -214
  30. package/dist/gui/components/ChatPanelTabContent.d.ts +9 -18
  31. package/dist/gui/components/ChatPanelTabContent.js +10 -88
  32. package/dist/gui/components/ChatSecondaryPanel.d.ts +11 -14
  33. package/dist/gui/components/ChatSecondaryPanel.js +38 -115
  34. package/dist/gui/components/ChatSidebar.d.ts +13 -26
  35. package/dist/gui/components/ChatSidebar.js +14 -48
  36. package/dist/gui/components/ChatStatus.d.ts +2 -4
  37. package/dist/gui/components/ChatStatus.js +34 -45
  38. package/dist/gui/components/Conversation.d.ts +14 -17
  39. package/dist/gui/components/Conversation.js +83 -143
  40. package/dist/gui/components/Dialog.d.ts +11 -57
  41. package/dist/gui/components/Dialog.js +8 -84
  42. package/dist/gui/components/DropdownMenu.d.ts +20 -101
  43. package/dist/gui/components/DropdownMenu.js +14 -161
  44. package/dist/gui/components/HeightTransition.d.ts +7 -12
  45. package/dist/gui/components/HeightTransition.js +77 -88
  46. package/dist/gui/components/Input.d.ts +6 -13
  47. package/dist/gui/components/Input.js +16 -27
  48. package/dist/gui/components/Label.d.ts +1 -7
  49. package/dist/gui/components/Label.js +2 -12
  50. package/dist/gui/components/MarkdownRenderer.d.ts +4 -6
  51. package/dist/gui/components/MarkdownRenderer.js +81 -178
  52. package/dist/gui/components/Message.d.ts +22 -25
  53. package/dist/gui/components/Message.js +97 -44
  54. package/dist/gui/components/MessageContent.d.ts +22 -29
  55. package/dist/gui/components/MessageContent.js +85 -157
  56. package/dist/gui/components/Reasoning.d.ts +24 -30
  57. package/dist/gui/components/Reasoning.js +60 -187
  58. package/dist/gui/components/Response.d.ts +9 -11
  59. package/dist/gui/components/Response.js +90 -229
  60. package/dist/gui/components/Select.d.ts +10 -69
  61. package/dist/gui/components/Select.js +12 -118
  62. package/dist/gui/components/Sonner.d.ts +1 -3
  63. package/dist/gui/components/Sonner.js +18 -29
  64. package/dist/gui/components/Tabs.d.ts +4 -24
  65. package/dist/gui/components/Tabs.js +4 -32
  66. package/dist/gui/components/Task.d.ts +24 -28
  67. package/dist/gui/components/Task.js +31 -164
  68. package/dist/gui/components/Textarea.d.ts +7 -15
  69. package/dist/gui/components/Textarea.js +46 -63
  70. package/dist/gui/components/ThinkingBlock.d.ts +10 -20
  71. package/dist/gui/components/ThinkingBlock.js +35 -134
  72. package/dist/gui/components/TodoList.d.ts +10 -12
  73. package/dist/gui/components/TodoList.js +7 -22
  74. package/dist/gui/components/TodoListItem.d.ts +6 -9
  75. package/dist/gui/components/TodoListItem.js +4 -18
  76. package/dist/gui/components/index.d.ts +16 -59
  77. package/dist/gui/components/index.js +18 -42
  78. package/dist/gui/lib/utils.js +1 -1
  79. package/dist/index.d.ts +1 -1
  80. package/dist/index.js +1 -1
  81. package/dist/index.test.js +1 -0
  82. package/dist/sdk/client/acp-client.d.ts +76 -88
  83. package/dist/sdk/client/acp-client.js +217 -215
  84. package/dist/sdk/index.d.ts +1 -1
  85. package/dist/sdk/index.js +1 -1
  86. package/dist/sdk/schemas/agent.d.ts +64 -111
  87. package/dist/sdk/schemas/agent.js +24 -24
  88. package/dist/sdk/schemas/message.d.ts +147 -245
  89. package/dist/sdk/schemas/message.js +40 -40
  90. package/dist/sdk/schemas/session.d.ts +135 -219
  91. package/dist/sdk/schemas/session.js +27 -27
  92. package/dist/sdk/transports/http.d.ts +55 -55
  93. package/dist/sdk/transports/http.js +469 -472
  94. package/dist/sdk/transports/stdio.d.ts +20 -20
  95. package/dist/sdk/transports/stdio.js +286 -289
  96. package/dist/sdk/transports/types.d.ts +42 -42
  97. package/dist/sdk/transports/websocket.d.ts +12 -12
  98. package/dist/sdk/transports/websocket.js +46 -52
  99. package/dist/tui/components/ChatView.d.ts +2 -4
  100. package/dist/tui/components/ChatView.js +18 -51
  101. package/dist/tui/components/GameOfLife.js +35 -64
  102. package/dist/tui/components/InputBox.d.ts +11 -18
  103. package/dist/tui/components/InputBox.js +10 -70
  104. package/dist/tui/components/MessageList.d.ts +2 -4
  105. package/dist/tui/components/MessageList.js +10 -37
  106. package/dist/tui/components/MultiSelect.d.ts +10 -15
  107. package/dist/tui/components/MultiSelect.js +73 -116
  108. package/dist/tui/components/ReadlineInput.d.ts +6 -12
  109. package/dist/tui/components/ReadlineInput.js +237 -252
  110. package/dist/tui/components/SingleSelect.d.ts +9 -15
  111. package/dist/tui/components/SingleSelect.js +43 -84
  112. package/dist/tui/components/StatusBar.d.ts +6 -11
  113. package/dist/tui/components/StatusBar.js +67 -102
  114. package/dist/tui/index.d.ts +1 -1
  115. package/dist/tui/index.js +1 -1
  116. package/package.json +6 -4
  117. package/src/styles/global.css +2 -0
  118. package/dist/core/hooks/index.d.ts.map +0 -1
  119. package/dist/core/hooks/index.js.map +0 -1
  120. package/dist/core/hooks/use-chat-input.d.ts.map +0 -1
  121. package/dist/core/hooks/use-chat-input.js.map +0 -1
  122. package/dist/core/hooks/use-chat-messages.d.ts.map +0 -1
  123. package/dist/core/hooks/use-chat-messages.js.map +0 -1
  124. package/dist/core/hooks/use-chat-session.d.ts.map +0 -1
  125. package/dist/core/hooks/use-chat-session.js.map +0 -1
  126. package/dist/core/index.d.ts.map +0 -1
  127. package/dist/core/index.js.map +0 -1
  128. package/dist/core/schemas/chat.d.ts.map +0 -1
  129. package/dist/core/schemas/chat.js.map +0 -1
  130. package/dist/core/schemas/index.d.ts.map +0 -1
  131. package/dist/core/schemas/index.js.map +0 -1
  132. package/dist/core/store/chat-store.d.ts.map +0 -1
  133. package/dist/core/store/chat-store.js.map +0 -1
  134. package/dist/gui/components/Button.d.ts.map +0 -1
  135. package/dist/gui/components/Button.js.map +0 -1
  136. package/dist/gui/components/Card.d.ts.map +0 -1
  137. package/dist/gui/components/Card.js.map +0 -1
  138. package/dist/gui/components/ChatInput.d.ts.map +0 -1
  139. package/dist/gui/components/ChatInput.js.map +0 -1
  140. package/dist/gui/components/ChatInterface.d.ts +0 -12
  141. package/dist/gui/components/ChatInterface.d.ts.map +0 -1
  142. package/dist/gui/components/ChatInterface.js +0 -204
  143. package/dist/gui/components/ChatInterface.js.map +0 -1
  144. package/dist/gui/components/ChatPreview.d.ts +0 -12
  145. package/dist/gui/components/ChatPreview.d.ts.map +0 -1
  146. package/dist/gui/components/ChatPreview.js +0 -214
  147. package/dist/gui/components/ChatPreview.js.map +0 -1
  148. package/dist/gui/components/ChatSecondaryPanel.d.ts.map +0 -1
  149. package/dist/gui/components/ChatSecondaryPanel.js.map +0 -1
  150. package/dist/gui/components/ChatStatus.d.ts.map +0 -1
  151. package/dist/gui/components/ChatStatus.js.map +0 -1
  152. package/dist/gui/components/ChatView.d.ts +0 -8
  153. package/dist/gui/components/ChatView.d.ts.map +0 -1
  154. package/dist/gui/components/ChatView.js +0 -42
  155. package/dist/gui/components/ChatView.js.map +0 -1
  156. package/dist/gui/components/ConfigPanel.d.ts +0 -20
  157. package/dist/gui/components/ConfigPanel.d.ts.map +0 -1
  158. package/dist/gui/components/ConfigPanel.js +0 -225
  159. package/dist/gui/components/ConfigPanel.js.map +0 -1
  160. package/dist/gui/components/Conversation.d.ts.map +0 -1
  161. package/dist/gui/components/Conversation.js.map +0 -1
  162. package/dist/gui/components/Dialog.d.ts.map +0 -1
  163. package/dist/gui/components/Dialog.js.map +0 -1
  164. package/dist/gui/components/HeightTransition.d.ts.map +0 -1
  165. package/dist/gui/components/HeightTransition.js.map +0 -1
  166. package/dist/gui/components/Input.d.ts.map +0 -1
  167. package/dist/gui/components/Input.js.map +0 -1
  168. package/dist/gui/components/InputBox.d.ts +0 -21
  169. package/dist/gui/components/InputBox.d.ts.map +0 -1
  170. package/dist/gui/components/InputBox.js +0 -90
  171. package/dist/gui/components/InputBox.js.map +0 -1
  172. package/dist/gui/components/Label.d.ts.map +0 -1
  173. package/dist/gui/components/Label.js.map +0 -1
  174. package/dist/gui/components/MarkdownRenderer.d.ts.map +0 -1
  175. package/dist/gui/components/MarkdownRenderer.js.map +0 -1
  176. package/dist/gui/components/Message.d.ts.map +0 -1
  177. package/dist/gui/components/Message.js.map +0 -1
  178. package/dist/gui/components/MessageContent.d.ts.map +0 -1
  179. package/dist/gui/components/MessageContent.js.map +0 -1
  180. package/dist/gui/components/MessageList.d.ts.map +0 -1
  181. package/dist/gui/components/MessageList.js.map +0 -1
  182. package/dist/gui/components/PlaygroundLayout.d.ts +0 -14
  183. package/dist/gui/components/PlaygroundLayout.d.ts.map +0 -1
  184. package/dist/gui/components/PlaygroundLayout.js +0 -49
  185. package/dist/gui/components/PlaygroundLayout.js.map +0 -1
  186. package/dist/gui/components/Reasoning.d.ts.map +0 -1
  187. package/dist/gui/components/Reasoning.js.map +0 -1
  188. package/dist/gui/components/Response.d.ts.map +0 -1
  189. package/dist/gui/components/Response.js.map +0 -1
  190. package/dist/gui/components/Select.d.ts.map +0 -1
  191. package/dist/gui/components/Select.js.map +0 -1
  192. package/dist/gui/components/StatusBar.d.ts +0 -12
  193. package/dist/gui/components/StatusBar.d.ts.map +0 -1
  194. package/dist/gui/components/StatusBar.js +0 -58
  195. package/dist/gui/components/StatusBar.js.map +0 -1
  196. package/dist/gui/components/Tabs.d.ts.map +0 -1
  197. package/dist/gui/components/Tabs.js.map +0 -1
  198. package/dist/gui/components/Task.d.ts.map +0 -1
  199. package/dist/gui/components/Task.js.map +0 -1
  200. package/dist/gui/components/Textarea.d.ts.map +0 -1
  201. package/dist/gui/components/Textarea.js.map +0 -1
  202. package/dist/gui/components/ThinkingBlock.d.ts.map +0 -1
  203. package/dist/gui/components/ThinkingBlock.js.map +0 -1
  204. package/dist/gui/components/TodoList.d.ts.map +0 -1
  205. package/dist/gui/components/TodoList.js.map +0 -1
  206. package/dist/gui/components/TodoListItem.d.ts.map +0 -1
  207. package/dist/gui/components/TodoListItem.js.map +0 -1
  208. package/dist/gui/components/index.d.ts.map +0 -1
  209. package/dist/gui/components/index.js.map +0 -1
  210. package/dist/gui/index.d.ts.map +0 -1
  211. package/dist/gui/index.js.map +0 -1
  212. package/dist/gui/lib/utils.d.ts.map +0 -1
  213. package/dist/gui/lib/utils.js.map +0 -1
  214. package/dist/index.d.ts.map +0 -1
  215. package/dist/index.js.map +0 -1
  216. package/dist/sdk/client/acp-client.d.ts.map +0 -1
  217. package/dist/sdk/client/acp-client.js.map +0 -1
  218. package/dist/sdk/client/index.d.ts.map +0 -1
  219. package/dist/sdk/client/index.js.map +0 -1
  220. package/dist/sdk/index.d.ts.map +0 -1
  221. package/dist/sdk/index.js.map +0 -1
  222. package/dist/sdk/schemas/agent.d.ts.map +0 -1
  223. package/dist/sdk/schemas/agent.js.map +0 -1
  224. package/dist/sdk/schemas/index.d.ts.map +0 -1
  225. package/dist/sdk/schemas/index.js.map +0 -1
  226. package/dist/sdk/schemas/message.d.ts.map +0 -1
  227. package/dist/sdk/schemas/message.js.map +0 -1
  228. package/dist/sdk/schemas/session.d.ts.map +0 -1
  229. package/dist/sdk/schemas/session.js.map +0 -1
  230. package/dist/sdk/transports/http.d.ts.map +0 -1
  231. package/dist/sdk/transports/http.js.map +0 -1
  232. package/dist/sdk/transports/index.d.ts.map +0 -1
  233. package/dist/sdk/transports/index.js.map +0 -1
  234. package/dist/sdk/transports/stdio.d.ts.map +0 -1
  235. package/dist/sdk/transports/stdio.js.map +0 -1
  236. package/dist/sdk/transports/types.d.ts.map +0 -1
  237. package/dist/sdk/transports/types.js.map +0 -1
  238. package/dist/sdk/transports/websocket.d.ts.map +0 -1
  239. package/dist/sdk/transports/websocket.js.map +0 -1
  240. package/dist/tui/components/ChatView.d.ts.map +0 -1
  241. package/dist/tui/components/ChatView.js.map +0 -1
  242. package/dist/tui/components/GameOfLife.d.ts.map +0 -1
  243. package/dist/tui/components/GameOfLife.js.map +0 -1
  244. package/dist/tui/components/InputBox.d.ts.map +0 -1
  245. package/dist/tui/components/InputBox.js.map +0 -1
  246. package/dist/tui/components/MessageList.d.ts.map +0 -1
  247. package/dist/tui/components/MessageList.js.map +0 -1
  248. package/dist/tui/components/ReadlineInput.d.ts.map +0 -1
  249. package/dist/tui/components/ReadlineInput.js.map +0 -1
  250. package/dist/tui/components/StatusBar.d.ts.map +0 -1
  251. package/dist/tui/components/StatusBar.js.map +0 -1
  252. package/dist/tui/components/index.d.ts.map +0 -1
  253. package/dist/tui/components/index.js.map +0 -1
  254. package/dist/tui/index.d.ts.map +0 -1
  255. package/dist/tui/index.js.map +0 -1
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @town/ui
2
+ * @townco/ui
3
3
  *
4
4
  * Unified UI package with composable components
5
5
  *
@@ -0,0 +1 @@
1
+ "use strict";
@@ -1,96 +1,84 @@
1
- import type {
2
- MessageChunk,
3
- Session,
4
- SessionConfig,
5
- SessionUpdate,
6
- } from "../schemas/index.js";
7
- import type {
8
- HttpTransportOptions,
9
- StdioTransportOptions,
10
- WebSocketTransportOptions,
11
- } from "../transports/index.js";
1
+ import type { MessageChunk, Session, SessionConfig, SessionUpdate } from "../schemas/index.js";
2
+ import type { HttpTransportOptions, StdioTransportOptions, WebSocketTransportOptions } from "../transports/index.js";
12
3
  /**
13
4
  * Client configuration with explicit transport selection
14
5
  */
15
- export type AcpClientConfig =
16
- | {
17
- type: "stdio";
18
- options: StdioTransportOptions;
19
- autoConnect?: boolean;
20
- }
21
- | {
22
- type: "http";
23
- options: HttpTransportOptions;
24
- autoConnect?: boolean;
25
- }
26
- | {
27
- type: "websocket";
28
- options: WebSocketTransportOptions;
29
- autoConnect?: boolean;
30
- };
6
+ export type AcpClientConfig = {
7
+ type: "stdio";
8
+ options: StdioTransportOptions;
9
+ autoConnect?: boolean;
10
+ } | {
11
+ type: "http";
12
+ options: HttpTransportOptions;
13
+ autoConnect?: boolean;
14
+ } | {
15
+ type: "websocket";
16
+ options: WebSocketTransportOptions;
17
+ autoConnect?: boolean;
18
+ };
31
19
  /**
32
20
  * Simplified ACP client with explicit transport selection
33
21
  */
34
22
  export declare class AcpClient {
35
- private config;
36
- private transport;
37
- private sessions;
38
- private currentSessionId;
39
- private sessionUpdateHandlers;
40
- private errorHandlers;
41
- constructor(config: AcpClientConfig);
42
- /**
43
- * Connect to the agent
44
- */
45
- connect(): Promise<void>;
46
- /**
47
- * Disconnect from the agent
48
- */
49
- disconnect(): Promise<void>;
50
- /**
51
- * Check if connected
52
- */
53
- isConnected(): boolean;
54
- /**
55
- * Start a new chat session
56
- */
57
- startSession(config?: Partial<SessionConfig>): Promise<string>;
58
- /**
59
- * Send a message in the current session
60
- */
61
- sendMessage(content: string, sessionId?: string): Promise<void>;
62
- /**
63
- * Receive messages from the agent (streaming)
64
- */
65
- receiveMessages(): AsyncIterableIterator<MessageChunk>;
66
- /**
67
- * Get a session by ID
68
- */
69
- getSession(sessionId: string): Session | undefined;
70
- /**
71
- * Get current session
72
- */
73
- getCurrentSession(): Session | undefined;
74
- /**
75
- * Get all sessions
76
- */
77
- getAllSessions(): Session[];
78
- /**
79
- * Subscribe to session updates
80
- */
81
- onSessionUpdate(handler: (update: SessionUpdate) => void): () => void;
82
- /**
83
- * Subscribe to errors
84
- */
85
- onError(handler: (error: Error) => void): () => void;
86
- /**
87
- * Create transport based on explicit configuration
88
- */
89
- private createTransport;
90
- private setupTransportListeners;
91
- private handleSessionUpdate;
92
- private handleError;
93
- private updateSessionStatus;
94
- private generateSessionId;
95
- private generateMessageId;
23
+ private config;
24
+ private transport;
25
+ private sessions;
26
+ private currentSessionId;
27
+ private sessionUpdateHandlers;
28
+ private errorHandlers;
29
+ constructor(config: AcpClientConfig);
30
+ /**
31
+ * Connect to the agent
32
+ */
33
+ connect(): Promise<void>;
34
+ /**
35
+ * Disconnect from the agent
36
+ */
37
+ disconnect(): Promise<void>;
38
+ /**
39
+ * Check if connected
40
+ */
41
+ isConnected(): boolean;
42
+ /**
43
+ * Start a new chat session
44
+ */
45
+ startSession(config?: Partial<SessionConfig>): Promise<string>;
46
+ /**
47
+ * Send a message in the current session
48
+ */
49
+ sendMessage(content: string, sessionId?: string): Promise<void>;
50
+ /**
51
+ * Receive messages from the agent (streaming)
52
+ */
53
+ receiveMessages(): AsyncIterableIterator<MessageChunk>;
54
+ /**
55
+ * Get a session by ID
56
+ */
57
+ getSession(sessionId: string): Session | undefined;
58
+ /**
59
+ * Get current session
60
+ */
61
+ getCurrentSession(): Session | undefined;
62
+ /**
63
+ * Get all sessions
64
+ */
65
+ getAllSessions(): Session[];
66
+ /**
67
+ * Subscribe to session updates
68
+ */
69
+ onSessionUpdate(handler: (update: SessionUpdate) => void): () => void;
70
+ /**
71
+ * Subscribe to errors
72
+ */
73
+ onError(handler: (error: Error) => void): () => void;
74
+ /**
75
+ * Create transport based on explicit configuration
76
+ */
77
+ private createTransport;
78
+ private setupTransportListeners;
79
+ private handleSessionUpdate;
80
+ private handleError;
81
+ private updateSessionStatus;
82
+ private generateSessionId;
83
+ private generateMessageId;
96
84
  }
@@ -5,219 +5,221 @@ import { WebSocketTransport } from "../transports/websocket.js";
5
5
  * Simplified ACP client with explicit transport selection
6
6
  */
7
7
  export class AcpClient {
8
- config;
9
- transport;
10
- sessions = new Map();
11
- currentSessionId = null;
12
- sessionUpdateHandlers = new Set();
13
- errorHandlers = new Set();
14
- constructor(config) {
15
- this.config = config;
16
- this.transport = this.createTransport();
17
- this.setupTransportListeners();
18
- if (config.autoConnect) {
19
- this.connect().catch((error) => {
20
- console.error("Failed to auto-connect:", error);
21
- });
22
- }
23
- }
24
- /**
25
- * Connect to the agent
26
- */
27
- async connect() {
28
- await this.transport.connect();
29
- }
30
- /**
31
- * Disconnect from the agent
32
- */
33
- async disconnect() {
34
- await this.transport.disconnect();
35
- this.currentSessionId = null;
36
- }
37
- /**
38
- * Check if connected
39
- */
40
- isConnected() {
41
- return this.transport.isConnected();
42
- }
43
- /**
44
- * Start a new chat session
45
- */
46
- async startSession(config) {
47
- const sessionId = this.generateSessionId();
48
- const now = new Date().toISOString();
49
- const session = {
50
- id: sessionId,
51
- status: "connecting",
52
- config: config
53
- ? { ...config, agentPath: config.agentPath || "" }
54
- : { agentPath: "" },
55
- messages: [],
56
- metadata: {
57
- startedAt: now,
58
- },
59
- };
60
- this.sessions.set(sessionId, session);
61
- this.currentSessionId = sessionId;
62
- // Update session status
63
- this.updateSessionStatus(sessionId, "connected");
64
- return sessionId;
65
- }
66
- /**
67
- * Send a message in the current session
68
- */
69
- async sendMessage(content, sessionId) {
70
- const targetSessionId = sessionId || this.currentSessionId;
71
- if (!targetSessionId) {
72
- throw new Error("No active session. Start a session first.");
73
- }
74
- if (!this.transport.isConnected()) {
75
- throw new Error("Transport not connected");
76
- }
77
- const session = this.sessions.get(targetSessionId);
78
- if (!session) {
79
- throw new Error(`Session ${targetSessionId} not found`);
80
- }
81
- // Create message
82
- const message = {
83
- id: this.generateMessageId(),
84
- role: "user",
85
- content: [
86
- {
87
- type: "text",
88
- text: content,
89
- },
90
- ],
91
- timestamp: new Date().toISOString(),
92
- };
93
- // Add to session
94
- session.messages.push(message);
95
- this.updateSessionStatus(targetSessionId, "active");
96
- // Send through transport
97
- await this.transport.send(message);
98
- }
99
- /**
100
- * Receive messages from the agent (streaming)
101
- */
102
- async *receiveMessages() {
103
- if (!this.transport.isConnected()) {
104
- throw new Error("Transport not connected");
105
- }
106
- yield* this.transport.receive();
107
- }
108
- /**
109
- * Get a session by ID
110
- */
111
- getSession(sessionId) {
112
- return this.sessions.get(sessionId);
113
- }
114
- /**
115
- * Get current session
116
- */
117
- getCurrentSession() {
118
- return this.currentSessionId
119
- ? this.sessions.get(this.currentSessionId)
120
- : undefined;
121
- }
122
- /**
123
- * Get all sessions
124
- */
125
- getAllSessions() {
126
- return Array.from(this.sessions.values());
127
- }
128
- /**
129
- * Subscribe to session updates
130
- */
131
- onSessionUpdate(handler) {
132
- this.sessionUpdateHandlers.add(handler);
133
- return () => {
134
- this.sessionUpdateHandlers.delete(handler);
135
- };
136
- }
137
- /**
138
- * Subscribe to errors
139
- */
140
- onError(handler) {
141
- this.errorHandlers.add(handler);
142
- return () => {
143
- this.errorHandlers.delete(handler);
144
- };
145
- }
146
- /**
147
- * Create transport based on explicit configuration
148
- */
149
- createTransport() {
150
- switch (this.config.type) {
151
- case "stdio":
152
- return new StdioTransport(this.config.options);
153
- case "http":
154
- return new HttpTransport(this.config.options);
155
- case "websocket":
156
- return new WebSocketTransport(this.config.options);
157
- default:
158
- // biome-ignore lint/suspicious/noExplicitAny: .
159
- throw new Error(`Unknown transport type: ${this.config.type}`);
160
- }
161
- }
162
- setupTransportListeners() {
163
- // Listen to transport session updates
164
- this.transport.onSessionUpdate((update) => {
165
- this.handleSessionUpdate(update);
166
- });
167
- // Listen to transport errors
168
- this.transport.onError((error) => {
169
- this.handleError(error);
170
- });
171
- }
172
- handleSessionUpdate(update) {
173
- // Update local session state
174
- if (update.sessionId) {
175
- const session = this.sessions.get(update.sessionId);
176
- if (session) {
177
- if (update.status) {
178
- session.status = update.status;
179
- }
180
- if (update.message) {
181
- session.messages.push(update.message);
182
- }
183
- if (update.error) {
184
- session.error = update.error;
185
- }
186
- }
187
- }
188
- // Notify handlers
189
- for (const handler of this.sessionUpdateHandlers) {
190
- try {
191
- handler(update);
192
- } catch (error) {
193
- console.error("Error in session update handler:", error);
194
- }
195
- }
196
- }
197
- handleError(error) {
198
- // Notify error handlers
199
- for (const handler of this.errorHandlers) {
200
- try {
201
- handler(error);
202
- } catch (err) {
203
- console.error("Error in error handler:", err);
204
- }
205
- }
206
- }
207
- updateSessionStatus(sessionId, status) {
208
- const session = this.sessions.get(sessionId);
209
- if (session) {
210
- session.status = status;
211
- this.handleSessionUpdate({
212
- sessionId,
213
- status,
214
- });
215
- }
216
- }
217
- generateSessionId() {
218
- return `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
219
- }
220
- generateMessageId() {
221
- return `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
222
- }
8
+ config;
9
+ transport;
10
+ sessions = new Map();
11
+ currentSessionId = null;
12
+ sessionUpdateHandlers = new Set();
13
+ errorHandlers = new Set();
14
+ constructor(config) {
15
+ this.config = config;
16
+ this.transport = this.createTransport();
17
+ this.setupTransportListeners();
18
+ if (config.autoConnect) {
19
+ this.connect().catch((error) => {
20
+ console.error("Failed to auto-connect:", error);
21
+ });
22
+ }
23
+ }
24
+ /**
25
+ * Connect to the agent
26
+ */
27
+ async connect() {
28
+ await this.transport.connect();
29
+ }
30
+ /**
31
+ * Disconnect from the agent
32
+ */
33
+ async disconnect() {
34
+ await this.transport.disconnect();
35
+ this.currentSessionId = null;
36
+ }
37
+ /**
38
+ * Check if connected
39
+ */
40
+ isConnected() {
41
+ return this.transport.isConnected();
42
+ }
43
+ /**
44
+ * Start a new chat session
45
+ */
46
+ async startSession(config) {
47
+ const sessionId = this.generateSessionId();
48
+ const now = new Date().toISOString();
49
+ const session = {
50
+ id: sessionId,
51
+ status: "connecting",
52
+ config: config
53
+ ? { ...config, agentPath: config.agentPath || "" }
54
+ : { agentPath: "" },
55
+ messages: [],
56
+ metadata: {
57
+ startedAt: now,
58
+ },
59
+ };
60
+ this.sessions.set(sessionId, session);
61
+ this.currentSessionId = sessionId;
62
+ // Update session status
63
+ this.updateSessionStatus(sessionId, "connected");
64
+ return sessionId;
65
+ }
66
+ /**
67
+ * Send a message in the current session
68
+ */
69
+ async sendMessage(content, sessionId) {
70
+ const targetSessionId = sessionId || this.currentSessionId;
71
+ if (!targetSessionId) {
72
+ throw new Error("No active session. Start a session first.");
73
+ }
74
+ if (!this.transport.isConnected()) {
75
+ throw new Error("Transport not connected");
76
+ }
77
+ const session = this.sessions.get(targetSessionId);
78
+ if (!session) {
79
+ throw new Error(`Session ${targetSessionId} not found`);
80
+ }
81
+ // Create message
82
+ const message = {
83
+ id: this.generateMessageId(),
84
+ role: "user",
85
+ content: [
86
+ {
87
+ type: "text",
88
+ text: content,
89
+ },
90
+ ],
91
+ timestamp: new Date().toISOString(),
92
+ };
93
+ // Add to session
94
+ session.messages.push(message);
95
+ this.updateSessionStatus(targetSessionId, "active");
96
+ // Send through transport
97
+ await this.transport.send(message);
98
+ }
99
+ /**
100
+ * Receive messages from the agent (streaming)
101
+ */
102
+ async *receiveMessages() {
103
+ if (!this.transport.isConnected()) {
104
+ throw new Error("Transport not connected");
105
+ }
106
+ yield* this.transport.receive();
107
+ }
108
+ /**
109
+ * Get a session by ID
110
+ */
111
+ getSession(sessionId) {
112
+ return this.sessions.get(sessionId);
113
+ }
114
+ /**
115
+ * Get current session
116
+ */
117
+ getCurrentSession() {
118
+ return this.currentSessionId
119
+ ? this.sessions.get(this.currentSessionId)
120
+ : undefined;
121
+ }
122
+ /**
123
+ * Get all sessions
124
+ */
125
+ getAllSessions() {
126
+ return Array.from(this.sessions.values());
127
+ }
128
+ /**
129
+ * Subscribe to session updates
130
+ */
131
+ onSessionUpdate(handler) {
132
+ this.sessionUpdateHandlers.add(handler);
133
+ return () => {
134
+ this.sessionUpdateHandlers.delete(handler);
135
+ };
136
+ }
137
+ /**
138
+ * Subscribe to errors
139
+ */
140
+ onError(handler) {
141
+ this.errorHandlers.add(handler);
142
+ return () => {
143
+ this.errorHandlers.delete(handler);
144
+ };
145
+ }
146
+ /**
147
+ * Create transport based on explicit configuration
148
+ */
149
+ createTransport() {
150
+ switch (this.config.type) {
151
+ case "stdio":
152
+ return new StdioTransport(this.config.options);
153
+ case "http":
154
+ return new HttpTransport(this.config.options);
155
+ case "websocket":
156
+ return new WebSocketTransport(this.config.options);
157
+ default:
158
+ // biome-ignore lint/suspicious/noExplicitAny: .
159
+ throw new Error(`Unknown transport type: ${this.config.type}`);
160
+ }
161
+ }
162
+ setupTransportListeners() {
163
+ // Listen to transport session updates
164
+ this.transport.onSessionUpdate((update) => {
165
+ this.handleSessionUpdate(update);
166
+ });
167
+ // Listen to transport errors
168
+ this.transport.onError((error) => {
169
+ this.handleError(error);
170
+ });
171
+ }
172
+ handleSessionUpdate(update) {
173
+ // Update local session state
174
+ if (update.sessionId) {
175
+ const session = this.sessions.get(update.sessionId);
176
+ if (session) {
177
+ if (update.status) {
178
+ session.status = update.status;
179
+ }
180
+ if (update.message) {
181
+ session.messages.push(update.message);
182
+ }
183
+ if (update.error) {
184
+ session.error = update.error;
185
+ }
186
+ }
187
+ }
188
+ // Notify handlers
189
+ for (const handler of this.sessionUpdateHandlers) {
190
+ try {
191
+ handler(update);
192
+ }
193
+ catch (error) {
194
+ console.error("Error in session update handler:", error);
195
+ }
196
+ }
197
+ }
198
+ handleError(error) {
199
+ // Notify error handlers
200
+ for (const handler of this.errorHandlers) {
201
+ try {
202
+ handler(error);
203
+ }
204
+ catch (err) {
205
+ console.error("Error in error handler:", err);
206
+ }
207
+ }
208
+ }
209
+ updateSessionStatus(sessionId, status) {
210
+ const session = this.sessions.get(sessionId);
211
+ if (session) {
212
+ session.status = status;
213
+ this.handleSessionUpdate({
214
+ sessionId,
215
+ status,
216
+ });
217
+ }
218
+ }
219
+ generateSessionId() {
220
+ return `session_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
221
+ }
222
+ generateMessageId() {
223
+ return `msg_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
224
+ }
223
225
  }
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @town/sdk
2
+ * @townco/sdk
3
3
  *
4
4
  * Simplified SDK for Agent Client Protocol (ACP)
5
5
  * Supports stdio, HTTP, and WebSocket transports
package/dist/sdk/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @town/sdk
2
+ * @townco/sdk
3
3
  *
4
4
  * Simplified SDK for Agent Client Protocol (ACP)
5
5
  * Supports stdio, HTTP, and WebSocket transports