@miaws/miaw 1.18.3 → 1.18.5
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 +131 -0
- package/Dockerfile +18 -0
- package/README.md +15 -0
- package/{miaw.js → bin/miaw} +0 -0
- package/bunfig.toml +7 -0
- package/git +0 -0
- package/migration/20260511173437_session-metadata/migration.sql +1 -0
- package/migration/20260511173437_session-metadata/snapshot.json +1500 -0
- package/package.json +154 -12
- package/parsers-config.ts +1 -0
- package/script/bench-search.ts +94 -0
- package/script/bench-test-suite.ts +52 -0
- package/script/build.ts +243 -0
- package/script/generate.ts +14 -0
- package/script/httpapi-exercise.ts +1 -0
- package/script/postinstall.mjs +189 -0
- package/script/profile-test-files.ts +42 -0
- package/script/publish.ts +213 -0
- package/script/run-workspace-server +106 -0
- package/script/schema.ts +77 -0
- package/script/time.ts +6 -0
- package/script/trace-imports.ts +153 -0
- package/specs/effect/error-boundaries-plan.md +235 -0
- package/specs/effect/errors.md +207 -0
- package/specs/effect/facades.md +218 -0
- package/specs/effect/guide.md +247 -0
- package/specs/effect/instance-context.md +13 -0
- package/specs/effect/loose-ends.md +30 -0
- package/specs/effect/migration.md +62 -0
- package/specs/effect/routes.md +61 -0
- package/specs/effect/schema.md +88 -0
- package/specs/effect/server-package.md +58 -0
- package/specs/effect/todo.md +241 -0
- package/specs/effect/tools.md +88 -0
- package/specs/openapi-translation-cleanup.md +204 -0
- package/specs/tui-plugins.md +544 -0
- package/specs/v2/api.ts +67 -0
- package/specs/v2/message-shape.md +136 -0
- package/specs/v2/notifications.md +13 -0
- package/specs/v2/tui-command-shim.md +67 -0
- package/src/account/account.ts +463 -0
- package/src/account/repo.ts +173 -0
- package/src/account/schema.ts +99 -0
- package/src/account/url.ts +8 -0
- package/src/acp/agent.ts +95 -0
- package/src/acp/config-option.ts +203 -0
- package/src/acp/content.ts +250 -0
- package/src/acp/directory.ts +210 -0
- package/src/acp/error.ts +90 -0
- package/src/acp/event.ts +336 -0
- package/src/acp/permission.ts +124 -0
- package/src/acp/profile.ts +42 -0
- package/src/acp/service.ts +1048 -0
- package/src/acp/session.ts +231 -0
- package/src/acp/tool.ts +321 -0
- package/src/acp/usage.ts +232 -0
- package/src/agent/agent.ts +467 -0
- package/src/agent/generate.txt +75 -0
- package/src/agent/prompt/compaction.txt +9 -0
- package/src/agent/prompt/explore.txt +18 -0
- package/src/agent/prompt/summary.txt +11 -0
- package/src/agent/prompt/title.txt +44 -0
- package/src/agent/subagent-permissions.ts +27 -0
- package/src/audio.d.ts +14 -0
- package/src/auth/index.ts +99 -0
- package/src/background/job.ts +39 -0
- package/src/bus/global.ts +22 -0
- package/src/cli/bootstrap.ts +11 -0
- package/src/cli/cmd/account.ts +264 -0
- package/src/cli/cmd/acp.ts +73 -0
- package/src/cli/cmd/agent.ts +253 -0
- package/src/cli/cmd/attach.ts +97 -0
- package/src/cli/cmd/cmd.ts +7 -0
- package/src/cli/cmd/db.ts +62 -0
- package/src/cli/cmd/debug/agent.handler.ts +193 -0
- package/src/cli/cmd/debug/agent.ts +27 -0
- package/src/cli/cmd/debug/config.ts +14 -0
- package/src/cli/cmd/debug/file.ts +73 -0
- package/src/cli/cmd/debug/index.ts +87 -0
- package/src/cli/cmd/debug/lsp.ts +50 -0
- package/src/cli/cmd/debug/ripgrep.ts +79 -0
- package/src/cli/cmd/debug/scrap.ts +15 -0
- package/src/cli/cmd/debug/skill.ts +15 -0
- package/src/cli/cmd/debug/snapshot.ts +50 -0
- package/src/cli/cmd/debug/startup.ts +11 -0
- package/src/cli/cmd/debug/v2.ts +49 -0
- package/src/cli/cmd/export.ts +292 -0
- package/src/cli/cmd/generate.ts +54 -0
- package/src/cli/cmd/github.handler.ts +1593 -0
- package/src/cli/cmd/github.shared.ts +30 -0
- package/src/cli/cmd/github.ts +42 -0
- package/src/cli/cmd/import.ts +224 -0
- package/src/cli/cmd/mcp.ts +849 -0
- package/src/cli/cmd/models.ts +66 -0
- package/src/cli/cmd/plug.ts +230 -0
- package/src/cli/cmd/pr.ts +115 -0
- package/src/cli/cmd/prompt-display.ts +1 -0
- package/src/cli/cmd/providers.ts +534 -0
- package/src/cli/cmd/run/demo.ts +1274 -0
- package/src/cli/cmd/run/entry.body.ts +205 -0
- package/src/cli/cmd/run/footer.command.tsx +1064 -0
- package/src/cli/cmd/run/footer.menu.tsx +351 -0
- package/src/cli/cmd/run/footer.permission.tsx +472 -0
- package/src/cli/cmd/run/footer.prompt.tsx +1306 -0
- package/src/cli/cmd/run/footer.question.tsx +573 -0
- package/src/cli/cmd/run/footer.subagent.tsx +173 -0
- package/src/cli/cmd/run/footer.ts +1129 -0
- package/src/cli/cmd/run/footer.view.tsx +943 -0
- package/src/cli/cmd/run/footer.width.ts +27 -0
- package/src/cli/cmd/run/permission.shared.ts +256 -0
- package/src/cli/cmd/run/prompt.editor.ts +157 -0
- package/src/cli/cmd/run/prompt.shared.ts +153 -0
- package/src/cli/cmd/run/question.shared.ts +340 -0
- package/src/cli/cmd/run/runtime.boot.ts +202 -0
- package/src/cli/cmd/run/runtime.lifecycle.ts +406 -0
- package/src/cli/cmd/run/runtime.queue.ts +349 -0
- package/src/cli/cmd/run/runtime.shared.ts +17 -0
- package/src/cli/cmd/run/runtime.stdin.ts +37 -0
- package/src/cli/cmd/run/runtime.ts +814 -0
- package/src/cli/cmd/run/scrollback.shared.ts +92 -0
- package/src/cli/cmd/run/scrollback.surface.ts +431 -0
- package/src/cli/cmd/run/scrollback.writer.tsx +352 -0
- package/src/cli/cmd/run/session-data.ts +1113 -0
- package/src/cli/cmd/run/session-replay.ts +374 -0
- package/src/cli/cmd/run/session.shared.ts +196 -0
- package/src/cli/cmd/run/splash.ts +280 -0
- package/src/cli/cmd/run/stream.transport.ts +1462 -0
- package/src/cli/cmd/run/stream.ts +175 -0
- package/src/cli/cmd/run/subagent-data.ts +876 -0
- package/src/cli/cmd/run/theme.ts +690 -0
- package/src/cli/cmd/run/tool.ts +1489 -0
- package/src/cli/cmd/run/trace.ts +94 -0
- package/src/cli/cmd/run/turn-summary.ts +47 -0
- package/src/cli/cmd/run/types.ts +350 -0
- package/src/cli/cmd/run/variant.shared.ts +215 -0
- package/src/cli/cmd/run.ts +894 -0
- package/src/cli/cmd/serve.ts +24 -0
- package/src/cli/cmd/session.ts +147 -0
- package/src/cli/cmd/stats.ts +393 -0
- package/src/cli/cmd/tui.ts +224 -0
- package/src/cli/cmd/uninstall.ts +353 -0
- package/src/cli/cmd/upgrade.ts +74 -0
- package/src/cli/cmd/web.ts +84 -0
- package/src/cli/effect/prompt.ts +37 -0
- package/src/cli/effect-cmd.ts +96 -0
- package/src/cli/error.ts +130 -0
- package/src/cli/heap.ts +45 -0
- package/src/cli/logo.ts +1 -0
- package/src/cli/network.ts +64 -0
- package/src/cli/tui/layer.ts +7 -0
- package/src/cli/tui/validate-session.ts +29 -0
- package/src/cli/tui/worker.ts +71 -0
- package/src/cli/ui.ts +132 -0
- package/src/cli/upgrade.ts +53 -0
- package/src/command/index.ts +184 -0
- package/src/command/template/initialize.txt +66 -0
- package/src/command/template/review.txt +101 -0
- package/src/config/agent-preset.ts +175 -0
- package/src/config/agent.ts +59 -0
- package/src/config/command.ts +39 -0
- package/src/config/config.ts +703 -0
- package/src/config/entry-name.ts +19 -0
- package/src/config/managed.ts +69 -0
- package/src/config/markdown.ts +36 -0
- package/src/config/parse.ts +79 -0
- package/src/config/paths.ts +45 -0
- package/src/config/plugin.ts +79 -0
- package/src/config/tui-cwd.ts +5 -0
- package/src/config/tui-host-attention.ts +21 -0
- package/src/config/tui-migrate.ts +132 -0
- package/src/config/tui.ts +274 -0
- package/src/config/variable.ts +91 -0
- package/src/control-plane/adapters/index.ts +41 -0
- package/src/control-plane/adapters/worktree.ts +96 -0
- package/src/control-plane/dev/README.md +19 -0
- package/src/control-plane/dev/debug-workspace-plugin.ts +73 -0
- package/src/control-plane/types.ts +59 -0
- package/src/control-plane/util.ts +39 -0
- package/src/control-plane/workspace-adapter-runtime.ts +51 -0
- package/src/control-plane/workspace-context.ts +26 -0
- package/src/control-plane/workspace.ts +989 -0
- package/src/effect/app-runtime.ts +132 -0
- package/src/effect/bootstrap-runtime.ts +23 -0
- package/src/effect/bridge.ts +84 -0
- package/src/effect/config-service.ts +67 -0
- package/src/effect/instance-ref.ts +11 -0
- package/src/effect/instance-registry.ts +12 -0
- package/src/effect/instance-state.ts +69 -0
- package/src/effect/promise.ts +17 -0
- package/src/effect/run-service.ts +47 -0
- package/src/effect/runner.ts +217 -0
- package/src/effect/runtime-flags.ts +79 -0
- package/src/env/index.ts +43 -0
- package/src/event-v2-bridge.ts +79 -0
- package/src/format/formatter.ts +404 -0
- package/src/format/index.ts +205 -0
- package/src/git/index.ts +350 -0
- package/src/id/id.ts +80 -0
- package/src/ide/index.ts +61 -0
- package/src/image/image.ts +174 -0
- package/src/index.ts +142 -0
- package/src/installation/index.ts +350 -0
- package/src/lsp/client.ts +650 -0
- package/src/lsp/diagnostic.ts +29 -0
- package/src/lsp/language.ts +121 -0
- package/src/lsp/launch.ts +21 -0
- package/src/lsp/lsp.ts +511 -0
- package/src/lsp/server.ts +1983 -0
- package/src/markdown.d.ts +4 -0
- package/src/mcp/auth.ts +174 -0
- package/src/mcp/catalog.ts +144 -0
- package/src/mcp/index.ts +953 -0
- package/src/mcp/oauth-callback.ts +221 -0
- package/src/mcp/oauth-provider.ts +206 -0
- package/src/node.ts +4 -0
- package/src/patch/index.ts +686 -0
- package/src/permission/arity.ts +163 -0
- package/src/permission/evaluate.ts +1 -0
- package/src/permission/index.ts +230 -0
- package/src/plugin/azure.ts +26 -0
- package/src/plugin/cloudflare.ts +76 -0
- package/src/plugin/digitalocean.ts +383 -0
- package/src/plugin/github-copilot/copilot.ts +413 -0
- package/src/plugin/github-copilot/models.ts +246 -0
- package/src/plugin/index.ts +315 -0
- package/src/plugin/install.ts +439 -0
- package/src/plugin/loader.ts +237 -0
- package/src/plugin/meta.ts +188 -0
- package/src/plugin/openai/README.md +31 -0
- package/src/plugin/openai/codex.ts +640 -0
- package/src/plugin/openai/ws-pool.ts +270 -0
- package/src/plugin/openai/ws.ts +381 -0
- package/src/plugin/pty-environment.ts +24 -0
- package/src/plugin/shared.ts +323 -0
- package/src/plugin/snowflake-cortex.ts +529 -0
- package/src/plugin/tui/internal.ts +10 -0
- package/src/plugin/tui/runtime.ts +1130 -0
- package/src/plugin/xai.ts +734 -0
- package/src/project/bootstrap-service.ts +9 -0
- package/src/project/bootstrap.ts +76 -0
- package/src/project/instance-context.ts +24 -0
- package/src/project/instance-layer.ts +11 -0
- package/src/project/instance-runtime.ts +16 -0
- package/src/project/instance-store.ts +209 -0
- package/src/project/project.ts +519 -0
- package/src/project/vcs.ts +431 -0
- package/src/provider/auth.ts +233 -0
- package/src/provider/error.ts +188 -0
- package/src/provider/model-status.ts +8 -0
- package/src/provider/provider.ts +1975 -0
- package/src/provider/transform.ts +1426 -0
- package/src/question/index.ts +229 -0
- package/src/question/schema.ts +10 -0
- package/src/server/auth.ts +48 -0
- package/src/server/event.ts +13 -0
- package/src/server/global-lifecycle.ts +28 -0
- package/src/server/init-projectors.ts +3 -0
- package/src/server/mdns.ts +47 -0
- package/src/server/projectors.ts +1 -0
- package/src/server/proxy-util.ts +48 -0
- package/src/server/routes/instance/httpapi/AGENTS.md +39 -0
- package/src/server/routes/instance/httpapi/api.ts +78 -0
- package/src/server/routes/instance/httpapi/errors.ts +193 -0
- package/src/server/routes/instance/httpapi/groups/config.ts +65 -0
- package/src/server/routes/instance/httpapi/groups/control-plane.ts +35 -0
- package/src/server/routes/instance/httpapi/groups/control.ts +76 -0
- package/src/server/routes/instance/httpapi/groups/event.ts +29 -0
- package/src/server/routes/instance/httpapi/groups/experimental.ts +260 -0
- package/src/server/routes/instance/httpapi/groups/file.ts +185 -0
- package/src/server/routes/instance/httpapi/groups/global.ts +138 -0
- package/src/server/routes/instance/httpapi/groups/instance.ts +206 -0
- package/src/server/routes/instance/httpapi/groups/mcp.ts +156 -0
- package/src/server/routes/instance/httpapi/groups/metadata.ts +18 -0
- package/src/server/routes/instance/httpapi/groups/permission.ts +61 -0
- package/src/server/routes/instance/httpapi/groups/project-copy.ts +32 -0
- package/src/server/routes/instance/httpapi/groups/project.ts +93 -0
- package/src/server/routes/instance/httpapi/groups/provider.ts +101 -0
- package/src/server/routes/instance/httpapi/groups/pty.ts +172 -0
- package/src/server/routes/instance/httpapi/groups/query.ts +12 -0
- package/src/server/routes/instance/httpapi/groups/question.ts +74 -0
- package/src/server/routes/instance/httpapi/groups/session.ts +462 -0
- package/src/server/routes/instance/httpapi/groups/sync.ts +113 -0
- package/src/server/routes/instance/httpapi/groups/tui.ts +208 -0
- package/src/server/routes/instance/httpapi/groups/workspace.ts +141 -0
- package/src/server/routes/instance/httpapi/handlers/config.ts +34 -0
- package/src/server/routes/instance/httpapi/handlers/control-plane.ts +37 -0
- package/src/server/routes/instance/httpapi/handlers/control.ts +43 -0
- package/src/server/routes/instance/httpapi/handlers/event.ts +99 -0
- package/src/server/routes/instance/httpapi/handlers/experimental.ts +187 -0
- package/src/server/routes/instance/httpapi/handlers/file.ts +139 -0
- package/src/server/routes/instance/httpapi/handlers/global.ts +156 -0
- package/src/server/routes/instance/httpapi/handlers/instance.ts +110 -0
- package/src/server/routes/instance/httpapi/handlers/mcp.ts +111 -0
- package/src/server/routes/instance/httpapi/handlers/permission.ts +41 -0
- package/src/server/routes/instance/httpapi/handlers/project-copy.ts +83 -0
- package/src/server/routes/instance/httpapi/handlers/project.ts +63 -0
- package/src/server/routes/instance/httpapi/handlers/provider.ts +113 -0
- package/src/server/routes/instance/httpapi/handlers/pty.ts +273 -0
- package/src/server/routes/instance/httpapi/handlers/question.ts +54 -0
- package/src/server/routes/instance/httpapi/handlers/session-errors.ts +21 -0
- package/src/server/routes/instance/httpapi/handlers/session.ts +440 -0
- package/src/server/routes/instance/httpapi/handlers/sync.ts +89 -0
- package/src/server/routes/instance/httpapi/handlers/tui.ts +131 -0
- package/src/server/routes/instance/httpapi/handlers/workspace.ts +102 -0
- package/src/server/routes/instance/httpapi/lifecycle.ts +54 -0
- package/src/server/routes/instance/httpapi/middleware/authorization.ts +150 -0
- package/src/server/routes/instance/httpapi/middleware/compression.ts +64 -0
- package/src/server/routes/instance/httpapi/middleware/cors-vary.ts +29 -0
- package/src/server/routes/instance/httpapi/middleware/error.ts +43 -0
- package/src/server/routes/instance/httpapi/middleware/fence.ts +25 -0
- package/src/server/routes/instance/httpapi/middleware/instance-context.ts +43 -0
- package/src/server/routes/instance/httpapi/middleware/proxy.ts +108 -0
- package/src/server/routes/instance/httpapi/middleware/schema-error.ts +41 -0
- package/src/server/routes/instance/httpapi/middleware/workspace-routing.ts +250 -0
- package/src/server/routes/instance/httpapi/public.ts +535 -0
- package/src/server/routes/instance/httpapi/server.ts +298 -0
- package/src/server/routes/instance/httpapi/websocket-tracker.ts +57 -0
- package/src/server/server.ts +217 -0
- package/src/server/shared/fence.ts +60 -0
- package/src/server/shared/pty-ticket.ts +15 -0
- package/src/server/shared/public-ui.ts +12 -0
- package/src/server/shared/tui-control.ts +28 -0
- package/src/server/shared/ui.ts +108 -0
- package/src/server/shared/workspace-routing.ts +38 -0
- package/src/server/tui-event.ts +53 -0
- package/src/session/compaction.ts +620 -0
- package/src/session/instruction.ts +250 -0
- package/src/session/llm/AGENTS.md +90 -0
- package/src/session/llm/ai-sdk.ts +288 -0
- package/src/session/llm/native-request.ts +196 -0
- package/src/session/llm/native-runtime.ts +195 -0
- package/src/session/llm/request.ts +216 -0
- package/src/session/llm.ts +415 -0
- package/src/session/message-error.ts +14 -0
- package/src/session/message-v2.ts +744 -0
- package/src/session/message.ts +148 -0
- package/src/session/overflow.ts +34 -0
- package/src/session/processor.ts +1084 -0
- package/src/session/prompt/anthropic.txt +105 -0
- package/src/session/prompt/beast.txt +147 -0
- package/src/session/prompt/build-switch.txt +5 -0
- package/src/session/prompt/codex.txt +79 -0
- package/src/session/prompt/copilot-gpt-5.txt +143 -0
- package/src/session/prompt/default.txt +95 -0
- package/src/session/prompt/gemini.txt +155 -0
- package/src/session/prompt/gpt.txt +107 -0
- package/src/session/prompt/kimi.txt +95 -0
- package/src/session/prompt/max-steps.txt +16 -0
- package/src/session/prompt/plan-mode.txt +70 -0
- package/src/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/src/session/prompt/plan.txt +26 -0
- package/src/session/prompt/trinity.txt +97 -0
- package/src/session/prompt/ultrawork.txt +289 -0
- package/src/session/prompt.ts +1725 -0
- package/src/session/reminders.ts +92 -0
- package/src/session/retry.ts +201 -0
- package/src/session/revert.ts +160 -0
- package/src/session/run-state.ts +156 -0
- package/src/session/schema.ts +26 -0
- package/src/session/session.ts +1119 -0
- package/src/session/status.ts +97 -0
- package/src/session/summary.ts +165 -0
- package/src/session/system.ts +117 -0
- package/src/session/todo.ts +90 -0
- package/src/session/tools.ts +207 -0
- package/src/session/ultrawork.ts +26 -0
- package/src/share/session.ts +61 -0
- package/src/share/share-next.ts +385 -0
- package/src/skill/discovery.ts +109 -0
- package/src/skill/index.ts +366 -0
- package/src/snapshot/index.ts +808 -0
- package/src/sql.d.ts +4 -0
- package/src/storage/schema.ts +5 -0
- package/src/storage/storage.ts +329 -0
- package/src/sync/README.md +179 -0
- package/src/sync/schema.ts +11 -0
- package/src/temporary.ts +31 -0
- package/src/tool/apply_patch.ts +313 -0
- package/src/tool/apply_patch.txt +33 -0
- package/src/tool/edit.ts +737 -0
- package/src/tool/edit.txt +10 -0
- package/src/tool/external-directory.ts +49 -0
- package/src/tool/glob.ts +76 -0
- package/src/tool/glob.txt +6 -0
- package/src/tool/grep.ts +112 -0
- package/src/tool/grep.txt +8 -0
- package/src/tool/invalid.ts +21 -0
- package/src/tool/json-schema.ts +164 -0
- package/src/tool/lsp.ts +113 -0
- package/src/tool/lsp.txt +24 -0
- package/src/tool/mcp-websearch.ts +96 -0
- package/src/tool/plan-enter.txt +14 -0
- package/src/tool/plan-exit.txt +13 -0
- package/src/tool/plan.ts +79 -0
- package/src/tool/question.ts +44 -0
- package/src/tool/question.txt +10 -0
- package/src/tool/read.ts +386 -0
- package/src/tool/read.txt +14 -0
- package/src/tool/registry.ts +440 -0
- package/src/tool/schema.ts +14 -0
- package/src/tool/shell/id.ts +19 -0
- package/src/tool/shell/prompt.ts +307 -0
- package/src/tool/shell/shell.txt +21 -0
- package/src/tool/shell.ts +657 -0
- package/src/tool/skill.ts +71 -0
- package/src/tool/skill.txt +5 -0
- package/src/tool/task.ts +346 -0
- package/src/tool/task.txt +19 -0
- package/src/tool/todo.ts +57 -0
- package/src/tool/todowrite.txt +44 -0
- package/src/tool/tool.ts +183 -0
- package/src/tool/truncate.ts +158 -0
- package/src/tool/truncation-dir.ts +4 -0
- package/src/tool/webfetch.ts +192 -0
- package/src/tool/webfetch.txt +13 -0
- package/src/tool/websearch.ts +143 -0
- package/src/tool/websearch.txt +14 -0
- package/src/tool/write.ts +104 -0
- package/src/tool/write.txt +8 -0
- package/src/util/archive.ts +17 -0
- package/src/util/bom.ts +27 -0
- package/src/util/data-url.ts +9 -0
- package/src/util/defer.ts +10 -0
- package/src/util/effect-http-client.ts +11 -0
- package/src/util/error.ts +1 -0
- package/src/util/filesystem.ts +251 -0
- package/src/util/iife.ts +3 -0
- package/src/util/lazy.ts +20 -0
- package/src/util/local-context.ts +25 -0
- package/src/util/locale.ts +2 -0
- package/src/util/media.ts +26 -0
- package/src/util/process.ts +177 -0
- package/src/util/proxy-env.ts +72 -0
- package/src/util/queue.ts +32 -0
- package/src/util/record.ts +1 -0
- package/src/util/repository.ts +232 -0
- package/src/util/rpc.ts +66 -0
- package/src/util/signal.ts +12 -0
- package/src/util/timeout.ts +13 -0
- package/src/util/token.ts +1 -0
- package/src/util/wildcard.ts +59 -0
- package/src/worktree/index.ts +654 -0
- package/sst-env.d.ts +10 -0
- package/test/AGENTS.md +204 -0
- package/test/EFFECT_TEST_MIGRATION.md +169 -0
- package/test/account/repo.test.ts +353 -0
- package/test/account/service.test.ts +453 -0
- package/test/acp/config-option.test.ts +229 -0
- package/test/acp/content.test.ts +201 -0
- package/test/acp/directory.test.ts +186 -0
- package/test/acp/error.test.ts +67 -0
- package/test/acp/event.test.ts +743 -0
- package/test/acp/permission.test.ts +273 -0
- package/test/acp/service-session.test.ts +1174 -0
- package/test/acp/session.test.ts +200 -0
- package/test/acp/tool.test.ts +210 -0
- package/test/acp/usage.test.ts +315 -0
- package/test/agent/agent.test.ts +760 -0
- package/test/agent/plan-mode-subagent-bypass.test.ts +159 -0
- package/test/agent/plugin-agent-regression.test.ts +64 -0
- package/test/auth/auth.test.ts +77 -0
- package/test/background/job.test.ts +243 -0
- package/test/cli/account.test.ts +30 -0
- package/test/cli/acp/acp-test-client.ts +97 -0
- package/test/cli/acp/config-options.test.ts +103 -0
- package/test/cli/acp/helpers.ts +96 -0
- package/test/cli/acp/initialize-auth.test.ts +61 -0
- package/test/cli/acp/lifecycle.test.ts +118 -0
- package/test/cli/acp/prompt-content.test.ts +97 -0
- package/test/cli/acp/skills.test.ts +38 -0
- package/test/cli/cmd/tui/attention.test.ts +484 -0
- package/test/cli/effect-cmd-instance-als.test.ts +39 -0
- package/test/cli/error.test.ts +95 -0
- package/test/cli/github-action.test.ts +199 -0
- package/test/cli/github-remote.test.ts +90 -0
- package/test/cli/help/__snapshots__/help-snapshots.test.ts.snap +631 -0
- package/test/cli/help/help-snapshots.test.ts +137 -0
- package/test/cli/import.test.ts +54 -0
- package/test/cli/mcp-add.test.ts +74 -0
- package/test/cli/plugin-auth-picker.test.ts +120 -0
- package/test/cli/run/entry.body.test.ts +536 -0
- package/test/cli/run/footer.menu.test.ts +43 -0
- package/test/cli/run/footer.view.test.tsx +1375 -0
- package/test/cli/run/footer.width.test.ts +35 -0
- package/test/cli/run/permission.shared.test.ts +144 -0
- package/test/cli/run/prompt.editor.test.ts +101 -0
- package/test/cli/run/prompt.shared.test.ts +101 -0
- package/test/cli/run/question.shared.test.ts +115 -0
- package/test/cli/run/run-process.test.ts +84 -0
- package/test/cli/run/runtime.boot.test.ts +283 -0
- package/test/cli/run/runtime.queue.test.ts +481 -0
- package/test/cli/run/runtime.stdin.test.ts +71 -0
- package/test/cli/run/runtime.test.ts +238 -0
- package/test/cli/run/scrollback.surface.test.ts +1065 -0
- package/test/cli/run/session-data.test.ts +595 -0
- package/test/cli/run/session-replay.test.ts +692 -0
- package/test/cli/run/session.shared.test.ts +247 -0
- package/test/cli/run/stream.test.ts +56 -0
- package/test/cli/run/stream.transport.test.ts +2363 -0
- package/test/cli/run/subagent-data.test.ts +547 -0
- package/test/cli/run/theme.test.ts +177 -0
- package/test/cli/run/variant.shared.test.ts +217 -0
- package/test/cli/serve/serve-process.test.ts +61 -0
- package/test/cli/smokes/read-only.test.ts +115 -0
- package/test/cli/tui/attach.test.ts +11 -0
- package/test/cli/tui/editor-context-zed.test.ts +379 -0
- package/test/cli/tui/editor-context.test.tsx +297 -0
- package/test/cli/tui/plugin-add.test.ts +110 -0
- package/test/cli/tui/plugin-install.test.ts +87 -0
- package/test/cli/tui/plugin-lifecycle.test.ts +224 -0
- package/test/cli/tui/plugin-loader-entrypoint.test.ts +485 -0
- package/test/cli/tui/plugin-loader-pure.test.ts +72 -0
- package/test/cli/tui/plugin-loader.test.ts +1332 -0
- package/test/cli/tui/plugin-toggle.test.ts +264 -0
- package/test/cli/tui/thread.test.ts +36 -0
- package/test/config/agent-color.test.ts +47 -0
- package/test/config/config.test.ts +2041 -0
- package/test/config/entry-name.test.ts +57 -0
- package/test/config/fixtures/empty-frontmatter.md +4 -0
- package/test/config/fixtures/frontmatter.md +28 -0
- package/test/config/fixtures/markdown-header.md +11 -0
- package/test/config/fixtures/no-frontmatter.md +1 -0
- package/test/config/fixtures/weird-model-id.md +13 -0
- package/test/config/lsp.test.ts +69 -0
- package/test/config/markdown.test.ts +228 -0
- package/test/config/plugin.test.ts +0 -0
- package/test/config/tui.test.ts +886 -0
- package/test/control-plane/adapters.test.ts +71 -0
- package/test/control-plane/workspace.test.ts +1703 -0
- package/test/effect/app-graph-types.test.ts +108 -0
- package/test/effect/app-graph.test.ts +204 -0
- package/test/effect/app-runtime-logger.test.ts +99 -0
- package/test/effect/config-service.test.ts +65 -0
- package/test/effect/instance-state.test.ts +391 -0
- package/test/effect/run-service.test.ts +89 -0
- package/test/effect/runner.test.ts +514 -0
- package/test/effect/runtime-flags.test.ts +373 -0
- package/test/fake/account.ts +9 -0
- package/test/fake/auth.ts +8 -0
- package/test/fake/npm.ts +8 -0
- package/test/fake/provider.ts +82 -0
- package/test/fake/skill.ts +8 -0
- package/test/filesystem/filesystem.test.ts +319 -0
- package/test/fixture/agent-plugin.constants.ts +6 -0
- package/test/fixture/agent-plugin.ts +12 -0
- package/test/fixture/config.ts +23 -0
- package/test/fixture/db.ts +11 -0
- package/test/fixture/fixture.test.ts +26 -0
- package/test/fixture/fixture.ts +224 -0
- package/test/fixture/flag.ts +20 -0
- package/test/fixture/flock-worker.ts +72 -0
- package/test/fixture/lsp/fake-lsp-server.js +249 -0
- package/test/fixture/mcp-session-recovery.ts +50 -0
- package/test/fixture/plug-worker.ts +93 -0
- package/test/fixture/plugin-meta-worker.ts +19 -0
- package/test/fixture/plugin.ts +10 -0
- package/test/fixture/skills/agents-sdk/SKILL.md +152 -0
- package/test/fixture/skills/agents-sdk/references/callable.md +92 -0
- package/test/fixture/skills/cloudflare/SKILL.md +211 -0
- package/test/fixture/skills/index.json +6 -0
- package/test/fixture/tui-environment.tsx +32 -0
- package/test/fixture/tui-plugin.ts +355 -0
- package/test/fixture/tui-runtime.ts +56 -0
- package/test/fixture/tui-sdk.ts +82 -0
- package/test/fixture/workspace.ts +30 -0
- package/test/fixtures/recordings/session/native-anthropic-tool-loop.json +49 -0
- package/test/fixtures/recordings/session/native-openai-oauth-tool-loop.json +45 -0
- package/test/fixtures/recordings/session/native-zen-tool-loop.json +49 -0
- package/test/format/format.test.ts +228 -0
- package/test/git/git.test.ts +178 -0
- package/test/ide/ide.test.ts +82 -0
- package/test/image/fixtures/picture-5mb-base64.png +0 -0
- package/test/image/image.test.ts +123 -0
- package/test/installation/installation.test.ts +230 -0
- package/test/lib/cli-process.ts +459 -0
- package/test/lib/effect.ts +177 -0
- package/test/lib/filesystem.ts +10 -0
- package/test/lib/llm-server.ts +779 -0
- package/test/lib/snapshot.ts +73 -0
- package/test/lib/test-provider.ts +37 -0
- package/test/lib/websocket.ts +46 -0
- package/test/lsp/client.test.ts +488 -0
- package/test/lsp/index.test.ts +232 -0
- package/test/lsp/jdtls-root.test.ts +459 -0
- package/test/lsp/launch.test.ts +22 -0
- package/test/lsp/lifecycle.test.ts +160 -0
- package/test/mcp/auth.test.ts +78 -0
- package/test/mcp/headers.test.ts +126 -0
- package/test/mcp/lifecycle.test.ts +1213 -0
- package/test/mcp/oauth-auto-connect.test.ts +276 -0
- package/test/mcp/oauth-browser.test.ts +239 -0
- package/test/mcp/oauth-callback.test.ts +34 -0
- package/test/mcp/oauth-provider.test.ts +61 -0
- package/test/mcp/session-recovery.test.ts +27 -0
- package/test/patch/patch.test.ts +383 -0
- package/test/permission/arity.test.ts +33 -0
- package/test/permission/next.test.ts +1176 -0
- package/test/permission-task.test.ts +318 -0
- package/test/plugin/auth-override.test.ts +105 -0
- package/test/plugin/cloudflare.test.ts +68 -0
- package/test/plugin/codex.test.ts +247 -0
- package/test/plugin/github-copilot-models.test.ts +332 -0
- package/test/plugin/install-concurrency.test.ts +140 -0
- package/test/plugin/install.test.ts +570 -0
- package/test/plugin/loader-shared.test.ts +1303 -0
- package/test/plugin/meta.test.ts +137 -0
- package/test/plugin/openai-rollout.test.ts +17 -0
- package/test/plugin/openai-ws.test.ts +877 -0
- package/test/plugin/shared.test.ts +88 -0
- package/test/plugin/snowflake-cortex.test.ts +278 -0
- package/test/plugin/trigger.test.ts +120 -0
- package/test/plugin/workspace-adapter.test.ts +140 -0
- package/test/plugin/xai.test.ts +634 -0
- package/test/preload.ts +92 -0
- package/test/project/instance-bootstrap.test.ts +110 -0
- package/test/project/instance.test.ts +245 -0
- package/test/project/migrate-global.test.ts +167 -0
- package/test/project/project-directory.test.ts +201 -0
- package/test/project/project.test.ts +815 -0
- package/test/project/vcs.test.ts +336 -0
- package/test/project/worktree-remove.test.ts +126 -0
- package/test/project/worktree.test.ts +320 -0
- package/test/provider/amazon-bedrock.test.ts +360 -0
- package/test/provider/cf-ai-gateway-e2e.test.ts +132 -0
- package/test/provider/digitalocean.test.ts +123 -0
- package/test/provider/gitlab-duo.test.ts +412 -0
- package/test/provider/header-timeout.test.ts +233 -0
- package/test/provider/model-status.test.ts +61 -0
- package/test/provider/provider.test.ts +1793 -0
- package/test/provider/transform.test.ts +4207 -0
- package/test/question/question.test.ts +465 -0
- package/test/server/AGENTS.md +15 -0
- package/test/server/auth.test.ts +59 -0
- package/test/server/global-bus.ts +31 -0
- package/test/server/global-session-list.test.ts +104 -0
- package/test/server/httpapi-authorization.test.ts +174 -0
- package/test/server/httpapi-compression.test.ts +151 -0
- package/test/server/httpapi-config.test.ts +110 -0
- package/test/server/httpapi-control-plane.test.ts +63 -0
- package/test/server/httpapi-cors-vary.test.ts +63 -0
- package/test/server/httpapi-cors.test.ts +122 -0
- package/test/server/httpapi-error-middleware.test.ts +101 -0
- package/test/server/httpapi-event.test.ts +94 -0
- package/test/server/httpapi-exercise/assertions.ts +64 -0
- package/test/server/httpapi-exercise/backend.ts +144 -0
- package/test/server/httpapi-exercise/dsl.ts +210 -0
- package/test/server/httpapi-exercise/environment.ts +40 -0
- package/test/server/httpapi-exercise/index.ts +1685 -0
- package/test/server/httpapi-exercise/report.ts +66 -0
- package/test/server/httpapi-exercise/routing.ts +96 -0
- package/test/server/httpapi-exercise/runner.ts +267 -0
- package/test/server/httpapi-exercise/runtime.ts +52 -0
- package/test/server/httpapi-exercise/types.ts +123 -0
- package/test/server/httpapi-experimental.test.ts +297 -0
- package/test/server/httpapi-file.test.ts +73 -0
- package/test/server/httpapi-global.test.ts +66 -0
- package/test/server/httpapi-instance-context.test.ts +348 -0
- package/test/server/httpapi-instance-route-auth.test.ts +81 -0
- package/test/server/httpapi-instance.test.ts +265 -0
- package/test/server/httpapi-layer.ts +33 -0
- package/test/server/httpapi-listen.test.ts +412 -0
- package/test/server/httpapi-mcp-oauth.test.ts +73 -0
- package/test/server/httpapi-mcp.test.ts +223 -0
- package/test/server/httpapi-mdns.test.ts +79 -0
- package/test/server/httpapi-promptasync-context.test.ts +223 -0
- package/test/server/httpapi-provider.test.ts +400 -0
- package/test/server/httpapi-pty.test.ts +299 -0
- package/test/server/httpapi-public-openapi.test.ts +319 -0
- package/test/server/httpapi-query-schema-drift.test.ts +330 -0
- package/test/server/httpapi-reference.test.ts +62 -0
- package/test/server/httpapi-schema-error-body.test.ts +165 -0
- package/test/server/httpapi-sdk.test.ts +909 -0
- package/test/server/httpapi-session.test.ts +1011 -0
- package/test/server/httpapi-sync.test.ts +148 -0
- package/test/server/httpapi-ui.test.ts +453 -0
- package/test/server/httpapi-v2-location.test.ts +82 -0
- package/test/server/httpapi-v2-pty.test.ts +250 -0
- package/test/server/httpapi-workspace-routing.test.ts +555 -0
- package/test/server/httpapi-workspace.test.ts +513 -0
- package/test/server/negative-tokens-regression.test.ts +83 -0
- package/test/server/project-copy.test.ts +121 -0
- package/test/server/project-init-git.test.ts +114 -0
- package/test/server/proxy-util.test.ts +113 -0
- package/test/server/sdk-error-shape.test.ts +81 -0
- package/test/server/sdk-v1-smoke.test.ts +57 -0
- package/test/server/session-actions.test.ts +109 -0
- package/test/server/session-diff-missing-patch.test.ts +96 -0
- package/test/server/session-list.test.ts +312 -0
- package/test/server/session-messages.test.ts +179 -0
- package/test/server/session-select.test.ts +66 -0
- package/test/server/workspace-proxy.test.ts +181 -0
- package/test/server/workspace-routing.test.ts +94 -0
- package/test/server/worktree-endpoint-repro.test.ts +307 -0
- package/test/session/compaction.test.ts +1834 -0
- package/test/session/instruction.test.ts +256 -0
- package/test/session/llm-native-recorded.test.ts +433 -0
- package/test/session/llm-native.test.ts +760 -0
- package/test/session/llm.test.ts +1932 -0
- package/test/session/message-v2.test.ts +1661 -0
- package/test/session/messages-pagination.test.ts +1056 -0
- package/test/session/processor-effect.test.ts +1076 -0
- package/test/session/prompt.test.ts +2326 -0
- package/test/session/retry.test.ts +439 -0
- package/test/session/revert-compact.test.ts +639 -0
- package/test/session/schema-decoding.test.ts +313 -0
- package/test/session/session-schema.test.ts +78 -0
- package/test/session/session.test.ts +248 -0
- package/test/session/snapshot-tool-race.test.ts +190 -0
- package/test/session/structured-output-integration.test.ts +235 -0
- package/test/session/structured-output.test.ts +387 -0
- package/test/session/system.test.ts +86 -0
- package/test/session/ultrawork.test.ts +25 -0
- package/test/share/share-next.test.ts +326 -0
- package/test/skill/discovery.test.ts +139 -0
- package/test/skill/skill.test.ts +571 -0
- package/test/snapshot/snapshot.test.ts +1121 -0
- package/test/storage/storage.test.ts +296 -0
- package/test/tool/__snapshots__/parameters.test.ts.snap +484 -0
- package/test/tool/__snapshots__/tool.test.ts.snap +9 -0
- package/test/tool/apply_patch.test.ts +533 -0
- package/test/tool/edit.test.ts +578 -0
- package/test/tool/external-directory.test.ts +155 -0
- package/test/tool/fixtures/large-image.png +0 -0
- package/test/tool/fixtures/models-api.json +117299 -0
- package/test/tool/glob.test.ts +136 -0
- package/test/tool/grep.test.ts +225 -0
- package/test/tool/lsp.test.ts +181 -0
- package/test/tool/parameters.test.ts +293 -0
- package/test/tool/question.test.ts +138 -0
- package/test/tool/read.test.ts +605 -0
- package/test/tool/registry.test.ts +497 -0
- package/test/tool/shell.test.ts +1238 -0
- package/test/tool/skill.test.ts +136 -0
- package/test/tool/task.test.ts +898 -0
- package/test/tool/tool-define.test.ts +153 -0
- package/test/tool/truncation.test.ts +266 -0
- package/test/tool/webfetch.test.ts +113 -0
- package/test/tool/websearch.test.ts +99 -0
- package/test/tool/write.test.ts +276 -0
- package/test/util/data-url.test.ts +14 -0
- package/test/util/error.test.ts +16 -0
- package/test/util/filesystem.test.ts +656 -0
- package/test/util/glob.test.ts +164 -0
- package/test/util/iife.test.ts +36 -0
- package/test/util/lazy.test.ts +50 -0
- package/test/util/module.test.ts +59 -0
- package/test/util/process.test.ts +128 -0
- package/test/util/repository.test.ts +93 -0
- package/test/util/timeout.test.ts +21 -0
- package/test/util/wildcard.test.ts +90 -0
- package/test/v2/session-message-updater.test.ts +269 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,1975 @@
|
|
|
1
|
+
import { LayerNode } from "@miaw/core/effect/layer-node"
|
|
2
|
+
import os from "os"
|
|
3
|
+
import { ConfigV1 } from "@miaw/core/v1/config/config"
|
|
4
|
+
import fuzzysort from "fuzzysort"
|
|
5
|
+
import { Config } from "@/config/config"
|
|
6
|
+
import { mapValues, mergeDeep, omit, pickBy, sortBy } from "remeda"
|
|
7
|
+
import { NoSuchModelError, type Provider as SDK } from "ai"
|
|
8
|
+
import { Npm } from "@miaw/core/npm"
|
|
9
|
+
import { Hash } from "@miaw/core/util/hash"
|
|
10
|
+
import { Plugin } from "../plugin"
|
|
11
|
+
import { serviceUse } from "@miaw/core/effect/service-use"
|
|
12
|
+
import { type LanguageModelV3 } from "@ai-sdk/provider"
|
|
13
|
+
import { ModelsDev } from "@miaw/core/models-dev"
|
|
14
|
+
import { Auth } from "../auth"
|
|
15
|
+
import { Env } from "../env"
|
|
16
|
+
import { InstallationVersion } from "@miaw/core/installation/version"
|
|
17
|
+
import { iife } from "@/util/iife"
|
|
18
|
+
import { Global } from "@miaw/core/global"
|
|
19
|
+
import path from "path"
|
|
20
|
+
import { pathToFileURL } from "url"
|
|
21
|
+
import { Effect, Layer, Context, Schema, Types } from "effect"
|
|
22
|
+
import { EffectBridge } from "@/effect/bridge"
|
|
23
|
+
import { InstanceState } from "@/effect/instance-state"
|
|
24
|
+
import { EffectPromise } from "@/effect/promise"
|
|
25
|
+
import { FSUtil } from "@miaw/core/fs-util"
|
|
26
|
+
import { isRecord } from "@/util/record"
|
|
27
|
+
import { optionalOmitUndefined } from "@miaw/core/schema"
|
|
28
|
+
import { ProviderTransform } from "./transform"
|
|
29
|
+
import { ProviderV2 } from "@miaw/core/provider"
|
|
30
|
+
import { ModelV2 } from "@miaw/core/model"
|
|
31
|
+
import { ModelStatus } from "./model-status"
|
|
32
|
+
import { RuntimeFlags } from "@/effect/runtime-flags"
|
|
33
|
+
import { ProviderError } from "./error"
|
|
34
|
+
|
|
35
|
+
const OPENAI_HEADER_TIMEOUT_DEFAULT = 10_000
|
|
36
|
+
|
|
37
|
+
function wrapSSE(res: Response, ms: number, ctl: AbortController) {
|
|
38
|
+
if (typeof ms !== "number" || ms <= 0) return res
|
|
39
|
+
if (!res.body) return res
|
|
40
|
+
if (!res.headers.get("content-type")?.includes("text/event-stream")) return res
|
|
41
|
+
|
|
42
|
+
const reader = res.body.getReader()
|
|
43
|
+
const body = new ReadableStream<Uint8Array>({
|
|
44
|
+
async pull(ctrl) {
|
|
45
|
+
const part = await new Promise<Awaited<ReturnType<typeof reader.read>>>((resolve, reject) => {
|
|
46
|
+
const id = setTimeout(() => {
|
|
47
|
+
const err = new ProviderError.ResponseStreamError("SSE read timed out")
|
|
48
|
+
ctl.abort(err)
|
|
49
|
+
void reader.cancel(err)
|
|
50
|
+
reject(err)
|
|
51
|
+
}, ms)
|
|
52
|
+
|
|
53
|
+
reader.read().then(
|
|
54
|
+
(part) => {
|
|
55
|
+
clearTimeout(id)
|
|
56
|
+
resolve(part)
|
|
57
|
+
},
|
|
58
|
+
(err) => {
|
|
59
|
+
clearTimeout(id)
|
|
60
|
+
reject(err)
|
|
61
|
+
},
|
|
62
|
+
)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
if (part.done) {
|
|
66
|
+
ctrl.close()
|
|
67
|
+
return
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
ctrl.enqueue(part.value)
|
|
71
|
+
},
|
|
72
|
+
async cancel(reason) {
|
|
73
|
+
ctl.abort(reason)
|
|
74
|
+
await reader.cancel(reason)
|
|
75
|
+
},
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
return new Response(body, {
|
|
79
|
+
headers: new Headers(res.headers),
|
|
80
|
+
status: res.status,
|
|
81
|
+
statusText: res.statusText,
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function timeoutController(ms: number) {
|
|
86
|
+
const ctl = new AbortController()
|
|
87
|
+
const id = setTimeout(() => ctl.abort(new ProviderError.HeaderTimeoutError(ms)), ms)
|
|
88
|
+
return {
|
|
89
|
+
signal: ctl.signal,
|
|
90
|
+
clear: () => clearTimeout(id),
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
function googleVertexAnthropicBaseURL(project: string | undefined, location: string | undefined) {
|
|
95
|
+
if (!project) return
|
|
96
|
+
if (location !== "eu" && location !== "us") return
|
|
97
|
+
// Continental multi-regions require Regional Endpoint Platform domains.
|
|
98
|
+
return `https://aiplatform.${location}.rep.googleapis.com/v1/projects/${project}/locations/${location}/publishers/anthropic/models`
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
type BundledSDK = {
|
|
102
|
+
languageModel(modelId: string): LanguageModelV3
|
|
103
|
+
chat?: (modelId: string) => LanguageModelV3
|
|
104
|
+
responses?: (modelId: string) => LanguageModelV3
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const BUNDLED_PROVIDERS: Record<string, () => Promise<(opts: any) => BundledSDK>> = {
|
|
108
|
+
"@ai-sdk/amazon-bedrock": () => import("@ai-sdk/amazon-bedrock").then((m) => m.createAmazonBedrock),
|
|
109
|
+
"@ai-sdk/amazon-bedrock/mantle": () => import("@ai-sdk/amazon-bedrock/mantle").then((m) => m.createBedrockMantle),
|
|
110
|
+
"@ai-sdk/anthropic": () => import("@ai-sdk/anthropic").then((m) => m.createAnthropic),
|
|
111
|
+
"@ai-sdk/azure": () => import("@ai-sdk/azure").then((m) => m.createAzure),
|
|
112
|
+
"@ai-sdk/google": () => import("@ai-sdk/google").then((m) => m.createGoogleGenerativeAI),
|
|
113
|
+
"@ai-sdk/google-vertex": () => import("@ai-sdk/google-vertex").then((m) => m.createVertex),
|
|
114
|
+
"@ai-sdk/google-vertex/anthropic": () =>
|
|
115
|
+
import("@ai-sdk/google-vertex/anthropic").then((m) => m.createVertexAnthropic),
|
|
116
|
+
"@ai-sdk/openai": () => import("@ai-sdk/openai").then((m) => m.createOpenAI),
|
|
117
|
+
"@ai-sdk/openai-compatible": () => import("@ai-sdk/openai-compatible").then((m) => m.createOpenAICompatible),
|
|
118
|
+
"@openrouter/ai-sdk-provider": () => import("@openrouter/ai-sdk-provider").then((m) => m.createOpenRouter),
|
|
119
|
+
"@ai-sdk/xai": () => import("@ai-sdk/xai").then((m) => m.createXai),
|
|
120
|
+
"@ai-sdk/mistral": () => import("@ai-sdk/mistral").then((m) => m.createMistral),
|
|
121
|
+
"@ai-sdk/groq": () => import("@ai-sdk/groq").then((m) => m.createGroq),
|
|
122
|
+
"@ai-sdk/deepinfra": () => import("@ai-sdk/deepinfra").then((m) => m.createDeepInfra),
|
|
123
|
+
"@ai-sdk/cerebras": () => import("@ai-sdk/cerebras").then((m) => m.createCerebras),
|
|
124
|
+
"@ai-sdk/cohere": () => import("@ai-sdk/cohere").then((m) => m.createCohere),
|
|
125
|
+
"@ai-sdk/gateway": () => import("@ai-sdk/gateway").then((m) => m.createGateway),
|
|
126
|
+
"@ai-sdk/togetherai": () => import("@ai-sdk/togetherai").then((m) => m.createTogetherAI),
|
|
127
|
+
"@ai-sdk/perplexity": () => import("@ai-sdk/perplexity").then((m) => m.createPerplexity),
|
|
128
|
+
"@ai-sdk/vercel": () => import("@ai-sdk/vercel").then((m) => m.createVercel),
|
|
129
|
+
"@ai-sdk/alibaba": () => import("@ai-sdk/alibaba").then((m) => m.createAlibaba),
|
|
130
|
+
"gitlab-ai-provider": () => import("gitlab-ai-provider").then((m) => m.createGitLab),
|
|
131
|
+
"@ai-sdk/github-copilot": () =>
|
|
132
|
+
import("@miaw/core/github-copilot/copilot-provider").then((m) => m.createOpenaiCompatible),
|
|
133
|
+
"venice-ai-sdk-provider": () => import("venice-ai-sdk-provider").then((m) => m.createVenice),
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
type CustomModelLoader = (sdk: any, modelID: string, options?: Record<string, any>, model?: Model) => Promise<any>
|
|
137
|
+
type CustomVarsLoader = (options: Record<string, any>) => Record<string, string>
|
|
138
|
+
type CustomDiscoverModels = () => Promise<Record<string, Model>>
|
|
139
|
+
type CustomLoader = (provider: Info) => Effect.Effect<{
|
|
140
|
+
autoload: boolean
|
|
141
|
+
getModel?: CustomModelLoader
|
|
142
|
+
vars?: CustomVarsLoader
|
|
143
|
+
options?: Record<string, any>
|
|
144
|
+
discoverModels?: CustomDiscoverModels
|
|
145
|
+
}>
|
|
146
|
+
|
|
147
|
+
type CustomDep = {
|
|
148
|
+
auth: (id: string) => Effect.Effect<Auth.Info | undefined>
|
|
149
|
+
config: () => Effect.Effect<ConfigV1.Info>
|
|
150
|
+
env: () => Effect.Effect<Record<string, string | undefined>>
|
|
151
|
+
get: (key: string) => Effect.Effect<string | undefined>
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function selectAzureLanguageModel(sdk: any, modelID: string, useChat: boolean) {
|
|
155
|
+
if (useChat && sdk.chat) return sdk.chat(modelID)
|
|
156
|
+
if (sdk.responses) return sdk.responses(modelID)
|
|
157
|
+
if (sdk.messages) return sdk.messages(modelID)
|
|
158
|
+
if (sdk.chat) return sdk.chat(modelID)
|
|
159
|
+
return sdk.languageModel(modelID)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function selectBedrockMantleLanguageModel(sdk: BundledSDK, modelID: string) {
|
|
163
|
+
if (modelID === "openai.gpt-oss-safeguard-20b" || modelID === "openai.gpt-oss-safeguard-120b")
|
|
164
|
+
return sdk.chat?.(modelID) ?? sdk.languageModel(modelID)
|
|
165
|
+
return sdk.responses?.(modelID) ?? sdk.languageModel(modelID)
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function custom(dep: CustomDep): Record<string, CustomLoader> {
|
|
169
|
+
return {
|
|
170
|
+
anthropic: () =>
|
|
171
|
+
Effect.succeed({
|
|
172
|
+
autoload: false,
|
|
173
|
+
options: {
|
|
174
|
+
headers: {
|
|
175
|
+
"anthropic-beta": "interleaved-thinking-2025-05-14,fine-grained-tool-streaming-2025-05-14",
|
|
176
|
+
},
|
|
177
|
+
},
|
|
178
|
+
}),
|
|
179
|
+
miaw: Effect.fnUntraced(function* (input: Info) {
|
|
180
|
+
const env = yield* dep.env()
|
|
181
|
+
const hasKey = iife(() => {
|
|
182
|
+
if (input.env.some((item) => env[item])) return true
|
|
183
|
+
return false
|
|
184
|
+
})
|
|
185
|
+
const ok =
|
|
186
|
+
hasKey ||
|
|
187
|
+
Boolean(yield* dep.auth(input.id)) ||
|
|
188
|
+
Boolean((yield* dep.config()).provider?.["miaw"]?.options?.apiKey)
|
|
189
|
+
|
|
190
|
+
if (!ok) {
|
|
191
|
+
for (const [key, value] of Object.entries(input.models)) {
|
|
192
|
+
if (value.cost.input === 0) continue
|
|
193
|
+
delete input.models[key]
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
autoload: Object.keys(input.models).length > 0,
|
|
199
|
+
options: ok ? {} : { apiKey: "public" },
|
|
200
|
+
}
|
|
201
|
+
}),
|
|
202
|
+
openai: () =>
|
|
203
|
+
Effect.succeed({
|
|
204
|
+
autoload: false,
|
|
205
|
+
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
206
|
+
return sdk.responses(modelID)
|
|
207
|
+
},
|
|
208
|
+
options: { headerTimeout: OPENAI_HEADER_TIMEOUT_DEFAULT },
|
|
209
|
+
}),
|
|
210
|
+
xai: () =>
|
|
211
|
+
Effect.succeed({
|
|
212
|
+
autoload: false,
|
|
213
|
+
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
214
|
+
return sdk.responses(modelID)
|
|
215
|
+
},
|
|
216
|
+
options: {},
|
|
217
|
+
}),
|
|
218
|
+
"github-copilot": () =>
|
|
219
|
+
Effect.succeed({
|
|
220
|
+
autoload: false,
|
|
221
|
+
async getModel(sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
222
|
+
if (sdk.responses === undefined && sdk.chat === undefined) return sdk.languageModel(modelID)
|
|
223
|
+
const match = /^gpt-(\d+)/.exec(modelID)
|
|
224
|
+
if (match && Number(match[1]) >= 5 && !modelID.startsWith("gpt-5-mini")) return sdk.responses(modelID)
|
|
225
|
+
return sdk.chat(modelID)
|
|
226
|
+
},
|
|
227
|
+
options: {},
|
|
228
|
+
}),
|
|
229
|
+
azure: Effect.fnUntraced(function* (provider: Info) {
|
|
230
|
+
const env = yield* dep.env()
|
|
231
|
+
const auth = yield* dep.auth(provider.id)
|
|
232
|
+
const resource = iife(() => {
|
|
233
|
+
return [
|
|
234
|
+
provider.options?.resourceName,
|
|
235
|
+
auth?.type === "api" ? auth.metadata?.resourceName : undefined,
|
|
236
|
+
env["AZURE_RESOURCE_NAME"],
|
|
237
|
+
].find((name) => typeof name === "string" && name.trim() !== "")
|
|
238
|
+
})
|
|
239
|
+
|
|
240
|
+
if (!resource && !provider.options?.baseURL) {
|
|
241
|
+
return {
|
|
242
|
+
autoload: false,
|
|
243
|
+
async getModel() {
|
|
244
|
+
throw new Error(
|
|
245
|
+
"AZURE_RESOURCE_NAME is missing, set it using env var or reconnecting the azure provider and setting it",
|
|
246
|
+
)
|
|
247
|
+
},
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return {
|
|
252
|
+
autoload: false,
|
|
253
|
+
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
|
|
254
|
+
return selectAzureLanguageModel(sdk, modelID, Boolean(options?.["useCompletionUrls"]))
|
|
255
|
+
},
|
|
256
|
+
options: {
|
|
257
|
+
resourceName: resource,
|
|
258
|
+
},
|
|
259
|
+
vars(_options): Record<string, string> {
|
|
260
|
+
if (resource) {
|
|
261
|
+
return {
|
|
262
|
+
AZURE_RESOURCE_NAME: resource,
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
return {}
|
|
266
|
+
},
|
|
267
|
+
}
|
|
268
|
+
}),
|
|
269
|
+
"azure-cognitive-services": Effect.fnUntraced(function* () {
|
|
270
|
+
const resourceName = yield* dep.get("AZURE_COGNITIVE_SERVICES_RESOURCE_NAME")
|
|
271
|
+
return {
|
|
272
|
+
autoload: false,
|
|
273
|
+
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
|
|
274
|
+
return selectAzureLanguageModel(sdk, modelID, Boolean(options?.["useCompletionUrls"]))
|
|
275
|
+
},
|
|
276
|
+
options: {
|
|
277
|
+
baseURL: resourceName ? `https://${resourceName}.cognitiveservices.azure.com/openai` : undefined,
|
|
278
|
+
},
|
|
279
|
+
}
|
|
280
|
+
}),
|
|
281
|
+
"amazon-bedrock": Effect.fnUntraced(function* () {
|
|
282
|
+
const providerConfig = (yield* dep.config()).provider?.["amazon-bedrock"]
|
|
283
|
+
const auth = yield* dep.auth("amazon-bedrock")
|
|
284
|
+
const env = yield* dep.env()
|
|
285
|
+
|
|
286
|
+
// Region precedence: 1) config file, 2) env var, 3) default
|
|
287
|
+
const configRegion = providerConfig?.options?.region
|
|
288
|
+
const envRegion = env["AWS_REGION"]
|
|
289
|
+
const defaultRegion = configRegion ?? envRegion ?? "us-east-1"
|
|
290
|
+
|
|
291
|
+
// Profile: config file takes precedence over env var
|
|
292
|
+
const configProfile = providerConfig?.options?.profile
|
|
293
|
+
const envProfile = env["AWS_PROFILE"]
|
|
294
|
+
const profile = configProfile ?? envProfile
|
|
295
|
+
|
|
296
|
+
const awsAccessKeyId = env["AWS_ACCESS_KEY_ID"]
|
|
297
|
+
const configApiKey = providerConfig?.options?.apiKey
|
|
298
|
+
|
|
299
|
+
// TODO: Using process.env directly because Env.set only updates a process.env shallow copy,
|
|
300
|
+
// until the scope of the Env API is clarified (test only or runtime?)
|
|
301
|
+
const awsBearerToken = iife(() => {
|
|
302
|
+
const envToken = process.env.AWS_BEARER_TOKEN_BEDROCK
|
|
303
|
+
if (envToken) return envToken
|
|
304
|
+
if (auth?.type === "api") {
|
|
305
|
+
process.env.AWS_BEARER_TOKEN_BEDROCK = auth.key
|
|
306
|
+
return auth.key
|
|
307
|
+
}
|
|
308
|
+
return undefined
|
|
309
|
+
})
|
|
310
|
+
|
|
311
|
+
const awsWebIdentityTokenFile = env["AWS_WEB_IDENTITY_TOKEN_FILE"]
|
|
312
|
+
|
|
313
|
+
const containerCreds = Boolean(
|
|
314
|
+
process.env.AWS_CONTAINER_CREDENTIALS_RELATIVE_URI || process.env.AWS_CONTAINER_CREDENTIALS_FULL_URI,
|
|
315
|
+
)
|
|
316
|
+
|
|
317
|
+
if (
|
|
318
|
+
!profile &&
|
|
319
|
+
!awsAccessKeyId &&
|
|
320
|
+
!awsBearerToken &&
|
|
321
|
+
!configApiKey &&
|
|
322
|
+
!awsWebIdentityTokenFile &&
|
|
323
|
+
!containerCreds
|
|
324
|
+
)
|
|
325
|
+
return { autoload: false }
|
|
326
|
+
|
|
327
|
+
const { fromNodeProviderChain } = yield* Effect.promise(() => import("@aws-sdk/credential-providers"))
|
|
328
|
+
|
|
329
|
+
const providerOptions: Record<string, any> = {
|
|
330
|
+
region: defaultRegion,
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// Only use credential chain if no bearer token exists
|
|
334
|
+
// Bearer token takes precedence over credential chain (profiles, access keys, IAM roles, web identity tokens)
|
|
335
|
+
if (!awsBearerToken && !configApiKey) {
|
|
336
|
+
// Build credential provider options (only pass profile if specified)
|
|
337
|
+
const credentialProviderOptions = profile ? { profile } : {}
|
|
338
|
+
|
|
339
|
+
providerOptions.credentialProvider = fromNodeProviderChain(credentialProviderOptions)
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
// Add custom endpoint if specified (endpoint takes precedence over baseURL)
|
|
343
|
+
const endpoint = providerConfig?.options?.endpoint ?? providerConfig?.options?.baseURL
|
|
344
|
+
if (endpoint) {
|
|
345
|
+
providerOptions.baseURL = endpoint
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
autoload: true,
|
|
350
|
+
options: providerOptions,
|
|
351
|
+
vars(options: Record<string, any>) {
|
|
352
|
+
return { AWS_REGION: options.region ?? defaultRegion }
|
|
353
|
+
},
|
|
354
|
+
async getModel(sdk: any, modelID: string, options?: Record<string, any>, model?: Model) {
|
|
355
|
+
if (model?.api.npm === "@ai-sdk/amazon-bedrock/mantle") return selectBedrockMantleLanguageModel(sdk, modelID)
|
|
356
|
+
|
|
357
|
+
// Skip region prefixing if model already has a cross-region inference profile prefix
|
|
358
|
+
// Models from models.dev may already include prefixes like us., eu., global., etc.
|
|
359
|
+
const crossRegionPrefixes = ["global.", "us.", "eu.", "jp.", "apac.", "au."]
|
|
360
|
+
if (crossRegionPrefixes.some((prefix) => modelID.startsWith(prefix))) {
|
|
361
|
+
return sdk.languageModel(modelID)
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Region resolution precedence (highest to lowest):
|
|
365
|
+
// 1. options.region from miaw.json provider config
|
|
366
|
+
// 2. defaultRegion from AWS_REGION environment variable
|
|
367
|
+
// 3. Default "us-east-1" (baked into defaultRegion)
|
|
368
|
+
const region = options?.region ?? defaultRegion
|
|
369
|
+
|
|
370
|
+
let regionPrefix = region.split("-")[0]
|
|
371
|
+
|
|
372
|
+
switch (regionPrefix) {
|
|
373
|
+
case "us": {
|
|
374
|
+
const modelRequiresPrefix = [
|
|
375
|
+
"nova-micro",
|
|
376
|
+
"nova-lite",
|
|
377
|
+
"nova-pro",
|
|
378
|
+
"nova-premier",
|
|
379
|
+
"nova-2",
|
|
380
|
+
"claude",
|
|
381
|
+
"deepseek",
|
|
382
|
+
].some((m) => modelID.includes(m))
|
|
383
|
+
const isGovCloud = region.startsWith("us-gov")
|
|
384
|
+
if (modelRequiresPrefix && !isGovCloud) {
|
|
385
|
+
modelID = `${regionPrefix}.${modelID}`
|
|
386
|
+
}
|
|
387
|
+
break
|
|
388
|
+
}
|
|
389
|
+
case "eu": {
|
|
390
|
+
const regionRequiresPrefix = [
|
|
391
|
+
"eu-west-1",
|
|
392
|
+
"eu-west-2",
|
|
393
|
+
"eu-west-3",
|
|
394
|
+
"eu-north-1",
|
|
395
|
+
"eu-central-1",
|
|
396
|
+
"eu-south-1",
|
|
397
|
+
"eu-south-2",
|
|
398
|
+
].some((r) => region.includes(r))
|
|
399
|
+
const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "llama3", "pixtral"].some((m) =>
|
|
400
|
+
modelID.includes(m),
|
|
401
|
+
)
|
|
402
|
+
if (regionRequiresPrefix && modelRequiresPrefix) {
|
|
403
|
+
modelID = `${regionPrefix}.${modelID}`
|
|
404
|
+
}
|
|
405
|
+
break
|
|
406
|
+
}
|
|
407
|
+
case "ap": {
|
|
408
|
+
const isAustraliaRegion = ["ap-southeast-2", "ap-southeast-4"].includes(region)
|
|
409
|
+
const isTokyoRegion = region === "ap-northeast-1"
|
|
410
|
+
if (
|
|
411
|
+
isAustraliaRegion &&
|
|
412
|
+
["anthropic.claude-sonnet-4-5", "anthropic.claude-haiku"].some((m) => modelID.includes(m))
|
|
413
|
+
) {
|
|
414
|
+
regionPrefix = "au"
|
|
415
|
+
modelID = `${regionPrefix}.${modelID}`
|
|
416
|
+
} else if (isTokyoRegion) {
|
|
417
|
+
// Tokyo region uses jp. prefix for cross-region inference
|
|
418
|
+
const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) =>
|
|
419
|
+
modelID.includes(m),
|
|
420
|
+
)
|
|
421
|
+
if (modelRequiresPrefix) {
|
|
422
|
+
regionPrefix = "jp"
|
|
423
|
+
modelID = `${regionPrefix}.${modelID}`
|
|
424
|
+
}
|
|
425
|
+
} else {
|
|
426
|
+
// Other APAC regions use apac. prefix
|
|
427
|
+
const modelRequiresPrefix = ["claude", "nova-lite", "nova-micro", "nova-pro"].some((m) =>
|
|
428
|
+
modelID.includes(m),
|
|
429
|
+
)
|
|
430
|
+
if (modelRequiresPrefix) {
|
|
431
|
+
regionPrefix = "apac"
|
|
432
|
+
modelID = `${regionPrefix}.${modelID}`
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
break
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
return sdk.languageModel(modelID)
|
|
440
|
+
},
|
|
441
|
+
}
|
|
442
|
+
}),
|
|
443
|
+
llmgateway: () =>
|
|
444
|
+
Effect.succeed({
|
|
445
|
+
autoload: false,
|
|
446
|
+
options: {
|
|
447
|
+
headers: {
|
|
448
|
+
"HTTP-Referer": "https://miaw/",
|
|
449
|
+
"X-Title": "miaw",
|
|
450
|
+
"X-Source": "miaw",
|
|
451
|
+
},
|
|
452
|
+
},
|
|
453
|
+
}),
|
|
454
|
+
openrouter: () =>
|
|
455
|
+
Effect.succeed({
|
|
456
|
+
autoload: false,
|
|
457
|
+
options: {
|
|
458
|
+
headers: {
|
|
459
|
+
"HTTP-Referer": "https://miaw/",
|
|
460
|
+
"X-Title": "miaw",
|
|
461
|
+
},
|
|
462
|
+
},
|
|
463
|
+
}),
|
|
464
|
+
nvidia: (provider) =>
|
|
465
|
+
Effect.succeed({
|
|
466
|
+
autoload: provider.source === "config",
|
|
467
|
+
options: {
|
|
468
|
+
headers: {
|
|
469
|
+
"HTTP-Referer": "https://miaw/",
|
|
470
|
+
"X-Title": "miaw",
|
|
471
|
+
"X-BILLING-INVOKE-ORIGIN": "Miaw",
|
|
472
|
+
},
|
|
473
|
+
},
|
|
474
|
+
}),
|
|
475
|
+
vercel: () =>
|
|
476
|
+
Effect.succeed({
|
|
477
|
+
autoload: false,
|
|
478
|
+
options: {
|
|
479
|
+
headers: {
|
|
480
|
+
"http-referer": "https://miaw/",
|
|
481
|
+
"x-title": "miaw",
|
|
482
|
+
},
|
|
483
|
+
},
|
|
484
|
+
}),
|
|
485
|
+
"google-vertex": Effect.fnUntraced(function* (provider: Info) {
|
|
486
|
+
const env = yield* dep.env()
|
|
487
|
+
// models.dev advertises GOOGLE_VERTEX_PROJECT for Vertex; keep the wider
|
|
488
|
+
// Google Cloud project env names as fallbacks for existing ADC setups.
|
|
489
|
+
const project =
|
|
490
|
+
provider.options?.project ??
|
|
491
|
+
env["GOOGLE_VERTEX_PROJECT"] ??
|
|
492
|
+
env["GOOGLE_CLOUD_PROJECT"] ??
|
|
493
|
+
env["GCP_PROJECT"] ??
|
|
494
|
+
env["GCLOUD_PROJECT"]
|
|
495
|
+
|
|
496
|
+
const location = String(
|
|
497
|
+
provider.options?.location ??
|
|
498
|
+
env["GOOGLE_VERTEX_LOCATION"] ??
|
|
499
|
+
env["GOOGLE_CLOUD_LOCATION"] ??
|
|
500
|
+
env["VERTEX_LOCATION"] ??
|
|
501
|
+
"us-central1",
|
|
502
|
+
)
|
|
503
|
+
|
|
504
|
+
const autoload = Boolean(project)
|
|
505
|
+
if (!autoload) return { autoload: false }
|
|
506
|
+
return {
|
|
507
|
+
autoload: true,
|
|
508
|
+
vars(_options: Record<string, any>) {
|
|
509
|
+
const endpoint = location === "global" ? "aiplatform.googleapis.com" : `${location}-aiplatform.googleapis.com`
|
|
510
|
+
return {
|
|
511
|
+
...(project && { GOOGLE_VERTEX_PROJECT: project }),
|
|
512
|
+
GOOGLE_VERTEX_LOCATION: location,
|
|
513
|
+
GOOGLE_VERTEX_ENDPOINT: endpoint,
|
|
514
|
+
}
|
|
515
|
+
},
|
|
516
|
+
options: {
|
|
517
|
+
project,
|
|
518
|
+
location,
|
|
519
|
+
fetch: async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
520
|
+
const { GoogleAuth } = await import("google-auth-library")
|
|
521
|
+
const auth = new GoogleAuth({ scopes: ["https://www.googleapis.com/auth/cloud-platform"] })
|
|
522
|
+
const client = await auth.getClient()
|
|
523
|
+
const token = await client.getAccessToken()
|
|
524
|
+
|
|
525
|
+
const headers = new Headers(init?.headers)
|
|
526
|
+
headers.set("Authorization", `Bearer ${token.token}`)
|
|
527
|
+
|
|
528
|
+
return fetch(input, { ...init, headers })
|
|
529
|
+
},
|
|
530
|
+
},
|
|
531
|
+
async getModel(sdk: any, modelID: string) {
|
|
532
|
+
const id = String(modelID).trim()
|
|
533
|
+
return sdk.languageModel(id)
|
|
534
|
+
},
|
|
535
|
+
}
|
|
536
|
+
}),
|
|
537
|
+
"google-vertex-anthropic": Effect.fnUntraced(function* () {
|
|
538
|
+
const env = yield* dep.env()
|
|
539
|
+
const project = env["GOOGLE_CLOUD_PROJECT"] ?? env["GCP_PROJECT"] ?? env["GCLOUD_PROJECT"]
|
|
540
|
+
const location = env["GOOGLE_CLOUD_LOCATION"] ?? env["VERTEX_LOCATION"] ?? "global"
|
|
541
|
+
const autoload = Boolean(project)
|
|
542
|
+
if (!autoload) return { autoload: false }
|
|
543
|
+
const baseURL = googleVertexAnthropicBaseURL(project, location)
|
|
544
|
+
return {
|
|
545
|
+
autoload: true,
|
|
546
|
+
options: {
|
|
547
|
+
project,
|
|
548
|
+
location,
|
|
549
|
+
...(baseURL && { baseURL }),
|
|
550
|
+
},
|
|
551
|
+
async getModel(sdk: any, modelID) {
|
|
552
|
+
const id = String(modelID).trim()
|
|
553
|
+
return sdk.languageModel(id)
|
|
554
|
+
},
|
|
555
|
+
}
|
|
556
|
+
}),
|
|
557
|
+
"sap-ai-core": Effect.fnUntraced(function* () {
|
|
558
|
+
const auth = yield* dep.auth("sap-ai-core")
|
|
559
|
+
// TODO: Using process.env directly because Env.set only updates a shallow copy (not process.env),
|
|
560
|
+
// until the scope of the Env API is clarified (test only or runtime?)
|
|
561
|
+
const envServiceKey = iife(() => {
|
|
562
|
+
const envAICoreServiceKey = process.env.AICORE_SERVICE_KEY
|
|
563
|
+
if (envAICoreServiceKey) return envAICoreServiceKey
|
|
564
|
+
if (auth?.type === "api") {
|
|
565
|
+
process.env.AICORE_SERVICE_KEY = auth.key
|
|
566
|
+
return auth.key
|
|
567
|
+
}
|
|
568
|
+
return undefined
|
|
569
|
+
})
|
|
570
|
+
const deploymentId = process.env.AICORE_DEPLOYMENT_ID
|
|
571
|
+
const resourceGroup = process.env.AICORE_RESOURCE_GROUP
|
|
572
|
+
|
|
573
|
+
return {
|
|
574
|
+
autoload: !!envServiceKey,
|
|
575
|
+
options: envServiceKey ? { deploymentId, resourceGroup } : {},
|
|
576
|
+
async getModel(sdk: any, modelID: string) {
|
|
577
|
+
return sdk(modelID)
|
|
578
|
+
},
|
|
579
|
+
}
|
|
580
|
+
}),
|
|
581
|
+
zenmux: () =>
|
|
582
|
+
Effect.succeed({
|
|
583
|
+
autoload: false,
|
|
584
|
+
options: {
|
|
585
|
+
headers: {
|
|
586
|
+
"HTTP-Referer": "https://miaw/",
|
|
587
|
+
"X-Title": "miaw",
|
|
588
|
+
},
|
|
589
|
+
},
|
|
590
|
+
}),
|
|
591
|
+
gitlab: Effect.fnUntraced(function* (input: Info) {
|
|
592
|
+
const {
|
|
593
|
+
VERSION: GITLAB_PROVIDER_VERSION,
|
|
594
|
+
isWorkflowModel,
|
|
595
|
+
discoverWorkflowModels,
|
|
596
|
+
} = yield* Effect.promise(() => import("gitlab-ai-provider"))
|
|
597
|
+
|
|
598
|
+
const instanceUrl = (yield* dep.get("GITLAB_INSTANCE_URL")) || "https://gitlab.com"
|
|
599
|
+
|
|
600
|
+
const auth = yield* dep.auth(input.id)
|
|
601
|
+
const apiKey = auth?.type === "oauth" ? auth.access : auth?.type === "api" ? auth.key : undefined
|
|
602
|
+
const token = apiKey ?? (yield* dep.get("GITLAB_TOKEN"))
|
|
603
|
+
|
|
604
|
+
const providerConfig = (yield* dep.config()).provider?.["gitlab"]
|
|
605
|
+
const directory = yield* InstanceState.directory
|
|
606
|
+
|
|
607
|
+
const aiGatewayHeaders = {
|
|
608
|
+
"User-Agent": `miaw/${InstallationVersion} gitlab-ai-provider/${GITLAB_PROVIDER_VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`,
|
|
609
|
+
"anthropic-beta": "context-1m-2025-08-07",
|
|
610
|
+
...providerConfig?.options?.aiGatewayHeaders,
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
const featureFlags = {
|
|
614
|
+
duo_agent_platform_agentic_chat: true,
|
|
615
|
+
duo_agent_platform: true,
|
|
616
|
+
...providerConfig?.options?.featureFlags,
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
return {
|
|
620
|
+
autoload: !!token,
|
|
621
|
+
options: {
|
|
622
|
+
instanceUrl,
|
|
623
|
+
apiKey: token,
|
|
624
|
+
aiGatewayHeaders,
|
|
625
|
+
featureFlags,
|
|
626
|
+
},
|
|
627
|
+
async getModel(sdk: any, modelID: string, options?: Record<string, any>) {
|
|
628
|
+
if (modelID.startsWith("duo-workflow-")) {
|
|
629
|
+
const workflowRef = typeof options?.workflowRef === "string" ? options.workflowRef : undefined
|
|
630
|
+
// Use the static mapping if it exists, otherwise use duo-workflow with selectedModelRef
|
|
631
|
+
const sdkModelID = isWorkflowModel(modelID) ? modelID : "duo-workflow"
|
|
632
|
+
const workflowDefinition =
|
|
633
|
+
typeof options?.workflowDefinition === "string" ? options.workflowDefinition : undefined
|
|
634
|
+
const model = sdk.workflowChat(sdkModelID, {
|
|
635
|
+
featureFlags,
|
|
636
|
+
workflowDefinition,
|
|
637
|
+
})
|
|
638
|
+
if (workflowRef) {
|
|
639
|
+
model.selectedModelRef = workflowRef
|
|
640
|
+
}
|
|
641
|
+
return model
|
|
642
|
+
}
|
|
643
|
+
return sdk.agenticChat(modelID, {
|
|
644
|
+
aiGatewayHeaders,
|
|
645
|
+
featureFlags,
|
|
646
|
+
})
|
|
647
|
+
},
|
|
648
|
+
async discoverModels(): Promise<Record<string, Model>> {
|
|
649
|
+
if (!apiKey) {
|
|
650
|
+
return {}
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
try {
|
|
654
|
+
const token = apiKey
|
|
655
|
+
const getHeaders = (): Record<string, string> =>
|
|
656
|
+
auth?.type === "api" ? { "PRIVATE-TOKEN": token } : { Authorization: `Bearer ${token}` }
|
|
657
|
+
|
|
658
|
+
const result = await discoverWorkflowModels({ instanceUrl, getHeaders }, { workingDirectory: directory })
|
|
659
|
+
|
|
660
|
+
if (!result.models.length) {
|
|
661
|
+
return {}
|
|
662
|
+
}
|
|
663
|
+
|
|
664
|
+
const models: Record<string, Model> = {}
|
|
665
|
+
for (const m of result.models) {
|
|
666
|
+
if (!input.models[m.id]) {
|
|
667
|
+
models[m.id] = {
|
|
668
|
+
id: ModelV2.ID.make(m.id),
|
|
669
|
+
providerID: ProviderV2.ID.make("gitlab"),
|
|
670
|
+
name: `Agent Platform (${m.name})`,
|
|
671
|
+
family: "",
|
|
672
|
+
api: {
|
|
673
|
+
id: m.id,
|
|
674
|
+
url: instanceUrl,
|
|
675
|
+
npm: "gitlab-ai-provider",
|
|
676
|
+
},
|
|
677
|
+
status: "active",
|
|
678
|
+
headers: {},
|
|
679
|
+
options: { workflowRef: m.ref },
|
|
680
|
+
cost: { input: 0, output: 0, cache: { read: 0, write: 0 } },
|
|
681
|
+
limit: { context: m.context, output: m.output },
|
|
682
|
+
capabilities: {
|
|
683
|
+
temperature: false,
|
|
684
|
+
reasoning: true,
|
|
685
|
+
attachment: true,
|
|
686
|
+
toolcall: true,
|
|
687
|
+
input: {
|
|
688
|
+
text: true,
|
|
689
|
+
audio: false,
|
|
690
|
+
image: true,
|
|
691
|
+
video: false,
|
|
692
|
+
pdf: true,
|
|
693
|
+
},
|
|
694
|
+
output: {
|
|
695
|
+
text: true,
|
|
696
|
+
audio: false,
|
|
697
|
+
image: false,
|
|
698
|
+
video: false,
|
|
699
|
+
pdf: false,
|
|
700
|
+
},
|
|
701
|
+
interleaved: false,
|
|
702
|
+
},
|
|
703
|
+
release_date: "",
|
|
704
|
+
variants: {},
|
|
705
|
+
}
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
|
|
709
|
+
return models
|
|
710
|
+
} catch (e) {
|
|
711
|
+
return {}
|
|
712
|
+
}
|
|
713
|
+
},
|
|
714
|
+
}
|
|
715
|
+
}),
|
|
716
|
+
"cloudflare-workers-ai": Effect.fnUntraced(function* (input: Info) {
|
|
717
|
+
// When baseURL is already configured (e.g. corporate config routing through a proxy/gateway),
|
|
718
|
+
// skip the account ID check because the URL is already fully specified.
|
|
719
|
+
if (input.options?.baseURL) return { autoload: false }
|
|
720
|
+
|
|
721
|
+
const auth = yield* dep.auth(input.id)
|
|
722
|
+
const env = yield* dep.env()
|
|
723
|
+
const accountId = env["CLOUDFLARE_ACCOUNT_ID"] || (auth?.type === "api" ? auth.metadata?.accountId : undefined)
|
|
724
|
+
if (!accountId)
|
|
725
|
+
return {
|
|
726
|
+
autoload: false,
|
|
727
|
+
async getModel() {
|
|
728
|
+
throw new Error(
|
|
729
|
+
"CLOUDFLARE_ACCOUNT_ID is missing. Set it with: export CLOUDFLARE_ACCOUNT_ID=<your-account-id>",
|
|
730
|
+
)
|
|
731
|
+
},
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
const apiKey = env["CLOUDFLARE_API_KEY"] || (auth?.type === "api" ? auth.key : undefined)
|
|
735
|
+
|
|
736
|
+
return {
|
|
737
|
+
autoload: !!apiKey,
|
|
738
|
+
options: {
|
|
739
|
+
apiKey,
|
|
740
|
+
headers: {
|
|
741
|
+
"User-Agent": `miaw/${InstallationVersion} cloudflare-workers-ai (${os.platform()} ${os.release()}; ${os.arch()})`,
|
|
742
|
+
},
|
|
743
|
+
},
|
|
744
|
+
async getModel(sdk: any, modelID: string) {
|
|
745
|
+
return sdk.languageModel(modelID)
|
|
746
|
+
},
|
|
747
|
+
vars(_options) {
|
|
748
|
+
return {
|
|
749
|
+
CLOUDFLARE_ACCOUNT_ID: accountId,
|
|
750
|
+
}
|
|
751
|
+
},
|
|
752
|
+
}
|
|
753
|
+
}),
|
|
754
|
+
"cloudflare-ai-gateway": Effect.fnUntraced(function* (input: Info) {
|
|
755
|
+
// When baseURL is already configured (e.g. corporate config), skip the ID checks.
|
|
756
|
+
if (input.options?.baseURL) return { autoload: false }
|
|
757
|
+
|
|
758
|
+
const auth = yield* dep.auth(input.id)
|
|
759
|
+
const env = yield* dep.env()
|
|
760
|
+
const accountId = env["CLOUDFLARE_ACCOUNT_ID"] || (auth?.type === "api" ? auth.metadata?.accountId : undefined)
|
|
761
|
+
// The Cloudflare auth prompt stores this value as gatewayId metadata.
|
|
762
|
+
const gateway = env["CLOUDFLARE_GATEWAY_ID"] || (auth?.type === "api" ? auth.metadata?.gatewayId : undefined)
|
|
763
|
+
|
|
764
|
+
if (!accountId || !gateway) {
|
|
765
|
+
const missing = [
|
|
766
|
+
!accountId ? "CLOUDFLARE_ACCOUNT_ID" : undefined,
|
|
767
|
+
!gateway ? "CLOUDFLARE_GATEWAY_ID" : undefined,
|
|
768
|
+
].filter((x): x is string => Boolean(x))
|
|
769
|
+
return {
|
|
770
|
+
autoload: false,
|
|
771
|
+
async getModel() {
|
|
772
|
+
throw new Error(
|
|
773
|
+
`${missing.join(" and ")} missing. Set with: ${missing.map((x) => `export ${x}=<value>`).join(" && ")}`,
|
|
774
|
+
)
|
|
775
|
+
},
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
|
|
779
|
+
// Get API token from env or auth - required for authenticated gateways
|
|
780
|
+
const apiToken =
|
|
781
|
+
env["CLOUDFLARE_API_TOKEN"] || env["CF_AIG_TOKEN"] || (auth?.type === "api" ? auth.key : undefined)
|
|
782
|
+
|
|
783
|
+
if (!apiToken) {
|
|
784
|
+
throw new Error(
|
|
785
|
+
"CLOUDFLARE_API_TOKEN (or CF_AIG_TOKEN) is required for Cloudflare AI Gateway. " +
|
|
786
|
+
"Set it via environment variable or run `miaw auth cloudflare-ai-gateway`.",
|
|
787
|
+
)
|
|
788
|
+
}
|
|
789
|
+
|
|
790
|
+
// Use official ai-gateway-provider package (v2.x for AI SDK v5 compatibility)
|
|
791
|
+
const { createAiGateway } = yield* Effect.promise(() => import("ai-gateway-provider"))
|
|
792
|
+
const { createUnified } = yield* Effect.promise(() => import("ai-gateway-provider/providers/unified"))
|
|
793
|
+
|
|
794
|
+
const metadata = iife(() => {
|
|
795
|
+
if (input.options?.metadata) return input.options.metadata
|
|
796
|
+
try {
|
|
797
|
+
return JSON.parse(input.options?.headers?.["cf-aig-metadata"])
|
|
798
|
+
} catch {
|
|
799
|
+
return undefined
|
|
800
|
+
}
|
|
801
|
+
})
|
|
802
|
+
const opts = {
|
|
803
|
+
metadata,
|
|
804
|
+
cacheTtl: input.options?.cacheTtl,
|
|
805
|
+
cacheKey: input.options?.cacheKey,
|
|
806
|
+
skipCache: input.options?.skipCache,
|
|
807
|
+
collectLog: input.options?.collectLog,
|
|
808
|
+
headers: {
|
|
809
|
+
"User-Agent": `miaw/${InstallationVersion} cloudflare-ai-gateway (${os.platform()} ${os.release()}; ${os.arch()})`,
|
|
810
|
+
},
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
const aigateway = createAiGateway({
|
|
814
|
+
accountId,
|
|
815
|
+
gateway,
|
|
816
|
+
apiKey: apiToken,
|
|
817
|
+
...(Object.values(opts).some((v) => v !== undefined) ? { options: opts } : {}),
|
|
818
|
+
})
|
|
819
|
+
const unified = createUnified()
|
|
820
|
+
|
|
821
|
+
return {
|
|
822
|
+
autoload: true,
|
|
823
|
+
async getModel(_sdk: any, modelID: string, _options?: Record<string, any>) {
|
|
824
|
+
// Model IDs use Unified API format: provider/model (e.g., "anthropic/claude-sonnet-4-5")
|
|
825
|
+
return aigateway(unified(modelID))
|
|
826
|
+
},
|
|
827
|
+
options: {},
|
|
828
|
+
}
|
|
829
|
+
}),
|
|
830
|
+
cerebras: () =>
|
|
831
|
+
Effect.succeed({
|
|
832
|
+
autoload: false,
|
|
833
|
+
options: {
|
|
834
|
+
headers: {
|
|
835
|
+
"X-Cerebras-3rd-Party-Integration": "miaw",
|
|
836
|
+
},
|
|
837
|
+
},
|
|
838
|
+
}),
|
|
839
|
+
kilo: () =>
|
|
840
|
+
Effect.succeed({
|
|
841
|
+
autoload: false,
|
|
842
|
+
options: {
|
|
843
|
+
headers: {
|
|
844
|
+
"HTTP-Referer": "https://miaw/",
|
|
845
|
+
"X-Title": "miaw",
|
|
846
|
+
},
|
|
847
|
+
},
|
|
848
|
+
}),
|
|
849
|
+
"snowflake-cortex": Effect.fnUntraced(function* (input: Info) {
|
|
850
|
+
const env = yield* dep.env()
|
|
851
|
+
const auth = yield* dep.auth(input.id)
|
|
852
|
+
|
|
853
|
+
const account =
|
|
854
|
+
env["SNOWFLAKE_ACCOUNT"] ??
|
|
855
|
+
(auth?.type === "api" ? auth.metadata?.account : undefined) ??
|
|
856
|
+
(auth?.type === "oauth" ? auth.accountId : undefined) ??
|
|
857
|
+
input.options?.account
|
|
858
|
+
|
|
859
|
+
const envToken = env["SNOWFLAKE_CORTEX_TOKEN"] ?? env["SNOWFLAKE_CORTEX_PAT"]
|
|
860
|
+
const apiKeyToken = auth?.type === "api" ? auth.key : undefined
|
|
861
|
+
const oauthToken = auth?.type === "oauth" ? auth.access : undefined
|
|
862
|
+
const configToken = input.options?.token ?? input.options?.apiKey
|
|
863
|
+
|
|
864
|
+
const token = envToken ?? apiKeyToken ?? oauthToken ?? configToken
|
|
865
|
+
|
|
866
|
+
if (!account || !token) {
|
|
867
|
+
const missing = [!account && "SNOWFLAKE_ACCOUNT", !token && "SNOWFLAKE_CORTEX_TOKEN"].filter(Boolean).join(", ")
|
|
868
|
+
return {
|
|
869
|
+
autoload: false,
|
|
870
|
+
async getModel() {
|
|
871
|
+
throw new Error(
|
|
872
|
+
`Snowflake Cortex: missing credentials (${missing}). Provide a bearer token (OAuth, JWT, or PAT) via env var, miaw auth, or provider options.`,
|
|
873
|
+
)
|
|
874
|
+
},
|
|
875
|
+
}
|
|
876
|
+
}
|
|
877
|
+
|
|
878
|
+
const baseURL = `https://${account}.snowflakecomputing.com/api/v2/cortex/v1`
|
|
879
|
+
|
|
880
|
+
const options: Record<string, any> = { baseURL, apiKey: token }
|
|
881
|
+
|
|
882
|
+
// Only skip provider-level fetch when the token is from OAuth with no override.
|
|
883
|
+
// For OAuth tokens, the plugin auth loader's combined fetch handles
|
|
884
|
+
// OAuth refresh + snowflake transformations in one place.
|
|
885
|
+
// For env/config/API-key tokens, the provider fetch applies snowflake
|
|
886
|
+
// transformations directly.
|
|
887
|
+
const useOAuthHandler =
|
|
888
|
+
oauthToken !== undefined && envToken === undefined && apiKeyToken === undefined && configToken === undefined
|
|
889
|
+
if (!useOAuthHandler) {
|
|
890
|
+
options.fetch = async (url: RequestInfo | URL, init?: RequestInit) => {
|
|
891
|
+
if (init?.body && typeof init.body === "string") {
|
|
892
|
+
try {
|
|
893
|
+
const body = JSON.parse(init.body)
|
|
894
|
+
if ("max_tokens" in body) {
|
|
895
|
+
body.max_completion_tokens = body.max_tokens
|
|
896
|
+
delete body.max_tokens
|
|
897
|
+
init = { ...init, body: JSON.stringify(body) }
|
|
898
|
+
}
|
|
899
|
+
} catch {}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
const response = await fetch(url, init)
|
|
903
|
+
|
|
904
|
+
if (!response.ok && response.status === 400) {
|
|
905
|
+
try {
|
|
906
|
+
const errorData = await response.clone().json()
|
|
907
|
+
const errorMessage = String(errorData.message || errorData.error || "")
|
|
908
|
+
if (errorMessage.toLowerCase().includes("conversation complete")) {
|
|
909
|
+
return new Response(
|
|
910
|
+
JSON.stringify({
|
|
911
|
+
choices: [{ finish_reason: "stop", message: { content: "", role: "assistant" } }],
|
|
912
|
+
}),
|
|
913
|
+
{ status: 200, headers: new Headers({ "content-type": "application/json" }) },
|
|
914
|
+
)
|
|
915
|
+
}
|
|
916
|
+
} catch {}
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
if (response.body && response.headers.get("content-type")?.includes("text/event-stream")) {
|
|
920
|
+
const reader = response.body.getReader()
|
|
921
|
+
const encoder = new TextEncoder()
|
|
922
|
+
const decoder = new TextDecoder()
|
|
923
|
+
const stream = new ReadableStream({
|
|
924
|
+
async pull(ctrl) {
|
|
925
|
+
const { done, value } = await reader.read()
|
|
926
|
+
if (done) {
|
|
927
|
+
ctrl.close()
|
|
928
|
+
return
|
|
929
|
+
}
|
|
930
|
+
const text = decoder.decode(value, { stream: true })
|
|
931
|
+
ctrl.enqueue(encoder.encode(text.replace(/"role"\s*:\s*""/g, '"role":"assistant"')))
|
|
932
|
+
},
|
|
933
|
+
cancel() {
|
|
934
|
+
reader.cancel()
|
|
935
|
+
},
|
|
936
|
+
})
|
|
937
|
+
return new Response(stream, { headers: response.headers, status: response.status })
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
return response
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
return {
|
|
945
|
+
autoload: input.source === "config",
|
|
946
|
+
options,
|
|
947
|
+
}
|
|
948
|
+
}),
|
|
949
|
+
}
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
const ProviderApiInfo = Schema.Struct({
|
|
953
|
+
id: Schema.String,
|
|
954
|
+
url: Schema.String,
|
|
955
|
+
npm: Schema.String,
|
|
956
|
+
})
|
|
957
|
+
|
|
958
|
+
const ProviderModalities = Schema.Struct({
|
|
959
|
+
text: Schema.Boolean,
|
|
960
|
+
audio: Schema.Boolean,
|
|
961
|
+
image: Schema.Boolean,
|
|
962
|
+
video: Schema.Boolean,
|
|
963
|
+
pdf: Schema.Boolean,
|
|
964
|
+
})
|
|
965
|
+
|
|
966
|
+
const ProviderInterleaved = Schema.Union([
|
|
967
|
+
Schema.Boolean,
|
|
968
|
+
Schema.Struct({
|
|
969
|
+
field: Schema.Literals(["reasoning", "reasoning_content", "reasoning_details"]),
|
|
970
|
+
}),
|
|
971
|
+
])
|
|
972
|
+
|
|
973
|
+
const ProviderCapabilities = Schema.Struct({
|
|
974
|
+
temperature: Schema.Boolean,
|
|
975
|
+
reasoning: Schema.Boolean,
|
|
976
|
+
attachment: Schema.Boolean,
|
|
977
|
+
toolcall: Schema.Boolean,
|
|
978
|
+
input: ProviderModalities,
|
|
979
|
+
output: ProviderModalities,
|
|
980
|
+
interleaved: ProviderInterleaved,
|
|
981
|
+
})
|
|
982
|
+
|
|
983
|
+
const ProviderCacheCost = Schema.Struct({
|
|
984
|
+
read: Schema.Finite,
|
|
985
|
+
write: Schema.Finite,
|
|
986
|
+
})
|
|
987
|
+
|
|
988
|
+
const ProviderCostTier = Schema.Struct({
|
|
989
|
+
input: Schema.Finite,
|
|
990
|
+
output: Schema.Finite,
|
|
991
|
+
cache: ProviderCacheCost,
|
|
992
|
+
tier: Schema.Struct({
|
|
993
|
+
type: Schema.Literal("context"),
|
|
994
|
+
size: Schema.Finite,
|
|
995
|
+
}),
|
|
996
|
+
})
|
|
997
|
+
|
|
998
|
+
const ProviderCost = Schema.Struct({
|
|
999
|
+
input: Schema.Finite,
|
|
1000
|
+
output: Schema.Finite,
|
|
1001
|
+
cache: ProviderCacheCost,
|
|
1002
|
+
tiers: optionalOmitUndefined(Schema.Array(ProviderCostTier)),
|
|
1003
|
+
experimentalOver200K: optionalOmitUndefined(
|
|
1004
|
+
Schema.Struct({
|
|
1005
|
+
input: Schema.Finite,
|
|
1006
|
+
output: Schema.Finite,
|
|
1007
|
+
cache: ProviderCacheCost,
|
|
1008
|
+
}),
|
|
1009
|
+
),
|
|
1010
|
+
})
|
|
1011
|
+
|
|
1012
|
+
const ProviderLimit = Schema.Struct({
|
|
1013
|
+
context: Schema.Finite,
|
|
1014
|
+
input: optionalOmitUndefined(Schema.Finite),
|
|
1015
|
+
output: Schema.Finite,
|
|
1016
|
+
})
|
|
1017
|
+
|
|
1018
|
+
export const Model = Schema.Struct({
|
|
1019
|
+
id: ModelV2.ID,
|
|
1020
|
+
providerID: ProviderV2.ID,
|
|
1021
|
+
api: ProviderApiInfo,
|
|
1022
|
+
name: Schema.String,
|
|
1023
|
+
family: optionalOmitUndefined(Schema.String),
|
|
1024
|
+
capabilities: ProviderCapabilities,
|
|
1025
|
+
cost: ProviderCost,
|
|
1026
|
+
limit: ProviderLimit,
|
|
1027
|
+
status: ModelStatus,
|
|
1028
|
+
options: Schema.Record(Schema.String, Schema.Any),
|
|
1029
|
+
headers: Schema.Record(Schema.String, Schema.String),
|
|
1030
|
+
release_date: Schema.String,
|
|
1031
|
+
variants: optionalOmitUndefined(Schema.Record(Schema.String, Schema.Record(Schema.String, Schema.Any))),
|
|
1032
|
+
}).annotate({ identifier: "Model" })
|
|
1033
|
+
export type Model = Types.DeepMutable<Schema.Schema.Type<typeof Model>>
|
|
1034
|
+
|
|
1035
|
+
export const Info = Schema.Struct({
|
|
1036
|
+
id: ProviderV2.ID,
|
|
1037
|
+
name: Schema.String,
|
|
1038
|
+
source: Schema.Literals(["env", "config", "custom", "api"]),
|
|
1039
|
+
env: Schema.Array(Schema.String),
|
|
1040
|
+
key: optionalOmitUndefined(Schema.String),
|
|
1041
|
+
options: Schema.Record(Schema.String, Schema.Any),
|
|
1042
|
+
models: Schema.Record(Schema.String, Model),
|
|
1043
|
+
}).annotate({ identifier: "Provider" })
|
|
1044
|
+
export type Info = Types.DeepMutable<Schema.Schema.Type<typeof Info>>
|
|
1045
|
+
|
|
1046
|
+
const DefaultModelIDs = Schema.Record(Schema.String, Schema.String)
|
|
1047
|
+
|
|
1048
|
+
export const ListResult = Schema.Struct({
|
|
1049
|
+
all: Schema.Array(Info),
|
|
1050
|
+
default: DefaultModelIDs,
|
|
1051
|
+
connected: Schema.Array(Schema.String),
|
|
1052
|
+
})
|
|
1053
|
+
export type ListResult = Types.DeepMutable<Schema.Schema.Type<typeof ListResult>>
|
|
1054
|
+
|
|
1055
|
+
export const ConfigProvidersResult = Schema.Struct({
|
|
1056
|
+
providers: Schema.Array(Info),
|
|
1057
|
+
default: DefaultModelIDs,
|
|
1058
|
+
})
|
|
1059
|
+
export type ConfigProvidersResult = Types.DeepMutable<Schema.Schema.Type<typeof ConfigProvidersResult>>
|
|
1060
|
+
|
|
1061
|
+
export function toPublicInfo(provider: Info): Info {
|
|
1062
|
+
return JSON.parse(
|
|
1063
|
+
JSON.stringify(provider, (_, value) => {
|
|
1064
|
+
if (typeof value === "function" || typeof value === "symbol" || value === undefined) return undefined
|
|
1065
|
+
if (typeof value === "bigint") return value.toString()
|
|
1066
|
+
return value
|
|
1067
|
+
}),
|
|
1068
|
+
)
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
export function defaultModelIDs<T extends { models: Record<string, { id: string }> }>(providers: Record<string, T>) {
|
|
1072
|
+
return mapValues(providers, (item) => sort(Object.values(item.models))[0].id)
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
export class ModelNotFoundError extends Schema.TaggedErrorClass<ModelNotFoundError>()("ProviderModelNotFoundError", {
|
|
1076
|
+
providerID: ProviderV2.ID,
|
|
1077
|
+
modelID: ModelV2.ID,
|
|
1078
|
+
suggestions: Schema.optional(Schema.Array(Schema.String)),
|
|
1079
|
+
cause: Schema.optional(Schema.Defect),
|
|
1080
|
+
}) {
|
|
1081
|
+
static isInstance(input: unknown): input is ModelNotFoundError {
|
|
1082
|
+
return input instanceof ModelNotFoundError
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
export class InitError extends Schema.TaggedErrorClass<InitError>()("ProviderInitError", {
|
|
1087
|
+
providerID: ProviderV2.ID,
|
|
1088
|
+
cause: Schema.optional(Schema.Defect),
|
|
1089
|
+
}) {
|
|
1090
|
+
static isInstance(input: unknown): input is InitError {
|
|
1091
|
+
return input instanceof InitError
|
|
1092
|
+
}
|
|
1093
|
+
}
|
|
1094
|
+
|
|
1095
|
+
export class NoProvidersError extends Schema.TaggedErrorClass<NoProvidersError>()("ProviderNoProvidersError", {}) {
|
|
1096
|
+
static isInstance(input: unknown): input is NoProvidersError {
|
|
1097
|
+
return input instanceof NoProvidersError
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
export class NoModelsError extends Schema.TaggedErrorClass<NoModelsError>()("ProviderNoModelsError", {
|
|
1102
|
+
providerID: ProviderV2.ID,
|
|
1103
|
+
}) {
|
|
1104
|
+
static isInstance(input: unknown): input is NoModelsError {
|
|
1105
|
+
return input instanceof NoModelsError
|
|
1106
|
+
}
|
|
1107
|
+
}
|
|
1108
|
+
|
|
1109
|
+
export type DefaultModelError = ModelNotFoundError | NoProvidersError | NoModelsError
|
|
1110
|
+
export type Error = ModelNotFoundError | InitError | NoProvidersError | NoModelsError
|
|
1111
|
+
|
|
1112
|
+
export interface Interface {
|
|
1113
|
+
readonly list: () => Effect.Effect<Record<ProviderV2.ID, Info>>
|
|
1114
|
+
readonly getProvider: (providerID: ProviderV2.ID) => Effect.Effect<Info>
|
|
1115
|
+
readonly getModel: (providerID: ProviderV2.ID, modelID: ModelV2.ID) => Effect.Effect<Model, ModelNotFoundError>
|
|
1116
|
+
readonly getLanguage: (model: Model) => Effect.Effect<LanguageModelV3, ModelNotFoundError>
|
|
1117
|
+
readonly closest: (
|
|
1118
|
+
providerID: ProviderV2.ID,
|
|
1119
|
+
query: string[],
|
|
1120
|
+
) => Effect.Effect<{ providerID: ProviderV2.ID; modelID: string } | undefined>
|
|
1121
|
+
readonly getSmallModel: (providerID: ProviderV2.ID) => Effect.Effect<Model | undefined>
|
|
1122
|
+
readonly defaultModel: () => Effect.Effect<{ providerID: ProviderV2.ID; modelID: ModelV2.ID }, DefaultModelError>
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
interface State {
|
|
1126
|
+
models: Map<string, LanguageModelV3>
|
|
1127
|
+
providers: Record<ProviderV2.ID, Info>
|
|
1128
|
+
catalog: Record<ProviderV2.ID, Info>
|
|
1129
|
+
sdk: Map<string, BundledSDK>
|
|
1130
|
+
modelLoaders: Record<string, CustomModelLoader>
|
|
1131
|
+
varsLoaders: Record<string, CustomVarsLoader>
|
|
1132
|
+
}
|
|
1133
|
+
|
|
1134
|
+
export class Service extends Context.Service<Service, Interface>()("@miaw/Provider") {}
|
|
1135
|
+
|
|
1136
|
+
export const use = serviceUse(Service)
|
|
1137
|
+
|
|
1138
|
+
function cost(c: ModelsDev.Model["cost"]): Model["cost"] {
|
|
1139
|
+
const result: Model["cost"] = {
|
|
1140
|
+
input: c?.input ?? 0,
|
|
1141
|
+
output: c?.output ?? 0,
|
|
1142
|
+
cache: {
|
|
1143
|
+
read: c?.cache_read ?? 0,
|
|
1144
|
+
write: c?.cache_write ?? 0,
|
|
1145
|
+
},
|
|
1146
|
+
}
|
|
1147
|
+
if (c?.tiers) {
|
|
1148
|
+
result.tiers = c.tiers.map((item) => ({
|
|
1149
|
+
input: item.input,
|
|
1150
|
+
output: item.output,
|
|
1151
|
+
cache: {
|
|
1152
|
+
read: item.cache_read ?? 0,
|
|
1153
|
+
write: item.cache_write ?? 0,
|
|
1154
|
+
},
|
|
1155
|
+
tier: item.tier,
|
|
1156
|
+
}))
|
|
1157
|
+
}
|
|
1158
|
+
if (c?.context_over_200k) {
|
|
1159
|
+
result.experimentalOver200K = {
|
|
1160
|
+
cache: {
|
|
1161
|
+
read: c.context_over_200k.cache_read ?? 0,
|
|
1162
|
+
write: c.context_over_200k.cache_write ?? 0,
|
|
1163
|
+
},
|
|
1164
|
+
input: c.context_over_200k.input,
|
|
1165
|
+
output: c.context_over_200k.output,
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
return result
|
|
1169
|
+
}
|
|
1170
|
+
|
|
1171
|
+
function fromModelsDevModel(provider: ModelsDev.Provider, model: ModelsDev.Model): Model {
|
|
1172
|
+
const base: Model = {
|
|
1173
|
+
id: ModelV2.ID.make(model.id),
|
|
1174
|
+
providerID: ProviderV2.ID.make(provider.id),
|
|
1175
|
+
name: model.name,
|
|
1176
|
+
family: model.family,
|
|
1177
|
+
api: {
|
|
1178
|
+
id: model.id,
|
|
1179
|
+
url: model.provider?.api ?? provider.api ?? "",
|
|
1180
|
+
npm: model.provider?.npm ?? provider.npm ?? "@ai-sdk/openai-compatible",
|
|
1181
|
+
},
|
|
1182
|
+
status: model.status ?? "active",
|
|
1183
|
+
headers: {},
|
|
1184
|
+
options: {},
|
|
1185
|
+
cost: cost(model.cost),
|
|
1186
|
+
limit: {
|
|
1187
|
+
context: model.limit.context,
|
|
1188
|
+
input: model.limit.input,
|
|
1189
|
+
output: model.limit.output,
|
|
1190
|
+
},
|
|
1191
|
+
capabilities: {
|
|
1192
|
+
temperature: model.temperature ?? false,
|
|
1193
|
+
reasoning: model.reasoning ?? false,
|
|
1194
|
+
attachment: model.attachment ?? false,
|
|
1195
|
+
toolcall: model.tool_call ?? true,
|
|
1196
|
+
input: {
|
|
1197
|
+
text: model.modalities?.input?.includes("text") ?? false,
|
|
1198
|
+
audio: model.modalities?.input?.includes("audio") ?? false,
|
|
1199
|
+
image: model.modalities?.input?.includes("image") ?? false,
|
|
1200
|
+
video: model.modalities?.input?.includes("video") ?? false,
|
|
1201
|
+
pdf: model.modalities?.input?.includes("pdf") ?? false,
|
|
1202
|
+
},
|
|
1203
|
+
output: {
|
|
1204
|
+
text: model.modalities?.output?.includes("text") ?? false,
|
|
1205
|
+
audio: model.modalities?.output?.includes("audio") ?? false,
|
|
1206
|
+
image: model.modalities?.output?.includes("image") ?? false,
|
|
1207
|
+
video: model.modalities?.output?.includes("video") ?? false,
|
|
1208
|
+
pdf: model.modalities?.output?.includes("pdf") ?? false,
|
|
1209
|
+
},
|
|
1210
|
+
interleaved: model.interleaved ?? false,
|
|
1211
|
+
},
|
|
1212
|
+
release_date: model.release_date ?? "",
|
|
1213
|
+
variants: {},
|
|
1214
|
+
}
|
|
1215
|
+
|
|
1216
|
+
return {
|
|
1217
|
+
...base,
|
|
1218
|
+
variants: mapValues(ProviderTransform.variants(base), (v) => v),
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
|
|
1222
|
+
export function fromModelsDevProvider(provider: ModelsDev.Provider): Info {
|
|
1223
|
+
const models: Record<string, Model> = {}
|
|
1224
|
+
for (const [key, model] of Object.entries(provider.models)) {
|
|
1225
|
+
models[key] = fromModelsDevModel(provider, model)
|
|
1226
|
+
for (const [mode, opts] of Object.entries(model.experimental?.modes ?? {})) {
|
|
1227
|
+
const id = `${model.id}-${mode}`
|
|
1228
|
+
const base = fromModelsDevModel(provider, model)
|
|
1229
|
+
models[id] = {
|
|
1230
|
+
...base,
|
|
1231
|
+
id: ModelV2.ID.make(id),
|
|
1232
|
+
name: `${model.name} ${mode[0].toUpperCase()}${mode.slice(1)}`,
|
|
1233
|
+
cost: opts.cost ? mergeDeep(base.cost, cost(opts.cost)) : base.cost,
|
|
1234
|
+
options: opts.provider?.body
|
|
1235
|
+
? Object.fromEntries(
|
|
1236
|
+
Object.entries(opts.provider.body).map(([k, v]) => [
|
|
1237
|
+
k.replace(/_([a-z])/g, (_, c) => c.toUpperCase()),
|
|
1238
|
+
v,
|
|
1239
|
+
]),
|
|
1240
|
+
)
|
|
1241
|
+
: base.options,
|
|
1242
|
+
headers: opts.provider?.headers ?? base.headers,
|
|
1243
|
+
}
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
return {
|
|
1247
|
+
id: ProviderV2.ID.make(provider.id),
|
|
1248
|
+
source: "custom",
|
|
1249
|
+
name: provider.name,
|
|
1250
|
+
env: [...(provider.env ?? [])],
|
|
1251
|
+
options: {},
|
|
1252
|
+
models,
|
|
1253
|
+
}
|
|
1254
|
+
}
|
|
1255
|
+
|
|
1256
|
+
function modelSuggestions(provider: Info | undefined, modelID: ModelV2.ID, enableExperimentalModels: boolean) {
|
|
1257
|
+
const available = provider
|
|
1258
|
+
? Object.keys(provider.models).filter((id) => {
|
|
1259
|
+
const model = provider.models[id]
|
|
1260
|
+
if (model.status === "deprecated") return false
|
|
1261
|
+
if (model.status === "alpha" && !enableExperimentalModels) return false
|
|
1262
|
+
return true
|
|
1263
|
+
})
|
|
1264
|
+
: []
|
|
1265
|
+
const fuzzy = fuzzysort.go(modelID, available, { limit: 3, threshold: -10000 }).map((m) => m.target)
|
|
1266
|
+
if (fuzzy.length) return fuzzy
|
|
1267
|
+
const query = modelID
|
|
1268
|
+
.toLowerCase()
|
|
1269
|
+
.split(/[^a-z0-9]+/)
|
|
1270
|
+
.filter((part) => part.length > 1)
|
|
1271
|
+
return sortBy(
|
|
1272
|
+
available
|
|
1273
|
+
.map((id) => ({
|
|
1274
|
+
id,
|
|
1275
|
+
score: query.filter((part) => id.toLowerCase().includes(part)).length,
|
|
1276
|
+
}))
|
|
1277
|
+
.filter((item) => item.score > 0),
|
|
1278
|
+
[(item) => item.score, "desc"],
|
|
1279
|
+
[(item) => item.id, "asc"],
|
|
1280
|
+
)
|
|
1281
|
+
.slice(0, 3)
|
|
1282
|
+
.map((item) => item.id)
|
|
1283
|
+
}
|
|
1284
|
+
|
|
1285
|
+
export const layer = Layer.effect(
|
|
1286
|
+
Service,
|
|
1287
|
+
Effect.gen(function* () {
|
|
1288
|
+
const fs = yield* FSUtil.Service
|
|
1289
|
+
const config = yield* Config.Service
|
|
1290
|
+
const auth = yield* Auth.Service
|
|
1291
|
+
const env = yield* Env.Service
|
|
1292
|
+
const plugin = yield* Plugin.Service
|
|
1293
|
+
const modelsDevSvc = yield* ModelsDev.Service
|
|
1294
|
+
const runtimeFlags = yield* RuntimeFlags.Service
|
|
1295
|
+
|
|
1296
|
+
const state = yield* InstanceState.make<State>(() =>
|
|
1297
|
+
Effect.gen(function* () {
|
|
1298
|
+
const bridge = yield* EffectBridge.make()
|
|
1299
|
+
const cfg = yield* config.get()
|
|
1300
|
+
const modelsDev = yield* modelsDevSvc.get()
|
|
1301
|
+
const catalog = mapValues(modelsDev, fromModelsDevProvider)
|
|
1302
|
+
const database = mapValues(catalog, toPublicInfo)
|
|
1303
|
+
|
|
1304
|
+
const providers: Record<ProviderV2.ID, Info> = {} as Record<ProviderV2.ID, Info>
|
|
1305
|
+
const languages = new Map<string, LanguageModelV3>()
|
|
1306
|
+
const modelLoaders: {
|
|
1307
|
+
[providerID: string]: CustomModelLoader
|
|
1308
|
+
} = {}
|
|
1309
|
+
const varsLoaders: {
|
|
1310
|
+
[providerID: string]: CustomVarsLoader
|
|
1311
|
+
} = {}
|
|
1312
|
+
const sdk = new Map<string, BundledSDK>()
|
|
1313
|
+
const discoveryLoaders: {
|
|
1314
|
+
[providerID: string]: CustomDiscoverModels
|
|
1315
|
+
} = {}
|
|
1316
|
+
const dep = {
|
|
1317
|
+
auth: (id: string) => auth.get(id).pipe(Effect.orDie),
|
|
1318
|
+
config: () => config.get(),
|
|
1319
|
+
env: () => env.all(),
|
|
1320
|
+
get: (key: string) => env.get(key),
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
function mergeProvider(providerID: ProviderV2.ID, provider: Partial<Info>) {
|
|
1324
|
+
const existing = providers[providerID]
|
|
1325
|
+
if (existing) {
|
|
1326
|
+
// @ts-expect-error
|
|
1327
|
+
providers[providerID] = mergeDeep(existing, provider)
|
|
1328
|
+
return
|
|
1329
|
+
}
|
|
1330
|
+
const match = database[providerID]
|
|
1331
|
+
if (!match) return
|
|
1332
|
+
// @ts-expect-error
|
|
1333
|
+
providers[providerID] = mergeDeep(match, provider)
|
|
1334
|
+
}
|
|
1335
|
+
|
|
1336
|
+
// load plugins first so config() hook runs before reading cfg.provider
|
|
1337
|
+
const plugins = yield* plugin.list()
|
|
1338
|
+
|
|
1339
|
+
// now read config providers - includes any modifications from plugin config() hook
|
|
1340
|
+
const configProviders = Object.entries(cfg.provider ?? {})
|
|
1341
|
+
const disabled = new Set(cfg.disabled_providers ?? [])
|
|
1342
|
+
const enabled = cfg.enabled_providers ? new Set(cfg.enabled_providers) : null
|
|
1343
|
+
|
|
1344
|
+
function isProviderAllowed(providerID: ProviderV2.ID): boolean {
|
|
1345
|
+
if (enabled && !enabled.has(providerID)) return false
|
|
1346
|
+
if (disabled.has(providerID)) return false
|
|
1347
|
+
return true
|
|
1348
|
+
}
|
|
1349
|
+
|
|
1350
|
+
for (const hook of plugins) {
|
|
1351
|
+
const p = hook.provider
|
|
1352
|
+
const models = p?.models
|
|
1353
|
+
if (!p || !models) continue
|
|
1354
|
+
|
|
1355
|
+
const providerID = ProviderV2.ID.make(p.id)
|
|
1356
|
+
if (disabled.has(providerID)) continue
|
|
1357
|
+
|
|
1358
|
+
const provider = database[providerID]
|
|
1359
|
+
if (!provider) continue
|
|
1360
|
+
const pluginAuth = yield* auth.get(providerID).pipe(Effect.orDie)
|
|
1361
|
+
|
|
1362
|
+
provider.models = yield* Effect.promise(async () => {
|
|
1363
|
+
const next = await models(toPublicInfo(provider), { auth: pluginAuth })
|
|
1364
|
+
return Object.fromEntries(
|
|
1365
|
+
Object.entries(next).map(([id, model]) => [
|
|
1366
|
+
id,
|
|
1367
|
+
{
|
|
1368
|
+
...model,
|
|
1369
|
+
id: ModelV2.ID.make(id),
|
|
1370
|
+
providerID,
|
|
1371
|
+
},
|
|
1372
|
+
]),
|
|
1373
|
+
)
|
|
1374
|
+
})
|
|
1375
|
+
}
|
|
1376
|
+
|
|
1377
|
+
// extend database from config
|
|
1378
|
+
for (const [providerID, provider] of configProviders) {
|
|
1379
|
+
const existing = database[providerID]
|
|
1380
|
+
const parsed: Info = {
|
|
1381
|
+
id: ProviderV2.ID.make(providerID),
|
|
1382
|
+
name: provider.name ?? existing?.name ?? providerID,
|
|
1383
|
+
env: provider.env ?? existing?.env ?? [],
|
|
1384
|
+
options: mergeDeep(existing?.options ?? {}, provider.options ?? {}),
|
|
1385
|
+
source: "config",
|
|
1386
|
+
models: existing?.models ?? {},
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1389
|
+
for (const [modelID, model] of Object.entries(provider.models ?? {})) {
|
|
1390
|
+
const existingModel = parsed.models[model.id ?? modelID]
|
|
1391
|
+
const apiID = model.id ?? existingModel?.api.id ?? modelID
|
|
1392
|
+
const apiNpm =
|
|
1393
|
+
model.provider?.npm ??
|
|
1394
|
+
provider.npm ??
|
|
1395
|
+
existingModel?.api.npm ??
|
|
1396
|
+
modelsDev[providerID]?.npm ??
|
|
1397
|
+
"@ai-sdk/openai-compatible"
|
|
1398
|
+
const name = iife(() => {
|
|
1399
|
+
if (model.name) return model.name
|
|
1400
|
+
if (model.id && model.id !== modelID) return modelID
|
|
1401
|
+
return existingModel?.name ?? modelID
|
|
1402
|
+
})
|
|
1403
|
+
const parsedModel: Model = {
|
|
1404
|
+
id: ModelV2.ID.make(modelID),
|
|
1405
|
+
api: {
|
|
1406
|
+
id: apiID,
|
|
1407
|
+
npm: apiNpm,
|
|
1408
|
+
url: model.provider?.api ?? provider?.api ?? existingModel?.api.url ?? modelsDev[providerID]?.api ?? "",
|
|
1409
|
+
},
|
|
1410
|
+
status: model.status ?? existingModel?.status ?? "active",
|
|
1411
|
+
name,
|
|
1412
|
+
providerID: ProviderV2.ID.make(providerID),
|
|
1413
|
+
capabilities: {
|
|
1414
|
+
temperature: model.temperature ?? existingModel?.capabilities.temperature ?? false,
|
|
1415
|
+
reasoning: model.reasoning ?? existingModel?.capabilities.reasoning ?? false,
|
|
1416
|
+
attachment: model.attachment ?? existingModel?.capabilities.attachment ?? false,
|
|
1417
|
+
toolcall: model.tool_call ?? existingModel?.capabilities.toolcall ?? true,
|
|
1418
|
+
input: {
|
|
1419
|
+
text: model.modalities?.input?.includes("text") ?? existingModel?.capabilities.input.text ?? true,
|
|
1420
|
+
audio: model.modalities?.input?.includes("audio") ?? existingModel?.capabilities.input.audio ?? false,
|
|
1421
|
+
image: model.modalities?.input?.includes("image") ?? existingModel?.capabilities.input.image ?? false,
|
|
1422
|
+
video: model.modalities?.input?.includes("video") ?? existingModel?.capabilities.input.video ?? false,
|
|
1423
|
+
pdf: model.modalities?.input?.includes("pdf") ?? existingModel?.capabilities.input.pdf ?? false,
|
|
1424
|
+
},
|
|
1425
|
+
output: {
|
|
1426
|
+
text: model.modalities?.output?.includes("text") ?? existingModel?.capabilities.output.text ?? true,
|
|
1427
|
+
audio:
|
|
1428
|
+
model.modalities?.output?.includes("audio") ?? existingModel?.capabilities.output.audio ?? false,
|
|
1429
|
+
image:
|
|
1430
|
+
model.modalities?.output?.includes("image") ?? existingModel?.capabilities.output.image ?? false,
|
|
1431
|
+
video:
|
|
1432
|
+
model.modalities?.output?.includes("video") ?? existingModel?.capabilities.output.video ?? false,
|
|
1433
|
+
pdf: model.modalities?.output?.includes("pdf") ?? existingModel?.capabilities.output.pdf ?? false,
|
|
1434
|
+
},
|
|
1435
|
+
interleaved:
|
|
1436
|
+
model.interleaved ??
|
|
1437
|
+
existingModel?.capabilities.interleaved ??
|
|
1438
|
+
(!existingModel && apiNpm === "@ai-sdk/openai-compatible" && apiID.includes("deepseek")
|
|
1439
|
+
? { field: "reasoning_content" }
|
|
1440
|
+
: false),
|
|
1441
|
+
},
|
|
1442
|
+
cost: {
|
|
1443
|
+
input: model?.cost?.input ?? existingModel?.cost?.input ?? 0,
|
|
1444
|
+
output: model?.cost?.output ?? existingModel?.cost?.output ?? 0,
|
|
1445
|
+
cache: {
|
|
1446
|
+
read: model?.cost?.cache_read ?? existingModel?.cost?.cache.read ?? 0,
|
|
1447
|
+
write: model?.cost?.cache_write ?? existingModel?.cost?.cache.write ?? 0,
|
|
1448
|
+
},
|
|
1449
|
+
},
|
|
1450
|
+
options: mergeDeep(existingModel?.options ?? {}, model.options ?? {}),
|
|
1451
|
+
limit: {
|
|
1452
|
+
context: model.limit?.context ?? existingModel?.limit?.context ?? 0,
|
|
1453
|
+
input: model.limit?.input ?? existingModel?.limit?.input,
|
|
1454
|
+
output: model.limit?.output ?? existingModel?.limit?.output ?? 0,
|
|
1455
|
+
},
|
|
1456
|
+
headers: mergeDeep(existingModel?.headers ?? {}, model.headers ?? {}),
|
|
1457
|
+
family: model.family ?? existingModel?.family ?? "",
|
|
1458
|
+
release_date: model.release_date ?? existingModel?.release_date ?? "",
|
|
1459
|
+
variants: {},
|
|
1460
|
+
}
|
|
1461
|
+
const merged = mergeDeep(ProviderTransform.variants(parsedModel), model.variants ?? {})
|
|
1462
|
+
parsedModel.variants = mapValues(
|
|
1463
|
+
pickBy(merged, (v) => !v.disabled),
|
|
1464
|
+
(v) => omit(v, ["disabled"]),
|
|
1465
|
+
)
|
|
1466
|
+
parsed.models[modelID] = parsedModel
|
|
1467
|
+
}
|
|
1468
|
+
database[providerID] = parsed
|
|
1469
|
+
}
|
|
1470
|
+
|
|
1471
|
+
// load env
|
|
1472
|
+
const envs = yield* env.all()
|
|
1473
|
+
for (const [id, provider] of Object.entries(database)) {
|
|
1474
|
+
const providerID = ProviderV2.ID.make(id)
|
|
1475
|
+
if (disabled.has(providerID)) continue
|
|
1476
|
+
const apiKey = provider.env.map((item) => envs[item]).find(Boolean)
|
|
1477
|
+
if (!apiKey) continue
|
|
1478
|
+
mergeProvider(providerID, {
|
|
1479
|
+
source: "env",
|
|
1480
|
+
key: provider.env.length === 1 ? apiKey : undefined,
|
|
1481
|
+
})
|
|
1482
|
+
}
|
|
1483
|
+
|
|
1484
|
+
// load apikeys
|
|
1485
|
+
const auths = yield* auth.all().pipe(Effect.orDie)
|
|
1486
|
+
for (const [id, provider] of Object.entries(auths)) {
|
|
1487
|
+
const providerID = ProviderV2.ID.make(id)
|
|
1488
|
+
if (disabled.has(providerID)) continue
|
|
1489
|
+
if (provider.type === "api") {
|
|
1490
|
+
mergeProvider(providerID, {
|
|
1491
|
+
source: "api",
|
|
1492
|
+
key: provider.key,
|
|
1493
|
+
})
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
|
|
1497
|
+
// plugin auth loader - database now has entries for config providers
|
|
1498
|
+
for (const plugin of plugins) {
|
|
1499
|
+
if (!plugin.auth) continue
|
|
1500
|
+
const providerID = ProviderV2.ID.make(plugin.auth.provider)
|
|
1501
|
+
if (disabled.has(providerID)) continue
|
|
1502
|
+
|
|
1503
|
+
const stored = yield* auth.get(providerID).pipe(Effect.orDie)
|
|
1504
|
+
if (!stored) continue
|
|
1505
|
+
if (!plugin.auth.loader) continue
|
|
1506
|
+
|
|
1507
|
+
const options = yield* Effect.promise(() =>
|
|
1508
|
+
plugin.auth!.loader!(
|
|
1509
|
+
() => bridge.promise(auth.get(providerID).pipe(Effect.orDie)) as any,
|
|
1510
|
+
toPublicInfo(database[plugin.auth!.provider]),
|
|
1511
|
+
),
|
|
1512
|
+
)
|
|
1513
|
+
const opts = options ?? {}
|
|
1514
|
+
const patch: Partial<Info> = providers[providerID] ? { options: opts } : { source: "custom", options: opts }
|
|
1515
|
+
mergeProvider(providerID, patch)
|
|
1516
|
+
}
|
|
1517
|
+
|
|
1518
|
+
for (const [id, fn] of Object.entries(custom(dep))) {
|
|
1519
|
+
const providerID = ProviderV2.ID.make(id)
|
|
1520
|
+
if (disabled.has(providerID)) continue
|
|
1521
|
+
const data = database[providerID]
|
|
1522
|
+
if (!data) {
|
|
1523
|
+
continue
|
|
1524
|
+
}
|
|
1525
|
+
const result = yield* fn(data)
|
|
1526
|
+
if (result && (result.autoload || providers[providerID])) {
|
|
1527
|
+
if (result.getModel) modelLoaders[providerID] = result.getModel
|
|
1528
|
+
if (result.vars) varsLoaders[providerID] = result.vars
|
|
1529
|
+
if (result.discoverModels) discoveryLoaders[providerID] = result.discoverModels
|
|
1530
|
+
const opts = result.options ?? {}
|
|
1531
|
+
const patch: Partial<Info> = providers[providerID] ? { options: opts } : { source: "custom", options: opts }
|
|
1532
|
+
mergeProvider(providerID, patch)
|
|
1533
|
+
}
|
|
1534
|
+
}
|
|
1535
|
+
|
|
1536
|
+
// load config - re-apply with updated data
|
|
1537
|
+
for (const [id, provider] of configProviders) {
|
|
1538
|
+
const providerID = ProviderV2.ID.make(id)
|
|
1539
|
+
const partial: Partial<Info> = { source: "config" }
|
|
1540
|
+
if (provider.env) partial.env = provider.env
|
|
1541
|
+
if (provider.name) partial.name = provider.name
|
|
1542
|
+
if (provider.options) partial.options = provider.options
|
|
1543
|
+
mergeProvider(providerID, partial)
|
|
1544
|
+
}
|
|
1545
|
+
|
|
1546
|
+
const gitlab = ProviderV2.ID.make("gitlab")
|
|
1547
|
+
if (discoveryLoaders[gitlab] && providers[gitlab] && isProviderAllowed(gitlab)) {
|
|
1548
|
+
yield* Effect.promise(async () => {
|
|
1549
|
+
try {
|
|
1550
|
+
const discovered = await discoveryLoaders[gitlab]()
|
|
1551
|
+
for (const [modelID, model] of Object.entries(discovered)) {
|
|
1552
|
+
if (!providers[gitlab].models[modelID]) {
|
|
1553
|
+
providers[gitlab].models[modelID] = model
|
|
1554
|
+
}
|
|
1555
|
+
}
|
|
1556
|
+
} catch (e) {}
|
|
1557
|
+
})
|
|
1558
|
+
}
|
|
1559
|
+
|
|
1560
|
+
for (const [id, provider] of Object.entries(providers)) {
|
|
1561
|
+
const providerID = ProviderV2.ID.make(id)
|
|
1562
|
+
if (!isProviderAllowed(providerID)) {
|
|
1563
|
+
delete providers[providerID]
|
|
1564
|
+
continue
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
const configProvider = cfg.provider?.[providerID]
|
|
1568
|
+
|
|
1569
|
+
for (const [modelID, model] of Object.entries(provider.models)) {
|
|
1570
|
+
model.api.id = model.api.id ?? model.id ?? modelID
|
|
1571
|
+
if (
|
|
1572
|
+
// These chat aliases are invalid for the special handling in the
|
|
1573
|
+
// built-in providers below, but custom providers may support them.
|
|
1574
|
+
(modelID === "gpt-5-chat-latest" &&
|
|
1575
|
+
(providerID === ProviderV2.ID.openai ||
|
|
1576
|
+
providerID === ProviderV2.ID.githubCopilot ||
|
|
1577
|
+
providerID === ProviderV2.ID.openrouter)) ||
|
|
1578
|
+
(providerID === ProviderV2.ID.openrouter && modelID === "openai/gpt-5-chat")
|
|
1579
|
+
)
|
|
1580
|
+
delete provider.models[modelID]
|
|
1581
|
+
if (model.status === "alpha" && !runtimeFlags.enableExperimentalModels) delete provider.models[modelID]
|
|
1582
|
+
if (model.status === "deprecated") delete provider.models[modelID]
|
|
1583
|
+
if (
|
|
1584
|
+
(configProvider?.blacklist && configProvider.blacklist.includes(modelID)) ||
|
|
1585
|
+
(configProvider?.whitelist && !configProvider.whitelist.includes(modelID))
|
|
1586
|
+
)
|
|
1587
|
+
delete provider.models[modelID]
|
|
1588
|
+
|
|
1589
|
+
if (!model.variants || Object.keys(model.variants).length === 0) {
|
|
1590
|
+
model.variants = mapValues(ProviderTransform.variants(model), (v) => v)
|
|
1591
|
+
}
|
|
1592
|
+
|
|
1593
|
+
const configVariants = configProvider?.models?.[modelID]?.variants
|
|
1594
|
+
if (configVariants && model.variants) {
|
|
1595
|
+
const merged = mergeDeep(model.variants, configVariants)
|
|
1596
|
+
model.variants = mapValues(
|
|
1597
|
+
pickBy(merged, (v) => !v.disabled),
|
|
1598
|
+
(v) => omit(v, ["disabled"]),
|
|
1599
|
+
)
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
if (Object.keys(provider.models).length === 0) {
|
|
1604
|
+
delete providers[providerID]
|
|
1605
|
+
continue
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
return {
|
|
1610
|
+
models: languages,
|
|
1611
|
+
providers,
|
|
1612
|
+
catalog,
|
|
1613
|
+
sdk,
|
|
1614
|
+
modelLoaders,
|
|
1615
|
+
varsLoaders,
|
|
1616
|
+
}
|
|
1617
|
+
}),
|
|
1618
|
+
)
|
|
1619
|
+
|
|
1620
|
+
const list = Effect.fn("Provider.list")(() => InstanceState.use(state, (s) => s.providers))
|
|
1621
|
+
|
|
1622
|
+
async function resolveSDK(model: Model, s: State, envs: Record<string, string | undefined>) {
|
|
1623
|
+
try {
|
|
1624
|
+
const provider = s.providers[model.providerID]
|
|
1625
|
+
const options = { ...provider.options }
|
|
1626
|
+
|
|
1627
|
+
if (
|
|
1628
|
+
model.providerID === "google-vertex" &&
|
|
1629
|
+
model.api.npm === "@ai-sdk/google-vertex/anthropic" &&
|
|
1630
|
+
!options.baseURL
|
|
1631
|
+
) {
|
|
1632
|
+
const baseURL = googleVertexAnthropicBaseURL(
|
|
1633
|
+
typeof options.project === "string" ? options.project : undefined,
|
|
1634
|
+
typeof options.location === "string" ? options.location : undefined,
|
|
1635
|
+
)
|
|
1636
|
+
if (baseURL) options.baseURL = baseURL
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
if (model.providerID === "google-vertex" && !model.api.npm.includes("@ai-sdk/openai-compatible")) {
|
|
1640
|
+
delete options.fetch
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
if (model.api.npm.includes("@ai-sdk/openai-compatible") && options["includeUsage"] !== false) {
|
|
1644
|
+
options["includeUsage"] = true
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
const baseURL = iife(() => {
|
|
1648
|
+
let url =
|
|
1649
|
+
typeof options["baseURL"] === "string" && options["baseURL"] !== "" ? options["baseURL"] : model.api.url
|
|
1650
|
+
if (!url) return
|
|
1651
|
+
|
|
1652
|
+
const loader = s.varsLoaders[model.providerID]
|
|
1653
|
+
if (loader) {
|
|
1654
|
+
const vars = loader(options)
|
|
1655
|
+
for (const [key, value] of Object.entries(vars)) {
|
|
1656
|
+
const field = "${" + key + "}"
|
|
1657
|
+
url = url.replaceAll(field, value)
|
|
1658
|
+
}
|
|
1659
|
+
}
|
|
1660
|
+
|
|
1661
|
+
url = url.replace(/\$\{([^}]+)\}/g, (item, key) => {
|
|
1662
|
+
const val = envs[String(key)]
|
|
1663
|
+
return val ?? item
|
|
1664
|
+
})
|
|
1665
|
+
return url
|
|
1666
|
+
})
|
|
1667
|
+
|
|
1668
|
+
if (baseURL !== undefined) options["baseURL"] = baseURL
|
|
1669
|
+
if (options["apiKey"] === undefined && provider.key) options["apiKey"] = provider.key
|
|
1670
|
+
if (model.headers)
|
|
1671
|
+
options["headers"] = {
|
|
1672
|
+
...options["headers"],
|
|
1673
|
+
...model.headers,
|
|
1674
|
+
}
|
|
1675
|
+
|
|
1676
|
+
const key = Hash.fast(
|
|
1677
|
+
JSON.stringify({
|
|
1678
|
+
providerID: model.providerID,
|
|
1679
|
+
npm: model.api.npm,
|
|
1680
|
+
options,
|
|
1681
|
+
}),
|
|
1682
|
+
)
|
|
1683
|
+
const existing = s.sdk.get(key)
|
|
1684
|
+
if (existing) return existing
|
|
1685
|
+
|
|
1686
|
+
const customFetch = options["fetch"]
|
|
1687
|
+
const chunkTimeout = options["chunkTimeout"]
|
|
1688
|
+
const headerTimeout = options["headerTimeout"]
|
|
1689
|
+
delete options["chunkTimeout"]
|
|
1690
|
+
delete options["headerTimeout"]
|
|
1691
|
+
|
|
1692
|
+
options["fetch"] = async (input: any, init?: BunFetchRequestInit) => {
|
|
1693
|
+
const fetchFn = customFetch ?? fetch
|
|
1694
|
+
const opts = init ?? {}
|
|
1695
|
+
const chunkAbortCtl = typeof chunkTimeout === "number" && chunkTimeout > 0 ? new AbortController() : undefined
|
|
1696
|
+
const headerTimeoutMs = headerTimeout === false ? undefined : headerTimeout
|
|
1697
|
+
const headerTimeoutCtl = typeof headerTimeoutMs === "number" ? timeoutController(headerTimeoutMs) : undefined
|
|
1698
|
+
const signals: AbortSignal[] = []
|
|
1699
|
+
|
|
1700
|
+
if (opts.signal) signals.push(opts.signal)
|
|
1701
|
+
if (chunkAbortCtl) signals.push(chunkAbortCtl.signal)
|
|
1702
|
+
if (headerTimeoutCtl) signals.push(headerTimeoutCtl.signal)
|
|
1703
|
+
if (options["timeout"] !== undefined && options["timeout"] !== null && options["timeout"] !== false)
|
|
1704
|
+
signals.push(AbortSignal.timeout(options["timeout"]))
|
|
1705
|
+
|
|
1706
|
+
const combined = signals.length === 0 ? null : signals.length === 1 ? signals[0] : AbortSignal.any(signals)
|
|
1707
|
+
if (combined) opts.signal = combined
|
|
1708
|
+
|
|
1709
|
+
const res = await fetchFn(input, {
|
|
1710
|
+
...opts,
|
|
1711
|
+
// @ts-ignore see here: https://github.com/oven-sh/bun/issues/16682
|
|
1712
|
+
timeout: false,
|
|
1713
|
+
}).finally(() => headerTimeoutCtl?.clear())
|
|
1714
|
+
|
|
1715
|
+
if (!chunkAbortCtl) return res
|
|
1716
|
+
return wrapSSE(res, chunkTimeout, chunkAbortCtl)
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
const bundledLoader = BUNDLED_PROVIDERS[model.api.npm]
|
|
1720
|
+
if (bundledLoader) {
|
|
1721
|
+
const factory = await bundledLoader()
|
|
1722
|
+
const loaded = factory({
|
|
1723
|
+
name: model.providerID,
|
|
1724
|
+
...options,
|
|
1725
|
+
})
|
|
1726
|
+
s.sdk.set(key, loaded)
|
|
1727
|
+
return loaded as SDK
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1730
|
+
const installedPath = await (async () => {
|
|
1731
|
+
if (model.api.npm.startsWith("file://")) {
|
|
1732
|
+
return model.api.npm
|
|
1733
|
+
}
|
|
1734
|
+
const item = await Npm.add(model.api.npm)
|
|
1735
|
+
if (!item.entrypoint) throw new Error(`Package ${model.api.npm} has no import entrypoint`)
|
|
1736
|
+
return item.entrypoint
|
|
1737
|
+
})()
|
|
1738
|
+
|
|
1739
|
+
// `installedPath` is a local entry path or an existing `file://` URL. Normalize
|
|
1740
|
+
// only path inputs so Node on Windows accepts the dynamic import.
|
|
1741
|
+
const importSpec = installedPath.startsWith("file://") ? installedPath : pathToFileURL(installedPath).href
|
|
1742
|
+
const mod = await import(importSpec)
|
|
1743
|
+
|
|
1744
|
+
const fn = mod[Object.keys(mod).find((key) => key.startsWith("create"))!]
|
|
1745
|
+
const loaded = fn({
|
|
1746
|
+
name: model.providerID,
|
|
1747
|
+
...options,
|
|
1748
|
+
})
|
|
1749
|
+
s.sdk.set(key, loaded)
|
|
1750
|
+
return loaded as SDK
|
|
1751
|
+
} catch (e) {
|
|
1752
|
+
throw new InitError({ providerID: model.providerID, cause: e })
|
|
1753
|
+
}
|
|
1754
|
+
}
|
|
1755
|
+
|
|
1756
|
+
const getProvider = Effect.fn("Provider.getProvider")((providerID: ProviderV2.ID) =>
|
|
1757
|
+
InstanceState.use(state, (s) => s.providers[providerID]),
|
|
1758
|
+
)
|
|
1759
|
+
|
|
1760
|
+
const getModel = Effect.fn("Provider.getModel")(function* (providerID: ProviderV2.ID, modelID: ModelV2.ID) {
|
|
1761
|
+
const s = yield* InstanceState.get(state)
|
|
1762
|
+
const provider = s.providers[providerID]
|
|
1763
|
+
if (!provider) {
|
|
1764
|
+
const catalogProvider = s.catalog[providerID]
|
|
1765
|
+
const suggestions = catalogProvider
|
|
1766
|
+
? modelSuggestions(catalogProvider, modelID, runtimeFlags.enableExperimentalModels)
|
|
1767
|
+
: fuzzysort
|
|
1768
|
+
.go(providerID, Object.keys({ ...s.catalog, ...s.providers }), { limit: 3, threshold: -10000 })
|
|
1769
|
+
.map((m) => m.target)
|
|
1770
|
+
return yield* new ModelNotFoundError({ providerID, modelID, suggestions })
|
|
1771
|
+
}
|
|
1772
|
+
|
|
1773
|
+
const info = provider.models[modelID]
|
|
1774
|
+
if (!info) {
|
|
1775
|
+
const current = modelSuggestions(provider, modelID, runtimeFlags.enableExperimentalModels)
|
|
1776
|
+
const suggestions = current.length
|
|
1777
|
+
? current
|
|
1778
|
+
: modelSuggestions(s.catalog[providerID], modelID, runtimeFlags.enableExperimentalModels)
|
|
1779
|
+
return yield* new ModelNotFoundError({ providerID, modelID, suggestions })
|
|
1780
|
+
}
|
|
1781
|
+
return info
|
|
1782
|
+
})
|
|
1783
|
+
|
|
1784
|
+
const getLanguage = Effect.fn("Provider.getLanguage")(function* (model: Model) {
|
|
1785
|
+
const s = yield* InstanceState.get(state)
|
|
1786
|
+
const envs = yield* env.all()
|
|
1787
|
+
const key = `${model.providerID}/${model.id}`
|
|
1788
|
+
if (s.models.has(key)) return s.models.get(key)!
|
|
1789
|
+
|
|
1790
|
+
const provider = s.providers[model.providerID]
|
|
1791
|
+
return yield* EffectPromise.refineRejection(
|
|
1792
|
+
async () => {
|
|
1793
|
+
const sdk = await resolveSDK(model, s, envs)
|
|
1794
|
+
const language = s.modelLoaders[model.providerID]
|
|
1795
|
+
? await s.modelLoaders[model.providerID](
|
|
1796
|
+
sdk,
|
|
1797
|
+
model.api.id,
|
|
1798
|
+
{
|
|
1799
|
+
...provider.options,
|
|
1800
|
+
...model.options,
|
|
1801
|
+
},
|
|
1802
|
+
model,
|
|
1803
|
+
)
|
|
1804
|
+
: sdk.languageModel(model.api.id)
|
|
1805
|
+
s.models.set(key, language)
|
|
1806
|
+
return language
|
|
1807
|
+
},
|
|
1808
|
+
(cause) =>
|
|
1809
|
+
cause instanceof NoSuchModelError
|
|
1810
|
+
? new ModelNotFoundError({ modelID: model.id, providerID: model.providerID, cause })
|
|
1811
|
+
: undefined,
|
|
1812
|
+
)
|
|
1813
|
+
})
|
|
1814
|
+
|
|
1815
|
+
const closest = Effect.fn("Provider.closest")(function* (providerID: ProviderV2.ID, query: string[]) {
|
|
1816
|
+
const s = yield* InstanceState.get(state)
|
|
1817
|
+
const provider = s.providers[providerID]
|
|
1818
|
+
if (!provider) return undefined
|
|
1819
|
+
for (const item of query) {
|
|
1820
|
+
for (const modelID of Object.keys(provider.models)) {
|
|
1821
|
+
if (modelID.includes(item)) return { providerID, modelID }
|
|
1822
|
+
}
|
|
1823
|
+
}
|
|
1824
|
+
return undefined
|
|
1825
|
+
})
|
|
1826
|
+
|
|
1827
|
+
const getSmallModel = Effect.fn("Provider.getSmallModel")(function* (providerID: ProviderV2.ID) {
|
|
1828
|
+
const cfg = yield* config.get()
|
|
1829
|
+
|
|
1830
|
+
if (cfg.small_model) {
|
|
1831
|
+
const parsed = parseModel(cfg.small_model)
|
|
1832
|
+
return yield* getModel(parsed.providerID, parsed.modelID).pipe(
|
|
1833
|
+
Effect.catchTag("ProviderModelNotFoundError", () => Effect.succeed(undefined)),
|
|
1834
|
+
)
|
|
1835
|
+
}
|
|
1836
|
+
|
|
1837
|
+
const s = yield* InstanceState.get(state)
|
|
1838
|
+
const provider = s.providers[providerID]
|
|
1839
|
+
if (!provider) return undefined
|
|
1840
|
+
|
|
1841
|
+
const experimental = yield* plugin.trigger<"experimental.provider.small_model">(
|
|
1842
|
+
"experimental.provider.small_model",
|
|
1843
|
+
{ provider: toPublicInfo(provider) },
|
|
1844
|
+
{ model: undefined },
|
|
1845
|
+
)
|
|
1846
|
+
if (experimental.model) {
|
|
1847
|
+
return {
|
|
1848
|
+
...experimental.model,
|
|
1849
|
+
id: ModelV2.ID.make(experimental.model.id),
|
|
1850
|
+
providerID: ProviderV2.ID.make(experimental.model.providerID),
|
|
1851
|
+
}
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
const defaultPriority = [
|
|
1855
|
+
"claude-haiku-4-5",
|
|
1856
|
+
"claude-haiku-4.5",
|
|
1857
|
+
"3-5-haiku",
|
|
1858
|
+
"3.5-haiku",
|
|
1859
|
+
"gemini-3-flash",
|
|
1860
|
+
"gemini-2.5-flash",
|
|
1861
|
+
"gpt-5-nano",
|
|
1862
|
+
]
|
|
1863
|
+
const priority = providerID.startsWith("miaw")
|
|
1864
|
+
? ["gpt-5-nano"]
|
|
1865
|
+
: providerID.startsWith("github-copilot")
|
|
1866
|
+
? ["gpt-5-mini", "claude-haiku-4.5", ...defaultPriority]
|
|
1867
|
+
: defaultPriority
|
|
1868
|
+
for (const item of priority) {
|
|
1869
|
+
if (providerID === ProviderV2.ID.amazonBedrock) {
|
|
1870
|
+
const crossRegionPrefixes = ["global.", "us.", "eu."]
|
|
1871
|
+
const candidates = Object.keys(provider.models).filter((m) => m.includes(item))
|
|
1872
|
+
|
|
1873
|
+
const globalMatch = candidates.find((m) => m.startsWith("global."))
|
|
1874
|
+
if (globalMatch) return provider.models[globalMatch]
|
|
1875
|
+
|
|
1876
|
+
const region = provider.options?.region
|
|
1877
|
+
if (region) {
|
|
1878
|
+
const regionPrefix = region.split("-")[0]
|
|
1879
|
+
if (regionPrefix === "us" || regionPrefix === "eu") {
|
|
1880
|
+
const regionalMatch = candidates.find((m) => m.startsWith(`${regionPrefix}.`))
|
|
1881
|
+
if (regionalMatch) return provider.models[regionalMatch]
|
|
1882
|
+
}
|
|
1883
|
+
}
|
|
1884
|
+
|
|
1885
|
+
const unprefixed = candidates.find((m) => !crossRegionPrefixes.some((p) => m.startsWith(p)))
|
|
1886
|
+
if (unprefixed) return provider.models[unprefixed]
|
|
1887
|
+
} else {
|
|
1888
|
+
for (const model of Object.keys(provider.models)) {
|
|
1889
|
+
if (model.includes(item)) return provider.models[model]
|
|
1890
|
+
}
|
|
1891
|
+
}
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
return undefined
|
|
1895
|
+
})
|
|
1896
|
+
|
|
1897
|
+
const defaultModel = Effect.fn("Provider.defaultModel")(function* () {
|
|
1898
|
+
const cfg = yield* config.get()
|
|
1899
|
+
if (cfg.model) return parseModel(cfg.model)
|
|
1900
|
+
|
|
1901
|
+
const s = yield* InstanceState.get(state)
|
|
1902
|
+
const recent = yield* fs.readJson(path.join(Global.Path.state, "model.json")).pipe(
|
|
1903
|
+
Effect.map((x): { providerID: ProviderV2.ID; modelID: ModelV2.ID }[] => {
|
|
1904
|
+
if (!isRecord(x) || !Array.isArray(x.recent)) return []
|
|
1905
|
+
return x.recent.flatMap((item) => {
|
|
1906
|
+
if (!isRecord(item)) return []
|
|
1907
|
+
if (typeof item.providerID !== "string") return []
|
|
1908
|
+
if (typeof item.modelID !== "string") return []
|
|
1909
|
+
return [{ providerID: ProviderV2.ID.make(item.providerID), modelID: ModelV2.ID.make(item.modelID) }]
|
|
1910
|
+
})
|
|
1911
|
+
}),
|
|
1912
|
+
Effect.catch(() => Effect.succeed([] as { providerID: ProviderV2.ID; modelID: ModelV2.ID }[])),
|
|
1913
|
+
)
|
|
1914
|
+
for (const entry of recent) {
|
|
1915
|
+
const provider = s.providers[entry.providerID]
|
|
1916
|
+
if (!provider) continue
|
|
1917
|
+
if (!provider.models[entry.modelID]) continue
|
|
1918
|
+
return { providerID: entry.providerID, modelID: entry.modelID }
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
const provider = Object.values(s.providers).find((p) => !cfg.provider || Object.keys(cfg.provider).includes(p.id))
|
|
1922
|
+
if (!provider) return yield* new NoProvidersError()
|
|
1923
|
+
const [model] = sort(Object.values(provider.models))
|
|
1924
|
+
if (!model) return yield* new NoModelsError({ providerID: provider.id })
|
|
1925
|
+
return {
|
|
1926
|
+
providerID: provider.id,
|
|
1927
|
+
modelID: model.id,
|
|
1928
|
+
}
|
|
1929
|
+
})
|
|
1930
|
+
|
|
1931
|
+
return Service.of({ list, getProvider, getModel, getLanguage, closest, getSmallModel, defaultModel })
|
|
1932
|
+
}),
|
|
1933
|
+
)
|
|
1934
|
+
|
|
1935
|
+
export const defaultLayer = Layer.suspend(() =>
|
|
1936
|
+
layer.pipe(
|
|
1937
|
+
Layer.provide(FSUtil.defaultLayer),
|
|
1938
|
+
Layer.provide(Env.defaultLayer),
|
|
1939
|
+
Layer.provide(Config.defaultLayer),
|
|
1940
|
+
Layer.provide(Auth.defaultLayer),
|
|
1941
|
+
Layer.provide(Plugin.defaultLayer),
|
|
1942
|
+
Layer.provide(ModelsDev.defaultLayer),
|
|
1943
|
+
Layer.provide(RuntimeFlags.defaultLayer),
|
|
1944
|
+
),
|
|
1945
|
+
)
|
|
1946
|
+
|
|
1947
|
+
const priority = ["gpt-5", "claude-sonnet-4", "big-pickle", "gemini-3-pro"]
|
|
1948
|
+
export function sort<T extends { id: string }>(models: T[]) {
|
|
1949
|
+
return sortBy(
|
|
1950
|
+
models,
|
|
1951
|
+
[(model) => priority.findIndex((filter) => model.id.includes(filter)), "desc"],
|
|
1952
|
+
[(model) => (model.id.includes("latest") ? 0 : 1), "asc"],
|
|
1953
|
+
[(model) => model.id, "desc"],
|
|
1954
|
+
)
|
|
1955
|
+
}
|
|
1956
|
+
|
|
1957
|
+
export function parseModel(model: string) {
|
|
1958
|
+
const [providerID, ...rest] = model.split("/")
|
|
1959
|
+
return {
|
|
1960
|
+
providerID: ProviderV2.ID.make(providerID),
|
|
1961
|
+
modelID: ModelV2.ID.make(rest.join("/")),
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
|
|
1965
|
+
export const node = LayerNode.make(layer, [
|
|
1966
|
+
FSUtil.node,
|
|
1967
|
+
Config.node,
|
|
1968
|
+
Auth.node,
|
|
1969
|
+
Env.node,
|
|
1970
|
+
Plugin.node,
|
|
1971
|
+
ModelsDev.node,
|
|
1972
|
+
RuntimeFlags.node,
|
|
1973
|
+
])
|
|
1974
|
+
|
|
1975
|
+
export * as Provider from "./provider"
|