@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
@@ -1,18 +1,58 @@
1
- import { and, eq, gte, notLike } from "drizzle-orm";
1
+ import { and, eq, gte, inArray, isNull, notLike, or } from "drizzle-orm";
2
2
  import { db, schema } from "../db/client.js";
3
- import { summarizeInput, writeLog } from "./log.js";
3
+ import { channelInGrant, grantOf } from "./grant.js";
4
+ import { startOfToday, summarizeInput, writeLog } from "./log.js";
5
+ import { runOver } from "./over.js";
4
6
  /*
5
7
  * Sends from a guest's run (docs/plans/capabilities.md, A6; rule 7 of the connections plan): a
6
8
  * mail leaves only for an address the run itself collected — what the person typed into a page
7
- * field of kind `email` — or for an address in one of the space's own mail domains. A run sends a
8
- * few, a wizard's guests a number per day the wizard sets. Everything beyond is refused and
9
- * logged as critical. Members' runs are not bound: they send as they always did.
9
+ * field of kind `email` — or for an address in one of the space's own mail domains. A post to a
10
+ * channel (Slack) leaves only from a fixed step, for the channel the published definition fixes
11
+ * (the run's grant). A run sends a few, a wizard's guests a number per day the wizard sets, mails
12
+ * and posts together. Everything beyond is refused and logged as critical. Members' runs are not
13
+ * bound: they send as they always did.
10
14
  */
11
- /** Mails one guest's run sends, from any address. */
15
+ /** Mails and posts one guest's run sends, from any address. */
12
16
  export const GUEST_MAILS_PER_RUN = 3;
17
+ /**
18
+ * The sends that go to a channel and name no address, by connector: the actions whose input
19
+ * names the channel as `channel`. Slack's post is the one.
20
+ */
21
+ const CHANNEL_POSTS = { slack: ["post_message"] };
22
+ /** Whether this action of this connector posts to a channel. */
23
+ export function postsToChannel(connectorId, actionId) {
24
+ return CHANNEL_POSTS[connectorId]?.includes(actionId) ?? false;
25
+ }
26
+ /** The channel a post's input names; null where it names none. */
27
+ export function channelOf(input) {
28
+ const channel = input && typeof input === "object" ? input.channel : null;
29
+ return typeof channel === "string" && channel.trim() ? channel.trim() : null;
30
+ }
31
+ /** A guest's post, as the log and „Was darf …?“ say it. */
32
+ export const FIXED_CHANNEL_ONLY = "A guest's run posts only from a fixed step, to the channel its published step fixes.";
33
+ /**
34
+ * Whether a guest's run may post to this channel, with no side effect: only its fixed step
35
+ * posts, and only to the channel the grant fixes for that step. The grant comes from the
36
+ * published definition, so nothing the guest typed decides it.
37
+ */
38
+ export function guestPostCheck(post) {
39
+ const { where, connectionId, actionId, channel } = post;
40
+ const fixed = post.who.kind === "step" &&
41
+ !!where.stepId &&
42
+ !!channel &&
43
+ channelInGrant(post.grant, where.stepId, { connectionId, actionId, channel });
44
+ return fixed
45
+ ? { ok: true }
46
+ : { ok: false, refused: "channel_not_fixed", reason: FIXED_CHANNEL_ONLY };
47
+ }
13
48
  const ADDRESS = /^[^\s@<>,;]+@[^\s@<>,;]+\.[^\s@<>,;]+$/;
14
- /** The addresses a send's input names: `to`, `cc`, `bcc`, `recipients`, as strings, lists or objects. */
15
- export function recipientsOf(input) {
49
+ /**
50
+ * The addresses a send's input names: the field the connector's action names as the recipient's
51
+ * (an imported connector: what an admin said, `body.to` for one inside an HTTP body), and `to`,
52
+ * `cc`, `bcc`, `recipients`, as strings, lists or objects. The named field comes on top of the
53
+ * guess, never in place of it: an address in `cc` is still one the send goes to.
54
+ */
55
+ export function recipientsOf(input, field) {
16
56
  if (!input || typeof input !== "object") {
17
57
  return [];
18
58
  }
@@ -37,6 +77,13 @@ export function recipientsOf(input) {
37
77
  take(o.email ?? o.address ?? o.emailAddress);
38
78
  }
39
79
  };
80
+ if (field) {
81
+ let value = input;
82
+ for (const part of field.split(".")) {
83
+ value = value && typeof value === "object" ? value[part] : null;
84
+ }
85
+ take(value);
86
+ }
40
87
  for (const key of ["to", "cc", "bcc", "recipients", "recipient"]) {
41
88
  take(input[key]);
42
89
  }
@@ -72,83 +119,96 @@ export function inMailDomains(address, domains) {
72
119
  return own && (domain === own || domain.endsWith(`.${own}`));
73
120
  }));
