cerebras-cli 1.0.1 → 1.0.4

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 (314) hide show
  1. package/README.md +3 -5
  2. package/cerebras-cli-1.0.0.tgz +0 -0
  3. package/package.json +7 -88
  4. package/AGENTS.md +0 -27
  5. package/Dockerfile +0 -10
  6. package/bunfig.toml +0 -4
  7. package/parsers-config.ts +0 -239
  8. package/script/build.ts +0 -151
  9. package/script/postinstall.mjs +0 -122
  10. package/script/publish.ts +0 -256
  11. package/script/schema.ts +0 -47
  12. package/src/acp/README.md +0 -164
  13. package/src/acp/agent.ts +0 -812
  14. package/src/acp/session.ts +0 -70
  15. package/src/acp/types.ts +0 -22
  16. package/src/agent/agent.ts +0 -310
  17. package/src/agent/generate.txt +0 -75
  18. package/src/auth/index.ts +0 -70
  19. package/src/bun/index.ts +0 -152
  20. package/src/bus/global.ts +0 -10
  21. package/src/bus/index.ts +0 -142
  22. package/src/cli/bootstrap.ts +0 -17
  23. package/src/cli/cmd/acp.ts +0 -88
  24. package/src/cli/cmd/agent.ts +0 -165
  25. package/src/cli/cmd/auth.ts +0 -369
  26. package/src/cli/cmd/cmd.ts +0 -7
  27. package/src/cli/cmd/debug/config.ts +0 -15
  28. package/src/cli/cmd/debug/file.ts +0 -91
  29. package/src/cli/cmd/debug/index.ts +0 -41
  30. package/src/cli/cmd/debug/lsp.ts +0 -47
  31. package/src/cli/cmd/debug/ripgrep.ts +0 -83
  32. package/src/cli/cmd/debug/scrap.ts +0 -15
  33. package/src/cli/cmd/debug/snapshot.ts +0 -48
  34. package/src/cli/cmd/export.ts +0 -88
  35. package/src/cli/cmd/generate.ts +0 -38
  36. package/src/cli/cmd/github.ts +0 -1200
  37. package/src/cli/cmd/import.ts +0 -98
  38. package/src/cli/cmd/mcp.ts +0 -400
  39. package/src/cli/cmd/models.ts +0 -77
  40. package/src/cli/cmd/pr.ts +0 -112
  41. package/src/cli/cmd/run.ts +0 -342
  42. package/src/cli/cmd/serve.ts +0 -31
  43. package/src/cli/cmd/session.ts +0 -106
  44. package/src/cli/cmd/stats.ts +0 -298
  45. package/src/cli/cmd/tui/app.tsx +0 -732
  46. package/src/cli/cmd/tui/attach.ts +0 -25
  47. package/src/cli/cmd/tui/component/border.tsx +0 -21
  48. package/src/cli/cmd/tui/component/dialog-agent.tsx +0 -31
  49. package/src/cli/cmd/tui/component/dialog-command.tsx +0 -124
  50. package/src/cli/cmd/tui/component/dialog-feedback.tsx +0 -160
  51. package/src/cli/cmd/tui/component/dialog-mcp.tsx +0 -86
  52. package/src/cli/cmd/tui/component/dialog-model.tsx +0 -223
  53. package/src/cli/cmd/tui/component/dialog-notification.tsx +0 -78
  54. package/src/cli/cmd/tui/component/dialog-provider.tsx +0 -222
  55. package/src/cli/cmd/tui/component/dialog-session-list.tsx +0 -97
  56. package/src/cli/cmd/tui/component/dialog-session-rename.tsx +0 -31
  57. package/src/cli/cmd/tui/component/dialog-status.tsx +0 -114
  58. package/src/cli/cmd/tui/component/dialog-tag.tsx +0 -44
  59. package/src/cli/cmd/tui/component/dialog-theme-list.tsx +0 -50
  60. package/src/cli/cmd/tui/component/logo.tsx +0 -37
  61. package/src/cli/cmd/tui/component/notification-banner.tsx +0 -58
  62. package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +0 -530
  63. package/src/cli/cmd/tui/component/prompt/history.tsx +0 -107
  64. package/src/cli/cmd/tui/component/prompt/index.tsx +0 -931
  65. package/src/cli/cmd/tui/context/args.tsx +0 -14
  66. package/src/cli/cmd/tui/context/directory.ts +0 -12
  67. package/src/cli/cmd/tui/context/exit.tsx +0 -23
  68. package/src/cli/cmd/tui/context/helper.tsx +0 -25
  69. package/src/cli/cmd/tui/context/keybind.tsx +0 -111
  70. package/src/cli/cmd/tui/context/kv.tsx +0 -49
  71. package/src/cli/cmd/tui/context/local.tsx +0 -339
  72. package/src/cli/cmd/tui/context/prompt.tsx +0 -18
  73. package/src/cli/cmd/tui/context/route.tsx +0 -45
  74. package/src/cli/cmd/tui/context/sdk.tsx +0 -75
  75. package/src/cli/cmd/tui/context/sync.tsx +0 -374
  76. package/src/cli/cmd/tui/context/theme/aura.json +0 -69
  77. package/src/cli/cmd/tui/context/theme/ayu.json +0 -80
  78. package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +0 -233
  79. package/src/cli/cmd/tui/context/theme/catppuccin.json +0 -112
  80. package/src/cli/cmd/tui/context/theme/cobalt2.json +0 -228
  81. package/src/cli/cmd/tui/context/theme/dracula.json +0 -219
  82. package/src/cli/cmd/tui/context/theme/everforest.json +0 -241
  83. package/src/cli/cmd/tui/context/theme/flexoki.json +0 -237
  84. package/src/cli/cmd/tui/context/theme/github.json +0 -233
  85. package/src/cli/cmd/tui/context/theme/gruvbox.json +0 -95
  86. package/src/cli/cmd/tui/context/theme/kanagawa.json +0 -77
  87. package/src/cli/cmd/tui/context/theme/material.json +0 -235
  88. package/src/cli/cmd/tui/context/theme/matrix.json +0 -77
  89. package/src/cli/cmd/tui/context/theme/mercury.json +0 -252
  90. package/src/cli/cmd/tui/context/theme/monokai.json +0 -221
  91. package/src/cli/cmd/tui/context/theme/nightowl.json +0 -221
  92. package/src/cli/cmd/tui/context/theme/nord.json +0 -223
  93. package/src/cli/cmd/tui/context/theme/one-dark.json +0 -84
  94. package/src/cli/cmd/tui/context/theme/orng.json +0 -245
  95. package/src/cli/cmd/tui/context/theme/palenight.json +0 -222
  96. package/src/cli/cmd/tui/context/theme/rosepine.json +0 -234
  97. package/src/cli/cmd/tui/context/theme/solarized.json +0 -223
  98. package/src/cli/cmd/tui/context/theme/synthwave84.json +0 -226
  99. package/src/cli/cmd/tui/context/theme/tokyonight.json +0 -243
  100. package/src/cli/cmd/tui/context/theme/vercel.json +0 -245
  101. package/src/cli/cmd/tui/context/theme/vesper.json +0 -218
  102. package/src/cli/cmd/tui/context/theme/zenburn.json +0 -223
  103. package/src/cli/cmd/tui/context/theme.tsx +0 -1077
  104. package/src/cli/cmd/tui/event.ts +0 -39
  105. package/src/cli/cmd/tui/routes/home.tsx +0 -104
  106. package/src/cli/cmd/tui/routes/session/dialog-message.tsx +0 -93
  107. package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +0 -37
  108. package/src/cli/cmd/tui/routes/session/footer.tsx +0 -76
  109. package/src/cli/cmd/tui/routes/session/header.tsx +0 -183
  110. package/src/cli/cmd/tui/routes/session/index.tsx +0 -1703
  111. package/src/cli/cmd/tui/routes/session/sidebar.tsx +0 -586
  112. package/src/cli/cmd/tui/spawn.ts +0 -60
  113. package/src/cli/cmd/tui/thread.ts +0 -120
  114. package/src/cli/cmd/tui/ui/dialog-alert.tsx +0 -55
  115. package/src/cli/cmd/tui/ui/dialog-confirm.tsx +0 -81
  116. package/src/cli/cmd/tui/ui/dialog-help.tsx +0 -36
  117. package/src/cli/cmd/tui/ui/dialog-prompt.tsx +0 -75
  118. package/src/cli/cmd/tui/ui/dialog-select.tsx +0 -317
  119. package/src/cli/cmd/tui/ui/dialog.tsx +0 -170
  120. package/src/cli/cmd/tui/ui/spinner.ts +0 -368
  121. package/src/cli/cmd/tui/ui/toast.tsx +0 -100
  122. package/src/cli/cmd/tui/util/clipboard.ts +0 -127
  123. package/src/cli/cmd/tui/util/editor.ts +0 -32
  124. package/src/cli/cmd/tui/util/terminal.ts +0 -114
  125. package/src/cli/cmd/tui/worker.ts +0 -63
  126. package/src/cli/cmd/uninstall.ts +0 -344
  127. package/src/cli/cmd/upgrade.ts +0 -67
  128. package/src/cli/cmd/web.ts +0 -84
  129. package/src/cli/error.ts +0 -55
  130. package/src/cli/ui.ts +0 -84
  131. package/src/cli/upgrade.ts +0 -25
  132. package/src/command/index.ts +0 -79
  133. package/src/command/template/initialize.txt +0 -10
  134. package/src/command/template/review.txt +0 -73
  135. package/src/config/config.ts +0 -886
  136. package/src/config/markdown.ts +0 -41
  137. package/src/env/index.ts +0 -26
  138. package/src/file/fzf.ts +0 -124
  139. package/src/file/ignore.ts +0 -83
  140. package/src/file/index.ts +0 -326
  141. package/src/file/ripgrep.ts +0 -391
  142. package/src/file/time.ts +0 -38
  143. package/src/file/watcher.ts +0 -89
  144. package/src/flag/flag.ts +0 -28
  145. package/src/format/formatter.ts +0 -277
  146. package/src/format/index.ts +0 -137
  147. package/src/global/index.ts +0 -52
  148. package/src/id/id.ts +0 -73
  149. package/src/ide/index.ts +0 -75
  150. package/src/index.ts +0 -158
  151. package/src/installation/index.ts +0 -194
  152. package/src/lsp/client.ts +0 -215
  153. package/src/lsp/index.ts +0 -370
  154. package/src/lsp/language.ts +0 -111
  155. package/src/lsp/server.ts +0 -1327
  156. package/src/mcp/auth.ts +0 -82
  157. package/src/mcp/index.ts +0 -576
  158. package/src/mcp/oauth-callback.ts +0 -203
  159. package/src/mcp/oauth-provider.ts +0 -132
  160. package/src/notification/index.ts +0 -101
  161. package/src/patch/index.ts +0 -622
  162. package/src/permission/index.ts +0 -198
  163. package/src/plugin/index.ts +0 -95
  164. package/src/project/bootstrap.ts +0 -31
  165. package/src/project/instance.ts +0 -68
  166. package/src/project/project.ts +0 -133
  167. package/src/project/state.ts +0 -65
  168. package/src/project/vcs.ts +0 -77
  169. package/src/provider/auth.ts +0 -143
  170. package/src/provider/models-macro.ts +0 -11
  171. package/src/provider/models.ts +0 -93
  172. package/src/provider/provider.ts +0 -996
  173. package/src/provider/sdk/openai-compatible/src/README.md +0 -5
  174. package/src/provider/sdk/openai-compatible/src/index.ts +0 -2
  175. package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +0 -100
  176. package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +0 -303
  177. package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +0 -27
  178. package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +0 -18
  179. package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +0 -22
  180. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +0 -207
  181. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +0 -1713
  182. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +0 -177
  183. package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +0 -1
  184. package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +0 -88
  185. package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +0 -128
  186. package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +0 -115
  187. package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +0 -65
  188. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +0 -104
  189. package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +0 -103
  190. package/src/provider/transform.ts +0 -406
  191. package/src/pty/index.ts +0 -226
  192. package/src/ratelimit/index.ts +0 -185
  193. package/src/server/error.ts +0 -36
  194. package/src/server/project.ts +0 -50
  195. package/src/server/server.ts +0 -2463
  196. package/src/server/tui.ts +0 -71
  197. package/src/session/compaction.ts +0 -257
  198. package/src/session/index.ts +0 -470
  199. package/src/session/message-v2.ts +0 -641
  200. package/src/session/message.ts +0 -189
  201. package/src/session/processor.ts +0 -443
  202. package/src/session/prompt/anthropic-20250930.txt +0 -166
  203. package/src/session/prompt/anthropic.txt +0 -105
  204. package/src/session/prompt/anthropic_spoof.txt +0 -1
  205. package/src/session/prompt/beast.txt +0 -147
  206. package/src/session/prompt/build-switch.txt +0 -5
  207. package/src/session/prompt/codex.txt +0 -318
  208. package/src/session/prompt/compaction.txt +0 -12
  209. package/src/session/prompt/copilot-gpt-5.txt +0 -143
  210. package/src/session/prompt/gemini.txt +0 -155
  211. package/src/session/prompt/max-steps.txt +0 -16
  212. package/src/session/prompt/plan-reminder-anthropic.txt +0 -67
  213. package/src/session/prompt/plan.txt +0 -26
  214. package/src/session/prompt/polaris.txt +0 -107
  215. package/src/session/prompt/qwen.txt +0 -109
  216. package/src/session/prompt/summarize.txt +0 -4
  217. package/src/session/prompt/title.txt +0 -36
  218. package/src/session/prompt.ts +0 -1541
  219. package/src/session/retry.ts +0 -82
  220. package/src/session/revert.ts +0 -108
  221. package/src/session/status.ts +0 -75
  222. package/src/session/summary.ts +0 -203
  223. package/src/session/system.ts +0 -148
  224. package/src/session/todo.ts +0 -36
  225. package/src/share/share-next.ts +0 -195
  226. package/src/share/share.ts +0 -87
  227. package/src/snapshot/index.ts +0 -197
  228. package/src/storage/storage.ts +0 -226
  229. package/src/telemetry/index.ts +0 -232
  230. package/src/tool/bash.ts +0 -365
  231. package/src/tool/bash.txt +0 -128
  232. package/src/tool/batch.ts +0 -173
  233. package/src/tool/batch.txt +0 -28
  234. package/src/tool/codesearch.ts +0 -138
  235. package/src/tool/codesearch.txt +0 -12
  236. package/src/tool/edit.ts +0 -674
  237. package/src/tool/edit.txt +0 -10
  238. package/src/tool/glob.ts +0 -65
  239. package/src/tool/glob.txt +0 -6
  240. package/src/tool/grep.ts +0 -120
  241. package/src/tool/grep.txt +0 -8
  242. package/src/tool/invalid.ts +0 -17
  243. package/src/tool/ls.ts +0 -110
  244. package/src/tool/ls.txt +0 -1
  245. package/src/tool/lsp-diagnostics.ts +0 -26
  246. package/src/tool/lsp-diagnostics.txt +0 -1
  247. package/src/tool/lsp-hover.ts +0 -31
  248. package/src/tool/lsp-hover.txt +0 -1
  249. package/src/tool/multiedit.ts +0 -46
  250. package/src/tool/multiedit.txt +0 -41
  251. package/src/tool/patch.ts +0 -233
  252. package/src/tool/patch.txt +0 -1
  253. package/src/tool/read.ts +0 -217
  254. package/src/tool/read.txt +0 -12
  255. package/src/tool/registry.ts +0 -148
  256. package/src/tool/task.ts +0 -135
  257. package/src/tool/task.txt +0 -60
  258. package/src/tool/todo.ts +0 -39
  259. package/src/tool/todoread.txt +0 -14
  260. package/src/tool/todowrite.txt +0 -167
  261. package/src/tool/tool.ts +0 -66
  262. package/src/tool/webfetch.ts +0 -187
  263. package/src/tool/webfetch.txt +0 -14
  264. package/src/tool/websearch.ts +0 -150
  265. package/src/tool/websearch.txt +0 -11
  266. package/src/tool/write.ts +0 -99
  267. package/src/tool/write.txt +0 -8
  268. package/src/types/shims.d.ts +0 -3
  269. package/src/util/color.ts +0 -19
  270. package/src/util/context.ts +0 -25
  271. package/src/util/defer.ts +0 -12
  272. package/src/util/eventloop.ts +0 -20
  273. package/src/util/filesystem.ts +0 -69
  274. package/src/util/fn.ts +0 -11
  275. package/src/util/iife.ts +0 -3
  276. package/src/util/keybind.ts +0 -79
  277. package/src/util/lazy.ts +0 -11
  278. package/src/util/locale.ts +0 -81
  279. package/src/util/lock.ts +0 -98
  280. package/src/util/log.ts +0 -177
  281. package/src/util/queue.ts +0 -32
  282. package/src/util/rpc.ts +0 -42
  283. package/src/util/scrap.ts +0 -10
  284. package/src/util/signal.ts +0 -12
  285. package/src/util/timeout.ts +0 -14
  286. package/src/util/token.ts +0 -7
  287. package/src/util/wildcard.ts +0 -54
  288. package/sst-env.d.ts +0 -9
  289. package/test/bun.test.ts +0 -53
  290. package/test/config/agent-color.test.ts +0 -66
  291. package/test/config/config.test.ts +0 -503
  292. package/test/config/markdown.test.ts +0 -89
  293. package/test/file/ignore.test.ts +0 -10
  294. package/test/fixture/fixture.ts +0 -28
  295. package/test/fixture/lsp/fake-lsp-server.js +0 -77
  296. package/test/ide/ide.test.ts +0 -82
  297. package/test/keybind.test.ts +0 -317
  298. package/test/lsp/client.test.ts +0 -95
  299. package/test/patch/patch.test.ts +0 -348
  300. package/test/preload.ts +0 -38
  301. package/test/project/project.test.ts +0 -42
  302. package/test/provider/provider.test.ts +0 -1809
  303. package/test/provider/transform.test.ts +0 -305
  304. package/test/session/retry.test.ts +0 -61
  305. package/test/session/session.test.ts +0 -71
  306. package/test/snapshot/snapshot.test.ts +0 -939
  307. package/test/tool/__snapshots__/tool.test.ts.snap +0 -9
  308. package/test/tool/bash.test.ts +0 -55
  309. package/test/tool/patch.test.ts +0 -259
  310. package/test/util/iife.test.ts +0 -36
  311. package/test/util/lazy.test.ts +0 -50
  312. package/test/util/timeout.test.ts +0 -21
  313. package/test/util/wildcard.test.ts +0 -55
  314. package/tsconfig.json +0 -17
