@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
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { Autowired } from '@celljs/core';
|
|
2
|
+
import { Rpc } from '@celljs/rpc';
|
|
3
|
+
import { RpcContext } from '@celljs/rpc/lib/node';
|
|
4
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
5
|
+
import { type TaPackageInfo, TaPackageServer, type TaSettingsScope, type TaWebExtensionInfo, type TaWebExtensionSource } from '@celljs/ta-protocol';
|
|
6
|
+
import { ExtensionDataStore, ExtensionDataUtil } from './extension-data';
|
|
7
|
+
import { ExtensionDataNotifier } from './extension-data-notifier';
|
|
8
|
+
import { WebExtensionCatalog } from './host-protocol';
|
|
9
|
+
import { PackageHosts } from './package-hosts';
|
|
10
|
+
import { TaErrors } from './ta-errors';
|
|
11
|
+
|
|
12
|
+
/** The packages pi loads and the data they keep; `cwd` defaults to the server's directory. */
|
|
13
|
+
@Rpc(TaPackageServer)
|
|
14
|
+
export class TaPackageServerImpl implements TaPackageServer {
|
|
15
|
+
@Autowired(ExecEnvironmentFactory)
|
|
16
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
17
|
+
|
|
18
|
+
@Autowired(WebExtensionCatalog)
|
|
19
|
+
protected readonly webExtensions: WebExtensionCatalog;
|
|
20
|
+
|
|
21
|
+
@Autowired(ExtensionDataStore)
|
|
22
|
+
protected readonly extensionData: ExtensionDataStore;
|
|
23
|
+
|
|
24
|
+
@Autowired(ExtensionDataNotifier)
|
|
25
|
+
protected readonly extensionDataNotifier: ExtensionDataNotifier;
|
|
26
|
+
|
|
27
|
+
@Autowired(PackageHosts)
|
|
28
|
+
protected readonly packageHosts: PackageHosts;
|
|
29
|
+
|
|
30
|
+
listWebExtensions(cwd?: string): Promise<TaWebExtensionInfo[]> {
|
|
31
|
+
return TaErrors.guard(async () => this.webExtensions.list(await this.cwd(cwd)));
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
readWebExtension(id: string, cwd?: string): Promise<TaWebExtensionSource> {
|
|
35
|
+
return TaErrors.guard(async () => this.webExtensions.read(await this.cwd(cwd), id));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
removeWebExtension(id: string, cwd?: string): Promise<void> {
|
|
39
|
+
return TaErrors.guard(async () => this.webExtensions.remove(await this.cwd(cwd), id));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
setPackageEnabled(nameOrId: string, enabled: boolean, cwd?: string): Promise<void> {
|
|
43
|
+
return TaErrors.guard(async () => {
|
|
44
|
+
await this.webExtensions.setEnabled(await this.cwd(cwd), nameOrId, enabled);
|
|
45
|
+
await this.packageHosts.sync();
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
listPackages(cwd?: string): Promise<TaPackageInfo[]> {
|
|
50
|
+
return TaErrors.guard(async () => this.webExtensions.packages(await this.cwd(cwd)));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
installPackage(source: string, scope: TaSettingsScope, cwd?: string): Promise<void> {
|
|
54
|
+
return TaErrors.guard(async () => {
|
|
55
|
+
if (scope !== 'user' && scope !== 'project') {
|
|
56
|
+
throw TaErrors.invalidArgument(`${String(scope)} is not where packages are installed.`, 'Pass user or project.');
|
|
57
|
+
}
|
|
58
|
+
await this.webExtensions.install(await this.cwd(cwd), source, scope);
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
removePackage(id: string, cwd?: string): Promise<void> {
|
|
63
|
+
return TaErrors.guard(async () => this.webExtensions.removePackage(await this.cwd(cwd), id));
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
callPackageHost(name: string, method: string, params?: unknown[]): Promise<unknown> {
|
|
67
|
+
return TaErrors.guard(() => this.packageHosts.call(name, method, params ?? []));
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
getExtensionData(name: string, key: string, cwd?: string): Promise<unknown> {
|
|
71
|
+
return TaErrors.guard(() => {
|
|
72
|
+
const connection = RpcContext.connection();
|
|
73
|
+
if (connection) {
|
|
74
|
+
this.extensionDataNotifier.watch(connection, name, ExtensionDataUtil.checkCwd(cwd));
|
|
75
|
+
}
|
|
76
|
+
return this.extensionData.get(name, key, cwd);
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
setExtensionData(name: string, key: string, value: unknown, cwd?: string): Promise<void> {
|
|
81
|
+
return TaErrors.guard(() => this.extensionData.set(name, key, value, cwd));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
protected async cwd(cwd?: string): Promise<string> {
|
|
85
|
+
return cwd || (await this.environments.defaultCwd());
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { dirname, relative, sep } from 'node:path';
|
|
2
|
+
import type { PiSdk } from '@celljs/ai-coding-agent';
|
|
3
|
+
|
|
4
|
+
type SettingsManager = ReturnType<PiSdk['SettingsManager']['create']>;
|
|
5
|
+
type ResolvedPaths = Awaited<ReturnType<InstanceType<PiSdk['DefaultPackageManager']>['resolve']>>;
|
|
6
|
+
type ResolvedResource = ResolvedPaths['extensions'][number];
|
|
7
|
+
type PackageSource = NonNullable<ReturnType<SettingsManager['getGlobalSettings']>['packages']>[number];
|
|
8
|
+
|
|
9
|
+
/** What pi loads, by the key of its settings. */
|
|
10
|
+
export const PI_RESOURCE_TYPES = ['extensions', 'skills', 'prompts', 'themes'] as const;
|
|
11
|
+
export type PiResourceType = (typeof PI_RESOURCE_TYPES)[number];
|
|
12
|
+
|
|
13
|
+
/** A resource pi resolved, with its kind. */
|
|
14
|
+
export interface PiResource {
|
|
15
|
+
type: PiResourceType;
|
|
16
|
+
resource: ResolvedResource;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* A package on or off as `pi config` turns each of its resources on or off: `+path` or `-path` in the
|
|
21
|
+
* resource list of the settings for a resource of `extensions/`, in the filters of its entry of
|
|
22
|
+
* `packages` for one of a package. Ta's switch of a package is that of all its resources at once, so
|
|
23
|
+
* `ta config` and `ta` in the terminal see the same.
|
|
24
|
+
*/
|
|
25
|
+
export namespace PackageSwitch {
|
|
26
|
+
/** The resources of the package in `root`. */
|
|
27
|
+
export function resourcesOf(resolved: ResolvedPaths, root: string): PiResource[] {
|
|
28
|
+
const inside = (path: string) => path === root || path.startsWith(`${root}${sep}`);
|
|
29
|
+
return PI_RESOURCE_TYPES.flatMap(type => resolved[type].filter(resource => inside(resource.path)).map(resource => ({ type, resource })));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** On while any of its resources loads. */
|
|
33
|
+
export function isEnabled(resources: readonly PiResource[]): boolean {
|
|
34
|
+
return resources.some(({ resource }) => resource.enabled);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function set(settings: SettingsManager, resources: readonly PiResource[], enabled: boolean, dirs: { agentDir: string; projectDir: string }): void {
|
|
38
|
+
for (const { type, resource } of resources) {
|
|
39
|
+
const { metadata } = resource;
|
|
40
|
+
const project = metadata.scope === 'project';
|
|
41
|
+
if (metadata.origin === 'top-level') {
|
|
42
|
+
const base = metadata.baseDir ?? (project ? dirs.projectDir : dirs.agentDir);
|
|
43
|
+
const file = project ? settings.getProjectSettings() : settings.getGlobalSettings();
|
|
44
|
+
const updated = mark((file[type] as string[] | undefined) ?? [], relative(base, resource.path), enabled);
|
|
45
|
+
SETTERS[type](settings, project)(updated);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
const file = project ? settings.getProjectSettings() : settings.getGlobalSettings();
|
|
49
|
+
const packages: PackageSource[] = [...(file.packages ?? [])];
|
|
50
|
+
const index = packages.findIndex(pkg => (typeof pkg === 'string' ? pkg : pkg.source) === metadata.source);
|
|
51
|
+
if (index < 0) {
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const entry = packages[index];
|
|
55
|
+
const pkg = typeof entry === 'string' ? { source: entry } : { ...entry };
|
|
56
|
+
const pattern = relative(metadata.baseDir ?? dirname(resource.path), resource.path);
|
|
57
|
+
const filters = mark((pkg[type] as string[] | undefined) ?? [], pattern, enabled);
|
|
58
|
+
pkg[type] = filters.length > 0 ? filters : undefined;
|
|
59
|
+
packages[index] = PI_RESOURCE_TYPES.some(key => pkg[key] !== undefined) ? pkg : pkg.source;
|
|
60
|
+
if (project) {
|
|
61
|
+
settings.setProjectPackages(packages);
|
|
62
|
+
} else {
|
|
63
|
+
settings.setPackages(packages);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** The list without the entries of `pattern`, and `+pattern` or `-pattern` at its end, as `pi config` writes it. */
|
|
69
|
+
function mark(list: readonly string[], pattern: string, enabled: boolean): string[] {
|
|
70
|
+
const target = (entry: string) => (/^[!+-]/.test(entry) ? entry.slice(1) : entry);
|
|
71
|
+
return [...list.filter(entry => target(entry) !== pattern), `${enabled ? '+' : '-'}${pattern}`];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const SETTERS: Record<PiResourceType, (settings: SettingsManager, project: boolean) => (paths: string[]) => void> = {
|
|
75
|
+
extensions: (s, project) => paths => (project ? s.setProjectExtensionPaths(paths) : s.setExtensionPaths(paths)),
|
|
76
|
+
skills: (s, project) => paths => (project ? s.setProjectSkillPaths(paths) : s.setSkillPaths(paths)),
|
|
77
|
+
prompts: (s, project) => paths => (project ? s.setProjectPromptTemplatePaths(paths) : s.setPromptTemplatePaths(paths)),
|
|
78
|
+
themes: (s, project) => paths => (project ? s.setProjectThemePaths(paths) : s.setThemePaths(paths)),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { unwatchFile, watchFile } from 'node:fs';
|
|
2
|
+
import type { Server } from 'node:http';
|
|
3
|
+
import type { AddressInfo } from 'node:net';
|
|
4
|
+
import { type Application, ApplicationLifecycle, Autowired, Component } from '@celljs/core';
|
|
5
|
+
import { Rpc } from '@celljs/rpc';
|
|
6
|
+
import { TaBrand } from '@celljs/ta-harness/lib/node/ta-brand';
|
|
7
|
+
import { TA_WEBSOCKET_PATH, type TaRelayDevice, type TaRelayInfo, TaRelayServer } from '@celljs/ta-protocol';
|
|
8
|
+
import { RELAY_DEFAULT_URL, RelayHost, RelayPairingUtil, RelayStore } from '@celljs/ta-relay/lib/node';
|
|
9
|
+
import { ServerAware } from '@celljs/web/lib/node';
|
|
10
|
+
import { TA_ACCESS, type TaAccess } from './ta-access';
|
|
11
|
+
import { TaErrors } from './ta-errors';
|
|
12
|
+
|
|
13
|
+
/** The id of {@link RelayBridge}, for TaRelayServerImpl. */
|
|
14
|
+
export const RELAY_BRIDGE = Symbol('RelayBridge');
|
|
15
|
+
|
|
16
|
+
/** How often relay.json is looked at, for devices that `ta pair` adds or removes while the server runs. */
|
|
17
|
+
const WATCH_INTERVAL_MS = 2000;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Paired devices reach this server through a relay (@celljs/ta-relay): with a device in relay.json, it
|
|
21
|
+
* keeps a connection out to the relay and gives each device a connection of its own to this server.
|
|
22
|
+
* Without one it does nothing.
|
|
23
|
+
*/
|
|
24
|
+
@Component([RELAY_BRIDGE, ServerAware, ApplicationLifecycle])
|
|
25
|
+
export class RelayBridge implements ServerAware, ApplicationLifecycle<Application> {
|
|
26
|
+
@Autowired(TA_ACCESS)
|
|
27
|
+
protected readonly access: TaAccess;
|
|
28
|
+
|
|
29
|
+
readonly store = new RelayStore(TaBrand.agentDir());
|
|
30
|
+
protected host?: RelayHost;
|
|
31
|
+
|
|
32
|
+
get online(): boolean {
|
|
33
|
+
return this.host?.online ?? false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** relay.json changed here: no need to wait for the watch. */
|
|
37
|
+
refresh(): void {
|
|
38
|
+
this.host?.refresh();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async setServer(server: Server): Promise<void> {
|
|
42
|
+
const start = () => this.start(server);
|
|
43
|
+
if (server.listening) {
|
|
44
|
+
start();
|
|
45
|
+
} else {
|
|
46
|
+
server.once('listening', start);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async onStop(): Promise<void> {
|
|
51
|
+
unwatchFile(this.store.file);
|
|
52
|
+
this.host?.stop();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
protected start(server: Server): void {
|
|
56
|
+
const { port } = server.address() as AddressInfo;
|
|
57
|
+
const localUrl = `ws://127.0.0.1:${port}${TA_WEBSOCKET_PATH}?token=${encodeURIComponent(this.access.token)}`;
|
|
58
|
+
const host = new RelayHost({ store: this.store, localUrl });
|
|
59
|
+
this.host = host;
|
|
60
|
+
host.start();
|
|
61
|
+
watchFile(this.store.file, { interval: WATCH_INTERVAL_MS }, () => host.refresh());
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
const device = ({ id, name, createdAt }: TaRelayDevice): TaRelayDevice => ({ id, name, createdAt });
|
|
65
|
+
|
|
66
|
+
/** `ta pair` for clients: the devices of relay.json, which RelayBridge serves. Their keys never leave, but in the link of a new one. */
|
|
67
|
+
@Rpc(TaRelayServer)
|
|
68
|
+
export class TaRelayServerImpl implements TaRelayServer {
|
|
69
|
+
@Autowired(RELAY_BRIDGE)
|
|
70
|
+
protected readonly bridge: RelayBridge;
|
|
71
|
+
|
|
72
|
+
async getRelay(): Promise<TaRelayInfo> {
|
|
73
|
+
const { url, devices } = this.bridge.store.read();
|
|
74
|
+
return { relay: url, defaultRelay: RELAY_DEFAULT_URL, online: this.bridge.online, devices: devices.map(device) };
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
pair(relay: string, name: string): Promise<{ link: string; device: TaRelayDevice }> {
|
|
78
|
+
return TaErrors.guard(() => {
|
|
79
|
+
const url = typeof relay === 'string' ? relay.trim().replace(/\/+$/, '') : '';
|
|
80
|
+
if (!/^https?:\/\/[^/\s]+$/.test(url)) {
|
|
81
|
+
throw TaErrors.invalidArgument(`${relay} is not the address of a relay.`, 'Pass https://<host> of a relay, such as the default one.');
|
|
82
|
+
}
|
|
83
|
+
const pairing = this.bridge.store.pair(url, (typeof name === 'string' && name.trim()) || 'device');
|
|
84
|
+
this.bridge.refresh();
|
|
85
|
+
const created = this.bridge.store.device(pairing.device);
|
|
86
|
+
return { link: RelayPairingUtil.link(pairing), device: device(created ?? { id: pairing.device, name, createdAt: Date.now(), key: '' }) };
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async unpair(deviceId: string): Promise<void> {
|
|
91
|
+
this.bridge.store.remove(deviceId);
|
|
92
|
+
this.bridge.refresh();
|
|
93
|
+
}
|
|
94
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { SessionInfoUtil } from './session-info-util';
|
|
3
|
+
|
|
4
|
+
describe('SessionInfoUtil', () => {
|
|
5
|
+
it('takes the first line with text of the first message of the user', () => {
|
|
6
|
+
expect(
|
|
7
|
+
SessionInfoUtil.firstMessage([
|
|
8
|
+
{ role: 'custom', content: 'not the user' },
|
|
9
|
+
{ role: 'user', content: [{ type: 'image' }] },
|
|
10
|
+
{ role: 'user', content: [{ type: 'image' }, { type: 'text', text: '\n Fix the login page \nand its tests' }] },
|
|
11
|
+
{ role: 'user', content: 'later' },
|
|
12
|
+
]),
|
|
13
|
+
).toBe('Fix the login page');
|
|
14
|
+
expect(SessionInfoUtil.firstMessage([{ role: 'user', content: 'x'.repeat(200) }])).toHaveLength(120);
|
|
15
|
+
expect(SessionInfoUtil.firstMessage([{ role: 'assistant', content: 'hi' }])).toBeUndefined();
|
|
16
|
+
expect(SessionInfoUtil.firstMessage([{ role: 'user', content: '<context>\n[#web]\n</context>\n@dev hi' }])).toBe('@dev hi');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('dates a conversation by its last message, or when it was opened', () => {
|
|
20
|
+
expect(SessionInfoUtil.updatedAt([{ role: 'user', timestamp: 5 }, { role: 'assistant', timestamp: 9 }, { role: 'custom' }], 7)).toBe(9);
|
|
21
|
+
expect(SessionInfoUtil.updatedAt([], 7)).toBe(7);
|
|
22
|
+
});
|
|
23
|
+
});
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { TaMessageContext } from '@celljs/ta-protocol';
|
|
2
|
+
|
|
3
|
+
/** What these functions read of pi's messages. */
|
|
4
|
+
interface MessageLike {
|
|
5
|
+
role: string;
|
|
6
|
+
content?: unknown;
|
|
7
|
+
timestamp?: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/** What a list of conversations shows of one: its first message, as pi's session list does, and when it last changed. */
|
|
11
|
+
export namespace SessionInfoUtil {
|
|
12
|
+
const LENGTH = 120;
|
|
13
|
+
|
|
14
|
+
/** The first line of the first message of the user that has text, without its context, cut to a title's length. */
|
|
15
|
+
export function firstMessage(messages: readonly MessageLike[]): string | undefined {
|
|
16
|
+
for (const message of messages) {
|
|
17
|
+
if (message.role !== 'user') {
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
const line = TaMessageContext.strip(text(message.content))
|
|
21
|
+
.split('\n')
|
|
22
|
+
.map(part => part.trim())
|
|
23
|
+
.find(Boolean);
|
|
24
|
+
if (line) {
|
|
25
|
+
return line.length > LENGTH ? `${line.slice(0, LENGTH - 1)}…` : line;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** The time of the last message that has one, else `fallback`. */
|
|
32
|
+
export function updatedAt(messages: readonly MessageLike[], fallback: number): number {
|
|
33
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
34
|
+
const { timestamp } = messages[i];
|
|
35
|
+
if (typeof timestamp === 'number') {
|
|
36
|
+
return timestamp;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return fallback;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** The text of the content of a message. */
|
|
43
|
+
export function text(content: unknown): string {
|
|
44
|
+
if (typeof content === 'string') {
|
|
45
|
+
return content;
|
|
46
|
+
}
|
|
47
|
+
if (!Array.isArray(content)) {
|
|
48
|
+
return '';
|
|
49
|
+
}
|
|
50
|
+
return content
|
|
51
|
+
.filter((part): part is { type: 'text'; text: string } => part?.type === 'text' && typeof part.text === 'string')
|
|
52
|
+
.map(part => part.text)
|
|
53
|
+
.join('\n');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { describe, expect, it } from 'bun:test';
|
|
2
|
+
import { mkdtempSync } from 'node:fs';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { FileSessionStorage } from './file-session-storage';
|
|
6
|
+
import { LeaseHeldError, StaleLeaseError } from './session-storage-protocol';
|
|
7
|
+
|
|
8
|
+
const storage = () => new FileSessionStorage(mkdtempSync(join(tmpdir(), 'ta-sessions-')));
|
|
9
|
+
const entry = (id: string) => ({ type: 'message', id });
|
|
10
|
+
|
|
11
|
+
describe('FileSessionStorage', () => {
|
|
12
|
+
it('keeps what the holder of the lease writes, in order', async () => {
|
|
13
|
+
const store = storage();
|
|
14
|
+
expect(await store.read('s1')).toBeUndefined();
|
|
15
|
+
const lease = await store.acquire('s1', 'a', 1000);
|
|
16
|
+
expect(lease.epoch).toBe(1);
|
|
17
|
+
await store.write(lease, [{ type: 'session', id: 's1' }, entry('1')], { replace: true });
|
|
18
|
+
await store.write(lease, [entry('2')]);
|
|
19
|
+
expect((await store.read('s1'))?.map(e => e.id)).toEqual(['s1', '1', '2']);
|
|
20
|
+
await store.write(lease, [entry('x')], { replace: true });
|
|
21
|
+
expect((await store.read('s1'))?.map(e => e.id)).toEqual(['x']);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('does not give a live lease to another holder, unless it takes the session over', async () => {
|
|
25
|
+
const store = storage();
|
|
26
|
+
const first = await store.acquire('s1', 'a', 60000);
|
|
27
|
+
await expect(store.acquire('s1', 'b', 60000)).rejects.toBeInstanceOf(LeaseHeldError);
|
|
28
|
+
const second = await store.acquire('s1', 'b', 60000, { takeOver: true });
|
|
29
|
+
expect(second.epoch).toBe(first.epoch + 1);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('lets another holder take an expired or released lease, with a higher epoch', async () => {
|
|
33
|
+
const store = storage();
|
|
34
|
+
const first = await store.acquire('s1', 'a', 1);
|
|
35
|
+
await Bun.sleep(5);
|
|
36
|
+
const second = await store.acquire('s1', 'b', 60000);
|
|
37
|
+
await store.release(second);
|
|
38
|
+
const third = await store.acquire('s1', 'c', 60000);
|
|
39
|
+
expect([first.epoch, second.epoch, third.epoch]).toEqual([1, 2, 3]);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('refuses the writes and renewals of a holder that lost the session, and writes nothing of them', async () => {
|
|
43
|
+
const store = storage();
|
|
44
|
+
const old = await store.acquire('s1', 'a', 60000);
|
|
45
|
+
await store.write(old, [entry('1')]);
|
|
46
|
+
const taken = await store.acquire('s1', 'b', 60000, { takeOver: true });
|
|
47
|
+
await expect(store.write(old, [entry('late')])).rejects.toBeInstanceOf(StaleLeaseError);
|
|
48
|
+
await expect(store.renew(old, 60000)).rejects.toBeInstanceOf(StaleLeaseError);
|
|
49
|
+
await store.release(old);
|
|
50
|
+
await store.write(taken, [entry('2')]);
|
|
51
|
+
expect((await store.read('s1'))?.map(e => e.id)).toEqual(['1', '2']);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('gives one lease of an epoch when holders race for it', async () => {
|
|
55
|
+
const store = storage();
|
|
56
|
+
const results = await Promise.allSettled(['a', 'b', 'c', 'd'].map(holder => store.acquire('s1', holder, 60000)));
|
|
57
|
+
expect(results.filter(result => result.status === 'fulfilled')).toHaveLength(1);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('skips a line cut off by a crash', async () => {
|
|
61
|
+
const store = storage();
|
|
62
|
+
const lease = await store.acquire('s1', 'a', 1000);
|
|
63
|
+
await store.write(lease, [entry('1')]);
|
|
64
|
+
const { appendFileSync } = await import('node:fs');
|
|
65
|
+
appendFileSync(join((store as unknown as { dir: string }).dir, 's1.jsonl'), '{"type":"mess');
|
|
66
|
+
expect((await store.read('s1'))?.map(e => e.id)).toEqual(['1']);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('refuses ids that are not names of a file in its directory', async () => {
|
|
70
|
+
await expect(storage().acquire('../x', 'a', 1000)).rejects.toThrow('Not a session id');
|
|
71
|
+
});
|
|
72
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { type SessionLease, SessionLeaseUtil, StaleLeaseError, type StoredEntry, type TaSessionStorage } from './session-storage-protocol';
|
|
4
|
+
|
|
5
|
+
const LOCK_WAIT_MS = 5;
|
|
6
|
+
const LOCK_TIMEOUT_MS = 5000;
|
|
7
|
+
/** A lock older than this was left by a process that died holding it. */
|
|
8
|
+
const STALE_LOCK_MS = 10000;
|
|
9
|
+
const SESSION_ID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Sessions in a directory, which processes of this machine or of a shared file system can share:
|
|
13
|
+
* `<id>.jsonl` (pi's format) and `<id>.lease.json`. A lock directory makes each operation atomic; a
|
|
14
|
+
* cloud implementation does the same with conditional writes.
|
|
15
|
+
*/
|
|
16
|
+
export class FileSessionStorage implements TaSessionStorage {
|
|
17
|
+
constructor(protected readonly dir: string) {
|
|
18
|
+
mkdirSync(dir, { recursive: true });
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
acquire(sessionId: string, holder: string, ttlMs: number, options: { takeOver?: boolean } = {}): Promise<SessionLease> {
|
|
22
|
+
return this.locked(sessionId, () => {
|
|
23
|
+
const lease = SessionLeaseUtil.next(this.lease(sessionId), sessionId, holder, ttlMs, options.takeOver);
|
|
24
|
+
this.writeLease(lease);
|
|
25
|
+
return lease;
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
renew(lease: SessionLease, ttlMs: number): Promise<SessionLease> {
|
|
30
|
+
return this.locked(lease.sessionId, () => {
|
|
31
|
+
this.check(lease);
|
|
32
|
+
const renewed = { ...lease, expiresAt: Date.now() + ttlMs };
|
|
33
|
+
this.writeLease(renewed);
|
|
34
|
+
return renewed;
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
release(lease: SessionLease): Promise<void> {
|
|
39
|
+
return this.locked(lease.sessionId, () => {
|
|
40
|
+
if (SessionLeaseUtil.isCurrent(lease, this.lease(lease.sessionId))) {
|
|
41
|
+
// The epoch stays, so that the next holder's is still higher.
|
|
42
|
+
this.writeLease({ ...lease, expiresAt: 0 });
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async read(sessionId: string): Promise<StoredEntry[] | undefined> {
|
|
48
|
+
const file = this.file(sessionId, 'jsonl');
|
|
49
|
+
if (!existsSync(file)) {
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
// A line cut off by a crash in the middle of an append is left out, as pi does.
|
|
53
|
+
return readFileSync(file, 'utf8')
|
|
54
|
+
.split('\n')
|
|
55
|
+
.filter(line => line.trim())
|
|
56
|
+
.flatMap(line => {
|
|
57
|
+
try {
|
|
58
|
+
return [JSON.parse(line) as StoredEntry];
|
|
59
|
+
} catch {
|
|
60
|
+
return [];
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
write(lease: SessionLease, entries: StoredEntry[], options: { replace?: boolean } = {}): Promise<void> {
|
|
66
|
+
return this.locked(lease.sessionId, () => {
|
|
67
|
+
this.check(lease);
|
|
68
|
+
const text = entries.map(entry => `${JSON.stringify(entry)}\n`).join('');
|
|
69
|
+
const file = this.file(lease.sessionId, 'jsonl');
|
|
70
|
+
if (options.replace) {
|
|
71
|
+
this.replaceFile(file, text);
|
|
72
|
+
} else {
|
|
73
|
+
appendFileSync(file, text, { mode: 0o600 });
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
protected check(lease: SessionLease): void {
|
|
79
|
+
if (!SessionLeaseUtil.isCurrent(lease, this.lease(lease.sessionId))) {
|
|
80
|
+
throw new StaleLeaseError(lease);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
protected lease(sessionId: string): SessionLease | undefined {
|
|
85
|
+
try {
|
|
86
|
+
return JSON.parse(readFileSync(this.file(sessionId, 'lease.json'), 'utf8')) as SessionLease;
|
|
87
|
+
} catch {
|
|
88
|
+
return undefined;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
protected writeLease(lease: SessionLease): void {
|
|
93
|
+
this.replaceFile(this.file(lease.sessionId, 'lease.json'), JSON.stringify(lease));
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected replaceFile(file: string, text: string): void {
|
|
97
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
98
|
+
writeFileSync(temporary, text, { mode: 0o600 });
|
|
99
|
+
renameSync(temporary, file);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
protected file(sessionId: string, extension: string): string {
|
|
103
|
+
if (!SESSION_ID.test(sessionId)) {
|
|
104
|
+
throw new Error(`Not a session id: ${sessionId}`);
|
|
105
|
+
}
|
|
106
|
+
return join(this.dir, `${sessionId}.${extension}`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Runs `action` holding `<id>.lock`, a directory: creating one is atomic on every file system. */
|
|
110
|
+
protected async locked<T>(sessionId: string, action: () => T): Promise<T> {
|
|
111
|
+
const lock = this.file(sessionId, 'lock');
|
|
112
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
113
|
+
for (;;) {
|
|
114
|
+
try {
|
|
115
|
+
mkdirSync(lock);
|
|
116
|
+
break;
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if ((error as NodeJS.ErrnoException).code !== 'EEXIST') {
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
if (this.isStale(lock)) {
|
|
122
|
+
rmSync(lock, { recursive: true, force: true });
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
if (Date.now() > deadline) {
|
|
126
|
+
throw new Error(`The session ${sessionId} stayed locked for ${LOCK_TIMEOUT_MS} ms.`);
|
|
127
|
+
}
|
|
128
|
+
await new Promise(resolve => setTimeout(resolve, LOCK_WAIT_MS));
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
try {
|
|
132
|
+
return action();
|
|
133
|
+
} finally {
|
|
134
|
+
rmSync(lock, { recursive: true, force: true });
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
protected isStale(lock: string): boolean {
|
|
139
|
+
try {
|
|
140
|
+
return Date.now() - statSync(lock).mtimeMs > STALE_LOCK_MS;
|
|
141
|
+
} catch {
|
|
142
|
+
return false;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/** A line of a pi session file: its header, or an entry. The storage keeps them as they are. */
|
|
2
|
+
export type StoredEntry = { type: string } & Record<string, unknown>;
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The right of one holder (a ta serve) to write a session. The epoch grows by one each time a holder
|
|
6
|
+
* takes the session, and every write names it: a holder that lost the session to another can no longer
|
|
7
|
+
* write, even if it does not know yet (it was paused, or cut off). The expiry only says when another may
|
|
8
|
+
* take the session without being asked to take it over.
|
|
9
|
+
*/
|
|
10
|
+
export interface SessionLease {
|
|
11
|
+
sessionId: string;
|
|
12
|
+
holder: string;
|
|
13
|
+
epoch: number;
|
|
14
|
+
/** `Date.now()` time. */
|
|
15
|
+
expiresAt: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const TaSessionStorage = Symbol('TaSessionStorage');
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Where sessions are kept when they are not pi's files on this machine, for example in the cloud, where
|
|
22
|
+
* any of several ta serve may take a session over. Implementations make every write conditional on the
|
|
23
|
+
* epoch of the lease (a conditional write in a database, a compare-and-swap in a key-value store).
|
|
24
|
+
*/
|
|
25
|
+
export interface TaSessionStorage {
|
|
26
|
+
/** A new lease, one epoch after the last. LeaseHeldError while another holder's lease is live, unless `takeOver`. */
|
|
27
|
+
acquire(sessionId: string, holder: string, ttlMs: number, options?: { takeOver?: boolean }): Promise<SessionLease>;
|
|
28
|
+
/** The same lease for longer. StaleLeaseError if another holder took the session since. */
|
|
29
|
+
renew(lease: SessionLease, ttlMs: number): Promise<SessionLease>;
|
|
30
|
+
/** Lets another take the session at once. Does nothing if the lease is no longer the current one. */
|
|
31
|
+
release(lease: SessionLease): Promise<void>;
|
|
32
|
+
/** The entries of the session, or nothing if it has none. */
|
|
33
|
+
read(sessionId: string): Promise<StoredEntry[] | undefined>;
|
|
34
|
+
/** Adds entries, or replaces them all; StaleLeaseError, and nothing written, if the lease is not the current one. */
|
|
35
|
+
write(lease: SessionLease, entries: StoredEntry[], options?: { replace?: boolean }): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Another holder has a live lease on the session. */
|
|
39
|
+
export class LeaseHeldError extends Error {
|
|
40
|
+
readonly code = 'LEASE_HELD';
|
|
41
|
+
|
|
42
|
+
constructor(readonly current: SessionLease) {
|
|
43
|
+
super(`The session ${current.sessionId} is held by ${current.holder} until ${new Date(current.expiresAt).toISOString()}.`);
|
|
44
|
+
this.name = 'LeaseHeldError';
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/** The lease was not the current one: another holder took the session. */
|
|
49
|
+
export class StaleLeaseError extends Error {
|
|
50
|
+
readonly code = 'STALE_LEASE';
|
|
51
|
+
|
|
52
|
+
constructor(readonly lease: SessionLease) {
|
|
53
|
+
super(`The lease of epoch ${lease.epoch} on the session ${lease.sessionId} is no longer current: another holder took it.`);
|
|
54
|
+
this.name = 'StaleLeaseError';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export namespace SessionLeaseUtil {
|
|
59
|
+
export function isLive(lease: SessionLease | undefined, now = Date.now()): lease is SessionLease {
|
|
60
|
+
return lease !== undefined && lease.expiresAt > now;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** What `acquire` gives after `current`, or why it cannot. */
|
|
64
|
+
export function next(
|
|
65
|
+
current: SessionLease | undefined,
|
|
66
|
+
sessionId: string,
|
|
67
|
+
holder: string,
|
|
68
|
+
ttlMs: number,
|
|
69
|
+
takeOver = false,
|
|
70
|
+
now = Date.now(),
|
|
71
|
+
): SessionLease {
|
|
72
|
+
if (!takeOver && isLive(current, now) && current.holder !== holder) {
|
|
73
|
+
throw new LeaseHeldError(current);
|
|
74
|
+
}
|
|
75
|
+
return { sessionId, holder, epoch: (current?.epoch ?? 0) + 1, expiresAt: now + ttlMs };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function isCurrent(lease: SessionLease, current: SessionLease | undefined): boolean {
|
|
79
|
+
return current !== undefined && current.epoch === lease.epoch && current.holder === lease.holder;
|
|
80
|
+
}
|
|
81
|
+
}
|