@robota-sdk/agent-transport 3.0.0-beta.75 → 3.0.0-beta.77

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 (231) hide show
  1. package/LICENSE +661 -21
  2. package/README.md +17 -14
  3. package/dist/node/headless/index.cjs +1 -1
  4. package/dist/node/headless/index.d.ts +1 -1
  5. package/dist/node/headless/index.js +1 -1
  6. package/dist/node/{headless-mRYilLfC.js → headless-DG_MEbf0.js} +7 -4
  7. package/dist/node/headless-DG_MEbf0.js.map +1 -0
  8. package/dist/node/headless-xq77JBq0.cjs +18 -0
  9. package/dist/node/{index-E8Gx4-lc.d.ts → index-CrJ2Lhn3.d.ts} +16 -3
  10. package/dist/node/index-CrJ2Lhn3.d.ts.map +1 -0
  11. package/dist/node/index-JIvzrtaA.d.ts +52 -0
  12. package/dist/node/index-JIvzrtaA.d.ts.map +1 -0
  13. package/dist/node/{index-CYl7ksS6.d.ts → index-JTD_xtEQ.d.ts} +15 -2
  14. package/dist/node/index-JTD_xtEQ.d.ts.map +1 -0
  15. package/dist/node/index-y_HBOfzO.d.ts +52 -0
  16. package/dist/node/index-y_HBOfzO.d.ts.map +1 -0
  17. package/dist/node/index.cjs +1 -1
  18. package/dist/node/index.d.ts +10 -9
  19. package/dist/node/index.d.ts.map +1 -1
  20. package/dist/node/index.js +1 -1
  21. package/dist/node/index.js.map +1 -1
  22. package/dist/node/programmatic/index.cjs +1 -0
  23. package/dist/node/programmatic/index.d.ts +2 -0
  24. package/dist/node/programmatic/index.js +1 -0
  25. package/dist/node/programmatic-6wLf68qJ.cjs +1 -0
  26. package/dist/node/programmatic-70dxIXtS.js +2 -0
  27. package/dist/node/programmatic-70dxIXtS.js.map +1 -0
  28. package/dist/node/testing/index.cjs +1 -1
  29. package/dist/node/testing/index.d.ts +2 -21
  30. package/dist/node/testing/index.js +1 -2
  31. package/package.json +15 -71
  32. package/src/__tests__/programmatic/programmatic-ask-user.test.ts +32 -0
  33. package/src/__tests__/programmatic/programmatic-driver.test.ts +107 -0
  34. package/src/__tests__/programmatic/programmatic-flows.test.ts +140 -0
  35. package/src/__tests__/transport-registry.stop-all.test.ts +53 -0
  36. package/src/headless/HeadlessInteractionChannel.ts +22 -5
  37. package/src/headless/__tests__/headless-goal-runner.test.ts +108 -0
  38. package/src/headless/__tests__/headless-runner.test.ts +2 -1
  39. package/src/headless/headless-runner.ts +67 -1
  40. package/src/headless/headless-stream-json.ts +1 -1
  41. package/src/headless/print-terminal.ts +1 -0
  42. package/src/index.ts +2 -5
  43. package/src/programmatic/ProgrammaticInteractionChannel.ts +82 -0
  44. package/src/programmatic/createProgrammaticAgent.ts +71 -0
  45. package/src/programmatic/index.ts +5 -0
  46. package/src/testing/index.ts +7 -4
  47. package/src/transport-registry.ts +15 -12
  48. package/dist/node/headless-CT2ibQnr.cjs +0 -15
  49. package/dist/node/headless-mRYilLfC.js.map +0 -1
  50. package/dist/node/http/index.cjs +0 -1
  51. package/dist/node/http/index.d.ts +0 -2
  52. package/dist/node/http/index.js +0 -1
  53. package/dist/node/http-2Jiuflc1.js +0 -2
  54. package/dist/node/http-2Jiuflc1.js.map +0 -1
  55. package/dist/node/http-CBAvefLw.cjs +0 -1
  56. package/dist/node/index-BNccqSpv.d.ts +0 -86
  57. package/dist/node/index-BNccqSpv.d.ts.map +0 -1
  58. package/dist/node/index-BUhHIf7X.d.ts +0 -86
  59. package/dist/node/index-BUhHIf7X.d.ts.map +0 -1
  60. package/dist/node/index-BnAGE-u9.d.ts +0 -33
  61. package/dist/node/index-BnAGE-u9.d.ts.map +0 -1
  62. package/dist/node/index-BrQ4gGw0.d.ts +0 -213
  63. package/dist/node/index-BrQ4gGw0.d.ts.map +0 -1
  64. package/dist/node/index-CYl7ksS6.d.ts.map +0 -1
  65. package/dist/node/index-CoeBF21y.d.ts +0 -213
  66. package/dist/node/index-CoeBF21y.d.ts.map +0 -1
  67. package/dist/node/index-DHt-2VQ-.d.ts +0 -46
  68. package/dist/node/index-DHt-2VQ-.d.ts.map +0 -1
  69. package/dist/node/index-DMwKN5Le.d.ts +0 -33
  70. package/dist/node/index-DMwKN5Le.d.ts.map +0 -1
  71. package/dist/node/index-E8Gx4-lc.d.ts.map +0 -1
  72. package/dist/node/index-c0M42fsA.d.ts +0 -46
  73. package/dist/node/index-c0M42fsA.d.ts.map +0 -1
  74. package/dist/node/mcp/index.cjs +0 -1
  75. package/dist/node/mcp/index.d.ts +0 -2
  76. package/dist/node/mcp/index.js +0 -1
  77. package/dist/node/mcp-BOglBJNy.cjs +0 -1
  78. package/dist/node/mcp-D3BBVK7C.js +0 -2
  79. package/dist/node/mcp-D3BBVK7C.js.map +0 -1
  80. package/dist/node/rolldown-runtime-CMqjfN_6.cjs +0 -1
  81. package/dist/node/testing/index.d.ts.map +0 -1
  82. package/dist/node/testing/index.js.map +0 -1
  83. package/dist/node/tui/index.cjs +0 -1
  84. package/dist/node/tui/index.d.ts +0 -2
  85. package/dist/node/tui/index.js +0 -1
  86. package/dist/node/tui-CcH5EsQh.js +0 -25
  87. package/dist/node/tui-CcH5EsQh.js.map +0 -1
  88. package/dist/node/tui-DznRbcku.cjs +0 -24
  89. package/dist/node/ws/index.cjs +0 -1
  90. package/dist/node/ws/index.d.ts +0 -2
  91. package/dist/node/ws/index.js +0 -1
  92. package/dist/node/ws-Dc2RUwVs.js +0 -2
  93. package/dist/node/ws-Dc2RUwVs.js.map +0 -1
  94. package/dist/node/ws-QNMQn5kg.cjs +0 -1
  95. package/src/http/__tests__/http-transport.test.ts +0 -55
  96. package/src/http/__tests__/routes.test.ts +0 -168
  97. package/src/http/http-transport.ts +0 -41
  98. package/src/http/index.ts +0 -4
  99. package/src/http/routes.ts +0 -152
  100. package/src/mcp/__tests__/mcp-server.test.ts +0 -66
  101. package/src/mcp/__tests__/mcp-transport.test.ts +0 -46
  102. package/src/mcp/index.ts +0 -4
  103. package/src/mcp/mcp-server.ts +0 -163
  104. package/src/mcp/mcp-transport.ts +0 -48
  105. package/src/testing/__tests__/scripted-provider.test.ts +0 -73
  106. package/src/testing/scripted-provider.ts +0 -73
  107. package/src/tui/App.tsx +0 -491
  108. package/src/tui/BackgroundTaskPanel.tsx +0 -36
  109. package/src/tui/CjkTextInput.tsx +0 -199
  110. package/src/tui/ConfirmPrompt.tsx +0 -70
  111. package/src/tui/ContextWarningBanner.tsx +0 -34
  112. package/src/tui/ExecutionWorkspaceDetailPane.tsx +0 -64
  113. package/src/tui/ExecutionWorkspaceSwitcher.tsx +0 -187
  114. package/src/tui/InputArea.tsx +0 -310
  115. package/src/tui/InteractivePrompt.tsx +0 -59
  116. package/src/tui/ListPicker.tsx +0 -95
  117. package/src/tui/MenuSelect.tsx +0 -104
  118. package/src/tui/MessageList.tsx +0 -284
  119. package/src/tui/PermissionPrompt.tsx +0 -86
  120. package/src/tui/PluginTUI.tsx +0 -258
  121. package/src/tui/SessionPicker.tsx +0 -68
  122. package/src/tui/SessionStatusBar.tsx +0 -73
  123. package/src/tui/SlashAutocomplete.tsx +0 -110
  124. package/src/tui/StatusBar.tsx +0 -236
  125. package/src/tui/StreamingIndicator.tsx +0 -93
  126. package/src/tui/TextPrompt.tsx +0 -81
  127. package/src/tui/ToolCommandOutput.tsx +0 -39
  128. package/src/tui/ToolDiffBlock.tsx +0 -32
  129. package/src/tui/TransportTUI.tsx +0 -117
  130. package/src/tui/TuiInteractionChannel.ts +0 -495
  131. package/src/tui/UpdateNotice.tsx +0 -14
  132. package/src/tui/UsageSummaryEntry.tsx +0 -39
  133. package/src/tui/WaveText.tsx +0 -44
  134. package/src/tui/__tests__/InteractivePrompt.test.tsx +0 -82
  135. package/src/tui/__tests__/ListPicker.test.tsx +0 -159
  136. package/src/tui/__tests__/MenuSelect.test.tsx +0 -103
  137. package/src/tui/__tests__/PluginTUI.test.tsx +0 -167
  138. package/src/tui/__tests__/SlashAutocomplete.test.tsx +0 -140
  139. package/src/tui/__tests__/TextPrompt.test.tsx +0 -98
  140. package/src/tui/__tests__/TuiInteractionChannel.display-contract.test.ts +0 -239
  141. package/src/tui/__tests__/TuiInteractionChannel.lifecycle.test.ts +0 -297
  142. package/src/tui/__tests__/TuiInteractionChannel.requestAction.test.ts +0 -124
  143. package/src/tui/__tests__/UpdateNotice.test.tsx +0 -15
  144. package/src/tui/__tests__/abort-after-permission.test.tsx +0 -169
  145. package/src/tui/__tests__/abort-streaming-e2e.test.tsx +0 -183
  146. package/src/tui/__tests__/background-task-panel.test.tsx +0 -53
  147. package/src/tui/__tests__/background-task-row-format.test.ts +0 -59
  148. package/src/tui/__tests__/channel-factory-integration.test.ts +0 -138
  149. package/src/tui/__tests__/cjk-text-input-flow.test.ts +0 -109
  150. package/src/tui/__tests__/cjk-text-input.test.ts +0 -191
  151. package/src/tui/__tests__/command-effect-handler.test.ts +0 -127
  152. package/src/tui/__tests__/command-output-summary.test.ts +0 -95
  153. package/src/tui/__tests__/compact-event-bridge.test.ts +0 -20
  154. package/src/tui/__tests__/confirm-permission-flow.test.ts +0 -130
  155. package/src/tui/__tests__/confirm-prompt.test.tsx +0 -87
  156. package/src/tui/__tests__/execution-workspace-switcher.test.tsx +0 -110
  157. package/src/tui/__tests__/execution-workspace-view-model.test.ts +0 -93
  158. package/src/tui/__tests__/fixtures/provider-setup-prompt-driver.tsx +0 -125
  159. package/src/tui/__tests__/input-area-flow.test.ts +0 -164
  160. package/src/tui/__tests__/message-list-rendering.test.tsx +0 -353
  161. package/src/tui/__tests__/prompt-queue.test.tsx +0 -255
  162. package/src/tui/__tests__/provider-setup-pty-e2e.test.ts +0 -233
  163. package/src/tui/__tests__/pty/pty-driver.ts +0 -135
  164. package/src/tui/__tests__/pty/tui-pty.ptytest.ts +0 -61
  165. package/src/tui/__tests__/render-channel-options.test.ts +0 -32
  166. package/src/tui/__tests__/render-markdown.test.ts +0 -72
  167. package/src/tui/__tests__/selection-flow.test.ts +0 -61
  168. package/src/tui/__tests__/session-init-poller.test.ts +0 -102
  169. package/src/tui/__tests__/session-naming.test.ts +0 -64
  170. package/src/tui/__tests__/session-switch-channel.test.tsx +0 -307
  171. package/src/tui/__tests__/slash-routing-effects.test.ts +0 -228
  172. package/src/tui/__tests__/status-activity.test.ts +0 -71
  173. package/src/tui/__tests__/status-bar.test.tsx +0 -177
  174. package/src/tui/__tests__/streaming-indicator.test.tsx +0 -137
  175. package/src/tui/__tests__/text-prompt-flow.test.ts +0 -77
  176. package/src/tui/__tests__/tui-channel-init-failure.test.ts +0 -57
  177. package/src/tui/__tests__/tui-state-manager.test.ts +0 -401
  178. package/src/tui/background-task-row-format.ts +0 -53
  179. package/src/tui/command-interaction.ts +0 -9
  180. package/src/tui/command-output-summary.ts +0 -122
  181. package/src/tui/create-default-tui-cli-adapter.ts +0 -41
  182. package/src/tui/execution-workspace-view-model.ts +0 -123
  183. package/src/tui/flows/cjk-text-input-flow.ts +0 -285
  184. package/src/tui/flows/confirm-prompt-flow.ts +0 -45
  185. package/src/tui/flows/input-area-flow.ts +0 -189
  186. package/src/tui/flows/permission-prompt-flow.ts +0 -85
  187. package/src/tui/flows/selection-flow.ts +0 -126
  188. package/src/tui/flows/session-init-poller.ts +0 -77
  189. package/src/tui/flows/text-prompt-flow.ts +0 -98
  190. package/src/tui/hooks/command-effect-handler.ts +0 -97
  191. package/src/tui/hooks/command-effect-queue.ts +0 -39
  192. package/src/tui/hooks/side-effects-types.ts +0 -35
  193. package/src/tui/hooks/useAutocomplete.ts +0 -87
  194. package/src/tui/hooks/usePluginCallbacks.ts +0 -31
  195. package/src/tui/hooks/usePluginScreenData.ts +0 -85
  196. package/src/tui/hooks/useSideEffects.ts +0 -175
  197. package/src/tui/hooks/useSlashRouting.ts +0 -118
  198. package/src/tui/hooks/useStatusLineSettings.ts +0 -37
  199. package/src/tui/hooks/useTuiChannel.ts +0 -95
  200. package/src/tui/index.ts +0 -14
  201. package/src/tui/interactions/CommandConfirm.tsx +0 -36
  202. package/src/tui/interactions/CommandPicker.tsx +0 -77
  203. package/src/tui/interactions/__tests__/CommandConfirm.test.tsx +0 -124
  204. package/src/tui/interactions/__tests__/CommandPicker.test.tsx +0 -138
  205. package/src/tui/plugin-tui-handlers.ts +0 -163
  206. package/src/tui/render-markdown.ts +0 -130
  207. package/src/tui/render.tsx +0 -129
  208. package/src/tui/session-naming.ts +0 -33
  209. package/src/tui/status-activity.ts +0 -63
  210. package/src/tui/tui-cli-adapter-context.tsx +0 -13
  211. package/src/tui/tui-cli-adapter.ts +0 -25
  212. package/src/tui/tui-state-manager.ts +0 -226
  213. package/src/tui/tui-transport.ts +0 -35
  214. package/src/tui/types.ts +0 -15
  215. package/src/tui/utils/__tests__/edit-diff.test.ts +0 -426
  216. package/src/tui/utils/__tests__/paste-detection.test.ts +0 -116
  217. package/src/tui/utils/__tests__/paste-labels.test.ts +0 -46
  218. package/src/tui/utils/__tests__/tool-call-extractor.test.ts +0 -227
  219. package/src/tui/utils/__tests__/tool-diff-summary.test.ts +0 -104
  220. package/src/tui/utils/edit-diff.ts +0 -153
  221. package/src/tui/utils/paste-labels.ts +0 -9
  222. package/src/tui/utils/tool-call-extractor.ts +0 -92
  223. package/src/tui/utils/tool-diff-summary.ts +0 -75
  224. package/src/ws/__tests__/ws-handler.test.ts +0 -409
  225. package/src/ws/__tests__/ws-transport.test.ts +0 -53
  226. package/src/ws/index.ts +0 -13
  227. package/src/ws/ws-background-messages.ts +0 -170
  228. package/src/ws/ws-handler.ts +0 -280
  229. package/src/ws/ws-protocol.ts +0 -78
  230. package/src/ws/ws-transport-configurable.ts +0 -128
  231. package/src/ws/ws-transport.ts +0 -42
