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,161 @@
1
+ /**
2
+ * A line's widgets, as the MCP route answers about them (MCP Apps, SEP-1865, and ChatGPT's
3
+ * Apps SDK aliases): the app's web Views drawn inside the assistant's conversation, through
4
+ * the web host's widgets (`Served.widgets`: `widgetsOf`, which owns front-end code).
5
+ *
6
+ * - **The template** (`resources/list`, `resources/read`): one page for all of a line's
7
+ * widgets, `ui://<line>/widget-<version>.html`, `text/html;profile=mcp-app`; any older
8
+ * version's URI is answered with the current page (assistants keep a tool's template URI).
9
+ * Its `_meta` says the app's own origin may serve its fonts and images (`ui.csp`
10
+ * `resourceDomains`; ChatGPT's `openai/widgetCSP`), and nothing may be connected to; the
11
+ * line's `widgets` settings say its domain, border and description.
12
+ * - **A tool shown through a widget** (`widget`: a View kind): its result carries, in `_meta`
13
+ * (the host's, for the widget; never the model's), what the widget draws: the View, its item
14
+ * (the call: `{ _id, name, input, output }`) and hints (`{ thread }`), who looks (the
15
+ * conversation's contact), what the View reads, answered (a snapshot, as that contact), the
16
+ * widget's grant, and which of the line's tools stand for which Actions.
17
+ * - **A widget's own call** (an entry only the widgets call: `appOnly`): refused without the
18
+ * grant (never the model's: it only ever reads `content`), and with one from another
19
+ * conversation. An Action is called as the conversation's contact (its `allow`, the rules),
20
+ * and its result goes in `_meta` alone (`content` says only that it's done); the reads it
21
+ * asks with (`_reads`) are answered after it, in the same reply. The read tool (`READ`)
22
+ * answers queries as the contact, and nothing else.
23
+ */
24
+ import { type Runtime } from "../../../../runtime/mod.js";
25
+ import type { Widgets } from "../../../../hosts/web/mod.js";
26
+ import { type Scope } from "../shared/keys.js";
27
+ import { type Line } from "../shared/settings.js";
28
+ /** What a widget's template is: MCP Apps' type for HTML that talks to its host. */
29
+ export declare const MIME = "text/html;profile=mcp-app";
30
+ /**
31
+ * The line's template now, when it has widgets and the host serves them. One that can't be
32
+ * made (its code didn't bundle) is said once in the log, and the line goes on without widgets:
33
+ * its tools still answer, drawn by nothing.
34
+ */
35
+ export declare function templateOf(line: Line, widgets: Widgets | undefined, origin: string | undefined): Promise<{
36
+ uri: string;
37
+ html: string;
38
+ } | undefined>;
39
+ /** `resources/list`: the template, when there is one. */
40
+ export declare function resourcesListed(line: Line, widgets: Widgets | undefined, origin: string | undefined): Promise<{
41
+ resources: {
42
+ uri: string;
43
+ name: string;
44
+ title: string;
45
+ mimeType: string;
46
+ _meta: {
47
+ "openai/widgetDescription"?: string | undefined;
48
+ "openai/widgetPrefersBorder"?: boolean | undefined;
49
+ "openai/widgetDomain"?: string | undefined;
50
+ ui: {
51
+ prefersBorder?: boolean | undefined;
52
+ domain?: string | undefined;
53
+ csp: {
54
+ connectDomains: never[];
55
+ resourceDomains: string[];
56
+ };
57
+ };
58
+ "openai/widgetCSP": {
59
+ connect_domains: never[];
60
+ resource_domains: string[];
61
+ };
62
+ };
63
+ }[];
64
+ }>;
65
+ /** `resources/read`: the template, at any version's URI of it; undefined for anything else. */
66
+ export declare function resourceRead(line: Line, widgets: Widgets | undefined, origin: string | undefined, uri: unknown): Promise<{
67
+ contents: {
68
+ uri: string;
69
+ mimeType: string;
70
+ text: string;
71
+ _meta: {
72
+ "openai/widgetDescription"?: string | undefined;
73
+ "openai/widgetPrefersBorder"?: boolean | undefined;
74
+ "openai/widgetDomain"?: string | undefined;
75
+ ui: {
76
+ prefersBorder?: boolean | undefined;
77
+ domain?: string | undefined;
78
+ csp: {
79
+ connectDomains: never[];
80
+ resourceDomains: string[];
81
+ };
82
+ };
83
+ "openai/widgetCSP": {
84
+ connect_domains: never[];
85
+ resource_domains: string[];
86
+ };
87
+ };
88
+ }[];
89
+ } | undefined>;
90
+ /**
91
+ * What a result drawn by a widget carries for it, in `_meta` (see the header): its View and
92
+ * item, what the View reads (answered as the contact), the grant, the Actions' tools.
93
+ */
94
+ export declare function widgetMeta(runtime: Pick<Runtime, "query">, widgets: Widgets, line: Line, scope: Scope & {
95
+ secrets: string[];
96
+ }, call: {
97
+ tool: string;
98
+ input: Record<string, unknown>;
99
+ }, conversation: {
100
+ contact: string;
101
+ thread: string;
102
+ output?: string;
103
+ call?: string;
104
+ }): Promise<{
105
+ "copilotz/widget": {
106
+ view: string;
107
+ item: {
108
+ output?: string | undefined;
109
+ _id: string;
110
+ name: string;
111
+ input: Record<string, unknown>;
112
+ };
113
+ hints: {
114
+ thread: string;
115
+ };
116
+ viewer: {
117
+ kind: string;
118
+ name: string;
119
+ };
120
+ reads: never[] | import("../../../../runtime/client.js").Known;
121
+ grant: string;
122
+ tools: Record<string, string>;
123
+ read: string;
124
+ };
125
+ }>;
126
+ /** What the model is told of a widget's tool it called itself: refused, and why. */
127
+ export declare const NOT_YOURS: string;
128
+ /**
129
+ * The contact a widget's call is for: its grant's, when the grant is good and, when the call
130
+ * names a conversation too (`openai/session`), that conversation's; else undefined (refused).
131
+ */
132
+ export declare function grantedContact(scope: Scope & {
133
+ secrets: string[];
134
+ }, line: Line, input: Record<string, unknown>, meta: Record<string, unknown> | undefined, named: () => Promise<string | undefined>): Promise<string | undefined>;
135
+ /** A widget call's own id (`_call`, which the widget host sends): its retries share it. */
136
+ export declare const CALL = "_call";
137
+ /**
138
+ * A widget's own call, its grant checked (`contact`): admitted by `mcp.call` (the
139
+ * conversation's there, not over, within its limits), then made as the contact: the read
140
+ * tool's reads, or the Action the entry stands for, with the reads asked after it.
141
+ */
142
+ export declare function appCalled(runtime: Pick<Runtime, "call" | "query">, line: Line, scope: Scope & {
143
+ secrets: string[];
144
+ }, call: {
145
+ tool: string;
146
+ input: Record<string, unknown>;
147
+ contact: string;
148
+ /** Its chat, as the route found it (under whichever of the app's secrets it was made). */
149
+ thread: string;
150
+ /** The call's id, for the Action: the same request sent again runs once. */
151
+ id: string;
152
+ user?: string;
153
+ address?: string;
154
+ }): Promise<{
155
+ _meta: Record<string, unknown>;
156
+ isError?: boolean | undefined;
157
+ content: {
158
+ type: string;
159
+ text: string;
160
+ }[];
161
+ }>;
@@ -0,0 +1,246 @@
1
+ /**
2
+ * A line's widgets, as the MCP route answers about them (MCP Apps, SEP-1865, and ChatGPT's
3
+ * Apps SDK aliases): the app's web Views drawn inside the assistant's conversation, through
4
+ * the web host's widgets (`Served.widgets`: `widgetsOf`, which owns front-end code).
5
+ *
6
+ * - **The template** (`resources/list`, `resources/read`): one page for all of a line's
7
+ * widgets, `ui://<line>/widget-<version>.html`, `text/html;profile=mcp-app`; any older
8
+ * version's URI is answered with the current page (assistants keep a tool's template URI).
9
+ * Its `_meta` says the app's own origin may serve its fonts and images (`ui.csp`
10
+ * `resourceDomains`; ChatGPT's `openai/widgetCSP`), and nothing may be connected to; the
11
+ * line's `widgets` settings say its domain, border and description.
12
+ * - **A tool shown through a widget** (`widget`: a View kind): its result carries, in `_meta`
13
+ * (the host's, for the widget; never the model's), what the widget draws: the View, its item
14
+ * (the call: `{ _id, name, input, output }`) and hints (`{ thread }`), who looks (the
15
+ * conversation's contact), what the View reads, answered (a snapshot, as that contact), the
16
+ * widget's grant, and which of the line's tools stand for which Actions.
17
+ * - **A widget's own call** (an entry only the widgets call: `appOnly`): refused without the
18
+ * grant (never the model's: it only ever reads `content`), and with one from another
19
+ * conversation. An Action is called as the conversation's contact (its `allow`, the rules),
20
+ * and its result goes in `_meta` alone (`content` says only that it's done); the reads it
21
+ * asks with (`_reads`) are answered after it, in the same reply. The read tool (`READ`)
22
+ * answers queries as the contact, and nothing else.
23
+ */
24
+ import { isPublic } from "../../../../runtime/mod.js";
25
+ import { answeredAs } from "../../../../runtime/browsers.js";
26
+ import { grantedTo, grantOf } from "../shared/keys.js";
27
+ import { hasWidgets, READ } from "../shared/settings.js";
28
+ import { GRANT, READS } from "./tools.js";
29
+ /** What a widget's template is: MCP Apps' type for HTML that talks to its host. */
30
+ export const MIME = "text/html;profile=mcp-app";
31
+ /** The template's URI, for a version of it. */
32
+ const uriOf = (line, version) => `ui://${line.name}/widget-${version}.html`;
33
+ /** Any version's URI of this line's template. */
34
+ const TEMPLATE = /^ui:\/\/([\w-]{1,60})\/widget-[\w-]{1,64}\.html$/;
35
+ /** The most reads a widget asks at once. */
36
+ const MOST_READS = 32;
37
+ /** Lines already told about in the log, whose template couldn't be made: once each. */
38
+ const broken = new Set();
39
+ /**
40
+ * The line's template now, when it has widgets and the host serves them. One that can't be
41
+ * made (its code didn't bundle) is said once in the log, and the line goes on without widgets:
42
+ * its tools still answer, drawn by nothing.
43
+ */
44
+ export async function templateOf(line, widgets, origin) {
45
+ if (!widgets || !origin || !hasWidgets(line))
46
+ return undefined;
47
+ try {
48
+ const { html, version } = await widgets.page(origin);
49
+ broken.delete(line.name);
50
+ return { uri: uriOf(line, version), html };
51
+ }
52
+ catch (error) {
53
+ if (!broken.has(line.name)) {
54
+ broken.add(line.name);
55
+ console.error(`mcp.${line.name}: no widgets, the template failed:`, error.message);
56
+ }
57
+ return undefined;
58
+ }
59
+ }
60
+ /** The template's `_meta`: MCP Apps' `ui`, with ChatGPT's aliases. */
61
+ function templateMeta(line, origin) {
62
+ const at = new URL(origin).origin;
63
+ const { domain, prefersBorder, description } = line.widgets ?? {};
64
+ return {
65
+ ui: {
66
+ csp: { connectDomains: [], resourceDomains: [at] },
67
+ ...(domain ? { domain } : {}),
68
+ ...(prefersBorder !== undefined ? { prefersBorder } : {}),
69
+ },
70
+ "openai/widgetCSP": { connect_domains: [], resource_domains: [at] },
71
+ ...(domain ? { "openai/widgetDomain": domain } : {}),
72
+ ...(prefersBorder !== undefined ? { "openai/widgetPrefersBorder": prefersBorder } : {}),
73
+ ...(description ? { "openai/widgetDescription": description } : {}),
74
+ };
75
+ }
76
+ /** `resources/list`: the template, when there is one. */
77
+ export async function resourcesListed(line, widgets, origin) {
78
+ const template = await templateOf(line, widgets, origin);
79
+ return {
80
+ resources: template
81
+ ? [{
82
+ uri: template.uri,
83
+ name: `${line.name}-widget`,
84
+ title: line.title ?? line.name,
85
+ mimeType: MIME,
86
+ _meta: templateMeta(line, origin),
87
+ }]
88
+ : [],
89
+ };
90
+ }
91
+ /** `resources/read`: the template, at any version's URI of it; undefined for anything else. */
92
+ export async function resourceRead(line, widgets, origin, uri) {
93
+ if (typeof uri !== "string" || TEMPLATE.exec(uri)?.[1] !== line.name)
94
+ return undefined;
95
+ const template = await templateOf(line, widgets, origin);
96
+ if (!template)
97
+ return undefined;
98
+ return {
99
+ contents: [{ uri, mimeType: MIME, text: template.html, _meta: templateMeta(line, origin) }],
100
+ };
101
+ }
102
+ /** Which of the line's tools stand for which Actions: how a widget's View calls them. */
103
+ const actionsOf = (line) => Object.fromEntries(Object.entries(line.tools).flatMap(([name, entry]) => entry.action ? [[entry.action, name]] : []));
104
+ /** The conversation's contact, as its widgets' Views see it. */
105
+ const contactActor = (line, contact) => ({
106
+ kind: "contact",
107
+ id: contact,
108
+ mcp: line.name,
109
+ client: line.client,
110
+ });
111
+ /**
112
+ * What a result drawn by a widget carries for it, in `_meta` (see the header): its View and
113
+ * item, what the View reads (answered as the contact), the grant, the Actions' tools.
114
+ */
115
+ export async function widgetMeta(runtime, widgets, line, scope, call, conversation) {
116
+ const view = line.tools[call.tool].widget;
117
+ const viewer = { kind: "contact", name: `contact:${conversation.contact}` };
118
+ const item = {
119
+ _id: conversation.call ?? `${conversation.thread}:${call.tool}`,
120
+ name: call.tool,
121
+ input: call.input,
122
+ ...(conversation.output !== undefined ? { output: conversation.output } : {}),
123
+ };
124
+ const hints = { thread: conversation.thread };
125
+ const actor = contactActor(line, conversation.contact);
126
+ const reads = await widgets.snapshot(runtime, {
127
+ actor,
128
+ viewer,
129
+ view,
130
+ item,
131
+ // What the widget observes first: inline, until its host says otherwise.
132
+ hints: { ...hints, mode: "inline", surface: "widget" },
133
+ }).catch((error) => {
134
+ console.error(`mcp.${line.name}: ${view}'s snapshot failed:`, error.message);
135
+ return [];
136
+ });
137
+ return {
138
+ "copilotz/widget": {
139
+ view,
140
+ item,
141
+ hints,
142
+ viewer,
143
+ reads,
144
+ grant: await grantOf(scope.secrets[0], scope, conversation.contact),
145
+ tools: actionsOf(line),
146
+ read: READ,
147
+ },
148
+ };
149
+ }
150
+ /**
151
+ * A widget's own call's reply: `content` says only whether it went (a host may show it, or its
152
+ * model read it); the rest is for the widget, with a new grant (one in use never runs out).
153
+ */
154
+ const reply = (text, meta, isError = false) => ({
155
+ content: [{ type: "text", text }],
156
+ ...(isError ? { isError: true } : {}),
157
+ _meta: meta,
158
+ });
159
+ /** What `content` says of a widget's own call that didn't go: never the refusal's words. */
160
+ const NOT_DONE = "It didn't go through in the widget.";
161
+ /** What the model is told of a widget's tool it called itself: refused, and why. */
162
+ export const NOT_YOURS = "This tool is the app's widget's own: only the widget calls it, when " +
163
+ "the user acts in it. Ask the user to use the widget.";
164
+ /**
165
+ * The contact a widget's call is for: its grant's, when the grant is good and, when the call
166
+ * names a conversation too (`openai/session`), that conversation's; else undefined (refused).
167
+ */
168
+ export async function grantedContact(scope, line, input, meta, named) {
169
+ const grant = input[GRANT] ?? meta?.["copilotz/grant"];
170
+ // Made with the app's own secret; one made with the secret before it is still good, its hours.
171
+ let contact;
172
+ for (const secret of scope.secrets)
173
+ contact ??= await grantedTo(secret, scope, grant);
174
+ if (!contact)
175
+ return undefined;
176
+ const session = await named();
177
+ // Where every conversation names itself (ChatGPT's lines, no handles), so must a widget's call:
178
+ // a grant alone, copied elsewhere, drives nothing.
179
+ if (session === undefined)
180
+ return line.handles ? contact : undefined;
181
+ return session === contact ? contact : undefined;
182
+ }
183
+ /** A widget call's own id (`_call`, which the widget host sends): its retries share it. */
184
+ export const CALL = "_call";
185
+ /** Reads a widget asked for, as the contact: queries only, at most `MOST_READS`. */
186
+ async function readsOf(runtime, actor, asked) {
187
+ const reads = Array.isArray(asked) ? asked.slice(0, MOST_READS) : [];
188
+ const answers = await Promise.all(reads.map((r) => {
189
+ const { key, read } = (r ?? {});
190
+ if (typeof key !== "string" || key.length > 2_000 || !read || typeof read !== "object") {
191
+ return undefined;
192
+ }
193
+ return answeredAs(runtime, actor, key, read);
194
+ }));
195
+ return answers.flatMap((a) => a ? [[a.key, "error" in a ? { error: a.error } : { value: a.value }]] : []);
196
+ }
197
+ /**
198
+ * A widget's own call, its grant checked (`contact`): admitted by `mcp.call` (the
199
+ * conversation's there, not over, within its limits), then made as the contact: the read
200
+ * tool's reads, or the Action the entry stands for, with the reads asked after it.
201
+ */
202
+ export async function appCalled(runtime, line, scope, call) {
203
+ const admitted = await runtime.call("mcp.call", {
204
+ line: line.name,
205
+ tool: call.tool,
206
+ input: {},
207
+ call: call.id.slice(0, 120),
208
+ thread: call.thread,
209
+ by: "handle",
210
+ app: true,
211
+ ...(call.user ? { user: call.user } : {}),
212
+ ...(call.address ? { address: call.address } : {}),
213
+ }, { actor: contactActor(line, call.contact) });
214
+ if (!admitted.admitted) {
215
+ return reply(admitted.text, { "copilotz/error": { message: admitted.text, code: "refused" } }, true);
216
+ }
217
+ const actor = contactActor(line, call.contact);
218
+ const { [GRANT]: _, [READS]: after, [CALL]: _call, ...input } = call.input;
219
+ const grant = { "copilotz/grant": await grantOf(scope.secrets[0], scope, call.contact) };
220
+ if (call.tool === READ) {
221
+ return reply("Read.", {
222
+ "copilotz/reads": await readsOf(runtime, actor, input.reads),
223
+ ...grant,
224
+ });
225
+ }
226
+ const action = line.tools[call.tool].action;
227
+ try {
228
+ const result = await runtime.call(action, input, { actor, id: call.id });
229
+ return reply("Done in the widget.", {
230
+ "copilotz/result": result ?? null,
231
+ ...(after ? { "copilotz/reads": await readsOf(runtime, actor, after) } : {}),
232
+ ...grant,
233
+ });
234
+ }
235
+ catch (error) {
236
+ const e = error;
237
+ const said = isPublic(error) && e.message ? e.message.slice(0, 1_000) : "Something went wrong.";
238
+ // The refusal's words (an upstream's, maybe naming what the person sent) go to the widget
239
+ // alone; `content` says only that it didn't go.
240
+ return reply(NOT_DONE, {
241
+ "copilotz/error": { message: said, ...(e.code ? { code: e.code } : {}) },
242
+ ...(after ? { "copilotz/reads": await readsOf(runtime, actor, after) } : {}),
243
+ ...grant,
244
+ }, true);
245
+ }
246
+ }
@@ -1,22 +1,3 @@
1
- /**
2
- * Which conversation a call belongs to, and its ids, without a table: each
3
- * comes from what names the conversation, through an HMAC with the app's
4
- * secret (the `links` resource's), so nobody without the secret can work
5
- * one out from another, and nothing maps sessions to chats.
6
- *
7
- * - **What names a conversation** (its key): ChatGPT's `openai/session`
8
- * with its `openai/subject`, or a `chat` handle the app gave.
9
- * - **Its contact** is `c-<hmac>`, of the key, the line and the org; **its
10
- * chat** is `mcp-<hmac>` of the contact. A contact, which is all an MCP
11
- * call ever acts as, is in that chat and no other.
12
- * - **A handle** is 128 random bits, shown to the client and kept nowhere.
13
- * - **A call's id** is `mcp:<thread>:<request>:<what>`, hashes of the
14
- * JSON-RPC request's id and of the tool and its input: a request sent
15
- * again is the same call, and two different ones under the same request
16
- * id (a client that counts from 1 for each call) never are. Nothing a
17
- * client sends goes into it as it came.
18
- * - **A caller's address** is counted as `a-<hmac>`, never kept as it is.
19
- */
20
1
  /** What names a conversation: ChatGPT's session (and user), or a handle the app gave. */
