@kruntime/komputer 0.1.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 +368 -0
- package/dist/agent/approvals.d.ts +28 -0
- package/dist/agent/approvals.js +109 -0
- package/dist/agent/approvals.js.map +1 -0
- package/dist/agent/messages.d.ts +67 -0
- package/dist/agent/messages.js +213 -0
- package/dist/agent/messages.js.map +1 -0
- package/dist/agent/recovery.d.ts +13 -0
- package/dist/agent/recovery.js +112 -0
- package/dist/agent/recovery.js.map +1 -0
- package/dist/agent/session-utils.d.ts +17 -0
- package/dist/agent/session-utils.js +77 -0
- package/dist/agent/session-utils.js.map +1 -0
- package/dist/agent-session.d.ts +303 -0
- package/dist/agent-session.js +3031 -0
- package/dist/agent-session.js.map +1 -0
- package/dist/authority.d.ts +34 -0
- package/dist/authority.js +105 -0
- package/dist/authority.js.map +1 -0
- package/dist/content.d.ts +17 -0
- package/dist/content.js +69 -0
- package/dist/content.js.map +1 -0
- package/dist/cron.d.ts +20 -0
- package/dist/cron.js +25 -0
- package/dist/cron.js.map +1 -0
- package/dist/drive.d.ts +198 -0
- package/dist/drive.js +656 -0
- package/dist/drive.js.map +1 -0
- package/dist/events.d.ts +20 -0
- package/dist/events.js +149 -0
- package/dist/events.js.map +1 -0
- package/dist/fs-mode.d.ts +9 -0
- package/dist/fs-mode.js +98 -0
- package/dist/fs-mode.js.map +1 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/komputer.d.ts +123 -0
- package/dist/komputer.js +733 -0
- package/dist/komputer.js.map +1 -0
- package/dist/kstate.d.ts +107 -0
- package/dist/kstate.js +529 -0
- package/dist/kstate.js.map +1 -0
- package/dist/model.d.ts +81 -0
- package/dist/model.js +824 -0
- package/dist/model.js.map +1 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -0
- package/dist/path.d.ts +4 -0
- package/dist/path.js +38 -0
- package/dist/path.js.map +1 -0
- package/dist/pool.d.ts +56 -0
- package/dist/pool.js +172 -0
- package/dist/pool.js.map +1 -0
- package/dist/receipts.d.ts +44 -0
- package/dist/receipts.js +35 -0
- package/dist/receipts.js.map +1 -0
- package/dist/remote-node.d.ts +2 -0
- package/dist/remote-node.js +122 -0
- package/dist/remote-node.js.map +1 -0
- package/dist/remote.d.ts +84 -0
- package/dist/remote.js +11 -0
- package/dist/remote.js.map +1 -0
- package/dist/sandbox.d.ts +4 -0
- package/dist/sandbox.js +70 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/shell/builtins/catalog.d.ts +6 -0
- package/dist/shell/builtins/catalog.js +12 -0
- package/dist/shell/builtins/catalog.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/basename.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/basename.js +23 -0
- package/dist/shell/builtins/commands/filesystem/basename.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/cat.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/cat.js +85 -0
- package/dist/shell/builtins/commands/filesystem/cat.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/cd.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/cd.js +14 -0
- package/dist/shell/builtins/commands/filesystem/cd.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/chmod.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/chmod.js +46 -0
- package/dist/shell/builtins/commands/filesystem/chmod.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/copy-directory.d.ts +4 -0
- package/dist/shell/builtins/commands/filesystem/copy-directory.js +48 -0
- package/dist/shell/builtins/commands/filesystem/copy-directory.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/cp.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/cp.js +92 -0
- package/dist/shell/builtins/commands/filesystem/cp.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/dirname.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/dirname.js +23 -0
- package/dist/shell/builtins/commands/filesystem/dirname.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/du.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/du.js +93 -0
- package/dist/shell/builtins/commands/filesystem/du.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/find.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/find.js +111 -0
- package/dist/shell/builtins/commands/filesystem/find.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/install.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/install.js +89 -0
- package/dist/shell/builtins/commands/filesystem/install.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/ln.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/ln.js +57 -0
- package/dist/shell/builtins/commands/filesystem/ln.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/ls.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/ls.js +153 -0
- package/dist/shell/builtins/commands/filesystem/ls.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/mkdir.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/mkdir.js +41 -0
- package/dist/shell/builtins/commands/filesystem/mkdir.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/mktemp.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/mktemp.js +94 -0
- package/dist/shell/builtins/commands/filesystem/mktemp.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/mv.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/mv.js +41 -0
- package/dist/shell/builtins/commands/filesystem/mv.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/pwd.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/pwd.js +2 -0
- package/dist/shell/builtins/commands/filesystem/pwd.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/realpath.d.ts +3 -0
- package/dist/shell/builtins/commands/filesystem/realpath.js +61 -0
- package/dist/shell/builtins/commands/filesystem/realpath.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/rm.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/rm.js +38 -0
- package/dist/shell/builtins/commands/filesystem/rm.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/stat.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/stat.js +141 -0
- package/dist/shell/builtins/commands/filesystem/stat.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem/touch.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem/touch.js +143 -0
- package/dist/shell/builtins/commands/filesystem/touch.js.map +1 -0
- package/dist/shell/builtins/commands/filesystem.d.ts +2 -0
- package/dist/shell/builtins/commands/filesystem.js +44 -0
- package/dist/shell/builtins/commands/filesystem.js.map +1 -0
- package/dist/shell/builtins/commands/network/curl.d.ts +2 -0
- package/dist/shell/builtins/commands/network/curl.js +25 -0
- package/dist/shell/builtins/commands/network/curl.js.map +1 -0
- package/dist/shell/builtins/commands/network/http.d.ts +27 -0
- package/dist/shell/builtins/commands/network/http.js +226 -0
- package/dist/shell/builtins/commands/network/http.js.map +1 -0
- package/dist/shell/builtins/commands/network/wget.d.ts +2 -0
- package/dist/shell/builtins/commands/network/wget.js +32 -0
- package/dist/shell/builtins/commands/network/wget.js.map +1 -0
- package/dist/shell/builtins/commands/network.d.ts +2 -0
- package/dist/shell/builtins/commands/network.js +9 -0
- package/dist/shell/builtins/commands/network.js.map +1 -0
- package/dist/shell/builtins/commands/process/bg.d.ts +2 -0
- package/dist/shell/builtins/commands/process/bg.js +10 -0
- package/dist/shell/builtins/commands/process/bg.js.map +1 -0
- package/dist/shell/builtins/commands/process/fg.d.ts +2 -0
- package/dist/shell/builtins/commands/process/fg.js +14 -0
- package/dist/shell/builtins/commands/process/fg.js.map +1 -0
- package/dist/shell/builtins/commands/process/jobs.d.ts +2 -0
- package/dist/shell/builtins/commands/process/jobs.js +8 -0
- package/dist/shell/builtins/commands/process/jobs.js.map +1 -0
- package/dist/shell/builtins/commands/process/jobspec.d.ts +4 -0
- package/dist/shell/builtins/commands/process/jobspec.js +20 -0
- package/dist/shell/builtins/commands/process/jobspec.js.map +1 -0
- package/dist/shell/builtins/commands/process/kill.d.ts +2 -0
- package/dist/shell/builtins/commands/process/kill.js +35 -0
- package/dist/shell/builtins/commands/process/kill.js.map +1 -0
- package/dist/shell/builtins/commands/process/ps.d.ts +2 -0
- package/dist/shell/builtins/commands/process/ps.js +6 -0
- package/dist/shell/builtins/commands/process/ps.js.map +1 -0
- package/dist/shell/builtins/commands/process/sleep.d.ts +2 -0
- package/dist/shell/builtins/commands/process/sleep.js +6 -0
- package/dist/shell/builtins/commands/process/sleep.js.map +1 -0
- package/dist/shell/builtins/commands/process/wait.d.ts +2 -0
- package/dist/shell/builtins/commands/process/wait.js +21 -0
- package/dist/shell/builtins/commands/process/wait.js.map +1 -0
- package/dist/shell/builtins/commands/process.d.ts +2 -0
- package/dist/shell/builtins/commands/process.js +19 -0
- package/dist/shell/builtins/commands/process.js.map +1 -0
- package/dist/shell/builtins/commands/system/command.d.ts +2 -0
- package/dist/shell/builtins/commands/system/command.js +42 -0
- package/dist/shell/builtins/commands/system/command.js.map +1 -0
- package/dist/shell/builtins/commands/system/date.d.ts +2 -0
- package/dist/shell/builtins/commands/system/date.js +121 -0
- package/dist/shell/builtins/commands/system/date.js.map +1 -0
- package/dist/shell/builtins/commands/system/env.d.ts +2 -0
- package/dist/shell/builtins/commands/system/env.js +84 -0
- package/dist/shell/builtins/commands/system/env.js.map +1 -0
- package/dist/shell/builtins/commands/system/export.d.ts +2 -0
- package/dist/shell/builtins/commands/system/export.js +18 -0
- package/dist/shell/builtins/commands/system/export.js.map +1 -0
- package/dist/shell/builtins/commands/system/false.d.ts +2 -0
- package/dist/shell/builtins/commands/system/false.js +4 -0
- package/dist/shell/builtins/commands/system/false.js.map +1 -0
- package/dist/shell/builtins/commands/system/help.d.ts +2 -0
- package/dist/shell/builtins/commands/system/help.js +38 -0
- package/dist/shell/builtins/commands/system/help.js.map +1 -0
- package/dist/shell/builtins/commands/system/hostname.d.ts +2 -0
- package/dist/shell/builtins/commands/system/hostname.js +6 -0
- package/dist/shell/builtins/commands/system/hostname.js.map +1 -0
- package/dist/shell/builtins/commands/system/id.d.ts +2 -0
- package/dist/shell/builtins/commands/system/id.js +55 -0
- package/dist/shell/builtins/commands/system/id.js.map +1 -0
- package/dist/shell/builtins/commands/system/printenv.d.ts +2 -0
- package/dist/shell/builtins/commands/system/printenv.js +22 -0
- package/dist/shell/builtins/commands/system/printenv.js.map +1 -0
- package/dist/shell/builtins/commands/system/read.d.ts +2 -0
- package/dist/shell/builtins/commands/system/read.js +75 -0
- package/dist/shell/builtins/commands/system/read.js.map +1 -0
- package/dist/shell/builtins/commands/system/resolve-command.d.ts +3 -0
- package/dist/shell/builtins/commands/system/resolve-command.js +34 -0
- package/dist/shell/builtins/commands/system/resolve-command.js.map +1 -0
- package/dist/shell/builtins/commands/system/set.d.ts +2 -0
- package/dist/shell/builtins/commands/system/set.js +49 -0
- package/dist/shell/builtins/commands/system/set.js.map +1 -0
- package/dist/shell/builtins/commands/system/sh.d.ts +2 -0
- package/dist/shell/builtins/commands/system/sh.js +55 -0
- package/dist/shell/builtins/commands/system/sh.js.map +1 -0
- package/dist/shell/builtins/commands/system/source.d.ts +2 -0
- package/dist/shell/builtins/commands/system/source.js +42 -0
- package/dist/shell/builtins/commands/system/source.js.map +1 -0
- package/dist/shell/builtins/commands/system/test-expression.d.ts +2 -0
- package/dist/shell/builtins/commands/system/test-expression.js +93 -0
- package/dist/shell/builtins/commands/system/test-expression.js.map +1 -0
- package/dist/shell/builtins/commands/system/test.d.ts +4 -0
- package/dist/shell/builtins/commands/system/test.js +19 -0
- package/dist/shell/builtins/commands/system/test.js.map +1 -0
- package/dist/shell/builtins/commands/system/true.d.ts +2 -0
- package/dist/shell/builtins/commands/system/true.js +2 -0
- package/dist/shell/builtins/commands/system/true.js.map +1 -0
- package/dist/shell/builtins/commands/system/type.d.ts +2 -0
- package/dist/shell/builtins/commands/system/type.js +77 -0
- package/dist/shell/builtins/commands/system/type.js.map +1 -0
- package/dist/shell/builtins/commands/system/uname.d.ts +2 -0
- package/dist/shell/builtins/commands/system/uname.js +39 -0
- package/dist/shell/builtins/commands/system/uname.js.map +1 -0
- package/dist/shell/builtins/commands/system/unset.d.ts +2 -0
- package/dist/shell/builtins/commands/system/unset.js +9 -0
- package/dist/shell/builtins/commands/system/unset.js.map +1 -0
- package/dist/shell/builtins/commands/system/which.d.ts +2 -0
- package/dist/shell/builtins/commands/system/which.js +38 -0
- package/dist/shell/builtins/commands/system/which.js.map +1 -0
- package/dist/shell/builtins/commands/system/whoami.d.ts +2 -0
- package/dist/shell/builtins/commands/system/whoami.js +6 -0
- package/dist/shell/builtins/commands/system/whoami.js.map +1 -0
- package/dist/shell/builtins/commands/system.d.ts +2 -0
- package/dist/shell/builtins/commands/system.js +49 -0
- package/dist/shell/builtins/commands/system.js.map +1 -0
- package/dist/shell/builtins/commands/text/awk.d.ts +2 -0
- package/dist/shell/builtins/commands/text/awk.js +8 -0
- package/dist/shell/builtins/commands/text/awk.js.map +1 -0
- package/dist/shell/builtins/commands/text/base64.d.ts +2 -0
- package/dist/shell/builtins/commands/text/base64.js +69 -0
- package/dist/shell/builtins/commands/text/base64.js.map +1 -0
- package/dist/shell/builtins/commands/text/cut.d.ts +2 -0
- package/dist/shell/builtins/commands/text/cut.js +35 -0
- package/dist/shell/builtins/commands/text/cut.js.map +1 -0
- package/dist/shell/builtins/commands/text/diff.d.ts +2 -0
- package/dist/shell/builtins/commands/text/diff.js +83 -0
- package/dist/shell/builtins/commands/text/diff.js.map +1 -0
- package/dist/shell/builtins/commands/text/echo.d.ts +2 -0
- package/dist/shell/builtins/commands/text/echo.js +6 -0
- package/dist/shell/builtins/commands/text/echo.js.map +1 -0
- package/dist/shell/builtins/commands/text/grep.d.ts +2 -0
- package/dist/shell/builtins/commands/text/grep.js +123 -0
- package/dist/shell/builtins/commands/text/grep.js.map +1 -0
- package/dist/shell/builtins/commands/text/head.d.ts +2 -0
- package/dist/shell/builtins/commands/text/head.js +8 -0
- package/dist/shell/builtins/commands/text/head.js.map +1 -0
- package/dist/shell/builtins/commands/text/jq.d.ts +2 -0
- package/dist/shell/builtins/commands/text/jq.js +239 -0
- package/dist/shell/builtins/commands/text/jq.js.map +1 -0
- package/dist/shell/builtins/commands/text/printf.d.ts +2 -0
- package/dist/shell/builtins/commands/text/printf.js +3 -0
- package/dist/shell/builtins/commands/text/printf.js.map +1 -0
- package/dist/shell/builtins/commands/text/sed.d.ts +2 -0
- package/dist/shell/builtins/commands/text/sed.js +21 -0
- package/dist/shell/builtins/commands/text/sed.js.map +1 -0
- package/dist/shell/builtins/commands/text/sha256sum.d.ts +2 -0
- package/dist/shell/builtins/commands/text/sha256sum.js +63 -0
- package/dist/shell/builtins/commands/text/sha256sum.js.map +1 -0
- package/dist/shell/builtins/commands/text/sort.d.ts +2 -0
- package/dist/shell/builtins/commands/text/sort.js +134 -0
- package/dist/shell/builtins/commands/text/sort.js.map +1 -0
- package/dist/shell/builtins/commands/text/tail.d.ts +2 -0
- package/dist/shell/builtins/commands/text/tail.js +10 -0
- package/dist/shell/builtins/commands/text/tail.js.map +1 -0
- package/dist/shell/builtins/commands/text/tee.d.ts +2 -0
- package/dist/shell/builtins/commands/text/tee.js +29 -0
- package/dist/shell/builtins/commands/text/tee.js.map +1 -0
- package/dist/shell/builtins/commands/text/tr.d.ts +2 -0
- package/dist/shell/builtins/commands/text/tr.js +3 -0
- package/dist/shell/builtins/commands/text/tr.js.map +1 -0
- package/dist/shell/builtins/commands/text/uniq.d.ts +2 -0
- package/dist/shell/builtins/commands/text/uniq.js +8 -0
- package/dist/shell/builtins/commands/text/uniq.js.map +1 -0
- package/dist/shell/builtins/commands/text/wc.d.ts +2 -0
- package/dist/shell/builtins/commands/text/wc.js +30 -0
- package/dist/shell/builtins/commands/text/wc.js.map +1 -0
- package/dist/shell/builtins/commands/text/xargs.d.ts +2 -0
- package/dist/shell/builtins/commands/text/xargs.js +138 -0
- package/dist/shell/builtins/commands/text/xargs.js.map +1 -0
- package/dist/shell/builtins/commands/text.d.ts +2 -0
- package/dist/shell/builtins/commands/text.js +41 -0
- package/dist/shell/builtins/commands/text.js.map +1 -0
- package/dist/shell/builtins/help-text.d.ts +69 -0
- package/dist/shell/builtins/help-text.js +381 -0
- package/dist/shell/builtins/help-text.js.map +1 -0
- package/dist/shell/builtins/path.d.ts +28 -0
- package/dist/shell/builtins/path.js +184 -0
- package/dist/shell/builtins/path.js.map +1 -0
- package/dist/shell/builtins/registry.d.ts +3 -0
- package/dist/shell/builtins/registry.js +75 -0
- package/dist/shell/builtins/registry.js.map +1 -0
- package/dist/shell/builtins/run.d.ts +4 -0
- package/dist/shell/builtins/run.js +29 -0
- package/dist/shell/builtins/run.js.map +1 -0
- package/dist/shell/builtins/runtime.d.ts +83 -0
- package/dist/shell/builtins/runtime.js +46 -0
- package/dist/shell/builtins/runtime.js.map +1 -0
- package/dist/shell/builtins/text.d.ts +90 -0
- package/dist/shell/builtins/text.js +970 -0
- package/dist/shell/builtins/text.js.map +1 -0
- package/dist/shell/control.d.ts +31 -0
- package/dist/shell/control.js +232 -0
- package/dist/shell/control.js.map +1 -0
- package/dist/shell/expansion.d.ts +30 -0
- package/dist/shell/expansion.js +514 -0
- package/dist/shell/expansion.js.map +1 -0
- package/dist/shell/functions.d.ts +10 -0
- package/dist/shell/functions.js +108 -0
- package/dist/shell/functions.js.map +1 -0
- package/dist/shell/runtime.d.ts +44 -0
- package/dist/shell/runtime.js +276 -0
- package/dist/shell/runtime.js.map +1 -0
- package/dist/shell/time.d.ts +1 -0
- package/dist/shell/time.js +68 -0
- package/dist/shell/time.js.map +1 -0
- package/dist/state-backend.d.ts +61 -0
- package/dist/state-backend.js +275 -0
- package/dist/state-backend.js.map +1 -0
- package/dist/state-node.d.ts +2 -0
- package/dist/state-node.js +266 -0
- package/dist/state-node.js.map +1 -0
- package/dist/types.d.ts +462 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +117 -0
- package/dist/utils.js.map +1 -0
- package/dist/vbash.d.ts +40 -0
- package/dist/vbash.js +389 -0
- package/dist/vbash.js.map +1 -0
- package/dist/vfs.d.ts +64 -0
- package/dist/vfs.js +248 -0
- package/dist/vfs.js.map +1 -0
- package/dist/vprocess.d.ts +139 -0
- package/dist/vprocess.js +756 -0
- package/dist/vprocess.js.map +1 -0
- package/package.json +28 -0
package/dist/komputer.js
ADDED
|
@@ -0,0 +1,733 @@
|
|
|
1
|
+
import { AgentSession } from "./agent-session.js";
|
|
2
|
+
import { defaultDisk, KState, normalizeKPath } from "./kstate.js";
|
|
3
|
+
import { echoModel, modelset } from "./model.js";
|
|
4
|
+
import { id, now } from "./utils.js";
|
|
5
|
+
import { isDrive, isMountBinding, KStateDrive, materializeFileTree, MountTable, readMountRegistry, recordMount } from "./drive.js";
|
|
6
|
+
import { appendCronRun, summarizeCronResult } from "./cron.js";
|
|
7
|
+
import { noShellRemote } from "./remote.js";
|
|
8
|
+
export class Komputer {
|
|
9
|
+
image;
|
|
10
|
+
kstate;
|
|
11
|
+
models;
|
|
12
|
+
remotes;
|
|
13
|
+
defaultRemoteName;
|
|
14
|
+
mounts;
|
|
15
|
+
commands;
|
|
16
|
+
hooks;
|
|
17
|
+
crons;
|
|
18
|
+
secrets;
|
|
19
|
+
networkHosts;
|
|
20
|
+
sessions;
|
|
21
|
+
restoredMountPaths;
|
|
22
|
+
closed;
|
|
23
|
+
workerId;
|
|
24
|
+
leaseTtlMs;
|
|
25
|
+
leaseHeartbeat;
|
|
26
|
+
constructor({ image, kstate, models, remotes, defaultRemoteName }) {
|
|
27
|
+
this.image = normalizeImage(image);
|
|
28
|
+
this.kstate = kstate;
|
|
29
|
+
this.models = models;
|
|
30
|
+
this.remotes = remotes;
|
|
31
|
+
this.defaultRemoteName = defaultRemoteName;
|
|
32
|
+
this.mounts = new MountTable(new KStateDrive(this.kstate, { name: 'kstate', prefix: '/' }));
|
|
33
|
+
this.commands = new Map(Object.entries(this.image.commands || {}));
|
|
34
|
+
this.hooks = [...(this.image.hooks || [])];
|
|
35
|
+
this.crons = [...(this.image.crons || [])];
|
|
36
|
+
this.secrets = new Map();
|
|
37
|
+
this.networkHosts = new Map();
|
|
38
|
+
this.sessions = new Map();
|
|
39
|
+
this.restoredMountPaths = new Set();
|
|
40
|
+
this.closed = false;
|
|
41
|
+
this.workerId = id('worker');
|
|
42
|
+
this.leaseTtlMs = 30_000;
|
|
43
|
+
this.leaseHeartbeat = true;
|
|
44
|
+
}
|
|
45
|
+
static async boot(image = {}, options = {}) {
|
|
46
|
+
const normalized = normalizeImage(image);
|
|
47
|
+
const disk = await resolveDisk(options);
|
|
48
|
+
const kstate = new KState(disk);
|
|
49
|
+
await kstate.init({ imageDigest: normalized.digest });
|
|
50
|
+
const models = modelset({ default: echoModel('default'), ...(normalized.models || {}), ...(options.models || {}) });
|
|
51
|
+
const { remotes, defaultRemoteName } = normalizeRemotes(options.remotes);
|
|
52
|
+
const computer = new Komputer({ image: normalized, kstate, models, remotes, defaultRemoteName });
|
|
53
|
+
computer.leaseTtlMs = options.leaseTtlMs ?? computer.leaseTtlMs;
|
|
54
|
+
computer.leaseHeartbeat = options.leaseHeartbeat ?? computer.leaseHeartbeat;
|
|
55
|
+
await computer.restoreMountRegistry();
|
|
56
|
+
await computer.mounts.activate();
|
|
57
|
+
for (const [name, value] of Object.entries(options.secrets || {})) {
|
|
58
|
+
if (typeof value === 'string')
|
|
59
|
+
computer.secret(name, value);
|
|
60
|
+
}
|
|
61
|
+
for (const [name, value] of Object.entries(options.hosts || {}))
|
|
62
|
+
computer.host(name, value);
|
|
63
|
+
await computer.installImage();
|
|
64
|
+
await computer.writeRuntimeMeta(options);
|
|
65
|
+
return computer;
|
|
66
|
+
}
|
|
67
|
+
async installImage() {
|
|
68
|
+
for (const [agentName, agentDefinition] of Object.entries(this.image.agents)) {
|
|
69
|
+
const agent = agentDefinition;
|
|
70
|
+
const home = agent.home || `/home/${agentName}`;
|
|
71
|
+
await this.kstate.ensureRootDir(home);
|
|
72
|
+
await this.kstate.ensureRootDir(`${home}/work`);
|
|
73
|
+
await this.kstate.ensureRootDir(`${home}/memory`);
|
|
74
|
+
await this.kstate.ensureRootDir(`${home}/.k`);
|
|
75
|
+
}
|
|
76
|
+
for (const [path, content] of Object.entries(this.image.files || {})) {
|
|
77
|
+
if (!(await this.kstate.exists(path))) {
|
|
78
|
+
await this.kstate.writeText(path, typeof content === 'string' ? content : JSON.stringify(content, null, 2));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
await this.kstate.ensureRootDir('/etc/k');
|
|
82
|
+
await this.kstate.ensureRootDir('/etc');
|
|
83
|
+
await this.kstate.ensureRootDir('/sys');
|
|
84
|
+
await this.writeSysFiles();
|
|
85
|
+
}
|
|
86
|
+
async writeRuntimeMeta(options = {}) {
|
|
87
|
+
const meta = {
|
|
88
|
+
schema: 'k.runtime.v1',
|
|
89
|
+
bootId: id('boot'),
|
|
90
|
+
bootedAt: now(),
|
|
91
|
+
workerId: this.workerId,
|
|
92
|
+
imageDigest: this.image.digest,
|
|
93
|
+
agents: Object.keys(this.image.agents),
|
|
94
|
+
models: this.models.aliases(),
|
|
95
|
+
remotes: [...this.remotes.keys()],
|
|
96
|
+
defaultRemote: this.defaultRemoteName,
|
|
97
|
+
hosts: Object.keys(options.hosts || {}),
|
|
98
|
+
};
|
|
99
|
+
await this.kstate.writeJson('/meta/runtime.json', meta);
|
|
100
|
+
}
|
|
101
|
+
async writeSysFiles() {
|
|
102
|
+
await this.kstate.writeText('/sys/models.md', `# Models\n\n${this.models.aliases().map((name) => `- ${name}`).join('\n')}\n`);
|
|
103
|
+
const registry = await readMountRegistry(this.kstate);
|
|
104
|
+
const mounts = this.mounts.list()
|
|
105
|
+
.map(({ path, drive, mode }) => `- ${path}: ${drive.name || 'drive'}${mode === 'ro' ? ' (ro)' : ''}`)
|
|
106
|
+
.concat(registry
|
|
107
|
+
.filter((entry) => entry.kind === 'external' && !this.mounts.has(entry.path))
|
|
108
|
+
.map((entry) => `- ${entry.path}: ${entry.driveName} (external, ${entry.mode}, not bound)`))
|
|
109
|
+
.join('\n');
|
|
110
|
+
await this.kstate.writeText('/sys/mounts.md', `# Mounts\n\n${mounts}\n`);
|
|
111
|
+
await this.kstate.writeText('/sys/session.md', '# Session\n\nSession information is written under /var/k/sessions.\n');
|
|
112
|
+
await this.kstate.writeText('/sys/secrets.md', `# Secrets\n\n${[...this.secrets.keys()].sort().map((name) => `- ${name}`).join('\n')}\n`);
|
|
113
|
+
await this.kstate.writeText('/sys/hosts.md', `# Hosts\n\n${[...this.networkHosts.keys()].sort().map((name) => `- ${name}`).join('\n')}\n`);
|
|
114
|
+
await this.kstate.writeText('/etc/hosts', [
|
|
115
|
+
'127.0.0.1 localhost',
|
|
116
|
+
...[...this.networkHosts.keys()].sort().map((name, index) => `10.0.0.${index + 2} ${name}`),
|
|
117
|
+
'',
|
|
118
|
+
].join('\n'));
|
|
119
|
+
await this.kstate.writeText('/etc/hostname', 'komputer\n');
|
|
120
|
+
}
|
|
121
|
+
async mount(path, sourceOrBinding, options = {}) {
|
|
122
|
+
const mountPath = normalizeKPath(path);
|
|
123
|
+
const binding = normalizeMountBinding(sourceOrBinding, options);
|
|
124
|
+
const { source, mode } = binding;
|
|
125
|
+
if (this.mounts.has(mountPath)) {
|
|
126
|
+
if (!this.restoredMountPaths.has(mountPath))
|
|
127
|
+
throw new Error(`drive already mounted at ${mountPath}`);
|
|
128
|
+
await this.mounts.unmount(mountPath);
|
|
129
|
+
this.restoredMountPaths.delete(mountPath);
|
|
130
|
+
}
|
|
131
|
+
if (isDrive(source)) {
|
|
132
|
+
this.mounts.mount(mountPath, source, { mode });
|
|
133
|
+
try {
|
|
134
|
+
await this.mounts.activate();
|
|
135
|
+
await recordMount(this.kstate, {
|
|
136
|
+
path: mountPath,
|
|
137
|
+
kind: 'external',
|
|
138
|
+
driveName: source.name || 'drive',
|
|
139
|
+
mode,
|
|
140
|
+
recoverable: false,
|
|
141
|
+
at: now(),
|
|
142
|
+
});
|
|
143
|
+
await this.writeSysFiles();
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
await this.mounts.unmount(mountPath).catch(() => undefined);
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
await materializeFileTree(this.kstate, mountPath, source);
|
|
152
|
+
const drive = new KStateDrive(this.kstate, { name: `owned:${mountPath}`, prefix: mountPath });
|
|
153
|
+
this.mounts.mount(mountPath, drive, { mode });
|
|
154
|
+
try {
|
|
155
|
+
await this.mounts.activate();
|
|
156
|
+
await recordMount(this.kstate, {
|
|
157
|
+
path: mountPath,
|
|
158
|
+
kind: 'owned',
|
|
159
|
+
driveName: drive.name,
|
|
160
|
+
mode,
|
|
161
|
+
recoverable: true,
|
|
162
|
+
prefix: mountPath,
|
|
163
|
+
at: now(),
|
|
164
|
+
});
|
|
165
|
+
await this.writeSysFiles();
|
|
166
|
+
}
|
|
167
|
+
catch (error) {
|
|
168
|
+
await this.mounts.unmount(mountPath).catch(() => undefined);
|
|
169
|
+
throw error;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
async restoreMountRegistry() {
|
|
173
|
+
const registry = await readMountRegistry(this.kstate);
|
|
174
|
+
for (const entry of registry) {
|
|
175
|
+
if (entry.path === '/' || this.mounts.has(entry.path))
|
|
176
|
+
continue;
|
|
177
|
+
if (entry.kind !== 'owned' || !entry.recoverable)
|
|
178
|
+
continue;
|
|
179
|
+
this.mounts.mount(entry.path, new KStateDrive(this.kstate, {
|
|
180
|
+
name: entry.driveName || `owned:${entry.path}`,
|
|
181
|
+
prefix: entry.prefix || entry.path,
|
|
182
|
+
}), { mode: entry.mode });
|
|
183
|
+
this.restoredMountPaths.add(entry.path);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
realPath(path) {
|
|
187
|
+
return this.realPathInfo(path)?.realPath;
|
|
188
|
+
}
|
|
189
|
+
realPathInfo(path) {
|
|
190
|
+
const resolved = this.mounts.resolve(path);
|
|
191
|
+
const realPath = resolved.drive.realPath?.(resolved.drivePath);
|
|
192
|
+
return realPath ? { realPath, mode: resolved.mode } : undefined;
|
|
193
|
+
}
|
|
194
|
+
secret(name, value) {
|
|
195
|
+
this.secrets.set(name, value);
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
remote(name, value) {
|
|
199
|
+
this.remotes.set(name, value);
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
defaultRemote() {
|
|
203
|
+
const remote = this.remotes.get(this.defaultRemoteName);
|
|
204
|
+
if (!remote)
|
|
205
|
+
throw new Error(`default remote not bound: ${this.defaultRemoteName}`);
|
|
206
|
+
return remote;
|
|
207
|
+
}
|
|
208
|
+
getRemote(name = this.defaultRemoteName) {
|
|
209
|
+
const remote = this.remotes.get(name);
|
|
210
|
+
if (!remote)
|
|
211
|
+
throw new Error(`remote not bound: ${name}`);
|
|
212
|
+
return remote;
|
|
213
|
+
}
|
|
214
|
+
host(name, value) {
|
|
215
|
+
this.networkHosts.set(name, value);
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
readSecret(name) {
|
|
219
|
+
const value = this.secrets.get(name);
|
|
220
|
+
if (value === undefined)
|
|
221
|
+
throw new Error(`secret not bound: ${name}`);
|
|
222
|
+
return value;
|
|
223
|
+
}
|
|
224
|
+
async fetchNetwork(url, init = {}) {
|
|
225
|
+
const parsed = parseNetworkUrl(url);
|
|
226
|
+
const request = {
|
|
227
|
+
url: parsed.url,
|
|
228
|
+
host: parsed.host,
|
|
229
|
+
path: parsed.path,
|
|
230
|
+
method: init.method || 'GET',
|
|
231
|
+
headers: init.headers || {},
|
|
232
|
+
body: init.body,
|
|
233
|
+
};
|
|
234
|
+
const local = this.networkHosts.get(parsed.host);
|
|
235
|
+
if (local)
|
|
236
|
+
return resolveNetworkHost(local, request);
|
|
237
|
+
if (!this.defaultRemote().capabilities.network || typeof fetch !== 'function') {
|
|
238
|
+
throw new Error(`network host not bound and remote fetch is unavailable: ${parsed.host}`);
|
|
239
|
+
}
|
|
240
|
+
const response = await fetch(parsed.url, {
|
|
241
|
+
method: request.method,
|
|
242
|
+
headers: request.headers,
|
|
243
|
+
body: request.body,
|
|
244
|
+
});
|
|
245
|
+
return {
|
|
246
|
+
status: response.status,
|
|
247
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
248
|
+
body: await response.text(),
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
async login(agentName = 'assistant', options = {}) {
|
|
252
|
+
if (this.closed)
|
|
253
|
+
throw new Error('Komputer is closed');
|
|
254
|
+
if (!this.image.agents[agentName])
|
|
255
|
+
throw new Error(`agent not declared in image: ${agentName}`);
|
|
256
|
+
const sessionId = options.id || `${agentName}:default`;
|
|
257
|
+
const key = `${agentName}\0${sessionId}`;
|
|
258
|
+
const cached = this.sessions.get(key);
|
|
259
|
+
if (cached && !cached.closed)
|
|
260
|
+
return cached;
|
|
261
|
+
if (cached?.closed)
|
|
262
|
+
this.sessions.delete(key);
|
|
263
|
+
const requestedMode = options.mode || 'normal';
|
|
264
|
+
const scopedMode = options.scope
|
|
265
|
+
? { name: typeof requestedMode === 'string' ? requestedMode : requestedMode.name, scope: options.scope }
|
|
266
|
+
: requestedMode;
|
|
267
|
+
const session = new AgentSession({
|
|
268
|
+
computer: this,
|
|
269
|
+
agentName,
|
|
270
|
+
sessionId,
|
|
271
|
+
mode: scopedMode,
|
|
272
|
+
});
|
|
273
|
+
let initialized = false;
|
|
274
|
+
try {
|
|
275
|
+
await session.init();
|
|
276
|
+
initialized = true;
|
|
277
|
+
this.installHooks(session);
|
|
278
|
+
await this.runLogin(session);
|
|
279
|
+
this.sessions.set(key, session);
|
|
280
|
+
await session.trace('session.login', {
|
|
281
|
+
agent: agentName,
|
|
282
|
+
sessionId,
|
|
283
|
+
mode: session.mode,
|
|
284
|
+
by: options.by || 'program:login',
|
|
285
|
+
});
|
|
286
|
+
return session;
|
|
287
|
+
}
|
|
288
|
+
catch (error) {
|
|
289
|
+
if (session.lease) {
|
|
290
|
+
if (initialized)
|
|
291
|
+
await session.detach().catch(() => undefined);
|
|
292
|
+
else
|
|
293
|
+
await session.abandonStartup().catch(() => undefined);
|
|
294
|
+
}
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
async runLogin(agent) {
|
|
299
|
+
const login = this.image.logins[agent.agentName];
|
|
300
|
+
if (!login)
|
|
301
|
+
return;
|
|
302
|
+
await login(agent, agent.initResult ?? { firstLogin: false, recovered: true });
|
|
303
|
+
await agent.ready();
|
|
304
|
+
}
|
|
305
|
+
async recoverSessions(options = {}) {
|
|
306
|
+
const skipBusy = options.skipBusy ?? true;
|
|
307
|
+
const recovered = [];
|
|
308
|
+
for (const sessionId of await this.kstate.listSessionIds()) {
|
|
309
|
+
const record = await this.kstate.readSession(sessionId);
|
|
310
|
+
if (!record?.agent || !this.image.agents[record.agent])
|
|
311
|
+
continue;
|
|
312
|
+
if (!options.includeClosed && record.status === 'closed')
|
|
313
|
+
continue;
|
|
314
|
+
try {
|
|
315
|
+
recovered.push(await this.login(record.agent, {
|
|
316
|
+
id: sessionId,
|
|
317
|
+
mode: record.mode || 'normal',
|
|
318
|
+
scope: record.scope,
|
|
319
|
+
by: 'system:recovery',
|
|
320
|
+
}));
|
|
321
|
+
}
|
|
322
|
+
catch (error) {
|
|
323
|
+
if (skipBusy && error instanceof Error && /lease busy/.test(error.message))
|
|
324
|
+
continue;
|
|
325
|
+
throw error;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
return recovered;
|
|
329
|
+
}
|
|
330
|
+
async detachIdleSessions(options = {}) {
|
|
331
|
+
const idleForMs = Math.max(0, options.idleForMs ?? 0);
|
|
332
|
+
const nowMs = timeMs(options.now ?? Date.now());
|
|
333
|
+
const detached = [];
|
|
334
|
+
for (const [key, session] of [...this.sessions.entries()]) {
|
|
335
|
+
if (session.closed) {
|
|
336
|
+
this.sessions.delete(key);
|
|
337
|
+
continue;
|
|
338
|
+
}
|
|
339
|
+
const record = await this.kstate.readSession(session.sessionId);
|
|
340
|
+
const activity = timeMs(record?.updatedAt ?? record?.createdAt ?? 0);
|
|
341
|
+
if (nowMs - activity < idleForMs)
|
|
342
|
+
continue;
|
|
343
|
+
if (!options.includeActiveJobs) {
|
|
344
|
+
const jobs = await session.jobs();
|
|
345
|
+
if (jobs.some((job) => job.status === 'running'))
|
|
346
|
+
continue;
|
|
347
|
+
}
|
|
348
|
+
await session.detach();
|
|
349
|
+
this.sessions.delete(key);
|
|
350
|
+
detached.push(session.sessionId);
|
|
351
|
+
}
|
|
352
|
+
return detached;
|
|
353
|
+
}
|
|
354
|
+
defineCommand(name, definition) {
|
|
355
|
+
const command = typeof definition === 'function'
|
|
356
|
+
? { name, kind: 'module', handler: definition }
|
|
357
|
+
: { name, ...definition, kind: definition.kind ?? 'module' };
|
|
358
|
+
this.commands.set(name, command);
|
|
359
|
+
return command;
|
|
360
|
+
}
|
|
361
|
+
bridgeCommand(name, definition) {
|
|
362
|
+
const command = makeBridgeCommand({ name, ...definition, remote: definition.remote || this.defaultRemoteName });
|
|
363
|
+
this.commands.set(name, command);
|
|
364
|
+
return command;
|
|
365
|
+
}
|
|
366
|
+
async runCron(name, options = {}) {
|
|
367
|
+
const matched = this.crons.filter((cron) => !name || cron.name === name);
|
|
368
|
+
if (!matched.length)
|
|
369
|
+
throw new Error(`cron not found: ${name}`);
|
|
370
|
+
const results = [];
|
|
371
|
+
for (const cron of matched) {
|
|
372
|
+
const agentName = options.agent || cron.user || cron.agent || this.defaultAgentName();
|
|
373
|
+
if (cron.scope === 'system' && !cron.user && !options.agent) {
|
|
374
|
+
throw new Error(`system cron must declare user: ${cron.path || cron.name}`);
|
|
375
|
+
}
|
|
376
|
+
const lease = await this.kstate.acquireRuntimeLease('crons', cron.name, { owner: this.workerId, ttlMs: this.leaseTtlMs });
|
|
377
|
+
const runId = id('cron');
|
|
378
|
+
const startedAt = now();
|
|
379
|
+
const sessionId = options.sessionId || `cron:${cron.name}`;
|
|
380
|
+
let agent;
|
|
381
|
+
try {
|
|
382
|
+
agent = await this.login(agentName, { id: sessionId });
|
|
383
|
+
const ctx = this.runtimeContext(agent, { kind: 'cron', name: cron.name });
|
|
384
|
+
await appendCronRun(this.kstate, {
|
|
385
|
+
runId,
|
|
386
|
+
name: cron.name,
|
|
387
|
+
agent: agentName,
|
|
388
|
+
sessionId,
|
|
389
|
+
workerId: this.workerId,
|
|
390
|
+
kind: cron.kind,
|
|
391
|
+
path: cron.path,
|
|
392
|
+
status: 'running',
|
|
393
|
+
startedAt,
|
|
394
|
+
});
|
|
395
|
+
await agent.signal('cron.start', { runId, name: cron.name, kind: cron.kind, path: cron.path });
|
|
396
|
+
let result;
|
|
397
|
+
if (cron.kind === 'module') {
|
|
398
|
+
if (!cron.handler)
|
|
399
|
+
throw new Error(`cron has no handler: ${cron.name}`);
|
|
400
|
+
result = await cron.handler(ctx, options.input || {});
|
|
401
|
+
}
|
|
402
|
+
else if (cron.kind === 'shell') {
|
|
403
|
+
result = await agent.execInternal(String(cron.script ?? ''), { by: `system:cron:${cron.name}` });
|
|
404
|
+
}
|
|
405
|
+
else if (cron.kind === 'markdown') {
|
|
406
|
+
result = await agent.run(String(cron.prompt ?? ''), { by: `system:cron:${cron.name}` });
|
|
407
|
+
}
|
|
408
|
+
const endedAt = now();
|
|
409
|
+
await appendCronRun(this.kstate, {
|
|
410
|
+
runId,
|
|
411
|
+
name: cron.name,
|
|
412
|
+
agent: agentName,
|
|
413
|
+
sessionId,
|
|
414
|
+
workerId: this.workerId,
|
|
415
|
+
status: 'completed',
|
|
416
|
+
endedAt,
|
|
417
|
+
result: summarizeCronResult(result),
|
|
418
|
+
});
|
|
419
|
+
await agent.signal('cron.end', { runId, name: cron.name, status: 'completed', result: summarizeCronResult(result) });
|
|
420
|
+
results.push(result);
|
|
421
|
+
}
|
|
422
|
+
catch (error) {
|
|
423
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
424
|
+
const endedAt = now();
|
|
425
|
+
await appendCronRun(this.kstate, {
|
|
426
|
+
runId,
|
|
427
|
+
name: cron.name,
|
|
428
|
+
agent: agentName,
|
|
429
|
+
sessionId,
|
|
430
|
+
workerId: this.workerId,
|
|
431
|
+
status: 'failed',
|
|
432
|
+
endedAt,
|
|
433
|
+
error: message,
|
|
434
|
+
});
|
|
435
|
+
await agent?.signal('cron.end', { runId, name: cron.name, status: 'failed', error: message }, { level: 'error' });
|
|
436
|
+
throw error;
|
|
437
|
+
}
|
|
438
|
+
finally {
|
|
439
|
+
await lease.release();
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return results;
|
|
443
|
+
}
|
|
444
|
+
installHooks(agent) {
|
|
445
|
+
for (const hook of this.hooks) {
|
|
446
|
+
if (!hook.event)
|
|
447
|
+
continue;
|
|
448
|
+
if (hook.scope === 'agent' && hook.agent && hook.agent !== agent.agentName)
|
|
449
|
+
continue;
|
|
450
|
+
agent.hook(hook.event, async (ctx) => {
|
|
451
|
+
if (hook.kind === 'module')
|
|
452
|
+
return hook.handler?.(ctx);
|
|
453
|
+
if (hook.kind === 'shell') {
|
|
454
|
+
await agent.execInternal(String(hook.script ?? ''), { by: `system:hook:${hook.event}` });
|
|
455
|
+
return undefined;
|
|
456
|
+
}
|
|
457
|
+
if (hook.kind === 'markdown') {
|
|
458
|
+
await agent.run(String(hook.prompt ?? ''), { by: `system:hook:${hook.event}` });
|
|
459
|
+
return undefined;
|
|
460
|
+
}
|
|
461
|
+
return undefined;
|
|
462
|
+
});
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
runtimeContext(agent, meta = {}) {
|
|
466
|
+
const args = recordField(meta.input) || recordField(meta.args) || {};
|
|
467
|
+
return {
|
|
468
|
+
...meta,
|
|
469
|
+
input: args,
|
|
470
|
+
args,
|
|
471
|
+
computer: agent.localComputer,
|
|
472
|
+
agent,
|
|
473
|
+
exec: (command, options = {}) => agent.execCommand(command, options),
|
|
474
|
+
remote: (name) => ({
|
|
475
|
+
name,
|
|
476
|
+
exec: (command, options = {}) => typeof command === 'string'
|
|
477
|
+
? agent.execCommand(command, { ...options, remote: options.remote || name })
|
|
478
|
+
: agent.execCommand({ ...command, remote: command.remote || name }, { ...options, remote: options.remote || name }),
|
|
479
|
+
}),
|
|
480
|
+
model: (alias = 'default') => {
|
|
481
|
+
const model = this.models.get(alias);
|
|
482
|
+
return {
|
|
483
|
+
generate: (req = {}) => model.complete({
|
|
484
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: req.input || req.text || req.prompt || '' }] }],
|
|
485
|
+
}),
|
|
486
|
+
ask: (input) => model.complete({
|
|
487
|
+
messages: [{ role: 'user', content: [{ type: 'text', text: String(input) }] }],
|
|
488
|
+
}),
|
|
489
|
+
};
|
|
490
|
+
},
|
|
491
|
+
stdout: (text) => ({ type: 'stdout', text: String(text) }),
|
|
492
|
+
stderr: (text, code = 1) => ({ type: 'stderr', text: String(text), code }),
|
|
493
|
+
result: (output) => ({
|
|
494
|
+
stdout: String(output.stdout ?? ''),
|
|
495
|
+
stderr: String(output.stderr ?? ''),
|
|
496
|
+
code: output.code ?? (output.stderr ? 1 : 0),
|
|
497
|
+
}),
|
|
498
|
+
json: (value) => ({ type: 'json', value }),
|
|
499
|
+
secret: (name) => this.readSecret(name),
|
|
500
|
+
emit: (name, data, options) => agent.signal(name, data, options),
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
defaultAgentName() {
|
|
504
|
+
return Object.keys(this.image.agents)[0] || 'assistant';
|
|
505
|
+
}
|
|
506
|
+
async snapshot({ label } = {}) {
|
|
507
|
+
const sessions = [];
|
|
508
|
+
for (const sessionId of await this.kstate.listSessionIds()) {
|
|
509
|
+
const session = await this.kstate.readSession(sessionId);
|
|
510
|
+
const trace = await this.kstate.readSessionJsonl(sessionId, 'trace.jsonl');
|
|
511
|
+
const messages = await this.kstate.readSessionJsonl(sessionId, 'messages.full.jsonl');
|
|
512
|
+
const events = await this.kstate.readSessionJsonl(sessionId, 'events.jsonl');
|
|
513
|
+
const inputs = await this.kstate.readSessionJsonl(sessionId, 'inputs.jsonl');
|
|
514
|
+
const processes = await this.kstate.readSessionJsonl(sessionId, 'processes/processes.jsonl');
|
|
515
|
+
sessions.push({
|
|
516
|
+
id: sessionId,
|
|
517
|
+
agent: session?.agent,
|
|
518
|
+
status: session?.status,
|
|
519
|
+
mode: session?.mode,
|
|
520
|
+
worker: session?.worker,
|
|
521
|
+
traceSeq: numberField(trace.at(-1), 'traceSeq', -1),
|
|
522
|
+
messageSeq: numberField(messages.at(-1), 'seq', -1),
|
|
523
|
+
eventCursor: numberField(events.at(-1), 'cursor', -1),
|
|
524
|
+
inputCount: inputs.length,
|
|
525
|
+
processCount: processes.length,
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
const snapshot = {
|
|
529
|
+
schema: 'k.snapshot.v1',
|
|
530
|
+
id: id('snapshot'),
|
|
531
|
+
label,
|
|
532
|
+
createdAt: now(),
|
|
533
|
+
imageDigest: this.image.digest,
|
|
534
|
+
disk: {
|
|
535
|
+
kind: this.kstate.backend.kind,
|
|
536
|
+
name: this.kstate.path,
|
|
537
|
+
},
|
|
538
|
+
sessions,
|
|
539
|
+
kstate: this.kstate.path,
|
|
540
|
+
};
|
|
541
|
+
await this.kstate.writeJson(`/snapshots/${snapshot.id}.json`, snapshot);
|
|
542
|
+
return snapshot;
|
|
543
|
+
}
|
|
544
|
+
stats() {
|
|
545
|
+
let localActiveProcesses = 0;
|
|
546
|
+
for (const session of this.sessions.values()) {
|
|
547
|
+
localActiveProcesses += session.stats().localActiveProcesses;
|
|
548
|
+
}
|
|
549
|
+
return {
|
|
550
|
+
closed: this.closed,
|
|
551
|
+
workerId: this.workerId,
|
|
552
|
+
imageDigest: this.image.digest,
|
|
553
|
+
remote: this.defaultRemoteName,
|
|
554
|
+
remotes: [...this.remotes.keys()],
|
|
555
|
+
localSessions: this.sessions.size,
|
|
556
|
+
localActiveProcesses,
|
|
557
|
+
commands: this.commands.size,
|
|
558
|
+
hooks: this.hooks.length,
|
|
559
|
+
crons: this.crons.length,
|
|
560
|
+
mounts: this.mounts.list().length,
|
|
561
|
+
hosts: this.networkHosts.size,
|
|
562
|
+
models: this.models.aliases().length,
|
|
563
|
+
};
|
|
564
|
+
}
|
|
565
|
+
async close(options = {}) {
|
|
566
|
+
if (this.closed)
|
|
567
|
+
return;
|
|
568
|
+
const action = options.endSessions ? 'close' : 'detach';
|
|
569
|
+
let closeError;
|
|
570
|
+
try {
|
|
571
|
+
await Promise.all([...this.sessions.values()].map((session) => session[action]()));
|
|
572
|
+
}
|
|
573
|
+
catch (error) {
|
|
574
|
+
closeError = error;
|
|
575
|
+
}
|
|
576
|
+
finally {
|
|
577
|
+
try {
|
|
578
|
+
await this.mounts.unmountAll();
|
|
579
|
+
}
|
|
580
|
+
catch (error) {
|
|
581
|
+
closeError ??= error;
|
|
582
|
+
}
|
|
583
|
+
try {
|
|
584
|
+
await Promise.all([...new Set(this.remotes.values())].map((remote) => remote.close?.()));
|
|
585
|
+
}
|
|
586
|
+
catch (error) {
|
|
587
|
+
closeError ??= error;
|
|
588
|
+
}
|
|
589
|
+
try {
|
|
590
|
+
await this.kstate.backend.close?.();
|
|
591
|
+
}
|
|
592
|
+
catch (error) {
|
|
593
|
+
closeError ??= error;
|
|
594
|
+
}
|
|
595
|
+
this.closed = true;
|
|
596
|
+
this.sessions.clear();
|
|
597
|
+
}
|
|
598
|
+
if (closeError)
|
|
599
|
+
throw closeError;
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
function normalizeRemotes(remotes) {
|
|
603
|
+
const entries = Object.entries(remotes || {});
|
|
604
|
+
if (!entries.length)
|
|
605
|
+
return { remotes: new Map([['local', noShellRemote]]), defaultRemoteName: 'local' };
|
|
606
|
+
const table = new Map(entries);
|
|
607
|
+
const defaultRemoteName = table.has('local') ? 'local' : entries[0][0];
|
|
608
|
+
return { remotes: table, defaultRemoteName };
|
|
609
|
+
}
|
|
610
|
+
async function resolveDisk(options) {
|
|
611
|
+
if (options.disk)
|
|
612
|
+
return options.disk;
|
|
613
|
+
return defaultDisk();
|
|
614
|
+
}
|
|
615
|
+
export function image(definition = {}) {
|
|
616
|
+
return normalizeImage(definition);
|
|
617
|
+
}
|
|
618
|
+
export function agent(definition = {}) {
|
|
619
|
+
return definition;
|
|
620
|
+
}
|
|
621
|
+
export async function boot(imageDefinition = {}, options = {}) {
|
|
622
|
+
return Komputer.boot(imageDefinition, options);
|
|
623
|
+
}
|
|
624
|
+
function normalizeImage(image) {
|
|
625
|
+
const agents = image.agents && Object.keys(image.agents).length
|
|
626
|
+
? image.agents
|
|
627
|
+
: { assistant: { home: '/home/assistant', model: 'default' } };
|
|
628
|
+
return {
|
|
629
|
+
schema: 'k.image.v1',
|
|
630
|
+
digest: image.digest || 'dev-image',
|
|
631
|
+
agents,
|
|
632
|
+
files: image.files || {},
|
|
633
|
+
models: image.models || {},
|
|
634
|
+
logins: image.logins || {},
|
|
635
|
+
needs: image.needs || {},
|
|
636
|
+
commands: image.commands || {},
|
|
637
|
+
hooks: image.hooks || [],
|
|
638
|
+
crons: image.crons || [],
|
|
639
|
+
sourceDir: image.sourceDir,
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
function makeBridgeCommand(definition) {
|
|
643
|
+
return {
|
|
644
|
+
...definition,
|
|
645
|
+
kind: 'module',
|
|
646
|
+
async handler(ctx, input = {}) {
|
|
647
|
+
const argv = typeof definition.argv === 'function'
|
|
648
|
+
? definition.argv(input)
|
|
649
|
+
: definition.argv ?? inputArgv(input);
|
|
650
|
+
return ctx.exec({
|
|
651
|
+
command: definition.command,
|
|
652
|
+
argv,
|
|
653
|
+
stdin: typeof input.stdin === 'string' ? input.stdin : undefined,
|
|
654
|
+
remote: definition.remote,
|
|
655
|
+
limits: definition.limits,
|
|
656
|
+
}, {
|
|
657
|
+
by: typeof input.by === 'string' ? input.by : 'system:bridge-command',
|
|
658
|
+
});
|
|
659
|
+
},
|
|
660
|
+
};
|
|
661
|
+
}
|
|
662
|
+
function inputArgv(input) {
|
|
663
|
+
const argv = input.argv;
|
|
664
|
+
if (!Array.isArray(argv))
|
|
665
|
+
return [];
|
|
666
|
+
return argv.filter((value) => typeof value === 'string');
|
|
667
|
+
}
|
|
668
|
+
function normalizeMountBinding(sourceOrBinding, options) {
|
|
669
|
+
if (isMountBinding(sourceOrBinding))
|
|
670
|
+
return sourceOrBinding;
|
|
671
|
+
return { source: sourceOrBinding, mode: options.mode ?? 'rw' };
|
|
672
|
+
}
|
|
673
|
+
function numberField(value, key, fallback) {
|
|
674
|
+
return value && typeof value === 'object' && typeof value[key] === 'number'
|
|
675
|
+
? value[key]
|
|
676
|
+
: fallback;
|
|
677
|
+
}
|
|
678
|
+
function timeMs(value) {
|
|
679
|
+
if (value instanceof Date)
|
|
680
|
+
return value.getTime();
|
|
681
|
+
if (typeof value === 'number')
|
|
682
|
+
return value;
|
|
683
|
+
const parsed = Date.parse(value);
|
|
684
|
+
return Number.isFinite(parsed) ? parsed : 0;
|
|
685
|
+
}
|
|
686
|
+
function recordField(value) {
|
|
687
|
+
return value && typeof value === 'object' && !Array.isArray(value) ? value : undefined;
|
|
688
|
+
}
|
|
689
|
+
function parseNetworkUrl(input) {
|
|
690
|
+
const raw = input.includes('://') ? input : `http://${input}`;
|
|
691
|
+
const parsed = new URL(raw);
|
|
692
|
+
return {
|
|
693
|
+
url: parsed.toString(),
|
|
694
|
+
host: parsed.hostname,
|
|
695
|
+
path: `${parsed.pathname}${parsed.search}`,
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
async function resolveNetworkHost(host, request) {
|
|
699
|
+
if (typeof host === 'function')
|
|
700
|
+
return host(request);
|
|
701
|
+
if (typeof host === 'string')
|
|
702
|
+
return fetchRealUrl(joinBaseUrl(host, request.path), request);
|
|
703
|
+
if ('fetch' in host && typeof host.fetch === 'function')
|
|
704
|
+
return host.fetch(request);
|
|
705
|
+
if ('url' in host && typeof host.url === 'string')
|
|
706
|
+
return fetchRealUrl(joinBaseUrl(host.url, request.path), request);
|
|
707
|
+
const files = (('files' in host && host.files && typeof host.files === 'object') ? host.files : host);
|
|
708
|
+
const key = request.path.split('?')[0] || '/';
|
|
709
|
+
const body = files[key] ?? files[key.replace(/^\//, '')];
|
|
710
|
+
if (body === undefined)
|
|
711
|
+
return { status: 404, body: `not found: ${key}\n` };
|
|
712
|
+
return { status: 200, body };
|
|
713
|
+
}
|
|
714
|
+
async function fetchRealUrl(url, request) {
|
|
715
|
+
if (typeof fetch !== 'function')
|
|
716
|
+
throw new Error('global fetch is not available for network host');
|
|
717
|
+
const response = await fetch(url, {
|
|
718
|
+
method: request.method,
|
|
719
|
+
headers: request.headers,
|
|
720
|
+
body: request.body,
|
|
721
|
+
});
|
|
722
|
+
return {
|
|
723
|
+
status: response.status,
|
|
724
|
+
headers: Object.fromEntries(response.headers.entries()),
|
|
725
|
+
body: await response.text(),
|
|
726
|
+
};
|
|
727
|
+
}
|
|
728
|
+
function joinBaseUrl(base, path) {
|
|
729
|
+
const cleanBase = base.replace(/\/+$/, '');
|
|
730
|
+
const cleanPath = path.startsWith('/') ? path : `/${path}`;
|
|
731
|
+
return `${cleanBase}${cleanPath}`;
|
|
732
|
+
}
|
|
733
|
+
//# sourceMappingURL=komputer.js.map
|