copilotz 1.0.0-rc.21 → 1.0.0-rc.23

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 (211) hide show
  1. package/CHANGELOG.md +320 -1
  2. package/README.md +217 -87
  3. package/hosts/orgs/mod.d.ts +33 -13
  4. package/hosts/orgs/mod.js +183 -84
  5. package/hosts/web/address.d.ts +11 -0
  6. package/hosts/web/address.js +46 -0
  7. package/hosts/web/assets.js +11 -5
  8. package/hosts/web/entrances.d.ts +250 -0
  9. package/hosts/web/entrances.js +362 -0
  10. package/hosts/web/host.d.ts +15 -3
  11. package/hosts/web/host.js +31 -18
  12. package/hosts/web/mod.d.ts +4 -2
  13. package/hosts/web/mod.js +6 -0
  14. package/hosts/web/node/bundle.d.ts +5 -0
  15. package/hosts/web/node/bundle.js +1 -1
  16. package/hosts/web/node/mod.d.ts +6 -4
  17. package/hosts/web/node/mod.js +6 -4
  18. package/hosts/web/node/serve.d.ts +20 -23
  19. package/hosts/web/node/serve.js +10 -21
  20. package/hosts/web/routes.d.ts +23 -17
  21. package/hosts/web/routes.js +56 -32
  22. package/hosts/web/server.d.ts +89 -44
  23. package/hosts/web/server.js +208 -108
  24. package/hosts/web/signin.d.ts +7 -10
  25. package/hosts/web/signin.js +6 -3
  26. package/hosts/web/surface.d.ts +20 -0
  27. package/hosts/web/surface.js +42 -0
  28. package/hosts/web/theme/css.d.ts +2 -1
  29. package/hosts/web/theme/css.js +9 -2
  30. package/hosts/web/tickets.d.ts +14 -1
  31. package/hosts/web/tickets.js +30 -7
  32. package/hosts/web/widget.d.ts +31 -0
  33. package/hosts/web/widget.js +429 -0
  34. package/hosts/web/widgets.d.ts +43 -0
  35. package/hosts/web/widgets.js +117 -0
  36. package/package.json +5 -1
  37. package/plugins/access/contacts/actions/forget.js +3 -1
  38. package/plugins/access/contacts/actions/start.js +18 -8
  39. package/plugins/access/contacts/collections/contact.d.ts +2 -2
  40. package/plugins/access/contacts/collections/contact.js +1 -1
  41. package/plugins/access/contacts/contract.d.ts +5 -5
  42. package/plugins/access/contacts/contract.js +5 -5
  43. package/plugins/access/contacts/mod.d.ts +4 -0
  44. package/plugins/access/contacts/mod.js +4 -0
  45. package/plugins/access/contacts/views/contact.js +1 -0
  46. package/plugins/access/dev/actions/sign-in.d.ts +1 -0
  47. package/plugins/access/dev/actions/sign-in.js +38 -0
  48. package/plugins/access/dev/mod.d.ts +17 -0
  49. package/plugins/access/dev/mod.js +31 -0
  50. package/plugins/access/dev/shared/nearby.d.ts +2 -0
  51. package/plugins/access/dev/shared/nearby.js +27 -0
  52. package/plugins/access/directory/actions/invite.js +2 -2
  53. package/plugins/access/directory/actions/org.js +11 -8
  54. package/plugins/access/directory/collections/credit-code.d.ts +1 -1
  55. package/plugins/access/google/actions/sign-in.js +2 -0
  56. package/plugins/access/google/mod.d.ts +4 -2
  57. package/plugins/access/google/mod.js +15 -2
  58. package/plugins/access/identity/actions/session.js +5 -3
  59. package/plugins/access/identity/mod.d.ts +4 -2
  60. package/plugins/access/identity/mod.js +17 -2
  61. package/plugins/access/mod.d.ts +7 -0
  62. package/plugins/access/mod.js +1 -0
  63. package/plugins/access/spaces/shared/declared.d.ts +1 -1
  64. package/plugins/access/spaces/shared/rules.d.ts +16 -5
  65. package/plugins/access/spaces/shared/rules.js +74 -6
  66. package/plugins/agents/chat/actions/send.d.ts +21 -0
  67. package/plugins/agents/chat/actions/send.js +72 -13
  68. package/plugins/agents/chat/collections/file.d.ts +15 -0
  69. package/plugins/agents/chat/collections/file.js +12 -3
  70. package/plugins/agents/chat/collections/live.d.ts +11 -0
  71. package/plugins/agents/chat/collections/live.js +11 -1
  72. package/plugins/agents/chat/collections/message.d.ts +1 -1
  73. package/plugins/agents/chat/collections/wait.d.ts +1 -1
  74. package/plugins/agents/chat/contract.d.ts +3 -2
  75. package/plugins/agents/chat/contract.js +1 -0
  76. package/plugins/agents/chat/mod.d.ts +9 -3
  77. package/plugins/agents/chat/mod.js +28 -7
  78. package/plugins/agents/chat/processors/failed.js +8 -3
  79. package/plugins/agents/chat/processors/turn.js +6 -3
  80. package/plugins/agents/chat/shared/agents.js +11 -2
  81. package/plugins/agents/chat/shared/live.d.ts +4 -2
  82. package/plugins/agents/chat/shared/live.js +2 -1
  83. package/plugins/agents/chat/shared/types.d.ts +8 -0
  84. package/plugins/agents/history/collections/outline.d.ts +2 -2
  85. package/plugins/agents/openapi/shared/files.js +2 -0
  86. package/plugins/agents/routines/collections/routine.d.ts +1 -1
  87. package/plugins/billing/credits/collections/plan.d.ts +2 -2
  88. package/plugins/billing/credits/collections/room.d.ts +1 -1
  89. package/plugins/billing/usage/collections/rollup.d.ts +2 -2
  90. package/plugins/billing/usage/collections/usage.d.ts +2 -2
  91. package/plugins/compute/cloud/actions/pool.js +2 -0
  92. package/plugins/compute/cloud/actions/remote.js +4 -2
  93. package/plugins/compute/cloud/actions/vms.js +2 -0
  94. package/plugins/compute/cloud/collections/pool-vm.d.ts +1 -1
  95. package/plugins/compute/computers/actions/admit.js +2 -0
  96. package/plugins/compute/computers/actions/code.js +2 -0
  97. package/plugins/compute/computers/actions/enrollment.js +2 -0
  98. package/plugins/compute/computers/actions/work.js +3 -0
  99. package/plugins/compute/computers/program/version.d.ts +1 -1
  100. package/plugins/compute/computers/program/version.js +1 -1
  101. package/plugins/compute/computers/resources/downloads.js +1 -1
  102. package/plugins/compute/computers/resources/previews.js +5 -2
  103. package/plugins/interfaces/maintenance/mod.d.ts +3 -1
  104. package/plugins/interfaces/maintenance/mod.js +3 -1
  105. package/plugins/interfaces/mcp/actions/call.d.ts +4 -0
  106. package/plugins/interfaces/mcp/actions/call.js +120 -18
  107. package/plugins/interfaces/mcp/actions/conversation.d.ts +1 -0
  108. package/plugins/interfaces/mcp/actions/conversation.js +49 -0
  109. package/plugins/interfaces/mcp/actions/prune.js +5 -3
  110. package/plugins/interfaces/mcp/collections/opening.d.ts +24 -0
  111. package/plugins/interfaces/mcp/collections/opening.js +28 -0
  112. package/plugins/interfaces/mcp/mod.d.ts +27 -3
  113. package/plugins/interfaces/mcp/mod.js +42 -7
  114. package/plugins/interfaces/mcp/resources/server.d.ts +13 -4
  115. package/plugins/interfaces/mcp/resources/server.js +163 -21
  116. package/plugins/interfaces/mcp/resources/tools.d.ts +88 -12
  117. package/plugins/interfaces/mcp/resources/tools.js +113 -19
  118. package/plugins/interfaces/mcp/resources/widgets.d.ts +161 -0
  119. package/plugins/interfaces/mcp/resources/widgets.js +246 -0
  120. package/plugins/interfaces/mcp/shared/keys.d.ts +25 -19
  121. package/plugins/interfaces/mcp/shared/keys.js +55 -9
  122. package/plugins/interfaces/mcp/shared/settings.d.ts +70 -37
  123. package/plugins/interfaces/mcp/shared/settings.js +64 -39
  124. package/plugins/interfaces/mcp/views/offered.d.ts +1 -1
  125. package/plugins/interfaces/mod.d.ts +2 -1
  126. package/plugins/interfaces/mod.js +2 -1
  127. package/plugins/interfaces/voice/collections/blocked.d.ts +2 -2
  128. package/plugins/interfaces/voice/collections/call.d.ts +3 -3
  129. package/plugins/interfaces/voice/contract.d.ts +1 -1
  130. package/plugins/interfaces/voice/mod.d.ts +3 -3
  131. package/plugins/interfaces/voice/mod.js +3 -3
  132. package/plugins/interfaces/voice/resources/relay.d.ts +5 -3
  133. package/plugins/interfaces/voice/resources/relay.js +17 -6
  134. package/plugins/interfaces/voice/resources/tools.js +2 -0
  135. package/plugins/interfaces/voice/shared/settings.d.ts +5 -0
  136. package/plugins/interfaces/voice/views/call.d.ts +18 -11
  137. package/plugins/interfaces/voice/views/call.js +15 -3
  138. package/plugins/interfaces/voice/views/message.d.ts +1 -1
  139. package/plugins/interfaces/web/actions/arrive.d.ts +1 -0
  140. package/plugins/interfaces/web/actions/arrive.js +120 -0
  141. package/plugins/interfaces/web/actions/prune.d.ts +1 -0
  142. package/plugins/interfaces/web/actions/prune.js +51 -0
  143. package/plugins/interfaces/web/actions/sending.d.ts +1 -0
  144. package/plugins/interfaces/web/actions/sending.js +222 -0
  145. package/plugins/interfaces/web/actions/spent.d.ts +2 -0
  146. package/plugins/interfaces/web/actions/spent.js +59 -0
  147. package/plugins/interfaces/web/collections/rate.d.ts +122 -0
  148. package/plugins/interfaces/web/collections/rate.js +37 -0
  149. package/plugins/interfaces/web/collections/visitor.d.ts +110 -0
  150. package/plugins/interfaces/web/collections/visitor.js +64 -0
  151. package/plugins/interfaces/web/contract.d.ts +4 -0
  152. package/plugins/interfaces/web/contract.js +1 -0
  153. package/plugins/interfaces/web/lines.d.ts +36 -0
  154. package/plugins/interfaces/web/lines.js +78 -0
  155. package/plugins/interfaces/web/processors/forget.d.ts +10 -0
  156. package/plugins/interfaces/web/processors/forget.js +14 -0
  157. package/plugins/interfaces/web/resources/entrance.d.ts +47 -0
  158. package/plugins/interfaces/web/resources/entrance.js +83 -0
  159. package/plugins/interfaces/web/resources/spend.d.ts +2 -0
  160. package/plugins/interfaces/web/resources/spend.js +62 -0
  161. package/plugins/interfaces/web/shared/budget.d.ts +65 -0
  162. package/plugins/interfaces/web/shared/budget.js +134 -0
  163. package/plugins/interfaces/web/shared/lines.d.ts +158 -0
  164. package/plugins/interfaces/web/shared/lines.js +156 -0
  165. package/plugins/interfaces/web/shared/secret.d.ts +33 -0
  166. package/plugins/interfaces/web/shared/secret.js +86 -0
  167. package/plugins/interfaces/whatsapp/collections/phone.d.ts +1 -1
  168. package/plugins/interfaces/whatsapp/mod.d.ts +1 -1
  169. package/plugins/interfaces/whatsapp/mod.js +1 -1
  170. package/plugins/interfaces/whatsapp/views/message.d.ts +1 -1
  171. package/plugins/models/contract.d.ts +1 -1
  172. package/plugins/models/shared/generate.js +13 -4
  173. package/plugins/models/shared/transcribe.js +7 -1
  174. package/plugins/models/shared/use.d.ts +16 -2
  175. package/plugins/models/shared/use.js +20 -0
  176. package/runtime/browsers.d.ts +153 -17
  177. package/runtime/browsers.js +336 -30
  178. package/runtime/cache.js +25 -5
  179. package/runtime/caller.d.ts +117 -0
  180. package/runtime/caller.js +215 -0
  181. package/runtime/client.d.ts +47 -3
  182. package/runtime/client.js +132 -42
  183. package/runtime/define.d.ts +30 -9
  184. package/runtime/executor.js +7 -6
  185. package/runtime/expiring.d.ts +23 -0
  186. package/runtime/expiring.js +64 -0
  187. package/runtime/links.d.ts +27 -4
  188. package/runtime/links.js +49 -11
  189. package/runtime/memory.js +102 -15
  190. package/runtime/mod.d.ts +3 -1
  191. package/runtime/mod.js +3 -1
  192. package/runtime/mongo.d.ts +12 -0
  193. package/runtime/mongo.js +44 -0
  194. package/runtime/observe.js +82 -9
  195. package/runtime/ops.d.ts +9 -1
  196. package/runtime/owner.d.ts +14 -1
  197. package/runtime/owner.js +78 -23
  198. package/runtime/reads.js +2 -2
  199. package/runtime/registry.js +9 -3
  200. package/runtime/runtime.d.ts +7 -0
  201. package/runtime/runtime.js +1 -0
  202. package/runtime/timers.js +2 -0
  203. package/runtime/views.d.ts +1 -1
  204. package/runtime/views.js +2 -2
  205. package/runtime/wire.d.ts +1 -1
  206. package/stores/sql/postgres/maintenance.d.ts +8 -1
  207. package/stores/sql/postgres/maintenance.js +76 -9
  208. package/stores/sql/postgres/mod.d.ts +12 -4
  209. package/stores/sql/postgres/mod.js +21 -8
  210. package/plugins/interfaces/mcp/shared/caller.d.ts +0 -45
  211. package/plugins/interfaces/mcp/shared/caller.js +0 -99
