@engenty/wizards-runtime 0.16.2 → 0.17.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 (156) hide show
  1. package/apps/runtime/dist/agents/studio-assistant.js +13 -6
  2. package/apps/runtime/dist/app.js +2 -0
  3. package/apps/runtime/dist/assistant/knows.js +15 -2
  4. package/apps/runtime/dist/broker/account.js +20 -1
  5. package/apps/runtime/dist/broker/call.js +131 -22
  6. package/apps/runtime/dist/broker/grant.js +25 -1
  7. package/apps/runtime/dist/broker/host.js +8 -2
  8. package/apps/runtime/dist/broker/index.js +5 -4
  9. package/apps/runtime/dist/broker/log.js +178 -24
  10. package/apps/runtime/dist/broker/over.js +38 -0
  11. package/apps/runtime/dist/broker/rights.js +192 -0
  12. package/apps/runtime/dist/broker/rwx.js +309 -53
  13. package/apps/runtime/dist/broker/send.js +109 -44
  14. package/apps/runtime/dist/bundles/fetch.js +119 -0
  15. package/apps/runtime/dist/bundles/index.js +621 -0
  16. package/apps/runtime/dist/bundles/read.js +607 -0
  17. package/apps/runtime/dist/connectors/external.js +532 -61
  18. package/apps/runtime/dist/connectors/person-sign-ins.js +1 -0
  19. package/apps/runtime/dist/connectors/reload.js +73 -0
  20. package/apps/runtime/dist/connectors/space-connections.js +118 -6
  21. package/apps/runtime/dist/db/migrations/tenant/0058_mcp_state_across_roles.sql +26 -0
  22. package/apps/runtime/dist/db/migrations/tenant/0059_space_connection_paused.sql +2 -0
  23. package/apps/runtime/dist/db/migrations/tenant/0060_space_bundle.sql +21 -0
  24. package/apps/runtime/dist/db/migrations/tenant/0061_space_connection_limits.sql +2 -0
  25. package/apps/runtime/dist/db/migrations/tenant/meta/0058_snapshot.json +6696 -0
  26. package/apps/runtime/dist/db/migrations/tenant/meta/0059_snapshot.json +6710 -0
  27. package/apps/runtime/dist/db/migrations/tenant/meta/0060_snapshot.json +6857 -0
  28. package/apps/runtime/dist/db/migrations/tenant/meta/0061_snapshot.json +6871 -0
  29. package/apps/runtime/dist/db/migrations/tenant/meta/_journal.json +28 -0
  30. package/apps/runtime/dist/db/schema.js +81 -3
  31. package/apps/runtime/dist/docs/dev/broker.md +316 -50
  32. package/apps/runtime/dist/docs/dev/plugins/space.md +13 -2
  33. package/apps/runtime/dist/docs/user-guide/capabilities.md +172 -10
  34. package/apps/runtime/dist/docs/user-guide/connectors.md +57 -7
  35. package/apps/runtime/dist/docs/user-guide/protocol.md +44 -7
  36. package/apps/runtime/dist/docs/user-guide/test-and-share.md +1 -1
  37. package/apps/runtime/dist/docs/user-guide/together.md +4 -4
  38. package/apps/runtime/dist/elements/caller.js +2 -2
  39. package/apps/runtime/dist/elements/knowledge.js +16 -12
  40. package/apps/runtime/dist/engenty/connections-external/build-connector.js +23 -8
  41. package/apps/runtime/dist/engenty/connections-external/import-service.js +18 -9
  42. package/apps/runtime/dist/engenty/connections-external/importer/classify.js +12 -0
  43. package/apps/runtime/dist/engenty/connections-external/invoke/http-invoker.js +1 -0
  44. package/apps/runtime/dist/engenty/connections-external/net/guarded-fetch.js +6 -3
  45. package/apps/runtime/dist/engenty/connections-sdk/oauth2.js +2 -2
  46. package/apps/runtime/dist/engenty/shims/web-ingest.js +64 -0
  47. package/apps/runtime/dist/engine/resources.js +15 -6
  48. package/apps/runtime/dist/engine/runner.js +2 -1
  49. package/apps/runtime/dist/engine/steps.js +3 -2
  50. package/apps/runtime/dist/env.js +2 -0
  51. package/apps/runtime/dist/index.js +19 -2
  52. package/apps/runtime/dist/live/hub.js +17 -3
  53. package/apps/runtime/dist/live/origin.js +6 -4
  54. package/apps/runtime/dist/live/views.js +38 -7
  55. package/apps/runtime/dist/mcp/handler.js +2 -2
  56. package/apps/runtime/dist/mcp/seen.js +71 -51
  57. package/apps/runtime/dist/mcp/tools.js +5 -1
  58. package/apps/runtime/dist/models.js +18 -0
  59. package/apps/runtime/dist/prompts/mcp.md +1 -1
  60. package/apps/runtime/dist/prompts/tools.md +17 -1
  61. package/apps/runtime/dist/routes/api.js +2 -2
  62. package/apps/runtime/dist/routes/broker.js +59 -20
  63. package/apps/runtime/dist/routes/bundles.js +91 -0
  64. package/apps/runtime/dist/routes/connections.js +14 -3
  65. package/apps/runtime/dist/routes/live.js +4 -2
  66. package/apps/runtime/dist/routes/rules.js +53 -9
  67. package/apps/runtime/dist/routes/studio.js +36 -20
  68. package/apps/runtime/dist/services/api.js +4 -2
  69. package/apps/runtime/dist/services/cloud.js +2 -0
  70. package/apps/runtime/dist/services/draft-events.js +79 -6
  71. package/apps/runtime/dist/services/errors.js +1 -0
  72. package/apps/runtime/dist/services/files.js +2 -2
  73. package/apps/runtime/dist/services/knowledge.js +93 -27
  74. package/apps/runtime/dist/services/members-studio.js +22 -2
  75. package/apps/runtime/dist/services/skills.js +61 -5
  76. package/apps/runtime/dist/services/space-categories.js +83 -5
  77. package/apps/runtime/dist/services/space-index.js +173 -25
  78. package/apps/runtime/dist/services/space-origin.js +28 -11
  79. package/apps/runtime/dist/services/synced-spaces.js +5 -1
  80. package/apps/runtime/dist/services/wizards.js +48 -2
  81. package/apps/runtime/dist/services/work-ai.js +15 -5
  82. package/apps/runtime/dist/sharing/jobs.js +8 -2
  83. package/apps/runtime/dist/sync/hub.js +17 -3
  84. package/apps/runtime/dist/sync/records.js +6 -1
  85. package/apps/runtime/dist/texts.js +42 -5
  86. package/apps/runtime/dist/tools/connections.js +8 -0
  87. package/apps/runtime/dist/tools/connector.js +13 -1
  88. package/apps/runtime/dist/tools/index.js +21 -10
  89. package/apps/runtime/dist/tools/skills.js +11 -1
  90. package/apps/runtime/dist/tools/web-search.js +36 -0
  91. package/apps/web/dist/assets/{AboutDialog-BCcD8hKm.js → AboutDialog-mZCBU7KF.js} +1 -1
  92. package/apps/web/dist/assets/{BlockEditor-DgaUm2pR.js → BlockEditor-Dk0QwLSU.js} +1 -1
  93. package/apps/web/dist/assets/CapabilitiesPage-GkpgF-wZ.js +2 -0
  94. package/apps/web/dist/assets/{ChangelogDialog-B08elwyr.js → ChangelogDialog-BhyilZWA.js} +1 -1
  95. package/apps/web/dist/assets/{CodeEditor-BiyOk_SC.js → CodeEditor-DULYZbuE.js} +1 -1
  96. package/apps/web/dist/assets/EditorPage-D_mCdYdZ.js +3 -0
  97. package/apps/web/dist/assets/{FlowPreview-DF_6TvZ0.js → FlowPreview-ykxZxWnk.js} +1 -1
  98. package/apps/web/dist/assets/{LangSwitch-Ba4OWLzJ.js → LangSwitch-iO54w9AP.js} +1 -1
  99. package/apps/web/dist/assets/{Marketplace-DqziLf66.js → Marketplace-C97fH7UC.js} +2 -2
  100. package/apps/web/dist/assets/{NewWizardChannels-CsIGp0nZ.js → NewWizardChannels-CsSV7sm2.js} +1 -1
  101. package/apps/web/dist/assets/{NewWizardPage-CzQAkKMy.js → NewWizardPage-Ebgw-Vf_.js} +1 -1
  102. package/apps/web/dist/assets/{OnboardingPage-B9gr2OX5.js → OnboardingPage-BZlloT7v.js} +1 -1
  103. package/apps/web/dist/assets/PreviewPane-CJuqY5uA.js +7 -0
  104. package/apps/web/dist/assets/ProtocolSection-CPaDKVpZ.js +1 -0
  105. package/apps/web/dist/assets/{PublicRunner-BVs8-Vu3.js → PublicRunner-4Lf4SnwB.js} +1 -1
  106. package/apps/web/dist/assets/{ReadOnly--4CIJCDD.js → ReadOnly-CXQ41R9Q.js} +1 -1
  107. package/apps/web/dist/assets/SettingsPage-C-8BgK9O.js +2 -0
  108. package/apps/web/dist/assets/{SetupPage-9Zk-KUGA.js → SetupPage-4_K083Wl.js} +1 -1
  109. package/apps/web/dist/assets/{SharePage-D_Ab0gjr.js → SharePage-CQjgMcDI.js} +1 -1
  110. package/apps/web/dist/assets/{SkillEditor-DvtameHg.js → SkillEditor-DT5hQwbg.js} +1 -1
  111. package/apps/web/dist/assets/{Skills-Cf1Kl4Fy.js → Skills-D7V_9_xo.js} +1 -1
  112. package/apps/web/dist/assets/SpacePage-D3rYS1bA.js +2 -0
  113. package/apps/web/dist/assets/{UpdateCheck-DweZBVP6.js → UpdateCheck-DVgAJmy8.js} +2 -2
  114. package/apps/web/dist/assets/changelog-DzQCUSsx.js +1 -0
  115. package/apps/web/dist/assets/{code-editor-B1RbFTAZ.js → code-editor-5T7ilBjd.js} +2 -2
  116. package/apps/web/dist/assets/{host-CKS5U5Vg.js → host-CCijW1hj.js} +6 -6
  117. package/apps/web/dist/assets/i18n-DCPDesT-.js +55 -0
  118. package/apps/web/dist/assets/{index-Ckcn5vy7.js → index-C_iWl8iS.js} +3 -3
  119. package/apps/web/dist/assets/index-zcPlodHx.css +1 -0
  120. package/apps/web/dist/assets/minus-BpYkOkYw.js +1 -0
  121. package/apps/web/dist/assets/{read-aloud-Bq88XUHC.js → read-aloud-Qb719not.js} +1 -1
  122. package/apps/web/dist/assets/{runners-DvYVe9Dt.js → runners-BeZ84YH_.js} +1 -1
  123. package/apps/web/dist/assets/{session-D2bjiA3P.js → session-CEJRkSP5.js} +1 -1
  124. package/apps/web/dist/assets/{theme-CNJIC2be.js → theme-y_owNosi.js} +1 -1
  125. package/apps/web/dist/assets/{ui-Mplfhz_Z.js → ui-BacfkQ7Q.js} +1 -1
  126. package/apps/web/dist/assets/{useMutation-8QRM-z4M.js → useMutation-CgzFr4qH.js} +1 -1
  127. package/apps/web/dist/assets/{viewers-CEocUV_2.js → viewers-CkyHAjnk.js} +2 -2
  128. package/apps/web/dist/index.html +12 -12
  129. package/apps/web/dist/mcp/flow.html +2 -2
  130. package/node_modules/@engenty/wizards/dist/plugin/index.d.ts +7 -2
  131. package/node_modules/@engenty/wizards-shared/dist/bundles.d.ts +219 -0
  132. package/node_modules/@engenty/wizards-shared/dist/bundles.js +19 -0
  133. package/node_modules/@engenty/wizards-shared/dist/connectors.d.ts +117 -4
  134. package/node_modules/@engenty/wizards-shared/dist/connectors.js +14 -0
  135. package/node_modules/@engenty/wizards-shared/dist/feature-flags.d.ts +12 -0
  136. package/node_modules/@engenty/wizards-shared/dist/feature-flags.js +12 -0
  137. package/node_modules/@engenty/wizards-shared/dist/run.d.ts +11 -0
  138. package/node_modules/@engenty/wizards-shared/dist/run.js +22 -0
  139. package/node_modules/@engenty/wizards-shared/dist/rwx.d.ts +84 -1
  140. package/node_modules/@engenty/wizards-shared/dist/rwx.js +69 -1
  141. package/node_modules/@engenty/wizards-shared/dist/skills.d.ts +20 -3
  142. package/node_modules/@engenty/wizards-shared/dist/skills.js +5 -2
  143. package/node_modules/@engenty/wizards-shared/dist/spaces.d.ts +3 -2
  144. package/node_modules/@engenty/wizards-shared/dist/spaces.js +12 -3
  145. package/package.json +1 -1
  146. package/plugin/.claude-plugin/plugin.json +1 -1
  147. package/plugin/plugin.json +1 -1
  148. package/apps/web/dist/assets/CapabilitiesPage-DuJJV1ll.js +0 -2
  149. package/apps/web/dist/assets/EditorPage-EMFTHEYD.js +0 -3
  150. package/apps/web/dist/assets/PreviewPane-DSOV3o-1.js +0 -7
  151. package/apps/web/dist/assets/SettingsPage-z2u8lSe2.js +0 -2
  152. package/apps/web/dist/assets/SpacePage-CyqVzX9b.js +0 -2
  153. package/apps/web/dist/assets/changelog-C85v9OWg.js +0 -1
  154. package/apps/web/dist/assets/connection-sign-in-CaVvh5GQ.js +0 -1
  155. package/apps/web/dist/assets/i18n-Bymk5Ev-.js +0 -55
  156. package/apps/web/dist/assets/index-w-3J3ZJ9.css +0 -1