21
2
  export type Key = {
22
3
  session: string;
@@ -34,6 +15,14 @@ export declare function conversationOf(secret: string, scope: Scope, key: Key):
34
15
  contact: string;
35
16
  thread: string;
36
17
  }>;
18
+ /**
19
+ * The contact and chat a conversation is under each of the app's secrets
20
+ * (`secretsOf`: its own first, then the one before it).
21
+ */
22
+ export declare const conversationsOf: (secrets: string[], scope: Scope, key: Key) => Promise<{
23
+ contact: string;
24
+ thread: string;
25
+ }[]>;
37
26
  /** A conversation's chat, from its contact. */
38
27
  export declare const threadOf: (secret: string, contact: string) => Promise<string>;
39
28
  /** A user, as their new conversations are counted (ChatGPT's `openai/subject`, never kept as it is). */
@@ -48,3 +37,20 @@ export declare const newHandle: () => string;
48
37
  export declare const sameCallOf: (thread: string, tool: string, input: unknown) => Promise<string>;
49
38
  /** A call's id: its conversation's chat, a hash of the request's id, and one of what it asks. */
50
39
  export declare function callIdOf(thread: string, request: string | number, tool: string, input: unknown): Promise<string>;
40
+ /**
41
+ * A widget's grant: what a call from the line's widgets carries to show it's theirs, never the
42
+ * model's (its widget-only tools, `appOnly`, refuse a call without one).
43
+ * `g2.<contact>.<until>.<hmac>`: the conversation's contact, until when it's good (seconds, base
44
+ * 36), and an HMAC of both with the line and the org, with the app's secret. It goes out only in
45
+ * results' `_meta` (the host's for the widget; a model never reads it), so the model can't make
46
+ * one, and it's good for `GRANT_MS` (6 hours): a widget gets a new one with each reply, so one
47
+ * in use never runs out, and one that leaked does. On a line where ChatGPT names every
48
+ * conversation (`handles: false`), the call must name the grant's conversation too.
49
+ */
50
+ export declare const GRANT_MS: number;
51
+ export declare function grantOf(secret: string, scope: Scope, contact: string, now?: number): Promise<string>;
52
+ /**
53
+ * The contact a grant is for, when it's one this app made for this line and it's still good;
54
+ * else undefined.
55
+ */
56
+ export declare function grantedTo(secret: string, scope: Scope, grant: unknown, now?: number): Promise<string | undefined>;
@@ -1,29 +1,34 @@
1
1
  /**
2
2
  * Which conversation a call belongs to, and its ids, without a table: each
3
- * comes from what names the conversation, through an HMAC with the app's
4
- * secret (the `links` resource's), so nobody without the secret can work
5
- * one out from another, and nothing maps sessions to chats.
3
+ * comes from what names the conversation, through an HMAC with the MCP key
4
+ * of the app's secret (the `links` resource's, through `keyOf`: never the
5
+ * secret itself, nor the key its links or visitors' cookies are signed
6
+ * with), so nobody without the secret can work one out from another, and
7
+ * nothing maps sessions to chats.
6
8
  *
7
9
  * - **What names a conversation** (its key): ChatGPT's `openai/session`
8
10
  * with its `openai/subject`, or a `chat` handle the app gave.
9
11
  * - **Its contact** is `c-<hmac>`, of the key, the line and the org; **its
10
12
  * chat** is `mcp-<hmac>` of the contact. A contact, which is all an MCP
11
13
  * call ever acts as, is in that chat and no other.
14
+ * - **The secret before** (`links.previous`): a conversation its contact
15
+ * was made with stays that contact, and its chat (`conversationsOf` gives
16
+ * each secret's, the app's own first; whoever looks them up takes the one
17
+ * that's there, else the first). Only the app's own secret makes new ones.
12
18
  * - **A handle** is 128 random bits, shown to the client and kept nowhere.
13
19
  * - **A call's id** is `mcp:<thread>:<request>:<what>`, hashes of the
14
20
  * JSON-RPC request's id and of the tool and its input: a request sent
15
21
  * again is the same call, and two different ones under the same request
16
22
  * id (a client that counts from 1 for each call) never are. Nothing a
17
23
  * client sends goes into it as it came.
18
- * - **A caller's address** is counted as `a-<hmac>`, never kept as it is.
24
+ * - **A caller's address** is counted as `a-<hmac>`, never kept as it is
25
+ * (and a user, `u-<hmac>`): counted afresh once the secret changes.
19
26
  */
