@zvada/agent-server 0.3.23 → 0.3.25
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/CHANGELOG.md +64 -0
- package/README.md +14 -5
- package/dist/accounts/chatgpt.js +144 -0
- package/dist/accounts/check.js +71 -0
- package/dist/accounts/claude.js +80 -0
- package/dist/accounts/endpoint.js +104 -0
- package/dist/accounts/http.js +58 -0
- package/dist/accounts/index.js +9 -0
- package/dist/client/client.js +745 -0
- package/dist/client/connection.js +338 -0
- package/dist/client/history.js +49 -0
- package/dist/client/index.js +6 -0
- package/dist/client/listeners.js +19 -0
- package/dist/client/logins.js +92 -0
- package/dist/client/rpc.js +105 -0
- package/dist/client/streams.js +432 -0
- package/dist/client/transports.js +118 -0
- package/dist/client/turns.js +122 -0
- package/dist/core/agents/acp/acp-agent.js +425 -0
- package/dist/core/agents/acp/adapter.js +231 -0
- package/dist/core/agents/acp/client.js +222 -0
- package/dist/core/agents/acp/known-agents.js +57 -0
- package/dist/core/agents/acp/mappings.js +164 -0
- package/dist/core/agents/acp/requests.js +79 -0
- package/dist/core/agents/attachments.js +14 -0
- package/dist/core/agents/base.js +64 -0
- package/dist/core/agents/claude-code/adapter.js +474 -0
- package/dist/core/agents/claude-code/approvals.js +245 -0
- package/dist/core/agents/claude-code/claude-agent.js +319 -0
- package/dist/core/agents/claude-code/credentials.js +142 -0
- package/dist/core/agents/claude-code/events.js +11 -0
- package/dist/core/agents/claude-code/login.js +217 -0
- package/dist/core/agents/claude-code/mcp.js +181 -0
- package/dist/core/agents/claude-code/models.js +74 -0
- package/dist/core/agents/claude-code/options.js +240 -0
- package/dist/core/agents/claude-code/outcome.js +204 -0
- package/dist/core/agents/claude-code/results.js +58 -0
- package/dist/core/agents/claude-code/sdk-pair.js +59 -0
- package/dist/core/agents/claude-code/session.js +593 -0
- package/dist/core/agents/claude-code/spawn.js +125 -0
- package/dist/core/agents/claude-code/spend.js +118 -0
- package/dist/core/agents/claude-code/tools.js +131 -0
- package/dist/core/agents/claude-code/turn-usage.js +121 -0
- package/dist/core/agents/claude-code/turn.js +199 -0
- package/dist/core/agents/codex-app-server/adapter.js +463 -0
- package/dist/core/agents/codex-app-server/client.js +338 -0
- package/dist/core/agents/codex-app-server/codex-agent.js +294 -0
- package/dist/core/agents/codex-app-server/commands.js +184 -0
- package/dist/core/agents/codex-app-server/credentials.js +92 -0
- package/dist/core/agents/codex-app-server/effort.js +13 -0
- package/dist/core/agents/codex-app-server/events.js +18 -0
- package/dist/core/agents/codex-app-server/homes.js +118 -0
- package/dist/core/agents/codex-app-server/input.js +52 -0
- package/dist/core/agents/codex-app-server/items.js +307 -0
- package/dist/core/agents/codex-app-server/login.js +183 -0
- package/dist/core/agents/codex-app-server/mcp.js +323 -0
- package/dist/core/agents/codex-app-server/models.js +160 -0
- package/dist/core/agents/codex-app-server/options.js +155 -0
- package/dist/core/agents/codex-app-server/plan-approval.js +124 -0
- package/dist/core/agents/codex-app-server/protocol.js +38 -0
- package/dist/core/agents/codex-app-server/requests.js +250 -0
- package/dist/core/agents/codex-app-server/session.js +278 -0
- package/dist/core/agents/codex-app-server/subagents.js +260 -0
- package/dist/core/agents/codex-app-server/thread-settings.js +214 -0
- package/dist/core/agents/codex-app-server/turn-items.js +69 -0
- package/dist/core/agents/codex-app-server/turn.js +448 -0
- package/dist/core/agents/codex-app-server/user-login.js +78 -0
- package/dist/core/agents/config-fingerprint.js +10 -0
- package/dist/core/agents/credentials.js +71 -0
- package/dist/core/agents/diffs.js +27 -0
- package/dist/core/agents/elicitation.js +42 -0
- package/dist/core/agents/inherit-config.js +5 -0
- package/dist/core/agents/login.js +56 -0
- package/dist/core/agents/mcp-config.js +109 -0
- package/dist/core/agents/plan.js +6 -0
- package/dist/core/agents/process-pool.js +307 -0
- package/dist/core/agents/registry.js +49 -0
- package/dist/core/agents/scripted/adapter.js +86 -0
- package/dist/core/agents/scripted/mcp-call.js +74 -0
- package/dist/core/agents/scripted/scripted-agent.js +331 -0
- package/dist/core/agents/scripted/scripts.js +154 -0
- package/dist/core/agents/scripted/steps.js +87 -0
- package/dist/core/agents/session-agent.js +209 -0
- package/dist/core/agents/stderr-tail.js +71 -0
- package/dist/core/agents/thinking-levels.js +9 -0
- package/dist/core/agents/types.js +1 -0
- package/dist/core/diagnostics.js +23 -0
- package/dist/core/index.js +31 -0
- package/dist/core/presets.js +114 -0
- package/dist/core/processes/identity.js +92 -0
- package/dist/core/processes/lifeline.js +218 -0
- package/dist/core/processes/native-processes.js +299 -0
- package/dist/core/processes/process-table.js +164 -0
- package/dist/core/provision/cache-trust.js +121 -0
- package/dist/core/provision/cache-usage.js +61 -0
- package/dist/core/provision/checksums.js +22 -0
- package/dist/core/provision/extract.js +29 -0
- package/dist/core/provision/index.js +4 -0
- package/dist/core/provision/install-build.js +99 -0
- package/dist/core/provision/npm.js +316 -0
- package/dist/core/provision/pins.js +35 -0
- package/dist/core/provision/platform.js +65 -0
- package/dist/core/provision/provisioner.js +372 -0
- package/dist/core/provision/releases.js +68 -0
- package/dist/core/provision/tools.js +279 -0
- package/dist/core/proxy/anthropic-proxy.js +163 -0
- package/dist/core/proxy/api-key-store.js +30 -0
- package/dist/core/proxy/index.js +3 -0
- package/dist/core/proxy/loopback.js +86 -0
- package/dist/core/runtime/agent-runtime.js +437 -0
- package/dist/core/runtime/event-processor.js +379 -0
- package/dist/core/runtime/event-sink.js +65 -0
- package/dist/core/runtime/harness-clis.js +83 -0
- package/dist/core/runtime/harness-logins.js +313 -0
- package/dist/core/runtime/login-reads.js +71 -0
- package/dist/core/runtime/model-lists.js +160 -0
- package/dist/core/runtime/permission-policy.js +84 -0
- package/dist/core/runtime/request-broker.js +139 -0
- package/dist/core/runtime/steering.js +79 -0
- package/dist/core/runtime/turn-outcome.js +16 -0
- package/dist/core/runtime/turn.js +339 -0
- package/dist/core/utils/bounded.js +13 -0
- package/dist/core/utils/deadline.js +57 -0
- package/dist/core/utils/errors.js +176 -0
- package/dist/core/utils/is-record.js +4 -0
- package/dist/core/utils/loopback.js +11 -0
- package/dist/index.js +4 -0
- package/dist/operator/index.js +10 -0
- package/dist/operator/mcp.js +139 -0
- package/dist/operator/operator.js +499 -0
- package/dist/operator/render.js +408 -0
- package/dist/operator/view.js +35 -0
- package/dist/protocol/account.js +96 -0
- package/dist/protocol/async-queue.js +65 -0
- package/dist/protocol/auth.js +169 -0
- package/dist/protocol/canonical.js +18 -0
- package/dist/protocol/clis.js +63 -0
- package/dist/protocol/config.js +137 -0
- package/dist/protocol/conversation.js +231 -0
- package/dist/protocol/credentials.js +142 -0
- package/dist/protocol/errors.js +190 -0
- package/dist/protocol/execution.js +28 -0
- package/dist/protocol/factories.js +231 -0
- package/dist/protocol/guards.js +71 -0
- package/dist/protocol/harness.js +93 -0
- package/dist/protocol/history.js +201 -0
- package/dist/protocol/ids.js +37 -0
- package/dist/protocol/index.js +36 -0
- package/dist/protocol/lifecycle.js +562 -0
- package/dist/protocol/meta-keys.js +17 -0
- package/dist/protocol/meta.js +13 -0
- package/dist/protocol/model-catalog.js +223 -0
- package/dist/protocol/models.js +84 -0
- package/dist/protocol/part-input.js +94 -0
- package/dist/protocol/parts.js +166 -0
- package/dist/protocol/plan.js +31 -0
- package/dist/protocol/reduce.js +484 -0
- package/dist/protocol/selectors.js +136 -0
- package/dist/protocol/seq-cursor.js +37 -0
- package/dist/protocol/stop-reasons.js +41 -0
- package/dist/protocol/thinking.js +31 -0
- package/dist/protocol/time.js +21 -0
- package/dist/protocol/tokens.js +57 -0
- package/dist/protocol/tool-closure.js +70 -0
- package/dist/protocol/tool-state.js +153 -0
- package/dist/protocol/verify.js +467 -0
- package/dist/protocol/vocabulary.js +27 -0
- package/dist/protocol/wire-api.js +94 -0
- package/dist/protocol/wire.js +669 -0
- package/dist/server/acp/binding.js +251 -0
- package/dist/server/acp/session-records.js +53 -0
- package/dist/server/acp/translate.js +401 -0
- package/dist/server/agent-server.js +338 -0
- package/dist/server/bin.js +492 -0
- package/dist/server/feedback.js +216 -0
- package/dist/server/history.js +123 -0
- package/dist/server/index.js +8 -0
- package/dist/server/install.js +171 -0
- package/dist/server/open-parts.js +87 -0
- package/dist/server/routes.js +292 -0
- package/dist/server/secrets.js +19 -0
- package/dist/server/session-gate.js +54 -0
- package/dist/server/session-log.js +66 -0
- package/dist/server/session-store.js +243 -0
- package/dist/server/subscriptions.js +189 -0
- package/dist/server/transports.js +214 -0
- package/dist/server/wire-error.js +26 -0
- package/dist/sync/event-log.js +89 -0
- package/dist/sync/fit.js +202 -0
- package/dist/sync/history-reader.js +33 -0
- package/dist/sync/history-sqlite.js +281 -0
- package/dist/sync/history.js +163 -0
- package/dist/sync/index.js +5 -0
- package/dist/sync/journal-sqlite.js +316 -0
- package/dist/sync/journal.js +10 -0
- package/dist/sync/live-tails.js +85 -0
- package/dist/sync/node-sqlite.js +27 -0
- package/dist/sync/projection.js +32 -0
- package/dist/sync/recovery.js +117 -0
- package/dist/sync/retry.js +45 -0
- package/dist/sync/schema.js +73 -0
- package/dist/sync/sql.js +94 -0
- package/dist/sync/sqlite-file.js +31 -0
- package/dist/sync/sqlite.js +18 -0
- package/dist/sync/worker.js +212 -0
- package/dist/types/client/client.d.ts +6 -1
- package/dist/types/client/client.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/approvals.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts +4 -1
- package/dist/types/core/agents/claude-code/claude-agent.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/options.d.ts +28 -11
- package/dist/types/core/agents/claude-code/options.d.ts.map +1 -1
- package/dist/types/core/agents/claude-code/session.d.ts +8 -0
- package/dist/types/core/agents/claude-code/session.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/homes.d.ts +6 -0
- package/dist/types/core/agents/codex-app-server/homes.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/login.d.ts +12 -0
- package/dist/types/core/agents/codex-app-server/login.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/requests.d.ts.map +1 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts +4 -1
- package/dist/types/core/agents/codex-app-server/session.d.ts.map +1 -1
- package/dist/types/core/agents/process-pool.d.ts +8 -0
- package/dist/types/core/agents/process-pool.d.ts.map +1 -1
- package/dist/types/core/agents/scripted/scripted-agent.d.ts.map +1 -1
- package/dist/types/core/agents/scripted/steps.d.ts +1 -1
- package/dist/types/core/presets.d.ts +2 -2
- package/dist/types/core/presets.d.ts.map +1 -1
- package/dist/types/core/processes/identity.d.ts.map +1 -1
- package/dist/types/core/processes/lifeline.d.ts.map +1 -1
- package/dist/types/core/processes/process-table.d.ts.map +1 -1
- package/dist/types/core/provision/extract.d.ts.map +1 -1
- package/dist/types/core/runtime/agent-runtime.d.ts +4 -1
- package/dist/types/core/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/types/core/runtime/turn.d.ts +12 -7
- package/dist/types/core/runtime/turn.d.ts.map +1 -1
- package/dist/types/protocol/clis.d.ts +3 -3
- package/dist/types/protocol/conversation.d.ts +6 -6
- package/dist/types/protocol/credentials.d.ts +13 -1
- package/dist/types/protocol/credentials.d.ts.map +1 -1
- package/dist/types/protocol/errors.d.ts +6 -4
- package/dist/types/protocol/errors.d.ts.map +1 -1
- package/dist/types/protocol/lifecycle.d.ts +4 -4
- package/dist/types/protocol/wire-api.d.ts +12 -12
- package/dist/types/protocol/wire.d.ts +8 -8
- package/dist/types/server/bin.d.ts +3 -0
- package/dist/types/server/bin.d.ts.map +1 -0
- package/dist/types/server/routes.d.ts.map +1 -1
- package/dist/types/server/transports.d.ts +2 -1
- package/dist/types/server/transports.d.ts.map +1 -1
- package/dist/types/sync/node-sqlite.d.ts +4 -1
- package/dist/types/sync/node-sqlite.d.ts.map +1 -1
- package/docs/consuming.md +56 -7
- package/docs/deploy.md +35 -0
- package/docs/harnesses.md +30 -5
- package/package.json +43 -18
- package/src/client/client.ts +114 -44
- package/src/core/agents/claude-code/approvals.ts +5 -1
- package/src/core/agents/claude-code/claude-agent.ts +32 -5
- package/src/core/agents/claude-code/options.ts +28 -11
- package/src/core/agents/claude-code/session.ts +13 -0
- package/src/core/agents/codex-app-server/homes.ts +11 -2
- package/src/core/agents/codex-app-server/login.ts +30 -0
- package/src/core/agents/codex-app-server/requests.ts +7 -6
- package/src/core/agents/codex-app-server/session.ts +11 -2
- package/src/core/agents/process-pool.ts +30 -4
- package/src/core/agents/scripted/scripted-agent.ts +2 -1
- package/src/core/agents/scripted/steps.ts +1 -1
- package/src/core/presets.ts +3 -2
- package/src/core/processes/identity.ts +1 -0
- package/src/core/processes/lifeline.ts +10 -1
- package/src/core/processes/process-table.ts +1 -0
- package/src/core/provision/extract.ts +1 -0
- package/src/core/runtime/agent-runtime.ts +6 -3
- package/src/core/runtime/turn.ts +21 -9
- package/src/protocol/credentials.ts +19 -5
- package/src/protocol/errors.ts +12 -3
- package/src/server/bin.ts +25 -10
- package/src/server/routes.ts +3 -2
- package/src/server/transports.ts +6 -1
- package/src/sync/node-sqlite.ts +19 -3
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import { accountLabel, canonicalJson, generateUUIDv7, } from "../../protocol/index.js";
|
|
2
|
+
import { withTimeout } from "../utils/deadline.js";
|
|
3
|
+
import { AgentServerError, InvalidRequestError, UnknownLoginError } from "../utils/errors.js";
|
|
4
|
+
import { LoginReads } from "./login-reads.js";
|
|
5
|
+
/** How long a login waits for its user. */
|
|
6
|
+
const LOGIN_TTL_MS = 10 * 60_000;
|
|
7
|
+
/** How long the CLI has to say where to sign in, inside a client's 30 s request deadline. */
|
|
8
|
+
const LOGIN_START_TIMEOUT_MS = 20_000;
|
|
9
|
+
/** A status read is reused this long once it answered: a settings page that polls starts no CLI each time. */
|
|
10
|
+
const STATUS_TTL_MS = 2_000;
|
|
11
|
+
/** A status read that takes longer (a CLI slow to start) answers `unknown`. */
|
|
12
|
+
const STATUS_TIMEOUT_MS = 20_000;
|
|
13
|
+
const message = (error) => (error instanceof Error ? error.message : String(error));
|
|
14
|
+
/**
|
|
15
|
+
* The harness and the store a turn with this `env` would use, by the whole
|
|
16
|
+
* `env`: two envs that reach one store by different variables count as two,
|
|
17
|
+
* which a product that sends its turns' `env` each time never meets.
|
|
18
|
+
*/
|
|
19
|
+
const storeKey = (harness, env) => canonicalJson([harness, env ?? {}]);
|
|
20
|
+
/**
|
|
21
|
+
* Each harness's login, read from its CLI, and the logins under way. A login
|
|
22
|
+
* that succeeds and a logout change the login, and so does a read that
|
|
23
|
+
* differs from the last one of its store (a login in a terminal): `onChanged`
|
|
24
|
+
* hears it, with the store as the harness names it (each of that store's
|
|
25
|
+
* sessions starts its next turn on a new CLI, model lists are read again),
|
|
26
|
+
* and `onUpdated` listeners are told, as they are when a login starts
|
|
27
|
+
* and when it ends. Every login that leaves here carries the store's login
|
|
28
|
+
* under way (`loginUnderWay`), added as it leaves: a reading kept for its
|
|
29
|
+
* store never holds it, so a login starting or ending is no change of the
|
|
30
|
+
* login.
|
|
31
|
+
*/
|
|
32
|
+
export class HarnessLogins {
|
|
33
|
+
agentFor;
|
|
34
|
+
onChanged;
|
|
35
|
+
options;
|
|
36
|
+
/** Logins under way, until their end is told. */
|
|
37
|
+
logins = new Map();
|
|
38
|
+
reads;
|
|
39
|
+
listeners = new Set();
|
|
40
|
+
stopped = false;
|
|
41
|
+
constructor(agentFor, onChanged, options = {}) {
|
|
42
|
+
this.agentFor = agentFor;
|
|
43
|
+
this.onChanged = onChanged;
|
|
44
|
+
this.options = options;
|
|
45
|
+
this.reads = new LoginReads(() => this.now(), options.statusTtlMs ?? STATUS_TTL_MS);
|
|
46
|
+
}
|
|
47
|
+
now() {
|
|
48
|
+
return this.options.now?.() ?? Date.now();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* The login changed in the store a turn with this `env` uses, as its
|
|
52
|
+
* harness names it. Status reads stay keyed by the whole `env`, which can
|
|
53
|
+
* differ in more than the store.
|
|
54
|
+
*/
|
|
55
|
+
changed(harness, env) {
|
|
56
|
+
// @ref LLP 0011#a-login-change-starts-the-next-turn-on-a-new-cli — the person's own store is every store's
|
|
57
|
+
// A change in the default store (no store variable: the person's own)
|
|
58
|
+
// is a change in every store, none named: a product may link or hand
|
|
59
|
+
// that login into stores of its sessions' own (Deus links
|
|
60
|
+
// `~/.codex/auth.json` into each session's `CODEX_HOME`).
|
|
61
|
+
const store = this.agentFor(harness).login?.store?.(env) || undefined;
|
|
62
|
+
this.onChanged(harness, store);
|
|
63
|
+
}
|
|
64
|
+
/** Each harness's login: a read under way, or one a moment old, is shared. */
|
|
65
|
+
status(harnesses, env) {
|
|
66
|
+
return Promise.all(harnesses.map((harness) => this.statusOf(harness, env)));
|
|
67
|
+
}
|
|
68
|
+
/** One harness's login, with the store's login under way. */
|
|
69
|
+
async statusOf(harness, env) {
|
|
70
|
+
const store = storeKey(harness, env);
|
|
71
|
+
const recent = this.reads.recent(store);
|
|
72
|
+
if (recent)
|
|
73
|
+
return this.withLogin((await recent).auth, store);
|
|
74
|
+
const { auth, differs } = await this.reread(harness, env);
|
|
75
|
+
// Changed where the engine did not change it: a login in a terminal.
|
|
76
|
+
if (differs) {
|
|
77
|
+
this.changed(harness, env);
|
|
78
|
+
this.emit({ auth: this.withLogin(auth, store) });
|
|
79
|
+
}
|
|
80
|
+
return this.withLogin(auth, store);
|
|
81
|
+
}
|
|
82
|
+
// @ref LLP 0011#the-wire — every reader sees a login under way, and can cancel it
|
|
83
|
+
/**
|
|
84
|
+
* The login as it leaves here: with the store's login under way, as
|
|
85
|
+
* `auth/login` answered it, when there is one. Made at the moment it is
|
|
86
|
+
* told, so no answer or update shows a login a newer one replaced.
|
|
87
|
+
*/
|
|
88
|
+
withLogin(auth, store) {
|
|
89
|
+
const login = this.underWay().find((candidate) => candidate.store === store);
|
|
90
|
+
return login ? { ...auth, loginUnderWay: { ...login.answer } } : auth;
|
|
91
|
+
}
|
|
92
|
+
reread(harness, env) {
|
|
93
|
+
return this.reads.read(storeKey(harness, env), () => this.read(harness, env));
|
|
94
|
+
}
|
|
95
|
+
/** Its login and the ways to sign in, each read apart: a login that cannot be read can still be made. */
|
|
96
|
+
async read(harness, env) {
|
|
97
|
+
const cli = this.agentFor(harness).login;
|
|
98
|
+
if (!cli) {
|
|
99
|
+
return {
|
|
100
|
+
harness,
|
|
101
|
+
state: "unknown",
|
|
102
|
+
methods: [],
|
|
103
|
+
error: "This harness's agent signs in on its own.",
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
const timeout = this.options.statusTimeoutMs ?? STATUS_TIMEOUT_MS;
|
|
107
|
+
const stop = new AbortController();
|
|
108
|
+
const [methods, status] = await Promise.all([
|
|
109
|
+
withTimeout(cli.methods({ ...(env && { env }) }), timeout).then((read) => read?.value ?? [], () => []),
|
|
110
|
+
withTimeout(cli.status({ ...(env && { env }), signal: stop.signal }), timeout).then((read) => {
|
|
111
|
+
if (read)
|
|
112
|
+
return read.value;
|
|
113
|
+
stop.abort();
|
|
114
|
+
return { state: "unknown", error: "The CLI did not say in time." };
|
|
115
|
+
}, (error) => ({ state: "unknown", error: message(error) })),
|
|
116
|
+
]);
|
|
117
|
+
const label = accountLabel(harness, status);
|
|
118
|
+
return { harness, ...status, ...(label && { label }), methods };
|
|
119
|
+
}
|
|
120
|
+
cliOf(harness) {
|
|
121
|
+
const cli = this.agentFor(harness).login;
|
|
122
|
+
if (cli)
|
|
123
|
+
return cli;
|
|
124
|
+
throw new InvalidRequestError([{ path: "harness", message: "its agent signs in on its own" }]);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Start one of a harness's logins, answered once the CLI says where the
|
|
128
|
+
* user signs in. Once it has, a login under way to the same harness and
|
|
129
|
+
* store is cancelled: each CLI runs one at a time, and the latest start
|
|
130
|
+
* wins.
|
|
131
|
+
*/
|
|
132
|
+
async login({ harness, method, apiKey, env }) {
|
|
133
|
+
const cli = this.cliOf(harness);
|
|
134
|
+
const shuttingDown = () => new AgentServerError("The server is shutting down.", "shuttingDown");
|
|
135
|
+
if (this.stopped)
|
|
136
|
+
throw shuttingDown();
|
|
137
|
+
const stop = new AbortController();
|
|
138
|
+
const starting = cli.start(method, {
|
|
139
|
+
...(env && { env }),
|
|
140
|
+
...(apiKey && { apiKey }),
|
|
141
|
+
signal: stop.signal,
|
|
142
|
+
});
|
|
143
|
+
const started = await withTimeout(starting, this.options.loginStartTimeoutMs ?? LOGIN_START_TIMEOUT_MS);
|
|
144
|
+
if (!started) {
|
|
145
|
+
// Its CLI is stopped; one that starts all the same is cancelled, as no one holds its id.
|
|
146
|
+
stop.abort();
|
|
147
|
+
void starting.then((late) => late.cancel(), () => { });
|
|
148
|
+
throw new AgentServerError("The CLI did not start the login in time.", "loginTimeout");
|
|
149
|
+
}
|
|
150
|
+
const native = started.value;
|
|
151
|
+
if (this.stopped) {
|
|
152
|
+
native.cancel();
|
|
153
|
+
throw shuttingDown();
|
|
154
|
+
}
|
|
155
|
+
// Only once it started: a start the CLI refuses cancels nothing.
|
|
156
|
+
const store = storeKey(harness, env);
|
|
157
|
+
for (const login of this.underWay()) {
|
|
158
|
+
if (login.store === store)
|
|
159
|
+
this.end(login, "cancelled");
|
|
160
|
+
}
|
|
161
|
+
const login = this.track(harness, method, store, env, native);
|
|
162
|
+
void this.tellStarted(login);
|
|
163
|
+
return login.answer;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Tell every listener a login started, after `auth/login` is answered:
|
|
167
|
+
* the store's login (its recent reading, else a new one) with this one
|
|
168
|
+
* under way. Nothing when it ended meanwhile, as its end says it; a read
|
|
169
|
+
* of its own that noticed a change is told all the same. A recent
|
|
170
|
+
* reading's change is its reader's, who told it already.
|
|
171
|
+
*/
|
|
172
|
+
async tellStarted(login) {
|
|
173
|
+
const recent = this.reads.recent(login.store);
|
|
174
|
+
const reading = await (recent ?? this.reread(login.harness, login.env)).catch(() => undefined);
|
|
175
|
+
if (!reading || this.stopped)
|
|
176
|
+
return;
|
|
177
|
+
const noticed = !recent && reading.differs;
|
|
178
|
+
if (noticed)
|
|
179
|
+
this.changed(login.harness, login.env);
|
|
180
|
+
else if (login.end)
|
|
181
|
+
return;
|
|
182
|
+
this.emit({ auth: this.withLogin(reading.auth, login.store) });
|
|
183
|
+
}
|
|
184
|
+
/** Keep a started login until it ends, and expire it. */
|
|
185
|
+
track(harness, method, store, env, native) {
|
|
186
|
+
const loginId = this.options.newId?.() ?? generateUUIDv7();
|
|
187
|
+
const ttl = this.options.loginTtlMs ?? LOGIN_TTL_MS;
|
|
188
|
+
const answer = {
|
|
189
|
+
loginId,
|
|
190
|
+
harness,
|
|
191
|
+
method,
|
|
192
|
+
...(native.url && { url: native.url }),
|
|
193
|
+
...(native.codeUrl && { codeUrl: native.codeUrl }),
|
|
194
|
+
...(native.userCode && { userCode: native.userCode }),
|
|
195
|
+
expiresAt: this.now() + ttl,
|
|
196
|
+
};
|
|
197
|
+
let settle;
|
|
198
|
+
const ended = new Promise((resolve) => {
|
|
199
|
+
settle = resolve;
|
|
200
|
+
});
|
|
201
|
+
const login = {
|
|
202
|
+
harness,
|
|
203
|
+
answer,
|
|
204
|
+
store,
|
|
205
|
+
env,
|
|
206
|
+
native,
|
|
207
|
+
ended,
|
|
208
|
+
settle,
|
|
209
|
+
timer: setTimeout(() => this.end(login, "expired"), ttl),
|
|
210
|
+
};
|
|
211
|
+
login.timer.unref?.();
|
|
212
|
+
this.logins.set(loginId, login);
|
|
213
|
+
native.done.then(() => this.finish(login, { loginId, outcome: "succeeded" }), (error) => this.finish(login, { loginId, outcome: "failed", error: message(error) }));
|
|
214
|
+
return login;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Finish a login with the code its `codeUrl` page showed: how it ended. A
|
|
218
|
+
* code the CLI could not be sent is refused, and the login goes on.
|
|
219
|
+
*/
|
|
220
|
+
async complete(loginId, code) {
|
|
221
|
+
const login = this.logins.get(loginId);
|
|
222
|
+
if (!login)
|
|
223
|
+
throw new UnknownLoginError(loginId);
|
|
224
|
+
if (login.end)
|
|
225
|
+
return login.ended;
|
|
226
|
+
if (!login.native.complete) {
|
|
227
|
+
throw new InvalidRequestError([{ path: "loginId", message: "this login takes no code" }]);
|
|
228
|
+
}
|
|
229
|
+
try {
|
|
230
|
+
await login.native.complete(code);
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
if (error instanceof InvalidRequestError)
|
|
234
|
+
throw error;
|
|
235
|
+
this.end(login, "failed", message(error));
|
|
236
|
+
}
|
|
237
|
+
return login.ended;
|
|
238
|
+
}
|
|
239
|
+
/** Stop a login under way. */
|
|
240
|
+
cancel(loginId) {
|
|
241
|
+
const login = this.logins.get(loginId);
|
|
242
|
+
if (!login || login.end)
|
|
243
|
+
return "not_found";
|
|
244
|
+
this.end(login, "cancelled");
|
|
245
|
+
return "cancelled";
|
|
246
|
+
}
|
|
247
|
+
/** Remove the CLI's own login: the harness's login after it. */
|
|
248
|
+
async logout(harness, env) {
|
|
249
|
+
await this.cliOf(harness).logout({ ...(env && { env }) });
|
|
250
|
+
this.changed(harness, env);
|
|
251
|
+
const { auth: read } = await this.reread(harness, env);
|
|
252
|
+
const auth = this.withLogin(read, storeKey(harness, env));
|
|
253
|
+
this.emit({ auth });
|
|
254
|
+
return auth;
|
|
255
|
+
}
|
|
256
|
+
/** Every change of a harness's login, and every login's start and end. Returns the unsubscribe. */
|
|
257
|
+
onUpdated(listener) {
|
|
258
|
+
this.listeners.add(listener);
|
|
259
|
+
return () => this.listeners.delete(listener);
|
|
260
|
+
}
|
|
261
|
+
/** Stop every login under way, and start none: its CLI goes, and no other starts to say so. */
|
|
262
|
+
shutdown() {
|
|
263
|
+
this.stopped = true;
|
|
264
|
+
for (const login of this.underWay())
|
|
265
|
+
this.end(login, "cancelled");
|
|
266
|
+
}
|
|
267
|
+
underWay() {
|
|
268
|
+
return [...this.logins.values()].filter((login) => !login.end);
|
|
269
|
+
}
|
|
270
|
+
/** End it here (cancelled, expired, a refused code): its CLI stops. */
|
|
271
|
+
end(login, outcome, error) {
|
|
272
|
+
void this.finish(login, { loginId: login.answer.loginId, outcome, ...(error && { error }) });
|
|
273
|
+
login.native.cancel();
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* It ended: read the harness's login again and tell everyone how. It stays
|
|
277
|
+
* a login under way until then, so a code pasted just after a browser
|
|
278
|
+
* finished it is answered with its end, not as unknown.
|
|
279
|
+
*/
|
|
280
|
+
async finish(login, end) {
|
|
281
|
+
if (login.end)
|
|
282
|
+
return;
|
|
283
|
+
login.end = end;
|
|
284
|
+
clearTimeout(login.timer);
|
|
285
|
+
const { harness } = login;
|
|
286
|
+
const succeeded = end.outcome === "succeeded";
|
|
287
|
+
// Before the read: a turn that starts meanwhile already runs a new CLI.
|
|
288
|
+
if (succeeded)
|
|
289
|
+
this.changed(harness, login.env);
|
|
290
|
+
// Shutting down starts no CLI to read it.
|
|
291
|
+
const { auth, differs } = this.stopped
|
|
292
|
+
? { auth: { harness, state: "unknown", methods: [] }, differs: false }
|
|
293
|
+
: await this.reread(harness, login.env);
|
|
294
|
+
if (differs && !succeeded)
|
|
295
|
+
this.changed(harness, login.env);
|
|
296
|
+
// Ended already, so it is no longer under way: a newer login that replaced it is.
|
|
297
|
+
const update = { auth: this.withLogin(auth, login.store), login: end };
|
|
298
|
+
this.logins.delete(end.loginId);
|
|
299
|
+
if (!this.stopped)
|
|
300
|
+
this.emit(update);
|
|
301
|
+
login.settle(update);
|
|
302
|
+
}
|
|
303
|
+
emit(update) {
|
|
304
|
+
for (const listener of this.listeners) {
|
|
305
|
+
try {
|
|
306
|
+
listener(update);
|
|
307
|
+
}
|
|
308
|
+
catch {
|
|
309
|
+
// One listener's failure is not the login's.
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { canonicalJson } from "../../protocol/index.js";
|
|
2
|
+
import { setBounded } from "../utils/bounded.js";
|
|
3
|
+
/** Stores whose reads are remembered; the oldest go first. */
|
|
4
|
+
const MAX_ENTRIES = 64;
|
|
5
|
+
/**
|
|
6
|
+
* What makes two reads of a login the same login. A key replaced by another
|
|
7
|
+
* outside the engine (in a terminal) reads the same, as no CLI says which key
|
|
8
|
+
* it holds: sessions notice a login the engine runs, or a change of kind or
|
|
9
|
+
* account.
|
|
10
|
+
*/
|
|
11
|
+
const loginPrint = (auth) => canonicalJson([
|
|
12
|
+
auth.state,
|
|
13
|
+
auth.credential,
|
|
14
|
+
auth.source,
|
|
15
|
+
auth.provider,
|
|
16
|
+
auth.email,
|
|
17
|
+
auth.organization,
|
|
18
|
+
auth.plan,
|
|
19
|
+
]);
|
|
20
|
+
/**
|
|
21
|
+
* Each store's login as last read. A read under way, or one a moment old, is
|
|
22
|
+
* shared, and only the latest read of a store counts: one that answers after
|
|
23
|
+
* a newer one began says nothing of a change, so a slow read from before a
|
|
24
|
+
* logout cannot announce the login back.
|
|
25
|
+
*/
|
|
26
|
+
export class LoginReads {
|
|
27
|
+
now;
|
|
28
|
+
ttlMs;
|
|
29
|
+
reads = new Map();
|
|
30
|
+
seen = new Map();
|
|
31
|
+
count = 0;
|
|
32
|
+
constructor(now, ttlMs) {
|
|
33
|
+
this.now = now;
|
|
34
|
+
this.ttlMs = ttlMs;
|
|
35
|
+
}
|
|
36
|
+
/** The store's read under way, or one a moment old. */
|
|
37
|
+
recent(key) {
|
|
38
|
+
const recent = this.reads.get(key);
|
|
39
|
+
if (recent && (recent.at === undefined || this.now() - recent.at < this.ttlMs)) {
|
|
40
|
+
return recent.reading;
|
|
41
|
+
}
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Read it now: it differs when it is the latest read and not the login last
|
|
46
|
+
* seen. One that a newer read overtook answers with the newer one's login.
|
|
47
|
+
*/
|
|
48
|
+
read(key, read) {
|
|
49
|
+
const n = ++this.count;
|
|
50
|
+
const reading = read().then(async (auth) => {
|
|
51
|
+
const latest = this.reads.get(key);
|
|
52
|
+
if (latest && latest.n !== n)
|
|
53
|
+
return { auth: (await latest.reading).auth, differs: false };
|
|
54
|
+
if (!latest)
|
|
55
|
+
return { auth, differs: false };
|
|
56
|
+
latest.at = this.now();
|
|
57
|
+
if (auth.state === "unknown")
|
|
58
|
+
return { auth, differs: false };
|
|
59
|
+
const before = this.seen.get(key);
|
|
60
|
+
setBounded(this.seen, key, loginPrint(auth), MAX_ENTRIES);
|
|
61
|
+
return { auth, differs: before !== undefined && before !== loginPrint(auth) };
|
|
62
|
+
});
|
|
63
|
+
setBounded(this.reads, key, { n, reading }, MAX_ENTRIES);
|
|
64
|
+
// A read that failed is not shared: the next one tries again.
|
|
65
|
+
reading.catch(() => {
|
|
66
|
+
if (this.reads.get(key)?.n === n)
|
|
67
|
+
this.reads.delete(key);
|
|
68
|
+
});
|
|
69
|
+
return reading;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { CATALOG_MODELS, canonicalJson, } from "../../protocol/index.js";
|
|
2
|
+
import { setBounded } from "../utils/bounded.js";
|
|
3
|
+
import { withTimeout } from "../utils/deadline.js";
|
|
4
|
+
/** A live list is reused this long. A login can change outside agent-server; `refresh` reads sooner. */
|
|
5
|
+
const LIVE_TTL_MS = 10 * 60_000;
|
|
6
|
+
/** A failed read is reused this long, so a logged-out CLI is not started on every call. */
|
|
7
|
+
const FAILURE_TTL_MS = 30_000;
|
|
8
|
+
/** A read that takes longer falls back to the catalog, inside a client's 30 s request deadline. */
|
|
9
|
+
const LIST_TIMEOUT_MS = 20_000;
|
|
10
|
+
/** Distinct folders/credentials remembered; the oldest is dropped first. */
|
|
11
|
+
const MAX_ENTRIES = 64;
|
|
12
|
+
/** The catalog's models for a harness, as a list (`source: "catalog"`). */
|
|
13
|
+
export function catalogModelList(harness, fetchedAt) {
|
|
14
|
+
return {
|
|
15
|
+
harness,
|
|
16
|
+
models: CATALOG_MODELS.filter((model) => model.harness === harness),
|
|
17
|
+
source: "catalog",
|
|
18
|
+
fetchedAt,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
/** An alias (Claude `opus`, `default`) runs a model that depends on the account. */
|
|
22
|
+
const isAlias = (model) => model.resolvedId !== undefined;
|
|
23
|
+
/**
|
|
24
|
+
* The row of `models` for `model`: the row with that id, else a row that runs
|
|
25
|
+
* it as an alias. Without `aliases`, a row whose own id is an alias does not
|
|
26
|
+
* match by id, since another login's alias may run another model.
|
|
27
|
+
*/
|
|
28
|
+
function rowFor(models, model, { aliases }) {
|
|
29
|
+
return (models.find((row) => row.id === model && (aliases || !isAlias(row))) ??
|
|
30
|
+
models.find((row) => row.resolvedId === model));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Model lists per harness, login and folder. Concurrent reads of one key share
|
|
34
|
+
* a single CLI read, and a result is stored only if no newer read started
|
|
35
|
+
* meanwhile, so a slow stale read cannot replace a refresh. A failed read keeps
|
|
36
|
+
* the last live list (with the error) rather than falling back to the catalog.
|
|
37
|
+
*/
|
|
38
|
+
export class ModelLists {
|
|
39
|
+
options;
|
|
40
|
+
entries = new Map();
|
|
41
|
+
reads = new Map();
|
|
42
|
+
generation = 0;
|
|
43
|
+
constructor(options = {}) {
|
|
44
|
+
this.options = options;
|
|
45
|
+
}
|
|
46
|
+
now() {
|
|
47
|
+
return this.options.now?.() ?? Date.now();
|
|
48
|
+
}
|
|
49
|
+
key(harness, cwd, sessionId, query) {
|
|
50
|
+
return canonicalJson([harness, cwd, sessionId, query.apiKey, query.env]);
|
|
51
|
+
}
|
|
52
|
+
get(agent, query) {
|
|
53
|
+
const listModels = agent.listModels?.bind(agent);
|
|
54
|
+
if (!listModels)
|
|
55
|
+
return Promise.resolve(catalogModelList(query.harness, this.now()));
|
|
56
|
+
const cwd = query.cwd ?? globalThis.process?.cwd?.() ?? "/";
|
|
57
|
+
// Only a harness whose credentials follow the session lists per session.
|
|
58
|
+
const key = this.key(query.harness, cwd, agent.modelsPerSession ? query.sessionId : undefined, query);
|
|
59
|
+
const pending = this.reads.get(key);
|
|
60
|
+
if (query.refresh) {
|
|
61
|
+
if (pending?.refresh)
|
|
62
|
+
return pending.list;
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
const entry = this.entries.get(key);
|
|
66
|
+
if (entry && entry.expiresAt > this.now())
|
|
67
|
+
return Promise.resolve(entry.list);
|
|
68
|
+
if (pending)
|
|
69
|
+
return pending.list;
|
|
70
|
+
}
|
|
71
|
+
const generation = ++this.generation;
|
|
72
|
+
const list = this.read(query.harness, (signal) => listModels({
|
|
73
|
+
cwd,
|
|
74
|
+
...(query.sessionId !== undefined && { sessionId: query.sessionId }),
|
|
75
|
+
...(query.env !== undefined && { env: query.env }),
|
|
76
|
+
...(query.apiKey !== undefined && { apiKey: query.apiKey }),
|
|
77
|
+
signal,
|
|
78
|
+
})).then((read) => {
|
|
79
|
+
const previous = this.entries.get(key)?.list;
|
|
80
|
+
const result = read.source === "catalog" && previous?.source === "live"
|
|
81
|
+
? { ...previous, ...(read.error !== undefined && { error: read.error }) }
|
|
82
|
+
: read;
|
|
83
|
+
if (this.reads.get(key)?.generation === generation) {
|
|
84
|
+
this.reads.delete(key);
|
|
85
|
+
const expiresAt = this.now() + (read.source === "live" ? LIVE_TTL_MS : FAILURE_TTL_MS);
|
|
86
|
+
setBounded(this.entries, key, { list: result, expiresAt }, MAX_ENTRIES);
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
});
|
|
90
|
+
this.reads.set(key, {
|
|
91
|
+
harness: query.harness,
|
|
92
|
+
generation,
|
|
93
|
+
refresh: Boolean(query.refresh),
|
|
94
|
+
list,
|
|
95
|
+
});
|
|
96
|
+
return list;
|
|
97
|
+
}
|
|
98
|
+
/** The harness's login changed: its lists are read again, and no read under way is kept. */
|
|
99
|
+
forget(harness) {
|
|
100
|
+
for (const [key, entry] of this.entries)
|
|
101
|
+
if (entry.list.harness === harness)
|
|
102
|
+
this.entries.delete(key);
|
|
103
|
+
for (const [key, read] of this.reads)
|
|
104
|
+
if (read.harness === harness)
|
|
105
|
+
this.reads.delete(key);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* The levels `model` runs on a turn with this login and folder. First the
|
|
109
|
+
* list read for that login; failing that, any login's list or the catalog,
|
|
110
|
+
* but only for a model id that is not an alias: an alias such as Claude's
|
|
111
|
+
* `opus` runs a model that depends on the account and provider. A full id
|
|
112
|
+
* that a list names only through an alias (Claude lists Opus 5.5 as `opus`)
|
|
113
|
+
* takes that row's levels, which are the levels of the model it runs.
|
|
114
|
+
* `undefined` when nothing known applies.
|
|
115
|
+
*/
|
|
116
|
+
thinkingFor(query, model) {
|
|
117
|
+
const own = [query.sessionId, undefined].map((sessionId) => this.key(query.harness, query.cwd, sessionId, query));
|
|
118
|
+
for (const key of own) {
|
|
119
|
+
const list = this.entries.get(key)?.list;
|
|
120
|
+
const found = list?.source === "live" && rowFor(list.models, model, { aliases: true });
|
|
121
|
+
if (found)
|
|
122
|
+
return found.thinking.levels;
|
|
123
|
+
}
|
|
124
|
+
let newest;
|
|
125
|
+
for (const { list } of this.entries.values()) {
|
|
126
|
+
if (list.harness !== query.harness || list.source !== "live")
|
|
127
|
+
continue;
|
|
128
|
+
if (newest && newest.fetchedAt >= list.fetchedAt)
|
|
129
|
+
continue;
|
|
130
|
+
const found = rowFor(list.models, model, { aliases: false });
|
|
131
|
+
if (found)
|
|
132
|
+
newest = { levels: found.thinking.levels, fetchedAt: list.fetchedAt };
|
|
133
|
+
}
|
|
134
|
+
if (newest)
|
|
135
|
+
return newest.levels;
|
|
136
|
+
const catalog = CATALOG_MODELS.filter((candidate) => candidate.harness === query.harness);
|
|
137
|
+
return rowFor(catalog, model, { aliases: false })?.thinking.levels;
|
|
138
|
+
}
|
|
139
|
+
async read(harness, list) {
|
|
140
|
+
const controller = new AbortController();
|
|
141
|
+
const timeoutMs = this.options.timeoutMs ?? LIST_TIMEOUT_MS;
|
|
142
|
+
try {
|
|
143
|
+
const listed = await withTimeout(list(controller.signal), timeoutMs);
|
|
144
|
+
if (!listed) {
|
|
145
|
+
const error = new Error(`the ${harness} CLI did not list its models within ${timeoutMs} ms`);
|
|
146
|
+
controller.abort(error);
|
|
147
|
+
throw error;
|
|
148
|
+
}
|
|
149
|
+
if (!listed.value.length)
|
|
150
|
+
throw new Error(`the ${harness} CLI listed no models`);
|
|
151
|
+
return { harness, models: listed.value, source: "live", fetchedAt: this.now() };
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
return {
|
|
155
|
+
...catalogModelList(harness, this.now()),
|
|
156
|
+
error: error instanceof Error ? error.message : String(error),
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { STANDARD_PERMISSION_OPTIONS, isAllowKind } from "../../protocol/index.js";
|
|
2
|
+
// @ref LLP 0001#d4--permission-requests-are-first-class-protocol — a harness's own choices
|
|
3
|
+
/**
|
|
4
|
+
* Read what a request offers. A harness's own choices ask something no
|
|
5
|
+
* standing answer covers. "Always" covers what the harness says it does,
|
|
6
|
+
* never every call to the tool: one `npm test` must not approve every later
|
|
7
|
+
* command, so a harness that keeps it itself, or cannot scope one, says so,
|
|
8
|
+
* and the engine keeps no standing rule for its requests. A request no
|
|
9
|
+
* "always" is kept for at all (an ACP agent's, Codex's consent for an MCP
|
|
10
|
+
* tool call) leaves the engine one per tool.
|
|
11
|
+
*/
|
|
12
|
+
export function permissionAsk(opts) {
|
|
13
|
+
if (opts?.options)
|
|
14
|
+
return { kind: "own_options", options: opts.options };
|
|
15
|
+
if (opts?.always === undefined)
|
|
16
|
+
return { kind: "engine_always" };
|
|
17
|
+
if (opts.always === null)
|
|
18
|
+
return { kind: "once" };
|
|
19
|
+
return { kind: "harness_always", rule: opts.always.rule };
|
|
20
|
+
}
|
|
21
|
+
/** The standard option of this kind. */
|
|
22
|
+
const standard = (kind) => STANDARD_PERMISSION_OPTIONS.find((option) => option.kind === kind);
|
|
23
|
+
/** The options a request offers. A harness that keeps "always" keeps no refusal: there is no "always reject" for it. */
|
|
24
|
+
export function optionsFor(ask) {
|
|
25
|
+
switch (ask.kind) {
|
|
26
|
+
case "own_options":
|
|
27
|
+
return ask.options;
|
|
28
|
+
case "engine_always":
|
|
29
|
+
return [...STANDARD_PERMISSION_OPTIONS];
|
|
30
|
+
case "harness_always":
|
|
31
|
+
return STANDARD_PERMISSION_OPTIONS.filter((o) => o.kind !== "reject_always").map((o) => o.kind === "allow_always" ? { ...o, name: `Always allow ${ask.rule}` } : o);
|
|
32
|
+
case "once":
|
|
33
|
+
return STANDARD_PERMISSION_OPTIONS.filter((o) => o.kind === "allow_once" || o.kind === "reject_once");
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* What the harness is told for a selected answer, and the option it counts
|
|
38
|
+
* as. A client that renders no options answers with the standard ids: one
|
|
39
|
+
* that was not offered means a plain allow or reject, never "always".
|
|
40
|
+
*/
|
|
41
|
+
export function decisionFor(outcome, offered) {
|
|
42
|
+
const plain = STANDARD_PERMISSION_OPTIONS.find((o) => o.optionId === outcome.optionId);
|
|
43
|
+
const selected = offered.find((o) => o.optionId === outcome.optionId) ??
|
|
44
|
+
(plain && standard(isAllowKind(plain.kind) ? "allow_once" : "reject_once"));
|
|
45
|
+
const chosen = {
|
|
46
|
+
...(selected && { optionId: selected.optionId }),
|
|
47
|
+
...(outcome.message && { message: outcome.message }),
|
|
48
|
+
};
|
|
49
|
+
const decision = isAllowKind(selected?.kind)
|
|
50
|
+
? {
|
|
51
|
+
decision: "allow",
|
|
52
|
+
// What was approved is what runs: an edited input replaces the
|
|
53
|
+
// original all the way into the harness.
|
|
54
|
+
...(outcome.updatedInput !== undefined && { updatedInput: outcome.updatedInput }),
|
|
55
|
+
...chosen,
|
|
56
|
+
}
|
|
57
|
+
: { decision: "deny", reason: "Denied by user", ...chosen };
|
|
58
|
+
return { decision, selected };
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A session's standing answers: an "always" the user gave for a tool answers
|
|
62
|
+
* its later calls without a round trip (and without a prompt event that never
|
|
63
|
+
* happened). A rule carries the verdict only: an edited input belongs to the
|
|
64
|
+
* call it was made for. Kept until the session closes.
|
|
65
|
+
*/
|
|
66
|
+
export class StandingRules {
|
|
67
|
+
verdicts = new Map();
|
|
68
|
+
/** The decision a standing rule gives this tool, if there is one. */
|
|
69
|
+
answer(toolName) {
|
|
70
|
+
const verdict = this.verdicts.get(toolName);
|
|
71
|
+
if (!verdict)
|
|
72
|
+
return undefined;
|
|
73
|
+
return verdict === "allow"
|
|
74
|
+
? { decision: "allow" }
|
|
75
|
+
: { decision: "deny", reason: "Denied by a standing rule" };
|
|
76
|
+
}
|
|
77
|
+
/** An `*_always` answer becomes the tool's standing rule. */
|
|
78
|
+
learn(toolName, selected) {
|
|
79
|
+
if (selected?.kind === "allow_always")
|
|
80
|
+
this.verdicts.set(toolName, "allow");
|
|
81
|
+
else if (selected?.kind === "reject_always")
|
|
82
|
+
this.verdicts.set(toolName, "deny");
|
|
83
|
+
}
|
|
84
|
+
}
|