@robota-sdk/agent-transport 3.0.0-beta.64

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 (183) hide show
  1. package/LICENSE +21 -0
  2. package/dist/node/headless/index.cjs +1 -0
  3. package/dist/node/headless/index.d.ts +2 -0
  4. package/dist/node/headless/index.js +1 -0
  5. package/dist/node/headless-CWEpJXFK.js +7 -0
  6. package/dist/node/headless-CWEpJXFK.js.map +1 -0
  7. package/dist/node/headless-CsZFelG9.cjs +6 -0
  8. package/dist/node/http/index.cjs +1 -0
  9. package/dist/node/http/index.d.ts +2 -0
  10. package/dist/node/http/index.js +1 -0
  11. package/dist/node/http-CM3TJhrF.cjs +1 -0
  12. package/dist/node/http-DwO1AHG-.js +2 -0
  13. package/dist/node/http-DwO1AHG-.js.map +1 -0
  14. package/dist/node/index--Ti9NzQX.d.ts +64 -0
  15. package/dist/node/index--Ti9NzQX.d.ts.map +1 -0
  16. package/dist/node/index-B_rcr14p.d.ts +47 -0
  17. package/dist/node/index-B_rcr14p.d.ts.map +1 -0
  18. package/dist/node/index-C9LWCL4l.d.ts +34 -0
  19. package/dist/node/index-C9LWCL4l.d.ts.map +1 -0
  20. package/dist/node/index-CAr3ioVh.d.ts +64 -0
  21. package/dist/node/index-CAr3ioVh.d.ts.map +1 -0
  22. package/dist/node/index-CEs25wVk.d.ts +213 -0
  23. package/dist/node/index-CEs25wVk.d.ts.map +1 -0
  24. package/dist/node/index-CvXLpjJO.d.ts +213 -0
  25. package/dist/node/index-CvXLpjJO.d.ts.map +1 -0
  26. package/dist/node/index-D34WUfFH.d.ts +26 -0
  27. package/dist/node/index-D34WUfFH.d.ts.map +1 -0
  28. package/dist/node/index-Y0zHb1Bz.d.ts +47 -0
  29. package/dist/node/index-Y0zHb1Bz.d.ts.map +1 -0
  30. package/dist/node/index-k3TUjA-T.d.ts +26 -0
  31. package/dist/node/index-k3TUjA-T.d.ts.map +1 -0
  32. package/dist/node/index-nBlMTFkZ.d.ts +34 -0
  33. package/dist/node/index-nBlMTFkZ.d.ts.map +1 -0
  34. package/dist/node/index.cjs +1 -0
  35. package/dist/node/index.d.ts +6 -0
  36. package/dist/node/index.js +1 -0
  37. package/dist/node/mcp/index.cjs +1 -0
  38. package/dist/node/mcp/index.d.ts +2 -0
  39. package/dist/node/mcp/index.js +1 -0
  40. package/dist/node/mcp-BXBwF6Wu.js +2 -0
  41. package/dist/node/mcp-BXBwF6Wu.js.map +1 -0
  42. package/dist/node/mcp-DcHuGokt.cjs +1 -0
  43. package/dist/node/tui/index.cjs +1 -0
  44. package/dist/node/tui/index.d.ts +2 -0
  45. package/dist/node/tui/index.js +1 -0
  46. package/dist/node/tui-CeD_6rSo.cjs +24 -0
  47. package/dist/node/tui-zmDTPk4b.js +25 -0
  48. package/dist/node/tui-zmDTPk4b.js.map +1 -0
  49. package/dist/node/ws/index.cjs +1 -0
  50. package/dist/node/ws/index.d.ts +2 -0
  51. package/dist/node/ws/index.js +1 -0
  52. package/dist/node/ws-B-oRccFl.js +2 -0
  53. package/dist/node/ws-B-oRccFl.js.map +1 -0
  54. package/dist/node/ws-COnIgnmn.cjs +1 -0
  55. package/package.json +141 -0
  56. package/src/headless/__tests__/headless-runner-initialization.test.ts +45 -0
  57. package/src/headless/__tests__/headless-runner.test.ts +484 -0
  58. package/src/headless/__tests__/headless-skill-activation.integration.test.ts +430 -0
  59. package/src/headless/__tests__/headless-transport.test.ts +268 -0
  60. package/src/headless/headless-runner.ts +141 -0
  61. package/src/headless/headless-stream-json.ts +142 -0
  62. package/src/headless/headless-transport.ts +43 -0
  63. package/src/headless/index.ts +4 -0
  64. package/src/http/__tests__/http-transport.test.ts +55 -0
  65. package/src/http/__tests__/routes.test.ts +168 -0
  66. package/src/http/http-transport.ts +42 -0
  67. package/src/http/index.ts +4 -0
  68. package/src/http/routes.ts +151 -0
  69. package/src/index.ts +5 -0
  70. package/src/mcp/__tests__/mcp-server.test.ts +66 -0
  71. package/src/mcp/__tests__/mcp-transport.test.ts +46 -0
  72. package/src/mcp/index.ts +4 -0
  73. package/src/mcp/mcp-server.ts +162 -0
  74. package/src/mcp/mcp-transport.ts +48 -0
  75. package/src/tui/App.tsx +478 -0
  76. package/src/tui/BackgroundTaskPanel.tsx +34 -0
  77. package/src/tui/CjkTextInput.tsx +204 -0
  78. package/src/tui/ConfirmPrompt.tsx +69 -0
  79. package/src/tui/ExecutionWorkspaceDetailPane.tsx +62 -0
  80. package/src/tui/ExecutionWorkspaceSwitcher.tsx +185 -0
  81. package/src/tui/InkTerminal.ts +42 -0
  82. package/src/tui/InputArea.tsx +298 -0
  83. package/src/tui/InteractivePrompt.tsx +57 -0
  84. package/src/tui/ListPicker.tsx +94 -0
  85. package/src/tui/MenuSelect.tsx +103 -0
  86. package/src/tui/MessageList.tsx +282 -0
  87. package/src/tui/PermissionPrompt.tsx +84 -0
  88. package/src/tui/PluginTUI.tsx +256 -0
  89. package/src/tui/SessionPicker.tsx +66 -0
  90. package/src/tui/SessionStatusBar.tsx +66 -0
  91. package/src/tui/SlashAutocomplete.tsx +110 -0
  92. package/src/tui/StatusBar.tsx +213 -0
  93. package/src/tui/StreamingIndicator.tsx +91 -0
  94. package/src/tui/TextPrompt.tsx +80 -0
  95. package/src/tui/ToolCommandOutput.tsx +37 -0
  96. package/src/tui/ToolDiffBlock.tsx +30 -0
  97. package/src/tui/TransportTUI.tsx +116 -0
  98. package/src/tui/UpdateNotice.tsx +14 -0
  99. package/src/tui/UsageSummaryEntry.tsx +38 -0
  100. package/src/tui/WaveText.tsx +44 -0
  101. package/src/tui/__tests__/InteractivePrompt.test.tsx +82 -0
  102. package/src/tui/__tests__/ListPicker.test.tsx +159 -0
  103. package/src/tui/__tests__/MenuSelect.test.tsx +103 -0
  104. package/src/tui/__tests__/PluginTUI.test.tsx +167 -0
  105. package/src/tui/__tests__/SlashAutocomplete.test.tsx +140 -0
  106. package/src/tui/__tests__/TextPrompt.test.tsx +98 -0
  107. package/src/tui/__tests__/UpdateNotice.test.tsx +15 -0
  108. package/src/tui/__tests__/abort-after-permission.test.tsx +169 -0
  109. package/src/tui/__tests__/abort-streaming-e2e.test.tsx +183 -0
  110. package/src/tui/__tests__/background-task-panel.test.tsx +53 -0
  111. package/src/tui/__tests__/background-task-row-format.test.ts +59 -0
  112. package/src/tui/__tests__/cjk-text-input-flow.test.ts +109 -0
  113. package/src/tui/__tests__/cjk-text-input.test.ts +191 -0
  114. package/src/tui/__tests__/command-effect-handler.test.ts +128 -0
  115. package/src/tui/__tests__/command-output-summary.test.ts +95 -0
  116. package/src/tui/__tests__/compact-event-bridge.test.ts +20 -0
  117. package/src/tui/__tests__/confirm-permission-flow.test.ts +91 -0
  118. package/src/tui/__tests__/confirm-prompt.test.tsx +87 -0
  119. package/src/tui/__tests__/execution-workspace-switcher.test.tsx +110 -0
  120. package/src/tui/__tests__/execution-workspace-view-model.test.ts +93 -0
  121. package/src/tui/__tests__/fixtures/provider-setup-prompt-driver.tsx +122 -0
  122. package/src/tui/__tests__/input-area-flow.test.ts +152 -0
  123. package/src/tui/__tests__/message-list-rendering.test.tsx +353 -0
  124. package/src/tui/__tests__/model-change-side-effect.test.ts +91 -0
  125. package/src/tui/__tests__/prompt-queue.test.tsx +255 -0
  126. package/src/tui/__tests__/provider-setup-pty-e2e.test.ts +233 -0
  127. package/src/tui/__tests__/render-markdown.test.ts +72 -0
  128. package/src/tui/__tests__/selection-flow.test.ts +61 -0
  129. package/src/tui/__tests__/slash-routing-effects.test.ts +225 -0
  130. package/src/tui/__tests__/status-activity.test.ts +71 -0
  131. package/src/tui/__tests__/status-bar.test.tsx +157 -0
  132. package/src/tui/__tests__/streaming-indicator.test.tsx +137 -0
  133. package/src/tui/__tests__/text-prompt-flow.test.ts +77 -0
  134. package/src/tui/__tests__/tui-state-manager.test.ts +401 -0
  135. package/src/tui/background-task-row-format.ts +52 -0
  136. package/src/tui/command-output-summary.ts +122 -0
  137. package/src/tui/execution-workspace-view-model.ts +123 -0
  138. package/src/tui/flows/cjk-text-input-flow.ts +285 -0
  139. package/src/tui/flows/confirm-prompt-flow.ts +45 -0
  140. package/src/tui/flows/input-area-flow.ts +186 -0
  141. package/src/tui/flows/permission-prompt-flow.ts +76 -0
  142. package/src/tui/flows/selection-flow.ts +126 -0
  143. package/src/tui/flows/text-prompt-flow.ts +98 -0
  144. package/src/tui/hooks/command-effect-handler.ts +98 -0
  145. package/src/tui/hooks/command-effect-queue.ts +39 -0
  146. package/src/tui/hooks/model-change-side-effect.ts +63 -0
  147. package/src/tui/hooks/side-effects-types.ts +38 -0
  148. package/src/tui/hooks/use-interactive-session-init.ts +50 -0
  149. package/src/tui/hooks/useAutocomplete.ts +85 -0
  150. package/src/tui/hooks/useInteractiveSession.ts +273 -0
  151. package/src/tui/hooks/usePermissionQueue.ts +51 -0
  152. package/src/tui/hooks/usePluginCallbacks.ts +30 -0
  153. package/src/tui/hooks/usePluginScreenData.ts +84 -0
  154. package/src/tui/hooks/useSideEffects.ts +210 -0
  155. package/src/tui/hooks/useSlashRouting.ts +117 -0
  156. package/src/tui/hooks/useStatusLineSettings.ts +35 -0
  157. package/src/tui/index.ts +3 -0
  158. package/src/tui/plugin-tui-handlers.ts +163 -0
  159. package/src/tui/render-markdown.ts +129 -0
  160. package/src/tui/render.tsx +60 -0
  161. package/src/tui/status-activity.ts +63 -0
  162. package/src/tui/tui-cli-adapter-context.tsx +12 -0
  163. package/src/tui/tui-cli-adapter.ts +25 -0
  164. package/src/tui/tui-state-manager.ts +225 -0
  165. package/src/tui/tui-transport.ts +32 -0
  166. package/src/tui/types.ts +14 -0
  167. package/src/tui/utils/__tests__/edit-diff.test.ts +426 -0
  168. package/src/tui/utils/__tests__/paste-detection.test.ts +116 -0
  169. package/src/tui/utils/__tests__/paste-labels.test.ts +46 -0
  170. package/src/tui/utils/__tests__/tool-call-extractor.test.ts +227 -0
  171. package/src/tui/utils/__tests__/tool-diff-summary.test.ts +104 -0
  172. package/src/tui/utils/edit-diff.ts +152 -0
  173. package/src/tui/utils/paste-labels.ts +9 -0
  174. package/src/tui/utils/tool-call-extractor.ts +91 -0
  175. package/src/tui/utils/tool-diff-summary.ts +75 -0
  176. package/src/ws/__tests__/ws-handler.test.ts +407 -0
  177. package/src/ws/__tests__/ws-transport.test.ts +53 -0
  178. package/src/ws/index.ts +13 -0
  179. package/src/ws/ws-background-messages.ts +170 -0
  180. package/src/ws/ws-handler.ts +279 -0
  181. package/src/ws/ws-protocol.ts +76 -0
  182. package/src/ws/ws-transport-configurable.ts +123 -0
  183. package/src/ws/ws-transport.ts +42 -0
