@morkaai/runtime 0.0.0-stage → 0.2.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.
Files changed (96) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +13 -2
  3. package/bin/morka.cjs +57 -0
  4. package/dist/agent-actions/access.js +44 -0
  5. package/dist/agent-actions/manifest.js +52 -0
  6. package/dist/agent-actions/runner.js +69 -0
  7. package/dist/agents/account.js +42 -0
  8. package/dist/agents/acp-sandbox.js +118 -0
  9. package/dist/agents/activity.js +26 -0
  10. package/dist/agents/attachments.js +129 -0
  11. package/dist/agents/context.js +76 -0
  12. package/dist/agents/contracts.js +2 -0
  13. package/dist/agents/process.js +61 -0
  14. package/dist/agents/providers/acp.js +411 -0
  15. package/dist/agents/providers/claude.js +180 -0
  16. package/dist/agents/providers/codex.js +416 -0
  17. package/dist/agents/registry.js +76 -0
  18. package/dist/agents/sessions.js +331 -0
  19. package/dist/app-runtime/bridge.js +40 -0
  20. package/dist/app-runtime/starter/agents.json +1 -0
  21. package/dist/app-runtime/starter/index.html +11 -0
  22. package/dist/app-runtime/starter/main.js +0 -0
  23. package/dist/app-runtime/starter/package-lock.json +13 -0
  24. package/dist/app-runtime/starter/package.json +9 -0
  25. package/dist/app-runtime/starter/server.mjs +20 -0
  26. package/dist/app-runtime/starter/styles.css +0 -0
  27. package/dist/app-runtime/text-file.js +18 -0
  28. package/dist/branding/Morka.icns +0 -0
  29. package/dist/branding/README.md +24 -0
  30. package/dist/branding/generated-app-icon.png +0 -0
  31. package/dist/branding/morka-icon.png +0 -0
  32. package/dist/branding/morka-logo.png +0 -0
  33. package/dist/electron/main.js +802 -0
  34. package/dist/electron/preload.js +51 -0
  35. package/dist/instances/desktop.js +223 -0
  36. package/dist/instances/distribution.js +240 -0
  37. package/dist/instances/macos.js +62 -0
  38. package/dist/instances/repository.js +183 -0
  39. package/dist/instances/shadow-repository.js +95 -0
  40. package/dist/project/app-proxy.js +325 -0
  41. package/dist/project/bundle-toolchain.js +35 -0
  42. package/dist/project/drafts.js +27 -0
  43. package/dist/project/files.js +176 -0
  44. package/dist/project/live-preview.js +198 -0
  45. package/dist/project/managed-runtime.js +324 -0
  46. package/dist/project/node-lifecycle.js +45 -0
  47. package/dist/project/packages.js +302 -0
  48. package/dist/project/page-state.js +198 -0
  49. package/dist/project/project.js +112 -0
  50. package/dist/project/shadow-effects.js +156 -0
  51. package/dist/project/shadows.js +122 -0
  52. package/dist/project/toolchain.js +216 -0
  53. package/dist/renderer/activity.js +91 -0
  54. package/dist/renderer/app-setup.js +126 -0
  55. package/dist/renderer/chat-tools.css +83 -0
  56. package/dist/renderer/highlight.css +118 -0
  57. package/dist/renderer/icons/attach.png +0 -0
  58. package/dist/renderer/icons/brain-weight.png +0 -0
  59. package/dist/renderer/icons/brain.png +0 -0
  60. package/dist/renderer/icons/chat.png +0 -0
  61. package/dist/renderer/icons/chevron.png +0 -0
  62. package/dist/renderer/icons/clone.png +0 -0
  63. package/dist/renderer/icons/close.png +0 -0
  64. package/dist/renderer/icons/document.png +0 -0
  65. package/dist/renderer/icons/edit.png +0 -0
  66. package/dist/renderer/icons/folder.png +0 -0
  67. package/dist/renderer/icons/import.png +0 -0
  68. package/dist/renderer/icons/open-folder.png +0 -0
  69. package/dist/renderer/icons/play.png +0 -0
  70. package/dist/renderer/icons/plus.png +0 -0
  71. package/dist/renderer/icons/remove.png +0 -0
  72. package/dist/renderer/icons/search.png +0 -0
  73. package/dist/renderer/icons/send.png +0 -0
  74. package/dist/renderer/icons/shadow.png +0 -0
  75. package/dist/renderer/icons/stop.png +0 -0
  76. package/dist/renderer/icons/terminal.png +0 -0
  77. package/dist/renderer/index.html +130 -0
  78. package/dist/renderer/launcher/OFL.txt +93 -0
  79. package/dist/renderer/launcher/cell.js +322 -0
  80. package/dist/renderer/launcher/launcher.css +79 -0
  81. package/dist/renderer/launcher/lexend-caption-bold.ttf +0 -0
  82. package/dist/renderer/launcher.html +23 -0
  83. package/dist/renderer/licenses/dompurify.txt +202 -0
  84. package/dist/renderer/licenses/github-slugger.txt +5 -0
  85. package/dist/renderer/licenses/highlight.js.txt +29 -0
  86. package/dist/renderer/licenses/marked-footnote.txt +21 -0
  87. package/dist/renderer/licenses/marked-gfm-heading-id.txt +21 -0
  88. package/dist/renderer/licenses/marked.txt +44 -0
  89. package/dist/renderer/markdown.css +43 -0
  90. package/dist/renderer/markdown.js +74 -0
  91. package/dist/renderer/renderer.js +18902 -0
  92. package/dist/renderer/styles.css +351 -0
  93. package/dist/skills/morka-agent-action/SKILL.md +39 -0
  94. package/dist/skills/morka-app/SKILL.md +36 -0
  95. package/dist/skills/morka-shadow-authoring/SKILL.md +32 -0
  96. package/package.json +30 -4
