opencode-ext-connector 0.3.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 (332) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/LICENSE +29 -0
  3. package/README.md +207 -0
  4. package/THIRD_PARTY_NOTICES.md +201 -0
  5. package/dist/catalog/parse-ids.d.ts +2 -0
  6. package/dist/catalog/parse-ids.js +49 -0
  7. package/dist/core/adapter.d.ts +16 -0
  8. package/dist/core/adapter.js +20 -0
  9. package/dist/core/clock.d.ts +7 -0
  10. package/dist/core/clock.js +1 -0
  11. package/dist/core/deadline.d.ts +13 -0
  12. package/dist/core/deadline.js +54 -0
  13. package/dist/core/errors.d.ts +57 -0
  14. package/dist/core/errors.js +81 -0
  15. package/dist/core/health.d.ts +22 -0
  16. package/dist/core/health.js +30 -0
  17. package/dist/core/http.d.ts +28 -0
  18. package/dist/core/http.js +1 -0
  19. package/dist/core/ids.d.ts +7 -0
  20. package/dist/core/ids.js +23 -0
  21. package/dist/core/lifecycle.d.ts +4 -0
  22. package/dist/core/lifecycle.js +16 -0
  23. package/dist/core/logger.d.ts +21 -0
  24. package/dist/core/logger.js +61 -0
  25. package/dist/core/models.d.ts +41 -0
  26. package/dist/core/models.js +59 -0
  27. package/dist/core/options.d.ts +31 -0
  28. package/dist/core/options.js +62 -0
  29. package/dist/core/process.d.ts +20 -0
  30. package/dist/core/process.js +1 -0
  31. package/dist/http/fetch-transport.d.ts +2 -0
  32. package/dist/http/fetch-transport.js +75 -0
  33. package/dist/http/package-version.d.ts +7 -0
  34. package/dist/http/package-version.js +46 -0
  35. package/dist/http/read-body.d.ts +9 -0
  36. package/dist/http/read-body.js +22 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/index.js +5 -0
  39. package/dist/logging/logger.d.ts +4 -0
  40. package/dist/logging/logger.js +11 -0
  41. package/dist/opencode/auth-store.d.ts +19 -0
  42. package/dist/opencode/auth-store.js +107 -0
  43. package/dist/opencode/beta-api.d.ts +3 -0
  44. package/dist/opencode/beta-api.js +1 -0
  45. package/dist/opencode/catalog-bridge.d.ts +3 -0
  46. package/dist/opencode/catalog-bridge.js +85 -0
  47. package/dist/opencode/health-refresh.d.ts +16 -0
  48. package/dist/opencode/health-refresh.js +49 -0
  49. package/dist/opencode/host-options.d.ts +2 -0
  50. package/dist/opencode/host-options.js +49 -0
  51. package/dist/opencode/language-factory.d.ts +13 -0
  52. package/dist/opencode/language-factory.js +37 -0
  53. package/dist/opencode/ollama-probe.d.ts +2 -0
  54. package/dist/opencode/ollama-probe.js +15 -0
  55. package/dist/opencode/ollama-production.d.ts +4 -0
  56. package/dist/opencode/ollama-production.js +10 -0
  57. package/dist/opencode/plugin.d.ts +33 -0
  58. package/dist/opencode/plugin.js +40 -0
  59. package/dist/opencode/process-exit.d.ts +4 -0
  60. package/dist/opencode/process-exit.js +22 -0
  61. package/dist/opencode/provider-entry.d.ts +25 -0
  62. package/dist/opencode/provider-entry.js +1 -0
  63. package/dist/opencode/providers.d.ts +16 -0
  64. package/dist/opencode/providers.js +140 -0
  65. package/dist/opencode/register-integrations.d.ts +3 -0
  66. package/dist/opencode/register-integrations.js +8 -0
  67. package/dist/opencode/reload.d.ts +9 -0
  68. package/dist/opencode/reload.js +30 -0
  69. package/dist/opencode/v1-anthropic-auth.d.ts +9 -0
  70. package/dist/opencode/v1-anthropic-auth.js +55 -0
  71. package/dist/opencode/v1-catalog.d.ts +13 -0
  72. package/dist/opencode/v1-catalog.js +80 -0
  73. package/dist/opencode/v1-language.d.ts +3 -0
  74. package/dist/opencode/v1-language.js +64 -0
  75. package/dist/opencode/v1-module.d.ts +23 -0
  76. package/dist/opencode/v1-module.js +74 -0
  77. package/dist/opencode/v1-session-auth.d.ts +5 -0
  78. package/dist/opencode/v1-session-auth.js +98 -0
  79. package/dist/providers/claude/adapter.d.ts +7 -0
  80. package/dist/providers/claude/adapter.js +37 -0
  81. package/dist/providers/claude/atomic-private-file.d.ts +13 -0
  82. package/dist/providers/claude/atomic-private-file.js +94 -0
  83. package/dist/providers/claude/auth-json.d.ts +2 -0
  84. package/dist/providers/claude/auth-json.js +14 -0
  85. package/dist/providers/claude/auth.d.ts +24 -0
  86. package/dist/providers/claude/auth.js +200 -0
  87. package/dist/providers/claude/cli-version.d.ts +10 -0
  88. package/dist/providers/claude/cli-version.js +36 -0
  89. package/dist/providers/claude/compat-betas.d.ts +5 -0
  90. package/dist/providers/claude/compat-betas.js +54 -0
  91. package/dist/providers/claude/compat-config.d.ts +8 -0
  92. package/dist/providers/claude/compat-config.js +31 -0
  93. package/dist/providers/claude/compat-request.d.ts +12 -0
  94. package/dist/providers/claude/compat-request.js +151 -0
  95. package/dist/providers/claude/compat-response.d.ts +1 -0
  96. package/dist/providers/claude/compat-response.js +54 -0
  97. package/dist/providers/claude/compat-signing.d.ts +9 -0
  98. package/dist/providers/claude/compat-signing.js +24 -0
  99. package/dist/providers/claude/compat-transform.d.ts +3 -0
  100. package/dist/providers/claude/compat-transform.js +224 -0
  101. package/dist/providers/claude/credentials.d.ts +7 -0
  102. package/dist/providers/claude/credentials.js +37 -0
  103. package/dist/providers/claude/emit-stream.d.ts +2 -0
  104. package/dist/providers/claude/emit-stream.js +62 -0
  105. package/dist/providers/claude/keychain-account.d.ts +1 -0
  106. package/dist/providers/claude/keychain-account.js +14 -0
  107. package/dist/providers/claude/language-model.d.ts +8 -0
  108. package/dist/providers/claude/language-model.js +109 -0
  109. package/dist/providers/claude/models.d.ts +9 -0
  110. package/dist/providers/claude/models.js +24 -0
  111. package/dist/providers/claude/prompt.d.ts +42 -0
  112. package/dist/providers/claude/prompt.js +134 -0
  113. package/dist/providers/claude/refresh.d.ts +19 -0
  114. package/dist/providers/claude/refresh.js +85 -0
  115. package/dist/providers/claude/sse-convert.d.ts +6 -0
  116. package/dist/providers/claude/sse-convert.js +74 -0
  117. package/dist/providers/claude/sse.d.ts +71 -0
  118. package/dist/providers/claude/sse.js +123 -0
  119. package/dist/providers/claude/transform.d.ts +32 -0
  120. package/dist/providers/claude/transform.js +52 -0
  121. package/dist/providers/claude/writeback.d.ts +18 -0
  122. package/dist/providers/claude/writeback.js +140 -0
  123. package/dist/providers/command-code/adapter.d.ts +7 -0
  124. package/dist/providers/command-code/adapter.js +37 -0
  125. package/dist/providers/command-code/auth.d.ts +5 -0
  126. package/dist/providers/command-code/auth.js +88 -0
  127. package/dist/providers/command-code/cli-version.d.ts +10 -0
  128. package/dist/providers/command-code/cli-version.js +36 -0
  129. package/dist/providers/command-code/emit-stream.d.ts +2 -0
  130. package/dist/providers/command-code/emit-stream.js +226 -0
  131. package/dist/providers/command-code/errors.d.ts +33 -0
  132. package/dist/providers/command-code/errors.js +105 -0
  133. package/dist/providers/command-code/language-model.d.ts +15 -0
  134. package/dist/providers/command-code/language-model.js +203 -0
  135. package/dist/providers/command-code/models.d.ts +3 -0
  136. package/dist/providers/command-code/models.js +18 -0
  137. package/dist/providers/command-code/record-stream.d.ts +1 -0
  138. package/dist/providers/command-code/record-stream.js +61 -0
  139. package/dist/providers/command-code/request-lifecycle.d.ts +8 -0
  140. package/dist/providers/command-code/request-lifecycle.js +33 -0
  141. package/dist/providers/command-code/request.d.ts +14 -0
  142. package/dist/providers/command-code/request.js +167 -0
  143. package/dist/providers/command-code/session.d.ts +5 -0
  144. package/dist/providers/command-code/session.js +9 -0
  145. package/dist/providers/cursor/adapter.d.ts +7 -0
  146. package/dist/providers/cursor/adapter.js +37 -0
  147. package/dist/providers/cursor/auth.d.ts +6 -0
  148. package/dist/providers/cursor/auth.js +56 -0
  149. package/dist/providers/cursor/blob-store.d.ts +26 -0
  150. package/dist/providers/cursor/blob-store.js +119 -0
  151. package/dist/providers/cursor/bridge-client.d.ts +33 -0
  152. package/dist/providers/cursor/bridge-client.js +200 -0
  153. package/dist/providers/cursor/bridge-event-sanitize.d.ts +3 -0
  154. package/dist/providers/cursor/bridge-event-sanitize.js +49 -0
  155. package/dist/providers/cursor/bridge-limits.d.ts +3 -0
  156. package/dist/providers/cursor/bridge-limits.js +10 -0
  157. package/dist/providers/cursor/bridge-process.d.ts +45 -0
  158. package/dist/providers/cursor/bridge-process.js +211 -0
  159. package/dist/providers/cursor/bridge-protocol.d.ts +68 -0
  160. package/dist/providers/cursor/bridge-protocol.js +49 -0
  161. package/dist/providers/cursor/bridge-serialize.d.ts +3 -0
  162. package/dist/providers/cursor/bridge-serialize.js +53 -0
  163. package/dist/providers/cursor/bridge-wire.d.ts +3 -0
  164. package/dist/providers/cursor/bridge-wire.js +163 -0
  165. package/dist/providers/cursor/checkpoint-store.d.ts +28 -0
  166. package/dist/providers/cursor/checkpoint-store.js +94 -0
  167. package/dist/providers/cursor/connect-frame-stream.d.ts +19 -0
  168. package/dist/providers/cursor/connect-frame-stream.js +160 -0
  169. package/dist/providers/cursor/connect-frame.d.ts +31 -0
  170. package/dist/providers/cursor/connect-frame.js +121 -0
  171. package/dist/providers/cursor/credentials.d.ts +5 -0
  172. package/dist/providers/cursor/credentials.js +22 -0
  173. package/dist/providers/cursor/direct-run-types.d.ts +33 -0
  174. package/dist/providers/cursor/direct-run-types.js +1 -0
  175. package/dist/providers/cursor/direct-run.d.ts +5 -0
  176. package/dist/providers/cursor/direct-run.js +213 -0
  177. package/dist/providers/cursor/direct-runtime.d.ts +23 -0
  178. package/dist/providers/cursor/direct-runtime.js +111 -0
  179. package/dist/providers/cursor/direct-stream.d.ts +28 -0
  180. package/dist/providers/cursor/direct-stream.js +190 -0
  181. package/dist/providers/cursor/exec-reply.d.ts +13 -0
  182. package/dist/providers/cursor/exec-reply.js +102 -0
  183. package/dist/providers/cursor/h2-bridge-response.d.ts +9 -0
  184. package/dist/providers/cursor/h2-bridge-response.js +51 -0
  185. package/dist/providers/cursor/h2-bridge-session.d.ts +24 -0
  186. package/dist/providers/cursor/h2-bridge-session.js +197 -0
  187. package/dist/providers/cursor/h2-bridge.d.ts +2 -0
  188. package/dist/providers/cursor/h2-bridge.js +12865 -0
  189. package/dist/providers/cursor/http2.d.ts +18 -0
  190. package/dist/providers/cursor/http2.js +144 -0
  191. package/dist/providers/cursor/interaction-reply.d.ts +6 -0
  192. package/dist/providers/cursor/interaction-reply.js +52 -0
  193. package/dist/providers/cursor/language-generate.d.ts +2 -0
  194. package/dist/providers/cursor/language-generate.js +35 -0
  195. package/dist/providers/cursor/language-model.d.ts +13 -0
  196. package/dist/providers/cursor/language-model.js +77 -0
  197. package/dist/providers/cursor/legacy-stream.d.ts +11 -0
  198. package/dist/providers/cursor/legacy-stream.js +115 -0
  199. package/dist/providers/cursor/mcp-tools.d.ts +7 -0
  200. package/dist/providers/cursor/mcp-tools.js +15 -0
  201. package/dist/providers/cursor/models.d.ts +14 -0
  202. package/dist/providers/cursor/models.js +116 -0
  203. package/dist/providers/cursor/ndjson.d.ts +3 -0
  204. package/dist/providers/cursor/ndjson.js +100 -0
  205. package/dist/providers/cursor/prompt.d.ts +4 -0
  206. package/dist/providers/cursor/prompt.js +142 -0
  207. package/dist/providers/cursor/proto/checkpoint.d.ts +35 -0
  208. package/dist/providers/cursor/proto/checkpoint.js +114 -0
  209. package/dist/providers/cursor/proto/context.d.ts +64 -0
  210. package/dist/providers/cursor/proto/context.js +178 -0
  211. package/dist/providers/cursor/proto/errors.d.ts +16 -0
  212. package/dist/providers/cursor/proto/errors.js +26 -0
  213. package/dist/providers/cursor/proto/exec-control.d.ts +20 -0
  214. package/dist/providers/cursor/proto/exec-control.js +54 -0
  215. package/dist/providers/cursor/proto/exec.d.ts +47 -0
  216. package/dist/providers/cursor/proto/exec.js +154 -0
  217. package/dist/providers/cursor/proto/fields.d.ts +22 -0
  218. package/dist/providers/cursor/proto/fields.js +97 -0
  219. package/dist/providers/cursor/proto/interaction-query.d.ts +15 -0
  220. package/dist/providers/cursor/proto/interaction-query.js +79 -0
  221. package/dist/providers/cursor/proto/interaction-tool-update.d.ts +10 -0
  222. package/dist/providers/cursor/proto/interaction-tool-update.js +58 -0
  223. package/dist/providers/cursor/proto/interaction-turn-ended.d.ts +6 -0
  224. package/dist/providers/cursor/proto/interaction-turn-ended.js +18 -0
  225. package/dist/providers/cursor/proto/interaction.d.ts +65 -0
  226. package/dist/providers/cursor/proto/interaction.js +185 -0
  227. package/dist/providers/cursor/proto/kv.d.ts +25 -0
  228. package/dist/providers/cursor/proto/kv.js +109 -0
  229. package/dist/providers/cursor/proto/mcp-result.d.ts +40 -0
  230. package/dist/providers/cursor/proto/mcp-result.js +150 -0
  231. package/dist/providers/cursor/proto/mcp.d.ts +19 -0
  232. package/dist/providers/cursor/proto/mcp.js +78 -0
  233. package/dist/providers/cursor/proto/model.d.ts +11 -0
  234. package/dist/providers/cursor/proto/model.js +36 -0
  235. package/dist/providers/cursor/proto/output-location.d.ts +7 -0
  236. package/dist/providers/cursor/proto/output-location.js +53 -0
  237. package/dist/providers/cursor/proto/prewarm.d.ts +16 -0
  238. package/dist/providers/cursor/proto/prewarm.js +68 -0
  239. package/dist/providers/cursor/proto/request-context.d.ts +16 -0
  240. package/dist/providers/cursor/proto/request-context.js +58 -0
  241. package/dist/providers/cursor/proto/request.d.ts +44 -0
  242. package/dist/providers/cursor/proto/request.js +111 -0
  243. package/dist/providers/cursor/proto/run-request.d.ts +19 -0
  244. package/dist/providers/cursor/proto/run-request.js +68 -0
  245. package/dist/providers/cursor/proto/server.d.ts +35 -0
  246. package/dist/providers/cursor/proto/server.js +93 -0
  247. package/dist/providers/cursor/proto/unknown-field.d.ts +1 -0
  248. package/dist/providers/cursor/proto/unknown-field.js +22 -0
  249. package/dist/providers/cursor/proto-value.d.ts +3 -0
  250. package/dist/providers/cursor/proto-value.js +170 -0
  251. package/dist/providers/cursor/proto-wire.d.ts +22 -0
  252. package/dist/providers/cursor/proto-wire.js +224 -0
  253. package/dist/providers/cursor/protocol-failure.d.ts +18 -0
  254. package/dist/providers/cursor/protocol-failure.js +45 -0
  255. package/dist/providers/cursor/recovery.d.ts +48 -0
  256. package/dist/providers/cursor/recovery.js +102 -0
  257. package/dist/providers/cursor/request-blob-ownership.d.ts +15 -0
  258. package/dist/providers/cursor/request-blob-ownership.js +39 -0
  259. package/dist/providers/cursor/request-build.d.ts +18 -0
  260. package/dist/providers/cursor/request-build.js +123 -0
  261. package/dist/providers/cursor/request-history.d.ts +32 -0
  262. package/dist/providers/cursor/request-history.js +163 -0
  263. package/dist/providers/cursor/request-input.d.ts +75 -0
  264. package/dist/providers/cursor/request-input.js +123 -0
  265. package/dist/providers/cursor/run-events.d.ts +15 -0
  266. package/dist/providers/cursor/run-events.js +43 -0
  267. package/dist/providers/cursor/run-session-expiry.d.ts +15 -0
  268. package/dist/providers/cursor/run-session-expiry.js +21 -0
  269. package/dist/providers/cursor/run-session.d.ts +50 -0
  270. package/dist/providers/cursor/run-session.js +205 -0
  271. package/dist/providers/cursor/run.d.ts +9 -0
  272. package/dist/providers/cursor/run.js +151 -0
  273. package/dist/providers/cursor/runner.d.ts +1 -0
  274. package/dist/providers/cursor/runner.js +55 -0
  275. package/dist/providers/cursor/server-dispatch-types.d.ts +83 -0
  276. package/dist/providers/cursor/server-dispatch-types.js +1 -0
  277. package/dist/providers/cursor/server-dispatch.d.ts +3 -0
  278. package/dist/providers/cursor/server-dispatch.js +178 -0
  279. package/dist/providers/cursor/session-state.d.ts +21 -0
  280. package/dist/providers/cursor/session-state.js +118 -0
  281. package/dist/providers/cursor/session.d.ts +1 -0
  282. package/dist/providers/cursor/session.js +26 -0
  283. package/dist/providers/cursor/settle-cleanup.d.ts +1 -0
  284. package/dist/providers/cursor/settle-cleanup.js +11 -0
  285. package/dist/providers/cursor/stream-adapter.d.ts +17 -0
  286. package/dist/providers/cursor/stream-adapter.js +99 -0
  287. package/dist/providers/cursor/tool-continuation.d.ts +14 -0
  288. package/dist/providers/cursor/tool-continuation.js +134 -0
  289. package/dist/providers/cursor/tool-stream.d.ts +2 -0
  290. package/dist/providers/cursor/tool-stream.js +220 -0
  291. package/dist/providers/cursor/usage.d.ts +3 -0
  292. package/dist/providers/cursor/usage.js +35 -0
  293. package/dist/providers/ollama/adapter.d.ts +8 -0
  294. package/dist/providers/ollama/adapter.js +65 -0
  295. package/dist/providers/ollama/catalog-state.d.ts +17 -0
  296. package/dist/providers/ollama/catalog-state.js +40 -0
  297. package/dist/providers/ollama/cloud-catalog.d.ts +3 -0
  298. package/dist/providers/ollama/cloud-catalog.js +82 -0
  299. package/dist/providers/ollama/errors.d.ts +14 -0
  300. package/dist/providers/ollama/errors.js +19 -0
  301. package/dist/providers/ollama/generate.d.ts +2 -0
  302. package/dist/providers/ollama/generate.js +49 -0
  303. package/dist/providers/ollama/html-links.d.ts +1 -0
  304. package/dist/providers/ollama/html-links.js +30 -0
  305. package/dist/providers/ollama/http.d.ts +12 -0
  306. package/dist/providers/ollama/http.js +67 -0
  307. package/dist/providers/ollama/index.d.ts +8 -0
  308. package/dist/providers/ollama/index.js +8 -0
  309. package/dist/providers/ollama/language-model.d.ts +11 -0
  310. package/dist/providers/ollama/language-model.js +62 -0
  311. package/dist/providers/ollama/local-catalog.d.ts +3 -0
  312. package/dist/providers/ollama/local-catalog.js +38 -0
  313. package/dist/providers/ollama/ndjson.d.ts +3 -0
  314. package/dist/providers/ollama/ndjson.js +51 -0
  315. package/dist/providers/ollama/prompt.d.ts +8 -0
  316. package/dist/providers/ollama/prompt.js +141 -0
  317. package/dist/providers/ollama/protocol.d.ts +56 -0
  318. package/dist/providers/ollama/protocol.js +26 -0
  319. package/dist/providers/ollama/runtime.d.ts +11 -0
  320. package/dist/providers/ollama/runtime.js +128 -0
  321. package/dist/providers/ollama/stream.d.ts +8 -0
  322. package/dist/providers/ollama/stream.js +110 -0
  323. package/dist/sdk/command-code.d.ts +5 -0
  324. package/dist/sdk/command-code.js +6 -0
  325. package/dist/sdk/cursor.d.ts +5 -0
  326. package/dist/sdk/cursor.js +6 -0
  327. package/dist/sdk/ollama.d.ts +5 -0
  328. package/dist/sdk/ollama.js +6 -0
  329. package/dist/server.d.ts +12 -0
  330. package/dist/server.js +94 -0
  331. package/docs/README.ko.md +189 -0
  332. package/package.json +75 -0
