@1agh/maude 2.2.1 → 2.3.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.
Files changed (74) hide show
  1. package/apps/studio/acp/agents/auth.ts +241 -0
  2. package/apps/studio/acp/agents/catalog.ts +308 -0
  3. package/apps/studio/acp/agents/chat-key.ts +64 -0
  4. package/apps/studio/acp/agents/conformance.ts +1853 -0
  5. package/apps/studio/acp/agents/consent.ts +92 -0
  6. package/apps/studio/acp/agents/descriptor.ts +188 -0
  7. package/apps/studio/acp/agents/discover.ts +491 -0
  8. package/apps/studio/acp/agents/e2e-model.ts +58 -0
  9. package/apps/studio/acp/agents/health.ts +46 -0
  10. package/apps/studio/acp/agents/link-scan.ts +230 -0
  11. package/apps/studio/acp/agents/spawn.ts +164 -0
  12. package/apps/studio/acp/agents/state.ts +132 -0
  13. package/apps/studio/acp/ask-queue.ts +13 -4
  14. package/apps/studio/acp/bootstrap-brief.ts +16 -3
  15. package/apps/studio/acp/bridge.ts +709 -99
  16. package/apps/studio/acp/env.ts +43 -1
  17. package/apps/studio/acp/index.ts +428 -43
  18. package/apps/studio/acp/may-scope.ts +244 -21
  19. package/apps/studio/acp/policy/agent-home.ts +202 -0
  20. package/apps/studio/acp/policy/bridge-gate.ts +68 -0
  21. package/apps/studio/acp/policy/codex-home.ts +228 -0
  22. package/apps/studio/acp/policy/enforce.ts +620 -0
  23. package/apps/studio/acp/policy/gate-types.ts +41 -0
  24. package/apps/studio/acp/policy/launch-codex.ts +351 -0
  25. package/apps/studio/acp/policy/launch-copilot.ts +119 -0
  26. package/apps/studio/acp/policy/launch-gemini.ts +120 -0
  27. package/apps/studio/acp/policy/launch-opencode.ts +131 -0
  28. package/apps/studio/acp/policy/opencode-user-config.ts +240 -0
  29. package/apps/studio/acp/probe.ts +78 -14
  30. package/apps/studio/acp/transcript.ts +14 -0
  31. package/apps/studio/acp/write-scope.ts +62 -2
  32. package/apps/studio/client/panels/ChatPanel.jsx +19 -4
  33. package/apps/studio/client/panels/PermissionPrompt.jsx +11 -6
  34. package/apps/studio/client/panels/acp-runtime.js +95 -7
  35. package/apps/studio/client/v2/ai/Advanced.jsx +4 -3
  36. package/apps/studio/client/v2/ai/AgentNotice.jsx +138 -0
  37. package/apps/studio/client/v2/ai/AgentOffer.jsx +110 -0
  38. package/apps/studio/client/v2/ai/AgentPicker.jsx +121 -0
  39. package/apps/studio/client/v2/ai/Composer.jsx +4 -0
  40. package/apps/studio/client/v2/ai/ConnectSheet.jsx +13 -0
  41. package/apps/studio/client/v2/ai/Feed.jsx +8 -0
  42. package/apps/studio/client/v2/ai/NeedsYouSlot.jsx +24 -1
  43. package/apps/studio/client/v2/ai/Suggest.jsx +5 -1
  44. package/apps/studio/client/v2/ai/advanced-model.js +17 -6
  45. package/apps/studio/client/v2/ai/agents-model.js +333 -0
  46. package/apps/studio/client/v2/ai/agents-store.js +223 -0
  47. package/apps/studio/client/v2/ai/ai.css +63 -0
  48. package/apps/studio/client/v2/ai/needs-you.js +1 -1
  49. package/apps/studio/client/v2/fold/fold-store.js +1 -0
  50. package/apps/studio/client/v2/settings/agent-keys.js +88 -0
  51. package/apps/studio/client/v2/settings/agents-card.jsx +408 -0
  52. package/apps/studio/client/v2/settings/connections-model.js +1 -1
  53. package/apps/studio/client/v2/settings/connections-section.jsx +4 -0
  54. package/apps/studio/client/v2/settings/settings-cards.js +2 -1
  55. package/apps/studio/client/v2/settings/settings-model.js +1 -0
  56. package/apps/studio/client/v2/settings/settings.css +7 -0
  57. package/apps/studio/client/v2/shell/served-routes.js +1 -0
  58. package/apps/studio/cloud-build.ts +5 -0
  59. package/apps/studio/dist/client.bundle.js +604 -604
  60. package/apps/studio/dist/styles.css +1 -1
  61. package/apps/studio/generation/keys.ts +197 -11
  62. package/apps/studio/outbox/policy.ts +8 -2
  63. package/apps/studio/outbox/types.ts +5 -0
  64. package/apps/studio/routes/agents.ts +479 -0
  65. package/apps/studio/routes/index.ts +3 -0
  66. package/apps/studio/scripts/acp-conformance-local.ts +95 -0
  67. package/apps/studio/scripts/acp-e2e-local.ts +1026 -0
  68. package/apps/studio/server.ts +9 -0
  69. package/apps/studio/sync/writer-registry.ts +12 -0
  70. package/apps/studio/whats-new.json +10 -0
  71. package/apps/studio/ws.ts +3 -0
  72. package/cli/commands/codex.mjs +36 -2
  73. package/cli/lib/harness/codex-runtime.mjs +98 -11
  74. package/package.json +9 -8