27
+ import { keyOf } from "../../../../runtime/mod.js";
20
28
  const encoder = new TextEncoder();
21
29
  const hex = (bytes) => [...new Uint8Array(bytes)].map((b) => b.toString(16).padStart(2, "0")).join("");
22
- /** HMAC-SHA256 of `parts` (kept apart as JSON), in hex. */
23
- async function mac(secret, parts) {
24
- const key = await crypto.subtle.importKey("raw", encoder.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
25
- return hex(await crypto.subtle.sign("HMAC", key, encoder.encode(JSON.stringify(parts))));
26
- }
30
+ /** HMAC-SHA256 of `parts` (kept apart as JSON) with the MCP key of `secret`, in hex. */
31
+ const mac = async (secret, parts) => hex(await crypto.subtle.sign("HMAC", await keyOf(secret, "mcp"), encoder.encode(JSON.stringify(parts))));
27
32
  /** The contact a conversation is, and its chat. */
28
33
  export async function conversationOf(secret, scope, key) {
29
34
  const named = "session" in key
@@ -32,6 +37,11 @@ export async function conversationOf(secret, scope, key) {
32
37
  const contact = `c-${(await mac(secret, ["mcp", "contact", scope.org ?? null, scope.line, ...named])).slice(0, 24)}`;
33
38
  return { contact, thread: await threadOf(secret, contact) };
34
39
  }
40
+ /**
41
+ * The contact and chat a conversation is under each of the app's secrets
42
+ * (`secretsOf`: its own first, then the one before it).
43
+ */
44
+ export const conversationsOf = (secrets, scope, key) => Promise.all(secrets.map((secret) => conversationOf(secret, scope, key)));
35
45
  /** A conversation's chat, from its contact. */
36
46
  export const threadOf = async (secret, contact) => `mcp-${(await mac(secret, ["mcp", "thread", contact])).slice(0, 32)}`;
37
47
  /** A user, as their new conversations are counted (ChatGPT's `openai/subject`, never kept as it is). */
@@ -51,3 +61,39 @@ export async function callIdOf(thread, request, tool, input) {
51
61
  const [asked, what] = await Promise.all([sha256(["request", request]), sha256([tool, input])]);
52
62
  return `mcp:${thread}:${asked.slice(0, 16)}:${what.slice(0, 16)}`;
53
63
  }
64
+ /**
65
+ * A widget's grant: what a call from the line's widgets carries to show it's theirs, never the
66
+ * model's (its widget-only tools, `appOnly`, refuse a call without one).
67
+ * `g2.<contact>.<until>.<hmac>`: the conversation's contact, until when it's good (seconds, base
68
+ * 36), and an HMAC of both with the line and the org, with the app's secret. It goes out only in
69
+ * results' `_meta` (the host's for the widget; a model never reads it), so the model can't make
70
+ * one, and it's good for `GRANT_MS` (6 hours): a widget gets a new one with each reply, so one
71
+ * in use never runs out, and one that leaked does. On a line where ChatGPT names every
72
+ * conversation (`handles: false`), the call must name the grant's conversation too.
73
+ */
74
+ export const GRANT_MS = 6 * 3_600_000;
75
+ export async function grantOf(secret, scope, contact, now = Date.now()) {
76
+ const until = Math.floor((now + GRANT_MS) / 1000).toString(36);
77
+ const said = await mac(secret, ["mcp", "grant", scope.org ?? null, scope.line, contact, until]);
78
+ return `g2.${contact}.${until}.${said.slice(0, 32)}`;
79
+ }
80
+ /**
81
+ * The contact a grant is for, when it's one this app made for this line and it's still good;
82
+ * else undefined.
83
+ */
84
+ export async function grantedTo(secret, scope, grant, now = Date.now()) {
85
+ if (typeof grant !== "string" || grant.length > 120)
86
+ return undefined;
87
+ const found = /^g2\.(c-[0-9a-f]{24})\.([0-9a-z]{1,10})\.([0-9a-f]{32})$/.exec(grant);
88
+ if (!found)
89
+ return undefined;
90
+ const [, contact, until] = found;
91
+ if (parseInt(until, 36) * 1000 <= now)
92
+ return undefined;
93
+ const said = await mac(secret, ["mcp", "grant", scope.org ?? null, scope.line, contact, until]);
94
+ const expected = `g2.${contact}.${until}.${said.slice(0, 32)}`;
95
+ let differ = expected.length ^ grant.length;
96
+ for (let i = 0; i < expected.length; i++)
97
+ differ |= expected.charCodeAt(i) ^ grant.charCodeAt(i);
98
+ return differ === 0 ? contact : undefined;
99
+ }