khala-cli 0.4.7 → 0.4.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (49) hide show
  1. package/README.md +20 -3
  2. package/dist/THIRD_PARTY_NOTICES.txt +781 -0
  3. package/dist/{chunk-SU6362YU.mjs → chunk-3KDOTR54.mjs} +4 -2
  4. package/dist/{chunk-K6EJSZYO.mjs → chunk-3R6HFSLL.mjs} +149 -40
  5. package/dist/{chunk-OAFYPBCP.mjs → chunk-47XNLAJG.mjs} +5 -1
  6. package/dist/chunk-6BO3JSUP.mjs +33 -0
  7. package/dist/chunk-7R3EIDGR.mjs +58 -0
  8. package/dist/{chunk-YIHRHD7W.mjs → chunk-ANSR4IAW.mjs} +5 -1
  9. package/dist/chunk-BEO7WIIM.mjs +153 -0
  10. package/dist/chunk-BLCRVRIK.mjs +1000 -0
  11. package/dist/{chunk-LNG3ZZBK.mjs → chunk-BV45NK7K.mjs} +5 -1
  12. package/dist/{chunk-6YEYXZZB.mjs → chunk-DJM23SIU.mjs} +8 -8
  13. package/dist/{chunk-25AX7UMF.mjs → chunk-FO5GXCST.mjs} +24 -10
  14. package/dist/{chunk-IG2NU5J6.mjs → chunk-FZEH2IXZ.mjs} +95 -15
  15. package/dist/{chunk-TOBQUAPD.mjs → chunk-GSZADJMW.mjs} +253 -73
  16. package/dist/chunk-ITFD3JZS.mjs +1137 -0
  17. package/dist/chunk-K7V7X6WU.mjs +207 -0
  18. package/dist/chunk-KMLXOPBQ.mjs +20 -0
  19. package/dist/chunk-LBT47HLZ.mjs +19 -0
  20. package/dist/{chunk-3OFKVYOX.mjs → chunk-NGSYOVQ4.mjs} +12 -1
  21. package/dist/chunk-NIX5F4LT.mjs +863 -0
  22. package/dist/chunk-NPSQNCOY.mjs +590 -0
  23. package/dist/chunk-P3YIN7G7.mjs +26 -0
  24. package/dist/chunk-PAJ4JJPV.mjs +9827 -0
  25. package/dist/{chunk-ODTSGEID.mjs → chunk-PDZ5Z5ZE.mjs} +9 -6
  26. package/dist/chunk-QKWEZXR3.mjs +52 -0
  27. package/dist/chunk-SSO5RFOX.mjs +7 -0
  28. package/dist/{chunk-W5HS2PQO.mjs → chunk-T7IL7V7Q.mjs} +11 -9
  29. package/dist/{chunk-OP2HZ7EM.mjs → chunk-TANCKTDK.mjs} +24 -5
  30. package/dist/chunk-TEFOQXBK.mjs +44 -0
  31. package/dist/{chunk-7V2YQIRI.mjs → chunk-UK5GJO4Y.mjs} +22 -9
  32. package/dist/{chunk-C5SMOUTO.mjs → chunk-V4F3SYKW.mjs} +38 -11
  33. package/dist/{chunk-L7L6UNXN.mjs → chunk-WDOHRV3D.mjs} +31 -19
  34. package/dist/chunk-XBQDIKB5.mjs +18 -0
  35. package/dist/chunk-XUCWSYVS.mjs +148 -0
  36. package/dist/khala.mjs +20 -13
  37. package/dist/web/assets/local-CFzRHZpB.js +9 -0
  38. package/dist/web/assets/{local-OwDq47O4.css → local-ciypUFoK.css} +1 -1
  39. package/dist/web/index.html +2 -2
  40. package/package.json +5 -3
  41. package/dist/chunk-4LSCOWWN.mjs +0 -237
  42. package/dist/chunk-5GLTKWJ5.mjs +0 -1
  43. package/dist/chunk-5RGEO5HN.mjs +0 -142
  44. package/dist/chunk-APIWKRKG.mjs +0 -8
  45. package/dist/chunk-JEI32DEA.mjs +0 -634
  46. package/dist/chunk-S3FX7VCY.mjs +0 -386
  47. package/dist/chunk-U3MKSPRO.mjs +0 -227
  48. package/dist/chunk-WNOIHCCC.mjs +0 -191
  49. package/dist/web/assets/local-BQl2KrE1.js +0 -9
