@celljs/ta-host 4.5.0 → 4.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +31 -8
- package/cell.schema.json +18 -0
- package/cell.yml +4 -0
- package/esm/node/auth-server-impl.d.ts +61 -0
- package/esm/node/auth-server-impl.js +247 -0
- package/esm/node/auth-server-impl.js.map +1 -0
- package/esm/node/client-tool-broker.d.ts +35 -0
- package/esm/node/client-tool-broker.js +82 -0
- package/esm/node/client-tool-broker.js.map +1 -0
- package/esm/node/exec-environment-factory.d.ts +17 -0
- package/esm/node/exec-environment-factory.js +58 -0
- package/esm/node/exec-environment-factory.js.map +1 -0
- package/esm/node/extension-data-notifier.d.ts +41 -0
- package/esm/node/extension-data-notifier.js +119 -0
- package/esm/node/extension-data-notifier.js.map +1 -0
- package/esm/node/extension-data.d.ts +45 -0
- package/esm/node/extension-data.js +181 -0
- package/esm/node/extension-data.js.map +1 -0
- package/esm/node/host-protocol.d.ts +29 -1
- package/esm/node/host-protocol.js +2 -0
- package/esm/node/host-protocol.js.map +1 -1
- package/esm/node/index.d.ts +24 -1
- package/esm/node/index.js +24 -1
- package/esm/node/index.js.map +1 -1
- package/esm/node/package-host-extension.d.ts +9 -0
- package/esm/node/package-host-extension.js +18 -0
- package/esm/node/package-host-extension.js.map +1 -0
- package/esm/node/package-hosts.d.ts +44 -0
- package/esm/node/package-hosts.js +167 -0
- package/esm/node/package-hosts.js.map +1 -0
- package/esm/node/package-server-impl.d.ts +25 -0
- package/esm/node/package-server-impl.js +94 -0
- package/esm/node/package-server-impl.js.map +1 -0
- package/esm/node/package-switch.d.ts +29 -0
- package/esm/node/package-switch.js +68 -0
- package/esm/node/package-switch.js.map +1 -0
- package/esm/node/relay-bridge.d.ts +34 -0
- package/esm/node/relay-bridge.js +105 -0
- package/esm/node/relay-bridge.js.map +1 -0
- package/esm/node/session-info-util.d.ts +16 -0
- package/esm/node/session-info-util.js +49 -0
- package/esm/node/session-info-util.js.map +1 -0
- package/esm/node/session-storage/file-session-storage.d.ts +27 -0
- package/esm/node/session-storage/file-session-storage.js +139 -0
- package/esm/node/session-storage/file-session-storage.js.map +1 -0
- package/esm/node/session-storage/index.d.ts +3 -0
- package/esm/node/session-storage/index.js +4 -0
- package/esm/node/session-storage/index.js.map +1 -0
- package/esm/node/session-storage/session-storage-protocol.d.ts +57 -0
- package/esm/node/session-storage/session-storage-protocol.js +40 -0
- package/esm/node/session-storage/session-storage-protocol.js.map +1 -0
- package/esm/node/session-storage/stored-session.d.ts +46 -0
- package/esm/node/session-storage/stored-session.js +116 -0
- package/esm/node/session-storage/stored-session.js.map +1 -0
- package/esm/node/settings-server-impl.d.ts +11 -0
- package/esm/node/settings-server-impl.js +45 -0
- package/esm/node/settings-server-impl.js.map +1 -0
- package/esm/node/settings-store.d.ts +56 -0
- package/esm/node/settings-store.js +306 -0
- package/esm/node/settings-store.js.map +1 -0
- package/esm/node/ta-access.d.ts +19 -2
- package/esm/node/ta-access.js +64 -7
- package/esm/node/ta-access.js.map +1 -1
- package/esm/node/ta-errors.d.ts +3 -1
- package/esm/node/ta-errors.js +17 -1
- package/esm/node/ta-errors.js.map +1 -1
- package/esm/node/ta-server-impl.d.ts +17 -5
- package/esm/node/ta-server-impl.js +70 -13
- package/esm/node/ta-server-impl.js.map +1 -1
- package/esm/node/ta-session-factory.d.ts +2 -0
- package/esm/node/ta-session-factory.js +25 -11
- package/esm/node/ta-session-factory.js.map +1 -1
- package/esm/node/ta-session.d.ts +9 -1
- package/esm/node/ta-session.js +26 -2
- package/esm/node/ta-session.js.map +1 -1
- package/esm/node/terminal-protocol.d.ts +18 -0
- package/esm/node/terminal-protocol.js +2 -0
- package/esm/node/terminal-protocol.js.map +1 -0
- package/esm/node/terminal-service.d.ts +59 -0
- package/esm/node/terminal-service.js +240 -0
- package/esm/node/terminal-service.js.map +1 -0
- package/esm/node/terminal-util.d.ts +62 -0
- package/esm/node/terminal-util.js +153 -0
- package/esm/node/terminal-util.js.map +1 -0
- package/esm/node/web-extension-catalog.d.ts +38 -0
- package/esm/node/web-extension-catalog.js +160 -0
- package/esm/node/web-extension-catalog.js.map +1 -0
- package/esm/node/web-extension-util.d.ts +21 -0
- package/esm/node/web-extension-util.js +123 -0
- package/esm/node/web-extension-util.js.map +1 -0
- package/esm/node/working-directory.d.ts +13 -0
- package/esm/node/working-directory.js +40 -0
- package/esm/node/working-directory.js.map +1 -0
- package/esm/node/workspace-files.d.ts +47 -0
- package/esm/node/workspace-files.js +265 -0
- package/esm/node/workspace-files.js.map +1 -0
- package/esm/node/workspace-protocol.d.ts +20 -0
- package/esm/node/workspace-protocol.js +3 -0
- package/esm/node/workspace-protocol.js.map +1 -0
- package/esm/node/workspace-server-impl.d.ts +27 -0
- package/esm/node/workspace-server-impl.js +88 -0
- package/esm/node/workspace-server-impl.js.map +1 -0
- package/esm/node/workspace-service.d.ts +31 -0
- package/esm/node/workspace-service.js +209 -0
- package/esm/node/workspace-service.js.map +1 -0
- package/esm/node/workspace-util.d.ts +29 -0
- package/esm/node/workspace-util.js +104 -0
- package/esm/node/workspace-util.js.map +1 -0
- package/lib/node/auth-server-impl.d.ts +62 -0
- package/lib/node/auth-server-impl.d.ts.map +1 -0
- package/lib/node/auth-server-impl.js +250 -0
- package/lib/node/auth-server-impl.js.map +1 -0
- package/lib/node/auth-server-impl.spec.d.ts +2 -0
- package/lib/node/auth-server-impl.spec.d.ts.map +1 -0
- package/lib/node/auth-server-impl.spec.js +47 -0
- package/lib/node/auth-server-impl.spec.js.map +1 -0
- package/lib/node/client-tool-broker.d.ts +36 -0
- package/lib/node/client-tool-broker.d.ts.map +1 -0
- package/lib/node/client-tool-broker.js +86 -0
- package/lib/node/client-tool-broker.js.map +1 -0
- package/lib/node/client-tool-broker.spec.d.ts +2 -0
- package/lib/node/client-tool-broker.spec.d.ts.map +1 -0
- package/lib/node/client-tool-broker.spec.js +49 -0
- package/lib/node/client-tool-broker.spec.js.map +1 -0
- package/lib/node/exec-environment-factory.d.ts +18 -0
- package/lib/node/exec-environment-factory.d.ts.map +1 -0
- package/lib/node/exec-environment-factory.js +61 -0
- package/lib/node/exec-environment-factory.js.map +1 -0
- package/lib/node/extension-data-notifier.d.ts +42 -0
- package/lib/node/extension-data-notifier.d.ts.map +1 -0
- package/lib/node/extension-data-notifier.js +123 -0
- package/lib/node/extension-data-notifier.js.map +1 -0
- package/lib/node/extension-data-notifier.spec.d.ts +2 -0
- package/lib/node/extension-data-notifier.spec.d.ts.map +1 -0
- package/lib/node/extension-data-notifier.spec.js +35 -0
- package/lib/node/extension-data-notifier.spec.js.map +1 -0
- package/lib/node/extension-data.d.ts +46 -0
- package/lib/node/extension-data.d.ts.map +1 -0
- package/lib/node/extension-data.js +184 -0
- package/lib/node/extension-data.js.map +1 -0
- package/lib/node/extension-data.spec.d.ts +2 -0
- package/lib/node/extension-data.spec.d.ts.map +1 -0
- package/lib/node/extension-data.spec.js +99 -0
- package/lib/node/extension-data.spec.js.map +1 -0
- package/lib/node/host-protocol.d.ts +29 -1
- package/lib/node/host-protocol.d.ts.map +1 -1
- package/lib/node/host-protocol.js +3 -1
- package/lib/node/host-protocol.js.map +1 -1
- package/lib/node/index.d.ts +24 -1
- package/lib/node/index.d.ts.map +1 -1
- package/lib/node/index.js +24 -1
- package/lib/node/index.js.map +1 -1
- package/lib/node/package-host-extension.d.ts +10 -0
- package/lib/node/package-host-extension.d.ts.map +1 -0
- package/lib/node/package-host-extension.js +22 -0
- package/lib/node/package-host-extension.js.map +1 -0
- package/lib/node/package-hosts.d.ts +45 -0
- package/lib/node/package-hosts.d.ts.map +1 -0
- package/lib/node/package-hosts.js +170 -0
- package/lib/node/package-hosts.js.map +1 -0
- package/lib/node/package-hosts.spec.d.ts +2 -0
- package/lib/node/package-hosts.spec.d.ts.map +1 -0
- package/lib/node/package-hosts.spec.js +53 -0
- package/lib/node/package-hosts.spec.js.map +1 -0
- package/lib/node/package-server-impl.d.ts +26 -0
- package/lib/node/package-server-impl.d.ts.map +1 -0
- package/lib/node/package-server-impl.js +97 -0
- package/lib/node/package-server-impl.js.map +1 -0
- package/lib/node/package-switch.d.ts +30 -0
- package/lib/node/package-switch.d.ts.map +1 -0
- package/lib/node/package-switch.js +71 -0
- package/lib/node/package-switch.js.map +1 -0
- package/lib/node/relay-bridge.d.ts +35 -0
- package/lib/node/relay-bridge.d.ts.map +1 -0
- package/lib/node/relay-bridge.js +108 -0
- package/lib/node/relay-bridge.js.map +1 -0
- package/lib/node/session-info-util.d.ts +17 -0
- package/lib/node/session-info-util.d.ts.map +1 -0
- package/lib/node/session-info-util.js +52 -0
- package/lib/node/session-info-util.js.map +1 -0
- package/lib/node/session-info-util.spec.d.ts +2 -0
- package/lib/node/session-info-util.spec.d.ts.map +1 -0
- package/lib/node/session-info-util.spec.js +22 -0
- package/lib/node/session-info-util.spec.js.map +1 -0
- package/lib/node/session-storage/file-session-storage.d.ts +28 -0
- package/lib/node/session-storage/file-session-storage.d.ts.map +1 -0
- package/lib/node/session-storage/file-session-storage.js +143 -0
- package/lib/node/session-storage/file-session-storage.js.map +1 -0
- package/lib/node/session-storage/file-session-storage.spec.d.ts +2 -0
- package/lib/node/session-storage/file-session-storage.spec.d.ts.map +1 -0
- package/lib/node/session-storage/file-session-storage.spec.js +70 -0
- package/lib/node/session-storage/file-session-storage.spec.js.map +1 -0
- package/lib/node/session-storage/index.d.ts +4 -0
- package/lib/node/session-storage/index.d.ts.map +1 -0
- package/lib/node/session-storage/index.js +20 -0
- package/lib/node/session-storage/index.js.map +1 -0
- package/lib/node/session-storage/session-storage-protocol.d.ts +58 -0
- package/lib/node/session-storage/session-storage-protocol.d.ts.map +1 -0
- package/lib/node/session-storage/session-storage-protocol.js +45 -0
- package/lib/node/session-storage/session-storage-protocol.js.map +1 -0
- package/lib/node/session-storage/stored-session.d.ts +47 -0
- package/lib/node/session-storage/stored-session.d.ts.map +1 -0
- package/lib/node/session-storage/stored-session.js +120 -0
- package/lib/node/session-storage/stored-session.js.map +1 -0
- package/lib/node/session-storage/stored-session.spec.d.ts +2 -0
- package/lib/node/session-storage/stored-session.spec.d.ts.map +1 -0
- package/lib/node/session-storage/stored-session.spec.js +73 -0
- package/lib/node/session-storage/stored-session.spec.js.map +1 -0
- package/lib/node/settings-server-impl.d.ts +12 -0
- package/lib/node/settings-server-impl.d.ts.map +1 -0
- package/lib/node/settings-server-impl.js +48 -0
- package/lib/node/settings-server-impl.js.map +1 -0
- package/lib/node/settings-store.d.ts +57 -0
- package/lib/node/settings-store.d.ts.map +1 -0
- package/lib/node/settings-store.js +309 -0
- package/lib/node/settings-store.js.map +1 -0
- package/lib/node/settings-store.spec.d.ts +2 -0
- package/lib/node/settings-store.spec.d.ts.map +1 -0
- package/lib/node/settings-store.spec.js +145 -0
- package/lib/node/settings-store.spec.js.map +1 -0
- package/lib/node/ta-access.d.ts +19 -2
- package/lib/node/ta-access.d.ts.map +1 -1
- package/lib/node/ta-access.js +66 -9
- package/lib/node/ta-access.js.map +1 -1
- package/lib/node/ta-access.spec.js +8 -0
- package/lib/node/ta-access.spec.js.map +1 -1
- package/lib/node/ta-errors.d.ts +3 -1
- package/lib/node/ta-errors.d.ts.map +1 -1
- package/lib/node/ta-errors.js +17 -1
- package/lib/node/ta-errors.js.map +1 -1
- package/lib/node/ta-errors.spec.d.ts +2 -0
- package/lib/node/ta-errors.spec.d.ts.map +1 -0
- package/lib/node/ta-errors.spec.js +25 -0
- package/lib/node/ta-errors.spec.js.map +1 -0
- package/lib/node/ta-server-impl.d.ts +17 -5
- package/lib/node/ta-server-impl.d.ts.map +1 -1
- package/lib/node/ta-server-impl.js +70 -13
- package/lib/node/ta-server-impl.js.map +1 -1
- package/lib/node/ta-session-factory.d.ts +2 -0
- package/lib/node/ta-session-factory.d.ts.map +1 -1
- package/lib/node/ta-session-factory.js +24 -10
- package/lib/node/ta-session-factory.js.map +1 -1
- package/lib/node/ta-session.d.ts +9 -1
- package/lib/node/ta-session.d.ts.map +1 -1
- package/lib/node/ta-session.js +26 -2
- package/lib/node/ta-session.js.map +1 -1
- package/lib/node/terminal-protocol.d.ts +19 -0
- package/lib/node/terminal-protocol.d.ts.map +1 -0
- package/lib/node/terminal-protocol.js +5 -0
- package/lib/node/terminal-protocol.js.map +1 -0
- package/lib/node/terminal-service.d.ts +60 -0
- package/lib/node/terminal-service.d.ts.map +1 -0
- package/lib/node/terminal-service.js +241 -0
- package/lib/node/terminal-service.js.map +1 -0
- package/lib/node/terminal-service.spec.d.ts +2 -0
- package/lib/node/terminal-service.spec.d.ts.map +1 -0
- package/lib/node/terminal-service.spec.js +79 -0
- package/lib/node/terminal-service.spec.js.map +1 -0
- package/lib/node/terminal-util.d.ts +63 -0
- package/lib/node/terminal-util.d.ts.map +1 -0
- package/lib/node/terminal-util.js +158 -0
- package/lib/node/terminal-util.js.map +1 -0
- package/lib/node/terminal-util.spec.d.ts +2 -0
- package/lib/node/terminal-util.spec.d.ts.map +1 -0
- package/lib/node/terminal-util.spec.js +71 -0
- package/lib/node/terminal-util.spec.js.map +1 -0
- package/lib/node/web-extension-catalog.d.ts +39 -0
- package/lib/node/web-extension-catalog.d.ts.map +1 -0
- package/lib/node/web-extension-catalog.js +163 -0
- package/lib/node/web-extension-catalog.js.map +1 -0
- package/lib/node/web-extension-catalog.spec.d.ts +2 -0
- package/lib/node/web-extension-catalog.spec.d.ts.map +1 -0
- package/lib/node/web-extension-catalog.spec.js +157 -0
- package/lib/node/web-extension-catalog.spec.js.map +1 -0
- package/lib/node/web-extension-util.d.ts +22 -0
- package/lib/node/web-extension-util.d.ts.map +1 -0
- package/lib/node/web-extension-util.js +126 -0
- package/lib/node/web-extension-util.js.map +1 -0
- package/lib/node/working-directory.d.ts +14 -0
- package/lib/node/working-directory.d.ts.map +1 -0
- package/lib/node/working-directory.js +43 -0
- package/lib/node/working-directory.js.map +1 -0
- package/lib/node/workspace-files.d.ts +48 -0
- package/lib/node/workspace-files.d.ts.map +1 -0
- package/lib/node/workspace-files.js +268 -0
- package/lib/node/workspace-files.js.map +1 -0
- package/lib/node/workspace-files.spec.d.ts +2 -0
- package/lib/node/workspace-files.spec.d.ts.map +1 -0
- package/lib/node/workspace-files.spec.js +133 -0
- package/lib/node/workspace-files.spec.js.map +1 -0
- package/lib/node/workspace-protocol.d.ts +21 -0
- package/lib/node/workspace-protocol.d.ts.map +1 -0
- package/lib/node/workspace-protocol.js +6 -0
- package/lib/node/workspace-protocol.js.map +1 -0
- package/lib/node/workspace-server-impl.d.ts +28 -0
- package/lib/node/workspace-server-impl.d.ts.map +1 -0
- package/lib/node/workspace-server-impl.js +91 -0
- package/lib/node/workspace-server-impl.js.map +1 -0
- package/lib/node/workspace-service.d.ts +32 -0
- package/lib/node/workspace-service.d.ts.map +1 -0
- package/lib/node/workspace-service.js +212 -0
- package/lib/node/workspace-service.js.map +1 -0
- package/lib/node/workspace-service.spec.d.ts +2 -0
- package/lib/node/workspace-service.spec.d.ts.map +1 -0
- package/lib/node/workspace-service.spec.js +104 -0
- package/lib/node/workspace-service.spec.js.map +1 -0
- package/lib/node/workspace-util.d.ts +30 -0
- package/lib/node/workspace-util.d.ts.map +1 -0
- package/lib/node/workspace-util.js +107 -0
- package/lib/node/workspace-util.js.map +1 -0
- package/lib/node/workspace-util.spec.d.ts +2 -0
- package/lib/node/workspace-util.spec.d.ts.map +1 -0
- package/lib/node/workspace-util.spec.js +44 -0
- package/lib/node/workspace-util.spec.js.map +1 -0
- package/package.json +14 -9
- package/src/node/auth-server-impl.spec.ts +49 -0
- package/src/node/auth-server-impl.ts +252 -0
- package/src/node/client-tool-broker.spec.ts +53 -0
- package/src/node/client-tool-broker.ts +106 -0
- package/src/node/exec-environment-factory.ts +46 -0
- package/src/node/extension-data-notifier.spec.ts +34 -0
- package/src/node/extension-data-notifier.ts +124 -0
- package/src/node/extension-data.spec.ts +105 -0
- package/src/node/extension-data.ts +180 -0
- package/src/node/host-protocol.ts +31 -1
- package/src/node/index.ts +24 -1
- package/src/node/package-host-extension.ts +20 -0
- package/src/node/package-hosts.spec.ts +54 -0
- package/src/node/package-hosts.ts +174 -0
- package/src/node/package-server-impl.ts +87 -0
- package/src/node/package-switch.ts +80 -0
- package/src/node/relay-bridge.ts +94 -0
- package/src/node/session-info-util.spec.ts +23 -0
- package/src/node/session-info-util.ts +55 -0
- package/src/node/session-storage/file-session-storage.spec.ts +72 -0
- package/src/node/session-storage/file-session-storage.ts +145 -0
- package/src/node/session-storage/index.ts +3 -0
- package/src/node/session-storage/session-storage-protocol.ts +81 -0
- package/src/node/session-storage/stored-session.spec.ts +80 -0
- package/src/node/session-storage/stored-session.ts +149 -0
- package/src/node/settings-server-impl.ts +33 -0
- package/src/node/settings-store.spec.ts +155 -0
- package/src/node/settings-store.ts +308 -0
- package/src/node/ta-access.spec.ts +9 -0
- package/src/node/ta-access.ts +65 -7
- package/src/node/ta-errors.spec.ts +25 -0
- package/src/node/ta-errors.ts +21 -1
- package/src/node/ta-server-impl.ts +75 -12
- package/src/node/ta-session-factory.ts +21 -7
- package/src/node/ta-session.ts +43 -3
- package/src/node/terminal-protocol.ts +18 -0
- package/src/node/terminal-service.spec.ts +84 -0
- package/src/node/terminal-service.ts +259 -0
- package/src/node/terminal-util.spec.ts +78 -0
- package/src/node/terminal-util.ts +166 -0
- package/src/node/web-extension-catalog.spec.ts +172 -0
- package/src/node/web-extension-catalog.ts +172 -0
- package/src/node/web-extension-util.ts +144 -0
- package/src/node/working-directory.ts +38 -0
- package/src/node/workspace-files.spec.ts +145 -0
- package/src/node/workspace-files.ts +256 -0
- package/src/node/workspace-protocol.ts +22 -0
- package/src/node/workspace-server-impl.ts +90 -0
- package/src/node/workspace-service.spec.ts +113 -0
- package/src/node/workspace-service.ts +223 -0
- package/src/node/workspace-util.spec.ts +47 -0
- package/src/node/workspace-util.ts +103 -0
- package/esm/node/local-exec-environment-factory.d.ts +0 -4
- package/esm/node/local-exec-environment-factory.js +0 -18
- package/esm/node/local-exec-environment-factory.js.map +0 -1
- package/lib/node/local-exec-environment-factory.d.ts +0 -5
- package/lib/node/local-exec-environment-factory.d.ts.map +0 -1
- package/lib/node/local-exec-environment-factory.js +0 -21
- package/lib/node/local-exec-environment-factory.js.map +0 -1
- package/src/node/local-exec-environment-factory.ts +0 -9
|
@@ -19,9 +19,13 @@ import {
|
|
|
19
19
|
SessionLock,
|
|
20
20
|
SessionLockExtension,
|
|
21
21
|
TaExtensionUtil,
|
|
22
|
+
TaPackages,
|
|
22
23
|
} from '@celljs/ta-harness';
|
|
23
24
|
import type { TaOpenSessionOptions } from '@celljs/ta-protocol';
|
|
25
|
+
import { ClientToolBroker } from './client-tool-broker';
|
|
24
26
|
import { TA_HOST_OPTIONS, type TaHostOptions, TaSessionFactory } from './host-protocol';
|
|
27
|
+
import { PackageHostExtension } from './package-host-extension';
|
|
28
|
+
import { PackageHosts } from './package-hosts';
|
|
25
29
|
import { TaErrors } from './ta-errors';
|
|
26
30
|
import { TaSession } from './ta-session';
|
|
27
31
|
|
|
@@ -52,10 +56,11 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
|
|
|
52
56
|
@Optional()
|
|
53
57
|
protected readonly tools: AgentTool[] = [];
|
|
54
58
|
|
|
59
|
+
@Autowired(PackageHosts)
|
|
60
|
+
protected readonly packageHosts: PackageHosts;
|
|
61
|
+
|
|
55
62
|
async create(options: TaOpenSessionOptions, requestClose: (session: TaSession) => void): Promise<TaSession> {
|
|
56
|
-
|
|
57
|
-
throw TaErrors.invalidArgument('This version of ta serve does not run client tools yet.', 'Open the session without clientTools.');
|
|
58
|
-
}
|
|
63
|
+
const clientTools = new ClientToolBroker(options.clientTools);
|
|
59
64
|
const sdk = await PiSdkUtil.load();
|
|
60
65
|
const agentDir = this.codingAgentOptions?.agentDir ?? sdk.getAgentDir();
|
|
61
66
|
const modelRuntime = await this.modelRuntimeProvider.provide();
|
|
@@ -75,7 +80,11 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
|
|
|
75
80
|
agentDir: dir,
|
|
76
81
|
settingsManager,
|
|
77
82
|
modelRuntime,
|
|
78
|
-
resourceLoaderOptions: {
|
|
83
|
+
resourceLoaderOptions: {
|
|
84
|
+
...TaPackages.loaderOptions(dir),
|
|
85
|
+
extensionFactories: this.extensionFactories(sdk, cwd, lock),
|
|
86
|
+
appendSystemPrompt: options.appendSystemPrompt ? [options.appendSystemPrompt] : undefined,
|
|
87
|
+
},
|
|
79
88
|
});
|
|
80
89
|
const created = await sdk.createAgentSessionFromServices({
|
|
81
90
|
services,
|
|
@@ -83,13 +92,13 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
|
|
|
83
92
|
sessionStartEvent,
|
|
84
93
|
model,
|
|
85
94
|
thinkingLevel: options.thinkingLevel,
|
|
86
|
-
customTools: (this.tools ?? []).map(AgentToolUtil.toToolDefinition),
|
|
95
|
+
customTools: [...(this.tools ?? []).map(AgentToolUtil.toToolDefinition), ...clientTools.tools()],
|
|
87
96
|
});
|
|
88
97
|
return { ...created, services, diagnostics: services.diagnostics };
|
|
89
98
|
},
|
|
90
99
|
{ cwd: options.cwd, agentDir, sessionManager },
|
|
91
100
|
);
|
|
92
|
-
const session = new TaSession({ runtime, lock, options: this.options, requestClose });
|
|
101
|
+
const session = new TaSession({ runtime, lock, clientTools, options: this.options, requestClose, group: options.group });
|
|
93
102
|
await session.start();
|
|
94
103
|
return session;
|
|
95
104
|
} catch (error) {
|
|
@@ -114,7 +123,12 @@ export class DefaultTaSessionFactory implements TaSessionFactory {
|
|
|
114
123
|
|
|
115
124
|
protected extensionFactories(sdk: PiSdk, cwd: string, lock: SessionLock): InlineExtension[] {
|
|
116
125
|
const environment = this.environments.create(cwd);
|
|
117
|
-
const builtIn = [
|
|
126
|
+
const builtIn = [
|
|
127
|
+
new SessionLockExtension(lock),
|
|
128
|
+
new PackageHostExtension(this.packageHosts),
|
|
129
|
+
new BrandExtension(sdk),
|
|
130
|
+
...(environment.local ? [] : [new ExecExtension(environment, sdk)]),
|
|
131
|
+
];
|
|
118
132
|
return [
|
|
119
133
|
...this.piExtensions(sdk, environment),
|
|
120
134
|
TaExtensionUtil.bundle(builtIn),
|
package/src/node/ta-session.ts
CHANGED
|
@@ -1,11 +1,22 @@
|
|
|
1
1
|
import { randomUUID } from 'node:crypto';
|
|
2
2
|
import type { SessionLock } from '@celljs/ta-harness';
|
|
3
|
-
import type {
|
|
3
|
+
import type {
|
|
4
|
+
RpcExtensionUIResponse,
|
|
5
|
+
TaAttachResult,
|
|
6
|
+
TaClientToolResult,
|
|
7
|
+
TaEvent,
|
|
8
|
+
TaNavigateResult,
|
|
9
|
+
TaPromptInput,
|
|
10
|
+
TaPromptReceipt,
|
|
11
|
+
TaSessionInfo,
|
|
12
|
+
} from '@celljs/ta-protocol';
|
|
4
13
|
import type { ClientChannel } from './client-channel';
|
|
14
|
+
import type { ClientToolBroker } from './client-tool-broker';
|
|
5
15
|
import { EventLog } from './event-log';
|
|
6
16
|
import type { TaHostOptions } from './host-protocol';
|
|
7
17
|
import { ReceiptBook } from './receipt-book';
|
|
8
18
|
import { RemoteUiContext } from './remote-ui-context';
|
|
19
|
+
import { SessionInfoUtil } from './session-info-util';
|
|
9
20
|
import { TaEventUtil } from './ta-event-util';
|
|
10
21
|
|
|
11
22
|
type AgentSessionRuntime = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' }}).AgentSessionRuntime;
|
|
@@ -15,9 +26,13 @@ type PromptDisposition = 'started' | 'queued' | 'handled';
|
|
|
15
26
|
export interface TaSessionContext {
|
|
16
27
|
runtime: AgentSessionRuntime;
|
|
17
28
|
lock: SessionLock;
|
|
29
|
+
/** The tools of the session that its clients run. */
|
|
30
|
+
clientTools: ClientToolBroker;
|
|
18
31
|
options: TaHostOptions;
|
|
19
32
|
/** The session should end: it has been idle, or an extension asked. */
|
|
20
33
|
requestClose(session: TaSession): void;
|
|
34
|
+
/** TaOpenSessionOptions.group. */
|
|
35
|
+
group?: string;
|
|
21
36
|
}
|
|
22
37
|
|
|
23
38
|
/**
|
|
@@ -40,6 +55,7 @@ export class TaSession {
|
|
|
40
55
|
this.runtime = context.runtime;
|
|
41
56
|
this.log = new EventLog(this.id, context.options.eventBufferBytes);
|
|
42
57
|
this.ui = new RemoteUiContext(event => this.publish(event));
|
|
58
|
+
context.clientTools.connect(event => this.publish(event));
|
|
43
59
|
}
|
|
44
60
|
|
|
45
61
|
get pi(): AgentSession {
|
|
@@ -63,11 +79,14 @@ export class TaSession {
|
|
|
63
79
|
cwd: this.runtime.cwd,
|
|
64
80
|
sessionFile: pi.sessionFile,
|
|
65
81
|
sessionName: pi.sessionName,
|
|
82
|
+
firstMessage: SessionInfoUtil.firstMessage(pi.messages),
|
|
66
83
|
model: model ? `${model.provider}/${model.id}` : undefined,
|
|
67
84
|
isStreaming: pi.isStreaming,
|
|
68
85
|
messageCount: pi.messages.length,
|
|
69
86
|
attached: this.channels.size,
|
|
70
87
|
createdAt: this.createdAt,
|
|
88
|
+
updatedAt: SessionInfoUtil.updatedAt(pi.messages, this.createdAt),
|
|
89
|
+
group: this.context.group,
|
|
71
90
|
};
|
|
72
91
|
}
|
|
73
92
|
|
|
@@ -75,7 +94,14 @@ export class TaSession {
|
|
|
75
94
|
this.channels.add(channel);
|
|
76
95
|
this.watchIdle();
|
|
77
96
|
const { events, gap } = afterSeq === undefined ? { events: [], gap: false } : this.log.since(afterSeq);
|
|
78
|
-
return {
|
|
97
|
+
return {
|
|
98
|
+
info: this.info(),
|
|
99
|
+
events,
|
|
100
|
+
pendingUi: this.ui.pendingRequests,
|
|
101
|
+
pendingClientTools: this.context.clientTools.pendingRequests,
|
|
102
|
+
lastSeq: this.log.lastSeq,
|
|
103
|
+
gap,
|
|
104
|
+
};
|
|
79
105
|
}
|
|
80
106
|
|
|
81
107
|
detach(channel: ClientChannel): void {
|
|
@@ -88,6 +114,10 @@ export class TaSession {
|
|
|
88
114
|
return this.ui.respond(response);
|
|
89
115
|
}
|
|
90
116
|
|
|
117
|
+
respondClientTool(result: TaClientToolResult): boolean {
|
|
118
|
+
return this.context.clientTools.respond(result);
|
|
119
|
+
}
|
|
120
|
+
|
|
91
121
|
prompt(input: TaPromptInput): Promise<TaPromptReceipt> {
|
|
92
122
|
return this.accept(
|
|
93
123
|
input,
|
|
@@ -114,9 +144,19 @@ export class TaSession {
|
|
|
114
144
|
return this.accept(input, () => this.pi.followUp(input.message, input.images, { source: 'rpc' }));
|
|
115
145
|
}
|
|
116
146
|
|
|
147
|
+
/** pi's runtime says nothing of a move in the tree: the clients are told to read the conversation again. */
|
|
148
|
+
async navigateTree(targetId: string, options?: Parameters<AgentSession['navigateTree']>[1]): Promise<TaNavigateResult> {
|
|
149
|
+
const { cancelled, editorText } = await this.pi.navigateTree(targetId, options);
|
|
150
|
+
if (!cancelled) {
|
|
151
|
+
this.publish({ type: 'session_replaced', info: this.info() });
|
|
152
|
+
}
|
|
153
|
+
return { cancelled, editorText };
|
|
154
|
+
}
|
|
155
|
+
|
|
117
156
|
async close(): Promise<void> {
|
|
118
157
|
clearTimeout(this.idleTimer);
|
|
119
158
|
this.ui.cancelAll();
|
|
159
|
+
this.context.clientTools.cancelAll();
|
|
120
160
|
this.publish({ type: 'session_closed' });
|
|
121
161
|
this.channels.clear();
|
|
122
162
|
this.unsubscribe?.();
|
|
@@ -152,7 +192,7 @@ export class TaSession {
|
|
|
152
192
|
waitForIdle: () => pi.waitForIdle(),
|
|
153
193
|
newSession: options => runtime.newSession(options),
|
|
154
194
|
fork: async (entryId, options) => ({ cancelled: (await runtime.fork(entryId, options)).cancelled }),
|
|
155
|
-
navigateTree: async (targetId, options) => ({ cancelled: (await
|
|
195
|
+
navigateTree: async (targetId, options) => ({ cancelled: (await this.navigateTree(targetId, options)).cancelled }),
|
|
156
196
|
switchSession: (sessionPath, options) => runtime.switchSession(sessionPath, options),
|
|
157
197
|
reload: () => pi.reload(),
|
|
158
198
|
},
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
2
|
+
import type { TaOpenTerminalOptions, TaTerminalInfo } from '@celljs/ta-protocol';
|
|
3
|
+
|
|
4
|
+
export const TaTerminals = Symbol('TaTerminals');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The shells of the web client's terminal. Each belongs to the connection that opened it and sends it
|
|
8
|
+
* its output (TaClient.onTerminalData). When that connection drops, the shell waits 30 s for another
|
|
9
|
+
* to take it (listTerminals, writeTerminal or resizeTerminal) and keeps its output meanwhile; then it ends.
|
|
10
|
+
*/
|
|
11
|
+
export interface TaTerminals {
|
|
12
|
+
open(connection: RpcConnection, options: TaOpenTerminalOptions & { cwd: string }): Promise<TaTerminalInfo>;
|
|
13
|
+
write(connection: RpcConnection, terminalId: string, data: string): void;
|
|
14
|
+
resize(connection: RpcConnection, terminalId: string, cols: number, rows: number): Promise<void>;
|
|
15
|
+
close(connection: RpcConnection, terminalId: string): Promise<void>;
|
|
16
|
+
list(connection: RpcConnection): TaTerminalInfo[];
|
|
17
|
+
closeAll(): Promise<void>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it } from 'bun:test';
|
|
2
|
+
import { realpathSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
5
|
+
import { LocalExecEnvironment } from '@celljs/ta-exec';
|
|
6
|
+
import { DefaultTaTerminals } from './terminal-service';
|
|
7
|
+
|
|
8
|
+
/** A connection that keeps what the shells sent it. */
|
|
9
|
+
class FakeConnection {
|
|
10
|
+
readonly id = String(Math.random());
|
|
11
|
+
closed = false;
|
|
12
|
+
output = '';
|
|
13
|
+
exitCode?: number;
|
|
14
|
+
protected readonly closeListeners: (() => void)[] = [];
|
|
15
|
+
|
|
16
|
+
client() {
|
|
17
|
+
return {
|
|
18
|
+
onTerminalData: async (_terminalId: string, data: string, exitCode?: number) => {
|
|
19
|
+
this.output += data;
|
|
20
|
+
this.exitCode ??= exitCode;
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
onClose(listener: () => void) {
|
|
26
|
+
this.closeListeners.push(listener);
|
|
27
|
+
return { dispose: () => undefined };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
close() {
|
|
31
|
+
this.closed = true;
|
|
32
|
+
for (const listener of this.closeListeners) {
|
|
33
|
+
listener();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function until(check: () => boolean, what: string): Promise<void> {
|
|
39
|
+
for (let i = 0; i < 500 && !check(); i++) {
|
|
40
|
+
await Bun.sleep(10);
|
|
41
|
+
}
|
|
42
|
+
if (!check()) {
|
|
43
|
+
throw new Error(`Timed out waiting until ${what}`);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
describe('DefaultTaTerminals', () => {
|
|
48
|
+
const cwd = realpathSync(tmpdir());
|
|
49
|
+
const terminals = new DefaultTaTerminals();
|
|
50
|
+
Object.assign(terminals, { environments: { create: (dir: string) => new LocalExecEnvironment(dir), defaultCwd: async () => cwd } });
|
|
51
|
+
const as = (connection: FakeConnection) => connection as unknown as RpcConnection;
|
|
52
|
+
|
|
53
|
+
afterEach(() => terminals.closeAll());
|
|
54
|
+
|
|
55
|
+
it('runs a shell and sends its output to the connection that opened it', async () => {
|
|
56
|
+
const connection = new FakeConnection();
|
|
57
|
+
const info = await terminals.open(as(connection), { cwd, cols: 80, rows: 24 });
|
|
58
|
+
expect(info.cwd).toBe(cwd);
|
|
59
|
+
expect(info.terminalId).toBeString();
|
|
60
|
+
terminals.write(as(connection), info.terminalId, 'echo ta-$((40+2))\r');
|
|
61
|
+
await until(() => connection.output.includes('ta-42'), 'the shell answered');
|
|
62
|
+
expect(connection.output).not.toContain('ta-tty=');
|
|
63
|
+
expect(terminals.list(as(connection))).toEqual([info]);
|
|
64
|
+
await terminals.resize(as(connection), info.terminalId, 100, 30);
|
|
65
|
+
terminals.write(as(connection), info.terminalId, 'exit\r');
|
|
66
|
+
await until(() => connection.exitCode !== undefined, 'the shell ended');
|
|
67
|
+
expect(terminals.list(as(connection))).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('keeps a shell for another connection after its own dropped', async () => {
|
|
71
|
+
const first = new FakeConnection();
|
|
72
|
+
const info = await terminals.open(as(first), { cwd, cols: 80, rows: 24 });
|
|
73
|
+
const stranger = new FakeConnection();
|
|
74
|
+
expect(terminals.list(as(stranger))).toEqual([]);
|
|
75
|
+
expect(() => terminals.write(as(stranger), info.terminalId, 'x')).toThrow('No terminal');
|
|
76
|
+
first.close();
|
|
77
|
+
const second = new FakeConnection();
|
|
78
|
+
expect(terminals.list(as(second))).toEqual([info]);
|
|
79
|
+
terminals.write(as(second), info.terminalId, 'echo back-$((1+1))\r');
|
|
80
|
+
await until(() => second.output.includes('back-2'), 'the second connection got the output');
|
|
81
|
+
await terminals.close(as(second), info.terminalId);
|
|
82
|
+
expect(terminals.list(as(second))).toEqual([]);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
import { type Application, ApplicationLifecycle, Autowired, Component } from '@celljs/core';
|
|
2
|
+
import type { RpcConnection } from '@celljs/rpc/lib/node';
|
|
3
|
+
import { type ExecEnvironment, ExecEnvironmentFactory, type ExecProcess } from '@celljs/ta-exec';
|
|
4
|
+
import { TaClient, type TaOpenTerminalOptions, type TaTerminalInfo } from '@celljs/ta-protocol';
|
|
5
|
+
import { TaErrors } from './ta-errors';
|
|
6
|
+
import { TaTerminals } from './terminal-protocol';
|
|
7
|
+
import { PipeDiscipline, type TerminalPlatform, TerminalUtil, TtyMarker } from './terminal-util';
|
|
8
|
+
|
|
9
|
+
/** How long a shell waits for a connection after its own dropped. */
|
|
10
|
+
export const TERMINAL_ORPHAN_MS = 30000;
|
|
11
|
+
/** Output a shell keeps while no connection takes it; older output goes first. */
|
|
12
|
+
const MAX_PENDING_CHARS = 256 * 1024;
|
|
13
|
+
/** Output of the same moment leaves in one message. */
|
|
14
|
+
const FLUSH_MS = 8;
|
|
15
|
+
const DEFAULT_COLS = 80;
|
|
16
|
+
const DEFAULT_ROWS = 24;
|
|
17
|
+
|
|
18
|
+
class Terminal {
|
|
19
|
+
owner?: RpcConnection;
|
|
20
|
+
orphanTimer?: ReturnType<typeof setTimeout>;
|
|
21
|
+
pending = '';
|
|
22
|
+
flushTimer?: ReturnType<typeof setTimeout>;
|
|
23
|
+
sending = false;
|
|
24
|
+
/** The code the shell ended with, as it said, or as its process did. */
|
|
25
|
+
exitCode?: number;
|
|
26
|
+
/** Its process ended. */
|
|
27
|
+
exited = false;
|
|
28
|
+
/** Its client knows it ended: nothing more goes. */
|
|
29
|
+
ended = false;
|
|
30
|
+
readonly decoder = new TextDecoder();
|
|
31
|
+
readonly marker = new TtyMarker();
|
|
32
|
+
readonly discipline?: PipeDiscipline;
|
|
33
|
+
|
|
34
|
+
constructor(
|
|
35
|
+
readonly info: TaTerminalInfo,
|
|
36
|
+
readonly env: ExecEnvironment,
|
|
37
|
+
readonly platform: TerminalPlatform,
|
|
38
|
+
readonly process: ExecProcess,
|
|
39
|
+
pty: boolean,
|
|
40
|
+
owner: RpcConnection,
|
|
41
|
+
) {
|
|
42
|
+
this.owner = owner;
|
|
43
|
+
this.discipline = pty ? undefined : new PipeDiscipline();
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@Component([TaTerminals, ApplicationLifecycle])
|
|
48
|
+
export class DefaultTaTerminals implements TaTerminals, ApplicationLifecycle<Application> {
|
|
49
|
+
@Autowired(ExecEnvironmentFactory)
|
|
50
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
51
|
+
|
|
52
|
+
protected readonly terminals = new Map<string, Terminal>();
|
|
53
|
+
protected readonly watched = new WeakSet<RpcConnection>();
|
|
54
|
+
protected platform?: Promise<TerminalPlatform>;
|
|
55
|
+
protected nextId = 0;
|
|
56
|
+
|
|
57
|
+
async open(connection: RpcConnection, options: TaOpenTerminalOptions & { cwd: string }): Promise<TaTerminalInfo> {
|
|
58
|
+
const env = this.environments.create(options.cwd);
|
|
59
|
+
const stat = await env.stat(options.cwd);
|
|
60
|
+
if (!stat?.isDirectory) {
|
|
61
|
+
throw TaErrors.invalidArgument(`${options.cwd} is not a directory.`, 'Open the terminal in a directory that exists.');
|
|
62
|
+
}
|
|
63
|
+
const platform = await this.detect(env);
|
|
64
|
+
const launch = TerminalUtil.launch(platform, TerminalUtil.size(options.cols, DEFAULT_COLS), TerminalUtil.size(options.rows, DEFAULT_ROWS));
|
|
65
|
+
// `pty`: false when the shell runs on pipes, so that the view can say why some programs behave otherwise.
|
|
66
|
+
const info: TaTerminalInfo & { pty: boolean } = { terminalId: `t${++this.nextId}`, cwd: options.cwd, shell: platform.shell, pty: launch.pty };
|
|
67
|
+
const terminal = new Terminal(info, env, platform, env.spawn(launch.command, launch.args), launch.pty, connection);
|
|
68
|
+
this.terminals.set(info.terminalId, terminal);
|
|
69
|
+
this.watch(connection);
|
|
70
|
+
const receive = (data: Uint8Array) => this.output(terminal, terminal.decoder.decode(data, { stream: true }));
|
|
71
|
+
terminal.process.onStdout(receive);
|
|
72
|
+
terminal.process.onStderr(receive);
|
|
73
|
+
terminal.process.onExit(code => this.exited(terminal, code));
|
|
74
|
+
return info;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
write(connection: RpcConnection, terminalId: string, data: string): void {
|
|
78
|
+
const terminal = this.require(connection, terminalId);
|
|
79
|
+
if (!terminal.discipline) {
|
|
80
|
+
terminal.process.write(data);
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
const { echo, send } = terminal.discipline.input(data);
|
|
84
|
+
this.queue(terminal, echo);
|
|
85
|
+
if (send) {
|
|
86
|
+
terminal.process.write(send);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async resize(connection: RpcConnection, terminalId: string, cols: number, rows: number): Promise<void> {
|
|
91
|
+
const terminal = this.require(connection, terminalId);
|
|
92
|
+
if (terminal.marker.tty) {
|
|
93
|
+
const command = TerminalUtil.resizeCommand(
|
|
94
|
+
terminal.platform,
|
|
95
|
+
terminal.marker.tty,
|
|
96
|
+
TerminalUtil.size(cols, DEFAULT_COLS),
|
|
97
|
+
TerminalUtil.size(rows, DEFAULT_ROWS),
|
|
98
|
+
);
|
|
99
|
+
await terminal.env.exec(command, { timeout: 5000 });
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
close(connection: RpcConnection, terminalId: string): Promise<void> {
|
|
104
|
+
return this.end(this.require(connection, terminalId));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
list(connection: RpcConnection): TaTerminalInfo[] {
|
|
108
|
+
const own: TaTerminalInfo[] = [];
|
|
109
|
+
for (const terminal of this.terminals.values()) {
|
|
110
|
+
if (!terminal.owner) {
|
|
111
|
+
this.adopt(terminal, connection);
|
|
112
|
+
}
|
|
113
|
+
if (terminal.owner === connection) {
|
|
114
|
+
own.push(terminal.info);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return own;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
onStop(): Promise<void> {
|
|
121
|
+
return this.closeAll();
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async closeAll(): Promise<void> {
|
|
125
|
+
await Promise.all([...this.terminals.values()].map(terminal => this.end(terminal)));
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** The shell of this connection, or one nobody has: it becomes this connection's. */
|
|
129
|
+
protected require(connection: RpcConnection, terminalId: string): Terminal {
|
|
130
|
+
const terminal = this.terminals.get(terminalId);
|
|
131
|
+
if (!terminal || (terminal.owner && terminal.owner !== connection)) {
|
|
132
|
+
throw TaErrors.invalidArgument(
|
|
133
|
+
`No terminal of this connection has the id ${terminalId}.`,
|
|
134
|
+
'Open one with openTerminal; listTerminals has those still open.',
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
if (!terminal.owner) {
|
|
138
|
+
this.adopt(terminal, connection);
|
|
139
|
+
}
|
|
140
|
+
return terminal;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
protected adopt(terminal: Terminal, connection: RpcConnection): void {
|
|
144
|
+
clearTimeout(terminal.orphanTimer);
|
|
145
|
+
terminal.orphanTimer = undefined;
|
|
146
|
+
terminal.owner = connection;
|
|
147
|
+
this.watch(connection);
|
|
148
|
+
this.schedule(terminal);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
protected watch(connection: RpcConnection): void {
|
|
152
|
+
if (this.watched.has(connection)) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
this.watched.add(connection);
|
|
156
|
+
connection.onClose(() => {
|
|
157
|
+
for (const terminal of this.terminals.values()) {
|
|
158
|
+
if (terminal.owner === connection) {
|
|
159
|
+
terminal.owner = undefined;
|
|
160
|
+
terminal.orphanTimer = setTimeout(() => void this.end(terminal), TERMINAL_ORPHAN_MS);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
protected output(terminal: Terminal, text: string): void {
|
|
167
|
+
const { text: rest, exitCode } = TerminalUtil.takeExitCode(text);
|
|
168
|
+
terminal.exitCode ??= exitCode;
|
|
169
|
+
const shown = terminal.marker.feed(rest);
|
|
170
|
+
this.queue(terminal, terminal.discipline ? terminal.discipline.output(shown) : shown);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
protected queue(terminal: Terminal, text: string): void {
|
|
174
|
+
if (!text) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
terminal.pending += text;
|
|
178
|
+
if (terminal.pending.length > MAX_PENDING_CHARS) {
|
|
179
|
+
terminal.pending = terminal.pending.slice(-MAX_PENDING_CHARS);
|
|
180
|
+
}
|
|
181
|
+
this.schedule(terminal);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
protected schedule(terminal: Terminal): void {
|
|
185
|
+
const due = terminal.pending || terminal.exited;
|
|
186
|
+
if (terminal.flushTimer === undefined && terminal.owner && !terminal.ended && due) {
|
|
187
|
+
terminal.flushTimer = setTimeout(() => this.flush(terminal), FLUSH_MS);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** One message at a time: the next leaves when the client has the last. */
|
|
192
|
+
protected flush(terminal: Terminal): void {
|
|
193
|
+
terminal.flushTimer = undefined;
|
|
194
|
+
const owner = terminal.owner;
|
|
195
|
+
if (!owner || owner.closed || terminal.sending) {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const data = terminal.pending;
|
|
199
|
+
const exitCode = terminal.exited ? (terminal.exitCode ?? -1) : undefined;
|
|
200
|
+
terminal.pending = '';
|
|
201
|
+
terminal.sending = true;
|
|
202
|
+
owner
|
|
203
|
+
.client<TaClient>(TaClient)
|
|
204
|
+
.onTerminalData(terminal.info.terminalId, data, exitCode)
|
|
205
|
+
.then(
|
|
206
|
+
() => {
|
|
207
|
+
if (exitCode !== undefined) {
|
|
208
|
+
terminal.ended = true;
|
|
209
|
+
this.terminals.delete(terminal.info.terminalId);
|
|
210
|
+
}
|
|
211
|
+
},
|
|
212
|
+
() => {
|
|
213
|
+
// Lost on the way: it goes again to whoever takes the shell.
|
|
214
|
+
terminal.pending = data + terminal.pending;
|
|
215
|
+
},
|
|
216
|
+
)
|
|
217
|
+
.finally(() => {
|
|
218
|
+
terminal.sending = false;
|
|
219
|
+
this.schedule(terminal);
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
protected exited(terminal: Terminal, exitCode: number | null): void {
|
|
224
|
+
const rest = terminal.decoder.decode() + terminal.marker.flush();
|
|
225
|
+
this.queue(terminal, terminal.discipline ? terminal.discipline.output(rest) : rest);
|
|
226
|
+
terminal.exitCode ??= exitCode ?? undefined;
|
|
227
|
+
terminal.exited = true;
|
|
228
|
+
clearTimeout(terminal.orphanTimer);
|
|
229
|
+
if (terminal.owner) {
|
|
230
|
+
this.schedule(terminal);
|
|
231
|
+
} else {
|
|
232
|
+
this.terminals.delete(terminal.info.terminalId);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** Hangs up its terminal, which ends what runs there, then the wrapper. */
|
|
237
|
+
protected async end(terminal: Terminal): Promise<void> {
|
|
238
|
+
clearTimeout(terminal.orphanTimer);
|
|
239
|
+
this.terminals.delete(terminal.info.terminalId);
|
|
240
|
+
if (terminal.marker.tty) {
|
|
241
|
+
await terminal.env.exec(TerminalUtil.hangUpCommand(terminal.marker.tty), { timeout: 5000 }).catch(() => undefined);
|
|
242
|
+
}
|
|
243
|
+
terminal.process.kill();
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
protected detect(env: ExecEnvironment): Promise<TerminalPlatform> {
|
|
247
|
+
this.platform ??= (async () => {
|
|
248
|
+
let output = '';
|
|
249
|
+
const decoder = new TextDecoder();
|
|
250
|
+
const { exitCode } = await env.exec(`${TerminalUtil.PROBE} 2>/dev/null`, {
|
|
251
|
+
timeout: 5000,
|
|
252
|
+
onData: data => (output += decoder.decode(data, { stream: true })),
|
|
253
|
+
});
|
|
254
|
+
const fallback = env.local && process.platform === 'win32' ? (process.env.ComSpec ?? 'cmd.exe') : '/bin/sh';
|
|
255
|
+
return exitCode === 0 ? TerminalUtil.parsePlatform(output, fallback) : { system: '', script: false, shell: fallback };
|
|
256
|
+
})();
|
|
257
|
+
return this.platform;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { PipeDiscipline, TerminalUtil, TtyMarker } from './terminal-util';
|
|
3
|
+
|
|
4
|
+
describe('TerminalUtil', () => {
|
|
5
|
+
it('reads the platform', () => {
|
|
6
|
+
expect(TerminalUtil.parsePlatform('Darwin\n/usr/bin/script\n/bin/zsh', '/bin/sh')).toEqual({ system: 'Darwin', script: true, shell: '/bin/zsh' });
|
|
7
|
+
expect(TerminalUtil.parsePlatform('Linux\n\n', '/bin/sh')).toEqual({ system: 'Linux', script: false, shell: '/bin/sh' });
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it('runs the shell on a pseudo-terminal when script is there', () => {
|
|
11
|
+
const mac = TerminalUtil.launch({ system: 'Darwin', script: true, shell: '/bin/zsh' }, 100, 30);
|
|
12
|
+
expect(mac).toMatchObject({ command: '/bin/sh', pty: true });
|
|
13
|
+
expect(mac.args[1]).toStartWith('cat | { script -q /dev/null /bin/sh -c ');
|
|
14
|
+
expect(mac.args[1]).toContain('stty cols 100 rows 30');
|
|
15
|
+
expect(mac.args[1]).toContain('/bin/zsh');
|
|
16
|
+
expect(mac.args[1]).toContain('BASH_SILENCE_DEPRECATION_WARNING=1');
|
|
17
|
+
expect(mac.args[1]).toEndWith('kill $$; }');
|
|
18
|
+
const linux = TerminalUtil.launch({ system: 'Linux', script: true, shell: '/bin/bash' }, 80, 24);
|
|
19
|
+
expect(linux.args[1]).toStartWith('cat | { script -qefc ');
|
|
20
|
+
expect(linux.args[1]).toContain(' /dev/null; code=$?');
|
|
21
|
+
expect(TerminalUtil.launch({ system: 'Linux', script: false, shell: '/bin/bash' }, 80, 24)).toEqual({ command: '/bin/bash', args: ['-i'], pty: false });
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('resizes with the stty of the system', () => {
|
|
25
|
+
expect(TerminalUtil.resizeCommand({ system: 'Linux', script: true, shell: '' }, '/dev/pts/3', 120, 40)).toBe("stty -F '/dev/pts/3' cols 120 rows 40");
|
|
26
|
+
expect(TerminalUtil.resizeCommand({ system: 'Darwin', script: true, shell: '' }, '/dev/ttys001', 80, 24)).toBe(
|
|
27
|
+
"stty -f '/dev/ttys001' cols 80 rows 24",
|
|
28
|
+
);
|
|
29
|
+
expect(TerminalUtil.hangUpCommand('/dev/pts/3')).toBe("kill -HUP $(ps -t 'pts/3' -o pid=)");
|
|
30
|
+
expect(TerminalUtil.size(0, 80)).toBe(1);
|
|
31
|
+
expect(TerminalUtil.size(Number.NaN, 80)).toBe(80);
|
|
32
|
+
expect(TerminalUtil.size(99999, 80)).toBe(1000);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
describe('TerminalUtil.takeExitCode', () => {
|
|
37
|
+
it('takes the code the wrapper printed', () => {
|
|
38
|
+
expect(TerminalUtil.takeExitCode('bye\x1b]1337;ta-exit=3\x07')).toEqual({ text: 'bye', exitCode: 3 });
|
|
39
|
+
expect(TerminalUtil.takeExitCode('plain')).toEqual({ text: 'plain' });
|
|
40
|
+
});
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
describe('TtyMarker', () => {
|
|
44
|
+
it('takes the name of the terminal out of the output, in pieces too', () => {
|
|
45
|
+
const marker = new TtyMarker();
|
|
46
|
+
expect(marker.feed('^D\x1b]1337;ta-')).toBe('');
|
|
47
|
+
expect(marker.feed('tty=/dev/ttys004\x07$ ')).toBe('^D$ ');
|
|
48
|
+
expect(marker.tty).toBe('/dev/ttys004');
|
|
49
|
+
expect(marker.feed('more')).toBe('more');
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('gives up after a while, and gives back what it held', () => {
|
|
53
|
+
const marker = new TtyMarker();
|
|
54
|
+
expect(marker.feed('x'.repeat(5000))).toHaveLength(5000);
|
|
55
|
+
expect(marker.tty).toBeUndefined();
|
|
56
|
+
const early = new TtyMarker();
|
|
57
|
+
early.feed('bye');
|
|
58
|
+
expect(early.flush()).toBe('bye');
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
describe('PipeDiscipline', () => {
|
|
63
|
+
it('echoes, edits the line, and sends it on Enter', () => {
|
|
64
|
+
const discipline = new PipeDiscipline();
|
|
65
|
+
expect(discipline.input('lsx')).toEqual({ echo: 'lsx', send: '' });
|
|
66
|
+
expect(discipline.input('\x7f')).toEqual({ echo: '\b \b', send: '' });
|
|
67
|
+
expect(discipline.input('\x1b[A -l\r')).toEqual({ echo: ' -l\r\n', send: 'ls -l\n' });
|
|
68
|
+
discipline.input('oops');
|
|
69
|
+
expect(discipline.input('\x03').echo).toBe('^C\r\n');
|
|
70
|
+
expect(discipline.input('\r').send).toBe('\n');
|
|
71
|
+
discipline.input('ab');
|
|
72
|
+
expect(discipline.input('\x15').echo).toBe('\b \b\b \b');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it('turns line feeds into what a terminal shows', () => {
|
|
76
|
+
expect(new PipeDiscipline().output('a\nb\r\n')).toBe('a\r\nb\r\n');
|
|
77
|
+
});
|
|
78
|
+
});
|