@@ -0,0 +1,110 @@
1
+ /**
2
+ * Visitors who spoke: the web lines' visitors that sent a message, each a
3
+ * contact now. One record each, by its contact: the line it came by, its
4
+ * chat, the tags of the networks it first came from (HMACs: never the
5
+ * address), until when, and whether it's been erased (`ended`: its cookie is
6
+ * no one from then, though it says who it was). Only the system reads or
7
+ * writes them: the web host asks who a cookie's secret is (`actor`), and
8
+ * nobody else, a contact included, sees one.
9
+ *
10
+ * A browser that came in and said nothing yet has no record: its secret
11
+ * stands on its own (see shared/secret.ts), and its contact, chat and record
12
+ * are made with its first message (`contact.sending`). Records past their
13
+ * time go (`contact.prune`), and so does a contact made for a first message
14
+ * that never went in (`spoke: false`, still, minutes later).
15
+ */
16
+ import { type Infer } from "../../../../runtime/mod.js";
17
+ declare const visitorSchema: {
18
+ readonly type: "object";
19
+ readonly properties: {
20
+ /** The web line it came by. */
21
+ readonly line: {
22
+ readonly type: "string";
23
+ };
24
+ /** Its chat. */
25
+ readonly thread: {
26
+ readonly type: "string";
27
+ };
28
+ /** The tags of the networks it first came from: what its messages are counted by. */
29
+ readonly tags: {
30
+ readonly type: "array";
31
+ readonly items: {
32
+ readonly type: "string";
33
+ };
34
+ };
35
+ /** Whether its first message went in: until then, a contact made for it may go. */
36
+ readonly spoke: {
37
+ readonly type: "boolean";
38
+ };
39
+ /** Erased (`contact.forget`): its cookie is no one. */
40
+ readonly ended: {
41
+ readonly type: "boolean";
42
+ };
43
+ readonly expires: {
44
+ readonly type: "string";
45
+ };
46
+ readonly at: {
47
+ readonly type: "string";
48
+ };
49
+ };
50
+ readonly required: readonly ["line", "thread", "tags", "spoke", "expires", "at"];
51
+ };
52
+ export declare const visitor: {
53
+ schema: {
54
+ readonly type: "object";
55
+ readonly properties: {
56
+ /** The web line it came by. */
57
+ readonly line: {
58
+ readonly type: "string";
59
+ };
60
+ /** Its chat. */
61
+ readonly thread: {
62
+ readonly type: "string";
63
+ };
64
+ /** The tags of the networks it first came from: what its messages are counted by. */
65
+ readonly tags: {
66
+ readonly type: "array";
67
+ readonly items: {
68
+ readonly type: "string";
69
+ };
70
+ };
71
+ /** Whether its first message went in: until then, a contact made for it may go. */
72
+ readonly spoke: {
73
+ readonly type: "boolean";
74
+ };
75
+ /** Erased (`contact.forget`): its cookie is no one. */
76
+ readonly ended: {
77
+ readonly type: "boolean";
78
+ };
79
+ readonly expires: {
80
+ readonly type: "string";
81
+ };
82
+ readonly at: {
83
+ readonly type: "string";
84
+ };
85
+ };
86
+ readonly required: readonly ["line", "thread", "tags", "spoke", "expires", "at"];
87
+ };
88
+ events?: boolean;
89
+ records?: boolean;
90
+ durable?: false;
91
+ indexes?: string[][];
92
+ private?: string[];
93
+ queries?: Record<string, import("../../../../runtime/define.js").QueryDef<{
94
+ [x: string]: unknown;
95
+ ended?: boolean | undefined;
96
+ at: string;
97
+ line: string;
98
+ tags: string[];
99
+ thread: string;
100
+ expires: string;
101
+ spoke: boolean;
102
+ } & {
103
+ _id: string;
104
+ }>> | undefined;
105
+ vectors?: string[];
106
+ };
107
+ export type Visitor = Infer<typeof visitorSchema> & {
108
+ _id: string;
109
+ };
110
+ export {};
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Visitors who spoke: the web lines' visitors that sent a message, each a
3
+ * contact now. One record each, by its contact: the line it came by, its
4
+ * chat, the tags of the networks it first came from (HMACs: never the
5
+ * address), until when, and whether it's been erased (`ended`: its cookie is
6
+ * no one from then, though it says who it was). Only the system reads or
7
+ * writes them: the web host asks who a cookie's secret is (`actor`), and
8
+ * nobody else, a contact included, sees one.
9
+ *
10
+ * A browser that came in and said nothing yet has no record: its secret
11
+ * stands on its own (see shared/secret.ts), and its contact, chat and record
12
+ * are made with its first message (`contact.sending`). Records past their
13
+ * time go (`contact.prune`), and so does a contact made for a first message
14
+ * that never went in (`spoke: false`, still, minutes later).
15
+ */
16
+ import { defineCollection } from "../../../../runtime/mod.js";
17
+ import { contactOf, visitOf } from "../shared/secret.js";
18
+ import { keyringOf, lineOf } from "../shared/lines.js";
19
+ const visitorSchema = {
20
+ type: "object",
21
+ properties: {
22
+ /** The web line it came by. */
23
+ line: { type: "string" },
24
+ /** Its chat. */
25
+ thread: { type: "string" },
26
+ /** The tags of the networks it first came from: what its messages are counted by. */
27
+ tags: { type: "array", items: { type: "string" } },
28
+ /** Whether its first message went in: until then, a contact made for it may go. */
29
+ spoke: { type: "boolean" },
30
+ /** Erased (`contact.forget`): its cookie is no one. */
31
+ ended: { type: "boolean" },
32
+ expires: { type: "string" },
33
+ at: { type: "string" },
34
+ },
35
+ required: ["line", "thread", "tags", "spoke", "expires", "at"],
36
+ };
37
+ export const visitor = defineCollection({
38
+ schema: visitorSchema,
39
+ indexes: [["expires"], ["spoke"]],
40
+ queries: {
41
+ /**
42
+ * Who a visitor's secret is: the contact it is, once it's spoken, while
43
+ * the secret lasts, its line is still the app's, and the contact is still
44
+ * there (erased, it's no one at once), with its line (`web`) and its
45
+ * networks' tags (`from`), what its messages and connections count by. A browser that hasn't spoken is no
46
+ * one here: the web host lets it back in as who it was (`contact.arrive`,
47
+ * given its secret). The system's alone: the web host asks, once per
48
+ * connection.
49
+ */
50
+ actor: async (visitors, params, ctx) => {
51
+ const ring = keyringOf(ctx);
52
+ const visit = ring ? await visitOf(ring, params?.secret) : undefined;
53
+ const line = visit ? lineOf(ctx, visit.line) : undefined;
54
+ if (!visit || !line || visit.issued + line.days * 86_400_000 < Date.now())
55
+ return null;
56
+ const contact = await contactOf(ring, visit);
57
+ const found = await visitors.findOne({ _id: contact });
58
+ if (!found || found.ended || found.line !== line.name)
59
+ return null;
60
+ const there = await ctx.db.collection("contact").findOne({ _id: contact });
61
+ return there ? { kind: "contact", id: contact, web: line.name, from: found.tags } : null;
62
+ },
63
+ },
64
+ });
@@ -7,6 +7,9 @@
7
7
  * serves many orgs: members, usage, about), each a View of the front end's
