@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.
- package/LICENSE +21 -0
- package/README.md +13 -2
- package/bin/morka.cjs +57 -0
- package/dist/agent-actions/access.js +44 -0
- package/dist/agent-actions/manifest.js +52 -0
- package/dist/agent-actions/runner.js +69 -0
- package/dist/agents/account.js +42 -0
- package/dist/agents/acp-sandbox.js +118 -0
- package/dist/agents/activity.js +26 -0
- package/dist/agents/attachments.js +129 -0
- package/dist/agents/context.js +76 -0
- package/dist/agents/contracts.js +2 -0
- package/dist/agents/process.js +61 -0
- package/dist/agents/providers/acp.js +411 -0
- package/dist/agents/providers/claude.js +180 -0
- package/dist/agents/providers/codex.js +416 -0
- package/dist/agents/registry.js +76 -0
- package/dist/agents/sessions.js +331 -0
- package/dist/app-runtime/bridge.js +40 -0
- package/dist/app-runtime/starter/agents.json +1 -0
- package/dist/app-runtime/starter/index.html +11 -0
- package/dist/app-runtime/starter/main.js +0 -0
- package/dist/app-runtime/starter/package-lock.json +13 -0
- package/dist/app-runtime/starter/package.json +9 -0
- package/dist/app-runtime/starter/server.mjs +20 -0
- package/dist/app-runtime/starter/styles.css +0 -0
- package/dist/app-runtime/text-file.js +18 -0
- package/dist/branding/Morka.icns +0 -0
- package/dist/branding/README.md +24 -0
- package/dist/branding/generated-app-icon.png +0 -0
- package/dist/branding/morka-icon.png +0 -0
- package/dist/branding/morka-logo.png +0 -0
- package/dist/electron/main.js +802 -0
- package/dist/electron/preload.js +51 -0
- package/dist/instances/desktop.js +223 -0
- package/dist/instances/distribution.js +240 -0
- package/dist/instances/macos.js +62 -0
- package/dist/instances/repository.js +183 -0
- package/dist/instances/shadow-repository.js +95 -0
- package/dist/project/app-proxy.js +325 -0
- package/dist/project/bundle-toolchain.js +35 -0
- package/dist/project/drafts.js +27 -0
- package/dist/project/files.js +176 -0
- package/dist/project/live-preview.js +198 -0
- package/dist/project/managed-runtime.js +324 -0
- package/dist/project/node-lifecycle.js +45 -0
- package/dist/project/packages.js +302 -0
- package/dist/project/page-state.js +198 -0
- package/dist/project/project.js +112 -0
- package/dist/project/shadow-effects.js +156 -0
- package/dist/project/shadows.js +122 -0
- package/dist/project/toolchain.js +216 -0
- package/dist/renderer/activity.js +91 -0
- package/dist/renderer/app-setup.js +126 -0
- package/dist/renderer/chat-tools.css +83 -0
- package/dist/renderer/highlight.css +118 -0
- package/dist/renderer/icons/attach.png +0 -0
- package/dist/renderer/icons/brain-weight.png +0 -0
- package/dist/renderer/icons/brain.png +0 -0
- package/dist/renderer/icons/chat.png +0 -0
- package/dist/renderer/icons/chevron.png +0 -0
- package/dist/renderer/icons/clone.png +0 -0
- package/dist/renderer/icons/close.png +0 -0
- package/dist/renderer/icons/document.png +0 -0
- package/dist/renderer/icons/edit.png +0 -0
- package/dist/renderer/icons/folder.png +0 -0
- package/dist/renderer/icons/import.png +0 -0
- package/dist/renderer/icons/open-folder.png +0 -0
- package/dist/renderer/icons/play.png +0 -0
- package/dist/renderer/icons/plus.png +0 -0
- package/dist/renderer/icons/remove.png +0 -0
- package/dist/renderer/icons/search.png +0 -0
- package/dist/renderer/icons/send.png +0 -0
- package/dist/renderer/icons/shadow.png +0 -0
- package/dist/renderer/icons/stop.png +0 -0
- package/dist/renderer/icons/terminal.png +0 -0
- package/dist/renderer/index.html +130 -0
- package/dist/renderer/launcher/OFL.txt +93 -0
- package/dist/renderer/launcher/cell.js +322 -0
- package/dist/renderer/launcher/launcher.css +79 -0
- package/dist/renderer/launcher/lexend-caption-bold.ttf +0 -0
- package/dist/renderer/launcher.html +23 -0
- package/dist/renderer/licenses/dompurify.txt +202 -0
- package/dist/renderer/licenses/github-slugger.txt +5 -0
- package/dist/renderer/licenses/highlight.js.txt +29 -0
- package/dist/renderer/licenses/marked-footnote.txt +21 -0
- package/dist/renderer/licenses/marked-gfm-heading-id.txt +21 -0
- package/dist/renderer/licenses/marked.txt +44 -0
- package/dist/renderer/markdown.css +43 -0
- package/dist/renderer/markdown.js +74 -0
- package/dist/renderer/renderer.js +18902 -0
- package/dist/renderer/styles.css +351 -0
- package/dist/skills/morka-agent-action/SKILL.md +39 -0
- package/dist/skills/morka-app/SKILL.md +36 -0
- package/dist/skills/morka-shadow-authoring/SKILL.md +32 -0
- package/package.json +30 -4
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.shadowStorage = void 0;
|
|
4
|
+
exports.isShadowStorage = isShadowStorage;
|
|
5
|
+
exports.editableAppFiles = editableAppFiles;
|
|
6
|
+
exports.saveShadowInstallation = saveShadowInstallation;
|
|
7
|
+
exports.installedShadowDescription = installedShadowDescription;
|
|
8
|
+
exports.planShadowRemoval = planShadowRemoval;
|
|
9
|
+
const node_path_1 = require("node:path");
|
|
10
|
+
const files_1 = require("./files");
|
|
11
|
+
const project_1 = require("./project");
|
|
12
|
+
exports.shadowStorage = ".morka-shadows/";
|
|
13
|
+
function isShadowStorage(path) { return path.startsWith(exports.shadowStorage); }
|
|
14
|
+
function editableAppFiles(files) {
|
|
15
|
+
return new Map([...files].filter(([path]) => !isShadowStorage(path)));
|
|
16
|
+
}
|
|
17
|
+
function receipt(files, id) {
|
|
18
|
+
const bytes = files.get(`${exports.shadowStorage}${id}/effects.json`);
|
|
19
|
+
if (!bytes || bytes.length > 1_500_000)
|
|
20
|
+
return;
|
|
21
|
+
try {
|
|
22
|
+
const value = (0, project_1.object)(JSON.parse(bytes.toString()));
|
|
23
|
+
if (!Array.isArray(value.effects) || !Array.isArray(value.uncertain))
|
|
24
|
+
return;
|
|
25
|
+
const effects = value.effects.map(item => {
|
|
26
|
+
const { path, before, after } = (0, project_1.object)(item);
|
|
27
|
+
const name = (0, files_1.projectPath)(path);
|
|
28
|
+
if (name === files_1.entryFile || isShadowStorage(name) ||
|
|
29
|
+
[before, after].some(text => text !== null && typeof text !== "string"))
|
|
30
|
+
throw new Error("Invalid effect.");
|
|
31
|
+
return { path: name, before, after };
|
|
32
|
+
});
|
|
33
|
+
return { effects, uncertain: value.uncertain.map(files_1.projectPath) };
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return undefined;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function text(bytes) {
|
|
40
|
+
if (!bytes)
|
|
41
|
+
return null;
|
|
42
|
+
if (bytes.length > 80_000 || bytes.includes(0))
|
|
43
|
+
return;
|
|
44
|
+
const value = bytes.toString();
|
|
45
|
+
return Buffer.from(value).equals(bytes) ? value : undefined;
|
|
46
|
+
}
|
|
47
|
+
function saveShadowInstallation(current, updated, shadow, previous) {
|
|
48
|
+
const prefix = `${exports.shadowStorage}${shadow.id}/`;
|
|
49
|
+
const old = receipt(previous, shadow.id);
|
|
50
|
+
const effects = new Map(old?.effects.map(effect => [effect.path, effect]));
|
|
51
|
+
const uncertain = new Set(old?.uncertain);
|
|
52
|
+
let size = old ? JSON.stringify(old).length : 0;
|
|
53
|
+
for (const path of new Set([...current.keys(), ...updated.keys()])) {
|
|
54
|
+
if (isShadowStorage(path))
|
|
55
|
+
continue;
|
|
56
|
+
const before = current.get(path);
|
|
57
|
+
const after = updated.get(path);
|
|
58
|
+
if (before && after && before.equals(after))
|
|
59
|
+
continue;
|
|
60
|
+
if (path === files_1.entryFile) {
|
|
61
|
+
const withoutShadows = (value) => {
|
|
62
|
+
const manifest = (0, project_1.object)(JSON.parse(value.toString()));
|
|
63
|
+
delete manifest.shadows;
|
|
64
|
+
return JSON.stringify(manifest);
|
|
65
|
+
};
|
|
66
|
+
if (withoutShadows(before) !== withoutShadows(after))
|
|
67
|
+
uncertain.add(path);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const from = text(before);
|
|
71
|
+
const to = text(after);
|
|
72
|
+
const previousEffect = effects.get(path);
|
|
73
|
+
if (from === undefined || to === undefined || (previousEffect && previousEffect.after !== from)) {
|
|
74
|
+
uncertain.add(path);
|
|
75
|
+
effects.delete(path);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const effect = { path, before: previousEffect ? previousEffect.before : from, after: to };
|
|
79
|
+
size += JSON.stringify(effect).length;
|
|
80
|
+
if (size > 1_000_000 || uncertain.has(path)) {
|
|
81
|
+
uncertain.add(path);
|
|
82
|
+
effects.delete(path);
|
|
83
|
+
}
|
|
84
|
+
else if (effect.before === effect.after)
|
|
85
|
+
effects.delete(path);
|
|
86
|
+
else
|
|
87
|
+
effects.set(path, effect);
|
|
88
|
+
}
|
|
89
|
+
for (const name of updated.keys())
|
|
90
|
+
if (name.startsWith(prefix))
|
|
91
|
+
updated.delete(name);
|
|
92
|
+
for (const [name, bytes] of shadow.files)
|
|
93
|
+
updated.set(prefix + "package/" + name, bytes);
|
|
94
|
+
updated.set(prefix + "effects.json", (0, project_1.jsonFile)({ effects: [...effects.values()], uncertain: [...uncertain] }));
|
|
95
|
+
}
|
|
96
|
+
function installedShadowDescription(files, id) {
|
|
97
|
+
const bytes = files.get(`${exports.shadowStorage}${id}/package/manifest.json`);
|
|
98
|
+
if (!bytes)
|
|
99
|
+
return;
|
|
100
|
+
try {
|
|
101
|
+
const description = (0, project_1.object)(JSON.parse(bytes.toString())).description;
|
|
102
|
+
return typeof description === "string" ? description : undefined;
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return undefined;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function planShadowRemoval(current, id) {
|
|
109
|
+
const manifest = (0, project_1.readManifest)(current);
|
|
110
|
+
const ref = manifest.shadows?.find(shadow => shadow.id === id);
|
|
111
|
+
if (!ref)
|
|
112
|
+
throw new Error("This Shadow is no longer installed.");
|
|
113
|
+
const only = new Map(current);
|
|
114
|
+
const raw = (0, project_1.object)(JSON.parse(current.get(files_1.entryFile).toString()));
|
|
115
|
+
raw.shadows = manifest.shadows.filter(shadow => shadow.id !== id);
|
|
116
|
+
only.set(files_1.entryFile, (0, project_1.jsonFile)(raw));
|
|
117
|
+
const prefix = `${exports.shadowStorage}${id}/`;
|
|
118
|
+
for (const name of only.keys())
|
|
119
|
+
if (name.startsWith(prefix))
|
|
120
|
+
only.delete(name);
|
|
121
|
+
const clean = new Map(only);
|
|
122
|
+
const stored = receipt(current, id);
|
|
123
|
+
const skipped = new Set(stored?.uncertain);
|
|
124
|
+
const changes = new Map();
|
|
125
|
+
const ownedInstructions = new Set((0, project_1.instructionPaths)((0, project_1.readManifest)(only)));
|
|
126
|
+
const deletions = [];
|
|
127
|
+
for (const effect of stored?.effects ?? []) {
|
|
128
|
+
if (text(current.get(effect.path)) !== effect.after || ownedInstructions.has(effect.path) ||
|
|
129
|
+
(effect.path.startsWith("assets/") && effect.before === null)) {
|
|
130
|
+
skipped.add(effect.path);
|
|
131
|
+
continue;
|
|
132
|
+
}
|
|
133
|
+
if (effect.before === null)
|
|
134
|
+
deletions.push(effect.path);
|
|
135
|
+
else {
|
|
136
|
+
clean.set(effect.path, Buffer.from(effect.before));
|
|
137
|
+
changes.set(effect.path, "Restore its pre-install contents");
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
for (const path of deletions) {
|
|
141
|
+
const name = (0, node_path_1.basename)(path);
|
|
142
|
+
const used = [...clean].some(([other, bytes]) => other !== path && !isShadowStorage(other) &&
|
|
143
|
+
(bytes.includes(path) || (name !== "SKILL.md" && bytes.includes(name))));
|
|
144
|
+
if (used)
|
|
145
|
+
skipped.add(path);
|
|
146
|
+
else {
|
|
147
|
+
clean.delete(path);
|
|
148
|
+
changes.set(path, "Remove an installed file");
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
only, clean,
|
|
153
|
+
summary: { id: ref.id, version: ref.version,
|
|
154
|
+
changes: [...changes].map(([path, reason]) => ({ path, reason })), skipped: [...skipped] },
|
|
155
|
+
};
|
|
156
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateShadow = validateShadow;
|
|
4
|
+
exports.readShadow = readShadow;
|
|
5
|
+
exports.importShadow = importShadow;
|
|
6
|
+
exports.stageShadow = stageShadow;
|
|
7
|
+
exports.shadowContext = shadowContext;
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const promises_1 = require("node:fs/promises");
|
|
10
|
+
const manifest_1 = require("../agent-actions/manifest");
|
|
11
|
+
const files_1 = require("./files");
|
|
12
|
+
const project_1 = require("./project");
|
|
13
|
+
function parseShadow(manifest) {
|
|
14
|
+
if (!manifest?.length || manifest.length > 32 * 1024)
|
|
15
|
+
throw new Error("The Shadow package manifest.json is invalid.");
|
|
16
|
+
const { id, version, description, skills, rules, assets = [], examples = [] } = (0, project_1.object)(JSON.parse(manifest.toString()));
|
|
17
|
+
if (typeof id !== "string" || !manifest_1.actionName.test(id) || typeof version !== "string" ||
|
|
18
|
+
!/^[0-9][a-zA-Z0-9.-]{0,47}$/.test(version) || typeof description !== "string" ||
|
|
19
|
+
!description.trim() || description.length > 1000)
|
|
20
|
+
throw new Error("The Shadow package ID, version, or description is invalid.");
|
|
21
|
+
const [skillPath, rulePath] = (0, project_1.pathList)([skills, rules]);
|
|
22
|
+
if (!rulePath || [skillPath, rulePath].includes("manifest.json"))
|
|
23
|
+
throw new Error("The Shadow skill and rule must use separate files.");
|
|
24
|
+
const assetPaths = (0, project_1.pathList)(assets);
|
|
25
|
+
const examplePaths = (0, project_1.pathList)(examples);
|
|
26
|
+
return { id, version, description, skills: skillPath, rules: rulePath, assets: assetPaths, examples: examplePaths };
|
|
27
|
+
}
|
|
28
|
+
function shadowPaths(spec) {
|
|
29
|
+
return new Set([spec.skills, spec.rules, ...spec.assets, ...spec.examples]);
|
|
30
|
+
}
|
|
31
|
+
function validateShadow(files) {
|
|
32
|
+
const manifest = files.get("manifest.json");
|
|
33
|
+
const spec = parseShadow(manifest);
|
|
34
|
+
const selected = new Map([["manifest.json", manifest]]);
|
|
35
|
+
let total = manifest.length;
|
|
36
|
+
for (const name of shadowPaths(spec)) {
|
|
37
|
+
const bytes = files.get(name);
|
|
38
|
+
if (!bytes?.length)
|
|
39
|
+
throw new Error(`Package file not found: ${name}`);
|
|
40
|
+
total += bytes.length;
|
|
41
|
+
const textual = [spec.skills, spec.rules, ...spec.examples].includes(name);
|
|
42
|
+
if (total > 32 * 1024 * 1024 || (textual && bytes.length > 32 * 1024))
|
|
43
|
+
throw new Error(`Package size limit exceeded: ${name}`);
|
|
44
|
+
if (textual && (bytes.includes(0) || !bytes.toString().trim())) {
|
|
45
|
+
throw new Error(`Invalid Shadow text file: ${name}`);
|
|
46
|
+
}
|
|
47
|
+
selected.set(name, bytes);
|
|
48
|
+
}
|
|
49
|
+
return { ...spec, digest: (0, files_1.digest)(selected), files: selected };
|
|
50
|
+
}
|
|
51
|
+
async function readShadow(directory) {
|
|
52
|
+
if (!(await (0, files_1.info)(directory))?.isDirectory())
|
|
53
|
+
throw new Error("Invalid Shadow package folder.");
|
|
54
|
+
const manifest = await (0, files_1.checkedPath)(directory, "manifest.json");
|
|
55
|
+
const file = await (0, files_1.info)(manifest);
|
|
56
|
+
if (!file?.isFile() || file.size > 32 * 1024)
|
|
57
|
+
throw new Error("The Shadow package manifest.json is invalid.");
|
|
58
|
+
const bytes = await (0, promises_1.readFile)(manifest);
|
|
59
|
+
const files = new Map([["manifest.json", bytes]]);
|
|
60
|
+
let total = bytes.length;
|
|
61
|
+
for (const name of shadowPaths(parseShadow(bytes))) {
|
|
62
|
+
const path = await (0, files_1.checkedPath)(directory, name);
|
|
63
|
+
const entry = await (0, files_1.info)(path);
|
|
64
|
+
if (!entry?.isFile() || !entry.size)
|
|
65
|
+
throw new Error(`Package file not found: ${name}`);
|
|
66
|
+
total += entry.size;
|
|
67
|
+
if (total > 32 * 1024 * 1024)
|
|
68
|
+
throw new Error(`Package size limit exceeded: ${name}`);
|
|
69
|
+
files.set(name, await (0, promises_1.readFile)(path));
|
|
70
|
+
}
|
|
71
|
+
return validateShadow(files);
|
|
72
|
+
}
|
|
73
|
+
async function importShadow(directory, library) {
|
|
74
|
+
const content = (0, node_path_1.join)(directory, "shadow");
|
|
75
|
+
const shadow = await readShadow(await (0, files_1.info)((0, node_path_1.join)(content, "manifest.json")) ? content : directory);
|
|
76
|
+
const location = (0, node_path_1.join)(library, shadow.id, shadow.version, shadow.digest);
|
|
77
|
+
const existing = await (0, files_1.snapshot)(location);
|
|
78
|
+
if (existing.size && (0, files_1.digest)(existing) !== shadow.digest)
|
|
79
|
+
throw new Error("The installed Shadow package data is invalid.");
|
|
80
|
+
if (!existing.size)
|
|
81
|
+
await (0, files_1.applyFiles)(location, existing, shadow.files);
|
|
82
|
+
return shadow;
|
|
83
|
+
}
|
|
84
|
+
function stageShadow(shadow, draft) {
|
|
85
|
+
const manifest = (0, project_1.readManifest)(draft);
|
|
86
|
+
const previous = manifest.shadows?.find(item => item.id === shadow.id);
|
|
87
|
+
const fingerprint = shadow.digest.slice(0, 12);
|
|
88
|
+
const availablePath = (name, alternate, source) => {
|
|
89
|
+
const contents = draft.get(name);
|
|
90
|
+
return contents && !contents.equals(shadow.files.get(source)) ? alternate : name;
|
|
91
|
+
};
|
|
92
|
+
const ref = {
|
|
93
|
+
id: shadow.id, version: shadow.version, digest: shadow.digest,
|
|
94
|
+
skill: previous?.skill ?? availablePath(`skills/shadows/${shadow.id}/SKILL.md`, `skills/shadows/${shadow.id}/${fingerprint}/SKILL.md`, shadow.skills),
|
|
95
|
+
rule: previous?.rule ?? availablePath(`rules/shadows/${shadow.id}.md`, `rules/shadows/${shadow.id}-${fingerprint}.md`, shadow.rules),
|
|
96
|
+
};
|
|
97
|
+
function install(name, bytes) {
|
|
98
|
+
if (draft.has(name) && !draft.get(name)?.equals(bytes)) {
|
|
99
|
+
throw new Error(`Resolve this file conflict before installing Shadow: ${name}`);
|
|
100
|
+
}
|
|
101
|
+
draft.set(name, bytes);
|
|
102
|
+
}
|
|
103
|
+
// Preserve the installed project version of a rule or skill during an update.
|
|
104
|
+
if (!previous) {
|
|
105
|
+
install(ref.skill, shadow.files.get(shadow.skills));
|
|
106
|
+
install(ref.rule, shadow.files.get(shadow.rules));
|
|
107
|
+
}
|
|
108
|
+
for (const name of shadow.assets)
|
|
109
|
+
install(`assets/shadows/${shadow.id}/${shadow.version}/${name}`, shadow.files.get(name));
|
|
110
|
+
const raw = (0, project_1.object)(JSON.parse(draft.get(files_1.entryFile).toString()));
|
|
111
|
+
raw.shadows = [...manifest.shadows?.filter(item => item.id !== shadow.id) ?? [], ref];
|
|
112
|
+
draft.set(files_1.entryFile, (0, project_1.jsonFile)(raw));
|
|
113
|
+
return ref;
|
|
114
|
+
}
|
|
115
|
+
function shadowContext(shadow) {
|
|
116
|
+
return JSON.stringify({
|
|
117
|
+
id: shadow.id, version: shadow.version, description: shadow.description,
|
|
118
|
+
skill: shadow.files.get(shadow.skills).toString(), rule: shadow.files.get(shadow.rules).toString(),
|
|
119
|
+
assets: shadow.assets.map(name => `assets/shadows/${shadow.id}/${shadow.version}/${name}`),
|
|
120
|
+
examples: Object.fromEntries(shadow.examples.map(name => [name, shadow.files.get(name).toString()])),
|
|
121
|
+
});
|
|
122
|
+
}
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.npmEntry = npmEntry;
|
|
4
|
+
exports.bundledToolchain = bundledToolchain;
|
|
5
|
+
exports.runtimeEnvironment = runtimeEnvironment;
|
|
6
|
+
exports.packageProcess = packageProcess;
|
|
7
|
+
exports.killProcess = killProcess;
|
|
8
|
+
exports.waitForProcess = waitForProcess;
|
|
9
|
+
const node_child_process_1 = require("node:child_process");
|
|
10
|
+
const promises_1 = require("node:fs/promises");
|
|
11
|
+
const node_path_1 = require("node:path");
|
|
12
|
+
const node_util_1 = require("node:util");
|
|
13
|
+
let developerDirectory;
|
|
14
|
+
function developerTools() {
|
|
15
|
+
developerDirectory ??= (0, node_util_1.promisify)(node_child_process_1.execFile)("/usr/bin/xcode-select", ["-p"]).then(async (result) => {
|
|
16
|
+
const path = await (0, promises_1.realpath)(result.stdout.trim());
|
|
17
|
+
return path.startsWith("/Library/Developer/") || path.endsWith(".app/Contents/Developer") ? path : undefined;
|
|
18
|
+
}).catch(() => undefined);
|
|
19
|
+
return developerDirectory;
|
|
20
|
+
}
|
|
21
|
+
async function npmEntry(node, configured) {
|
|
22
|
+
const bin = (0, node_path_1.dirname)(await (0, promises_1.realpath)(node));
|
|
23
|
+
const candidates = [
|
|
24
|
+
configured,
|
|
25
|
+
(0, node_path_1.join)(bin, "node_modules", "npm", "bin", "npm-cli.js"),
|
|
26
|
+
(0, node_path_1.join)(bin, "..", "lib", "node_modules", "npm", "bin", "npm-cli.js"),
|
|
27
|
+
(0, node_path_1.join)(bin, "..", "share", "nodejs", "npm", "bin", "npm-cli.js"),
|
|
28
|
+
];
|
|
29
|
+
for (const candidate of candidates) {
|
|
30
|
+
if (!candidate || !(0, node_path_1.isAbsolute)(candidate) || (0, node_path_1.basename)(candidate).toLowerCase() !== "npm-cli.js")
|
|
31
|
+
continue;
|
|
32
|
+
try {
|
|
33
|
+
const npm = await (0, promises_1.realpath)(candidate);
|
|
34
|
+
const metadata = JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(npm, "..", "..", "package.json"), "utf8"));
|
|
35
|
+
if (metadata.name === "npm")
|
|
36
|
+
return npm;
|
|
37
|
+
}
|
|
38
|
+
catch { /* Try the next installation layout. */ }
|
|
39
|
+
}
|
|
40
|
+
throw new Error("Morka needs Node.js 24 or newer and npm. Run this project with npm start.");
|
|
41
|
+
}
|
|
42
|
+
async function bundledToolchain(root) {
|
|
43
|
+
if (!root && process.env.MORKA_LOCAL_NODE && process.env.MORKA_LOCAL_NPM) {
|
|
44
|
+
const node = await (0, promises_1.realpath)(process.env.MORKA_LOCAL_NODE);
|
|
45
|
+
const npm = await npmEntry(node, process.env.MORKA_LOCAL_NPM);
|
|
46
|
+
const { stdout } = await (0, node_util_1.promisify)(node_child_process_1.execFile)(node, ["-p",
|
|
47
|
+
"JSON.stringify({node:process.version,platform:process.platform,arch:process.arch,abi:process.versions.modules})"], { timeout: 5000, env: { ...process.env, NODE_OPTIONS: "" }, windowsHide: true });
|
|
48
|
+
const info = JSON.parse(stdout);
|
|
49
|
+
if (info.platform !== process.platform || info.arch !== process.arch || Number(info.node.split(".")[0].slice(1)) < 24) {
|
|
50
|
+
throw new Error("Use Node.js 24 or newer for this platform and architecture.");
|
|
51
|
+
}
|
|
52
|
+
const npmPackage = JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(npm, "..", "..", "package.json"), "utf8"));
|
|
53
|
+
const identity = JSON.stringify({ version: 1, node: info.node, npm: npmPackage.version,
|
|
54
|
+
platform: info.platform, arch: info.arch, abi: info.abi });
|
|
55
|
+
const nodeRoot = (0, node_path_1.dirname)((0, node_path_1.dirname)(node));
|
|
56
|
+
const headers = await (0, promises_1.realpath)((0, node_path_1.join)(nodeRoot, "include", "node", "node.h")).then(() => nodeRoot, () => undefined);
|
|
57
|
+
return { root: (0, node_path_1.dirname)(node), node, npm, identity, platform: info.platform, arch: info.arch, system: true, headers };
|
|
58
|
+
}
|
|
59
|
+
root ??= (0, node_path_1.join)(__dirname, "../toolchain");
|
|
60
|
+
const metadata = JSON.parse(await (0, promises_1.readFile)((0, node_path_1.join)(root, "toolchain.json"), "utf8"));
|
|
61
|
+
if (metadata.version !== 1 || metadata.platform !== process.platform || metadata.arch !== process.arch) {
|
|
62
|
+
throw new Error("The bundled Node runtime does not match this computer's platform and architecture.");
|
|
63
|
+
}
|
|
64
|
+
root = await (0, promises_1.realpath)(root);
|
|
65
|
+
return { root, node: (0, node_path_1.join)(root, "bin", "node"), npm: (0, node_path_1.join)(root, "npm", "bin", "npm-cli.js"), headers: root,
|
|
66
|
+
identity: JSON.stringify(metadata), platform: metadata.platform, arch: metadata.arch };
|
|
67
|
+
}
|
|
68
|
+
function runtimeEnvironment(toolchain, home, extra = {}) {
|
|
69
|
+
const windows = process.platform === "win32";
|
|
70
|
+
const temp = (0, node_path_1.join)(home, "tmp");
|
|
71
|
+
const systemRoot = process.env.SystemRoot || "C:\\Windows";
|
|
72
|
+
const systemPath = windows
|
|
73
|
+
? [(0, node_path_1.join)(systemRoot, "System32"), systemRoot, process.env.ProgramFiles && (0, node_path_1.join)(process.env.ProgramFiles, "Git", "cmd")]
|
|
74
|
+
: ["/usr/bin", "/bin", "/usr/sbin", "/sbin", "/opt/homebrew/bin", "/usr/local/bin"];
|
|
75
|
+
return {
|
|
76
|
+
LANG: process.env.LANG || "en_US.UTF-8",
|
|
77
|
+
PATH: [(0, node_path_1.dirname)(toolchain.node), ...systemPath].filter(Boolean).join(node_path_1.delimiter),
|
|
78
|
+
...extra,
|
|
79
|
+
HOME: home, TMPDIR: temp,
|
|
80
|
+
...(windows ? { SystemRoot: systemRoot, ComSpec: (0, node_path_1.join)(systemRoot, "System32", "cmd.exe"), USERPROFILE: home,
|
|
81
|
+
APPDATA: home, LOCALAPPDATA: home, TEMP: temp, TMP: temp, PATHEXT: ".COM;.EXE;.BAT;.CMD" } : {}),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
// Package lifecycle scripts can build native addons and use the network, but
|
|
85
|
+
// cannot read other projects, user data, the host HOME or agent credentials.
|
|
86
|
+
async function packageProcess(toolchain, args, directory, profile, cache, options = {}) {
|
|
87
|
+
const mac = process.platform === "darwin";
|
|
88
|
+
const buildHome = (0, node_path_1.join)((0, node_path_1.dirname)(directory), "home");
|
|
89
|
+
await (0, promises_1.mkdir)((0, node_path_1.join)(buildHome, "tmp"), { recursive: true, mode: 0o700 });
|
|
90
|
+
await (0, promises_1.mkdir)(profile, { recursive: true, mode: 0o700 });
|
|
91
|
+
await (0, promises_1.mkdir)(cache, { recursive: true, mode: 0o700 });
|
|
92
|
+
const roots = await Promise.all([(0, node_path_1.dirname)(directory), profile, cache].map(path => (0, promises_1.realpath)(path)));
|
|
93
|
+
const toolchainPaths = toolchain.system ? [(0, node_path_1.join)(toolchain.npm, "..", "..")] : [toolchain.root];
|
|
94
|
+
if (toolchain.system && toolchain.headers)
|
|
95
|
+
toolchainPaths.push((0, node_path_1.join)(toolchain.headers, "include", "node"));
|
|
96
|
+
const executables = [toolchain.node, (0, node_path_1.join)((0, node_path_1.dirname)(toolchain.node), "npm"), (0, node_path_1.join)((0, node_path_1.dirname)(toolchain.node), "npx")];
|
|
97
|
+
const developer = mac ? await developerTools() : undefined;
|
|
98
|
+
const developerRoot = developer?.endsWith(".app/Contents/Developer") ? (0, node_path_1.dirname)((0, node_path_1.dirname)(developer)) : developer;
|
|
99
|
+
const sdk = developer ? await (0, promises_1.realpath)((0, node_path_1.join)(developer, developer.endsWith("Contents/Developer") ?
|
|
100
|
+
"Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk" : "SDKs/MacOSX.sdk")).catch(() => undefined) : undefined;
|
|
101
|
+
const subpath = (path) => `(subpath ${JSON.stringify(path)})`;
|
|
102
|
+
const literal = (path) => `(literal ${JSON.stringify(path)})`;
|
|
103
|
+
const ancestors = new Set(["/", "/var", "/tmp", "/etc", "/var/db", "/private/var/db", "/var/db/xcode_select_link", "/private/var/db/xcode_select_link", "/Library", "/Library/Preferences", "/Library/Preferences/com.apple.dt.Xcode.plist"]);
|
|
104
|
+
for (const root of [...roots, ...toolchainPaths, toolchain.node, ...(developerRoot ? [developerRoot] : []), "/opt/homebrew"]) {
|
|
105
|
+
for (let parent = (0, node_path_1.dirname)(root); parent !== (0, node_path_1.dirname)(parent); parent = (0, node_path_1.dirname)(parent))
|
|
106
|
+
ancestors.add(parent);
|
|
107
|
+
}
|
|
108
|
+
const rules = ["(version 1)", "(allow default)", "(deny appleevent-send)", "(deny file-read*)",
|
|
109
|
+
`(allow file-read* ${[...roots, ...toolchainPaths, ...(developerRoot ? [developerRoot] : []), "/System", "/usr", "/bin", "/sbin", "/Library/Developer", "/Library/Apple", "/private/etc", "/private/var/select", "/dev", "/opt/homebrew/bin", "/opt/homebrew/include", "/opt/homebrew/lib", "/opt/homebrew/opt", "/opt/homebrew/Cellar"].map(subpath).join(" ")} ${executables.map(literal).join(" ")})`,
|
|
110
|
+
`(allow file-read* ${[...ancestors].map(literal).join(" ")})`, "(deny file-write*)",
|
|
111
|
+
`(allow file-write* ${[roots[0], roots[2]].map(subpath).join(" ")} ${literal("/dev/null")})`,
|
|
112
|
+
].join("\n");
|
|
113
|
+
const settings = await (0, promises_1.readFile)((0, node_path_1.join)(profile, options.auth === false ? "build.env" : "npm.env"), "utf8").catch(() => "");
|
|
114
|
+
const configured = (0, node_util_1.parseEnv)(settings);
|
|
115
|
+
const registryConfig = await (0, promises_1.readFile)((0, node_path_1.join)(profile, "npmrc"), "utf8").catch(() => "");
|
|
116
|
+
const secrets = [
|
|
117
|
+
...Object.entries(configured).filter(([key]) => /token|password|secret|auth|key/i.test(key)).map(([, value]) => value),
|
|
118
|
+
...[...registryConfig.matchAll(/(?:_authToken|_auth|_password|password)\s*=\s*([^\r\n]+)/gi)].map(match => match[1].trim()),
|
|
119
|
+
].filter((value) => typeof value === "string" && value.length >= 4);
|
|
120
|
+
const redact = (value) => secrets.reduce((text, secret) => text.split(secret).join("[redacted]"), value);
|
|
121
|
+
const canonicalHome = (0, node_path_1.join)(roots[0], "home");
|
|
122
|
+
const env = runtimeEnvironment(toolchain, canonicalHome, {
|
|
123
|
+
...configured,
|
|
124
|
+
npm_config_cache: roots[2], npm_config_userconfig: (0, node_path_1.join)(roots[1], options.auth === false ? "empty-user-npmrc" : "npmrc"),
|
|
125
|
+
npm_config_globalconfig: (0, node_path_1.join)(roots[1], "empty-global-npmrc"), npm_config_audit: "false", npm_config_fund: "false",
|
|
126
|
+
npm_config_update_notifier: "false", npm_config_engine_strict: "true", NODE_ENV: "development",
|
|
127
|
+
...(toolchain.headers ? { npm_package_config_node_gyp_nodedir: toolchain.headers } : {}),
|
|
128
|
+
npm_config_install_links: "true",
|
|
129
|
+
...(developer ? { DEVELOPER_DIR: developer,
|
|
130
|
+
CC: configured.CC ?? (0, node_path_1.join)(developer, developer.endsWith("Contents/Developer") ? "Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" : "usr/bin/clang"),
|
|
131
|
+
CXX: configured.CXX ?? (0, node_path_1.join)(developer, developer.endsWith("Contents/Developer") ? "Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++" : "usr/bin/clang++"),
|
|
132
|
+
} : {}), xcrun_nocache: "1",
|
|
133
|
+
...(sdk ? { SDKROOT: configured.SDKROOT ?? sdk } : {}),
|
|
134
|
+
});
|
|
135
|
+
let logs = "";
|
|
136
|
+
let pendingLog = "";
|
|
137
|
+
const command = mac ? "/usr/bin/sandbox-exec" : toolchain.node;
|
|
138
|
+
const nodeArgs = [toolchain.npm, ...args];
|
|
139
|
+
const child = (0, node_child_process_1.spawn)(command, mac ? ["-p", rules, toolchain.node, ...nodeArgs] : nodeArgs, {
|
|
140
|
+
cwd: await (0, promises_1.realpath)(directory), env, stdio: ["ignore", "pipe", "pipe"], detached: process.platform !== "win32", windowsHide: true,
|
|
141
|
+
});
|
|
142
|
+
const emit = (value) => { const clean = redact(value); logs = (logs + clean).slice(-12000); options.onLog?.(clean); };
|
|
143
|
+
const log = (data) => {
|
|
144
|
+
pendingLog += data.toString();
|
|
145
|
+
const last = pendingLog.lastIndexOf("\n");
|
|
146
|
+
if (last >= 0) {
|
|
147
|
+
emit(pendingLog.slice(0, last + 1));
|
|
148
|
+
pendingLog = pendingLog.slice(last + 1);
|
|
149
|
+
}
|
|
150
|
+
if (pendingLog.length > 64_000) {
|
|
151
|
+
emit(pendingLog);
|
|
152
|
+
pendingLog = "";
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
child.stdout?.on("data", log);
|
|
156
|
+
child.stderr?.on("data", log);
|
|
157
|
+
try {
|
|
158
|
+
await waitForProcess(child, options.signal, 10 * 60_000);
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
emit(pendingLog);
|
|
162
|
+
pendingLog = "";
|
|
163
|
+
throw new Error(`npm ${args.join(" ")} failed: ${error instanceof Error ? error.message : error}\n${logs.trim()}`);
|
|
164
|
+
}
|
|
165
|
+
emit(pendingLog);
|
|
166
|
+
}
|
|
167
|
+
function killProcess(child, signal = "SIGTERM") {
|
|
168
|
+
if (!child.pid || child.exitCode !== null || child.signalCode !== null)
|
|
169
|
+
return;
|
|
170
|
+
if (process.platform === "win32") {
|
|
171
|
+
const taskkill = (0, node_path_1.join)(process.env.SystemRoot || "C:\\Windows", "System32", "taskkill.exe");
|
|
172
|
+
(0, node_child_process_1.execFile)(taskkill, ["/pid", String(child.pid), "/t", "/f"], { windowsHide: true, timeout: 5000 }, error => {
|
|
173
|
+
if (error && child.exitCode === null && child.signalCode === null)
|
|
174
|
+
child.kill(signal);
|
|
175
|
+
});
|
|
176
|
+
return;
|
|
177
|
+
}
|
|
178
|
+
try {
|
|
179
|
+
process.kill(-child.pid, signal);
|
|
180
|
+
}
|
|
181
|
+
catch (error) {
|
|
182
|
+
if (error.code !== "ESRCH")
|
|
183
|
+
throw error;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
async function waitForProcess(child, signal, timeout = 30_000) {
|
|
187
|
+
await new Promise((resolve, reject) => {
|
|
188
|
+
let timer;
|
|
189
|
+
let stopReason;
|
|
190
|
+
const stop = (reason) => {
|
|
191
|
+
if (stopReason)
|
|
192
|
+
return;
|
|
193
|
+
stopReason = reason;
|
|
194
|
+
killProcess(child, "SIGKILL");
|
|
195
|
+
};
|
|
196
|
+
const abort = () => stop(signal?.reason || new Error("The process was cancelled."));
|
|
197
|
+
const onError = (error) => finish(stopReason ?? error);
|
|
198
|
+
const onClose = (code, killed) => {
|
|
199
|
+
finish(stopReason ?? (code === 0 ? undefined : new Error(killed ? `Terminated by ${killed}` : `Exit code ${code}`)));
|
|
200
|
+
};
|
|
201
|
+
const finish = (error) => {
|
|
202
|
+
clearTimeout(timer);
|
|
203
|
+
signal?.removeEventListener("abort", abort);
|
|
204
|
+
child.off("error", onError);
|
|
205
|
+
child.off("close", onClose);
|
|
206
|
+
error ? reject(error) : resolve();
|
|
207
|
+
};
|
|
208
|
+
timer = setTimeout(() => stop(new Error("The process timed out.")), timeout);
|
|
209
|
+
child.once("error", onError);
|
|
210
|
+
child.once("close", onClose);
|
|
211
|
+
if (signal?.aborted)
|
|
212
|
+
abort();
|
|
213
|
+
else
|
|
214
|
+
signal?.addEventListener("abort", abort, { once: true });
|
|
215
|
+
});
|
|
216
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
export class ActivityFeed {
|
|
2
|
+
transcript;
|
|
3
|
+
groups = new Map();
|
|
4
|
+
entries = new Map();
|
|
5
|
+
constructor(transcript) {
|
|
6
|
+
this.transcript = transcript;
|
|
7
|
+
window.setInterval(() => {
|
|
8
|
+
for (const group of this.groups.values())
|
|
9
|
+
if (group.status === "running")
|
|
10
|
+
this.summary(group);
|
|
11
|
+
}, 1000);
|
|
12
|
+
}
|
|
13
|
+
clear() { this.groups.clear(); this.entries.clear(); }
|
|
14
|
+
summary(group) {
|
|
15
|
+
group.root.dataset.status = group.status;
|
|
16
|
+
group.title.textContent = group.status === "running" ? group.latest || "Working…"
|
|
17
|
+
: group.resultTitle ?? "Completed";
|
|
18
|
+
group.title.title = group.title.textContent;
|
|
19
|
+
const elapsed = group.startedAt ? Math.max(0, Math.round(((group.completedAt ?? Date.now()) - group.startedAt) / 1000)) : undefined;
|
|
20
|
+
group.meta.textContent = [group.count ? `${group.count} ${group.count === 1 ? "action" : "actions"}` : "",
|
|
21
|
+
elapsed === undefined ? "" : elapsed >= 60 ? `${Math.floor(elapsed / 60)}m ${elapsed % 60}s` : `${elapsed}s`].filter(Boolean).join(" · ");
|
|
22
|
+
}
|
|
23
|
+
show(message) {
|
|
24
|
+
const { turnId: id, activity } = message;
|
|
25
|
+
if (!id || !activity)
|
|
26
|
+
return;
|
|
27
|
+
let group = this.groups.get(id);
|
|
28
|
+
if (!group) {
|
|
29
|
+
const root = document.createElement("details");
|
|
30
|
+
root.className = "turn-activity";
|
|
31
|
+
root.open = activity.status === "running";
|
|
32
|
+
const summary = document.createElement("summary");
|
|
33
|
+
const indicator = document.createElement("span");
|
|
34
|
+
indicator.className = "activity-indicator";
|
|
35
|
+
indicator.setAttribute("aria-hidden", "true");
|
|
36
|
+
const title = document.createElement("span");
|
|
37
|
+
title.className = "activity-title";
|
|
38
|
+
const meta = document.createElement("span");
|
|
39
|
+
meta.className = "activity-meta";
|
|
40
|
+
summary.append(indicator, title, meta);
|
|
41
|
+
const list = document.createElement("div");
|
|
42
|
+
list.className = "activity-list";
|
|
43
|
+
const notice = document.createElement("p");
|
|
44
|
+
notice.className = "activity-notice";
|
|
45
|
+
notice.dir = "auto";
|
|
46
|
+
notice.hidden = true;
|
|
47
|
+
root.append(summary, notice, list);
|
|
48
|
+
this.transcript.append(root);
|
|
49
|
+
group = { root, title, meta, list, notice, count: 0, status: activity.kind === "run" ? activity.status : "completed", latest: "" };
|
|
50
|
+
this.groups.set(id, group);
|
|
51
|
+
}
|
|
52
|
+
if (activity.kind === "run") {
|
|
53
|
+
const finished = group.status === "running" && activity.status !== "running";
|
|
54
|
+
group.status = activity.status;
|
|
55
|
+
group.resultTitle = activity.title;
|
|
56
|
+
group.startedAt = activity.startedAt;
|
|
57
|
+
group.completedAt = activity.completedAt;
|
|
58
|
+
group.notice.textContent = activity.detail ?? "";
|
|
59
|
+
group.notice.hidden = !activity.detail;
|
|
60
|
+
if (finished)
|
|
61
|
+
group.root.open = false;
|
|
62
|
+
}
|
|
63
|
+
else {
|
|
64
|
+
let entry = this.entries.get(message.id);
|
|
65
|
+
if (!entry) {
|
|
66
|
+
const root = document.createElement("details");
|
|
67
|
+
root.className = "activity-entry";
|
|
68
|
+
const summary = document.createElement("summary");
|
|
69
|
+
const icon = document.createElement("span");
|
|
70
|
+
icon.className = `icon icon-${{ read: "document", edit: "edit", search: "search", command: "terminal", plan: "document", tool: "shadow" }[activity.kind]}`;
|
|
71
|
+
icon.setAttribute("aria-hidden", "true");
|
|
72
|
+
const title = document.createElement("span");
|
|
73
|
+
const detail = document.createElement("pre");
|
|
74
|
+
detail.className = "activity-detail";
|
|
75
|
+
summary.append(icon, title);
|
|
76
|
+
root.append(summary, detail);
|
|
77
|
+
group.list.append(root);
|
|
78
|
+
entry = { root, title, detail };
|
|
79
|
+
this.entries.set(message.id, entry);
|
|
80
|
+
group.count++;
|
|
81
|
+
}
|
|
82
|
+
entry.root.dataset.status = activity.status;
|
|
83
|
+
entry.title.textContent = activity.title;
|
|
84
|
+
entry.title.title = activity.title;
|
|
85
|
+
entry.detail.textContent = activity.detail ?? "";
|
|
86
|
+
entry.detail.hidden = !activity.detail;
|
|
87
|
+
group.latest = activity.title;
|
|
88
|
+
}
|
|
89
|
+
this.summary(group);
|
|
90
|
+
}
|
|
91
|
+
}
|