@agentorg/cli 0.0.4 → 0.0.6

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 (206) hide show
  1. package/README.md +18 -102
  2. package/dist/node_modules/@agentorg/hub/dist/src/hub-actor.d.ts +2 -7
  3. package/dist/node_modules/@agentorg/hub/dist/src/hub-actor.js +4 -111
  4. package/dist/node_modules/@agentorg/hub/dist/src/index.d.ts +1 -3
  5. package/dist/node_modules/@agentorg/hub/dist/src/index.js +1 -3
  6. package/dist/node_modules/@agentorg/hub/dist/src/router.d.ts +6 -57
  7. package/dist/node_modules/@agentorg/hub/dist/src/router.js +38 -413
  8. package/dist/node_modules/@agentorg/hub/dist/src/state.d.ts +20 -298
  9. package/dist/node_modules/@agentorg/hub/dist/src/state.js +73 -456
  10. package/dist/node_modules/@agentorg/hub/dist/src/validate.d.ts +1 -3
  11. package/dist/node_modules/@agentorg/hub/dist/src/validate.js +3 -15
  12. package/dist/node_modules/@agentorg/hub/package.json +0 -3
  13. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.d.ts +132 -0
  14. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-provider.js +105 -0
  15. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-settings.d.ts +42 -0
  16. package/dist/node_modules/@agentorg/interfaces/dist/src/agent-settings.js +53 -0
  17. package/dist/node_modules/@agentorg/interfaces/dist/src/chat.d.ts +15 -0
  18. package/dist/node_modules/@agentorg/interfaces/dist/src/chat.js +14 -0
  19. package/dist/node_modules/@agentorg/interfaces/dist/src/hub.d.ts +16 -267
  20. package/dist/node_modules/@agentorg/interfaces/dist/src/hub.js +17 -145
  21. package/dist/node_modules/@agentorg/interfaces/dist/src/index.d.ts +6 -8
  22. package/dist/node_modules/@agentorg/interfaces/dist/src/index.js +6 -8
  23. package/dist/node_modules/@agentorg/interfaces/dist/src/kv.d.ts +2 -2
  24. package/dist/node_modules/@agentorg/interfaces/dist/src/kv.js +2 -2
  25. package/dist/node_modules/@agentorg/interfaces/dist/src/manager.d.ts +272 -0
  26. package/dist/node_modules/@agentorg/interfaces/dist/src/manager.js +185 -0
  27. package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.d.ts +88 -0
  28. package/dist/node_modules/@agentorg/interfaces/dist/src/mcp-provider.js +78 -0
  29. package/dist/node_modules/@agentorg/interfaces/dist/src/tools.d.ts +39 -0
  30. package/dist/node_modules/@agentorg/interfaces/dist/src/tools.js +43 -0
  31. package/dist/node_modules/@agentorg/manager/dist/src/index.d.ts +1 -0
  32. package/dist/node_modules/@agentorg/manager/dist/src/index.js +1 -0
  33. package/dist/node_modules/@agentorg/manager/dist/src/manager.d.ts +21 -0
  34. package/dist/node_modules/@agentorg/manager/dist/src/manager.js +528 -0
  35. package/dist/node_modules/@agentorg/manager/dist/src/skills.d.ts +25 -0
  36. package/dist/node_modules/@agentorg/manager/dist/src/skills.js +145 -0
  37. package/dist/node_modules/@agentorg/manager/package.json +30 -0
  38. package/dist/node_modules/@agentorg/mcp/dist/src/connection.d.ts +33 -0
  39. package/dist/node_modules/@agentorg/mcp/dist/src/connection.js +290 -0
  40. package/dist/node_modules/@agentorg/mcp/dist/src/index.d.ts +1 -0
  41. package/dist/node_modules/@agentorg/mcp/dist/src/index.js +1 -0
  42. package/dist/node_modules/@agentorg/mcp/dist/src/kinds.d.ts +50 -0
  43. package/dist/node_modules/@agentorg/mcp/dist/src/kinds.js +69 -0
  44. package/dist/node_modules/@agentorg/mcp/dist/src/provider.d.ts +22 -0
  45. package/dist/node_modules/@agentorg/mcp/dist/src/provider.js +167 -0
  46. package/dist/node_modules/@agentorg/mcp/package.json +31 -0
  47. package/dist/node_modules/@agentorg/sdk/dist/src/cli.js +3 -23
  48. package/dist/node_modules/@agentorg/sdk/dist/src/client.d.ts +4 -25
  49. package/dist/node_modules/@agentorg/sdk/dist/src/client.js +5 -55
  50. package/dist/node_modules/@agentorg/sdk/dist/src/errors.d.ts +1 -1
  51. package/dist/node_modules/@agentorg/sdk/dist/src/errors.js +3 -3
  52. package/dist/node_modules/@agentorg/sdk/dist/src/index.d.ts +2 -2
  53. package/dist/node_modules/@agentorg/sdk/dist/src/index.js +1 -1
  54. package/dist/node_modules/@agentorg/sdk/dist/src/interface.d.ts +6 -34
  55. package/dist/node_modules/@agentorg/sdk/dist/src/interface.js +3 -24
  56. package/dist/node_modules/@agentorg/sdk/dist/src/module-host.d.ts +8 -17
  57. package/dist/node_modules/@agentorg/sdk/dist/src/module-host.js +15 -87
  58. package/dist/node_modules/@agentorg/sdk/dist/src/module.d.ts +10 -76
  59. package/dist/node_modules/@agentorg/sdk/dist/src/module.js +3 -23
  60. package/dist/node_modules/@agentorg/sdk/dist/src/person-commands.js +0 -5
  61. package/dist/node_modules/@agentorg/sdk/dist/src/runtime.d.ts +3 -27
  62. package/dist/node_modules/@agentorg/sdk/dist/src/runtime.js +5 -100
  63. package/dist/node_modules/@agentorg/sdk/dist/src/testing.d.ts +7 -8
  64. package/dist/node_modules/@agentorg/sdk/dist/src/testing.js +12 -24
  65. package/dist/node_modules/@agentorg/sdk/package.json +4 -37
  66. package/dist/node_modules/@agentorg/shell/dist/app.css +3435 -739
  67. package/dist/node_modules/@agentorg/shell/dist/app.js +204 -202
  68. package/dist/node_modules/@agentorg/shell/package.json +18 -10
  69. package/dist/node_modules/@agentorg/wire/dist/src/frames.d.ts +2 -3
  70. package/dist/node_modules/@agentorg/wire/dist/src/frames.js +2 -3
  71. package/dist/node_modules/@agentorg/wire/dist/src/ops.d.ts +5 -34
  72. package/dist/node_modules/@agentorg/wire/dist/src/ops.js +2 -17
  73. package/dist/node_modules/assistant/dist/src/agent.d.ts +62 -0
  74. package/dist/node_modules/assistant/dist/src/agent.js +226 -0
  75. package/dist/node_modules/assistant/dist/src/engine.d.ts +23 -0
  76. package/dist/node_modules/assistant/dist/src/engine.js +88 -0
  77. package/dist/node_modules/assistant/dist/src/history.d.ts +97 -0
  78. package/dist/node_modules/assistant/dist/src/history.js +235 -0
  79. package/dist/node_modules/assistant/dist/src/local-store.d.ts +3 -0
  80. package/dist/node_modules/assistant/dist/src/local-store.js +58 -0
  81. package/dist/node_modules/assistant/dist/src/provider.d.ts +21 -0
  82. package/dist/node_modules/assistant/dist/src/provider.js +231 -0
  83. package/dist/node_modules/assistant/dist/src/settings.d.ts +17 -0
  84. package/dist/node_modules/assistant/dist/src/settings.js +43 -0
  85. package/dist/node_modules/assistant/dist/src/tools.d.ts +74 -0
  86. package/dist/node_modules/assistant/dist/src/tools.js +123 -0
  87. package/dist/node_modules/assistant/package.json +29 -0
  88. package/dist/node_modules/chatgpt/dist/src/account.d.ts +52 -0
  89. package/dist/node_modules/chatgpt/dist/src/account.js +270 -0
  90. package/dist/node_modules/chatgpt/dist/src/encoding.d.ts +7 -0
  91. package/dist/node_modules/chatgpt/dist/src/encoding.js +20 -0
  92. package/dist/node_modules/chatgpt/dist/src/file-store.d.ts +9 -0
  93. package/dist/node_modules/chatgpt/dist/src/file-store.js +68 -0
  94. package/dist/node_modules/chatgpt/dist/src/index.d.ts +1 -0
  95. package/dist/node_modules/chatgpt/dist/src/index.js +1 -0
  96. package/dist/node_modules/chatgpt/dist/src/jwt.d.ts +15 -0
  97. package/dist/node_modules/chatgpt/dist/src/jwt.js +37 -0
  98. package/dist/node_modules/chatgpt/dist/src/oauth.d.ts +43 -0
  99. package/dist/node_modules/chatgpt/dist/src/oauth.js +104 -0
  100. package/dist/node_modules/chatgpt/dist/src/program.d.ts +23 -0
  101. package/dist/node_modules/chatgpt/dist/src/program.js +41 -0
  102. package/dist/node_modules/chatgpt/dist/src/responses.d.ts +54 -0
  103. package/dist/node_modules/chatgpt/dist/src/responses.js +300 -0
  104. package/dist/node_modules/chatgpt/dist/src/serve.d.ts +65 -0
  105. package/dist/node_modules/chatgpt/dist/src/serve.js +46 -0
  106. package/dist/node_modules/chatgpt/package.json +33 -0
  107. package/dist/src/api.d.ts +5 -34
  108. package/dist/src/api.js +6 -68
  109. package/dist/src/cli.js +3 -7
  110. package/dist/src/gateway.d.ts +6 -7
  111. package/dist/src/gateway.js +39 -285
  112. package/dist/src/index.d.ts +4 -5
  113. package/dist/src/index.js +3 -4
  114. package/dist/src/notes.d.ts +11 -0
  115. package/dist/src/notes.js +64 -0
  116. package/dist/src/oauth.d.ts +16 -0
  117. package/dist/src/oauth.js +46 -0
  118. package/dist/src/programs.d.ts +29 -0
  119. package/dist/src/programs.js +171 -0
  120. package/package.json +3 -11
  121. package/dist/bundled/index.json +0 -6
  122. package/dist/bundled/sha256-01d69bc3888c2a53963f4413d47de909d129ad27c78e9af6d830bb50c3478419.agentorg +0 -0
  123. package/dist/bundled/sha256-84165e3be5c904fc971d983dab31102e07cca725d8629a6fd7d118fa3a0a88b8.agentorg +0 -0
  124. package/dist/bundled/sha256-ade9132b3f34b4c39db92adeadc7e540dc745e4a81b5b54f5d45bf9ec92bca75.agentorg +0 -0
  125. package/dist/bundled/sha256-f4330f5987f37d8cf3aeb44d2f831b573641a87186daa2f811f0c35ba04804f0.agentorg +0 -0
  126. package/dist/node_modules/@agentorg/hub/dist/src/build.d.ts +0 -17
  127. package/dist/node_modules/@agentorg/hub/dist/src/build.js +0 -120
  128. package/dist/node_modules/@agentorg/hub/dist/src/logs.d.ts +0 -33
  129. package/dist/node_modules/@agentorg/hub/dist/src/logs.js +0 -162
  130. package/dist/node_modules/@agentorg/hub/dist/src/net.d.ts +0 -41
  131. package/dist/node_modules/@agentorg/hub/dist/src/net.js +0 -265
  132. package/dist/node_modules/@agentorg/hub/dist/src/processes.d.ts +0 -78
  133. package/dist/node_modules/@agentorg/hub/dist/src/processes.js +0 -485
  134. package/dist/node_modules/@agentorg/hub/dist/src/storage.d.ts +0 -38
  135. package/dist/node_modules/@agentorg/hub/dist/src/storage.js +0 -194
  136. package/dist/node_modules/@agentorg/interfaces/dist/src/http-egress.d.ts +0 -6
  137. package/dist/node_modules/@agentorg/interfaces/dist/src/http-egress.js +0 -7
  138. package/dist/node_modules/@agentorg/interfaces/dist/src/installer.d.ts +0 -191
  139. package/dist/node_modules/@agentorg/interfaces/dist/src/installer.js +0 -136
  140. package/dist/node_modules/@agentorg/interfaces/dist/src/layout.d.ts +0 -47
  141. package/dist/node_modules/@agentorg/interfaces/dist/src/layout.js +0 -52
  142. package/dist/node_modules/@agentorg/interfaces/dist/src/rules.d.ts +0 -35
  143. package/dist/node_modules/@agentorg/interfaces/dist/src/rules.js +0 -37
  144. package/dist/node_modules/@agentorg/interfaces/dist/src/storage-admin.d.ts +0 -23
  145. package/dist/node_modules/@agentorg/interfaces/dist/src/storage-admin.js +0 -31
  146. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-shell.d.ts +0 -14
  147. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-shell.js +0 -26
  148. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-view.d.ts +0 -27
  149. package/dist/node_modules/@agentorg/interfaces/dist/src/ui-view.js +0 -26
  150. package/dist/node_modules/@agentorg/kit/dist/src/chat.d.ts +0 -37
  151. package/dist/node_modules/@agentorg/kit/dist/src/chat.js +0 -131
  152. package/dist/node_modules/@agentorg/kit/dist/src/components.d.ts +0 -94
  153. package/dist/node_modules/@agentorg/kit/dist/src/components.js +0 -66
  154. package/dist/node_modules/@agentorg/kit/dist/src/icons.d.ts +0 -17
  155. package/dist/node_modules/@agentorg/kit/dist/src/icons.js +0 -30
  156. package/dist/node_modules/@agentorg/kit/dist/src/index.d.ts +0 -6
  157. package/dist/node_modules/@agentorg/kit/dist/src/index.js +0 -10
  158. package/dist/node_modules/@agentorg/kit/dist/src/markdown.d.ts +0 -9
  159. package/dist/node_modules/@agentorg/kit/dist/src/markdown.js +0 -14
  160. package/dist/node_modules/@agentorg/kit/dist/src/resizable.d.ts +0 -5
  161. package/dist/node_modules/@agentorg/kit/dist/src/resizable.js +0 -8
  162. package/dist/node_modules/@agentorg/kit/dist/src/styles.d.ts +0 -11
  163. package/dist/node_modules/@agentorg/kit/dist/src/styles.js +0 -166
  164. package/dist/node_modules/@agentorg/kit/package.json +0 -44
  165. package/dist/node_modules/@agentorg/sdk/dist/frame/runtime.js +0 -753
  166. package/dist/node_modules/@agentorg/sdk/dist/host/runner.js +0 -21963
  167. package/dist/node_modules/@agentorg/sdk/dist/src/build.d.ts +0 -2
  168. package/dist/node_modules/@agentorg/sdk/dist/src/build.js +0 -2
  169. package/dist/node_modules/@agentorg/sdk/dist/src/client-http.d.ts +0 -24
  170. package/dist/node_modules/@agentorg/sdk/dist/src/client-http.js +0 -247
  171. package/dist/node_modules/@agentorg/sdk/dist/src/describe.d.ts +0 -13
  172. package/dist/node_modules/@agentorg/sdk/dist/src/describe.js +0 -16
  173. package/dist/node_modules/@agentorg/sdk/dist/src/frame-base.d.ts +0 -8
  174. package/dist/node_modules/@agentorg/sdk/dist/src/frame-base.js +0 -64
  175. package/dist/node_modules/@agentorg/sdk/dist/src/frame-entry.d.ts +0 -1
  176. package/dist/node_modules/@agentorg/sdk/dist/src/frame-entry.js +0 -35
  177. package/dist/node_modules/@agentorg/sdk/dist/src/frame-host.d.ts +0 -57
  178. package/dist/node_modules/@agentorg/sdk/dist/src/frame-host.js +0 -293
  179. package/dist/node_modules/@agentorg/sdk/dist/src/frame-protocol.d.ts +0 -124
  180. package/dist/node_modules/@agentorg/sdk/dist/src/frame-protocol.js +0 -16
  181. package/dist/node_modules/@agentorg/sdk/dist/src/frame.d.ts +0 -25
  182. package/dist/node_modules/@agentorg/sdk/dist/src/frame.js +0 -248
  183. package/dist/node_modules/@agentorg/sdk/dist/src/host-main.d.ts +0 -1
  184. package/dist/node_modules/@agentorg/sdk/dist/src/host-main.js +0 -60
  185. package/dist/node_modules/@agentorg/sdk/dist/src/host.d.ts +0 -25
  186. package/dist/node_modules/@agentorg/sdk/dist/src/host.js +0 -92
  187. package/dist/node_modules/@agentorg/sdk/dist/src/manifest-api.d.ts +0 -2
  188. package/dist/node_modules/@agentorg/sdk/dist/src/manifest-api.js +0 -2
  189. package/dist/node_modules/@agentorg/sdk/dist/src/manifest.d.ts +0 -56
  190. package/dist/node_modules/@agentorg/sdk/dist/src/manifest.js +0 -165
  191. package/dist/node_modules/@agentorg/sdk/dist/src/package.d.ts +0 -60
  192. package/dist/node_modules/@agentorg/sdk/dist/src/package.js +0 -538
  193. package/dist/node_modules/@agentorg/sdk/dist/src/tar.d.ts +0 -7
  194. package/dist/node_modules/@agentorg/sdk/dist/src/tar.js +0 -71
  195. package/dist/node_modules/@agentorg/shell/dist/agent-bindings.js +0 -59
  196. package/dist/node_modules/@agentorg/shell/src/agent-bindings.ts +0 -107
  197. package/dist/src/frames.d.ts +0 -23
  198. package/dist/src/frames.js +0 -78
  199. package/dist/src/layout.d.ts +0 -44
  200. package/dist/src/layout.js +0 -90
  201. package/dist/src/model.d.ts +0 -51
  202. package/dist/src/model.js +0 -230
  203. package/dist/src/rules.d.ts +0 -71
  204. package/dist/src/rules.js +0 -157
  205. package/dist/src/transfer.d.ts +0 -30
  206. package/dist/src/transfer.js +0 -110