@@ -0,0 +1,176 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.agentConfigNames = exports.entryFile = void 0;
4
+ exports.projectPath = projectPath;
5
+ exports.info = info;
6
+ exports.checkedPath = checkedPath;
7
+ exports.snapshot = snapshot;
8
+ exports.digest = digest;
9
+ exports.changes = changes;
10
+ exports.describeChanges = describeChanges;
11
+ exports.applyFiles = applyFiles;
12
+ exports.sourceContext = sourceContext;
13
+ const node_crypto_1 = require("node:crypto");
14
+ const promises_1 = require("node:fs/promises");
15
+ const node_path_1 = require("node:path");
16
+ exports.entryFile = "morka.project.json";
17
+ exports.agentConfigNames = [".codex", ".agents", ".claude", ".hermes", ".pi", ".mcp.json"];
18
+ const ignored = new Set([".git", ...exports.agentConfigNames, ".DS_Store", "node_modules"]);
19
+ function protectedName(name) {
20
+ return ignored.has(name) || name === ".env" || (name.startsWith(".env.") && name !== ".env.example") ||
21
+ name === ".morka-data" || /\.(sqlite3?|db)(-(wal|shm|journal))?$/i.test(name);
22
+ }
23
+ function projectPath(value) {
24
+ if (typeof value !== "string" || value.includes("\\") || /[\x00-\x1f]/.test(value) ||
25
+ value.split("/").some(part => !part || part === "." || part === ".." || protectedName(part))) {
26
+ throw new Error(`Invalid project path: ${String(value)}`);
27
+ }
28
+ return value;
29
+ }
30
+ function info(path) {
31
+ return (0, promises_1.lstat)(path).catch((error) => {
32
+ if (error.code === "ENOENT")
33
+ return undefined;
34
+ throw error;
35
+ });
36
+ }
37
+ async function checkedPath(root, relativePath) {
38
+ if ((await info(root))?.isSymbolicLink())
39
+ throw new Error("The root must be a regular directory.");
40
+ let path = root;
41
+ for (const part of projectPath(relativePath).split("/")) {
42
+ path = (0, node_path_1.join)(path, part);
43
+ if ((await info(path))?.isSymbolicLink())
44
+ throw new Error(`Symbolic links are not allowed: ${relativePath}`);
45
+ }
46
+ return path;
47
+ }
48
+ async function snapshot(root) {
49
+ const files = new Map();
50
+ let bytes = 0;
51
+ async function walk(path, prefix) {
52
+ const entries = await (0, promises_1.readdir)(path, { withFileTypes: true });
53
+ for (const entry of entries) {
54
+ if (protectedName(entry.name) || entry.isSymbolicLink())
55
+ continue;
56
+ const name = projectPath(prefix + entry.name);
57
+ const full = (0, node_path_1.join)(path, entry.name);
58
+ if (entry.isDirectory())
59
+ await walk(full, name + "/");
60
+ if (entry.isFile()) {
61
+ const size = (await (0, promises_1.lstat)(full)).size;
62
+ bytes += size;
63
+ if (files.size >= 4000 || bytes > 96 * 1024 * 1024) {
64
+ throw new Error("This project exceeds the supported draft size or file count.");
65
+ }
66
+ files.set(name, await (0, promises_1.readFile)(full));
67
+ }
68
+ }
69
+ }
70
+ if ((await info(root))?.isDirectory())
71
+ await walk(root, "");
72
+ return files;
73
+ }
74
+ function digest(files) {
75
+ const hash = (0, node_crypto_1.createHash)("sha256");
76
+ for (const [name, bytes] of [...files].sort(([a], [b]) => a < b ? -1 : a === b ? 0 : 1)) {
77
+ hash.update(JSON.stringify([name, bytes.length])).update(bytes);
78
+ }
79
+ return hash.digest("hex");
80
+ }
81
+ function changes(before, after) {
82
+ return [...new Set([...before.keys(), ...after.keys()])]
83
+ .filter(name => {
84
+ const old = before.get(name);
85
+ const next = after.get(name);
86
+ return !old || !next || !old.equals(next);
87
+ })
88
+ .sort((left, right) => Number(after.has(left)) - Number(after.has(right)));
89
+ }
90
+ function describeChanges(before, after) {
91
+ return changes(before, after).slice(0, 30).map(name => {
92
+ const old = before.get(name);
93
+ const next = after.get(name);
94
+ if (!old)
95
+ return `+ ${name}`;
96
+ if (!next)
97
+ return `− ${name}`;
98
+ if (old.includes(0) || next.includes(0))
99
+ return `~ ${name}`;
100
+ const previous = old.toString().split("\n");
101
+ const current = next.toString().split("\n");
102
+ let begin = 0;
103
+ while (begin < previous.length && begin < current.length && previous[begin] === current[begin])
104
+ begin++;
105
+ let oldEnd = previous.length;
106
+ let nextEnd = current.length;
107
+ while (oldEnd > begin && nextEnd > begin && previous[oldEnd - 1] === current[nextEnd - 1]) {
108
+ oldEnd--;
109
+ nextEnd--;
110
+ }
111
+ const removed = previous.slice(begin, Math.min(oldEnd, begin + 4)).map(line => "− " + line.slice(0, 180));
112
+ const added = current.slice(begin, Math.min(nextEnd, begin + 4)).map(line => "+ " + line.slice(0, 180));
113
+ return [`~ ${name} · ${begin + 1}`, ...removed, ...added].join("\n");
114
+ }).join("\n\n").slice(0, 5000);
115
+ }
116
+ async function write(root, name, data) {
117
+ const path = await checkedPath(root, name);
118
+ if (data === undefined) {
119
+ await (0, promises_1.rm)(path, { force: true });
120
+ return;
121
+ }
122
+ const temporary = `${path}.${(0, node_crypto_1.randomUUID)()}.tmp`;
123
+ await (0, promises_1.mkdir)((0, node_path_1.dirname)(path), { recursive: true });
124
+ const previous = await info(path);
125
+ try {
126
+ await (0, promises_1.writeFile)(temporary, data, { flag: "wx", mode: previous?.mode ?? 0o600, flush: true });
127
+ await (0, promises_1.rename)(temporary, path);
128
+ }
129
+ finally {
130
+ await (0, promises_1.rm)(temporary, { force: true });
131
+ }
132
+ }
133
+ async function applyFiles(root, before, after, signal) {
134
+ const paths = changes(before, after);
135
+ for (const name of paths) {
136
+ const path = await checkedPath(root, name);
137
+ const live = await (0, promises_1.readFile)(path).catch((error) => {
138
+ if (error.code !== "ENOENT")
139
+ throw error;
140
+ return undefined;
141
+ });
142
+ const expected = before.get(name);
143
+ if (live ? !expected?.equals(live) : expected !== undefined) {
144
+ throw new Error(`Project file changed during the turn: ${name}`);
145
+ }
146
+ }
147
+ signal?.throwIfAborted();
148
+ const written = [];
149
+ try {
150
+ for (const name of paths) {
151
+ await write(root, name, after.get(name));
152
+ written.push(name);
153
+ }
154
+ }
155
+ catch (error) {
156
+ for (const name of written.reverse())
157
+ await write(root, name, before.get(name)).catch(console.error);
158
+ throw error;
159
+ }
160
+ return paths;
161
+ }
162
+ function sourceContext(files, preferred) {
163
+ const output = Object.create(null);
164
+ let left = 150_000;
165
+ for (const name of new Set([exports.entryFile, ...preferred, ...files.keys()])) {
166
+ const bytes = files.get(name);
167
+ if (!bytes || bytes.includes(0) || bytes.length > left)
168
+ continue;
169
+ const text = bytes.toString("utf8");
170
+ if (text.includes("\ufffd"))
171
+ continue;
172
+ output[name] = text;
173
+ left -= bytes.length;
174
+ }
175
+ return output;
176
+ }
@@ -0,0 +1,198 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LivePreview = void 0;
4
+ exports.preservedUrl = preservedUrl;
5
+ const page_state_1 = require("./page-state");
6
+ function preservedUrl(next, current, previous) {
7
+ const target = new URL(next);
8
+ const location = new URL(current);
9
+ const before = new URL(previous);
10
+ if (location.origin === before.origin) {
11
+ return new URL(location.pathname + location.search + location.hash, target.origin).href;
12
+ }
13
+ target.search = location.search;
14
+ target.hash = location.hash;
15
+ return target.href;
16
+ }
17
+ class LivePreview {
18
+ contents;
19
+ onError;
20
+ root;
21
+ saved;
22
+ prepared = false;
23
+ confirming = false;
24
+ restoring = new Set();
25
+ queue = Promise.resolve();
26
+ navigation;
27
+ constructor(contents, onError, initial) {
28
+ this.contents = contents;
29
+ this.onError = onError;
30
+ this.root = initial;
31
+ contents.on("did-frame-finish-load", (_event, isMainFrame, processId, routingId) => {
32
+ const frame = this.frame();
33
+ if (isMainFrame || !frame || frame.processId !== processId || frame.routingId !== routingId)
34
+ return;
35
+ const navigation = this.navigation;
36
+ // An HTTP error can finish loading while the rollback navigation is
37
+ // already queued. Only the rollback's committed document can confirm it.
38
+ if (this.confirming && navigation?.frame !== frame)
39
+ return;
40
+ void this.loaded(frame);
41
+ });
42
+ contents.on("did-fail-load", (_event, code, detail, _url, isMainFrame, processId, routingId) => {
43
+ if (code !== -3 && this.isPreviewFrame(isMainFrame, processId, routingId))
44
+ this.navigation?.finish(Error(detail));
45
+ });
46
+ contents.on("did-frame-navigate", (_event, _url, code, detail, isMainFrame, processId, routingId) => {
47
+ if (!this.navigation || !this.isPreviewFrame(isMainFrame, processId, routingId))
48
+ return;
49
+ if (code >= 400)
50
+ this.navigation.finish(Error(`The app page returned HTTP ${code} ${detail}.`));
51
+ else
52
+ this.navigation.frame = this.frame(true);
53
+ });
54
+ }
55
+ frame(includeBlank = false) {
56
+ if (this.contents.isDestroyed())
57
+ return;
58
+ const frames = this.contents.mainFrame.frames ?? [];
59
+ const available = frames.filter(frame => includeBlank || frame.url !== "about:blank");
60
+ return available.find(frame => frame.name === "morka-preview") ?? available[0];
61
+ }
62
+ isPreviewFrame(isMainFrame, processId, routingId) {
63
+ const frame = this.frame(true);
64
+ return !isMainFrame && frame?.processId === processId && frame?.routingId === routingId;
65
+ }
66
+ isCurrentRuntime(url, runtimeUrl) {
67
+ if (typeof url !== "string" || !runtimeUrl)
68
+ return false;
69
+ try {
70
+ const origin = new URL(runtimeUrl).origin;
71
+ return new URL(url).origin === origin && new URL(this.frame()?.url ?? "about:blank").origin === origin;
72
+ }
73
+ catch {
74
+ return false;
75
+ }
76
+ }
77
+ call(frame, command, value) {
78
+ return frame.executeJavaScript(`(${page_state_1.pageState.toString()})(${JSON.stringify(command)},${JSON.stringify(value) ?? "undefined"})`);
79
+ }
80
+ async prepare() {
81
+ if (this.prepared)
82
+ return;
83
+ const frame = this.frame();
84
+ if (!frame)
85
+ return;
86
+ let timer;
87
+ const timeout = new Promise((_, reject) => {
88
+ timer = setTimeout(() => reject(Error("The preview did not finish saving its state.")), 35_000);
89
+ });
90
+ try {
91
+ this.saved = await Promise.race([this.call(frame, "capture"), timeout]);
92
+ }
93
+ finally {
94
+ clearTimeout(timer);
95
+ }
96
+ this.prepared = true;
97
+ this.contents.send("canvas:updating", true);
98
+ }
99
+ async resume() {
100
+ const frame = this.frame();
101
+ if (frame)
102
+ await this.call(frame, "resume").catch(() => { });
103
+ this.prepared = false;
104
+ this.confirming = false;
105
+ if (!this.contents.isDestroyed())
106
+ this.contents.send("canvas:updating", false);
107
+ }
108
+ async completeUpdate() {
109
+ this.saved = undefined;
110
+ await this.resume();
111
+ }
112
+ cancelUpdate() { this.navigation?.finish(Error("The app is closing.")); }
113
+ show(url, styles, options = {}) {
114
+ const update = this.queue.then(async () => {
115
+ if (this.contents.isDestroyed())
116
+ return;
117
+ this.confirming = Boolean(options.confirm);
118
+ const frame = this.frame();
119
+ if (!url) {
120
+ // Keep the current page and its state visible while its service restarts.
121
+ if (!frame || options.clear) {
122
+ this.root = undefined;
123
+ this.contents.send("canvas:update", undefined);
124
+ }
125
+ return;
126
+ }
127
+ if (frame && this.root && styles?.length) {
128
+ const files = styles.map(path => {
129
+ const asset = new URL(path, url);
130
+ asset.searchParams.set("morka-update", String(Date.now()));
131
+ return { path, url: asset.href };
132
+ });
133
+ await this.contents.session.clearCache();
134
+ if (await this.call(frame, "styles", files)) {
135
+ this.root = url;
136
+ if (!options.confirm)
137
+ await this.completeUpdate();
138
+ return;
139
+ }
140
+ }
141
+ if (frame && this.root && !options.recover)
142
+ await this.prepare();
143
+ const destination = this.saved?.url && this.root ? preservedUrl(url, this.saved.url, this.root) : url;
144
+ this.root = url;
145
+ const navigation = (frame || options.confirm && this.frame(true)) ? new Promise((resolve, reject) => {
146
+ const timer = setTimeout(() => { this.navigation = undefined; reject(Error("The updated page did not finish loading; its saved state is retained.")); }, 30_000);
147
+ this.navigation = { confirm: Boolean(options.confirm),
148
+ finish: error => { clearTimeout(timer); this.navigation = undefined; error ? reject(error) : resolve(); } };
149
+ }) : undefined;
150
+ // Assigning the same URL with a hash can be a same-document navigation.
151
+ // Force a real reload when code changed, retaining the full current route.
152
+ if (frame && destination === frame.url)
153
+ void frame.executeJavaScript("location.reload()").catch(error => this.navigation?.finish(error));
154
+ else
155
+ this.contents.send("canvas:update", destination);
156
+ await navigation;
157
+ }).catch(async (error) => {
158
+ if (options.confirm)
159
+ throw error;
160
+ await this.resume();
161
+ this.onError(error instanceof Error ? error.message : String(error));
162
+ });
163
+ this.queue = update.catch(() => { });
164
+ return update;
165
+ }
166
+ async loaded(frame) {
167
+ if (this.restoring.has(frame))
168
+ return;
169
+ this.restoring.add(frame);
170
+ const navigation = this.navigation;
171
+ try {
172
+ await this.call(frame, "install");
173
+ if (navigation !== this.navigation || frame !== this.frame())
174
+ return;
175
+ if (this.saved)
176
+ await this.call(frame, "restore", this.saved);
177
+ else if (navigation?.confirm)
178
+ await this.call(frame, "ready");
179
+ if (navigation !== this.navigation || frame !== this.frame())
180
+ return;
181
+ if (!navigation?.confirm)
182
+ await this.completeUpdate();
183
+ navigation?.finish();
184
+ }
185
+ catch (error) {
186
+ if (navigation !== this.navigation)
187
+ return;
188
+ if (navigation)
189
+ navigation.finish(error instanceof Error ? error : new Error(String(error)));
190
+ else
191
+ this.onError(error instanceof Error ? error.message : String(error));
192
+ }
193
+ finally {
194
+ this.restoring.delete(frame);
195
+ }
196
+ }
197
+ }
198
+ exports.LivePreview = LivePreview;