alchemylab-opencode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NODE_COMPAT.md +184 -0
- package/package.json +112 -0
- package/packages/opencode/migration/20260127222353_familiar_lady_ursula/migration.sql +90 -0
- package/packages/opencode/migration/20260127222353_familiar_lady_ursula/snapshot.json +796 -0
- package/packages/opencode/migration/20260211171708_add_project_commands/migration.sql +1 -0
- package/packages/opencode/migration/20260211171708_add_project_commands/snapshot.json +806 -0
- package/packages/opencode/migration/20260213144116_wakeful_the_professor/migration.sql +11 -0
- package/packages/opencode/migration/20260213144116_wakeful_the_professor/snapshot.json +897 -0
- package/packages/opencode/migration/20260225215848_workspace/migration.sql +7 -0
- package/packages/opencode/migration/20260225215848_workspace/snapshot.json +959 -0
- package/packages/opencode/migration/20260227213759_add_session_workspace_id/migration.sql +2 -0
- package/packages/opencode/migration/20260227213759_add_session_workspace_id/snapshot.json +983 -0
- package/packages/opencode/migration/20260228203230_blue_harpoon/migration.sql +17 -0
- package/packages/opencode/migration/20260228203230_blue_harpoon/snapshot.json +1102 -0
- package/packages/opencode/migration/20260303231226_add_workspace_fields/migration.sql +5 -0
- package/packages/opencode/migration/20260303231226_add_workspace_fields/snapshot.json +1013 -0
- package/packages/opencode/migration/20260309230000_move_org_to_state/migration.sql +3 -0
- package/packages/opencode/migration/20260309230000_move_org_to_state/snapshot.json +1156 -0
- package/packages/opencode/migration/20260312043431_session_message_cursor/migration.sql +4 -0
- package/packages/opencode/migration/20260312043431_session_message_cursor/snapshot.json +1168 -0
- package/packages/opencode/migration/20260323234822_events/migration.sql +13 -0
- package/packages/opencode/migration/20260323234822_events/snapshot.json +1271 -0
- package/packages/opencode/package.json +140 -0
- package/packages/opencode/src/account/account.sql.ts +39 -0
- package/packages/opencode/src/account/index.ts +463 -0
- package/packages/opencode/src/account/repo.ts +163 -0
- package/packages/opencode/src/account/schema.ts +91 -0
- package/packages/opencode/src/acp/README.md +174 -0
- package/packages/opencode/src/acp/agent.ts +1847 -0
- package/packages/opencode/src/acp/session.ts +116 -0
- package/packages/opencode/src/acp/types.ts +24 -0
- package/packages/opencode/src/agent/agent.ts +420 -0
- package/packages/opencode/src/agent/generate.txt +75 -0
- package/packages/opencode/src/agent/prompt/compaction.txt +15 -0
- package/packages/opencode/src/agent/prompt/explore.txt +18 -0
- package/packages/opencode/src/agent/prompt/summary.txt +11 -0
- package/packages/opencode/src/agent/prompt/title.txt +44 -0
- package/packages/opencode/src/auth/index.ts +110 -0
- package/packages/opencode/src/bus/bus-event.ts +40 -0
- package/packages/opencode/src/bus/global.ts +10 -0
- package/packages/opencode/src/bus/index.ts +185 -0
- package/packages/opencode/src/cli/bootstrap.ts +17 -0
- package/packages/opencode/src/cli/cmd/cmd.ts +7 -0
- package/packages/opencode/src/cli/cmd/run.ts +689 -0
- package/packages/opencode/src/cli/cmd/serve.ts +24 -0
- package/packages/opencode/src/cli/cmd/tui/event.ts +49 -0
- package/packages/opencode/src/cli/effect/prompt.ts +25 -0
- package/packages/opencode/src/cli/error.ts +46 -0
- package/packages/opencode/src/cli/heap.ts +59 -0
- package/packages/opencode/src/cli/logo.ts +6 -0
- package/packages/opencode/src/cli/network.ts +60 -0
- package/packages/opencode/src/cli/ui.ts +133 -0
- package/packages/opencode/src/cli/upgrade.ts +31 -0
- package/packages/opencode/src/command/index.ts +195 -0
- package/packages/opencode/src/command/template/initialize.txt +66 -0
- package/packages/opencode/src/command/template/review.txt +101 -0
- package/packages/opencode/src/config/config.ts +1599 -0
- package/packages/opencode/src/config/console-state.ts +15 -0
- package/packages/opencode/src/config/markdown.ts +99 -0
- package/packages/opencode/src/config/paths.ts +167 -0
- package/packages/opencode/src/config/tui-migrate.ts +155 -0
- package/packages/opencode/src/config/tui-schema.ts +37 -0
- package/packages/opencode/src/config/tui.ts +179 -0
- package/packages/opencode/src/control-plane/adaptors/index.ts +20 -0
- package/packages/opencode/src/control-plane/adaptors/worktree.ts +42 -0
- package/packages/opencode/src/control-plane/schema.ts +17 -0
- package/packages/opencode/src/control-plane/sse.ts +66 -0
- package/packages/opencode/src/control-plane/types.ts +32 -0
- package/packages/opencode/src/control-plane/workspace.sql.ts +17 -0
- package/packages/opencode/src/control-plane/workspace.ts +168 -0
- package/packages/opencode/src/effect/cross-spawn-spawner.ts +502 -0
- package/packages/opencode/src/effect/instance-ref.ts +6 -0
- package/packages/opencode/src/effect/instance-registry.ts +12 -0
- package/packages/opencode/src/effect/instance-state.ts +82 -0
- package/packages/opencode/src/effect/run-service.ts +33 -0
- package/packages/opencode/src/effect/runner.ts +216 -0
- package/packages/opencode/src/env/index.ts +28 -0
- package/packages/opencode/src/file/ignore.ts +82 -0
- package/packages/opencode/src/file/index.ts +686 -0
- package/packages/opencode/src/file/protected.ts +59 -0
- package/packages/opencode/src/file/ripgrep.ts +376 -0
- package/packages/opencode/src/file/time.ts +133 -0
- package/packages/opencode/src/file/watcher.ts +173 -0
- package/packages/opencode/src/filesystem/index.ts +236 -0
- package/packages/opencode/src/flag/flag.ts +156 -0
- package/packages/opencode/src/format/formatter.ts +413 -0
- package/packages/opencode/src/format/index.ts +203 -0
- package/packages/opencode/src/git/index.ts +303 -0
- package/packages/opencode/src/global/index.ts +56 -0
- package/packages/opencode/src/id/id.ts +85 -0
- package/packages/opencode/src/ide/index.ts +74 -0
- package/packages/opencode/src/installation/index.ts +355 -0
- package/packages/opencode/src/installation/meta.ts +7 -0
- package/packages/opencode/src/lsp/client.ts +252 -0
- package/packages/opencode/src/lsp/index.ts +558 -0
- package/packages/opencode/src/lsp/language.ts +120 -0
- package/packages/opencode/src/lsp/launch.ts +21 -0
- package/packages/opencode/src/lsp/server.ts +1968 -0
- package/packages/opencode/src/mcp/auth.ts +173 -0
- package/packages/opencode/src/mcp/index.ts +931 -0
- package/packages/opencode/src/mcp/oauth-callback.ts +216 -0
- package/packages/opencode/src/mcp/oauth-provider.ts +185 -0
- package/packages/opencode/src/node-loader.mjs +104 -0
- package/packages/opencode/src/node-preload.mjs +39 -0
- package/packages/opencode/src/node.ts +1 -0
- package/packages/opencode/src/npm/index.ts +188 -0
- package/packages/opencode/src/patch/index.ts +680 -0
- package/packages/opencode/src/permission/arity.ts +163 -0
- package/packages/opencode/src/permission/evaluate.ts +15 -0
- package/packages/opencode/src/permission/index.ts +325 -0
- package/packages/opencode/src/permission/schema.ts +17 -0
- package/packages/opencode/src/plugin/cloudflare.ts +67 -0
- package/packages/opencode/src/plugin/codex.ts +608 -0
- package/packages/opencode/src/plugin/github-copilot/copilot.ts +361 -0
- package/packages/opencode/src/plugin/github-copilot/models.ts +144 -0
- package/packages/opencode/src/plugin/index.ts +299 -0
- package/packages/opencode/src/plugin/install.ts +439 -0
- package/packages/opencode/src/plugin/loader.ts +174 -0
- package/packages/opencode/src/plugin/meta.ts +188 -0
- package/packages/opencode/src/plugin/shared.ts +323 -0
- package/packages/opencode/src/project/bootstrap.ts +31 -0
- package/packages/opencode/src/project/instance.ts +175 -0
- package/packages/opencode/src/project/project.sql.ts +16 -0
- package/packages/opencode/src/project/project.ts +519 -0
- package/packages/opencode/src/project/schema.ts +16 -0
- package/packages/opencode/src/project/state.ts +70 -0
- package/packages/opencode/src/project/vcs.ts +240 -0
- package/packages/opencode/src/provider/auth.ts +253 -0
- package/packages/opencode/src/provider/error.ts +197 -0
- package/packages/opencode/src/provider/models.ts +162 -0
- package/packages/opencode/src/provider/provider.ts +1709 -0
- package/packages/opencode/src/provider/schema.ts +38 -0
- package/packages/opencode/src/provider/sdk/copilot/README.md +5 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/convert-to-openai-compatible-chat-messages.ts +170 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/get-response-metadata.ts +15 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/map-openai-compatible-finish-reason.ts +19 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-api-types.ts +64 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-chat-language-model.ts +815 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-chat-options.ts +28 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-metadata-extractor.ts +44 -0
- package/packages/opencode/src/provider/sdk/copilot/chat/openai-compatible-prepare-tools.ts +83 -0
- package/packages/opencode/src/provider/sdk/copilot/copilot-provider.ts +100 -0
- package/packages/opencode/src/provider/sdk/copilot/index.ts +2 -0
- package/packages/opencode/src/provider/sdk/copilot/openai-compatible-error.ts +27 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/convert-to-openai-responses-input.ts +335 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/map-openai-responses-finish-reason.ts +22 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/openai-config.ts +18 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/openai-error.ts +22 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-api-types.ts +214 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-language-model.ts +1769 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-prepare-tools.ts +173 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/openai-responses-settings.ts +1 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/tool/code-interpreter.ts +87 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/tool/file-search.ts +127 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/tool/image-generation.ts +114 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/tool/local-shell.ts +64 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/tool/web-search-preview.ts +103 -0
- package/packages/opencode/src/provider/sdk/copilot/responses/tool/web-search.ts +102 -0
- package/packages/opencode/src/provider/transform.ts +1050 -0
- package/packages/opencode/src/pty/index.ts +397 -0
- package/packages/opencode/src/pty/pty.node.ts +27 -0
- package/packages/opencode/src/pty/pty.ts +25 -0
- package/packages/opencode/src/pty/schema.ts +17 -0
- package/packages/opencode/src/question/index.ts +224 -0
- package/packages/opencode/src/question/schema.ts +17 -0
- package/packages/opencode/src/run.ts +28 -0
- package/packages/opencode/src/serve.ts +45 -0
- package/packages/opencode/src/server/error.ts +36 -0
- package/packages/opencode/src/server/event.ts +7 -0
- package/packages/opencode/src/server/instance.ts +324 -0
- package/packages/opencode/src/server/mdns.ts +60 -0
- package/packages/opencode/src/server/middleware.ts +33 -0
- package/packages/opencode/src/server/projectors.ts +28 -0
- package/packages/opencode/src/server/proxy.ts +131 -0
- package/packages/opencode/src/server/router.ts +105 -0
- package/packages/opencode/src/server/routes/config.ts +92 -0
- package/packages/opencode/src/server/routes/event.ts +83 -0
- package/packages/opencode/src/server/routes/experimental.ts +374 -0
- package/packages/opencode/src/server/routes/file.ts +197 -0
- package/packages/opencode/src/server/routes/global.ts +312 -0
- package/packages/opencode/src/server/routes/mcp.ts +225 -0
- package/packages/opencode/src/server/routes/permission.ts +69 -0
- package/packages/opencode/src/server/routes/project.ts +118 -0
- package/packages/opencode/src/server/routes/provider.ts +171 -0
- package/packages/opencode/src/server/routes/pty.ts +210 -0
- package/packages/opencode/src/server/routes/question.ts +99 -0
- package/packages/opencode/src/server/routes/session.ts +1030 -0
- package/packages/opencode/src/server/routes/tui.ts +379 -0
- package/packages/opencode/src/server/routes/workspace.ts +94 -0
- package/packages/opencode/src/server/server.ts +353 -0
- package/packages/opencode/src/session/compaction.ts +428 -0
- package/packages/opencode/src/session/index.ts +887 -0
- package/packages/opencode/src/session/instruction.ts +258 -0
- package/packages/opencode/src/session/llm.ts +354 -0
- package/packages/opencode/src/session/message-v2.ts +1031 -0
- package/packages/opencode/src/session/message.ts +191 -0
- package/packages/opencode/src/session/overflow.ts +22 -0
- package/packages/opencode/src/session/processor.ts +523 -0
- package/packages/opencode/src/session/projectors.ts +135 -0
- package/packages/opencode/src/session/prompt/anthropic.txt +105 -0
- package/packages/opencode/src/session/prompt/beast.txt +147 -0
- package/packages/opencode/src/session/prompt/build-switch.txt +5 -0
- package/packages/opencode/src/session/prompt/codex.txt +79 -0
- package/packages/opencode/src/session/prompt/copilot-gpt-5.txt +143 -0
- package/packages/opencode/src/session/prompt/default.txt +105 -0
- package/packages/opencode/src/session/prompt/gemini.txt +155 -0
- package/packages/opencode/src/session/prompt/gpt.txt +107 -0
- package/packages/opencode/src/session/prompt/kimi.txt +95 -0
- package/packages/opencode/src/session/prompt/max-steps.txt +16 -0
- package/packages/opencode/src/session/prompt/plan-reminder-anthropic.txt +67 -0
- package/packages/opencode/src/session/prompt/plan.txt +26 -0
- package/packages/opencode/src/session/prompt/trinity.txt +97 -0
- package/packages/opencode/src/session/prompt.ts +1911 -0
- package/packages/opencode/src/session/retry.ts +106 -0
- package/packages/opencode/src/session/revert.ts +176 -0
- package/packages/opencode/src/session/schema.ts +38 -0
- package/packages/opencode/src/session/session.sql.ts +103 -0
- package/packages/opencode/src/session/status.ts +102 -0
- package/packages/opencode/src/session/summary.ts +177 -0
- package/packages/opencode/src/session/system.ts +76 -0
- package/packages/opencode/src/session/todo.ts +95 -0
- package/packages/opencode/src/share/share-next.ts +369 -0
- package/packages/opencode/src/share/share.sql.ts +13 -0
- package/packages/opencode/src/shell/shell.ts +110 -0
- package/packages/opencode/src/skill/discovery.ts +116 -0
- package/packages/opencode/src/skill/index.ts +277 -0
- package/packages/opencode/src/snapshot/index.ts +726 -0
- package/packages/opencode/src/sql.d.ts +4 -0
- package/packages/opencode/src/storage/db.node.ts +8 -0
- package/packages/opencode/src/storage/db.ts +177 -0
- package/packages/opencode/src/storage/json-migration.ts +425 -0
- package/packages/opencode/src/storage/schema.sql.ts +10 -0
- package/packages/opencode/src/storage/schema.ts +5 -0
- package/packages/opencode/src/storage/storage.ts +353 -0
- package/packages/opencode/src/sync/README.md +179 -0
- package/packages/opencode/src/sync/event.sql.ts +16 -0
- package/packages/opencode/src/sync/index.ts +263 -0
- package/packages/opencode/src/sync/schema.ts +14 -0
- package/packages/opencode/src/tool/apply_patch.ts +281 -0
- package/packages/opencode/src/tool/apply_patch.txt +33 -0
- package/packages/opencode/src/tool/bash.ts +496 -0
- package/packages/opencode/src/tool/bash.txt +117 -0
- package/packages/opencode/src/tool/batch.ts +183 -0
- package/packages/opencode/src/tool/batch.txt +24 -0
- package/packages/opencode/src/tool/codesearch.ts +132 -0
- package/packages/opencode/src/tool/codesearch.txt +12 -0
- package/packages/opencode/src/tool/edit.ts +667 -0
- package/packages/opencode/src/tool/edit.txt +10 -0
- package/packages/opencode/src/tool/external-directory.ts +46 -0
- package/packages/opencode/src/tool/glob.ts +78 -0
- package/packages/opencode/src/tool/glob.txt +6 -0
- package/packages/opencode/src/tool/grep.ts +156 -0
- package/packages/opencode/src/tool/grep.txt +8 -0
- package/packages/opencode/src/tool/invalid.ts +17 -0
- package/packages/opencode/src/tool/ls.ts +121 -0
- package/packages/opencode/src/tool/ls.txt +1 -0
- package/packages/opencode/src/tool/lsp.ts +97 -0
- package/packages/opencode/src/tool/lsp.txt +19 -0
- package/packages/opencode/src/tool/multiedit.ts +46 -0
- package/packages/opencode/src/tool/multiedit.txt +41 -0
- package/packages/opencode/src/tool/plan-enter.txt +14 -0
- package/packages/opencode/src/tool/plan-exit.txt +13 -0
- package/packages/opencode/src/tool/plan.ts +131 -0
- package/packages/opencode/src/tool/question.ts +46 -0
- package/packages/opencode/src/tool/question.txt +10 -0
- package/packages/opencode/src/tool/read.ts +332 -0
- package/packages/opencode/src/tool/read.txt +14 -0
- package/packages/opencode/src/tool/registry.ts +265 -0
- package/packages/opencode/src/tool/schema.ts +17 -0
- package/packages/opencode/src/tool/skill.ts +105 -0
- package/packages/opencode/src/tool/task.ts +166 -0
- package/packages/opencode/src/tool/task.txt +60 -0
- package/packages/opencode/src/tool/todo.ts +48 -0
- package/packages/opencode/src/tool/todowrite.txt +167 -0
- package/packages/opencode/src/tool/tool.ts +112 -0
- package/packages/opencode/src/tool/truncate.ts +144 -0
- package/packages/opencode/src/tool/truncation-dir.ts +4 -0
- package/packages/opencode/src/tool/webfetch.ts +206 -0
- package/packages/opencode/src/tool/webfetch.txt +13 -0
- package/packages/opencode/src/tool/websearch.ts +150 -0
- package/packages/opencode/src/tool/websearch.txt +14 -0
- package/packages/opencode/src/tool/write.ts +84 -0
- package/packages/opencode/src/tool/write.txt +8 -0
- package/packages/opencode/src/util/abort.ts +35 -0
- package/packages/opencode/src/util/archive.ts +17 -0
- package/packages/opencode/src/util/color.ts +19 -0
- package/packages/opencode/src/util/context.ts +25 -0
- package/packages/opencode/src/util/data-url.ts +9 -0
- package/packages/opencode/src/util/defer.ts +12 -0
- package/packages/opencode/src/util/effect-http-client.ts +11 -0
- package/packages/opencode/src/util/effect-zod.ts +98 -0
- package/packages/opencode/src/util/error.ts +77 -0
- package/packages/opencode/src/util/filesystem.ts +245 -0
- package/packages/opencode/src/util/flock.ts +333 -0
- package/packages/opencode/src/util/fn.ts +21 -0
- package/packages/opencode/src/util/format.ts +20 -0
- package/packages/opencode/src/util/glob.ts +34 -0
- package/packages/opencode/src/util/hash.ts +7 -0
- package/packages/opencode/src/util/iife.ts +3 -0
- package/packages/opencode/src/util/keybind.ts +103 -0
- package/packages/opencode/src/util/lazy.ts +23 -0
- package/packages/opencode/src/util/locale.ts +81 -0
- package/packages/opencode/src/util/lock.ts +98 -0
- package/packages/opencode/src/util/log.ts +182 -0
- package/packages/opencode/src/util/network.ts +9 -0
- package/packages/opencode/src/util/process.ts +176 -0
- package/packages/opencode/src/util/queue.ts +32 -0
- package/packages/opencode/src/util/record.ts +3 -0
- package/packages/opencode/src/util/rpc.ts +66 -0
- package/packages/opencode/src/util/schema.ts +53 -0
- package/packages/opencode/src/util/scrap.ts +10 -0
- package/packages/opencode/src/util/signal.ts +12 -0
- package/packages/opencode/src/util/timeout.ts +14 -0
- package/packages/opencode/src/util/token.ts +7 -0
- package/packages/opencode/src/util/update-schema.ts +13 -0
- package/packages/opencode/src/util/which.ts +14 -0
- package/packages/opencode/src/util/wildcard.ts +59 -0
- package/packages/opencode/src/worktree/index.ts +612 -0
- package/packages/plugin/package.json +38 -0
- package/packages/plugin/src/example.ts +18 -0
- package/packages/plugin/src/index.ts +282 -0
- package/packages/plugin/src/shell.ts +136 -0
- package/packages/plugin/src/tool.ts +38 -0
- package/packages/plugin/src/tui.ts +508 -0
- package/packages/script/package.json +16 -0
- package/packages/script/src/index.ts +77 -0
- package/packages/sdk/package.json +33 -0
- package/packages/sdk/src/client.ts +55 -0
- package/packages/sdk/src/gen/client/client.gen.ts +212 -0
- package/packages/sdk/src/gen/client/index.ts +25 -0
- package/packages/sdk/src/gen/client/types.gen.ts +222 -0
- package/packages/sdk/src/gen/client/utils.gen.ts +287 -0
- package/packages/sdk/src/gen/client.gen.ts +22 -0
- package/packages/sdk/src/gen/core/auth.gen.ts +41 -0
- package/packages/sdk/src/gen/core/bodySerializer.gen.ts +74 -0
- package/packages/sdk/src/gen/core/params.gen.ts +144 -0
- package/packages/sdk/src/gen/core/pathSerializer.gen.ts +167 -0
- package/packages/sdk/src/gen/core/queryKeySerializer.gen.ts +111 -0
- package/packages/sdk/src/gen/core/serverSentEvents.gen.ts +210 -0
- package/packages/sdk/src/gen/core/types.gen.ts +91 -0
- package/packages/sdk/src/gen/core/utils.gen.ts +109 -0
- package/packages/sdk/src/gen/sdk.gen.ts +1197 -0
- package/packages/sdk/src/gen/types.gen.ts +3904 -0
- package/packages/sdk/src/index.ts +21 -0
- package/packages/sdk/src/process.ts +31 -0
- package/packages/sdk/src/server.ts +134 -0
- package/packages/sdk/src/v2/client.ts +81 -0
- package/packages/sdk/src/v2/gen/client/client.gen.ts +285 -0
- package/packages/sdk/src/v2/gen/client/index.ts +25 -0
- package/packages/sdk/src/v2/gen/client/types.gen.ts +202 -0
- package/packages/sdk/src/v2/gen/client/utils.gen.ts +289 -0
- package/packages/sdk/src/v2/gen/client.gen.ts +18 -0
- package/packages/sdk/src/v2/gen/core/auth.gen.ts +41 -0
- package/packages/sdk/src/v2/gen/core/bodySerializer.gen.ts +82 -0
- package/packages/sdk/src/v2/gen/core/params.gen.ts +169 -0
- package/packages/sdk/src/v2/gen/core/pathSerializer.gen.ts +167 -0
- package/packages/sdk/src/v2/gen/core/queryKeySerializer.gen.ts +111 -0
- package/packages/sdk/src/v2/gen/core/serverSentEvents.gen.ts +239 -0
- package/packages/sdk/src/v2/gen/core/types.gen.ts +86 -0
- package/packages/sdk/src/v2/gen/core/utils.gen.ts +137 -0
- package/packages/sdk/src/v2/gen/sdk.gen.ts +4223 -0
- package/packages/sdk/src/v2/gen/types.gen.ts +5265 -0
- package/packages/sdk/src/v2/index.ts +21 -0
- package/packages/sdk/src/v2/server.ts +134 -0
- package/packages/util/package.json +20 -0
- package/packages/util/src/array.ts +10 -0
- package/packages/util/src/binary.ts +41 -0
- package/packages/util/src/encode.ts +51 -0
- package/packages/util/src/error.ts +54 -0
- package/packages/util/src/fn.ts +11 -0
- package/packages/util/src/identifier.ts +48 -0
- package/packages/util/src/iife.ts +3 -0
- package/packages/util/src/lazy.ts +11 -0
- package/packages/util/src/module.ts +10 -0
- package/packages/util/src/path.ts +37 -0
- package/packages/util/src/retry.ts +41 -0
- package/packages/util/src/slug.ts +74 -0
- package/postinstall.cjs +55 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Schema } from "effect"
|
|
2
|
+
import z from "zod"
|
|
3
|
+
|
|
4
|
+
import { withStatics } from "@/util/schema"
|
|
5
|
+
|
|
6
|
+
const projectIdSchema = Schema.String.pipe(Schema.brand("ProjectID"))
|
|
7
|
+
|
|
8
|
+
export type ProjectID = typeof projectIdSchema.Type
|
|
9
|
+
|
|
10
|
+
export const ProjectID = projectIdSchema.pipe(
|
|
11
|
+
withStatics((schema: typeof projectIdSchema) => ({
|
|
12
|
+
global: schema.makeUnsafe("global"),
|
|
13
|
+
make: (id: string) => schema.makeUnsafe(id),
|
|
14
|
+
zod: z.string().pipe(z.custom<ProjectID>()),
|
|
15
|
+
})),
|
|
16
|
+
)
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { Log } from "@/util/log"
|
|
2
|
+
|
|
3
|
+
export namespace State {
|
|
4
|
+
interface Entry {
|
|
5
|
+
state: any
|
|
6
|
+
dispose?: (state: any) => Promise<void>
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const log = Log.create({ service: "state" })
|
|
10
|
+
const recordsByKey = new Map<string, Map<any, Entry>>()
|
|
11
|
+
|
|
12
|
+
export function create<S>(root: () => string, init: () => S, dispose?: (state: Awaited<S>) => Promise<void>) {
|
|
13
|
+
return () => {
|
|
14
|
+
const key = root()
|
|
15
|
+
let entries = recordsByKey.get(key)
|
|
16
|
+
if (!entries) {
|
|
17
|
+
entries = new Map<string, Entry>()
|
|
18
|
+
recordsByKey.set(key, entries)
|
|
19
|
+
}
|
|
20
|
+
const exists = entries.get(init)
|
|
21
|
+
if (exists) return exists.state as S
|
|
22
|
+
const state = init()
|
|
23
|
+
entries.set(init, {
|
|
24
|
+
state,
|
|
25
|
+
dispose,
|
|
26
|
+
})
|
|
27
|
+
return state
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export async function dispose(key: string) {
|
|
32
|
+
const entries = recordsByKey.get(key)
|
|
33
|
+
if (!entries) return
|
|
34
|
+
|
|
35
|
+
log.info("waiting for state disposal to complete", { key })
|
|
36
|
+
|
|
37
|
+
let disposalFinished = false
|
|
38
|
+
|
|
39
|
+
setTimeout(() => {
|
|
40
|
+
if (!disposalFinished) {
|
|
41
|
+
log.warn(
|
|
42
|
+
"state disposal is taking an unusually long time - if it does not complete in a reasonable time, please report this as a bug",
|
|
43
|
+
{ key },
|
|
44
|
+
)
|
|
45
|
+
}
|
|
46
|
+
}, 10000).unref()
|
|
47
|
+
|
|
48
|
+
const tasks: Promise<void>[] = []
|
|
49
|
+
for (const [init, entry] of entries) {
|
|
50
|
+
if (!entry.dispose) continue
|
|
51
|
+
|
|
52
|
+
const label = typeof init === "function" ? init.name : String(init)
|
|
53
|
+
|
|
54
|
+
const task = Promise.resolve(entry.state)
|
|
55
|
+
.then((state) => entry.dispose!(state))
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
log.error("Error while disposing state:", { error, key, init: label })
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
tasks.push(task)
|
|
61
|
+
}
|
|
62
|
+
await Promise.all(tasks)
|
|
63
|
+
|
|
64
|
+
entries.clear()
|
|
65
|
+
recordsByKey.delete(key)
|
|
66
|
+
|
|
67
|
+
disposalFinished = true
|
|
68
|
+
log.info("state disposal completed", { key })
|
|
69
|
+
}
|
|
70
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { Effect, Layer, ServiceMap, Stream } from "effect"
|
|
2
|
+
import path from "path"
|
|
3
|
+
import { Bus } from "@/bus"
|
|
4
|
+
import { BusEvent } from "@/bus/bus-event"
|
|
5
|
+
import { InstanceState } from "@/effect/instance-state"
|
|
6
|
+
import { makeRuntime } from "@/effect/run-service"
|
|
7
|
+
import { AppFileSystem } from "@/filesystem"
|
|
8
|
+
import { FileWatcher } from "@/file/watcher"
|
|
9
|
+
import { Git } from "@/git"
|
|
10
|
+
import { Snapshot } from "@/snapshot"
|
|
11
|
+
import { Log } from "@/util/log"
|
|
12
|
+
import { Instance } from "./instance"
|
|
13
|
+
import z from "zod"
|
|
14
|
+
|
|
15
|
+
export namespace Vcs {
|
|
16
|
+
const log = Log.create({ service: "vcs" })
|
|
17
|
+
|
|
18
|
+
const count = (text: string) => {
|
|
19
|
+
if (!text) return 0
|
|
20
|
+
if (!text.endsWith("\n")) return text.split("\n").length
|
|
21
|
+
return text.slice(0, -1).split("\n").length
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const work = Effect.fnUntraced(function* (fs: AppFileSystem.Interface, cwd: string, file: string) {
|
|
25
|
+
const full = path.join(cwd, file)
|
|
26
|
+
if (!(yield* fs.exists(full).pipe(Effect.orDie))) return ""
|
|
27
|
+
const buf = yield* fs.readFile(full).pipe(Effect.catch(() => Effect.succeed(new Uint8Array())))
|
|
28
|
+
if (Buffer.from(buf).includes(0)) return ""
|
|
29
|
+
return Buffer.from(buf).toString("utf8")
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
const nums = (list: Git.Stat[]) =>
|
|
33
|
+
new Map(list.map((item) => [item.file, { additions: item.additions, deletions: item.deletions }] as const))
|
|
34
|
+
|
|
35
|
+
const merge = (...lists: Git.Item[][]) => {
|
|
36
|
+
const out = new Map<string, Git.Item>()
|
|
37
|
+
lists.flat().forEach((item) => {
|
|
38
|
+
if (!out.has(item.file)) out.set(item.file, item)
|
|
39
|
+
})
|
|
40
|
+
return [...out.values()]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const files = Effect.fnUntraced(function* (
|
|
44
|
+
fs: AppFileSystem.Interface,
|
|
45
|
+
git: Git.Interface,
|
|
46
|
+
cwd: string,
|
|
47
|
+
ref: string | undefined,
|
|
48
|
+
list: Git.Item[],
|
|
49
|
+
map: Map<string, { additions: number; deletions: number }>,
|
|
50
|
+
) {
|
|
51
|
+
const base = ref ? yield* git.prefix(cwd) : ""
|
|
52
|
+
const next = yield* Effect.forEach(
|
|
53
|
+
list,
|
|
54
|
+
(item) =>
|
|
55
|
+
Effect.gen(function* () {
|
|
56
|
+
const before = item.status === "added" || !ref ? "" : yield* git.show(cwd, ref, item.file, base)
|
|
57
|
+
const after = item.status === "deleted" ? "" : yield* work(fs, cwd, item.file)
|
|
58
|
+
const stat = map.get(item.file)
|
|
59
|
+
return {
|
|
60
|
+
file: item.file,
|
|
61
|
+
before,
|
|
62
|
+
after,
|
|
63
|
+
additions: stat?.additions ?? (item.status === "added" ? count(after) : 0),
|
|
64
|
+
deletions: stat?.deletions ?? (item.status === "deleted" ? count(before) : 0),
|
|
65
|
+
status: item.status,
|
|
66
|
+
} satisfies Snapshot.FileDiff
|
|
67
|
+
}),
|
|
68
|
+
{ concurrency: 8 },
|
|
69
|
+
)
|
|
70
|
+
return next.toSorted((a, b) => a.file.localeCompare(b.file))
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
const track = Effect.fnUntraced(function* (
|
|
74
|
+
fs: AppFileSystem.Interface,
|
|
75
|
+
git: Git.Interface,
|
|
76
|
+
cwd: string,
|
|
77
|
+
ref: string | undefined,
|
|
78
|
+
) {
|
|
79
|
+
if (!ref) return yield* files(fs, git, cwd, ref, yield* git.status(cwd), new Map())
|
|
80
|
+
const [list, stats] = yield* Effect.all([git.status(cwd), git.stats(cwd, ref)], { concurrency: 2 })
|
|
81
|
+
return yield* files(fs, git, cwd, ref, list, nums(stats))
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const compare = Effect.fnUntraced(function* (
|
|
85
|
+
fs: AppFileSystem.Interface,
|
|
86
|
+
git: Git.Interface,
|
|
87
|
+
cwd: string,
|
|
88
|
+
ref: string,
|
|
89
|
+
) {
|
|
90
|
+
const [list, stats, extra] = yield* Effect.all([git.diff(cwd, ref), git.stats(cwd, ref), git.status(cwd)], {
|
|
91
|
+
concurrency: 3,
|
|
92
|
+
})
|
|
93
|
+
return yield* files(
|
|
94
|
+
fs,
|
|
95
|
+
git,
|
|
96
|
+
cwd,
|
|
97
|
+
ref,
|
|
98
|
+
merge(
|
|
99
|
+
list,
|
|
100
|
+
extra.filter((item) => item.code === "??"),
|
|
101
|
+
),
|
|
102
|
+
nums(stats),
|
|
103
|
+
)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
export const Mode = z.enum(["git", "branch"])
|
|
107
|
+
export type Mode = z.infer<typeof Mode>
|
|
108
|
+
|
|
109
|
+
export const Event = {
|
|
110
|
+
BranchUpdated: BusEvent.define(
|
|
111
|
+
"vcs.branch.updated",
|
|
112
|
+
z.object({
|
|
113
|
+
branch: z.string().optional(),
|
|
114
|
+
}),
|
|
115
|
+
),
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export const Info = z
|
|
119
|
+
.object({
|
|
120
|
+
branch: z.string().optional(),
|
|
121
|
+
default_branch: z.string().optional(),
|
|
122
|
+
})
|
|
123
|
+
.meta({
|
|
124
|
+
ref: "VcsInfo",
|
|
125
|
+
})
|
|
126
|
+
export type Info = z.infer<typeof Info>
|
|
127
|
+
|
|
128
|
+
export interface Interface {
|
|
129
|
+
readonly init: () => Effect.Effect<void>
|
|
130
|
+
readonly branch: () => Effect.Effect<string | undefined>
|
|
131
|
+
readonly defaultBranch: () => Effect.Effect<string | undefined>
|
|
132
|
+
readonly diff: (mode: Mode) => Effect.Effect<Snapshot.FileDiff[]>
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
interface State {
|
|
136
|
+
current: string | undefined
|
|
137
|
+
root: Git.Base | undefined
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/Vcs") {}
|
|
141
|
+
|
|
142
|
+
export const layer: Layer.Layer<Service, never, AppFileSystem.Service | Git.Service | Bus.Service> = Layer.effect(
|
|
143
|
+
Service,
|
|
144
|
+
Effect.gen(function* () {
|
|
145
|
+
const fs = yield* AppFileSystem.Service
|
|
146
|
+
const git = yield* Git.Service
|
|
147
|
+
const bus = yield* Bus.Service
|
|
148
|
+
|
|
149
|
+
const state = yield* InstanceState.make<State>(
|
|
150
|
+
Effect.fn("Vcs.state")((ctx) =>
|
|
151
|
+
Effect.gen(function* () {
|
|
152
|
+
if (ctx.project.vcs !== "git") {
|
|
153
|
+
return { current: undefined, root: undefined }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
const get = Effect.fnUntraced(function* () {
|
|
157
|
+
return yield* git.branch(ctx.directory)
|
|
158
|
+
})
|
|
159
|
+
const [current, root] = yield* Effect.all([git.branch(ctx.directory), git.defaultBranch(ctx.directory)], {
|
|
160
|
+
concurrency: 2,
|
|
161
|
+
})
|
|
162
|
+
const value = { current, root }
|
|
163
|
+
log.info("initialized", { branch: value.current, default_branch: value.root?.name })
|
|
164
|
+
|
|
165
|
+
yield* bus.subscribe(FileWatcher.Event.Updated).pipe(
|
|
166
|
+
Stream.filter((evt) => evt.properties.file.endsWith("HEAD")),
|
|
167
|
+
Stream.runForEach((_evt) =>
|
|
168
|
+
Effect.gen(function* () {
|
|
169
|
+
const next = yield* get()
|
|
170
|
+
if (next !== value.current) {
|
|
171
|
+
log.info("branch changed", { from: value.current, to: next })
|
|
172
|
+
value.current = next
|
|
173
|
+
yield* bus.publish(Event.BranchUpdated, { branch: next })
|
|
174
|
+
}
|
|
175
|
+
}),
|
|
176
|
+
),
|
|
177
|
+
Effect.forkScoped,
|
|
178
|
+
)
|
|
179
|
+
|
|
180
|
+
return value
|
|
181
|
+
}),
|
|
182
|
+
),
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
return Service.of({
|
|
186
|
+
init: Effect.fn("Vcs.init")(function* () {
|
|
187
|
+
yield* InstanceState.get(state)
|
|
188
|
+
}),
|
|
189
|
+
branch: Effect.fn("Vcs.branch")(function* () {
|
|
190
|
+
return yield* InstanceState.use(state, (x) => x.current)
|
|
191
|
+
}),
|
|
192
|
+
defaultBranch: Effect.fn("Vcs.defaultBranch")(function* () {
|
|
193
|
+
return yield* InstanceState.use(state, (x) => x.root?.name)
|
|
194
|
+
}),
|
|
195
|
+
diff: Effect.fn("Vcs.diff")(function* (mode: Mode) {
|
|
196
|
+
const value = yield* InstanceState.get(state)
|
|
197
|
+
if (Instance.project.vcs !== "git") return []
|
|
198
|
+
if (mode === "git") {
|
|
199
|
+
return yield* track(
|
|
200
|
+
fs,
|
|
201
|
+
git,
|
|
202
|
+
Instance.directory,
|
|
203
|
+
(yield* git.hasHead(Instance.directory)) ? "HEAD" : undefined,
|
|
204
|
+
)
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!value.root) return []
|
|
208
|
+
if (value.current && value.current === value.root.name) return []
|
|
209
|
+
const ref = yield* git.mergeBase(Instance.directory, value.root.ref)
|
|
210
|
+
if (!ref) return []
|
|
211
|
+
return yield* compare(fs, git, Instance.directory, ref)
|
|
212
|
+
}),
|
|
213
|
+
})
|
|
214
|
+
}),
|
|
215
|
+
)
|
|
216
|
+
|
|
217
|
+
const defaultLayer = layer.pipe(
|
|
218
|
+
Layer.provide(Git.defaultLayer),
|
|
219
|
+
Layer.provide(AppFileSystem.defaultLayer),
|
|
220
|
+
Layer.provide(Bus.layer),
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
const { runPromise } = makeRuntime(Service, defaultLayer)
|
|
224
|
+
|
|
225
|
+
export async function init() {
|
|
226
|
+
return runPromise((svc) => svc.init())
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export async function branch() {
|
|
230
|
+
return runPromise((svc) => svc.branch())
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export async function defaultBranch() {
|
|
234
|
+
return runPromise((svc) => svc.defaultBranch())
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export async function diff(mode: Mode) {
|
|
238
|
+
return runPromise((svc) => svc.diff(mode))
|
|
239
|
+
}
|
|
240
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import type { AuthOAuthResult, Hooks } from "@opencode-ai/plugin"
|
|
2
|
+
import { NamedError } from "@opencode-ai/util/error"
|
|
3
|
+
import { Auth } from "@/auth"
|
|
4
|
+
import { InstanceState } from "@/effect/instance-state"
|
|
5
|
+
import { makeRuntime } from "@/effect/run-service"
|
|
6
|
+
import { Plugin } from "../plugin"
|
|
7
|
+
import { ProviderID } from "./schema"
|
|
8
|
+
import { Array as Arr, Effect, Layer, Record, Result, ServiceMap } from "effect"
|
|
9
|
+
import z from "zod"
|
|
10
|
+
|
|
11
|
+
export namespace ProviderAuth {
|
|
12
|
+
export const Method = z
|
|
13
|
+
.object({
|
|
14
|
+
type: z.union([z.literal("oauth"), z.literal("api")]),
|
|
15
|
+
label: z.string(),
|
|
16
|
+
prompts: z
|
|
17
|
+
.array(
|
|
18
|
+
z.union([
|
|
19
|
+
z.object({
|
|
20
|
+
type: z.literal("text"),
|
|
21
|
+
key: z.string(),
|
|
22
|
+
message: z.string(),
|
|
23
|
+
placeholder: z.string().optional(),
|
|
24
|
+
when: z
|
|
25
|
+
.object({
|
|
26
|
+
key: z.string(),
|
|
27
|
+
op: z.union([z.literal("eq"), z.literal("neq")]),
|
|
28
|
+
value: z.string(),
|
|
29
|
+
})
|
|
30
|
+
.optional(),
|
|
31
|
+
}),
|
|
32
|
+
z.object({
|
|
33
|
+
type: z.literal("select"),
|
|
34
|
+
key: z.string(),
|
|
35
|
+
message: z.string(),
|
|
36
|
+
options: z.array(
|
|
37
|
+
z.object({
|
|
38
|
+
label: z.string(),
|
|
39
|
+
value: z.string(),
|
|
40
|
+
hint: z.string().optional(),
|
|
41
|
+
}),
|
|
42
|
+
),
|
|
43
|
+
when: z
|
|
44
|
+
.object({
|
|
45
|
+
key: z.string(),
|
|
46
|
+
op: z.union([z.literal("eq"), z.literal("neq")]),
|
|
47
|
+
value: z.string(),
|
|
48
|
+
})
|
|
49
|
+
.optional(),
|
|
50
|
+
}),
|
|
51
|
+
]),
|
|
52
|
+
)
|
|
53
|
+
.optional(),
|
|
54
|
+
})
|
|
55
|
+
.meta({
|
|
56
|
+
ref: "ProviderAuthMethod",
|
|
57
|
+
})
|
|
58
|
+
export type Method = z.infer<typeof Method>
|
|
59
|
+
|
|
60
|
+
export const Authorization = z
|
|
61
|
+
.object({
|
|
62
|
+
url: z.string(),
|
|
63
|
+
method: z.union([z.literal("auto"), z.literal("code")]),
|
|
64
|
+
instructions: z.string(),
|
|
65
|
+
})
|
|
66
|
+
.meta({
|
|
67
|
+
ref: "ProviderAuthAuthorization",
|
|
68
|
+
})
|
|
69
|
+
export type Authorization = z.infer<typeof Authorization>
|
|
70
|
+
|
|
71
|
+
export const OauthMissing = NamedError.create("ProviderAuthOauthMissing", z.object({ providerID: ProviderID.zod }))
|
|
72
|
+
|
|
73
|
+
export const OauthCodeMissing = NamedError.create(
|
|
74
|
+
"ProviderAuthOauthCodeMissing",
|
|
75
|
+
z.object({ providerID: ProviderID.zod }),
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
export const OauthCallbackFailed = NamedError.create("ProviderAuthOauthCallbackFailed", z.object({}))
|
|
79
|
+
|
|
80
|
+
export const ValidationFailed = NamedError.create(
|
|
81
|
+
"ProviderAuthValidationFailed",
|
|
82
|
+
z.object({
|
|
83
|
+
field: z.string(),
|
|
84
|
+
message: z.string(),
|
|
85
|
+
}),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
export type Error =
|
|
89
|
+
| Auth.AuthError
|
|
90
|
+
| InstanceType<typeof OauthMissing>
|
|
91
|
+
| InstanceType<typeof OauthCodeMissing>
|
|
92
|
+
| InstanceType<typeof OauthCallbackFailed>
|
|
93
|
+
| InstanceType<typeof ValidationFailed>
|
|
94
|
+
|
|
95
|
+
type Hook = NonNullable<Hooks["auth"]>
|
|
96
|
+
|
|
97
|
+
export interface Interface {
|
|
98
|
+
readonly methods: () => Effect.Effect<Record<ProviderID, Method[]>>
|
|
99
|
+
readonly authorize: (input: {
|
|
100
|
+
providerID: ProviderID
|
|
101
|
+
method: number
|
|
102
|
+
inputs?: Record<string, string>
|
|
103
|
+
}) => Effect.Effect<Authorization | undefined, Error>
|
|
104
|
+
readonly callback: (input: { providerID: ProviderID; method: number; code?: string }) => Effect.Effect<void, Error>
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface State {
|
|
108
|
+
hooks: Record<ProviderID, Hook>
|
|
109
|
+
pending: Map<ProviderID, AuthOAuthResult>
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export class Service extends ServiceMap.Service<Service, Interface>()("@opencode/ProviderAuth") {}
|
|
113
|
+
|
|
114
|
+
export const layer: Layer.Layer<Service, never, Auth.Service | Plugin.Service> = Layer.effect(
|
|
115
|
+
Service,
|
|
116
|
+
Effect.gen(function* () {
|
|
117
|
+
const auth = yield* Auth.Service
|
|
118
|
+
const plugin = yield* Plugin.Service
|
|
119
|
+
const state = yield* InstanceState.make<State>(
|
|
120
|
+
Effect.fn("ProviderAuth.state")(function* () {
|
|
121
|
+
const plugins = yield* plugin.list()
|
|
122
|
+
return {
|
|
123
|
+
hooks: Record.fromEntries(
|
|
124
|
+
Arr.filterMap(plugins, (x) =>
|
|
125
|
+
x.auth?.provider !== undefined
|
|
126
|
+
? Result.succeed([ProviderID.make(x.auth.provider), x.auth] as const)
|
|
127
|
+
: Result.failVoid,
|
|
128
|
+
),
|
|
129
|
+
),
|
|
130
|
+
pending: new Map<ProviderID, AuthOAuthResult>(),
|
|
131
|
+
}
|
|
132
|
+
}),
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
const methods = Effect.fn("ProviderAuth.methods")(function* () {
|
|
136
|
+
const hooks = (yield* InstanceState.get(state)).hooks
|
|
137
|
+
return Record.map(hooks, (item) =>
|
|
138
|
+
item.methods.map(
|
|
139
|
+
(method): Method => ({
|
|
140
|
+
type: method.type,
|
|
141
|
+
label: method.label,
|
|
142
|
+
prompts: method.prompts?.map((prompt) => {
|
|
143
|
+
if (prompt.type === "select") {
|
|
144
|
+
return {
|
|
145
|
+
type: "select" as const,
|
|
146
|
+
key: prompt.key,
|
|
147
|
+
message: prompt.message,
|
|
148
|
+
options: prompt.options,
|
|
149
|
+
when: prompt.when,
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
return {
|
|
153
|
+
type: "text" as const,
|
|
154
|
+
key: prompt.key,
|
|
155
|
+
message: prompt.message,
|
|
156
|
+
placeholder: prompt.placeholder,
|
|
157
|
+
when: prompt.when,
|
|
158
|
+
}
|
|
159
|
+
}),
|
|
160
|
+
}),
|
|
161
|
+
),
|
|
162
|
+
)
|
|
163
|
+
})
|
|
164
|
+
|
|
165
|
+
const authorize = Effect.fn("ProviderAuth.authorize")(function* (input: {
|
|
166
|
+
providerID: ProviderID
|
|
167
|
+
method: number
|
|
168
|
+
inputs?: Record<string, string>
|
|
169
|
+
}) {
|
|
170
|
+
const { hooks, pending } = yield* InstanceState.get(state)
|
|
171
|
+
const method = hooks[input.providerID].methods[input.method]
|
|
172
|
+
if (method.type !== "oauth") return
|
|
173
|
+
|
|
174
|
+
if (method.prompts && input.inputs) {
|
|
175
|
+
for (const prompt of method.prompts) {
|
|
176
|
+
if (prompt.type === "text" && prompt.validate && input.inputs[prompt.key] !== undefined) {
|
|
177
|
+
const error = prompt.validate(input.inputs[prompt.key])
|
|
178
|
+
if (error) return yield* Effect.fail(new ValidationFailed({ field: prompt.key, message: error }))
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const result = yield* Effect.promise(() => method.authorize(input.inputs))
|
|
184
|
+
pending.set(input.providerID, result)
|
|
185
|
+
return {
|
|
186
|
+
url: result.url,
|
|
187
|
+
method: result.method,
|
|
188
|
+
instructions: result.instructions,
|
|
189
|
+
}
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
const callback = Effect.fn("ProviderAuth.callback")(function* (input: {
|
|
193
|
+
providerID: ProviderID
|
|
194
|
+
method: number
|
|
195
|
+
code?: string
|
|
196
|
+
}) {
|
|
197
|
+
const pending = (yield* InstanceState.get(state)).pending
|
|
198
|
+
const match = pending.get(input.providerID)
|
|
199
|
+
if (!match) return yield* Effect.fail(new OauthMissing({ providerID: input.providerID }))
|
|
200
|
+
if (match.method === "code" && !input.code) {
|
|
201
|
+
return yield* Effect.fail(new OauthCodeMissing({ providerID: input.providerID }))
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const result = yield* Effect.promise(() =>
|
|
205
|
+
match.method === "code" ? match.callback(input.code!) : match.callback(),
|
|
206
|
+
)
|
|
207
|
+
if (!result || result.type !== "success") return yield* Effect.fail(new OauthCallbackFailed({}))
|
|
208
|
+
|
|
209
|
+
if ("key" in result) {
|
|
210
|
+
yield* auth.set(input.providerID, {
|
|
211
|
+
type: "api",
|
|
212
|
+
key: result.key,
|
|
213
|
+
})
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
if ("refresh" in result) {
|
|
217
|
+
const { type: _, provider: __, refresh, access, expires, ...extra } = result
|
|
218
|
+
yield* auth.set(input.providerID, {
|
|
219
|
+
type: "oauth",
|
|
220
|
+
access,
|
|
221
|
+
refresh,
|
|
222
|
+
expires,
|
|
223
|
+
...extra,
|
|
224
|
+
})
|
|
225
|
+
}
|
|
226
|
+
})
|
|
227
|
+
|
|
228
|
+
return Service.of({ methods, authorize, callback })
|
|
229
|
+
}),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
export const defaultLayer = Layer.suspend(() =>
|
|
233
|
+
layer.pipe(Layer.provide(Auth.defaultLayer), Layer.provide(Plugin.defaultLayer)),
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
const { runPromise } = makeRuntime(Service, defaultLayer)
|
|
237
|
+
|
|
238
|
+
export async function methods() {
|
|
239
|
+
return runPromise((svc) => svc.methods())
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
export async function authorize(input: {
|
|
243
|
+
providerID: ProviderID
|
|
244
|
+
method: number
|
|
245
|
+
inputs?: Record<string, string>
|
|
246
|
+
}): Promise<Authorization | undefined> {
|
|
247
|
+
return runPromise((svc) => svc.authorize(input))
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export async function callback(input: { providerID: ProviderID; method: number; code?: string }) {
|
|
251
|
+
return runPromise((svc) => svc.callback(input))
|
|
252
|
+
}
|
|
253
|
+
}
|