nuwaxcode 1.1.34 → 1.1.45
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/bin/nuwaxcode +19 -1
- package/package.json +15 -117
- package/{script/postinstall.mjs → postinstall.mjs} +18 -6
- package/AGENTS.md +0 -27
- package/Dockerfile +0 -18
- package/README.md +0 -15
- package/bunfig.toml +0 -7
- package/parsers-config.ts +0 -253
- package/script/build.ts +0 -172
- package/script/publish-registries.ts +0 -187
- package/script/publish.ts +0 -70
- package/script/schema.ts +0 -47
- package/src/acp/README.md +0 -164
- package/src/acp/agent.ts +0 -1280
- package/src/acp/session.ts +0 -111
- package/src/acp/types.ts +0 -24
- package/src/agent/agent.ts +0 -332
- package/src/agent/generate.txt +0 -75
- package/src/agent/prompt/compaction.txt +0 -12
- package/src/agent/prompt/explore.txt +0 -18
- package/src/agent/prompt/summary.txt +0 -11
- package/src/agent/prompt/title.txt +0 -43
- package/src/auth/index.ts +0 -73
- package/src/bun/index.ts +0 -134
- package/src/bus/bus-event.ts +0 -43
- package/src/bus/global.ts +0 -10
- package/src/bus/index.ts +0 -105
- package/src/cli/bootstrap.ts +0 -17
- package/src/cli/cmd/acp.ts +0 -69
- package/src/cli/cmd/agent.ts +0 -257
- package/src/cli/cmd/auth.ts +0 -400
- package/src/cli/cmd/cmd.ts +0 -7
- package/src/cli/cmd/debug/agent.ts +0 -166
- package/src/cli/cmd/debug/config.ts +0 -16
- package/src/cli/cmd/debug/file.ts +0 -97
- package/src/cli/cmd/debug/index.ts +0 -48
- package/src/cli/cmd/debug/lsp.ts +0 -52
- package/src/cli/cmd/debug/ripgrep.ts +0 -87
- package/src/cli/cmd/debug/scrap.ts +0 -16
- package/src/cli/cmd/debug/skill.ts +0 -16
- package/src/cli/cmd/debug/snapshot.ts +0 -52
- package/src/cli/cmd/export.ts +0 -88
- package/src/cli/cmd/generate.ts +0 -38
- package/src/cli/cmd/github.ts +0 -1548
- package/src/cli/cmd/import.ts +0 -98
- package/src/cli/cmd/mcp.ts +0 -755
- package/src/cli/cmd/models.ts +0 -77
- package/src/cli/cmd/pr.ts +0 -112
- package/src/cli/cmd/run.ts +0 -395
- package/src/cli/cmd/serve.ts +0 -20
- package/src/cli/cmd/session.ts +0 -135
- package/src/cli/cmd/stats.ts +0 -402
- package/src/cli/cmd/tui/app.tsx +0 -761
- package/src/cli/cmd/tui/attach.ts +0 -31
- package/src/cli/cmd/tui/component/border.tsx +0 -21
- package/src/cli/cmd/tui/component/dialog-agent.tsx +0 -31
- package/src/cli/cmd/tui/component/dialog-command.tsx +0 -148
- package/src/cli/cmd/tui/component/dialog-mcp.tsx +0 -86
- package/src/cli/cmd/tui/component/dialog-model.tsx +0 -234
- package/src/cli/cmd/tui/component/dialog-provider.tsx +0 -256
- package/src/cli/cmd/tui/component/dialog-session-list.tsx +0 -114
- package/src/cli/cmd/tui/component/dialog-session-rename.tsx +0 -31
- package/src/cli/cmd/tui/component/dialog-stash.tsx +0 -87
- package/src/cli/cmd/tui/component/dialog-status.tsx +0 -164
- package/src/cli/cmd/tui/component/dialog-tag.tsx +0 -44
- package/src/cli/cmd/tui/component/dialog-theme-list.tsx +0 -50
- package/src/cli/cmd/tui/component/logo.tsx +0 -88
- package/src/cli/cmd/tui/component/prompt/autocomplete.tsx +0 -632
- package/src/cli/cmd/tui/component/prompt/frecency.tsx +0 -89
- package/src/cli/cmd/tui/component/prompt/history.tsx +0 -108
- package/src/cli/cmd/tui/component/prompt/index.tsx +0 -1096
- package/src/cli/cmd/tui/component/prompt/stash.tsx +0 -101
- package/src/cli/cmd/tui/component/textarea-keybindings.ts +0 -73
- package/src/cli/cmd/tui/component/tips.tsx +0 -153
- package/src/cli/cmd/tui/component/todo-item.tsx +0 -32
- package/src/cli/cmd/tui/context/args.tsx +0 -14
- package/src/cli/cmd/tui/context/directory.ts +0 -13
- package/src/cli/cmd/tui/context/exit.tsx +0 -23
- package/src/cli/cmd/tui/context/helper.tsx +0 -25
- package/src/cli/cmd/tui/context/keybind.tsx +0 -101
- package/src/cli/cmd/tui/context/kv.tsx +0 -52
- package/src/cli/cmd/tui/context/local.tsx +0 -402
- package/src/cli/cmd/tui/context/prompt.tsx +0 -18
- package/src/cli/cmd/tui/context/route.tsx +0 -46
- package/src/cli/cmd/tui/context/sdk.tsx +0 -94
- package/src/cli/cmd/tui/context/sync.tsx +0 -427
- package/src/cli/cmd/tui/context/theme/aura.json +0 -69
- package/src/cli/cmd/tui/context/theme/ayu.json +0 -80
- package/src/cli/cmd/tui/context/theme/carbonfox.json +0 -248
- package/src/cli/cmd/tui/context/theme/catppuccin-frappe.json +0 -233
- package/src/cli/cmd/tui/context/theme/catppuccin-macchiato.json +0 -233
- package/src/cli/cmd/tui/context/theme/catppuccin.json +0 -112
- package/src/cli/cmd/tui/context/theme/cobalt2.json +0 -228
- package/src/cli/cmd/tui/context/theme/cursor.json +0 -249
- package/src/cli/cmd/tui/context/theme/dracula.json +0 -219
- package/src/cli/cmd/tui/context/theme/everforest.json +0 -241
- package/src/cli/cmd/tui/context/theme/flexoki.json +0 -237
- package/src/cli/cmd/tui/context/theme/github.json +0 -233
- package/src/cli/cmd/tui/context/theme/gruvbox.json +0 -95
- package/src/cli/cmd/tui/context/theme/kanagawa.json +0 -77
- package/src/cli/cmd/tui/context/theme/lucent-orng.json +0 -237
- package/src/cli/cmd/tui/context/theme/material.json +0 -235
- package/src/cli/cmd/tui/context/theme/matrix.json +0 -77
- package/src/cli/cmd/tui/context/theme/mercury.json +0 -252
- package/src/cli/cmd/tui/context/theme/monokai.json +0 -221
- package/src/cli/cmd/tui/context/theme/nightowl.json +0 -221
- package/src/cli/cmd/tui/context/theme/nord.json +0 -223
- package/src/cli/cmd/tui/context/theme/one-dark.json +0 -84
- package/src/cli/cmd/tui/context/theme/orng.json +0 -249
- package/src/cli/cmd/tui/context/theme/osaka-jade.json +0 -93
- package/src/cli/cmd/tui/context/theme/palenight.json +0 -222
- package/src/cli/cmd/tui/context/theme/rosepine.json +0 -234
- package/src/cli/cmd/tui/context/theme/solarized.json +0 -223
- package/src/cli/cmd/tui/context/theme/synthwave84.json +0 -226
- package/src/cli/cmd/tui/context/theme/tokyonight.json +0 -243
- package/src/cli/cmd/tui/context/theme/vercel.json +0 -245
- package/src/cli/cmd/tui/context/theme/vesper.json +0 -218
- package/src/cli/cmd/tui/context/theme/zenburn.json +0 -223
- package/src/cli/cmd/tui/context/theme.tsx +0 -1152
- package/src/cli/cmd/tui/event.ts +0 -48
- package/src/cli/cmd/tui/routes/home.tsx +0 -140
- package/src/cli/cmd/tui/routes/session/dialog-fork-from-timeline.tsx +0 -64
- package/src/cli/cmd/tui/routes/session/dialog-message.tsx +0 -109
- package/src/cli/cmd/tui/routes/session/dialog-subagent.tsx +0 -26
- package/src/cli/cmd/tui/routes/session/dialog-timeline.tsx +0 -47
- package/src/cli/cmd/tui/routes/session/footer.tsx +0 -91
- package/src/cli/cmd/tui/routes/session/header.tsx +0 -136
- package/src/cli/cmd/tui/routes/session/index.tsx +0 -2050
- package/src/cli/cmd/tui/routes/session/permission.tsx +0 -495
- package/src/cli/cmd/tui/routes/session/question.tsx +0 -435
- package/src/cli/cmd/tui/routes/session/sidebar.tsx +0 -312
- package/src/cli/cmd/tui/thread.ts +0 -165
- package/src/cli/cmd/tui/ui/dialog-alert.tsx +0 -57
- package/src/cli/cmd/tui/ui/dialog-confirm.tsx +0 -83
- package/src/cli/cmd/tui/ui/dialog-export-options.tsx +0 -204
- package/src/cli/cmd/tui/ui/dialog-help.tsx +0 -38
- package/src/cli/cmd/tui/ui/dialog-prompt.tsx +0 -77
- package/src/cli/cmd/tui/ui/dialog-select.tsx +0 -354
- package/src/cli/cmd/tui/ui/dialog.tsx +0 -167
- package/src/cli/cmd/tui/ui/link.tsx +0 -28
- package/src/cli/cmd/tui/ui/spinner.ts +0 -368
- package/src/cli/cmd/tui/ui/toast.tsx +0 -100
- package/src/cli/cmd/tui/util/clipboard.ts +0 -160
- package/src/cli/cmd/tui/util/editor.ts +0 -32
- package/src/cli/cmd/tui/util/signal.ts +0 -7
- package/src/cli/cmd/tui/util/terminal.ts +0 -114
- package/src/cli/cmd/tui/util/transcript.ts +0 -98
- package/src/cli/cmd/tui/worker.ts +0 -152
- package/src/cli/cmd/uninstall.ts +0 -357
- package/src/cli/cmd/upgrade.ts +0 -73
- package/src/cli/cmd/web.ts +0 -81
- package/src/cli/error.ts +0 -57
- package/src/cli/network.ts +0 -53
- package/src/cli/ui.ts +0 -84
- package/src/cli/upgrade.ts +0 -25
- package/src/command/index.ts +0 -131
- package/src/command/template/initialize.txt +0 -10
- package/src/command/template/review.txt +0 -99
- package/src/config/config.ts +0 -1255
- package/src/config/markdown.ts +0 -93
- package/src/env/index.ts +0 -26
- package/src/file/ignore.ts +0 -83
- package/src/file/index.ts +0 -411
- package/src/file/ripgrep.ts +0 -409
- package/src/file/time.ts +0 -64
- package/src/file/watcher.ts +0 -118
- package/src/flag/flag.ts +0 -54
- package/src/format/formatter.ts +0 -359
- package/src/format/index.ts +0 -137
- package/src/global/index.ts +0 -55
- package/src/id/id.ts +0 -83
- package/src/ide/index.ts +0 -76
- package/src/index.ts +0 -159
- package/src/installation/index.ts +0 -246
- package/src/lsp/client.ts +0 -252
- package/src/lsp/index.ts +0 -485
- package/src/lsp/language.ts +0 -119
- package/src/lsp/server.ts +0 -2046
- package/src/mcp/auth.ts +0 -135
- package/src/mcp/index.ts +0 -926
- package/src/mcp/oauth-callback.ts +0 -200
- package/src/mcp/oauth-provider.ts +0 -154
- package/src/patch/index.ts +0 -680
- package/src/permission/arity.ts +0 -163
- package/src/permission/index.ts +0 -210
- package/src/permission/next.ts +0 -269
- package/src/plugin/codex.ts +0 -493
- package/src/plugin/copilot.ts +0 -269
- package/src/plugin/index.ts +0 -135
- package/src/project/bootstrap.ts +0 -35
- package/src/project/instance.ts +0 -91
- package/src/project/project.ts +0 -320
- package/src/project/state.ts +0 -66
- package/src/project/vcs.ts +0 -76
- package/src/provider/auth.ts +0 -147
- package/src/provider/models-macro.ts +0 -11
- package/src/provider/models.ts +0 -112
- package/src/provider/provider.ts +0 -1219
- package/src/provider/sdk/openai-compatible/src/README.md +0 -5
- package/src/provider/sdk/openai-compatible/src/index.ts +0 -2
- package/src/provider/sdk/openai-compatible/src/openai-compatible-provider.ts +0 -100
- package/src/provider/sdk/openai-compatible/src/responses/convert-to-openai-responses-input.ts +0 -303
- package/src/provider/sdk/openai-compatible/src/responses/map-openai-responses-finish-reason.ts +0 -22
- package/src/provider/sdk/openai-compatible/src/responses/openai-config.ts +0 -18
- package/src/provider/sdk/openai-compatible/src/responses/openai-error.ts +0 -22
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-api-types.ts +0 -207
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-language-model.ts +0 -1732
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-prepare-tools.ts +0 -177
- package/src/provider/sdk/openai-compatible/src/responses/openai-responses-settings.ts +0 -1
- package/src/provider/sdk/openai-compatible/src/responses/tool/code-interpreter.ts +0 -88
- package/src/provider/sdk/openai-compatible/src/responses/tool/file-search.ts +0 -128
- package/src/provider/sdk/openai-compatible/src/responses/tool/image-generation.ts +0 -115
- package/src/provider/sdk/openai-compatible/src/responses/tool/local-shell.ts +0 -65
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search-preview.ts +0 -104
- package/src/provider/sdk/openai-compatible/src/responses/tool/web-search.ts +0 -103
- package/src/provider/transform.ts +0 -724
- package/src/pty/index.ts +0 -229
- package/src/question/index.ts +0 -171
- package/src/scheduler/index.ts +0 -61
- package/src/server/error.ts +0 -36
- package/src/server/mdns.ts +0 -59
- package/src/server/routes/config.ts +0 -92
- package/src/server/routes/experimental.ts +0 -157
- package/src/server/routes/file.ts +0 -197
- package/src/server/routes/global.ts +0 -135
- package/src/server/routes/mcp.ts +0 -225
- package/src/server/routes/permission.ts +0 -68
- package/src/server/routes/project.ts +0 -82
- package/src/server/routes/provider.ts +0 -165
- package/src/server/routes/pty.ts +0 -169
- package/src/server/routes/question.ts +0 -98
- package/src/server/routes/session.ts +0 -935
- package/src/server/routes/tui.ts +0 -379
- package/src/server/server.ts +0 -578
- package/src/session/compaction.ts +0 -225
- package/src/session/index.ts +0 -488
- package/src/session/llm.ts +0 -279
- package/src/session/message-v2.ts +0 -702
- package/src/session/message.ts +0 -189
- package/src/session/processor.ts +0 -406
- package/src/session/prompt/anthropic-20250930.txt +0 -166
- package/src/session/prompt/anthropic.txt +0 -105
- package/src/session/prompt/anthropic_spoof.txt +0 -1
- package/src/session/prompt/beast.txt +0 -147
- package/src/session/prompt/build-switch.txt +0 -5
- package/src/session/prompt/codex.txt +0 -73
- package/src/session/prompt/codex_header.txt +0 -72
- package/src/session/prompt/copilot-gpt-5.txt +0 -143
- package/src/session/prompt/gemini.txt +0 -155
- package/src/session/prompt/max-steps.txt +0 -16
- package/src/session/prompt/plan-reminder-anthropic.txt +0 -67
- package/src/session/prompt/plan.txt +0 -26
- package/src/session/prompt/qwen.txt +0 -109
- package/src/session/prompt.ts +0 -1805
- package/src/session/retry.ts +0 -90
- package/src/session/revert.ts +0 -108
- package/src/session/status.ts +0 -76
- package/src/session/summary.ts +0 -150
- package/src/session/system.ts +0 -138
- package/src/session/todo.ts +0 -37
- package/src/share/share-next.ts +0 -194
- package/src/share/share.ts +0 -87
- package/src/shell/shell.ts +0 -67
- package/src/skill/index.ts +0 -1
- package/src/skill/skill.ts +0 -136
- package/src/snapshot/index.ts +0 -236
- package/src/storage/storage.ts +0 -227
- package/src/tool/apply_patch.ts +0 -277
- package/src/tool/apply_patch.txt +0 -1
- package/src/tool/bash.ts +0 -258
- package/src/tool/bash.txt +0 -115
- package/src/tool/batch.ts +0 -175
- package/src/tool/batch.txt +0 -24
- package/src/tool/codesearch.ts +0 -132
- package/src/tool/codesearch.txt +0 -12
- package/src/tool/edit.ts +0 -645
- package/src/tool/edit.txt +0 -10
- package/src/tool/external-directory.ts +0 -32
- package/src/tool/glob.ts +0 -77
- package/src/tool/glob.txt +0 -6
- package/src/tool/grep.ts +0 -154
- package/src/tool/grep.txt +0 -8
- package/src/tool/invalid.ts +0 -17
- package/src/tool/ls.ts +0 -121
- package/src/tool/ls.txt +0 -1
- package/src/tool/lsp.ts +0 -96
- package/src/tool/lsp.txt +0 -19
- package/src/tool/multiedit.ts +0 -46
- package/src/tool/multiedit.txt +0 -41
- package/src/tool/plan-enter.txt +0 -14
- package/src/tool/plan-exit.txt +0 -13
- package/src/tool/plan.ts +0 -130
- package/src/tool/question.ts +0 -33
- package/src/tool/question.txt +0 -10
- package/src/tool/read.ts +0 -202
- package/src/tool/read.txt +0 -12
- package/src/tool/registry.ts +0 -158
- package/src/tool/skill.ts +0 -75
- package/src/tool/task.ts +0 -188
- package/src/tool/task.txt +0 -60
- package/src/tool/todo.ts +0 -53
- package/src/tool/todoread.txt +0 -14
- package/src/tool/todowrite.txt +0 -167
- package/src/tool/tool.ts +0 -88
- package/src/tool/truncation.ts +0 -106
- package/src/tool/webfetch.ts +0 -182
- package/src/tool/webfetch.txt +0 -13
- package/src/tool/websearch.ts +0 -150
- package/src/tool/websearch.txt +0 -14
- package/src/tool/write.ts +0 -80
- package/src/tool/write.txt +0 -8
- package/src/util/archive.ts +0 -16
- package/src/util/color.ts +0 -19
- package/src/util/context.ts +0 -25
- package/src/util/defer.ts +0 -12
- package/src/util/eventloop.ts +0 -20
- package/src/util/filesystem.ts +0 -93
- package/src/util/fn.ts +0 -11
- package/src/util/format.ts +0 -20
- package/src/util/iife.ts +0 -3
- package/src/util/keybind.ts +0 -103
- package/src/util/lazy.ts +0 -18
- package/src/util/locale.ts +0 -81
- package/src/util/lock.ts +0 -98
- package/src/util/log.ts +0 -180
- package/src/util/queue.ts +0 -32
- package/src/util/rpc.ts +0 -66
- package/src/util/scrap.ts +0 -10
- package/src/util/signal.ts +0 -12
- package/src/util/timeout.ts +0 -14
- package/src/util/token.ts +0 -7
- package/src/util/wildcard.ts +0 -56
- package/src/worktree/index.ts +0 -217
- package/sst-env.d.ts +0 -9
- package/test/acp/event-subscription.test.ts +0 -436
- package/test/acp/system-prompt.test.ts +0 -262
- package/test/agent/agent.test.ts +0 -638
- package/test/bun.test.ts +0 -53
- package/test/cli/github-action.test.ts +0 -129
- package/test/cli/github-remote.test.ts +0 -80
- package/test/cli/tui/transcript.test.ts +0 -297
- package/test/config/agent-color.test.ts +0 -66
- package/test/config/config.test.ts +0 -1414
- package/test/config/fixtures/empty-frontmatter.md +0 -4
- package/test/config/fixtures/frontmatter.md +0 -28
- package/test/config/fixtures/no-frontmatter.md +0 -1
- package/test/config/markdown.test.ts +0 -192
- package/test/file/ignore.test.ts +0 -10
- package/test/file/path-traversal.test.ts +0 -198
- package/test/fixture/fixture.ts +0 -45
- package/test/fixture/lsp/fake-lsp-server.js +0 -77
- package/test/ide/ide.test.ts +0 -82
- package/test/keybind.test.ts +0 -421
- package/test/lsp/client.test.ts +0 -95
- package/test/mcp/headers.test.ts +0 -153
- package/test/mcp/oauth-browser.test.ts +0 -261
- package/test/patch/patch.test.ts +0 -348
- package/test/permission/arity.test.ts +0 -33
- package/test/permission/next.test.ts +0 -652
- package/test/permission-task.test.ts +0 -319
- package/test/plugin/codex.test.ts +0 -123
- package/test/preload.ts +0 -65
- package/test/project/project.test.ts +0 -120
- package/test/provider/amazon-bedrock.test.ts +0 -268
- package/test/provider/gitlab-duo.test.ts +0 -286
- package/test/provider/provider.test.ts +0 -2149
- package/test/provider/transform.test.ts +0 -1596
- package/test/question/question.test.ts +0 -300
- package/test/scheduler.test.ts +0 -73
- package/test/server/session-list.test.ts +0 -39
- package/test/server/session-select.test.ts +0 -78
- package/test/session/compaction.test.ts +0 -293
- package/test/session/llm.test.ts +0 -90
- package/test/session/message-v2.test.ts +0 -662
- package/test/session/retry.test.ts +0 -131
- package/test/session/revert-compact.test.ts +0 -285
- package/test/session/session.test.ts +0 -71
- package/test/skill/skill.test.ts +0 -185
- package/test/snapshot/snapshot.test.ts +0 -939
- package/test/tool/__snapshots__/tool.test.ts.snap +0 -9
- package/test/tool/apply_patch.test.ts +0 -515
- package/test/tool/bash.test.ts +0 -320
- package/test/tool/external-directory.test.ts +0 -126
- package/test/tool/fixtures/large-image.png +0 -0
- package/test/tool/fixtures/models-api.json +0 -33453
- package/test/tool/grep.test.ts +0 -109
- package/test/tool/question.test.ts +0 -105
- package/test/tool/read.test.ts +0 -332
- package/test/tool/registry.test.ts +0 -76
- package/test/tool/truncation.test.ts +0 -159
- package/test/util/filesystem.test.ts +0 -39
- package/test/util/format.test.ts +0 -59
- package/test/util/iife.test.ts +0 -36
- package/test/util/lazy.test.ts +0 -50
- package/test/util/lock.test.ts +0 -72
- package/test/util/timeout.test.ts +0 -21
- package/test/util/wildcard.test.ts +0 -75
- package/tsconfig.json +0 -16
package/src/provider/provider.ts
DELETED
|
@@ -1,1219 +0,0 @@
|
|
|
1
|
-
import z from "zod"
|
|
2
|
-
import fuzzysort from "fuzzysort"
|
|
3
|
-
import { Config } from "../config/config"
|
|
4
|
-
import { mapValues, mergeDeep, omit, pickBy, sortBy } from "remeda"
|
|
5
|
-
import { NoSuchModelError, type Provider as SDK } from "ai"
|
|
6
|
-
import { Log } from "../util/log"
|
|
7
|
-
import { BunProc } from "../bun"
|
|
8
|
-
import { Plugin } from "../plugin"
|
|
9
|
-
import { ModelsDev } from "./models"
|
|
10
|
-
import { NamedError } from "@opencode-ai/util/error"
|
|
11
|
-
import { Auth } from "../auth"
|
|
12
|
-
import { Env } from "../env"
|
|
13
|
-
import { Instance } from "../project/instance"
|
|
14
|
-
import { Flag } from "../flag/flag"
|
|
15
|
-
import { iife } from "@/util/iife"
|
|
16
|
-
|
|
17
|
-
// Direct imports for bundled providers
|
|
18
|
-
import { createAmazonBedrock, type AmazonBedrockProviderSettings } from "@ai-sdk/amazon-bedrock"
|
|
19
|
-
import { createAnthropic } from "@ai-sdk/anthropic"
|
|
20
|
-
import { createAzure } from "@ai-sdk/azure"
|
|
21
|
-
import { createGoogleGenerativeAI } from "@ai-sdk/google"
|
|
22
|
-
import { createVertex } from "@ai-sdk/google-vertex"
|
|
23
|
-
import { createVertexAnthropic } from "@ai-sdk/google-vertex/anthropic"
|
|
24
|
-
import { createOpenAI } from "@ai-sdk/openai"
|
|
25
|
-
import { createOpenAICompatible } from "@ai-sdk/openai-compatible"
|
|
26
|
-
import { createOpenRouter, type LanguageModelV2 } from "@openrouter/ai-sdk-provider"
|
|
27
|
-
import { createOpenaiCompatible as createGitHubCopilotOpenAICompatible } from "./sdk/openai-compatible/src"
|
|
28
|
-
import { createXai } from "@ai-sdk/xai"
|
|
29
|
-
import { createMistral } from "@ai-sdk/mistral"
|
|
30
|
-
import { createGroq } from "@ai-sdk/groq"
|
|
31
|
-
import { createDeepInfra } from "@ai-sdk/deepinfra"
|
|
32
|
-
import { createCerebras } from "@ai-sdk/cerebras"
|
|
33
|
-
import { createCohere } from "@ai-sdk/cohere"
|
|
34
|
-
import { createGateway } from "@ai-sdk/gateway"
|
|
35
|
-
import { createTogetherAI } from "@ai-sdk/togetherai"
|
|
36
|
-
import { createPerplexity } from "@ai-sdk/perplexity"
|
|
37
|
-
import { createVercel } from "@ai-sdk/vercel"
|
|
38
|
-
import { createGitLab } from "@gitlab/gitlab-ai-provider"
|
|
39
|
-
import { ProviderTransform } from "./transform"
|
|
40
|
-
|
|
41
|
-
export namespace Provider {
|
|
42
|
-
const log = Log.create({ service: "provider" })
|
|
43
|
-
|
|
44
|
-
function isGpt5OrLater(modelID: string): boolean {
|
|
45
|
-
const match = /^gpt-(\d+)/.exec(modelID)
|
|
46
|
-
if (!match) {
|
|
47
|
-
return false
|
|
48
|
-
}
|
|
49
|
-
return Number(match[1]) >= 5
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function shouldUseCopilotResponsesApi(modelID: string): boolean {
|
|
53
|
-
return isGpt5OrLater(modelID) && !modelID.startsWith("gpt-5-mini")
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
const BUNDLED_PROVIDERS: Record<string, (options: any) => SDK> = {
|
|
57
|
-
"@ai-sdk/amazon-bedrock": createAmazonBedrock,
|
|
58
|
-
"@ai-sdk/anthropic": createAnthropic,
|
|
59
|
-
"@ai-sdk/azure": createAzure,
|
|
60
|
-
"@ai-sdk/google": createGoogleGenerativeAI,
|
|
61
|
-
"@ai-sdk/google-vertex": createVertex,
|
|
62
|
-
"@ai-sdk/google-vertex/anthropic": createVertexAnthropic,
|
|
63
|
-
"@ai-sdk/openai": createOpenAI,
|
|
64
|
-
"@ai-sdk/openai-compatible": createOpenAICompatible,
|
|
65
|
-
"@openrouter/ai-sdk-provider": createOpenRouter,
|
|
66
|
-
"@ai-sdk/xai": createXai,
|
|
67
|
-
"@ai-sdk/mistral": createMistral,
|
|
68
|
-
"@ai-sdk/groq": createGroq,
|
|
69
|
-
"@ai-sdk/deepinfra": createDeepInfra,
|
|
70
|
-
"@ai-sdk/cerebras": createCerebras,
|
|
71
|
-
"@ai-sdk/cohere": createCohere,
|
|
72
|
-
"@ai-sdk/gateway": createGateway,
|
|
73
|
-
"@ai-sdk/togetherai": createTogetherAI,
|
|
74
|
-
"@ai-sdk/perplexity": createPerplexity,
|
|
75
|
-
"@ai-sdk/vercel": createVercel,
|
|
76
|
-
"@gitlab/gitlab-ai-provider": createGitLab,
|
|
77
|
-
// @ts-ignore (TODO: kill this code so we dont have to maintain it)
|
|
78
|
-
"@ai-sdk/github-copilot": createGitHubCopilotOpenAICompatible,
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
type CustomModelLoader = (sdk: any, modelID: string, options?: Record<string, any>) => Promise<any>
|
|
82
|
-
type CustomLoader = (provider: Info) => Promise<{
|
|
83
|
-
autoload: boolean
|
|
84
|
-
getModel?: CustomModelLoader
|
|
85
|
-
options?: Record<string, any>
|
|
86
|
-
}>
|
|
87
|
-
|
|
88
|
-
const CUSTOM_LOADERS: Record<string, CustomLoader> = {
|
|
89
|
-
async anthropic() {
|
|
90
|
-
return {
|
|
91
|
-
autoload: false,
|
|
92
|
-
options: {
|
|
93
|
-
headers: {
|
|
94
|
-
"anthropic-beta":
|
|
95
|
-
"claude-code-20250219,interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14",
|
|
96
|
-
},
|
|
97
|
-
},
|
|
98
|
-
}
|
|
99
|
-
},
|
|
100
|
-
async opencode(input) {
|
|
101
|
-
const hasKey = await (async () => {
|
|
102
|
-
const env = Env.all()
|
|
103
|
-
if (input.env.some((item) => env[item])) return true
|
|
104
|
-
if (await Auth.get(input.id)) return true
|
|
105
|
-
const config = await Config.get()
|
|
106
|
-
if (config.provider?.["opencode"]?.options?.apiKey) return true
|
|
107
|
-
return false
|
|
108
|
-
})()
|
|
109
|
-
|
|
110
|
-
if (!hasKey) {
|
|
111
|
-
for (const [key, value] of Object.entries(input.models)) {
|
|
112
|
-
if (value.cost.input === 0) continue
|
|
113
|
-
delete input.models[key]
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return {
|
|
118
|
-
autoload: Object.keys(input.models).length > 0,
|
|
119
|
-
options: hasKey ? {} : { apiKey: "public" },
|
|
120
|
-
}
|
|
121
|
-
},
|
|
122
|
-
openai: async () => {
|
|
123
|
-
return {
|
|
124
|
-
autoload: false,
|
|
125
|
-
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
126
|
-
return sdk.responses(modelID)
|
|
127
|
-
},
|
|
128
|
-
options: {},
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
"github-copilot": async () => {
|
|
132
|
-
return {
|
|
133
|
-
autoload: false,
|
|
134
|
-
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
135
|
-
return shouldUseCopilotResponsesApi(modelID) ? sdk.responses(modelID) : sdk.chat(modelID)
|
|
136
|
-
},
|
|
137
|
-
options: {},
|
|
138
|
-
}
|
|
139
|
-
},
|
|
140
|
-
"github-copilot-enterprise": async () => {
|
|
141
|
-
return {
|
|
142
|
-
autoload: false,
|
|
143
|
-
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
144
|
-
return shouldUseCopilotResponsesApi(modelID) ? sdk.responses(modelID) : sdk.chat(modelID)
|
|
145
|
-
},
|
|
146
|
-
options: {},
|
|
147
|
-
}
|
|
148
|
-
},
|
|
149
|
-
azure: async () => {
|
|
150
|
-
return {
|
|
151
|
-
autoload: false,
|
|
152
|
-
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
|
|
153
|
-
if (options?.["useCompletionUrls"]) {
|
|
154
|
-
return sdk.chat(modelID)
|
|
155
|
-
} else {
|
|
156
|
-
return sdk.responses(modelID)
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
options: {},
|
|
160
|
-
}
|
|
161
|
-
},
|
|
162
|
-
"azure-cognitive-services": async () => {
|
|
163
|
-
const resourceName = Env.get("AZURE_COGNITIVE_SERVICES_RESOURCE_NAME")
|
|
164
|
-
return {
|
|
165
|
-
autoload: false,
|
|
166
|
-
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
|
|
167
|
-
if (options?.["useCompletionUrls"]) {
|
|
168
|
-
return sdk.chat(modelID)
|
|
169
|
-
} else {
|
|
170
|
-
return sdk.responses(modelID)
|
|
171
|
-
}
|
|
172
|
-
},
|
|
173
|
-
options: {
|
|
174
|
-
baseURL: resourceName ? `https://${resourceName}.cognitiveservices.azure.com/openai` : undefined,
|
|
175
|
-
},
|
|
176
|
-
}
|
|
177
|
-
},
|
|
178
|
-
"amazon-bedrock": async () => {
|
|
179
|
-
const config = await Config.get()
|
|
180
|
-
const providerConfig = config.provider?.["amazon-bedrock"]
|
|
181
|
-
|
|
182
|
-
const auth = await Auth.get("amazon-bedrock")
|
|
183
|
-
|
|
184
|
-
// Region precedence: 1) config file, 2) env var, 3) default
|
|
185
|
-
const configRegion = providerConfig?.options?.region
|
|
186
|
-
const envRegion = Env.get("AWS_REGION")
|
|
187
|
-
const defaultRegion = configRegion ?? envRegion ?? "us-east-1"
|
|
188
|
-
|
|
189
|
-
// Profile: config file takes precedence over env var
|
|
190
|
-
const configProfile = providerConfig?.options?.profile
|
|
191
|
-
const envProfile = Env.get("AWS_PROFILE")
|
|
192
|
-
const profile = configProfile ?? envProfile
|
|
193
|
-
|
|
194
|
-
const awsAccessKeyId = Env.get("AWS_ACCESS_KEY_ID")
|
|
195
|
-
|
|
196
|
-
const awsBearerToken = iife(() => {
|
|
197
|
-
const envToken = Env.get("AWS_BEARER_TOKEN_BEDROCK")
|
|
198
|
-
if (envToken) return envToken
|
|
199
|
-
if (auth?.type === "api") {
|
|
200
|
-
Env.set("AWS_BEARER_TOKEN_BEDROCK", auth.key)
|
|
201
|
-
return auth.key
|
|
202
|
-
}
|
|
203
|
-
return undefined
|
|
204
|
-
})
|
|
205
|
-
|
|
206
|
-
const awsWebIdentityTokenFile = Env.get("AWS_WEB_IDENTITY_TOKEN_FILE")
|
|
207
|
-
|
|
208
|
-
if (!profile && !awsAccessKeyId && !awsBearerToken && !awsWebIdentityTokenFile) return { autoload: false }
|
|
209
|
-
|
|
210
|
-
const providerOptions: AmazonBedrockProviderSettings = {
|
|
211
|
-
region: defaultRegion,
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Only use credential chain if no bearer token exists
|
|
215
|
-
// Bearer token takes precedence over credential chain (profiles, access keys, IAM roles, web identity tokens)
|
|
216
|
-
if (!awsBearerToken) {
|
|
217
|
-
const { fromNodeProviderChain } = await import(await BunProc.install("@aws-sdk/credential-providers"))
|
|
218
|
-
|
|
219
|
-
// Build credential provider options (only pass profile if specified)
|
|
220
|
-
const credentialProviderOptions = profile ? { profile } : {}
|
|
221
|
-
|
|
222
|
-
providerOptions.credentialProvider = fromNodeProviderChain(credentialProviderOptions)
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
// Add custom endpoint if specified (endpoint takes precedence over baseURL)
|
|
226
|
-
const endpoint = providerConfig?.options?.endpoint ?? providerConfig?.options?.baseURL
|
|
227
|
-
if (endpoint) {
|
|
228
|
-
providerOptions.baseURL = endpoint
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
return {
|
|
232
|
-
autoload: true,
|
|
233
|
-
options: providerOptions,
|
|
234
|
-
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
|
|
235
|
-
// Skip region prefixing if model already has a cross-region inference profile prefix
|
|
236
|
-
if (modelID.startsWith("global.") || modelID.startsWith("jp.")) {
|
|
237
|
-
return sdk.languageModel(modelID)
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// Region resolution precedence (highest to lowest):
|
|
241
|
-
// 1. options.region from opencode.json provider config
|
|
242
|
-
// 2. defaultRegion from AWS_REGION environment variable
|
|
243
|
-
// 3. Default "us-east-1" (baked into defaultRegion)
|
|
244
|
-
const region = options?.region ?? defaultRegion
|
|
245
|
-
|
|
246
|
-
let regionPrefix = region.split("-")[0]
|
|
247
|
-
|
|
248
|
-
switch (regionPrefix) {
|
|
249
|
-
case "us": {
|
|
250
|
-
const modelRequiresPrefix = [
|
|
251
|
-
"nova-micro",
|
|
252
|
-
"nova-lite",
|
|
253
|
-
"nova-pro",
|
|
254
|
-
"nova-premier",
|
|
255
|
-
"nova-2",
|
|
256
|
-
"claude",
|
|
257
|
-
"deepseek",
|
|
258
|
-
].some((m) => modelID.includes(m))
|
|
259
|
-
const isGovCloud = region.startsWith("us-gov")
|
|
260
|
-
if (modelRequiresPrefix && !isGovCloud) {
|
|
261
|
-
modelID = `${regionPrefix}.${modelID}`
|
|
262
|
-
}
|
|
263
|
-
break
|
|
264
|
-
}
|
|
265
|
-
case "eu": {
|
|
266
|
-
const regionRequiresPrefix = [
|
|
267
|
-
"eu-west-1",
|
|
268
|
-
"eu-west-2",
|
|
269
|
-
"eu-west-3",
|
|
270
|
-
"eu-north-1",
|
|
271
|
-
"eu-central-1",
|
|
272
|
-
"eu-south-1",
|
|
273
|
-
"eu-south-2",
|
|
274
|
-
].some((r) => region.includes(r))
|
|
275
|
-
const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "llama3", "pixtral"].some((m) =>
|
|
276
|
-
modelID.includes(m),
|
|
277
|
-
)
|
|
278
|
-
if (regionRequiresPrefix && modelRequiresPrefix) {
|
|
279
|
-
modelID = `${regionPrefix}.${modelID}`
|
|
280
|
-
}
|
|
281
|
-
break
|
|
282
|
-
}
|
|
283
|
-
case "ap": {
|
|
284
|
-
const isAustraliaRegion = ["ap-southeast-2", "ap-southeast-4"].includes(region)
|
|
285
|
-
const isTokyoRegion = region === "ap-northeast-1"
|
|
286
|
-
if (
|
|
287
|
-
isAustraliaRegion &&
|
|
288
|
-
["anthropic.claude-sonnet-4-5", "anthropic.claude-haiku"].some((m) => modelID.includes(m))
|
|
289
|
-
) {
|
|
290
|
-
regionPrefix = "au"
|
|
291
|
-
modelID = `${regionPrefix}.${modelID}`
|
|
292
|
-
} else if (isTokyoRegion) {
|
|
293
|
-
// Tokyo region uses jp. prefix for cross-region inference
|
|
294
|
-
const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) =>
|
|
295
|
-
modelID.includes(m),
|
|
296
|
-
)
|
|
297
|
-
if (modelRequiresPrefix) {
|
|
298
|
-
regionPrefix = "jp"
|
|
299
|
-
modelID = `${regionPrefix}.${modelID}`
|
|
300
|
-
}
|
|
301
|
-
} else {
|
|
302
|
-
// Other APAC regions use apac. prefix
|
|
303
|
-
const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) =>
|
|
304
|
-
modelID.includes(m),
|
|
305
|
-
)
|
|
306
|
-
if (modelRequiresPrefix) {
|
|
307
|
-
regionPrefix = "apac"
|
|
308
|
-
modelID = `${regionPrefix}.${modelID}`
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
break
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
return sdk.languageModel(modelID)
|
|
316
|
-
},
|
|
317
|
-
}
|
|
318
|
-
},
|
|
319
|
-
openrouter: async () => {
|
|
320
|
-
return {
|
|
321
|
-
autoload: false,
|
|
322
|
-
options: {
|
|
323
|
-
headers: {
|
|
324
|
-
"HTTP-Referer": "https://opencode.ai/",
|
|
325
|
-
"X-Title": "opencode",
|
|
326
|
-
},
|
|
327
|
-
},
|
|
328
|
-
}
|
|
329
|
-
},
|
|
330
|
-
vercel: async () => {
|
|
331
|
-
return {
|
|
332
|
-
autoload: false,
|
|
333
|
-
options: {
|
|
334
|
-
headers: {
|
|
335
|
-
"http-referer": "https://opencode.ai/",
|
|
336
|
-
"x-title": "opencode",
|
|
337
|
-
},
|
|
338
|
-
},
|
|
339
|
-
}
|
|
340
|
-
},
|
|
341
|
-
"google-vertex": async () => {
|
|
342
|
-
const project = Env.get("GOOGLE_CLOUD_PROJECT") ?? Env.get("GCP_PROJECT") ?? Env.get("GCLOUD_PROJECT")
|
|
343
|
-
const location = Env.get("GOOGLE_CLOUD_LOCATION") ?? Env.get("VERTEX_LOCATION") ?? "us-east5"
|
|
344
|
-
const autoload = Boolean(project)
|
|
345
|
-
if (!autoload) return { autoload: false }
|
|
346
|
-
return {
|
|
347
|
-
autoload: true,
|
|
348
|
-
options: {
|
|
349
|
-
project,
|
|
350
|
-
location,
|
|
351
|
-
},
|
|
352
|
-
async getModel(sdk: any, modelID: string) {
|
|
353
|
-
const id = String(modelID).trim()
|
|
354
|
-
return sdk.languageModel(id)
|
|
355
|
-
},
|
|
356
|
-
}
|
|
357
|
-
},
|
|
358
|
-
"google-vertex-anthropic": async () => {
|
|
359
|
-
const project = Env.get("GOOGLE_CLOUD_PROJECT") ?? Env.get("GCP_PROJECT") ?? Env.get("GCLOUD_PROJECT")
|
|
360
|
-
const location = Env.get("GOOGLE_CLOUD_LOCATION") ?? Env.get("VERTEX_LOCATION") ?? "global"
|
|
361
|
-
const autoload = Boolean(project)
|
|
362
|
-
if (!autoload) return { autoload: false }
|
|
363
|
-
return {
|
|
364
|
-
autoload: true,
|
|
365
|
-
options: {
|
|
366
|
-
project,
|
|
367
|
-
location,
|
|
368
|
-
},
|
|
369
|
-
async getModel(sdk: any, modelID) {
|
|
370
|
-
const id = String(modelID).trim()
|
|
371
|
-
return sdk.languageModel(id)
|
|
372
|
-
},
|
|
373
|
-
}
|
|
374
|
-
},
|
|
375
|
-
"sap-ai-core": async () => {
|
|
376
|
-
const auth = await Auth.get("sap-ai-core")
|
|
377
|
-
const envServiceKey = iife(() => {
|
|
378
|
-
const envAICoreServiceKey = Env.get("AICORE_SERVICE_KEY")
|
|
379
|
-
if (envAICoreServiceKey) return envAICoreServiceKey
|
|
380
|
-
if (auth?.type === "api") {
|
|
381
|
-
Env.set("AICORE_SERVICE_KEY", auth.key)
|
|
382
|
-
return auth.key
|
|
383
|
-
}
|
|
384
|
-
return undefined
|
|
385
|
-
})
|
|
386
|
-
const deploymentId = Env.get("AICORE_DEPLOYMENT_ID")
|
|
387
|
-
const resourceGroup = Env.get("AICORE_RESOURCE_GROUP")
|
|
388
|
-
|
|
389
|
-
return {
|
|
390
|
-
autoload: !!envServiceKey,
|
|
391
|
-
options: envServiceKey ? { deploymentId, resourceGroup } : {},
|
|
392
|
-
async getModel(sdk: any, modelID: string) {
|
|
393
|
-
return sdk(modelID)
|
|
394
|
-
},
|
|
395
|
-
}
|
|
396
|
-
},
|
|
397
|
-
zenmux: async () => {
|
|
398
|
-
return {
|
|
399
|
-
autoload: false,
|
|
400
|
-
options: {
|
|
401
|
-
headers: {
|
|
402
|
-
"HTTP-Referer": "https://opencode.ai/",
|
|
403
|
-
"X-Title": "opencode",
|
|
404
|
-
},
|
|
405
|
-
},
|
|
406
|
-
}
|
|
407
|
-
},
|
|
408
|
-
gitlab: async (input) => {
|
|
409
|
-
const instanceUrl = Env.get("GITLAB_INSTANCE_URL") || "https://gitlab.com"
|
|
410
|
-
|
|
411
|
-
const auth = await Auth.get(input.id)
|
|
412
|
-
const apiKey = await (async () => {
|
|
413
|
-
if (auth?.type === "oauth") return auth.access
|
|
414
|
-
if (auth?.type === "api") return auth.key
|
|
415
|
-
return Env.get("GITLAB_TOKEN")
|
|
416
|
-
})()
|
|
417
|
-
|
|
418
|
-
const config = await Config.get()
|
|
419
|
-
const providerConfig = config.provider?.["gitlab"]
|
|
420
|
-
|
|
421
|
-
return {
|
|
422
|
-
autoload: !!apiKey,
|
|
423
|
-
options: {
|
|
424
|
-
instanceUrl,
|
|
425
|
-
apiKey,
|
|
426
|
-
featureFlags: {
|
|
427
|
-
duo_agent_platform_agentic_chat: true,
|
|
428
|
-
duo_agent_platform: true,
|
|
429
|
-
...(providerConfig?.options?.featureFlags || {}),
|
|
430
|
-
},
|
|
431
|
-
},
|
|
432
|
-
async getModel(sdk: ReturnType<typeof createGitLab>, modelID: string) {
|
|
433
|
-
return sdk.agenticChat(modelID, {
|
|
434
|
-
featureFlags: {
|
|
435
|
-
duo_agent_platform_agentic_chat: true,
|
|
436
|
-
duo_agent_platform: true,
|
|
437
|
-
...(providerConfig?.options?.featureFlags || {}),
|
|
438
|
-
},
|
|
439
|
-
})
|
|
440
|
-
},
|
|
441
|
-
}
|
|
442
|
-
},
|
|
443
|
-
"cloudflare-ai-gateway": async (input) => {
|
|
444
|
-
const accountId = Env.get("CLOUDFLARE_ACCOUNT_ID")
|
|
445
|
-
const gateway = Env.get("CLOUDFLARE_GATEWAY_ID")
|
|
446
|
-
|
|
447
|
-
if (!accountId || !gateway) return { autoload: false }
|
|
448
|
-
|
|
449
|
-
// Get API token from env or auth prompt
|
|
450
|
-
const apiToken = await (async () => {
|
|
451
|
-
const envToken = Env.get("CLOUDFLARE_API_TOKEN")
|
|
452
|
-
if (envToken) return envToken
|
|
453
|
-
const auth = await Auth.get(input.id)
|
|
454
|
-
if (auth?.type === "api") return auth.key
|
|
455
|
-
return undefined
|
|
456
|
-
})()
|
|
457
|
-
|
|
458
|
-
return {
|
|
459
|
-
autoload: true,
|
|
460
|
-
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
461
|
-
return sdk.languageModel(modelID)
|
|
462
|
-
},
|
|
463
|
-
options: {
|
|
464
|
-
baseURL: `https://gateway.ai.cloudflare.com/v1/${accountId}/${gateway}/compat`,
|
|
465
|
-
headers: {
|
|
466
|
-
// Cloudflare AI Gateway uses cf-aig-authorization for authenticated gateways
|
|
467
|
-
// This enables Unified Billing where Cloudflare handles upstream provider auth
|
|
468
|
-
...(apiToken ? { "cf-aig-authorization": `Bearer ${apiToken}` } : {}),
|
|
469
|
-
"HTTP-Referer": "https://opencode.ai/",
|
|
470
|
-
"X-Title": "opencode",
|
|
471
|
-
},
|
|
472
|
-
// Custom fetch to handle parameter transformation and auth
|
|
473
|
-
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
474
|
-
const headers = new Headers(init?.headers)
|
|
475
|
-
// Strip Authorization header - AI Gateway uses cf-aig-authorization instead
|
|
476
|
-
headers.delete("Authorization")
|
|
477
|
-
|
|
478
|
-
// Transform max_tokens to max_completion_tokens for newer models
|
|
479
|
-
if (init?.body && init.method === "POST") {
|
|
480
|
-
try {
|
|
481
|
-
const body = JSON.parse(init.body as string)
|
|
482
|
-
if (body.max_tokens !== undefined && !body.max_completion_tokens) {
|
|
483
|
-
body.max_completion_tokens = body.max_tokens
|
|
484
|
-
delete body.max_tokens
|
|
485
|
-
init = { ...init, body: JSON.stringify(body) }
|
|
486
|
-
}
|
|
487
|
-
} catch (e) {
|
|
488
|
-
// If body parsing fails, continue with original request
|
|
489
|
-
}
|
|
490
|
-
}
|
|
491
|
-
|
|
492
|
-
return fetch(input, { ...init, headers })
|
|
493
|
-
},
|
|
494
|
-
},
|
|
495
|
-
}
|
|
496
|
-
},
|
|
497
|
-
cerebras: async () => {
|
|
498
|
-
return {
|
|
499
|
-
autoload: false,
|
|
500
|
-
options: {
|
|
501
|
-
headers: {
|
|
502
|
-
"X-Cerebras-3rd-Party-Integration": "opencode",
|
|
503
|
-
},
|
|
504
|
-
},
|
|
505
|
-
}
|
|
506
|
-
},
|
|
507
|
-
}
|
|
508
|
-
|
|
509
|
-
export const Model = z
|
|
510
|
-
.object({
|
|
511
|
-
id: z.string(),
|
|
512
|
-
providerID: z.string(),
|
|
513
|
-
api: z.object({
|
|
514
|
-
id: z.string(),
|
|
515
|
-
url: z.string(),
|
|
516
|
-
npm: z.string(),
|
|
517
|
-
}),
|
|
518
|
-
name: z.string(),
|
|
519
|
-
family: z.string().optional(),
|
|
520
|
-
capabilities: z.object({
|
|
521
|
-
temperature: z.boolean(),
|
|
522
|
-
reasoning: z.boolean(),
|
|
523
|
-
attachment: z.boolean(),
|
|
524
|
-
toolcall: z.boolean(),
|
|
525
|
-
input: z.object({
|
|
526
|
-
text: z.boolean(),
|
|
527
|
-
audio: z.boolean(),
|
|
528
|
-
image: z.boolean(),
|
|
529
|
-
video: z.boolean(),
|
|
530
|
-
pdf: z.boolean(),
|
|
531
|
-
}),
|
|
532
|
-
output: z.object({
|
|
533
|
-
text: z.boolean(),
|
|
534
|
-
audio: z.boolean(),
|
|
535
|
-
image: z.boolean(),
|
|
536
|
-
video: z.boolean(),
|
|
537
|
-
pdf: z.boolean(),
|
|
538
|
-
}),
|
|
539
|
-
interleaved: z.union([
|
|
540
|
-
z.boolean(),
|
|
541
|
-
z.object({
|
|
542
|
-
field: z.enum(["reasoning_content", "reasoning_details"]),
|
|
543
|
-
}),
|
|
544
|
-
]),
|
|
545
|
-
}),
|
|
546
|
-
cost: z.object({
|
|
547
|
-
input: z.number(),
|
|
548
|
-
output: z.number(),
|
|
549
|
-
cache: z.object({
|
|
550
|
-
read: z.number(),
|
|
551
|
-
write: z.number(),
|
|
552
|
-
}),
|
|
553
|
-
experimentalOver200K: z
|
|
554
|
-
.object({
|
|
555
|
-
input: z.number(),
|
|
556
|
-
output: z.number(),
|
|
557
|
-
cache: z.object({
|
|
558
|
-
read: z.number(),
|
|
559
|
-
write: z.number(),
|
|
560
|
-
}),
|
|
561
|
-
})
|
|
562
|
-
.optional(),
|
|
563
|
-
}),
|
|
564
|
-
limit: z.object({
|
|
565
|
-
context: z.number(),
|
|
566
|
-
input: z.number().optional(),
|
|
567
|
-
output: z.number(),
|
|
568
|
-
}),
|
|
569
|
-
status: z.enum(["alpha", "beta", "deprecated", "active"]),
|
|
570
|
-
options: z.record(z.string(), z.any()),
|
|
571
|
-
headers: z.record(z.string(), z.string()),
|
|
572
|
-
release_date: z.string(),
|
|
573
|
-
variants: z.record(z.string(), z.record(z.string(), z.any())).optional(),
|
|
574
|
-
})
|
|
575
|
-
.meta({
|
|
576
|
-
ref: "Model",
|
|
577
|
-
})
|
|
578
|
-
export type Model = z.infer<typeof Model>
|
|
579
|
-
|
|
580
|
-
export const Info = z
|
|
581
|
-
.object({
|
|
582
|
-
id: z.string(),
|
|
583
|
-
name: z.string(),
|
|
584
|
-
source: z.enum(["env", "config", "custom", "api"]),
|
|
585
|
-
env: z.string().array(),
|
|
586
|
-
key: z.string().optional(),
|
|
587
|
-
options: z.record(z.string(), z.any()),
|
|
588
|
-
models: z.record(z.string(), Model),
|
|
589
|
-
})
|
|
590
|
-
.meta({
|
|
591
|
-
ref: "Provider",
|
|
592
|
-
})
|
|
593
|
-
export type Info = z.infer<typeof Info>
|
|
594
|
-
|
|
595
|
-
function fromModelsDevModel(provider: ModelsDev.Provider, model: ModelsDev.Model): Model {
|
|
596
|
-
const m: Model = {
|
|
597
|
-
id: model.id,
|
|
598
|
-
providerID: provider.id,
|
|
599
|
-
name: model.name,
|
|
600
|
-
family: model.family,
|
|
601
|
-
api: {
|
|
602
|
-
id: model.id,
|
|
603
|
-
url: provider.api!,
|
|
604
|
-
npm: iife(() => {
|
|
605
|
-
if (provider.id.startsWith("github-copilot")) return "@ai-sdk/github-copilot"
|
|
606
|
-
return model.provider?.npm ?? provider.npm ?? "@ai-sdk/openai-compatible"
|
|
607
|
-
}),
|
|
608
|
-
},
|
|
609
|
-
status: model.status ?? "active",
|
|
610
|
-
headers: model.headers ?? {},
|
|
611
|
-
options: model.options ?? {},
|
|
612
|
-
cost: {
|
|
613
|
-
input: model.cost?.input ?? 0,
|
|
614
|
-
output: model.cost?.output ?? 0,
|
|
615
|
-
cache: {
|
|
616
|
-
read: model.cost?.cache_read ?? 0,
|
|
617
|
-
write: model.cost?.cache_write ?? 0,
|
|
618
|
-
},
|
|
619
|
-
experimentalOver200K: model.cost?.context_over_200k
|
|
620
|
-
? {
|
|
621
|
-
cache: {
|
|
622
|
-
read: model.cost.context_over_200k.cache_read ?? 0,
|
|
623
|
-
write: model.cost.context_over_200k.cache_write ?? 0,
|
|
624
|
-
},
|
|
625
|
-
input: model.cost.context_over_200k.input,
|
|
626
|
-
output: model.cost.context_over_200k.output,
|
|
627
|
-
}
|
|
628
|
-
: undefined,
|
|
629
|
-
},
|
|
630
|
-
limit: {
|
|
631
|
-
context: model.limit.context,
|
|
632
|
-
input: model.limit.input,
|
|
633
|
-
output: model.limit.output,
|
|
634
|
-
},
|
|
635
|
-
capabilities: {
|
|
636
|
-
temperature: model.temperature,
|
|
637
|
-
reasoning: model.reasoning,
|
|
638
|
-
attachment: model.attachment,
|
|
639
|
-
toolcall: model.tool_call,
|
|
640
|
-
input: {
|
|
641
|
-
text: model.modalities?.input?.includes("text") ?? false,
|
|
642
|
-
audio: model.modalities?.input?.includes("audio") ?? false,
|
|
643
|
-
image: model.modalities?.input?.includes("image") ?? false,
|
|
644
|
-
video: model.modalities?.input?.includes("video") ?? false,
|
|
645
|
-
pdf: model.modalities?.input?.includes("pdf") ?? false,
|
|
646
|
-
},
|
|
647
|
-
output: {
|
|
648
|
-
text: model.modalities?.output?.includes("text") ?? false,
|
|
649
|
-
audio: model.modalities?.output?.includes("audio") ?? false,
|
|
650
|
-
image: model.modalities?.output?.includes("image") ?? false,
|
|
651
|
-
video: model.modalities?.output?.includes("video") ?? false,
|
|
652
|
-
pdf: model.modalities?.output?.includes("pdf") ?? false,
|
|
653
|
-
},
|
|
654
|
-
interleaved: model.interleaved ?? false,
|
|
655
|
-
},
|
|
656
|
-
release_date: model.release_date,
|
|
657
|
-
variants: {},
|
|
658
|
-
}
|
|
659
|
-
|
|
660
|
-
m.variants = mapValues(ProviderTransform.variants(m), (v) => v)
|
|
661
|
-
|
|
662
|
-
return m
|
|
663
|
-
}
|
|
664
|
-
|
|
665
|
-
export function fromModelsDevProvider(provider: ModelsDev.Provider): Info {
|
|
666
|
-
return {
|
|
667
|
-
id: provider.id,
|
|
668
|
-
source: "custom",
|
|
669
|
-
name: provider.name,
|
|
670
|
-
env: provider.env ?? [],
|
|
671
|
-
options: {},
|
|
672
|
-
models: mapValues(provider.models, (model) => fromModelsDevModel(provider, model)),
|
|
673
|
-
}
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
const state = Instance.state(async () => {
|
|
677
|
-
using _ = log.time("state")
|
|
678
|
-
const config = await Config.get()
|
|
679
|
-
const modelsDev = await ModelsDev.get()
|
|
680
|
-
const database = mapValues(modelsDev, fromModelsDevProvider)
|
|
681
|
-
|
|
682
|
-
const disabled = new Set(config.disabled_providers ?? [])
|
|
683
|
-
const enabled = config.enabled_providers ? new Set(config.enabled_providers) : null
|
|
684
|
-
|
|
685
|
-
function isProviderAllowed(providerID: string): boolean {
|
|
686
|
-
if (enabled && !enabled.has(providerID)) return false
|
|
687
|
-
if (disabled.has(providerID)) return false
|
|
688
|
-
return true
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
const providers: { [providerID: string]: Info } = {}
|
|
692
|
-
const languages = new Map<string, LanguageModelV2>()
|
|
693
|
-
const modelLoaders: {
|
|
694
|
-
[providerID: string]: CustomModelLoader
|
|
695
|
-
} = {}
|
|
696
|
-
const sdk = new Map<number, SDK>()
|
|
697
|
-
|
|
698
|
-
log.info("init")
|
|
699
|
-
|
|
700
|
-
const configProviders = Object.entries(config.provider ?? {})
|
|
701
|
-
|
|
702
|
-
// Add GitHub Copilot Enterprise provider that inherits from GitHub Copilot
|
|
703
|
-
if (database["github-copilot"]) {
|
|
704
|
-
const githubCopilot = database["github-copilot"]
|
|
705
|
-
database["github-copilot-enterprise"] = {
|
|
706
|
-
...githubCopilot,
|
|
707
|
-
id: "github-copilot-enterprise",
|
|
708
|
-
name: "GitHub Copilot Enterprise",
|
|
709
|
-
models: mapValues(githubCopilot.models, (model) => ({
|
|
710
|
-
...model,
|
|
711
|
-
providerID: "github-copilot-enterprise",
|
|
712
|
-
})),
|
|
713
|
-
}
|
|
714
|
-
}
|
|
715
|
-
|
|
716
|
-
function mergeProvider(providerID: string, provider: Partial<Info>) {
|
|
717
|
-
const existing = providers[providerID]
|
|
718
|
-
if (existing) {
|
|
719
|
-
// @ts-expect-error
|
|
720
|
-
providers[providerID] = mergeDeep(existing, provider)
|
|
721
|
-
return
|
|
722
|
-
}
|
|
723
|
-
const match = database[providerID]
|
|
724
|
-
if (!match) return
|
|
725
|
-
// @ts-expect-error
|
|
726
|
-
providers[providerID] = mergeDeep(match, provider)
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
// extend database from config
|
|
730
|
-
for (const [providerID, provider] of configProviders) {
|
|
731
|
-
const existing = database[providerID]
|
|
732
|
-
const parsed: Info = {
|
|
733
|
-
id: providerID,
|
|
734
|
-
name: provider.name ?? existing?.name ?? providerID,
|
|
735
|
-
env: provider.env ?? existing?.env ?? [],
|
|
736
|
-
options: mergeDeep(existing?.options ?? {}, provider.options ?? {}),
|
|
737
|
-
source: "config",
|
|
738
|
-
models: existing?.models ?? {},
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
for (const [modelID, model] of Object.entries(provider.models ?? {})) {
|
|
742
|
-
const existingModel = parsed.models[model.id ?? modelID]
|
|
743
|
-
const name = iife(() => {
|
|
744
|
-
if (model.name) return model.name
|
|
745
|
-
if (model.id && model.id !== modelID) return modelID
|
|
746
|
-
return existingModel?.name ?? modelID
|
|
747
|
-
})
|
|
748
|
-
const parsedModel: Model = {
|
|
749
|
-
id: modelID,
|
|
750
|
-
api: {
|
|
751
|
-
id: model.id ?? existingModel?.api.id ?? modelID,
|
|
752
|
-
npm:
|
|
753
|
-
model.provider?.npm ??
|
|
754
|
-
provider.npm ??
|
|
755
|
-
existingModel?.api.npm ??
|
|
756
|
-
modelsDev[providerID]?.npm ??
|
|
757
|
-
"@ai-sdk/openai-compatible",
|
|
758
|
-
url: provider?.api ?? existingModel?.api.url ?? modelsDev[providerID]?.api,
|
|
759
|
-
},
|
|
760
|
-
status: model.status ?? existingModel?.status ?? "active",
|
|
761
|
-
name,
|
|
762
|
-
providerID,
|
|
763
|
-
capabilities: {
|
|
764
|
-
temperature: model.temperature ?? existingModel?.capabilities.temperature ?? false,
|
|
765
|
-
reasoning: model.reasoning ?? existingModel?.capabilities.reasoning ?? false,
|
|
766
|
-
attachment: model.attachment ?? existingModel?.capabilities.attachment ?? false,
|
|
767
|
-
toolcall: model.tool_call ?? existingModel?.capabilities.toolcall ?? true,
|
|
768
|
-
input: {
|
|
769
|
-
text: model.modalities?.input?.includes("text") ?? existingModel?.capabilities.input.text ?? true,
|
|
770
|
-
audio: model.modalities?.input?.includes("audio") ?? existingModel?.capabilities.input.audio ?? false,
|
|
771
|
-
image: model.modalities?.input?.includes("image") ?? existingModel?.capabilities.input.image ?? false,
|
|
772
|
-
video: model.modalities?.input?.includes("video") ?? existingModel?.capabilities.input.video ?? false,
|
|
773
|
-
pdf: model.modalities?.input?.includes("pdf") ?? existingModel?.capabilities.input.pdf ?? false,
|
|
774
|
-
},
|
|
775
|
-
output: {
|
|
776
|
-
text: model.modalities?.output?.includes("text") ?? existingModel?.capabilities.output.text ?? true,
|
|
777
|
-
audio: model.modalities?.output?.includes("audio") ?? existingModel?.capabilities.output.audio ?? false,
|
|
778
|
-
image: model.modalities?.output?.includes("image") ?? existingModel?.capabilities.output.image ?? false,
|
|
779
|
-
video: model.modalities?.output?.includes("video") ?? existingModel?.capabilities.output.video ?? false,
|
|
780
|
-
pdf: model.modalities?.output?.includes("pdf") ?? existingModel?.capabilities.output.pdf ?? false,
|
|
781
|
-
},
|
|
782
|
-
interleaved: model.interleaved ?? false,
|
|
783
|
-
},
|
|
784
|
-
cost: {
|
|
785
|
-
input: model?.cost?.input ?? existingModel?.cost?.input ?? 0,
|
|
786
|
-
output: model?.cost?.output ?? existingModel?.cost?.output ?? 0,
|
|
787
|
-
cache: {
|
|
788
|
-
read: model?.cost?.cache_read ?? existingModel?.cost?.cache.read ?? 0,
|
|
789
|
-
write: model?.cost?.cache_write ?? existingModel?.cost?.cache.write ?? 0,
|
|
790
|
-
},
|
|
791
|
-
},
|
|
792
|
-
options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
|
|
793
|
-
limit: {
|
|
794
|
-
context: model.limit?.context ?? existingModel?.limit?.context ?? 0,
|
|
795
|
-
output: model.limit?.output ?? existingModel?.limit?.output ?? 0,
|
|
796
|
-
},
|
|
797
|
-
headers: mergeDeep(existingModel?.headers ?? {}, model.headers ?? {}),
|
|
798
|
-
family: model.family ?? existingModel?.family ?? "",
|
|
799
|
-
release_date: model.release_date ?? existingModel?.release_date ?? "",
|
|
800
|
-
variants: {},
|
|
801
|
-
}
|
|
802
|
-
const merged = mergeDeep(ProviderTransform.variants(parsedModel), model.variants ?? {})
|
|
803
|
-
parsedModel.variants = mapValues(
|
|
804
|
-
pickBy(merged, (v) => !v.disabled),
|
|
805
|
-
(v) => omit(v, ["disabled"]),
|
|
806
|
-
)
|
|
807
|
-
parsed.models[modelID] = parsedModel
|
|
808
|
-
}
|
|
809
|
-
database[providerID] = parsed
|
|
810
|
-
}
|
|
811
|
-
|
|
812
|
-
// load env
|
|
813
|
-
const env = Env.all()
|
|
814
|
-
for (const [providerID, provider] of Object.entries(database)) {
|
|
815
|
-
if (disabled.has(providerID)) continue
|
|
816
|
-
const apiKey = provider.env.map((item) => env[item]).find(Boolean)
|
|
817
|
-
if (!apiKey) continue
|
|
818
|
-
mergeProvider(providerID, {
|
|
819
|
-
source: "env",
|
|
820
|
-
key: provider.env.length === 1 ? apiKey : undefined,
|
|
821
|
-
})
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
// load apikeys
|
|
825
|
-
for (const [providerID, provider] of Object.entries(await Auth.all())) {
|
|
826
|
-
if (disabled.has(providerID)) continue
|
|
827
|
-
if (provider.type === "api") {
|
|
828
|
-
mergeProvider(providerID, {
|
|
829
|
-
source: "api",
|
|
830
|
-
key: provider.key,
|
|
831
|
-
})
|
|
832
|
-
}
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
for (const plugin of await Plugin.list()) {
|
|
836
|
-
if (!plugin.auth) continue
|
|
837
|
-
const providerID = plugin.auth.provider
|
|
838
|
-
if (disabled.has(providerID)) continue
|
|
839
|
-
|
|
840
|
-
// For github-copilot plugin, check if auth exists for either github-copilot or github-copilot-enterprise
|
|
841
|
-
let hasAuth = false
|
|
842
|
-
const auth = await Auth.get(providerID)
|
|
843
|
-
if (auth) hasAuth = true
|
|
844
|
-
|
|
845
|
-
// Special handling for github-copilot: also check for enterprise auth
|
|
846
|
-
if (providerID === "github-copilot" && !hasAuth) {
|
|
847
|
-
const enterpriseAuth = await Auth.get("github-copilot-enterprise")
|
|
848
|
-
if (enterpriseAuth) hasAuth = true
|
|
849
|
-
}
|
|
850
|
-
|
|
851
|
-
if (!hasAuth) continue
|
|
852
|
-
if (!plugin.auth.loader) continue
|
|
853
|
-
|
|
854
|
-
// Load for the main provider if auth exists
|
|
855
|
-
if (auth) {
|
|
856
|
-
const options = await plugin.auth.loader(() => Auth.get(providerID) as any, database[plugin.auth.provider])
|
|
857
|
-
mergeProvider(plugin.auth.provider, {
|
|
858
|
-
source: "custom",
|
|
859
|
-
options: options,
|
|
860
|
-
})
|
|
861
|
-
}
|
|
862
|
-
|
|
863
|
-
// If this is github-copilot plugin, also register for github-copilot-enterprise if auth exists
|
|
864
|
-
if (providerID === "github-copilot") {
|
|
865
|
-
const enterpriseProviderID = "github-copilot-enterprise"
|
|
866
|
-
if (!disabled.has(enterpriseProviderID)) {
|
|
867
|
-
const enterpriseAuth = await Auth.get(enterpriseProviderID)
|
|
868
|
-
if (enterpriseAuth) {
|
|
869
|
-
const enterpriseOptions = await plugin.auth.loader(
|
|
870
|
-
() => Auth.get(enterpriseProviderID) as any,
|
|
871
|
-
database[enterpriseProviderID],
|
|
872
|
-
)
|
|
873
|
-
mergeProvider(enterpriseProviderID, {
|
|
874
|
-
source: "custom",
|
|
875
|
-
options: enterpriseOptions,
|
|
876
|
-
})
|
|
877
|
-
}
|
|
878
|
-
}
|
|
879
|
-
}
|
|
880
|
-
}
|
|
881
|
-
|
|
882
|
-
for (const [providerID, fn] of Object.entries(CUSTOM_LOADERS)) {
|
|
883
|
-
if (disabled.has(providerID)) continue
|
|
884
|
-
const data = database[providerID]
|
|
885
|
-
if (!data) {
|
|
886
|
-
log.error("Provider does not exist in model list " + providerID)
|
|
887
|
-
continue
|
|
888
|
-
}
|
|
889
|
-
const result = await fn(data)
|
|
890
|
-
if (result && (result.autoload || providers[providerID])) {
|
|
891
|
-
if (result.getModel) modelLoaders[providerID] = result.getModel
|
|
892
|
-
mergeProvider(providerID, {
|
|
893
|
-
source: "custom",
|
|
894
|
-
options: result.options,
|
|
895
|
-
})
|
|
896
|
-
}
|
|
897
|
-
}
|
|
898
|
-
|
|
899
|
-
// load config
|
|
900
|
-
for (const [providerID, provider] of configProviders) {
|
|
901
|
-
const partial: Partial<Info> = { source: "config" }
|
|
902
|
-
if (provider.env) partial.env = provider.env
|
|
903
|
-
if (provider.name) partial.name = provider.name
|
|
904
|
-
if (provider.options) partial.options = provider.options
|
|
905
|
-
mergeProvider(providerID, partial)
|
|
906
|
-
}
|
|
907
|
-
|
|
908
|
-
for (const [providerID, provider] of Object.entries(providers)) {
|
|
909
|
-
if (!isProviderAllowed(providerID)) {
|
|
910
|
-
delete providers[providerID]
|
|
911
|
-
continue
|
|
912
|
-
}
|
|
913
|
-
|
|
914
|
-
const configProvider = config.provider?.[providerID]
|
|
915
|
-
|
|
916
|
-
for (const [modelID, model] of Object.entries(provider.models)) {
|
|
917
|
-
model.api.id = model.api.id ?? model.id ?? modelID
|
|
918
|
-
if (modelID === "gpt-5-chat-latest" || (providerID === "openrouter" && modelID === "openai/gpt-5-chat"))
|
|
919
|
-
delete provider.models[modelID]
|
|
920
|
-
if (model.status === "alpha" && !Flag.OPENCODE_ENABLE_EXPERIMENTAL_MODELS) delete provider.models[modelID]
|
|
921
|
-
if (model.status === "deprecated") delete provider.models[modelID]
|
|
922
|
-
if (
|
|
923
|
-
(configProvider?.blacklist && configProvider.blacklist.includes(modelID)) ||
|
|
924
|
-
(configProvider?.whitelist && !configProvider.whitelist.includes(modelID))
|
|
925
|
-
)
|
|
926
|
-
delete provider.models[modelID]
|
|
927
|
-
|
|
928
|
-
// Filter out disabled variants from config
|
|
929
|
-
const configVariants = configProvider?.models?.[modelID]?.variants
|
|
930
|
-
if (configVariants && model.variants) {
|
|
931
|
-
const merged = mergeDeep(model.variants, configVariants)
|
|
932
|
-
model.variants = mapValues(
|
|
933
|
-
pickBy(merged, (v) => !v.disabled),
|
|
934
|
-
(v) => omit(v, ["disabled"]),
|
|
935
|
-
)
|
|
936
|
-
}
|
|
937
|
-
}
|
|
938
|
-
|
|
939
|
-
if (Object.keys(provider.models).length === 0) {
|
|
940
|
-
delete providers[providerID]
|
|
941
|
-
continue
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
log.info("found", { providerID })
|
|
945
|
-
}
|
|
946
|
-
|
|
947
|
-
return {
|
|
948
|
-
models: languages,
|
|
949
|
-
providers,
|
|
950
|
-
sdk,
|
|
951
|
-
modelLoaders,
|
|
952
|
-
}
|
|
953
|
-
})
|
|
954
|
-
|
|
955
|
-
export async function list() {
|
|
956
|
-
return state().then((state) => state.providers)
|
|
957
|
-
}
|
|
958
|
-
|
|
959
|
-
async function getSDK(model: Model) {
|
|
960
|
-
try {
|
|
961
|
-
using _ = log.time("getSDK", {
|
|
962
|
-
providerID: model.providerID,
|
|
963
|
-
})
|
|
964
|
-
const s = await state()
|
|
965
|
-
const provider = s.providers[model.providerID]
|
|
966
|
-
const options = { ...provider.options }
|
|
967
|
-
|
|
968
|
-
if (model.api.npm.includes("@ai-sdk/openai-compatible") && options["includeUsage"] !== false) {
|
|
969
|
-
options["includeUsage"] = true
|
|
970
|
-
}
|
|
971
|
-
|
|
972
|
-
if (!options["baseURL"]) options["baseURL"] = model.api.url
|
|
973
|
-
if (options["apiKey"] === undefined && provider.key) options["apiKey"] = provider.key
|
|
974
|
-
if (model.headers)
|
|
975
|
-
options["headers"] = {
|
|
976
|
-
...options["headers"],
|
|
977
|
-
...model.headers,
|
|
978
|
-
}
|
|
979
|
-
|
|
980
|
-
const key = Bun.hash.xxHash32(JSON.stringify({ npm: model.api.npm, options }))
|
|
981
|
-
const existing = s.sdk.get(key)
|
|
982
|
-
if (existing) return existing
|
|
983
|
-
|
|
984
|
-
const customFetch = options["fetch"]
|
|
985
|
-
|
|
986
|
-
options["fetch"] = async (input: any, init?: BunFetchRequestInit) => {
|
|
987
|
-
// Preserve custom fetch if it exists, wrap it with timeout logic
|
|
988
|
-
const fetchFn = customFetch ?? fetch
|
|
989
|
-
const opts = init ?? {}
|
|
990
|
-
|
|
991
|
-
if (options["timeout"] !== undefined && options["timeout"] !== null) {
|
|
992
|
-
const signals: AbortSignal[] = []
|
|
993
|
-
if (opts.signal) signals.push(opts.signal)
|
|
994
|
-
if (options["timeout"] !== false) signals.push(AbortSignal.timeout(options["timeout"]))
|
|
995
|
-
|
|
996
|
-
const combined = signals.length > 1 ? AbortSignal.any(signals) : signals[0]
|
|
997
|
-
|
|
998
|
-
opts.signal = combined
|
|
999
|
-
}
|
|
1000
|
-
|
|
1001
|
-
// Strip openai itemId metadata following what codex does
|
|
1002
|
-
// Codex uses #[serde(skip_serializing)] on id fields for all item types:
|
|
1003
|
-
// Message, Reasoning, FunctionCall, LocalShellCall, CustomToolCall, WebSearchCall
|
|
1004
|
-
// IDs are only re-attached for Azure with store=true
|
|
1005
|
-
if (model.api.npm === "@ai-sdk/openai" && opts.body && opts.method === "POST") {
|
|
1006
|
-
const body = JSON.parse(opts.body as string)
|
|
1007
|
-
const isAzure = model.providerID.includes("azure")
|
|
1008
|
-
const keepIds = isAzure && body.store === true
|
|
1009
|
-
if (!keepIds && Array.isArray(body.input)) {
|
|
1010
|
-
for (const item of body.input) {
|
|
1011
|
-
if ("id" in item) {
|
|
1012
|
-
delete item.id
|
|
1013
|
-
}
|
|
1014
|
-
}
|
|
1015
|
-
opts.body = JSON.stringify(body)
|
|
1016
|
-
}
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
|
-
return fetchFn(input, {
|
|
1020
|
-
...opts,
|
|
1021
|
-
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682
|
|
1022
|
-
timeout: false,
|
|
1023
|
-
})
|
|
1024
|
-
}
|
|
1025
|
-
|
|
1026
|
-
// Special case: google-vertex-anthropic uses a subpath import
|
|
1027
|
-
const bundledKey =
|
|
1028
|
-
model.providerID === "google-vertex-anthropic" ? "@ai-sdk/google-vertex/anthropic" : model.api.npm
|
|
1029
|
-
const bundledFn = BUNDLED_PROVIDERS[bundledKey]
|
|
1030
|
-
if (bundledFn) {
|
|
1031
|
-
log.info("using bundled provider", { providerID: model.providerID, pkg: bundledKey })
|
|
1032
|
-
const loaded = bundledFn({
|
|
1033
|
-
name: model.providerID,
|
|
1034
|
-
...options,
|
|
1035
|
-
})
|
|
1036
|
-
s.sdk.set(key, loaded)
|
|
1037
|
-
return loaded as SDK
|
|
1038
|
-
}
|
|
1039
|
-
|
|
1040
|
-
let installedPath: string
|
|
1041
|
-
if (!model.api.npm.startsWith("file://")) {
|
|
1042
|
-
installedPath = await BunProc.install(model.api.npm, "latest")
|
|
1043
|
-
} else {
|
|
1044
|
-
log.info("loading local provider", { pkg: model.api.npm })
|
|
1045
|
-
installedPath = model.api.npm
|
|
1046
|
-
}
|
|
1047
|
-
|
|
1048
|
-
const mod = await import(installedPath)
|
|
1049
|
-
|
|
1050
|
-
const fn = mod[Object.keys(mod).find((key) => key.startsWith("create"))!]
|
|
1051
|
-
const loaded = fn({
|
|
1052
|
-
name: model.providerID,
|
|
1053
|
-
...options,
|
|
1054
|
-
})
|
|
1055
|
-
s.sdk.set(key, loaded)
|
|
1056
|
-
return loaded as SDK
|
|
1057
|
-
} catch (e) {
|
|
1058
|
-
throw new InitError({ providerID: model.providerID }, { cause: e })
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
export async function getProvider(providerID: string) {
|
|
1063
|
-
return state().then((s) => s.providers[providerID])
|
|
1064
|
-
}
|
|
1065
|
-
|
|
1066
|
-
export async function getModel(providerID: string, modelID: string) {
|
|
1067
|
-
const s = await state()
|
|
1068
|
-
const provider = s.providers[providerID]
|
|
1069
|
-
if (!provider) {
|
|
1070
|
-
const availableProviders = Object.keys(s.providers)
|
|
1071
|
-
const matches = fuzzysort.go(providerID, availableProviders, { limit: 3, threshold: -10000 })
|
|
1072
|
-
const suggestions = matches.map((m) => m.target)
|
|
1073
|
-
throw new ModelNotFoundError({ providerID, modelID, suggestions })
|
|
1074
|
-
}
|
|
1075
|
-
|
|
1076
|
-
const info = provider.models[modelID]
|
|
1077
|
-
if (!info) {
|
|
1078
|
-
const availableModels = Object.keys(provider.models)
|
|
1079
|
-
const matches = fuzzysort.go(modelID, availableModels, { limit: 3, threshold: -10000 })
|
|
1080
|
-
const suggestions = matches.map((m) => m.target)
|
|
1081
|
-
throw new ModelNotFoundError({ providerID, modelID, suggestions })
|
|
1082
|
-
}
|
|
1083
|
-
return info
|
|
1084
|
-
}
|
|
1085
|
-
|
|
1086
|
-
export async function getLanguage(model: Model): Promise<LanguageModelV2> {
|
|
1087
|
-
const s = await state()
|
|
1088
|
-
const key = `${model.providerID}/${model.id}`
|
|
1089
|
-
if (s.models.has(key)) return s.models.get(key)!
|
|
1090
|
-
|
|
1091
|
-
const provider = s.providers[model.providerID]
|
|
1092
|
-
const sdk = await getSDK(model)
|
|
1093
|
-
|
|
1094
|
-
try {
|
|
1095
|
-
const language = s.modelLoaders[model.providerID]
|
|
1096
|
-
? await s.modelLoaders[model.providerID](sdk, model.api.id, provider.options)
|
|
1097
|
-
: sdk.languageModel(model.api.id)
|
|
1098
|
-
s.models.set(key, language)
|
|
1099
|
-
return language
|
|
1100
|
-
} catch (e) {
|
|
1101
|
-
if (e instanceof NoSuchModelError)
|
|
1102
|
-
throw new ModelNotFoundError(
|
|
1103
|
-
{
|
|
1104
|
-
modelID: model.id,
|
|
1105
|
-
providerID: model.providerID,
|
|
1106
|
-
},
|
|
1107
|
-
{ cause: e },
|
|
1108
|
-
)
|
|
1109
|
-
throw e
|
|
1110
|
-
}
|
|
1111
|
-
}
|
|
1112
|
-
|
|
1113
|
-
export async function closest(providerID: string, query: string[]) {
|
|
1114
|
-
const s = await state()
|
|
1115
|
-
const provider = s.providers[providerID]
|
|
1116
|
-
if (!provider) return undefined
|
|
1117
|
-
for (const item of query) {
|
|
1118
|
-
for (const modelID of Object.keys(provider.models)) {
|
|
1119
|
-
if (modelID.includes(item))
|
|
1120
|
-
return {
|
|
1121
|
-
providerID,
|
|
1122
|
-
modelID,
|
|
1123
|
-
}
|
|
1124
|
-
}
|
|
1125
|
-
}
|
|
1126
|
-
}
|
|
1127
|
-
|
|
1128
|
-
export async function getSmallModel(providerID: string) {
|
|
1129
|
-
const cfg = await Config.get()
|
|
1130
|
-
|
|
1131
|
-
if (cfg.small_model) {
|
|
1132
|
-
const parsed = parseModel(cfg.small_model)
|
|
1133
|
-
return getModel(parsed.providerID, parsed.modelID)
|
|
1134
|
-
}
|
|
1135
|
-
|
|
1136
|
-
const provider = await state().then((state) => state.providers[providerID])
|
|
1137
|
-
if (provider) {
|
|
1138
|
-
let priority = [
|
|
1139
|
-
"claude-haiku-4-5",
|
|
1140
|
-
"claude-haiku-4.5",
|
|
1141
|
-
"3-5-haiku",
|
|
1142
|
-
"3.5-haiku",
|
|
1143
|
-
"gemini-3-flash",
|
|
1144
|
-
"gemini-2.5-flash",
|
|
1145
|
-
"gpt-5-nano",
|
|
1146
|
-
]
|
|
1147
|
-
if (providerID.startsWith("opencode")) {
|
|
1148
|
-
priority = ["gpt-5-nano"]
|
|
1149
|
-
}
|
|
1150
|
-
if (providerID.startsWith("github-copilot")) {
|
|
1151
|
-
// prioritize free models for github copilot
|
|
1152
|
-
priority = ["gpt-5-mini", "claude-haiku-4.5", ...priority]
|
|
1153
|
-
}
|
|
1154
|
-
for (const item of priority) {
|
|
1155
|
-
for (const model of Object.keys(provider.models)) {
|
|
1156
|
-
if (model.includes(item)) return getModel(providerID, model)
|
|
1157
|
-
}
|
|
1158
|
-
}
|
|
1159
|
-
}
|
|
1160
|
-
|
|
1161
|
-
// Check if opencode provider is available before using it
|
|
1162
|
-
const opencodeProvider = await state().then((state) => state.providers["opencode"])
|
|
1163
|
-
if (opencodeProvider && opencodeProvider.models["gpt-5-nano"]) {
|
|
1164
|
-
return getModel("opencode", "gpt-5-nano")
|
|
1165
|
-
}
|
|
1166
|
-
|
|
1167
|
-
return undefined
|
|
1168
|
-
}
|
|
1169
|
-
|
|
1170
|
-
const priority = ["gpt-5", "claude-sonnet-4", "big-pickle", "gemini-3-pro"]
|
|
1171
|
-
export function sort(models: Model[]) {
|
|
1172
|
-
return sortBy(
|
|
1173
|
-
models,
|
|
1174
|
-
[(model) => priority.findIndex((filter) => model.id.includes(filter)), "desc"],
|
|
1175
|
-
[(model) => (model.id.includes("latest") ? 0 : 1), "asc"],
|
|
1176
|
-
[(model) => model.id, "desc"],
|
|
1177
|
-
)
|
|
1178
|
-
}
|
|
1179
|
-
|
|
1180
|
-
export async function defaultModel() {
|
|
1181
|
-
const cfg = await Config.get()
|
|
1182
|
-
if (cfg.model) return parseModel(cfg.model)
|
|
1183
|
-
|
|
1184
|
-
const provider = await list()
|
|
1185
|
-
.then((val) => Object.values(val))
|
|
1186
|
-
.then((x) => x.find((p) => !cfg.provider || Object.keys(cfg.provider).includes(p.id)))
|
|
1187
|
-
if (!provider) throw new Error("no providers found")
|
|
1188
|
-
const [model] = sort(Object.values(provider.models))
|
|
1189
|
-
if (!model) throw new Error("no models found")
|
|
1190
|
-
return {
|
|
1191
|
-
providerID: provider.id,
|
|
1192
|
-
modelID: model.id,
|
|
1193
|
-
}
|
|
1194
|
-
}
|
|
1195
|
-
|
|
1196
|
-
export function parseModel(model: string) {
|
|
1197
|
-
const [providerID, ...rest] = model.split("/")
|
|
1198
|
-
return {
|
|
1199
|
-
providerID: providerID,
|
|
1200
|
-
modelID: rest.join("/"),
|
|
1201
|
-
}
|
|
1202
|
-
}
|
|
1203
|
-
|
|
1204
|
-
export const ModelNotFoundError = NamedError.create(
|
|
1205
|
-
"ProviderModelNotFoundError",
|
|
1206
|
-
z.object({
|
|
1207
|
-
providerID: z.string(),
|
|
1208
|
-
modelID: z.string(),
|
|
1209
|
-
suggestions: z.array(z.string()).optional(),
|
|
1210
|
-
}),
|
|
1211
|
-
)
|
|
1212
|
-
|
|
1213
|
-
export const InitError = NamedError.create(
|
|
1214
|
-
"ProviderInitError",
|
|
1215
|
-
z.object({
|
|
1216
|
-
providerID: z.string(),
|
|
1217
|
-
}),
|
|
1218
|
-
)
|
|
1219
|
-
}
|