@nyte-ai/host 0.0.0-stage → 0.0.19

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 (250) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +6 -2
  3. package/THIRD-PARTY-NOTICES.md +866 -0
  4. package/dist/chunks/canvas-preview-0cmbkp2j.js +65 -0
  5. package/dist/chunks/canvas-preview-0nq75h9k.js +7 -0
  6. package/dist/chunks/canvas-preview-11nadadc.js +241 -0
  7. package/dist/chunks/canvas-preview-1sq7z9jq.js +17 -0
  8. package/dist/chunks/canvas-preview-2jwbe14k.js +130 -0
  9. package/dist/chunks/canvas-preview-2zdyh9ny.js +26 -0
  10. package/dist/chunks/canvas-preview-31w7xm8z.js +34 -0
  11. package/dist/chunks/canvas-preview-4b6sayhg.js +295 -0
  12. package/dist/chunks/canvas-preview-4e0pxa32.js +63 -0
  13. package/dist/chunks/canvas-preview-518dh8by.js +169 -0
  14. package/dist/chunks/canvas-preview-55s0y68g.js +387 -0
  15. package/dist/chunks/canvas-preview-5ekjx6sw.js +613 -0
  16. package/dist/chunks/canvas-preview-5mex725c.js +14 -0
  17. package/dist/chunks/canvas-preview-5z63am63.js +295 -0
  18. package/dist/chunks/canvas-preview-65xqjw7f.js +20 -0
  19. package/dist/chunks/canvas-preview-69fkk9yn.js +95 -0
  20. package/dist/chunks/canvas-preview-8xc0vn7v.js +458 -0
  21. package/dist/chunks/canvas-preview-9aqczacs.js +73 -0
  22. package/dist/chunks/canvas-preview-af86jz8t.js +141 -0
  23. package/dist/chunks/canvas-preview-aq5j4p25.js +240 -0
  24. package/dist/chunks/canvas-preview-b2v8wfnz.js +36 -0
  25. package/dist/chunks/canvas-preview-befkjea2.js +60 -0
  26. package/dist/chunks/canvas-preview-bxnqt8cd.js +103 -0
  27. package/dist/chunks/canvas-preview-cawy6vbz.js +352 -0
  28. package/dist/chunks/canvas-preview-crmzxxt8.js +518 -0
  29. package/dist/chunks/canvas-preview-drctegz4.js +591 -0
  30. package/dist/chunks/canvas-preview-e3vnkkc5.js +134 -0
  31. package/dist/chunks/canvas-preview-ec2mt7es.js +108 -0
  32. package/dist/chunks/canvas-preview-fxa0rfb0.js +61 -0
  33. package/dist/chunks/canvas-preview-g5fkjs34.js +598 -0
  34. package/dist/chunks/canvas-preview-jeddjex8.js +166 -0
  35. package/dist/chunks/canvas-preview-k1yg4fgp.js +571 -0
  36. package/dist/chunks/canvas-preview-kdqgrstc.js +194 -0
  37. package/dist/chunks/canvas-preview-kn7kvbfj.js +12 -0
  38. package/dist/chunks/canvas-preview-kp75gb63.js +141 -0
  39. package/dist/chunks/canvas-preview-mgcx2b2p.js +271 -0
  40. package/dist/chunks/canvas-preview-n2a35zv5.js +81 -0
  41. package/dist/chunks/canvas-preview-pkzqg96v.js +63 -0
  42. package/dist/chunks/canvas-preview-rp4x6y1h.js +144 -0
  43. package/dist/chunks/canvas-preview-s94ebwf5.js +112 -0
  44. package/dist/chunks/canvas-preview-skzwvw4d.js +23 -0
  45. package/dist/chunks/canvas-preview-srgfpdmf.js +60 -0
  46. package/dist/chunks/canvas-preview-tjhabynd.js +252 -0
  47. package/dist/chunks/canvas-preview-tnqe7k0b.js +374 -0
  48. package/dist/chunks/canvas-preview-vaatfznm.js +189 -0
  49. package/dist/chunks/canvas-preview-vm9zdp2t.js +398 -0
  50. package/dist/chunks/canvas-preview-w54ymtb3.js +272 -0
  51. package/dist/chunks/canvas-preview-wg80rbc2.js +46 -0
  52. package/dist/chunks/canvas-preview-y5y6ghaw.js +125 -0
  53. package/dist/chunks/canvas-preview-ycepb05s.js +176 -0
  54. package/dist/chunks/canvas-preview-yjz0f7n4.js +50 -0
  55. package/dist/chunks/canvas-preview-zaaj99wd.js +244 -0
  56. package/dist/src/canvas-preview.d.ts +29 -0
  57. package/dist/src/canvas-preview.js +271 -0
  58. package/dist/src/credentials.d.ts +44 -0
  59. package/dist/src/credentials.js +10 -0
  60. package/dist/src/deploy/cloudflare/worker.d.ts +50 -0
  61. package/dist/src/deploy/cloudflare/worker.js +196 -0
  62. package/dist/src/deploy/vercel/nitro.config.d.ts +2 -0
  63. package/dist/src/deploy/vercel/nitro.config.js +30 -0
  64. package/dist/src/deploy/vercel/server/routes/[...].d.ts +2 -0
  65. package/dist/src/deploy/vercel/server/routes/[...].js +18 -0
  66. package/dist/src/deploy/vercel/server/utils/host.d.ts +13 -0
  67. package/dist/src/deploy/vercel/server/utils/host.js +7 -0
  68. package/dist/src/deploy/vercel/server/utils/repair.d.ts +1 -0
  69. package/dist/src/deploy/vercel/server/utils/repair.js +6 -0
  70. package/dist/src/deploy/vercel/server/utils/step.d.ts +10 -0
  71. package/dist/src/deploy/vercel/server/utils/step.js +6 -0
  72. package/dist/src/deploy/vercel/server/workflows/drive.d.ts +14 -0
  73. package/dist/src/deploy/vercel/server/workflows/drive.js +13 -0
  74. package/dist/src/durable-object.d.ts +14 -0
  75. package/dist/src/durable-object.js +231 -0
  76. package/dist/src/hash.d.ts +1 -0
  77. package/dist/src/hash.js +6 -0
  78. package/dist/src/identity-key.d.ts +11 -0
  79. package/dist/src/identity-key.js +9 -0
  80. package/dist/src/identity.d.ts +12 -0
  81. package/dist/src/identity.js +6 -0
  82. package/dist/src/images/exif.d.ts +1 -0
  83. package/dist/src/images/exif.js +6 -0
  84. package/dist/src/images/node.d.ts +37 -0
  85. package/dist/src/images/node.js +12 -0
  86. package/dist/src/images/photon.d.ts +3 -0
  87. package/dist/src/images/photon.js +7 -0
  88. package/dist/src/images/worker.d.ts +1 -0
  89. package/dist/src/images/worker.js +23 -0
  90. package/dist/src/index.d.ts +20 -0
  91. package/dist/src/index.js +121 -0
  92. package/dist/src/key-file.d.ts +1 -0
  93. package/dist/src/key-file.js +7 -0
  94. package/dist/src/listen-fetch.d.ts +8 -0
  95. package/dist/src/listen-fetch.js +9 -0
  96. package/dist/src/listen.d.ts +4 -0
  97. package/dist/src/listen.js +10 -0
  98. package/dist/src/local/command.d.ts +20 -0
  99. package/dist/src/local/command.js +12 -0
  100. package/dist/src/local/feed.d.ts +7 -0
  101. package/dist/src/local/feed.js +6 -0
  102. package/dist/src/local/files.d.ts +18 -0
  103. package/dist/src/local/files.js +13 -0
  104. package/dist/src/local/git-watch.d.ts +15 -0
  105. package/dist/src/local/git-watch.js +7 -0
  106. package/dist/src/local/git.d.ts +5 -0
  107. package/dist/src/local/git.js +13 -0
  108. package/dist/src/local/github.d.ts +30 -0
  109. package/dist/src/local/github.js +9 -0
  110. package/dist/src/local/identity.d.ts +3 -0
  111. package/dist/src/local/identity.js +7 -0
  112. package/dist/src/local/jobs.d.ts +7 -0
  113. package/dist/src/local/jobs.js +7 -0
  114. package/dist/src/local/legacy-trust.d.ts +10 -0
  115. package/dist/src/local/legacy-trust.js +6 -0
  116. package/dist/src/local/patch.d.ts +16 -0
  117. package/dist/src/local/patch.js +17 -0
  118. package/dist/src/local/provider.d.ts +11 -0
  119. package/dist/src/local/provider.js +20 -0
  120. package/dist/src/local/snapshots.d.ts +7 -0
  121. package/dist/src/local/snapshots.js +11 -0
  122. package/dist/src/local/text.d.ts +1 -0
  123. package/dist/src/local/text.js +6 -0
  124. package/dist/src/local/tree.d.ts +50 -0
  125. package/dist/src/local/tree.js +16 -0
  126. package/dist/src/local/workspace-migration.d.ts +2 -0
  127. package/dist/src/local/workspace-migration.js +7 -0
  128. package/dist/src/local/worktrees.d.ts +46 -0
  129. package/dist/src/local/worktrees.js +14 -0
  130. package/dist/src/mcp/config.d.ts +75 -0
  131. package/dist/src/mcp/config.js +12 -0
  132. package/dist/src/mcp/connect.d.ts +15 -0
  133. package/dist/src/mcp/connect.js +9 -0
  134. package/dist/src/mcp/hub.d.ts +19 -0
  135. package/dist/src/mcp/hub.js +11 -0
  136. package/dist/src/mcp/oauth.d.ts +46 -0
  137. package/dist/src/mcp/oauth.js +12 -0
  138. package/dist/src/mcp/tools.d.ts +29 -0
  139. package/dist/src/mcp/tools.js +12 -0
  140. package/dist/src/open.d.ts +14 -0
  141. package/dist/src/open.js +46 -0
  142. package/dist/src/plugins/loader.d.ts +21 -0
  143. package/dist/src/plugins/loader.js +11 -0
  144. package/dist/src/postgres.d.ts +19 -0
  145. package/dist/src/postgres.js +8 -0
  146. package/dist/src/providers.d.ts +16 -0
  147. package/dist/src/providers.js +112 -0
  148. package/dist/src/secrets.d.ts +12 -0
  149. package/dist/src/secrets.js +8 -0
  150. package/dist/src/serve/binding.d.ts +39 -0
  151. package/dist/src/serve/binding.js +6 -0
  152. package/dist/src/serve/handler.d.ts +15 -0
  153. package/dist/src/serve/handler.js +8 -0
  154. package/dist/src/serve/stream-server.d.ts +6 -0
  155. package/dist/src/serve/stream-server.js +6 -0
  156. package/dist/src/session-index.d.ts +11 -0
  157. package/dist/src/session-index.js +11 -0
  158. package/dist/src/shell-env.d.ts +14 -0
  159. package/dist/src/shell-env.js +108 -0
  160. package/dist/src/sqlite-workers.d.ts +6 -0
  161. package/dist/src/sqlite-workers.js +6 -0
  162. package/dist/src/sqlite.d.ts +8 -0
  163. package/dist/src/sqlite.js +11 -0
  164. package/dist/src/store-rows.d.ts +13 -0
  165. package/dist/src/store-rows.js +26 -0
  166. package/dist/src/store-usage.d.ts +25 -0
  167. package/dist/src/store-usage.js +19 -0
  168. package/dist/src/usage-history.d.ts +54 -0
  169. package/dist/src/usage-history.js +14 -0
  170. package/dist/src/usage-scan.d.ts +24 -0
  171. package/dist/src/usage-scan.js +65 -0
  172. package/dist/src/usage-service.d.ts +23 -0
  173. package/dist/src/usage-service.js +16 -0
  174. package/dist/src/usage-summary.d.ts +9 -0
  175. package/dist/src/usage-summary.js +16 -0
  176. package/dist/src/usage-types.d.ts +86 -0
  177. package/dist/src/usage-types.js +0 -0
  178. package/dist/src/usage-worker.d.ts +1 -0
  179. package/dist/src/usage-worker.js +29 -0
  180. package/dist/src/usage.d.ts +195 -0
  181. package/dist/src/usage.js +23 -0
  182. package/package.json +140 -3
  183. package/src/canvas-preview.ts +403 -0
  184. package/src/credentials.ts +294 -0
  185. package/src/deploy/cloudflare/README.md +3 -0
  186. package/src/deploy/cloudflare/worker.ts +306 -0
  187. package/src/deploy/cloudflare/wrangler.jsonc +12 -0
  188. package/src/deploy/vercel/README.md +3 -0
  189. package/src/deploy/vercel/nitro.config.ts +29 -0
  190. package/src/deploy/vercel/server/routes/[...].ts +10 -0
  191. package/src/deploy/vercel/server/utils/host.ts +104 -0
  192. package/src/deploy/vercel/server/utils/repair.ts +35 -0
  193. package/src/deploy/vercel/server/utils/step.ts +25 -0
  194. package/src/deploy/vercel/server/workflows/drive.ts +46 -0
  195. package/src/deploy/vercel/tsconfig.json +9 -0
  196. package/src/deploy/vercel/types/bun.d.ts +2 -0
  197. package/src/durable-object.ts +386 -0
  198. package/src/hash.ts +5 -0
  199. package/src/identity-key.ts +20 -0
  200. package/src/identity.ts +66 -0
  201. package/src/images/exif.ts +69 -0
  202. package/src/images/node.ts +163 -0
  203. package/src/images/photon.ts +141 -0
  204. package/src/images/worker.ts +29 -0
  205. package/src/index.ts +39 -0
  206. package/src/key-file.ts +53 -0
  207. package/src/listen-fetch.ts +409 -0
  208. package/src/listen.ts +228 -0
  209. package/src/local/command.ts +161 -0
  210. package/src/local/feed.ts +70 -0
  211. package/src/local/files.ts +817 -0
  212. package/src/local/git-watch.ts +199 -0
  213. package/src/local/git.ts +919 -0
  214. package/src/local/github.ts +691 -0
  215. package/src/local/identity.ts +21 -0
  216. package/src/local/jobs.ts +573 -0
  217. package/src/local/legacy-trust.ts +105 -0
  218. package/src/local/patch.ts +334 -0
  219. package/src/local/provider.ts +184 -0
  220. package/src/local/snapshots.ts +778 -0
  221. package/src/local/text.ts +9 -0
  222. package/src/local/tree.ts +398 -0
  223. package/src/local/workspace-migration.ts +95 -0
  224. package/src/local/worktrees.ts +239 -0
  225. package/src/mcp/config.ts +330 -0
  226. package/src/mcp/connect.ts +92 -0
  227. package/src/mcp/hub.ts +836 -0
  228. package/src/mcp/oauth.ts +436 -0
  229. package/src/mcp/tools.ts +333 -0
  230. package/src/open.ts +449 -0
  231. package/src/plugins/loader.ts +186 -0
  232. package/src/postgres.ts +492 -0
  233. package/src/providers.ts +135 -0
  234. package/src/secrets.ts +31 -0
  235. package/src/serve/binding.ts +629 -0
  236. package/src/serve/handler.ts +362 -0
  237. package/src/serve/stream-server.ts +176 -0
  238. package/src/session-index.ts +99 -0
  239. package/src/shell-env.ts +153 -0
  240. package/src/sqlite-workers.ts +283 -0
  241. package/src/sqlite.ts +553 -0
  242. package/src/store-rows.ts +110 -0
  243. package/src/store-usage.ts +347 -0
  244. package/src/usage-history.ts +219 -0
  245. package/src/usage-scan.ts +81 -0
  246. package/src/usage-service.ts +82 -0
  247. package/src/usage-summary.ts +79 -0
  248. package/src/usage-types.ts +87 -0
  249. package/src/usage-worker.ts +24 -0
  250. package/src/usage.ts +776 -0
