cerebras-cli 1.0.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.
- package/AGENTS.md +27 -0
- package/Dockerfile +10 -0
- package/README.md +15 -0
- package/bin/opencode +84 -0
- package/bunfig.toml +4 -0
- package/package.json +128 -0
- package/parsers-config.ts +239 -0
- package/script/build.ts +151 -0
- package/script/postinstall.mjs +122 -0
- package/script/publish.ts +256 -0
- package/script/schema.ts +47 -0
- package/src/acp/README.md +164 -0
- package/src/acp/agent.ts +812 -0
- package/src/acp/session.ts +70 -0
- package/src/acp/types.ts +22 -0
- package/src/agent/agent.ts +310 -0
- package/src/agent/generate.txt +75 -0
- package/src/auth/index.ts +70 -0
- package/src/bun/index.ts +152 -0
- package/src/bus/global.ts +10 -0
- package/src/bus/index.ts +142 -0
- package/src/cli/bootstrap.ts +17 -0
- package/src/cli/cmd/acp.ts +88 -0
- package/src/cli/cmd/agent.ts +165 -0
- package/src/cli/cmd/auth.ts +369 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/debug/config.ts +15 -0
- package/src/cli/cmd/debug/file.ts +91 -0
- package/src/cli/cmd/debug/index.ts +41 -0
- package/src/cli/cmd/debug/lsp.ts +47 -0
- package/src/cli/cmd/debug/ripgrep.ts +83 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +48 -0
- package/src/cli/cmd/export.ts +88 -0
- package/src/cli/cmd/generate.ts +38 -0
- package/src/cli/cmd/github.ts +1200 -0
- package/src/cli/cmd/import.ts +98 -0
- package/src/cli/cmd/mcp.ts +400 -0
- package/src/cli/cmd/models.ts +77 -0
- package/src/cli/cmd/pr.ts +112 -0
- package/src/cli/cmd/run.ts +342 -0
- package/src/cli/cmd/serve.ts +31 -0
- package/src/cli/cmd/session.ts +106 -0
- package/src/cli/cmd/stats.ts +298 -0
- package/src/cli/cmd/tui/app.tsx +732 -0
- package/src/cli/cmd/tui/attach.ts +25 -0
- package/src/cli/cmd/tui/component/border.tsx +21 -0
- package/src/cli/cmd/tui/component/dialog-agent.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-command.tsx +124 -0
- package/src/cli/cmd/tui/component/dialog-feedback.tsx +160 -0
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +86 -0
- package/src/cli/cmd/tui/component/dialog-model.tsx +223 -0
- package/src/cli/cmd/tui/component/dialog-notification.tsx +78 -0
- package/src/cli/cmd/tui/component/dialog-provider.tsx +222 -0
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +97 -0
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +31 -0
- package/src/cli/cmd/tui/component/dialog-status.tsx +114 -0
- package/src/cli/cmd/tui/component/dialog-tag.tsx +44 -0
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +50 -0
- package/src/cli/cmd/tui/component/logo.tsx +37 -0
- package/src/cli/cmd/tui/component/notification-banner.tsx +58 -0
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +530 -0
- package/src/cli/cmd/tui/component/prompt/history.tsx +107 -0
- package/src/cli/cmd/tui/component/prompt/index.tsx +931 -0
- package/src/cli/cmd/tui/context/args.tsx +14 -0
- package/src/cli/cmd/tui/context/directory.ts +12 -0
- package/src/cli/cmd/tui/context/exit.tsx +23 -0
- package/src/cli/cmd/tui/context/helper.tsx +25 -0
- package/src/cli/cmd/tui/context/keybind.tsx +111 -0
- package/src/cli/cmd/tui/context/kv.tsx +49 -0
- package/src/cli/cmd/tui/context/local.tsx +339 -0
- package/src/cli/cmd/tui/context/prompt.tsx +18 -0
- package/src/cli/cmd/tui/context/route.tsx +45 -0
- package/src/cli/cmd/tui/context/sdk.tsx +75 -0
- package/src/cli/cmd/tui/context/sync.tsx +374 -0
- package/src/cli/cmd/tui/context/theme/aura.json +69 -0
- package/src/cli/cmd/tui/context/theme/ayu.json +80 -0
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +233 -0
- package/src/cli/cmd/tui/context/theme/catppuccin.json +112 -0
- package/src/cli/cmd/tui/context/theme/cobalt2.json +228 -0
- package/src/cli/cmd/tui/context/theme/dracula.json +219 -0
- package/src/cli/cmd/tui/context/theme/everforest.json +241 -0
- package/src/cli/cmd/tui/context/theme/flexoki.json +237 -0
- package/src/cli/cmd/tui/context/theme/github.json +233 -0
- package/src/cli/cmd/tui/context/theme/gruvbox.json +95 -0
- package/src/cli/cmd/tui/context/theme/kanagawa.json +77 -0
- package/src/cli/cmd/tui/context/theme/material.json +235 -0
- package/src/cli/cmd/tui/context/theme/matrix.json +77 -0
- package/src/cli/cmd/tui/context/theme/mercury.json +252 -0
- package/src/cli/cmd/tui/context/theme/monokai.json +221 -0
- package/src/cli/cmd/tui/context/theme/nightowl.json +221 -0
- package/src/cli/cmd/tui/context/theme/nord.json +223 -0
- package/src/cli/cmd/tui/context/theme/one-dark.json +84 -0
- package/src/cli/cmd/tui/context/theme/orng.json +245 -0
- package/src/cli/cmd/tui/context/theme/palenight.json +222 -0
- package/src/cli/cmd/tui/context/theme/rosepine.json +234 -0
- package/src/cli/cmd/tui/context/theme/solarized.json +223 -0
- package/src/cli/cmd/tui/context/theme/synthwave84.json +226 -0
- package/src/cli/cmd/tui/context/theme/tokyonight.json +243 -0
- package/src/cli/cmd/tui/context/theme/vercel.json +245 -0
- package/src/cli/cmd/tui/context/theme/vesper.json +218 -0
- package/src/cli/cmd/tui/context/theme/zenburn.json +223 -0
- package/src/cli/cmd/tui/context/theme.tsx +1077 -0
- package/src/cli/cmd/tui/event.ts +39 -0
- package/src/cli/cmd/tui/routes/home.tsx +104 -0
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +93 -0
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +37 -0
- package/src/cli/cmd/tui/routes/session/footer.tsx +76 -0
- package/src/cli/cmd/tui/routes/session/header.tsx +183 -0
- package/src/cli/cmd/tui/routes/session/index.tsx +1703 -0
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +586 -0
- package/src/cli/cmd/tui/spawn.ts +60 -0
- package/src/cli/cmd/tui/thread.ts +120 -0
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +55 -0
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +81 -0
- package/src/cli/cmd/tui/ui/dialog-help.tsx +36 -0
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +75 -0
- package/src/cli/cmd/tui/ui/dialog-select.tsx +317 -0
- package/src/cli/cmd/tui/ui/dialog.tsx +170 -0
- package/src/cli/cmd/tui/ui/spinner.ts +368 -0
- package/src/cli/cmd/tui/ui/toast.tsx +100 -0
- package/src/cli/cmd/tui/util/clipboard.ts +127 -0
- package/src/cli/cmd/tui/util/editor.ts +32 -0
- package/src/cli/cmd/tui/util/terminal.ts +114 -0
- package/src/cli/cmd/tui/worker.ts +63 -0
- package/src/cli/cmd/uninstall.ts +344 -0
- package/src/cli/cmd/upgrade.ts +67 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/error.ts +55 -0
- package/src/cli/ui.ts +84 -0
- package/src/cli/upgrade.ts +25 -0
- package/src/command/index.ts +79 -0
- package/src/command/template/initialize.txt +10 -0
- package/src/command/template/review.txt +73 -0
- package/src/config/config.ts +886 -0
- package/src/config/markdown.ts +41 -0
- package/src/env/index.ts +26 -0
- package/src/file/fzf.ts +124 -0
- package/src/file/ignore.ts +83 -0
- package/src/file/index.ts +326 -0
- package/src/file/ripgrep.ts +391 -0
- package/src/file/time.ts +38 -0
- package/src/file/watcher.ts +89 -0
- package/src/flag/flag.ts +28 -0
- package/src/format/formatter.ts +277 -0
- package/src/format/index.ts +137 -0
- package/src/global/index.ts +52 -0
- package/src/id/id.ts +73 -0
- package/src/ide/index.ts +75 -0
- package/src/index.ts +158 -0
- package/src/installation/index.ts +194 -0
- package/src/lsp/client.ts +215 -0
- package/src/lsp/index.ts +370 -0
- package/src/lsp/language.ts +111 -0
- package/src/lsp/server.ts +1327 -0
- package/src/mcp/auth.ts +82 -0
- package/src/mcp/index.ts +576 -0
- package/src/mcp/oauth-callback.ts +203 -0
- package/src/mcp/oauth-provider.ts +132 -0
- package/src/notification/index.ts +101 -0
- package/src/patch/index.ts +622 -0
- package/src/permission/index.ts +198 -0
- package/src/plugin/index.ts +95 -0
- package/src/project/bootstrap.ts +31 -0
- package/src/project/instance.ts +68 -0
- package/src/project/project.ts +133 -0
- package/src/project/state.ts +65 -0
- package/src/project/vcs.ts +77 -0
- package/src/provider/auth.ts +143 -0
- package/src/provider/models-macro.ts +11 -0
- package/src/provider/models.ts +93 -0
- package/src/provider/provider.ts +996 -0
- package/src/provider/sdk/openai-compatible/src/README.md +5 -0
- package/src/provider/sdk/openai-compatible/src/index.ts +2 -0
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +100 -0
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +303 -0
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +27 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +18 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +22 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +207 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +1713 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +177 -0
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +1 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +88 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +128 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +115 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +65 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +104 -0
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +103 -0
- package/src/provider/transform.ts +406 -0
- package/src/pty/index.ts +226 -0
- package/src/ratelimit/index.ts +185 -0
- package/src/server/error.ts +36 -0
- package/src/server/project.ts +50 -0
- package/src/server/server.ts +2463 -0
- package/src/server/tui.ts +71 -0
- package/src/session/compaction.ts +257 -0
- package/src/session/index.ts +470 -0
- package/src/session/message-v2.ts +641 -0
- package/src/session/message.ts +189 -0
- package/src/session/processor.ts +443 -0
- package/src/session/prompt/anthropic-20250930.txt +166 -0
- package/src/session/prompt/anthropic.txt +105 -0
- package/src/session/prompt/anthropic_spoof.txt +1 -0
- package/src/session/prompt/beast.txt +147 -0
- package/src/session/prompt/build-switch.txt +5 -0
- package/src/session/prompt/codex.txt +318 -0
- package/src/session/prompt/compaction.txt +12 -0
- package/src/session/prompt/copilot-gpt-5.txt +143 -0
- package/src/session/prompt/gemini.txt +155 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/src/session/prompt/plan.txt +26 -0
- package/src/session/prompt/polaris.txt +107 -0
- package/src/session/prompt/qwen.txt +109 -0
- package/src/session/prompt/summarize.txt +4 -0
- package/src/session/prompt/title.txt +36 -0
- package/src/session/prompt.ts +1541 -0
- package/src/session/retry.ts +82 -0
- package/src/session/revert.ts +108 -0
- package/src/session/status.ts +75 -0
- package/src/session/summary.ts +203 -0
- package/src/session/system.ts +148 -0
- package/src/session/todo.ts +36 -0
- package/src/share/share-next.ts +195 -0
- package/src/share/share.ts +87 -0
- package/src/snapshot/index.ts +197 -0
- package/src/storage/storage.ts +226 -0
- package/src/telemetry/index.ts +232 -0
- package/src/tool/bash.ts +365 -0
- package/src/tool/bash.txt +128 -0
- package/src/tool/batch.ts +173 -0
- package/src/tool/batch.txt +28 -0
- package/src/tool/codesearch.ts +138 -0
- package/src/tool/codesearch.txt +12 -0
- package/src/tool/edit.ts +674 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/glob.ts +65 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +120 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +17 -0
- package/src/tool/ls.ts +110 -0
- package/src/tool/ls.txt +1 -0
- package/src/tool/lsp-diagnostics.ts +26 -0
- package/src/tool/lsp-diagnostics.txt +1 -0
- package/src/tool/lsp-hover.ts +31 -0
- package/src/tool/lsp-hover.txt +1 -0
- package/src/tool/multiedit.ts +46 -0
- package/src/tool/multiedit.txt +41 -0
- package/src/tool/patch.ts +233 -0
- package/src/tool/patch.txt +1 -0
- package/src/tool/read.ts +217 -0
- package/src/tool/read.txt +12 -0
- package/src/tool/registry.ts +148 -0
- package/src/tool/task.ts +135 -0
- package/src/tool/task.txt +60 -0
- package/src/tool/todo.ts +39 -0
- package/src/tool/todoread.txt +14 -0
- package/src/tool/todowrite.txt +167 -0
- package/src/tool/tool.ts +66 -0
- package/src/tool/webfetch.ts +187 -0
- package/src/tool/webfetch.txt +14 -0
- package/src/tool/websearch.ts +150 -0
- package/src/tool/websearch.txt +11 -0
- package/src/tool/write.ts +99 -0
- package/src/tool/write.txt +8 -0
- package/src/types/shims.d.ts +3 -0
- package/src/util/color.ts +19 -0
- package/src/util/context.ts +25 -0
- package/src/util/defer.ts +12 -0
- package/src/util/eventloop.ts +20 -0
- package/src/util/filesystem.ts +69 -0
- package/src/util/fn.ts +11 -0
- package/src/util/iife.ts +3 -0
- package/src/util/keybind.ts +79 -0
- package/src/util/lazy.ts +11 -0
- package/src/util/locale.ts +81 -0
- package/src/util/lock.ts +98 -0
- package/src/util/log.ts +177 -0
- package/src/util/queue.ts +32 -0
- package/src/util/rpc.ts +42 -0
- package/src/util/scrap.ts +10 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +14 -0
- package/src/util/token.ts +7 -0
- package/src/util/wildcard.ts +54 -0
- package/sst-env.d.ts +9 -0
- package/test/bun.test.ts +53 -0
- package/test/config/agent-color.test.ts +66 -0
- package/test/config/config.test.ts +503 -0
- package/test/config/markdown.test.ts +89 -0
- package/test/file/ignore.test.ts +10 -0
- package/test/fixture/fixture.ts +28 -0
- package/test/fixture/lsp/fake-lsp-server.js +77 -0
- package/test/ide/ide.test.ts +82 -0
- package/test/keybind.test.ts +317 -0
- package/test/lsp/client.test.ts +95 -0
- package/test/patch/patch.test.ts +348 -0
- package/test/preload.ts +38 -0
- package/test/project/project.test.ts +42 -0
- package/test/provider/provider.test.ts +1809 -0
- package/test/provider/transform.test.ts +305 -0
- package/test/session/retry.test.ts +61 -0
- package/test/session/session.test.ts +71 -0
- package/test/snapshot/snapshot.test.ts +939 -0
- package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
- package/test/tool/bash.test.ts +55 -0
- package/test/tool/patch.test.ts +259 -0
- package/test/util/iife.test.ts +36 -0
- package/test/util/lazy.test.ts +50 -0
- package/test/util/timeout.test.ts +21 -0
- package/test/util/wildcard.test.ts +55 -0
- package/tsconfig.json +17 -0
package/src/pty/index.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,185 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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
|
+
)
|