@@ -15,7 +15,7 @@ import { elementTools } from "../elements/catalogue.js";
15
15
  import { brokered, elementCall } from "../elements/gate.js";
16
16
  import { currentFeatures } from "../features.js";
17
17
  import { managed } from "../manage.js";
18
- import { chatModel, contextWindowOf, costOf, currentTier, gatewayTools, searchAllowed, textModel, totalUsage, usageOf, } from "../models.js";
18
+ import { chatModel, contextWindowOf, costOf, currentTier, gatewayTools, searchAllowed, textModel, toolAccess, totalUsage, usageOf, } from "../models.js";
19
19
  import { mountsOf } from "../mounts/source.js";
20
20
  import { prompt } from "../prompts.js";
21
21
  import { contentPolicy } from "../safety.js";
@@ -31,6 +31,7 @@ import { currentTenant } from "../tenants/tenant.js";
31
31
  import { serverText } from "../texts.js";
32
32
  import { assistantPageOf, assistantToolsOf } from "../tools/plugin.js";
33
33
  import { attempt as attemptTool, imageOutput } from "../tools/shared.js";
34
+ import { gatewayWebSearch } from "../tools/web-search.js";
34
35
  import { gooseConnectionTools } from "./goose-connections.js";
35
36
  import { spaceAgent } from "./space-agent.js";
