@cmdoss/agent-pass-plugin 0.0.0-stage → 0.1.0
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/README.md +131 -2
- package/dist/agentpass.js +199 -0
- package/dist/cli.js +37 -0
- package/dist/domain.js +103 -0
- package/dist/engine.js +260 -0
- package/dist/http.js +170 -0
- package/dist/mcp.js +259 -0
- package/dist/onboarding.js +258 -0
- package/dist/public/catalog.js +35 -0
- package/dist/public/http.js +89 -0
- package/dist/public-server.js +11 -0
- package/dist/runtime/config.js +16 -0
- package/dist/runtime/installation.js +107 -0
- package/dist/runtime/lock.js +38 -0
- package/dist/runtime/paths.js +56 -0
- package/dist/runtime/setup.js +155 -0
- package/dist/sdk-ui.js +48 -0
- package/dist/server.js +107 -0
- package/dist/shared/constants.js +9 -0
- package/dist/store.js +84 -0
- package/dist/testnet/adapter.js +275 -0
- package/dist/testnet/authorization.js +620 -0
- package/dist/testnet/chain/codec.js +68 -0
- package/dist/testnet/chain/inspection.js +54 -0
- package/dist/testnet/chain/policy.js +78 -0
- package/dist/testnet/chain/prepared.js +150 -0
- package/dist/testnet/chain/receipt.js +49 -0
- package/dist/testnet/chain/sdk.js +34 -0
- package/dist/testnet/chain/types.js +1 -0
- package/dist/testnet/engine.js +395 -0
- package/dist/testnet/http.js +139 -0
- package/dist/testnet/model.js +49 -0
- package/dist/testnet/policy.js +44 -0
- package/dist/testnet/read-model.js +217 -0
- package/dist/testnet/repository.js +62 -0
- package/dist/testnet/runner.js +306 -0
- package/dist/testnet/types.js +1 -0
- package/dist/workflows.js +379 -0
- package/docs/agent-pass-integration.md +75 -0
- package/docs/architecture.md +93 -0
- package/docs/npm-release.md +83 -0
- package/npm-shrinkwrap.json +5376 -0
- package/package.json +72 -4
- package/public/assets/agent-pass-plugin-client.js +16 -0
- package/public/assets/agent-pass-plugin-sdk.js +1 -0
- package/public/assets/sui-payment-rails.avif +0 -0
- package/public/assets/sui-payments-hero.json +1 -0
- package/public/fonts/TWKEverett-Medium.woff2 +0 -0
- package/public/fonts/TWKEverett-Regular.woff2 +0 -0
- package/public/fonts/TWKEverettMono-Medium.woff2 +0 -0
- package/public/fonts/TWKEverettMono-Regular.woff2 +0 -0
- package/public/index.html +81 -0
- package/public/styles/base.css +117 -0
- package/public/styles/catalog.css +279 -0
- package/public/styles/controls.css +118 -0
- package/public/styles/dialog.css +257 -0
- package/public/styles/home.css +82 -0
- package/public/styles/layout.css +45 -0
- package/public/styles/plans.css +271 -0
- package/public/styles/responsive.css +643 -0
- package/public/styles/review.css +150 -0
- package/public/styles/sdk-wallet.css +125 -0
- package/public/styles/sdk.css +441 -0
- package/public/styles/shell.css +348 -0
- package/public/styles/tokens.css +75 -0
- package/public/styles.css +11 -0
- package/scripts/setup.mjs +10 -0
- package/skills/agent-pass-plugin-trading/SKILL.md +60 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdir, mkdtemp, readFile, rename, rm, writeFile, } from "node:fs/promises";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { dirname, isAbsolute, join, relative, resolve } from "node:path";
|
|
5
|
+
import { installationDirectory, isCheckout, packageMetadata, packageRoot, payloadFingerprint, } from "./paths.js";
|
|
6
|
+
function npm(args, cwd) {
|
|
7
|
+
const executable = process.env.npm_execpath;
|
|
8
|
+
const script = [
|
|
9
|
+
...(executable ? [join(dirname(executable), "npm-cli.js")] : []),
|
|
10
|
+
join(dirname(process.execPath), "node_modules/npm/bin/npm-cli.js"),
|
|
11
|
+
join(dirname(process.execPath), "../lib/node_modules/npm/bin/npm-cli.js"),
|
|
12
|
+
].find((candidate) => existsSync(candidate));
|
|
13
|
+
if (!script && process.platform === "win32")
|
|
14
|
+
throw new Error("Cannot locate npm-cli.js. Install npm alongside Node before setup.");
|
|
15
|
+
const result = spawnSync(script ? process.execPath : "npm", script ? [script, ...args] : args, {
|
|
16
|
+
cwd,
|
|
17
|
+
env: {
|
|
18
|
+
...process.env,
|
|
19
|
+
npm_config_audit: "false",
|
|
20
|
+
npm_config_fund: "false",
|
|
21
|
+
},
|
|
22
|
+
encoding: "utf8",
|
|
23
|
+
maxBuffer: 8 * 1024 * 1024,
|
|
24
|
+
});
|
|
25
|
+
if (result.error || result.status !== 0) {
|
|
26
|
+
const code = result.stderr?.match(/\bcode (E[A-Z_0-9]+)\b/)?.[1];
|
|
27
|
+
throw new Error(`Preparing the durable runtime failed${code ? ` (${code})` : ""}. Check npm availability, registry access and local disk permissions, then retry. No client was registered.`);
|
|
28
|
+
}
|
|
29
|
+
return result.stdout;
|
|
30
|
+
}
|
|
31
|
+
/** Install the exact local payload durably so npx cache cleanup cannot break MCP. */
|
|
32
|
+
export async function ensureRuntime(root = packageRoot, env = process.env) {
|
|
33
|
+
if (isCheckout(root))
|
|
34
|
+
return root;
|
|
35
|
+
const { name, version } = packageMetadata(root);
|
|
36
|
+
const fingerprint = payloadFingerprint(root);
|
|
37
|
+
const home = installationDirectory(env);
|
|
38
|
+
let ancestor = resolve(root);
|
|
39
|
+
const belowHome = (path) => {
|
|
40
|
+
const value = relative(home, path);
|
|
41
|
+
return value !== "" && !value.startsWith("..") && !isAbsolute(value);
|
|
42
|
+
};
|
|
43
|
+
while (ancestor !== dirname(ancestor) && belowHome(ancestor)) {
|
|
44
|
+
try {
|
|
45
|
+
const marker = JSON.parse(await readFile(join(ancestor, "installation.json"), "utf8"));
|
|
46
|
+
if (marker.name === name &&
|
|
47
|
+
marker.version === version &&
|
|
48
|
+
marker.fingerprint === fingerprint)
|
|
49
|
+
return root;
|
|
50
|
+
}
|
|
51
|
+
catch { }
|
|
52
|
+
ancestor = dirname(ancestor);
|
|
53
|
+
}
|
|
54
|
+
const destination = join(home, `${version.replace(/[^A-Za-z0-9._-]/g, "_")}-${fingerprint.slice(0, 20)}`);
|
|
55
|
+
const installed = join(destination, "node_modules", ...name.split("/"));
|
|
56
|
+
const complete = async () => {
|
|
57
|
+
try {
|
|
58
|
+
const marker = JSON.parse(await readFile(join(destination, "installation.json"), "utf8"));
|
|
59
|
+
return (marker.sourceFingerprint === fingerprint &&
|
|
60
|
+
marker.name === name &&
|
|
61
|
+
marker.version === version &&
|
|
62
|
+
existsSync(join(installed, "dist/cli.js")) &&
|
|
63
|
+
marker.fingerprint === payloadFingerprint(installed));
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
if (await complete())
|
|
70
|
+
return installed;
|
|
71
|
+
await mkdir(home, { recursive: true, mode: 0o700 });
|
|
72
|
+
const staging = await mkdtemp(join(home, ".prepare-"));
|
|
73
|
+
try {
|
|
74
|
+
const packed = JSON.parse(npm(["pack", "--ignore-scripts", "--json", "--pack-destination", staging], root));
|
|
75
|
+
const tarball = join(staging, packed[0].filename);
|
|
76
|
+
npm([
|
|
77
|
+
"install",
|
|
78
|
+
"--prefix",
|
|
79
|
+
staging,
|
|
80
|
+
"--omit=dev",
|
|
81
|
+
"--ignore-scripts",
|
|
82
|
+
"--no-save",
|
|
83
|
+
"--package-lock=false",
|
|
84
|
+
tarball,
|
|
85
|
+
], root);
|
|
86
|
+
await rm(tarball);
|
|
87
|
+
const stagedRoot = join(staging, "node_modules", ...name.split("/"));
|
|
88
|
+
await writeFile(join(staging, "installation.json"), JSON.stringify({
|
|
89
|
+
name,
|
|
90
|
+
version,
|
|
91
|
+
sourceFingerprint: fingerprint,
|
|
92
|
+
fingerprint: payloadFingerprint(stagedRoot),
|
|
93
|
+
}) + "\n", { mode: 0o600 });
|
|
94
|
+
try {
|
|
95
|
+
await rename(staging, destination);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
// Another installer may have completed the exact immutable payload first.
|
|
99
|
+
if (!(await complete()))
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
return installed;
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
await rm(staging, { recursive: true, force: true });
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { closeSync, openSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
2
|
+
/** Never remove a live/stale lock implicitly or release another process's lock. */
|
|
3
|
+
export function acquireRunnerLock(file, pid = process.pid) {
|
|
4
|
+
try {
|
|
5
|
+
const descriptor = openSync(file, "wx", 0o600);
|
|
6
|
+
try {
|
|
7
|
+
writeFileSync(descriptor, String(pid));
|
|
8
|
+
}
|
|
9
|
+
finally {
|
|
10
|
+
closeSync(descriptor);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error.code !== "EEXIST")
|
|
15
|
+
throw error;
|
|
16
|
+
const owner = Number(readFileSync(file, "utf8"));
|
|
17
|
+
if (!Number.isInteger(owner) || owner <= 0)
|
|
18
|
+
throw new Error("Invalid runner lock; inspect it before removing.");
|
|
19
|
+
try {
|
|
20
|
+
process.kill(owner, 0);
|
|
21
|
+
}
|
|
22
|
+
catch (cause) {
|
|
23
|
+
if (cause.code === "ESRCH")
|
|
24
|
+
throw new Error("Stale runner.lock found. Confirm no runner is active, then remove this lock manually before restarting.");
|
|
25
|
+
throw cause;
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`A runner already owns this data directory (PID ${owner}).`);
|
|
28
|
+
}
|
|
29
|
+
let released = false;
|
|
30
|
+
return () => {
|
|
31
|
+
if (released)
|
|
32
|
+
return;
|
|
33
|
+
if (readFileSync(file, "utf8") !== String(pid))
|
|
34
|
+
throw new Error("Runner lock ownership changed; refusing to remove it.");
|
|
35
|
+
unlinkSync(file);
|
|
36
|
+
released = true;
|
|
37
|
+
};
|
|
38
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readFileSync, readdirSync } from "node:fs";
|
|
3
|
+
import { homedir } from "node:os";
|
|
4
|
+
import { join, resolve } from "node:path";
|
|
5
|
+
import { fileURLToPath } from "node:url";
|
|
6
|
+
export const packageRoot = fileURLToPath(new URL("../../", import.meta.url));
|
|
7
|
+
export const packageMetadata = (root = packageRoot) => JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
8
|
+
export const isCheckout = (root = packageRoot) => existsSync(join(root, "src/server.ts")) &&
|
|
9
|
+
existsSync(join(root, "tsconfig.json"));
|
|
10
|
+
export function dataDirectory(env, cwd, root = packageRoot) {
|
|
11
|
+
return resolve(cwd, env.AGENT_PASS_PLUGIN_DATA_DIR ??
|
|
12
|
+
(isCheckout(root)
|
|
13
|
+
? ".data"
|
|
14
|
+
: join(homedir(), ".agent-pass-plugin", "data")));
|
|
15
|
+
}
|
|
16
|
+
export function installationDirectory(env = process.env) {
|
|
17
|
+
return resolve(env.AGENT_PASS_PLUGIN_INSTALL_DIR ??
|
|
18
|
+
join(homedir(), ".agent-pass-plugin", "runtimes"));
|
|
19
|
+
}
|
|
20
|
+
/** Hash only distributable payload; local signer data and dependencies are never read. */
|
|
21
|
+
export function payloadFingerprint(root = packageRoot) {
|
|
22
|
+
const hash = createHash("sha256");
|
|
23
|
+
function visit(relative) {
|
|
24
|
+
const absolute = join(root, relative);
|
|
25
|
+
if (!existsSync(absolute))
|
|
26
|
+
return;
|
|
27
|
+
const names = readdirSync(absolute, { withFileTypes: true }).sort((a, b) => a.name.localeCompare(b.name));
|
|
28
|
+
for (const entry of names) {
|
|
29
|
+
const child = `${relative}/${entry.name}`;
|
|
30
|
+
if (entry.isSymbolicLink())
|
|
31
|
+
throw new Error("Distributable payload cannot contain symbolic links.");
|
|
32
|
+
if (entry.isDirectory())
|
|
33
|
+
visit(child);
|
|
34
|
+
else if (entry.isFile())
|
|
35
|
+
hash
|
|
36
|
+
.update(child)
|
|
37
|
+
.update("\0")
|
|
38
|
+
.update(readFileSync(join(root, child)))
|
|
39
|
+
.update("\0");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
hash.update(readFileSync(join(root, "package.json")));
|
|
43
|
+
const shrinkwrap = join(root, "npm-shrinkwrap.json");
|
|
44
|
+
if (existsSync(shrinkwrap))
|
|
45
|
+
hash.update(readFileSync(shrinkwrap));
|
|
46
|
+
for (const path of ["dist", "public", "skills"])
|
|
47
|
+
visit(path);
|
|
48
|
+
return hash.digest("hex");
|
|
49
|
+
}
|
|
50
|
+
export function workspaceIdentity(directory, root = packageRoot) {
|
|
51
|
+
return createHash("sha256")
|
|
52
|
+
.update(isCheckout(root)
|
|
53
|
+
? `${resolve(root)}|${resolve(directory)}`
|
|
54
|
+
: `agent-pass-plugin|${resolve(directory)}`)
|
|
55
|
+
.digest("hex");
|
|
56
|
+
}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { spawn, spawnSync } from "node:child_process";
|
|
2
|
+
import { mkdir, open, writeFile } from "node:fs/promises";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import { readRuntimeConfig } from "./config.js";
|
|
5
|
+
import { ensureRuntime } from "./installation.js";
|
|
6
|
+
import { packageMetadata, packageRoot, payloadFingerprint, workspaceIdentity, } from "./paths.js";
|
|
7
|
+
export async function setup(args) {
|
|
8
|
+
const allowed = new Set(["--client", "--register", "--start"]);
|
|
9
|
+
for (let i = 0; i < args.length; i++) {
|
|
10
|
+
if (!allowed.has(args[i]))
|
|
11
|
+
throw new Error(`Unknown setup option: ${args[i]}`);
|
|
12
|
+
if (args[i] === "--client")
|
|
13
|
+
i++;
|
|
14
|
+
}
|
|
15
|
+
const client = args[args.indexOf("--client") + 1];
|
|
16
|
+
if (!["codex", "claude"].includes(client ?? ""))
|
|
17
|
+
throw new Error("Select --client codex or --client claude.");
|
|
18
|
+
const setupEnv = { ...process.env };
|
|
19
|
+
if (setupEnv.AGENT_PASS_PLUGIN_DATA_DIR)
|
|
20
|
+
setupEnv.AGENT_PASS_PLUGIN_DATA_DIR = resolve(setupEnv.AGENT_PASS_PLUGIN_DATA_DIR);
|
|
21
|
+
const { port, mode, directory, origin } = readRuntimeConfig(setupEnv, packageRoot);
|
|
22
|
+
const root = await ensureRuntime();
|
|
23
|
+
const version = packageMetadata(root).version;
|
|
24
|
+
const buildId = payloadFingerprint(root);
|
|
25
|
+
const workspaceId = workspaceIdentity(directory, root);
|
|
26
|
+
const config = {
|
|
27
|
+
command: process.execPath,
|
|
28
|
+
args: [join(root, "dist/mcp.js")],
|
|
29
|
+
env: { AGENT_PASS_PLUGIN_LOCAL_URL: origin },
|
|
30
|
+
};
|
|
31
|
+
async function health() {
|
|
32
|
+
let response;
|
|
33
|
+
try {
|
|
34
|
+
response = await fetch(`${origin}/api/health`, {
|
|
35
|
+
signal: AbortSignal.timeout(1000),
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
let parsed;
|
|
42
|
+
try {
|
|
43
|
+
parsed = await response.json();
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
49
|
+
return false;
|
|
50
|
+
const value = parsed;
|
|
51
|
+
if (!response.ok ||
|
|
52
|
+
value.product !== "agent-pass-plugin" ||
|
|
53
|
+
value.apiVersion !== 1)
|
|
54
|
+
return false;
|
|
55
|
+
if (value.workspaceId !== workspaceId)
|
|
56
|
+
throw new Error("This port belongs to a different Agent Pass Plugin workspace. Choose another PORT or connect to the intended data directory.");
|
|
57
|
+
if (value.mode !== mode)
|
|
58
|
+
throw new Error(`This runtime uses ${value.mode}; expected ${mode}. Restart it explicitly in the intended mode.`);
|
|
59
|
+
if (value.runtimeVersion !== version || value.runtimeBuildId !== buildId)
|
|
60
|
+
throw new Error("A different runtime version is already running. Stop it explicitly before upgrading; running plans and data were not changed.");
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
64
|
+
let runnerReady = await health();
|
|
65
|
+
if (args.includes("--start") && !runnerReady) {
|
|
66
|
+
const log = await open(join(directory, "runtime.log"), "a", 0o600);
|
|
67
|
+
try {
|
|
68
|
+
const child = spawn(process.execPath, [join(root, "dist/server.js")], {
|
|
69
|
+
cwd: root,
|
|
70
|
+
env: {
|
|
71
|
+
...process.env,
|
|
72
|
+
PORT: String(port),
|
|
73
|
+
AGENT_PASS_PLUGIN_MODE: mode,
|
|
74
|
+
AGENT_PASS_PLUGIN_DATA_DIR: directory,
|
|
75
|
+
},
|
|
76
|
+
detached: true,
|
|
77
|
+
stdio: ["ignore", log.fd, log.fd],
|
|
78
|
+
});
|
|
79
|
+
const started = new Promise((resolve, reject) => {
|
|
80
|
+
child.once("spawn", resolve);
|
|
81
|
+
child.once("error", reject);
|
|
82
|
+
});
|
|
83
|
+
child.unref();
|
|
84
|
+
await started;
|
|
85
|
+
}
|
|
86
|
+
finally {
|
|
87
|
+
await log.close();
|
|
88
|
+
}
|
|
89
|
+
for (let i = 0; i < 50; i++) {
|
|
90
|
+
if (await health()) {
|
|
91
|
+
runnerReady = true;
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
95
|
+
}
|
|
96
|
+
if (!runnerReady)
|
|
97
|
+
throw new Error("Runner did not become ready. Inspect local runtime.log and any existing runner lock; do not launch a second instance blindly.");
|
|
98
|
+
}
|
|
99
|
+
let registered = false;
|
|
100
|
+
if (args.includes("--register")) {
|
|
101
|
+
const cliArgs = client === "codex"
|
|
102
|
+
? [
|
|
103
|
+
"mcp",
|
|
104
|
+
"add",
|
|
105
|
+
"agent-pass-plugin",
|
|
106
|
+
"--env",
|
|
107
|
+
`AGENT_PASS_PLUGIN_LOCAL_URL=${origin}`,
|
|
108
|
+
"--",
|
|
109
|
+
config.command,
|
|
110
|
+
...config.args,
|
|
111
|
+
]
|
|
112
|
+
: [
|
|
113
|
+
"mcp",
|
|
114
|
+
"add",
|
|
115
|
+
"--transport",
|
|
116
|
+
"stdio",
|
|
117
|
+
"--env",
|
|
118
|
+
`AGENT_PASS_PLUGIN_LOCAL_URL=${origin}`,
|
|
119
|
+
"agent-pass-plugin",
|
|
120
|
+
"--",
|
|
121
|
+
config.command,
|
|
122
|
+
...config.args,
|
|
123
|
+
];
|
|
124
|
+
const result = spawnSync(client, cliArgs, { cwd: root, stdio: "inherit" });
|
|
125
|
+
if (result.error)
|
|
126
|
+
throw new Error(`${client} CLI unavailable. Install/open the intended client first, then retry setup.`);
|
|
127
|
+
if (result.status !== 0)
|
|
128
|
+
throw new Error("MCP registration did not complete. No success has been recorded.");
|
|
129
|
+
registered = true;
|
|
130
|
+
}
|
|
131
|
+
const metadata = {
|
|
132
|
+
product: "Agent Pass Plugin",
|
|
133
|
+
version: 1,
|
|
134
|
+
workspaceId,
|
|
135
|
+
runtimeVersion: version,
|
|
136
|
+
runtimeBuildId: buildId,
|
|
137
|
+
mode,
|
|
138
|
+
client,
|
|
139
|
+
registered,
|
|
140
|
+
runnerReady,
|
|
141
|
+
onboarding: {
|
|
142
|
+
required: mode === "testnet",
|
|
143
|
+
nextTool: mode === "testnet" ? "onboard" : "create_account",
|
|
144
|
+
completionVerified: false,
|
|
145
|
+
},
|
|
146
|
+
root,
|
|
147
|
+
localUrl: origin,
|
|
148
|
+
skillPath: join(root, "skills/agent-pass-plugin-trading/SKILL.md"),
|
|
149
|
+
mcpConfig: { mcpServers: { "agent-pass-plugin": config } },
|
|
150
|
+
};
|
|
151
|
+
await writeFile(join(directory, "setup.json"), JSON.stringify(metadata, null, 2) + "\n", { mode: 0o600 });
|
|
152
|
+
console.log(JSON.stringify(metadata, null, 2));
|
|
153
|
+
if (registered)
|
|
154
|
+
console.log("Open a new client session if tools are not visible. Verify marketplace_status, then call onboard in Testnet. Report onboarding complete only after onboarding_status confirms onboardingComplete:true.");
|
|
155
|
+
}
|
package/dist/sdk-ui.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import fastifyStatic from "@fastify/static";
|
|
2
|
+
import Fastify from "fastify";
|
|
3
|
+
import { readFile } from "node:fs/promises";
|
|
4
|
+
import { createRequire } from "node:module";
|
|
5
|
+
import { dirname, join } from "node:path";
|
|
6
|
+
import { fileURLToPath } from "node:url";
|
|
7
|
+
import { SDK_UI_PORT } from "./shared/constants.js";
|
|
8
|
+
export { SDK_UI_ORIGIN, SDK_UI_PORT } from "./shared/constants.js";
|
|
9
|
+
/** Official SDK UI with shared presentation assets; no signer/session API. */
|
|
10
|
+
export async function buildSdkUi() {
|
|
11
|
+
const app = Fastify({ logger: false });
|
|
12
|
+
const require = createRequire(import.meta.url);
|
|
13
|
+
const sdk = dirname(dirname(require.resolve("@mysten-incubation/sui-agent-pass/agent")));
|
|
14
|
+
const publicRoot = fileURLToPath(new URL("../public/", import.meta.url));
|
|
15
|
+
app.addHook("onRequest", async (req, reply) => {
|
|
16
|
+
if (req.headers.host !== `127.0.0.1:${SDK_UI_PORT}`)
|
|
17
|
+
return reply.code(403).send({ error: "Unexpected Host" });
|
|
18
|
+
reply.header("Cache-Control", "no-store");
|
|
19
|
+
reply.header("X-Content-Type-Options", "nosniff");
|
|
20
|
+
reply.header("Referrer-Policy", "no-referrer");
|
|
21
|
+
});
|
|
22
|
+
app.get("/", async (_req, reply) => {
|
|
23
|
+
let html = await readFile(join(sdk, "web-dist/index.html"), "utf8");
|
|
24
|
+
html = html
|
|
25
|
+
.replace(/<link rel="icon"[^>]*>/, "")
|
|
26
|
+
.replace("<title>Sui Agent Pass · Your money. Your rules.</title>", "<title>Agent Pass Plugin · Agent Pass</title>")
|
|
27
|
+
.replace("</head>", `<link rel="stylesheet" href="/plugin/styles/tokens.css"><link rel="stylesheet" href="/plugin/styles.css"><link rel="stylesheet" href="/plugin/styles/sdk.css"><script type="module" src="/assets/agent-pass-plugin-sdk.js"></script></head>`);
|
|
28
|
+
return reply.type("text/html").send(html);
|
|
29
|
+
});
|
|
30
|
+
app.get("/assets/agent-pass-plugin-sdk.js", async (_req, reply) => reply
|
|
31
|
+
.type("application/javascript")
|
|
32
|
+
.send(await readFile(join(publicRoot, "assets/agent-pass-plugin-sdk.js"))));
|
|
33
|
+
await app.register(fastifyStatic, {
|
|
34
|
+
root: join(sdk, "web-dist"),
|
|
35
|
+
index: false,
|
|
36
|
+
});
|
|
37
|
+
await app.register(fastifyStatic, {
|
|
38
|
+
root: publicRoot,
|
|
39
|
+
prefix: "/plugin/",
|
|
40
|
+
decorateReply: false,
|
|
41
|
+
});
|
|
42
|
+
await app.register(fastifyStatic, {
|
|
43
|
+
root: join(publicRoot, "fonts"),
|
|
44
|
+
prefix: "/fonts/",
|
|
45
|
+
decorateReply: false,
|
|
46
|
+
});
|
|
47
|
+
return app;
|
|
48
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { mkdirSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { Engine } from "./engine.js";
|
|
4
|
+
import { buildServer } from "./http.js";
|
|
5
|
+
import { readRuntimeConfig } from "./runtime/config.js";
|
|
6
|
+
import { acquireRunnerLock } from "./runtime/lock.js";
|
|
7
|
+
import { packageMetadata, payloadFingerprint, workspaceIdentity, } from "./runtime/paths.js";
|
|
8
|
+
import { buildSdkUi, SDK_UI_ORIGIN, SDK_UI_PORT } from "./sdk-ui.js";
|
|
9
|
+
import { LOOP_INTERVAL_MS } from "./shared/constants.js";
|
|
10
|
+
import { Store } from "./store.js";
|
|
11
|
+
import { OfficialChainAdapter } from "./testnet/adapter.js";
|
|
12
|
+
import { OfficialAuthorization } from "./testnet/authorization.js";
|
|
13
|
+
import { TestnetEngine } from "./testnet/engine.js";
|
|
14
|
+
const { port, directory, mode } = readRuntimeConfig();
|
|
15
|
+
mkdirSync(directory, { recursive: true, mode: 0o700 });
|
|
16
|
+
const releaseLock = acquireRunnerLock(resolve(directory, "runner.lock"));
|
|
17
|
+
let store;
|
|
18
|
+
let testnet;
|
|
19
|
+
let app;
|
|
20
|
+
let timer;
|
|
21
|
+
let sdkUi;
|
|
22
|
+
let closing = false;
|
|
23
|
+
async function close() {
|
|
24
|
+
if (closing)
|
|
25
|
+
return;
|
|
26
|
+
closing = true;
|
|
27
|
+
if (timer)
|
|
28
|
+
clearInterval(timer);
|
|
29
|
+
// Always release initialized resources, including failures before listen().
|
|
30
|
+
const failures = [];
|
|
31
|
+
for (const dispose of [
|
|
32
|
+
() => testnet?.close(),
|
|
33
|
+
() => sdkUi?.close(),
|
|
34
|
+
() => app?.close(),
|
|
35
|
+
() => store?.close(),
|
|
36
|
+
releaseLock,
|
|
37
|
+
]) {
|
|
38
|
+
try {
|
|
39
|
+
await dispose();
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
failures.push(error);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (failures.length)
|
|
46
|
+
throw new AggregateError(failures, "Runtime shutdown failed");
|
|
47
|
+
}
|
|
48
|
+
process.on("SIGINT", () => {
|
|
49
|
+
void close();
|
|
50
|
+
});
|
|
51
|
+
process.on("SIGTERM", () => {
|
|
52
|
+
void close();
|
|
53
|
+
});
|
|
54
|
+
try {
|
|
55
|
+
store = new Store(resolve(directory, mode === "testnet" ? "testnet.sqlite" : "paper.sqlite"));
|
|
56
|
+
const engine = new Engine(store);
|
|
57
|
+
testnet =
|
|
58
|
+
mode === "testnet"
|
|
59
|
+
? new TestnetEngine(store, new OfficialAuthorization({
|
|
60
|
+
root: resolve(directory, "agent-pass"),
|
|
61
|
+
reviewOrigin: SDK_UI_ORIGIN,
|
|
62
|
+
}), new OfficialChainAdapter())
|
|
63
|
+
: undefined;
|
|
64
|
+
if (testnet)
|
|
65
|
+
testnet.recover();
|
|
66
|
+
else
|
|
67
|
+
engine.recover();
|
|
68
|
+
app = await buildServer(engine, port, {
|
|
69
|
+
testnet,
|
|
70
|
+
metadataPath: resolve(directory, "account.json"),
|
|
71
|
+
workspaceId: workspaceIdentity(directory),
|
|
72
|
+
runtimeVersion: packageMetadata().version,
|
|
73
|
+
runtimeBuildId: payloadFingerprint(),
|
|
74
|
+
});
|
|
75
|
+
if (testnet) {
|
|
76
|
+
sdkUi = await buildSdkUi();
|
|
77
|
+
await sdkUi.listen({ host: "127.0.0.1", port: SDK_UI_PORT });
|
|
78
|
+
}
|
|
79
|
+
await app.listen({ host: "127.0.0.1", port });
|
|
80
|
+
timer = setInterval(() => {
|
|
81
|
+
try {
|
|
82
|
+
if (testnet)
|
|
83
|
+
void testnet
|
|
84
|
+
.tick()
|
|
85
|
+
.catch(() => console.error("Testnet cycle interrupted; persisted digests will reconcile before new work."));
|
|
86
|
+
else
|
|
87
|
+
engine.tick();
|
|
88
|
+
}
|
|
89
|
+
catch (e) {
|
|
90
|
+
console.error("Runner cycle failed; pending intents will reconcile before further work.", e.message);
|
|
91
|
+
}
|
|
92
|
+
}, LOOP_INTERVAL_MS);
|
|
93
|
+
console.log(`Agent Pass Plugin local workspace: http://127.0.0.1:${port}`);
|
|
94
|
+
console.log(mode === "testnet"
|
|
95
|
+
? "Mode: SUI TESTNET. Owner wallet authorization and explicit Start are required. Demo assets only; mainnet is disabled."
|
|
96
|
+
: "Mode: PAPER. All balances, approvals, fills and prices are simulated. No private keys needed.");
|
|
97
|
+
console.log("Runner is independent of the browser and MCP client. Keep this process running.");
|
|
98
|
+
}
|
|
99
|
+
catch (e) {
|
|
100
|
+
try {
|
|
101
|
+
await close();
|
|
102
|
+
}
|
|
103
|
+
catch (cleanupError) {
|
|
104
|
+
throw new AggregateError([e, cleanupError], "Runtime startup failed");
|
|
105
|
+
}
|
|
106
|
+
throw e;
|
|
107
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** Product and local transport defaults shared by server and browser bundles. */
|
|
2
|
+
export const PRODUCT_NAME = "Agent Pass Plugin";
|
|
3
|
+
export const SERVICE_ID = "agent-pass-plugin";
|
|
4
|
+
export const DEFAULT_APP_PORT = 4317;
|
|
5
|
+
export const SDK_UI_PORT = 58529;
|
|
6
|
+
export const SDK_UI_ORIGIN = `http://127.0.0.1:${SDK_UI_PORT}`;
|
|
7
|
+
export const STRATEGY_VERSION = "1.1.0";
|
|
8
|
+
export const LOOP_INTERVAL_MS = 1_000;
|
|
9
|
+
export const VALUATION_MAX_AGE_MS = 60_000;
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { chmodSync, mkdirSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { DatabaseSync } from "node:sqlite";
|
|
4
|
+
export class Store {
|
|
5
|
+
db;
|
|
6
|
+
constructor(file) {
|
|
7
|
+
if (file !== ":memory:") {
|
|
8
|
+
mkdirSync(dirname(file), { recursive: true, mode: 0o700 });
|
|
9
|
+
}
|
|
10
|
+
this.db = new DatabaseSync(file);
|
|
11
|
+
if (file !== ":memory:")
|
|
12
|
+
chmodSync(file, 0o600);
|
|
13
|
+
this.db
|
|
14
|
+
.exec(`PRAGMA journal_mode=WAL; PRAGMA synchronous=FULL; PRAGMA busy_timeout=5000;
|
|
15
|
+
CREATE TABLE IF NOT EXISTS plans(id TEXT PRIMARY KEY, data TEXT NOT NULL);
|
|
16
|
+
CREATE TABLE IF NOT EXISTS state(key TEXT PRIMARY KEY,data TEXT NOT NULL);
|
|
17
|
+
CREATE TABLE IF NOT EXISTS intents(id TEXT PRIMARY KEY,plan_id TEXT NOT NULL,data TEXT NOT NULL);
|
|
18
|
+
CREATE TABLE IF NOT EXISTS receipts(intent_id TEXT PRIMARY KEY,data TEXT NOT NULL);`);
|
|
19
|
+
}
|
|
20
|
+
transaction(fn) {
|
|
21
|
+
this.db.exec("BEGIN IMMEDIATE");
|
|
22
|
+
try {
|
|
23
|
+
const v = fn();
|
|
24
|
+
this.db.exec("COMMIT");
|
|
25
|
+
return v;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
this.db.exec("ROLLBACK");
|
|
29
|
+
throw e;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
plans() {
|
|
33
|
+
return this.db
|
|
34
|
+
.prepare("SELECT data FROM plans ORDER BY rowid DESC")
|
|
35
|
+
.all()
|
|
36
|
+
.map((r) => JSON.parse(String(r.data)));
|
|
37
|
+
}
|
|
38
|
+
get(id) {
|
|
39
|
+
const r = this.db.prepare("SELECT data FROM plans WHERE id=?").get(id);
|
|
40
|
+
if (!r)
|
|
41
|
+
throw new Error("Plan not found");
|
|
42
|
+
return JSON.parse(String(r.data));
|
|
43
|
+
}
|
|
44
|
+
save(p) {
|
|
45
|
+
this.db
|
|
46
|
+
.prepare("INSERT INTO plans VALUES(?,?) ON CONFLICT(id) DO UPDATE SET data=excluded.data")
|
|
47
|
+
.run(p.id, JSON.stringify(p));
|
|
48
|
+
}
|
|
49
|
+
read(key, initial) {
|
|
50
|
+
const r = this.db.prepare("SELECT data FROM state WHERE key=?").get(key);
|
|
51
|
+
return r ? JSON.parse(String(r.data)) : initial;
|
|
52
|
+
}
|
|
53
|
+
write(key, data) {
|
|
54
|
+
this.db
|
|
55
|
+
.prepare("INSERT INTO state VALUES(?,?) ON CONFLICT(key) DO UPDATE SET data=excluded.data")
|
|
56
|
+
.run(key, JSON.stringify(data));
|
|
57
|
+
}
|
|
58
|
+
intent(i) {
|
|
59
|
+
this.db
|
|
60
|
+
.prepare("INSERT INTO intents VALUES(?,?,?) ON CONFLICT(id) DO UPDATE SET data=excluded.data")
|
|
61
|
+
.run(i.id, i.planId, JSON.stringify(i));
|
|
62
|
+
}
|
|
63
|
+
pending() {
|
|
64
|
+
return this.db
|
|
65
|
+
.prepare("SELECT data FROM intents")
|
|
66
|
+
.all()
|
|
67
|
+
.map((r) => JSON.parse(String(r.data)))
|
|
68
|
+
.filter((i) => i.state !== "settled");
|
|
69
|
+
}
|
|
70
|
+
receipt(id) {
|
|
71
|
+
const r = this.db
|
|
72
|
+
.prepare("SELECT data FROM receipts WHERE intent_id=?")
|
|
73
|
+
.get(id);
|
|
74
|
+
return r ? JSON.parse(String(r.data)) : undefined;
|
|
75
|
+
}
|
|
76
|
+
putReceipt(i) {
|
|
77
|
+
this.db
|
|
78
|
+
.prepare("INSERT OR IGNORE INTO receipts VALUES(?,?)")
|
|
79
|
+
.run(i.id, JSON.stringify(i));
|
|
80
|
+
}
|
|
81
|
+
close() {
|
|
82
|
+
this.db.close();
|
|
83
|
+
}
|
|
84
|
+
}
|