@cjhyy/code-shell 0.1.5 → 0.1.7

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 (158) hide show
  1. package/CHANGELOG.md +30 -0
  2. package/dist/anthropic-UBSB5AQH.js +1 -0
  3. package/dist/arena/index.js +1 -104
  4. package/dist/arena-OW55ZCHT.js +3 -0
  5. package/dist/bwrap-PVZMLHZR.js +2 -0
  6. package/dist/chunk-2NS3JMNK.js +1 -0
  7. package/dist/chunk-3QP5BQ3L.js +1 -0
  8. package/dist/chunk-B7K2OWKC.js +2 -0
  9. package/dist/chunk-EWWK56K6.js +2 -0
  10. package/dist/chunk-FU7AHZNN.js +1 -0
  11. package/dist/chunk-HIJ2XXGX.js +1 -0
  12. package/dist/chunk-KCXSARAA.js +1 -0
  13. package/dist/chunk-NFH3UQ3E.js +2 -0
  14. package/dist/chunk-NMBYBGC7.js +250 -0
  15. package/dist/chunk-NVAZJ5XS.js +2 -0
  16. package/dist/chunk-O5HFCH2U.js +1 -0
  17. package/dist/chunk-P6JSTIFM.js +1 -0
  18. package/dist/chunk-PGANL6F2.js +3 -0
  19. package/dist/chunk-Q4O2HGWS.js +251 -0
  20. package/dist/chunk-QTWU7UNA.js +5 -0
  21. package/dist/chunk-RP26WO4R.js +1 -0
  22. package/dist/chunk-SNFAPSLA.js +1 -0
  23. package/dist/chunk-T222K6IV.js +18 -0
  24. package/dist/chunk-TKJA6W36.js +3 -0
  25. package/dist/chunk-UDPUTFQT.js +2 -0
  26. package/dist/chunk-W47Z7MGS.js +33 -0
  27. package/dist/chunk-WI6FSWGU.js +1 -0
  28. package/dist/chunk-WOP6MJKW.js +1 -0
  29. package/dist/chunk-XDOWC7EG.js +4 -0
  30. package/dist/chunk-Y2NS6HPO.js +641 -0
  31. package/dist/cli/main.js +303 -22037
  32. package/dist/cli/updater.d.ts +8 -0
  33. package/dist/client-base-6ZZJJYDY.js +1 -0
  34. package/dist/compaction-ECVQVOCT.js +1 -0
  35. package/dist/devtools-SGDUOOM4.js +1 -0
  36. package/dist/engine/engine.d.ts +15 -0
  37. package/dist/export-md-U72T7NW7.js +22 -0
  38. package/dist/file-history-7FQFEQOL.js +1 -0
  39. package/dist/index.d.ts +1 -1
  40. package/dist/index.js +1 -347
  41. package/dist/llm/token-counter.d.ts +17 -0
  42. package/dist/manager-C3TDN6MS.js +1 -0
  43. package/dist/mcp-manager-OP4XYV53.js +1 -0
  44. package/dist/memory-BNI4WEWL.js +1 -0
  45. package/dist/model-cache-HEDXI7UJ.js +1 -0
  46. package/dist/model-fetcher-BJ64META.js +1 -0
  47. package/dist/off-7SDVMMXW.js +1 -0
  48. package/dist/openai-KEWQ54TM.js +1 -0
  49. package/dist/openrouter-models-AM6M52D6.js +1 -0
  50. package/dist/openrouter-sync-5JNBL3Z6.js +1 -0
  51. package/dist/product/index.js +1 -21
  52. package/dist/run/index.js +1 -48
  53. package/dist/seatbelt-WBYROQT6.js +37 -0
  54. package/dist/session/session-manager.d.ts +2 -0
  55. package/dist/session-manager-AGWS5SMJ.js +1 -0
  56. package/dist/settings/schema.d.ts +72 -6
  57. package/dist/tool-summary-5UE62VU4.js +2 -0
  58. package/dist/tool-system/builtin/bash.d.ts +7 -1
  59. package/dist/tool-system/context.d.ts +7 -0
  60. package/dist/tool-system/investigation-guard.d.ts +9 -0
  61. package/dist/tool-system/sandbox/bwrap.d.ts +13 -0
  62. package/dist/tool-system/sandbox/index.d.ts +71 -0
  63. package/dist/tool-system/sandbox/off.d.ts +2 -0
  64. package/dist/tool-system/sandbox/seatbelt.d.ts +18 -0
  65. package/dist/types.d.ts +4 -0
  66. package/dist/ui/components/SessionPicker.d.ts +2 -0
  67. package/dist/ui/components/SpinnerWithVerb.d.ts +2 -2
  68. package/dist/ui/components/StatusLine.d.ts +2 -2
  69. package/dist/updater-OCNU2K2Z.js +1 -0
  70. package/package.json +8 -10
  71. package/skills-builtin/codeshell-help.md +135 -0
  72. package/dist/anthropic-IBTI4WL2.cjs +0 -10
  73. package/dist/anthropic-PRZZKHEZ.js +0 -10
  74. package/dist/arena/index.cjs +0 -104
  75. package/dist/arena-KBQOPLFV.cjs +0 -261
  76. package/dist/arena-O2L4TN6A.js +0 -261
  77. package/dist/chunk-2EP3CLEN.js +0 -44
  78. package/dist/chunk-2YW4FPFG.cjs +0 -101
  79. package/dist/chunk-3OA5G4TM.js +0 -47
  80. package/dist/chunk-52LGZKT3.cjs +0 -47
  81. package/dist/chunk-55XGPMAP.cjs +0 -146
  82. package/dist/chunk-5ECBT4YA.js +0 -450
  83. package/dist/chunk-6QVAYW7C.cjs +0 -438
  84. package/dist/chunk-6S4BJ65N.js +0 -113
  85. package/dist/chunk-76HYEPDM.js +0 -307
  86. package/dist/chunk-76W334S4.js +0 -54
  87. package/dist/chunk-7EG3V4JY.cjs +0 -113
  88. package/dist/chunk-7JVTYEEC.js +0 -340
  89. package/dist/chunk-7K6SDRRF.js +0 -9047
  90. package/dist/chunk-7Y6S3OCX.js +0 -438
  91. package/dist/chunk-A5LCRPDP.cjs +0 -54
  92. package/dist/chunk-BSW22MPA.js +0 -146
  93. package/dist/chunk-BUU5PZK2.cjs +0 -9048
  94. package/dist/chunk-BWTOS5BH.cjs +0 -5128
  95. package/dist/chunk-BXCY2FSE.js +0 -203
  96. package/dist/chunk-C5YQBIRK.js +0 -5128
  97. package/dist/chunk-HEKQCI7B.cjs +0 -224
  98. package/dist/chunk-HS2MFNUC.cjs +0 -44
  99. package/dist/chunk-IK4EWJBP.cjs +0 -203
  100. package/dist/chunk-JMR33QVF.cjs +0 -169
  101. package/dist/chunk-KOVYSDHZ.cjs +0 -340
  102. package/dist/chunk-KS3RYAYL.cjs +0 -45
  103. package/dist/chunk-MD32R22I.js +0 -50
  104. package/dist/chunk-MY5XHADU.cjs +0 -216
  105. package/dist/chunk-NUNC3VFB.cjs +0 -307
  106. package/dist/chunk-QRM7IF2S.cjs +0 -4711
  107. package/dist/chunk-R47ROHGL.cjs +0 -298
  108. package/dist/chunk-RMYKO74M.js +0 -1252
  109. package/dist/chunk-SFXHZSRF.js +0 -101
  110. package/dist/chunk-SIHHIQLO.js +0 -224
  111. package/dist/chunk-SNT563K3.cjs +0 -107
  112. package/dist/chunk-T6VYG46N.js +0 -105
  113. package/dist/chunk-TUY2T5KP.cjs +0 -105
  114. package/dist/chunk-VIAGTYYW.js +0 -169
  115. package/dist/chunk-VYL4EFRC.cjs +0 -1252
  116. package/dist/chunk-W22P377T.js +0 -299
  117. package/dist/chunk-WASS5YWB.js +0 -215
  118. package/dist/chunk-XDIQWQJ4.js +0 -4711
  119. package/dist/chunk-YIQBXAYU.cjs +0 -264
  120. package/dist/chunk-YJ77GQK5.js +0 -107
  121. package/dist/chunk-YUI524OE.cjs +0 -450
  122. package/dist/chunk-ZBPSQCPU.js +0 -264
  123. package/dist/cli/main.cjs +0 -22133
  124. package/dist/client-base-HTF236ZD.cjs +0 -9
  125. package/dist/client-base-QKRKCOWB.js +0 -9
  126. package/dist/compaction-6VU2T643.js +0 -33
  127. package/dist/compaction-OKUNTCA7.cjs +0 -33
  128. package/dist/devtools-T2NIZBJK.cjs +0 -6
  129. package/dist/devtools-YXWHOVKR.js +0 -6
  130. package/dist/export-md-RXDW6F5W.cjs +0 -135
  131. package/dist/export-md-XXKZANW5.js +0 -135
  132. package/dist/file-history-KYPE3ECT.cjs +0 -7
  133. package/dist/file-history-LF4F7X26.js +0 -7
  134. package/dist/index.cjs +0 -347
  135. package/dist/manager-C2IUXKRC.js +0 -7
  136. package/dist/manager-YYKWYTRM.cjs +0 -7
  137. package/dist/mcp-manager-QMKWPBZ4.js +0 -8
  138. package/dist/mcp-manager-Z3OKI74O.cjs +0 -8
  139. package/dist/memory-5LWZ6FI2.cjs +0 -9
  140. package/dist/memory-VIJLBAMQ.js +0 -9
  141. package/dist/model-cache-5KIYL5SL.js +0 -13
  142. package/dist/model-cache-OED3IM4R.cjs +0 -13
  143. package/dist/model-fetcher-HKGICACL.js +0 -8
  144. package/dist/model-fetcher-UR6WJOQP.cjs +0 -8
  145. package/dist/openai-JR2WEKOF.js +0 -10
  146. package/dist/openai-YBTOET4O.cjs +0 -10
  147. package/dist/openrouter-models-OU4GRZQY.cjs +0 -15
  148. package/dist/openrouter-models-TGKDKY52.js +0 -15
  149. package/dist/openrouter-sync-BBHUM3R6.cjs +0 -11
  150. package/dist/openrouter-sync-UXUSJCIR.js +0 -11
  151. package/dist/product/index.cjs +0 -21
  152. package/dist/run/index.cjs +0 -48
  153. package/dist/session-manager-Q22BBMD3.js +0 -8
  154. package/dist/session-manager-XODQVL3C.cjs +0 -8
  155. package/dist/tool-summary-63DPIS7G.cjs +0 -31
  156. package/dist/tool-summary-NP5CDBKG.js +0 -31
  157. package/dist/updater-QW473OF4.cjs +0 -17
  158. package/dist/updater-VFUAHCVY.js +0 -17
