@missionsquad/browserai 0.1.0

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 (146) hide show
  1. package/LICENSE +15 -0
  2. package/README.md +229 -0
  3. package/dist/audio/clip.d.ts +51 -0
  4. package/dist/audio/clip.d.ts.map +1 -0
  5. package/dist/audio/clip.js +71 -0
  6. package/dist/audio/clip.js.map +1 -0
  7. package/dist/audio/decode.d.ts +7 -0
  8. package/dist/audio/decode.d.ts.map +1 -0
  9. package/dist/audio/decode.js +19 -0
  10. package/dist/audio/decode.js.map +1 -0
  11. package/dist/audio/pcm.d.ts +13 -0
  12. package/dist/audio/pcm.d.ts.map +1 -0
  13. package/dist/audio/pcm.js +66 -0
  14. package/dist/audio/pcm.js.map +1 -0
  15. package/dist/audio/recorder.d.ts +40 -0
  16. package/dist/audio/recorder.d.ts.map +1 -0
  17. package/dist/audio/recorder.js +127 -0
  18. package/dist/audio/recorder.js.map +1 -0
  19. package/dist/audio/streaming-player.d.ts +29 -0
  20. package/dist/audio/streaming-player.d.ts.map +1 -0
  21. package/dist/audio/streaming-player.js +82 -0
  22. package/dist/audio/streaming-player.js.map +1 -0
  23. package/dist/client.d.ts +195 -0
  24. package/dist/client.d.ts.map +1 -0
  25. package/dist/client.js +638 -0
  26. package/dist/client.js.map +1 -0
  27. package/dist/config.d.ts +81 -0
  28. package/dist/config.d.ts.map +1 -0
  29. package/dist/config.js +19 -0
  30. package/dist/config.js.map +1 -0
  31. package/dist/errors.d.ts +52 -0
  32. package/dist/errors.d.ts.map +1 -0
  33. package/dist/errors.js +74 -0
  34. package/dist/errors.js.map +1 -0
  35. package/dist/events.d.ts +18 -0
  36. package/dist/events.d.ts.map +1 -0
  37. package/dist/events.js +43 -0
  38. package/dist/events.js.map +1 -0
  39. package/dist/generation.d.ts +12 -0
  40. package/dist/generation.d.ts.map +1 -0
  41. package/dist/generation.js +30 -0
  42. package/dist/generation.js.map +1 -0
  43. package/dist/hardware.d.ts +23 -0
  44. package/dist/hardware.d.ts.map +1 -0
  45. package/dist/hardware.js +150 -0
  46. package/dist/hardware.js.map +1 -0
  47. package/dist/history.d.ts +81 -0
  48. package/dist/history.d.ts.map +1 -0
  49. package/dist/history.js +193 -0
  50. package/dist/history.js.map +1 -0
  51. package/dist/index.d.ts +51 -0
  52. package/dist/index.d.ts.map +1 -0
  53. package/dist/index.js +50 -0
  54. package/dist/index.js.map +1 -0
  55. package/dist/inference.d.ts +89 -0
  56. package/dist/inference.d.ts.map +1 -0
  57. package/dist/inference.js +411 -0
  58. package/dist/inference.js.map +1 -0
  59. package/dist/json.d.ts +22 -0
  60. package/dist/json.d.ts.map +1 -0
  61. package/dist/json.js +145 -0
  62. package/dist/json.js.map +1 -0
  63. package/dist/metrics.d.ts +18 -0
  64. package/dist/metrics.d.ts.map +1 -0
  65. package/dist/metrics.js +59 -0
  66. package/dist/metrics.js.map +1 -0
  67. package/dist/model-cache.d.ts +49 -0
  68. package/dist/model-cache.d.ts.map +1 -0
  69. package/dist/model-cache.js +511 -0
  70. package/dist/model-cache.js.map +1 -0
  71. package/dist/model-source.d.ts +27 -0
  72. package/dist/model-source.d.ts.map +1 -0
  73. package/dist/model-source.js +110 -0
  74. package/dist/model-source.js.map +1 -0
  75. package/dist/models.d.ts +103 -0
  76. package/dist/models.d.ts.map +1 -0
  77. package/dist/models.js +1052 -0
  78. package/dist/models.js.map +1 -0
  79. package/dist/multimodal.d.ts +261 -0
  80. package/dist/multimodal.d.ts.map +1 -0
  81. package/dist/multimodal.js +506 -0
  82. package/dist/multimodal.js.map +1 -0
  83. package/dist/prompt.d.ts +13 -0
  84. package/dist/prompt.d.ts.map +1 -0
  85. package/dist/prompt.js +32 -0
  86. package/dist/prompt.js.map +1 -0
  87. package/dist/proxy-verify.d.ts +10 -0
  88. package/dist/proxy-verify.d.ts.map +1 -0
  89. package/dist/proxy-verify.js +98 -0
  90. package/dist/proxy-verify.js.map +1 -0
  91. package/dist/schema-validator.d.ts +6 -0
  92. package/dist/schema-validator.d.ts.map +1 -0
  93. package/dist/schema-validator.js +104 -0
  94. package/dist/schema-validator.js.map +1 -0
  95. package/dist/schema.d.ts +98 -0
  96. package/dist/schema.d.ts.map +1 -0
  97. package/dist/schema.js +115 -0
  98. package/dist/schema.js.map +1 -0
  99. package/dist/text-utils.d.ts +8 -0
  100. package/dist/text-utils.d.ts.map +1 -0
  101. package/dist/text-utils.js +14 -0
  102. package/dist/text-utils.js.map +1 -0
  103. package/dist/voice/helpers.d.ts +35 -0
  104. package/dist/voice/helpers.d.ts.map +1 -0
  105. package/dist/voice/helpers.js +49 -0
  106. package/dist/voice/helpers.js.map +1 -0
  107. package/dist/voice/voice-pipeline.d.ts +155 -0
  108. package/dist/voice/voice-pipeline.d.ts.map +1 -0
  109. package/dist/voice/voice-pipeline.js +407 -0
  110. package/dist/voice/voice-pipeline.js.map +1 -0
  111. package/dist/webllm.worker.d.ts +2 -0
  112. package/dist/webllm.worker.d.ts.map +1 -0
  113. package/dist/webllm.worker.js +8 -0
  114. package/dist/webllm.worker.js.map +1 -0
  115. package/package.json +62 -0
  116. package/src/audio/clip.ts +94 -0
  117. package/src/audio/decode.ts +19 -0
  118. package/src/audio/pcm.ts +62 -0
  119. package/src/audio/recorder.ts +146 -0
  120. package/src/audio/streaming-player.ts +99 -0
  121. package/src/client.ts +784 -0
  122. package/src/config.ts +104 -0
  123. package/src/errors.ts +81 -0
  124. package/src/events.ts +53 -0
  125. package/src/generation.ts +39 -0
  126. package/src/hardware.ts +193 -0
  127. package/src/history.ts +257 -0
  128. package/src/index.ts +203 -0
  129. package/src/inference.ts +579 -0
  130. package/src/json.ts +169 -0
  131. package/src/metrics.ts +63 -0
  132. package/src/model-cache.ts +637 -0
  133. package/src/model-source.ts +142 -0
  134. package/src/models.ts +1171 -0
  135. package/src/multimodal.ts +697 -0
  136. package/src/prompt.ts +33 -0
  137. package/src/proxy-verify.ts +113 -0
  138. package/src/schema-validator.ts +117 -0
  139. package/src/schema.ts +118 -0
  140. package/src/text-utils.ts +12 -0
  141. package/src/voice/helpers.ts +59 -0
  142. package/src/voice/voice-pipeline.ts +513 -0
  143. package/src/webllm.worker.ts +9 -0
  144. package/worker-template/README.md +39 -0
  145. package/worker-template/cloudflare-worker.ts +389 -0
  146. package/worker-template/wrangler.jsonc +19 -0