@@ -0,0 +1,223 @@
1
+ // client/v2/ai/agents-store.js — universal ACP B6: the one `/_api/agents` list every surface reads (the offer
2
+ // line, the composer's agent chip, the Connect sheet's link, Settings › Connections › AI agents), and the
3
+ // verbs that change it (connect · sign in · disconnect · Not now).
4
+ //
5
+ // One module-level snapshot, so the panel and Settings never disagree; each write reads the list again.
6
+ // `servedFetch` (#204): a cell, or an older server without the route (404), reads as "no agents", which is
7
+ // exactly the Claude-only picture: nothing new is drawn.
8
+ //
9
+ // connectAgentFlow({ id, consent, fetchImpl, sleep, now, signal, onPhase }) → { ok } | { ok:false, reason, detail? }
10
+ //
11
+ // Phases: 'connecting' · 'waiting' (for the vendor's own sign-in in the browser). Cancel = abort the signal
12
+ // (the sign-in is withdrawn). Pure of React so the tests drive it with a scripted fetch.
13
+
14
+ import { useCallback, useEffect, useRef, useState, useSyncExternalStore } from 'react';
15
+ import { servedFetch } from '../shell/served-routes.js';
16
+ import { connectFailWords, normalizeAgents } from './agents-model.js';
17
+
18
+ const SIGNIN_POLL_MS = 2000;
19
+ const SIGNIN_MAX_MS = 180_000;
20
+
21
+ const EMPTY = Object.freeze({ agents: [], offerAgents: [], loaded: false });
22
+ let snap = EMPTY;
23
+ const subs = new Set();
24
+ let inflight = null;
25
+
26
+ function emit() {
27
+ for (const fn of subs) fn();
28
+ }
29
+
30
+ export function subscribeAgents(fn) {
31
+ subs.add(fn);
32
+ return () => subs.delete(fn);
33
+ }
34
+ export const agentsSnapshot = () => snap;
35
+
36
+ /** Tests only: put a list in place (or reset with no argument). */
37
+ export function setAgentsForTest(body) {
38
+ snap = body ? { ...normalizeAgents(body), loaded: true } : EMPTY;
39
+ inflight = null;
40
+ emit();
41
+ }
42
+
43
+ /** Read the list (once at a time); `force` asks the server to look on disk again. */
44
+ export function loadAgents({ force = false, fetchImpl } = {}) {
45
+ if (inflight && !force) return inflight;
46
+ const url = force ? '/_api/agents?refresh=1' : '/_api/agents';
47
+ const run = servedFetch(url, { cache: 'no-store' }, fetchImpl ?? ((...a) => fetch(...a)))
48
+ .then(async (res) => (res?.ok ? res.json() : null))
49
+ .catch(() => null)
50
+ .then((body) => {
51
+ snap = { ...normalizeAgents(body), loaded: true };
52
+ emit();
53
+ return snap;
54
+ })
55
+ .finally(() => {
56
+ if (inflight === run) inflight = null;
57
+ });
58
+ inflight = run;
59
+ return run;
60
+ }
61
+
62
+ // The profile this project talks as (`/_config.profile.id`): the last-used agent is remembered per profile.
63
+ let profileId = null;
64
+ let profileAsk = null;
65
+ export function loadProfileId(fetchImpl = (...a) => fetch(...a)) {
66
+ if (profileId) return Promise.resolve(profileId);
67
+ profileAsk ??= fetchImpl('/_config', { cache: 'no-store', credentials: 'same-origin' })
68
+ .then((r) => (r?.ok ? r.json() : null))
69
+ .catch(() => null)
70
+ .then((cfg) => {
71
+ const id = cfg?.profile?.id;
72
+ profileId = typeof id === 'string' && /^[a-z0-9-]{1,32}$/.test(id) ? id : 'default';
73
+ return profileId;
74
+ });
75
+ return profileAsk;
76
+ }
77
+ export const currentProfileId = () => profileId ?? 'default';
78
+
79
+ /** The list, read once on first use. */
80
+ export function useAgents() {
81
+ const s = useSyncExternalStore(subscribeAgents, agentsSnapshot, agentsSnapshot);
82
+ useEffect(() => {
83
+ if (!snap.loaded) void loadAgents();
84
+ }, []);
85
+ return s;
86
+ }
87
+
88
+ const enc = (id) => encodeURIComponent(id);
89
+ const json = (res) => (res?.json ? res.json().catch(() => null) : null);
90
+
91
+ function post(fetchImpl, url, body) {
92
+ return fetchImpl(url, {
93
+ method: 'POST',
94
+ ...(body
95
+ ? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }
96
+ : {}),
97
+ })
98
+ .then(json)
99
+ .catch(() => null);
100
+ }
101
+
102
+ /** "Not now" on the offer: permanent, for every agent the line named. */
103
+ export async function dismissAgentOffers(ids, { fetchImpl = (...a) => fetch(...a) } = {}) {
104
+ await Promise.all(
105
+ (ids ?? []).map((id) => post(fetchImpl, `/_api/agents/${enc(id)}/dismiss-offer`))
106
+ );
107
+ await loadAgents({ fetchImpl });
108
+ }
109
+
110
+ export async function disconnectAgent(id, { fetchImpl = (...a) => fetch(...a) } = {}) {
111
+ const r = await post(fetchImpl, `/_api/agents/${enc(id)}/disconnect`);
112
+ await loadAgents({ fetchImpl });
113
+ return r ?? { ok: false };
114
+ }
115
+
116
+ /** Start the vendor's own sign-in, then wait for it (or for Cancel). */
117
+ async function signIn({ id, fetchImpl, sleep, now, signal, onPhase }) {
118
+ const started = await post(fetchImpl, `/_api/agents/${enc(id)}/signin`);
119
+ if (!started?.ok) return { ok: false, reason: 'signin-failed' };
120
+ onPhase('waiting');
121
+ const deadline = now() + SIGNIN_MAX_MS;
122
+ for (;;) {
123
+ if (signal?.aborted) {
124
+ await post(fetchImpl, `/_api/agents/${enc(id)}/signin-cancel`);
125
+ return { ok: false, reason: 'cancelled' };
126
+ }
127
+ if (now() > deadline) {
128
+ await post(fetchImpl, `/_api/agents/${enc(id)}/signin-cancel`);
129
+ return { ok: false, reason: 'timeout' };
130
+ }
131
+ const st = await fetchImpl(`/_api/agents/${enc(id)}/signin-status`)
132
+ .then(json)
133
+ .catch(() => null);
134
+ if (st?.auth === 'signed-in' || st?.auth === 'not-needed') return { ok: true };
135
+ // the vendor's login exited without signing in (a closed browser tab, a refused login)
136
+ if (st && st.inFlight === false && typeof st.lastExit === 'number' && st.lastExit !== 0)
137
+ return { ok: false, reason: 'signin-failed' };
138
+ await sleep(SIGNIN_POLL_MS);
139
+ }
140
+ }
141
+
142
+ /**
143
+ * Connect one agent: register it, and when its vendor needs a sign-in first, run that sign-in and connect
144
+ * again. It does not change the default agent, open a session or widen any permission (Design Decision 1).
145
+ */
146
+ export async function connectAgentFlow({
147
+ id,
148
+ consent = false,
149
+ signinFirst = false,
150
+ fetchImpl = (...a) => fetch(...a),
151
+ sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
152
+ now = () => Date.now(),
153
+ signal,
154
+ onPhase = () => {},
155
+ } = {}) {
156
+ const deps = { id, fetchImpl, sleep, now, signal, onPhase };
157
+ const connect = () =>
158
+ post(fetchImpl, `/_api/agents/${enc(id)}/connect`, consent ? { consent: true } : undefined);
159
+ let r;
160
+ if (signinFirst) {
161
+ const s = await signIn(deps);
162
+ if (!s.ok) return s;
163
+ }
164
+ onPhase('connecting');
165
+ r = await connect();
166
+ if (r?.needsSignin) {
167
+ const s = await signIn(deps);
168
+ if (!s.ok) {
169
+ await loadAgents({ fetchImpl });
170
+ return s;
171
+ }
172
+ onPhase('connecting');
173
+ r = await connect();
174
+ }
175
+ await loadAgents({ fetchImpl });
176
+ if (r?.ok) return { ok: true };
177
+ return { ok: false, reason: r?.reason ?? 'unknown', ...(r?.detail ? { detail: r.detail } : {}) };
178
+ }
179
+
180
+ /**
181
+ * One connect at a time, with its progress and Cancel: what the offer line and the Settings card share.
182
+ * `phase` = 'idle' · 'connecting' · 'waiting'; `busyId` = the agent being connected; `error` = words.
183
+ * @param deps test seam: `fetchImpl`, `sleep`, `now`
184
+ */
185
+ export function useAgentConnect(deps = {}) {
186
+ const [state, setState] = useState({ phase: 'idle', busyId: null, error: null, errorId: null });
187
+ const ctl = useRef(null);
188
+ const depsRef = useRef(deps);
189
+ depsRef.current = deps;
190
+ useEffect(() => () => ctl.current?.abort(), []);
191
+ const connect = useCallback(async (row, opts = {}) => {
192
+ if (!row?.id || ctl.current) return false;
193
+ const abort = new AbortController();
194
+ ctl.current = abort;
195
+ setState({ phase: 'connecting', busyId: row.id, error: null, errorId: null });
196
+ const r = await connectAgentFlow({
197
+ ...depsRef.current,
198
+ id: row.id,
199
+ consent: opts.consent === true,
200
+ signinFirst: opts.signinFirst === true,
201
+ signal: abort.signal,
202
+ onPhase: (phase) => setState((s) => ({ ...s, phase })),
203
+ });
204
+ if (ctl.current === abort) ctl.current = null;
205
+ const failed = !r.ok && r.reason !== 'cancelled';
206
+ setState({
207
+ phase: 'idle',
208
+ busyId: null,
209
+ error: failed ? connectFailWords(r, row.label) : null,
210
+ errorId: failed ? row.id : null,
211
+ });
212
+ return r.ok;
213
+ }, []);
214
+ const cancel = useCallback(() => ctl.current?.abort(), []);
215
+ return { ...state, connect, cancel };
216
+ }
217
+
218
+ /** The progress words of a connect: "Connecting Codex…" · "Sign in to Codex in your browser…". */
219
+ export function connectProgressWords(phase, label) {
220
+ if (phase === 'waiting') return `Sign in to ${label} in your browser…`;
221
+ if (phase === 'connecting') return `Connecting ${label}…`;
222
+ return null;
223
+ }
@@ -246,6 +246,69 @@
246
246
  line-height: var(--lh-xs);