@@ -1,280 +0,0 @@
1
- /**
2
- * WebSocket transport adapter — exposes IInteractiveSession over WebSocket.
3
- *
4
- * Framework-agnostic: works with any WebSocket implementation via
5
- * send/onMessage callbacks. No dependency on ws, uWebSockets, etc.
6
- *
7
- * Protocol: JSON messages with { type, ...payload } structure.
8
- * Server pushes IInteractiveSession events to client in real-time.
9
- */
10
-
11
- import {
12
- handleBackgroundControlMessage,
13
- handleBackgroundQueryMessage,
14
- } from './ws-background-messages.js';
15
-
16
- import type { TClientMessage, TServerMessage } from './ws-protocol.js';
17
- import type { TBackgroundTaskEvent } from '@robota-sdk/agent-framework';
18
- import type {
19
- IExecutionResult,
20
- IExecutionWorkspaceEvent,
21
- IInteractiveSession,
22
- IToolState,
23
- TBackgroundJobGroupEvent,
24
- } from '@robota-sdk/agent-interface-transport';
25
-
26
- export interface IWsHandlerOptions {
27
- /** IInteractiveSession to expose. */
28
- session: IInteractiveSession;
29
- /** Send a JSON message to the client. */
30
- send: (message: TServerMessage) => void;
31
- }
32
-
33
- /**
34
- * Create a WebSocket message handler for an IInteractiveSession.
35
- *
36
- * Returns:
37
- * - `onMessage(data)`: call this when the WebSocket receives a message
38
- * - `cleanup()`: call this when the WebSocket disconnects
39
- *
40
- * Usage:
41
- * ```typescript
42
- * const { onMessage, cleanup } = createWsHandler({
43
- * session: interactiveSession,
44
- * send: (msg) => ws.send(JSON.stringify(msg)),
45
- * });
46
- *
47
- * ws.on('message', (data) => onMessage(String(data)));
48
- * ws.on('close', cleanup);
49
- * ```
50
- */
51
- export function createWsHandler(options: IWsHandlerOptions): {
52
- onMessage: (data: string) => void;
53
- cleanup: () => void;
54
- } {
55
- const cleanup = subscribeSessionEvents(options.session, options.send);
56
- const onMessage = createWsMessageHandler(options.session, options.send);
57
-
58
- return { onMessage, cleanup };
59
- }
60
-
61
- function subscribeSessionEvents(
62
- session: IInteractiveSession,
63
- send: (message: TServerMessage) => void,
64
- ): () => void {
65
- const onUserMessage = (content: string): void => send({ type: 'user_message', content });
66
- const onTextDelta = (delta: string): void => send({ type: 'text_delta', delta });
67
- const onToolStart = (state: IToolState): void => send({ type: 'tool_start', state });
68
- const onToolEnd = (state: IToolState): void => send({ type: 'tool_end', state });
69
- const onThinking = (isThinking: boolean): void => send({ type: 'thinking', isThinking });
70
- const onComplete = (result: IExecutionResult): void => send({ type: 'complete', result });
71
- const onInterrupted = (result: IExecutionResult): void => send({ type: 'interrupted', result });
72
- const onError = (error: Error): void => send({ type: 'error', message: error.message });
73
- const onBackgroundTaskEvent = (event: TBackgroundTaskEvent): void =>
74
- send({ type: 'background_task_event', event });
75
- const onBackgroundJobGroupEvent = (event: TBackgroundJobGroupEvent): void =>
76
- send({ type: 'background_job_group_event', event });
77
- const onExecutionWorkspace = (event: IExecutionWorkspaceEvent): void =>
78
- send({ type: 'execution_workspace_event', snapshot: event.snapshot });
79
-
80
- session.on('user_message', onUserMessage);
81
- session.on('text_delta', onTextDelta);
82
- session.on('tool_start', onToolStart);
83
- session.on('tool_end', onToolEnd);
84
- session.on('thinking', onThinking);
85
- session.on('complete', onComplete);
86
- session.on('interrupted', onInterrupted);
87
- session.on('error', onError);
88
- session.on('background_task_event', onBackgroundTaskEvent);
89
- session.on('background_job_group_event', onBackgroundJobGroupEvent);
90
- session.on('execution_workspace_event', onExecutionWorkspace);
91
-
92
- return (): void => {
93
- session.off('user_message', onUserMessage);
94
- session.off('text_delta', onTextDelta);
95
- session.off('tool_start', onToolStart);
96
- session.off('tool_end', onToolEnd);
97
- session.off('thinking', onThinking);
98
- session.off('complete', onComplete);
99
- session.off('interrupted', onInterrupted);
100
- session.off('error', onError);
101
- session.off('background_task_event', onBackgroundTaskEvent);
102
- session.off('background_job_group_event', onBackgroundJobGroupEvent);
103
- session.off('execution_workspace_event', onExecutionWorkspace);
104
- };
105
- }
106
-
107
- function createWsMessageHandler(
108
- session: IInteractiveSession,
109
- send: (message: TServerMessage) => void,
110
- ): (data: string) => void {
111
- return (data: string): void => {
112
- const msg = parseClientMessage(data, send);
113
- if (!msg) return;
114
- handleClientMessage(session, send, msg);
115
- };
116
- }
117
-
118
- function parseClientMessage(
119
- data: string,
120
- send: (message: TServerMessage) => void,
121
- ): TClientMessage | null {
122
- try {
123
- return JSON.parse(data) as TClientMessage;
124
- } catch {
125
- send({ type: 'protocol_error', message: 'Invalid JSON' });
126
- return null;
127
- }
128
- }
129
-
130
- function handleClientMessage(
131
- session: IInteractiveSession,
132
- send: (message: TServerMessage) => void,
133
- msg: TClientMessage,
134
- ): void {
135
- if (isSessionControlMessage(msg)) {
136
- handleSessionControlMessage(session, send, msg);
137
- return;
138
- }
139
- if (isSessionQueryMessage(msg)) {
140
- handleSessionQueryMessage(session, send, msg);
141
- return;
142
- }
143
- if (isBackgroundQueryMessage(msg)) {
144
- handleBackgroundQueryMessage(session, send, msg);
145
- return;
146
- }
147
- if (isBackgroundControlMessage(msg)) {
148
- handleBackgroundControlMessage(session, send, msg);
149
- return;
150
- }
151
- send({ type: 'protocol_error', message: `Unknown message type: ${getMessageType(msg)}` });
152
- }
153
-
154
- function getMessageType(msg: TClientMessage): string {
155
- return (msg as { type: string }).type;
156
- }
157
-
158
- function isSessionControlMessage(
159
- msg: TClientMessage,
160
- ): msg is Extract<TClientMessage, { type: 'submit' | 'command' | 'abort' | 'cancel-queue' }> {
161
- return (
162
- msg.type === 'submit' ||
163
- msg.type === 'command' ||
164
- msg.type === 'abort' ||
165
- msg.type === 'cancel-queue'
166
- );
167
- }
168
-
169
- function isSessionQueryMessage(msg: TClientMessage): msg is Extract<
170
- TClientMessage,
171
- {
172
- type:
173
- | 'get-messages'
174
- | 'get-context'
175
- | 'get-executing'
176
- | 'get-pending'
177
- | 'get-execution-workspace';
178
- }
179
- > {
180
- return (
181
- msg.type === 'get-messages' ||
182
- msg.type === 'get-context' ||
183
- msg.type === 'get-executing' ||
184
- msg.type === 'get-pending' ||
185
- msg.type === 'get-execution-workspace'
186
- );
187
- }
188
-
189
- function isBackgroundQueryMessage(
190
- msg: TClientMessage,
191
- ): msg is Extract<
192
- TClientMessage,
193
- | { type: 'get-background-tasks' | 'get-background-task' | 'read-background-task-log' }
194
- | { type: 'get-background-job-groups' | 'get-background-job-group' | 'wait-background-job-group' }
195
- > {
196
- return (
197
- msg.type === 'get-background-tasks' ||
198
- msg.type === 'get-background-task' ||
199
- msg.type === 'read-background-task-log' ||
200
- msg.type === 'get-background-job-groups' ||
201
- msg.type === 'get-background-job-group' ||
202
- msg.type === 'wait-background-job-group'
203
- );
204
- }
205
-
206
- function isBackgroundControlMessage(
207
- msg: TClientMessage,
208
- ): msg is Extract<
209
- TClientMessage,
210
- { type: 'cancel-background-task' | 'close-background-task' | 'send-background-task' }
211
- > {
212
- return (
213
- msg.type === 'cancel-background-task' ||
214
- msg.type === 'close-background-task' ||
215
- msg.type === 'send-background-task'
216
- );
217
- }
218
-
219
- function handleSessionControlMessage(
220
- session: IInteractiveSession,
221
- send: (message: TServerMessage) => void,
222
- msg: Extract<TClientMessage, { type: 'submit' | 'command' | 'abort' | 'cancel-queue' }>,
223
- ): void {
224
- if (msg.type === 'submit') {
225
- if (!msg.prompt) {
226
- send({ type: 'protocol_error', message: 'prompt is required' });
227
- return;
228
- }
229
- session.submit(msg.prompt);
230
- } else if (msg.type === 'command') {
231
- if (!msg.name) {
232
- send({ type: 'protocol_error', message: 'name is required' });
233
- return;
234
- }
235
- session.executeCommand(msg.name, msg.args ?? '').then((result) => {
236
- send({
237
- type: 'command_result',
238
- name: msg.name,
239
- message: result?.message ?? `Unknown command: ${msg.name}`,
240
- success: result?.success ?? false,
241
- data: result?.data,
242
- });
243
- });
244
- } else if (msg.type === 'abort') {
245
- session.abort();
246
- } else {
247
- session.cancelQueue();
248
- }
249
- }
250
-
251
- function handleSessionQueryMessage(
252
- session: IInteractiveSession,
253
- send: (message: TServerMessage) => void,
254
- msg: Extract<
255
- TClientMessage,
256
- {
257
- type:
258
- | 'get-messages'
259
- | 'get-context'
260
- | 'get-executing'
261
- | 'get-pending'
262
- | 'get-execution-workspace';
263
- }
264
- >,
265
- ): void {
266
- if (msg.type === 'get-messages') {
267
- send({ type: 'messages', messages: session.getMessages() });
268
- } else if (msg.type === 'get-context') {
269
- send({ type: 'context', state: session.getContextState() });
270
- } else if (msg.type === 'get-executing') {
271
- send({ type: 'executing', executing: session.isExecuting() });
272
- } else if (msg.type === 'get-execution-workspace') {
273
- send({
274
- type: 'execution_workspace_event',
275
- snapshot: session.getExecutionWorkspaceSnapshot(),
276
- });
277
- } else {
278
- send({ type: 'pending', pending: session.getPendingPrompt() });
279
- }
280
- }
@@ -1,78 +0,0 @@
1
- import type {
2
- InteractiveSession,
3
- IBackgroundTaskInput,
4
- IBackgroundTaskListFilter,
5
- IBackgroundTaskLogCursor,
6
- IBackgroundTaskLogPage,
7
- IBackgroundTaskState,
8
- TBackgroundTaskEvent,
9
- } from '@robota-sdk/agent-framework';
10
- import type {
11
- IBackgroundJobGroupState,
12
- ICommandResult,
13
- IExecutionResult,
14
- IExecutionWorkspaceSnapshot,
15
- IToolState,
16
- TBackgroundJobGroupEvent,
17
- } from '@robota-sdk/agent-interface-transport';
18
-
19
- export type TBackgroundControlAction = 'cancel' | 'close' | 'send';
20
-
21
- /** Inbound message from client to server. */
22
- export type TClientMessage =
23
- | { type: 'submit'; prompt: string }
24
- | { type: 'command'; name: string; args?: string }
25
- | { type: 'abort' }
26
- | { type: 'cancel-queue' }
27
- | { type: 'get-messages' }
28
- | { type: 'get-context' }
29
- | { type: 'get-executing' }
30
- | { type: 'get-pending' }
31
- | { type: 'get-execution-workspace' }
32
- | { type: 'get-background-tasks'; filter?: IBackgroundTaskListFilter }
33
- | { type: 'get-background-task'; taskId: string }
34
- | { type: 'get-background-job-groups' }
35
- | { type: 'get-background-job-group'; groupId: string }
36
- | { type: 'wait-background-job-group'; groupId: string }
37
- | { type: 'cancel-background-task'; taskId: string; reason?: string }
38
- | { type: 'close-background-task'; taskId: string }
39
- | { type: 'send-background-task'; taskId: string; input: IBackgroundTaskInput }
40
- | { type: 'read-background-task-log'; taskId: string; cursor?: IBackgroundTaskLogCursor };
41
-
42
- /** Outbound message from server to client. */
43
- export type TServerMessage =
44
- | { type: 'text_delta'; delta: string }
45
- | { type: 'user_message'; content: string }
46
- | { type: 'tool_start'; state: IToolState }
47
- | { type: 'tool_end'; state: IToolState }
48
- | { type: 'thinking'; isThinking: boolean }
49
- | { type: 'complete'; result: IExecutionResult }
50
- | { type: 'interrupted'; result: IExecutionResult }
51
- | { type: 'error'; message: string }
52
- | {
53
- type: 'command_result';
54
- name: string;
55
- message: string;
56
- success: boolean;
57
- data?: ICommandResult['data'];
58
- }
59
- | { type: 'messages'; messages: ReturnType<InteractiveSession['getMessages']> }
60
- | { type: 'context'; state: ReturnType<InteractiveSession['getContextState']> }
61
- | { type: 'executing'; executing: boolean }
62
- | { type: 'pending'; pending: string | null }
63
- | { type: 'execution_workspace_event'; snapshot: IExecutionWorkspaceSnapshot }
64
- | { type: 'background_task_event'; event: TBackgroundTaskEvent }
65
- | { type: 'background_job_group_event'; event: TBackgroundJobGroupEvent }
66
- | { type: 'background_tasks'; tasks: IBackgroundTaskState[] }
67
- | { type: 'background_task'; taskId: string; task: IBackgroundTaskState | null }
68
- | { type: 'background_job_groups'; groups: IBackgroundJobGroupState[] }
69
- | { type: 'background_job_group'; groupId: string; group: IBackgroundJobGroupState | null }
70
- | { type: 'background_task_log'; taskId: string; page: IBackgroundTaskLogPage }
71
- | {
72
- type: 'background_task_control_result';
73
- action: TBackgroundControlAction;
74
- taskId: string;
75
- success: boolean;
76
- message?: string;
77
- }
78
- | { type: 'protocol_error'; message: string };
@@ -1,128 +0,0 @@
1
- /**
2
- * Self-contained WS transport implementing IConfigurableTransport.
3
- * Owns the WebSocket server lifecycle (ws package), started/stopped via the transport registry.
4
- */
5
-
6
- import { createServer, type Server } from 'node:http';
7
-
8
- import { WebSocketServer, WebSocket } from 'ws';
9
-
10
- import { createWsHandler } from './ws-handler.js';
11
-
12
- import type { TServerMessage } from './ws-protocol.js';
13
- import type { TUniversalValue } from '@robota-sdk/agent-core';
14
- import type {
15
- IConfigurableTransport,
16
- IInteractiveSession,
17
- } from '@robota-sdk/agent-interface-transport';
18
-
19
- const DEFAULT_PORT = 7070;
20
- const DEFAULT_MAX_RETRIES = 20;
21
-
22
- export interface IWsTransportConfig {
23
- port?: number;
24
- maxRetries?: number;
25
- }
26
-
27
- export class WsTransport implements IConfigurableTransport<IInteractiveSession> {
28
- readonly name = 'ws';
29
- readonly defaultEnabled = true;
30
- readonly optionsSchema = {
31
- port: { type: 'number', description: 'WebSocket server port', default: DEFAULT_PORT },
32
- maxRetries: {
33
- type: 'number',
34
- description: 'Port retry attempts when port is occupied',
35
- default: DEFAULT_MAX_RETRIES,
36
- },
37
- };
38
-
39
- private session: IInteractiveSession | null = null;
40
- private stopFn: (() => Promise<void>) | null = null;
41
- private readonly port: number;
42
- private readonly maxRetries: number;
43
-
44
- constructor(config: IWsTransportConfig = {}) {
45
- this.port = config.port ?? DEFAULT_PORT;
46
- this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
47
- }
48
-
49
- attach(session: IInteractiveSession): void {
50
- this.session = session;
51
- }
52
-
53
- async start(): Promise<void> {
54
- if (!this.session) throw new Error('WsTransport: attach() must be called before start()');
55
- const handle = await this.bindWithRetry(this.session, this.port, this.maxRetries);
56
- this.stopFn = handle.stop;
57
- }
58
-
59
- async stop(): Promise<void> {
60
- await this.stopFn?.();
61
- this.stopFn = null;
62
- }
63
-
64
- validateOptions(options: Record<string, TUniversalValue>): boolean {
65
- const { port, maxRetries } = options;
66
- if (port !== undefined && (typeof port !== 'number' || port < 1 || port > 65535)) return false;
67
- if (maxRetries !== undefined && (typeof maxRetries !== 'number' || maxRetries < 0))
68
- return false;
69
- return true;
70
- }
71
-
72
- private bindWithRetry(
73
- session: IInteractiveSession,
74
- port: number,
75
- retriesLeft: number,
76
- ): Promise<{ stop: () => Promise<void> }> {
77
- return this.tryBind(session, port).catch((err: NodeJS.ErrnoException) => {
78
- if (err.code === 'EADDRINUSE' && retriesLeft > 0)
79
- return this.bindWithRetry(session, port + 1, retriesLeft - 1);
80
- throw err;
81
- });
82
- }
83
-
84
- private tryBind(
85
- session: IInteractiveSession,
86
- port: number,
87
- ): Promise<{ stop: () => Promise<void> }> {
88
- return new Promise((resolve, reject) => {
89
- const httpServer: Server = createServer((_, res) => {
90
- res.writeHead(400).end('WebSocket endpoint');
91
- });
92
-
93
- httpServer.on('error', (err: NodeJS.ErrnoException) => {
94
- httpServer.close();
95
- reject(err);
96
- });
97
-
98
- httpServer.listen(port, '127.0.0.1', () => {
99
- const wss = new WebSocketServer({ server: httpServer });
100
-
101
- wss.on('connection', (ws: WebSocket) => {
102
- const send = (message: TServerMessage): void => {
103
- if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message));
104
- };
105
-
106
- const { onMessage, cleanup } = createWsHandler({ session, send });
107
-
108
- ws.on('message', (data) => onMessage(String(data)));
109
- ws.on('close', cleanup);
110
- ws.on('error', cleanup);
111
-
112
- send({ type: 'messages', messages: session.getMessages() });
113
- send({
114
- type: 'execution_workspace_event',
115
- snapshot: session.getExecutionWorkspaceSnapshot(),
116
- });
117
- });
118
-
119
- resolve({
120
- stop: () =>
121
- new Promise<void>((res) => {
122
- wss.close(() => httpServer.close(() => res()));
123
- }),
124
- });
125
- });
126
- });
127
- }
128
- }
@@ -1,42 +0,0 @@
1
- /**
2
- * ITransportAdapter implementation for WebSocket transport.
3
- *
4
- * Wraps createWsHandler into the unified ITransportAdapter interface.
5
- * After start(), the consumer must wire onMessage to their WebSocket.
6
- */
7
-
8
- import { createWsHandler } from './ws-handler.js';
9
-
10
- import type { TServerMessage } from './ws-protocol.js';
11
- import type { IInteractiveSession, ITransportAdapter } from '@robota-sdk/agent-interface-transport';
12
-
13
- export interface IWsTransportOptions {
14
- /** Send a JSON message to the connected WebSocket client. */
15
- send: (message: TServerMessage) => void;
16
- }
17
-
18
- export function createWsTransport(
19
- options: IWsTransportOptions,
20
- ): ITransportAdapter<IInteractiveSession> & { onMessage: ((data: string) => void) | null } {
21
- let session: IInteractiveSession | null = null;
22
- let cleanup: (() => void) | null = null;
23
-
24
- return {
25
- name: 'ws',
26
- onMessage: null,
27
- attach(s: IInteractiveSession) {
28
- session = s;
29
- },
30
- async start() {
31
- if (!session) throw new Error('No session attached. Call attach() first.');
32
- const handler = createWsHandler({ session, send: options.send });
33
- cleanup = handler.cleanup;
34
- this.onMessage = handler.onMessage;
35
- },
36
- async stop() {
37
- cleanup?.();
38
- cleanup = null;
39
- this.onMessage = null;
40
- },
41
- };
42
- }