@@ -0,0 +1,142 @@
1
+ import type { AppConfig, ModelRecord } from "@mlc-ai/web-llm";
2
+ import { applyCuratedModelRecordOverrides, CUSTOM_MODEL_RECORDS } from "./models.js";
3
+
4
+ type WebLLMModule = typeof import("@mlc-ai/web-llm");
5
+
6
+ const HUGGING_FACE_HOST = "huggingface.co";
7
+ const RAW_GITHUB_HOST = "raw.githubusercontent.com";
8
+
9
+ export type ModelSourceMode = "direct" | "same-origin-proxy";
10
+ export type BrowserCacheBackend = "cache" | "indexeddb" | "opfs" | "cross-origin";
11
+
12
+ /** The Cache API bucket Transformers.js artifacts are cached in (shared with model-cache cleanup). */
13
+ export const TRANSFORMERS_CACHE_KEY = "transformers-cache";
14
+
15
+ /**
16
+ * Build the WebLLM AppConfig for a load: the bundled prebuilt records merged with the SDK's custom
17
+ * MLC records and curated overrides, optionally rewritten to same-origin proxy routes, with the
18
+ * chosen artifact cache backend applied. `proxyOrigin` is only read in proxy mode.
19
+ */
20
+ export function buildWebLLMAppConfig(
21
+ webllm: WebLLMModule,
22
+ mode: ModelSourceMode,
23
+ proxyOrigin: string,
24
+ cacheBackend: BrowserCacheBackend,
25
+ ): AppConfig {
26
+ const mergedConfig = withCustomModelRecords(webllm.prebuiltAppConfig);
27
+ const baseConfig = mode === "direct" ? mergedConfig : buildProxiedAppConfig(mergedConfig, proxyOrigin);
28
+
29
+ return {
30
+ ...baseConfig,
31
+ cacheBackend,
32
+ };
33
+ }
34
+
35
+ export function withCustomModelRecords(appConfig: AppConfig): AppConfig {
36
+ const existingModelIds = new Set(appConfig.model_list.map((record) => record.model_id));
37
+ const missingCustomRecords = CUSTOM_MODEL_RECORDS.filter((record) => !existingModelIds.has(record.model_id));
38
+ const modelList = [...appConfig.model_list, ...missingCustomRecords].map(applyCuratedModelRecordOverrides);
39
+
40
+ return {
41
+ ...appConfig,
42
+ model_list: modelList,
43
+ };
44
+ }
45
+
46
+ export function buildProxiedAppConfig(appConfig: AppConfig, origin: string): AppConfig {
47
+ const modelList = appConfig.model_list.map((record) => proxyModelRecord(record, origin));
48
+ return {
49
+ ...appConfig,
50
+ model_list: modelList,
51
+ };
52
+ }
53
+
54
+ export function proxyModelRecord(record: ModelRecord, origin: string): ModelRecord {
55
+ return {
56
+ ...record,
57
+ model: proxyHuggingFaceUrl(record.model, origin),
58
+ // WebLLM's bundled model_lib values usually point at raw.githubusercontent.com, while custom
59
+ // MLC builds may point at Hugging Face. Proxy both forms so hosted Worker deployments stay
60
+ // same-origin and avoid CORS/cache differences.
61
+ model_lib: proxyModelLibraryUrl(record.model_lib, origin),
62
+ };
63
+ }
64
+
65
+ export function proxyModelLibraryUrl(rawUrl: string, origin: string): string {
66
+ return proxyRawGithubUrl(proxyHuggingFaceUrl(rawUrl, origin), origin);
67
+ }
68
+
69
+ export function proxyHuggingFaceUrl(rawUrl: string, origin: string): string {
70
+ let modelUrl: URL;
71
+ try {
72
+ modelUrl = new URL(rawUrl);
73
+ } catch {
74
+ return rawUrl;
75
+ }
76
+
77
+ if (modelUrl.hostname !== HUGGING_FACE_HOST) {
78
+ return rawUrl;
79
+ }
80
+
81
+ const parts = modelUrl.pathname.split("/").filter(Boolean);
82
+ const [owner, repo, ...suffixParts] = parts;
83
+ if (!owner || !repo) {
84
+ return rawUrl;
85
+ }
86
+
87
+ const proxiedPath = ["hf", owner, repo, ...suffixParts].map(encodeURIComponent).join("/");
88
+ const proxied = new URL(`/${proxiedPath}`, origin);
89
+ proxied.search = modelUrl.search;
90
+
91
+ return proxied.href;
92
+ }
93
+
94
+ export function proxyRawGithubUrl(rawUrl: string, origin: string): string {
95
+ let modelLibUrl: URL;
96
+ try {
97
+ modelLibUrl = new URL(rawUrl);
98
+ } catch {
99
+ return rawUrl;
100
+ }
101
+
102
+ if (modelLibUrl.hostname !== RAW_GITHUB_HOST) {
103
+ return rawUrl;
104
+ }
105
+
106
+ const parts = modelLibUrl.pathname.split("/").filter(Boolean);
107
+ const [owner, repo, branch, ...fileParts] = parts;
108
+ if (!owner || !repo || !branch || fileParts.length === 0) {
109
+ return rawUrl;
110
+ }
111
+
112
+ const proxiedPath = ["gh-raw", owner, repo, branch, ...fileParts].map(encodeURIComponent).join("/");
113
+ const proxied = new URL(`/${proxiedPath}`, origin);
114
+ proxied.search = modelLibUrl.search;
115
+
116
+ return proxied.href;
117
+ }
118
+
119
+ /**
120
+ * Merge a caller-supplied context length into the selected model's record overrides. Preserves any
121
+ * curated overrides (e.g. Gemma's fixed sliding-window config) by merging rather than replacing.
122
+ */
123
+ export function applyContextOverride(appConfig: AppConfig, modelId: string, contextLength: number | null): AppConfig {
124
+ if (contextLength === null) return appConfig;
125
+ return {
126
+ ...appConfig,
127
+ model_list: appConfig.model_list.map((record) =>
128
+ record.model_id === modelId ? { ...record, overrides: { ...(record.overrides ?? {}), context_window_size: contextLength } } : record,
129
+ ),
130
+ };
131
+ }
132
+
133
+ /** Human-readable description of where artifacts come from and how they are cached. */
134
+ export function modelSourceDescription(mode: ModelSourceMode, cacheBackend: BrowserCacheBackend): string {
135
+ const cacheText = cacheBackend === "indexeddb" ? "browser IndexedDB" : `the browser ${cacheBackend} backend`;
136
+
137
+ if (mode === "same-origin-proxy") {
138
+ return `Model files are fetched through the proxy Worker at /hf/* and WebLLM libraries through /gh-raw/*, then cached in ${cacheText}.`;
139
+ }
140
+
141
+ return `Model files are fetched directly from Hugging Face and cached in ${cacheText}.`;
142
+ }