wayform 0.1.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 +262 -0
- package/dist/cli.js +65 -0
- package/dist/cli.js.map +1 -0
- package/dist/config.js +174 -0
- package/dist/config.js.map +1 -0
- package/dist/context-format.js +18 -0
- package/dist/context-format.js.map +1 -0
- package/dist/doctor.js +182 -0
- package/dist/doctor.js.map +1 -0
- package/dist/frontmatter.js +50 -0
- package/dist/frontmatter.js.map +1 -0
- package/dist/git-repo.js +166 -0
- package/dist/git-repo.js.map +1 -0
- package/dist/hook-clients.js +90 -0
- package/dist/hook-clients.js.map +1 -0
- package/dist/hook.js +95 -0
- package/dist/hook.js.map +1 -0
- package/dist/index.js +213 -0
- package/dist/index.js.map +1 -0
- package/dist/init-configs.js +119 -0
- package/dist/init-configs.js.map +1 -0
- package/dist/init-env.js +70 -0
- package/dist/init-env.js.map +1 -0
- package/dist/init-remote.js +155 -0
- package/dist/init-remote.js.map +1 -0
- package/dist/init.js +132 -0
- package/dist/init.js.map +1 -0
- package/dist/is-main.js +14 -0
- package/dist/is-main.js.map +1 -0
- package/dist/metrics.js +35 -0
- package/dist/metrics.js.map +1 -0
- package/dist/remote-read.js +57 -0
- package/dist/remote-read.js.map +1 -0
- package/dist/review-prompt.js +23 -0
- package/dist/review-prompt.js.map +1 -0
- package/dist/slug.js +19 -0
- package/dist/slug.js.map +1 -0
- package/dist/space-create.js +195 -0
- package/dist/space-create.js.map +1 -0
- package/dist/stop-hook.js +66 -0
- package/dist/stop-hook.js.map +1 -0
- package/dist/store.js +177 -0
- package/dist/store.js.map +1 -0
- package/dist/token-budget.js +35 -0
- package/dist/token-budget.js.map +1 -0
- package/package.json +39 -0
package/dist/init.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wayform init` — wire Wayform into the current project repo.
|
|
3
|
+
*
|
|
4
|
+
* LOUD, not fail-open (the inverse of the runtime hooks): a half-written setup
|
|
5
|
+
* must surface. Unparseable existing configs are backed up (.bak), never
|
|
6
|
+
* destroyed. Idempotent: re-running adds our entries at most once.
|
|
7
|
+
*/
|
|
8
|
+
import fs from "node:fs";
|
|
9
|
+
import path from "node:path";
|
|
10
|
+
import readline from "node:readline/promises";
|
|
11
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
12
|
+
import { mergeClaudeSettings, mergeCursorHooks, mergeCodexHooks, mergeMcpJson, CODEX_MCP_TOML, } from "./init-configs.js";
|
|
13
|
+
import { buildHookEnv, gitConfigDefault, ensureGitignore, writeSecretFile, hardenSecretFile, } from "./init-env.js";
|
|
14
|
+
const USAGE = `wayform init — wire Wayform into this project
|
|
15
|
+
|
|
16
|
+
Options (all optional; missing identity values are prompted for):
|
|
17
|
+
--author <name> commit author / attribution
|
|
18
|
+
--email <email> commit email
|
|
19
|
+
--context-repo <url> shared context repo URL (may embed a token)
|
|
20
|
+
--project <name> shared project/space name (default: repo dir name)
|
|
21
|
+
--force rewrite an existing .memorylayer-hook.env
|
|
22
|
+
--yes accept git-config / directory defaults, no prompts
|
|
23
|
+
--help show this help
|
|
24
|
+
|
|
25
|
+
Hosted member (gateway) mode:
|
|
26
|
+
--remote wire a hosted member (gateway URL+token, no clone)
|
|
27
|
+
--gateway <url> (remote) hosted gateway base URL
|
|
28
|
+
--token <mlk_...> (remote) member token
|
|
29
|
+
`;
|
|
30
|
+
function flag(args, name) {
|
|
31
|
+
const i = args.indexOf(`--${name}`);
|
|
32
|
+
return i >= 0 && i + 1 < args.length ? args[i + 1] : undefined;
|
|
33
|
+
}
|
|
34
|
+
const has = (args, name) => args.includes(`--${name}`);
|
|
35
|
+
/** Read+parse a JSON config; on parse error, back it up and treat as absent. */
|
|
36
|
+
function readJson(file) {
|
|
37
|
+
if (!fs.existsSync(file))
|
|
38
|
+
return undefined;
|
|
39
|
+
const raw = fs.readFileSync(file, "utf8");
|
|
40
|
+
try {
|
|
41
|
+
return JSON.parse(raw);
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
fs.copyFileSync(file, `${file}.bak`);
|
|
45
|
+
console.warn(`! ${file} was not valid JSON — backed up to ${file}.bak and rewriting.`);
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function writeJson(cwd, rel, data) {
|
|
50
|
+
const file = path.join(cwd, rel);
|
|
51
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
52
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n");
|
|
53
|
+
console.log(` wrote ${rel}`);
|
|
54
|
+
}
|
|
55
|
+
function writeText(cwd, rel, text) {
|
|
56
|
+
const file = path.join(cwd, rel);
|
|
57
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
58
|
+
fs.writeFileSync(file, text);
|
|
59
|
+
console.log(` wrote ${rel}`);
|
|
60
|
+
}
|
|
61
|
+
export async function runInit(args) {
|
|
62
|
+
if (has(args, "help")) {
|
|
63
|
+
output.write(USAGE);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
if (has(args, "remote")) {
|
|
67
|
+
const { runInitRemote } = await import("./init-remote.js");
|
|
68
|
+
await runInitRemote(args);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const cwd = process.cwd();
|
|
72
|
+
if (!fs.existsSync(path.join(cwd, ".git"))) {
|
|
73
|
+
console.warn("! Not a git repository. Hooks are project-scoped; run this in your project root.");
|
|
74
|
+
}
|
|
75
|
+
// --- Project tier: hook configs (all three clients) ---
|
|
76
|
+
writeJson(cwd, ".claude/settings.json", mergeClaudeSettings(readJson(path.join(cwd, ".claude/settings.json"))));
|
|
77
|
+
writeJson(cwd, ".cursor/hooks.json", mergeCursorHooks(readJson(path.join(cwd, ".cursor/hooks.json"))));
|
|
78
|
+
writeJson(cwd, ".codex/hooks.json", mergeCodexHooks(readJson(path.join(cwd, ".codex/hooks.json"))));
|
|
79
|
+
// --- Project tier: MCP registration (Claude Code + Cursor + Codex) ---
|
|
80
|
+
writeJson(cwd, ".mcp.json", mergeMcpJson(readJson(path.join(cwd, ".mcp.json"))));
|
|
81
|
+
writeJson(cwd, ".cursor/mcp.json", mergeMcpJson(readJson(path.join(cwd, ".cursor/mcp.json"))));
|
|
82
|
+
writeText(cwd, ".codex/config.toml", CODEX_MCP_TOML);
|
|
83
|
+
// --- User tier: identity env file ---
|
|
84
|
+
const envFile = path.join(cwd, ".memorylayer-hook.env");
|
|
85
|
+
if (fs.existsSync(envFile) && !has(args, "force")) {
|
|
86
|
+
hardenSecretFile(envFile); // retro-tighten a pre-existing 0644 file
|
|
87
|
+
console.log(" .memorylayer-hook.env exists — leaving it (use --force to rewrite).");
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
const useDefaults = has(args, "yes");
|
|
91
|
+
const rl = useDefaults ||
|
|
92
|
+
(flag(args, "author") &&
|
|
93
|
+
flag(args, "email") &&
|
|
94
|
+
flag(args, "context-repo"))
|
|
95
|
+
? undefined
|
|
96
|
+
: readline.createInterface({ input, output });
|
|
97
|
+
const ask = async (q, def) => {
|
|
98
|
+
if (!rl)
|
|
99
|
+
return def;
|
|
100
|
+
const a = (await rl.question(def ? `${q} [${def}]: ` : `${q}: `)).trim();
|
|
101
|
+
return a || def;
|
|
102
|
+
};
|
|
103
|
+
const author = flag(args, "author") ??
|
|
104
|
+
(await ask("Author name", gitConfigDefault("user.name")));
|
|
105
|
+
const email = flag(args, "email") ??
|
|
106
|
+
(await ask("Author email", gitConfigDefault("user.email")));
|
|
107
|
+
const repoUrl = flag(args, "context-repo") ??
|
|
108
|
+
(await ask("Context repo URL (with token)", ""));
|
|
109
|
+
const project = flag(args, "project") ?? (await ask("Project name", path.basename(cwd)));
|
|
110
|
+
rl?.close();
|
|
111
|
+
if (!author || !repoUrl) {
|
|
112
|
+
throw new Error("author and context-repo are required to write .memorylayer-hook.env");
|
|
113
|
+
}
|
|
114
|
+
writeSecretFile(envFile, buildHookEnv({ author, email, repoUrl, project }));
|
|
115
|
+
console.log(" wrote .memorylayer-hook.env (gitignored)");
|
|
116
|
+
}
|
|
117
|
+
// --- Gitignore the per-user + per-user-local files ---
|
|
118
|
+
const giPath = path.join(cwd, ".gitignore");
|
|
119
|
+
const gi = fs.existsSync(giPath) ? fs.readFileSync(giPath, "utf8") : "";
|
|
120
|
+
fs.writeFileSync(giPath, ensureGitignore(gi, [
|
|
121
|
+
".memorylayer-hook.env",
|
|
122
|
+
".claude/settings.local.json",
|
|
123
|
+
".codex/config.toml",
|
|
124
|
+
]));
|
|
125
|
+
console.log(" updated .gitignore");
|
|
126
|
+
console.log("\nNext steps:");
|
|
127
|
+
console.log(" 1. Commit the project configs so teammates inherit them:");
|
|
128
|
+
console.log(" git add .claude .cursor .codex/hooks.json .mcp.json .gitignore && git commit -m 'chore: wire Wayform'");
|
|
129
|
+
console.log(" 2. Each teammate runs `wayform init` to set their own identity and");
|
|
130
|
+
console.log(" regenerate the gitignored .codex/config.toml (project-scoped MCP).");
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=init.js.map
|
package/dist/init.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"init.js","sourceRoot":"","sources":["../src/init.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAAE,MAAM,SAAS,CAAC;AACzB,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,QAAQ,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,KAAK,IAAI,KAAK,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EACL,mBAAmB,EACnB,gBAAgB,EAChB,eAAe,EACf,YAAY,EACZ,cAAc,GACf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,YAAY,EACZ,gBAAgB,EAChB,eAAe,EACf,eAAe,EACf,gBAAgB,GACjB,MAAM,eAAe,CAAC;AAEvB,MAAM,KAAK,GAA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;QACJ,CAAC;QACD,eAAe,CAAC,OAAO,EAAE,YAAY,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC;QAC5E,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAC;IAC5D,CAAC;IAED,wDAAwD;IACxD,MAAM,MAAM,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,YAAY,CAAC,CAAC;IAC5C,MAAM,EAAE,GAAG,EAAE,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IACxE,EAAE,CAAC,aAAa,CACd,MAAM,EACN,eAAe,CAAC,EAAE,EAAE;QAClB,uBAAuB;QACvB,6BAA6B;QAC7B,oBAAoB;KACrB,CAAC,CACH,CAAC;IACF,OAAO,CAAC,GAAG,CAAC,sBAAsB,CAAC,CAAC;IAEpC,OAAO,CAAC,GAAG,CAAC,eAAe,CAAC,CAAC;IAC7B,OAAO,CAAC,GAAG,CAAC,4DAA4D,CAAC,CAAC;IAC1E,OAAO,CAAC,GAAG,CACT,8GAA8G,CAC/G,CAAC;IACF,OAAO,CAAC,GAAG,CACT,sEAAsE,CACvE,CAAC;IACF,OAAO,CAAC,GAAG,CACT,yEAAyE,CAC1E,CAAC;AACJ,CAAC"}
|
package/dist/is-main.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { pathToFileURL } from "node:url";
|
|
2
|
+
/**
|
|
3
|
+
* True when the module identified by `importMetaUrl` is the process entry point
|
|
4
|
+
* (i.e. it was spawned directly: `node dist/hook.js`), false when it was merely
|
|
5
|
+
* imported (e.g. by cli.ts). Lets an entrypoint keep a direct-spawn self-run for
|
|
6
|
+
* existing tests while being safely importable by the dispatcher.
|
|
7
|
+
*/
|
|
8
|
+
export function isMain(importMetaUrl) {
|
|
9
|
+
const entry = process.argv[1];
|
|
10
|
+
if (!entry)
|
|
11
|
+
return false;
|
|
12
|
+
return importMetaUrl === pathToFileURL(entry).href;
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=is-main.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"is-main.js","sourceRoot":"","sources":["../src/is-main.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAEzC;;;;;GAKG;AACH,MAAM,UAAU,MAAM,CAAC,aAAqB;IAC1C,MAAM,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAC9B,IAAI,CAAC,KAAK;QAAE,OAAO,KAAK,CAAC;IACzB,OAAO,aAAa,KAAK,aAAa,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC;AACrD,CAAC"}
|
package/dist/metrics.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { slug } from "./store.js";
|
|
4
|
+
/** Repo-relative path of an author's append log: `metrics/<slug(author)>.jsonl`. */
|
|
5
|
+
export function metricsRelPath(author) {
|
|
6
|
+
return path.join("metrics", `${slug(author)}.jsonl`);
|
|
7
|
+
}
|
|
8
|
+
export async function recordMetric(cfg, rec) {
|
|
9
|
+
// Gateway-only members have no local clone to append metrics to; their reads
|
|
10
|
+
// are logged server-side (retrieval_log). No-op instead of writing a stray
|
|
11
|
+
// metrics/ file into the member's project cwd. (recordMetric is fail-open.)
|
|
12
|
+
if (!cfg.repoPath)
|
|
13
|
+
return;
|
|
14
|
+
try {
|
|
15
|
+
const line = JSON.stringify({
|
|
16
|
+
ts: new Date().toISOString(),
|
|
17
|
+
author: cfg.author,
|
|
18
|
+
source: rec.source,
|
|
19
|
+
event: rec.event,
|
|
20
|
+
project: rec.project,
|
|
21
|
+
// total is reads-only to avoid overloading one field with two types.
|
|
22
|
+
...(rec.event === "read" && rec.total !== undefined
|
|
23
|
+
? { total: rec.total }
|
|
24
|
+
: {}),
|
|
25
|
+
}) + "\n";
|
|
26
|
+
const abs = path.join(cfg.repoPath, metricsRelPath(cfg.author));
|
|
27
|
+
await fs.mkdir(path.dirname(abs), { recursive: true });
|
|
28
|
+
await fs.appendFile(abs, line, "utf8");
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
// Fail-open: never break or slow a read/write. A lost metric is invisible;
|
|
32
|
+
// a throwing metric would poison the signal it measures.
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=metrics.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"metrics.js","sourceRoot":"","sources":["../src/metrics.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAyBlC,oFAAoF;AACpF,MAAM,UAAU,cAAc,CAAC,MAAc;IAC3C,OAAO,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACvD,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,GAAW,EACX,GAAiB;IAEjB,6EAA6E;IAC7E,2EAA2E;IAC3E,4EAA4E;IAC5E,IAAI,CAAC,GAAG,CAAC,QAAQ;QAAE,OAAO;IAC1B,IAAI,CAAC;QACH,MAAM,IAAI,GACR,IAAI,CAAC,SAAS,CAAC;YACb,EAAE,EAAE,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;YAC5B,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,MAAM,EAAE,GAAG,CAAC,MAAM;YAClB,KAAK,EAAE,GAAG,CAAC,KAAK;YAChB,OAAO,EAAE,GAAG,CAAC,OAAO;YACpB,qEAAqE;YACrE,GAAG,CAAC,GAAG,CAAC,KAAK,KAAK,MAAM,IAAI,GAAG,CAAC,KAAK,KAAK,SAAS;gBACjD,CAAC,CAAC,EAAE,KAAK,EAAE,GAAG,CAAC,KAAK,EAAE;gBACtB,CAAC,CAAC,EAAE,CAAC;SACR,CAAC,GAAG,IAAI,CAAC;QACZ,MAAM,GAAG,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,CAAC;QAChE,MAAM,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;QACvD,MAAM,EAAE,CAAC,UAAU,CAAC,GAAG,EAAE,IAAI,EAAE,MAAM,CAAC,CAAC;IACzC,CAAC;IAAC,MAAM,CAAC;QACP,2EAA2E;QAC3E,yDAAyD;IAC3D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
const REMOTE_TIMEOUT_MS = 4000;
|
|
2
|
+
function configured(cfg) {
|
|
3
|
+
return Boolean(cfg.gatewayUrl && cfg.gatewayToken);
|
|
4
|
+
}
|
|
5
|
+
export async function remoteApiRead(cfg, opts, fetchImpl = fetch) {
|
|
6
|
+
if (!configured(cfg))
|
|
7
|
+
return null;
|
|
8
|
+
try {
|
|
9
|
+
const url = new URL(`${cfg.gatewayUrl}/api/read`);
|
|
10
|
+
url.searchParams.set("project", opts.project);
|
|
11
|
+
if (opts.query)
|
|
12
|
+
url.searchParams.set("query", opts.query);
|
|
13
|
+
if (opts.budgetTokens)
|
|
14
|
+
url.searchParams.set("budget", String(opts.budgetTokens));
|
|
15
|
+
if (opts.kinds?.length)
|
|
16
|
+
url.searchParams.set("kinds", opts.kinds.join(","));
|
|
17
|
+
if (opts.trigger)
|
|
18
|
+
url.searchParams.set("trigger", opts.trigger);
|
|
19
|
+
const res = await fetchImpl(url.toString(), {
|
|
20
|
+
headers: { authorization: `Bearer ${cfg.gatewayToken}` },
|
|
21
|
+
signal: AbortSignal.timeout(REMOTE_TIMEOUT_MS),
|
|
22
|
+
});
|
|
23
|
+
if (!res.ok)
|
|
24
|
+
return null;
|
|
25
|
+
const body = (await res.json());
|
|
26
|
+
if (typeof body.text !== "string" || typeof body.total !== "number")
|
|
27
|
+
return null;
|
|
28
|
+
return {
|
|
29
|
+
text: body.text,
|
|
30
|
+
total: body.total,
|
|
31
|
+
matched: typeof body.matched === "number" ? body.matched : 0,
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
export async function remoteHookRead(cfg, project, budgetTokens, fetchImpl = fetch) {
|
|
39
|
+
if (!configured(cfg))
|
|
40
|
+
return null;
|
|
41
|
+
try {
|
|
42
|
+
const url = new URL(`${cfg.gatewayUrl}/hook/read`);
|
|
43
|
+
url.searchParams.set("project", project);
|
|
44
|
+
url.searchParams.set("budget", String(budgetTokens));
|
|
45
|
+
const res = await fetchImpl(url.toString(), {
|
|
46
|
+
headers: { authorization: `Bearer ${cfg.gatewayToken}` },
|
|
47
|
+
signal: AbortSignal.timeout(REMOTE_TIMEOUT_MS),
|
|
48
|
+
});
|
|
49
|
+
if (!res.ok)
|
|
50
|
+
return null;
|
|
51
|
+
return await res.text();
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=remote-read.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"remote-read.js","sourceRoot":"","sources":["../src/remote-read.ts"],"names":[],"mappings":"AAeA,MAAM,iBAAiB,GAAG,IAAI,CAAC;AAI/B,SAAS,UAAU,CAAC,GAAe;IACjC,OAAO,OAAO,CAAC,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,YAAY,CAAC,CAAC;AACrD,CAAC;AAED,MAAM,CAAC,KAAK,UAA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
* Neutral builder for the end-of-turn self-review instruction (write-trigger Path C).
|
|
3
|
+
*
|
|
4
|
+
* This text is vendor-NEUTRAL and lives in exactly one place: only the Stop-hook
|
|
5
|
+
* envelope that carries it is per-vendor (see ./hook-clients.ts). The instruction is
|
|
6
|
+
* the reliable, hook-forced counterpart to the model spontaneously choosing to call
|
|
7
|
+
* write_context (the ~60-85% path that decays over long chats).
|
|
8
|
+
*/
|
|
9
|
+
export function reviewInstruction(project) {
|
|
10
|
+
return (`Wayform end-of-turn review for project "${project}". ` +
|
|
11
|
+
`Before finishing: did THIS turn settle a deliberate decision, or establish ` +
|
|
12
|
+
`durable context, that is not already recorded in the shared store? A settled ` +
|
|
13
|
+
`decision is "we decided X because Y" — not an open question, an option still ` +
|
|
14
|
+
`under discussion, or an intermediate reasoning step. ` +
|
|
15
|
+
`If yes, call the write_context tool now (project: "${project}", ` +
|
|
16
|
+
`type: "decision" for a settled call or "context" for durable background) with ` +
|
|
17
|
+
`a compact statement that includes the "because". ` +
|
|
18
|
+
`Deduplicate against what is already recorded in this session — do NOT rewrite ` +
|
|
19
|
+
`anything already stored. ` +
|
|
20
|
+
`If nothing qualifies, do nothing and finish normally. ` +
|
|
21
|
+
`Keep the store curated, not a firehose.`);
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=review-prompt.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"review-prompt.js","sourceRoot":"","sources":["../src/review-prompt.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,OAAe;IAC/C,OAAO,CACL,2CAA2C,OAAO,KAAK;QACvD,6EAA6E;QAC7E,+EAA+E;QAC/E,+EAA+E;QAC/E,uDAAuD;QACvD,sDAAsD,OAAO,KAAK;QAClE,gFAAgF;QAChF,mDAAmD;QACnD,gFAAgF;QAChF,2BAA2B;QAC3B,wDAAwD;QACxD,yCAAyC,CAC1C,CAAC;AACJ,CAAC"}
|
package/dist/slug.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Collapse arbitrary text to a filesystem-safe slug. Also the path-traversal
|
|
3
|
+
* guard: stripping every non-alphanumeric run means "../../etc" -> "etc", so a
|
|
4
|
+
* hostile project/author name can never escape the context/ directory.
|
|
5
|
+
*
|
|
6
|
+
* Lives in its own dependency-free module so the hosted gateway (Web APIs
|
|
7
|
+
* only, no node:*) shares the exact same guard as the local store.
|
|
8
|
+
*/
|
|
9
|
+
export function slug(s) {
|
|
10
|
+
return (s
|
|
11
|
+
.toLowerCase()
|
|
12
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
13
|
+
.replace(/^-+|-+$/g, "") || "unknown");
|
|
14
|
+
}
|
|
15
|
+
/** ISO timestamp -> filename-safe form (colons/dots to dashes). */
|
|
16
|
+
export function fsSafeTimestamp(iso) {
|
|
17
|
+
return iso.replace(/[:.]/g, "-");
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=slug.js.map
|
package/dist/slug.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slug.js","sourceRoot":"","sources":["../src/slug.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,MAAM,UAAU,IAAI,CAAC,CAAS;IAC5B,OAAO,CACL,CAAC;SACE,WAAW,EAAE;SACb,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC;SAC3B,OAAO,CAAC,UAAU,EAAE,EAAE,CAAC,IAAI,SAAS,CACxC,CAAC;AACJ,CAAC;AAED,mEAAmE;AACnE,MAAM,UAAU,eAAe,CAAC,GAAW;IACzC,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,CAAC;AACnC,CAAC"}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `wayform space create` — Plan C: automates the operator flow for standing
|
|
3
|
+
* up a new hosted space (repo creation, GitHub App install detection, member
|
|
4
|
+
* token mint). Operator-only: requires WAYFORM_ADMIN_SECRET, the same shared
|
|
5
|
+
* credential the manual `curl` flow against POST /admin/members already uses.
|
|
6
|
+
*/
|
|
7
|
+
import { execFileSync } from "node:child_process";
|
|
8
|
+
import readline from "node:readline/promises";
|
|
9
|
+
import { stdin as input, stdout as output } from "node:process";
|
|
10
|
+
import { gitConfigDefault } from "./init-env.js";
|
|
11
|
+
const flag = (args, name) => {
|
|
12
|
+
const i = args.indexOf(`--${name}`);
|
|
13
|
+
return i >= 0 && i + 1 < args.length ? args[i + 1] : undefined;
|
|
14
|
+
};
|
|
15
|
+
const has = (args, name) => args.includes(`--${name}`);
|
|
16
|
+
export function parseSpaceCreateArgs(args) {
|
|
17
|
+
const space = flag(args, "space");
|
|
18
|
+
const owner = flag(args, "owner");
|
|
19
|
+
const repo = flag(args, "repo");
|
|
20
|
+
if (!space || !owner || !repo) {
|
|
21
|
+
throw new Error("wayform space create requires --space <name> --owner <owner> --repo <repo>");
|
|
22
|
+
}
|
|
23
|
+
const appSlug = flag(args, "app-slug") ?? process.env.WAYFORM_GITHUB_APP_SLUG;
|
|
24
|
+
if (!appSlug) {
|
|
25
|
+
throw new Error("wayform space create requires --app-slug <slug> or WAYFORM_GITHUB_APP_SLUG " +
|
|
26
|
+
"(the GitHub App's slug, used to build the install URL)");
|
|
27
|
+
}
|
|
28
|
+
return {
|
|
29
|
+
space,
|
|
30
|
+
owner,
|
|
31
|
+
repo,
|
|
32
|
+
isPublic: has(args, "public"),
|
|
33
|
+
appSlug,
|
|
34
|
+
author: flag(args, "author"),
|
|
35
|
+
authorEmail: flag(args, "author-email"),
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
export function resolveAdminSecret() {
|
|
39
|
+
const secret = process.env.WAYFORM_ADMIN_SECRET;
|
|
40
|
+
if (!secret) {
|
|
41
|
+
throw new Error("WAYFORM_ADMIN_SECRET is not set. `wayform space create` is an " +
|
|
42
|
+
"operator-only command — export the gateway's ADMIN_SECRET before running it.");
|
|
43
|
+
}
|
|
44
|
+
return secret;
|
|
45
|
+
}
|
|
46
|
+
const defaultRunner = (cmd, args) =>
|
|
47
|
+
// Bounded + non-interactive, same posture as init-remote's registerClaudeCodeMcp:
|
|
48
|
+
// a hanging or prompting child process must never freeze space create.
|
|
49
|
+
void execFileSync(cmd, args, {
|
|
50
|
+
stdio: ["ignore", "ignore", "ignore"],
|
|
51
|
+
timeout: 15000,
|
|
52
|
+
});
|
|
53
|
+
export function ghAuthenticated(run = defaultRunner) {
|
|
54
|
+
try {
|
|
55
|
+
run("gh", ["auth", "status"]);
|
|
56
|
+
return true;
|
|
57
|
+
}
|
|
58
|
+
catch {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export function createRepoWithGh(owner, repo, isPublic, run = defaultRunner) {
|
|
63
|
+
run("gh", [
|
|
64
|
+
"repo",
|
|
65
|
+
"create",
|
|
66
|
+
`${owner}/${repo}`,
|
|
67
|
+
isPublic ? "--public" : "--private",
|
|
68
|
+
]);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Tries the org repo-creation endpoint first, falls back to /user/repos on a
|
|
72
|
+
* 404 (the owner isn't an org this token can create under — the common case
|
|
73
|
+
* when --owner is the token holder's own username).
|
|
74
|
+
*/
|
|
75
|
+
export async function createRepoWithPat(owner, repo, isPublic, pat, fetchImpl = fetch) {
|
|
76
|
+
const body = JSON.stringify({ name: repo, private: !isPublic });
|
|
77
|
+
const headers = {
|
|
78
|
+
authorization: `Bearer ${pat}`,
|
|
79
|
+
accept: "application/vnd.github+json",
|
|
80
|
+
"content-type": "application/json",
|
|
81
|
+
"user-agent": "wayform-cli",
|
|
82
|
+
};
|
|
83
|
+
let res = await fetchImpl(`https://api.github.com/orgs/${owner}/repos`, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers,
|
|
86
|
+
body,
|
|
87
|
+
});
|
|
88
|
+
if (res.status === 404) {
|
|
89
|
+
res = await fetchImpl("https://api.github.com/user/repos", {
|
|
90
|
+
method: "POST",
|
|
91
|
+
headers,
|
|
92
|
+
body,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (!res.ok) {
|
|
96
|
+
const detail = await res.text();
|
|
97
|
+
throw new Error(`GitHub repo creation failed (${res.status}): ${detail}`);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const DEFAULT_POLL = { intervalMs: 3000, timeoutMs: 120_000 };
|
|
101
|
+
const defaultSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
102
|
+
export async function pollInstallation(gatewayUrl, adminSecret, owner, fetchImpl = fetch, opts = DEFAULT_POLL, sleep = defaultSleep) {
|
|
103
|
+
const deadline = Date.now() + opts.timeoutMs;
|
|
104
|
+
const url = `${gatewayUrl}/admin/installations?owner=${encodeURIComponent(owner)}`;
|
|
105
|
+
const manualFallback = `curl -X POST ${gatewayUrl}/admin/members -H "x-admin-secret: <secret>" ` +
|
|
106
|
+
`-H "content-type: application/json" -d '{"space":"...","installationId":<id>,` +
|
|
107
|
+
`"owner":"${owner}","repo":"...","author":"...","authorEmail":"..."}'`;
|
|
108
|
+
while (true) {
|
|
109
|
+
const res = await fetchImpl(url, {
|
|
110
|
+
headers: { "x-admin-secret": adminSecret },
|
|
111
|
+
});
|
|
112
|
+
if (res.status === 200) {
|
|
113
|
+
const body = (await res.json());
|
|
114
|
+
return body.installationId;
|
|
115
|
+
}
|
|
116
|
+
if (res.status === 409) {
|
|
117
|
+
const body = (await res.json());
|
|
118
|
+
throw new Error(`Multiple GitHub App installations found for owner "${owner}" ` +
|
|
119
|
+
`(${body.installationIds.join(", ")}). Resolve manually, then mint the ` +
|
|
120
|
+
`token directly:\n ${manualFallback}`);
|
|
121
|
+
}
|
|
122
|
+
if (Date.now() >= deadline) {
|
|
123
|
+
throw new Error(`Timed out waiting for the GitHub App install on "${owner}". Install it, ` +
|
|
124
|
+
`then mint the token manually:\n ${manualFallback}`);
|
|
125
|
+
}
|
|
126
|
+
await sleep(opts.intervalMs);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
export async function mintMemberToken(gatewayUrl, adminSecret, body, fetchImpl = fetch) {
|
|
130
|
+
const res = await fetchImpl(`${gatewayUrl}/admin/members`, {
|
|
131
|
+
method: "POST",
|
|
132
|
+
headers: {
|
|
133
|
+
"x-admin-secret": adminSecret,
|
|
134
|
+
"content-type": "application/json",
|
|
135
|
+
},
|
|
136
|
+
body: JSON.stringify(body),
|
|
137
|
+
});
|
|
138
|
+
if (!res.ok) {
|
|
139
|
+
const detail = await res.text();
|
|
140
|
+
throw new Error(`token mint failed (${res.status}): ${detail}`);
|
|
141
|
+
}
|
|
142
|
+
return (await res.json());
|
|
143
|
+
}
|
|
144
|
+
export async function promptForPat() {
|
|
145
|
+
const rl = readline.createInterface({ input, output });
|
|
146
|
+
const pat = await rl.question("gh not found or not authenticated. Paste a GitHub PAT with repo-creation scope: ");
|
|
147
|
+
rl.close();
|
|
148
|
+
return pat.trim();
|
|
149
|
+
}
|
|
150
|
+
const defaultDeps = {
|
|
151
|
+
run: defaultRunner,
|
|
152
|
+
fetchImpl: fetch,
|
|
153
|
+
promptForPat,
|
|
154
|
+
sleep: defaultSleep,
|
|
155
|
+
log: (msg) => console.log(msg),
|
|
156
|
+
poll: DEFAULT_POLL,
|
|
157
|
+
};
|
|
158
|
+
export async function runSpaceCreate(args, deps = {}) {
|
|
159
|
+
const d = { ...defaultDeps, ...deps };
|
|
160
|
+
const parsed = parseSpaceCreateArgs(args);
|
|
161
|
+
const gatewayUrl = (flag(args, "gateway") ?? "").replace(/\/+$/, "");
|
|
162
|
+
if (!gatewayUrl) {
|
|
163
|
+
throw new Error("wayform space create requires --gateway <url>");
|
|
164
|
+
}
|
|
165
|
+
const adminSecret = resolveAdminSecret();
|
|
166
|
+
const author = parsed.author ?? gitConfigDefault("user.name");
|
|
167
|
+
const authorEmail = parsed.authorEmail ?? gitConfigDefault("user.email");
|
|
168
|
+
d.log(`Creating repo ${parsed.owner}/${parsed.repo}...`);
|
|
169
|
+
if (ghAuthenticated(d.run)) {
|
|
170
|
+
createRepoWithGh(parsed.owner, parsed.repo, parsed.isPublic, d.run);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
const pat = await d.promptForPat();
|
|
174
|
+
await createRepoWithPat(parsed.owner, parsed.repo, parsed.isPublic, pat, d.fetchImpl);
|
|
175
|
+
}
|
|
176
|
+
d.log(`Repo created. Install the GitHub App: https://github.com/apps/${parsed.appSlug}/installations/new`);
|
|
177
|
+
d.log("Waiting for the App to be installed...");
|
|
178
|
+
const installationId = await pollInstallation(gatewayUrl, adminSecret, parsed.owner, d.fetchImpl, d.poll, d.sleep);
|
|
179
|
+
d.log(`Detected installation ${installationId}. Minting member token...`);
|
|
180
|
+
const minted = await mintMemberToken(gatewayUrl, adminSecret, {
|
|
181
|
+
space: parsed.space,
|
|
182
|
+
installationId,
|
|
183
|
+
owner: parsed.owner,
|
|
184
|
+
repo: parsed.repo,
|
|
185
|
+
author,
|
|
186
|
+
authorEmail,
|
|
187
|
+
}, d.fetchImpl);
|
|
188
|
+
d.log("");
|
|
189
|
+
d.log(`Space "${parsed.space}" created.`);
|
|
190
|
+
d.log(`Token (shown once): ${minted.token}`);
|
|
191
|
+
d.log("");
|
|
192
|
+
d.log("Hand this to each teammate:");
|
|
193
|
+
d.log(` wayform init --remote --gateway ${gatewayUrl} --token ${minted.token}`);
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=space-create.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"space-create.js","sourceRoot":"","sources":["../src/space-create.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAClD,OAAO,QAAQ,MAAM,wBAAwB,CAAC;AAC9C,OAAO,EAAE,KAAK,IAAI,KAAK,EAAE,MAAM,IAAI,MAAM,EAAE,MAAM,cAAc,CAAC;AAChE,OAAO,EAAE,gBA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AAU,EACV,WAAW,EACX;QACE,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,cAAc;QACd,KAAK,EAAE,MAAM,CAAC,KAAK;QACnB,IAAI,EAAE,MAAM,CAAC,IAAI;QACjB,MAAM;QACN,WAAW;KACZ,EACD,CAAC,CAAC,SAAS,CACZ,CAAC;IAEF,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACV,CAAC,CAAC,GAAG,CAAC,UAAU,MAAM,CAAC,KAAK,YAAY,CAAC,CAAC;IAC1C,CAAC,CAAC,GAAG,CAAC,uBAAuB,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC;IAC7C,CAAC,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACV,CAAC,CAAC,GAAG,CAAC,6BAA6B,CAAC,CAAC;IACrC,CAAC,CAAC,GAAG,CACH,qCAAqC,UAAU,YAAY,MAAM,CAAC,KAAK,EAAE,CAC1E,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* Stop-hook entrypoint — the write-side counterpart to hook.ts (write-trigger Path C).
|
|
4
|
+
*
|
|
5
|
+
* A client runs this at the END OF EVERY TURN (Claude Code `Stop`). It injects a
|
|
6
|
+
* self-review instruction so the model records a decision it just settled, without the
|
|
7
|
+
* human having to remember to. Per-turn (not session-end) because SessionEnd is
|
|
8
|
+
* cleanup-only and cannot re-engage the model; only Stop can.
|
|
9
|
+
*
|
|
10
|
+
* LOOP GUARD is load-bearing: when the model is already continuing because of THIS hook,
|
|
11
|
+
* the client sets stop_hook_active=true on the payload. We MUST emit a no-op then, or we
|
|
12
|
+
* create an infinite Stop loop.
|
|
13
|
+
*
|
|
14
|
+
* FAIL-OPEN: on any error (unparseable payload, unknown state) emit the client no-op and
|
|
15
|
+
* exit 0. A hook that traps a turn would itself cause the "can't rely on this" failure
|
|
16
|
+
* this feature exists to prevent.
|
|
17
|
+
*/
|
|
18
|
+
import { defaultProject } from "./config.js";
|
|
19
|
+
import { reviewInstruction } from "./review-prompt.js";
|
|
20
|
+
import { resolveClient, renderStopReview, renderStopNoop, } from "./hook-clients.js";
|
|
21
|
+
import { isMain } from "./is-main.js";
|
|
22
|
+
export async function runStopHook() {
|
|
23
|
+
// Resolved here (not at module load) so the dispatcher can set the client from
|
|
24
|
+
// the subcommand arg before calling.
|
|
25
|
+
const client = resolveClient(process.env.MEMORYLAYER_HOOK_CLIENT);
|
|
26
|
+
const emitNoop = () => {
|
|
27
|
+
process.stdout.write(renderStopNoop(client));
|
|
28
|
+
process.exit(0);
|
|
29
|
+
};
|
|
30
|
+
const readStdin = async () => {
|
|
31
|
+
if (process.stdin.isTTY)
|
|
32
|
+
return "";
|
|
33
|
+
let data = "";
|
|
34
|
+
try {
|
|
35
|
+
for await (const chunk of process.stdin)
|
|
36
|
+
data += chunk;
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
// stdin not readable — treat as empty payload.
|
|
40
|
+
}
|
|
41
|
+
return data;
|
|
42
|
+
};
|
|
43
|
+
const raw = await readStdin();
|
|
44
|
+
// Loop guard: if we cannot confirm we are NOT already in a hook-driven
|
|
45
|
+
// continuation, the safe choice is to let the turn end (never risk a loop).
|
|
46
|
+
// Client-agnostic: Claude Code / Codex set stop_hook_active; Cursor increments
|
|
47
|
+
// loop_count (and enforces loop_limit in config as a hard backstop).
|
|
48
|
+
let payload = {};
|
|
49
|
+
try {
|
|
50
|
+
payload = raw.trim() ? JSON.parse(raw) : {};
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
emitNoop();
|
|
54
|
+
}
|
|
55
|
+
const isContinuation = payload.stop_hook_active === true ||
|
|
56
|
+
(typeof payload.loop_count === "number" && payload.loop_count > 0);
|
|
57
|
+
if (isContinuation)
|
|
58
|
+
emitNoop();
|
|
59
|
+
const project = process.env.MEMORYLAYER_PROJECT?.trim() || defaultProject();
|
|
60
|
+
process.stdout.write(renderStopReview(client, reviewInstruction(project)));
|
|
61
|
+
process.exit(0);
|
|
62
|
+
}
|
|
63
|
+
if (isMain(import.meta.url)) {
|
|
64
|
+
void runStopHook();
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=stop-hook.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"stop-hook.js","sourceRoot":"","sources":["../src/stop-hook.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;GAeG;AACH,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAC7C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AACvD,OAAO,EACL,aAAa,EACb,gBAAgB,EAChB,cAAc,GAEf,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,MAAM,EAAE,MAAM,cAAc,CAAC;AAEtC,MAAM,CAAC,KAAK,UAAU,WAAW;IAC/B,+EAA+E;IAC/E,qCAAqC;IACrC,MAAM,MAAM,GAAe,aAAa,CAAC,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAE9E,MAAM,QAAQ,GAAG,GAAU,EAAE;QAC3B,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;QAC7C,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IAClB,CAAC,CAAC;IAEF,MAAM,SAAS,GAAG,KAAK,IAAqB,EAAE;QAC5C,IAAI,OAAO,CAAC,KAAK,CAAC,KAAK;YAAE,OAAO,EAAE,CAAC;QACnC,IAAI,IAAI,GAAG,EAAE,CAAC;QACd,IAAI,CAAC;YACH,IAAI,KAAK,EAAE,MAAM,KAAK,IAAI,OAAO,CAAC,KAAK;gBAAE,IAAI,IAAI,KAAK,CAAC;QACzD,CAAC;QAAC,MAAM,CAAC;YACP,+CAA+C;QACjD,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEF,MAAM,GAAG,GAAG,MAAM,SAAS,E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