passport-bridge 0.16.10 → 0.16.13
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 +16 -7
- package/dist/cli.js +6 -2
- package/dist/cliActions.js +20 -14
- package/dist/cliArgs.d.ts +3 -0
- package/dist/cliArgs.js +50 -6
- package/dist/cliAsk.d.ts +28 -0
- package/dist/cliAsk.js +49 -0
- package/dist/cliAutopilotPolicy.d.ts +15 -0
- package/dist/cliExec.d.ts +4 -0
- package/dist/cliExec.js +8 -1
- package/dist/cliGuardState.d.ts +20 -3
- package/dist/cliGuardState.js +24 -12
- package/dist/cliHook.js +4 -2
- package/dist/cliHookDecide.d.ts +16 -4
- package/dist/cliHookDecide.js +58 -14
- package/dist/cliHookDeliver.d.ts +17 -1
- package/dist/cliHookDeliver.js +70 -19
- package/dist/cliHookGuard.d.ts +9 -0
- package/dist/cliHookGuard.js +16 -2
- package/dist/cliHookInstall.d.ts +2 -0
- package/dist/cliHookInstall.js +2 -1
- package/dist/cliHookStatus.js +2 -4
- package/dist/cliKeys.d.ts +10 -3
- package/dist/cliKeys.js +60 -45
- package/dist/cliLogin.js +5 -2
- package/dist/cliMain.d.ts +7 -0
- package/dist/cliMain.js +502 -0
- package/dist/cliOutput.d.ts +3 -0
- package/dist/cliOutput.js +115 -8
- package/dist/cliPolicyV2.js +53 -2
- package/dist/cliSuggest.d.ts +19 -0
- package/dist/cliSuggest.js +57 -0
- package/dist/cliUndo.js +3 -8
- package/dist/compileCache.d.ts +8 -0
- package/dist/compileCache.js +60 -0
- package/dist/execNoKey.d.ts +9 -0
- package/dist/execNoKey.js +25 -0
- package/dist/hookMain.js +7 -11
- package/dist/hookOutbox.d.ts +15 -2
- package/dist/hookOutbox.js +143 -3
- package/dist/initKeysStep.d.ts +1 -1
- package/dist/initKeysStep.js +8 -7
- package/dist/initOutro.js +2 -1
- package/dist/initUi.d.ts +7 -2
- package/dist/initUi.js +22 -9
- package/dist/passport.d.ts +1 -7
- package/dist/passport.js +15 -499
- package/dist/pluginPin.d.ts +1 -1
- package/dist/pluginPin.js +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/webLinks.d.ts +2 -0
- package/dist/webLinks.js +7 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
package/dist/cliMain.js
ADDED
|
@@ -0,0 +1,502 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `passport` CLI. Its entry is `passport.ts`, which turns on Node's
|
|
3
|
+
* compile cache before loading this.
|
|
4
|
+
*/
|
|
5
|
+
import { writeFileSync } from "node:fs";
|
|
6
|
+
import { bool, parseArgs, repeated, value } from "./cliArgs.js";
|
|
7
|
+
import { VERSION } from "./version.js";
|
|
8
|
+
import { login } from "./cliLogin.js";
|
|
9
|
+
import { asFailure, emitFailure, emitResult } from "./cliOutput.js";
|
|
10
|
+
import { completionResult, profilesResult, useProfileResult, versionResult } from "./cliOffline.js";
|
|
11
|
+
import { describedToolPayload } from "./cliDescribe.js";
|
|
12
|
+
import { connect } from "./cliConnect.js";
|
|
13
|
+
import { firstText, hasNonText, payloadOf, refusalOf, requireSuccess } from "./cliResult.js";
|
|
14
|
+
import { callerFrom, endpoint, failure, firstSentence, holdForCommand, passportOrigin, readBoundedJson, resolveCliAuth, safeApiError, sanitize, sleep, timeoutMs } from "./cliSupport.js";
|
|
15
|
+
import { readBoundedUtf8Descriptor } from "./boundedFile.js";
|
|
16
|
+
import { linkApproval, runCliSession, writeCliSessionFailure } from "./cliSession.js";
|
|
17
|
+
export { refusalOf } from "./cliResult.js";
|
|
18
|
+
export { callerFrom, sanitize } from "./cliSupport.js";
|
|
19
|
+
const APPROVAL_RETRY_MS = 3_000;
|
|
20
|
+
const CLI_ARGUMENT_STDIN_MAX_BYTES = 1024 * 1024;
|
|
21
|
+
const CLI_POLICY_SYNC_MS = 5_000;
|
|
22
|
+
async function withGateway(args, runtime, fn) {
|
|
23
|
+
if (runtime.sharedGateway)
|
|
24
|
+
return fn(runtime.sharedGateway.gateway, runtime.sharedGateway.auth);
|
|
25
|
+
const auth = resolveCliAuth(args, runtime.env);
|
|
26
|
+
const { CliGateway } = await import("./cliClient.js");
|
|
27
|
+
const gateway = await CliGateway.connect({
|
|
28
|
+
auth,
|
|
29
|
+
caller: callerFrom(args, runtime.env),
|
|
30
|
+
timeoutMs: timeoutMs(args),
|
|
31
|
+
signal: runtime.signal
|
|
32
|
+
});
|
|
33
|
+
try {
|
|
34
|
+
await gateway.assertCliProtocol();
|
|
35
|
+
return await fn(gateway, auth);
|
|
36
|
+
}
|
|
37
|
+
finally {
|
|
38
|
+
await gateway.close();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Keep the hook guard's local copy of the member's CLI policy current. A failure
|
|
42
|
+
* or an older server leaves the previous copy, and never fails the command. */
|
|
43
|
+
async function syncCliPolicy(profile, args, runtime, api) {
|
|
44
|
+
const { refreshRules: refreshCliPolicy } = await import("./cliPolicyRefresh.js");
|
|
45
|
+
const signal = AbortSignal.any([runtime.signal, AbortSignal.timeout(Math.min(timeoutMs(args), CLI_POLICY_SYNC_MS))]);
|
|
46
|
+
await refreshCliPolicy(profile, signal, api ? { api } : {});
|
|
47
|
+
}
|
|
48
|
+
async function doctor(args, runtime) {
|
|
49
|
+
const { formatDoctorReport, runDoctor } = await import("./doctor.js");
|
|
50
|
+
const report = await runDoctor({
|
|
51
|
+
version: VERSION,
|
|
52
|
+
profile: value(args, "profile"),
|
|
53
|
+
env: runtime.env,
|
|
54
|
+
timeoutMs: timeoutMs(args),
|
|
55
|
+
signal: runtime.signal,
|
|
56
|
+
caller: callerFrom(args, runtime.env),
|
|
57
|
+
normalizeEndpoint: endpoint,
|
|
58
|
+
redactError: sanitize
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
json: report,
|
|
62
|
+
human: formatDoctorReport(report),
|
|
63
|
+
essential: true,
|
|
64
|
+
exitCode: report.ok ? 0 : 1
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
async function discover(args, runtime) {
|
|
68
|
+
return withGateway(args, runtime, async (gateway) => {
|
|
69
|
+
const { evaluateDiscoveryCompliance, scanLocalMcpConfigs } = await import("./discover.js");
|
|
70
|
+
const snapshot = scanLocalMcpConfigs(gateway.origin, runtime.env);
|
|
71
|
+
const response = await gateway.fetchApi("/api/my/discovery/scan", {
|
|
72
|
+
method: "POST",
|
|
73
|
+
headers: { "content-type": "application/json" },
|
|
74
|
+
body: JSON.stringify(snapshot),
|
|
75
|
+
signal: runtime.signal
|
|
76
|
+
});
|
|
77
|
+
if (!response.ok) {
|
|
78
|
+
const message = await safeApiError(response, "Could not submit the fleet discovery scan.");
|
|
79
|
+
return failure(message, response.status === 401 || response.status === 403 ? 2 : 1, "server");
|
|
80
|
+
}
|
|
81
|
+
const report = await readBoundedJson(response);
|
|
82
|
+
const direct = Number(report.stats?.directConnections ?? 0);
|
|
83
|
+
const review = Number(report.stats?.needsReview ?? 0);
|
|
84
|
+
const enforcement = String(report.policy?.enforcement ?? "observe");
|
|
85
|
+
const compliance = evaluateDiscoveryCompliance(report);
|
|
86
|
+
return {
|
|
87
|
+
json: { snapshot, report, compliant: compliance.compliant },
|
|
88
|
+
human: [
|
|
89
|
+
`Fleet discovery scan submitted from ${snapshot.installationLabel}.`,
|
|
90
|
+
`${direct} direct MCP configuration${direct === 1 ? "" : "s"} · ${review} awaiting review.`,
|
|
91
|
+
`Workspace policy: ${String(report.policy?.fleetMode ?? "personal")} / ${enforcement}.`,
|
|
92
|
+
!compliance.compliant
|
|
93
|
+
? `${compliance.routeRequired} must route through Passport · ${compliance.denied} denied.`
|
|
94
|
+
: "No enforced fleet-policy violation was reported."
|
|
95
|
+
].join("\n"),
|
|
96
|
+
essential: true,
|
|
97
|
+
exitCode: compliance.compliant ? 0 : 5
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
async function search(args, runtime) {
|
|
102
|
+
const query = args.positionals.join(" ").trim();
|
|
103
|
+
if (value(args, "app") !== undefined && !value(args, "app").trim())
|
|
104
|
+
return failure("--app cannot be empty.", 6, "usage");
|
|
105
|
+
return withGateway(args, runtime, async (gateway) => {
|
|
106
|
+
const result = requireSuccess(await gateway.callTool("passport_search_tools", {
|
|
107
|
+
...(query ? { query } : {}),
|
|
108
|
+
...(value(args, "app") ? { app: value(args, "app") } : {})
|
|
109
|
+
}));
|
|
110
|
+
const payload = payloadOf(result, "search");
|
|
111
|
+
const lines = [];
|
|
112
|
+
if (query) {
|
|
113
|
+
const results = Array.isArray(payload.results) ? payload.results : [];
|
|
114
|
+
for (const hit of results) {
|
|
115
|
+
lines.push(`${String(hit.tool ?? "")} · ${hit.readonly ? "read-only" : "write-capable"} · ${firstSentence(String(hit.description ?? ""))}`);
|
|
116
|
+
}
|
|
117
|
+
if (!results.length)
|
|
118
|
+
lines.push("No matching tools.");
|
|
119
|
+
}
|
|
120
|
+
else {
|
|
121
|
+
const catalog = Array.isArray(payload.catalog) ? payload.catalog : [];
|
|
122
|
+
for (const group of catalog) {
|
|
123
|
+
const tools = Array.isArray(group.tools) ? group.tools.map(String) : [];
|
|
124
|
+
lines.push(`${String(group.app ?? group.key ?? "app")} (${tools.length}, ${String(group.access ?? "access")}):`);
|
|
125
|
+
lines.push(...tools.map((tool) => ` ${tool}`));
|
|
126
|
+
}
|
|
127
|
+
if (!catalog.length)
|
|
128
|
+
lines.push("No permitted tools.");
|
|
129
|
+
}
|
|
130
|
+
if (typeof payload.note === "string")
|
|
131
|
+
lines.push(payload.note);
|
|
132
|
+
return { json: payload, human: lines.join("\n") };
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
async function describe(args, runtime) {
|
|
136
|
+
const name = args.positionals[0];
|
|
137
|
+
return withGateway(args, runtime, async (gateway) => {
|
|
138
|
+
const result = requireSuccess(await gateway.callTool("passport_search_tools", { query: name, limit: 25 }));
|
|
139
|
+
const payload = payloadOf(result, "search");
|
|
140
|
+
const results = Array.isArray(payload.results) ? payload.results : [];
|
|
141
|
+
const exact = results.find((hit) => hit.tool === name);
|
|
142
|
+
if (!exact) {
|
|
143
|
+
const suggestions = results
|
|
144
|
+
.slice(0, 5)
|
|
145
|
+
.map((hit) => String(hit.tool ?? ""))
|
|
146
|
+
.filter(Boolean);
|
|
147
|
+
const suffix = suggestions.length ? ` Near matches: ${suggestions.join(", ")}.` : "";
|
|
148
|
+
return failure(`Unknown tool "${name}".${suffix}`, 6, "unknown_tool", {
|
|
149
|
+
hint: `Run: passport search "${name}"`,
|
|
150
|
+
next: [
|
|
151
|
+
{
|
|
152
|
+
argv: ["passport", "search", name],
|
|
153
|
+
description: "Search permitted tool names."
|
|
154
|
+
}
|
|
155
|
+
]
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
const described = describedToolPayload(exact, name);
|
|
159
|
+
const schema = described.parameters ?? {};
|
|
160
|
+
const human = [
|
|
161
|
+
String(described.tool),
|
|
162
|
+
`${described.readonly ? "Read-only" : "Write-capable"}`,
|
|
163
|
+
String(described.description ?? ""),
|
|
164
|
+
"Parameters:",
|
|
165
|
+
JSON.stringify(schema, null, 2)
|
|
166
|
+
].join("\n");
|
|
167
|
+
return { json: described, human };
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
async function run(args, runtime) {
|
|
171
|
+
const tool = args.positionals[0];
|
|
172
|
+
const toolArgs = runArguments(args);
|
|
173
|
+
const waitSeconds = value(args, "wait") ? Number(value(args, "wait")) : 0;
|
|
174
|
+
return withGateway(args, runtime, async (gateway, auth) => {
|
|
175
|
+
const deadlineAt = waitSeconds ? Date.now() + waitSeconds * 1000 : 0;
|
|
176
|
+
let waitingForApproval = false;
|
|
177
|
+
let result = await gateway.callTool("passport_call", { tool, args: toolArgs });
|
|
178
|
+
for (;;) {
|
|
179
|
+
const refusal = refusalOf(result);
|
|
180
|
+
if (!refusal) {
|
|
181
|
+
if (waitingForApproval && !bool(args, "quiet"))
|
|
182
|
+
runtime.stderr.write("Approval received. Continuing...\n");
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
if (refusal.cause === "needs_connect" && auth.kind === "agent-key") {
|
|
186
|
+
return failure("This agent key cannot create a person's sign-in link.", 3, "member_profile_required", {
|
|
187
|
+
app: refusal.details.app,
|
|
188
|
+
hint: "Use an interactive member profile, delegate the agent to a member, or configure a company-managed connection.",
|
|
189
|
+
remediation: {
|
|
190
|
+
action: "use_member_profile_or_managed_connection",
|
|
191
|
+
description: "Use an interactive member profile, delegate the agent to a member, or configure a company-managed connection."
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
if (refusal.cause === "approval_pending") {
|
|
196
|
+
refusal.details.retry = {
|
|
197
|
+
command: "run",
|
|
198
|
+
tool,
|
|
199
|
+
waitSeconds: waitSeconds || 120,
|
|
200
|
+
arguments: Object.keys(toolArgs).length ? "reuse_original" : "none"
|
|
201
|
+
};
|
|
202
|
+
if (!Object.keys(toolArgs).length) {
|
|
203
|
+
refusal.details.next = [
|
|
204
|
+
{
|
|
205
|
+
argv: ["passport", "run", tool, "--wait", String(waitSeconds || 120)],
|
|
206
|
+
description: "Retry this tool and wait for approval."
|
|
207
|
+
}
|
|
208
|
+
];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
if (refusal.cause !== "approval_pending" || !waitSeconds)
|
|
212
|
+
throw refusal;
|
|
213
|
+
const remaining = deadlineAt - Date.now();
|
|
214
|
+
if (remaining <= 0)
|
|
215
|
+
throw refusal;
|
|
216
|
+
if (!waitingForApproval && !bool(args, "quiet")) {
|
|
217
|
+
const approval = refusal.details.approvalId ? ` (${refusal.details.approvalId})` : "";
|
|
218
|
+
runtime.stderr.write(`Approval pending${approval}. Waiting up to ${waitSeconds} seconds...\n`);
|
|
219
|
+
waitingForApproval = true;
|
|
220
|
+
}
|
|
221
|
+
await sleep(Math.min(APPROVAL_RETRY_MS, remaining), runtime.signal);
|
|
222
|
+
if (Date.now() >= deadlineAt)
|
|
223
|
+
throw refusal;
|
|
224
|
+
// Only a definite approval_pending retries; a thrown timeout/network error (outcome unknown) leaves.
|
|
225
|
+
result = await gateway.callTool("passport_call", { tool, args: toolArgs });
|
|
226
|
+
}
|
|
227
|
+
if (hasNonText(result) && !bool(args, "json")) {
|
|
228
|
+
return failure("The tool returned non-text content, which human mode cannot display. Use --json for calls that may return media or resources.", 1, "non_text_content");
|
|
229
|
+
}
|
|
230
|
+
return { json: result, human: firstText(result), essential: true, raw: true };
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
function runArguments(args) {
|
|
234
|
+
const positional = args.positionals[1];
|
|
235
|
+
const fromStdin = value(args, "args");
|
|
236
|
+
const sets = repeated(args, "set");
|
|
237
|
+
const jsonSets = repeated(args, "set-json");
|
|
238
|
+
if (fromStdin !== undefined && fromStdin !== "-")
|
|
239
|
+
return failure("--args only accepts '-' to read a JSON object from stdin.", 6, "usage");
|
|
240
|
+
const forms = Number(positional !== undefined) + Number(fromStdin !== undefined) + Number(sets.length + jsonSets.length > 0);
|
|
241
|
+
if (forms > 1)
|
|
242
|
+
return failure("Use one argument form: positional JSON, --args -, or --set/--set-json.", 6, "usage");
|
|
243
|
+
if (positional !== undefined)
|
|
244
|
+
return jsonObject(positional, "tool arguments");
|
|
245
|
+
if (fromStdin !== undefined)
|
|
246
|
+
return jsonObject(readBoundedUtf8Descriptor(0, CLI_ARGUMENT_STDIN_MAX_BYTES), "stdin");
|
|
247
|
+
const out = Object.create(null);
|
|
248
|
+
const seen = new Set();
|
|
249
|
+
for (const raw of sets)
|
|
250
|
+
assign(raw, false, out, seen);
|
|
251
|
+
for (const raw of jsonSets)
|
|
252
|
+
assign(raw, true, out, seen);
|
|
253
|
+
return out;
|
|
254
|
+
}
|
|
255
|
+
function assign(raw, parseJson, out, seen) {
|
|
256
|
+
const equals = raw.indexOf("=");
|
|
257
|
+
if (equals <= 0)
|
|
258
|
+
return failure(`${parseJson ? "--set-json" : "--set"} expects key=value.`, 6, "usage");
|
|
259
|
+
const key = raw.slice(0, equals).trim();
|
|
260
|
+
if (!key || key === "__proto__" || key === "prototype" || key === "constructor")
|
|
261
|
+
return failure(`Invalid argument key: ${key || "<empty>"}.`, 6, "usage");
|
|
262
|
+
if (seen.has(key))
|
|
263
|
+
return failure(`Argument "${key}" was provided more than once.`, 6, "usage");
|
|
264
|
+
seen.add(key);
|
|
265
|
+
const rawValue = raw.slice(equals + 1);
|
|
266
|
+
if (!parseJson)
|
|
267
|
+
out[key] = rawValue;
|
|
268
|
+
else {
|
|
269
|
+
try {
|
|
270
|
+
out[key] = JSON.parse(rawValue);
|
|
271
|
+
}
|
|
272
|
+
catch {
|
|
273
|
+
return failure(`--set-json ${key} contains invalid JSON.`, 6, "usage");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function jsonObject(raw, label) {
|
|
278
|
+
try {
|
|
279
|
+
const parsed = JSON.parse(raw);
|
|
280
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed))
|
|
281
|
+
throw new Error();
|
|
282
|
+
return parsed;
|
|
283
|
+
}
|
|
284
|
+
catch {
|
|
285
|
+
return failure(`${label} must be a valid JSON object.`, 6, "usage");
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async function resources(args, runtime) {
|
|
289
|
+
if (value(args, "out") !== undefined && !value(args, "out"))
|
|
290
|
+
return failure("--out cannot be empty.", 6, "usage");
|
|
291
|
+
return withGateway(args, runtime, async (gateway) => {
|
|
292
|
+
const uri = args.positionals[0];
|
|
293
|
+
if (!uri) {
|
|
294
|
+
const listed = await gateway.listResources();
|
|
295
|
+
const human = listed.resources.length
|
|
296
|
+
? listed.resources
|
|
297
|
+
.map((resource) => `${resource.uri} · ${resource.name}${resource.mimeType ? ` · ${resource.mimeType}` : ""}`)
|
|
298
|
+
.join("\n")
|
|
299
|
+
: "No resources are available.";
|
|
300
|
+
return { json: listed, human };
|
|
301
|
+
}
|
|
302
|
+
const read = await gateway.readResource(uri);
|
|
303
|
+
const out = value(args, "out");
|
|
304
|
+
if (out) {
|
|
305
|
+
const bytes = resourceBytes(read.contents);
|
|
306
|
+
writeFileSync(out, bytes);
|
|
307
|
+
return { json: { uri, out, bytes: bytes.byteLength }, human: `Wrote ${bytes.byteLength} bytes to ${out}.` };
|
|
308
|
+
}
|
|
309
|
+
if (bool(args, "json"))
|
|
310
|
+
return { json: read };
|
|
311
|
+
if (read.contents.some((content) => "blob" in content)) {
|
|
312
|
+
return failure("The resource is binary. Use --out <file> or --json.", 1, "non_text_content");
|
|
313
|
+
}
|
|
314
|
+
return {
|
|
315
|
+
json: read,
|
|
316
|
+
human: read.contents.map((content) => ("text" in content ? content.text : "")).join(""),
|
|
317
|
+
essential: true,
|
|
318
|
+
raw: true
|
|
319
|
+
};
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
function resourceBytes(contents) {
|
|
323
|
+
if (!contents.length)
|
|
324
|
+
return Buffer.alloc(0);
|
|
325
|
+
if (contents.every((content) => "text" in content))
|
|
326
|
+
return Buffer.from(contents.map((content) => ("text" in content ? content.text : "")).join(""));
|
|
327
|
+
if (contents.length === 1 && "blob" in contents[0])
|
|
328
|
+
return Buffer.from(contents[0].blob, "base64");
|
|
329
|
+
return failure("A multi-part binary resource cannot be written to one file. Use --json.", 6, "usage");
|
|
330
|
+
}
|
|
331
|
+
async function prompts(args, runtime) {
|
|
332
|
+
if (value(args, "out") !== undefined && !value(args, "out"))
|
|
333
|
+
return failure("--out cannot be empty.", 6, "usage");
|
|
334
|
+
return withGateway(args, runtime, async (gateway) => {
|
|
335
|
+
const name = args.positionals[0];
|
|
336
|
+
if (!name) {
|
|
337
|
+
const listed = await gateway.listPrompts();
|
|
338
|
+
const human = listed.prompts.length
|
|
339
|
+
? listed.prompts
|
|
340
|
+
.map((prompt) => {
|
|
341
|
+
const names = prompt.arguments
|
|
342
|
+
?.map((argument) => `${argument.name}${argument.required ? "*" : ""}`)
|
|
343
|
+
.join(", ");
|
|
344
|
+
return `${prompt.name}${names ? ` (${names})` : ""}${prompt.description ? ` · ${prompt.description}` : ""}`;
|
|
345
|
+
})
|
|
346
|
+
.join("\n")
|
|
347
|
+
: "No prompts are available.";
|
|
348
|
+
return { json: listed, human };
|
|
349
|
+
}
|
|
350
|
+
const promptArgs = Object.create(null);
|
|
351
|
+
const seen = new Set();
|
|
352
|
+
for (const raw of repeated(args, "set"))
|
|
353
|
+
assignPrompt(raw, promptArgs, seen);
|
|
354
|
+
const result = await gateway.getPrompt(name, promptArgs);
|
|
355
|
+
const out = value(args, "out");
|
|
356
|
+
if (out) {
|
|
357
|
+
const bytes = promptBytes(result.messages);
|
|
358
|
+
writeFileSync(out, bytes);
|
|
359
|
+
return { json: { name, out, bytes: bytes.byteLength }, human: `Wrote ${bytes.byteLength} bytes to ${out}.` };
|
|
360
|
+
}
|
|
361
|
+
if (bool(args, "json"))
|
|
362
|
+
return { json: result };
|
|
363
|
+
const contents = result.messages.map((message) => message.content);
|
|
364
|
+
if (contents.some((content) => content.type !== "text"))
|
|
365
|
+
return failure("The prompt contains non-text content. Use --out <file> or --json.", 1, "non_text_content");
|
|
366
|
+
return {
|
|
367
|
+
json: result,
|
|
368
|
+
human: contents.map((content) => (content.type === "text" ? content.text : "")).join(""),
|
|
369
|
+
essential: true,
|
|
370
|
+
raw: true
|
|
371
|
+
};
|
|
372
|
+
});
|
|
373
|
+
}
|
|
374
|
+
function assignPrompt(raw, out, seen) {
|
|
375
|
+
const equals = raw.indexOf("=");
|
|
376
|
+
if (equals <= 0)
|
|
377
|
+
return failure("--set expects key=value.", 6, "usage");
|
|
378
|
+
const key = raw.slice(0, equals).trim();
|
|
379
|
+
if (!key || key === "__proto__" || key === "prototype" || key === "constructor" || seen.has(key)) {
|
|
380
|
+
return failure(!key
|
|
381
|
+
? "Prompt argument key cannot be empty."
|
|
382
|
+
: seen.has(key)
|
|
383
|
+
? `Prompt argument "${key}" was provided more than once.`
|
|
384
|
+
: `Invalid prompt argument key: ${key}.`, 6, "usage");
|
|
385
|
+
}
|
|
386
|
+
seen.add(key);
|
|
387
|
+
out[key] = raw.slice(equals + 1);
|
|
388
|
+
}
|
|
389
|
+
function promptBytes(messages) {
|
|
390
|
+
const contents = messages.map((message) => message.content);
|
|
391
|
+
if (contents.every((content) => content.type === "text"))
|
|
392
|
+
return Buffer.from(contents.map((content) => content.text ?? "").join(""));
|
|
393
|
+
if (contents.length === 1 && (contents[0].type === "image" || contents[0].type === "audio") && contents[0].data) {
|
|
394
|
+
return Buffer.from(contents[0].data, "base64");
|
|
395
|
+
}
|
|
396
|
+
return failure("A multi-part binary prompt cannot be written to one file. Use --json.", 6, "usage");
|
|
397
|
+
}
|
|
398
|
+
async function dispatch(args, runtime) {
|
|
399
|
+
switch (args.command) {
|
|
400
|
+
case "init":
|
|
401
|
+
return (await import("./cliInit.js")).init(args, runtime);
|
|
402
|
+
case "login":
|
|
403
|
+
return login(args, runtime);
|
|
404
|
+
case "logout":
|
|
405
|
+
return (await import("./cliLogout.js")).logout(args, runtime);
|
|
406
|
+
case "profiles":
|
|
407
|
+
return profilesResult(args);
|
|
408
|
+
case "use":
|
|
409
|
+
return useProfileResult(args, runtime.env);
|
|
410
|
+
case "status":
|
|
411
|
+
return (await import("./cliStatus.js")).status(args, runtime, withGateway, (profile, api) => syncCliPolicy(profile, args, runtime, api));
|
|
412
|
+
case "doctor":
|
|
413
|
+
return doctor(args, runtime);
|
|
414
|
+
case "ready":
|
|
415
|
+
return (await import("./cliReady.js")).ready(args, runtime);
|
|
416
|
+
case "search":
|
|
417
|
+
return search(args, runtime);
|
|
418
|
+
case "describe":
|
|
419
|
+
return describe(args, runtime);
|
|
420
|
+
case "run":
|
|
421
|
+
return run(args, runtime);
|
|
422
|
+
case "connect":
|
|
423
|
+
return connect(args, runtime, withGateway);
|
|
424
|
+
case "resources":
|
|
425
|
+
return resources(args, runtime);
|
|
426
|
+
case "prompts":
|
|
427
|
+
return prompts(args, runtime);
|
|
428
|
+
case "discover":
|
|
429
|
+
return discover(args, runtime);
|
|
430
|
+
case "scan":
|
|
431
|
+
return (await import("./cliScan.js")).scan(args, runtime);
|
|
432
|
+
case "hook":
|
|
433
|
+
return (await import("./cliHook.js")).hook(args, runtime);
|
|
434
|
+
case "env":
|
|
435
|
+
case "rules":
|
|
436
|
+
case "actions":
|
|
437
|
+
return (await import("./cliAutopilot.js"))[args.command](args, runtime);
|
|
438
|
+
case "wait":
|
|
439
|
+
return (await import("./cliWait.js")).wait(args, runtime);
|
|
440
|
+
case "sessions":
|
|
441
|
+
return (await import("./cliSessions.js")).sessions(args, runtime);
|
|
442
|
+
case "sync":
|
|
443
|
+
return (await import("./cliSync.js")).sync(args, runtime);
|
|
444
|
+
case "exec":
|
|
445
|
+
return (await import("./cliExec.js")).exec(args, runtime);
|
|
446
|
+
case "undo":
|
|
447
|
+
return (await import("./cliUndo.js")).undo(args, runtime);
|
|
448
|
+
case "keys":
|
|
449
|
+
return (await import("./cliKeys.js")).keys(args, runtime);
|
|
450
|
+
case "setup":
|
|
451
|
+
if (value(args, "org") !== undefined)
|
|
452
|
+
return (await import("./cliSetupOrg.js")).setupOrg(args, runtime);
|
|
453
|
+
return (await import("./cliSetup.js")).setup(args, runtime, withGateway);
|
|
454
|
+
case "session":
|
|
455
|
+
return runCliSession(args, runtime, dispatch);
|
|
456
|
+
case "access":
|
|
457
|
+
return (await import("./cliAccess.js")).access(args, runtime);
|
|
458
|
+
case "completion":
|
|
459
|
+
return completionResult(args);
|
|
460
|
+
case "help":
|
|
461
|
+
return (await import("./cliHelp.js")).help(args, runtime);
|
|
462
|
+
case "version":
|
|
463
|
+
return versionResult(args, runtime.env);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
export async function main(argv = process.argv.slice(2), env = process.env, io = {
|
|
467
|
+
stdin: process.stdin,
|
|
468
|
+
stdout: process.stdout,
|
|
469
|
+
stderr: process.stderr
|
|
470
|
+
}) {
|
|
471
|
+
const controller = new AbortController();
|
|
472
|
+
const release = holdForCommand(() => controller.abort(new Error("Cancelled.")));
|
|
473
|
+
const runtime = {
|
|
474
|
+
env,
|
|
475
|
+
stdin: io.stdin ?? process.stdin,
|
|
476
|
+
stdout: io.stdout,
|
|
477
|
+
stderr: io.stderr,
|
|
478
|
+
signal: controller.signal,
|
|
479
|
+
invokedAs: io.invokedAs ?? "passport"
|
|
480
|
+
};
|
|
481
|
+
let args;
|
|
482
|
+
const wantsJson = argv.some((item) => item === "--json" || item.startsWith("--json="));
|
|
483
|
+
const wantsQuiet = argv.includes("--quiet");
|
|
484
|
+
try {
|
|
485
|
+
args = parseArgs(argv);
|
|
486
|
+
const result = await dispatch(args, runtime);
|
|
487
|
+
if (args.command !== "session")
|
|
488
|
+
emitResult(result, args, runtime);
|
|
489
|
+
return result.exitCode ?? 0;
|
|
490
|
+
}
|
|
491
|
+
catch (error) {
|
|
492
|
+
const cliError = linkApproval(asFailure(error, args && passportOrigin(args, env)), args, env);
|
|
493
|
+
if (args?.command === "session")
|
|
494
|
+
await writeCliSessionFailure(runtime, cliError);
|
|
495
|
+
else
|
|
496
|
+
emitFailure(cliError, args ? bool(args, "json") : wantsJson, args ? bool(args, "quiet") : wantsQuiet, runtime);
|
|
497
|
+
return cliError.exitCode;
|
|
498
|
+
}
|
|
499
|
+
finally {
|
|
500
|
+
release();
|
|
501
|
+
}
|
|
502
|
+
}
|
package/dist/cliOutput.d.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { type ParsedArgs } from "./cliArgs.js";
|
|
2
2
|
import { CliFailure, type CommandResult, type Runtime } from "./cliSupport.js";
|
|
3
|
+
/** What each exit code means. Scripts and agents branch on these; the
|
|
4
|
+
* meanings never change (bridge/README.md, docs/troubleshooting.mdx). */
|
|
5
|
+
export declare const EXIT_CODES: ReadonlyArray<readonly [number, string]>;
|
|
3
6
|
export declare function helpResult(args: ParsedArgs): CommandResult;
|
|
4
7
|
export declare function emitResult(result: CommandResult, args: ParsedArgs, runtime: Runtime): void;
|
|
5
8
|
/**
|