@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,802 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const electron_1 = require("electron");
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const node_crypto_1 = require("node:crypto");
|
|
6
|
+
const node_fs_1 = require("node:fs");
|
|
7
|
+
const promises_1 = require("node:fs/promises");
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const node_util_1 = require("node:util");
|
|
10
|
+
const node_url_1 = require("node:url");
|
|
11
|
+
const text_file_1 = require("../app-runtime/text-file");
|
|
12
|
+
const files_1 = require("../project/files");
|
|
13
|
+
const project_1 = require("../project/project");
|
|
14
|
+
const live_preview_1 = require("../project/live-preview");
|
|
15
|
+
const managed_runtime_1 = require("../project/managed-runtime");
|
|
16
|
+
const toolchain_1 = require("../project/toolchain");
|
|
17
|
+
const shadows_1 = require("../project/shadows");
|
|
18
|
+
const shadow_effects_1 = require("../project/shadow-effects");
|
|
19
|
+
const runner_1 = require("../agent-actions/runner");
|
|
20
|
+
const access_1 = require("../agent-actions/access");
|
|
21
|
+
const sessions_1 = require("../agents/sessions");
|
|
22
|
+
const registry_1 = require("../agents/registry");
|
|
23
|
+
const macos_1 = require("../instances/macos");
|
|
24
|
+
const distribution_1 = require("../instances/distribution");
|
|
25
|
+
const desktop_1 = require("../instances/desktop");
|
|
26
|
+
const repository_1 = require("../instances/repository");
|
|
27
|
+
const shadow_repository_1 = require("../instances/shadow-repository");
|
|
28
|
+
const titleBarStyle = process.platform === "darwin" ? "hiddenInset" : undefined;
|
|
29
|
+
function projectKey(directory) {
|
|
30
|
+
return (0, node_crypto_1.createHash)("sha256").update(directory).digest("hex");
|
|
31
|
+
}
|
|
32
|
+
function kitDirectory() {
|
|
33
|
+
return process.env.MORKA_NPM_KIT || (electron_1.app.isPackaged ? (0, node_path_1.join)(process.resourcesPath, repository_1.bundledKit) : (0, node_path_1.join)(__dirname, "../npm"));
|
|
34
|
+
}
|
|
35
|
+
async function openRepository(directory) {
|
|
36
|
+
const repository = (0, node_path_1.dirname)(await (0, repository_1.repositoryProject)(directory));
|
|
37
|
+
const spec = (0, repository_1.readRelease)((0, repository_1.repositoryFile)(repository, "release.json"));
|
|
38
|
+
const executable = process.platform === "darwin"
|
|
39
|
+
? await (0, distribution_1.prepareMacProjectExecutable)(process.execPath, electron_1.app.getPath("appData"), repository, spec)
|
|
40
|
+
: process.execPath;
|
|
41
|
+
const env = { ...process.env, MORKA_NPM_REPOSITORY: repository };
|
|
42
|
+
delete env.ELECTRON_RUN_AS_NODE;
|
|
43
|
+
const child = (0, node_child_process_1.spawn)(executable, electron_1.app.isPackaged ? [] : [electron_1.app.getAppPath()], {
|
|
44
|
+
cwd: electron_1.app.getAppPath(), env, detached: true, stdio: "ignore", windowsHide: true,
|
|
45
|
+
});
|
|
46
|
+
await new Promise((resolve, reject) => {
|
|
47
|
+
child.once("spawn", resolve);
|
|
48
|
+
child.once("error", reject);
|
|
49
|
+
});
|
|
50
|
+
child.unref();
|
|
51
|
+
}
|
|
52
|
+
async function installRepository(repository) {
|
|
53
|
+
const toolchain = await (0, toolchain_1.bundledToolchain)(process.env.MORKA_DESKTOP_TOOLCHAIN);
|
|
54
|
+
await (0, node_util_1.promisify)(node_child_process_1.execFile)(toolchain.node, [(0, repository_1.repositoryFile)(repository, "npm", "install-app.cjs"), "--open"], {
|
|
55
|
+
cwd: repository, windowsHide: true, maxBuffer: 4 * 1024 * 1024,
|
|
56
|
+
env: { ...process.env, npm_execpath: toolchain.npm,
|
|
57
|
+
MORKA_INSTALL_TOOLCHAIN: toolchain.system ? "" : toolchain.root },
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
function registerInstaller(window, requireShell, getFiles, options = {}) {
|
|
61
|
+
let installing = false;
|
|
62
|
+
electron_1.ipcMain.handle("instance:install", async (event, rawName) => {
|
|
63
|
+
requireShell(event);
|
|
64
|
+
if (installing)
|
|
65
|
+
return;
|
|
66
|
+
installing = true;
|
|
67
|
+
try {
|
|
68
|
+
const name = (0, repository_1.portableAppName)(rawName);
|
|
69
|
+
const choice = await electron_1.dialog.showOpenDialog(window, {
|
|
70
|
+
properties: ["openDirectory", "createDirectory"],
|
|
71
|
+
title: `Choose where to create “${name}”`,
|
|
72
|
+
message: "Morka will create the project folder inside the selected folder.",
|
|
73
|
+
defaultPath: electron_1.app.getPath("documents"),
|
|
74
|
+
buttonLabel: "Choose Folder",
|
|
75
|
+
});
|
|
76
|
+
const parent = choice.filePaths[0];
|
|
77
|
+
if (choice.canceled || !parent)
|
|
78
|
+
return;
|
|
79
|
+
const files = await getFiles();
|
|
80
|
+
await (0, project_1.compileProject)(files);
|
|
81
|
+
const source = options.source?.();
|
|
82
|
+
if (source && (0, shadow_repository_1.containsPath)(source.repositoryDirectory ?? source.projectDirectory, await (0, promises_1.realpath)(parent))) {
|
|
83
|
+
throw new Error("Choose a folder outside the original project.");
|
|
84
|
+
}
|
|
85
|
+
const iconColor = (source ? source.iconColor + (0, node_crypto_1.randomInt)(1, 8) : (0, node_crypto_1.randomInt)(8)) % 8;
|
|
86
|
+
const spec = { version: 1, id: (0, node_crypto_1.randomUUID)(), name, iconColor,
|
|
87
|
+
...(source ? { clonedFrom: { id: source.id, revision: (0, files_1.digest)(files) } } : {}) };
|
|
88
|
+
const path = await (0, repository_1.materializeRepository)(parent, spec, files, kitDirectory(), (0, macos_1.makeIcon)(iconColor));
|
|
89
|
+
const openError = await installRepository(path).then(() => "", error => String(error));
|
|
90
|
+
if (options.onInstalled)
|
|
91
|
+
setImmediate(options.onInstalled, path, openError);
|
|
92
|
+
return { path, openError };
|
|
93
|
+
}
|
|
94
|
+
catch (error) {
|
|
95
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
installing = false;
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
window.on("close", event => { if (installing)
|
|
102
|
+
event.preventDefault(); });
|
|
103
|
+
window.once("closed", () => electron_1.ipcMain.removeHandler("instance:install"));
|
|
104
|
+
}
|
|
105
|
+
async function createLauncher() {
|
|
106
|
+
const index = (0, node_path_1.join)(__dirname, "../renderer/launcher.html");
|
|
107
|
+
const url = (0, node_url_1.pathToFileURL)(index).href;
|
|
108
|
+
const window = new electron_1.BrowserWindow({
|
|
109
|
+
width: 560, height: 400, minWidth: 440, minHeight: 360,
|
|
110
|
+
title: "Morka", titleBarStyle, backgroundColor: "#ffffff",
|
|
111
|
+
webPreferences: {
|
|
112
|
+
preload: (0, node_path_1.join)(__dirname, "preload.js"), contextIsolation: true, sandbox: true, nodeIntegration: false,
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
const contents = window.webContents;
|
|
116
|
+
contents.setWindowOpenHandler(() => ({ action: "deny" }));
|
|
117
|
+
contents.on("will-navigate", event => event.preventDefault());
|
|
118
|
+
const requireLauncher = (event) => {
|
|
119
|
+
if (event.sender !== contents || event.senderFrame !== contents.mainFrame || event.senderFrame?.url !== url) {
|
|
120
|
+
throw new Error("This request is not allowed.");
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
registerInstaller(window, requireLauncher, async () => {
|
|
124
|
+
const files = await (0, files_1.snapshot)((0, node_path_1.join)(__dirname, "../app-runtime/starter"));
|
|
125
|
+
files.set(files_1.entryFile, (0, project_1.jsonFile)(project_1.starterManifest));
|
|
126
|
+
return files;
|
|
127
|
+
}, {
|
|
128
|
+
onInstalled(path, openError) {
|
|
129
|
+
if (!openError)
|
|
130
|
+
electron_1.app.quit();
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
electron_1.ipcMain.handle("instance:open", async (event) => {
|
|
134
|
+
requireLauncher(event);
|
|
135
|
+
const choice = await electron_1.dialog.showOpenDialog(window, { title: "Open a Morka project", properties: ["openDirectory"] });
|
|
136
|
+
if (choice.canceled || !choice.filePaths[0])
|
|
137
|
+
return;
|
|
138
|
+
await openRepository(choice.filePaths[0]);
|
|
139
|
+
setImmediate(() => electron_1.app.quit());
|
|
140
|
+
});
|
|
141
|
+
window.once("closed", () => electron_1.ipcMain.removeHandler("instance:open"));
|
|
142
|
+
await window.loadFile(index);
|
|
143
|
+
}
|
|
144
|
+
async function createWindow(instance, release, sourceLaunch = false) {
|
|
145
|
+
const index = (0, node_path_1.join)(__dirname, "../renderer/index.html");
|
|
146
|
+
const shellUrl = (0, node_url_1.pathToFileURL)(index).href;
|
|
147
|
+
const dataDirectory = electron_1.app.getPath("userData");
|
|
148
|
+
const appLibrary = (0, repository_1.appsDirectory)(electron_1.app.getPath("appData"));
|
|
149
|
+
const workspaceFile = (0, node_path_1.join)(dataDirectory, "workspace-path");
|
|
150
|
+
let repository = instance.repositoryDirectory;
|
|
151
|
+
if (release && (await (0, files_1.info)(workspaceFile))?.isFile()) {
|
|
152
|
+
repository = (await (0, promises_1.readFile)(workspaceFile, "utf8")).trim();
|
|
153
|
+
if (!(0, node_path_1.isAbsolute)(repository))
|
|
154
|
+
throw new Error("The saved project folder is invalid.");
|
|
155
|
+
}
|
|
156
|
+
let projectDirectory = repository ? await (0, repository_1.repositoryProject)(repository) : await (0, promises_1.realpath)(instance.projectDirectory);
|
|
157
|
+
if (repository)
|
|
158
|
+
repository = (0, node_path_1.dirname)(projectDirectory);
|
|
159
|
+
if ((await (0, files_1.info)(projectDirectory))?.isSymbolicLink())
|
|
160
|
+
throw new Error("The project root cannot be a symbolic link.");
|
|
161
|
+
const projectId = instance.id;
|
|
162
|
+
const runtimeHome = (0, node_path_1.join)(dataDirectory, "runtime", projectId);
|
|
163
|
+
const [checkout, bridge, registry] = await Promise.all([
|
|
164
|
+
(0, files_1.snapshot)(projectDirectory).then(project_1.compileProject)
|
|
165
|
+
.then(project => ({ project, error: undefined }), error => ({ project: undefined, error })),
|
|
166
|
+
(0, promises_1.readFile)((0, node_path_1.join)(__dirname, "../app-runtime/bridge.js"), "utf8"),
|
|
167
|
+
registry_1.AgentRegistry.open(dataDirectory, repository ?? projectDirectory, (0, node_path_1.join)(__dirname, "../skills")),
|
|
168
|
+
]);
|
|
169
|
+
const createManaged = () => new managed_runtime_1.ManagedRuntime(projectDirectory, runtimeHome, (0, node_path_1.join)(dataDirectory, "runtime-data"), detail => setRuntimeState("error", detail), undefined, (0, node_path_1.join)(appLibrary, "origins"), bridge);
|
|
170
|
+
let managed = createManaged();
|
|
171
|
+
let savedProject;
|
|
172
|
+
let savedArtifact;
|
|
173
|
+
if ((await (0, files_1.info)((0, node_path_1.join)(runtimeHome, "active.json")))?.isFile()) {
|
|
174
|
+
for (const previous of [false, true]) {
|
|
175
|
+
try {
|
|
176
|
+
const artifact = await managed.recover(previous);
|
|
177
|
+
if (artifact) {
|
|
178
|
+
savedProject = await (0, project_1.compileProject)(artifact.source);
|
|
179
|
+
savedArtifact = artifact;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch (error) {
|
|
184
|
+
console.error(error);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
const initialProject = savedProject ?? checkout.project;
|
|
189
|
+
if (!initialProject)
|
|
190
|
+
throw checkout.error;
|
|
191
|
+
let project = initialProject;
|
|
192
|
+
let previewProject = project;
|
|
193
|
+
let recoveredCheckout = Boolean(savedProject && (!checkout.project || (0, files_1.digest)(savedProject.files) !== (0, files_1.digest)(checkout.project.files)));
|
|
194
|
+
const chatDirectory = (0, node_path_1.join)(dataDirectory, "codex", projectId);
|
|
195
|
+
const chats = await sessions_1.ChatSessions.open(chatDirectory, projectDirectory, registry, repository);
|
|
196
|
+
const window = new electron_1.BrowserWindow({
|
|
197
|
+
width: 1120, height: 730, minWidth: 720, minHeight: 460,
|
|
198
|
+
title: instance.name, titleBarStyle,
|
|
199
|
+
backgroundColor: "#f2f3f7",
|
|
200
|
+
webPreferences: {
|
|
201
|
+
preload: (0, node_path_1.join)(__dirname, "preload.js"), contextIsolation: true, sandbox: true, nodeIntegration: false,
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
window.on("page-title-updated", event => event.preventDefault());
|
|
205
|
+
const access = sourceLaunch ? new access_1.ActionAccess((0, node_path_1.join)(dataDirectory, "agent-access", (0, repository_1.checkoutKey)(repository ?? projectDirectory) + ".json")) : undefined;
|
|
206
|
+
const agents = new runner_1.AgentRunner((0, node_path_1.join)(chatDirectory, "runs"), registry, access && (action => access.choose(action, async (choices) => {
|
|
207
|
+
const accounts = (await registry.setup()).filter(account => choices.includes(account.id) &&
|
|
208
|
+
(account.state === "ready" || account.state === "unavailable"));
|
|
209
|
+
if (!accounts.length) {
|
|
210
|
+
const names = choices.map(id => registry.get(id).info.name);
|
|
211
|
+
throw new Error(`Open AI setup and sign in to ${names.join(" or ")} to use this action.`);
|
|
212
|
+
}
|
|
213
|
+
const { response } = await electron_1.dialog.showMessageBox(window, {
|
|
214
|
+
type: "question", buttons: ["Cancel", ...accounts.map(account => `Allow ${account.name}`)], defaultId: 0, cancelId: 0,
|
|
215
|
+
message: `Allow ${instance.name} to use your local AI account?`,
|
|
216
|
+
detail: `The app requested “${action.id}”. Its AI actions can send app-provided inputs to the chosen provider and use that account's limits. You can reset access in AI setup.`,
|
|
217
|
+
});
|
|
218
|
+
return accounts[response - 1]?.id;
|
|
219
|
+
})));
|
|
220
|
+
const contents = window.webContents;
|
|
221
|
+
let activeAgentCount = 0;
|
|
222
|
+
async function whileAgentActive(run) {
|
|
223
|
+
const notify = () => { if (!contents.isDestroyed())
|
|
224
|
+
contents.send("agent:activity", activeAgentCount > 0); };
|
|
225
|
+
activeAgentCount += 1;
|
|
226
|
+
notify();
|
|
227
|
+
try {
|
|
228
|
+
return await run();
|
|
229
|
+
}
|
|
230
|
+
finally {
|
|
231
|
+
activeAgentCount -= 1;
|
|
232
|
+
notify();
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
let runtimeState = { status: "stopped" };
|
|
236
|
+
let runtimeUrl;
|
|
237
|
+
let operation = false;
|
|
238
|
+
let closing = false;
|
|
239
|
+
let runtimeTask = Promise.resolve();
|
|
240
|
+
const setRuntimeState = (status, detail) => {
|
|
241
|
+
runtimeState = { status, detail };
|
|
242
|
+
if (!contents.isDestroyed())
|
|
243
|
+
contents.send("runtime:state", runtimeState);
|
|
244
|
+
};
|
|
245
|
+
const livePreview = new live_preview_1.LivePreview(contents, detail => setRuntimeState("error", detail));
|
|
246
|
+
chats.setProjectPreparer((files, signal, onLog) => managed.prepare(files, { signal, updateLock: true, onLog }));
|
|
247
|
+
chats.setProjectActivator((next, publish, signal) => activateDraft(next, publish, signal));
|
|
248
|
+
chats.setShadowLoader(loadImportedShadow);
|
|
249
|
+
const isShell = (event) => event.sender === contents && event.senderFrame === contents.mainFrame && event.senderFrame?.url === shellUrl;
|
|
250
|
+
const requireShell = (event) => {
|
|
251
|
+
if (!isShell(event))
|
|
252
|
+
throw new Error("This request is not allowed.");
|
|
253
|
+
};
|
|
254
|
+
const shadowUrl = shellUrl + "?scope=shadow";
|
|
255
|
+
const editors = new Map();
|
|
256
|
+
const openingEditors = new Map();
|
|
257
|
+
function chatScope(event) {
|
|
258
|
+
if (isShell(event))
|
|
259
|
+
return { chats, registry, window, workspace: undefined };
|
|
260
|
+
const editor = editors.get(event.sender.id);
|
|
261
|
+
if (editor && event.senderFrame === event.sender.mainFrame && event.senderFrame?.url === shadowUrl)
|
|
262
|
+
return editor;
|
|
263
|
+
throw new Error("This request is not allowed.");
|
|
264
|
+
}
|
|
265
|
+
async function openShadowEditor(workspace) {
|
|
266
|
+
const current = [...editors.values()].find(editor => editor.workspace.repository === workspace.repository);
|
|
267
|
+
if (current) {
|
|
268
|
+
if (current.window.isMinimized())
|
|
269
|
+
current.window.restore();
|
|
270
|
+
current.window.focus();
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
const pending = openingEditors.get(workspace.repository);
|
|
274
|
+
if (pending)
|
|
275
|
+
return pending;
|
|
276
|
+
const open = async () => {
|
|
277
|
+
const library = (0, node_path_1.join)(appLibrary, "shadow-chats", workspace.id, projectKey(workspace.repository));
|
|
278
|
+
const services = await registry_1.AgentRegistry.open(library, workspace.repository, (0, node_path_1.join)(__dirname, "../skills"));
|
|
279
|
+
try {
|
|
280
|
+
const sessions = await sessions_1.ChatSessions.open(library, workspace.root, services, workspace.repository, "shadow", repository ?? projectDirectory);
|
|
281
|
+
if (window.isDestroyed())
|
|
282
|
+
throw new Error("The app window closed.");
|
|
283
|
+
const editor = new electron_1.BrowserWindow({
|
|
284
|
+
parent: window, title: `Shadow · ${workspace.name}`, width: 690, height: 670,
|
|
285
|
+
minWidth: 470, minHeight: 420, titleBarStyle, backgroundColor: "#f2f3f7",
|
|
286
|
+
webPreferences: { preload: (0, node_path_1.join)(__dirname, "preload.js"), contextIsolation: true, sandbox: true, nodeIntegration: false },
|
|
287
|
+
});
|
|
288
|
+
const view = editor.webContents;
|
|
289
|
+
const id = view.id;
|
|
290
|
+
editors.set(id, { window: editor, workspace, chats: sessions, registry: services });
|
|
291
|
+
view.setWindowOpenHandler(() => ({ action: "deny" }));
|
|
292
|
+
view.on("will-navigate", event => event.preventDefault());
|
|
293
|
+
editor.once("closed", () => {
|
|
294
|
+
editors.delete(id);
|
|
295
|
+
sessions.close();
|
|
296
|
+
services.close();
|
|
297
|
+
});
|
|
298
|
+
try {
|
|
299
|
+
await editor.loadFile(index, { query: { scope: "shadow" } });
|
|
300
|
+
}
|
|
301
|
+
catch (error) {
|
|
302
|
+
editor.destroy();
|
|
303
|
+
throw error;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
catch (error) {
|
|
307
|
+
services.close();
|
|
308
|
+
throw error;
|
|
309
|
+
}
|
|
310
|
+
};
|
|
311
|
+
const result = open().finally(() => openingEditors.delete(workspace.repository));
|
|
312
|
+
openingEditors.set(workspace.repository, result);
|
|
313
|
+
return result;
|
|
314
|
+
}
|
|
315
|
+
electron_1.ipcMain.handle("instance:current", event => {
|
|
316
|
+
requireShell(event);
|
|
317
|
+
return instance.name;
|
|
318
|
+
});
|
|
319
|
+
registerInstaller(window, requireShell, async () => {
|
|
320
|
+
if (activeAgentCount)
|
|
321
|
+
throw new Error("Wait for the agent to finish before cloning this app.");
|
|
322
|
+
return managed.sourceFiles ?? (0, files_1.snapshot)(projectDirectory);
|
|
323
|
+
}, { source: () => ({ ...instance, projectDirectory, repositoryDirectory: repository }) });
|
|
324
|
+
let creatingWorkspace;
|
|
325
|
+
async function ensureEditable() {
|
|
326
|
+
if (!release || repository)
|
|
327
|
+
return true;
|
|
328
|
+
creatingWorkspace ??= createEditableWorkspace(release).finally(() => { creatingWorkspace = undefined; });
|
|
329
|
+
return creatingWorkspace;
|
|
330
|
+
}
|
|
331
|
+
async function createEditableWorkspace(spec) {
|
|
332
|
+
const choice = await electron_1.dialog.showOpenDialog(window, {
|
|
333
|
+
title: "Choose where to create your editable project",
|
|
334
|
+
message: "Morka will create a Git repository with the app source in the selected folder.",
|
|
335
|
+
buttonLabel: "Create Project",
|
|
336
|
+
properties: ["openDirectory", "createDirectory"],
|
|
337
|
+
defaultPath: electron_1.app.getPath("documents"),
|
|
338
|
+
});
|
|
339
|
+
const parent = choice.filePaths[0];
|
|
340
|
+
if (choice.canceled || !parent)
|
|
341
|
+
return false;
|
|
342
|
+
if (operation)
|
|
343
|
+
throw new Error("Wait for the app service to finish starting or stopping.");
|
|
344
|
+
operation = true;
|
|
345
|
+
try {
|
|
346
|
+
const icon = await (0, promises_1.readFile)((0, node_path_1.join)(process.resourcesPath, repository_1.appIcon));
|
|
347
|
+
const directory = await (0, repository_1.materializeRepository)(parent, spec, project.files, (0, node_path_1.join)(process.resourcesPath, repository_1.bundledKit), icon);
|
|
348
|
+
const editable = await (0, repository_1.repositoryProject)(directory);
|
|
349
|
+
await livePreview.prepare();
|
|
350
|
+
await managed.stop();
|
|
351
|
+
await (0, text_file_1.writeTextAtomically)(workspaceFile, directory);
|
|
352
|
+
repository = directory;
|
|
353
|
+
projectDirectory = editable;
|
|
354
|
+
project = await (0, project_1.compileProject)(await (0, files_1.snapshot)(editable));
|
|
355
|
+
previewProject = project;
|
|
356
|
+
chats.setProjectRoot(editable, directory);
|
|
357
|
+
managed = createManaged();
|
|
358
|
+
runtimeUrl = undefined;
|
|
359
|
+
setRuntimeState("stopped");
|
|
360
|
+
return true;
|
|
361
|
+
}
|
|
362
|
+
finally {
|
|
363
|
+
operation = false;
|
|
364
|
+
void syncRuntime();
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
function previewTransition(next) {
|
|
368
|
+
const previous = previewProject;
|
|
369
|
+
const previousUrl = runtimeUrl;
|
|
370
|
+
let started = false;
|
|
371
|
+
let stylesShown;
|
|
372
|
+
return {
|
|
373
|
+
async activate(url, styles) {
|
|
374
|
+
started = true;
|
|
375
|
+
stylesShown = styles;
|
|
376
|
+
if (!contents.isDestroyed())
|
|
377
|
+
contents.send("canvas:updating", true);
|
|
378
|
+
previewProject = next;
|
|
379
|
+
runtimeUrl = url;
|
|
380
|
+
await livePreview.show(url, styles, { confirm: true, clear: !url });
|
|
381
|
+
},
|
|
382
|
+
async rollback() {
|
|
383
|
+
previewProject = previous;
|
|
384
|
+
runtimeUrl = previousUrl;
|
|
385
|
+
if (started && !closing)
|
|
386
|
+
await livePreview.show(previousUrl, stylesShown, { confirm: true, recover: true, clear: !previousUrl });
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|
|
390
|
+
async function activateManaged(next, prepared, publish, signal) {
|
|
391
|
+
const spec = next.manifest.runtime;
|
|
392
|
+
if (!prepared && !(await (0, files_1.info)((0, node_path_1.join)(managed.home, "active.json")))) {
|
|
393
|
+
const directory = (0, node_path_1.join)(process.resourcesPath, repository_1.preparedRelease);
|
|
394
|
+
if (release && !repository)
|
|
395
|
+
await managed.packages.importPrepared(directory, next.files);
|
|
396
|
+
else {
|
|
397
|
+
const marker = await (0, promises_1.readFile)((0, node_path_1.join)(directory, "artifact.json"), "utf8").then(JSON.parse).catch(() => undefined);
|
|
398
|
+
if (marker?.sourceHash === (0, files_1.digest)(next.files))
|
|
399
|
+
await managed.packages.importPrepared(directory, next.files);
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
const preview = previewTransition(next);
|
|
403
|
+
setRuntimeState(managed.url ? "preparing" : "starting");
|
|
404
|
+
let completed = false;
|
|
405
|
+
try {
|
|
406
|
+
runtimeUrl = await managed.update(next.files, spec, {
|
|
407
|
+
beforeActivate: () => livePreview.prepare(),
|
|
408
|
+
activate: preview.activate,
|
|
409
|
+
rollback: preview.rollback,
|
|
410
|
+
publish,
|
|
411
|
+
}, { signal, prepared });
|
|
412
|
+
project = previewProject = next;
|
|
413
|
+
completed = true;
|
|
414
|
+
}
|
|
415
|
+
finally {
|
|
416
|
+
if (completed)
|
|
417
|
+
await livePreview.completeUpdate();
|
|
418
|
+
else
|
|
419
|
+
await livePreview.resume();
|
|
420
|
+
}
|
|
421
|
+
setRuntimeState("running");
|
|
422
|
+
}
|
|
423
|
+
function reportUpdateFailure(error, draft = false) {
|
|
424
|
+
const detail = error instanceof Error ? error.message : String(error);
|
|
425
|
+
const recovery = runtimeUrl ? "The last working release is still open. " : "";
|
|
426
|
+
setRuntimeState("error", (draft ? "Update failed. Continue in the same chat to repair the saved draft. " : "") + recovery + detail);
|
|
427
|
+
}
|
|
428
|
+
async function activateDraft(next, publish, signal, draft = true) {
|
|
429
|
+
await runtimeTask.catch(() => { });
|
|
430
|
+
signal.throwIfAborted();
|
|
431
|
+
if (operation || closing)
|
|
432
|
+
throw new Error("Wait for the app service to finish its current operation.");
|
|
433
|
+
operation = true;
|
|
434
|
+
runtimeTask = activateManaged(next, undefined, publish, signal)
|
|
435
|
+
.catch(error => {
|
|
436
|
+
if (!closing && !signal.aborted)
|
|
437
|
+
reportUpdateFailure(error, draft);
|
|
438
|
+
if (!draft)
|
|
439
|
+
throw error;
|
|
440
|
+
throw new Error(`The update could not be applied. Your edits are still in this chat's draft.\n${error instanceof Error ? error.message : error}`, { cause: error });
|
|
441
|
+
}).finally(() => { operation = false; });
|
|
442
|
+
await runtimeTask;
|
|
443
|
+
if (!contents.isDestroyed())
|
|
444
|
+
contents.send("shadow:changed");
|
|
445
|
+
}
|
|
446
|
+
function syncRuntime(next = project) {
|
|
447
|
+
if (operation || closing)
|
|
448
|
+
return runtimeTask;
|
|
449
|
+
operation = true;
|
|
450
|
+
const prepared = savedArtifact;
|
|
451
|
+
savedArtifact = undefined;
|
|
452
|
+
runtimeTask = (async () => {
|
|
453
|
+
try {
|
|
454
|
+
await activateManaged(next, prepared);
|
|
455
|
+
}
|
|
456
|
+
catch (error) {
|
|
457
|
+
if (!prepared || closing)
|
|
458
|
+
throw error;
|
|
459
|
+
const previous = await managed.recover(true);
|
|
460
|
+
if (!previous || previous.key === prepared.key)
|
|
461
|
+
throw error;
|
|
462
|
+
await activateManaged(await (0, project_1.compileProject)(previous.source), previous);
|
|
463
|
+
recoveredCheckout = true;
|
|
464
|
+
}
|
|
465
|
+
if (recoveredCheckout) {
|
|
466
|
+
recoveredCheckout = false;
|
|
467
|
+
setRuntimeState("error", "Opened the last working release. Retry to check the current source.");
|
|
468
|
+
}
|
|
469
|
+
if (!contents.isDestroyed())
|
|
470
|
+
contents.send("shadow:changed");
|
|
471
|
+
})().catch(error => {
|
|
472
|
+
if (!closing)
|
|
473
|
+
reportUpdateFailure(error);
|
|
474
|
+
}).finally(() => { operation = false; });
|
|
475
|
+
return runtimeTask;
|
|
476
|
+
}
|
|
477
|
+
contents.setWindowOpenHandler(() => ({ action: "deny" }));
|
|
478
|
+
contents.on("will-frame-navigate", (event) => {
|
|
479
|
+
const fromRuntime = runtimeUrl && URL.parse(event.url)?.origin === new URL(runtimeUrl).origin;
|
|
480
|
+
if (event.isMainFrame || (!fromRuntime && event.url !== "about:blank"))
|
|
481
|
+
event.preventDefault();
|
|
482
|
+
});
|
|
483
|
+
contents.session.setPermissionRequestHandler((_contents, _permission, respond) => respond(false));
|
|
484
|
+
contents.session.setPermissionCheckHandler(() => false);
|
|
485
|
+
electron_1.ipcMain.handle("canvas:current", (event) => { requireShell(event); return runtimeUrl; });
|
|
486
|
+
electron_1.ipcMain.handle("runtime:state", (event) => { requireShell(event); return runtimeState; });
|
|
487
|
+
electron_1.ipcMain.handle("runtime:retry", async (event) => {
|
|
488
|
+
requireShell(event);
|
|
489
|
+
if (operation || closing || runtimeState.status !== "error")
|
|
490
|
+
return;
|
|
491
|
+
try {
|
|
492
|
+
const fresh = await (0, project_1.compileProject)(await (0, files_1.snapshot)(projectDirectory));
|
|
493
|
+
await syncRuntime(fresh);
|
|
494
|
+
}
|
|
495
|
+
catch (error) {
|
|
496
|
+
reportUpdateFailure(error);
|
|
497
|
+
}
|
|
498
|
+
});
|
|
499
|
+
electron_1.ipcMain.handle("shadow:import", async (event) => {
|
|
500
|
+
requireShell(event);
|
|
501
|
+
const choice = await electron_1.dialog.showOpenDialog(window, { properties: ["openDirectory"], title: "Choose a Plugin Shadow folder" });
|
|
502
|
+
if (choice.canceled || !choice.filePaths[0])
|
|
503
|
+
return;
|
|
504
|
+
try {
|
|
505
|
+
const shadow = await (0, shadows_1.importShadow)(choice.filePaths[0], (0, node_path_1.join)(dataDirectory, "shadows"));
|
|
506
|
+
return { id: shadow.id, version: shadow.version, description: shadow.description, digest: shadow.digest };
|
|
507
|
+
}
|
|
508
|
+
catch (error) {
|
|
509
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
510
|
+
}
|
|
511
|
+
});
|
|
512
|
+
electron_1.ipcMain.handle("shadow:list", event => {
|
|
513
|
+
requireShell(event);
|
|
514
|
+
return (project.manifest.shadows ?? []).map(({ id, version }) => ({
|
|
515
|
+
id, version, description: (0, shadow_effects_1.installedShadowDescription)(project.files, id),
|
|
516
|
+
}));
|
|
517
|
+
});
|
|
518
|
+
let removingShadow = false;
|
|
519
|
+
electron_1.ipcMain.handle("shadow:remove", async (event, id) => {
|
|
520
|
+
requireShell(event);
|
|
521
|
+
if (typeof id !== "string" || removingShadow || chats.running)
|
|
522
|
+
throw new Error("Wait for the current edit to finish.");
|
|
523
|
+
removingShadow = true;
|
|
524
|
+
try {
|
|
525
|
+
const current = await (0, files_1.snapshot)(projectDirectory);
|
|
526
|
+
const { only, clean, summary } = (0, shadow_effects_1.planShadowRemoval)(current, id);
|
|
527
|
+
const detail = [
|
|
528
|
+
"Removing the Shadow stops its rules from guiding future app edits.",
|
|
529
|
+
summary.changes.length
|
|
530
|
+
? "Cleanup can also change these files:\n" + summary.changes.map(row => `• ${row.path} — ${row.reason}`).join("\n")
|
|
531
|
+
: "Morka found no prior source changes it could safely reverse.",
|
|
532
|
+
summary.skipped.length ? "These files will be kept because the app may still use them or they changed later:\n" +
|
|
533
|
+
summary.skipped.map(path => `• ${path}`).join("\n") : "",
|
|
534
|
+
].filter(Boolean).join("\n\n");
|
|
535
|
+
const buttons = ["Cancel", "Remove Shadow Only"];
|
|
536
|
+
if (summary.changes.length)
|
|
537
|
+
buttons.push("Remove and Clean Up");
|
|
538
|
+
const choice = await electron_1.dialog.showMessageBox(window, {
|
|
539
|
+
type: "question", buttons, defaultId: 0, cancelId: 0, message: `Remove “${summary.id}” v${summary.version}?`, detail,
|
|
540
|
+
});
|
|
541
|
+
if (choice.response === 0 || !(await ensureEditable()))
|
|
542
|
+
return false;
|
|
543
|
+
if (chats.running || (0, files_1.digest)(await (0, files_1.snapshot)(projectDirectory)) !== (0, files_1.digest)(current)) {
|
|
544
|
+
throw new Error("The app changed during review. Open Remove again to see an updated plan.");
|
|
545
|
+
}
|
|
546
|
+
const selected = choice.response === 2 ? clean : only;
|
|
547
|
+
const compiled = await (0, project_1.compileProject)(selected);
|
|
548
|
+
await activateDraft(compiled, async () => {
|
|
549
|
+
await (0, files_1.applyFiles)(projectDirectory, current, selected);
|
|
550
|
+
return async () => { await (0, files_1.applyFiles)(projectDirectory, selected, current); };
|
|
551
|
+
}, new AbortController().signal, false);
|
|
552
|
+
return true;
|
|
553
|
+
}
|
|
554
|
+
finally {
|
|
555
|
+
removingShadow = false;
|
|
556
|
+
}
|
|
557
|
+
});
|
|
558
|
+
electron_1.ipcMain.handle("shadow:new", async (event, rawName) => {
|
|
559
|
+
requireShell(event);
|
|
560
|
+
const name = (0, repository_1.appName)(rawName);
|
|
561
|
+
const answer = await electron_1.dialog.showOpenDialog(window, {
|
|
562
|
+
properties: ["openDirectory", "createDirectory"], title: `Choose where to create “${name}”`,
|
|
563
|
+
message: "Morka will create the Shadow repository inside the selected folder.",
|
|
564
|
+
buttonLabel: "Choose Folder", defaultPath: electron_1.app.getPath("documents"),
|
|
565
|
+
});
|
|
566
|
+
if (answer.canceled || !answer.filePaths[0])
|
|
567
|
+
return false;
|
|
568
|
+
const parent = await (0, promises_1.realpath)(answer.filePaths[0]);
|
|
569
|
+
if ((0, shadow_repository_1.containsPath)(repository ?? projectDirectory, parent))
|
|
570
|
+
throw new Error("Choose a location outside the app repository.");
|
|
571
|
+
await openShadowEditor(await (0, shadow_repository_1.createShadowRepository)(parent, name, kitDirectory()));
|
|
572
|
+
return true;
|
|
573
|
+
});
|
|
574
|
+
electron_1.ipcMain.handle("shadow:open", async (event) => {
|
|
575
|
+
requireShell(event);
|
|
576
|
+
const answer = await electron_1.dialog.showOpenDialog(window, {
|
|
577
|
+
properties: ["openDirectory"], title: "Open a Shadow repository", buttonLabel: "Open Shadow",
|
|
578
|
+
});
|
|
579
|
+
if (answer.canceled || !answer.filePaths[0])
|
|
580
|
+
return false;
|
|
581
|
+
const workspace = await (0, shadow_repository_1.readShadowWorkspace)(answer.filePaths[0]);
|
|
582
|
+
const root = repository ?? projectDirectory;
|
|
583
|
+
if ((0, shadow_repository_1.containsPath)(root, workspace.repository) || (0, shadow_repository_1.containsPath)(workspace.repository, root)) {
|
|
584
|
+
throw new Error("A Shadow needs a repository outside the app repository.");
|
|
585
|
+
}
|
|
586
|
+
await openShadowEditor(workspace);
|
|
587
|
+
return true;
|
|
588
|
+
});
|
|
589
|
+
electron_1.ipcMain.handle("agent:activity", event => {
|
|
590
|
+
const scope = chatScope(event);
|
|
591
|
+
return scope.workspace ? scope.chats.running : activeAgentCount > 0;
|
|
592
|
+
});
|
|
593
|
+
electron_1.ipcMain.handle("addon:invoke", async (event, url, request) => {
|
|
594
|
+
if (!isShell(event) || !livePreview.isCurrentRuntime(url, runtimeUrl))
|
|
595
|
+
return { error: "This request is not allowed." };
|
|
596
|
+
if (request?.type !== "runAgentAction")
|
|
597
|
+
return { error: "Invalid project action." };
|
|
598
|
+
const requestedProject = previewProject;
|
|
599
|
+
try {
|
|
600
|
+
return { value: await whileAgentActive(() => agents.run(requestedProject, request.action, request.input)) };
|
|
601
|
+
}
|
|
602
|
+
catch (error) {
|
|
603
|
+
console.error(error);
|
|
604
|
+
return { error: error instanceof Error ? error.message : "The agent action could not be completed." };
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
electron_1.ipcMain.handle("chat:target", event => {
|
|
608
|
+
const { workspace } = chatScope(event);
|
|
609
|
+
return workspace ? { kind: "shadow", name: workspace.name, directory: workspace.repository }
|
|
610
|
+
: { kind: "app", name: instance.name };
|
|
611
|
+
});
|
|
612
|
+
electron_1.ipcMain.handle("chat:list", event => chatScope(event).chats.list());
|
|
613
|
+
electron_1.ipcMain.handle("chat:new", event => chatScope(event).chats.create());
|
|
614
|
+
electron_1.ipcMain.handle("chat:open", (event, id) => chatScope(event).chats.select(id));
|
|
615
|
+
electron_1.ipcMain.handle("chat:attach", async (event, kind) => {
|
|
616
|
+
const scope = chatScope(event);
|
|
617
|
+
if (kind !== "file" && kind !== "folder")
|
|
618
|
+
throw new Error("Choose files or a folder.");
|
|
619
|
+
const selection = await electron_1.dialog.showOpenDialog(scope.window, {
|
|
620
|
+
title: kind === "folder" ? "Attach folder" : "Attach files", buttonLabel: "Attach",
|
|
621
|
+
properties: [kind === "folder" ? "openDirectory" : "openFile", "multiSelections"],
|
|
622
|
+
});
|
|
623
|
+
return selection.canceled ? [] : scope.chats.attachments.import(selection.filePaths);
|
|
624
|
+
});
|
|
625
|
+
electron_1.ipcMain.handle("chat:attach-paths", async (event, paths) => {
|
|
626
|
+
const scope = chatScope(event);
|
|
627
|
+
if (!Array.isArray(paths) || paths.length > 100 || !paths.every(path => typeof path === "string" && (0, node_path_1.isAbsolute)(path)))
|
|
628
|
+
throw new Error("Invalid attachments.");
|
|
629
|
+
return scope.chats.attachments.import(paths);
|
|
630
|
+
});
|
|
631
|
+
electron_1.ipcMain.handle("chat:attachment-reveal", async (event, id) => {
|
|
632
|
+
const path = await chatScope(event).chats.attachments.path(id);
|
|
633
|
+
electron_1.shell.showItemInFolder(path);
|
|
634
|
+
});
|
|
635
|
+
electron_1.ipcMain.handle("chat:open-link", async (event, url) => {
|
|
636
|
+
chatScope(event);
|
|
637
|
+
const link = typeof url === "string" && url.length <= 8192 ? URL.parse(url) : null;
|
|
638
|
+
if (!link || !["https:", "http:", "mailto:"].includes(link.protocol))
|
|
639
|
+
throw new Error("Unsupported link.");
|
|
640
|
+
await electron_1.shell.openExternal(link.href);
|
|
641
|
+
});
|
|
642
|
+
electron_1.ipcMain.handle("chat:providers", event => chatScope(event).registry.providers());
|
|
643
|
+
electron_1.ipcMain.handle("agent:setup", async (event) => {
|
|
644
|
+
const scope = chatScope(event);
|
|
645
|
+
const accounts = await scope.registry.setup();
|
|
646
|
+
const authorized = scope.workspace ? [] : await access?.current() ?? [];
|
|
647
|
+
return accounts.map(account => ({ ...account, authorized: authorized.includes(account.id) }));
|
|
648
|
+
});
|
|
649
|
+
electron_1.ipcMain.handle("agent:clear-access", async (event) => {
|
|
650
|
+
requireShell(event);
|
|
651
|
+
if (activeAgentCount)
|
|
652
|
+
throw new Error("Wait for the current agent action to finish.");
|
|
653
|
+
await access?.clear();
|
|
654
|
+
});
|
|
655
|
+
electron_1.ipcMain.handle("chat:agent-options", (event, providerId = "codex") => chatScope(event).chats.options(providerId));
|
|
656
|
+
electron_1.ipcMain.handle("chat:stop", (event, id) => chatScope(event).chats.stop(id));
|
|
657
|
+
async function loadImportedShadow({ id, version, digest }) {
|
|
658
|
+
if (!/^[a-z0-9][a-z0-9-]{0,62}$/.test(id) || !/^[0-9][a-zA-Z0-9.-]{0,47}$/.test(version) || !/^[a-f0-9]{64}$/.test(digest)) {
|
|
659
|
+
throw new Error("Invalid Shadow package ID.");
|
|
660
|
+
}
|
|
661
|
+
const shadow = await (0, shadows_1.readShadow)((0, node_path_1.join)(dataDirectory, "shadows", id, version, digest));
|
|
662
|
+
if (shadow.id !== id || shadow.version !== version || shadow.digest !== digest)
|
|
663
|
+
throw new Error("The Shadow package contents changed.");
|
|
664
|
+
return shadow;
|
|
665
|
+
}
|
|
666
|
+
electron_1.ipcMain.handle("chat:send", async (event, id, message, model, shadowRef, agent, attachments = []) => {
|
|
667
|
+
if (!Array.isArray(attachments) || attachments.length > 100 || !attachments.every(id => typeof id === "string"))
|
|
668
|
+
return { error: "Invalid attachments." };
|
|
669
|
+
if (typeof message !== "string" || (!message.trim() && !attachments.length) || message.length > 4_000) {
|
|
670
|
+
return { error: "Enter a message or attach files. Messages can contain up to 4,000 characters." };
|
|
671
|
+
}
|
|
672
|
+
try {
|
|
673
|
+
const scope = chatScope(event);
|
|
674
|
+
if (!scope.workspace) {
|
|
675
|
+
if (removingShadow)
|
|
676
|
+
throw new Error("Finish reviewing the Shadow removal first.");
|
|
677
|
+
if (!(await ensureEditable()))
|
|
678
|
+
return { stopped: true };
|
|
679
|
+
}
|
|
680
|
+
const options = agent;
|
|
681
|
+
const selection = {
|
|
682
|
+
providerId: typeof options?.providerId === "string" ? options.providerId : "codex",
|
|
683
|
+
effort: typeof options?.effort === "string" && options.effort ? options.effort : undefined,
|
|
684
|
+
};
|
|
685
|
+
let shadow;
|
|
686
|
+
if (shadowRef !== undefined) {
|
|
687
|
+
if (scope.workspace)
|
|
688
|
+
throw new Error("Install Shadows from an app workspace.");
|
|
689
|
+
shadow = await loadImportedShadow(shadowRef);
|
|
690
|
+
}
|
|
691
|
+
const target = event.sender;
|
|
692
|
+
const send = () => scope.chats.send(id, message, typeof model === "string" && model ? model : undefined, chatEvent => { if (!target.isDestroyed())
|
|
693
|
+
target.send("chat:event", chatEvent); }, shadow, selection, attachments);
|
|
694
|
+
let result;
|
|
695
|
+
if (scope.workspace) {
|
|
696
|
+
target.send("agent:activity", true);
|
|
697
|
+
try {
|
|
698
|
+
result = await send();
|
|
699
|
+
}
|
|
700
|
+
finally {
|
|
701
|
+
if (!target.isDestroyed())
|
|
702
|
+
target.send("agent:activity", scope.chats.running);
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
else
|
|
706
|
+
result = await whileAgentActive(send);
|
|
707
|
+
return { stopped: result.stopped };
|
|
708
|
+
}
|
|
709
|
+
catch (error) {
|
|
710
|
+
return { error: error instanceof Error ? error.message : "The change could not be completed." };
|
|
711
|
+
}
|
|
712
|
+
});
|
|
713
|
+
let readyToClose = false;
|
|
714
|
+
window.on("close", (event) => {
|
|
715
|
+
closing = true;
|
|
716
|
+
livePreview.cancelUpdate();
|
|
717
|
+
void livePreview.resume().then(() => { if (!contents.isDestroyed())
|
|
718
|
+
contents.send("canvas:updating", true); });
|
|
719
|
+
if (readyToClose)
|
|
720
|
+
return;
|
|
721
|
+
event.preventDefault();
|
|
722
|
+
void Promise.allSettled([managed.stop(), runtimeTask]).finally(() => {
|
|
723
|
+
readyToClose = true;
|
|
724
|
+
if (!window.isDestroyed())
|
|
725
|
+
window.close();
|
|
726
|
+
});
|
|
727
|
+
});
|
|
728
|
+
window.once("closed", () => {
|
|
729
|
+
for (const editor of editors.values())
|
|
730
|
+
if (!editor.window.isDestroyed())
|
|
731
|
+
editor.window.destroy();
|
|
732
|
+
chats.close();
|
|
733
|
+
agents.close();
|
|
734
|
+
registry.close();
|
|
735
|
+
for (const channel of ["instance:current", "canvas:current", "runtime:state", "runtime:retry", "shadow:import", "shadow:list", "shadow:remove", "shadow:new", "shadow:open", "addon:invoke", "chat:target", "chat:list", "chat:new", "chat:open", "chat:providers", "chat:agent-options", "chat:send", "chat:stop", "agent:activity", "agent:setup", "agent:clear-access"]) {
|
|
736
|
+
electron_1.ipcMain.removeHandler(channel);
|
|
737
|
+
}
|
|
738
|
+
});
|
|
739
|
+
await window.loadFile(index);
|
|
740
|
+
if (!closing)
|
|
741
|
+
void syncRuntime();
|
|
742
|
+
}
|
|
743
|
+
async function start() {
|
|
744
|
+
const macBundle = electron_1.app.isPackaged && process.platform === "darwin";
|
|
745
|
+
const requestedRepository = process.env.MORKA_NPM_REPOSITORY?.trim();
|
|
746
|
+
const npmRepository = requestedRepository ? (0, node_path_1.dirname)(await (0, repository_1.repositoryProject)(requestedRepository)) : undefined;
|
|
747
|
+
const install = process.env.MORKA_INSTALL_APP;
|
|
748
|
+
if (install === "1" || install === "open") {
|
|
749
|
+
if (!npmRepository)
|
|
750
|
+
throw new Error("App installation requires a Morka project.");
|
|
751
|
+
const spec = (0, repository_1.readRelease)((0, repository_1.repositoryFile)(npmRepository, "release.json"));
|
|
752
|
+
if (process.platform === "darwin")
|
|
753
|
+
electron_1.app.setActivationPolicy("prohibited");
|
|
754
|
+
await electron_1.app.whenReady();
|
|
755
|
+
await (0, desktop_1.installDesktopApp)(npmRepository, spec, install === "open");
|
|
756
|
+
electron_1.app.exit(0);
|
|
757
|
+
return;
|
|
758
|
+
}
|
|
759
|
+
const metadata = macBundle ? (0, node_path_1.join)(process.resourcesPath, repository_1.releaseFile) : "";
|
|
760
|
+
const release = macBundle && (0, node_fs_1.statSync)(metadata, { throwIfNoEntry: false })?.isFile() ? (0, repository_1.readRelease)(metadata) : undefined;
|
|
761
|
+
const instance = release ? { ...release, projectDirectory: (0, node_path_1.join)(process.resourcesPath, repository_1.releaseProject) }
|
|
762
|
+
: npmRepository ? {
|
|
763
|
+
...(0, repository_1.readRelease)((0, repository_1.repositoryFile)(npmRepository, "release.json")),
|
|
764
|
+
repositoryDirectory: npmRepository,
|
|
765
|
+
projectDirectory: (0, node_path_1.join)(npmRepository, "app"),
|
|
766
|
+
} : undefined;
|
|
767
|
+
const localId = instance && npmRepository ? (0, repository_1.repositoryProfileId)(instance, npmRepository) : instance?.id;
|
|
768
|
+
const profile = localId ? (0, node_path_1.join)(release ? "releases" : "instances", localId) : "launcher";
|
|
769
|
+
const data = (0, node_path_1.join)((0, repository_1.appsDirectory)(electron_1.app.getPath("appData")), profile);
|
|
770
|
+
(0, node_fs_1.mkdirSync)(data, { recursive: true, mode: 0o700 });
|
|
771
|
+
electron_1.app.setPath("userData", data);
|
|
772
|
+
electron_1.app.setName(instance?.name ?? "Morka");
|
|
773
|
+
if (!electron_1.app.requestSingleInstanceLock()) {
|
|
774
|
+
electron_1.app.quit();
|
|
775
|
+
return;
|
|
776
|
+
}
|
|
777
|
+
electron_1.app.on("second-instance", () => {
|
|
778
|
+
const [window] = electron_1.BrowserWindow.getAllWindows();
|
|
779
|
+
if (!window)
|
|
780
|
+
return;
|
|
781
|
+
if (window.isMinimized())
|
|
782
|
+
window.restore();
|
|
783
|
+
window.focus();
|
|
784
|
+
});
|
|
785
|
+
electron_1.app.on("window-all-closed", () => electron_1.app.quit());
|
|
786
|
+
await electron_1.app.whenReady();
|
|
787
|
+
if (instance)
|
|
788
|
+
await createWindow(instance, release, Boolean(npmRepository && !release));
|
|
789
|
+
else {
|
|
790
|
+
electron_1.app.dock?.setIcon((0, node_path_1.join)(__dirname, "../branding/morka-icon.png"));
|
|
791
|
+
await createLauncher();
|
|
792
|
+
}
|
|
793
|
+
}
|
|
794
|
+
void start().catch(error => {
|
|
795
|
+
console.error(error);
|
|
796
|
+
if (process.env.MORKA_INSTALL_APP) {
|
|
797
|
+
electron_1.app.exit(1);
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
electron_1.dialog.showErrorBox(electron_1.app.name, error instanceof Error ? error.message : String(error));
|
|
801
|
+
electron_1.app.quit();
|
|
802
|
+
});
|