@sharpps/agent 0.0.0-stage → 1.0.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/README.md +4 -2
- package/dist/auth.d.ts +43 -0
- package/dist/auth.js +54 -0
- package/dist/auth.js.map +1 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +285 -0
- package/dist/cli.js.map +1 -0
- package/dist/client.d.ts +95 -0
- package/dist/client.js +245 -0
- package/dist/client.js.map +1 -0
- package/dist/credentials.d.ts +23 -0
- package/dist/credentials.js +27 -0
- package/dist/credentials.js.map +1 -0
- package/dist/device.d.ts +55 -0
- package/dist/device.js +111 -0
- package/dist/device.js.map +1 -0
- package/dist/host.d.ts +179 -0
- package/dist/host.js +283 -0
- package/dist/host.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +11 -0
- package/dist/index.js.map +1 -0
- package/dist/plugin.d.ts +60 -0
- package/dist/plugin.js +83 -0
- package/dist/plugin.js.map +1 -0
- package/dist/protocol.d.ts +92 -0
- package/dist/protocol.js +35 -0
- package/dist/protocol.js.map +1 -0
- package/dist/registry.d.ts +38 -0
- package/dist/registry.js +97 -0
- package/dist/registry.js.map +1 -0
- package/dist/server.d.ts +58 -0
- package/dist/server.js +220 -0
- package/dist/server.js.map +1 -0
- package/dist/service.d.ts +2 -0
- package/dist/service.js +101 -0
- package/dist/service.js.map +1 -0
- package/dist/setup.d.ts +103 -0
- package/dist/setup.js +217 -0
- package/dist/setup.js.map +1 -0
- package/package.json +40 -4
package/README.md
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sharpps/agent
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
General-purpose agent channel: one outbound connection from a machine to a server, with plugins on both sides.
|
|
4
|
+
|
|
5
|
+
Commands: `sharpps-agent install|list|login|run|serve` (see `sharpps-agent --help`).
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Who an agent's access token belongs to. Produced only by an Authenticator, never by the agent itself.
|
|
3
|
+
*/
|
|
4
|
+
export interface AgentIdentity {
|
|
5
|
+
userId: string;
|
|
6
|
+
clientId: string;
|
|
7
|
+
scopes: string[];
|
|
8
|
+
}
|
|
9
|
+
/** Turns a bearer access token into an identity, or null when it is not valid for this server. */
|
|
10
|
+
export type Authenticator = (accessToken: string) => Promise<AgentIdentity | null>;
|
|
11
|
+
/** What Clerk's token check returns (the subset used here). */
|
|
12
|
+
export interface VerifiedToken {
|
|
13
|
+
subject: string;
|
|
14
|
+
clientId: string;
|
|
15
|
+
scopes: string[];
|
|
16
|
+
revoked: boolean;
|
|
17
|
+
expired: boolean;
|
|
18
|
+
}
|
|
19
|
+
export interface ClerkAuthOptions {
|
|
20
|
+
/** Clerk secret key of the instance that issued the tokens. */
|
|
21
|
+
secretKey: string;
|
|
22
|
+
/** The OAuth client the agents sign in with (the "Replayr agent" client). A token issued to any other client is refused. */
|
|
23
|
+
clientId: string;
|
|
24
|
+
/**
|
|
25
|
+
* The server's own address. When Clerk puts an audience in a token, it must include this. Device-flow tokens carry no audience
|
|
26
|
+
* today, so for them the client check above is what binds the token to this server.
|
|
27
|
+
*/
|
|
28
|
+
audience?: string;
|
|
29
|
+
/** Clerk's issuer URL, e.g. https://clerk.example.com. Tokens must carry it as `iss`. */
|
|
30
|
+
issuer: string;
|
|
31
|
+
/** Scopes every token must hold, e.g. "offline_access". Default: none. */
|
|
32
|
+
requiredScopes?: string[];
|
|
33
|
+
/** Replaces Clerk's token check (tests, or a different identity provider with the same answer shape). */
|
|
34
|
+
verify?: (token: string) => Promise<VerifiedToken>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Clerk as the default identity provider. The token check is Clerk's own (signature, expiry, revocation). Then the issuer, the OAuth
|
|
38
|
+
* client and (when present) the audience are checked from the verified token, because the check does not report them. Anything else
|
|
39
|
+
* is refused.
|
|
40
|
+
*/
|
|
41
|
+
export declare function clerkAuthenticator(o: ClerkAuthOptions): Authenticator;
|
|
42
|
+
/** Reads the payload of a JWT. Only call after the token has been verified: this does not check the signature. */
|
|
43
|
+
export declare function jwtClaims(token: string): Record<string, unknown> | null;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { createClerkClient } from "@clerk/backend";
|
|
2
|
+
/**
|
|
3
|
+
* Clerk as the default identity provider. The token check is Clerk's own (signature, expiry, revocation). Then the issuer, the OAuth
|
|
4
|
+
* client and (when present) the audience are checked from the verified token, because the check does not report them. Anything else
|
|
5
|
+
* is refused.
|
|
6
|
+
*/
|
|
7
|
+
export function clerkAuthenticator(o) {
|
|
8
|
+
const verify = o.verify ?? (() => {
|
|
9
|
+
const clerk = createClerkClient({ secretKey: o.secretKey });
|
|
10
|
+
return (token) => clerk.idPOAuthAccessToken.verify(token);
|
|
11
|
+
})();
|
|
12
|
+
const issuer = o.issuer.replace(/\/$/, "");
|
|
13
|
+
return async (accessToken) => {
|
|
14
|
+
let t;
|
|
15
|
+
try {
|
|
16
|
+
t = await verify(accessToken);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
if (t.revoked || t.expired || !t.subject)
|
|
22
|
+
return null;
|
|
23
|
+
const claims = jwtClaims(accessToken);
|
|
24
|
+
if (!claims)
|
|
25
|
+
return null; // an opaque token cannot be checked
|
|
26
|
+
if (String(claims.iss ?? "").replace(/\/$/, "") !== issuer)
|
|
27
|
+
return null;
|
|
28
|
+
if (t.clientId !== o.clientId)
|
|
29
|
+
return null; // issued to another OAuth client
|
|
30
|
+
if (o.audience !== undefined && claims.aud !== undefined) {
|
|
31
|
+
const aud = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
|
|
32
|
+
if (!aud.includes(o.audience))
|
|
33
|
+
return null; // issued for another resource
|
|
34
|
+
}
|
|
35
|
+
const scopes = t.scopes ?? [];
|
|
36
|
+
if ((o.requiredScopes ?? []).some((s) => !scopes.includes(s)))
|
|
37
|
+
return null;
|
|
38
|
+
return { userId: t.subject, clientId: t.clientId, scopes };
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/** Reads the payload of a JWT. Only call after the token has been verified: this does not check the signature. */
|
|
42
|
+
export function jwtClaims(token) {
|
|
43
|
+
const parts = token.split(".");
|
|
44
|
+
if (parts.length !== 3)
|
|
45
|
+
return null;
|
|
46
|
+
try {
|
|
47
|
+
const v = JSON.parse(Buffer.from(parts[1], "base64url").toString("utf8"));
|
|
48
|
+
return v && typeof v === "object" ? v : null;
|
|
49
|
+
}
|
|
50
|
+
catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=auth.js.map
|
package/dist/auth.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../src/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAyCnD;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,CAAmB;IACpD,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,IAAI,CAAC,GAAG,EAAE;QAC/B,MAAM,KAAK,GAAG,iBAAiB,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC;QAC5D,OAAO,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,mBAAmB,CAAC,MAAM,CAAC,KAAK,CAA2B,CAAC;IAC9F,CAAC,CAAC,EAAE,CAAC;IACL,MAAM,MAAM,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;IAC3C,OAAO,KAAK,EAAE,WAAW,EAAE,EAAE;QAC3B,IAAI,CAAgB,CAAC;QACrB,IAAI,CAAC;YACH,CAAC,GAAG,MAAM,MAAM,CAAC,WAAW,CAAC,CA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,MAAM,EAAE,CAAC;IAC7D,CAAC,CAAC;AACJ,CAAC;AAED,kHAAkH;AAClH,MAAM,UAAU,SAAS,CAAC,KAAa;IACrC,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC/B,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACpC,IAAI,CAAC;QACH,MAAM,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,EAAE,WAAW,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;QAC3E,OAAO,CAAC,IAAI,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAE,CAA6B,CAAC,CAAC,CAAC,IAAI,CAAC;IAC5E,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC"}
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { hostname, platform, release, arch } from "node:os";
|
|
4
|
+
import { join } from "node:path";
|
|
5
|
+
import { createAgentClient } from "./client.js";
|
|
6
|
+
import { loadCredentials, saveCredentials, withTokens } from "./credentials.js";
|
|
7
|
+
import { refreshTokens, startDeviceLogin, waitForApproval } from "./device.js";
|
|
8
|
+
import { checkPrerequisites, hasBlockingFailure } from "./plugin.js";
|
|
9
|
+
import { homeDir, installPlugin, loadInstalled, loadRegistry, recordPlugin } from "./registry.js";
|
|
10
|
+
import { isEnabled, loadStoredConfig, resolvePlugin, runWizard, saveStoredConfig, terminalAsk } from "./setup.js";
|
|
11
|
+
/**
|
|
12
|
+
* sharpps-agent: install plugins, sign in, run a set of plugins on one connection, or serve the server side of plugins.
|
|
13
|
+
*
|
|
14
|
+
* sharpps-agent install <plugin...> npm packages (or local folders) that carry plugins
|
|
15
|
+
* sharpps-agent list installed plugins, their prerequisites, and sign-in status
|
|
16
|
+
* sharpps-agent login --issuer <url> --client-id <id>
|
|
17
|
+
* sharpps-agent run <plugin...> [--server wss://…] [--setup]
|
|
18
|
+
*/
|
|
19
|
+
const BOOLEAN_FLAGS = new Set(["setup", "force", "help"]);
|
|
20
|
+
function parse(argv) {
|
|
21
|
+
const flags = {};
|
|
22
|
+
const args = [];
|
|
23
|
+
for (let i = 0; i < argv.length; i++) {
|
|
24
|
+
const a = argv[i];
|
|
25
|
+
if (!a.startsWith("--")) {
|
|
26
|
+
args.push(a);
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
const key = a.slice(2);
|
|
30
|
+
if (BOOLEAN_FLAGS.has(key)) {
|
|
31
|
+
flags[key] = true;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
const next = argv[i + 1];
|
|
35
|
+
if (next === undefined || next.startsWith("--"))
|
|
36
|
+
throw new Error(`--${key} needs a value`);
|
|
37
|
+
flags[key] = next;
|
|
38
|
+
i++;
|
|
39
|
+
}
|
|
40
|
+
const [command, ...rest] = args;
|
|
41
|
+
return { command, args: rest, flags };
|
|
42
|
+
}
|
|
43
|
+
const HELP = `sharpps-agent: run plugins on one connection to a server
|
|
44
|
+
|
|
45
|
+
install <plugin...> install plugins from npm (or a local folder) and check what they need
|
|
46
|
+
list show installed plugins, their prerequisites and sign-in status
|
|
47
|
+
login sign in on this machine (device code: open the page shown and enter the code)
|
|
48
|
+
run <plugin...> set up the plugins (wizard), then connect them all on one connection
|
|
49
|
+
--server <wss://…> the server to connect to (kept for next time)
|
|
50
|
+
--setup run the wizard again even if every setting is answered
|
|
51
|
+
serve <plugin...> run the server listener with the server side of these plugins
|
|
52
|
+
--client-id <id> the OAuth client the agents sign in with (or SHARPPS_AGENT_CLIENT_ID)
|
|
53
|
+
--audience <url> optional: the listener's address, checked when Clerk puts an audience in the token
|
|
54
|
+
--port <n> default 8791
|
|
55
|
+
--bind <address> default 127.0.0.1 (a reverse proxy such as Traefik sits in front)
|
|
56
|
+
--control-port <n> local port for the apps on this server (the runner); needs SHARPPS_AGENT_CONTROL_TOKEN
|
|
57
|
+
--path <path> default /agent
|
|
58
|
+
Needs CLERK_SECRET_KEY, the issuer (--issuer or SHARPPS_AGENT_ISSUER) and the client id.
|
|
59
|
+
|
|
60
|
+
install --force install even when a required prerequisite fails
|
|
61
|
+
Files live in $SHARPPS_AGENT_HOME or ~/.sharpps-agent.`;
|
|
62
|
+
const ok = (s) => console.log(` ok ${s}`);
|
|
63
|
+
const bad = (s) => console.log(` FAIL ${s}`);
|
|
64
|
+
function printReports(name, reports) {
|
|
65
|
+
if (!reports.length)
|
|
66
|
+
return;
|
|
67
|
+
console.log(`${name}: prerequisites`);
|
|
68
|
+
for (const r of reports) {
|
|
69
|
+
if (r.result.ok)
|
|
70
|
+
ok(`${r.label}${r.result.detail ? ` (${r.result.detail})` : ""}`);
|
|
71
|
+
else {
|
|
72
|
+
bad(`${r.label}${r.required ? "" : " (optional)"}: ${r.result.message}`);
|
|
73
|
+
if (r.result.fix)
|
|
74
|
+
console.log(` fix: ${r.result.fix}`);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
const credsPath = (home) => join(home, "credentials.json");
|
|
79
|
+
const configPath = (home) => join(home, "config.json");
|
|
80
|
+
function deviceOptions(issuer, clientId) {
|
|
81
|
+
return { issuer, clientId, scope: "profile email offline_access public_metadata" };
|
|
82
|
+
}
|
|
83
|
+
async function signIn(home, issuer, clientId) {
|
|
84
|
+
const opts = deviceOptions(issuer, clientId);
|
|
85
|
+
const code = await startDeviceLogin(opts);
|
|
86
|
+
console.log(`\nSign in: open ${code.verificationUriComplete ?? code.verificationUri}`);
|
|
87
|
+
console.log(`and enter the code ${code.userCode} (valid for ${Math.round(code.expiresIn / 60)} minutes)`);
|
|
88
|
+
console.log("Waiting for approval …");
|
|
89
|
+
const tokens = await waitForApproval(opts, code);
|
|
90
|
+
const previous = await loadCredentials(credsPath(home));
|
|
91
|
+
const base = previous ?? { app: "sharpps-agent", issuer, clientId, machineId: randomUUID() };
|
|
92
|
+
const saved = withTokens({ ...base, issuer, clientId }, tokens);
|
|
93
|
+
await saveCredentials(credsPath(home), saved);
|
|
94
|
+
console.log("Signed in.");
|
|
95
|
+
return saved;
|
|
96
|
+
}
|
|
97
|
+
/** The access token for the connection. Refreshed from the stored refresh token when it is close to expiry. */
|
|
98
|
+
async function currentToken(home) {
|
|
99
|
+
let c = await loadCredentials(credsPath(home));
|
|
100
|
+
if (!c?.refreshToken)
|
|
101
|
+
throw new Error("not signed in; run: sharpps-agent login");
|
|
102
|
+
if (c.accessToken && c.accessExpiresAt && c.accessExpiresAt - Date.now() > 60_000)
|
|
103
|
+
return c.accessToken;
|
|
104
|
+
const t = await refreshTokens(deviceOptions(c.issuer, c.clientId), c.refreshToken);
|
|
105
|
+
c = withTokens(c, t);
|
|
106
|
+
await saveCredentials(credsPath(home), c);
|
|
107
|
+
return c.accessToken;
|
|
108
|
+
}
|
|
109
|
+
async function cmdInstall(home, specs, force) {
|
|
110
|
+
if (!specs.length)
|
|
111
|
+
throw new Error("name at least one plugin: sharpps-agent install <plugin…>");
|
|
112
|
+
let failed = 0;
|
|
113
|
+
for (const spec of specs) {
|
|
114
|
+
console.log(`\nInstalling ${spec} …`);
|
|
115
|
+
const { module, record } = await installPlugin(spec, home);
|
|
116
|
+
const reports = await checkPrerequisites(module);
|
|
117
|
+
printReports(module.name, reports);
|
|
118
|
+
const blocked = hasBlockingFailure(reports);
|
|
119
|
+
if (blocked && !force) {
|
|
120
|
+
bad(`${module.name} was not recorded: fix the failing prerequisites above, or add --force`);
|
|
121
|
+
failed++;
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
recordPlugin(record, home);
|
|
125
|
+
const sides = [module.client && "client", module.server && "server"].filter(Boolean).join(" + ");
|
|
126
|
+
ok(`${module.name}${record.version ? ` ${record.version}` : ""} installed (${sides})`);
|
|
127
|
+
if (module.client)
|
|
128
|
+
console.log(` next: sharpps-agent run ${module.name}`);
|
|
129
|
+
}
|
|
130
|
+
return failed ? 1 : 0;
|
|
131
|
+
}
|
|
132
|
+
async function cmdList(home) {
|
|
133
|
+
const reg = loadRegistry(home);
|
|
134
|
+
const names = Object.keys(reg.plugins);
|
|
135
|
+
if (!names.length)
|
|
136
|
+
console.log("No plugins installed. Try: sharpps-agent install <plugin>");
|
|
137
|
+
for (const name of names) {
|
|
138
|
+
const rec = reg.plugins[name];
|
|
139
|
+
console.log(`\n${name}${rec.version ? ` ${rec.version}` : ""} (${rec.spec})`);
|
|
140
|
+
try {
|
|
141
|
+
const [mod] = await loadInstalled([name], home);
|
|
142
|
+
printReports(name, await checkPrerequisites(mod));
|
|
143
|
+
}
|
|
144
|
+
catch (e) {
|
|
145
|
+
bad(e instanceof Error ? e.message : String(e));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const creds = await loadCredentials(credsPath(home));
|
|
149
|
+
console.log(`\nSign-in: ${creds?.refreshToken ? `signed in to ${creds.issuer}` : "not signed in (sharpps-agent login)"}`);
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
async function cmdLogin(home, flags) {
|
|
153
|
+
const prev = await loadCredentials(credsPath(home));
|
|
154
|
+
const issuer = flags.issuer ?? process.env.SHARPPS_AGENT_ISSUER ?? prev?.issuer;
|
|
155
|
+
const clientId = flags["client-id"] ?? process.env.SHARPPS_AGENT_CLIENT_ID ?? prev?.clientId;
|
|
156
|
+
if (!issuer || !clientId)
|
|
157
|
+
throw new Error("login needs --issuer <url> and --client-id <id> the first time");
|
|
158
|
+
await signIn(home, issuer, clientId);
|
|
159
|
+
return 0;
|
|
160
|
+
}
|
|
161
|
+
/** Makes sure every plugin's settings are answered: runs the wizard for the plugins with settings, or when asked to. */
|
|
162
|
+
async function setup(home, mods, force) {
|
|
163
|
+
const stored = await loadStoredConfig(configPath(home));
|
|
164
|
+
const withSettings = mods.filter((m) => m.config?.length);
|
|
165
|
+
const needed = withSettings.some((m) => {
|
|
166
|
+
const r = resolvePlugin(m.name, m.config, stored);
|
|
167
|
+
return r.missing.length > 0 || r.invalid.length > 0;
|
|
168
|
+
});
|
|
169
|
+
if (!force && !needed)
|
|
170
|
+
return stored;
|
|
171
|
+
console.log("\nSetup");
|
|
172
|
+
const next = await runWizard({
|
|
173
|
+
plugins: withSettings.map((m) => ({ name: m.name, config: m.config })),
|
|
174
|
+
stored,
|
|
175
|
+
ask: terminalAsk(),
|
|
176
|
+
print: (line) => console.log(line),
|
|
177
|
+
});
|
|
178
|
+
await saveStoredConfig(configPath(home), next);
|
|
179
|
+
return next;
|
|
180
|
+
}
|
|
181
|
+
async function cmdRun(home, names, flags) {
|
|
182
|
+
if (!names.length)
|
|
183
|
+
throw new Error("name at least one plugin: sharpps-agent run <plugin…>");
|
|
184
|
+
const mods = await loadInstalled(names, home);
|
|
185
|
+
let blocked = false;
|
|
186
|
+
for (const m of mods) {
|
|
187
|
+
const reports = await checkPrerequisites(m);
|
|
188
|
+
printReports(m.name, reports);
|
|
189
|
+
blocked = blocked || hasBlockingFailure(reports);
|
|
190
|
+
}
|
|
191
|
+
if (blocked)
|
|
192
|
+
throw new Error("a required prerequisite failed; fix it, then run again");
|
|
193
|
+
const stored = await setup(home, mods, flags.setup === true);
|
|
194
|
+
let creds = await loadCredentials(credsPath(home));
|
|
195
|
+
if (!creds?.refreshToken) {
|
|
196
|
+
const issuer = flags.issuer ?? process.env.SHARPPS_AGENT_ISSUER ?? creds?.issuer;
|
|
197
|
+
const clientId = flags["client-id"] ?? process.env.SHARPPS_AGENT_CLIENT_ID ?? creds?.clientId;
|
|
198
|
+
if (!issuer || !clientId)
|
|
199
|
+
throw new Error("not signed in: run sharpps-agent login --issuer <url> --client-id <id> first");
|
|
200
|
+
creds = await signIn(home, issuer, clientId);
|
|
201
|
+
}
|
|
202
|
+
const server = flags.server ?? process.env.SHARPPS_AGENT_SERVER ?? creds.server;
|
|
203
|
+
if (!server)
|
|
204
|
+
throw new Error("no server: pass --server wss://… (it is remembered next time)");
|
|
205
|
+
if (creds.server !== server)
|
|
206
|
+
await saveCredentials(credsPath(home), { ...creds, server });
|
|
207
|
+
const plugins = [];
|
|
208
|
+
for (const m of mods) {
|
|
209
|
+
if (!isEnabled(resolvePlugin(m.name, m.config ?? [], stored).values)) {
|
|
210
|
+
console.log(`${m.name}: switched off in its settings, not started`);
|
|
211
|
+
continue;
|
|
212
|
+
}
|
|
213
|
+
if (!m.client) {
|
|
214
|
+
console.log(`${m.name}: has no client side, skipped`);
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
plugins.push({ name: m.name, config: m.config, ...m.client() });
|
|
218
|
+
}
|
|
219
|
+
if (!plugins.length)
|
|
220
|
+
throw new Error("none of the plugins has a client side that is switched on");
|
|
221
|
+
const onEvent = (e) => {
|
|
222
|
+
switch (e.type) {
|
|
223
|
+
case "connecting":
|
|
224
|
+
console.log(`connecting to ${e.server} …`);
|
|
225
|
+
break;
|
|
226
|
+
case "connected":
|
|
227
|
+
console.log(`connected as "${e.name}". Running: ${plugins.map((p) => p.name).join(", ")}. Ctrl+C to stop.`);
|
|
228
|
+
break;
|
|
229
|
+
case "disconnected":
|
|
230
|
+
console.log(`disconnected (${e.reason})${e.willRetry ? ", retrying" : ""}`);
|
|
231
|
+
break;
|
|
232
|
+
case "refused":
|
|
233
|
+
console.log(`refused by the server: ${e.message}`);
|
|
234
|
+
break;
|
|
235
|
+
case "call":
|
|
236
|
+
console.log(`→ ${e.capability}`);
|
|
237
|
+
break;
|
|
238
|
+
case "call-done":
|
|
239
|
+
console.log(`← ${e.ok ? "done" : "failed"}`);
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
const client = createAgentClient({
|
|
244
|
+
server,
|
|
245
|
+
accessToken: () => currentToken(home),
|
|
246
|
+
machine: { id: creds.machineId, name: hostname(), os: `${platform()} ${release()} ${arch()}` },
|
|
247
|
+
agentVersion: "0.2.0-alpha.0",
|
|
248
|
+
plugins,
|
|
249
|
+
config: stored,
|
|
250
|
+
onEvent,
|
|
251
|
+
});
|
|
252
|
+
const stop = async () => { console.log("\nstopping …"); await client.stop(); process.exit(0); };
|
|
253
|
+
process.once("SIGINT", stop);
|
|
254
|
+
process.once("SIGTERM", stop);
|
|
255
|
+
try {
|
|
256
|
+
await client.ready;
|
|
257
|
+
}
|
|
258
|
+
catch (e) {
|
|
259
|
+
console.log(e instanceof Error ? e.message : String(e));
|
|
260
|
+
await client.stop();
|
|
261
|
+
return 1;
|
|
262
|
+
}
|
|
263
|
+
await new Promise(() => { }); // runs until stopped
|
|
264
|
+
return 0;
|
|
265
|
+
}
|
|
266
|
+
async function main() {
|
|
267
|
+
const { command, args, flags } = parse(process.argv.slice(2));
|
|
268
|
+
const home = homeDir();
|
|
269
|
+
if (!command || command === "help" || flags.help) {
|
|
270
|
+
console.log(HELP);
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
273
|
+
switch (command) {
|
|
274
|
+
case "install": return cmdInstall(home, args, flags.force === true);
|
|
275
|
+
case "list": return cmdList(home);
|
|
276
|
+
case "login": return cmdLogin(home, flags);
|
|
277
|
+
case "run": return cmdRun(home, args, flags);
|
|
278
|
+
default:
|
|
279
|
+
console.log(HELP);
|
|
280
|
+
return 2;
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
main().then((code) => { if (code !== 0)
|
|
284
|
+
process.exitCode = code; }, (e) => { console.error(`error: ${e instanceof Error ? e.message : String(e)}`); process.exitCode = 1; });
|
|
285
|
+
//# sourceMappingURL=cli.js.map
|
package/dist/cli.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,OAAO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
|
2
|
+
import { type ConfigParam, type PluginValues, type StoredConfig } from "./setup.js";
|
|
3
|
+
/**
|
|
4
|
+
* The client side of @sharpps/agent: one outbound WebSocket to a server, carrying any number of plugins. Each plugin is an agent
|
|
5
|
+
* module with its own capabilities; the server calls a capability by name and the client routes the call to the plugin that owns it.
|
|
6
|
+
*/
|
|
7
|
+
export type CapabilityHandler = (input: unknown, ctx: CallContext) => unknown | Promise<unknown>;
|
|
8
|
+
export interface ClientCapability {
|
|
9
|
+
description?: string;
|
|
10
|
+
handler: CapabilityHandler;
|
|
11
|
+
}
|
|
12
|
+
export interface AgentPlugin {
|
|
13
|
+
/** Unique within this client. Shown to the server in hello. */
|
|
14
|
+
name: string;
|
|
15
|
+
capabilities: Record<string, ClientCapability>;
|
|
16
|
+
/** Settings this plugin needs from the person who installs it. The setup wizard asks for them; see setup.ts. */
|
|
17
|
+
config?: ConfigParam[];
|
|
18
|
+
/** Runs once when the client starts, with the resolved settings. Throw to stop the client from starting. */
|
|
19
|
+
start?(values: PluginValues): void | Promise<void>;
|
|
20
|
+
/** Runs when the client stops. Running calls are already cancelled when this runs. */
|
|
21
|
+
stop?(): void | Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
/** Handed to a capability handler for one call. */
|
|
24
|
+
export interface CallContext {
|
|
25
|
+
callId: string;
|
|
26
|
+
/**
|
|
27
|
+
* Receives the bytes the server sends on streams of this call. `onEnd` runs when the server ends a stream.
|
|
28
|
+
* Use it to pass data on (see `stream`) or to store it.
|
|
29
|
+
*/
|
|
30
|
+
inbound(onData: (streamId: number, payload: Uint8Array, kind: "text" | "binary") => void, onEnd?: (streamId: number) => void): void;
|
|
31
|
+
/** Ends a stream this call sent, so the server knows no more bytes will come. */
|
|
32
|
+
streamEnd(streamId: number): void;
|
|
33
|
+
/** Aborts when the server cancels the call or the connection drops. Check it in long work. */
|
|
34
|
+
signal: AbortSignal;
|
|
35
|
+
/** Send progress to the server while the call runs. */
|
|
36
|
+
event(data: unknown): void;
|
|
37
|
+
/** Send data on a stream of this call (for example a browser connection). `kind` keeps text and binary messages apart. */
|
|
38
|
+
stream(streamId: number, payload: Uint8Array, kind?: "text" | "binary"): void;
|
|
39
|
+
}
|
|
40
|
+
export interface ClientOptions {
|
|
41
|
+
/** wss://your-server/agent */
|
|
42
|
+
server: string;
|
|
43
|
+
/** Returns a current access token. Called before every connection attempt, so refresh it here. */
|
|
44
|
+
accessToken(): Promise<string>;
|
|
45
|
+
machine: MachineInfo;
|
|
46
|
+
agentVersion: string;
|
|
47
|
+
plugins: AgentPlugin[];
|
|
48
|
+
/** The answers stored by the setup wizard. Missing required answers stop the client before it connects. */
|
|
49
|
+
config?: StoredConfig;
|
|
50
|
+
/** Where the settings come from first (environment variables SHARPPS_AGENT_<PLUGIN>_<KEY>). Default: process.env. */
|
|
51
|
+
env?: NodeJS.ProcessEnv;
|
|
52
|
+
/** Keep reconnecting after a drop (default true). A refusal from the server is never retried. */
|
|
53
|
+
reconnect?: boolean;
|
|
54
|
+
/** Largest wait between reconnect attempts, in ms. Default 30 s. */
|
|
55
|
+
maxBackoffMs?: number;
|
|
56
|
+
heartbeatMs?: number;
|
|
57
|
+
log?: (msg: string, extra?: Record<string, unknown>) => void;
|
|
58
|
+
onEvent?: (e: ClientEvent) => void;
|
|
59
|
+
}
|
|
60
|
+
export type ClientEvent = {
|
|
61
|
+
type: "connecting";
|
|
62
|
+
server: string;
|
|
63
|
+
} | {
|
|
64
|
+
type: "connected";
|
|
65
|
+
agentId: string;
|
|
66
|
+
name: string;
|
|
67
|
+
} | {
|
|
68
|
+
type: "disconnected";
|
|
69
|
+
reason: string;
|
|
70
|
+
willRetry: boolean;
|
|
71
|
+
} | {
|
|
72
|
+
type: "refused";
|
|
73
|
+
code: string;
|
|
74
|
+
message: string;
|
|
75
|
+
} | {
|
|
76
|
+
type: "call";
|
|
77
|
+
capability: string;
|
|
78
|
+
callId: string;
|
|
79
|
+
} | {
|
|
80
|
+
type: "call-done";
|
|
81
|
+
callId: string;
|
|
82
|
+
ok: boolean;
|
|
83
|
+
};
|
|
84
|
+
export interface AgentClient {
|
|
85
|
+
/** Resolves once the server accepts the connection; rejects if it refuses. */
|
|
86
|
+
ready: Promise<{
|
|
87
|
+
agentId: string;
|
|
88
|
+
name: string;
|
|
89
|
+
}>;
|
|
90
|
+
stop(): Promise<void>;
|
|
91
|
+
readonly connected: boolean;
|
|
92
|
+
/** Capabilities this client offers, across all plugins. */
|
|
93
|
+
readonly capabilities: string[];
|
|
94
|
+
}
|
|
95
|
+
export declare function createAgentClient(o: ClientOptions): AgentClient;
|