@wirecat/drive-cli 0.63.0 → 0.65.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 (193) hide show
  1. package/README.md +2 -1
  2. package/dist/bin/drive.js +2 -2
  3. package/dist/bundle/{all-profiles-GZJS3UKC.js → all-profiles-PD7GJHFA.js} +14 -13
  4. package/dist/bundle/{anthropic-RGV3WXX5.js → anthropic-PJKFON5H.js} +25 -14
  5. package/dist/bundle/{app-server-AA2PJ7PV.js → app-server-6ELQA6FJ.js} +59 -56
  6. package/dist/bundle/{bun-OXZYIA3R.js → bun-P54TGES2.js} +2 -2
  7. package/dist/bundle/{bun-sqlite-D45NSDM3.js → bun-sqlite-TND5ZRJH.js} +12 -8
  8. package/dist/bundle/chunk-2AFI4KGI.js +185 -0
  9. package/dist/bundle/{chunk-Q5DJTRKV.js → chunk-3HXNDF6Q.js} +2 -2
  10. package/dist/bundle/{chunk-DJHLVUL6.js → chunk-3ONWN3IA.js} +278 -49
  11. package/dist/bundle/{chunk-WYGWWHZP.js → chunk-42G2XNPN.js} +2 -2
  12. package/dist/bundle/chunk-4AOINPN2.js +66 -0
  13. package/dist/bundle/{chunk-J4DIHPZ6.js → chunk-4EDWLR22.js} +1 -1
  14. package/dist/bundle/{chunk-ABYGSJZ4.js → chunk-4Y5YBFMS.js} +7 -7
  15. package/dist/bundle/{chunk-YGU6ULU5.js → chunk-54V4H6PM.js} +16 -16
  16. package/dist/bundle/{chunk-2TFXK3R4.js → chunk-5XSBCMAF.js} +4 -4
  17. package/dist/bundle/{chunk-2HF2MRXI.js → chunk-6DCLZLGX.js} +1 -1
  18. package/dist/bundle/{chunk-OD4U7GUY.js → chunk-7AQGVX5O.js} +20 -20
  19. package/dist/bundle/{chunk-YIP7ELON.js → chunk-7TAP7Y2E.js} +727 -518
  20. package/dist/bundle/{chunk-TF23BZW5.js → chunk-A27UVLPK.js} +3 -3
  21. package/dist/bundle/{chunk-LZEOQW7S.js → chunk-A4QNADEX.js} +58 -52
  22. package/dist/bundle/{chunk-FXBHOCJK.js → chunk-ADLS6FVG.js} +1 -1
  23. package/dist/bundle/{chunk-LMAVCYLQ.js → chunk-B2LPCHQN.js} +4 -4
  24. package/dist/bundle/{chunk-5PPL7JDM.js → chunk-CGQRZRTH.js} +2 -2
  25. package/dist/bundle/{chunk-VAUMOZU5.js → chunk-CWLL63JD.js} +107 -48
  26. package/dist/bundle/{chunk-V3NVXIQR.js → chunk-D6Z43SOG.js} +1563 -266
  27. package/dist/bundle/{chunk-OXY6SSZG.js → chunk-D7WB54P7.js} +127 -28
  28. package/dist/bundle/{chunk-VJEOGKAN.js → chunk-DA63E2UM.js} +13 -9
  29. package/dist/bundle/{chunk-EOOGWUTP.js → chunk-ECEP4SO3.js} +20 -20
  30. package/dist/bundle/{chunk-NEFSGCVA.js → chunk-EHU6EYGO.js} +38 -28
  31. package/dist/bundle/{chunk-4OSSOWDM.js → chunk-ELJUBPY4.js} +1 -1
  32. package/dist/bundle/{chunk-FRIJUILO.js → chunk-EM7NL5ES.js} +3 -3
  33. package/dist/bundle/{chunk-3BZRKLJ6.js → chunk-EU5MD24W.js} +13 -13
  34. package/dist/bundle/{chunk-MOTRI3SY.js → chunk-EZ2OLYEK.js} +24 -2
  35. package/dist/bundle/{chunk-6MQSQ4LX.js → chunk-F56DRBGY.js} +370 -717
  36. package/dist/bundle/{chunk-ZOJU7ZFI.js → chunk-FA6JNXCC.js} +8 -8
  37. package/dist/bundle/{chunk-ND6LLYGF.js → chunk-FBHSWQU7.js} +1531 -136
  38. package/dist/bundle/{chunk-Z42YYLTS.js → chunk-GW3LBY3T.js} +74 -72
  39. package/dist/bundle/{chunk-Y6GNA7HE.js → chunk-HP3NT6JJ.js} +23 -5
  40. package/dist/bundle/{chunk-DJ3SDJZY.js → chunk-I272E6UR.js} +9 -8
  41. package/dist/bundle/{chunk-L37LMMRJ.js → chunk-IBO23OID.js} +2 -2
  42. package/dist/bundle/{chunk-CE2TJBLP.js → chunk-II4S2DBJ.js} +3 -3
  43. package/dist/bundle/{chunk-ZM5J6ZMB.js → chunk-IKHOIXJR.js} +2 -2
  44. package/dist/bundle/{chunk-PO5RQQIG.js → chunk-IMHW7VIV.js} +5 -5
  45. package/dist/bundle/{chunk-3HK5WMI5.js → chunk-JMJIH6JH.js} +6 -6
  46. package/dist/bundle/{chunk-FUITWY7O.js → chunk-JPPPLKAO.js} +26 -1
  47. package/dist/bundle/{chunk-4S4JMW6I.js → chunk-LMKPP2JP.js} +1 -1
  48. package/dist/bundle/{chunk-HZZCEQTH.js → chunk-M7ELUGU6.js} +4 -4
  49. package/dist/bundle/{chunk-ZMOUUOT2.js → chunk-MCPOZNWG.js} +1 -1
  50. package/dist/bundle/{chunk-XNVFKMVR.js → chunk-NI5PD5KM.js} +39485 -35729
  51. package/dist/bundle/{chunk-2TDUGC5D.js → chunk-NQ5XMSLA.js} +4927 -1978
  52. package/dist/bundle/chunk-NQRTKYJB.js +257 -0
  53. package/dist/bundle/{chunk-44SBK6KB.js → chunk-O6K2E7OA.js} +4201 -2565
  54. package/dist/bundle/{chunk-534OBX5Y.js → chunk-O7ZDCOZE.js} +1 -1
  55. package/dist/bundle/{chunk-7EMQ26BA.js → chunk-OFJXHEFT.js} +13 -13
  56. package/dist/bundle/chunk-OWMZUWCM.js +109 -0
  57. package/dist/bundle/chunk-PHOL2DZM.js +138 -0
  58. package/dist/bundle/{chunk-UQLWMGSD.js → chunk-PLP5527C.js} +5 -5
  59. package/dist/bundle/{chunk-3QINVLZI.js → chunk-RNHGQ35R.js} +1 -1
  60. package/dist/bundle/{chunk-27YUUGXQ.js → chunk-RX5CJ55S.js} +1 -1
  61. package/dist/bundle/{chunk-XIUDS3OK.js → chunk-SE352DBY.js} +1 -1
  62. package/dist/bundle/chunk-SEQWBZYH.js +18 -0
  63. package/dist/bundle/{chunk-33MGFUED.js → chunk-SIWXYB4E.js} +3789 -3538
  64. package/dist/bundle/{chunk-4CIZWCXV.js → chunk-SNLSEYJO.js} +1 -1
  65. package/dist/bundle/{chunk-XDC3TWTL.js → chunk-SS4LKLD7.js} +52 -605
  66. package/dist/bundle/{chunk-BKYMKVHT.js → chunk-TMHF4BCL.js} +2 -2
  67. package/dist/bundle/{chunk-FEZF3TII.js → chunk-TOVWMURV.js} +74 -6
  68. package/dist/bundle/{chunk-QCKZ3GAA.js → chunk-TTP4NOP3.js} +4 -4
  69. package/dist/bundle/chunk-UTNRBOEN.js +53 -0
  70. package/dist/bundle/{chunk-2NIC72HH.js → chunk-VDZY3L7U.js} +3 -1
  71. package/dist/bundle/chunk-VHQYWFWV.js +13 -0
  72. package/dist/bundle/{chunk-VKPCONDQ.js → chunk-VLXWAH5E.js} +4 -4
  73. package/dist/bundle/{chunk-6KQJSFYF.js → chunk-VMAMRFFC.js} +23 -23
  74. package/dist/bundle/{chunk-6EY46GAN.js → chunk-W2XY6C5S.js} +2 -2
  75. package/dist/bundle/{chunk-J24ZFCWM.js → chunk-WRAUO5EJ.js} +22 -24
  76. package/dist/bundle/{chunk-S4RXABDR.js → chunk-WRMJN6UQ.js} +3 -3
  77. package/dist/bundle/{chunk-UPOSDTPT.js → chunk-WVJHZIDH.js} +3 -3
  78. package/dist/bundle/{chunk-E4SDKMBJ.js → chunk-X7YGGWXI.js} +1 -59
  79. package/dist/bundle/{chunk-O2XOX4HB.js → chunk-XPWZSW6Y.js} +50 -34
  80. package/dist/bundle/{chunk-PDCCYHCH.js → chunk-XYE7XZPB.js} +5 -5
  81. package/dist/bundle/{chunk-RKELRWH7.js → chunk-Y5PKRITX.js} +5 -5
  82. package/dist/bundle/{chunk-XR6ESRU6.js → chunk-Z2ADO6UA.js} +2 -2
  83. package/dist/bundle/{chunk-YUWPNG7B.js → chunk-ZD4MB7NH.js} +1 -1
  84. package/dist/bundle/{chunk-TWUH22Z5.js → chunk-ZILALBYQ.js} +4 -4
  85. package/dist/bundle/{chunk-SXM6MHRR.js → chunk-ZR2EJPMQ.js} +44 -15
  86. package/dist/bundle/command-contract-25E4PYMD.js +40 -0
  87. package/dist/bundle/command-server-entry-36JXYIJB.js +170 -0
  88. package/dist/bundle/{decode-G5YEIPNC.js → decode-T5ZW57WZ.js} +8 -8
  89. package/dist/bundle/{dispatch-GEFIXA6X.js → dispatch-RMA2XFYA.js} +11 -11
  90. package/dist/bundle/drive.js +11 -11
  91. package/dist/bundle/{echarts-W2CWHNXR.js → echarts-KQ44LPSF.js} +2 -2
  92. package/dist/bundle/{embed-OBFFPVF4.js → embed-Q7XST53J.js} +9 -9
  93. package/dist/bundle/{embedder-NGCBQHIG.js → embedder-RWZSBVW2.js} +12 -12
  94. package/dist/bundle/entry-HPLW4QL2.js +25 -0
  95. package/dist/bundle/{failure-GV77W3CD.js → failure-7NP4QPIN.js} +4 -4
  96. package/dist/bundle/first-run-RL3RKYZW.js +30 -0
  97. package/dist/bundle/first-screen-NX3TM2VJ.js +38 -0
  98. package/dist/bundle/google-DPD5N6AQ.js +42 -0
  99. package/dist/bundle/{google-api-Z3NSTBBR.js → google-api-P5DRHD7L.js} +61 -56
  100. package/dist/bundle/grants-TX4MHLI4.js +109 -0
  101. package/dist/bundle/{identity-OM6GFZ7T.js → identity-VDV6CHRW.js} +6 -6
  102. package/dist/bundle/{instructions-YQBQSYSS.js → instructions-Q45FVY6H.js} +1 -1
  103. package/dist/bundle/{login-7RYSDVN3.js → login-XKN3NI6X.js} +67 -62
  104. package/dist/bundle/{mcp-GWDA3HOA.js → mcp-7KQUJNFN.js} +2 -2
  105. package/dist/bundle/{node-IVU33RL7.js → node-MSW565D3.js} +2 -2
  106. package/dist/bundle/{node-KM3CUABH.js → node-P4E63KXA.js} +9 -9
  107. package/dist/bundle/{node-sqlite-SIH5BEW2.js → node-sqlite-S74XVVHL.js} +13 -9
  108. package/dist/bundle/oauth-6R7AWAES.js +22 -0
  109. package/dist/bundle/{open-IAO2SNII.js → open-P7UQG7XJ.js} +8 -8
  110. package/dist/bundle/{openai-SGOUBHXP.js → openai-DDHJ57KZ.js} +19 -40
  111. package/dist/bundle/{process-BPBMV3AT.js → process-NMUB5SHK.js} +1 -1
  112. package/dist/bundle/{program-MYKKJ4F6.js → program-PJNWP6JW.js} +517 -330
  113. package/dist/bundle/prompt-VOSMK4C2.js +19 -0
  114. package/dist/bundle/{reasons-57J2D77K.js → reasons-KDAXOC5S.js} +5 -5
  115. package/dist/bundle/{recognize-A5C5RPB3.js → recognize-GZIY63HS.js} +7 -7
  116. package/dist/bundle/{remote-VWYFLRGO.js → remote-V3YGN7LN.js} +10 -10
  117. package/dist/bundle/{rerank-4NGQWREA.js → rerank-XRU6AVN7.js} +10 -10
  118. package/dist/bundle/{serve-Y6LQTFH5.js → serve-NDOAFA57.js} +12 -12
  119. package/dist/bundle/{server-6AUHIIUA.js → server-BKGWJYMC.js} +46 -44
  120. package/dist/bundle/server-QD2MUJJZ.js +72 -0
  121. package/dist/bundle/{services-2W4J7EMW.js → services-TYDMFQCA.js} +129 -39
  122. package/dist/bundle/{sqlite-runtime-P6BBDX2H.js → sqlite-runtime-UAFJKSJR.js} +9 -9
  123. package/dist/bundle/{start-AFFA3LKM.js → start-2ZLDP2OE.js} +30 -8
  124. package/dist/bundle/{state-PXKKXR3R.js → state-6SP27J2K.js} +9 -9
  125. package/dist/bundle/{store-CN6JEUC4.js → store-4UUY545Q.js} +42 -18
  126. package/dist/bundle/{store-IVV2BAGZ.js → store-EJN27MXI.js} +69 -28
  127. package/dist/bundle/store-maintenance-command-LETURZDC.js +60 -0
  128. package/dist/bundle/tokens-MTFJPTYT.js +107 -0
  129. package/dist/commands/account.d.ts +4 -0
  130. package/dist/commands/account.d.ts.map +1 -0
  131. package/dist/commands/account.js +33 -0
  132. package/dist/commands/account.js.map +1 -0
  133. package/dist/commands/config.d.ts.map +1 -1
  134. package/dist/commands/config.js +6 -5
  135. package/dist/commands/config.js.map +1 -1
  136. package/dist/commands/context.d.ts +4 -2
  137. package/dist/commands/context.d.ts.map +1 -1
  138. package/dist/commands/context.js.map +1 -1
  139. package/dist/commands/documents.d.ts +2 -0
  140. package/dist/commands/documents.d.ts.map +1 -1
  141. package/dist/commands/documents.js +39 -3
  142. package/dist/commands/documents.js.map +1 -1
  143. package/dist/config.d.ts +13 -0
  144. package/dist/config.d.ts.map +1 -1
  145. package/dist/config.js +32 -2
  146. package/dist/config.js.map +1 -1
  147. package/dist/documents-add.d.ts +49 -0
  148. package/dist/documents-add.d.ts.map +1 -0
  149. package/dist/documents-add.js +122 -0
  150. package/dist/documents-add.js.map +1 -0
  151. package/dist/errors.d.ts +1 -0
  152. package/dist/errors.d.ts.map +1 -1
  153. package/dist/errors.js +9 -1
  154. package/dist/errors.js.map +1 -1
  155. package/dist/knowledge.d.ts +1 -1
  156. package/dist/knowledge.d.ts.map +1 -1
  157. package/dist/knowledge.js +15 -17
  158. package/dist/knowledge.js.map +1 -1
  159. package/dist/model.d.ts +3 -0
  160. package/dist/model.d.ts.map +1 -1
  161. package/dist/model.js.map +1 -1
  162. package/dist/profile-lock.d.ts +1 -0
  163. package/dist/profile-lock.d.ts.map +1 -1
  164. package/dist/profile-lock.js +1 -0
  165. package/dist/profile-lock.js.map +1 -1
  166. package/dist/program.d.ts.map +1 -1
  167. package/dist/program.js +24 -15
  168. package/dist/program.js.map +1 -1
  169. package/dist/reasons.d.ts.map +1 -1
  170. package/dist/reasons.js +25 -0
  171. package/dist/reasons.js.map +1 -1
  172. package/dist/store.d.ts.map +1 -1
  173. package/dist/store.js +20 -0
  174. package/dist/store.js.map +1 -1
  175. package/dist/version.d.ts +1 -1
  176. package/dist/version.js +1 -1
  177. package/package.json +4 -4
  178. package/dist/bundle/chunk-AEX37ETW.js +0 -35
  179. package/dist/bundle/chunk-OTROVC4E.js +0 -62
  180. package/dist/bundle/chunk-Z5KC7XBR.js +0 -33
  181. package/dist/bundle/command-contract-BRW3AP44.js +0 -38
  182. package/dist/bundle/entry-ET6HGM7A.js +0 -22
  183. package/dist/bundle/first-run-JG52UZVO.js +0 -30
  184. package/dist/bundle/first-screen-CIL3GFAA.js +0 -36
  185. package/dist/bundle/google-CIWUVJ5C.js +0 -36
  186. package/dist/bundle/grants-3S4RL3WB.js +0 -104
  187. package/dist/bundle/oauth-PT4JEHLL.js +0 -22
  188. package/dist/bundle/prompt-3ZM64DCB.js +0 -17
  189. package/dist/bundle/server-PFHI35HA.js +0 -69
  190. package/dist/bundle/store-maintenance-command-MSY2QNEH.js +0 -59
  191. package/dist/bundle/tokens-P4LPNUFW.js +0 -102
  192. package/dist/bundle/{chunk-ZQ3GWKDQ.js → chunk-YVNHQFLC.js} +0 -0
  193. package/dist/bundle/{mdast-util-from-markdown-FXEPSNER.js → mdast-util-from-markdown-CE73EWJ2.js} +3 -3