247
247
  }
248
248
 
249
+ /* ── Universal ACP B6: another agent on this Mac (agents-model.js). Quiet: the hint's ink, a text link. ── */
250
+ .ai-agent-offer {
251
+ padding: var(--space-1) var(--space-1) 0;
252
+ }
253
+ .ai-agent-offer-t {
254
+ margin: 0;
255
+ color: var(--fg-2);
256
+ font-size: var(--type-sm);
257
+ line-height: var(--lh-sm);
258
+ }
259
+ .maude .ai-agent-link {
260
+ display: inline;
261
+ padding: 0;
262
+ border: 0;
263
+ background: none;
264
+ color: var(--accent);
265
+ font: inherit;
266
+ cursor: pointer;
267
+ }
268
+ .maude .ai-agent-link:hover {
269
+ text-decoration: underline;
270
+ }
271
+ .maude .ai-agent-link:focus-visible {
272
+ outline: 2px solid var(--focus-ring);
273
+ outline-offset: 2px;
274
+ border-radius: var(--radius-sm);
275
+ }
276
+ .maude .ai-agent-link--quiet {
277
+ color: var(--fg-2);
278
+ }
279
+ .ai-agent-sep {
280
+ margin: 0 var(--space-1);
281
+ color: var(--fg-2);
282
+ }
283
+ .ai-agent-note {
284
+ margin: var(--space-1) 0 0;
285
+ color: var(--fg-2);
286
+ font-size: var(--type-xs);
287
+ }
288
+ .ai-agent-note--err {
289
+ color: var(--status-error);
290
+ }
291
+ .ai-agent-badge {
292
+ display: inline-block;
293
+ margin-bottom: var(--space-1);
294
+ color: var(--fg-2);
295
+ font-size: var(--type-xs);
296
+ line-height: var(--lh-xs);
297
+ }
298
+ .maude .ai-agent-chip {
299
+ flex: none;
300
+ max-width: 120px;
301
+ font-size: var(--type-xs);
302
+ line-height: var(--lh-xs);
303
+ }
304
+ .maude .ai-agent-chip--locked {
305
+ color: var(--fg-2);
306
+ }
307
+ .maude .ai-conn-other {
308
+ align-self: flex-start;
309
+ font-size: var(--type-xs);
310
+ }
311
+
249
312
  /* ── An answer that touched nothing ── */
