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
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { Argv } from "yargs"
|
|
2
|
+
import { Session } from "../../session"
|
|
3
|
+
import { cmd } from "./cmd"
|
|
4
|
+
import { bootstrap } from "../bootstrap"
|
|
5
|
+
import { Storage } from "../../storage/storage"
|
|
6
|
+
import { Instance } from "../../project/instance"
|
|
7
|
+
import { EOL } from "os"
|
|
8
|
+
|
|
9
|
+
export const ImportCommand = cmd({
|
|
10
|
+
command: "import <file>",
|
|
11
|
+
describe: "import session data from JSON file or URL",
|
|
12
|
+
builder: (yargs: Argv) => {
|
|
13
|
+
return yargs.positional("file", {
|
|
14
|
+
describe: "path to JSON file or opencode.ai share URL",
|
|
15
|
+
type: "string",
|
|
16
|
+
demandOption: true,
|
|
17
|
+
})
|
|
18
|
+
},
|
|
19
|
+
handler: async (args) => {
|
|
20
|
+
await bootstrap(process.cwd(), async () => {
|
|
21
|
+
let exportData:
|
|
22
|
+
| {
|
|
23
|
+
info: Session.Info
|
|
24
|
+
messages: Array<{
|
|
25
|
+
info: any
|
|
26
|
+
parts: any[]
|
|
27
|
+
}>
|
|
28
|
+
}
|
|
29
|
+
| undefined
|
|
30
|
+
|
|
31
|
+
const isUrl = args.file.startsWith("http://") || args.file.startsWith("https://")
|
|
32
|
+
|
|
33
|
+
if (isUrl) {
|
|
34
|
+
const urlMatch = args.file.match(/https?:\/\/opencode\.ai\/s\/([a-zA-Z0-9_-]+)/)
|
|
35
|
+
if (!urlMatch) {
|
|
36
|
+
process.stdout.write(`Invalid URL format. Expected: https://opencode.ai/s/<slug>`)
|
|
37
|
+
process.stdout.write(EOL)
|
|
38
|
+
return
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const slug = urlMatch[1]
|
|
42
|
+
const response = await fetch(`https://api.opencode.ai/share_data?id=${slug}`)
|
|
43
|
+
|
|
44
|
+
if (!response.ok) {
|
|
45
|
+
process.stdout.write(`Failed to fetch share data: ${response.statusText}`)
|
|
46
|
+
process.stdout.write(EOL)
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
const data = await response.json()
|
|
51
|
+
|
|
52
|
+
if (!data.info || !data.messages || Object.keys(data.messages).length === 0) {
|
|
53
|
+
process.stdout.write(`Share not found: ${slug}`)
|
|
54
|
+
process.stdout.write(EOL)
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
exportData = {
|
|
59
|
+
info: data.info,
|
|
60
|
+
messages: Object.values(data.messages).map((msg: any) => {
|
|
61
|
+
const { parts, ...info } = msg
|
|
62
|
+
return {
|
|
63
|
+
info,
|
|
64
|
+
parts,
|
|
65
|
+
}
|
|
66
|
+
}),
|
|
67
|
+
}
|
|
68
|
+
} else {
|
|
69
|
+
const file = Bun.file(args.file)
|
|
70
|
+
exportData = await file.json().catch(() => {})
|
|
71
|
+
if (!exportData) {
|
|
72
|
+
process.stdout.write(`File not found: ${args.file}`)
|
|
73
|
+
process.stdout.write(EOL)
|
|
74
|
+
return
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!exportData) {
|
|
79
|
+
process.stdout.write(`Failed to read session data`)
|
|
80
|
+
process.stdout.write(EOL)
|
|
81
|
+
return
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
await Storage.write(["session", Instance.project.id, exportData.info.id], exportData.info)
|
|
85
|
+
|
|
86
|
+
for (const msg of exportData.messages) {
|
|
87
|
+
await Storage.write(["message", exportData.info.id, msg.info.id], msg.info)
|
|
88
|
+
|
|
89
|
+
for (const part of msg.parts) {
|
|
90
|
+
await Storage.write(["part", msg.info.id, part.id], part)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
process.stdout.write(`Imported session: ${exportData.info.id}`)
|
|
95
|
+
process.stdout.write(EOL)
|
|
96
|
+
})
|
|
97
|
+
},
|
|
98
|
+
})
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
import { cmd } from "./cmd"
|
|
2
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js"
|
|
3
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js"
|
|
4
|
+
import * as prompts from "@clack/prompts"
|
|
5
|
+
import { UI } from "../ui"
|
|
6
|
+
import { MCP } from "../../mcp"
|
|
7
|
+
import { McpAuth } from "../../mcp/auth"
|
|
8
|
+
import { Config } from "../../config/config"
|
|
9
|
+
import { Instance } from "../../project/instance"
|
|
10
|
+
import path from "path"
|
|
11
|
+
import os from "os"
|
|
12
|
+
import { Global } from "../../global"
|
|
13
|
+
|
|
14
|
+
export const McpCommand = cmd({
|
|
15
|
+
command: "mcp",
|
|
16
|
+
builder: (yargs) =>
|
|
17
|
+
yargs
|
|
18
|
+
.command(McpAddCommand)
|
|
19
|
+
.command(McpListCommand)
|
|
20
|
+
.command(McpAuthCommand)
|
|
21
|
+
.command(McpLogoutCommand)
|
|
22
|
+
.demandCommand(),
|
|
23
|
+
async handler() {},
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
export const McpListCommand = cmd({
|
|
27
|
+
command: "list",
|
|
28
|
+
aliases: ["ls"],
|
|
29
|
+
describe: "list MCP servers and their status",
|
|
30
|
+
async handler() {
|
|
31
|
+
await Instance.provide({
|
|
32
|
+
directory: process.cwd(),
|
|
33
|
+
async fn() {
|
|
34
|
+
UI.empty()
|
|
35
|
+
prompts.intro("MCP Servers")
|
|
36
|
+
|
|
37
|
+
const config = await Config.get()
|
|
38
|
+
const mcpServers = config.mcp ?? {}
|
|
39
|
+
const statuses = await MCP.status()
|
|
40
|
+
|
|
41
|
+
if (Object.keys(mcpServers).length === 0) {
|
|
42
|
+
prompts.log.warn("No MCP servers configured")
|
|
43
|
+
prompts.outro("Add servers with: opencode mcp add")
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (const [name, serverConfig] of Object.entries(mcpServers)) {
|
|
48
|
+
const status = statuses[name]
|
|
49
|
+
const hasOAuth = serverConfig.type === "remote" && !!serverConfig.oauth
|
|
50
|
+
const hasStoredTokens = await MCP.hasStoredTokens(name)
|
|
51
|
+
|
|
52
|
+
let statusIcon: string
|
|
53
|
+
let statusText: string
|
|
54
|
+
let hint = ""
|
|
55
|
+
|
|
56
|
+
if (!status) {
|
|
57
|
+
statusIcon = "○"
|
|
58
|
+
statusText = "not initialized"
|
|
59
|
+
} else if (status.status === "connected") {
|
|
60
|
+
statusIcon = "✓"
|
|
61
|
+
statusText = "connected"
|
|
62
|
+
if (hasOAuth && hasStoredTokens) {
|
|
63
|
+
hint = " (OAuth)"
|
|
64
|
+
}
|
|
65
|
+
} else if (status.status === "disabled") {
|
|
66
|
+
statusIcon = "○"
|
|
67
|
+
statusText = "disabled"
|
|
68
|
+
} else if (status.status === "needs_auth") {
|
|
69
|
+
statusIcon = "⚠"
|
|
70
|
+
statusText = "needs authentication"
|
|
71
|
+
} else if (status.status === "needs_client_registration") {
|
|
72
|
+
statusIcon = "✗"
|
|
73
|
+
statusText = "needs client registration"
|
|
74
|
+
hint = "\n " + status.error
|
|
75
|
+
} else {
|
|
76
|
+
statusIcon = "✗"
|
|
77
|
+
statusText = "failed"
|
|
78
|
+
hint = "\n " + status.error
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const typeHint = serverConfig.type === "remote" ? serverConfig.url : serverConfig.command.join(" ")
|
|
82
|
+
prompts.log.info(
|
|
83
|
+
`${statusIcon} ${name} ${UI.Style.TEXT_DIM}${statusText}${hint}\n ${UI.Style.TEXT_DIM}${typeHint}`,
|
|
84
|
+
)
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
prompts.outro(`${Object.keys(mcpServers).length} server(s)`)
|
|
88
|
+
},
|
|
89
|
+
})
|
|
90
|
+
},
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
export const McpAuthCommand = cmd({
|
|
94
|
+
command: "auth [name]",
|
|
95
|
+
describe: "authenticate with an OAuth-enabled MCP server",
|
|
96
|
+
builder: (yargs) =>
|
|
97
|
+
yargs.positional("name", {
|
|
98
|
+
describe: "name of the MCP server",
|
|
99
|
+
type: "string",
|
|
100
|
+
}),
|
|
101
|
+
async handler(args) {
|
|
102
|
+
await Instance.provide({
|
|
103
|
+
directory: process.cwd(),
|
|
104
|
+
async fn() {
|
|
105
|
+
UI.empty()
|
|
106
|
+
prompts.intro("MCP OAuth Authentication")
|
|
107
|
+
|
|
108
|
+
const config = await Config.get()
|
|
109
|
+
const mcpServers = config.mcp ?? {}
|
|
110
|
+
|
|
111
|
+
// Get OAuth-enabled servers
|
|
112
|
+
const oauthServers = Object.entries(mcpServers).filter(([_, cfg]) => cfg.type === "remote" && !!cfg.oauth)
|
|
113
|
+
|
|
114
|
+
if (oauthServers.length === 0) {
|
|
115
|
+
prompts.log.warn("No OAuth-enabled MCP servers configured")
|
|
116
|
+
prompts.log.info("Add OAuth config to a remote MCP server in opencode.json:")
|
|
117
|
+
prompts.log.info(`
|
|
118
|
+
"mcp": {
|
|
119
|
+
"my-server": {
|
|
120
|
+
"type": "remote",
|
|
121
|
+
"url": "https://example.com/mcp",
|
|
122
|
+
"oauth": {
|
|
123
|
+
"scope": "tools:read"
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}`)
|
|
127
|
+
prompts.outro("Done")
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
let serverName = args.name
|
|
132
|
+
if (!serverName) {
|
|
133
|
+
const selected = await prompts.select({
|
|
134
|
+
message: "Select MCP server to authenticate",
|
|
135
|
+
options: oauthServers.map(([name, cfg]) => ({
|
|
136
|
+
label: name,
|
|
137
|
+
value: name,
|
|
138
|
+
hint: cfg.type === "remote" ? cfg.url : undefined,
|
|
139
|
+
})),
|
|
140
|
+
})
|
|
141
|
+
if (prompts.isCancel(selected)) throw new UI.CancelledError()
|
|
142
|
+
serverName = selected
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const serverConfig = mcpServers[serverName]
|
|
146
|
+
if (!serverConfig) {
|
|
147
|
+
prompts.log.error(`MCP server not found: ${serverName}`)
|
|
148
|
+
prompts.outro("Done")
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (serverConfig.type !== "remote" || !serverConfig.oauth) {
|
|
153
|
+
prompts.log.error(`MCP server ${serverName} does not have OAuth configured`)
|
|
154
|
+
prompts.outro("Done")
|
|
155
|
+
return
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Check if already authenticated
|
|
159
|
+
const hasTokens = await MCP.hasStoredTokens(serverName)
|
|
160
|
+
if (hasTokens) {
|
|
161
|
+
const confirm = await prompts.confirm({
|
|
162
|
+
message: `${serverName} already has stored credentials. Re-authenticate?`,
|
|
163
|
+
})
|
|
164
|
+
if (prompts.isCancel(confirm) || !confirm) {
|
|
165
|
+
prompts.outro("Cancelled")
|
|
166
|
+
return
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
const spinner = prompts.spinner()
|
|
171
|
+
spinner.start("Starting OAuth flow...")
|
|
172
|
+
|
|
173
|
+
try {
|
|
174
|
+
const status = await MCP.authenticate(serverName)
|
|
175
|
+
|
|
176
|
+
if (status.status === "connected") {
|
|
177
|
+
spinner.stop("Authentication successful!")
|
|
178
|
+
} else if (status.status === "needs_client_registration") {
|
|
179
|
+
spinner.stop("Authentication failed", 1)
|
|
180
|
+
prompts.log.error(status.error)
|
|
181
|
+
prompts.log.info("Add clientId to your MCP server config:")
|
|
182
|
+
prompts.log.info(`
|
|
183
|
+
"mcp": {
|
|
184
|
+
"${serverName}": {
|
|
185
|
+
"type": "remote",
|
|
186
|
+
"url": "${serverConfig.url}",
|
|
187
|
+
"oauth": {
|
|
188
|
+
"clientId": "your-client-id",
|
|
189
|
+
"clientSecret": "your-client-secret"
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
}`)
|
|
193
|
+
} else if (status.status === "failed") {
|
|
194
|
+
spinner.stop("Authentication failed", 1)
|
|
195
|
+
prompts.log.error(status.error)
|
|
196
|
+
} else {
|
|
197
|
+
spinner.stop("Unexpected status: " + status.status, 1)
|
|
198
|
+
}
|
|
199
|
+
} catch (error) {
|
|
200
|
+
spinner.stop("Authentication failed", 1)
|
|
201
|
+
prompts.log.error(error instanceof Error ? error.message : String(error))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
prompts.outro("Done")
|
|
205
|
+
},
|
|
206
|
+
})
|
|
207
|
+
},
|
|
208
|
+
})
|
|
209
|
+
|
|
210
|
+
export const McpLogoutCommand = cmd({
|
|
211
|
+
command: "logout [name]",
|
|
212
|
+
describe: "remove OAuth credentials for an MCP server",
|
|
213
|
+
builder: (yargs) =>
|
|
214
|
+
yargs.positional("name", {
|
|
215
|
+
describe: "name of the MCP server",
|
|
216
|
+
type: "string",
|
|
217
|
+
}),
|
|
218
|
+
async handler(args) {
|
|
219
|
+
await Instance.provide({
|
|
220
|
+
directory: process.cwd(),
|
|
221
|
+
async fn() {
|
|
222
|
+
UI.empty()
|
|
223
|
+
prompts.intro("MCP OAuth Logout")
|
|
224
|
+
|
|
225
|
+
const authPath = path.join(Global.Path.data, "mcp-auth.json")
|
|
226
|
+
const credentials = await McpAuth.all()
|
|
227
|
+
const serverNames = Object.keys(credentials)
|
|
228
|
+
|
|
229
|
+
if (serverNames.length === 0) {
|
|
230
|
+
prompts.log.warn("No MCP OAuth credentials stored")
|
|
231
|
+
prompts.outro("Done")
|
|
232
|
+
return
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
let serverName = args.name
|
|
236
|
+
if (!serverName) {
|
|
237
|
+
const selected = await prompts.select({
|
|
238
|
+
message: "Select MCP server to logout",
|
|
239
|
+
options: serverNames.map((name) => {
|
|
240
|
+
const entry = credentials[name]
|
|
241
|
+
const hasTokens = !!entry.tokens
|
|
242
|
+
const hasClient = !!entry.clientInfo
|
|
243
|
+
let hint = ""
|
|
244
|
+
if (hasTokens && hasClient) hint = "tokens + client"
|
|
245
|
+
else if (hasTokens) hint = "tokens"
|
|
246
|
+
else if (hasClient) hint = "client registration"
|
|
247
|
+
return {
|
|
248
|
+
label: name,
|
|
249
|
+
value: name,
|
|
250
|
+
hint,
|
|
251
|
+
}
|
|
252
|
+
}),
|
|
253
|
+
})
|
|
254
|
+
if (prompts.isCancel(selected)) throw new UI.CancelledError()
|
|
255
|
+
serverName = selected
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (!credentials[serverName]) {
|
|
259
|
+
prompts.log.error(`No credentials found for: ${serverName}`)
|
|
260
|
+
prompts.outro("Done")
|
|
261
|
+
return
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
await MCP.removeAuth(serverName)
|
|
265
|
+
prompts.log.success(`Removed OAuth credentials for ${serverName}`)
|
|
266
|
+
prompts.outro("Done")
|
|
267
|
+
},
|
|
268
|
+
})
|
|
269
|
+
},
|
|
270
|
+
})
|
|
271
|
+
|
|
272
|
+
export const McpAddCommand = cmd({
|
|
273
|
+
command: "add",
|
|
274
|
+
describe: "add an MCP server",
|
|
275
|
+
async handler() {
|
|
276
|
+
UI.empty()
|
|
277
|
+
prompts.intro("Add MCP server")
|
|
278
|
+
|
|
279
|
+
const name = await prompts.text({
|
|
280
|
+
message: "Enter MCP server name",
|
|
281
|
+
validate: (x) => (x && x.length > 0 ? undefined : "Required"),
|
|
282
|
+
})
|
|
283
|
+
if (prompts.isCancel(name)) throw new UI.CancelledError()
|
|
284
|
+
|
|
285
|
+
const type = await prompts.select({
|
|
286
|
+
message: "Select MCP server type",
|
|
287
|
+
options: [
|
|
288
|
+
{
|
|
289
|
+
label: "Local",
|
|
290
|
+
value: "local",
|
|
291
|
+
hint: "Run a local command",
|
|
292
|
+
},
|
|
293
|
+
{
|
|
294
|
+
label: "Remote",
|
|
295
|
+
value: "remote",
|
|
296
|
+
hint: "Connect to a remote URL",
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
})
|
|
300
|
+
if (prompts.isCancel(type)) throw new UI.CancelledError()
|
|
301
|
+
|
|
302
|
+
if (type === "local") {
|
|
303
|
+
const command = await prompts.text({
|
|
304
|
+
message: "Enter command to run",
|
|
305
|
+
placeholder: "e.g., opencode x @modelcontextprotocol/server-filesystem",
|
|
306
|
+
validate: (x) => (x && x.length > 0 ? undefined : "Required"),
|
|
307
|
+
})
|
|
308
|
+
if (prompts.isCancel(command)) throw new UI.CancelledError()
|
|
309
|
+
|
|
310
|
+
prompts.log.info(`Local MCP server "${name}" configured with command: ${command}`)
|
|
311
|
+
prompts.outro("MCP server added successfully")
|
|
312
|
+
return
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
if (type === "remote") {
|
|
316
|
+
const url = await prompts.text({
|
|
317
|
+
message: "Enter MCP server URL",
|
|
318
|
+
placeholder: "e.g., https://example.com/mcp",
|
|
319
|
+
validate: (x) => {
|
|
320
|
+
if (!x) return "Required"
|
|
321
|
+
if (x.length === 0) return "Required"
|
|
322
|
+
const isValid = URL.canParse(x)
|
|
323
|
+
return isValid ? undefined : "Invalid URL"
|
|
324
|
+
},
|
|
325
|
+
})
|
|
326
|
+
if (prompts.isCancel(url)) throw new UI.CancelledError()
|
|
327
|
+
|
|
328
|
+
const useOAuth = await prompts.confirm({
|
|
329
|
+
message: "Does this server require OAuth authentication?",
|
|
330
|
+
initialValue: false,
|
|
331
|
+
})
|
|
332
|
+
if (prompts.isCancel(useOAuth)) throw new UI.CancelledError()
|
|
333
|
+
|
|
334
|
+
if (useOAuth) {
|
|
335
|
+
const hasClientId = await prompts.confirm({
|
|
336
|
+
message: "Do you have a pre-registered client ID?",
|
|
337
|
+
initialValue: false,
|
|
338
|
+
})
|
|
339
|
+
if (prompts.isCancel(hasClientId)) throw new UI.CancelledError()
|
|
340
|
+
|
|
341
|
+
if (hasClientId) {
|
|
342
|
+
const clientId = await prompts.text({
|
|
343
|
+
message: "Enter client ID",
|
|
344
|
+
validate: (x) => (x && x.length > 0 ? undefined : "Required"),
|
|
345
|
+
})
|
|
346
|
+
if (prompts.isCancel(clientId)) throw new UI.CancelledError()
|
|
347
|
+
|
|
348
|
+
const hasSecret = await prompts.confirm({
|
|
349
|
+
message: "Do you have a client secret?",
|
|
350
|
+
initialValue: false,
|
|
351
|
+
})
|
|
352
|
+
if (prompts.isCancel(hasSecret)) throw new UI.CancelledError()
|
|
353
|
+
|
|
354
|
+
let clientSecret: string | undefined
|
|
355
|
+
if (hasSecret) {
|
|
356
|
+
const secret = await prompts.password({
|
|
357
|
+
message: "Enter client secret",
|
|
358
|
+
})
|
|
359
|
+
if (prompts.isCancel(secret)) throw new UI.CancelledError()
|
|
360
|
+
clientSecret = secret
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
prompts.log.info(`Remote MCP server "${name}" configured with OAuth (client ID: ${clientId})`)
|
|
364
|
+
prompts.log.info("Add this to your opencode.json:")
|
|
365
|
+
prompts.log.info(`
|
|
366
|
+
"mcp": {
|
|
367
|
+
"${name}": {
|
|
368
|
+
"type": "remote",
|
|
369
|
+
"url": "${url}",
|
|
370
|
+
"oauth": {
|
|
371
|
+
"clientId": "${clientId}"${clientSecret ? `,\n "clientSecret": "${clientSecret}"` : ""}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
}`)
|
|
375
|
+
} else {
|
|
376
|
+
prompts.log.info(`Remote MCP server "${name}" configured with OAuth (dynamic registration)`)
|
|
377
|
+
prompts.log.info("Add this to your opencode.json:")
|
|
378
|
+
prompts.log.info(`
|
|
379
|
+
"mcp": {
|
|
380
|
+
"${name}": {
|
|
381
|
+
"type": "remote",
|
|
382
|
+
"url": "${url}",
|
|
383
|
+
"oauth": {}
|
|
384
|
+
}
|
|
385
|
+
}`)
|
|
386
|
+
}
|
|
387
|
+
} else {
|
|
388
|
+
const client = new Client({
|
|
389
|
+
name: "opencode",
|
|
390
|
+
version: "1.0.0",
|
|
391
|
+
})
|
|
392
|
+
const transport = new StreamableHTTPClientTransport(new URL(url))
|
|
393
|
+
await client.connect(transport)
|
|
394
|
+
prompts.log.info(`Remote MCP server "${name}" configured with URL: ${url}`)
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
prompts.outro("MCP server added successfully")
|
|
399
|
+
},
|
|
400
|
+
})
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import type { Argv } from "yargs"
|
|
2
|
+
import { Instance } from "../../project/instance"
|
|
3
|
+
import { Provider } from "../../provider/provider"
|
|
4
|
+
import { ModelsDev } from "../../provider/models"
|
|
5
|
+
import { cmd } from "./cmd"
|
|
6
|
+
import { UI } from "../ui"
|
|
7
|
+
import { EOL } from "os"
|
|
8
|
+
|
|
9
|
+
export const ModelsCommand = cmd({
|
|
10
|
+
command: "models [provider]",
|
|
11
|
+
describe: "list all available models",
|
|
12
|
+
builder: (yargs: Argv) => {
|
|
13
|
+
return yargs
|
|
14
|
+
.positional("provider", {
|
|
15
|
+
describe: "provider ID to filter models by",
|
|
16
|
+
type: "string",
|
|
17
|
+
array: false,
|
|
18
|
+
})
|
|
19
|
+
.option("verbose", {
|
|
20
|
+
describe: "use more verbose model output (includes metadata like costs)",
|
|
21
|
+
type: "boolean",
|
|
22
|
+
})
|
|
23
|
+
.option("refresh", {
|
|
24
|
+
describe: "refresh the models cache from models.dev",
|
|
25
|
+
type: "boolean",
|
|
26
|
+
})
|
|
27
|
+
},
|
|
28
|
+
handler: async (args) => {
|
|
29
|
+
if (args.refresh) {
|
|
30
|
+
await ModelsDev.refresh()
|
|
31
|
+
UI.println(UI.Style.TEXT_SUCCESS_BOLD + "Models cache refreshed" + UI.Style.TEXT_NORMAL)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
await Instance.provide({
|
|
35
|
+
directory: process.cwd(),
|
|
36
|
+
async fn() {
|
|
37
|
+
const providers = await Provider.list()
|
|
38
|
+
|
|
39
|
+
function printModels(providerID: string, verbose?: boolean) {
|
|
40
|
+
const provider = providers[providerID]
|
|
41
|
+
const sortedModels = Object.entries(provider.models).sort(([a], [b]) => a.localeCompare(b))
|
|
42
|
+
for (const [modelID, model] of sortedModels) {
|
|
43
|
+
process.stdout.write(`${providerID}/${modelID}`)
|
|
44
|
+
process.stdout.write(EOL)
|
|
45
|
+
if (verbose) {
|
|
46
|
+
process.stdout.write(JSON.stringify(model, null, 2))
|
|
47
|
+
process.stdout.write(EOL)
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (args.provider) {
|
|
53
|
+
const provider = providers[args.provider]
|
|
54
|
+
if (!provider) {
|
|
55
|
+
UI.error(`Provider not found: ${args.provider}`)
|
|
56
|
+
return
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
printModels(args.provider, args.verbose)
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const providerIDs = Object.keys(providers).sort((a, b) => {
|
|
64
|
+
const aIsOpencode = a.startsWith("opencode")
|
|
65
|
+
const bIsOpencode = b.startsWith("opencode")
|
|
66
|
+
if (aIsOpencode && !bIsOpencode) return -1
|
|
67
|
+
if (!aIsOpencode && bIsOpencode) return 1
|
|
68
|
+
return a.localeCompare(b)
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
for (const providerID of providerIDs) {
|
|
72
|
+
printModels(providerID, args.verbose)
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
})
|
|
76
|
+
},
|
|
77
|
+
})
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { UI } from "../ui"
|
|
2
|
+
import { cmd } from "./cmd"
|
|
3
|
+
import { Instance } from "@/project/instance"
|
|
4
|
+
import { $ } from "bun"
|
|
5
|
+
|
|
6
|
+
export const PrCommand = cmd({
|
|
7
|
+
command: "pr <number>",
|
|
8
|
+
describe: "fetch and checkout a GitHub PR branch, then run opencode",
|
|
9
|
+
builder: (yargs) =>
|
|
10
|
+
yargs.positional("number", {
|
|
11
|
+
type: "number",
|
|
12
|
+
describe: "PR number to checkout",
|
|
13
|
+
demandOption: true,
|
|
14
|
+
}),
|
|
15
|
+
async handler(args) {
|
|
16
|
+
await Instance.provide({
|
|
17
|
+
directory: process.cwd(),
|
|
18
|
+
async fn() {
|
|
19
|
+
const project = Instance.project
|
|
20
|
+
if (project.vcs !== "git") {
|
|
21
|
+
UI.error("Could not find git repository. Please run this command from a git repository.")
|
|
22
|
+
process.exit(1)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const prNumber = args.number
|
|
26
|
+
const localBranchName = `pr/${prNumber}`
|
|
27
|
+
UI.println(`Fetching and checking out PR #${prNumber}...`)
|
|
28
|
+
|
|
29
|
+
// Use gh pr checkout with custom branch name
|
|
30
|
+
const result = await $`gh pr checkout ${prNumber} --branch ${localBranchName} --force`.nothrow()
|
|
31
|
+
|
|
32
|
+
if (result.exitCode !== 0) {
|
|
33
|
+
UI.error(`Failed to checkout PR #${prNumber}. Make sure you have gh CLI installed and authenticated.`)
|
|
34
|
+
process.exit(1)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Fetch PR info for fork handling and session link detection
|
|
38
|
+
const prInfoResult =
|
|
39
|
+
await $`gh pr view ${prNumber} --json headRepository,headRepositoryOwner,isCrossRepository,headRefName,body`.nothrow()
|
|
40
|
+
|
|
41
|
+
let sessionId: string | undefined
|
|
42
|
+
|
|
43
|
+
if (prInfoResult.exitCode === 0) {
|
|
44
|
+
const prInfoText = prInfoResult.text()
|
|
45
|
+
if (prInfoText.trim()) {
|
|
46
|
+
const prInfo = JSON.parse(prInfoText)
|
|
47
|
+
|
|
48
|
+
// Handle fork PRs
|
|
49
|
+
if (prInfo && prInfo.isCrossRepository && prInfo.headRepository && prInfo.headRepositoryOwner) {
|
|
50
|
+
const forkOwner = prInfo.headRepositoryOwner.login
|
|
51
|
+
const forkName = prInfo.headRepository.name
|
|
52
|
+
const remoteName = forkOwner
|
|
53
|
+
|
|
54
|
+
// Check if remote already exists
|
|
55
|
+
const remotes = (await $`git remote`.nothrow().text()).trim()
|
|
56
|
+
if (!remotes.split("\n").includes(remoteName)) {
|
|
57
|
+
await $`git remote add ${remoteName} https://github.com/${forkOwner}/${forkName}.git`.nothrow()
|
|
58
|
+
UI.println(`Added fork remote: ${remoteName}`)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// Set upstream to the fork so pushes go there
|
|
62
|
+
const headRefName = prInfo.headRefName
|
|
63
|
+
await $`git branch --set-upstream-to=${remoteName}/${headRefName} ${localBranchName}`.nothrow()
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Check for opencode session link in PR body
|
|
67
|
+
if (prInfo && prInfo.body) {
|
|
68
|
+
const sessionMatch = prInfo.body.match(/https:\/\/opencode\.ai\/s\/([a-zA-Z0-9_-]+)/)
|
|
69
|
+
if (sessionMatch) {
|
|
70
|
+
const sessionUrl = sessionMatch[0]
|
|
71
|
+
UI.println(`Found opencode session: ${sessionUrl}`)
|
|
72
|
+
UI.println(`Importing session...`)
|
|
73
|
+
|
|
74
|
+
const importResult = await $`opencode import ${sessionUrl}`.nothrow()
|
|
75
|
+
if (importResult.exitCode === 0) {
|
|
76
|
+
const importOutput = importResult.text().trim()
|
|
77
|
+
// Extract session ID from the output (format: "Imported session: <session-id>")
|
|
78
|
+
const sessionIdMatch = importOutput.match(/Imported session: ([a-zA-Z0-9_-]+)/)
|
|
79
|
+
if (sessionIdMatch) {
|
|
80
|
+
sessionId = sessionIdMatch[1]
|
|
81
|
+
UI.println(`Session imported: ${sessionId}`)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
UI.println(`Successfully checked out PR #${prNumber} as branch '${localBranchName}'`)
|
|
90
|
+
UI.println()
|
|
91
|
+
UI.println("Starting opencode...")
|
|
92
|
+
UI.println()
|
|
93
|
+
|
|
94
|
+
// Launch opencode TUI with session ID if available
|
|
95
|
+
const { spawn } = await import("child_process")
|
|
96
|
+
const opencodeArgs = sessionId ? ["-s", sessionId] : []
|
|
97
|
+
const opencodeProcess = spawn("opencode", opencodeArgs, {
|
|
98
|
+
stdio: "inherit",
|
|
99
|
+
cwd: process.cwd(),
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
await new Promise<void>((resolve, reject) => {
|
|
103
|
+
opencodeProcess.on("exit", (code) => {
|
|
104
|
+
if (code === 0) resolve()
|
|
105
|
+
else reject(new Error(`opencode exited with code ${code}`))
|
|
106
|
+
})
|
|
107
|
+
opencodeProcess.on("error", reject)
|
|
108
|
+
})
|
|
109
|
+
},
|
|
110
|
+
})
|
|
111
|
+
},
|
|
112
|
+
})
|