@@ -0,0 +1,387 @@
1
+ // src/serve/binding.ts
2
+ import { isJson, SdkError } from "@nyte-ai/schema";
3
+ import { mapped, viewFrames } from "@nyte-ai/protocol";
4
+ var LOGIN_LIFETIME = 10 * 60 * 1000;
5
+ var attempts = new WeakMap;
6
+ var holds = new WeakMap;
7
+ function bindConnection2(connection, options) {
8
+ const { wire } = options.server;
9
+ const { principal } = connection;
10
+ const logins = attempts.get(options.server) ?? new Map;
11
+ attempts.set(options.server, logins);
12
+ const pages = holds.get(options.server) ?? new Map;
13
+ holds.set(options.server, pages);
14
+ const session = (id) => connection.session(id);
15
+ const workspace = (id) => connection.workspace(id);
16
+ const files = async (id) => available(await workspace(id).files(), "files");
17
+ const git = async (id) => available(await workspace(id).git(), "git");
18
+ const github = async (id) => available(await workspace(id).github(), "github");
19
+ const browser = async (id) => available(await workspace(id).browser(), "browser");
20
+ const findLogin = (provider) => logins.get(principalKey(principal, provider)) ?? fail("unknown", `No login for ${provider} is in progress.`);
21
+ const endLogin = (provider, attempt) => {
22
+ clearTimeout(attempt.timer);
23
+ const key = principalKey(principal, provider);
24
+ if (logins.get(key) === attempt)
25
+ logins.delete(key);
26
+ attempt.steps?.return?.();
27
+ };
28
+ const hold = (page) => {
29
+ const key = principalKey(principal, page.id);
30
+ const known = pages.get(key) ?? [];
31
+ pages.set(key, known.includes(page) ? known : [...known, page]);
32
+ return page;
33
+ };
34
+ const release = async (page) => {
35
+ const key = principalKey(principal, page);
36
+ const [last, ...rest] = [...pages.get(key) ?? []].reverse();
37
+ if (rest.length === 0)
38
+ pages.delete(key);
39
+ else
40
+ pages.set(key, rest.reverse());
41
+ await last?.close();
42
+ };
43
+ const findPage = async (at, id) => pages.get(principalKey(principal, id))?.[0] ?? wire.page(id) ?? hold(await (await browser(at)).adopt(id));
44
+ const describePage = async (page) => {
45
+ const view = await page.view();
46
+ return { id: page.id, url: view.url, title: view.title, holders: wire.holders(page.id) };
47
+ };
48
+ const findReceipt = (at, id) => at === null ? wire.batchReceipt(id) : session(at).receipt(id);
49
+ const startedJob = (job) => ({
50
+ ...wire.job(job.id) ?? fail("unknown", `Job ${job.id} is gone.`),
51
+ key: job.key
52
+ });
53
+ return {
54
+ "connection.watch": async () => mapped(connection.watch(), (view) => hostWire(view, principal)),
55
+ "connection.blob": async ({ id }) => ({ reader: await connection.blob(id) }),
56
+ "models.list": () => connection.models.list(),
57
+ "models.accounts": ({ provider }) => connection.models.accounts(provider),
58
+ "models.selectAccount": async ({ provider, account }) => {
59
+ await connection.models.selectAccount(provider, account);
60
+ return null;
61
+ },
62
+ "models.usage": () => connection.models.usage(),
63
+ "models.refresh": async ({ provider }) => {
64
+ await connection.models.refresh(provider);
65
+ return null;
66
+ },
67
+ "models.setDefault": ({ ref }) => connection.models.setDefault(ref),
68
+ "models.setListing": ({ refs, listing }) => connection.models.setListing(refs, listing),
69
+ "models.verify": ({ provider }) => connection.models.verify(provider),
70
+ "models.login": async (input) => {
71
+ const { provider } = input;
72
+ const login = connection.models.login(input);
73
+ const key = principalKey(principal, provider);
74
+ const previous = logins.get(key);
75
+ if (previous !== undefined)
76
+ endLogin(provider, previous);
77
+ const attempt = {
78
+ login,
79
+ steps: null,
80
+ timer: setTimeout(() => endLogin(provider, attempt), LOGIN_LIFETIME)
81
+ };
82
+ logins.set(key, attempt);
83
+ return null;
84
+ },
85
+ "models.logout": async ({ provider }) => {
86
+ await connection.models.logout(provider);
87
+ return null;
88
+ },
89
+ "login.steps": async ({ provider }) => {
90
+ const attempt = findLogin(provider);
91
+ if (attempt.steps !== null)
92
+ fail("invalid", `The login for ${provider} is already streaming.`);
93
+ const steps = attempt.login.steps()[Symbol.asyncIterator]();
94
+ attempt.steps = steps;
95
+ return {
96
+ [Symbol.asyncIterator]: () => ({
97
+ async next() {
98
+ const next = await steps.next();
99
+ if (next.done === true || next.value.kind === "done" || next.value.kind === "failed")
100
+ endLogin(provider, attempt);
101
+ return next;
102
+ },
103
+ async return() {
104
+ endLogin(provider, attempt);
105
+ return { done: true, value: undefined };
106
+ }
107
+ })
108
+ };
109
+ },
110
+ "login.answer": async ({ provider, code }) => {
111
+ await findLogin(provider).login.answer(code);
112
+ return null;
113
+ },
114
+ "settings.apply": ({ id, value }) => connection.settings.apply(id, value),
115
+ "workspaces.list": () => connection.workspaces.list(),
116
+ "workspaces.inspect": ({ target }) => connection.workspaces.inspect(target),
117
+ "workspaces.add": ({ target, options }) => connection.workspaces.add(target, options),
118
+ "workspaces.forget": ({ id }) => connection.workspaces.forget(id),
119
+ "sessions.watch": async (input) => connection.sessions.watch(input),
120
+ "sessions.start": async (input) => {
121
+ const { session, receipt } = await connection.sessions.start(input);
122
+ return { session: session.id, receipt: receipt.id, message: receipt.message };
123
+ },
124
+ "sessions.list": (input) => connection.sessions.list(input),
125
+ "session.info": ({ session: id }) => session(id).info(),
126
+ "session.update": ({ session: id, patch }) => session(id).update(patch),
127
+ "session.remove": ({ session: id }) => session(id).remove(),
128
+ "session.stop": ({ session: id }) => session(id).stop(),
129
+ "session.command": ({ session: id, name, text }) => session(id).command(name, text),
130
+ "session.completeCommand": ({ session: id, name, prefix }) => session(id).completeCommand(name, prefix),
131
+ "head.send": async ({ session: id, head, content, options }) => {
132
+ const receipt = await session(id).head(head).send(content, options);
133
+ return { receipt: receipt.id, message: receipt.message };
134
+ },
135
+ "head.configure": ({ session: id, head, patch }) => session(id).head(head).configure(patch),
136
+ "head.view": ({ session: id, head, options }) => session(id).head(head).view(options),
137
+ "head.watch": async ({ session: id, head, options }) => {
138
+ await session(id).info();
139
+ return viewFrames(session(id).head(head).watch(options));
140
+ },
141
+ "head.history": ({ session: id, head, before, limit }) => session(id).head(head).history({ before, limit }),
142
+ "head.tree": ({ session: id, head }) => session(id).head(head).tree(),
143
+ "head.move": async ({ session: id, head, position, options, id: receipt }) => ({
144
+ receipt: (await wire.move(id, head, position, { ...options, id: receipt })).id
145
+ }),
146
+ "head.compact": ({ session: id, head, instructions }) => session(id).head(head).compact({ instructions }),
147
+ "heads.create": ({ session: id, name, input }) => session(id).heads.create(name, input),
148
+ "heads.merge": ({ session: id, name, options }) => session(id).heads.merge(name, options),
149
+ "heads.remove": ({ session: id, name, options }) => session(id).heads.remove(name, options),
150
+ "run.stop": ({ session: id, run }) => session(id).run(run).stop(),
151
+ "run.settled": ({ session: id, run }, { signal }) => session(id).run(run).settled({ signal }),
152
+ "receipt.status": async ({ session: id, receipt }) => {
153
+ const status = await findReceipt(id, receipt)?.status() ?? { kind: "missing" };
154
+ return status.kind === "settled" ? { kind: "settled", outcome: toJson(status.outcome) } : status;
155
+ },
156
+ "receipt.settled": async ({ session: id, receipt }, { signal }) => {
157
+ const handle = findReceipt(id, receipt) ?? fail("unknown", `No receipt has the id ${receipt}.`);
158
+ return toJson(await handle.settled({ signal }));
159
+ },
160
+ "receipt.stop": async ({ session: id, receipt }) => {
161
+ const stopped = await findReceipt(id, receipt)?.stop() ?? { kind: "missing" };
162
+ return stopped.kind === "settled" ? { kind: "settled", outcome: toJson(stopped.outcome) } : stopped;
163
+ },
164
+ "receipt.landed": async ({ session: id, receipt }) => wire.sendReceipt(id ?? fail("invalid", "Only a send's receipt has landed."), receipt).landed(),
165
+ "parts.create": async ({ session: id, kind, input }) => {
166
+ const definition = await wire.partDefinition(id, kind) ?? fail("unknown", `No part definition has the kind ${kind}.`);
167
+ return { part: (await session(id).parts.create(definition, input)).id };
168
+ },
169
+ "part.update": ({ session: id, part, next, options }) => session(id).part(part).update(next, options),
170
+ "part.act": async ({ session: id, part, action, input }) => {
171
+ const outcome = await session(id).part(part).act(action, input);
172
+ return outcome.kind === "ok" ? { kind: "ok", output: toJson(outcome.output) } : outcome;
173
+ },
174
+ "part.answer": async ({ session: id, part }, { signal }) => {
175
+ const answer = await session(id).part(part).answer({ signal });
176
+ return answer === null ? null : { action: String(answer.action), input: toJson(answer.input) };
177
+ },
178
+ "part.close": async ({ session: id, part }) => {
179
+ await session(id).part(part).close();
180
+ return null;
181
+ },
182
+ "workspace.info": ({ workspace: id }) => workspace(id).info(),
183
+ "workspace.catalog": ({ workspace: id }) => workspace(id).catalog(),
184
+ "workspace.command": ({ workspace: id, name, text }) => workspace(id).command(name, text),
185
+ "workspace.completeCommand": ({ workspace: id, name, prefix }) => workspace(id).completeCommand(name, prefix),
186
+ "workspace.watch": async ({ workspace: id }) => workspace(id).watch(),
187
+ "mcp.watch": async ({ workspace: id }) => workspace(id).mcp.watch(),
188
+ "mcp.reconnect": ({ workspace: id, server }) => workspace(id).mcp.reconnect(server),
189
+ "mcp.setEnabled": ({ workspace: id, server, enabled }) => workspace(id).mcp.setEnabled(server, enabled),
190
+ "mcp.login": async ({ workspace: id, server }) => {
191
+ const login = JSON.stringify(["mcp", id, server]);
192
+ const previous = logins.get(principalKey(principal, login));
193
+ if (previous !== undefined)
194
+ endLogin(login, previous);
195
+ const attempt = {
196
+ login: workspace(id).mcp.login(server),
197
+ steps: null,
198
+ timer: setTimeout(() => endLogin(login, attempt), LOGIN_LIFETIME)
199
+ };
200
+ logins.set(principalKey(principal, login), attempt);
201
+ return { login };
202
+ },
203
+ "mcp.logout": ({ workspace: id, server }) => workspace(id).mcp.logout(server),
204
+ "mcp.add": ({ workspace: id, server, config, scope }) => workspace(id).mcp.add(server, config, scope),
205
+ "mcp.remove": ({ workspace: id, server, scope }) => workspace(id).mcp.remove(server, scope),
206
+ "workspace.update": ({ workspace: id, patch }) => workspace(id).update(patch),
207
+ "workspace.reload": ({ workspace: id }) => workspace(id).reload(),
208
+ "workspace.changes": ({ workspace: id, source }) => workspace(id).changes(source),
209
+ "workspace.patches": async ({ workspace: id, source, paths, options }) => mapped(workspace(id).patches(source, paths, options), patchesWire),
210
+ "workspace.files": async ({ workspace: id }) => capabilityWire(await workspace(id).files()),
211
+ "workspace.git": async ({ workspace: id }) => capabilityWire(await workspace(id).git()),
212
+ "workspace.github": async ({ workspace: id }) => capabilityWire(await workspace(id).github()),
213
+ "workspace.browser": async ({ workspace: id }) => capabilityWire(await workspace(id).browser()),
214
+ "jobs.start": async ({ workspace: id, command, session, title, pty, id: job }) => startedJob(await workspace(id).jobs.start({ command, session, title, pty, id: job })),
215
+ "jobs.recover": async ({ workspace: id, key }) => {
216
+ const recovered = await workspace(id).jobs.recover(key);
217
+ return "id" in recovered ? startedJob(recovered) : recovered;
218
+ },
219
+ "jobs.list": ({ workspace: id }) => workspace(id).jobs.list(),
220
+ "jobs.watch": async ({ workspace: id }) => jobChanges(workspace(id).jobs.watch()),
221
+ "job.write": async ({ workspace: id, job }, { body }) => {
222
+ await workspace(id).job(job).write(await readBytes(body));
223
+ return null;
224
+ },
225
+ "job.output": async ({ workspace: id, job }) => workspace(id).job(job).output(),
226
+ "job.resize": ({ workspace: id, job, size }) => workspace(id).job(job).resize(size),
227
+ "job.idle": ({ workspace: id, job }) => workspace(id).job(job).idle(),
228
+ "job.stop": ({ workspace: id, job }) => workspace(id).job(job).stop(),
229
+ "job.settled": ({ workspace: id, job }, { signal }) => workspace(id).job(job).settled({ signal }),
230
+ "files.read": async ({ workspace: id, path }) => (await files(id)).read(path),
231
+ "files.bytes": async ({ workspace: id, path }) => {
232
+ const reader = await (await files(id)).bytes(path);
233
+ const version = "version" in reader && typeof reader.version === "string" ? reader.version : null;
234
+ return version === null ? { reader } : { reader, headers: { "X-Nyte-Version": version } };
235
+ },
236
+ "files.stat": async ({ workspace: id, path }) => (await files(id)).stat(path),
237
+ "files.write": async ({ workspace: id, path, options }, { body }) => (await files(id)).write(path, await readBytes(body), options),
238
+ "files.remove": async ({ workspace: id, path, options }) => (await files(id)).remove(path, options),
239
+ "files.batch": async ({ workspace: id, entries, id: receipt }) => ({
240
+ receipt: (await wire.batch(id, await files(id), entries, receipt)).id
241
+ }),
242
+ "files.format": async ({ workspace: id, path, contents }) => (await files(id)).format(path, contents),
243
+ "files.scan": async ({ workspace: id, directory, ignored }) => (await files(id)).scan({ directory, ignored }),
244
+ "files.find": async ({ workspace: id, query, limit }) => (await files(id)).find({ query, limit }),
245
+ "files.search": async ({ workspace: id, ...input }) => (await files(id)).search(input),
246
+ "files.watch": async ({ workspace: id, paths }) => (await files(id)).watch({ paths }),
247
+ "files.upload": async ({ workspace: id }, { body }) => (await files(id)).upload(readChunks(body)),
248
+ "git.status": async ({ workspace: id }) => (await git(id)).status(),
249
+ "git.watch": async ({ workspace: id }) => (await git(id)).watch(),
250
+ "git.changes": async ({ workspace: id, scope, options }) => (await git(id)).changes(scope, options),
251
+ "git.watchChanges": async ({ workspace: id, scope, options }) => (await git(id)).watchChanges(scope, options),
252
+ "git.patches": async ({ workspace: id, scope, paths, options }) => mapped((await git(id)).patches(scope, paths, options), patchesWire),
253
+ "git.contents": async ({ workspace: id, path, scope }) => (await git(id)).contents(path, scope),
254
+ "git.log": async ({ workspace: id, cursor, limit }) => (await git(id)).log({ cursor, limit }),
255
+ "git.refs": async ({ workspace: id }) => (await git(id)).refs(),
256
+ "git.blame": async ({ workspace: id, path }) => (await git(id)).blame(path),
257
+ "git.stage": async ({ workspace: id, paths, options }) => (await git(id)).stage(paths, options),
258
+ "git.unstage": async ({ workspace: id, paths, options }) => (await git(id)).unstage(paths, options),
259
+ "git.discard": async ({ workspace: id, paths, options }) => (await git(id)).discard(paths, options),
260
+ "git.commit": async ({ workspace: id, input, options }) => (await git(id)).commit(input, options),
261
+ "git.branch": async ({ workspace: id, input, options }) => (await git(id)).branch(input, options),
262
+ "git.push": async ({ workspace: id, input, options }) => (await git(id)).push(input, options),
263
+ "github.account": () => connection.github.account(),
264
+ "github.signIn": () => connection.github.signIn(),
265
+ "github.signOut": () => connection.github.signOut(),
266
+ "github.state": async ({ workspace: id }) => (await github(id)).state(),
267
+ "github.createPullRequest": async ({ workspace: id, input }) => (await github(id)).createPullRequest(input),
268
+ "browser.open": async ({ workspace: id, url, id: key }) => {
269
+ const api = await browser(id);
270
+ return describePage(hold(await wire.openPage({
271
+ id: key,
272
+ input: { workspace: id, url },
273
+ open: () => api.open({ url })
274
+ })));
275
+ },
276
+ "browser.adopt": async ({ workspace: id, id: page }) => describePage(hold(await (await browser(id)).adopt(page))),
277
+ "browser.list": async ({ workspace: id }) => (await browser(id)).list(),
278
+ "browser.watch": async ({ workspace: id }) => (await browser(id)).watch(),
279
+ "page.view": async ({ workspace: id, page }) => (await findPage(id, page)).view(),
280
+ "page.watch": async ({ workspace: id, page }) => (await findPage(id, page)).watch(),
281
+ "page.snapshot": async ({ workspace: id, page, ref }) => (await findPage(id, page)).snapshot({ ref }),
282
+ "page.navigate": async ({ workspace: id, page, url }) => (await findPage(id, page)).navigate(url),
283
+ "page.back": async ({ workspace: id, page }) => (await findPage(id, page)).back(),
284
+ "page.forward": async ({ workspace: id, page }) => (await findPage(id, page)).forward(),
285
+ "page.reload": async ({ workspace: id, page }) => (await findPage(id, page)).reload(),
286
+ "page.stopLoading": async ({ workspace: id, page }) => (await findPage(id, page)).stopLoading(),
287
+ "page.click": async ({ workspace: id, page, ref, button, double, expect }) => (await findPage(id, page)).click({ ref, button, double, expect }),
288
+ "page.type": async ({ workspace: id, page, ref, text, clear, submit, expect }) => (await findPage(id, page)).type({ ref, text, clear, submit, expect }),
289
+ "page.press": async ({ workspace: id, page, key, ref }) => (await findPage(id, page)).press({ key, ref }),
290
+ "page.scroll": async ({ workspace: id, page, direction, ref, pages: count }) => (await findPage(id, page)).scroll({ direction, ref, pages: count }),
291
+ "page.wait": async ({ workspace: id, page, condition }) => (await findPage(id, page)).wait(condition),
292
+ "page.console": async ({ workspace: id, page, limit, clear }) => (await findPage(id, page)).console({ limit, clear }),
293
+ "page.capture": async ({ workspace: id, page }) => ({
294
+ reader: await (await findPage(id, page)).capture()
295
+ }),
296
+ "page.evaluate": async ({ workspace: id, page, expression, options }) => (await findPage(id, page)).evaluate(expression, options),
297
+ "page.close": async ({ page }) => {
298
+ await release(page);
299
+ return null;
300
+ },
301
+ "plugins.replace": ({ plugins, scope }) => connection.plugins.replace(plugins, scope),
302
+ "plugins.check": ({ plugins }) => connection.plugins.check(plugins),
303
+ "principals.list": async () => (await connection.principals.list()).map((view) => ({
304
+ ...view,
305
+ authority: [...view.authority]
306
+ })),
307
+ "principals.pair": ({ authority, label }) => connection.principals.pair({ authority: new Set(authority), label }),
308
+ "principals.revoke": async ({ id }) => {
309
+ await connection.principals.revoke(id);
310
+ return null;
311
+ }
312
+ };
313
+ }
314
+ async function* jobChanges(lists) {
315
+ let previous = null;
316
+ for await (const jobs of lists) {
317
+ if (previous === null)
318
+ yield { kind: "jobs", jobs };
319
+ else {
320
+ const kept = new Set(jobs.map((job) => job.id));
321
+ const unmoved = new Set(previous);
322
+ for (const job of previous)
323
+ if (!kept.has(job.id))
324
+ yield { kind: "forgotten", id: job.id };
325
+ for (const job of jobs)
326
+ if (!unmoved.has(job))
327
+ yield { kind: "job", job };
328
+ }
329
+ previous = jobs;
330
+ }
331
+ }
332
+ async function* readChunks(body) {
333
+ if (body === null)
334
+ return;
335
+ const reader = body.getReader();
336
+ try {
337
+ for (;; ) {
338
+ const read = await reader.read();
339
+ if (read.done)
340
+ return;
341
+ yield read.value;
342
+ }
343
+ } finally {
344
+ reader.releaseLock();
345
+ }
346
+ }
347
+ async function readBytes(body) {
348
+ const chunks = [];
349
+ for await (const chunk of readChunks(body))
350
+ chunks.push(chunk);
351
+ const bytes = new Uint8Array(chunks.reduce((size, chunk) => size + chunk.byteLength, 0));
352
+ let offset = 0;
353
+ for (const chunk of chunks) {
354
+ bytes.set(chunk, offset);
355
+ offset += chunk.byteLength;
356
+ }
357
+ return bytes;
358
+ }
359
+ function hostWire(view, principal) {
360
+ return { ...view, authority: [...view.authority], principal };
361
+ }
362
+ function patchesWire(set) {
363
+ return { revision: set.revision, entries: Object.fromEntries(set.entries) };
364
+ }
365
+ function capabilityWire(capability) {
366
+ if (capability.kind !== "available")
367
+ return capability;
368
+ return { kind: "available" };
369
+ }
370
+ function available(capability, what) {
371
+ if (capability.kind === "available")
372
+ return capability.api;
373
+ if (capability.kind === "denied")
374
+ fail("denied", `${what}: ${capability.reason}`);
375
+ return fail("unavailable", `${what}: ${capability.reason}`);
376
+ }
377
+ function toJson(value) {
378
+ return isJson(value) ? value : null;
379
+ }
380
+ function principalKey(principal, name) {
381
+ return JSON.stringify([principal, name]);
382
+ }
383
+ function fail(code, message) {
384
+ throw new SdkError(code, message);
385
+ }
386
+
387
+ export { bindConnection2 };