shapewall 0.3.3 → 0.7.0

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 (157) hide show
  1. package/CHANGELOG.md +501 -0
  2. package/README.md +87 -4
  3. package/THIRD_PARTY.md +15 -0
  4. package/dist/agent/package-lock.json +594 -2
  5. package/dist/client/assets/BoardChat-WS8mAIW6.js +27 -0
  6. package/dist/client/assets/{BuzzDialog-Ix9qJvim.js → BuzzDialog-B-Mlr6wF.js} +1 -1
  7. package/dist/client/assets/ChecksPanel-Dx1ZLBR6.js +1 -0
  8. package/dist/client/assets/History-sXhmpcvs.js +1 -0
  9. package/dist/client/assets/LocalSettings-CtGzz9F3.js +1 -0
  10. package/dist/client/assets/{Management-NVFDtSZB.js → Management-CGHe8Wt9.js} +1 -1
  11. package/dist/client/assets/QuickConnect-DEk84E4A.js +1 -0
  12. package/dist/client/assets/{ShareDialog-By0ETg6n.js → ShareDialog-CGGx-AYK.js} +2 -2
  13. package/dist/client/assets/{app-update-dialog-RGIw24iZ.js → app-update-dialog-_bVp8hnC.js} +1 -1
  14. package/dist/client/assets/{circle-x-B6KfScOE.js → circle-x-BWoHA1QY.js} +1 -1
  15. package/dist/client/assets/index-Bm-Xd3YN.js +37 -0
  16. package/dist/client/assets/index-C7Ostk3K.css +2 -0
  17. package/dist/client/assets/play-BzQ3tRTv.js +1 -0
  18. package/dist/client/assets/{refresh-cw-vkgvCCfR.js → refresh-cw-BYBMyDKT.js} +1 -1
  19. package/dist/client/assets/{runtime-Bft6x_6E.js → runtime-Bvq10IhH.js} +1 -1
  20. package/dist/client/assets/ui-CAY6myRA.js +41 -0
  21. package/dist/client/build-info.json +1 -1
  22. package/dist/client/index.html +3 -3
  23. package/dist/client/sw.js +2 -2
  24. package/dist/console/assets/LocalAccountsApp-CWRguRRL.js +1 -0
  25. package/dist/console/assets/LocalConsole--I2SaIpr.js +2 -0
  26. package/dist/console/assets/LocalUsage-BmpCZ36k.js +1 -0
  27. package/dist/console/assets/app-update-dialog-B8M9plaT.js +1 -0
  28. package/dist/console/assets/app-version-muKrQqbL.js +1 -0
  29. package/dist/console/assets/console-CrhTsPbz.css +2 -0
  30. package/dist/console/assets/console-DxapQ9oS.js +11 -0
  31. package/dist/console/assets/local-agents-CoqD4dSN.js +6 -0
  32. package/dist/console/assets/minus-DDQ0ydcM.js +1 -0
  33. package/dist/console/assets/tailnet-C5Flwl_d.js +2 -0
  34. package/dist/console/assets/theme-DCd0qh4l.js +1 -0
  35. package/dist/console/assets/ui-Bry1mnCY.js +41 -0
  36. package/dist/console/assets/usage-CIPWnHaS.css +1 -0
  37. package/dist/console/assets/usage-CqVjMb7m.js +5 -0
  38. package/dist/console/assets/usage-D1G0GgE3.js +56 -0
  39. package/dist/console/build-info.json +1 -1
  40. package/dist/console/console.html +8 -2
  41. package/docs/agent-hardening.md +206 -0
  42. package/docs/agent-recipes.md +23 -0
  43. package/docs/agent-workflow.md +5 -0
  44. package/docs/agents.md +49 -6
  45. package/docs/architecture.md +10 -4
  46. package/docs/deployment.md +39 -17
  47. package/docs/design-system.md +56 -7
  48. package/docs/hosted.md +1800 -46
  49. package/docs/images/board-overview.png +0 -0
  50. package/docs/images/journeys.png +0 -0
  51. package/docs/images/live-comment.png +0 -0
  52. package/docs/images/phone-comment.png +0 -0
  53. package/docs/images/phone-overview.png +0 -0
  54. package/docs/images/variant.png +0 -0
  55. package/docs/where-to-run.md +119 -3
  56. package/package.json +6 -1
  57. package/src/agent/backends/acp.js +745 -44
  58. package/src/agent/branch-mode.js +17 -3
  59. package/src/agent/card-hash.js +65 -0
  60. package/src/agent/context.js +41 -0
  61. package/src/agent/exec-paths.js +105 -0
  62. package/src/agent/routes.js +23 -2
  63. package/src/agent/run.js +11 -1
  64. package/src/agent/starter/cli.js +6 -1
  65. package/src/agent/turn-changes.js +70 -8
  66. package/src/agent/usage-log.js +251 -0
  67. package/src/cli/args.js +37 -0
  68. package/src/cli/commands.js +20 -5
  69. package/src/cli/help.js +73 -3
  70. package/src/cli/init.js +2 -1
  71. package/src/cli/serve.js +20 -5
  72. package/src/cli.js +8 -0
  73. package/src/hosted/access-cli.js +99 -1
  74. package/src/hosted/activity-core.js +232 -0
  75. package/src/hosted/activity.js +375 -0
  76. package/src/hosted/admin.js +87 -13
  77. package/src/hosted/agent-approvals.js +29 -4
  78. package/src/hosted/agent-link.js +1032 -0
  79. package/src/hosted/agent-policy.js +10 -3
  80. package/src/hosted/agent-presence.js +63 -22
  81. package/src/hosted/agent-routes.js +135 -0
  82. package/src/hosted/agents.js +1206 -0
  83. package/src/hosted/app.js +178 -12
  84. package/src/hosted/auth-routes.js +156 -15
  85. package/src/hosted/auth.js +123 -20
  86. package/src/hosted/board-routes.js +129 -5
  87. package/src/hosted/chat-frames.js +246 -0
  88. package/src/hosted/chat-job.js +204 -0
  89. package/src/hosted/chat-permissions.js +203 -0
  90. package/src/hosted/chat-routes.js +1009 -0
  91. package/src/hosted/chat-store.js +477 -0
  92. package/src/hosted/comment-files.js +11 -1
  93. package/src/hosted/comments.js +1 -1
  94. package/src/hosted/console-routes.js +122 -34
  95. package/src/hosted/db.js +295 -0
  96. package/src/hosted/device-routes.js +88 -10
  97. package/src/hosted/device.js +54 -4
  98. package/src/hosted/events.js +3 -3
  99. package/src/hosted/guards.js +337 -27
  100. package/src/hosted/http.js +6 -3
  101. package/src/hosted/membership.js +22 -3
  102. package/src/hosted/operator-access.js +418 -0
  103. package/src/hosted/operator.js +106 -2
  104. package/src/hosted/ops-cli.js +238 -3
  105. package/src/hosted/pages.js +4 -0
  106. package/src/hosted/passkey-policy.js +75 -0
  107. package/src/hosted/permissions.js +52 -6
  108. package/src/hosted/presence.js +31 -2
  109. package/src/hosted/projects.js +30 -2
  110. package/src/hosted/public.js +4 -4
  111. package/src/hosted/ratelimit.js +14 -0
  112. package/src/hosted/retention.js +18 -2
  113. package/src/hosted/server.js +123 -11
  114. package/src/hosted/shares.js +2 -2
  115. package/src/hosted/step-up-settings.js +68 -0
  116. package/src/hosted/step-up.js +135 -0
  117. package/src/hosted/tailnet-identity.js +24 -0
  118. package/src/hosted/tailnet-routes.js +342 -0
  119. package/src/hosted/tailnet.js +220 -0
  120. package/src/hosted/tailscale.js +75 -9
  121. package/src/hosted/tenancy.js +66 -1
  122. package/src/hosted/token-api.js +119 -20
  123. package/src/hosted/token-events.js +5 -3
  124. package/src/hosted/tokens.js +29 -6
  125. package/src/hosted/turn-hub.js +152 -0
  126. package/src/hosted/usage-core.js +624 -0
  127. package/src/hosted/usage-layout.js +724 -0
  128. package/src/hosted/usage-routes.js +375 -0
  129. package/src/hosted/usage-shape.js +256 -0
  130. package/src/hosted/usage.js +641 -0
  131. package/src/remote/agent-connect.js +469 -0
  132. package/src/remote/agent-fix.js +611 -0
  133. package/src/remote/agent-job.js +216 -0
  134. package/src/remote/agent-link.js +906 -0
  135. package/src/remote/agent-policy-file.js +242 -0
  136. package/src/remote/credentials.js +35 -1
  137. package/src/remote/login.js +44 -9
  138. package/src/remote/share.js +1 -1
  139. package/src/remote/tailnet-cli.js +547 -0
  140. package/src/server/app.js +15 -0
  141. package/src/server/pairing-routes.js +34 -5
  142. package/src/server/tailscale.js +97 -4
  143. package/src/tailnet-proof.js +766 -0
  144. package/dist/client/assets/BoardChat-DxehB761.js +0 -27
  145. package/dist/client/assets/ChecksPanel-pA_ueIaJ.js +0 -1
  146. package/dist/client/assets/History-C9N32kCv.js +0 -1
  147. package/dist/client/assets/LocalSettings-DawtKnvi.js +0 -1
  148. package/dist/client/assets/QuickConnect-DO1TjS1t.js +0 -1
  149. package/dist/client/assets/index-CsqLdqGx.css +0 -2
  150. package/dist/client/assets/index-DnZarJ3U.js +0 -37
  151. package/dist/client/assets/shield-check-QzwGhz0t.js +0 -1
  152. package/dist/client/assets/ui-DI-FGWWK.js +0 -41
  153. package/dist/console/assets/LocalAccountsApp-VOCZg8dg.js +0 -1
  154. package/dist/console/assets/LocalConsole-CMwAW-mF.js +0 -1
  155. package/dist/console/assets/app-update-dialog-Cw6UDqNP.js +0 -1
  156. package/dist/console/assets/console-DgfWmUEx.js +0 -55
  157. package/dist/console/assets/console-Lis0vvdL.css +0 -2
