@gajae-code/ai 0.1.1
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/CHANGELOG.md +2644 -0
- package/README.md +1181 -0
- package/dist/types/api-registry.d.ts +30 -0
- package/dist/types/auth-broker/client.d.ts +66 -0
- package/dist/types/auth-broker/index.d.ts +5 -0
- package/dist/types/auth-broker/refresher.d.ts +25 -0
- package/dist/types/auth-broker/remote-store.d.ts +96 -0
- package/dist/types/auth-broker/server.d.ts +32 -0
- package/dist/types/auth-broker/types.d.ts +105 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +412 -0
- package/dist/types/auth-gateway/http.d.ts +39 -0
- package/dist/types/auth-gateway/index.d.ts +3 -0
- package/dist/types/auth-gateway/server.d.ts +17 -0
- package/dist/types/auth-gateway/types.d.ts +115 -0
- package/dist/types/auth-storage.d.ts +641 -0
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/index.d.ts +49 -0
- package/dist/types/model-cache.d.ts +17 -0
- package/dist/types/model-manager.d.ts +62 -0
- package/dist/types/model-thinking.d.ts +71 -0
- package/dist/types/models.d.ts +12 -0
- package/dist/types/provider-details.d.ts +24 -0
- package/dist/types/provider-models/bundled-references.d.ts +4 -0
- package/dist/types/provider-models/descriptors.d.ts +48 -0
- package/dist/types/provider-models/google.d.ts +20 -0
- package/dist/types/provider-models/index.d.ts +5 -0
- package/dist/types/provider-models/ollama.d.ts +7 -0
- package/dist/types/provider-models/openai-compat.d.ts +237 -0
- package/dist/types/provider-models/special.d.ts +16 -0
- package/dist/types/providers/amazon-bedrock.d.ts +36 -0
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +450 -0
- package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
- package/dist/types/providers/anthropic.d.ts +188 -0
- package/dist/types/providers/aws-credentials.d.ts +43 -0
- package/dist/types/providers/aws-eventstream.d.ts +38 -0
- package/dist/types/providers/aws-sigv4.d.ts +55 -0
- package/dist/types/providers/azure-openai-responses.d.ts +15 -0
- package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
- package/dist/types/providers/cursor.d.ts +42 -0
- package/dist/types/providers/error-message.d.ts +27 -0
- package/dist/types/providers/github-copilot-headers.d.ts +40 -0
- package/dist/types/providers/gitlab-duo.d.ts +27 -0
- package/dist/types/providers/google-auth.d.ts +24 -0
- package/dist/types/providers/google-gemini-cli.d.ts +72 -0
- package/dist/types/providers/google-gemini-headers.d.ts +18 -0
- package/dist/types/providers/google-shared.d.ts +163 -0
- package/dist/types/providers/google-types.d.ts +138 -0
- package/dist/types/providers/google-vertex.d.ts +7 -0
- package/dist/types/providers/google.d.ts +4 -0
- package/dist/types/providers/grammar.d.ts +1 -0
- package/dist/types/providers/kimi.d.ts +27 -0
- package/dist/types/providers/mock.d.ts +175 -0
- package/dist/types/providers/ollama.d.ts +6 -0
- package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
- package/dist/types/providers/openai-chat-server-schema.d.ts +814 -0
- package/dist/types/providers/openai-chat-server.d.ts +16 -0
- package/dist/types/providers/openai-codex/constants.d.ts +26 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
- package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
- package/dist/types/providers/openai-codex-responses.d.ts +67 -0
- package/dist/types/providers/openai-completions-compat.d.ts +25 -0
- package/dist/types/providers/openai-completions.d.ts +33 -0
- package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
- package/dist/types/providers/openai-responses-server.d.ts +17 -0
- package/dist/types/providers/openai-responses-shared.d.ts +89 -0
- package/dist/types/providers/openai-responses.d.ts +32 -0
- package/dist/types/providers/pi-native-client.d.ts +13 -0
- package/dist/types/providers/pi-native-server.d.ts +68 -0
- package/dist/types/providers/register-builtins.d.ts +31 -0
- package/dist/types/providers/synthetic.d.ts +26 -0
- package/dist/types/providers/transform-messages.d.ts +12 -0
- package/dist/types/providers/vision-guard.d.ts +8 -0
- package/dist/types/rate-limit-utils.d.ts +19 -0
- package/dist/types/stream.d.ts +24 -0
- package/dist/types/types.d.ts +746 -0
- package/dist/types/usage/claude.d.ts +3 -0
- package/dist/types/usage/gemini.d.ts +2 -0
- package/dist/types/usage/github-copilot.d.ts +7 -0
- package/dist/types/usage/google-antigravity.d.ts +2 -0
- package/dist/types/usage/kimi.d.ts +2 -0
- package/dist/types/usage/minimax-code.d.ts +2 -0
- package/dist/types/usage/openai-codex.d.ts +3 -0
- package/dist/types/usage/shared.d.ts +1 -0
- package/dist/types/usage/zai.d.ts +2 -0
- package/dist/types/usage.d.ts +258 -0
- package/dist/types/utils/abort.d.ts +19 -0
- package/dist/types/utils/anthropic-auth.d.ts +31 -0
- package/dist/types/utils/discovery/antigravity.d.ts +61 -0
- package/dist/types/utils/discovery/codex.d.ts +38 -0
- package/dist/types/utils/discovery/cursor.d.ts +23 -0
- package/dist/types/utils/discovery/gemini.d.ts +25 -0
- package/dist/types/utils/discovery/index.d.ts +4 -0
- package/dist/types/utils/discovery/openai-compatible.d.ts +72 -0
- package/dist/types/utils/event-stream.d.ts +28 -0
- package/dist/types/utils/fireworks-model-id.d.ts +10 -0
- package/dist/types/utils/foundry.d.ts +1 -0
- package/dist/types/utils/h2-fetch.d.ts +22 -0
- package/dist/types/utils/http-inspector.d.ts +31 -0
- package/dist/types/utils/idle-iterator.d.ts +67 -0
- package/dist/types/utils/json-parse.d.ts +10 -0
- package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
- package/dist/types/utils/oauth/anthropic.d.ts +22 -0
- package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
- package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
- package/dist/types/utils/oauth/callback-server.d.ts +57 -0
- package/dist/types/utils/oauth/cerebras.d.ts +1 -0
- package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/cursor.d.ts +15 -0
- package/dist/types/utils/oauth/deepseek.d.ts +10 -0
- package/dist/types/utils/oauth/firepass.d.ts +1 -0
- package/dist/types/utils/oauth/fireworks.d.ts +1 -0
- package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
- package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
- package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
- package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
- package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
- package/dist/types/utils/oauth/huggingface.d.ts +19 -0
- package/dist/types/utils/oauth/index.d.ts +38 -0
- package/dist/types/utils/oauth/kagi.d.ts +17 -0
- package/dist/types/utils/oauth/kilo.d.ts +5 -0
- package/dist/types/utils/oauth/kimi.d.ts +21 -0
- package/dist/types/utils/oauth/litellm.d.ts +18 -0
- package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
- package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
- package/dist/types/utils/oauth/moonshot.d.ts +1 -0
- package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
- package/dist/types/utils/oauth/nvidia.d.ts +18 -0
- package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
- package/dist/types/utils/oauth/ollama.d.ts +18 -0
- package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
- package/dist/types/utils/oauth/opencode.d.ts +18 -0
- package/dist/types/utils/oauth/parallel.d.ts +17 -0
- package/dist/types/utils/oauth/perplexity.d.ts +9 -0
- package/dist/types/utils/oauth/pkce.d.ts +8 -0
- package/dist/types/utils/oauth/qianfan.d.ts +17 -0
- package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
- package/dist/types/utils/oauth/synthetic.d.ts +1 -0
- package/dist/types/utils/oauth/tavily.d.ts +17 -0
- package/dist/types/utils/oauth/together.d.ts +1 -0
- package/dist/types/utils/oauth/types.d.ts +44 -0
- package/dist/types/utils/oauth/venice.d.ts +18 -0
- package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/vllm.d.ts +16 -0
- package/dist/types/utils/oauth/xiaomi.d.ts +19 -0
- package/dist/types/utils/oauth/zai.d.ts +18 -0
- package/dist/types/utils/oauth/zenmux.d.ts +1 -0
- package/dist/types/utils/overflow.d.ts +54 -0
- package/dist/types/utils/parse-bind.d.ts +23 -0
- package/dist/types/utils/provider-response.d.ts +3 -0
- package/dist/types/utils/retry-after.d.ts +3 -0
- package/dist/types/utils/retry.d.ts +26 -0
- package/dist/types/utils/schema/adapt.d.ts +24 -0
- package/dist/types/utils/schema/compatibility.d.ts +30 -0
- package/dist/types/utils/schema/dereference.d.ts +11 -0
- package/dist/types/utils/schema/draft.d.ts +10 -0
- package/dist/types/utils/schema/equality.d.ts +4 -0
- package/dist/types/utils/schema/fields.d.ts +49 -0
- package/dist/types/utils/schema/index.d.ts +13 -0
- package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
- package/dist/types/utils/schema/meta-validator.d.ts +2 -0
- package/dist/types/utils/schema/normalize.d.ts +93 -0
- package/dist/types/utils/schema/spill.d.ts +8 -0
- package/dist/types/utils/schema/stamps.d.ts +25 -0
- package/dist/types/utils/schema/types.d.ts +4 -0
- package/dist/types/utils/schema/wire.d.ts +54 -0
- package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
- package/dist/types/utils/sse-debug.d.ts +10 -0
- package/dist/types/utils/tool-call-healing.d.ts +71 -0
- package/dist/types/utils/tool-choice.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +17 -0
- package/dist/types/utils.d.ts +28 -0
- package/package.json +146 -0
- package/src/api-registry.ts +96 -0
- package/src/auth-broker/client.ts +358 -0
- package/src/auth-broker/index.ts +5 -0
- package/src/auth-broker/refresher.ts +127 -0
- package/src/auth-broker/remote-store.ts +623 -0
- package/src/auth-broker/server.ts +644 -0
- package/src/auth-broker/types.ts +127 -0
- package/src/auth-broker/wire-schemas.ts +200 -0
- package/src/auth-gateway/http.ts +194 -0
- package/src/auth-gateway/index.ts +3 -0
- package/src/auth-gateway/server.ts +717 -0
- package/src/auth-gateway/types.ts +134 -0
- package/src/auth-storage.ts +4104 -0
- package/src/cli.ts +262 -0
- package/src/index.ts +54 -0
- package/src/model-cache.ts +129 -0
- package/src/model-manager.ts +450 -0
- package/src/model-thinking.ts +691 -0
- package/src/models.json +73853 -0
- package/src/models.json.d.ts +9 -0
- package/src/models.ts +56 -0
- package/src/prompts/turn-aborted-guidance.md +4 -0
- package/src/provider-details.ts +90 -0
- package/src/provider-models/bundled-references.ts +38 -0
- package/src/provider-models/descriptors.ts +308 -0
- package/src/provider-models/google.ts +91 -0
- package/src/provider-models/index.ts +5 -0
- package/src/provider-models/ollama.ts +153 -0
- package/src/provider-models/openai-compat.ts +2275 -0
- package/src/provider-models/special.ts +67 -0
- package/src/providers/amazon-bedrock.ts +849 -0
- package/src/providers/anthropic-messages-server-schema.ts +229 -0
- package/src/providers/anthropic-messages-server.ts +677 -0
- package/src/providers/anthropic.ts +2696 -0
- package/src/providers/aws-credentials.ts +501 -0
- package/src/providers/aws-eventstream.ts +185 -0
- package/src/providers/aws-sigv4.ts +218 -0
- package/src/providers/azure-openai-responses.ts +337 -0
- package/src/providers/cursor/gen/agent_pb.ts +15274 -0
- package/src/providers/cursor/proto/agent.proto +3526 -0
- package/src/providers/cursor/proto/buf.gen.yaml +6 -0
- package/src/providers/cursor/proto/buf.yaml +17 -0
- package/src/providers/cursor.ts +2561 -0
- package/src/providers/error-message.ts +21 -0
- package/src/providers/github-copilot-headers.ts +140 -0
- package/src/providers/gitlab-duo.ts +372 -0
- package/src/providers/google-auth.ts +252 -0
- package/src/providers/google-gemini-cli.ts +795 -0
- package/src/providers/google-gemini-headers.ts +41 -0
- package/src/providers/google-shared.ts +902 -0
- package/src/providers/google-types.ts +167 -0
- package/src/providers/google-vertex.ts +88 -0
- package/src/providers/google.ts +41 -0
- package/src/providers/grammar.ts +70 -0
- package/src/providers/kimi.ts +52 -0
- package/src/providers/mock.ts +500 -0
- package/src/providers/ollama.ts +544 -0
- package/src/providers/openai-anthropic-shim.ts +138 -0
- package/src/providers/openai-chat-server-schema.ts +243 -0
- package/src/providers/openai-chat-server.ts +628 -0
- package/src/providers/openai-codex/constants.ts +43 -0
- package/src/providers/openai-codex/request-transformer.ts +161 -0
- package/src/providers/openai-codex/response-handler.ts +81 -0
- package/src/providers/openai-codex-responses.ts +2598 -0
- package/src/providers/openai-completions-compat.ts +279 -0
- package/src/providers/openai-completions.ts +1853 -0
- package/src/providers/openai-responses-server-schema.ts +290 -0
- package/src/providers/openai-responses-server.ts +1183 -0
- package/src/providers/openai-responses-shared.ts +800 -0
- package/src/providers/openai-responses.ts +621 -0
- package/src/providers/pi-native-client.ts +228 -0
- package/src/providers/pi-native-server.ts +210 -0
- package/src/providers/register-builtins.ts +412 -0
- package/src/providers/synthetic.ts +50 -0
- package/src/providers/transform-messages.ts +309 -0
- package/src/providers/vision-guard.ts +31 -0
- package/src/rate-limit-utils.ts +84 -0
- package/src/stream.ts +895 -0
- package/src/types.ts +884 -0
- package/src/usage/claude.ts +431 -0
- package/src/usage/gemini.ts +250 -0
- package/src/usage/github-copilot.ts +421 -0
- package/src/usage/google-antigravity.ts +201 -0
- package/src/usage/kimi.ts +271 -0
- package/src/usage/minimax-code.ts +31 -0
- package/src/usage/openai-codex.ts +503 -0
- package/src/usage/shared.ts +10 -0
- package/src/usage/zai.ts +247 -0
- package/src/usage.ts +183 -0
- package/src/utils/abort.ts +51 -0
- package/src/utils/anthropic-auth.ts +87 -0
- package/src/utils/discovery/antigravity.ts +261 -0
- package/src/utils/discovery/codex.ts +371 -0
- package/src/utils/discovery/cursor.ts +306 -0
- package/src/utils/discovery/gemini.ts +248 -0
- package/src/utils/discovery/index.ts +4 -0
- package/src/utils/discovery/openai-compatible.ts +224 -0
- package/src/utils/event-stream.ts +142 -0
- package/src/utils/fireworks-model-id.ts +30 -0
- package/src/utils/foundry.ts +8 -0
- package/src/utils/h2-fetch.ts +60 -0
- package/src/utils/http-inspector.ts +176 -0
- package/src/utils/idle-iterator.ts +250 -0
- package/src/utils/json-parse.ts +148 -0
- package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
- package/src/utils/oauth/anthropic.ts +200 -0
- package/src/utils/oauth/api-key-login.ts +87 -0
- package/src/utils/oauth/api-key-validation.ts +92 -0
- package/src/utils/oauth/callback-server.ts +276 -0
- package/src/utils/oauth/cerebras.ts +16 -0
- package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
- package/src/utils/oauth/cursor.ts +157 -0
- package/src/utils/oauth/deepseek.ts +53 -0
- package/src/utils/oauth/firepass.ts +24 -0
- package/src/utils/oauth/fireworks.ts +15 -0
- package/src/utils/oauth/github-copilot.ts +362 -0
- package/src/utils/oauth/gitlab-duo.ts +123 -0
- package/src/utils/oauth/google-antigravity.ts +200 -0
- package/src/utils/oauth/google-gemini-cli.ts +256 -0
- package/src/utils/oauth/google-oauth-shared.ts +110 -0
- package/src/utils/oauth/huggingface.ts +62 -0
- package/src/utils/oauth/index.ts +444 -0
- package/src/utils/oauth/kagi.ts +47 -0
- package/src/utils/oauth/kilo.ts +87 -0
- package/src/utils/oauth/kimi.ts +254 -0
- package/src/utils/oauth/litellm.ts +47 -0
- package/src/utils/oauth/lm-studio.ts +38 -0
- package/src/utils/oauth/minimax-code.ts +78 -0
- package/src/utils/oauth/moonshot.ts +16 -0
- package/src/utils/oauth/nanogpt.ts +15 -0
- package/src/utils/oauth/nvidia.ts +70 -0
- package/src/utils/oauth/oauth.html +199 -0
- package/src/utils/oauth/ollama-cloud.ts +28 -0
- package/src/utils/oauth/ollama.ts +47 -0
- package/src/utils/oauth/openai-codex.ts +299 -0
- package/src/utils/oauth/opencode.ts +49 -0
- package/src/utils/oauth/parallel.ts +46 -0
- package/src/utils/oauth/perplexity.ts +206 -0
- package/src/utils/oauth/pkce.ts +18 -0
- package/src/utils/oauth/qianfan.ts +58 -0
- package/src/utils/oauth/qwen-portal.ts +60 -0
- package/src/utils/oauth/synthetic.ts +16 -0
- package/src/utils/oauth/tavily.ts +46 -0
- package/src/utils/oauth/together.ts +16 -0
- package/src/utils/oauth/types.ts +94 -0
- package/src/utils/oauth/venice.ts +59 -0
- package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
- package/src/utils/oauth/vllm.ts +40 -0
- package/src/utils/oauth/xiaomi.ts +137 -0
- package/src/utils/oauth/zai.ts +60 -0
- package/src/utils/oauth/zenmux.ts +15 -0
- package/src/utils/overflow.ts +137 -0
- package/src/utils/parse-bind.ts +54 -0
- package/src/utils/provider-response.ts +30 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +54 -0
- package/src/utils/schema/CONSTRAINTS.md +164 -0
- package/src/utils/schema/adapt.ts +36 -0
- package/src/utils/schema/compatibility.ts +435 -0
- package/src/utils/schema/dereference.ts +98 -0
- package/src/utils/schema/draft.ts +341 -0
- package/src/utils/schema/equality.ts +97 -0
- package/src/utils/schema/fields.ts +190 -0
- package/src/utils/schema/index.ts +13 -0
- package/src/utils/schema/json-schema-validator.ts +577 -0
- package/src/utils/schema/meta-validator.ts +167 -0
- package/src/utils/schema/normalize.ts +1588 -0
- package/src/utils/schema/spill.ts +43 -0
- package/src/utils/schema/stamps.ts +97 -0
- package/src/utils/schema/types.ts +11 -0
- package/src/utils/schema/wire.ts +213 -0
- package/src/utils/schema/zod-decontaminate.ts +331 -0
- package/src/utils/sse-debug.ts +289 -0
- package/src/utils/tool-call-healing.ts +271 -0
- package/src/utils/tool-choice.ts +99 -0
- package/src/utils/validation.ts +1019 -0
- package/src/utils.ts +166 -0
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom API provider registry.
|
|
3
|
+
*
|
|
4
|
+
* Allows extensions to register streaming functions for custom API types
|
|
5
|
+
* (e.g., "vertex-Anthropic model-api") that are not built into stream.ts.
|
|
6
|
+
*/
|
|
7
|
+
import type { Api, AssistantMessageEventStream, Context, Model, SimpleStreamOptions, StreamOptions } from "./types";
|
|
8
|
+
export type CustomStreamFn = (model: Model<Api>, context: Context, options?: StreamOptions) => AssistantMessageEventStream;
|
|
9
|
+
export type CustomStreamSimpleFn = (model: Model<Api>, context: Context, options?: SimpleStreamOptions) => AssistantMessageEventStream;
|
|
10
|
+
export interface RegisteredCustomApi {
|
|
11
|
+
stream: CustomStreamFn;
|
|
12
|
+
streamSimple: CustomStreamSimpleFn;
|
|
13
|
+
sourceId?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Register a custom API streaming function.
|
|
17
|
+
*/
|
|
18
|
+
export declare function registerCustomApi(api: string, streamSimple: CustomStreamSimpleFn, sourceId?: string, stream?: CustomStreamFn): void;
|
|
19
|
+
/**
|
|
20
|
+
* Get a custom API provider by API identifier.
|
|
21
|
+
*/
|
|
22
|
+
export declare function getCustomApi(api: string): RegisteredCustomApi | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Remove all custom APIs registered by a specific source (e.g., extension path).
|
|
25
|
+
*/
|
|
26
|
+
export declare function unregisterCustomApis(sourceId: string): void;
|
|
27
|
+
/**
|
|
28
|
+
* Clear all custom API registrations.
|
|
29
|
+
*/
|
|
30
|
+
export declare function clearCustomApis(): void;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { AuthCredential } from "../auth-storage";
|
|
2
|
+
import type { CredentialDisableResponse, CredentialRefreshResponse, CredentialUploadResponse, HealthzResponse, SnapshotResponse, SnapshotStreamEvent, UsageResponse } from "./types";
|
|
3
|
+
export interface AuthBrokerClientOptions {
|
|
4
|
+
/** Base URL (e.g. `https://broker.tailnet:8765`). Trailing slashes are trimmed. */
|
|
5
|
+
url: string;
|
|
6
|
+
/** Bearer token used for everything except `healthz`. */
|
|
7
|
+
token: string;
|
|
8
|
+
/** Per-request timeout in milliseconds. Default 10s. */
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
/** Retry connection errors this many times. Default 1. */
|
|
11
|
+
maxRetries?: number;
|
|
12
|
+
/** Override fetch (used in tests). Default global `fetch`. */
|
|
13
|
+
fetchImpl?: typeof fetch;
|
|
14
|
+
}
|
|
15
|
+
export declare class AuthBrokerError extends Error {
|
|
16
|
+
readonly status: number | undefined;
|
|
17
|
+
readonly body: string | undefined;
|
|
18
|
+
constructor(message: string, opts?: {
|
|
19
|
+
status?: number;
|
|
20
|
+
body?: string;
|
|
21
|
+
cause?: unknown;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Thrown when a broker responds 404 to `GET /v1/snapshot/stream` — old
|
|
26
|
+
* brokers that predate the SSE endpoint. Callers (`RemoteAuthCredentialStore`)
|
|
27
|
+
* detect this sentinel to fall back to long-polling permanently.
|
|
28
|
+
*/
|
|
29
|
+
export declare class AuthBrokerStreamUnsupportedError extends AuthBrokerError {
|
|
30
|
+
constructor(message?: string);
|
|
31
|
+
}
|
|
32
|
+
export interface FetchSnapshotOptions {
|
|
33
|
+
ifGenerationGt?: number;
|
|
34
|
+
waitMs?: number;
|
|
35
|
+
signal?: AbortSignal;
|
|
36
|
+
}
|
|
37
|
+
export type FetchSnapshotResult = {
|
|
38
|
+
status: 200;
|
|
39
|
+
snapshot: SnapshotResponse;
|
|
40
|
+
generation: number;
|
|
41
|
+
} | {
|
|
42
|
+
status: 304;
|
|
43
|
+
generation: number;
|
|
44
|
+
};
|
|
45
|
+
export declare class AuthBrokerClient {
|
|
46
|
+
#private;
|
|
47
|
+
constructor(opts: AuthBrokerClientOptions);
|
|
48
|
+
healthz(signal?: AbortSignal): Promise<HealthzResponse>;
|
|
49
|
+
fetchSnapshot(opts?: FetchSnapshotOptions): Promise<FetchSnapshotResult>;
|
|
50
|
+
/**
|
|
51
|
+
* Subscribe to the broker's SSE snapshot stream. The first frame is always
|
|
52
|
+
* a full `snapshot`; subsequent frames are `entry` upserts / refreshes or
|
|
53
|
+
* `removed` deletes. Caller controls lifecycle via `opts.signal`.
|
|
54
|
+
*
|
|
55
|
+
* Throws {@link AuthBrokerStreamUnsupportedError} when the broker responds
|
|
56
|
+
* 404 — older brokers predate this endpoint and the caller should fall back
|
|
57
|
+
* to long-polling for the remainder of its lifetime.
|
|
58
|
+
*/
|
|
59
|
+
openSnapshotStream(opts?: {
|
|
60
|
+
signal?: AbortSignal;
|
|
61
|
+
}): AsyncGenerator<SnapshotStreamEvent>;
|
|
62
|
+
fetchUsage(signal?: AbortSignal): Promise<UsageResponse>;
|
|
63
|
+
refreshCredential(id: number, signal?: AbortSignal): Promise<CredentialRefreshResponse>;
|
|
64
|
+
disableCredential(id: number, cause: string, signal?: AbortSignal): Promise<CredentialDisableResponse>;
|
|
65
|
+
uploadCredential(provider: string, credential: AuthCredential, signal?: AbortSignal): Promise<CredentialUploadResponse>;
|
|
66
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { AuthStorage } from "../auth-storage";
|
|
2
|
+
export interface AuthBrokerRefresherOptions {
|
|
3
|
+
storage: AuthStorage;
|
|
4
|
+
/** Refresh credentials expiring within this window. Default 5 min. */
|
|
5
|
+
refreshSkewMs?: number;
|
|
6
|
+
/** Loop cadence. Default 60s. */
|
|
7
|
+
refreshIntervalMs?: number;
|
|
8
|
+
/** Override clock (tests). */
|
|
9
|
+
now?: () => number;
|
|
10
|
+
}
|
|
11
|
+
export interface AuthBrokerRefresherSchedule {
|
|
12
|
+
enabled: boolean;
|
|
13
|
+
intervalMs: number;
|
|
14
|
+
skewMs: number;
|
|
15
|
+
nextSweepAt: number;
|
|
16
|
+
}
|
|
17
|
+
export declare class AuthBrokerRefresher {
|
|
18
|
+
#private;
|
|
19
|
+
constructor(opts: AuthBrokerRefresherOptions);
|
|
20
|
+
start(): void;
|
|
21
|
+
stop(): void;
|
|
22
|
+
getSchedule(): AuthBrokerRefresherSchedule;
|
|
23
|
+
/** Run one sweep. Exposed for tests. */
|
|
24
|
+
tick(): Promise<void>;
|
|
25
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type AuthCredential, type AuthCredentialStore, type OAuthCredential, type StoredAuthCredential } from "../auth-storage";
|
|
2
|
+
import type { Provider } from "../types";
|
|
3
|
+
import type { UsageReport } from "../usage";
|
|
4
|
+
import type { OAuthCredentials } from "../utils/oauth/types";
|
|
5
|
+
import { type AuthBrokerClient } from "./client";
|
|
6
|
+
import type { SnapshotResponse } from "./types";
|
|
7
|
+
export interface RemoteAuthCredentialStoreOptions {
|
|
8
|
+
client: AuthBrokerClient;
|
|
9
|
+
/**
|
|
10
|
+
* Initial snapshot. When omitted, callers must call
|
|
11
|
+
* {@link RemoteAuthCredentialStore.refreshSnapshot} before the first read.
|
|
12
|
+
*/
|
|
13
|
+
initialSnapshot?: SnapshotResponse;
|
|
14
|
+
/**
|
|
15
|
+
* Subscribe to the broker's SSE snapshot stream when available. Falls back
|
|
16
|
+
* to long-poll permanently when the broker returns 404. Default `true`.
|
|
17
|
+
*/
|
|
18
|
+
streamSnapshots?: boolean;
|
|
19
|
+
}
|
|
20
|
+
export declare class RemoteAuthCredentialStore implements AuthCredentialStore {
|
|
21
|
+
#private;
|
|
22
|
+
constructor(opts: RemoteAuthCredentialStoreOptions);
|
|
23
|
+
get client(): AuthBrokerClient;
|
|
24
|
+
get snapshot(): SnapshotResponse;
|
|
25
|
+
/** Re-hydrate the in-memory snapshot from the broker. */
|
|
26
|
+
refreshSnapshot(): Promise<SnapshotResponse>;
|
|
27
|
+
listAuthCredentials(provider?: string): StoredAuthCredential[];
|
|
28
|
+
/**
|
|
29
|
+
* In-memory update from a successful refresh through the broker. AuthStorage
|
|
30
|
+
* calls this after `#replaceCredentialAt`; the broker already persisted the
|
|
31
|
+
* authoritative row, so we just mirror it.
|
|
32
|
+
*/
|
|
33
|
+
updateAuthCredential(id: number, credential: AuthCredential): void;
|
|
34
|
+
deleteAuthCredential(id: number, disabledCause: string): void;
|
|
35
|
+
tryDisableAuthCredentialIfMatches(id: number, _expectedData: string, disabledCause: string): boolean;
|
|
36
|
+
waitForFreshSnapshot(maxWaitMs: number, opts?: {
|
|
37
|
+
signal?: AbortSignal;
|
|
38
|
+
}): Promise<boolean>;
|
|
39
|
+
prepareForRequest(credentialId: number, opts?: {
|
|
40
|
+
signal?: AbortSignal;
|
|
41
|
+
}): Promise<boolean>;
|
|
42
|
+
markCredentialSuspect(credentialId: number, opts?: {
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
}): Promise<void>;
|
|
45
|
+
replaceAuthCredentialsForProvider(_provider: string, _credentials: AuthCredential[]): StoredAuthCredential[];
|
|
46
|
+
upsertAuthCredentialForProvider(_provider: string, _credential: AuthCredential): StoredAuthCredential[];
|
|
47
|
+
deleteAuthCredentialsForProvider(_provider: string, _disabledCause: string): void;
|
|
48
|
+
/**
|
|
49
|
+
* Upsert a single credential through the broker. The broker server is the
|
|
50
|
+
* canonical writer — see `POST /v1/credential`. The redacted snapshot
|
|
51
|
+
* entries returned by the server replace the provider's rows in our local
|
|
52
|
+
* snapshot, and the global snapshot is then refreshed in the background so
|
|
53
|
+
* any concurrent peer (refresh, generation bump) stays in sync.
|
|
54
|
+
*/
|
|
55
|
+
upsertAuthCredentialRemote(provider: string, credential: AuthCredential): Promise<StoredAuthCredential[]>;
|
|
56
|
+
/**
|
|
57
|
+
* Replace-all semantics: disable every active credential for the provider,
|
|
58
|
+
* then upload each of the new credentials. Used by API-key login so a new
|
|
59
|
+
* key clobbers any previously stored key for the same provider.
|
|
60
|
+
*/
|
|
61
|
+
replaceAuthCredentialsRemote(provider: string, credentials: AuthCredential[]): Promise<StoredAuthCredential[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Logout: disable every active credential for the provider on the broker,
|
|
64
|
+
* then drop them from the local snapshot. Refresh fetches the authoritative
|
|
65
|
+
* post-state in the background.
|
|
66
|
+
*/
|
|
67
|
+
deleteAuthCredentialsRemote(provider: string, disabledCause: string): Promise<void>;
|
|
68
|
+
getCache(key: string): string | null;
|
|
69
|
+
setCache(key: string, value: string, expiresAtSec: number): void;
|
|
70
|
+
cleanExpiredCache(): void;
|
|
71
|
+
/**
|
|
72
|
+
* Store-level hook consumed by `AuthStorage` — routes refresh through the
|
|
73
|
+
* broker so the actual refresh token never leaves the broker host. Returns
|
|
74
|
+
* the broker-redacted credential with {@link REMOTE_REFRESH_SENTINEL} in
|
|
75
|
+
* the `refresh` slot.
|
|
76
|
+
*/
|
|
77
|
+
refreshOAuthCredential(_provider: Provider, credentialId: number, _credential: OAuthCredential, signal?: AbortSignal): Promise<OAuthCredentials>;
|
|
78
|
+
/**
|
|
79
|
+
* Store-level hook consumed by `AuthStorage.fetchUsageReports()` — proxies
|
|
80
|
+
* to the broker's `/v1/usage` endpoint. The broker's egress IP isn't
|
|
81
|
+
* rate-limited by Anthropic's per-IP `/usage` cap the way a heavy
|
|
82
|
+
* residential laptop is, so all credentials surface every cycle.
|
|
83
|
+
*/
|
|
84
|
+
fetchUsageReports(signal?: AbortSignal): Promise<UsageReport[] | null>;
|
|
85
|
+
/**
|
|
86
|
+
* Per-credential usage hook consumed by `AuthStorage.#getUsageReport`. Pulls
|
|
87
|
+
* the aggregate broker `/v1/usage` once and serves all callers from the
|
|
88
|
+
* same response (coalesced + cached), then matches the credential to a
|
|
89
|
+
* report by provider + identity (accountId / email / projectId).
|
|
90
|
+
*
|
|
91
|
+
* The broker already aggregates with its own 30s TTL on the server side; our
|
|
92
|
+
* 15s client TTL is below that so we usually re-use the broker's cache too.
|
|
93
|
+
*/
|
|
94
|
+
getUsageReport(provider: Provider, credential: OAuthCredential, signal?: AbortSignal): Promise<UsageReport | null>;
|
|
95
|
+
close(): void;
|
|
96
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { AuthStorage } from "../auth-storage";
|
|
2
|
+
export interface AuthBrokerServerOptions {
|
|
3
|
+
/** Underlying credential storage (wraps the local SQLite store on the broker). */
|
|
4
|
+
storage: AuthStorage;
|
|
5
|
+
/** Listen address; accepts `host:port` or just `port`. */
|
|
6
|
+
bind?: string;
|
|
7
|
+
/** Accept any of these bearer tokens. Empty disables auth (loopback only). */
|
|
8
|
+
bearerTokens: string[];
|
|
9
|
+
/** Broker version string surfaced on `/v1/healthz`. */
|
|
10
|
+
version?: string;
|
|
11
|
+
/** Refresh credentials expiring within this window. Default 5 min. */
|
|
12
|
+
refreshSkewMs?: number;
|
|
13
|
+
/** Background refresh cadence. Default 60s. */
|
|
14
|
+
refreshIntervalMs?: number;
|
|
15
|
+
/** Disable the background refresher (e.g. for tests). */
|
|
16
|
+
disableRefresher?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Override SSE keepalive cadence in milliseconds for `/v1/snapshot/stream`.
|
|
19
|
+
* Internal-only — tests use a short interval so they can assert heartbeats
|
|
20
|
+
* without long sleeps. Default {@link DEFAULT_STREAM_KEEPALIVE_MS}.
|
|
21
|
+
*/
|
|
22
|
+
streamKeepaliveMs?: number;
|
|
23
|
+
}
|
|
24
|
+
export interface AuthBrokerServerHandle {
|
|
25
|
+
/** Bound URL (`http://host:port`). */
|
|
26
|
+
url: string;
|
|
27
|
+
port: number;
|
|
28
|
+
hostname: string;
|
|
29
|
+
close(): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
/** Boot the broker. Caller owns lifecycle; `handle.close()` to stop. */
|
|
32
|
+
export declare function startAuthBroker(opts: AuthBrokerServerOptions): AuthBrokerServerHandle;
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wire types shared between the auth-broker server and clients.
|
|
3
|
+
*
|
|
4
|
+
* The broker holds OAuth refresh tokens and exposes a redacted snapshot;
|
|
5
|
+
* clients use `access` tokens directly and call back to the broker when a
|
|
6
|
+
* credential expires or a 401 surfaces on a supposedly-fresh credential.
|
|
7
|
+
*/
|
|
8
|
+
import type { AuthCredential, AuthCredentialSnapshot, AuthCredentialSnapshotEntry } from "../auth-storage";
|
|
9
|
+
import type { UsageReport } from "../usage";
|
|
10
|
+
/** GET /v1/healthz response body. */
|
|
11
|
+
export interface HealthzResponse {
|
|
12
|
+
ok: boolean;
|
|
13
|
+
version?: string;
|
|
14
|
+
}
|
|
15
|
+
export interface RefresherSchedule {
|
|
16
|
+
enabled: boolean;
|
|
17
|
+
intervalMs: number;
|
|
18
|
+
skewMs: number;
|
|
19
|
+
nextSweepInMs: number;
|
|
20
|
+
}
|
|
21
|
+
export type SnapshotEntry = AuthCredentialSnapshotEntry & {
|
|
22
|
+
rotatesInMs: number | null;
|
|
23
|
+
};
|
|
24
|
+
/** GET /v1/snapshot response body. */
|
|
25
|
+
export interface SnapshotResponse extends Omit<AuthCredentialSnapshot, "credentials"> {
|
|
26
|
+
serverNowMs: number;
|
|
27
|
+
refresher: RefresherSchedule;
|
|
28
|
+
credentials: SnapshotEntry[];
|
|
29
|
+
}
|
|
30
|
+
/** GET /v1/usage response body — matches the local `AuthStorage.fetchUsageReports` shape. */
|
|
31
|
+
export interface UsageResponse {
|
|
32
|
+
generatedAt: number;
|
|
33
|
+
reports: UsageReport[];
|
|
34
|
+
}
|
|
35
|
+
/** POST /v1/credential/:id/refresh response body. */
|
|
36
|
+
export interface CredentialRefreshResponse {
|
|
37
|
+
entry: AuthCredentialSnapshotEntry;
|
|
38
|
+
}
|
|
39
|
+
/** POST /v1/credential/:id/disable request body. */
|
|
40
|
+
export interface CredentialDisableRequest {
|
|
41
|
+
cause: string;
|
|
42
|
+
}
|
|
43
|
+
/** POST /v1/credential/:id/disable response body. */
|
|
44
|
+
export interface CredentialDisableResponse {
|
|
45
|
+
ok: boolean;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* POST /v1/credential request body. The OAuth `refresh` must be the *real*
|
|
49
|
+
* refresh token (not the sentinel) — the broker is the canonical writer.
|
|
50
|
+
*/
|
|
51
|
+
export interface CredentialUploadRequest {
|
|
52
|
+
provider: string;
|
|
53
|
+
credential: AuthCredential;
|
|
54
|
+
}
|
|
55
|
+
/** POST /v1/credential response body — redacted snapshot of the provider's rows after upsert. */
|
|
56
|
+
export interface CredentialUploadResponse {
|
|
57
|
+
entries: AuthCredentialSnapshotEntry[];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* SSE event kinds emitted on `GET /v1/snapshot/stream`. The same value is set
|
|
61
|
+
* as the SSE `event:` name (load-bearing for clients) **and** embedded as a
|
|
62
|
+
* `kind` field inside the JSON body so a Zod discriminated union can validate
|
|
63
|
+
* the payload without consulting the line metadata.
|
|
64
|
+
*/
|
|
65
|
+
export type SnapshotStreamEventKind = "snapshot" | "entry" | "removed";
|
|
66
|
+
/** Initial frame emitted on connect — the full {@link SnapshotResponse}. */
|
|
67
|
+
export interface SnapshotStreamSnapshotEvent extends SnapshotResponse {
|
|
68
|
+
kind: "snapshot";
|
|
69
|
+
}
|
|
70
|
+
/** Single credential added/changed (upsert or refresh). */
|
|
71
|
+
export interface SnapshotStreamEntryEvent {
|
|
72
|
+
kind: "entry";
|
|
73
|
+
generation: number;
|
|
74
|
+
serverNowMs: number;
|
|
75
|
+
refresher: RefresherSchedule;
|
|
76
|
+
entry: SnapshotEntry;
|
|
77
|
+
}
|
|
78
|
+
/** Single credential disabled/deleted. */
|
|
79
|
+
export interface SnapshotStreamRemovedEvent {
|
|
80
|
+
kind: "removed";
|
|
81
|
+
generation: number;
|
|
82
|
+
serverNowMs: number;
|
|
83
|
+
refresher: RefresherSchedule;
|
|
84
|
+
id: number;
|
|
85
|
+
}
|
|
86
|
+
/** Discriminated union of every event the snapshot stream emits. */
|
|
87
|
+
export type SnapshotStreamEvent = SnapshotStreamSnapshotEvent | SnapshotStreamEntryEvent | SnapshotStreamRemovedEvent;
|
|
88
|
+
/**
|
|
89
|
+
* Default bearer-protected route prefix. The broker exposes `/v1/healthz`
|
|
90
|
+
* unauthenticated for liveness probes; everything else requires a bearer.
|
|
91
|
+
*/
|
|
92
|
+
export declare const AUTH_BROKER_API_PREFIX = "/v1";
|
|
93
|
+
/** Default port when none is configured. Loopback-only, no external exposure. */
|
|
94
|
+
export declare const DEFAULT_AUTH_BROKER_BIND = "127.0.0.1:8765";
|
|
95
|
+
/** Default broker→provider refresh skew. Refresh credentials this close to expiry. */
|
|
96
|
+
export declare const DEFAULT_REFRESH_SKEW_MS: number;
|
|
97
|
+
/** Default broker refresh-loop cadence. */
|
|
98
|
+
export declare const DEFAULT_REFRESH_INTERVAL_MS = 60000;
|
|
99
|
+
/** Keepalive cadence for `GET /v1/snapshot/stream` SSE comments. */
|
|
100
|
+
export declare const DEFAULT_STREAM_KEEPALIVE_MS = 20000;
|
|
101
|
+
/**
|
|
102
|
+
* Bun.serve `idleTimeout` (seconds) used by the broker. Default Bun idle
|
|
103
|
+
* timeout (10s) would close long-lived SSE connections between keepalives.
|
|
104
|
+
*/
|
|
105
|
+
export declare const DEFAULT_SERVER_IDLE_TIMEOUT_S = 255;
|