@@ -1,299 +0,0 @@
1
- import {
2
- __dirname,
3
- __filename,
4
- __require,
5
- init_esm_shims
6
- } from "./chunk-MD32R22I.js";
7
-
8
- // src/cli/updater.ts
9
- init_esm_shims();
10
- import { execFile, spawn } from "child_process";
11
- import { constants as fsConstants } from "fs";
12
- import { access, mkdir, readFile, stat, unlink, writeFile } from "fs/promises";
13
- import { createRequire } from "module";
14
- import { homedir } from "os";
15
- import { dirname, join, resolve } from "path";
16
- import { readFileSync } from "fs";
17
- import { fileURLToPath } from "url";
18
- import { promisify } from "util";
19
-
20
- // src/utils/semver.ts
21
- init_esm_shims();
22
- import { gte as semverGte, gt as semverGt, lt as semverLt } from "semver";
23
- function gt(a, b) {
24
- return semverGt(a, b);
25
- }
26
-
27
- // src/cli/updater.ts
28
- var execFileAsync = promisify(execFile);
29
- var PACKAGE_NAME = "@cjhyy/code-shell";
30
- var LOCK_FILE = ".update.lock";
31
- var LOCK_TIMEOUT_MS = 5 * 60 * 1e3;
32
- var NPM_VIEW_TIMEOUT_MS = 5e3;
33
- var _updateInfo;
34
- var _exitHookInstalled = false;
35
- var _disabledReason = null;
36
- function checkForUpdate(packageName = PACKAGE_NAME) {
37
- void runCheck(packageName);
38
- }
39
- function getUpdateAvailable() {
40
- return _updateInfo;
41
- }
42
- function getAutoUpdateDisabledReason() {
43
- return _disabledReason;
44
- }
45
- function scheduleAutoInstallOnExit() {
46
- if (_exitHookInstalled) return;
47
- _exitHookInstalled = true;
48
- const fire = () => {
49
- const info = _updateInfo;
50
- if (!info || !info.canAutoInstall) return;
51
- try {
52
- launchDetachedInstall(info.latestVersion);
53
- } catch {
54
- }
55
- };
56
- process.once("exit", fire);
57
- }
58
- var _cachedVersion;
59
- function getCurrentVersion() {
60
- if (_cachedVersion) return _cachedVersion;
61
- const found = resolveVersion();
62
- _cachedVersion = found;
63
- return found;
64
- }
65
- function resolveVersion() {
66
- try {
67
- const here = typeof import.meta !== "undefined" && import.meta.url ? dirname(fileURLToPath(import.meta.url)) : __dirname;
68
- let dir = here;
69
- for (let i = 0; i < 6; i++) {
70
- try {
71
- const pkg = JSON.parse(readFileSync(resolve(dir, "package.json"), "utf-8"));
72
- if (pkg && typeof pkg.version === "string") return pkg.version;
73
- } catch {
74
- }
75
- const parent = dirname(dir);
76
- if (parent === dir) break;
77
- dir = parent;
78
- }
79
- } catch {
80
- }
81
- try {
82
- const req = createRequire(import.meta.url ?? __filename);
83
- const pkg = req("../../package.json");
84
- if (pkg?.version) return pkg.version;
85
- } catch {
86
- }
87
- return "0.0.0";
88
- }
89
- async function runCheck(packageName) {
90
- const disabled = isAutoUpdaterDisabled();
91
- if (disabled) {
92
- _disabledReason = disabled;
93
- return;
94
- }
95
- _disabledReason = null;
96
- try {
97
- const currentVersion = getCurrentVersion();
98
- const latestVersion = await getLatestVersion(packageName);
99
- if (!latestVersion) return;
100
- let isNewer = false;
101
- try {
102
- isNewer = gt(latestVersion, currentVersion);
103
- } catch {
104
- isNewer = latestVersion !== currentVersion;
105
- }
106
- if (!isNewer) {
107
- _updateInfo = void 0;
108
- return;
109
- }
110
- const { hasPermissions, npmPrefix } = await checkGlobalInstallPermissions();
111
- _updateInfo = { latestVersion, canAutoInstall: hasPermissions, npmPrefix };
112
- } catch {
113
- }
114
- }
115
- async function getLatestVersion(packageName) {
116
- try {
117
- const { stdout } = await execFileAsync(
118
- "npm",
119
- ["view", `${packageName}@latest`, "version", "--prefer-online"],
120
- { cwd: homedir(), timeout: NPM_VIEW_TIMEOUT_MS }
121
- );
122
- const v = stdout.trim();
123
- return v.length > 0 ? v : null;
124
- } catch {
125
- return null;
126
- }
127
- }
128
- async function checkGlobalInstallPermissions() {
129
- let prefix = null;
130
- try {
131
- const { stdout } = await execFileAsync(
132
- "npm",
133
- ["-g", "config", "get", "prefix"],
134
- { cwd: homedir(), timeout: 5e3 }
135
- );
136
- prefix = stdout.trim();
137
- } catch {
138
- return { hasPermissions: false, npmPrefix: null };
139
- }
140
- if (!prefix) return { hasPermissions: false, npmPrefix: null };
141
- try {
142
- await access(prefix, fsConstants.W_OK);
143
- return { hasPermissions: true, npmPrefix: prefix };
144
- } catch {
145
- return { hasPermissions: false, npmPrefix: prefix };
146
- }
147
- }
148
- function userHome() {
149
- return process.env.HOME ?? homedir();
150
- }
151
- function configDir() {
152
- return join(userHome(), ".code-shell");
153
- }
154
- function getLockFilePath() {
155
- return join(configDir(), LOCK_FILE);
156
- }
157
- function getUpdateLogPath() {
158
- return join(configDir(), "update.log");
159
- }
160
- async function acquireLock() {
161
- const lockPath = getLockFilePath();
162
- try {
163
- const s = await stat(lockPath);
164
- if (Date.now() - s.mtimeMs < LOCK_TIMEOUT_MS) return false;
165
- try {
166
- const recheck = await stat(lockPath);
167
- if (Date.now() - recheck.mtimeMs < LOCK_TIMEOUT_MS) return false;
168
- await unlink(lockPath);
169
- } catch (err) {
170
- if (!isENOENT(err)) return false;
171
- }
172
- } catch (err) {
173
- if (!isENOENT(err)) return false;
174
- }
175
- try {
176
- await writeFile(lockPath, String(process.pid), { encoding: "utf8", flag: "wx" });
177
- return true;
178
- } catch (err) {
179
- const code = errnoCode(err);
180
- if (code === "EEXIST") return false;
181
- if (code === "ENOENT") {
182
- try {
183
- await mkdir(configDir(), { recursive: true });
184
- await writeFile(lockPath, String(process.pid), { encoding: "utf8", flag: "wx" });
185
- return true;
186
- } catch {
187
- return false;
188
- }
189
- }
190
- return false;
191
- }
192
- }
193
- async function releaseLock() {
194
- const lockPath = getLockFilePath();
195
- try {
196
- const data = await readFile(lockPath, "utf8");
197
- if (data === String(process.pid)) await unlink(lockPath);
198
- } catch {
199
- }
200
- }
201
- function launchDetachedInstall(version) {
202
- const lockPath = getLockFilePath();
203
- try {
204
- try {
205
- const fs2 = __require("fs");
206
- fs2.mkdirSync(configDir(), { recursive: true });
207
- fs2.writeFileSync(lockPath, String(process.pid), { encoding: "utf8", flag: "wx" });
208
- } catch (err) {
209
- if (errnoCode(err) === "EEXIST") {
210
- try {
211
- const fs2 = __require("fs");
212
- const s = fs2.statSync(lockPath);
213
- if (Date.now() - s.mtimeMs < LOCK_TIMEOUT_MS) return;
214
- fs2.unlinkSync(lockPath);
215
- fs2.writeFileSync(lockPath, String(process.pid), { encoding: "utf8", flag: "wx" });
216
- } catch {
217
- return;
218
- }
219
- } else {
220
- return;
221
- }
222
- }
223
- } catch {
224
- return;
225
- }
226
- const fs = __require("fs");
227
- const logFd = fs.openSync(getUpdateLogPath(), "a");
228
- try {
229
- fs.writeSync(
230
- logFd,
231
- `
232
- [${(/* @__PURE__ */ new Date()).toISOString()}] code-shell auto-update: installing ${PACKAGE_NAME}@${version} (pid=${process.pid})
233
- `
234
- );
235
- } catch {
236
- }
237
- const cmd = [
238
- `npm install -g ${PACKAGE_NAME}@${shellEscape(version)}`,
239
- `; rm -f ${shellEscape(lockPath)}`
240
- ].join(" ");
241
- try {
242
- const child = spawn("sh", ["-c", cmd], {
243
- detached: true,
244
- stdio: ["ignore", logFd, logFd],
245
- cwd: homedir()
246
- });
247
- child.unref();
248
- } catch {
249
- try {
250
- fs.unlinkSync(lockPath);
251
- } catch {
252
- }
253
- }
254
- }
255
- function isAutoUpdaterDisabled() {
256
- if (process.env.NODE_ENV === "development") return "development build";
257
- if (isEnvTruthy(process.env.DISABLE_AUTOUPDATER)) return "DISABLE_AUTOUPDATER set";
258
- if (readAutoUpdatesFlagFromDisk() === false) return "settings.autoUpdates = false";
259
- return null;
260
- }
261
- function readAutoUpdatesFlagFromDisk() {
262
- const candidates = [
263
- join(userHome(), ".code-shell", "settings.json"),
264
- join(userHome(), ".claude", "settings.json")
265
- ];
266
- for (const path of candidates) {
267
- try {
268
- const raw = readFileSync(path, "utf8");
269
- const parsed = JSON.parse(raw);
270
- if (typeof parsed.autoUpdates === "boolean") return parsed.autoUpdates;
271
- } catch {
272
- }
273
- }
274
- return void 0;
275
- }
276
- function isEnvTruthy(v) {
277
- if (!v) return false;
278
- const lower = v.toLowerCase();
279
- return lower === "1" || lower === "true" || lower === "yes" || lower === "on";
280
- }
281
- function errnoCode(err) {
282
- return typeof err === "object" && err !== null && "code" in err ? String(err.code) : void 0;
283
- }
284
- function isENOENT(err) {
285
- return errnoCode(err) === "ENOENT";
286
- }
287
- function shellEscape(s) {
288
- return `'${s.replace(/'/g, `'\\''`)}'`;
289
- }
290
- var __internal = { acquireLock, releaseLock, getLockFilePath };
291
-
292
- export {
293
- checkForUpdate,
294
- getUpdateAvailable,
295
- getAutoUpdateDisabledReason,
296
- scheduleAutoInstallOnExit,
297
- getCurrentVersion,
298
- __internal
299
- };
@@ -1,215 +0,0 @@
1
- import {
2
- isStale,
3
- readCache,
4
- writeCache
5
- } from "./chunk-3OA5G4TM.js";
6
- import {
7
- init_esm_shims
8
- } from "./chunk-MD32R22I.js";
9
-
10
- // src/llm/model-fetcher.ts
11
- init_esm_shims();
12
- import { readFileSync, existsSync } from "fs";
13
- import { fileURLToPath } from "url";
14
- import { dirname, join } from "path";
15
-
16
- // src/llm/provider-kinds.ts
17
- init_esm_shims();
18
- var NON_CHAT_PATTERNS = /(?:^|[-_/])(?:embed(?:ding)?|whisper|tts|audio|image|dall-?e|moderation|rerank|guard|vision-only)(?:$|[-_/])/i;
19
- var isChatLike = (id) => !NON_CHAT_PATTERNS.test(id);
20
- var bearer = (k) => k ? { Authorization: `Bearer ${k}` } : {};
21
- var PROVIDER_KINDS = {
22
- openai: {
23
- label: "OpenAI",
24
- defaultBaseUrl: "https://api.openai.com/v1",
25
- modelsPath: "/models",
26
- protocol: "openai-compat",
27
- authHeader: bearer,
28
- chatFilter: isChatLike
29
- },
30
- anthropic: {
31
- label: "Anthropic",
32
- defaultBaseUrl: "https://api.anthropic.com/v1",
33
- modelsPath: "/models",
34
- protocol: "anthropic-style",
35
- authHeader: (k) => k ? { "x-api-key": k, "anthropic-version": "2023-06-01" } : { "anthropic-version": "2023-06-01" },
36
- chatFilter: isChatLike
37
- },
38
- deepseek: {
39
- label: "DeepSeek",
40
- defaultBaseUrl: "https://api.deepseek.com/v1",
41
- modelsPath: "/models",
42
- protocol: "openai-compat",
43
- authHeader: bearer,
44
- chatFilter: isChatLike
45
- },
46
- xai: {
47
- label: "xAI (Grok)",
48
- defaultBaseUrl: "https://api.x.ai/v1",
49
- modelsPath: "/models",
50
- protocol: "openai-compat",
51
- authHeader: bearer,
52
- chatFilter: isChatLike
53
- },
54
- mistral: {
55
- label: "Mistral",
56
- defaultBaseUrl: "https://api.mistral.ai/v1",
57
- modelsPath: "/models",
58
- protocol: "openai-compat",
59
- authHeader: bearer,
60
- chatFilter: isChatLike
61
- },
62
- groq: {
63
- label: "Groq",
64
- defaultBaseUrl: "https://api.groq.com/openai/v1",
65
- modelsPath: "/models",
66
- protocol: "openai-compat",
67
- authHeader: bearer,
68
- chatFilter: isChatLike
69
- },
70
- google: {
71
- label: "Google Gemini",
72
- defaultBaseUrl: "https://generativelanguage.googleapis.com/v1beta",
73
- modelsPath: "/models",
74
- protocol: "gemini",
75
- authHeader: () => ({}),
76
- authQuery: (k) => k ? { key: k } : {},
77
- chatFilter: (id) => isChatLike(id) && /gemini/i.test(id)
78
- },
79
- openrouter: {
80
- label: "OpenRouter",
81
- defaultBaseUrl: "https://openrouter.ai/api/v1",
82
- modelsPath: "/models",
83
- protocol: "openai-compat",
84
- authHeader: bearer,
85
- chatFilter: isChatLike
86
- },
87
- ollama: {
88
- label: "Ollama (local)",
89
- defaultBaseUrl: "http://localhost:11434",
90
- modelsPath: "/api/tags",
91
- protocol: "ollama",
92
- authHeader: () => ({}),
93
- chatFilter: isChatLike
94
- },
95
- custom: {
96
- label: "Custom",
97
- defaultBaseUrl: "",
98
- modelsPath: "/models",
99
- protocol: "openai-compat",
100
- authHeader: bearer,
101
- chatFilter: isChatLike
102
- }
103
- };
104
- function getKindMeta(kind) {
105
- return PROVIDER_KINDS[kind] ?? PROVIDER_KINDS.custom;
106
- }
107
-
108
- // src/llm/model-fetcher.ts
109
- var HERE = dirname(fileURLToPath(import.meta.url));
110
- var OPENROUTER_SNAPSHOT = join(HERE, "..", "data", "openrouter-models.json");
111
- async function fetchModelList(provider, opts) {
112
- const meta = getKindMeta(provider.kind);
113
- if (!opts.refresh) {
114
- const cached = readCache(opts.cacheDir, provider.key);
115
- if (cached && !isStale(cached)) return { ...cached, fromCache: true };
116
- }
117
- if (provider.kind === "openrouter") {
118
- return loadOpenRouterSnapshot(provider.key, opts.cacheDir);
119
- }
120
- try {
121
- const path = provider.modelsPath ?? meta.modelsPath;
122
- const url = new URL(joinUrl(provider.baseUrl, path));
123
- const headers = meta.authHeader(provider.apiKey ?? "");
124
- if (meta.authQuery) {
125
- const q = meta.authQuery(provider.apiKey ?? "");
126
- for (const [k, v] of Object.entries(q)) url.searchParams.set(k, v);
127
- }
128
- const res = await fetch(url.toString(), {
129
- headers: { Accept: "application/json", ...headers },
130
- signal: AbortSignal.timeout(opts.timeoutMs ?? 2e4)
131
- });
132
- if (!res.ok) {
133
- return errorResult(provider.key, opts.cacheDir, `HTTP ${res.status}`);
134
- }
135
- const payload = await res.json();
136
- const raw = normalize(payload, meta.protocol);
137
- const filtered = raw.filter((m) => meta.chatFilter(m.id));
138
- const file = writeCache(opts.cacheDir, provider.key, filtered);
139
- return file;
140
- } catch (err) {
141
- const msg = err.message || String(err);
142
- return errorResult(provider.key, opts.cacheDir, msg);
143
- }
144
- }
145
- function normalize(payload, protocol) {
146
- switch (protocol) {
147
- case "ollama": {
148
- const p = payload;
149
- return (p.models ?? []).map((m) => ({ id: m.name, contextLength: 0, maxOutputTokens: 0 }));
150
- }
151
- case "anthropic-style": {
152
- const p = payload;
153
- return (p.data ?? []).map((m) => ({
154
- id: m.id,
155
- contextLength: m.context_window ?? 2e5,
156
- maxOutputTokens: m.max_output_tokens ?? 0
157
- }));
158
- }
159
- case "gemini": {
160
- const p = payload;
161
- return (p.models ?? []).map((m) => ({
162
- id: m.name.replace(/^models\//, ""),
163
- contextLength: m.inputTokenLimit ?? 0,
164
- maxOutputTokens: m.outputTokenLimit ?? 0
165
- }));
166
- }
167
- case "openai-compat": {
168
- const p = payload;
169
- return (p.data ?? []).map((m) => ({
170
- id: m.id,
171
- contextLength: m.context_window ?? m.context_length ?? 0,
172
- maxOutputTokens: m.max_completion_tokens ?? 0
173
- }));
174
- }
175
- }
176
- const _exhaustive = protocol;
177
- return _exhaustive;
178
- }
179
- function loadOpenRouterSnapshot(providerKey, cacheDir) {
180
- if (!existsSync(OPENROUTER_SNAPSHOT)) {
181
- return errorResult(providerKey, cacheDir, "openrouter snapshot missing");
182
- }
183
- try {
184
- const snapshot = JSON.parse(
185
- readFileSync(OPENROUTER_SNAPSHOT, "utf-8")
186
- );
187
- const models = (snapshot.models ?? []).map((m) => ({
188
- id: m.id,
189
- contextLength: m.contextLength ?? 0,
190
- maxOutputTokens: m.maxOutputTokens ?? 0
191
- }));
192
- return writeCache(cacheDir, providerKey, models);
193
- } catch (err) {
194
- return errorResult(providerKey, cacheDir, err.message);
195
- }
196
- }
197
- function errorResult(providerKey, cacheDir, error) {
198
- const stale = readCache(cacheDir, providerKey);
199
- if (stale) return { ...stale, error, fromCache: true };
200
- return {
201
- fetchedAt: (/* @__PURE__ */ new Date()).toISOString(),
202
- providerKey,
203
- models: [],
204
- error
205
- };
206
- }
207
- function joinUrl(base, path) {
208
- if (!base) return path;
209
- return `${base.replace(/\/$/, "")}${path.startsWith("/") ? "" : "/"}${path}`;
210
- }
211
-
212
- export {
213
- PROVIDER_KINDS,
214
- fetchModelList
215
- };