@@ -18,6 +18,8 @@ export const DEFAULT_POLICY = {
18
18
  onlyMentions: false,
19
19
  dailyClaims: 50,
20
20
  maxOpenClaims: 3,
21
+ // Chat turns with connected agents, per project and day.
22
+ dailyTurns: 200,
21
23
  approveNonAdmin: true,
22
24
  }
23
25
  export const expandScopes = (scopes) => [...new Set(scopes.flatMap((scope) => (scope === 'comment' ? ['reply', 'status'] : [scope])))]
@@ -47,6 +49,7 @@ export function cleanPolicy(input) {
47
49
  for (const [key, max] of [
48
50
  ['dailyClaims', 10000],
49
51
  ['maxOpenClaims', 100],
52
+ ['dailyTurns', 100000],
50
53
  ]) {
51
54
  if (input[key] !== undefined && (!Number.isInteger(input[key]) || input[key] < 0 || input[key] > max))
52
55
  throw new AgentPolicyError(400, 'invalid_policy', `Invalid ${key}.`)
@@ -133,7 +136,9 @@ export function createAgentPolicy(t, { handleFor = () => null, now = () => new D
133
136
  function autoVouch(project, board, row, user, role) {
134
137
  if (trusted(agentPolicy(t, project, board), user)) vouch(project, board, row, user, { role })
135
138
  }
136
- function claim(project, board, row, token) {
139
+ // `agent`: the id of the token's agent (src/hosted/agents.js), kept on
140
+ // the claim. Returns the claim's id.
141
+ function claim(project, board, row, token, agent = null) {
137
142
  if (!state(project, board, row).dispatchable)
138
143
  throw new AgentPolicyError(403, 'not_dispatchable', 'A trusted member must send the current comment to an agent first.')
139
144
  const policy = agentPolicy(t, project, board)
@@ -180,10 +185,12 @@ export function createAgentPolicy(t, { handleFor = () => null, now = () => new D
180
185
  }).n
181
186
  if (daily >= policy.dailyClaims || open >= policy.maxOpenClaims)
182
187
  throw new AgentPolicyError(429, 'agent_budget', 'The project agent budget is exhausted. Ask an admin to review Agent policy.')
188
+ const id = randomBytes(16).toString('hex')
183
189
  t.run(
184
- 'INSERT INTO agent_claims (workspace_id,id,project_id,board_id,comment_number,token_id,hash,created_at) VALUES (:ws,:id,:project,:board,:number,:token,:hash,:at)',
185
- { project, board, number: row.number, token, id: randomBytes(16).toString('hex'), hash: dispatchHash(row), at },
190
+ 'INSERT INTO agent_claims (workspace_id,id,project_id,board_id,comment_number,token_id,agent_id,hash,created_at) VALUES (:ws,:id,:project,:board,:number,:token,:agent,:hash,:at)',
191
+ { project, board, number: row.number, token, agent, id, hash: dispatchHash(row), at },
186
192
  )
193
+ return id
187
194
  }
188
195
  return { state, vouch, autoVouch, claim, trusted }
189
196
  }
@@ -1,40 +1,81 @@
1
- // Is an agent listening? A comment on a hosted board is only acted on while
2
- // some machine runs `shapewall watch --remote` (or an MCP client) for its
3
- // project, with a token that has the claim scope. This remembers, per
4
- // project, when such a token last called the token API or held its event
5
- // stream open, so the board can say "Agent online: <name>" or "No agent is
6
- // listening".
1
+ // Which agents are listening? A comment on a hosted board is only acted on
2
+ // while some machine runs `shapewall watch --remote` (or an MCP client) for
3
+ // its project, with a token that has the claim scope. Each such token is an
4
+ // agent (src/hosted/agents.js). This remembers, per agent, when its token
5
+ // last called the token API or held its event stream open, so the board can
6
+ // say "Builder: online" or "offline since 10:42" for each one.
7
7
  //
8
8
  // In memory only, on purpose: it is a few seconds of truth, empty after a
9
- // restart until the next poll, and never written anywhere. It holds a
10
- // token's display name and a time, nothing else: no token id, no hash, no
11
- // address.
9
+ // restart until the next poll. (The agent's row keeps a last-seen time for
10
+ // "offline since"; src/hosted/agents.js writes it, at most once a minute,
11
+ // when seen() says it is due.) It holds an agent's id and display name and a
12
+ // time, nothing else: no token id, no hash, no address.
12
13
 
13
14
  export const AGENT_PRESENCE_TTL_MS = 90_000
14
- const PROJECTS_MAX = 5000
15
+ // How often an agent's last-seen time is worth writing to the database.
16
+ export const AGENT_SEEN_SAVE_MS = 60_000
17
+ const AGENTS_MAX = 20_000
15
18
 
16
19
  export function createAgentPresence({ now = () => new Date(), ttlMs = AGENT_PRESENCE_TTL_MS } = {}) {
20
+ // agent key -> { workspaceId, projectId, name, at, savedAt }, oldest first.
17
21
  const seenAt = new Map()
18
- const key = (workspaceId, projectId) => `${workspaceId}:${projectId}`
22
+ // project key -> the agent key last heard from (the `of` summary).
23
+ const latest = new Map()
24
+ const projectKey = (workspaceId, projectId) => `${workspaceId}:${projectId}`
25
+ const agentKey = (workspaceId, agentId) => `${workspaceId}:${agentId}`
26
+ const fresh = (entry) => Boolean(entry) && !entry.gone && now().getTime() - entry.at <= ttlMs
19
27
  return {
20
- // A claim-scoped token of this project was just heard from.
21
- seen(workspaceId, projectId, name) {
22
- if (!workspaceId || !projectId) return
23
- const id = key(workspaceId, projectId)
28
+ // A claim-scoped token of this project was just heard from. Returns
29
+ // { cameOnline, save }: whether the agent was offline until now, and
30
+ // whether its last-seen time is due to be written down.
31
+ seen(workspaceId, projectId, agentId, name) {
32
+ if (!workspaceId || !projectId || !agentId) return { cameOnline: false, save: false }
33
+ const id = agentKey(workspaceId, agentId)
34
+ const before = seenAt.get(id)
35
+ const at = now().getTime()
36
+ const save = !before || at - before.savedAt >= AGENT_SEEN_SAVE_MS
24
37
  // Re-inserted, so the map stays in order of last seen (oldest first).
25
38
  seenAt.delete(id)
26
- seenAt.set(id, { name: String(name || 'agent').slice(0, 80), at: now().getTime() })
27
- if (seenAt.size > PROJECTS_MAX) seenAt.delete(seenAt.keys().next().value)
39
+ seenAt.set(id, { workspaceId, projectId, name: String(name || 'agent').slice(0, 80), at, savedAt: save ? at : before.savedAt })
40
+ latest.set(projectKey(workspaceId, projectId), id)
41
+ if (seenAt.size > AGENTS_MAX) seenAt.delete(seenAt.keys().next().value)
42
+ return { cameOnline: !fresh(before), save }
28
43
  },
29
- // What board viewers are told: { online, name, lastSeenAt }. `name` and
30
- // `lastSeenAt` stay after it goes quiet, until the server restarts.
44
+ // One agent: { online, lastSeenAt }. `lastSeenAt` stays after it goes
45
+ // quiet, until the server restarts.
46
+ ofAgent(workspaceId, agentId) {
47
+ const entry = seenAt.get(agentKey(workspaceId, agentId))
48
+ if (!entry) return { online: false, lastSeenAt: null }
49
+ return { online: fresh(entry), lastSeenAt: new Date(entry.at).toISOString() }
50
+ },
51
+ // The project as a whole, in the shape boards read since 0.3.3
52
+ // (`agentListening`): { online, name, lastSeenAt } of the agent heard
53
+ // from last.
31
54
  of(workspaceId, projectId) {
32
- const entry = seenAt.get(key(workspaceId, projectId))
55
+ const entry = seenAt.get(latest.get(projectKey(workspaceId, projectId)))
33
56
  if (!entry) return { online: false, name: null, lastSeenAt: null }
34
- return { online: now().getTime() - entry.at <= ttlMs, name: entry.name, lastSeenAt: new Date(entry.at).toISOString() }
57
+ return { online: fresh(entry), name: entry.name, lastSeenAt: new Date(entry.at).toISOString() }
58
+ },
59
+ // A connected agent's link closed (src/hosted/agent-link.js): it is
60
+ // offline now, not in 90 seconds. Its last-seen time stays.
61
+ gone(workspaceId, agentId) {
62
+ const entry = seenAt.get(agentKey(workspaceId, agentId))
63
+ if (entry) entry.gone = true
64
+ },
65
+ // A revoked agent is not listening any more.
66
+ forgetAgent(workspaceId, agentId) {
67
+ const id = agentKey(workspaceId, agentId)
68
+ const entry = seenAt.get(id)
69
+ seenAt.delete(id)
70
+ if (!entry || latest.get(projectKey(entry.workspaceId, entry.projectId)) !== id) return
71
+ // The project's summary falls back to the agent heard from before it.
72
+ const project = projectKey(entry.workspaceId, entry.projectId)
73
+ latest.delete(project)
74
+ for (const [key, other] of seenAt) if (projectKey(other.workspaceId, other.projectId) === project) latest.set(project, key)
35
75
  },
36
76
  forget(workspaceId, projectId) {
37
- seenAt.delete(key(workspaceId, projectId))
77
+ for (const [id, entry] of [...seenAt]) if (entry.workspaceId === workspaceId && entry.projectId === projectId) seenAt.delete(id)
78
+ latest.delete(projectKey(workspaceId, projectId))
38
79
  },
39
80
  }
40
81
  }
@@ -0,0 +1,135 @@
1
+ // The console API for agents (src/hosted/app.js): the workspace overview,
2
+ // a project's Agents page, and each agent setting. Thin on purpose: every
3
+ // setting is changed by one function in src/hosted/agents.js (setIdentity,
4
+ // setDefault, setAudience, setApprovalRule, revoke), which checks the role,
5
+ // validates, writes and audits. These routes only read the request and call
6
+ // it, so a CLI command or an admin tool can call the same function later.
7
+ //
8
+ // Listing and managing agents is for the people who manage tokens (admins
9
+ // and owners). Widening who may use an agent, and its approval rule, ask
10
+ // for a fresh passkey check, like the agent policy.
11
+
12
+ import { agentPolicy } from './agent-policy.js'
13
+ import { transaction } from './db.js'
14
+ import { HttpError } from './http.js'
15
+ import { AGENT_AUDIENCES, APPROVAL_ROLES, APPROVAL_RULES, NEEDS, roleAtLeast } from './permissions.js'
16
+ import { AGENT_KINDS } from './agents.js'
17
+
18
+ export function mountAgentRoutes(consoleApi, { db, publicOrigin, handle, inWorkspace, inProject, stepUp, auditOf, agentsFor }) {
19
+ const actorOf = (req) => ({
20
+ userId: req.user.id,
21
+ role: req.role,
22
+ audit: (action, target, detail) => auditOf(req, action, target, detail),
23
+ })
24
+ // What the editors offer, so the console never hardcodes a list the
25
+ // server would refuse.
26
+ const options = { audiences: AGENT_AUDIENCES, approvalRules: APPROVAL_RULES, approvalRoles: APPROVAL_ROLES, kinds: AGENT_KINDS }
27
+
28
+ // Every agent of the workspace, with the project each serves.
29
+ consoleApi.get(
30
+ '/workspaces/:ws/agents',
31
+ inWorkspace(NEEDS.tokens),
32
+ handle((req, res) => {
33
+ res.json({ agents: transaction(db, () => agentsFor(req.t).consoleList(null, { audit: actorOf(req).audit })), options })
34
+ }),
35
+ )
36
+
37
+ // One project's agents, its default agent, and what the audience editor
38
+ // needs: the members who could be named, and whom the agent policy trusts.
39
+ consoleApi.get(
40
+ '/workspaces/:ws/projects/:pid/agents',
41
+ inWorkspace(NEEDS.tokens),
42
+ inProject,
43
+ handle((req, res) => {
44
+ const agents = agentsFor(req.t)
45
+ const policy = agentPolicy(req.t, req.project.id)
46
+ const members = req.t
47
+ .all(
48
+ 'SELECT m.user_id, m.role, u.handle, u.display_name FROM memberships m JOIN users u ON u.id = m.user_id WHERE m.workspace_id = :ws ORDER BY u.display_name, u.handle',
49
+ )
50
+ .filter((row) => roleAtLeast(row.role, 'member'))
51
+ .map((row) => ({
52
+ userId: row.user_id,
53
+ handle: row.handle,
54
+ displayName: row.display_name,
55
+ role: row.role,
56
+ // Whether the agent policy lets this person send work to an agent at all.
57
+ trusted: agents.trusted(req.project.id, null, row.user_id),
58
+ }))
59
+ res.json({
60
+ agents: transaction(db, () => agents.consoleList(req.project.id, { audit: actorOf(req).audit })),
61
+ defaultAgentId: agents.defaultAgent(req.project.id)?.id || null,
62
+ members,
63
+ policy: { trustedRoles: policy.trustedRoles, trustedHandles: policy.trustedHandles },
64
+ connect: { origin: publicOrigin },
65
+ options,
66
+ })
67
+ }),
68
+ )
69
+
70
+ // Name, icon, tint, kind: { name?, icon?, tint?, kind? }.
71
+ consoleApi.patch(
72
+ '/workspaces/:ws/projects/:pid/agents/:aid',
73
+ inWorkspace(NEEDS.tokens),
74
+ inProject,
75
+ handle((req, res) => {
76
+ const { name, icon, tint, kind } = req.body || {}
77
+ const agent = transaction(db, () =>
78
+ agentsFor(req.t).setIdentity(actorOf(req), req.project.id, req.params.aid, { name, icon, tint, kind }),
79
+ )
80
+ res.json({ agent })
81
+ }),
82
+ )
83
+
84
+ // Who may send it work: { audience, users }.
85
+ consoleApi.put(
86
+ '/workspaces/:ws/projects/:pid/agents/:aid/audience',
87
+ inWorkspace(NEEDS.tokens),
88
+ stepUp('agents'),
89
+ inProject,
90
+ handle((req, res) => {
91
+ const { audience, users } = req.body || {}
92
+ const agent = transaction(db, () => agentsFor(req.t).setAudience(actorOf(req), req.project.id, req.params.aid, { audience, users }))
93
+ res.json({ agent })
94
+ }),
95
+ )
96
+
97
+ // The approval rule and who decides: { rule?, changeRole?, answerRole? }.
98
+ consoleApi.put(
99
+ '/workspaces/:ws/projects/:pid/agents/:aid/approval',
100
+ inWorkspace(NEEDS.tokens),
101
+ stepUp('agents'),
102
+ inProject,
103
+ handle((req, res) => {
104
+ const { rule, changeRole, answerRole } = req.body || {}
105
+ const agent = transaction(db, () =>
106
+ agentsFor(req.t).setApprovalRule(actorOf(req), req.project.id, req.params.aid, { rule, changeRole, answerRole }),
107
+ )
108
+ res.json({ agent })
109
+ }),
110
+ )
111
+
112
+ // The project's default agent: { agentId } (null: any agent).
113
+ consoleApi.put(
114
+ '/workspaces/:ws/projects/:pid/default-agent',
115
+ inWorkspace(NEEDS.tokens),
116
+ inProject,
117
+ handle((req, res) => {
118
+ const agentId = req.body?.agentId
119
+ if (agentId !== null && typeof agentId !== 'string')
120
+ throw new HttpError(400, 'invalid_agent', 'agentId: an agent id, or null for any agent.')
121
+ const defaultAgentId = transaction(db, () => agentsFor(req.t).setDefault(actorOf(req), req.project.id, agentId))
122
+ res.json({ defaultAgentId })
123
+ }),
124
+ )
125
+
126
+ // Revokes the agent's token (the same as revoking it on the Tokens page).
127
+ consoleApi.delete(
128
+ '/workspaces/:ws/projects/:pid/agents/:aid',
129
+ inWorkspace(NEEDS.tokens),
130
+ inProject,
131
+ handle((req, res) => {
132
+ res.json(transaction(db, () => agentsFor(req.t).revoke(actorOf(req), req.project.id, req.params.aid)))
133
+ }),
134
+ )
135
+ }