@@ -0,0 +1,257 @@
1
+ import { createRequire as __bundleRequire } from "node:module";
2
+ const require = __bundleRequire(import.meta.url);
3
+ import {
4
+ embeddingKeys,
5
+ endpointKeyName,
6
+ modelGateway
7
+ } from "./chunk-FBHSWQU7.js";
8
+ import {
9
+ endpoint
10
+ } from "./chunk-OWMZUWCM.js";
11
+ import {
12
+ readSharedSection,
13
+ sharedConfigPath,
14
+ writeSharedSection
15
+ } from "./chunk-4Y5YBFMS.js";
16
+ import {
17
+ storePath
18
+ } from "./chunk-UTNRBOEN.js";
19
+ import {
20
+ Credentials,
21
+ isMovedRun,
22
+ resolvePaths
23
+ } from "./chunk-7AQGVX5O.js";
24
+ import {
25
+ check,
26
+ getDotPath,
27
+ maxLength,
28
+ minLength,
29
+ optional,
30
+ picklist,
31
+ pipe,
32
+ safeParse,
33
+ strictObject,
34
+ string,
35
+ trim
36
+ } from "./chunk-TOVWMURV.js";
37
+ import {
38
+ CliError,
39
+ isCliError
40
+ } from "./chunk-ELJUBPY4.js";
41
+ import {
42
+ __name
43
+ } from "./chunk-ZVVMGPBJ.js";
44
+
45
+ // dist/errors.js
46
+ var googleError = /* @__PURE__ */ __name((error) => {
47
+ if (isCliError(error))
48
+ return error;
49
+ const value = error;
50
+ if (value?.name === "AbortError" || value?.code === "ERR_CANCELED")
51
+ return new CliError("cancelled", "Google operation was cancelled", { reason: "command.cancelled" });
52
+ const status = value?.status ?? value?.response?.status;
53
+ const payload = value?.response?.data?.error;
54
+ const reason = typeof payload === "string" ? payload : payload?.errors?.[0]?.reason;
55
+ const told = status === 403 && typeof reason === "string" && /^[A-Za-z]{1,64}$/.test(reason) ? { google_reason: reason } : {};
56
+ const disabled = typeof payload === "object" && payload !== null && Array.isArray(payload.details) && payload.details.some((detail) => detail !== null && typeof detail === "object" && "@type" in detail && detail["@type"] === "type.googleapis.com/google.rpc.ErrorInfo" && "domain" in detail && detail.domain === "googleapis.com" && "reason" in detail && detail.reason === "SERVICE_DISABLED");
57
+ if (reason === "invalid_grant" || reason === "invalid_token")
58
+ return new CliError("authentication_error", "Google sign-in is no longer valid \u2014 run drive <profile> session start again", {
59
+ reason: "oauth.expired"
60
+ });
61
+ if (reason === "invalid_client")
62
+ return new CliError("configuration_error", "Google rejected the OAuth application; check its Desktop client setup", {
63
+ reason: "sign-in.client-rejected"
64
+ });
65
+ if (status === 403 && (reason === "accessNotConfigured" || disabled))
66
+ return new CliError("configuration_error", "Google API is disabled for this OAuth project; its owner must enable it in Google Cloud API Library", { reason: "api.disabled", status });
67
+ if (reason === "rateLimitExceeded" || reason === "userRateLimitExceeded" || reason === "dailyLimitExceeded" || reason === "quotaExceeded")
68
+ return new CliError("rate_limited", "Google rate limit reached; retry the command", {
69
+ retryable: true,
70
+ ...status ? { status } : {},
71
+ ...told
72
+ });
73
+ if (reason === "exportSizeLimitExceeded")
74
+ return new CliError("validation_error", "Google exports at most 10 MB of a native file; this one is larger", {
75
+ reason: "export.too-large"
76
+ });
77
+ if (status === 401)
78
+ return new CliError("authentication_error", "Google sign-in expired \u2014 run drive <profile> session start again", {
79
+ reason: "oauth.expired"
80
+ });
81
+ if (status === 403)
82
+ return new CliError("permission_error", "Google denied this operation; check access and OAuth permissions", {
83
+ status,
84
+ ...told
85
+ });
86
+ if (status === 404)
87
+ return new CliError("not_found", "Google source is unavailable or no longer accessible", { status });
88
+ if (status === 429)
89
+ return new CliError("rate_limited", "Google rate limit reached; retry the command", { retryable: true, status });
90
+ if (status !== void 0)
91
+ return new CliError("provider_error", `Google request failed (HTTP ${status})`, { status });
92
+ return new CliError("provider_unavailable", "Google request failed; no request or credential details were logged", {
93
+ retryable: true
94
+ });
95
+ }, "googleError");
96
+ var offlineRefused = /* @__PURE__ */ __name((what = "this command may require network access") => new CliError("permission_error", `${what}; --offline was requested`, { reason: "offline.refused" }), "offlineRefused");
97
+
98
+ // node_modules/.pnpm/@wirecat+cli-core@0.634.0_@wirecat+cli-base@0.93.0_commander@15.0.0_esbuild@0.28.2_type_795ed7a67bda78d624888739490b3720/node_modules/@wirecat/cli-core/dist/models/ai.js
99
+ var AI_PROVIDERS = ["anthropic", "openai", "ollama"];
100
+ var DEFAULT_BASE_URL = {
101
+ anthropic: "https://api.anthropic.com",
102
+ openai: "https://api.openai.com/v1",
103
+ ollama: "http://127.0.0.1:11434/v1"
104
+ };
105
+ var OFFICIAL = {
106
+ "https://api.openai.com": "openai",
107
+ "https://api.anthropic.com": "anthropic"
108
+ };
109
+ var SECTION = "ai";
110
+ var loopback = /* @__PURE__ */ __name((hostname) => hostname === "localhost" || hostname === "[::1]" || /^127(\.\d{1,3}){3}$/.test(hostname), "loopback");
111
+ var aiAddressAllowed = /* @__PURE__ */ __name((value) => {
112
+ if (!endpoint(value))
113
+ return false;
114
+ const url = new URL(value);
115
+ return url.protocol === "https:" || loopback(url.hostname);
116
+ }, "aiAddressAllowed");
117
+ var consentShape = strictObject({
118
+ origin: string(),
119
+ model: string(),
120
+ at: string()
121
+ });
122
+ var settingShape = strictObject({
123
+ provider: picklist(AI_PROVIDERS),
124
+ model: pipe(string(), trim(), minLength(1), maxLength(200)),
125
+ base_url: optional(pipe(string(), check(aiAddressAllowed, "use https, or http only on this computer"))),
126
+ images: optional(consentShape)
127
+ });
128
+ var aiSettingPath = /* @__PURE__ */ __name((env) => sharedConfigPath(storePath(env), env), "aiSettingPath");
129
+ var readAiSetting = /* @__PURE__ */ __name((path) => {
130
+ const section = readSharedSection(path, SECTION);
131
+ if (section === void 0)
132
+ return void 0;
133
+ const checked = safeParse(settingShape, section);
134
+ if (!checked.success)
135
+ throw new CliError("configuration_error", `${path}: ai.${getDotPath(checked.issues[0]) ?? "setting"} is invalid`, {
136
+ reason: "ai.setting-invalid"
137
+ });
138
+ return checked.output;
139
+ }, "readAiSetting");
140
+ var writeAiSetting = /* @__PURE__ */ __name((path, setting) => writeSharedSection(path, SECTION, setting), "writeAiSetting");
141
+ var aiSettingOf = /* @__PURE__ */ __name((given) => {
142
+ const draft = {
143
+ provider: given.provider,
144
+ model: given.model,
145
+ ...given.baseUrl?.trim() ? { base_url: given.baseUrl.trim().replace(/\/+$/, "") } : {}
146
+ };
147
+ const checked = safeParse(settingShape, draft);
148
+ if (!checked.success) {
149
+ const field = getDotPath(checked.issues[0]);
150
+ throw new CliError("validation_error", field === "provider" ? `the provider is one of ${AI_PROVIDERS.join(", ")}` : field === "base_url" ? "this address is not allowed: use https, or http only on this computer" : "name the model, 1 to 200 characters", { reason: field === "base_url" ? "ai.address-refused" : "ai.setting-invalid" });
151
+ }
152
+ if (checked.output.provider === "ollama" && !loopback(new URL(aiBaseUrl(checked.output)).hostname))
153
+ throw new CliError("validation_error", "Ollama is read only on this computer", { reason: "ai.address-refused" });
154
+ return given.allowImages ? {
155
+ ...checked.output,
156
+ images: { origin: aiOrigin(checked.output), model: checked.output.model, at: (/* @__PURE__ */ new Date()).toISOString() }
157
+ } : checked.output;
158
+ }, "aiSettingOf");
159
+ var aiBaseUrl = /* @__PURE__ */ __name((setting) => setting.base_url ?? DEFAULT_BASE_URL[setting.provider], "aiBaseUrl");
160
+ var aiOrigin = /* @__PURE__ */ __name((setting) => new URL(aiBaseUrl(setting)).origin, "aiOrigin");
161
+ var aiTarget = /* @__PURE__ */ __name((setting) => ({
162
+ provider: setting.provider === "anthropic" ? "anthropic" : "openai",
163
+ model: setting.model,
164
+ baseUrl: aiBaseUrl(setting)
165
+ }), "aiTarget");
166
+ var aiImagesAllowed = /* @__PURE__ */ __name((setting) => setting.images?.origin === aiOrigin(setting) && setting.images.model === setting.model, "aiImagesAllowed");
167
+ var aiKeys = /* @__PURE__ */ __name((app, env, keyring) => {
168
+ const where = { appName: "WireCat", prefix: "WIRECAT", env };
169
+ const credentials = new Credentials({
170
+ configDir: resolvePaths(where).config,
171
+ service: "WireCat",
172
+ // A tool run with its own folders moved (MEMO_CONFIG_DIR…) is a test or a sandbox, not the owner.
173
+ isolated: isMovedRun(where) || isMovedRun({ appName: app.appName, prefix: app.envPrefix, env }),
174
+ env,
175
+ ...keyring ? { keyring } : {},
176
+ warn: /* @__PURE__ */ __name((message) => process.stderr.write(`${message}
177
+ `), "warn")
178
+ });
179
+ const account = /* @__PURE__ */ __name((origin) => `ai:${origin}`, "account");
180
+ return {
181
+ read: /* @__PURE__ */ __name((origin) => {
182
+ const stored = credentials.read(account(origin));
183
+ if (stored)
184
+ return { key: stored.secret, source: stored.source };
185
+ const official = OFFICIAL[origin];
186
+ const legacy = official ?? (origin.startsWith("https://") ? endpointKeyName(origin) : void 0);
187
+ const found = legacy === void 0 ? void 0 : embeddingKeys(app, env, keyring).read(legacy);
188
+ return found ? { key: found.key, source: found.source === "env" ? "environment" : found.source } : void 0;
189
+ }, "read"),
190
+ write: /* @__PURE__ */ __name((origin, key) => credentials.write(account(origin), key, { signIn: true }), "write"),
191
+ remove: /* @__PURE__ */ __name((origin) => credentials.remove(account(origin)), "remove")
192
+ };
193
+ }, "aiKeys");
194
+ var aiNeedsKey = /* @__PURE__ */ __name((setting) => setting.provider !== "ollama", "aiNeedsKey");
195
+ var keyFor = /* @__PURE__ */ __name((setting, deps) => aiNeedsKey(setting) ? aiKeys(deps.app, deps.env, deps.keyring).read(aiOrigin(setting))?.key : void 0, "keyFor");
196
+ var gatewayFor = /* @__PURE__ */ __name((setting, deps, key) => modelGateway({
197
+ resolve: /* @__PURE__ */ __name(() => aiTarget(setting), "resolve"),
198
+ consent: /* @__PURE__ */ __name(() => true, "consent"),
199
+ key: /* @__PURE__ */ __name(() => key, "key"),
200
+ ...deps.fetch ? { fetch: deps.fetch } : {},
201
+ ...deps.signal ? { signal: deps.signal } : {}
202
+ }), "gatewayFor");
203
+ var reasonOf = /* @__PURE__ */ __name((error) => {
204
+ if (!(error instanceof CliError))
205
+ return "ai.failed";
206
+ if (error.code === "rate_limited")
207
+ return "ai.rate-limited";
208
+ const { reason, status } = error.details;
209
+ if (reason === "model.key-rejected")
210
+ return "ai.key-rejected";
211
+ if (reason === "model.unreachable")
212
+ return "ai.unreachable";
213
+ if (status === 402)
214
+ return "ai.no-credit";
215
+ if (status === 404)
216
+ return "ai.model-unknown";
217
+ if (typeof status === "number" && status >= 500)
218
+ return "ai.provider-error";
219
+ return "ai.failed";
220
+ }, "reasonOf");
221
+ var checkAi = /* @__PURE__ */ __name(async (deps) => {
222
+ const setting = readAiSetting(aiSettingPath(deps.env));
223
+ if (!setting)
224
+ return { ok: false, reason: "ai.not-set", ms: null };
225
+ const key = keyFor(setting, deps);
226
+ if (aiNeedsKey(setting) && !key)
227
+ return { ok: false, reason: "ai.key-missing", ms: null };
228
+ const started = Date.now();
229
+ try {
230
+ await gatewayFor(setting, deps, key).complete({
231
+ purpose: "check",
232
+ system: "Reply with the word OK.",
233
+ prompt: "OK?",
234
+ // Reasoning models spend tokens before they answer; too low a cap reads as a broken setup.
235
+ maxTokens: 2048
236
+ });
237
+ return { ok: true, reason: "ai.ok", ms: Date.now() - started };
238
+ } catch (error) {
239
+ return { ok: false, reason: reasonOf(error), ms: null };
240
+ }
241
+ }, "checkAi");
242
+
243
+ export {
244
+ AI_PROVIDERS,
245
+ aiSettingPath,
246
+ readAiSetting,
247
+ writeAiSetting,
248
+ aiSettingOf,
249
+ aiBaseUrl,
250
+ aiOrigin,
251
+ aiImagesAllowed,
252
+ aiKeys,
253
+ aiNeedsKey,
254
+ checkAi,
255
+ googleError,
256
+ offlineRefused
257
+ };