@@ -1,191 +0,0 @@
1
- import { createRequire as __khalaCreateRequire } from 'node:module'; const require = __khalaCreateRequire(import.meta.url);
2
- import {
3
- isHarnessId
4
- } from "./chunk-U3MKSPRO.mjs";
5
-
6
- // src/state.ts
7
- import { constants } from "node:fs";
8
- import * as fs from "node:fs/promises";
9
- import { createHash, randomUUID } from "node:crypto";
10
- import os from "node:os";
11
- import path from "node:path";
12
- var SESSION_ID_PATTERN = /^[A-Za-z0-9][A-Za-z0-9._:-]{0,127}$/;
13
- var StateError = class extends Error {
14
- code;
15
- constructor(code) {
16
- super(code);
17
- this.name = "StateError";
18
- this.code = code;
19
- }
20
- };
21
- var WINDOWS = process.platform === "win32";
22
- async function renameWithRetry(from, to) {
23
- for (let attempt = 0; ; attempt++) {
24
- try {
25
- await fs.rename(from, to);
26
- return;
27
- } catch (error) {
28
- const code = error.code;
29
- if (!WINDOWS || attempt >= 20 || !["EPERM", "EACCES", "EBUSY"].includes(code ?? "")) throw error;
30
- await new Promise((resolve) => setTimeout(resolve, 10 + attempt * 5));
31
- }
32
- }
33
- }
34
- function stateRoot(env = process.env) {
35
- const base = env.XDG_STATE_HOME && path.isAbsolute(env.XDG_STATE_HOME) ? env.XDG_STATE_HOME : path.join(WINDOWS ? os.homedir() : env.HOME ?? os.homedir(), ".local", "state");
36
- return path.join(base, "khala");
37
- }
38
- function filesForDir(dir) {
39
- return { dir, mode: path.join(dir, "mode.json"), join: path.join(dir, "join.json"), session: path.join(dir, "session.json"), inbox: path.join(dir, "inbox.jsonl"), cursor: path.join(dir, "cursor.json"), status: path.join(dir, "status.json") };
40
- }
41
- function stateKey(value) {
42
- return createHash("sha256").update(value).digest("hex").slice(0, 24);
43
- }
44
- function channelsDir(files) {
45
- return path.join(files.dir, "channels");
46
- }
47
- function channelFiles(files, roomId) {
48
- return filesForDir(path.join(channelsDir(files), stateKey(roomId)));
49
- }
50
- function joinFilePath(files, key) {
51
- if (!/^[a-f0-9]{24}$/.test(key)) throw new StateError("storage_failed");
52
- return path.join(files.dir, "joins", `${key}.json`);
53
- }
54
- async function writeJoinFile(files, link, join) {
55
- const file = joinFilePath(files, stateKey(link));
56
- await ensureStateDir(path.dirname(file));
57
- await writeJsonAtomic(file, join);
58
- }
59
- async function readJoinFile(files, link) {
60
- const file = joinFilePath(files, stateKey(link));
61
- await ensureStateDir(path.dirname(file));
62
- return readJson(file);
63
- }
64
- async function removeJoinFile(files, link) {
65
- const file = joinFilePath(files, stateKey(link));
66
- await ensureStateDir(path.dirname(file));
67
- try {
68
- await fs.unlink(file);
69
- } catch (error) {
70
- if (error.code !== "ENOENT") throw new StateError("storage_failed");
71
- }
72
- }
73
- function sessionFiles(harness, sessionId, env) {
74
- if (!isHarnessId(harness) || !SESSION_ID_PATTERN.test(sessionId)) throw new StateError("invalid_session_id");
75
- return filesForDir(path.join(stateRoot(env), harness, sessionId));
76
- }
77
- function resolveStateDir(harness, sessionId, env) {
78
- return sessionFiles(harness, sessionId, env).dir;
79
- }
80
- async function ensureStateDir(dir) {
81
- try {
82
- let sessionDir = dir;
83
- if (path.basename(path.dirname(dir)) === "channels" && /^[a-f0-9]{24}$/.test(path.basename(dir)) && isHarnessId(path.basename(path.dirname(path.dirname(path.dirname(dir))))) && path.basename(path.dirname(path.dirname(path.dirname(path.dirname(dir))))) === "khala") {
84
- sessionDir = path.dirname(path.dirname(dir));
85
- } else if (["channels", "joins"].includes(path.basename(dir)) && isHarnessId(path.basename(path.dirname(path.dirname(dir)))) && path.basename(path.dirname(path.dirname(path.dirname(dir)))) === "khala") {
86
- sessionDir = path.dirname(dir);
87
- }
88
- const directories = [path.dirname(path.dirname(sessionDir)), path.dirname(sessionDir), sessionDir];
89
- const relative = path.relative(sessionDir, dir);
90
- let parent = sessionDir;
91
- for (const component of relative ? relative.split(path.sep) : []) {
92
- parent = path.join(parent, component);
93
- directories.push(parent);
94
- }
95
- for (const directory of directories) {
96
- try {
97
- await fs.mkdir(directory, { recursive: true, mode: 448 });
98
- } catch (error) {
99
- if (error.code !== "EEXIST") throw error;
100
- }
101
- const stat = await fs.lstat(directory);
102
- if (!stat.isDirectory() || stat.isSymbolicLink() || !WINDOWS && (stat.mode & 63) !== 0 || typeof process.getuid === "function" && stat.uid !== process.getuid()) throw new StateError("unsafe_state_dir");
103
- }
104
- } catch (error) {
105
- if (error instanceof StateError) throw error;
106
- throw new StateError("storage_failed");
107
- }
108
- }
109
- async function writeJsonAtomic(file, value) {
110
- const dir = path.dirname(file);
111
- const temporary = path.join(dir, `.${path.basename(file)}-${randomUUID()}.tmp`);
112
- let handle;
113
- try {
114
- handle = await fs.open(temporary, constants.O_CREAT | constants.O_EXCL | constants.O_WRONLY, 384);
115
- await handle.writeFile(JSON.stringify(value) + "\n", "utf8");
116
- await handle.sync();
117
- await handle.close();
118
- handle = void 0;
119
- await renameWithRetry(temporary, file);
120
- if (!WINDOWS) {
121
- const directory = await fs.open(dir, constants.O_RDONLY);
122
- try {
123
- await directory.sync();
124
- } finally {
125
- await directory.close();
126
- }
127
- }
128
- } catch {
129
- throw new StateError("storage_failed");
130
- } finally {
131
- await handle?.close().catch(() => void 0);
132
- await fs.unlink(temporary).catch(() => void 0);
133
- }
134
- }
135
- async function readJson(file) {
136
- let content;
137
- try {
138
- content = await fs.readFile(file, "utf8");
139
- } catch (error) {
140
- if (error.code === "ENOENT") return null;
141
- throw new StateError("storage_failed");
142
- }
143
- try {
144
- return JSON.parse(content);
145
- } catch {
146
- return null;
147
- }
148
- }
149
- function readStatus(files) {
150
- return readJson(files.status);
151
- }
152
- async function writeStateFile(dir, name, value) {
153
- if (!["join.json", "session.json", "cursor.json", "status.json", "mode.json", "rejoin.json", "channel.json", "resume.json"].includes(name)) throw new StateError("storage_failed");
154
- await writeJsonAtomic(path.join(dir, name), value);
155
- }
156
- function readStateFile(dir, name) {
157
- if (!/^[a-z]+\.json$/.test(name)) throw new StateError("storage_failed");
158
- return readJson(path.join(dir, name));
159
- }
160
- async function removeStateFile(dir, name) {
161
- if (!/^[a-z]+\.json$/.test(name) && name !== "inbox.jsonl") throw new StateError("storage_failed");
162
- try {
163
- await fs.unlink(path.join(dir, name));
164
- } catch (error) {
165
- if (error.code !== "ENOENT") throw new StateError("storage_failed");
166
- }
167
- }
168
- var TERMINAL_SESSION_DETAILS = ["left", "removed", "revoked", "unauthorized", "channel_deleted"];
169
-
170
- export {
171
- SESSION_ID_PATTERN,
172
- StateError,
173
- stateRoot,
174
- filesForDir,
175
- stateKey,
176
- channelsDir,
177
- channelFiles,
178
- writeJoinFile,
179
- readJoinFile,
180
- removeJoinFile,
181
- sessionFiles,
182
- resolveStateDir,
183
- ensureStateDir,
184
- writeJsonAtomic,
185
- readJson,
186
- readStatus,
187
- writeStateFile,
188
- readStateFile,
189
- removeStateFile,
190
- TERMINAL_SESSION_DETAILS
191
- };