@scp-dev/cli 0.0.0-stage → 0.1.2
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 +201 -0
- package/README.md +33 -2
- package/dist/chunk-4DNQFATX.js +221 -0
- package/dist/chunk-4QYUPPGV.js +147 -0
- package/dist/chunk-6UFMKYPH.js +255 -0
- package/dist/chunk-7KP6TAJW.js +336 -0
- package/dist/chunk-DA3EB2YF.js +359 -0
- package/dist/chunk-KN7WEIE4.js +41 -0
- package/dist/chunk-N3ZROLFA.js +1182 -0
- package/dist/chunk-V5I7KEU7.js +948 -0
- package/dist/chunk-XWLF7ON2.js +757 -0
- package/dist/chunk-YYSLYVNZ.js +38 -0
- package/dist/cli.js +22 -0
- package/dist/hooks-O3UZXDYG.js +71 -0
- package/dist/install-HFBSKSTG.js +14 -0
- package/dist/login-JTRUWG6Q.js +129 -0
- package/dist/main-5CAHHN55.js +1271 -0
- package/dist/mcp-P2AZYP5G.js +72 -0
- package/dist/peek-YCOWBEPD.js +60 -0
- package/dist/relay-VVJPYVXE.js +30 -0
- package/dist/remote-3BWFISXJ.js +27 -0
- package/dist/setup-RDPVGE4H.js +39 -0
- package/dist/spawn-TQ3HOZTQ.js +74 -0
- package/dist/up-KXOYO7QV.js +37 -0
- package/package.json +38 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
CHANGED
|
@@ -1,3 +1,34 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @scp-dev/cli
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The `swarm` command line tool, the reference binding of SCP for shells, hooks and MCP.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
npm install -g @scp-dev/cli
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
swarm login
|
|
13
|
+
swarm setup --write
|
|
14
|
+
swarm spawn --name parser -- claude "Fix the parser tests"
|
|
15
|
+
swarm send parser "Also cover the empty-input case."
|
|
16
|
+
swarm list --tree
|
|
17
|
+
swarm inbox --wait 30
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Headless (CI, cron, cloud runners): `swarm login --token scp_t_...` or `SCP_TOKEN`. `swarm up --token ...` logs in, links the broker to the hub and runs setup in one step. The default relay is https://app.scp.dev; `swarm relay use <url>` picks another. Runs on Node 22.13 or later.
|
|
21
|
+
|
|
22
|
+
## Provides
|
|
23
|
+
|
|
24
|
+
- Messaging: `send`, `reply`, `ask`, `inbox`, `ack`.
|
|
25
|
+
- Spawn and supervision: `spawn`, `spawn-env`, `peek`.
|
|
26
|
+
- Scopes and policy: `rules`, `why`, `group`, `admin`, `policy`, `holds`.
|
|
27
|
+
- Relay and federation: `login`, `logout`, `up`, `relay use`, `relay show`, `cloud`, `machines`.
|
|
28
|
+
- Integration: `daemon`, `mcp`, `hooks install`, `setup`, `hook`.
|
|
29
|
+
|
|
30
|
+
Implements [Appendix E, CLI verbs](https://scp.dev/spec#appendix-e-reference-binding-cli-verbs) and [Appendix F, MCP tools](https://scp.dev/spec#appendix-f-reference-binding-mcp-tools). See the [root README](https://github.com/scphq/scp#readme).
|
|
31
|
+
|
|
32
|
+
## License
|
|
33
|
+
|
|
34
|
+
[Apache-2.0](LICENSE).
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RelayUsage
|
|
3
|
+
} from "./chunk-7KP6TAJW.js";
|
|
4
|
+
import {
|
|
5
|
+
AGY_HOOK_SET,
|
|
6
|
+
AGY_UNVERIFIED,
|
|
7
|
+
BLOCK_STOP,
|
|
8
|
+
PUSH_WAIT,
|
|
9
|
+
PUSH_WAIT_DEFAULT_S,
|
|
10
|
+
agyHookSet,
|
|
11
|
+
hasOurHooks,
|
|
12
|
+
hookEntries,
|
|
13
|
+
mergeHooks,
|
|
14
|
+
musePlugin,
|
|
15
|
+
opencodePlugin
|
|
16
|
+
} from "./chunk-XWLF7ON2.js";
|
|
17
|
+
import {
|
|
18
|
+
gitToplevel
|
|
19
|
+
} from "./chunk-DA3EB2YF.js";
|
|
20
|
+
|
|
21
|
+
// src/install.ts
|
|
22
|
+
import { chmodSync, copyFileSync, existsSync, lstatSync, mkdirSync, readFileSync, realpathSync, renameSync, statSync, unlinkSync, writeFileSync } from "fs";
|
|
23
|
+
import { homedir } from "os";
|
|
24
|
+
import { basename, dirname, join } from "path";
|
|
25
|
+
import { CLI_NAME, LIMITS, validHandle } from "@scp-dev/core";
|
|
26
|
+
function home(env) {
|
|
27
|
+
return env.HOME || homedir();
|
|
28
|
+
}
|
|
29
|
+
function hooksTarget(harness, scope, env, cwd) {
|
|
30
|
+
const project = gitToplevel(cwd) ?? cwd;
|
|
31
|
+
switch (harness) {
|
|
32
|
+
case "claude":
|
|
33
|
+
if (scope === "local") return join(project, ".claude", "settings.local.json");
|
|
34
|
+
return scope === "user" ? join(env.CLAUDE_CONFIG_DIR || join(home(env), ".claude"), "settings.json") : join(project, ".claude", "settings.json");
|
|
35
|
+
case "codex":
|
|
36
|
+
return scope === "user" ? join(env.CODEX_HOME || join(home(env), ".codex"), "hooks.json") : join(project, ".codex", "hooks.json");
|
|
37
|
+
case "opencode":
|
|
38
|
+
return scope === "user" ? join(env.XDG_CONFIG_HOME || join(home(env), ".config"), "opencode", "plugin", "scp.ts") : join(project, ".opencode", "plugin", "scp.ts");
|
|
39
|
+
case "agy":
|
|
40
|
+
return scope === "user" ? join(home(env), ".gemini", "config", "hooks.json") : join(project, ".agents", "hooks.json");
|
|
41
|
+
case "muse":
|
|
42
|
+
return join(env.XDG_CONFIG_HOME || join(home(env), ".config"), "scp", "muse-plugin", ".muse-plugin", "plugin.json");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
function stamp(now) {
|
|
46
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
47
|
+
return `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
48
|
+
}
|
|
49
|
+
function writeAtomic(target, content, now) {
|
|
50
|
+
const file = existsSync(target) && lstatSync(target).isSymbolicLink() ? realpathSync(target) : target;
|
|
51
|
+
const existing = existsSync(file) ? readFileSync(file, "utf8") : void 0;
|
|
52
|
+
if (existing === content) return { changed: false };
|
|
53
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
54
|
+
let backup;
|
|
55
|
+
let mode = 420;
|
|
56
|
+
if (existing !== void 0) {
|
|
57
|
+
mode = statSync(file).mode & 4095;
|
|
58
|
+
const stamped = `${file}.bak-${stamp(now)}`;
|
|
59
|
+
backup = stamped;
|
|
60
|
+
for (let n = 2; existsSync(backup); n++) backup = `${stamped}-${n}`;
|
|
61
|
+
copyFileSync(file, backup);
|
|
62
|
+
chmodSync(backup, mode);
|
|
63
|
+
}
|
|
64
|
+
const temp = join(dirname(file), `.${basename(file)}.tmp-${process.pid}`);
|
|
65
|
+
try {
|
|
66
|
+
writeFileSync(temp, content, { mode });
|
|
67
|
+
renameSync(temp, file);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
try {
|
|
70
|
+
unlinkSync(temp);
|
|
71
|
+
} catch {
|
|
72
|
+
}
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
return { changed: true, ...backup ? { backup } : {} };
|
|
76
|
+
}
|
|
77
|
+
function installHooks(options) {
|
|
78
|
+
if (options.env.SCP_TRANSPORT === "cloud") {
|
|
79
|
+
throw new RelayUsage(
|
|
80
|
+
`relay hooks report to the local broker, and SCP_TRANSPORT=cloud bypasses it; link the broker instead (${CLI_NAME} cloud enable) and install the hooks without it`
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
const harness = options.harness;
|
|
84
|
+
if (harness === "grok") {
|
|
85
|
+
throw new RelayUsage(
|
|
86
|
+
"Grok runs the Claude Code hooks (install those with --harness claude) and is labelled grok by them; it drops a prompt hook's context, so new messages reach it after tools only"
|
|
87
|
+
);
|
|
88
|
+
}
|
|
89
|
+
if (harness !== "claude" && harness !== "codex" && harness !== "opencode" && harness !== "agy" && harness !== "muse") {
|
|
90
|
+
throw new RelayUsage("hooks install takes --harness claude, codex, opencode, agy or muse");
|
|
91
|
+
}
|
|
92
|
+
const scope = options.scope ?? (harness === "claude" ? "local" : harness === "muse" ? "user" : "project");
|
|
93
|
+
if (scope !== "project" && scope !== "user" && !(scope === "local" && harness === "claude")) {
|
|
94
|
+
throw new RelayUsage("--scope is project or user (and local for claude, its default: .claude/settings.local.json, the per-person file)");
|
|
95
|
+
}
|
|
96
|
+
const target = hooksTarget(harness, scope, options.env, options.cwd);
|
|
97
|
+
const now = options.now ?? /* @__PURE__ */ new Date();
|
|
98
|
+
if (options.pushWait !== void 0 && (!Number.isInteger(options.pushWait) || options.pushWait < 0 || options.pushWait > 3600)) {
|
|
99
|
+
throw new RelayUsage(`${PUSH_WAIT} takes whole seconds, 0 to 3600`);
|
|
100
|
+
}
|
|
101
|
+
const blockStop = { blockStop: options.blockStop === true, ...options.pushWait !== void 0 ? { pushWait: options.pushWait } : {} };
|
|
102
|
+
let content;
|
|
103
|
+
if (harness === "opencode") content = opencodePlugin({ blockStop: blockStop.blockStop });
|
|
104
|
+
else if (harness === "muse") content = `${JSON.stringify(musePlugin(blockStop), null, 2)}
|
|
105
|
+
`;
|
|
106
|
+
else {
|
|
107
|
+
let settings = {};
|
|
108
|
+
if (options.write && existsSync(target)) {
|
|
109
|
+
try {
|
|
110
|
+
settings = JSON.parse(readFileSync(target, "utf8"));
|
|
111
|
+
} catch (error) {
|
|
112
|
+
throw new RelayUsage(`${target} is not valid JSON (${error instanceof Error ? error.message : String(error)}); fix it or add the hooks by hand`);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
try {
|
|
116
|
+
if (harness === "agy") {
|
|
117
|
+
if (typeof settings !== "object" || settings === null || Array.isArray(settings)) throw new Error("the hooks file is not a JSON object");
|
|
118
|
+
content = `${JSON.stringify({ ...settings, [AGY_HOOK_SET]: agyHookSet({ blockStop: blockStop.blockStop }) }, null, 2)}
|
|
119
|
+
`;
|
|
120
|
+
} else
|
|
121
|
+
content = `${JSON.stringify(options.write ? mergeHooks(settings, hookEntries(harness, blockStop)) : { hooks: hookEntries(harness, blockStop) }, null, 2)}
|
|
122
|
+
`;
|
|
123
|
+
} catch (error) {
|
|
124
|
+
throw new RelayUsage(`${target}: ${error instanceof Error ? error.message : String(error)}; fix it or add the hooks by hand`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
const shared = harness === "claude" && scope === "local" ? hooksTarget("claude", "project", options.env, options.cwd) : void 0;
|
|
128
|
+
if (shared && hasOurHooks(shared)) {
|
|
129
|
+
options.out(`# ${shared} already runs the ${CLI_NAME} hooks; remove them there, or every event reports twice`);
|
|
130
|
+
}
|
|
131
|
+
if (!options.write) {
|
|
132
|
+
options.out(`# ${harness === "opencode" || harness === "muse" ? "the plugin file" : "merge into"} ${target} (or rerun with --write)`);
|
|
133
|
+
options.out(content.trimEnd());
|
|
134
|
+
} else {
|
|
135
|
+
const result = writeAtomic(target, content, now);
|
|
136
|
+
options.out(result.changed ? `wrote ${target}${result.backup ? ` (backup ${result.backup})` : ""}` : `${target} already has the ${CLI_NAME} hooks`);
|
|
137
|
+
if (result.changed) options.out("# restart sessions started before this install: harnesses read hooks only at session start");
|
|
138
|
+
}
|
|
139
|
+
for (const line of injectionNotes(harness, scope, target, options.blockStop === true, options.pushWait ?? PUSH_WAIT_DEFAULT_S)) options.out(`# ${line}`);
|
|
140
|
+
return 0;
|
|
141
|
+
}
|
|
142
|
+
function injectionNotes(harness, scope, target, blockStop, wait) {
|
|
143
|
+
const stop = `a stop names each request not yet answered once, and is held while a require_reply request waits (at most ${LIMITS.stopBlocks} times a session)`;
|
|
144
|
+
const held = `${stop}; ${blockStop ? `with ${BLOCK_STOP} it is also held while messages are unread` : `add ${BLOCK_STOP} to also hold it while messages are unread`}`;
|
|
145
|
+
const pushed = wait > 0 ? `while workers you started with ${CLI_NAME} spawn still run, a stop waits up to ${wait} s for their next message or handback and hands it to the model, which goes on (on by default, since spawning is explicit; ${PUSH_WAIT} 0 turns it off, SCP_PUSH_WAIT=<s> lowers it per session, 0 opts out); spawn itself returns at once without a terminal, so the model never backgrounds it` : `the stop never waits on workers started with ${CLI_NAME} spawn (${PUSH_WAIT} 0); ${PUSH_WAIT} <s> lets it wait for their next message`;
|
|
146
|
+
switch (harness) {
|
|
147
|
+
case "claude":
|
|
148
|
+
return [
|
|
149
|
+
`new relay messages reach the model after each tool call and with each prompt; ${held}`,
|
|
150
|
+
pushed,
|
|
151
|
+
"Grok runs these hooks too, but drops a prompt hook's context: it gets new messages after tools"
|
|
152
|
+
];
|
|
153
|
+
case "codex":
|
|
154
|
+
return [
|
|
155
|
+
`new relay messages reach the model after each tool call and with each prompt; ${held}`,
|
|
156
|
+
pushed,
|
|
157
|
+
"Codex runs new hooks only after you review them in /hooks"
|
|
158
|
+
];
|
|
159
|
+
case "agy":
|
|
160
|
+
return [
|
|
161
|
+
`agy reads nothing from a PostToolUse hook, so new relay messages go in before each model call (PreInvocation) as ephemeral steps; ${held}`,
|
|
162
|
+
AGY_UNVERIFIED
|
|
163
|
+
];
|
|
164
|
+
case "opencode":
|
|
165
|
+
return [
|
|
166
|
+
"new relay messages are appended to tool results; a prompt gets only how many are unread",
|
|
167
|
+
`OpenCode has no stop hook: when the session goes idle, the plugin starts a new turn instead of holding a stop; ${held}`
|
|
168
|
+
];
|
|
169
|
+
case "muse":
|
|
170
|
+
return [
|
|
171
|
+
`Muse loads hooks only from plugins: muse plugins install ${dirname(dirname(target))} --scope ${scope}`,
|
|
172
|
+
`new relay messages reach the model after each tool call and with each prompt; ${held}`,
|
|
173
|
+
pushed,
|
|
174
|
+
"Muse hands plugin hooks no SCP_* variables: they key the session by the harness process, so set no SCP_SESSION for Muse's shim"
|
|
175
|
+
];
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
function shellWord(word) {
|
|
179
|
+
return /^[A-Za-z0-9._/=-]+$/.test(word) ? word : `'${word.replaceAll("'", "'\\''")}'`;
|
|
180
|
+
}
|
|
181
|
+
function mcpInstall(harness, as, out) {
|
|
182
|
+
if (as !== void 0 && !validHandle(as)) throw new RelayUsage(`--as ${JSON.stringify(as)} is not a valid agent handle`);
|
|
183
|
+
const tail = (extra) => [...extra, ...as ? ["--as", as] : []];
|
|
184
|
+
const args = (extra = []) => ["mcp", ...tail(extra)];
|
|
185
|
+
const line = (cli, extra = []) => out([...cli, CLI_NAME, ...args(extra)].map(shellWord).join(" "));
|
|
186
|
+
switch (harness) {
|
|
187
|
+
case "claude":
|
|
188
|
+
line(["claude", "mcp", "add", "scp", "--"]);
|
|
189
|
+
return 0;
|
|
190
|
+
case "codex":
|
|
191
|
+
line(["codex", "mcp", "add", "scp", "--"], ["--harness", "codex"]);
|
|
192
|
+
return 0;
|
|
193
|
+
case "grok":
|
|
194
|
+
line(["grok", "mcp", "add", "scp", "--"], ["--harness", "grok"]);
|
|
195
|
+
return 0;
|
|
196
|
+
case "opencode":
|
|
197
|
+
line(["opencode", "mcp", "add", "scp", "--"], ["--harness", "opencode"]);
|
|
198
|
+
out("# that writes OpenCode's global config; for one project, add this to its opencode.json instead:");
|
|
199
|
+
out(JSON.stringify({ mcp: { scp: { type: "local", command: [CLI_NAME, ...args(["--harness", "opencode"])], enabled: true } } }, null, 2));
|
|
200
|
+
return 0;
|
|
201
|
+
case "agy":
|
|
202
|
+
line(["agy", "mcp", "add", "scp", "--"], ["--harness", "agy"]);
|
|
203
|
+
out("# that always writes your user file ~/.gemini/config/mcp_config.json; for one project, add this to <repo>/.agents/mcp_config.json instead:");
|
|
204
|
+
out(JSON.stringify({ mcpServers: { scp: { command: CLI_NAME, args: args(["--harness", "agy"]) } } }, null, 2));
|
|
205
|
+
out(`# ${AGY_UNVERIFIED}`);
|
|
206
|
+
return 0;
|
|
207
|
+
case "muse":
|
|
208
|
+
out(
|
|
209
|
+
"# Muse has no mcp add: merge this into ~/.config/muse/settings.json (never add a mcp_servers key next to mcpServers, Muse then drops every MCP server)"
|
|
210
|
+
);
|
|
211
|
+
out(JSON.stringify({ mcpServers: { scp: { type: "stdio", command: CLI_NAME, args: args(["--harness", "muse"]), mode: "optional" } } }, null, 2));
|
|
212
|
+
return 0;
|
|
213
|
+
}
|
|
214
|
+
throw new RelayUsage("mcp install takes --harness claude, codex, opencode, grok, muse or agy");
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export {
|
|
218
|
+
hooksTarget,
|
|
219
|
+
installHooks,
|
|
220
|
+
mcpInstall
|
|
221
|
+
};
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import {
|
|
2
|
+
RelayUsage
|
|
3
|
+
} from "./chunk-7KP6TAJW.js";
|
|
4
|
+
import {
|
|
5
|
+
note
|
|
6
|
+
} from "./chunk-DA3EB2YF.js";
|
|
7
|
+
|
|
8
|
+
// src/relay.ts
|
|
9
|
+
import { chmodSync, mkdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "fs";
|
|
10
|
+
import { join, dirname } from "path";
|
|
11
|
+
import { randomUUID } from "crypto";
|
|
12
|
+
import { CLI_NAME } from "@scp-dev/core";
|
|
13
|
+
var DEFAULT_RELAY = "https://app.scp.dev";
|
|
14
|
+
function configDir(env = process.env) {
|
|
15
|
+
const base = env.XDG_CONFIG_HOME || (env.HOME ? join(env.HOME, ".config") : void 0);
|
|
16
|
+
if (!base) throw new RelayUsage("neither XDG_CONFIG_HOME nor HOME is set, so there is no place for the relay configuration");
|
|
17
|
+
return join(base, "scp");
|
|
18
|
+
}
|
|
19
|
+
function checkRelayUrl(text, what = "relay URL") {
|
|
20
|
+
let url;
|
|
21
|
+
try {
|
|
22
|
+
url = new URL(text);
|
|
23
|
+
} catch {
|
|
24
|
+
throw new RelayUsage(`${what} must be an absolute https URL (http is allowed only for localhost)`);
|
|
25
|
+
}
|
|
26
|
+
if (url.protocol !== "https:" && !(url.protocol === "http:" && ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname)) || url.username || url.password) {
|
|
27
|
+
throw new RelayUsage(`${what} must be an absolute https URL without credentials (http is allowed only for localhost)`);
|
|
28
|
+
}
|
|
29
|
+
return url.origin;
|
|
30
|
+
}
|
|
31
|
+
function readObject(path) {
|
|
32
|
+
let text;
|
|
33
|
+
try {
|
|
34
|
+
text = readFileSync(path, "utf8");
|
|
35
|
+
} catch (error) {
|
|
36
|
+
if (error.code === "ENOENT") return void 0;
|
|
37
|
+
throw new RelayUsage(`could not read ${path}`);
|
|
38
|
+
}
|
|
39
|
+
try {
|
|
40
|
+
const value = JSON.parse(text);
|
|
41
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error();
|
|
42
|
+
return value;
|
|
43
|
+
} catch {
|
|
44
|
+
throw new RelayUsage(`${path} must contain a JSON object`);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function writePrivateJson(path, value) {
|
|
48
|
+
const dir = dirname(path);
|
|
49
|
+
mkdirSync(dir, { recursive: true, mode: 448 });
|
|
50
|
+
chmodSync(dir, 448);
|
|
51
|
+
const temp = join(dir, `.${randomUUID()}.tmp`);
|
|
52
|
+
try {
|
|
53
|
+
writeFileSync(temp, `${JSON.stringify(value, null, 2)}
|
|
54
|
+
`, { mode: 384, flag: "wx" });
|
|
55
|
+
chmodSync(temp, 384);
|
|
56
|
+
renameSync(temp, path);
|
|
57
|
+
} finally {
|
|
58
|
+
rmSync(temp, { force: true });
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function activeRelay(env = process.env) {
|
|
62
|
+
if (env.SCP_RELAY !== void 0) return { url: checkRelayUrl(env.SCP_RELAY, "$SCP_RELAY"), source: "SCP_RELAY" };
|
|
63
|
+
const path = join(configDir(env), "config.json");
|
|
64
|
+
const config = readObject(path);
|
|
65
|
+
if (config?.relay !== void 0) {
|
|
66
|
+
if (typeof config.relay !== "string") throw new RelayUsage(`relay in ${path} must be a URL`);
|
|
67
|
+
return { url: checkRelayUrl(config.relay, `relay in ${path}`), source: "config" };
|
|
68
|
+
}
|
|
69
|
+
return { url: DEFAULT_RELAY, source: "default" };
|
|
70
|
+
}
|
|
71
|
+
function readCredentials(env = process.env) {
|
|
72
|
+
const path = join(configDir(env), "credentials.json");
|
|
73
|
+
const entries = readObject(path) ?? {};
|
|
74
|
+
for (const entry of Object.values(entries)) {
|
|
75
|
+
if (!entry || typeof entry !== "object" || typeof entry.token !== "string" || !entry.token.trim() || typeof entry.saved_at !== "string") {
|
|
76
|
+
throw new RelayUsage(`${path} contains an invalid credential`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
return entries;
|
|
80
|
+
}
|
|
81
|
+
function saveCredential(env, origin, credential) {
|
|
82
|
+
const url = checkRelayUrl(origin);
|
|
83
|
+
writePrivateJson(join(configDir(env), "credentials.json"), { ...readCredentials(env), [url]: { ...credential, saved_at: (/* @__PURE__ */ new Date()).toISOString() } });
|
|
84
|
+
}
|
|
85
|
+
function removeCredential(env, origin) {
|
|
86
|
+
const entries = readCredentials(env);
|
|
87
|
+
const url = checkRelayUrl(origin);
|
|
88
|
+
if (!Object.hasOwn(entries, url)) return false;
|
|
89
|
+
delete entries[url];
|
|
90
|
+
writePrivateJson(join(configDir(env), "credentials.json"), entries);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
function relayCredential(env, origin) {
|
|
94
|
+
const token = env.SCP_TOKEN?.trim();
|
|
95
|
+
if (token) return { token, source: "SCP_TOKEN", problem: null };
|
|
96
|
+
const stored = readCredentials(env)[checkRelayUrl(origin)];
|
|
97
|
+
if (stored) return { token: stored.token, source: "credentials", problem: null };
|
|
98
|
+
return { token: null, source: null, problem: `no login for ${origin}: run ${CLI_NAME} login, or set SCP_TOKEN` };
|
|
99
|
+
}
|
|
100
|
+
function relayVerb(positionals, asJson, env = process.env) {
|
|
101
|
+
const [action, text] = positionals;
|
|
102
|
+
if (action === "use" && text !== void 0 && positionals.length === 2) {
|
|
103
|
+
const url = checkRelayUrl(text);
|
|
104
|
+
writePrivateJson(join(configDir(env), "config.json"), { relay: url });
|
|
105
|
+
if (asJson) console.log(JSON.stringify({ url }));
|
|
106
|
+
else console.log(`saved relay ${url}`);
|
|
107
|
+
if (env.SCP_RELAY !== void 0) note("$SCP_RELAY still selects the active relay");
|
|
108
|
+
return 0;
|
|
109
|
+
}
|
|
110
|
+
if (action === "show" && positionals.length === 1) {
|
|
111
|
+
const relay = activeRelay(env);
|
|
112
|
+
const credential = relayCredential(env, relay.url);
|
|
113
|
+
const result = { ...relay, credential: credential.token ? "found" : "missing", credential_source: credential.source };
|
|
114
|
+
if (asJson) console.log(JSON.stringify(result));
|
|
115
|
+
else console.log(`${relay.url} (${relay.source}); credential ${result.credential}${credential.source ? ` (${credential.source})` : ""}`);
|
|
116
|
+
return 0;
|
|
117
|
+
}
|
|
118
|
+
throw new RelayUsage(`${CLI_NAME} relay takes use <url> or show`);
|
|
119
|
+
}
|
|
120
|
+
async function loginMain(positionals, values) {
|
|
121
|
+
if (positionals.length) throw new RelayUsage(`${CLI_NAME} login takes only flags`);
|
|
122
|
+
return (await import("./login-JTRUWG6Q.js")).login(values);
|
|
123
|
+
}
|
|
124
|
+
function logoutMain(positionals, asJson, env = process.env) {
|
|
125
|
+
if (positionals.length) throw new RelayUsage(`${CLI_NAME} logout takes no arguments`);
|
|
126
|
+
const { url } = activeRelay(env);
|
|
127
|
+
const removed = removeCredential(env, url);
|
|
128
|
+
if (asJson) console.log(JSON.stringify({ url, removed, env_token: !!env.SCP_TOKEN?.trim() }));
|
|
129
|
+
else console.log(removed ? `logged out of ${url}` : `no stored login for ${url}`);
|
|
130
|
+
if (env.SCP_TOKEN?.trim()) note("$SCP_TOKEN still applies");
|
|
131
|
+
return 0;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export {
|
|
135
|
+
DEFAULT_RELAY,
|
|
136
|
+
configDir,
|
|
137
|
+
checkRelayUrl,
|
|
138
|
+
writePrivateJson,
|
|
139
|
+
activeRelay,
|
|
140
|
+
readCredentials,
|
|
141
|
+
saveCredential,
|
|
142
|
+
removeCredential,
|
|
143
|
+
relayCredential,
|
|
144
|
+
relayVerb,
|
|
145
|
+
loginMain,
|
|
146
|
+
logoutMain
|
|
147
|
+
};
|