@@ -0,0 +1,170 @@
1
+ import type { IInteractiveSession } from '@robota-sdk/agent-framework';
2
+ import type { TBackgroundControlAction, TClientMessage, TServerMessage } from './ws-protocol.js';
3
+
4
+ export function handleBackgroundQueryMessage(
5
+ session: IInteractiveSession,
6
+ send: (message: TServerMessage) => void,
7
+ msg: Extract<
8
+ TClientMessage,
9
+ | { type: 'get-background-tasks' | 'get-background-task' | 'read-background-task-log' }
10
+ | {
11
+ type:
12
+ | 'get-background-job-groups'
13
+ | 'get-background-job-group'
14
+ | 'wait-background-job-group';
15
+ }
16
+ >,
17
+ ): void {
18
+ if (msg.type === 'get-background-tasks') {
19
+ send({ type: 'background_tasks', tasks: session.listBackgroundTasks(msg.filter) });
20
+ return;
21
+ }
22
+ if (msg.type === 'get-background-task') {
23
+ sendBackgroundTaskSnapshot(session, send, msg);
24
+ return;
25
+ }
26
+ if (msg.type === 'get-background-job-groups') {
27
+ send({ type: 'background_job_groups', groups: session.listBackgroundJobGroups() });
28
+ return;
29
+ }
30
+ if (msg.type === 'get-background-job-group') {
31
+ sendBackgroundJobGroupSnapshot(session, send, msg);
32
+ return;
33
+ }
34
+ if (msg.type === 'wait-background-job-group') {
35
+ sendBackgroundJobGroupWaitResult(session, send, msg);
36
+ return;
37
+ }
38
+ sendBackgroundTaskLogPage(session, send, msg);
39
+ }
40
+
41
+ export function handleBackgroundControlMessage(
42
+ session: IInteractiveSession,
43
+ send: (message: TServerMessage) => void,
44
+ msg: Extract<
45
+ TClientMessage,
46
+ { type: 'cancel-background-task' | 'close-background-task' | 'send-background-task' }
47
+ >,
48
+ ): void {
49
+ if (!msg.taskId) {
50
+ send({ type: 'protocol_error', message: 'taskId is required' });
51
+ return;
52
+ }
53
+ if (msg.type === 'cancel-background-task') {
54
+ sendBackgroundTaskControlResult(
55
+ send,
56
+ 'cancel',
57
+ msg.taskId,
58
+ session.cancelBackgroundTask(msg.taskId, msg.reason),
59
+ );
60
+ return;
61
+ }
62
+ if (msg.type === 'close-background-task') {
63
+ sendBackgroundTaskControlResult(
64
+ send,
65
+ 'close',
66
+ msg.taskId,
67
+ session.closeBackgroundTask(msg.taskId),
68
+ );
69
+ return;
70
+ }
71
+ sendBackgroundTaskInput(session, send, msg);
72
+ }
73
+
74
+ function sendBackgroundTaskSnapshot(
75
+ session: IInteractiveSession,
76
+ send: (message: TServerMessage) => void,
77
+ msg: Extract<TClientMessage, { type: 'get-background-task' }>,
78
+ ): void {
79
+ if (!msg.taskId) {
80
+ send({ type: 'protocol_error', message: 'taskId is required' });
81
+ return;
82
+ }
83
+ send({
84
+ type: 'background_task',
85
+ taskId: msg.taskId,
86
+ task: session.getBackgroundTask(msg.taskId) ?? null,
87
+ });
88
+ }
89
+
90
+ function sendBackgroundTaskLogPage(
91
+ session: IInteractiveSession,
92
+ send: (message: TServerMessage) => void,
93
+ msg: Extract<TClientMessage, { type: 'read-background-task-log' }>,
94
+ ): void {
95
+ if (!msg.taskId) {
96
+ send({ type: 'protocol_error', message: 'taskId is required' });
97
+ return;
98
+ }
99
+ session.readBackgroundTaskLog(msg.taskId, msg.cursor).then(
100
+ (page) => send({ type: 'background_task_log', taskId: msg.taskId, page }),
101
+ (error: Error) => send({ type: 'protocol_error', message: error.message }),
102
+ );
103
+ }
104
+
105
+ function sendBackgroundJobGroupSnapshot(
106
+ session: IInteractiveSession,
107
+ send: (message: TServerMessage) => void,
108
+ msg: Extract<TClientMessage, { type: 'get-background-job-group' }>,
109
+ ): void {
110
+ if (!msg.groupId) {
111
+ send({ type: 'protocol_error', message: 'groupId is required' });
112
+ return;
113
+ }
114
+ send({
115
+ type: 'background_job_group',
116
+ groupId: msg.groupId,
117
+ group: session.getBackgroundJobGroup(msg.groupId) ?? null,
118
+ });
119
+ }
120
+
121
+ function sendBackgroundJobGroupWaitResult(
122
+ session: IInteractiveSession,
123
+ send: (message: TServerMessage) => void,
124
+ msg: Extract<TClientMessage, { type: 'wait-background-job-group' }>,
125
+ ): void {
126
+ if (!msg.groupId) {
127
+ send({ type: 'protocol_error', message: 'groupId is required' });
128
+ return;
129
+ }
130
+ session.waitBackgroundJobGroup(msg.groupId).then(
131
+ (group) => send({ type: 'background_job_group', groupId: msg.groupId, group }),
132
+ (error: Error) => send({ type: 'protocol_error', message: error.message }),
133
+ );
134
+ }
135
+
136
+ function sendBackgroundTaskInput(
137
+ session: IInteractiveSession,
138
+ send: (message: TServerMessage) => void,
139
+ msg: Extract<TClientMessage, { type: 'send-background-task' }>,
140
+ ): void {
141
+ if (!msg.input) {
142
+ send({ type: 'protocol_error', message: 'input is required' });
143
+ return;
144
+ }
145
+ sendBackgroundTaskControlResult(
146
+ send,
147
+ 'send',
148
+ msg.taskId,
149
+ session.sendBackgroundTask(msg.taskId, msg.input),
150
+ );
151
+ }
152
+
153
+ function sendBackgroundTaskControlResult(
154
+ send: (message: TServerMessage) => void,
155
+ action: TBackgroundControlAction,
156
+ taskId: string,
157
+ operation: Promise<void>,
158
+ ): void {
159
+ operation.then(
160
+ () => send({ type: 'background_task_control_result', action, taskId, success: true }),
161
+ (error: Error) =>
162
+ send({
163
+ type: 'background_task_control_result',
164
+ action,
165
+ taskId,
166
+ success: false,
167
+ message: error.message,
168
+ }),
169
+ );
170
+ }
@@ -0,0 +1,279 @@
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 type {
12
+ IInteractiveSession,
13
+ IExecutionResult,
14
+ IExecutionWorkspaceEvent,
15
+ TBackgroundJobGroupEvent,
16
+ TBackgroundTaskEvent,
17
+ IToolState,
18
+ } from '@robota-sdk/agent-framework';
19
+ import type { TClientMessage, TServerMessage } from './ws-protocol.js';
20
+ import {
21
+ handleBackgroundControlMessage,
22
+ handleBackgroundQueryMessage,
23
+ } from './ws-background-messages.js';
24
+
25
+ export interface IWsHandlerOptions {
26
+ /** IInteractiveSession to expose. */
27
+ session: IInteractiveSession;
28
+ /** Send a JSON message to the client. */
29
+ send: (message: TServerMessage) => void;
30
+ }
31
+
32
+ /**
33
+ * Create a WebSocket message handler for an IInteractiveSession.
34
+ *
35
+ * Returns:
36
+ * - `onMessage(data)`: call this when the WebSocket receives a message
37
+ * - `cleanup()`: call this when the WebSocket disconnects
38
+ *
39
+ * Usage:
40
+ * ```typescript
41
+ * const { onMessage, cleanup } = createWsHandler({
42
+ * session: interactiveSession,
43
+ * send: (msg) => ws.send(JSON.stringify(msg)),
44
+ * });
45
+ *
46
+ * ws.on('message', (data) => onMessage(String(data)));
47
+ * ws.on('close', cleanup);
48
+ * ```
49
+ */
50
+ export function createWsHandler(options: IWsHandlerOptions): {
51
+ onMessage: (data: string) => void;
52
+ cleanup: () => void;
53
+ } {
54
+ const cleanup = subscribeSessionEvents(options.session, options.send);
55
+ const onMessage = createWsMessageHandler(options.session, options.send);
56
+
57
+ return { onMessage, cleanup };
58
+ }
59
+
60
+ function subscribeSessionEvents(
61
+ session: IInteractiveSession,
62
+ send: (message: TServerMessage) => void,
63
+ ): () => void {
64
+ const onUserMessage = (content: string): void => send({ type: 'user_message', content });
65
+ const onTextDelta = (delta: string): void => send({ type: 'text_delta', delta });
66
+ const onToolStart = (state: IToolState): void => send({ type: 'tool_start', state });
67
+ const onToolEnd = (state: IToolState): void => send({ type: 'tool_end', state });
68
+ const onThinking = (isThinking: boolean): void => send({ type: 'thinking', isThinking });
69
+ const onComplete = (result: IExecutionResult): void => send({ type: 'complete', result });
70
+ const onInterrupted = (result: IExecutionResult): void => send({ type: 'interrupted', result });
71
+ const onError = (error: Error): void => send({ type: 'error', message: error.message });
72
+ const onBackgroundTaskEvent = (event: TBackgroundTaskEvent): void =>
73
+ send({ type: 'background_task_event', event });
74
+ const onBackgroundJobGroupEvent = (event: TBackgroundJobGroupEvent): void =>
75
+ send({ type: 'background_job_group_event', event });
76
+ const onExecutionWorkspace = (event: IExecutionWorkspaceEvent): void =>
77
+ send({ type: 'execution_workspace_event', snapshot: event.snapshot });
78
+
79
+ session.on('user_message', onUserMessage);
80
+ session.on('text_delta', onTextDelta);
81
+ session.on('tool_start', onToolStart);
82
+ session.on('tool_end', onToolEnd);
83
+ session.on('thinking', onThinking);
84
+ session.on('complete', onComplete);
85
+ session.on('interrupted', onInterrupted);
86
+ session.on('error', onError);
87
+ session.on('background_task_event', onBackgroundTaskEvent);
88
+ session.on('background_job_group_event', onBackgroundJobGroupEvent);
89
+ session.on('execution_workspace_event', onExecutionWorkspace);
90
+
91
+ return (): void => {
92
+ session.off('user_message', onUserMessage);
93
+ session.off('text_delta', onTextDelta);
94
+ session.off('tool_start', onToolStart);
95
+ session.off('tool_end', onToolEnd);
96
+ session.off('thinking', onThinking);
97
+ session.off('complete', onComplete);
98
+ session.off('interrupted', onInterrupted);
99
+ session.off('error', onError);
100
+ session.off('background_task_event', onBackgroundTaskEvent);
101
+ session.off('background_job_group_event', onBackgroundJobGroupEvent);
102
+ session.off('execution_workspace_event', onExecutionWorkspace);
103
+ };
104
+ }
105
+
106
+ function createWsMessageHandler(
107
+ session: IInteractiveSession,
108
+ send: (message: TServerMessage) => void,
109
+ ): (data: string) => void {
110
+ return (data: string): void => {
111
+ const msg = parseClientMessage(data, send);
112
+ if (!msg) return;
113
+ handleClientMessage(session, send, msg);
114
+ };
115
+ }
116
+
117
+ function parseClientMessage(
118
+ data: string,
119
+ send: (message: TServerMessage) => void,
120
+ ): TClientMessage | null {
121
+ try {
122
+ return JSON.parse(data) as TClientMessage;
123
+ } catch {
124
+ send({ type: 'protocol_error', message: 'Invalid JSON' });
125
+ return null;
126
+ }
127
+ }
128
+
129
+ function handleClientMessage(
130
+ session: IInteractiveSession,
131
+ send: (message: TServerMessage) => void,
132
+ msg: TClientMessage,
133
+ ): void {
134
+ if (isSessionControlMessage(msg)) {
135
+ handleSessionControlMessage(session, send, msg);
136
+ return;
137
+ }
138
+ if (isSessionQueryMessage(msg)) {
139
+ handleSessionQueryMessage(session, send, msg);
140
+ return;
141
+ }
142
+ if (isBackgroundQueryMessage(msg)) {
143
+ handleBackgroundQueryMessage(session, send, msg);
144
+ return;
145
+ }
146
+ if (isBackgroundControlMessage(msg)) {
147
+ handleBackgroundControlMessage(session, send, msg);
148
+ return;
149
+ }
150
+ send({ type: 'protocol_error', message: `Unknown message type: ${getMessageType(msg)}` });
151
+ }
152
+
153
+ function getMessageType(msg: TClientMessage): string {
154
+ return (msg as { type: string }).type;
155
+ }
156
+
157
+ function isSessionControlMessage(
158
+ msg: TClientMessage,
159
+ ): msg is Extract<TClientMessage, { type: 'submit' | 'command' | 'abort' | 'cancel-queue' }> {
160
+ return (
161
+ msg.type === 'submit' ||
162
+ msg.type === 'command' ||
163
+ msg.type === 'abort' ||
164
+ msg.type === 'cancel-queue'
165
+ );
166
+ }
167
+
168
+ function isSessionQueryMessage(msg: TClientMessage): msg is Extract<
169
+ TClientMessage,
170
+ {
171
+ type:
172
+ | 'get-messages'
173
+ | 'get-context'
174
+ | 'get-executing'
175
+ | 'get-pending'
176
+ | 'get-execution-workspace';
177
+ }
178
+ > {
179
+ return (
180
+ msg.type === 'get-messages' ||
181
+ msg.type === 'get-context' ||
182
+ msg.type === 'get-executing' ||
183
+ msg.type === 'get-pending' ||
184
+ msg.type === 'get-execution-workspace'
185
+ );
186
+ }
187
+
188
+ function isBackgroundQueryMessage(
189
+ msg: TClientMessage,
190
+ ): msg is Extract<
191
+ TClientMessage,
192
+ | { type: 'get-background-tasks' | 'get-background-task' | 'read-background-task-log' }
193
+ | { type: 'get-background-job-groups' | 'get-background-job-group' | 'wait-background-job-group' }
194
+ > {
195
+ return (
196
+ msg.type === 'get-background-tasks' ||
197
+ msg.type === 'get-background-task' ||
198
+ msg.type === 'read-background-task-log' ||
199
+ msg.type === 'get-background-job-groups' ||
200
+ msg.type === 'get-background-job-group' ||
201
+ msg.type === 'wait-background-job-group'
202
+ );
203
+ }
204
+
205
+ function isBackgroundControlMessage(
206
+ msg: TClientMessage,
207
+ ): msg is Extract<
208
+ TClientMessage,
209
+ { type: 'cancel-background-task' | 'close-background-task' | 'send-background-task' }
210
+ > {
211
+ return (
212
+ msg.type === 'cancel-background-task' ||
213
+ msg.type === 'close-background-task' ||
214
+ msg.type === 'send-background-task'
215
+ );
216
+ }
217
+
218
+ function handleSessionControlMessage(
219
+ session: IInteractiveSession,
220
+ send: (message: TServerMessage) => void,
221
+ msg: Extract<TClientMessage, { type: 'submit' | 'command' | 'abort' | 'cancel-queue' }>,
222
+ ): void {
223
+ if (msg.type === 'submit') {
224
+ if (!msg.prompt) {
225
+ send({ type: 'protocol_error', message: 'prompt is required' });
226
+ return;
227
+ }
228
+ session.submit(msg.prompt);
229
+ } else if (msg.type === 'command') {
230
+ if (!msg.name) {
231
+ send({ type: 'protocol_error', message: 'name is required' });
232
+ return;
233
+ }
234
+ session.executeCommand(msg.name, msg.args ?? '').then((result) => {
235
+ send({
236
+ type: 'command_result',
237
+ name: msg.name,
238
+ message: result?.message ?? `Unknown command: ${msg.name}`,
239
+ success: result?.success ?? false,
240
+ data: result?.data,
241
+ });
242
+ });
243
+ } else if (msg.type === 'abort') {
244
+ session.abort();
245
+ } else {
246
+ session.cancelQueue();
247
+ }
248
+ }
249
+
250
+ function handleSessionQueryMessage(
251
+ session: IInteractiveSession,
252
+ send: (message: TServerMessage) => void,
253
+ msg: Extract<
254
+ TClientMessage,
255
+ {
256
+ type:
257
+ | 'get-messages'
258
+ | 'get-context'
259
+ | 'get-executing'
260
+ | 'get-pending'
261
+ | 'get-execution-workspace';
262
+ }
263
+ >,
264
+ ): void {
265
+ if (msg.type === 'get-messages') {
266
+ send({ type: 'messages', messages: session.getMessages() });
267
+ } else if (msg.type === 'get-context') {
268
+ send({ type: 'context', state: session.getContextState() });
269
+ } else if (msg.type === 'get-executing') {
270
+ send({ type: 'executing', executing: session.isExecuting() });
271
+ } else if (msg.type === 'get-execution-workspace') {
272
+ send({
273
+ type: 'execution_workspace_event',
274
+ snapshot: session.getExecutionWorkspaceSnapshot(),
275
+ });
276
+ } else {
277
+ send({ type: 'pending', pending: session.getPendingPrompt() });
278
+ }
279
+ }
@@ -0,0 +1,76 @@
1
+ import type {
2
+ InteractiveSession,
3
+ ICommandResult,
4
+ IBackgroundJobGroupState,
5
+ IBackgroundTaskInput,
6
+ IBackgroundTaskListFilter,
7
+ IBackgroundTaskLogCursor,
8
+ IBackgroundTaskLogPage,
9
+ IBackgroundTaskState,
10
+ IExecutionResult,
11
+ IExecutionWorkspaceSnapshot,
12
+ IToolState,
13
+ TBackgroundJobGroupEvent,
14
+ TBackgroundTaskEvent,
15
+ } from '@robota-sdk/agent-framework';
16
+
17
+ export type TBackgroundControlAction = 'cancel' | 'close' | 'send';
18
+
19
+ /** Inbound message from client to server. */
20
+ export type TClientMessage =
21
+ | { type: 'submit'; prompt: string }
22
+ | { type: 'command'; name: string; args?: string }
23
+ | { type: 'abort' }
24
+ | { type: 'cancel-queue' }
25
+ | { type: 'get-messages' }
26
+ | { type: 'get-context' }
27
+ | { type: 'get-executing' }
28
+ | { type: 'get-pending' }
29
+ | { type: 'get-execution-workspace' }
30
+ | { type: 'get-background-tasks'; filter?: IBackgroundTaskListFilter }
31
+ | { type: 'get-background-task'; taskId: string }
32
+ | { type: 'get-background-job-groups' }
33
+ | { type: 'get-background-job-group'; groupId: string }
34
+ | { type: 'wait-background-job-group'; groupId: string }
35
+ | { type: 'cancel-background-task'; taskId: string; reason?: string }
36
+ | { type: 'close-background-task'; taskId: string }
37
+ | { type: 'send-background-task'; taskId: string; input: IBackgroundTaskInput }
38
+ | { type: 'read-background-task-log'; taskId: string; cursor?: IBackgroundTaskLogCursor };
39
+
40
+ /** Outbound message from server to client. */
41
+ export type TServerMessage =
42
+ | { type: 'text_delta'; delta: string }
43
+ | { type: 'user_message'; content: string }
44
+ | { type: 'tool_start'; state: IToolState }
45
+ | { type: 'tool_end'; state: IToolState }
46
+ | { type: 'thinking'; isThinking: boolean }
47
+ | { type: 'complete'; result: IExecutionResult }
48
+ | { type: 'interrupted'; result: IExecutionResult }
49
+ | { type: 'error'; message: string }
50
+ | {
51
+ type: 'command_result';
52
+ name: string;
53
+ message: string;
54
+ success: boolean;
55
+ data?: ICommandResult['data'];
56
+ }
57
+ | { type: 'messages'; messages: ReturnType<InteractiveSession['getMessages']> }
58
+ | { type: 'context'; state: ReturnType<InteractiveSession['getContextState']> }
59
+ | { type: 'executing'; executing: boolean }
60
+ | { type: 'pending'; pending: string | null }
61
+ | { type: 'execution_workspace_event'; snapshot: IExecutionWorkspaceSnapshot }
62
+ | { type: 'background_task_event'; event: TBackgroundTaskEvent }
63
+ | { type: 'background_job_group_event'; event: TBackgroundJobGroupEvent }
64
+ | { type: 'background_tasks'; tasks: IBackgroundTaskState[] }
65
+ | { type: 'background_task'; taskId: string; task: IBackgroundTaskState | null }
66
+ | { type: 'background_job_groups'; groups: IBackgroundJobGroupState[] }
67
+ | { type: 'background_job_group'; groupId: string; group: IBackgroundJobGroupState | null }
68
+ | { type: 'background_task_log'; taskId: string; page: IBackgroundTaskLogPage }
69
+ | {
70
+ type: 'background_task_control_result';
71
+ action: TBackgroundControlAction;
72
+ taskId: string;
73
+ success: boolean;
74
+ message?: string;
75
+ }
76
+ | { type: 'protocol_error'; message: string };
@@ -0,0 +1,123 @@
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
+ import { WebSocketServer, WebSocket } from 'ws';
8
+ import type { IInteractiveSession } from '@robota-sdk/agent-framework';
9
+ import type { TUniversalValue } from '@robota-sdk/agent-core';
10
+ import type { IConfigurableTransport } from '@robota-sdk/agent-interface-transport';
11
+ import { createWsHandler } from './ws-handler.js';
12
+ import type { TServerMessage } from './ws-protocol.js';
13
+
14
+ const DEFAULT_PORT = 7070;
15
+ const DEFAULT_MAX_RETRIES = 20;
16
+
17
+ export interface IWsTransportConfig {
18
+ port?: number;
19
+ maxRetries?: number;
20
+ }
21
+
22
+ export class WsTransport implements IConfigurableTransport<IInteractiveSession> {
23
+ readonly name = 'ws';
24
+ readonly defaultEnabled = true;
25
+ readonly optionsSchema = {
26
+ port: { type: 'number', description: 'WebSocket server port', default: DEFAULT_PORT },
27
+ maxRetries: {
28
+ type: 'number',
29
+ description: 'Port retry attempts when port is occupied',
30
+ default: DEFAULT_MAX_RETRIES,
31
+ },
32
+ };
33
+
34
+ private session: IInteractiveSession | null = null;
35
+ private stopFn: (() => Promise<void>) | null = null;
36
+ private readonly port: number;
37
+ private readonly maxRetries: number;
38
+
39
+ constructor(config: IWsTransportConfig = {}) {
40
+ this.port = config.port ?? DEFAULT_PORT;
41
+ this.maxRetries = config.maxRetries ?? DEFAULT_MAX_RETRIES;
42
+ }
43
+
44
+ attach(session: IInteractiveSession): void {
45
+ this.session = session;
46
+ }
47
+
48
+ async start(): Promise<void> {
49
+ if (!this.session) throw new Error('WsTransport: attach() must be called before start()');
50
+ const handle = await this.bindWithRetry(this.session, this.port, this.maxRetries);
51
+ this.stopFn = handle.stop;
52
+ }
53
+
54
+ async stop(): Promise<void> {
55
+ await this.stopFn?.();
56
+ this.stopFn = null;
57
+ }
58
+
59
+ validateOptions(options: Record<string, TUniversalValue>): boolean {
60
+ const { port, maxRetries } = options;
61
+ if (port !== undefined && (typeof port !== 'number' || port < 1 || port > 65535)) return false;
62
+ if (maxRetries !== undefined && (typeof maxRetries !== 'number' || maxRetries < 0))
63
+ return false;
64
+ return true;
65
+ }
66
+
67
+ private bindWithRetry(
68
+ session: IInteractiveSession,
69
+ port: number,
70
+ retriesLeft: number,
71
+ ): Promise<{ stop: () => Promise<void> }> {
72
+ return this.tryBind(session, port).catch((err: NodeJS.ErrnoException) => {
73
+ if (err.code === 'EADDRINUSE' && retriesLeft > 0)
74
+ return this.bindWithRetry(session, port + 1, retriesLeft - 1);
75
+ throw err;
76
+ });
77
+ }
78
+
79
+ private tryBind(
80
+ session: IInteractiveSession,
81
+ port: number,
82
+ ): Promise<{ stop: () => Promise<void> }> {
83
+ return new Promise((resolve, reject) => {
84
+ const httpServer: Server = createServer((_, res) => {
85
+ res.writeHead(400).end('WebSocket endpoint');
86
+ });
87
+
88
+ httpServer.on('error', (err: NodeJS.ErrnoException) => {
89
+ httpServer.close();
90
+ reject(err);
91
+ });
92
+
93
+ httpServer.listen(port, '127.0.0.1', () => {
94
+ const wss = new WebSocketServer({ server: httpServer });
95
+
96
+ wss.on('connection', (ws: WebSocket) => {
97
+ const send = (message: TServerMessage): void => {
98
+ if (ws.readyState === WebSocket.OPEN) ws.send(JSON.stringify(message));
99
+ };
100
+
101
+ const { onMessage, cleanup } = createWsHandler({ session, send });
102
+
103
+ ws.on('message', (data) => onMessage(String(data)));
104
+ ws.on('close', cleanup);
105
+ ws.on('error', cleanup);
106
+
107
+ send({ type: 'messages', messages: session.getMessages() });
108
+ send({
109
+ type: 'execution_workspace_event',
110
+ snapshot: session.getExecutionWorkspaceSnapshot(),
111
+ });
112
+ });
113
+
114
+ resolve({
115
+ stop: () =>
116
+ new Promise<void>((res) => {
117
+ wss.close(() => httpServer.close(() => res()));
118
+ }),
119
+ });
120
+ });
121
+ });
122
+ }
123
+ }
@@ -0,0 +1,42 @@
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 type { IInteractiveSession } from '@robota-sdk/agent-framework';
9
+ import type { ITransportAdapter } from '@robota-sdk/agent-interface-transport';
10
+ import { createWsHandler } from './ws-handler.js';
11
+ import type { TServerMessage } from './ws-protocol.js';
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
+ }