8
8
  * own, and the words for org roles.
9
9
  *
10
+ * The web lines' settings (`resources.web`) have their shapes here too (`WebLine`, `WebSettings`,
11
+ * `WEB_LIMITS`). See lines.ts.
12
+ *
10
13
  * ```ts
11
14
  * resources: {
12
15
  * orgSettings: { usage: { title: "Usage", view: "usage.org", icon: "chart", order: 30 } },
@@ -18,3 +21,4 @@ export type { Panel } from "./shared/panel.js";
18
21
  export type { PaletteSource } from "./shared/palette.js";
19
22
  export type { UserMenuEntry } from "./shared/menu.js";
20
23
  export { ORG_ROLES, orgRoleName, type OrgSection, runs, type Standing } from "./shared/orgs.js";
24
+ export { LIMITS as WEB_LIMITS, type WebLimits, type WebLine, type WebSettings, } from "./shared/lines.js";
@@ -1 +1,2 @@
1
1
  export { ORG_ROLES, orgRoleName, runs } from "./shared/orgs.js";
2
+ export { LIMITS as WEB_LIMITS, } from "./shared/lines.js";
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Web lines (`resources.web.lines.<name>`, declared in the app's settings, with the app's secret,
3
+ * `links`): a front door for browsers that aren't anyone, as WhatsApp's, the phone's and MCP's
4
+ * lines are for theirs. A page that comes in by one (`web({ arrive: { line } })`, `POST /connect`
5
+ * `{ line }`) is a visitor, its browser keeping a secret in an httpOnly cookie (the web host's
6
+ * `visitor` entrance) that is the same visitor while it lasts (`days`, 30). Coming in makes and
7
+ * keeps nothing: the secret is signed. Its first message makes it a contact (the contacts
8
+ * plugin's) in the line's space, with a chat of its own answered by the line's agent
9
+ * (`contact.sending`, chat's `contactSend` hook). New visitors an hour are limited by the caller's
10
+ * address (as the app's front says it: `web.caller`, as MCP's; else as the socket's) and its wider
11
+ * networks (an IPv4 /24, an IPv6 /56 and /48), and a visitor's messages a minute and a day, its
12
+ * networks', and the line's (some of it kept for those who spoke before); past any, it's refused
13
+ * plainly (429), only that caller. A line may have a budget (what its visitors' turns cost a day,
14
+ * as the usage ledger prices them: `contact.spent`, through `modelUse`) and a challenge (an
15
+ * Action of the app's a newcomer passes first). Files only when the line takes them, transcribed
16
+ * only when it says. A visitor reaches what any contact does: their chat, never its agents'
17
+ * private steps, and what's shared with them. Erasing the contact ends its cookie and its
18
+ * connections with it (a processor of `contact.deleted`); what's past is collected every ten
19
+ * minutes (`contact.prune`), a contact never heard from too.
20
+ *
21
+ * ```ts
22
+ * plugins: [models, chat, ...access, webLines]
23
+ * resources: {
24
+ * spaces: { visitors: { name: "Website", agents: ["guide"] } },
25
+ * web: {
26
+ * caller: { header: "cf-connecting-ip", front: env.FRONT_SECRET },
27
+ * lines: { site: { in: "visitors", agent: "guide" } },
28
+ * },
29
+ * links: { origin: "https://example.com", secret: env.LINKS_SECRET },
30
+ * }
31
+ * ```
32
+ */
33
+ import { type Plugin } from "../../../runtime/mod.js";
34
+ export { WEB_LIMITS, type WebLimits, type WebLine, type WebSettings } from "./contract.js";
35
+ export declare const webLines: Plugin;
36
+ export default webLines;
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Web lines (`resources.web.lines.<name>`, declared in the app's settings, with the app's secret,
3
+ * `links`): a front door for browsers that aren't anyone, as WhatsApp's, the phone's and MCP's
4
+ * lines are for theirs. A page that comes in by one (`web({ arrive: { line } })`, `POST /connect`
5
+ * `{ line }`) is a visitor, its browser keeping a secret in an httpOnly cookie (the web host's
6
+ * `visitor` entrance) that is the same visitor while it lasts (`days`, 30). Coming in makes and
7
+ * keeps nothing: the secret is signed. Its first message makes it a contact (the contacts
8
+ * plugin's) in the line's space, with a chat of its own answered by the line's agent
9
+ * (`contact.sending`, chat's `contactSend` hook). New visitors an hour are limited by the caller's
10
+ * address (as the app's front says it: `web.caller`, as MCP's; else as the socket's) and its wider
11
+ * networks (an IPv4 /24, an IPv6 /56 and /48), and a visitor's messages a minute and a day, its
12
+ * networks', and the line's (some of it kept for those who spoke before); past any, it's refused
13
+ * plainly (429), only that caller. A line may have a budget (what its visitors' turns cost a day,
14
+ * as the usage ledger prices them: `contact.spent`, through `modelUse`) and a challenge (an
15
+ * Action of the app's a newcomer passes first). Files only when the line takes them, transcribed
16
+ * only when it says. A visitor reaches what any contact does: their chat, never its agents'
17
+ * private steps, and what's shared with them. Erasing the contact ends its cookie and its
18
+ * connections with it (a processor of `contact.deleted`); what's past is collected every ten
19
+ * minutes (`contact.prune`), a contact never heard from too.
20
+ *
21
+ * ```ts
22
+ * plugins: [models, chat, ...access, webLines]
23
+ * resources: {
24
+ * spaces: { visitors: { name: "Website", agents: ["guide"] } },
25
+ * web: {
26
+ * caller: { header: "cf-connecting-ip", front: env.FRONT_SECRET },
27
+ * lines: { site: { in: "visitors", agent: "guide" } },
28
+ * },
29
+ * links: { origin: "https://example.com", secret: env.LINKS_SECRET },
30
+ * }
31
+ * ```
32
+ */
33
+ import { definePlugin } from "../../../runtime/mod.js";
34
+ import { arriveAction } from "./actions/arrive.js";
35
+ import { pruneAction } from "./actions/prune.js";
36
+ import { sendingAction } from "./actions/sending.js";
37
+ import { budgetAction, spentAction } from "./actions/spent.js";
38
+ import { rate } from "./collections/rate.js";
39
+ import { visitor } from "./collections/visitor.js";
40
+ import { forgetProcessor } from "./processors/forget.js";
41
+ import { visitorEntrance } from "./resources/entrance.js";
42
+ import { visitorsSpend } from "./resources/spend.js";
43
+ import { settingsProblem } from "./shared/lines.js";
44
+ export { WEB_LIMITS } from "./contract.js";
45
+ /** What only the system reads or writes: visitors' records, and the lines' counters. */
46
+ const SYSTEM_ONLY = ["contact_visitor", "contact_rate"];
47
+ export const webLines = definePlugin({
48
+ name: "web-lines",
49
+ requires: ["contacts"],
50
+ collections: { contact_visitor: visitor, contact_rate: rate },
51
+ actions: {
52
+ "contact.arrive": arriveAction,
53
+ "contact.sending": sendingAction,
54
+ "contact.prune": pruneAction,
55
+ "contact.spent": spentAction,
56
+ "contact.budget": budgetAction,
57
+ },
58
+ processors: { "web.forget": forgetProcessor },
59
+ schedules: { "contact.prune": { every: "10m", call: "contact.prune", input: {} } },
60
+ // Visitors' secrets and counters are nobody's to read but the system's, a contact's least.
61
+ guard: (_actor, { collection }) => SYSTEM_ONLY.includes(collection) ? { _id: { $in: [] } } : undefined,
62
+ // At start: the web lines the app declares, and how it knows who's calling.
63
+ check: (merged) => {
64
+ const problem = settingsProblem(merged.resources, merged.actions);
65
+ if (problem)
66
+ throw new Error(problem);
67
+ },
68
+ resources: {
69
+ web: {},
70
+ // A browser that's a visitor of a web line, asked after people's sessions.
71
+ entrances: { visitor: visitorEntrance },
72
+ // What a contact sends goes in through here first: a web visitor's limits, and its own chat.
73
+ contactSend: { web: { action: "contact.sending" } },
74
+ // What a web line's visitors' turns cost, for its budget.
75
+ modelUse: { webLines: visitorsSpend },
76
+ },
77
+ });
78
+ export default webLines;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * A contact erased (`contact.deleted`): if it was a web visitor, its browser's cookie is no one
3
+ * now. Its record says so, ended, for as long as any cookie made before could last (a line's days
4
+ * may be raised after, and a cookie lasts as its line says now, so the most a line may say, from
5
+ * now); it keeps no personal data (its line, its chat's id, its networks' HMACs). Heard or not,
6
+ * it's never taken for a first message that didn't go in (`contact.prune`). What it has open
7
+ * ends too: its browsers' connections, and their way back without a cookie.
8
+ */
9
+ import type { ProcessorDef } from "../../../../runtime/mod.js";
10
+ export declare const forgetProcessor: ProcessorDef;
@@ -0,0 +1,14 @@
1
+ import { MOST_DAYS } from "../shared/lines.js";
2
+ export const forgetProcessor = {
3
+ on: "contact.deleted",
4
+ key: (event) => String(event.data._id),
5
+ run: async (ctx, event) => {
6
+ const id = String(event.data._id);
7
+ const system = ctx.as();
8
+ const lasts = new Date(Date.now() + MOST_DAYS * 86_400_000).toISOString();
9
+ await system.db.collection("contact_visitor").updateOne({ _id: id }, {
10
+ $set: { ended: true, spoke: true, expires: lasts },
11
+ });
12
+ await system.disconnect({ kind: "contact", id });
13
+ },
14
+ };
@@ -0,0 +1,47 @@
1
+ /**
2
+ * The web lines' entrance to the web host (its `entrances` resource): a
3
+ * browser is the visitor its cookie's secret is (`contact_visitor.actor`),
4
+ * asked after people's sessions. A browser that isn't anyone, asking to come
5
+ * in by one of the app's web lines (`POST /connect` `{ line }`: the page's
6
+ * `web({ arrive: { line } })`), is let in as a visitor (`contact.arrive`):
7
+ * one back with its secret, who hasn't spoken yet, as who it was; a newcomer
8
+ * within the line's limits by address. The address is the app's front's
9
+ * word for it (`web.caller`), else the socket's, as the web host's listener
10
+ * saw it; with neither, nobody is let in (the limits by address would hold
11
+ * nothing). Its cookie comes only with the app's own pages' requests
12
+ * (`SameSite=Strict`), and is this host's alone (`__Host-visitor` wherever
13
+ * it's Secure): no other host under the domain can set one to make a browser
14
+ * a visitor of its choosing, and read its chat.
15
+ */
16
+ import { type Runtime } from "../../../../runtime/mod.js";
17
+ /** What the web host hands an entrance letting someone in (its `Door`). */
18
+ type Door = {
19
+ runtime: Pick<Runtime, "call" | "describe">;
20
+ asked: Record<string, unknown>;
21
+ /** Where the request came from, as its socket says. */
22
+ address?: string;
23
+ /** The secret this entrance's cookie holds, that's nobody to its query. */
24
+ secret?: string;
25
+ };
26
+ /**
27
+ * Lets a browser in as a visitor of the line the page names: back as the
28
+ * one it was, with its secret, or new. Another line's, or no line asked, it
29
+ * leaves alone.
30
+ */
31
+ declare function arrive(request: Request, door: Door): Promise<{
32
+ actor: {
33
+ kind: string;
34
+ id: string;
35
+ };
36
+ secret: string;
37
+ days: number;
38
+ } | null>;
39
+ export declare const visitorEntrance: {
40
+ cookie: string;
41
+ query: string;
42
+ order: number;
43
+ sameSite: "strict";
44
+ hostOnly: boolean;
45
+ arrive: typeof arrive;
46
+ };
47
+ export {};
@@ -0,0 +1,83 @@
1
+ /**
2
+ * The web lines' entrance to the web host (its `entrances` resource): a
3
+ * browser is the visitor its cookie's secret is (`contact_visitor.actor`),
4
+ * asked after people's sessions. A browser that isn't anyone, asking to come
5
+ * in by one of the app's web lines (`POST /connect` `{ line }`: the page's
6
+ * `web({ arrive: { line } })`), is let in as a visitor (`contact.arrive`):
7
+ * one back with its secret, who hasn't spoken yet, as who it was; a newcomer
8
+ * within the line's limits by address. The address is the app's front's
9
+ * word for it (`web.caller`), else the socket's, as the web host's listener
10
+ * saw it; with neither, nobody is let in (the limits by address would hold
11
+ * nothing). Its cookie comes only with the app's own pages' requests
12
+ * (`SameSite=Strict`), and is this host's alone (`__Host-visitor` wherever
13
+ * it's Secure): no other host under the domain can set one to make a browser
14
+ * a visitor of its choosing, and read its chat.
15
+ */
16
+ import { callerFrom, countedOf, failure, privateAddress, } from "../../../../runtime/mod.js";
17
+ import { lineOf, settingsOf } from "../shared/lines.js";
18
+ /** The runtimes already told their visitors all come from a private address. */
19
+ const told = new WeakSet();
20
+ /**
21
+ * Says, once a runtime, when visitors are counted by a socket's private
22
+ * address (a proxy's or a load balancer's before the app, not theirs): every
23
+ * visitor is then one caller, and the limits by address are the whole line's.
24
+ */
25
+ function tellPrivate(runtime, address) {
26
+ if (told.has(runtime) || !privateAddress(address))
27
+ return;
28
+ told.add(runtime);
29
+ console.warn(`web.lines: visitors come from ${address}, a private address: a proxy or a load ` +
30
+ "balancer's before the app, not theirs. Every visitor is then counted as one caller, " +
31
+ "so the limits by address hold for everyone together. Say how the app's front names " +
32
+ "callers (web.caller), unless this is a machine of your own.");
33
+ }
34
+ /** What a visitor is counted by: refused, or an address (none: exempt from limits by address). */
35
+ function countedBy(request, door, resources) {
36
+ const caller = settingsOf({ resources }).caller;
37
+ if (caller) {
38
+ // Around the app's front, straight to the app: refused, when the front vouches for callers.
39
+ const said = callerFrom(request, caller);
40
+ if (said === "refused") {
41
+ throw failure("forbidden", "Not through the app's front.", { public: true });
42
+ }
43
+ return said && !said.exempt ? { address: said.counted } : {};
44
+ }
45
+ const address = door.address ? countedOf(door.address) : undefined;
46
+ if (!address) {
47
+ throw failure("unavailable", "web.lines: no way to tell visitors apart, so none is let in: say how the app's front " +
48
+ "names callers (web.caller), or serve with a listener that gives the socket's address " +
49
+ "(copilotz/web/node's serve or listen; with one of your own, setClientAddress)");
50
+ }
51
+ tellPrivate(door.runtime, door.address);
52
+ return { address };
53
+ }
54
+ /**
55
+ * Lets a browser in as a visitor of the line the page names: back as the
56
+ * one it was, with its secret, or new. Another line's, or no line asked, it
57
+ * leaves alone.
58
+ */
59
+ async function arrive(request, door) {
60
+ const line = door.asked.line;
61
+ if (typeof line !== "string")
62
+ return null;
63
+ const resources = door.runtime.describe().resources;
64
+ if (!lineOf({ resources }, line))
65
+ return null;
66
+ const { address } = countedBy(request, door, resources);
67
+ const answer = door.asked.challenge;
68
+ const { actor, secret, days } = await door.runtime.call("contact.arrive", {
69
+ line,
70
+ ...(address ? { address } : {}),
71
+ ...(door.secret ? { secret: door.secret } : {}),
72
+ ...(typeof answer === "string" && answer ? { challenge: answer.slice(0, 4096) } : {}),
73
+ });
74
+ return { actor, secret, days };
75
+ }
76
+ export const visitorEntrance = {
77
+ cookie: "visitor",
78
+ query: "contact_visitor.actor",
79
+ order: 100,
80
+ sameSite: "strict",
81
+ hostOnly: true,
82
+ arrive,
83
+ };
@@ -0,0 +1,2 @@
1
+ import type { UseListener } from "../../../models/contract.js";
2
+ export declare const visitorsSpend: UseListener;
@@ -0,0 +1,62 @@
1
+ /**
2
+ * What a web line's visitors cost, for its `budget`: the web lines' listener
3
+ * on the models plugin's `modelUse`, told every model call's use in the job
4
+ * that made it, and asked before each (`may`). A call made for a web line's
5
+ * visitor (the turn working for it, a transcription of what it sent) on a
6
+ * line with a budget:
7
+ *
8
+ * - before it's made, is refused once the line's day is spent (or closed):
9
+ * a turn's tool loop stops there (`contact.budget`);
10
+ * - once made, is priced as the usage ledger prices it (`usage.quote`) and
11
+ * charged to the line's day, the visitor's hold with it (`contact.spent`).
12
+ * Use nothing prices (the ledger has no price for its model, or pricing
13
+ * failed) closes the line for the day, said in the log as it does: a
14
+ * budget that can't be counted holds nothing.
15
+ *
16
+ * Nothing else is looked at.
17
+ */
18
+ import { failure, principalOf } from "../../../../runtime/mod.js";
19
+ import { BUDGET } from "../shared/budget.js";
20
+ import { lineOf } from "../shared/lines.js";
21
+ /** The budgeted line a call is made for, and its visitor: none for anyone else. */
22
+ function budgeted(ctx) {
23
+ const visitor = principalOf(ctx.actor);
24
+ if (visitor?.kind !== "contact" || typeof visitor.web !== "string" || !ctx.call)
25
+ return;
26
+ const line = lineOf(ctx, visitor.web);
27
+ return line?.budget ? { line: line.name, contact: visitor.id, call: ctx.call } : undefined;
28
+ }
29
+ async function used(ctx, use) {
30
+ const on = budgeted(ctx);
31
+ if (!on)
32
+ return;
33
+ const price = await on.call("usage.quote", {
34
+ meter: use.meter,
35
+ subject: use.subject,
36
+ ...(use.catalog ? { catalog: use.catalog } : {}),
37
+ quantities: Object.fromEntries(Object.entries(use.quantities).filter(([, n]) => typeof n === "number")),
38
+ }).then(({ price }) => price, () => null);
39
+ if (typeof price === "number" && Number.isFinite(price) && price >= 0) {
40
+ await on.call("contact.spent", { line: on.line, contact: on.contact, price });
41
+ return;
42
+ }
43
+ const { closed } = await on.call("contact.spent", {
44
+ line: on.line,
45
+ contact: on.contact,
46
+ unpriced: use.subject,
47
+ });
48
+ if (closed) {
49
+ console.error(`web.lines.${on.line}: what its visitors used (${use.subject}) has no price, so its ` +
50
+ "budget can't be kept: the line takes no more messages today. Price the model " +
51
+ "(the usage plugin's price resource, or its catalog).");
52
+ }
53
+ }
54
+ async function may(ctx) {
55
+ const on = budgeted(ctx);
56
+ if (!on)
57
+ return;
58
+ const { left } = await on.call("contact.budget", { line: on.line });
59
+ if (!left)
60
+ throw failure("too_many", BUDGET.spent, { public: true });
61
+ }
62
+ export const visitorsSpend = { used, may };
@@ -0,0 +1,65 @@
1
+ /**
2
+ * A web line's budget for the day (UTC), kept with its counters, in memory:
3
+ * what its visitors' model use was charged (`spent`, as the usage ledger
4
+ * prices it), and what's held for messages let in whose turns haven't been
5
+ * charged yet (`holds`, each a visitor's).
6
+ *
7
+ * - **Sending** holds an estimate first (`hold`): the most one of its
8
+ * visitors' model calls cost here lately; before any is known, the rest of
9
+ * the day's budget (so one turn goes at a time until a cost is known). One
10
+ * that doesn't fit beside what's spent and held is refused: messages sent
11
+ * at once can't all pass a budget only one fits.
12
+ * - **A priced call** is charged (`charge`), and the visitor's hold goes
13
+ * with it. A hold not charged in two minutes lapses (its turn failed).
14
+ * - **Before each call** (`left`), a visitor's turn goes on only while the
15
+ * day isn't spent: a tool loop stops there.
16
+ * - **Use nothing prices** (a model the ledger has no price for) closes the
17
+ * line for the day (`close`): a budget that can't be counted holds nothing.
18
+ *
19
+ * Each change is one write that applies only to the day as it was read (its
20
+ * `v`), tried again when another came between.
21
+ */
22
+ import { type Collection } from "../../../../runtime/mod.js";
23
+ import type { Rate } from "../collections/rate.js";
24
+ /** How long a message's hold lasts without being charged. */
25
+ export declare const HOLD_MS = 120000;
26
+ /** The web lines' counters (`contact_rate`), as the system reaches them. */
27
+ type Rates = Collection<Rate & Day>;
28
+ /** The day's budget, as kept. */
29
+ export type Day = {
30
+ spent?: number;
31
+ holds?: Record<string, {
32
+ amount: number;
33
+ until: number;
34
+ }>;
35
+ /** Why the day is closed (use nothing priced: its model). */
36
+ closed?: string;
37
+ v?: number;
38
+ };
39
+ /** What visitors are told when the budget refuses. */
40
+ export declare const BUDGET: {
41
+ readonly spent: "This chat has done all it can for today: come back tomorrow.";
42
+ readonly held: "This chat is busy right now: try again in a moment.";
43
+ };
44
+ /**
45
+ * Holds an estimate of a visitor's message's cost against the day's budget,
46
+ * or refuses it (`too_many`, said to anyone): spent, or what's held already
47
+ * leaves no room.
48
+ */
49
+ export declare function hold(rates: Rates, line: {
50
+ name: string;
51
+ budget: number;
52
+ }, contact: string, now?: number): Promise<void>;
53
+ /** A visitor's priced model call: charged to the day, its hold gone with it; remembered as the estimate. */
54
+ export declare function charge(rates: Rates, line: string, contact: string | undefined, price: number, now?: number): Promise<void>;
55
+ /**
56
+ * Use nothing prices: the day is closed, its visitors' messages refused
57
+ * until tomorrow. Whether this closed it (it wasn't already).
58
+ */
59
+ export declare function close(rates: Rates, line: string, why: string, now?: number): Promise<boolean>;
60
+ /** Whether a visitor's turn may make another call: not once the day is spent, or closed. */
61
+ export declare function left(rates: Rates, line: {
62
+ name: string;
63
+ budget: number;
64
+ }, now?: number): Promise<boolean>;
65
+ export {};