@cydm/magic-shell-agent-node 0.1.17 → 0.1.19
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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/local-direct-server.js +2 -21
- package/dist/node.js +32 -1
- package/dist/plugin-loader.js +8 -17
- package/dist/primary-agent-bridge.d.ts +0 -1
- package/dist/primary-agent-bridge.js +35 -5
- package/dist/primary-pie-extension/magic-shell-agent/index.js +1019 -0
- package/dist/primary-pie-extension/magic-shell-agent/package.json +9 -0
- package/dist/runtime-assets.d.ts +4 -0
- package/dist/runtime-assets.js +83 -0
- package/dist/workbench/index.html +30 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { createServer } from "node:http";
|
|
2
2
|
import { existsSync, createReadStream } from "node:fs";
|
|
3
3
|
import path from "node:path";
|
|
4
|
-
import { fileURLToPath } from "node:url";
|
|
5
4
|
import { WebSocketServer, WebSocket } from "ws";
|
|
5
|
+
import { getWorkbenchRoot } from "./runtime-assets.js";
|
|
6
6
|
function createConnectionId() {
|
|
7
7
|
return `local-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
8
8
|
}
|
|
@@ -21,25 +21,6 @@ function guessContentType(filePath) {
|
|
|
21
21
|
return "image/png";
|
|
22
22
|
return "text/plain; charset=utf-8";
|
|
23
23
|
}
|
|
24
|
-
function findWorkbenchRoot() {
|
|
25
|
-
const currentDir = path.dirname(fileURLToPath(import.meta.url));
|
|
26
|
-
const envOverride = process.env.MAGIC_SHELL_WORKBENCH_ROOT;
|
|
27
|
-
const candidates = [
|
|
28
|
-
envOverride ? path.resolve(envOverride) : null,
|
|
29
|
-
path.resolve(currentDir, "./workbench"),
|
|
30
|
-
path.resolve(currentDir, "../dist/workbench"),
|
|
31
|
-
path.resolve(currentDir, "../workbench"),
|
|
32
|
-
path.resolve(currentDir, "../../../apps/web/src"),
|
|
33
|
-
path.resolve(currentDir, "../../../../apps/web/src"),
|
|
34
|
-
path.resolve(process.cwd(), "apps/web/src"),
|
|
35
|
-
].filter((value) => Boolean(value));
|
|
36
|
-
for (const candidate of candidates) {
|
|
37
|
-
if (existsSync(path.join(candidate, "index.html"))) {
|
|
38
|
-
return candidate;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
24
|
export class LocalDirectServer {
|
|
44
25
|
options;
|
|
45
26
|
delegate;
|
|
@@ -47,7 +28,7 @@ export class LocalDirectServer {
|
|
|
47
28
|
wsServer = new WebSocketServer({ noServer: true });
|
|
48
29
|
connections = new Map();
|
|
49
30
|
sessionToConnections = new Map();
|
|
50
|
-
workbenchRoot =
|
|
31
|
+
workbenchRoot = getWorkbenchRoot();
|
|
51
32
|
constructor(options, delegate) {
|
|
52
33
|
this.options = options;
|
|
53
34
|
this.delegate = delegate;
|
package/dist/node.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
1
2
|
import { WebSocketClient } from "./ws-client.js";
|
|
2
3
|
import { SessionManager } from "./session-manager.js";
|
|
3
4
|
import { LocalDirectServer } from "./local-direct-server.js";
|
|
@@ -16,9 +17,10 @@ import { runClaudeExec } from "./claude-exec.js";
|
|
|
16
17
|
import { codexNeedsTrustConfirmation, getLastCodexWorkerMessage, isCodexReadyForTask, isCodexWorker, summarizeCodexWorkerOutput, } from "./codex-worker.js";
|
|
17
18
|
import { runCodexExec } from "./codex-exec.js";
|
|
18
19
|
import { extractTerminalMetadata, normalizeWorkerTitleCandidate, } from "./terminal-metadata.js";
|
|
19
|
-
import { existsSync } from "node:fs";
|
|
20
|
+
import { chmodSync, existsSync, readdirSync } from "node:fs";
|
|
20
21
|
import os from "node:os";
|
|
21
22
|
import path from "node:path";
|
|
23
|
+
const require = createRequire(import.meta.url);
|
|
22
24
|
function generateSessionId() {
|
|
23
25
|
return `session-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
24
26
|
}
|
|
@@ -84,6 +86,7 @@ export class AgentNode {
|
|
|
84
86
|
console.log(`[AgentNode] Starting...`);
|
|
85
87
|
console.log(`[AgentNode] Node ID: ${this.options.nodeId}`);
|
|
86
88
|
console.log(`[AgentNode] Relay: ${this.options.relayUrl}`);
|
|
89
|
+
ensureSpawnHelperExecutable();
|
|
87
90
|
// 1. 加载插件
|
|
88
91
|
this.plugins = loadPlugins({ dir: this.options.pluginDir });
|
|
89
92
|
if (this.plugins.size === 0) {
|
|
@@ -1933,6 +1936,34 @@ Environment Variables:
|
|
|
1933
1936
|
MAGIC_SHELL_DISABLE_LOCAL_DIRECT Set to 1 to disable the local direct server
|
|
1934
1937
|
`);
|
|
1935
1938
|
}
|
|
1939
|
+
function ensureSpawnHelperExecutable() {
|
|
1940
|
+
try {
|
|
1941
|
+
const pkgPath = require.resolve("node-pty/package.json");
|
|
1942
|
+
const prebuildRoot = path.join(path.dirname(pkgPath), "prebuilds");
|
|
1943
|
+
if (!existsSync(prebuildRoot))
|
|
1944
|
+
return;
|
|
1945
|
+
for (const entry of walk(prebuildRoot)) {
|
|
1946
|
+
if (path.basename(entry) === "spawn-helper") {
|
|
1947
|
+
chmodSync(entry, 0o755);
|
|
1948
|
+
}
|
|
1949
|
+
}
|
|
1950
|
+
}
|
|
1951
|
+
catch {
|
|
1952
|
+
// Ignore best-effort fixups during startup.
|
|
1953
|
+
}
|
|
1954
|
+
}
|
|
1955
|
+
function walk(dir) {
|
|
1956
|
+
const files = [];
|
|
1957
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
1958
|
+
const fullPath = path.join(dir, entry.name);
|
|
1959
|
+
if (entry.isDirectory()) {
|
|
1960
|
+
files.push(...walk(fullPath));
|
|
1961
|
+
continue;
|
|
1962
|
+
}
|
|
1963
|
+
files.push(fullPath);
|
|
1964
|
+
}
|
|
1965
|
+
return files;
|
|
1966
|
+
}
|
|
1936
1967
|
// 主函数
|
|
1937
1968
|
async function main() {
|
|
1938
1969
|
const options = parseArgs();
|
package/dist/plugin-loader.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { readFileSync, readdirSync, existsSync } from "fs";
|
|
2
2
|
import { join, extname, dirname, isAbsolute, resolve } from "path";
|
|
3
|
-
import { fileURLToPath } from "url";
|
|
4
3
|
import { createRequire } from "module";
|
|
4
|
+
import { getPackagedPluginDir } from "./runtime-assets.js";
|
|
5
5
|
const require = createRequire(import.meta.url);
|
|
6
6
|
/**
|
|
7
7
|
* 加载单个插件配置
|
|
@@ -39,9 +39,6 @@ export function loadPlugin(path) {
|
|
|
39
39
|
});
|
|
40
40
|
}
|
|
41
41
|
function normalizePluginConfigForRuntime(config) {
|
|
42
|
-
if (process.platform !== "win32") {
|
|
43
|
-
return config;
|
|
44
|
-
}
|
|
45
42
|
if (config.name === "pie") {
|
|
46
43
|
const pieEntry = resolvePackageBinEntry("@cydm/pie", "pie");
|
|
47
44
|
if (pieEntry) {
|
|
@@ -52,6 +49,9 @@ function normalizePluginConfigForRuntime(config) {
|
|
|
52
49
|
};
|
|
53
50
|
}
|
|
54
51
|
}
|
|
52
|
+
if (process.platform !== "win32") {
|
|
53
|
+
return config;
|
|
54
|
+
}
|
|
55
55
|
if ((config.name === "claude-code" || config.name === "codex") && isShellCommand(config.command)) {
|
|
56
56
|
return {
|
|
57
57
|
...config,
|
|
@@ -174,18 +174,9 @@ export function loadPlugins(options) {
|
|
|
174
174
|
* 获取默认插件目录
|
|
175
175
|
*/
|
|
176
176
|
export function getDefaultPluginDir() {
|
|
177
|
-
|
|
178
|
-
if (
|
|
179
|
-
return
|
|
180
|
-
}
|
|
181
|
-
const packagePluginDir = resolve(dirname(fileURLToPath(import.meta.url)), "..", "plugins");
|
|
182
|
-
if (existsSync(packagePluginDir)) {
|
|
183
|
-
return packagePluginDir;
|
|
184
|
-
}
|
|
185
|
-
const distPluginDir = resolve(dirname(fileURLToPath(import.meta.url)), "plugins");
|
|
186
|
-
if (existsSync(distPluginDir)) {
|
|
187
|
-
return distPluginDir;
|
|
177
|
+
const pluginDir = getPackagedPluginDir();
|
|
178
|
+
if (pluginDir) {
|
|
179
|
+
return pluginDir;
|
|
188
180
|
}
|
|
189
|
-
|
|
190
|
-
return join(process.cwd(), "plugins");
|
|
181
|
+
throw new Error("Magic Shell plugin assets are unavailable. Set MAGIC_SHELL_PLUGINS_DIR or run from a built source checkout.");
|
|
191
182
|
}
|
|
@@ -55,7 +55,6 @@ export interface PrimarySessionSnapshot {
|
|
|
55
55
|
lastMessageRole?: string;
|
|
56
56
|
lastAssistantHasToolCall?: boolean;
|
|
57
57
|
}
|
|
58
|
-
export declare function getPrimaryPieExtensionDistDir(): string;
|
|
59
58
|
export declare function withPrimaryPieExtensionPath(plugin: PluginConfig): PluginConfig;
|
|
60
59
|
export declare function buildPrimaryPrompt(text: string, context: PrimaryAgentPromptContext): string;
|
|
61
60
|
export declare function buildPrimarySessionPrompt(text: string, context: PrimarySessionPromptContext): string;
|
|
@@ -2,20 +2,50 @@ import { spawn } from "node:child_process";
|
|
|
2
2
|
import fs from "node:fs";
|
|
3
3
|
import os from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
const
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
import { getMagicShellCliPath, getPrimaryPieExtensionDistDir } from "./runtime-assets.js";
|
|
6
|
+
function getPrimaryPieExtensionPackageDir(extensionRoot) {
|
|
7
|
+
const packagedDir = path.join(extensionRoot, "magic-shell-agent");
|
|
8
|
+
if (fs.existsSync(path.join(packagedDir, "package.json"))) {
|
|
9
|
+
return packagedDir;
|
|
10
|
+
}
|
|
11
|
+
if (fs.existsSync(path.join(extensionRoot, "package.json"))) {
|
|
12
|
+
return extensionRoot;
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
function ensurePieExtensionDiscovery(extensionRoot) {
|
|
17
|
+
const extensionPackageDir = getPrimaryPieExtensionPackageDir(extensionRoot);
|
|
18
|
+
if (!extensionPackageDir) {
|
|
19
|
+
throw new Error(`Primary PIE extension package is missing under ${extensionRoot}`);
|
|
20
|
+
}
|
|
21
|
+
const targetDir = path.join(os.homedir(), ".pie", "extensions", path.basename(extensionPackageDir));
|
|
22
|
+
fs.rmSync(targetDir, { recursive: true, force: true });
|
|
23
|
+
fs.mkdirSync(path.dirname(targetDir), { recursive: true });
|
|
24
|
+
fs.cpSync(extensionPackageDir, targetDir, { recursive: true });
|
|
25
|
+
return targetDir;
|
|
10
26
|
}
|
|
11
27
|
export function withPrimaryPieExtensionPath(plugin) {
|
|
12
28
|
if (plugin.name !== "pie") {
|
|
13
29
|
return plugin;
|
|
14
30
|
}
|
|
15
31
|
const extensionDir = getPrimaryPieExtensionDistDir();
|
|
32
|
+
if (!extensionDir) {
|
|
33
|
+
throw new Error("Primary PIE extension assets are unavailable. Use the packaged @cydm/magic-shell runtime or build packages/primary-pie-extension first.");
|
|
34
|
+
}
|
|
35
|
+
const cliPath = getMagicShellCliPath();
|
|
36
|
+
if (!cliPath) {
|
|
37
|
+
throw new Error("Magic Shell CLI runtime is unavailable for the primary PIE extension. Start the node via `magic-shell node start` or set MAGIC_SHELL_CLI_PATH.");
|
|
38
|
+
}
|
|
39
|
+
const installedExtensionDir = ensurePieExtensionDiscovery(extensionDir);
|
|
16
40
|
return {
|
|
17
41
|
...plugin,
|
|
18
42
|
args: [...(plugin.args || []), "--extension-path", extensionDir],
|
|
43
|
+
env: {
|
|
44
|
+
...(plugin.env || {}),
|
|
45
|
+
MAGIC_SHELL_CLI_PATH: cliPath,
|
|
46
|
+
MAGIC_SHELL_PRIMARY_EXTENSION_DIR: extensionDir,
|
|
47
|
+
MAGIC_SHELL_PRIMARY_EXTENSION_PACKAGE_DIR: installedExtensionDir,
|
|
48
|
+
},
|
|
19
49
|
};
|
|
20
50
|
}
|
|
21
51
|
export function buildPrimaryPrompt(text, context) {
|