@stage-labs/metro 0.1.0-beta.2 → 0.1.0-beta.200
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/dist/api.js +11 -0
- package/dist/claude-settings.js +40 -0
- package/dist/claude.js +163 -0
- package/dist/cli.js +77 -567
- package/dist/control.js +74 -0
- package/dist/hold.js +211 -0
- package/dist/local.js +43 -0
- package/dist/mcp-config.js +25 -0
- package/dist/node-name.js +71 -0
- package/dist/onboarding.js +44 -0
- package/dist/provider-flags.js +9 -0
- package/dist/route.js +58 -0
- package/dist/runtime-install.js +97 -0
- package/dist/runtime.js +74 -0
- package/dist/serve.js +158 -0
- package/dist/service.js +243 -0
- package/dist/tail.js +102 -144
- package/dist/update.js +45 -0
- package/dist/version.js +97 -0
- package/package.json +14 -23
- package/runtime/marketplace/.claude-plugin/marketplace.json +12 -0
- package/runtime/marketplace/plugin/.claude-plugin/plugin.json +17 -0
- package/runtime/marketplace/plugin/.mcp.json +1 -0
- package/runtime/marketplace/plugin/bin/guard.mjs +71 -0
- package/runtime/marketplace/plugin/bin/metro-plugin.mjs +30 -0
- package/runtime/marketplace/plugin/bin/policy.mjs +97 -0
- package/runtime/marketplace/plugin/bin/session-start.mjs +31 -0
- package/runtime/marketplace/plugin/hooks/hooks.json +25 -0
- package/runtime/marketplace/plugin/orchestrator.md +24 -0
- package/runtime/node_modules/@metro-labs/core/package.json +47 -0
- package/runtime/node_modules/@metro-labs/core/src/endpoints.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/events.ts +119 -0
- package/runtime/node_modules/@metro-labs/core/src/identity.ts +7 -0
- package/runtime/node_modules/@metro-labs/core/src/ids.ts +24 -0
- package/runtime/node_modules/@metro-labs/core/src/is-record.ts +2 -0
- package/runtime/node_modules/@metro-labs/core/src/lines.ts +91 -0
- package/runtime/node_modules/@metro-labs/core/src/log.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/protocol.ts +227 -0
- package/runtime/node_modules/@metro-labs/core/src/secure-fs.ts +74 -0
- package/runtime/node_modules/@metro-labs/core/src/station-names.ts +13 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-files.ts +41 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/account-store.ts +60 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/attachments.ts +196 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/messaging-normalize.ts +88 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/profile.ts +58 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/sender-profile.ts +64 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/station-runtime.ts +61 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-boot.ts +42 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/train-events.ts +89 -0
- package/runtime/node_modules/@metro-labs/core/src/stations/types.ts +123 -0
- package/runtime/node_modules/@metro-labs/core/src/str.ts +5 -0
- package/runtime/node_modules/@metro-labs/core/src/tickets.ts +37 -0
- package/runtime/node_modules/@metro-labs/core/src/train-error.ts +53 -0
- package/runtime/node_modules/@metro-labs/core/src/version.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/package.json +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/agent-fs.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files-api.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/files.ts +119 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/home-fs.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/job-env.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/provision.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedule-detail.ts +100 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules-api.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/schedules.ts +252 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/unit-files.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/user.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agent-user/view.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-attach.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-lookups.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/account-routes.ts +76 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/accounts-api.ts +357 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/admin.ts +30 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/allowlist.ts +59 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/api.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/bundle.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/file-admin.ts +305 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/files.ts +168 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/keys.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/map.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/policy-route.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/scope.ts +64 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/sender-cards.ts +86 -0
- package/runtime/node_modules/@metro-labs/daemon/src/agents/senders.ts +38 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/api.ts +55 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/needs.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/pending.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/approvals/preview.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/boot.ts +221 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/connect-hint.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/crash-guard.ts +49 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/local-owner.ts +21 -0
- package/runtime/node_modules/@metro-labs/daemon/src/boot/paths.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/addressed.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/dedupe.ts +12 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/inbound.ts +370 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/media-note.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/pending.ts +78 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/react-note.ts +13 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/relay.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/reply-id.ts +1 -0
- package/runtime/node_modules/@metro-labs/daemon/src/channels/webhook-note.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/api.ts +355 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/files.ts +359 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/login.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/memory.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/onboarding.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/plugin-install.ts +149 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session-files.ts +75 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/session.ts +283 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/settings.ts +109 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/setup.ts +271 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/skills.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/claude/version.ts +113 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/api.ts +306 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/config.ts +176 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/gates.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/health.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-client.ts +159 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-discovery.ts +135 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth-routes.ts +158 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/oauth.ts +166 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/plugin-sync.ts +131 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/reach.ts +27 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-policy.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay-target.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/relay.ts +348 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/store.ts +358 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/tools.ts +81 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/url.ts +37 -0
- package/runtime/node_modules/@metro-labs/daemon/src/connectors/verify.ts +106 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-grant.ts +99 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-owner.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-reaper.ts +174 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/attach-serve.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-api.ts +278 -0
- package/runtime/node_modules/@metro-labs/daemon/src/files/upload-store.ts +181 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/bedrock.ts +233 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-auth.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-device.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-stream.ts +173 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex-translate.ts +194 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/codex.ts +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/effort.ts +77 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/eventstream.ts +123 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/forward.ts +237 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/frames.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gateway.ts +274 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-auth.ts +160 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-client.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-setup.ts +150 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-stream.ts +139 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini-translate.ts +223 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/gemini.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-api.ts +199 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-config.ts +299 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-signin.ts +193 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/model-store.ts +70 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/openrouter.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/pkce.ts +36 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/provider-models.ts +66 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/served.ts +18 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/subscription.ts +120 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/text.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/gateway/usage.ts +244 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/accounts.ts +156 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/attachment-schema.ts +91 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/call-tools.ts +226 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/ctx.ts +62 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/event-store.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/group-tools.ts +129 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/index.ts +137 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/instructions.ts +19 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/member-tools.ts +72 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-prompt.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/permission-relay.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-gate.ts +83 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/policy-snapshot.ts +79 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-lookup.ts +60 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/profile-tool.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/raw-get-stream.ts +207 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/read-tool.ts +87 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/request-identity.ts +46 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session-slot.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/session.ts +202 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-catalog.ts +88 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-def.ts +9 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-dispatch.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/tool-schemas.ts +333 -0
- package/runtime/node_modules/@metro-labs/daemon/src/mcp/upload-tool.ts +61 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/api.ts +43 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/helper-script.ts +126 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/move.ts +165 -0
- package/runtime/node_modules/@metro-labs/daemon/src/metro-user/privilege.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/monitor/api.ts +132 -0
- package/runtime/node_modules/@metro-labs/daemon/src/net/tunnel.ts +316 -0
- package/runtime/node_modules/@metro-labs/daemon/src/policy/policy.ts +118 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/bearer.ts +35 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/body.ts +24 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/http.ts +365 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/local-mode.ts +208 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session-apis.ts +71 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/session.ts +29 -0
- package/runtime/node_modules/@metro-labs/daemon/src/routes/threema-callback.ts +103 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/control.ts +39 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/machine.ts +58 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/owner.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server/update.ts +90 -0
- package/runtime/node_modules/@metro-labs/daemon/src/server.ts +2 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-driver.ts +42 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-inline.ts +128 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-interactive.ts +134 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-outlook.ts +63 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-resolve.ts +225 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-session.ts +288 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach-xmtp.ts +111 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/attach.ts +248 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/materialize.ts +265 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/registry.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/runtime-deps.ts +82 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor-io.ts +97 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/supervisor.ts +320 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/threema-callbacks.ts +32 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/train-call.ts +22 -0
- package/runtime/node_modules/@metro-labs/daemon/src/stations/webhook-endpoints.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/api.ts +84 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/socket.ts +124 -0
- package/runtime/node_modules/@metro-labs/daemon/src/terminal/tickets.ts +15 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/api.ts +45 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/config.ts +51 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/firewall.ts +48 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/index.ts +96 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/install.ts +56 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/paths.ts +26 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/proxy.ts +148 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/store.ts +130 -0
- package/runtime/node_modules/@metro-labs/daemon/src/vault/trust.ts +61 -0
- package/runtime/node_modules/@metro-labs/discord-bot/package.json +26 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/accounts.ts +117 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/actions.ts +240 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/api-base.ts +1 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/attachments.ts +29 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/format.ts +174 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group-actions.ts +50 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/group.ts +115 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/index.ts +122 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/members.ts +215 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/profile.ts +68 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/send-files.ts +43 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/station.ts +22 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/verify.ts +82 -0
- package/runtime/node_modules/@metro-labs/discord-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/http/package.json +29 -0
- package/runtime/node_modules/@metro-labs/http/src/api-error.ts +8 -0
- package/runtime/node_modules/@metro-labs/http/src/api-http.ts +138 -0
- package/runtime/node_modules/@metro-labs/http/src/cors.ts +29 -0
- package/runtime/node_modules/@metro-labs/http/src/mode-api.ts +32 -0
- package/runtime/node_modules/@metro-labs/http/src/return-to.ts +21 -0
- package/runtime/node_modules/@metro-labs/http/src/workos-token.ts +149 -0
- package/runtime/node_modules/@metro-labs/outlook/package.json +25 -0
- package/runtime/node_modules/@metro-labs/outlook/src/accounts.ts +107 -0
- package/runtime/node_modules/@metro-labs/outlook/src/actions.ts +20 -0
- package/runtime/node_modules/@metro-labs/outlook/src/attachments.ts +97 -0
- package/runtime/node_modules/@metro-labs/outlook/src/auth.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/browser.ts +53 -0
- package/runtime/node_modules/@metro-labs/outlook/src/config.ts +36 -0
- package/runtime/node_modules/@metro-labs/outlook/src/format.ts +141 -0
- package/runtime/node_modules/@metro-labs/outlook/src/graph.ts +60 -0
- package/runtime/node_modules/@metro-labs/outlook/src/html.ts +72 -0
- package/runtime/node_modules/@metro-labs/outlook/src/inbound.ts +104 -0
- package/runtime/node_modules/@metro-labs/outlook/src/index.ts +12 -0
- package/runtime/node_modules/@metro-labs/outlook/src/login.ts +150 -0
- package/runtime/node_modules/@metro-labs/outlook/src/me.ts +41 -0
- package/runtime/node_modules/@metro-labs/outlook/src/outbound.ts +116 -0
- package/runtime/node_modules/@metro-labs/outlook/src/query.ts +119 -0
- package/runtime/node_modules/@metro-labs/outlook/src/state.ts +43 -0
- package/runtime/node_modules/@metro-labs/outlook/src/station.ts +15 -0
- package/runtime/node_modules/@metro-labs/outlook/src/trust.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/package.json +26 -0
- package/runtime/node_modules/@metro-labs/telegram/src/accounts.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/actions.ts +256 -0
- package/runtime/node_modules/@metro-labs/telegram/src/attachments.ts +87 -0
- package/runtime/node_modules/@metro-labs/telegram/src/client.ts +77 -0
- package/runtime/node_modules/@metro-labs/telegram/src/format.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram/src/group.ts +261 -0
- package/runtime/node_modules/@metro-labs/telegram/src/history.ts +52 -0
- package/runtime/node_modules/@metro-labs/telegram/src/inbound.ts +57 -0
- package/runtime/node_modules/@metro-labs/telegram/src/index.ts +7 -0
- package/runtime/node_modules/@metro-labs/telegram/src/login.ts +166 -0
- package/runtime/node_modules/@metro-labs/telegram/src/media-actions.ts +80 -0
- package/runtime/node_modules/@metro-labs/telegram/src/members.ts +75 -0
- package/runtime/node_modules/@metro-labs/telegram/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/telegram/src/profile.ts +70 -0
- package/runtime/node_modules/@metro-labs/telegram/src/reactions.ts +167 -0
- package/runtime/node_modules/@metro-labs/telegram/src/station.ts +27 -0
- package/runtime/node_modules/@metro-labs/telegram/src/types.ts +6 -0
- package/runtime/node_modules/@metro-labs/telegram/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/package.json +25 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/accounts.ts +119 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/actions.ts +246 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/attachments.ts +128 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/format.ts +173 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/index.ts +103 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/media-actions.ts +95 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/members.ts +47 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/profile.ts +65 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/station.ts +20 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/types.ts +56 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/verify.ts +38 -0
- package/runtime/node_modules/@metro-labs/telegram-bot/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/threema/package.json +26 -0
- package/runtime/node_modules/@metro-labs/threema/src/accounts.ts +106 -0
- package/runtime/node_modules/@metro-labs/threema/src/actions.ts +134 -0
- package/runtime/node_modules/@metro-labs/threema/src/api.ts +152 -0
- package/runtime/node_modules/@metro-labs/threema/src/crypto.ts +100 -0
- package/runtime/node_modules/@metro-labs/threema/src/files.ts +58 -0
- package/runtime/node_modules/@metro-labs/threema/src/format.ts +169 -0
- package/runtime/node_modules/@metro-labs/threema/src/groups.ts +96 -0
- package/runtime/node_modules/@metro-labs/threema/src/ids.ts +21 -0
- package/runtime/node_modules/@metro-labs/threema/src/inbound.ts +89 -0
- package/runtime/node_modules/@metro-labs/threema/src/index.ts +24 -0
- package/runtime/node_modules/@metro-labs/threema/src/messages.ts +264 -0
- package/runtime/node_modules/@metro-labs/threema/src/outbound.ts +160 -0
- package/runtime/node_modules/@metro-labs/threema/src/station.ts +13 -0
- package/runtime/node_modules/@metro-labs/threema/src/verify.ts +117 -0
- package/runtime/node_modules/@metro-labs/webhook/package.json +23 -0
- package/runtime/node_modules/@metro-labs/webhook/src/station.ts +37 -0
- package/runtime/node_modules/@metro-labs/whatsapp/package.json +27 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/accounts.ts +66 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/ack.ts +101 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/actions.ts +213 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/attachments.ts +72 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/auth-state.ts +129 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/client.ts +388 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/delivery.ts +49 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/format.ts +83 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/inbound.ts +40 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/index.ts +7 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/keys.ts +59 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/logger.ts +60 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/login.ts +176 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/media.ts +84 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/names.ts +28 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/normalize.ts +22 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/outbox.ts +38 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/parse.ts +185 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/resolve.ts +52 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/station.ts +24 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/token-store.ts +94 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/types.ts +5 -0
- package/runtime/node_modules/@metro-labs/whatsapp/src/wire.ts +1 -0
- package/runtime/node_modules/@metro-labs/xmtp/package.json +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/accounts.ts +114 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-close.ts +110 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-conv.ts +101 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions-meta.ts +118 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/actions.ts +244 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/attachments.ts +107 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/codecs.ts +171 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/conv-helpers.ts +239 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit-payloads.ts +316 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/emit.ts +69 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group-members.ts +34 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/group.ts +56 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/identity.ts +47 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/index.ts +122 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/labels.ts +139 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/member-args.ts +40 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/names.ts +61 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/profile.ts +153 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/sender.ts +112 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/smart.ts +72 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/station.ts +17 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools-handlers.ts +79 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/tools.ts +198 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/transcribe.ts +88 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/verify.ts +98 -0
- package/runtime/node_modules/@metro-labs/xmtp/src/wire.ts +51 -0
- package/runtime/runtime.json +3 -0
- package/runtime/server.ts +1 -0
- package/runtime/stations.json +37 -0
- package/runtime/trains/.keep +0 -0
- package/LICENSE +0 -21
- package/README.md +0 -78
- package/dist/channels/discord.js +0 -148
- package/dist/channels/telegram.js +0 -137
- package/dist/lib/address.js +0 -21
- package/dist/lib/codex-rc.js +0 -274
- package/dist/lib/dotenv.js +0 -31
- package/dist/log.js +0 -12
- package/dist/paths.js +0 -45
- package/skills/metro/SKILL.md +0 -122
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
2
|
+
import { errorFrame } from './forward.js';
|
|
3
|
+
import { frame, messageEnd, messageStart, type Usage } from './frames.js';
|
|
4
|
+
import { encodeSignature, newCallId, rememberSignature } from './gemini-translate.js';
|
|
5
|
+
import { stringOf } from './text.js';
|
|
6
|
+
|
|
7
|
+
type Item = Record<string, unknown>;
|
|
8
|
+
|
|
9
|
+
interface Open {
|
|
10
|
+
index: number;
|
|
11
|
+
kind: 'text' | 'thinking';
|
|
12
|
+
signature: string | null;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const num = (value: unknown): number => (typeof value === 'number' ? value : 0);
|
|
16
|
+
|
|
17
|
+
const STOP_OF: Record<string, string> = { STOP: 'end_turn', MAX_TOKENS: 'max_tokens' };
|
|
18
|
+
const REFUSALS = new Set(['SAFETY', 'RECITATION', 'BLOCKLIST', 'PROHIBITED_CONTENT', 'SPII', 'MALFORMED_FUNCTION_CALL']);
|
|
19
|
+
|
|
20
|
+
function usageOf(meta: Item): Usage {
|
|
21
|
+
const cached = num(meta.cachedContentTokenCount);
|
|
22
|
+
return {
|
|
23
|
+
input_tokens: Math.max(0, num(meta.promptTokenCount) - cached),
|
|
24
|
+
output_tokens: num(meta.candidatesTokenCount) + num(meta.thoughtsTokenCount),
|
|
25
|
+
cache_read_input_tokens: cached,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export class GeminiStreamTranslator {
|
|
30
|
+
private next = 0;
|
|
31
|
+
private open: Open | null = null;
|
|
32
|
+
private started = false;
|
|
33
|
+
private done = false;
|
|
34
|
+
private toolCalls = 0;
|
|
35
|
+
private usage: Usage = { input_tokens: 0, output_tokens: 0, cache_read_input_tokens: 0 };
|
|
36
|
+
private stop = 'end_turn';
|
|
37
|
+
private finishedBy: string | null = null;
|
|
38
|
+
private refusal: string | null = null;
|
|
39
|
+
private readonly model: string;
|
|
40
|
+
private readonly restore: (name: string) => string;
|
|
41
|
+
|
|
42
|
+
constructor(model: string, restore: (name: string) => string = (name) => name) {
|
|
43
|
+
this.model = model;
|
|
44
|
+
this.restore = restore;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
get finished(): boolean {
|
|
48
|
+
return this.done;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private start(): string {
|
|
52
|
+
if (this.started) return '';
|
|
53
|
+
this.started = true;
|
|
54
|
+
return messageStart('msg_gemini', this.model);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private closeOpen(): string {
|
|
58
|
+
const block = this.open;
|
|
59
|
+
if (block === null) return '';
|
|
60
|
+
this.open = null;
|
|
61
|
+
const signature = block.kind === 'thinking' ? frame('content_block_delta', { type: 'content_block_delta', index: block.index, delta: { type: 'signature_delta', signature: encodeSignature(block.signature ?? '') } }) : '';
|
|
62
|
+
return signature + frame('content_block_stop', { type: 'content_block_stop', index: block.index });
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private ensure(kind: Open['kind']): string {
|
|
66
|
+
if (this.open?.kind === kind) return '';
|
|
67
|
+
const closed = this.closeOpen();
|
|
68
|
+
this.open = { index: this.next, kind, signature: null };
|
|
69
|
+
this.next += 1;
|
|
70
|
+
const content = kind === 'text' ? { type: 'text', text: '' } : { type: 'thinking', thinking: '' };
|
|
71
|
+
return closed + frame('content_block_start', { type: 'content_block_start', index: this.open.index, content_block: content });
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
private textPart(part: Item): string {
|
|
75
|
+
const kind = part.thought === true ? 'thinking' : 'text';
|
|
76
|
+
const text = stringOf(part.text);
|
|
77
|
+
let out = this.ensure(kind);
|
|
78
|
+
const block = this.open;
|
|
79
|
+
if (block !== null && stringOf(part.thoughtSignature) !== '') block.signature = stringOf(part.thoughtSignature);
|
|
80
|
+
if (text === '') return out;
|
|
81
|
+
const delta = kind === 'text' ? { type: 'text_delta', text } : { type: 'thinking_delta', thinking: text };
|
|
82
|
+
out += frame('content_block_delta', { type: 'content_block_delta', index: block?.index ?? 0, delta });
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
private callPart(call: Item, signature: string): string {
|
|
87
|
+
this.toolCalls += 1;
|
|
88
|
+
const id = stringOf(call.id) || newCallId();
|
|
89
|
+
if (signature !== '') rememberSignature(id, signature);
|
|
90
|
+
const index = this.next;
|
|
91
|
+
this.next += 1;
|
|
92
|
+
return (
|
|
93
|
+
this.closeOpen() +
|
|
94
|
+
frame('content_block_start', { type: 'content_block_start', index, content_block: { type: 'tool_use', id, name: this.restore(stringOf(call.name)), input: {} } }) +
|
|
95
|
+
frame('content_block_delta', { type: 'content_block_delta', index, delta: { type: 'input_json_delta', partial_json: JSON.stringify(isRecord(call.args) ? call.args : {}) } }) +
|
|
96
|
+
frame('content_block_stop', { type: 'content_block_stop', index })
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private part(part: Item): string {
|
|
101
|
+
if (isRecord(part.functionCall)) return this.callPart(part.functionCall, stringOf(part.thoughtSignature));
|
|
102
|
+
if (typeof part.text === 'string') return this.textPart(part);
|
|
103
|
+
return '';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private candidate(candidate: Item): string {
|
|
107
|
+
const content = isRecord(candidate.content) ? candidate.content : {};
|
|
108
|
+
const parts = Array.isArray(content.parts) ? content.parts.filter(isRecord) : [];
|
|
109
|
+
const out = parts.map((part) => this.part(part)).join('');
|
|
110
|
+
const finish = stringOf(candidate.finishReason);
|
|
111
|
+
if (finish !== '') this.finishedBy = finish;
|
|
112
|
+
const stop = STOP_OF[finish];
|
|
113
|
+
if (stop !== undefined) this.stop = stop;
|
|
114
|
+
else if (REFUSALS.has(finish)) this.refusal = `Gemini stopped the answer (${finish})`;
|
|
115
|
+
return out;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
push(data: Item): string {
|
|
119
|
+
if (this.done) return '';
|
|
120
|
+
const response = isRecord(data.response) ? data.response : data;
|
|
121
|
+
const out = this.start();
|
|
122
|
+
if (isRecord(response.usageMetadata)) this.usage = usageOf(response.usageMetadata);
|
|
123
|
+
const candidate = Array.isArray(response.candidates) ? response.candidates.find(isRecord) : undefined;
|
|
124
|
+
if (candidate !== undefined) return out + this.candidate(candidate);
|
|
125
|
+
const feedback = isRecord(response.promptFeedback) ? stringOf(response.promptFeedback.blockReason) : '';
|
|
126
|
+
if (feedback !== '') this.refusal = `Gemini blocked the prompt (${feedback})`;
|
|
127
|
+
return out;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
close(error?: string): string {
|
|
131
|
+
if (this.done) return '';
|
|
132
|
+
this.done = true;
|
|
133
|
+
const out = this.start() + this.closeOpen();
|
|
134
|
+
const unfinished = this.finishedBy === null && this.refusal === null ? 'Gemini ended the stream before completing the response' : null;
|
|
135
|
+
const failure = error ?? this.refusal ?? unfinished;
|
|
136
|
+
if (failure !== null) return out + errorFrame('api_error', failure);
|
|
137
|
+
return out + messageEnd(this.toolCalls > 0 ? 'tool_use' : this.stop, this.usage);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
+
import { ToolNames } from './codex-translate.js';
|
|
4
|
+
import { CLIENT_NAME, requestId, SYSTEM_PREFIX } from './gemini-client.js';
|
|
5
|
+
import { cappedEffort, effortToApply } from './effort.js';
|
|
6
|
+
import { resultText, stringOf } from './text.js';
|
|
7
|
+
|
|
8
|
+
type Item = Record<string, unknown>;
|
|
9
|
+
|
|
10
|
+
export const SIGNATURE_PREFIX = 'metro-gemini:';
|
|
11
|
+
export const SKIP_SIGNATURE = 'skip_thought_signature_validator';
|
|
12
|
+
const SCHEMA_KEEP = new Set([
|
|
13
|
+
'type',
|
|
14
|
+
'description',
|
|
15
|
+
'nullable',
|
|
16
|
+
'enum',
|
|
17
|
+
'required',
|
|
18
|
+
'minItems',
|
|
19
|
+
'maxItems',
|
|
20
|
+
'minProperties',
|
|
21
|
+
'maxProperties',
|
|
22
|
+
'minLength',
|
|
23
|
+
'maxLength',
|
|
24
|
+
'pattern',
|
|
25
|
+
'minimum',
|
|
26
|
+
'maximum',
|
|
27
|
+
'propertyOrdering',
|
|
28
|
+
]);
|
|
29
|
+
const SCHEMA_LISTS = new Set(['anyOf', 'oneOf']);
|
|
30
|
+
const SIGNATURES_MAX = 2000;
|
|
31
|
+
const MAX_OUTPUT_TOKENS = 16384;
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const callSignatures = new Map<string, string>();
|
|
35
|
+
|
|
36
|
+
export function rememberSignature(id: string, signature: string): void {
|
|
37
|
+
callSignatures.set(id, signature);
|
|
38
|
+
if (callSignatures.size > SIGNATURES_MAX) {
|
|
39
|
+
const first = callSignatures.keys().next().value;
|
|
40
|
+
if (first !== undefined) callSignatures.delete(first);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const newCallId = (): string => `toolu_gm_${randomBytes(12).toString('base64url')}`;
|
|
45
|
+
|
|
46
|
+
export const encodeSignature = (signature: string): string => `${SIGNATURE_PREFIX}${Buffer.from(signature).toString('base64url')}`;
|
|
47
|
+
|
|
48
|
+
export function decodeSignature(signature: unknown): string | null {
|
|
49
|
+
if (typeof signature !== 'string' || !signature.startsWith(SIGNATURE_PREFIX)) return null;
|
|
50
|
+
const raw = Buffer.from(signature.slice(SIGNATURE_PREFIX.length), 'base64url').toString('utf8');
|
|
51
|
+
return raw === '' ? null : raw;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function systemText(system: unknown): string {
|
|
55
|
+
if (typeof system === 'string') return system.trim();
|
|
56
|
+
if (!Array.isArray(system)) return '';
|
|
57
|
+
return system
|
|
58
|
+
.filter(isRecord)
|
|
59
|
+
.map((block) => stringOf(block.text))
|
|
60
|
+
.filter((t) => t !== '')
|
|
61
|
+
.join('\n\n')
|
|
62
|
+
.trim();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function userPart(block: Item): Item | null {
|
|
66
|
+
if (block.type === 'text') return { text: stringOf(block.text) };
|
|
67
|
+
if (block.type === 'image' && isRecord(block.source) && block.source.type === 'base64')
|
|
68
|
+
return { inlineData: { mimeType: stringOf(block.source.media_type), data: stringOf(block.source.data) } };
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function userParts(content: unknown, calls: Map<string, string>): Item[] {
|
|
73
|
+
if (typeof content === 'string') return content === '' ? [] : [{ text: content }];
|
|
74
|
+
if (!Array.isArray(content)) return [];
|
|
75
|
+
const out: Item[] = [];
|
|
76
|
+
for (const block of content.filter(isRecord)) {
|
|
77
|
+
if (block.type === 'tool_result') {
|
|
78
|
+
const id = stringOf(block.tool_use_id);
|
|
79
|
+
out.push({ functionResponse: { id, name: calls.get(id) ?? 'tool', response: { result: resultText(block) } } });
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const part = userPart(block);
|
|
83
|
+
if (part !== null) out.push(part);
|
|
84
|
+
}
|
|
85
|
+
return out;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function callPart(block: Item, names: ToolNames, calls: Map<string, string>): Item {
|
|
89
|
+
const id = stringOf(block.id);
|
|
90
|
+
const name = names.alias(stringOf(block.name));
|
|
91
|
+
calls.set(id, name);
|
|
92
|
+
return { functionCall: { id, name, args: isRecord(block.input) ? block.input : {} }, thoughtSignature: callSignatures.get(id) ?? SKIP_SIGNATURE };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function assistantPart(block: Item, names: ToolNames, calls: Map<string, string>): Item | null {
|
|
96
|
+
if (block.type === 'text') return { text: stringOf(block.text) };
|
|
97
|
+
if (block.type === 'tool_use') return callPart(block, names, calls);
|
|
98
|
+
if (block.type !== 'thinking') return null;
|
|
99
|
+
const signature = decodeSignature(block.signature);
|
|
100
|
+
return signature === null ? null : { text: stringOf(block.thinking) || ' ', thought: true, thoughtSignature: signature };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function assistantParts(content: unknown, names: ToolNames, calls: Map<string, string>): Item[] {
|
|
104
|
+
if (typeof content === 'string') return content === '' ? [] : [{ text: content }];
|
|
105
|
+
if (!Array.isArray(content)) return [];
|
|
106
|
+
const out: Item[] = [];
|
|
107
|
+
for (const block of content.filter(isRecord)) {
|
|
108
|
+
const part = assistantPart(block, names, calls);
|
|
109
|
+
if (part !== null) out.push(part);
|
|
110
|
+
}
|
|
111
|
+
return out;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function appendTurn(out: Item[], role: string, parts: Item[]): void {
|
|
115
|
+
if (parts.length === 0) return;
|
|
116
|
+
const last = out.at(-1);
|
|
117
|
+
const held = last?.role === role && Array.isArray(last.parts) ? (last.parts as Item[]) : null;
|
|
118
|
+
if (held !== null) held.push(...parts);
|
|
119
|
+
else out.push({ role, parts });
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function contentsOf(messages: unknown, names = new ToolNames()): Item[] {
|
|
123
|
+
const out: Item[] = [];
|
|
124
|
+
if (!Array.isArray(messages)) return out;
|
|
125
|
+
const calls = new Map<string, string>();
|
|
126
|
+
for (const message of messages.filter(isRecord)) {
|
|
127
|
+
const model = message.role === 'assistant';
|
|
128
|
+
appendTurn(out, model ? 'model' : 'user', model ? assistantParts(message.content, names, calls) : userParts(message.content, calls));
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
function schemaType(value: unknown, out: Item): void {
|
|
134
|
+
if (!Array.isArray(value)) {
|
|
135
|
+
out.type = value;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
const kinds = value.filter((k) => k !== 'null');
|
|
139
|
+
if (kinds.length !== value.length) out.nullable = true;
|
|
140
|
+
if (kinds.length > 0) out.type = kinds[0];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function schemaField(key: string, value: unknown, out: Item): void {
|
|
144
|
+
if (key === 'type') schemaType(value, out);
|
|
145
|
+
else if (key === 'const') out.enum = [value];
|
|
146
|
+
else if (key === 'items') out.items = cleanSchema(value);
|
|
147
|
+
else if (key === 'properties' && isRecord(value)) out.properties = Object.fromEntries(Object.entries(value).map(([name, v]) => [name, cleanSchema(v)]));
|
|
148
|
+
else if (SCHEMA_LISTS.has(key) && Array.isArray(value)) out.anyOf = value.map(cleanSchema);
|
|
149
|
+
else if (SCHEMA_KEEP.has(key)) out[key] = value;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function cleanSchema(schema: unknown): Item {
|
|
153
|
+
if (!isRecord(schema)) return {};
|
|
154
|
+
const out: Item = {};
|
|
155
|
+
for (const [key, value] of Object.entries(schema)) schemaField(key, value, out);
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function toolDeclarations(tools: unknown, names = new ToolNames()): Item[] {
|
|
160
|
+
if (!Array.isArray(tools)) return [];
|
|
161
|
+
return tools
|
|
162
|
+
.filter(isRecord)
|
|
163
|
+
.filter((tool) => typeof tool.name === 'string' && (tool.type === undefined || tool.type === 'custom'))
|
|
164
|
+
.map((tool) => ({
|
|
165
|
+
name: names.alias(stringOf(tool.name)),
|
|
166
|
+
description: stringOf(tool.description),
|
|
167
|
+
parameters: cleanSchema(isRecord(tool.input_schema) ? tool.input_schema : { type: 'object', properties: {} }),
|
|
168
|
+
}));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function toolConfig(choice: unknown, names: ToolNames): Item | null {
|
|
172
|
+
if (!isRecord(choice)) return null;
|
|
173
|
+
if (choice.type === 'any') return { functionCallingConfig: { mode: 'ANY' } };
|
|
174
|
+
if (choice.type === 'none') return { functionCallingConfig: { mode: 'NONE' } };
|
|
175
|
+
if (choice.type === 'tool') return { functionCallingConfig: { mode: 'ANY', allowedFunctionNames: [names.alias(stringOf(choice.name))] } };
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function generationConfig(body: Item): Item {
|
|
180
|
+
const out: Item = {};
|
|
181
|
+
if (typeof body.max_tokens === 'number') out.maxOutputTokens = Math.min(body.max_tokens, MAX_OUTPUT_TOKENS);
|
|
182
|
+
if (typeof body.temperature === 'number') out.temperature = body.temperature;
|
|
183
|
+
if (typeof body.top_p === 'number') out.topP = body.top_p;
|
|
184
|
+
if (isRecord(body.thinking) && body.thinking.type === 'enabled') out.thinkingConfig = { includeThoughts: true };
|
|
185
|
+
const effort = effortToApply(body);
|
|
186
|
+
if (effort !== null) out.thinkingLevel = cappedEffort(effort);
|
|
187
|
+
return out;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface GeminiRequest {
|
|
191
|
+
model: string;
|
|
192
|
+
project: string;
|
|
193
|
+
userAgent: string;
|
|
194
|
+
requestType: string;
|
|
195
|
+
requestId: string;
|
|
196
|
+
request: Item;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
const systemParts = (system: string): Item[] => [
|
|
200
|
+
{ text: SYSTEM_PREFIX },
|
|
201
|
+
{ text: `Please ignore the following [ignore]${SYSTEM_PREFIX}[/ignore]` },
|
|
202
|
+
...(system === '' ? [] : [{ text: system }]),
|
|
203
|
+
];
|
|
204
|
+
|
|
205
|
+
export function toGeminiRequest(body: Item, model: string, project: string, promptId: string, names = new ToolNames()): GeminiRequest {
|
|
206
|
+
const declarations = toolDeclarations(body.tools, names);
|
|
207
|
+
const config = toolConfig(body.tool_choice, names);
|
|
208
|
+
return {
|
|
209
|
+
model,
|
|
210
|
+
project,
|
|
211
|
+
userAgent: CLIENT_NAME,
|
|
212
|
+
requestType: 'agent',
|
|
213
|
+
requestId: requestId(),
|
|
214
|
+
request: {
|
|
215
|
+
contents: contentsOf(body.messages, names),
|
|
216
|
+
systemInstruction: { role: 'user', parts: systemParts(systemText(body.system)) },
|
|
217
|
+
...(declarations.length === 0 ? {} : { tools: [{ functionDeclarations: declarations }] }),
|
|
218
|
+
...(config === null ? {} : { toolConfig: config }),
|
|
219
|
+
generationConfig: generationConfig(body),
|
|
220
|
+
sessionId: promptId,
|
|
221
|
+
},
|
|
222
|
+
};
|
|
223
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
2
|
+
import { isRecord } from '@metro-labs/core/is-record';
|
|
3
|
+
import { errMsg, log } from '@metro-labs/core/log';
|
|
4
|
+
import { refreshTokens, tokensStale, type GeminiTokens } from './gemini-auth.js';
|
|
5
|
+
import { API, clientHeaders, generateBases, machineSessionId, type Bases } from './gemini-client.js';
|
|
6
|
+
import { GeminiStreamTranslator } from './gemini-stream.js';
|
|
7
|
+
import { toGeminiRequest } from './gemini-translate.js';
|
|
8
|
+
import { ToolNames } from './codex-translate.js';
|
|
9
|
+
import { GatewayError, providerStatus, sendError, upstreamMessage, type Watch } from './forward.js';
|
|
10
|
+
import type { Connection } from './model-config.js';
|
|
11
|
+
import { answerWhole, currentOf, errorKind, reach, refreshed, relayTranslated, sessionHeader, type TokenSource, type TokenState } from './subscription.js';
|
|
12
|
+
import { effortToApply, withoutEffort } from './effort.js';
|
|
13
|
+
|
|
14
|
+
export interface GeminiDeps {
|
|
15
|
+
base?: Bases;
|
|
16
|
+
tokenBase?: string;
|
|
17
|
+
fetchImpl?: typeof fetch;
|
|
18
|
+
save: (id: string, tokens: GeminiTokens) => void;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type GeminiState = TokenState<GeminiTokens>;
|
|
22
|
+
|
|
23
|
+
export const sharedGeminiState: GeminiState = new Map();
|
|
24
|
+
|
|
25
|
+
const sourceOf = (deps: GeminiDeps): TokenSource<GeminiTokens> => ({
|
|
26
|
+
label: 'Google',
|
|
27
|
+
stale: (tokens) => tokensStale(tokens),
|
|
28
|
+
refresh: (tokens) => refreshTokens(tokens, deps.tokenBase, deps.fetchImpl),
|
|
29
|
+
save: deps.save,
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export const currentGeminiTokens = (conn: Connection, deps: GeminiDeps, state: GeminiState): Promise<GeminiTokens> =>
|
|
33
|
+
currentOf(state, conn.id, conn.gemini, sourceOf(deps), 'Gemini is not connected: sign in with Google on the Model page.');
|
|
34
|
+
|
|
35
|
+
interface Call {
|
|
36
|
+
body: Record<string, unknown>;
|
|
37
|
+
model: string;
|
|
38
|
+
promptId: string;
|
|
39
|
+
watch: Watch;
|
|
40
|
+
deps: GeminiDeps;
|
|
41
|
+
names: ToolNames;
|
|
42
|
+
conn: Connection;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const headersFor = (token: string, stream: boolean): Record<string, string> => ({
|
|
46
|
+
authorization: `Bearer ${token}`,
|
|
47
|
+
'content-type': 'application/json',
|
|
48
|
+
...clientHeaders(),
|
|
49
|
+
'x-machine-session-id': machineSessionId(),
|
|
50
|
+
...(stream ? { accept: 'text/event-stream' } : {}),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
async function sendTo(base: string, call: Call, tokens: GeminiTokens, stream: boolean): Promise<Response> {
|
|
54
|
+
const request = toGeminiRequest(call.body, call.model, tokens.project, call.promptId, call.names);
|
|
55
|
+
const method = stream ? 'streamGenerateContent?alt=sse' : 'generateContent';
|
|
56
|
+
return (call.deps.fetchImpl ?? fetch)(`${base}/${API}:${method}`, {
|
|
57
|
+
method: 'POST',
|
|
58
|
+
headers: headersFor(tokens.accessToken, stream),
|
|
59
|
+
body: JSON.stringify(request),
|
|
60
|
+
signal: call.watch.signal,
|
|
61
|
+
redirect: 'manual',
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async function send(call: Call, tokens: GeminiTokens, stream: boolean): Promise<Response> {
|
|
66
|
+
const bases = generateBases(call.deps.base);
|
|
67
|
+
let last: Response | null = null;
|
|
68
|
+
for (const base of bases) {
|
|
69
|
+
if (last !== null) await last.body?.cancel();
|
|
70
|
+
try {
|
|
71
|
+
last = await sendTo(base, call, tokens, stream);
|
|
72
|
+
} catch (err) {
|
|
73
|
+
if (base === bases.at(-1) || call.watch.signal.aborted) throw err;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
if (last.status < 500 && last.status !== 429) return last;
|
|
77
|
+
}
|
|
78
|
+
if (last === null) throw new GatewayError(502, 'api_error', 'no Code Assist endpoint to call');
|
|
79
|
+
return last;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const REFUSAL_LOG_MAX = 2000;
|
|
83
|
+
|
|
84
|
+
const detailText = (detail: Record<string, unknown>): string[] => {
|
|
85
|
+
const out: string[] = [];
|
|
86
|
+
if (typeof detail.reason === 'string') out.push(detail.reason);
|
|
87
|
+
if (isRecord(detail.metadata)) out.push(...Object.entries(detail.metadata).map(([k, v]) => `${k}=${String(v)}`));
|
|
88
|
+
if (typeof detail.retryDelay === 'string') out.push(`retry after ${detail.retryDelay}`);
|
|
89
|
+
if (Array.isArray(detail.violations))
|
|
90
|
+
out.push(...detail.violations.filter(isRecord).map((v) => [v.subject, v.description].filter((x) => typeof x === 'string').join(': ')));
|
|
91
|
+
return out.filter((x) => x !== '');
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
export function refusalMessage(text: string, status: number): string {
|
|
95
|
+
const message = upstreamMessage(text, `Gemini answered ${String(status)}`);
|
|
96
|
+
let details: unknown = null;
|
|
97
|
+
try {
|
|
98
|
+
const parsed: unknown = JSON.parse(text);
|
|
99
|
+
details = isRecord(parsed) && isRecord(parsed.error) ? parsed.error.details : null;
|
|
100
|
+
} catch {
|
|
101
|
+
return message;
|
|
102
|
+
}
|
|
103
|
+
const notes = Array.isArray(details) ? details.filter(isRecord).flatMap(detailText) : [];
|
|
104
|
+
return notes.length === 0 ? message : `${message} (${notes.join('; ')})`;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const reachWith = (call: Call, state: GeminiState, stream: boolean): Promise<Response> =>
|
|
108
|
+
reach(
|
|
109
|
+
() => currentGeminiTokens(call.conn, call.deps, state),
|
|
110
|
+
(tokens) => refreshed(state, call.conn.id, tokens, sourceOf(call.deps)),
|
|
111
|
+
(tokens) => send(call, tokens, stream),
|
|
112
|
+
);
|
|
113
|
+
|
|
114
|
+
export interface GeminiModel {
|
|
115
|
+
id: string;
|
|
116
|
+
name: string;
|
|
117
|
+
remaining: number | null;
|
|
118
|
+
resetAt: string | null;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const modelOf = (id: string, raw: unknown): GeminiModel => {
|
|
122
|
+
const entry = isRecord(raw) ? raw : {};
|
|
123
|
+
const quota = isRecord(entry.quotaInfo) ? entry.quotaInfo : null;
|
|
124
|
+
const fraction = quota !== null && typeof quota.remainingFraction === 'number' ? quota.remainingFraction : null;
|
|
125
|
+
const resetAt = quota !== null && typeof quota.resetTime === 'string' ? quota.resetTime : null;
|
|
126
|
+
return { id, name: typeof entry.displayName === 'string' ? entry.displayName : id, remaining: fraction ?? (resetAt === null ? null : 0), resetAt };
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export async function listGeminiModels(tokens: GeminiTokens, deps: GeminiDeps): Promise<GeminiModel[]> {
|
|
130
|
+
let last = 'no Code Assist endpoint to call';
|
|
131
|
+
for (const base of generateBases(deps.base)) {
|
|
132
|
+
try {
|
|
133
|
+
const res = await (deps.fetchImpl ?? fetch)(`${base}/${API}:fetchAvailableModels`, {
|
|
134
|
+
method: 'POST',
|
|
135
|
+
headers: headersFor(tokens.accessToken, false),
|
|
136
|
+
body: JSON.stringify({ project: tokens.project }),
|
|
137
|
+
signal: AbortSignal.timeout(30_000),
|
|
138
|
+
});
|
|
139
|
+
const body: unknown = await res.json().catch(() => null);
|
|
140
|
+
if (!res.ok) {
|
|
141
|
+
last = upstreamMessage(JSON.stringify(body), `Google answered ${String(res.status)}`);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
const models = isRecord(body) && isRecord(body.models) ? body.models : {};
|
|
145
|
+
return Object.entries(models)
|
|
146
|
+
.filter(([id]) => id.startsWith('gemini'))
|
|
147
|
+
.map(([id, raw]) => modelOf(id, raw));
|
|
148
|
+
} catch (err) {
|
|
149
|
+
last = errMsg(err);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
throw new GatewayError(502, 'api_error', `could not list the Gemini models: ${last}`);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const parseData = (raw: string): Record<string, unknown> | null => {
|
|
156
|
+
try {
|
|
157
|
+
const parsed: unknown = JSON.parse(raw);
|
|
158
|
+
return isRecord(parsed) ? parsed : null;
|
|
159
|
+
} catch {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const NAMES_THINKING = /thinking|thought/i;
|
|
165
|
+
|
|
166
|
+
function refuse(res: ServerResponse, model: string, status: number, text: string): void {
|
|
167
|
+
log.warn({ provider: 'gemini', model, status, body: text.slice(0, REFUSAL_LOG_MAX) }, 'gateway: the provider refused the request');
|
|
168
|
+
sendError(res, providerStatus(status), errorKind(status), refusalMessage(text, status));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export async function geminiMessages(
|
|
172
|
+
req: IncomingMessage,
|
|
173
|
+
res: ServerResponse,
|
|
174
|
+
body: Record<string, unknown>,
|
|
175
|
+
model: string,
|
|
176
|
+
conn: Connection,
|
|
177
|
+
deps: GeminiDeps,
|
|
178
|
+
state: GeminiState,
|
|
179
|
+
watch: Watch,
|
|
180
|
+
): Promise<void> {
|
|
181
|
+
const stream = body.stream === true;
|
|
182
|
+
let call: Call = { body, model, promptId: sessionHeader(req) || 'metro', watch, deps, names: new ToolNames(), conn };
|
|
183
|
+
let upstream = await reachWith(call, state, stream);
|
|
184
|
+
if (upstream.status === 400 && effortToApply(body) !== null) {
|
|
185
|
+
const text = await upstream.text();
|
|
186
|
+
if (!NAMES_THINKING.test(text)) {
|
|
187
|
+
refuse(res, model, 400, text);
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
log.warn({ provider: 'gemini', model }, 'gateway: Gemini refused the thinking level, so metro asked again without it');
|
|
191
|
+
call = { ...call, body: withoutEffort(body) };
|
|
192
|
+
upstream = await reachWith(call, state, stream);
|
|
193
|
+
}
|
|
194
|
+
if (!upstream.ok) {
|
|
195
|
+
refuse(res, model, upstream.status, await upstream.text());
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
const translator = new GeminiStreamTranslator(model, (name) => call.names.restore(name));
|
|
199
|
+
if (stream) {
|
|
200
|
+
await relayTranslated(upstream, res, watch, conn.id, translator, (raw) => {
|
|
201
|
+
const data = parseData(raw.data);
|
|
202
|
+
return data === null ? '' : translator.push(data);
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const data = parseData(await upstream.text());
|
|
207
|
+
answerWhole(res, (data === null ? '' : translator.push(data)) + translator.close(), conn.id);
|
|
208
|
+
}
|