250
313
  .ai-unchanged {
251
314
  margin: var(--space-2) 0 0;
@@ -105,7 +105,7 @@ function canvasName(path) {
105
105
  */
106
106
  export function permissionModel(request, work = null) {
107
107
  const toolCall = request?.toolCall ?? {};
108
- const outside = describeOutOfProject(request?.scope);
108
+ const outside = describeOutOfProject(request?.scope, request?.agent);
109
109
  const all = Array.isArray(request?.options) ? request.options.filter(Boolean) : [];
110
110
  // Decision D is enforced server-side; this second pass only keeps a stale frame from painting a
111
111
  // button the server would refuse.
@@ -31,6 +31,7 @@ export const FOLD_PANELS = Object.freeze([
31
31
  'connections.github',
32
32
  'connections.ai',
33
33
  'connections.figma',
34
+ 'connections.agents', // universal ACP B6 — Settings › AI agents › Advanced (the unrestricted agents)
34
35
  'ai',
35
36
  'share',
36
37
  'timeline',
@@ -0,0 +1,88 @@
1
+ // client/v2/settings/agent-keys.js — B7 (DDR-252 § BYOK): own API keys for the chat agents, the model and
2
+ // the two writes behind Settings › AI agents › Advanced. The server never sends a key back: a row carries
3
+ // `acceptsKey`, `keySet` and `keyLast4` only (routes/agents.ts), and this file never keeps one past the
4
+ // save call. Pure of React so the tests drive it with a scripted fetch.
5
+
6
+ import { loadAgents } from '../ai/agents-store.js';
7
+
8
+ const CLAUDE_ID = 'claude';
9
+ const enc = (id) => encodeURIComponent(id);
10
+
11
+ /** The rows that take an own key: installed agents the server says accept one, Claude last. */
12
+ export function agentKeyRows(agents = []) {
13
+ const rows = agents
14
+ .filter((a) => a && a.installed === true && a.acceptsKey === true)
15
+ .map((a) => ({
16
+ id: a.id,
17
+ label: typeof a.label === 'string' && a.label ? a.label : a.id,
18
+ keySet: a.keySet === true,
19
+ last4: a.keySet === true && typeof a.keyLast4 === 'string' ? a.keyLast4.slice(-4) : null,
20
+ }));
21
+ return {
22
+ keys: rows.filter((r) => r.id !== CLAUDE_ID),
23
+ claude: rows.find((r) => r.id === CLAUDE_ID) ?? null,
24
+ };
25
+ }
26
+
27
+ /** "••••1234" for a stored key, "" when none. */
28
+ export const maskedKey = (last4) => (last4 ? `••••${last4}` : '');
29
+
30
+ /** The confirm card's words for Claude's opt-in (DDR-252: the billing switch is explicit). */
31
+ export const CLAUDE_OPTIN_WORDS =
32
+ 'Claude will use this Anthropic API key instead of your Claude account. Every message is billed to that API account, per token, not to your Claude plan. Only for this profile; turn it off to go back to your account.';
33
+
34
+ /** A key as typed is worth sending: non-empty, one line, at most 512 characters. */
35
+ export function keyLooksValid(value) {
36
+ if (typeof value !== 'string') return false;
37
+ const v = value.trim();
38
+ return v.length > 0 && v.length <= 512 && !/\s/.test(v);
39
+ }
40
+
41
+ /** A refused save, in words. */
42
+ export function keyErrorWords(r) {
43
+ switch (r?.reason) {
44
+ case 'invalid-key':
45
+ return 'That does not look like an API key. Paste it again, on one line.';
46
+ case 'opt-in-required':
47
+ return 'Turn on the Anthropic API key first.';
48
+ case 'no-api-key':
49
+ return 'This agent takes no API key from Maude. Sign in to it instead.';
50
+ default:
51
+ return 'Could not save the key. Try again.';
52
+ }
53
+ }
54
+
55
+ async function post(fetchImpl, url, body) {
56
+ try {
57
+ const res = await fetchImpl(url, {
58
+ method: 'POST',
59
+ ...(body
60
+ ? { headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) }
61
+ : {}),
62
+ });
63
+ return (await res.json().catch(() => null)) ?? { ok: false };
64
+ } catch {
65
+ return { ok: false };
66
+ }
67
+ }
68
+
69
+ /** Save an agent's key (Claude's needs `optIn: true`, from the confirm card). Reloads the list. */
70
+ export async function saveAgentKey(
71
+ id,
72
+ key,
73
+ { optIn = false, fetchImpl = (...a) => fetch(...a) } = {}
74
+ ) {
75
+ const r = await post(fetchImpl, `/_api/agents/${enc(id)}/key`, {
76
+ key,
77
+ ...(optIn ? { optIn: true } : {}),
78
+ });
79
+ await loadAgents({ force: false, fetchImpl });
80
+ return r;
81
+ }
82
+
83
+ /** Remove an agent's key (for Claude: the opt-in goes with it). Reloads the list. */
84
+ export async function removeAgentKey(id, { fetchImpl = (...a) => fetch(...a) } = {}) {
85
+ const r = await post(fetchImpl, `/_api/agents/${enc(id)}/key-remove`);
86
+ await loadAgents({ force: false, fetchImpl });
87
+ return r;
88
+ }