36
37
  import { chatTurnWay } from "./subscription.js";
@@ -98,13 +99,14 @@ function activity(tool, args, lang) {
98
99
  }
99
100
  /** A tool failure the model can read and fix, instead of a crashed turn: the whole message. */
100
101
  const attempt = (run) => attemptTool(run, { limit: Number.POSITIVE_INFINITY });
101
- async function spaceState(userId, spaceId) {
102
+ async function spaceState(userId, role, spaceId) {
102
103
  const p = await ownedSpace(userId, spaceId);
103
104
  const files = await spaceFiles(spaceId);
104
105
  const categories = await categoryRows(spaceId);
105
106
  return {
106
107
  knowledge: {
107
- ...(await knowledgeCounts(spaceId)),
108
+ // How much of it Goose may read for the person.
109
+ ...(await knowledgeCounts(spaceId, subjectsOfGoose(userId, role))),
108
110
  categories: categories.map((c) => `${c.name} (${c.type}${c.proposed ? ", proposed" : ""})`),
109
111
  },
110
112
  title: p.brand.name ?? "",
@@ -806,7 +808,8 @@ export async function runStudioAssistant(input) {
806
808
  let changes = 0;
807
809
  // The way the settings chose for the studio's chats: on a client, its tools reach it over MCP.
808
810
  const way = await chatTurnWay(managed);
809
- const resolved = await assistantModel(way, input);
811
+ // The gateway's way for web search, asked beside the model rather than after it.
812
+ const [resolved, webAccess] = await Promise.all([assistantModel(way, input), toolAccess()]);
810
813
  const wrote = () => {
811
814
  changes++;
812
815
  input.onChanged?.();
@@ -972,7 +975,11 @@ export async function runStudioAssistant(input) {
972
975
  traced(tools, input, toolCalls);
973
976
  // The organization's tier may allow no web search.
974
977
  const search = await searchAllowed();
975
- if (search && resolved.vendor === "anthropic") {
978
+ const access = search ? webAccess : null;
979
+ if (access) {
980
+ tools.web_search = gatewayWebSearch(access);
981
+ }
982
+ else if (search && resolved.vendor === "anthropic") {
976
983
  tools.web_search = anthropic.tools.webSearch_20250305({ maxUses: 5 });
977
984
  }
978
985
  else if (search && resolved.gateway) {
@@ -1018,7 +1025,7 @@ export async function runStudioAssistant(input) {
1018
1025
  ].join("\n\n")
1019
1026
  : "",
1020
1027
  writable
1021
- ? `The space now:\n\`\`\`json\n${JSON.stringify(await spaceState(input.user.id, writable.id))}\n\`\`\``
1028
+ ? `The space now:\n\`\`\`json\n${JSON.stringify(await spaceState(input.user.id, input.user.role, writable.id))}\n\`\`\``
1022
1029
  : space
1023
1030
  ? prompt("goose", "turn.space_read_only", { name: space.name })
1024
1031
  : "No space is open.",
@@ -26,6 +26,7 @@ import { openCommand } from "./open-command.js";
26
26
  import { PLUGIN_NAME, pluginArchive, pluginMarketplace } from "./plugin.js";
27
27
  import { apiRoutes } from "./routes/api.js";
28
28
  import { brokerRoutes } from "./routes/broker.js";
29
+ import { bundleRoutes } from "./routes/bundles.js";
29
30
  import { clientContractRoutes } from "./routes/client-contract.js";
30
31
  import { clientSpaceRoutes } from "./routes/client-spaces.js";
31
32
  import { connectionRoutes } from "./routes/connections.js";
@@ -348,6 +349,7 @@ if (serves.studio) {
348
349
  app.route("/api/studio", ruleRoutes);
349
350
  app.route("/api/studio", clientSpaceRoutes);
350
351
  app.route("/api/studio", connectionRoutes);
352
+ app.route("/api/studio", bundleRoutes);
351
353
  app.route("/api/studio", brokerRoutes);
352
354
  app.route("/api/studio", dateienRoutes);
353
355
  app.route("/api/studio/runtime", runtimeSettingsRoutes);
@@ -14,6 +14,7 @@ import { pluginToolsOf } from "../plugins/registry.js";
14
14
  import { prompt } from "../prompts.js";
15
15
  import { earlierChats, readChat, searchChats } from "../services/assistant-chats.js";
16
16
  import { asLang, marketplaceDetail, searchMarketplace } from "../services/marketplace.js";
17
+ import { studioSpaces } from "../services/members-studio.js";
17
18
  import { listWizards } from "../services/wizards.js";
18
19
  import { serverText } from "../texts.js";
19
20
  import { attempt } from "../tools/shared.js";
@@ -207,17 +208,29 @@ export function knowsTools(ctx) {
207
208
  const runIds = [...new Set(usage.months.flatMap((m) => m.runs.map((r) => r.runId)))];
208
209
  const runs = runIds.length
209
210
  ? await db
210
- .select({ id: schema.run.id, wizard: schema.wizard.title, mode: schema.run.mode })
211
+ .select({
212
+ id: schema.run.id,
213
+ wizard: schema.wizard.title,
214
+ spaceId: schema.wizard.spaceId,
215
+ mode: schema.run.mode,
216
+ })
211
217
  .from(schema.run)
212
218
  .innerJoin(schema.wizard, eq(schema.wizard.id, schema.run.wizardId))
213
219
  .where(inArray(schema.run.id, runIds))
214
220
  : [];
215
221
  const byRun = new Map(runs.map((r) => [r.id, r]));
222
+ // A member reads no wizard's use in a space whose Studio is closed to them.
223
+ const open = await studioSpaces();
216
224
  return {
217
225
  months: usage.months.map((m) => {
218
226
  const perWizard = new Map();
219
227
  for (const r of m.runs) {
220
- const title = byRun.get(r.runId)?.wizard ?? "(a run of another install)";
228
+ const run = byRun.get(r.runId);
229
+ const title = !run
230
+ ? "(a run of another install)"
231
+ : open && !open.has(run.spaceId)
232
+ ? "(wizards of spaces whose Studio is closed to you)"
233
+ : run.wizard;
221
234
  perWizard.set(title, (perWizard.get(title) ?? 0) + r.credits);
222
235
  }
223
236
  return {
@@ -153,12 +153,31 @@ export async function spaceAccount(row, connector, who) {
153
153
  };
154
154
  return { connector, ctx, label };
155
155
  }
156
- /** „Letzte Nutzung“: the connection was just used. */
156
+ /**
157
+ * What a connection of the space says of a call now: whether it pauses, its limits, and whether
158
+ * it was used before. Read on every call, so a pause or a limit binds a step or a chat turn that
159
+ * read the row before it, and only the first call of all is a first use. Null where the
160
+ * connection is gone.
161
+ */
162
+ export async function stateOf(id) {
163
+ const row = await db.query.spaceConnection.findFirst({
164
+ where: eq(schema.spaceConnection.id, id),
165
+ columns: {
166
+ pausedAt: true,
167
+ pausedBy: true,
168
+ callsPerRun: true,
169
+ callsPerDay: true,
170
+ lastUsedAt: true,
171
+ },
172
+ });
173
+ return row ?? null;
174
+ }
157
175
  /** The rights the provider granted a connection of the space at its sign-in; none for keys. */
158
176
  export function grantedScopes(row) {
159
177
  const data = row.secret ? unseal(row.secret) : null;
160
178
  return data?.scopes ?? [];
161
179
  }
180
+ /** „Letzte Nutzung“: the connection was just used. */
162
181
  export async function markUsed(row) {
163
182
  await db
164
183
  .update(schema.spaceConnection)
@@ -1,7 +1,8 @@
1
- import { keptSecret, markUsed, spaceAccount, wizardAccount } from "./account.js";
1
+ import { keptSecret, markUsed, spaceAccount, stateOf, wizardAccount, } from "./account.js";
2
2
  import { inGrant, runGrant } from "./grant.js";
3
- import { SIGN_IN_FAILED, summarizeInput, writeLog } from "./log.js";
4
- import { guestSendCheck, recipientsOf } from "./send.js";
3
+ import { CALLS_PER_DAY, CALLS_PER_RUN, closeLine, limitHeard, SIGN_IN_FAILED, summarizeInput, takePlace, writeLog, } from "./log.js";
4
+ import { runOver } from "./over.js";
5
+ import { channelOf, guestSendCheck, postsToChannel, recipientsOf } from "./send.js";
5
6
  /**
6
7
  * A wizard's step calls as the person running it: the store's holder says who that is, their
7
8
  * role in the organization whether they count as an admin.
@@ -22,6 +23,7 @@ export function whereOf(ctx) {
22
23
  return {
23
24
  ...whereOfRun({ id: ctx.runId, wizardId: ctx.store.wizardId, mode: ctx.test ? "test" : "live" }, ctx.store.holder, ctx.space.id),
24
25
  stepId: ctx.stepId,
26
+ ...(ctx.share ? { share: true } : {}),
25
27
  };
26
28
  }
27
29
  /** Where a call about a run is made (a share job, a step). */
@@ -35,7 +37,10 @@ export function whereOfRun(run, holder, spaceId) {
35
37
  guest: mode === "live" && !holder.startsWith("u:"),
36
38
  };
37
39
  }
38
- /** The rules' view of a connector action's group. */
40
+ /**
41
+ * The rules' view of a connector action's group. An imported connector's group is what an admin
42
+ * said the action does, where one did, before the importer's guess (`groupIn`, importer/classify.ts).
43
+ */
39
44
  export function kindOf(group) {
40
45
  return group === "destructive" || group === "write" || group === "send" ? group : "read";
41
46
  }
@@ -56,8 +61,9 @@ export function inAudience(audience, who) {
56
61
  * comes first: a wizard's connection outside it, or an action the grant does not name, is denied
57
62
  * whatever the rules say.
58
63
  *
59
- * A connection of the space: outside its audience nothing runs, nor an action the connection is
60
- * not for. Its rules say per kind of action Erlaubt, Mit Freigabe or Nie. On a connection that
64
+ * A connection of the space: a pause is asked first, for every caller and before a plugin's own
65
+ * job: nobody reaches a connection an admin paused. Outside its audience nothing runs, nor an
66
+ * action the connection is not for. Its rules say per kind of action Erlaubt, Mit Freigabe or Nie. On a connection that
61
67
  * signs in as the space an AI step only reads: a write there comes from a fixed `call` step (the
62
68
  * wizard's fields, no model) or a Work chat's confirmed action, and never asks the person running;
63
69
  * „Mit Freigabe“ there asks the space's admins (`admins`). Where each person signs in, „Mit
@@ -79,6 +85,10 @@ export function decide(call) {
79
85
  }
80
86
  if (connection.kind === "space_connection") {
81
87
  const { row } = connection;
88
+ const paused = pausedDecision(row);
89
+ if (paused) {
90
+ return paused;
91
+ }
82
92
  const ownJob = who.kind === "plugin" && row.plugin === who.id && !call.where.runId && !call.where.chatId;
83
93
  if (ownJob) {
84
94
  return { outcome: "allow", rule: "own_job", reason: "The plugin's own connection." };
@@ -154,6 +164,14 @@ export function decide(call) {
154
164
  reason: kind === "read" ? "Reading is allowed." : "The wizard allows this action.",
155
165
  };
156
166
  }
167
+ /** What the log and „Was darf …?“ say of a connection that pauses. */
168
+ export const CONNECTION_PAUSED = "The connection is paused: nobody reaches it until an admin resumes it.";
169
+ /** The decision for a connection of the space that pauses, whoever calls; null where it does not. */
170
+ function pausedDecision(row) {
171
+ return row.pausedAt
172
+ ? { outcome: "deny", rule: "paused", refused: "paused", reason: CONNECTION_PAUSED }
173
+ : null;
174
+ }
157
175
  /**
158
176
  * Whether a call in a run comes from an AI step: a model's tool call, made as the person running
159
177
  * (a guest, a person, an admin), or a plugin tool it called. A fixed step calls as `step`; a
@@ -209,13 +227,12 @@ const ASK_INPUT = 4000;
209
227
  */
210
228
  export async function brokerCall(call) {
211
229
  const started = Date.now();
212
- const { who, where, connection, ctx = {} } = call;
230
+ const { who, where, ctx = {} } = call;
231
+ let { connection } = call;
213
232
  const connectionId = connectionIdOf(connection);
214
- // A connection of the space used for the first time is heard of (log.ts, `first_use`).
215
- const firstUse = connection.kind === "space_connection" && !connection.row.lastUsedAt;
216
- const line = (kind, outcome, rule, reason, result, size = null, context = {}) => writeLog({
233
+ /** What every line of this call says, whatever came of it. */
234
+ const lineOf = (kind, outcome, rule, reason) => ({
217
235
  at: new Date(started),
218
- durationMs: Date.now() - started,
219
236
  whoKind: who.kind,
220
237
  whoId: who.id ?? null,
221
238
  runId: where.runId ?? null,
@@ -231,9 +248,37 @@ export async function brokerCall(call) {
231
248
  outcome,
232
249
  rule,
233
250
  reason,
251
+ });
252
+ const line = (kind, outcome, rule, reason, result, size = null, context = {}) => writeLog({
253
+ ...lineOf(kind, outcome, rule, reason),
254
+ durationMs: Date.now() - started,
234
255
  result,
235
256
  resultSize: size,
236
257
  }, context);
258
+ // Before anything else: a run that is over calls nothing more, whatever the connection.
259
+ const over = await runOver(where);
260
+ if (over) {
261
+ await line(kindBefore(call), "deny", over.rule, over.reason, "run_ended");
262
+ return { ok: false, refused: "run_ended", reason: over.reason };
263
+ }
264
+ // A connection of the space: whether it pauses, and its limits, are read now, not taken from the
265
+ // row a step or a chat turn read when it started; one that was removed since is gone for them too.
266
+ if (connection.kind === "space_connection") {
267
+ const now = await stateOf(connection.row.id);
268
+ if (!now) {
269
+ const reason = "The connection was removed.";
270
+ await line(kindBefore(call), "deny", null, reason, "not_connected");
271
+ return { ok: false, refused: "not_connected", reason };
272
+ }
273
+ connection = { ...connection, row: { ...connection.row, ...now } };
274
+ const paused = pausedDecision(connection.row);
275
+ if (paused) {
276
+ await line(kindBefore(call), "deny", paused.rule, paused.reason, "paused");
277
+ return { ok: false, refused: "paused", reason: paused.reason };
278
+ }
279
+ }
280
+ // A connection of the space used for the first time is heard of (log.ts, `first_use`).
281
+ const firstUse = connection.kind === "space_connection" && !connection.row.lastUsedAt;
237
282
  // The account says what kind the action is; its secret stays inside the broker. A sign-in
238
283
  // that ran out and cannot be renewed, or that the service refuses, is heard of.
239
284
  let account;
@@ -281,9 +326,22 @@ export async function brokerCall(call) {
281
326
  await line(kind, "deny", rule, decision.reason, refused);
282
327
  return { ok: false, refused, reason: decision.reason };
283
328
  }
284
- // A guest's send goes only to the run's own address or the space's domains, a few per run and day.
329
+ // A guest's send goes only to the run's own address or the space's domains, a post only to the
330
+ // channel its fixed step is fixed to, a few per run and day.
285
331
  if (kind === "send" && where.guest) {
286
- const sent = await guestSendCheck(where, recipientsOf(call.input));
332
+ const sent = await guestSendCheck(where, {
333
+ recipients: recipientsOf(call.input, found?.recipientField),
334
+ ...(postsToChannel(account.connector.id, call.action.id)
335
+ ? {
336
+ post: {
337
+ who,
338
+ connectionId,
339
+ actionId: call.action.id,
340
+ channel: channelOf(call.input),
341
+ },
342
+ }
343
+ : {}),
344
+ });
287
345
  if (!sent.ok) {
288
346
  await line(kind, "deny", "guest_send", sent.reason, sent.refused);
289
347
  return { ok: false, refused: sent.refused, reason: sent.reason };
@@ -336,11 +394,38 @@ export async function brokerCall(call) {
336
394
  }
337
395
  }
338
396
  }
397
+ // The call would run. Through a connection of the space with a limit it takes its place first:
398
+ // its line is written now, if the limits leave room, and its end is filled in after.
399
+ let open = null;
400
+ if (connection.kind === "space_connection") {
401
+ const { callsPerRun: perRun, callsPerDay: perDay } = connection.row;
402
+ if (perRun !== null || perDay !== null) {
403
+ const place = await takePlace(lineOf(kind, decision.outcome, rule, decision.reason), {
404
+ perRun,
405
+ perDay,
406
+ });
407
+ if ("full" in place) {
408
+ const refused = place.full === "run" ? CALLS_PER_RUN : CALLS_PER_DAY;
409
+ const reason = place.full === "run"
410
+ ? `The connection takes ${perRun} calls from one run, and this run has made them.`
411
+ : `The connection takes ${perDay} calls a day, and today's are made.`;
412
+ await line(kind, "deny", `limits.${place.full}`, reason, refused, null, {
413
+ limitHeard: await limitHeard(connectionId, place.full, where.runId ?? null),
414
+ });
415
+ return { ok: false, refused, reason };
416
+ }
417
+ open = place;
418
+ }
419
+ }
420
+ /** The call's end: on the line that took its place, else as a line of its own. */
421
+ const end = (result, size = null, context = {}) => open
422
+ ? closeLine(open, { durationMs: Date.now() - started, result, resultSize: size }, context)
423
+ : line(kind, decision.outcome, rule, decision.reason, result, size, context);
339
424
  try {
340
425
  const result = call.handler
341
426
  ? await call.handler(account)
342
427
  : (await found.handler(call.input ?? {}, account.ctx));
343
- await line(kind, decision.outcome, rule, decision.reason, "ok", sizeOf(result), { firstUse });
428
+ await end("ok", sizeOf(result), { firstUse });
344
429
  if (connection.kind === "space_connection") {
345
430
  await markUsed(connection.row);
346
431
  }
@@ -353,10 +438,25 @@ export async function brokerCall(call) {
353
438
  : err instanceof Error
354
439
  ? err.constructor.name || "Error"
355
440
  : "Error";
356
- await line(kind, decision.outcome, rule, decision.reason, result);
441
+ await end(result);
357
442
  throw err;
358
443
  }
359
444
  }
445
+ /**
446
+ * An action's kind before the account is read: what the caller says, else what the connector of
447
+ * a connection of the space says; a wizard's own connection names its connector only once its
448
+ * account is read, so an action of unknown kind counts as a write there.
449
+ */
450
+ function kindBefore(call) {
451
+ if (call.action.kind) {
452
+ return call.action.kind;
453
+ }
454
+ const { connection } = call;
455
+ const found = connection.kind === "space_connection"
456
+ ? connection.connector.actions.find((a) => a.id === call.action.id)
457
+ : undefined;
458
+ return found ? kindOf(found.group) : "write";
459
+ }
360
460
  /** Whether the service answered a call with "who are you?": its 401, or an OAuth grant gone. */
361
461
  export function signInRefused(err) {
362
462
  const text = err instanceof Error ? err.message : String(err);
@@ -364,12 +464,16 @@ export function signInRefused(err) {
364
464
  }
365
465
  /**
366
466
  * A secret of a run's store that is no connection (the browser's kept sign-ins), read through
367
- * the broker as a `read`: the handler alone sees it, and the call is logged like any other.
467
+ * the broker as a `read`: the handler alone sees it, and the call is logged like any other. A run
468
+ * that is over reads none.
368
469
  */
369
470
  export async function brokerSecret(call) {
370
471
  const started = Date.now();
371
- const kept = await keptSecret(call.scope, call.slot);
372
- const line = (result) => writeLog({
472
+ const line = (result, decided = {
473
+ outcome: "allow",
474
+ rule: "read",
475
+ reason: "Reading is allowed.",
476
+ }) => writeLog({
373
477
  at: new Date(started),
374
478
  durationMs: Date.now() - started,
375
479
  whoKind: call.who.kind,
@@ -384,16 +488,21 @@ export async function brokerSecret(call) {
384
488
  actionId: "read_secret",
385
489
  kind: "read",
386
490
  inputSummary: summarizeInput({}),
387
- outcome: "allow",
388
- rule: "read",
389
- reason: "Reading is allowed.",
491
+ ...decided,
390
492
  result,
391
493
  resultSize: null,
392
494
  });
495
+ // A run that is over reads no secret: the handler does not run.
496
+ const over = await runOver(call.where);
497
+ if (over) {
498
+ await line("run_ended", { outcome: "deny", ...over });
499
+ return { ok: false, refused: "run_ended", reason: over.reason };
500
+ }
501
+ const kept = await keptSecret(call.scope, call.slot);
393
502
  try {
394
503
  const result = await call.handler(kept?.data ?? null);
395
504
  await line(kept ? "ok" : "not_connected");
396
- return result;
505
+ return { ok: true, result };
397
506
  }
398
507
  catch (err) {
399
508
  await line(err instanceof Error ? err.constructor.name || "Error" : "Error");
@@ -1,9 +1,14 @@
1
1
  import { note } from "@engenty/wizards-shared/run";
2
2
  import { eq } from "drizzle-orm";
3
3
  import { db, schema } from "../db/client.js";
4
+ /** A value of a call's input as the definition fixes it: a text with no template; else null. */
5
+ function literal(value) {
6
+ return typeof value === "string" && !value.includes("{{") && value.trim() ? value.trim() : null;
7
+ }
4
8
  /** The grant a definition gives its runs. Pure: the same definition, the same grant. */
5
9
  export function grantOf(definition) {
6
10
  const connections = {};
11
+ const channels = {};
7
12
  const tools = new Set();
8
13
  const hosts = new Set();
9
14
  const defs = new Map((definition.connections ?? []).map((c) => [c.id, c]));
@@ -18,6 +23,14 @@ export function grantOf(definition) {
18
23
  const have = new Set(connections[step.call.connection] ?? []);
19
24
  have.add(step.call.action);
20
25
  connections[step.call.connection] = [...have].sort();
26
+ const channel = literal(step.call.input.channel);
27
+ if (channel) {
28
+ channels[step.id] = {
29
+ connection: defs.get(step.call.connection)?.spaceConnection ?? step.call.connection,
30
+ action: step.call.action,
31
+ channel,
32
+ };
33
+ }
21
34
  }
22
35
  for (const host of step.hosts ?? []) {
23
36
  hosts.add(host.trim().toLowerCase());
@@ -34,7 +47,18 @@ export function grantOf(definition) {
34
47
  connections[id] = [...have].sort();
35
48
  }
36
49
  }
37
- return { connections, hosts: [...hosts].sort(), tools: [...tools].sort() };
50
+ return { connections, hosts: [...hosts].sort(), tools: [...tools].sort(), channels };
51
+ }
52
+ /**
53
+ * Whether the grant fixes this step's post to this channel: the step's own connection and action,
54
+ * and the channel its definition names as a literal.
55
+ */
56
+ export function channelInGrant(grant, stepId, post) {
57
+ const fixed = grant.channels?.[stepId];
58
+ return (!!fixed &&
59
+ fixed.connection === post.connectionId &&
60
+ fixed.action === post.actionId &&
61
+ fixed.channel === post.channel);
38
62
  }
39
63
  /**
40
64
  * Whether the grant lets `http_request` reach this host: named as it is, or below a `*.` entry
@@ -1,11 +1,12 @@
1
1
  import { hostInGrant, runGrant } from "./grant.js";
2
2
  import { summarizeInput, writeLog } from "./log.js";
3
+ import { runOver } from "./over.js";
3
4
  import { brokerOn } from "./rwx.js";
4
5
  export async function brokerHost(call) {
5
6
  const started = Date.now();
6
7
  const { who, where, url } = call;
7
8
  const host = url.hostname.toLowerCase();
8
- const line = (outcome, reason, result) => writeLog({
9
+ const line = (outcome, reason, result, rule = "grant") => writeLog({
9
10
  at: new Date(started),
10
11
  durationMs: Date.now() - started,
11
12
  whoKind: who.kind,
@@ -22,11 +23,16 @@ export async function brokerHost(call) {
22
23
  // The path without its query: a query may carry a key.
23
24
  inputSummary: summarizeInput({ method: call.method, host, path: url.pathname }),
24
25
  outcome,
25
- rule: "grant",
26
+ rule,
26
27
  reason,
27
28
  result,
28
29
  resultSize: null,
29
30
  });
31
+ const over = await runOver(where);
32
+ if (over) {
33
+ await line("deny", over.reason, "run_ended", over.rule);
34
+ return { ok: false, refused: "run_ended", reason: over.reason };
35
+ }
30
36
  const bound = where.guest || (await brokerOn());
31
37
  let reason = "Members reach every public host.";
32
38
  if (bound) {
@@ -3,11 +3,12 @@
3
3
  * targets, plugins and the run's browser import from here; `account.ts` stays the broker's own.
4
4
  */
5
5
  export { grantedScopes } from "./account.js";
6
- export { brokerCall, brokerConnected, brokerSecret, decide, grantBinds, inAudience, kindOf, signInRefused, whereOf, whereOfRun, whoOf, } from "./call.js";
6
+ export { brokerCall, brokerConnected, brokerSecret, CONNECTION_PAUSED, decide, grantBinds, inAudience, kindOf, signInRefused, whereOf, whereOfRun, whoOf, } from "./call.js";
7
7
  export { currentCaller, withCaller } from "./caller.js";
8
- export { grantNote, grantOf, hostInGrant, inGrant, runGrant, } from "./grant.js";
8
+ export { channelInGrant, grantNote, grantOf, hostInGrant, inGrant, runGrant, } from "./grant.js";
9
9
  export { brokerHost } from "./host.js";
10
10
  export { keptStepIds } from "./kept.js";
11
- export { CRITICAL_KINDS, criticalOf, criticalSince, cursorOf, LOG_PAGE, lastUseOf, logLine, logOfSpace, logRuleChange, onCritical, RULE_CHANGE_ACTION, readCursor, SIGN_IN_FAILED, summarizeInput, } from "./log.js";
11
+ export { CALLS_PER_DAY, CALLS_PER_RUN, CRITICAL_KINDS, callsToday, criticalOf, criticalSince, cursorOf, LOG_PAGE, lastUseOf, logLine, logOfSpace, logRuleChange, onCritical, RULE_CHANGE_ACTION, readCursor, SIGN_IN_FAILED, summarizeInput, } from "./log.js";
12
+ export { runOver } from "./over.js";
12
13
  export { outsideReaches, reachesOutside } from "./registry.js";
13
- export { brokerSend, GUEST_MAILS_PER_RUN, guestSendCheck, recipientsOf, runAddresses, } from "./send.js";
14
+ export { brokerSend, channelOf, FIXED_CHANNEL_ONLY, GUEST_MAILS_PER_RUN, guestPostCheck, guestSendCheck, postsToChannel, recipientsOf, runAddresses, } from "./send.js";