@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,134 @@
1
+ import {
2
+ sha2562
3
+ } from "./canvas-preview-0nq75h9k.js";
4
+ import {
5
+ localFiles2
6
+ } from "./canvas-preview-drctegz4.js";
7
+ import {
8
+ localGit2
9
+ } from "./canvas-preview-5ekjx6sw.js";
10
+ import {
11
+ localGitHub2
12
+ } from "./canvas-preview-8xc0vn7v.js";
13
+ import {
14
+ folderIdentity2
15
+ } from "./canvas-preview-1sq7z9jq.js";
16
+ import {
17
+ localExec2
18
+ } from "./canvas-preview-vm9zdp2t.js";
19
+ import {
20
+ localSnapshots2
21
+ } from "./canvas-preview-crmzxxt8.js";
22
+ import {
23
+ localWorktrees2
24
+ } from "./canvas-preview-2jwbe14k.js";
25
+
26
+ // src/local/provider.ts
27
+ import { constants, realpathSync } from "node:fs";
28
+ import { access, readdir, realpath, stat } from "node:fs/promises";
29
+ import { homedir } from "node:os";
30
+ import { isAbsolute, join, relative, sep } from "node:path";
31
+ import { asId, SdkError } from "@nyte-ai/schema";
32
+ var PLUGIN_FILE = /\.(?:ts|mts|js|mjs)$/;
33
+ var ROOT_SOURCES = [".nyte/SYSTEM.md", ".nyte/APPEND_SYSTEM.md", ".nyte/mcp.json"];
34
+ function localProvider2(options) {
35
+ const home = options?.home ?? homedir();
36
+ const store = join(home, ".nyte", "snapshots");
37
+ let state;
38
+ const github = localGitHub2({ gh: options?.gh, git: options?.git });
39
+ return {
40
+ kind: "local",
41
+ async inspect(target) {
42
+ const path = readTargetPath(target);
43
+ if (path === null)
44
+ return {
45
+ identity: asId(sha2562(JSON.stringify(target))),
46
+ availability: { kind: "unavailable", reason: "unsupported" },
47
+ projectSources: []
48
+ };
49
+ const identity = await folderIdentity2(path);
50
+ if (!await readableDirectory(path))
51
+ return {
52
+ identity,
53
+ availability: { kind: "unavailable", reason: "unreachable" },
54
+ projectSources: []
55
+ };
56
+ return {
57
+ identity,
58
+ availability: { kind: "available" },
59
+ projectSources: await projectSources(path)
60
+ };
61
+ },
62
+ async attach(target) {
63
+ const path = readTargetPath(target);
64
+ if (path === null)
65
+ throw new SdkError("unavailable", "A local workspace needs a path.");
66
+ const real = await realpath(path).catch(() => null);
67
+ if (real === null || !await readableDirectory(real))
68
+ throw new SdkError("unavailable", `${path} is not a readable directory.`);
69
+ const detached = new AbortController;
70
+ const env = {
71
+ identity: await folderIdentity2(real),
72
+ cwd: real,
73
+ files: localFiles2(real, {
74
+ ripgrep: options?.ripgrep,
75
+ formatter: options?.formatter,
76
+ signal: detached.signal
77
+ }),
78
+ exec: localExec2(real, { shell: options?.shell, signal: detached.signal }),
79
+ close: async () => detached.abort()
80
+ };
81
+ return env;
82
+ },
83
+ git: (env) => localGit2(env, { git: options?.git }),
84
+ github,
85
+ snapshots(env) {
86
+ state ??= join(realpathOrSelf(home), ".nyte");
87
+ return nests(env.cwd, state) || nests(state, env.cwd) && !nests(join(state, "worktrees"), env.cwd) ? undefined : localSnapshots2(env, { store, git: options?.git });
88
+ },
89
+ worktrees: localWorktrees2({ home, git: options?.git }),
90
+ async destroy() {
91
+ throw new SdkError("unavailable", "A local folder is never torn down.");
92
+ }
93
+ };
94
+ }
95
+ function realpathOrSelf(path) {
96
+ try {
97
+ return realpathSync(path);
98
+ } catch {
99
+ return path;
100
+ }
101
+ }
102
+ function nests(outer, inner) {
103
+ const path = relative(outer, inner);
104
+ return path === "" || path !== ".." && !path.startsWith(`..${sep}`) && !isAbsolute(path);
105
+ }
106
+ function readTargetPath(target) {
107
+ return "path" in target && typeof target.path === "string" ? target.path : null;
108
+ }
109
+ async function readableDirectory(path) {
110
+ const stats = await stat(path).catch(() => null);
111
+ return stats !== null && stats.isDirectory() && await access(path, constants.R_OK | constants.X_OK).then(() => true, () => false);
112
+ }
113
+ async function projectSources(root) {
114
+ const names = async (directory) => (await readdir(join(root, directory)).catch(() => [])).sort((left, right) => left < right ? -1 : left > right ? 1 : 0);
115
+ const fileExists = async (path) => (await stat(join(root, path)).catch(() => null))?.isFile() === true;
116
+ const plugins = (await Promise.all((await names(".nyte/plugins")).map(async (name) => {
117
+ const path = `.nyte/plugins/${name}`;
118
+ if (PLUGIN_FILE.test(name) && await fileExists(path))
119
+ return [path];
120
+ for (const index of ["index.ts", "index.mts", "index.js", "index.mjs"])
121
+ if (await fileExists(`${path}/${index}`))
122
+ return [`${path}/${index}`];
123
+ return [];
124
+ }))).flat();
125
+ const skills = (await Promise.all([".nyte/skills", ".agents/skills"].map(async (directory) => (await Promise.all((await names(directory)).map(async (name) => {
126
+ const path = `${directory}/${name}/SKILL.md`;
127
+ return await fileExists(path) ? [path] : [];
128
+ }))).flat()))).flat();
129
+ const prompts = (await names(".nyte/prompts")).filter((name) => name.endsWith(".md")).map((name) => `.nyte/prompts/${name}`);
130
+ const present = await Promise.all(ROOT_SOURCES.map(async (path) => await fileExists(path) ? [path] : []));
131
+ return [...plugins, ...skills, ...prompts, ...present.flat()];
132
+ }
133
+
134
+ export { localProvider2 };
@@ -0,0 +1,108 @@
1
+ import {
2
+ exifOrientation2
3
+ } from "./canvas-preview-befkjea2.js";
4
+
5
+ // src/images/photon.ts
6
+ import {
7
+ PhotonImage,
8
+ SamplingFilter,
9
+ fliph,
10
+ flipv,
11
+ initSync,
12
+ resize
13
+ } from "@silvia-odwyer/photon/photon_rs.js";
14
+ var INLINE_IMAGE_TYPES = new Set([
15
+ "image/png",
16
+ "image/jpeg",
17
+ "image/gif",
18
+ "image/webp"
19
+ ]);
20
+ var JPEG_QUALITIES = [80, 70, 55, 40];
21
+ function photonImages2(wasm) {
22
+ initSync({ module: wasm });
23
+ return {
24
+ prepare: (bytes, mimeType, limits) => Promise.resolve(prepare(bytes, mimeType, limits))
25
+ };
26
+ }
27
+ function prepare(bytes, mimeType, limits) {
28
+ const orientation = exifOrientation2(bytes);
29
+ let image;
30
+ try {
31
+ image = PhotonImage.new_from_byteslice(bytes);
32
+ if (INLINE_IMAGE_TYPES.has(mimeType) && orientation === 1 && image.get_width() <= limits.maxWidth && image.get_height() <= limits.maxHeight && base64Length(bytes.byteLength) <= limits.maxBase64Bytes)
33
+ return { data: bytes.toBase64(), mimeType };
34
+ if (orientation >= 5) {
35
+ const upright = transposed(image, orientation);
36
+ image.free();
37
+ image = undefined;
38
+ image = new PhotonImage(upright.pixels, upright.width, upright.height);
39
+ } else if (orientation !== 1) {
40
+ if (orientation !== 4)
41
+ fliph(image);
42
+ if (orientation !== 2)
43
+ flipv(image);
44
+ }
45
+ const width = image.get_width();
46
+ const height = image.get_height();
47
+ const scale = Math.min(1, limits.maxWidth / width, limits.maxHeight / height);
48
+ let target = {
49
+ width: Math.max(1, Math.round(width * scale)),
50
+ height: Math.max(1, Math.round(height * scale))
51
+ };
52
+ for (;; ) {
53
+ const encoded = encodeWithin(image, target, limits.maxBase64Bytes, mimeType === "image/jpeg");
54
+ if (encoded !== undefined)
55
+ return target.width === width && target.height === height ? encoded : { ...encoded, resized: { from: { width, height }, to: target } };
56
+ if (target.width === 1 && target.height === 1)
57
+ return;
58
+ target = {
59
+ width: Math.max(1, Math.floor(target.width * 0.75)),
60
+ height: Math.max(1, Math.floor(target.height * 0.75))
61
+ };
62
+ }
63
+ } catch {
64
+ return;
65
+ } finally {
66
+ image?.free();
67
+ }
68
+ }
69
+ function encodeWithin(image, size, maxBase64Bytes, jpegFirst) {
70
+ const same = size.width === image.get_width() && size.height === image.get_height();
71
+ const scaled = same ? image : resize(image, size.width, size.height, SamplingFilter.Lanczos3);
72
+ try {
73
+ const png = { mimeType: "image/png", encode: () => scaled.get_bytes() };
74
+ const jpegs = JPEG_QUALITIES.map((quality) => ({
75
+ mimeType: "image/jpeg",
76
+ encode: () => scaled.get_bytes_jpeg(quality)
77
+ }));
78
+ for (const candidate of jpegFirst ? [...jpegs, png] : [png, ...jpegs]) {
79
+ const encoded = candidate.encode();
80
+ if (base64Length(encoded.byteLength) <= maxBase64Bytes)
81
+ return { data: encoded.toBase64(), mimeType: candidate.mimeType };
82
+ }
83
+ return;
84
+ } finally {
85
+ if (!same)
86
+ scaled.free();
87
+ }
88
+ }
89
+ function transposed(image, orientation) {
90
+ const width = image.get_width();
91
+ const height = image.get_height();
92
+ const source = image.get_raw_pixels();
93
+ const pixels = new Uint8Array(source.length);
94
+ for (let y = 0;y < height; y++) {
95
+ for (let x = 0;x < width; x++) {
96
+ const outX = orientation === 5 || orientation === 8 ? y : height - 1 - y;
97
+ const outY = orientation === 5 || orientation === 6 ? x : width - 1 - x;
98
+ const from = (y * width + x) * 4;
99
+ pixels.set(source.subarray(from, from + 4), (outY * height + outX) * 4);
100
+ }
101
+ }
102
+ return { pixels, width: height, height: width };
103
+ }
104
+ function base64Length(byteLength) {
105
+ return Math.ceil(byteLength / 3) * 4;
106
+ }
107
+
108
+ export { photonImages2 };
@@ -0,0 +1,61 @@
1
+ import {
2
+ readNyteUsage2
3
+ } from "./canvas-preview-11nadadc.js";
4
+ import {
5
+ loadUsageScanCaches2,
6
+ saveUsageScanCaches2,
7
+ readLocalUsage2
8
+ } from "./canvas-preview-k1yg4fgp.js";
9
+
10
+ // src/usage-service.ts
11
+ import { join } from "node:path";
12
+ import { FileModelsStore, nyteProviders } from "@nyte-ai/ai";
13
+ import { createModels } from "@nyte-ai/ai/models";
14
+ import { serial } from "@nyte-ai/schema/serial";
15
+ function catalogForUsage2(models) {
16
+ return [...models.getModels("anthropic"), ...models.getModels("openai-codex")];
17
+ }
18
+
19
+ class UsageScanner2 {
20
+ home;
21
+ cachePath;
22
+ caches;
23
+ models;
24
+ scans = serial();
25
+ constructor(home) {
26
+ this.home = home;
27
+ this.cachePath = join(home, ".nyte", "usage-scan-cache.json");
28
+ this.models = createModels({
29
+ modelsStore: new FileModelsStore(join(home, ".nyte", "models-store.json"))
30
+ });
31
+ for (const provider of nyteProviders())
32
+ this.models.setProvider(provider);
33
+ }
34
+ scan(request) {
35
+ return this.scans.run(async () => {
36
+ if (request.catalog === undefined)
37
+ await this.models.refresh({
38
+ providers: ["anthropic", "openai-codex"],
39
+ allowNetwork: false
40
+ });
41
+ const catalog = request.catalog;
42
+ const models = catalog === undefined ? this.models : {
43
+ getModels: (provider) => catalog.filter((model) => provider === undefined || model.provider === provider)
44
+ };
45
+ const caches = this.caches ??= await loadUsageScanCaches2(this.cachePath);
46
+ const local = await readLocalUsage2({ models, window: request.window, caches });
47
+ const report = readNyteUsage2(this.home, request.window);
48
+ this.scans.run(() => saveUsageScanCaches2(this.cachePath, caches));
49
+ return {
50
+ ...report,
51
+ ...local,
52
+ nyteError: report.sources.find((source) => source.status === "failed")?.message ?? null
53
+ };
54
+ });
55
+ }
56
+ close() {
57
+ return this.scans.settled();
58
+ }
59
+ }
60
+
61
+ export { catalogForUsage2, UsageScanner2 };