74
121
  }
75
- /** Today's first moment, as the per-day count sees it. */
76
- const startOfToday = () => {
77
- const now = new Date();
78
- return new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
79
- };
80
- /** How many mails guests sent through this run, and through this wizard today. */
81
- async function guestMailsSent(where) {
82
- // Mails only: a request to a host is a send too, counted nowhere here.
83
- const sent = and(eq(schema.brokerLog.whoKind, "guest"), eq(schema.brokerLog.kind, "send"), eq(schema.brokerLog.result, "ok"), notLike(schema.brokerLog.connectionId, "host:%"));
122
+ /**
123
+ * How often guests sent through this run, and through this wizard today: mails and posts, a
124
+ * guest's own and those of the fixed steps of guests' runs.
125
+ */
126
+ async function guestSends(where) {
127
+ // A request to a host is a send too, counted nowhere here.
128
+ const sent = and(eq(schema.brokerLog.kind, "send"), eq(schema.brokerLog.result, "ok"), notLike(schema.brokerLog.connectionId, "host:%"));
129
+ // A fixed step is logged as `step`: it counts where its run is a guest's.
130
+ const guestRuns = (wizardId) => db
131
+ .select({ id: schema.run.id })
132
+ .from(schema.run)
133
+ .where(and(eq(schema.run.wizardId, wizardId), eq(schema.run.mode, "live"), isNull(schema.run.userId)));
84
134
  const [inRun, today] = await Promise.all([
85
135
  where.runId
86
- ? db.$count(schema.brokerLog, and(sent, eq(schema.brokerLog.runId, where.runId)))
136
+ ? db.$count(schema.brokerLog, and(sent, eq(schema.brokerLog.runId, where.runId), inArray(schema.brokerLog.whoKind, ["guest", "step"])))
87
137
  : 0,
88
138
  where.wizardId
89
- ? db.$count(schema.brokerLog, and(sent, eq(schema.brokerLog.wizardId, where.wizardId), gte(schema.brokerLog.at, startOfToday())))
139
+ ? db.$count(schema.brokerLog, and(sent, eq(schema.brokerLog.wizardId, where.wizardId), gte(schema.brokerLog.at, startOfToday()), or(eq(schema.brokerLog.whoKind, "guest"), and(eq(schema.brokerLog.whoKind, "step"), inArray(schema.brokerLog.runId, guestRuns(where.wizardId))))))
90
140
  : 0,
91
141
  ]);
92
142
  return { run: inRun, day: today };
93
143
  }
94
144
  /**
95
- * Whether a guest's run may send to these addresses now: each one the run's own or in the space's
96
- * domains, and the run and the wizard under their caps. The run, the wizard and the space are
97
- * read here; a run or wizard not known refuses.
145
+ * Whether a guest's run may send this now: each address the run's own or in the space's domains,
146
+ * a post only to the channel its fixed step is fixed to, and the run and the wizard under their
147
+ * caps. The run, the wizard and the space are read here; a run or wizard not known refuses.
98
148
  */
99
- export async function guestSendCheck(where, recipients) {
149
+ export async function guestSendCheck(where, send) {
150
+ const { recipients, post } = send;
100
151
  const refuse = (refused, reason) => ({
101
152
  ok: false,
102
153
  refused,
103
154
  reason,
104
155
  });
105
- if (!recipients.length) {
156
+ if (!(recipients.length || post)) {
106
157
  return refuse("not_allowed_recipient", "The send names no recipient.");
107
158
  }
108
159
  const run = where.runId
109
160
  ? await db.query.run.findFirst({
110
161
  where: eq(schema.run.id, where.runId),
111
- columns: { definition: true, state: true, wizardId: true },
162
+ columns: { definition: true, state: true, wizardId: true, grant: true },
112
163
  })
113
164
  : null;
114
165
  if (!run) {
115
166
  return refuse("not_allowed_recipient", "The run is not known here.");
116
167
  }
168
+ if (post) {
169
+ // A run from before grants were kept: the grant its definition gives (grant.ts, `runGrant`).
170
+ const fixed = guestPostCheck({ ...post, where, grant: run.grant ?? grantOf(run.definition) });
171
+ if (!fixed.ok) {
172
+ return fixed;
173
+ }
174
+ }
117
175
  const wizard = await db.query.wizard.findFirst({
118
176
  where: eq(schema.wizard.id, run.wizardId),
119
177
  columns: { spaceId: true, dailyMailLimit: true },
120
178
  });
121
- const space = wizard
122
- ? await db.query.space.findFirst({
123
- where: eq(schema.space.id, wizard.spaceId),
124
- columns: { mailDomains: true },
125
- })
126
- : null;
127
- const own = runAddresses(run);
128
- const domains = space?.mailDomains ?? [];
129
- const foreign = recipients.find((address) => !(own.includes(address.toLowerCase()) || inMailDomains(address, domains)));
130
- if (foreign) {
131
- return refuse("not_allowed_recipient", `A guest's run sends only to its own address or the space's domains, not to ${foreign}.`);
179
+ if (recipients.length) {
180
+ const space = wizard
181
+ ? await db.query.space.findFirst({
182
+ where: eq(schema.space.id, wizard.spaceId),
183
+ columns: { mailDomains: true },
184
+ })
185
+ : null;
186
+ const own = runAddresses(run);
187
+ const domains = space?.mailDomains ?? [];
188
+ const foreign = recipients.find((address) => !(own.includes(address.toLowerCase()) || inMailDomains(address, domains)));
189
+ if (foreign) {
190
+ return refuse("not_allowed_recipient", `A guest's run sends only to its own address or the space's domains, not to ${foreign}.`);
191
+ }
132
192
  }
133
- const sent = await guestMailsSent({ ...where, wizardId: where.wizardId ?? run.wizardId });
193
+ const sent = await guestSends({ ...where, wizardId: where.wizardId ?? run.wizardId });
134
194
  if (sent.run >= GUEST_MAILS_PER_RUN) {
135
- return refuse("run_limit", `This run has sent its ${GUEST_MAILS_PER_RUN} mails.`);
195
+ return refuse("run_limit", `This run has sent its ${GUEST_MAILS_PER_RUN} mails and posts.`);
136
196
  }
137
197
  const perDay = wizard?.dailyMailLimit ?? 50;
138
198
  if (sent.day >= perDay) {
139
- return refuse("day_limit", `This wizard's guests have sent today's ${perDay} mails.`);
199
+ return refuse("day_limit", `This wizard's guests have sent today's ${perDay} mails and posts.`);
140
200
  }
141
201
  return { ok: true };
142
202
  }
143
203
  /**
144
204
  * A mail from the server's own address, which no account holds: the one send that is not a
145
- * connector's action. A guest's run is bound by the rules above; the call is logged like any
146
- * other, under the connection `mail:server`.
205
+ * connector's action. A run that is over sends nothing (over.ts), asked first; a guest's run is
206
+ * bound by the rules above; the call is logged like any other, under the connection `mail:server`.
147
207
  */
148
208
  export async function brokerSend(call) {
149
209
  const started = Date.now();
150
210
  const { who, where } = call;
151
- const line = (outcome, reason, result) => writeLog({
211
+ const line = (outcome, reason, result, rule = "guest_send") => writeLog({
152
212
  at: new Date(started),
153
213
  durationMs: Date.now() - started,
154
214
  whoKind: who.kind,
@@ -163,14 +223,19 @@ export async function brokerSend(call) {
163
223
  kind: "send",
164
224
  inputSummary: summarizeInput(call.input),
165
225
  outcome,
166
- rule: "guest_send",
226
+ rule,
167
227
  reason,
168
228
  result,
169
229
  resultSize: null,
170
230
  });
231
+ const over = await runOver(where);
232
+ if (over) {
233
+ await line("deny", over.reason, "run_ended", over.rule);
234
+ return { ok: false, refused: "run_ended", reason: over.reason };
235
+ }
171
236
  let reason = "The server's address sends for members.";
172
237
  if (where.guest) {
173
- const check = await guestSendCheck(where, call.recipients);
238
+ const check = await guestSendCheck(where, { recipients: call.recipients });
174
239
  if (!check.ok) {
175
240
  await line("deny", check.reason, check.refused);
176
241
  return check;
@@ -0,0 +1,119 @@
1
+ import { createGuardedFetch } from "../engenty/connections-external/net/guarded-fetch.js";
2
+ import { ServiceError } from "../services/errors.js";
3
+ import { MAX_ARCHIVE_MB } from "./read.js";
4
+ /**
5
+ * A public repository on GitHub as a zip (docs/plans/capabilities-next.md, piece 4). The address
6
+ * is one a person copies from the browser: the repository, or a folder of it on a branch or tag.
7
+ * The archive comes from GitHub's own download host through the guarded fetch, which follows no
8
+ * redirect into a private network, and is refused before it is read whole where it is too large.
9
+ */
10
+ const refuse = (server, params) => new ServiceError("invalid", { server, params });
11
+ const PART = /^[A-Za-z0-9_.-]{1,100}$/;
12
+ /**
13
+ * A repository's address on GitHub: `https://github.com/owner/name`, with `.git` or without, and
14
+ * a folder as `…/tree/<branch>/<folder>`. A branch whose name has a slash cannot be told from a
15
+ * folder: its first part is read as the branch. Null for any other address.
16
+ */
17
+ export function parseRepoAddress(address) {
18
+ const typed = address.trim();
19
+ let url;
20
+ try {
21
+ url = new URL(/^[a-z][a-z0-9+.-]*:\/\//i.test(typed) ? typed : `https://${typed}`);
22
+ }
23
+ catch {
24
+ return null;
25
+ }
26
+ if (!/^(www\.)?github\.com$/i.test(url.hostname) || !/^https?:$/.test(url.protocol)) {
27
+ return null;
28
+ }
29
+ const [owner, rawName, kind, ref, ...rest] = url.pathname.split("/").filter(Boolean);
30
+ const name = rawName?.replace(/\.git$/i, "");
31
+ if (!(owner && name && PART.test(owner) && PART.test(name))) {
32
+ return null;
33
+ }
34
+ const folder = kind === "tree" || kind === "blob";
35
+ let path;
36
+ try {
37
+ path = folder ? rest.map((part) => decodeURIComponent(part)) : [];
38
+ }
39
+ catch {
40
+ return null;
41
+ }
42
+ if (path.some((part) => part === ".." || part === "." || part.includes("\\"))) {
43
+ return null;
44
+ }
45
+ return {
46
+ repo: `${owner}/${name}`,
47
+ ref: folder && ref ? decodeURIComponent(ref) : null,
48
+ path: path.join("/"),
49
+ };
50
+ }
51
+ /** The address a person opens to look at the origin. */
52
+ export function repoUrl(address) {
53
+ const tail = address.ref || address.path
54
+ ? `/tree/${address.ref ?? "HEAD"}${address.path ? `/${address.path}` : ""}`
55
+ : "";
56
+ return `https://github.com/${address.repo}${tail}`;
57
+ }
58
+ /** The repository as a zip, as it is on its branch or tag now. */
59
+ export async function fetchRepoZip(address) {
60
+ const guarded = createGuardedFetch();
61
+ const url = `https://codeload.github.com/${address.repo}/zip/${encodeURIComponent(address.ref ?? "HEAD")}`;
62
+ let res;
63
+ try {
64
+ res = await guarded(url, {
65
+ headers: { accept: "application/zip", "user-agent": "engenty-wizards" },
66
+ signal: AbortSignal.timeout(45_000),
67
+ });
68
+ }
69
+ catch {
70
+ throw new ServiceError("refused", { server: "bundleRepoDown" });
71
+ }
72
+ if (res.status === 404) {
73
+ throw refuse("bundleRepoNotFound", { repo: address.repo });
74
+ }
75
+ if (!res.ok) {
76
+ throw new ServiceError("refused", { server: "bundleRepoDown" });
77
+ }
78
+ const max = MAX_ARCHIVE_MB * 1_000_000;
79
+ const tooLarge = () => refuse("bundleTooLarge", { mb: MAX_ARCHIVE_MB });
80
+ if (Number(res.headers.get("content-length")) > max) {
81
+ throw tooLarge();
82
+ }
83
+ const reader = res.body?.getReader();
84
+ if (!reader) {
85
+ const all = new Uint8Array(await res.arrayBuffer());
86
+ if (all.length > max) {
87
+ throw tooLarge();
88
+ }
89
+ return all;
90
+ }
91
+ const chunks = [];
92
+ let total = 0;
93
+ try {
94
+ while (true) {
95
+ const { done, value } = await reader.read();
96
+ if (done) {
97
+ break;
98
+ }
99
+ total += value.byteLength;
100
+ if (total > max) {
101
+ throw tooLarge();
102
+ }
103
+ chunks.push(value);
104
+ }
105
+ }
106
+ catch (err) {
107
+ await reader.cancel().catch(() => undefined);
108
+ throw err instanceof ServiceError
109
+ ? err
110
+ : new ServiceError("refused", { server: "bundleRepoDown" });
111
+ }
112
+ const all = new Uint8Array(total);
113
+ let at = 0;
114
+ for (const chunk of chunks) {
115
+ all.set(chunk, at);
116
+ at += chunk.byteLength;
117
+ }
118
+ return all;
119
+ }