package/src/pty/index.ts DELETED
@@ -1,226 +0,0 @@
1
- import { type IPty } from "bun-pty"
2
- import z from "zod"
3
- import { Identifier } from "../id/id"
4
- import { Log } from "../util/log"
5
- import { Bus } from "../bus"
6
- import type { WSContext } from "hono/ws"
7
- import { Instance } from "../project/instance"
8
- import { shell } from "@opencode-ai/util/shell"
9
- import { lazy } from "@opencode-ai/util/lazy"
10
- import {} from "process"
11
- import { Installation } from "@/installation"
12
-
13
- export namespace Pty {
14
- const log = Log.create({ service: "pty" })
15
-
16
- const pty = lazy(async () => {
17
- if (!Installation.isLocal()) {
18
- const path = require(
19
- `bun-pty/rust-pty/target/release/${
20
- process.platform === "win32"
21
- ? "rust_pty.dll"
22
- : process.platform === "linux" && process.arch === "x64"
23
- ? "librust_pty.so"
24
- : process.platform === "darwin" && process.arch === "x64"
25
- ? "librust_pty.dylib"
26
- : process.platform === "darwin" && process.arch === "arm64"
27
- ? "librust_pty_arm64.dylib"
28
- : process.platform === "linux" && process.arch === "arm64"
29
- ? "librust_pty_arm64.so"
30
- : ""
31
- }`,
32
- )
33
- process.env.BUN_PTY_LIB = path
34
- }
35
- const { spawn } = await import("bun-pty")
36
- return spawn
37
- })
38
-
39
- export const Info = z
40
- .object({
41
- id: Identifier.schema("pty"),
42
- title: z.string(),
43
- command: z.string(),
44
- args: z.array(z.string()),
45
- cwd: z.string(),
46
- status: z.enum(["running", "exited"]),
47
- pid: z.number(),
48
- })
49
- .meta({ ref: "Pty" })
50
-
51
- export type Info = z.infer<typeof Info>
52
-
53
- export const CreateInput = z.object({
54
- command: z.string().optional(),
55
- args: z.array(z.string()).optional(),
56
- cwd: z.string().optional(),
57
- title: z.string().optional(),
58
- env: z.record(z.string(), z.string()).optional(),
59
- })
60
-
61
- export type CreateInput = z.infer<typeof CreateInput>
62
-
63
- export const UpdateInput = z.object({
64
- title: z.string().optional(),
65
- size: z
66
- .object({
67
- rows: z.number(),
68
- cols: z.number(),
69
- })
70
- .optional(),
71
- })
72
-
73
- export type UpdateInput = z.infer<typeof UpdateInput>
74
-
75
- export const Event = {
76
- Created: Bus.event("pty.created", z.object({ info: Info })),
77
- Updated: Bus.event("pty.updated", z.object({ info: Info })),
78
- Exited: Bus.event("pty.exited", z.object({ id: Identifier.schema("pty"), exitCode: z.number() })),
79
- Deleted: Bus.event("pty.deleted", z.object({ id: Identifier.schema("pty") })),
80
- }
81
-
82
- interface ActiveSession {
83
- info: Info
84
- process: IPty
85
- buffer: string
86
- subscribers: Set<WSContext>
87
- }
88
-
89
- const state = Instance.state(
90
- () => new Map<string, ActiveSession>(),
91
- async (sessions) => {
92
- for (const session of sessions.values()) {
93
- try {
94
- session.process.kill()
95
- } catch {}
96
- for (const ws of session.subscribers) {
97
- ws.close()
98
- }
99
- }
100
- sessions.clear()
101
- },
102
- )
103
-
104
- export function list() {
105
- return Array.from(state().values()).map((s) => s.info)
106
- }
107
-
108
- export function get(id: string) {
109
- return state().get(id)?.info
110
- }
111
-
112
- export async function create(input: CreateInput) {
113
- const id = Identifier.create("pty", false)
114
- const command = input.command || shell()
115
- const args = input.args || []
116
- const cwd = input.cwd || Instance.directory
117
- const env = { ...process.env, ...input.env } as Record<string, string>
118
- log.info("creating session", { id, cmd: command, args, cwd })
119
-
120
- const spawn = await pty()
121
- const ptyProcess = spawn(command, args, {
122
- name: "xterm-256color",
123
- cwd,
124
- env,
125
- })
126
- const info = {
127
- id,
128
- title: input.title || `Terminal ${id.slice(-4)}`,
129
- command,
130
- args,
131
- cwd,
132
- status: "running",
133
- pid: ptyProcess.pid,
134
- } as const
135
- const session: ActiveSession = {
136
- info,
137
- process: ptyProcess,
138
- buffer: "",
139
- subscribers: new Set(),
140
- }
141
- state().set(id, session)
142
- ptyProcess.onData((data) => {
143
- if (session.subscribers.size === 0) {
144
- session.buffer += data
145
- return
146
- }
147
- for (const ws of session.subscribers) {
148
- if (ws.readyState === 1) {
149
- ws.send(data)
150
- }
151
- }
152
- })
153
- ptyProcess.onExit(({ exitCode }) => {
154
- log.info("session exited", { id, exitCode })
155
- session.info.status = "exited"
156
- Bus.publish(Event.Exited, { id, exitCode })
157
- state().delete(id)
158
- })
159
- Bus.publish(Event.Created, { info })
160
- return info
161
- }
162
-
163
- export async function update(id: string, input: UpdateInput) {
164
- const session = state().get(id)
165
- if (!session) return
166
- if (input.title) {
167
- session.info.title = input.title
168
- }
169
- if (input.size) {
170
- session.process.resize(input.size.cols, input.size.rows)
171
- }
172
- Bus.publish(Event.Updated, { info: session.info })
173
- return session.info
174
- }
175
-
176
- export async function remove(id: string) {
177
- const session = state().get(id)
178
- if (!session) return
179
- log.info("removing session", { id })
180
- try {
181
- session.process.kill()
182
- } catch {}
183
- for (const ws of session.subscribers) {
184
- ws.close()
185
- }
186
- state().delete(id)
187
- Bus.publish(Event.Deleted, { id })
188
- }
189
-
190
- export function resize(id: string, cols: number, rows: number) {
191
- const session = state().get(id)
192
- if (session && session.info.status === "running") {
193
- session.process.resize(cols, rows)
194
- }
195
- }
196
-
197
- export function write(id: string, data: string) {
198
- const session = state().get(id)
199
- if (session && session.info.status === "running") {
200
- session.process.write(data)
201
- }
202
- }
203
-
204
- export function connect(id: string, ws: WSContext) {
205
- const session = state().get(id)
206
- if (!session) {
207
- ws.close()
208
- return
209
- }
210
- log.info("client connected to session", { id })
211
- session.subscribers.add(ws)
212
- if (session.buffer) {
213
- ws.send(session.buffer)
214
- session.buffer = ""
215
- }
216
- return {
217
- onMessage: (message: string | ArrayBuffer) => {
218
- session.process.write(String(message))
219
- },
220
- onClose: () => {
221
- log.info("client disconnected from session", { id })
222
- session.subscribers.delete(ws)
223
- },
224
- }
225
- }
226
- }
@@ -1,185 +0,0 @@
1
- import { Bus } from "@/bus"
2
- import z from "zod"
3
-
4
- export namespace RateLimit {
5
- export const WindowInfo = z.object({
6
- remaining: z.number(),
7
- limit: z.number(),
8
- reset: z.string().optional(),
9
- window: z.string(), // "minute", "hour", "day", or "unknown"
10
- })
11
- export type WindowInfo = z.infer<typeof WindowInfo>
12
-
13
- export const Info = z.object({
14
- providerID: z.string(),
15
- // Legacy fields for backwards compatibility
16
- remainingRequests: z.number().optional(),
17
- limitRequests: z.number().optional(),
18
- remainingTokens: z.number().optional(),
19
- limitTokens: z.number().optional(),
20
- resetRequests: z.string().optional(),
21
- resetTokens: z.string().optional(),
22
- updatedAt: z.number().optional(),
23
- // All rate limits by window
24
- requestLimits: z.array(WindowInfo).optional(),
25
- tokenLimits: z.array(WindowInfo).optional(),
26
- })
27
- export type Info = z.infer<typeof Info>
28
-
29
- export const Event = {
30
- Update: Bus.event(
31
- "ratelimit.update",
32
- z.object({
33
- providerID: z.string(),
34
- info: Info,
35
- }),
36
- ),
37
- }
38
-
39
- // Simple global state (not instance-scoped since rate limits are global)
40
- const globalState = {
41
- data: {} as Record<string, Info>,
42
- }
43
- const state = () => globalState
44
-
45
- /**
46
- * Parse rate limit info from response headers.
47
- * Supports common header formats from various providers.
48
- * Captures all time windows (minute, hour, day) and finds the most restrictive.
49
- */
50
- export function parseHeaders(
51
- headers: Record<string, string | undefined> | Headers | undefined,
52
- providerID: string,
53
- ): Info | undefined {
54
- if (!headers) return undefined
55
-
56
- // Normalize headers to a simple object
57
- const h: Record<string, string | undefined> = {}
58
- if (headers instanceof Headers) {
59
- headers.forEach((value, key) => {
60
- h[key.toLowerCase()] = value
61
- })
62
- } else {
63
- for (const [key, value] of Object.entries(headers)) {
64
- h[key.toLowerCase()] = value
65
- }
66
- }
67
-
68
- const info: Info = { providerID, requestLimits: [], tokenLimits: [] }
69
-
70
- // Helper to parse a window's limits
71
- const parseWindow = (
72
- type: "requests" | "tokens",
73
- window: string,
74
- remainingKey: string,
75
- limitKey: string,
76
- resetKey: string,
77
- ) => {
78
- const remaining = h[remainingKey]
79
- const limit = h[limitKey]
80
- const reset = h[resetKey]
81
-
82
- if (remaining !== undefined && limit !== undefined) {
83
- const remainingNum = parseInt(remaining, 10)
84
- const limitNum = parseInt(limit, 10)
85
- // Skip if values aren't valid numbers
86
- if (isNaN(remainingNum) || isNaN(limitNum)) return
87
-
88
- const windowInfo: WindowInfo = {
89
- remaining: remainingNum,
90
- limit: limitNum,
91
- reset,
92
- window,
93
- }
94
- if (type === "requests") {
95
- info.requestLimits!.push(windowInfo)
96
- } else {
97
- info.tokenLimits!.push(windowInfo)
98
- }
99
- }
100
- }
101
-
102
- // Parse all time windows for requests
103
- parseWindow("requests", "minute", "x-ratelimit-remaining-requests-minute", "x-ratelimit-limit-requests-minute", "x-ratelimit-reset-requests-minute")
104
- parseWindow("requests", "hour", "x-ratelimit-remaining-requests-hour", "x-ratelimit-limit-requests-hour", "x-ratelimit-reset-requests-hour")
105
- parseWindow("requests", "day", "x-ratelimit-remaining-requests-day", "x-ratelimit-limit-requests-day", "x-ratelimit-reset-requests-day")
106
- parseWindow("requests", "unknown", "x-ratelimit-remaining-requests", "x-ratelimit-limit-requests", "x-ratelimit-reset-requests")
107
- parseWindow("requests", "unknown", "ratelimit-remaining-requests", "ratelimit-limit-requests", "ratelimit-reset-requests")
108
-
109
- // Parse all time windows for tokens
110
- parseWindow("tokens", "minute", "x-ratelimit-remaining-tokens-minute", "x-ratelimit-limit-tokens-minute", "x-ratelimit-reset-tokens-minute")
111
- parseWindow("tokens", "hour", "x-ratelimit-remaining-tokens-hour", "x-ratelimit-limit-tokens-hour", "x-ratelimit-reset-tokens-hour")
112
- parseWindow("tokens", "day", "x-ratelimit-remaining-tokens-day", "x-ratelimit-limit-tokens-day", "x-ratelimit-reset-tokens-day")
113
- parseWindow("tokens", "unknown", "x-ratelimit-remaining-tokens", "x-ratelimit-limit-tokens", "x-ratelimit-reset-tokens")
114
- parseWindow("tokens", "unknown", "ratelimit-remaining-tokens", "ratelimit-limit-tokens", "ratelimit-reset-tokens")
115
-
116
- // Find the most restrictive limits (lowest percentage remaining)
117
- const findMostRestrictive = (limits: WindowInfo[]): WindowInfo | undefined => {
118
- if (limits.length === 0) return undefined
119
- return limits.reduce((most, current) => {
120
- const mostPct = most.limit > 0 ? most.remaining / most.limit : 1
121
- const currentPct = current.limit > 0 ? current.remaining / current.limit : 1
122
- return currentPct < mostPct ? current : most
123
- })
124
- }
125
-
126
- const mostRestrictiveRequests = findMostRestrictive(info.requestLimits!)
127
- const mostRestrictiveTokens = findMostRestrictive(info.tokenLimits!)
128
-
129
- // Set legacy fields from most restrictive limits
130
- if (mostRestrictiveRequests) {
131
- info.remainingRequests = mostRestrictiveRequests.remaining
132
- info.limitRequests = mostRestrictiveRequests.limit
133
- info.resetRequests = mostRestrictiveRequests.reset
134
- }
135
- if (mostRestrictiveTokens) {
136
- info.remainingTokens = mostRestrictiveTokens.remaining
137
- info.limitTokens = mostRestrictiveTokens.limit
138
- info.resetTokens = mostRestrictiveTokens.reset
139
- }
140
-
141
- // Only return if we found any rate limit info
142
- if (
143
- info.remainingRequests !== undefined ||
144
- info.remainingTokens !== undefined ||
145
- info.limitRequests !== undefined ||
146
- info.limitTokens !== undefined
147
- ) {
148
- return info
149
- }
150
-
151
- return undefined
152
- }
153
-
154
- export function get(providerID: string): Info | undefined {
155
- return state().data[providerID]
156
- }
157
-
158
- /**
159
- * Store the latest rate limit info from a fetch response.
160
- * This is called from the provider's fetch wrapper.
161
- */
162
- export function setLatest(providerID: string, info: Info): void {
163
- const s = state()
164
-
165
- const infoWithTimestamp: Info = {
166
- ...info,
167
- updatedAt: Date.now(),
168
- }
169
-
170
- s.data[providerID] = infoWithTimestamp
171
-
172
- // Publish update event so UI can show progress bar
173
- Bus.publish(Event.Update, {
174
- providerID,
175
- info: infoWithTimestamp,
176
- })
177
- }
178
-
179
- /**
180
- * Get all rate limit info for all providers
181
- */
182
- export function getAll(): Record<string, Info> {
183
- return { ...state().data }
184
- }
185
- }
@@ -1,36 +0,0 @@
1
- import { resolver } from "hono-openapi"
2
- import z from "zod"
3
- import { Storage } from "../storage/storage"
4
-
5
- export const ERRORS = {
6
- 400: {
7
- description: "Bad request",
8
- content: {
9
- "application/json": {
10
- schema: resolver(
11
- z
12
- .object({
13
- data: z.any(),
14
- errors: z.array(z.record(z.string(), z.any())),
15
- success: z.literal(false),
16
- })
17
- .meta({
18
- ref: "BadRequestError",
19
- }),
20
- ),
21
- },
22
- },
23
- },
24
- 404: {
25
- description: "Not found",
26
- content: {
27
- "application/json": {
28
- schema: resolver(Storage.NotFoundError.Schema),
29
- },
30
- },
31
- },
32
- } as const
33
-
34
- export function errors(...codes: number[]) {
35
- return Object.fromEntries(codes.map((code) => [code, ERRORS[code as keyof typeof ERRORS]]))
36
- }
@@ -1,50 +0,0 @@
1
- import { Hono } from "hono"
2
- import { describeRoute } from "hono-openapi"
3
- import { resolver } from "hono-openapi"
4
- import { Instance } from "../project/instance"
5
- import { Project } from "../project/project"
6
-
7
- export const ProjectRoute = new Hono()
8
- .get(
9
- "/",
10
- describeRoute({
11
- summary: "List all projects",
12
- description: "Get a list of projects that have been opened with OpenCode.",
13
- operationId: "project.list",
14
- responses: {
15
- 200: {
16
- description: "List of projects",
17
- content: {
18
- "application/json": {
19
- schema: resolver(Project.Info.array()),
20
- },
21
- },
22
- },
23
- },
24
- }),
25
- async (c) => {
26
- const projects = await Project.list()
27
- return c.json(projects)
28
- },
29
- )
30
- .get(
31
- "/current",
32
- describeRoute({
33
- summary: "Get current project",
34
- description: "Retrieve the currently active project that OpenCode is working with.",
35
- operationId: "project.current",
36
- responses: {
37
- 200: {
38
- description: "Current project information",
39
- content: {
40
- "application/json": {
41
- schema: resolver(Project.Info),
42
- },
43
- },
44
- },
45
- },
46
- }),
47
- async (c) => {
48
- return c.json(Instance.project)
49
- },
50
- )