@@ -0,0 +1,123 @@
1
+ // Derived from griffinmartin/opencode-claude-auth@0f0ff6f12c367339130cbfd250393863ed2c8d9e.
2
+ // Licensed under MIT. See THIRD_PARTY_NOTICES.md.
3
+ import { convertEventToPart } from "./sse-convert";
4
+ function isAnthropicSseEvent(value) {
5
+ if (typeof value !== "object" || value === null) {
6
+ return false;
7
+ }
8
+ const typeValue = Reflect.get(value, "type");
9
+ if (typeof typeValue !== "string") {
10
+ return false;
11
+ }
12
+ const validTypes = [
13
+ "message_start",
14
+ "content_block_start",
15
+ "content_block_delta",
16
+ "content_block_stop",
17
+ "message_delta",
18
+ "message_stop",
19
+ "error",
20
+ ];
21
+ return validTypes.includes(typeValue);
22
+ }
23
+ function parseSseLine(line) {
24
+ if (line.length === 0) {
25
+ return null;
26
+ }
27
+ if (line.startsWith(":")) {
28
+ return null;
29
+ }
30
+ const colonIndex = line.indexOf(":");
31
+ if (colonIndex === -1) {
32
+ return { event: line };
33
+ }
34
+ const field = line.slice(0, colonIndex);
35
+ const value = line.slice(colonIndex + 1).trimStart();
36
+ if (field === "event") {
37
+ return { event: value };
38
+ }
39
+ if (field === "data") {
40
+ return { data: value };
41
+ }
42
+ return null;
43
+ }
44
+ function parseEventType(event) {
45
+ const validEvents = {
46
+ message_start: "message_start",
47
+ content_block_start: "content_block_start",
48
+ content_block_delta: "content_block_delta",
49
+ content_block_stop: "content_block_stop",
50
+ message_delta: "message_delta",
51
+ message_stop: "message_stop",
52
+ error: "error",
53
+ };
54
+ return validEvents[event] ?? null;
55
+ }
56
+ export function createSseParseState() {
57
+ return { buffer: "", event: null };
58
+ }
59
+ export function parseAnthropicSse(chunk, state) {
60
+ const text = new TextDecoder().decode(chunk);
61
+ const combined = state.buffer + text;
62
+ const lines = combined.split("\n");
63
+ const newBuffer = lines.pop() ?? "";
64
+ const events = [];
65
+ let currentEvent = state.event ?? undefined;
66
+ let currentData;
67
+ for (const line of lines) {
68
+ const parsed = parseSseLine(line);
69
+ if (parsed === null) {
70
+ continue;
71
+ }
72
+ if ("event" in parsed) {
73
+ currentEvent = parsed.event;
74
+ currentData = undefined;
75
+ continue;
76
+ }
77
+ if ("data" in parsed) {
78
+ currentData = parsed.data;
79
+ if (currentEvent === undefined) {
80
+ continue;
81
+ }
82
+ const eventType = parseEventType(currentEvent);
83
+ if (eventType === null) {
84
+ currentEvent = undefined;
85
+ currentData = undefined;
86
+ continue;
87
+ }
88
+ let event;
89
+ try {
90
+ const parsed = JSON.parse(currentData);
91
+ if (isAnthropicSseEvent(parsed)) {
92
+ event = parsed;
93
+ }
94
+ else {
95
+ currentEvent = undefined;
96
+ currentData = undefined;
97
+ continue;
98
+ }
99
+ }
100
+ catch {
101
+ currentEvent = undefined;
102
+ currentData = undefined;
103
+ continue;
104
+ }
105
+ if (event.type !== eventType) {
106
+ currentEvent = undefined;
107
+ currentData = undefined;
108
+ continue;
109
+ }
110
+ const result = convertEventToPart(event);
111
+ if (result !== null) {
112
+ events.push(result);
113
+ }
114
+ currentEvent = undefined;
115
+ currentData = undefined;
116
+ }
117
+ }
118
+ return {
119
+ events,
120
+ state: { buffer: newBuffer, event: currentEvent ?? null },
121
+ };
122
+ }
123
+ export { convertEventToPart, mapStopReason } from "./sse-convert";
@@ -0,0 +1,32 @@
1
+ export type ToolUseBlock = {
2
+ readonly type: "tool_use";
3
+ readonly id: string;
4
+ readonly name: string;
5
+ readonly input: Record<string, unknown>;
6
+ };
7
+ export type ToolResultBlock = {
8
+ readonly type: "tool_result";
9
+ readonly tool_use_id: string;
10
+ readonly content: string;
11
+ readonly is_error: true;
12
+ };
13
+ export type ContentBlock = ToolUseBlock | ToolResultBlock | {
14
+ readonly type: "text";
15
+ readonly text: string;
16
+ } | {
17
+ readonly type: string;
18
+ readonly [key: string]: unknown;
19
+ };
20
+ export type Message = {
21
+ readonly role: "user" | "assistant" | "system";
22
+ readonly content: string | readonly ContentBlock[];
23
+ };
24
+ export type RequestBody = {
25
+ readonly model: string;
26
+ readonly max_tokens: number;
27
+ readonly system?: string;
28
+ readonly messages: readonly Message[];
29
+ readonly stream?: boolean;
30
+ readonly [key: string]: unknown;
31
+ };
32
+ export declare function repairOrphanToolUses(body: RequestBody): RequestBody;
@@ -0,0 +1,52 @@
1
+ // Derived from griffinmartin/opencode-claude-auth@0f0ff6f12c367339130cbfd250393863ed2c8d9e.
2
+ // Licensed under MIT. See THIRD_PARTY_NOTICES.md.
3
+ function isToolUseBlock(block) {
4
+ return block.type === "tool_use" && typeof block.id === "string" && typeof block.name === "string";
5
+ }
6
+ function isToolResultBlock(block) {
7
+ return block.type === "tool_result" && typeof block.tool_use_id === "string";
8
+ }
9
+ function findOrphanToolUses(content) {
10
+ const toolUseIds = new Set();
11
+ const toolResultIds = new Set();
12
+ for (const block of content) {
13
+ if (isToolUseBlock(block)) {
14
+ toolUseIds.add(block.id);
15
+ }
16
+ if (isToolResultBlock(block)) {
17
+ toolResultIds.add(block.tool_use_id);
18
+ }
19
+ }
20
+ const orphans = [];
21
+ for (const block of content) {
22
+ if (isToolUseBlock(block) && !toolResultIds.has(block.id)) {
23
+ orphans.push(block);
24
+ }
25
+ }
26
+ return orphans;
27
+ }
28
+ function createPlaceholderToolResult(toolUse) {
29
+ return {
30
+ type: "tool_result",
31
+ tool_use_id: toolUse.id,
32
+ content: `Tool ${toolUse.name} was called but no result was provided.`,
33
+ is_error: true,
34
+ };
35
+ }
36
+ export function repairOrphanToolUses(body) {
37
+ const messages = body.messages.map((message) => {
38
+ if (message.role !== "assistant" || typeof message.content === "string") {
39
+ return message;
40
+ }
41
+ const orphans = findOrphanToolUses(message.content);
42
+ if (orphans.length === 0) {
43
+ return message;
44
+ }
45
+ const newContent = [...message.content];
46
+ for (const orphan of orphans) {
47
+ newContent.push(createPlaceholderToolResult(orphan));
48
+ }
49
+ return { ...message, content: newContent };
50
+ });
51
+ return { ...body, messages };
52
+ }
@@ -0,0 +1,18 @@
1
+ import type { ClaudeCredentials } from "./credentials";
2
+ export type ClaudeWriteBackHooks = {
3
+ readonly writeFile?: (path: string, body: string) => Promise<void>;
4
+ readonly readFile?: (path: string) => Promise<string | null>;
5
+ readonly writeKeychain?: (args: readonly string[]) => Promise<void>;
6
+ readonly readKeychainDump?: () => Promise<string | null>;
7
+ readonly platform?: string;
8
+ readonly expectedPriorAccessToken?: string;
9
+ readonly source?: "file" | "keychain";
10
+ };
11
+ export declare function claudeCredentialPath(env: Readonly<Record<string, string | undefined>>): string;
12
+ export declare function claudeWindowsCredentialPaths(env: Readonly<Record<string, string | undefined>>): readonly string[];
13
+ export declare function claudeCredentialsFileBody(credentials: ClaudeCredentials): string;
14
+ export declare function updateClaudeCredentialBlob(raw: string, credentials: ClaudeCredentials, expectedPriorAccessToken?: string): string | null;
15
+ export declare function claudeKeychainWriteArgs(credentials: ClaudeCredentials, account?: string): readonly string[];
16
+ export declare function resolveKeychainAccount(readDump?: () => Promise<string | null>): Promise<string>;
17
+ export declare function writeClaudeCredentials(env: Readonly<Record<string, string | undefined>>, credentials: ClaudeCredentials, hooks?: ClaudeWriteBackHooks): Promise<void>;
18
+ export declare function writeClaudeCredentialsFile(env: Readonly<Record<string, string | undefined>>, credentials: ClaudeCredentials): Promise<void>;
@@ -0,0 +1,140 @@
1
+ import { execFile } from "node:child_process";
2
+ import { readFile } from "node:fs/promises";
3
+ import { homedir } from "node:os";
4
+ import { join } from "node:path";
5
+ import { promisify } from "node:util";
6
+ import { opencodeAuthJsonPaths } from "../../opencode/auth-store";
7
+ import { writeClaudePrivateFile } from "./atomic-private-file";
8
+ import { mergeOpencodeAuthJson } from "./auth-json";
9
+ import { parseKeychainAccount } from "./keychain-account";
10
+ const execFileAsync = promisify(execFile);
11
+ const CLAUDE_KEYCHAIN_SERVICE = "Claude Code-credentials";
12
+ const CLAUDE_KEYCHAIN_ACCOUNT = "Claude Code";
13
+ export function claudeCredentialPath(env) {
14
+ const configDir = env["CLAUDE_CONFIG_DIR"] ?? join(homedir(), ".claude");
15
+ return join(configDir, ".credentials.json");
16
+ }
17
+ export function claudeWindowsCredentialPaths(env) {
18
+ const appdata = env["APPDATA"];
19
+ if (appdata === undefined || appdata.length === 0) {
20
+ return [];
21
+ }
22
+ return [join(appdata, "Claude", ".credentials.json")];
23
+ }
24
+ export function claudeCredentialsFileBody(credentials) {
25
+ return `${JSON.stringify({
26
+ claudeAiOauth: {
27
+ accessToken: credentials.accessToken,
28
+ refreshToken: credentials.refreshToken,
29
+ expiresAt: credentials.expiresAtMs,
30
+ },
31
+ }, null, 2)}\n`;
32
+ }
33
+ export function updateClaudeCredentialBlob(raw, credentials, expectedPriorAccessToken) {
34
+ let parsed;
35
+ try {
36
+ parsed = JSON.parse(raw);
37
+ }
38
+ catch {
39
+ return null;
40
+ }
41
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
42
+ return null;
43
+ }
44
+ const wrapped = "claudeAiOauth" in parsed ? Reflect.get(parsed, "claudeAiOauth") : parsed;
45
+ if (typeof wrapped !== "object" || wrapped === null || Array.isArray(wrapped)) {
46
+ return null;
47
+ }
48
+ const current = "accessToken" in wrapped ? Reflect.get(wrapped, "accessToken") : undefined;
49
+ if (expectedPriorAccessToken !== undefined && current !== expectedPriorAccessToken) {
50
+ return null;
51
+ }
52
+ Reflect.set(wrapped, "accessToken", credentials.accessToken);
53
+ Reflect.set(wrapped, "refreshToken", credentials.refreshToken);
54
+ Reflect.set(wrapped, "expiresAt", credentials.expiresAtMs);
55
+ return `${JSON.stringify(parsed, null, 2)}\n`;
56
+ }
57
+ export function claudeKeychainWriteArgs(credentials, account = CLAUDE_KEYCHAIN_ACCOUNT) {
58
+ return [
59
+ "add-generic-password",
60
+ "-U",
61
+ "-s",
62
+ CLAUDE_KEYCHAIN_SERVICE,
63
+ "-a",
64
+ account,
65
+ "-w",
66
+ claudeCredentialsFileBody(credentials).trim(),
67
+ ];
68
+ }
69
+ async function defaultReadFile(path) {
70
+ try {
71
+ return await readFile(path, "utf8");
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ }
77
+ async function defaultWriteFile(path, body) {
78
+ await writeClaudePrivateFile(path, body);
79
+ }
80
+ async function defaultWriteKeychain(args) {
81
+ await execFileAsync("security", [...args], { timeout: 2_000 });
82
+ }
83
+ async function defaultReadKeychainDump() {
84
+ try {
85
+ const result = await execFileAsync("security", ["find-generic-password", "-s", CLAUDE_KEYCHAIN_SERVICE], { timeout: 2_000 });
86
+ return result.stdout;
87
+ }
88
+ catch {
89
+ return null;
90
+ }
91
+ }
92
+ export async function resolveKeychainAccount(readDump = defaultReadKeychainDump) {
93
+ const dump = await readDump();
94
+ if (dump === null) {
95
+ return CLAUDE_KEYCHAIN_ACCOUNT;
96
+ }
97
+ return parseKeychainAccount(dump) ?? CLAUDE_KEYCHAIN_ACCOUNT;
98
+ }
99
+ export async function writeClaudeCredentials(env, credentials, hooks = {}) {
100
+ const write = hooks.writeFile ?? defaultWriteFile;
101
+ const body = claudeCredentialsFileBody(credentials);
102
+ const paths = [claudeCredentialPath(env)];
103
+ const platform = hooks.platform ?? process.platform;
104
+ if (platform === "win32") {
105
+ paths.push(...claudeWindowsCredentialPaths(env));
106
+ }
107
+ const readExisting = hooks.readFile ?? defaultReadFile;
108
+ if (hooks.source !== "keychain") {
109
+ for (const path of paths) {
110
+ const existing = await readExisting(path);
111
+ const next = existing === null
112
+ ? body
113
+ : updateClaudeCredentialBlob(existing, credentials, hooks.expectedPriorAccessToken);
114
+ if (next !== null) {
115
+ await write(path, next);
116
+ }
117
+ }
118
+ }
119
+ for (const path of opencodeAuthJsonPaths(env, platform)) {
120
+ const raw = await readExisting(path);
121
+ let existing = {};
122
+ if (raw !== null) {
123
+ try {
124
+ existing = JSON.parse(raw);
125
+ }
126
+ catch {
127
+ existing = {};
128
+ }
129
+ }
130
+ await write(path, mergeOpencodeAuthJson(existing, credentials));
131
+ }
132
+ if (platform === "darwin" && hooks.source !== "file") {
133
+ const writeKeychain = hooks.writeKeychain ?? defaultWriteKeychain;
134
+ const account = await resolveKeychainAccount(hooks.readKeychainDump ?? defaultReadKeychainDump);
135
+ await writeKeychain(claudeKeychainWriteArgs(credentials, account));
136
+ }
137
+ }
138
+ export async function writeClaudeCredentialsFile(env, credentials) {
139
+ await writeClaudeCredentials(env, credentials);
140
+ }
@@ -0,0 +1,7 @@
1
+ import type { ProviderAdapter } from "../../core/adapter";
2
+ import type { AdapterModel } from "../../core/models";
3
+ export type CommandCodeAdapterOptions = {
4
+ readonly readAccessToken: (signal: AbortSignal) => Promise<string | null>;
5
+ readonly listModels: (token: string, signal: AbortSignal) => Promise<readonly AdapterModel[]>;
6
+ };
7
+ export declare function createCommandCodeAdapter(options: CommandCodeAdapterOptions): ProviderAdapter;
@@ -0,0 +1,37 @@
1
+ import { OperationCancelledError } from "../../core/errors";
2
+ import { parseProviderId } from "../../core/ids";
3
+ import { createAsyncDisposable } from "../../core/lifecycle";
4
+ export function createCommandCodeAdapter(options) {
5
+ const providerId = parseProviderId("command-code");
6
+ const disposal = createAsyncDisposable(() => undefined);
7
+ let lastModels = null;
8
+ return {
9
+ providerId,
10
+ snapshot: async (signal) => {
11
+ if (signal.aborted) {
12
+ throw new OperationCancelledError("command-code-snapshot");
13
+ }
14
+ const token = await options.readAccessToken(signal);
15
+ if (token === null) {
16
+ return { status: "unavailable", providerId, reason: "invalid-data" };
17
+ }
18
+ try {
19
+ const models = await options.listModels(token, signal);
20
+ if (models.length === 0) {
21
+ return lastModels === null
22
+ ? { status: "unavailable", providerId, reason: "invalid-data" }
23
+ : { status: "stale", providerId, models: lastModels, reason: "invalid-data" };
24
+ }
25
+ lastModels = models;
26
+ return { status: "ready", providerId, models };
27
+ }
28
+ catch {
29
+ return lastModels === null
30
+ ? { status: "unavailable", providerId, reason: "transport-error" }
31
+ : { status: "stale", providerId, models: lastModels, reason: "transport-error" };
32
+ }
33
+ },
34
+ dispose: disposal.dispose,
35
+ [Symbol.asyncDispose]: disposal[Symbol.asyncDispose],
36
+ };
37
+ }
@@ -0,0 +1,5 @@
1
+ export type CommandCodeAuthLookup = {
2
+ readonly homeDir?: string;
3
+ };
4
+ export declare function commandCodeCredentialPaths(homeDir: string, env: Readonly<Record<string, string | undefined>>): readonly string[];
5
+ export declare function readCommandCodeAccessToken(env: Readonly<Record<string, string | undefined>>, signal: AbortSignal, lookup?: CommandCodeAuthLookup): Promise<string | null>;
@@ -0,0 +1,88 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import { homedir } from "node:os";
3
+ import { join } from "node:path";
4
+ import { z } from "zod";
5
+ import { OperationCancelledError } from "../../core/errors";
6
+ const credentialSchema = z
7
+ .object({
8
+ apiKey: z.string().optional(),
9
+ accessToken: z.string().optional(),
10
+ token: z.string().optional(),
11
+ commandcode: z
12
+ .union([z.string(), z.object({ type: z.literal("oauth"), access: z.string() })])
13
+ .optional(),
14
+ })
15
+ .passthrough();
16
+ function tokenFromUnknown(value, allowsGenericToken) {
17
+ const parsed = credentialSchema.safeParse(value);
18
+ if (!parsed.success) {
19
+ return null;
20
+ }
21
+ if (allowsGenericToken) {
22
+ for (const key of ["apiKey", "accessToken", "token"]) {
23
+ const token = parsed.data[key];
24
+ if (token !== undefined && token.length > 0) {
25
+ return token;
26
+ }
27
+ }
28
+ }
29
+ const commandCode = parsed.data.commandcode;
30
+ if (typeof commandCode === "string" && commandCode.length > 0) {
31
+ return commandCode;
32
+ }
33
+ if (typeof commandCode === "object" && commandCode.access.length > 0) {
34
+ return commandCode.access;
35
+ }
36
+ return null;
37
+ }
38
+ export function commandCodeCredentialPaths(homeDir, env) {
39
+ const xdg = env["XDG_CONFIG_HOME"];
40
+ const paths = [
41
+ join(homeDir, ".commandcode", "auth.json"),
42
+ join(homeDir, ".commandcode", "cli-config.json"),
43
+ join(homeDir, ".pi", "agent", "auth.json"),
44
+ join(homeDir, ".config", "commandcode", "auth.json"),
45
+ join(homeDir, ".config", "commandcode", "cli-config.json"),
46
+ join(homeDir, ".config", "command-code", "auth.json"),
47
+ ];
48
+ if (xdg !== undefined && xdg.length > 0) {
49
+ return [
50
+ join(xdg, "commandcode", "auth.json"),
51
+ join(xdg, "commandcode", "cli-config.json"),
52
+ ...paths,
53
+ ];
54
+ }
55
+ return paths;
56
+ }
57
+ export async function readCommandCodeAccessToken(env, signal, lookup = {}) {
58
+ if (signal.aborted) {
59
+ throw new OperationCancelledError("command-code-read-credentials");
60
+ }
61
+ const envToken = env["COMMAND_CODE_API_KEY"] ?? env["COMMANDCODE_API_KEY"] ?? env["CC_API_KEY"];
62
+ if (envToken !== undefined && envToken.length > 0) {
63
+ return envToken;
64
+ }
65
+ const home = lookup.homeDir ?? env["HOME"] ?? homedir();
66
+ const candidates = commandCodeCredentialPaths(home, env);
67
+ const sharedPiPath = join(home, ".pi", "agent", "auth.json");
68
+ const ownedCandidates = candidates.map((path) => ({
69
+ path,
70
+ allowsGenericToken: path !== sharedPiPath,
71
+ }));
72
+ for (const candidate of ownedCandidates) {
73
+ try {
74
+ const parsed = JSON.parse(await readFile(candidate.path, "utf8"));
75
+ const token = tokenFromUnknown(parsed, candidate.allowsGenericToken);
76
+ if (token !== null) {
77
+ return token;
78
+ }
79
+ }
80
+ catch (error) {
81
+ if (error instanceof Error) {
82
+ continue;
83
+ }
84
+ throw error;
85
+ }
86
+ }
87
+ return null;
88
+ }
@@ -0,0 +1,10 @@
1
+ import type { HttpTransport } from "../../core/http";
2
+ export type CommandCodeVersionResolver = (signal: AbortSignal) => Promise<string | null>;
3
+ export type CommandCodeVersionResolverOptions = {
4
+ readonly env: Readonly<Record<string, string | undefined>>;
5
+ readonly transport: HttpTransport;
6
+ readonly readInstalledVersion?: () => string | null;
7
+ };
8
+ export declare function parseCommandCodeCliVersion(stdout: string): string | null;
9
+ export declare function readInstalledCommandCodeVersion(env: Readonly<Record<string, string | undefined>>): string | null;
10
+ export declare function createCommandCodeVersionResolver(options: CommandCodeVersionResolverOptions): CommandCodeVersionResolver;
@@ -0,0 +1,36 @@
1
+ import { spawnSync } from "node:child_process";
2
+ import { createPackageVersionResolver } from "../../http/package-version";
3
+ const COMMAND_CODE_PACKAGE = "command-code";
4
+ export function parseCommandCodeCliVersion(stdout) {
5
+ const match = /(\d+\.\d+\.\d+)/.exec(stdout);
6
+ return match?.[1] ?? null;
7
+ }
8
+ export function readInstalledCommandCodeVersion(env) {
9
+ const result = spawnSync("command-code", ["--version"], {
10
+ encoding: "utf8",
11
+ timeout: 3_000,
12
+ env: { ...env },
13
+ });
14
+ if (result.error !== undefined || result.status !== 0) {
15
+ return null;
16
+ }
17
+ return parseCommandCodeCliVersion(result.stdout);
18
+ }
19
+ export function createCommandCodeVersionResolver(options) {
20
+ const readInstalled = options.readInstalledVersion ?? (() => readInstalledCommandCodeVersion(options.env));
21
+ const readPublished = createPackageVersionResolver({
22
+ transport: options.transport,
23
+ packageName: COMMAND_CODE_PACKAGE,
24
+ });
25
+ let local;
26
+ return async (signal) => {
27
+ if (local === undefined) {
28
+ const override = options.env["COMMAND_CODE_CLI_VERSION"]?.trim();
29
+ local =
30
+ override !== undefined && override.length > 0
31
+ ? parseCommandCodeCliVersion(override)
32
+ : readInstalled();
33
+ }
34
+ return local ?? readPublished(signal);
35
+ };
36
+ }
@@ -0,0 +1,2 @@
1
+ import type { LanguageModelV3StreamPart } from "@ai-sdk/provider";
2
+ export declare function emitCommandCodeChunks(chunks: AsyncIterable<Uint8Array>, controller: ReadableStreamDefaultController<LanguageModelV3StreamPart>): Promise<void>;