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,70 @@
|
|
|
1
|
+
import { RequestError, type McpServer } from "@agentclientprotocol/sdk"
|
|
2
|
+
import type { ACPSessionState } from "./types"
|
|
3
|
+
import { Log } from "@/util/log"
|
|
4
|
+
import type { OpencodeClient } from "@opencode-ai/sdk/v2"
|
|
5
|
+
|
|
6
|
+
const log = Log.create({ service: "acp-session-manager" })
|
|
7
|
+
|
|
8
|
+
export class ACPSessionManager {
|
|
9
|
+
private sessions = new Map<string, ACPSessionState>()
|
|
10
|
+
private sdk: OpencodeClient
|
|
11
|
+
|
|
12
|
+
constructor(sdk: OpencodeClient) {
|
|
13
|
+
this.sdk = sdk
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async create(cwd: string, mcpServers: McpServer[], model?: ACPSessionState["model"]): Promise<ACPSessionState> {
|
|
17
|
+
const session = await this.sdk.session
|
|
18
|
+
.create(
|
|
19
|
+
{
|
|
20
|
+
title: `ACP Session ${crypto.randomUUID()}`,
|
|
21
|
+
directory: cwd,
|
|
22
|
+
},
|
|
23
|
+
{ throwOnError: true },
|
|
24
|
+
)
|
|
25
|
+
.then((x) => x.data!)
|
|
26
|
+
|
|
27
|
+
const sessionId = session.id
|
|
28
|
+
const resolvedModel = model
|
|
29
|
+
|
|
30
|
+
const state: ACPSessionState = {
|
|
31
|
+
id: sessionId,
|
|
32
|
+
cwd,
|
|
33
|
+
mcpServers,
|
|
34
|
+
createdAt: new Date(),
|
|
35
|
+
model: resolvedModel,
|
|
36
|
+
}
|
|
37
|
+
log.info("creating_session", { state })
|
|
38
|
+
|
|
39
|
+
this.sessions.set(sessionId, state)
|
|
40
|
+
return state
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
get(sessionId: string): ACPSessionState {
|
|
44
|
+
const session = this.sessions.get(sessionId)
|
|
45
|
+
if (!session) {
|
|
46
|
+
log.error("session not found", { sessionId })
|
|
47
|
+
throw RequestError.invalidParams(JSON.stringify({ error: `Session not found: ${sessionId}` }))
|
|
48
|
+
}
|
|
49
|
+
return session
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getModel(sessionId: string) {
|
|
53
|
+
const session = this.get(sessionId)
|
|
54
|
+
return session.model
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setModel(sessionId: string, model: ACPSessionState["model"]) {
|
|
58
|
+
const session = this.get(sessionId)
|
|
59
|
+
session.model = model
|
|
60
|
+
this.sessions.set(sessionId, session)
|
|
61
|
+
return session
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
setMode(sessionId: string, modeId: string) {
|
|
65
|
+
const session = this.get(sessionId)
|
|
66
|
+
session.modeId = modeId
|
|
67
|
+
this.sessions.set(sessionId, session)
|
|
68
|
+
return session
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/acp/types.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { McpServer } from "@agentclientprotocol/sdk"
|
|
2
|
+
import type { OpencodeClient } from "@opencode-ai/sdk/v2"
|
|
3
|
+
|
|
4
|
+
export interface ACPSessionState {
|
|
5
|
+
id: string
|
|
6
|
+
cwd: string
|
|
7
|
+
mcpServers: McpServer[]
|
|
8
|
+
createdAt: Date
|
|
9
|
+
model?: {
|
|
10
|
+
providerID: string
|
|
11
|
+
modelID: string
|
|
12
|
+
}
|
|
13
|
+
modeId?: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface ACPConfig {
|
|
17
|
+
sdk: OpencodeClient
|
|
18
|
+
defaultModel?: {
|
|
19
|
+
providerID: string
|
|
20
|
+
modelID: string
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
import { Config } from "../config/config"
|
|
2
|
+
import z from "zod"
|
|
3
|
+
import { Provider } from "../provider/provider"
|
|
4
|
+
import { generateObject, type ModelMessage } from "ai"
|
|
5
|
+
import PROMPT_GENERATE from "./generate.txt"
|
|
6
|
+
import { SystemPrompt } from "../session/system"
|
|
7
|
+
import { Instance } from "../project/instance"
|
|
8
|
+
import { mergeDeep } from "remeda"
|
|
9
|
+
|
|
10
|
+
export namespace Agent {
|
|
11
|
+
export const Info = z
|
|
12
|
+
.object({
|
|
13
|
+
name: z.string(),
|
|
14
|
+
description: z.string().optional(),
|
|
15
|
+
mode: z.enum(["subagent", "primary", "all"]),
|
|
16
|
+
builtIn: z.boolean(),
|
|
17
|
+
topP: z.number().optional(),
|
|
18
|
+
temperature: z.number().optional(),
|
|
19
|
+
color: z.string().optional(),
|
|
20
|
+
permission: z.object({
|
|
21
|
+
edit: Config.Permission,
|
|
22
|
+
bash: z.record(z.string(), Config.Permission),
|
|
23
|
+
webfetch: Config.Permission.optional(),
|
|
24
|
+
doom_loop: Config.Permission.optional(),
|
|
25
|
+
external_directory: Config.Permission.optional(),
|
|
26
|
+
}),
|
|
27
|
+
model: z
|
|
28
|
+
.object({
|
|
29
|
+
modelID: z.string(),
|
|
30
|
+
providerID: z.string(),
|
|
31
|
+
})
|
|
32
|
+
.optional(),
|
|
33
|
+
prompt: z.string().optional(),
|
|
34
|
+
tools: z.record(z.string(), z.boolean()),
|
|
35
|
+
options: z.record(z.string(), z.any()),
|
|
36
|
+
maxSteps: z.number().int().positive().optional(),
|
|
37
|
+
})
|
|
38
|
+
.meta({
|
|
39
|
+
ref: "Agent",
|
|
40
|
+
})
|
|
41
|
+
export type Info = z.infer<typeof Info>
|
|
42
|
+
|
|
43
|
+
const state = Instance.state(async () => {
|
|
44
|
+
const cfg = await Config.get()
|
|
45
|
+
const defaultTools = cfg.tools ?? {}
|
|
46
|
+
const defaultPermission: Info["permission"] = {
|
|
47
|
+
edit: "allow",
|
|
48
|
+
bash: {
|
|
49
|
+
"*": "allow",
|
|
50
|
+
},
|
|
51
|
+
webfetch: "allow",
|
|
52
|
+
doom_loop: "ask",
|
|
53
|
+
external_directory: "ask",
|
|
54
|
+
}
|
|
55
|
+
const agentPermission = mergeAgentPermissions(defaultPermission, cfg.permission ?? {})
|
|
56
|
+
|
|
57
|
+
const planPermission = mergeAgentPermissions(
|
|
58
|
+
{
|
|
59
|
+
edit: "deny",
|
|
60
|
+
bash: {
|
|
61
|
+
"cut*": "allow",
|
|
62
|
+
"diff*": "allow",
|
|
63
|
+
"du*": "allow",
|
|
64
|
+
"file *": "allow",
|
|
65
|
+
"find * -delete*": "ask",
|
|
66
|
+
"find * -exec*": "ask",
|
|
67
|
+
"find * -fprint*": "ask",
|
|
68
|
+
"find * -fls*": "ask",
|
|
69
|
+
"find * -fprintf*": "ask",
|
|
70
|
+
"find * -ok*": "ask",
|
|
71
|
+
"find *": "allow",
|
|
72
|
+
"git diff*": "allow",
|
|
73
|
+
"git log*": "allow",
|
|
74
|
+
"git show*": "allow",
|
|
75
|
+
"git status*": "allow",
|
|
76
|
+
"git branch": "allow",
|
|
77
|
+
"git branch -v": "allow",
|
|
78
|
+
"grep*": "allow",
|
|
79
|
+
"head*": "allow",
|
|
80
|
+
"less*": "allow",
|
|
81
|
+
"ls*": "allow",
|
|
82
|
+
"more*": "allow",
|
|
83
|
+
"pwd*": "allow",
|
|
84
|
+
"rg*": "allow",
|
|
85
|
+
"sort --output=*": "ask",
|
|
86
|
+
"sort -o *": "ask",
|
|
87
|
+
"sort*": "allow",
|
|
88
|
+
"stat*": "allow",
|
|
89
|
+
"tail*": "allow",
|
|
90
|
+
"tree -o *": "ask",
|
|
91
|
+
"tree*": "allow",
|
|
92
|
+
"uniq*": "allow",
|
|
93
|
+
"wc*": "allow",
|
|
94
|
+
"whereis*": "allow",
|
|
95
|
+
"which*": "allow",
|
|
96
|
+
"*": "ask",
|
|
97
|
+
},
|
|
98
|
+
webfetch: "allow",
|
|
99
|
+
},
|
|
100
|
+
cfg.permission ?? {},
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
const result: Record<string, Info> = {
|
|
104
|
+
general: {
|
|
105
|
+
name: "general",
|
|
106
|
+
description: `General-purpose agent for researching complex questions and executing multi-step tasks. Use this agent to execute multiple units of work in parallel.`,
|
|
107
|
+
tools: {
|
|
108
|
+
todoread: false,
|
|
109
|
+
todowrite: false,
|
|
110
|
+
...defaultTools,
|
|
111
|
+
},
|
|
112
|
+
options: {},
|
|
113
|
+
permission: agentPermission,
|
|
114
|
+
mode: "subagent",
|
|
115
|
+
builtIn: true,
|
|
116
|
+
},
|
|
117
|
+
explore: {
|
|
118
|
+
name: "explore",
|
|
119
|
+
tools: {
|
|
120
|
+
todoread: false,
|
|
121
|
+
todowrite: false,
|
|
122
|
+
edit: false,
|
|
123
|
+
write: false,
|
|
124
|
+
...defaultTools,
|
|
125
|
+
},
|
|
126
|
+
description: `Fast agent specialized for exploring codebases. Use this when you need to quickly find files by patterns (eg. "src/components/**/*.tsx"), search code for keywords (eg. "API endpoints"), or answer questions about the codebase (eg. "how do API endpoints work?"). When calling this agent, specify the desired thoroughness level: "quick" for basic searches, "medium" for moderate exploration, or "very thorough" for comprehensive analysis across multiple locations and naming conventions.`,
|
|
127
|
+
prompt: [
|
|
128
|
+
`You are a file search specialist. You excel at thoroughly navigating and exploring codebases.`,
|
|
129
|
+
``,
|
|
130
|
+
`Your strengths:`,
|
|
131
|
+
`- Rapidly finding files using glob patterns`,
|
|
132
|
+
`- Searching code and text with powerful regex patterns`,
|
|
133
|
+
`- Reading and analyzing file contents`,
|
|
134
|
+
``,
|
|
135
|
+
`Guidelines:`,
|
|
136
|
+
`- Use Glob for broad file pattern matching`,
|
|
137
|
+
`- Use Grep for searching file contents with regex`,
|
|
138
|
+
`- Use Read when you know the specific file path you need to read`,
|
|
139
|
+
`- Use Bash for file operations like copying, moving, or listing directory contents`,
|
|
140
|
+
`- Adapt your search approach based on the thoroughness level specified by the caller`,
|
|
141
|
+
`- Return file paths as absolute paths in your final response`,
|
|
142
|
+
`- For clear communication, avoid using emojis`,
|
|
143
|
+
`- Do not create any files, or run bash commands that modify the user's system state in any way`,
|
|
144
|
+
``,
|
|
145
|
+
`Complete the user's search request efficiently and report your findings clearly.`,
|
|
146
|
+
].join("\n"),
|
|
147
|
+
options: {},
|
|
148
|
+
permission: agentPermission,
|
|
149
|
+
mode: "subagent",
|
|
150
|
+
builtIn: true,
|
|
151
|
+
},
|
|
152
|
+
build: {
|
|
153
|
+
name: "build",
|
|
154
|
+
tools: { ...defaultTools },
|
|
155
|
+
options: {},
|
|
156
|
+
permission: agentPermission,
|
|
157
|
+
mode: "primary",
|
|
158
|
+
builtIn: true,
|
|
159
|
+
},
|
|
160
|
+
plan: {
|
|
161
|
+
name: "plan",
|
|
162
|
+
options: {},
|
|
163
|
+
permission: planPermission,
|
|
164
|
+
tools: {
|
|
165
|
+
...defaultTools,
|
|
166
|
+
},
|
|
167
|
+
mode: "primary",
|
|
168
|
+
builtIn: true,
|
|
169
|
+
},
|
|
170
|
+
}
|
|
171
|
+
for (const [key, value] of Object.entries(cfg.agent ?? {})) {
|
|
172
|
+
if (value.disable) {
|
|
173
|
+
delete result[key]
|
|
174
|
+
continue
|
|
175
|
+
}
|
|
176
|
+
let item = result[key]
|
|
177
|
+
if (!item)
|
|
178
|
+
item = result[key] = {
|
|
179
|
+
name: key,
|
|
180
|
+
mode: "all",
|
|
181
|
+
permission: agentPermission,
|
|
182
|
+
options: {},
|
|
183
|
+
tools: {},
|
|
184
|
+
builtIn: false,
|
|
185
|
+
}
|
|
186
|
+
const {
|
|
187
|
+
name,
|
|
188
|
+
model,
|
|
189
|
+
prompt,
|
|
190
|
+
tools,
|
|
191
|
+
description,
|
|
192
|
+
temperature,
|
|
193
|
+
top_p,
|
|
194
|
+
mode,
|
|
195
|
+
permission,
|
|
196
|
+
color,
|
|
197
|
+
maxSteps,
|
|
198
|
+
...extra
|
|
199
|
+
} = value
|
|
200
|
+
item.options = {
|
|
201
|
+
...item.options,
|
|
202
|
+
...extra,
|
|
203
|
+
}
|
|
204
|
+
if (model) item.model = Provider.parseModel(model)
|
|
205
|
+
if (prompt) item.prompt = prompt
|
|
206
|
+
if (tools)
|
|
207
|
+
item.tools = {
|
|
208
|
+
...item.tools,
|
|
209
|
+
...tools,
|
|
210
|
+
}
|
|
211
|
+
item.tools = {
|
|
212
|
+
...defaultTools,
|
|
213
|
+
...item.tools,
|
|
214
|
+
}
|
|
215
|
+
if (description) item.description = description
|
|
216
|
+
if (temperature != undefined) item.temperature = temperature
|
|
217
|
+
if (top_p != undefined) item.topP = top_p
|
|
218
|
+
if (mode) item.mode = mode
|
|
219
|
+
if (color) item.color = color
|
|
220
|
+
// just here for consistency & to prevent it from being added as an option
|
|
221
|
+
if (name) item.name = name
|
|
222
|
+
if (maxSteps != undefined) item.maxSteps = maxSteps
|
|
223
|
+
|
|
224
|
+
if (permission ?? cfg.permission) {
|
|
225
|
+
item.permission = mergeAgentPermissions(cfg.permission ?? {}, permission ?? {})
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
return result
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
export async function get(agent: string) {
|
|
232
|
+
return state().then((x) => x[agent])
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function list() {
|
|
236
|
+
return state().then((x) => Object.values(x))
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
export async function generate(input: { description: string }) {
|
|
240
|
+
const cfg = await Config.get()
|
|
241
|
+
const defaultModel = await Provider.defaultModel()
|
|
242
|
+
const model = await Provider.getModel(defaultModel.providerID, defaultModel.modelID)
|
|
243
|
+
const language = await Provider.getLanguage(model)
|
|
244
|
+
const system = SystemPrompt.header(defaultModel.providerID)
|
|
245
|
+
system.push(PROMPT_GENERATE)
|
|
246
|
+
const existing = await list()
|
|
247
|
+
const result = await generateObject({
|
|
248
|
+
experimental_telemetry: { isEnabled: cfg.experimental?.openTelemetry },
|
|
249
|
+
temperature: 0.3,
|
|
250
|
+
prompt: [
|
|
251
|
+
...system.map(
|
|
252
|
+
(item): ModelMessage => ({
|
|
253
|
+
role: "system",
|
|
254
|
+
content: item,
|
|
255
|
+
}),
|
|
256
|
+
),
|
|
257
|
+
{
|
|
258
|
+
role: "user",
|
|
259
|
+
content: `Create an agent configuration based on this request: \"${input.description}\".\n\nIMPORTANT: The following identifiers already exist and must NOT be used: ${existing.map((i) => i.name).join(", ")}\n Return ONLY the JSON object, no other text, do not wrap in backticks`,
|
|
260
|
+
},
|
|
261
|
+
],
|
|
262
|
+
model: language,
|
|
263
|
+
schema: z.object({
|
|
264
|
+
identifier: z.string(),
|
|
265
|
+
whenToUse: z.string(),
|
|
266
|
+
systemPrompt: z.string(),
|
|
267
|
+
}),
|
|
268
|
+
})
|
|
269
|
+
return result.object
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
function mergeAgentPermissions(basePermission: any, overridePermission: any): Agent.Info["permission"] {
|
|
274
|
+
if (typeof basePermission.bash === "string") {
|
|
275
|
+
basePermission.bash = {
|
|
276
|
+
"*": basePermission.bash,
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (typeof overridePermission.bash === "string") {
|
|
280
|
+
overridePermission.bash = {
|
|
281
|
+
"*": overridePermission.bash,
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
const merged = mergeDeep(basePermission ?? {}, overridePermission ?? {}) as any
|
|
285
|
+
let mergedBash
|
|
286
|
+
if (merged.bash) {
|
|
287
|
+
if (typeof merged.bash === "string") {
|
|
288
|
+
mergedBash = {
|
|
289
|
+
"*": merged.bash,
|
|
290
|
+
}
|
|
291
|
+
} else if (typeof merged.bash === "object") {
|
|
292
|
+
mergedBash = mergeDeep(
|
|
293
|
+
{
|
|
294
|
+
"*": "allow",
|
|
295
|
+
},
|
|
296
|
+
merged.bash,
|
|
297
|
+
)
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const result: Agent.Info["permission"] = {
|
|
302
|
+
edit: merged.edit ?? "allow",
|
|
303
|
+
webfetch: merged.webfetch ?? "allow",
|
|
304
|
+
bash: mergedBash ?? { "*": "allow" },
|
|
305
|
+
doom_loop: merged.doom_loop,
|
|
306
|
+
external_directory: merged.external_directory,
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
return result
|
|
310
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
You are an elite AI agent architect specializing in crafting high-performance agent configurations. Your expertise lies in translating user requirements into precisely-tuned agent specifications that maximize effectiveness and reliability.
|
|
2
|
+
|
|
3
|
+
**Important Context**: You may have access to project-specific instructions from CLAUDE.md files and other context that may include coding standards, project structure, and custom requirements. Consider this context when creating agents to ensure they align with the project's established patterns and practices.
|
|
4
|
+
|
|
5
|
+
When a user describes what they want an agent to do, you will:
|
|
6
|
+
|
|
7
|
+
1. **Extract Core Intent**: Identify the fundamental purpose, key responsibilities, and success criteria for the agent. Look for both explicit requirements and implicit needs. Consider any project-specific context from CLAUDE.md files. For agents that are meant to review code, you should assume that the user is asking to review recently written code and not the whole codebase, unless the user has explicitly instructed you otherwise.
|
|
8
|
+
|
|
9
|
+
2. **Design Expert Persona**: Create a compelling expert identity that embodies deep domain knowledge relevant to the task. The persona should inspire confidence and guide the agent's decision-making approach.
|
|
10
|
+
|
|
11
|
+
3. **Architect Comprehensive Instructions**: Develop a system prompt that:
|
|
12
|
+
|
|
13
|
+
- Establishes clear behavioral boundaries and operational parameters
|
|
14
|
+
- Provides specific methodologies and best practices for task execution
|
|
15
|
+
- Anticipates edge cases and provides guidance for handling them
|
|
16
|
+
- Incorporates any specific requirements or preferences mentioned by the user
|
|
17
|
+
- Defines output format expectations when relevant
|
|
18
|
+
- Aligns with project-specific coding standards and patterns from CLAUDE.md
|
|
19
|
+
|
|
20
|
+
4. **Optimize for Performance**: Include:
|
|
21
|
+
|
|
22
|
+
- Decision-making frameworks appropriate to the domain
|
|
23
|
+
- Quality control mechanisms and self-verification steps
|
|
24
|
+
- Efficient workflow patterns
|
|
25
|
+
- Clear escalation or fallback strategies
|
|
26
|
+
|
|
27
|
+
5. **Create Identifier**: Design a concise, descriptive identifier that:
|
|
28
|
+
- Uses lowercase letters, numbers, and hyphens only
|
|
29
|
+
- Is typically 2-4 words joined by hyphens
|
|
30
|
+
- Clearly indicates the agent's primary function
|
|
31
|
+
- Is memorable and easy to type
|
|
32
|
+
- Avoids generic terms like "helper" or "assistant"
|
|
33
|
+
|
|
34
|
+
6 **Example agent descriptions**:
|
|
35
|
+
|
|
36
|
+
- in the 'whenToUse' field of the JSON object, you should include examples of when this agent should be used.
|
|
37
|
+
- examples should be of the form:
|
|
38
|
+
- <example>
|
|
39
|
+
Context: The user is creating a code-review agent that should be called after a logical chunk of code is written.
|
|
40
|
+
user: "Please write a function that checks if a number is prime"
|
|
41
|
+
assistant: "Here is the relevant function: "
|
|
42
|
+
<function call omitted for brevity only for this example>
|
|
43
|
+
<commentary>
|
|
44
|
+
Since the user is greeting, use the Task tool to launch the greeting-responder agent to respond with a friendly joke.
|
|
45
|
+
</commentary>
|
|
46
|
+
assistant: "Now let me use the code-reviewer agent to review the code"
|
|
47
|
+
</example>
|
|
48
|
+
- <example>
|
|
49
|
+
Context: User is creating an agent to respond to the word "hello" with a friendly jok.
|
|
50
|
+
user: "Hello"
|
|
51
|
+
assistant: "I'm going to use the Task tool to launch the greeting-responder agent to respond with a friendly joke"
|
|
52
|
+
<commentary>
|
|
53
|
+
Since the user is greeting, use the greeting-responder agent to respond with a friendly joke.
|
|
54
|
+
</commentary>
|
|
55
|
+
</example>
|
|
56
|
+
- If the user mentioned or implied that the agent should be used proactively, you should include examples of this.
|
|
57
|
+
- NOTE: Ensure that in the examples, you are making the assistant use the Agent tool and not simply respond directly to the task.
|
|
58
|
+
|
|
59
|
+
Your output must be a valid JSON object with exactly these fields:
|
|
60
|
+
{
|
|
61
|
+
"identifier": "A unique, descriptive identifier using lowercase letters, numbers, and hyphens (e.g., 'code-reviewer', 'api-docs-writer', 'test-generator')",
|
|
62
|
+
"whenToUse": "A precise, actionable description starting with 'Use this agent when...' that clearly defines the triggering conditions and use cases. Ensure you include examples as described above.",
|
|
63
|
+
"systemPrompt": "The complete system prompt that will govern the agent's behavior, written in second person ('You are...', 'You will...') and structured for maximum clarity and effectiveness"
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
Key principles for your system prompts:
|
|
67
|
+
|
|
68
|
+
- Be specific rather than generic - avoid vague instructions
|
|
69
|
+
- Include concrete examples when they would clarify behavior
|
|
70
|
+
- Balance comprehensiveness with clarity - every instruction should add value
|
|
71
|
+
- Ensure the agent has enough context to handle variations of the core task
|
|
72
|
+
- Make the agent proactive in seeking clarification when needed
|
|
73
|
+
- Build in quality assurance and self-correction mechanisms
|
|
74
|
+
|
|
75
|
+
Remember: The agents you create should be autonomous experts capable of handling their designated tasks with minimal additional guidance. Your system prompts are their complete operational manual.
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import path from "path"
|
|
2
|
+
import { Global } from "../global"
|
|
3
|
+
import fs from "fs/promises"
|
|
4
|
+
import z from "zod"
|
|
5
|
+
|
|
6
|
+
export namespace Auth {
|
|
7
|
+
export const Oauth = z
|
|
8
|
+
.object({
|
|
9
|
+
type: z.literal("oauth"),
|
|
10
|
+
refresh: z.string(),
|
|
11
|
+
access: z.string(),
|
|
12
|
+
expires: z.number(),
|
|
13
|
+
enterpriseUrl: z.string().optional(),
|
|
14
|
+
})
|
|
15
|
+
.meta({ ref: "OAuth" })
|
|
16
|
+
|
|
17
|
+
export const Api = z
|
|
18
|
+
.object({
|
|
19
|
+
type: z.literal("api"),
|
|
20
|
+
key: z.string(),
|
|
21
|
+
})
|
|
22
|
+
.meta({ ref: "ApiAuth" })
|
|
23
|
+
|
|
24
|
+
export const WellKnown = z
|
|
25
|
+
.object({
|
|
26
|
+
type: z.literal("wellknown"),
|
|
27
|
+
key: z.string(),
|
|
28
|
+
token: z.string(),
|
|
29
|
+
})
|
|
30
|
+
.meta({ ref: "WellKnownAuth" })
|
|
31
|
+
|
|
32
|
+
export const Info = z.discriminatedUnion("type", [Oauth, Api, WellKnown]).meta({ ref: "Auth" })
|
|
33
|
+
export type Info = z.infer<typeof Info>
|
|
34
|
+
|
|
35
|
+
const filepath = path.join(Global.Path.data, "auth.json")
|
|
36
|
+
|
|
37
|
+
export async function get(providerID: string) {
|
|
38
|
+
const auth = await all()
|
|
39
|
+
return auth[providerID]
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export async function all(): Promise<Record<string, Info>> {
|
|
43
|
+
const file = Bun.file(filepath)
|
|
44
|
+
const data = await file.json().catch(() => ({}) as Record<string, unknown>)
|
|
45
|
+
return Object.entries(data).reduce(
|
|
46
|
+
(acc, [key, value]) => {
|
|
47
|
+
const parsed = Info.safeParse(value)
|
|
48
|
+
if (!parsed.success) return acc
|
|
49
|
+
acc[key] = parsed.data
|
|
50
|
+
return acc
|
|
51
|
+
},
|
|
52
|
+
{} as Record<string, Info>,
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function set(key: string, info: Info) {
|
|
57
|
+
const file = Bun.file(filepath)
|
|
58
|
+
const data = await all()
|
|
59
|
+
await Bun.write(file, JSON.stringify({ ...data, [key]: info }, null, 2))
|
|
60
|
+
await fs.chmod(file.name!, 0o600)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export async function remove(key: string) {
|
|
64
|
+
const file = Bun.file(filepath)
|
|
65
|
+
const data = await all()
|
|
66
|
+
delete data[key]
|
|
67
|
+
await Bun.write(file, JSON.stringify(data, null, 2))
|
|
68
|
+
await fs.chmod(file.name!, 0o600)
|
|
69
|
+
}
|
|
70
|
+
}
|
package/src/bun/index.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import z from "zod"
|
|
2
|
+
import { Global } from "../global"
|
|
3
|
+
import { Log } from "../util/log"
|
|
4
|
+
import path from "path"
|
|
5
|
+
import { NamedError } from "@opencode-ai/util/error"
|
|
6
|
+
import { readableStreamToText } from "bun"
|
|
7
|
+
import { createRequire } from "module"
|
|
8
|
+
import { Lock } from "../util/lock"
|
|
9
|
+
|
|
10
|
+
export namespace BunProc {
|
|
11
|
+
const log = Log.create({ service: "bun" })
|
|
12
|
+
const req = createRequire(import.meta.url)
|
|
13
|
+
|
|
14
|
+
export async function run(cmd: string[], options?: Bun.SpawnOptions.OptionsObject<any, any, any>) {
|
|
15
|
+
log.info("running", {
|
|
16
|
+
cmd: [which(), ...cmd],
|
|
17
|
+
...options,
|
|
18
|
+
})
|
|
19
|
+
const result = Bun.spawn([which(), ...cmd], {
|
|
20
|
+
...options,
|
|
21
|
+
stdout: "pipe",
|
|
22
|
+
stderr: "pipe",
|
|
23
|
+
env: {
|
|
24
|
+
...process.env,
|
|
25
|
+
...options?.env,
|
|
26
|
+
BUN_BE_BUN: "1",
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
const code = await result.exited
|
|
30
|
+
const stdout = result.stdout
|
|
31
|
+
? typeof result.stdout === "number"
|
|
32
|
+
? result.stdout
|
|
33
|
+
: await readableStreamToText(result.stdout)
|
|
34
|
+
: undefined
|
|
35
|
+
const stderr = result.stderr
|
|
36
|
+
? typeof result.stderr === "number"
|
|
37
|
+
? result.stderr
|
|
38
|
+
: await readableStreamToText(result.stderr)
|
|
39
|
+
: undefined
|
|
40
|
+
log.info("done", {
|
|
41
|
+
code,
|
|
42
|
+
stdout,
|
|
43
|
+
stderr,
|
|
44
|
+
})
|
|
45
|
+
if (code !== 0) {
|
|
46
|
+
throw new Error(`Command failed with exit code ${result.exitCode}`)
|
|
47
|
+
}
|
|
48
|
+
return result
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function which() {
|
|
52
|
+
return process.execPath
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const InstallFailedError = NamedError.create(
|
|
56
|
+
"BunInstallFailedError",
|
|
57
|
+
z.object({
|
|
58
|
+
pkg: z.string(),
|
|
59
|
+
version: z.string(),
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
export async function install(pkg: string, version = "latest") {
|
|
64
|
+
// Use lock to ensure only one install at a time
|
|
65
|
+
using _ = await Lock.write("bun-install")
|
|
66
|
+
|
|
67
|
+
const mod = path.join(Global.Path.cache, "node_modules", pkg)
|
|
68
|
+
const pkgjson = Bun.file(path.join(Global.Path.cache, "package.json"))
|
|
69
|
+
const parsed = await pkgjson.json().catch(async () => {
|
|
70
|
+
const result = { dependencies: {} }
|
|
71
|
+
await Bun.write(pkgjson.name!, JSON.stringify(result, null, 2))
|
|
72
|
+
return result
|
|
73
|
+
})
|
|
74
|
+
if (parsed.dependencies[pkg] === version) return mod
|
|
75
|
+
|
|
76
|
+
// Build command arguments
|
|
77
|
+
const args = ["add", "--force", "--exact", "--cwd", Global.Path.cache, pkg + "@" + version]
|
|
78
|
+
|
|
79
|
+
// Let Bun handle registry resolution:
|
|
80
|
+
// - If .npmrc files exist, Bun will use them automatically
|
|
81
|
+
// - If no .npmrc files exist, Bun will default to https://registry.npmjs.org
|
|
82
|
+
// - No need to pass --registry flag
|
|
83
|
+
log.info("installing package using Bun's default registry resolution", {
|
|
84
|
+
pkg,
|
|
85
|
+
version,
|
|
86
|
+
})
|
|
87
|
+
|
|
88
|
+
const total = 3
|
|
89
|
+
const wait = 500
|
|
90
|
+
|
|
91
|
+
const runInstall = async (count: number = 1): Promise<void> => {
|
|
92
|
+
log.info("bun install attempt", {
|
|
93
|
+
pkg,
|
|
94
|
+
version,
|
|
95
|
+
attempt: count,
|
|
96
|
+
total,
|
|
97
|
+
})
|
|
98
|
+
await BunProc.run(args, {
|
|
99
|
+
cwd: Global.Path.cache,
|
|
100
|
+
}).catch(async (error) => {
|
|
101
|
+
log.warn("bun install failed", {
|
|
102
|
+
pkg,
|
|
103
|
+
version,
|
|
104
|
+
attempt: count,
|
|
105
|
+
total,
|
|
106
|
+
error,
|
|
107
|
+
})
|
|
108
|
+
if (count >= total) {
|
|
109
|
+
throw new InstallFailedError(
|
|
110
|
+
{ pkg, version },
|
|
111
|
+
{
|
|
112
|
+
cause: error,
|
|
113
|
+
},
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
const delay = wait * count
|
|
117
|
+
log.info("bun install retrying", {
|
|
118
|
+
pkg,
|
|
119
|
+
version,
|
|
120
|
+
next: count + 1,
|
|
121
|
+
delay,
|
|
122
|
+
})
|
|
123
|
+
await Bun.sleep(delay)
|
|
124
|
+
return runInstall(count + 1)
|
|
125
|
+
})
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
await runInstall()
|
|
129
|
+
|
|
130
|
+
parsed.dependencies[pkg] = version
|
|
131
|
+
await Bun.write(pkgjson.name!, JSON.stringify(parsed, null, 2))
|
|
132
|
+
return mod
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export async function resolve(pkg: string) {
|
|
136
|
+
const local = workspace(pkg)
|
|
137
|
+
if (local) return local
|
|
138
|
+
const dir = path.join(Global.Path.cache, "node_modules", pkg)
|
|
139
|
+
const pkgjson = Bun.file(path.join(dir, "package.json"))
|
|
140
|
+
const exists = await pkgjson.exists()
|
|
141
|
+
if (exists) return dir
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function workspace(pkg: string) {
|
|
145
|
+
try {
|
|
146
|
+
const target = req.resolve(`${pkg}/package.json`)
|
|
147
|
+
return path.dirname(target)
|
|
148
|
+
} catch {
|
|
149
|
+
return
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|