@celljs/ta-host 4.5.1 → 4.6.1
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,27 @@
|
|
|
1
|
+
import { type SessionLease, type StoredEntry, type TaSessionStorage } from './session-storage-protocol.js';
|
|
2
|
+
/**
|
|
3
|
+
* Sessions in a directory, which processes of this machine or of a shared file system can share:
|
|
4
|
+
* `<id>.jsonl` (pi's format) and `<id>.lease.json`. A lock directory makes each operation atomic; a
|
|
5
|
+
* cloud implementation does the same with conditional writes.
|
|
6
|
+
*/
|
|
7
|
+
export declare class FileSessionStorage implements TaSessionStorage {
|
|
8
|
+
protected readonly dir: string;
|
|
9
|
+
constructor(dir: string);
|
|
10
|
+
acquire(sessionId: string, holder: string, ttlMs: number, options?: {
|
|
11
|
+
takeOver?: boolean;
|
|
12
|
+
}): Promise<SessionLease>;
|
|
13
|
+
renew(lease: SessionLease, ttlMs: number): Promise<SessionLease>;
|
|
14
|
+
release(lease: SessionLease): Promise<void>;
|
|
15
|
+
read(sessionId: string): Promise<StoredEntry[] | undefined>;
|
|
16
|
+
write(lease: SessionLease, entries: StoredEntry[], options?: {
|
|
17
|
+
replace?: boolean;
|
|
18
|
+
}): Promise<void>;
|
|
19
|
+
protected check(lease: SessionLease): void;
|
|
20
|
+
protected lease(sessionId: string): SessionLease | undefined;
|
|
21
|
+
protected writeLease(lease: SessionLease): void;
|
|
22
|
+
protected replaceFile(file: string, text: string): void;
|
|
23
|
+
protected file(sessionId: string, extension: string): string;
|
|
24
|
+
/** Runs `action` holding `<id>.lock`, a directory: creating one is atomic on every file system. */
|
|
25
|
+
protected locked<T>(sessionId: string, action: () => T): Promise<T>;
|
|
26
|
+
protected isStale(lock: string): boolean;
|
|
27
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { SessionLeaseUtil, StaleLeaseError } from './session-storage-protocol.js';
|
|
4
|
+
const LOCK_WAIT_MS = 5;
|
|
5
|
+
const LOCK_TIMEOUT_MS = 5000;
|
|
6
|
+
/** A lock older than this was left by a process that died holding it. */
|
|
7
|
+
const STALE_LOCK_MS = 10000;
|
|
8
|
+
const SESSION_ID = /^[A-Za-z0-9_-]{1,128}$/;
|
|
9
|
+
/**
|
|
10
|
+
* Sessions in a directory, which processes of this machine or of a shared file system can share:
|
|
11
|
+
* `<id>.jsonl` (pi's format) and `<id>.lease.json`. A lock directory makes each operation atomic; a
|
|
12
|
+
* cloud implementation does the same with conditional writes.
|
|
13
|
+
*/
|
|
14
|
+
export class FileSessionStorage {
|
|
15
|
+
constructor(dir) {
|
|
16
|
+
this.dir = dir;
|
|
17
|
+
mkdirSync(dir, { recursive: true });
|
|
18
|
+
}
|
|
19
|
+
acquire(sessionId, holder, ttlMs, options = {}) {
|
|
20
|
+
return this.locked(sessionId, () => {
|
|
21
|
+
const lease = SessionLeaseUtil.next(this.lease(sessionId), sessionId, holder, ttlMs, options.takeOver);
|
|
22
|
+
this.writeLease(lease);
|
|
23
|
+
return lease;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
renew(lease, ttlMs) {
|
|
27
|
+
return this.locked(lease.sessionId, () => {
|
|
28
|
+
this.check(lease);
|
|
29
|
+
const renewed = { ...lease, expiresAt: Date.now() + ttlMs };
|
|
30
|
+
this.writeLease(renewed);
|
|
31
|
+
return renewed;
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
release(lease) {
|
|
35
|
+
return this.locked(lease.sessionId, () => {
|
|
36
|
+
if (SessionLeaseUtil.isCurrent(lease, this.lease(lease.sessionId))) {
|
|
37
|
+
// The epoch stays, so that the next holder's is still higher.
|
|
38
|
+
this.writeLease({ ...lease, expiresAt: 0 });
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
async read(sessionId) {
|
|
43
|
+
const file = this.file(sessionId, 'jsonl');
|
|
44
|
+
if (!existsSync(file)) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
// A line cut off by a crash in the middle of an append is left out, as pi does.
|
|
48
|
+
return readFileSync(file, 'utf8')
|
|
49
|
+
.split('\n')
|
|
50
|
+
.filter(line => line.trim())
|
|
51
|
+
.flatMap(line => {
|
|
52
|
+
try {
|
|
53
|
+
return [JSON.parse(line)];
|
|
54
|
+
}
|
|
55
|
+
catch (_a) {
|
|
56
|
+
return [];
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
write(lease, entries, options = {}) {
|
|
61
|
+
return this.locked(lease.sessionId, () => {
|
|
62
|
+
this.check(lease);
|
|
63
|
+
const text = entries.map(entry => `${JSON.stringify(entry)}\n`).join('');
|
|
64
|
+
const file = this.file(lease.sessionId, 'jsonl');
|
|
65
|
+
if (options.replace) {
|
|
66
|
+
this.replaceFile(file, text);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
appendFileSync(file, text, { mode: 0o600 });
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
check(lease) {
|
|
74
|
+
if (!SessionLeaseUtil.isCurrent(lease, this.lease(lease.sessionId))) {
|
|
75
|
+
throw new StaleLeaseError(lease);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
lease(sessionId) {
|
|
79
|
+
try {
|
|
80
|
+
return JSON.parse(readFileSync(this.file(sessionId, 'lease.json'), 'utf8'));
|
|
81
|
+
}
|
|
82
|
+
catch (_a) {
|
|
83
|
+
return undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
writeLease(lease) {
|
|
87
|
+
this.replaceFile(this.file(lease.sessionId, 'lease.json'), JSON.stringify(lease));
|
|
88
|
+
}
|
|
89
|
+
replaceFile(file, text) {
|
|
90
|
+
const temporary = `${file}.${process.pid}.tmp`;
|
|
91
|
+
writeFileSync(temporary, text, { mode: 0o600 });
|
|
92
|
+
renameSync(temporary, file);
|
|
93
|
+
}
|
|
94
|
+
file(sessionId, extension) {
|
|
95
|
+
if (!SESSION_ID.test(sessionId)) {
|
|
96
|
+
throw new Error(`Not a session id: ${sessionId}`);
|
|
97
|
+
}
|
|
98
|
+
return join(this.dir, `${sessionId}.${extension}`);
|
|
99
|
+
}
|
|
100
|
+
/** Runs `action` holding `<id>.lock`, a directory: creating one is atomic on every file system. */
|
|
101
|
+
async locked(sessionId, action) {
|
|
102
|
+
const lock = this.file(sessionId, 'lock');
|
|
103
|
+
const deadline = Date.now() + LOCK_TIMEOUT_MS;
|
|
104
|
+
for (;;) {
|
|
105
|
+
try {
|
|
106
|
+
mkdirSync(lock);
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
catch (error) {
|
|
110
|
+
if (error.code !== 'EEXIST') {
|
|
111
|
+
throw error;
|
|
112
|
+
}
|
|
113
|
+
if (this.isStale(lock)) {
|
|
114
|
+
rmSync(lock, { recursive: true, force: true });
|
|
115
|
+
continue;
|
|
116
|
+
}
|
|
117
|
+
if (Date.now() > deadline) {
|
|
118
|
+
throw new Error(`The session ${sessionId} stayed locked for ${LOCK_TIMEOUT_MS} ms.`);
|
|
119
|
+
}
|
|
120
|
+
await new Promise(resolve => setTimeout(resolve, LOCK_WAIT_MS));
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
return action();
|
|
125
|
+
}
|
|
126
|
+
finally {
|
|
127
|
+
rmSync(lock, { recursive: true, force: true });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
isStale(lock) {
|
|
131
|
+
try {
|
|
132
|
+
return Date.now() - statSync(lock).mtimeMs > STALE_LOCK_MS;
|
|
133
|
+
}
|
|
134
|
+
catch (_a) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
//# sourceMappingURL=file-session-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-session-storage.js","sourceRoot":"","sources":["../../../src/node/session-storage/file-session-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAC3H,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAqB,gBAAgB,EAAE,eAAe,EAA2C,MAAM,4BAA4B,CAAC;AAE3I,MAAM,YAAY,GAAG,CAAC,CAAC;AACvB,MAAM,eAAe,GAAG,IAAI,CAAC;AAC7B,yEAAyE;AACzE,MAAM,aAAa,GAAG,KAAK,CAAC;AAC5B,MAAM,UAAU,GAAG,wBAAwB,CAAC;AAE5C;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IAC3B,YAA+B,GAAW;mBAAX,GAAG;QAC9B,SAAS,CAAC,GAAG,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IACxC,CAAC;IAED,OAAO,CAAC,SAAiB,EAAE,MAAc,EAAE,KAAa,EAAE,OAAO,GAA2B,EAAE;QAC1F,OAAO,IAAI,CAAC,MAAM,CAAC,SAAS,EAAE,GAAG,EAAE;YAC/B,MAAM,KAAK,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,SAAS,CAAC,EAAE,SAAS,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC;YACvG,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC;YACvB,OAAO,KAAK,CAAC;QACjB,CAAC,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,KAAmB,EAAE,KAAa;QACpC,OAAO,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,EAAE,GAAG,EAAE;YACrC,IAAI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ACD,IAAI,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,EAAE,CAAC;oBACxB,MAAM,IAAI,KAAK,CAAC,eAAe,SAAS,sBAAsB,eAAe,MAAM,CAAC,CAAC;gBACzF,CAAC;gBACD,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC;YACpE,CAAC;QACL,CAAC;QACD,IAAI,CAAC;YACD,OAAO,MAAM,EAAE,CAAC;QACpB,CAAC;gBAAS,CAAC;YACP,MAAM,CAAC,IAAI,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,CAAC;QACnD,CAAC;IACL,CAAC;IAES,OAAO,CAAC,IAAY;QAC1B,IAAI,CAAC;YACD,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,OAAO,GAAG,aAAa,CAAC;QAC/D,CAAC;mBAAO,CAAC;YACL,OAAO,KAAK,CAAC;QACjB,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/session-storage/index.ts"],"names":[],"mappings":"AAAA,cAAc,wBAAwB,CAAC;AACvC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,kBAAkB,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/** A line of a pi session file: its header, or an entry. The storage keeps them as they are. */
|
|
2
|
+
export type StoredEntry = {
|
|
3
|
+
type: string;
|
|
4
|
+
} & Record<string, unknown>;
|
|
5
|
+
/**
|
|
6
|
+
* The right of one holder (a ta serve) to write a session. The epoch grows by one each time a holder
|
|
7
|
+
* takes the session, and every write names it: a holder that lost the session to another can no longer
|
|
8
|
+
* write, even if it does not know yet (it was paused, or cut off). The expiry only says when another may
|
|
9
|
+
* take the session without being asked to take it over.
|
|
10
|
+
*/
|
|
11
|
+
export interface SessionLease {
|
|
12
|
+
sessionId: string;
|
|
13
|
+
holder: string;
|
|
14
|
+
epoch: number;
|
|
15
|
+
/** `Date.now()` time. */
|
|
16
|
+
expiresAt: number;
|
|
17
|
+
}
|
|
18
|
+
export declare const TaSessionStorage: unique symbol;
|
|
19
|
+
/**
|
|
20
|
+
* Where sessions are kept when they are not pi's files on this machine, for example in the cloud, where
|
|
21
|
+
* any of several ta serve may take a session over. Implementations make every write conditional on the
|
|
22
|
+
* epoch of the lease (a conditional write in a database, a compare-and-swap in a key-value store).
|
|
23
|
+
*/
|
|
24
|
+
export interface TaSessionStorage {
|
|
25
|
+
/** A new lease, one epoch after the last. LeaseHeldError while another holder's lease is live, unless `takeOver`. */
|
|
26
|
+
acquire(sessionId: string, holder: string, ttlMs: number, options?: {
|
|
27
|
+
takeOver?: boolean;
|
|
28
|
+
}): Promise<SessionLease>;
|
|
29
|
+
/** The same lease for longer. StaleLeaseError if another holder took the session since. */
|
|
30
|
+
renew(lease: SessionLease, ttlMs: number): Promise<SessionLease>;
|
|
31
|
+
/** Lets another take the session at once. Does nothing if the lease is no longer the current one. */
|
|
32
|
+
release(lease: SessionLease): Promise<void>;
|
|
33
|
+
/** The entries of the session, or nothing if it has none. */
|
|
34
|
+
read(sessionId: string): Promise<StoredEntry[] | undefined>;
|
|
35
|
+
/** Adds entries, or replaces them all; StaleLeaseError, and nothing written, if the lease is not the current one. */
|
|
36
|
+
write(lease: SessionLease, entries: StoredEntry[], options?: {
|
|
37
|
+
replace?: boolean;
|
|
38
|
+
}): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
/** Another holder has a live lease on the session. */
|
|
41
|
+
export declare class LeaseHeldError extends Error {
|
|
42
|
+
readonly current: SessionLease;
|
|
43
|
+
readonly code = "LEASE_HELD";
|
|
44
|
+
constructor(current: SessionLease);
|
|
45
|
+
}
|
|
46
|
+
/** The lease was not the current one: another holder took the session. */
|
|
47
|
+
export declare class StaleLeaseError extends Error {
|
|
48
|
+
readonly lease: SessionLease;
|
|
49
|
+
readonly code = "STALE_LEASE";
|
|
50
|
+
constructor(lease: SessionLease);
|
|
51
|
+
}
|
|
52
|
+
export declare namespace SessionLeaseUtil {
|
|
53
|
+
function isLive(lease: SessionLease | undefined, now?: number): lease is SessionLease;
|
|
54
|
+
/** What `acquire` gives after `current`, or why it cannot. */
|
|
55
|
+
function next(current: SessionLease | undefined, sessionId: string, holder: string, ttlMs: number, takeOver?: boolean, now?: number): SessionLease;
|
|
56
|
+
function isCurrent(lease: SessionLease, current: SessionLease | undefined): boolean;
|
|
57
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export const TaSessionStorage = Symbol('TaSessionStorage');
|
|
2
|
+
/** Another holder has a live lease on the session. */
|
|
3
|
+
export class LeaseHeldError extends Error {
|
|
4
|
+
constructor(current) {
|
|
5
|
+
super(`The session ${current.sessionId} is held by ${current.holder} until ${new Date(current.expiresAt).toISOString()}.`);
|
|
6
|
+
this.current = current;
|
|
7
|
+
this.code = 'LEASE_HELD';
|
|
8
|
+
this.name = 'LeaseHeldError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
/** The lease was not the current one: another holder took the session. */
|
|
12
|
+
export class StaleLeaseError extends Error {
|
|
13
|
+
constructor(lease) {
|
|
14
|
+
super(`The lease of epoch ${lease.epoch} on the session ${lease.sessionId} is no longer current: another holder took it.`);
|
|
15
|
+
this.lease = lease;
|
|
16
|
+
this.code = 'STALE_LEASE';
|
|
17
|
+
this.name = 'StaleLeaseError';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export var SessionLeaseUtil;
|
|
21
|
+
(function (SessionLeaseUtil) {
|
|
22
|
+
function isLive(lease, now = Date.now()) {
|
|
23
|
+
return lease !== undefined && lease.expiresAt > now;
|
|
24
|
+
}
|
|
25
|
+
SessionLeaseUtil.isLive = isLive;
|
|
26
|
+
/** What `acquire` gives after `current`, or why it cannot. */
|
|
27
|
+
function next(current, sessionId, holder, ttlMs, takeOver = false, now = Date.now()) {
|
|
28
|
+
var _a;
|
|
29
|
+
if (!takeOver && isLive(current, now) && current.holder !== holder) {
|
|
30
|
+
throw new LeaseHeldError(current);
|
|
31
|
+
}
|
|
32
|
+
return { sessionId, holder, epoch: ((_a = current === null || current === void 0 ? void 0 : current.epoch) !== null && _a !== void 0 ? _a : 0) + 1, expiresAt: now + ttlMs };
|
|
33
|
+
}
|
|
34
|
+
SessionLeaseUtil.next = next;
|
|
35
|
+
function isCurrent(lease, current) {
|
|
36
|
+
return current !== undefined && current.epoch === lease.epoch && current.holder === lease.holder;
|
|
37
|
+
}
|
|
38
|
+
SessionLeaseUtil.isCurrent = isCurrent;
|
|
39
|
+
})(SessionLeaseUtil || (SessionLeaseUtil = {}));
|
|
40
|
+
//# sourceMappingURL=session-storage-protocol.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"session-storage-protocol.js","sourceRoot":"","sources":["../../../src/node/session-storage/session-storage-protocol.ts"],"names":[],"mappings":"AAiBA,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC,kBAAkB,CAAC,CAAC;AAoB3D,sDAAsD;AACtD,MAAM,OAAO,cAAe,SAAQ,KAAK;IAGrC,YAAqB,OAAqB;QACtC,KAAK,CAAC,eAAe,OAAO,CAAC,SAAS,eAAe,OAAO,CAAC,MAAM,UAAU,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,GAAG,CAAC,CAAC;QAD1G,YAAO,GAAP,OAAO,CAAc;QAFjC,SAAI,GAAG,YAAY,CAAC;QAIzB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IACjC,CAAC;CACJ;AAED,0EAA0E;AAC1E,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAGtC,YAAqB,KAAmB;QACpC,KAAK,CAAC,sBAAsB,KAAK,CAAC,KAAK,mBAAmB,KAAK,CAAC,SAAS,gDAAgD,CAAC,CAAC;QAD1G,UAAK,GA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essionLease, type StoredEntry, type TaSessionStorage } from './session-storage-protocol.js';
|
|
2
|
+
type SessionManager = import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).SessionManager;
|
|
3
|
+
type SessionManagerClass = typeof import('@earendil-works/pi-coding-agent', { with: { 'resolution-mode': 'import' } }).SessionManager;
|
|
4
|
+
export interface StoredSessionOptions {
|
|
5
|
+
storage: TaSessionStorage;
|
|
6
|
+
sessionId: string;
|
|
7
|
+
cwd: string;
|
|
8
|
+
/** This ta serve, as the storage knows it. */
|
|
9
|
+
holder: string;
|
|
10
|
+
/** How long a lease lasts without being renewed; it is renewed at a third of that. Default 30000. */
|
|
11
|
+
leaseMs?: number;
|
|
12
|
+
/** Takes the session even while another holder's lease is live. */
|
|
13
|
+
takeOver?: boolean;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* A pi session kept in a TaSessionStorage rather than in a file: pi's SessionManager in memory, with
|
|
17
|
+
* what it would append to its file written to the storage under a lease. Writes go out in order, after
|
|
18
|
+
* pi added the entry; once the lease is lost, the next entry pi adds fails, as a failed write to its
|
|
19
|
+
* file does, and {@link onLost} tells the host to stop the session.
|
|
20
|
+
*/
|
|
21
|
+
export declare class StoredSession {
|
|
22
|
+
readonly manager: SessionManager;
|
|
23
|
+
protected lease: SessionLease;
|
|
24
|
+
protected readonly options: StoredSessionOptions;
|
|
25
|
+
protected flushed: boolean;
|
|
26
|
+
protected writing: Promise<void>;
|
|
27
|
+
protected lost?: Error;
|
|
28
|
+
protected closed: boolean;
|
|
29
|
+
protected readonly renewTimer: ReturnType<typeof setInterval>;
|
|
30
|
+
protected readonly lostListeners: Set<(error: Error) => void>;
|
|
31
|
+
protected constructor(manager: SessionManager, lease: SessionLease, options: StoredSessionOptions, stored: boolean);
|
|
32
|
+
/** Takes the lease, and opens the session from what the storage has, or a new one with that id. */
|
|
33
|
+
static open(sessionManager: SessionManagerClass, options: StoredSessionOptions): Promise<StoredSession>;
|
|
34
|
+
get epoch(): number;
|
|
35
|
+
/** Runs once, when another holder took the session. */
|
|
36
|
+
onLost(listener: (error: Error) => void): () => void;
|
|
37
|
+
/** Resolves when what pi added so far is stored; rejects if it could not be. */
|
|
38
|
+
flush(): Promise<void>;
|
|
39
|
+
/** Stores what is left and lets another holder take the session at once. */
|
|
40
|
+
close(): Promise<void>;
|
|
41
|
+
protected persist(entry: StoredEntry): void;
|
|
42
|
+
protected enqueue(entries: StoredEntry[], replace: boolean): void;
|
|
43
|
+
protected renew(leaseMs: number): Promise<void>;
|
|
44
|
+
protected fail(error: unknown): void;
|
|
45
|
+
}
|
|
46
|
+
export {};
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { StaleLeaseError } from './session-storage-protocol.js';
|
|
2
|
+
const DEFAULT_LEASE_MS = 30000;
|
|
3
|
+
/**
|
|
4
|
+
* A pi session kept in a TaSessionStorage rather than in a file: pi's SessionManager in memory, with
|
|
5
|
+
* what it would append to its file written to the storage under a lease. Writes go out in order, after
|
|
6
|
+
* pi added the entry; once the lease is lost, the next entry pi adds fails, as a failed write to its
|
|
7
|
+
* file does, and {@link onLost} tells the host to stop the session.
|
|
8
|
+
*/
|
|
9
|
+
export class StoredSession {
|
|
10
|
+
constructor(manager, lease, options, stored) {
|
|
11
|
+
var _a, _b;
|
|
12
|
+
var _c;
|
|
13
|
+
this.manager = manager;
|
|
14
|
+
this.lease = lease;
|
|
15
|
+
this.options = options;
|
|
16
|
+
this.writing = Promise.resolve();
|
|
17
|
+
this.closed = false;
|
|
18
|
+
this.lostListeners = new Set();
|
|
19
|
+
// pi writes nothing until the session has a message of the user or the model; neither does this.
|
|
20
|
+
this.flushed = stored;
|
|
21
|
+
const leaseMs = (_c = options.leaseMs) !== null && _c !== void 0 ? _c : DEFAULT_LEASE_MS;
|
|
22
|
+
this.renewTimer = setInterval(() => void this.renew(leaseMs), leaseMs / 3);
|
|
23
|
+
(_b = (_a = this.renewTimer).unref) === null || _b === void 0 ? void 0 : _b.call(_a);
|
|
24
|
+
manager._persist = entry => this.persist(entry);
|
|
25
|
+
}
|
|
26
|
+
/** Takes the lease, and opens the session from what the storage has, or a new one with that id. */
|
|
27
|
+
static async open(sessionManager, options) {
|
|
28
|
+
var _a;
|
|
29
|
+
const { storage, sessionId, holder, takeOver } = options;
|
|
30
|
+
const lease = await storage.acquire(sessionId, holder, (_a = options.leaseMs) !== null && _a !== void 0 ? _a : DEFAULT_LEASE_MS, { takeOver });
|
|
31
|
+
try {
|
|
32
|
+
const entries = await storage.read(sessionId);
|
|
33
|
+
const manager = sessionManager.inMemory(options.cwd, { id: sessionId }, entries);
|
|
34
|
+
return new StoredSession(manager, lease, options, entries !== undefined && entries.length > 0);
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
await storage.release(lease);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
get epoch() {
|
|
42
|
+
return this.lease.epoch;
|
|
43
|
+
}
|
|
44
|
+
/** Runs once, when another holder took the session. */
|
|
45
|
+
onLost(listener) {
|
|
46
|
+
this.lostListeners.add(listener);
|
|
47
|
+
return () => this.lostListeners.delete(listener);
|
|
48
|
+
}
|
|
49
|
+
/** Resolves when what pi added so far is stored; rejects if it could not be. */
|
|
50
|
+
async flush() {
|
|
51
|
+
await this.writing;
|
|
52
|
+
if (this.lost) {
|
|
53
|
+
throw this.lost;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Stores what is left and lets another holder take the session at once. */
|
|
57
|
+
async close() {
|
|
58
|
+
if (this.closed) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
this.closed = true;
|
|
62
|
+
clearInterval(this.renewTimer);
|
|
63
|
+
await this.writing.catch(() => undefined);
|
|
64
|
+
if (!this.lost) {
|
|
65
|
+
await this.options.storage.release(this.lease);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
persist(entry) {
|
|
69
|
+
if (this.lost) {
|
|
70
|
+
throw this.lost;
|
|
71
|
+
}
|
|
72
|
+
if (this.closed) {
|
|
73
|
+
throw new Error(`The session ${this.options.sessionId} is closed.`);
|
|
74
|
+
}
|
|
75
|
+
if (!this.flushed) {
|
|
76
|
+
const entries = this.manager.getEntries();
|
|
77
|
+
if (!entries.some(candidate => candidate.type === 'message' && (candidate.message.role === 'user' || candidate.message.role === 'assistant'))) {
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
this.flushed = true;
|
|
81
|
+
const header = this.manager.getHeader();
|
|
82
|
+
this.enqueue([...(header ? [header] : []), ...entries], true);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
this.enqueue([entry], false);
|
|
86
|
+
}
|
|
87
|
+
enqueue(entries, replace) {
|
|
88
|
+
const lease = this.lease;
|
|
89
|
+
this.writing = this.writing.then(() => this.lost ? undefined : this.options.storage.write(lease, entries, { replace }).catch(error => this.fail(error)));
|
|
90
|
+
}
|
|
91
|
+
async renew(leaseMs) {
|
|
92
|
+
if (this.lost || this.closed) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
this.lease = await this.options.storage.renew(this.lease, leaseMs);
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
if (error instanceof StaleLeaseError) {
|
|
100
|
+
this.fail(error);
|
|
101
|
+
}
|
|
102
|
+
// Other failures (the storage is unreachable) are tried again; the epoch guards the writes meanwhile.
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
fail(error) {
|
|
106
|
+
if (this.lost) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.lost = error instanceof Error ? error : new Error(String(error));
|
|
110
|
+
clearInterval(this.renewTimer);
|
|
111
|
+
for (const listener of this.lostListeners) {
|
|
112
|
+
listener(this.lost);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
//# sourceMappingURL=stored-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"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
|
1
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
2
|
+
import { type TaSettingsScope, TaSettingsServer, type TaSettingsSnapshot } from '@celljs/ta-protocol';
|
|
3
|
+
import { TaSettingsStore } from './settings-store.js';
|
|
4
|
+
/** The settings of ta in pi's files; `cwd` defaults to the server's directory. */
|
|
5
|
+
export declare class TaSettingsServerImpl implements TaSettingsServer {
|
|
6
|
+
protected readonly environments: ExecEnvironmentFactory;
|
|
7
|
+
protected readonly settings: TaSettingsStore;
|
|
8
|
+
getSettings(cwd?: string): Promise<TaSettingsSnapshot>;
|
|
9
|
+
updateSettings(scope: TaSettingsScope, changes: Record<string, unknown>, cwd?: string): Promise<TaSettingsSnapshot>;
|
|
10
|
+
protected cwd(cwd?: string): Promise<string>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Autowired } from '@celljs/core';
|
|
11
|
+
import { Rpc } from '@celljs/rpc';
|
|
12
|
+
import { ExecEnvironmentFactory } from '@celljs/ta-exec';
|
|
13
|
+
import { TaSettingsServer } from '@celljs/ta-protocol';
|
|
14
|
+
import { TaSettingsStore } from './settings-store.js';
|
|
15
|
+
import { TaErrors } from './ta-errors.js';
|
|
16
|
+
/** The settings of ta in pi's files; `cwd` defaults to the server's directory. */
|
|
17
|
+
let TaSettingsServerImpl = class TaSettingsServerImpl {
|
|
18
|
+
getSettings(cwd) {
|
|
19
|
+
return TaErrors.guard(async () => this.settings.get(await this.cwd(cwd)));
|
|
20
|
+
}
|
|
21
|
+
updateSettings(scope, changes, cwd) {
|
|
22
|
+
return TaErrors.guard(async () => {
|
|
23
|
+
if (scope !== 'user' && scope !== 'project') {
|
|
24
|
+
throw TaErrors.invalidArgument(`${String(scope)} is not where settings are kept.`, 'Pass user or project.');
|
|
25
|
+
}
|
|
26
|
+
return this.settings.update(scope, changes, await this.cwd(cwd));
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
async cwd(cwd) {
|
|
30
|
+
return cwd || (await this.environments.defaultCwd());
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
__decorate([
|
|
34
|
+
Autowired(ExecEnvironmentFactory),
|
|
35
|
+
__metadata("design:type", Object)
|
|
36
|
+
], TaSettingsServerImpl.prototype, "environments", void 0);
|
|
37
|
+
__decorate([
|
|
38
|
+
Autowired(TaSettingsStore),
|
|
39
|
+
__metadata("design:type", Object)
|
|
40
|
+
], TaSettingsServerImpl.prototype, "settings", void 0);
|
|
41
|
+
TaSettingsServerImpl = __decorate([
|
|
42
|
+
Rpc(TaSettingsServer)
|
|
43
|
+
], TaSettingsServerImpl);
|
|
44
|
+
export { TaSettingsServerImpl };
|
|
45
|
+
//# sourceMappingURL=settings-server-impl.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"settings-server-impl.js","sourceRoot":"","sources":["../../src/node/settings-server-impl.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO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celljs/ai-coding-agent';
|
|
2
|
+
import { type TaSettingSpec, type TaSettingsScope, type TaSettingsSnapshot } from '@celljs/ta-protocol';
|
|
3
|
+
import { TaSessionRegistry } from './host-protocol.js';
|
|
4
|
+
export declare const TaSettingsStore: unique symbol;
|
|
5
|
+
/** The settings of ta (TA_SETTINGS) in pi's `settings.json` of the user and of a project. */
|
|
6
|
+
export interface TaSettingsStore {
|
|
7
|
+
get(cwd: string): Promise<TaSettingsSnapshot>;
|
|
8
|
+
update(scope: TaSettingsScope, changes: Record<string, unknown>, cwd: string): Promise<TaSettingsSnapshot>;
|
|
9
|
+
}
|
|
10
|
+
type Json = Record<string, unknown>;
|
|
11
|
+
export declare namespace TaSettingsUtil {
|
|
12
|
+
/** The settings of TA_SETTINGS that a file sets, by key. */
|
|
13
|
+
function read(data: Json, specs?: readonly TaSettingSpec[]): Json;
|
|
14
|
+
/** The file with the changes; the other keys stay. Throws for a key or a value ta does not take. */
|
|
15
|
+
function apply(data: Json, changes: Json, specs?: readonly TaSettingSpec[]): Json;
|
|
16
|
+
/** The value as the setting keeps it, or undefined when it is not one. An empty string is no value. */
|
|
17
|
+
function check(spec: TaSettingSpec, value: unknown): unknown;
|
|
18
|
+
function parse(text: string | undefined, file: string): Json;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* The lock pi takes on a settings file while it writes it (proper-lockfile: a directory `<file>.lock`, taken
|
|
22
|
+
* over when older than 10 s), so that a write of ta serve and one of `ta` in a terminal do not lose each
|
|
23
|
+
* other's changes: pi reads the file again under the lock and writes back only what it changed.
|
|
24
|
+
*/
|
|
25
|
+
export declare namespace SettingsLockUtil {
|
|
26
|
+
const STALE_MS = 10000;
|
|
27
|
+
function run<T>(file: string, work: () => Promise<T>): Promise<T>;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Reads and writes the settings of ta where pi keeps them, under pi's lock of the file (SettingsLockUtil), and has the open sessions read them again. A
|
|
31
|
+
* file is written whole, through a temporary file, with its other keys as they were.
|
|
32
|
+
*/
|
|
33
|
+
export declare class TaSettingsStoreImpl implements TaSettingsStore {
|
|
34
|
+
protected readonly codingAgentOptions?: CodingAgentOptions;
|
|
35
|
+
protected readonly modelRuntimeProvider: ModelRuntimeProvider;
|
|
36
|
+
protected readonly sessions: TaSessionRegistry;
|
|
37
|
+
/** One write at a time, so that two changes of one file both stay. */
|
|
38
|
+
protected writing: Promise<unknown>;
|
|
39
|
+
get(cwd: string): Promise<TaSettingsSnapshot>;
|
|
40
|
+
update(scope: TaSettingsScope, changes: Record<string, unknown>, cwd: string): Promise<TaSettingsSnapshot>;
|
|
41
|
+
protected write(scope: TaSettingsScope, changes: Record<string, unknown>, cwd: string): Promise<TaSettingsSnapshot>;
|
|
42
|
+
/**
|
|
43
|
+
* `trusted`: pi's decision for the project, or false when it has resources pi asks about first;
|
|
44
|
+
* undefined when nobody decided and there is nothing to ask about, as pi trusts it then.
|
|
45
|
+
*/
|
|
46
|
+
protected files(cwd: string): Promise<{
|
|
47
|
+
userFile: string;
|
|
48
|
+
projectFile: string;
|
|
49
|
+
trusted?: boolean;
|
|
50
|
+
trust(): void;
|
|
51
|
+
}>;
|
|
52
|
+
protected read(file: string): Promise<Record<string, unknown>>;
|
|
53
|
+
protected models(): Promise<string[]>;
|
|
54
|
+
protected reloadSessions(scope: TaSettingsScope, cwd: string): Promise<void>;
|
|
55
|
+
}
|
|
56
|
+
export {};
|