@@ -1,293 +0,0 @@
1
- import { errorBodyOf, errorFromBody, OutcomeUnknown, Refused } from "./errors.js";
2
- import { FRAME_HELLO } from "./frame-protocol.js";
3
- const isRecord = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
4
- const isId = (value) => Number.isSafeInteger(value) && value > 0;
5
- const isName = (value) => typeof value === "string" && value.length <= 256;
6
- const LEVELS = new Set(["info", "warn", "error"]);
7
- /** A log line's text from a frame is cut to this. */
8
- const MAX_LOG = 4096;
9
- /** A log line's fields from a frame: this many keys at most, each value cut to this length. */
10
- const MAX_FIELDS = 16;
11
- const MAX_FIELD = 1024;
12
- /** Log lines a frame may send per window; the rest of the window is dropped, and that's said once. */
13
- const LOG_LINES = 50;
14
- const LOG_WINDOW_MS = 10_000;
15
- /** Live subscriptions one frame may hold. */
16
- const MAX_SUBSCRIPTIONS = 64;
17
- /** Calls on its slots one frame may have in flight; past it, `busy`. */
18
- const MAX_CALLS = 64;
19
- /**
20
- * A frame's log fields, rebuilt: a fresh object of at most `MAX_FIELDS` plain keys (no prototype
21
- * keys), each value a primitive or JSON text, cut to `MAX_FIELD`.
22
- */
23
- function fieldsOf(value) {
24
- if (!isRecord(value))
25
- return undefined;
26
- const fields = {};
27
- let kept = 0;
28
- for (const key of Object.keys(value)) {
29
- if (kept >= MAX_FIELDS)
30
- break;
31
- if (key === "__proto__" || key === "constructor" || key === "prototype" || key.length > 64)
32
- continue;
33
- const field = value[key];
34
- let text;
35
- if (field === null || typeof field === "number" || typeof field === "boolean")
36
- text = field;
37
- else if (typeof field === "string")
38
- text = field.slice(0, MAX_FIELD);
39
- else
40
- try {
41
- text = String(JSON.stringify(field)).slice(0, MAX_FIELD);
42
- }
43
- catch {
44
- text = "[unreadable]";
45
- }
46
- Object.defineProperty(fields, key, {
47
- value: text,
48
- enumerable: true,
49
- writable: true,
50
- configurable: true,
51
- });
52
- kept++;
53
- }
54
- return fields;
55
- }
56
- /** A frame's error body, read as no more than it claims: one of the three names, as text. */
57
- function portErrorOf(error) {
58
- const name = error.name === "NotConnected" || error.name === "OutcomeUnknown" ? error.name : "Refused";
59
- return {
60
- name,
61
- message: String(error.message ?? "").slice(0, MAX_LOG),
62
- ...(typeof error.code === "string" ? { code: error.code.slice(0, 64) } : {}),
63
- };
64
- }
65
- /** `ui.shell@1.openExternal`'s URL, if it is one the shell opens: https only. */
66
- function externalUrl(input) {
67
- const url = isRecord(input) ? input.url : undefined;
68
- if (typeof url !== "string" || url.length > 8192)
69
- throw new Refused("invalid", "openExternal takes {url}.");
70
- let parsed;
71
- try {
72
- parsed = new URL(url);
73
- }
74
- catch {
75
- throw new Refused("invalid", "openExternal: the URL doesn't parse.");
76
- }
77
- if (parsed.protocol !== "https:")
78
- throw new Refused("invalid", "openExternal opens https URLs only.");
79
- return parsed.href;
80
- }
81
- /**
82
- * Hand a frame its port: post the hello with the other end to the frame's window, which has an
83
- * opaque origin, so the target origin is `*`; the frame accepts it only from its parent.
84
- */
85
- export function openFrame(frameWindow) {
86
- const channel = new MessageChannel();
87
- frameWindow.postMessage(FRAME_HELLO, "*", [channel.port2]);
88
- return channel.port1;
89
- }
90
- /**
91
- * The shell's end of a view's frame. It drives `ui.view@1` (load, mount, resize, unmount), tells
92
- * the frame which slots are bound, and serves what the frame asks: calls and subscriptions on the
93
- * view's slots through `handlers`, and `ui.shell@1` itself. The frame isn't trusted: every message
94
- * is checked, and one that doesn't fit is dropped or refused.
95
- */
96
- export class FrameHost {
97
- #port;
98
- #handlers;
99
- #nextId = 0;
100
- #pending = new Map();
101
- /** Live subscriptions by id, and the id of each slot and event's one subscription. */
102
- #subscriptions = new Map();
103
- #byEvent = new Map();
104
- /** ui.shell@1 methods with a call in flight: one at a time each. */
105
- #shellBusy = new Set();
106
- #logWindow = { start: 0, lines: 0 };
107
- #calls = 0;
108
- #closed = false;
109
- /** The latest height not yet passed on, and the wait before the next may be. */
110
- constructor(port, handlers) {
111
- this.#port = port;
112
- this.#handlers = handlers;
113
- port.onmessage = (event) => this.#receive(event.data);
114
- }
115
- /** Import the view's bundle in the frame and run its `start`; `slots` says which are bound. */
116
- load(options) {
117
- return this.#request((id) => ({
118
- op: "load",
119
- id,
120
- src: options.src,
121
- self: { actorId: options.actorId, module: options.module },
122
- slots: options.slots,
123
- }));
124
- }
125
- /** A slot became bound or unbound; `bound` again for a bound slot means its provider came back. */
126
- slot(name, bound) {
127
- this.#post({ op: "slot", name, bound });
128
- }
129
- mount(theme, size) {
130
- return this.#request((id) => ({ op: "mount", id, theme, size }));
131
- }
132
- resize(size) {
133
- return this.#request((id) => ({ op: "resize", id, size }));
134
- }
135
- unmount() {
136
- return this.#request((id) => ({ op: "unmount", id }));
137
- }
138
- /** Stop: every subscription ends, and what waits on the frame fails. */
139
- close() {
140
- if (this.#closed)
141
- return;
142
- this.#closed = true;
143
- this.#port.onmessage = null;
144
- for (const { stop } of this.#subscriptions.values())
145
- stop();
146
- this.#subscriptions.clear();
147
- this.#byEvent.clear();
148
- for (const waiting of this.#pending.values())
149
- waiting.reject(new OutcomeUnknown("The frame closed."));
150
- this.#pending.clear();
151
- this.#port.close();
152
- }
153
- #post(message) {
154
- if (!this.#closed)
155
- this.#port.postMessage(message);
156
- }
157
- #request(make) {
158
- if (this.#closed)
159
- return Promise.reject(new OutcomeUnknown("The frame closed."));
160
- const id = ++this.#nextId;
161
- return new Promise((resolve, reject) => {
162
- this.#pending.set(id, { resolve, reject });
163
- this.#post(make(id));
164
- }).then(() => undefined);
165
- }
166
- async #answer(id, run) {
167
- try {
168
- this.#post({ op: "reply", id, result: (await run()) ?? {} });
169
- }
170
- catch (error) {
171
- this.#post({ op: "reply", id, error: errorBodyOf(error) });
172
- }
173
- }
174
- #receive(data) {
175
- if (!isRecord(data))
176
- return;
177
- const message = data;
178
- switch (message.op) {
179
- case "call":
180
- if (!isId(message.id))
181
- return;
182
- if (!isName(message.slot) || !isName(message.method))
183
- return void this.#answer(message.id, () => {
184
- throw new Refused("invalid", "A call names its slot and method.");
185
- });
186
- return void this.#answer(message.id, async () => {
187
- // One view can't flood the gateway and the Hub: past the cap, it waits its turn.
188
- if (this.#calls >= MAX_CALLS)
189
- throw new Refused("busy", `A view has ${MAX_CALLS} calls in flight at most.`);
190
- this.#calls++;
191
- try {
192
- return await this.#handlers.call(message.slot, message.method, message.input);
193
- }
194
- finally {
195
- this.#calls--;
196
- }
197
- });
198
- case "shell": {
199
- if (!isId(message.id))
200
- return;
201
- const { method, input } = message;
202
- return void this.#answer(message.id, async () => {
203
- if (method !== "openExternal" && method !== "signIn")
204
- throw new Refused("invalid", "ui.shell@1 has openExternal and signIn.");
205
- const url = method === "openExternal" ? externalUrl(input) : undefined;
206
- if (this.#shellBusy.has(method))
207
- throw new Refused("busy", `ui.shell@1.${method} is already running.`);
208
- this.#shellBusy.add(method);
209
- try {
210
- if (url !== undefined)
211
- await this.#handlers.shell.openExternal(url);
212
- else
213
- await this.#handlers.shell.signIn();
214
- }
215
- finally {
216
- this.#shellBusy.delete(method);
217
- }
218
- return {};
219
- });
220
- }
221
- case "subscribe": {
222
- const { sub, slot, event } = message;
223
- if (!isId(sub) || !isName(slot) || !isName(event) || this.#subscriptions.has(sub))
224
- return;
225
- const key = `${slot}\0${event}`;
226
- let ended = false;
227
- const end = () => {
228
- if (ended)
229
- return;
230
- ended = true;
231
- if (this.#subscriptions.get(sub)?.key === key) {
232
- this.#subscriptions.delete(sub);
233
- this.#byEvent.delete(key);
234
- }
235
- this.#post({ op: "end", sub });
236
- };
237
- // One subscription per slot and event, as the frame keeps; past the cap, none.
238
- if (this.#byEvent.has(key) || this.#subscriptions.size >= MAX_SUBSCRIPTIONS)
239
- return end();
240
- try {
241
- const stop = this.#handlers.subscribe(slot, event, (payload) => {
242
- if (!ended)
243
- this.#post({ op: "event", sub, payload });
244
- }, end);
245
- if (ended)
246
- stop();
247
- else {
248
- this.#subscriptions.set(sub, { key, stop });
249
- this.#byEvent.set(key, sub);
250
- }
251
- }
252
- catch {
253
- end();
254
- }
255
- return;
256
- }
257
- case "unsubscribe": {
258
- const live = isId(message.sub) ? this.#subscriptions.get(message.sub) : undefined;
259
- if (!live)
260
- return;
261
- this.#subscriptions.delete(message.sub);
262
- this.#byEvent.delete(live.key);
263
- live.stop();
264
- return;
265
- }
266
- case "log": {
267
- if (!LEVELS.has(message.level) || typeof message.message !== "string")
268
- return;
269
- const now = Date.now();
270
- if (now - this.#logWindow.start > LOG_WINDOW_MS)
271
- this.#logWindow = { start: now, lines: 0 };
272
- const lines = ++this.#logWindow.lines;
273
- if (lines > LOG_LINES + 1)
274
- return;
275
- if (lines === LOG_LINES + 1)
276
- return this.#handlers.log("warn", "The view logged too much; its lines are dropped for a while.");
277
- this.#handlers.log(message.level, message.message.slice(0, MAX_LOG), fieldsOf(message.fields));
278
- return;
279
- }
280
- case "reply": {
281
- const waiting = isId(message.id) ? this.#pending.get(message.id) : undefined;
282
- if (!waiting)
283
- return;
284
- this.#pending.delete(message.id);
285
- if (isRecord(message.error))
286
- waiting.reject(errorFromBody(portErrorOf(message.error), "view"));
287
- else
288
- waiting.resolve(message.result);
289
- return;
290
- }
291
- }
292
- }
293
- }
@@ -1,124 +0,0 @@
1
- import type { ErrorBody } from "./errors.js";
2
- import type { Interface } from "./interface.js";
3
- /**
4
- * The messages between a shell and a view's sandboxed frame, over the one `MessagePort` the shell
5
- * hands the frame. The frame has no network and no shell state: everything it reaches goes
6
- * through these. The shell is trusted by the frame; the frame is not trusted by the shell, so
7
- * `FrameHost` checks every message it gets.
8
- */
9
- /** What the shell posts to the frame's window, with the port, to start. */
10
- export declare const FRAME_HELLO = "frame/1";
11
- /** From the shell to the frame. */
12
- export type ToFrame =
13
- /** Import the view's bundle, run its `start`, then answer. `slots` says which are bound now. */
14
- {
15
- op: "load";
16
- id: number;
17
- src: string;
18
- self: {
19
- actorId: string;
20
- module: string;
21
- };
22
- slots: Record<string, boolean>;
23
- }
24
- /** A slot became bound or unbound; `bound` again for a bound slot means its provider came back. */
25
- | {
26
- op: "slot";
27
- name: string;
28
- bound: boolean;
29
- } | {
30
- op: "mount";
31
- id: number;
32
- theme: Record<string, string>;
33
- size: {
34
- width: number;
35
- height: number;
36
- };
37
- } | {
38
- op: "resize";
39
- id: number;
40
- size: {
41
- width: number;
42
- height: number;
43
- };
44
- } | {
45
- op: "unmount";
46
- id: number;
47
- }
48
- /** An event of a subscription the frame opened. */
49
- | {
50
- op: "event";
51
- sub: number;
52
- payload: unknown;
53
- }
54
- /** A subscription ended from the shell's side: an unbind, or its provider gone. */
55
- | {
56
- op: "end";
57
- sub: number;
58
- }
59
- /** The answer to one of the frame's requests. */
60
- | {
61
- op: "reply";
62
- id: number;
63
- result: unknown;
64
- } | {
65
- op: "reply";
66
- id: number;
67
- error: ErrorBody;
68
- };
69
- /** From the frame to the shell. */
70
- export type FromFrame =
71
- /** A call on one of the view's slots, run as the view's Actor. */
72
- {
73
- op: "call";
74
- id: number;
75
- slot: string;
76
- method: string;
77
- input: unknown;
78
- } | {
79
- op: "subscribe";
80
- sub: number;
81
- slot: string;
82
- event: string;
83
- } | {
84
- op: "unsubscribe";
85
- sub: number;
86
- }
87
- /** A call on `ui.shell@1`, which the shell itself serves, with no binding. */
88
- | {
89
- op: "shell";
90
- id: number;
91
- method: string;
92
- input: unknown;
93
- }
94
- /** A line for the view's Actor's log: the view's own, or an error it didn't catch. */
95
- | {
96
- op: "log";
97
- level: "info" | "warn" | "error";
98
- message: string;
99
- fields?: Record<string, unknown>;
100
- }
101
- /** The answer to one of the shell's requests. */
102
- | {
103
- op: "reply";
104
- id: number;
105
- result: unknown;
106
- } | {
107
- op: "reply";
108
- id: number;
109
- error: ErrorBody;
110
- };
111
- /** A port both sides speak on: the browser's `MessagePort`, or Node's in tests. */
112
- export interface PortLike {
113
- postMessage(message: unknown): void;
114
- onmessage: ((event: {
115
- data: unknown;
116
- }) => void) | null;
117
- close(): void;
118
- }
119
- /**
120
- * Whether an interface is `ui.shell@1`: the shell's own in-frame interface, the mirror of
121
- * `ui.view@1`. The frame runtime binds a slot of it to the shell itself, so it is always bound and
122
- * no binding in the Hub ever names it.
123
- */
124
- export declare function isShellInterface(iface: Interface): boolean;
@@ -1,16 +0,0 @@
1
- /**
2
- * The messages between a shell and a view's sandboxed frame, over the one `MessagePort` the shell
3
- * hands the frame. The frame has no network and no shell state: everything it reaches goes
4
- * through these. The shell is trusted by the frame; the frame is not trusted by the shell, so
5
- * `FrameHost` checks every message it gets.
6
- */
7
- /** What the shell posts to the frame's window, with the port, to start. */
8
- export const FRAME_HELLO = "frame/1";
9
- /**
10
- * Whether an interface is `ui.shell@1`: the shell's own in-frame interface, the mirror of
11
- * `ui.view@1`. The frame runtime binds a slot of it to the shell itself, so it is always bound and
12
- * no binding in the Hub ever names it.
13
- */
14
- export function isShellInterface(iface) {
15
- return iface.kind === "rpc" && iface.name === "ui.shell" && iface.major === 1;
16
- }
@@ -1,25 +0,0 @@
1
- import { type PortLike } from "./frame-protocol.js";
2
- import { type FrameRoot } from "./module.js";
3
- export interface FrameOptions {
4
- /** The port the shell handed this frame. */
5
- port: PortLike;
6
- /** The element the view renders into. */
7
- root: FrameRoot;
8
- /** Import a view's bundle, as `import(src)` does in a browser. */
9
- load(src: string): Promise<unknown>;
10
- /** Set the shell's theme tokens, CSS variables by name, on the frame's document. */
11
- theme?(tokens: Record<string, string>): void;
12
- }
13
- /** A running frame: report what the view didn't catch, or stop. */
14
- export interface Frame {
15
- /** An error the view didn't catch, for its Actor's log. */
16
- failed(error: unknown): void;
17
- close(): void;
18
- }
19
- /**
20
- * Run a view module in a frame, on the same runtime as a host module, so a view behaves as it does
21
- * in tests. Every slot of `ui.shell@1` is served by the shell and always bound; every other slot is
22
- * served over the port while the shell says it is bound. The shell drives `ui.view@1`: `mount`,
23
- * `resize` and `unmount` reach the view's handle. Only `ui.view@1` is served among its exports.
24
- */
25
- export declare function runFrame(options: FrameOptions): Frame;