@sayknow-cli/ai 0.3.0 → 0.3.2
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 +36 -0
- package/dist/types/auth-gateway/http.d.ts +1 -0
- package/dist/types/model-thinking.d.ts +1 -1
- package/dist/types/provider-models/openai-compat.d.ts +5 -0
- package/dist/types/providers/google-gemini-cli.d.ts +4 -1
- package/dist/types/providers/google-gemini-headers.d.ts +27 -5
- package/dist/types/providers/mock.d.ts +2 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +1 -0
- package/dist/types/providers/openai-responses-shared.d.ts +16 -1
- package/dist/types/types.d.ts +9 -1
- package/dist/types/utils/json-parse.d.ts +8 -0
- package/dist/types/utils/oauth/fugu.d.ts +1 -0
- package/dist/types/utils/oauth/index.d.ts +1 -0
- package/dist/types/utils/oauth/types.d.ts +1 -1
- package/dist/types/utils/overflow.d.ts +11 -1
- package/package.json +2 -2
- package/src/auth-gateway/http.ts +5 -1
- package/src/auth-gateway/server.ts +18 -1
- package/src/auth-storage.ts +41 -27
- package/src/model-thinking.ts +2 -9
- package/src/models.json +67 -1
- package/src/provider-models/descriptors.ts +7 -0
- package/src/provider-models/openai-compat.ts +9 -0
- package/src/providers/google-gemini-cli.ts +64 -18
- package/src/providers/google-gemini-headers.ts +72 -18
- package/src/providers/mock.ts +3 -0
- package/src/providers/openai-codex/request-transformer.ts +53 -0
- package/src/providers/openai-codex-responses.ts +24 -11
- package/src/providers/openai-completions-compat.ts +2 -1
- package/src/providers/openai-completions.ts +12 -1
- package/src/providers/openai-responses-shared.ts +43 -1
- package/src/stream.ts +1 -0
- package/src/types.ts +9 -0
- package/src/utils/json-parse.ts +18 -0
- package/src/utils/oauth/fugu.ts +15 -0
- package/src/utils/oauth/index.ts +9 -0
- package/src/utils/oauth/types.ts +1 -0
- package/src/utils/overflow.ts +35 -1
- package/src/utils.ts +87 -0
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,42 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.7.5] - 2026-06-27
|
|
6
|
+
|
|
7
|
+
### Fixed
|
|
8
|
+
|
|
9
|
+
- Sanitized Codex history replay text fields so malformed/non-string replay text no longer corrupts the rebuilt request payload (#1199).
|
|
10
|
+
- Preserved object-valued tool replay fields when rebuilding Codex history payloads instead of coercing them to strings (#1200).
|
|
11
|
+
|
|
12
|
+
## [0.7.4] - 2026-06-27
|
|
13
|
+
|
|
14
|
+
### Fixed
|
|
15
|
+
|
|
16
|
+
- Treated `openai-codex-device` as an auth-storage alias for `openai-codex`, so headless/device Codex logins show as authenticated and logout/remove the stored Codex credential instead of appearing unsaved (#1151).
|
|
17
|
+
- Disabled thinking for OpenCode Go forced tool calls so forced-tool turns no longer emit unsupported thinking content (#1185).
|
|
18
|
+
- Restored the GPT-5.5 context window to its correct size (#1186).
|
|
19
|
+
|
|
20
|
+
## [0.7.3] - 2026-06-25
|
|
21
|
+
### Added
|
|
22
|
+
|
|
23
|
+
- Added the Sakana Fugu provider (`fugu`) with API-key auth (`FUGU_API_KEY`) and bundled catalog models (#1086).
|
|
24
|
+
|
|
25
|
+
### Fixed
|
|
26
|
+
|
|
27
|
+
- Wired Fugu API-key auth login so `skc login fugu` stores a reusable `FUGU_API_KEY` credential instead of the provider having no supported login flow (#1090).
|
|
28
|
+
- Matched the real google-antigravity IDE request headers, system prompt, and preamble config so requests are accepted by Cloud Code Assist (#1080).
|
|
29
|
+
- `isContextOverflow` now detects a third proxy-level overflow case — an empty response with `stopReason: "stop"` and anomalously low usage (input + output ≤ 5 tokens), as emitted by some proxies (notably LiteLLM) when the upstream context window is exceeded — so callers surface overflow instead of treating it as a clean completion (#1102).
|
|
30
|
+
|
|
31
|
+
### Security
|
|
32
|
+
|
|
33
|
+
- In no-auth (tokenless) auth-gateway mode, requests carrying a browser `Origin` header are now rejected before CORS preflight handling or route dispatch, while local non-browser CLI clients keep the existing tokenless flow and token-configured browser clients keep the bearer-token/preflight flow (#1115).
|
|
34
|
+
|
|
35
|
+
## [0.7.2] - 2026-06-24
|
|
36
|
+
|
|
37
|
+
### Fixed
|
|
38
|
+
|
|
39
|
+
- Reject truncated or incomplete streamed tool calls instead of executing them with partial arguments, so a cut-off tool-call payload fails fast rather than running against a mismatched schema.
|
|
40
|
+
|
|
5
41
|
## [0.7.1] - 2026-06-23
|
|
6
42
|
|
|
7
43
|
### Changed
|
|
@@ -7,6 +7,7 @@ export declare function resolvePeer(req: Request): string;
|
|
|
7
7
|
*/
|
|
8
8
|
export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
|
|
9
9
|
export declare function isAuthorized(req: Request, tokens: ReadonlySet<string>): boolean;
|
|
10
|
+
export declare function isNoAuthBrowserOriginRequest(req: Request, tokens: ReadonlySet<string>): boolean;
|
|
10
11
|
/**
|
|
11
12
|
* Extract allow-listed passthrough headers from an inbound request. Keys are
|
|
12
13
|
* lowercased; empty values are dropped. Called once per request in
|
|
@@ -41,7 +41,7 @@ export declare function applyGeneratedModelPolicies(models: ApiModel<Api>[]): vo
|
|
|
41
41
|
* model with a larger context window on the same provider:
|
|
42
42
|
* - `OpenAI code backend-spark` variants promote to `gpt-5.5`.
|
|
43
43
|
*
|
|
44
|
-
* `gpt-5.5` itself is a
|
|
44
|
+
* `gpt-5.5` itself is a 1M-context model and is not demoted to `gpt-5.4`
|
|
45
45
|
* (which has a smaller window), so it has no promotion target.
|
|
46
46
|
*/
|
|
47
47
|
export declare function linkOpenAIPromotionTargets(models: ApiModel<Api>[]): void;
|
|
@@ -75,6 +75,11 @@ export interface FirepassModelManagerConfig {
|
|
|
75
75
|
* See https://docs.fireworks.ai/firepass.
|
|
76
76
|
*/
|
|
77
77
|
export declare function firepassModelManagerOptions(_config?: FirepassModelManagerConfig): ModelManagerOptions<"openai-completions">;
|
|
78
|
+
export interface FuguModelManagerConfig {
|
|
79
|
+
apiKey?: string;
|
|
80
|
+
baseUrl?: string;
|
|
81
|
+
}
|
|
82
|
+
export declare function fuguModelManagerOptions(config?: FuguModelManagerConfig): ModelManagerOptions<"openai-completions">;
|
|
78
83
|
export interface MistralModelManagerConfig {
|
|
79
84
|
apiKey?: string;
|
|
80
85
|
baseUrl?: string;
|
|
@@ -24,7 +24,7 @@ export interface GoogleGeminiCliOptions extends StreamOptions {
|
|
|
24
24
|
};
|
|
25
25
|
projectId?: string;
|
|
26
26
|
}
|
|
27
|
-
export { ANTIGRAVITY_SYSTEM_INSTRUCTION,
|
|
27
|
+
export { ANTIGRAVITY_SYSTEM_INSTRUCTION, getAntigravityRequestHeaders, getGeminiCliHeaders, getGeminiCliUserAgent, } from "./google-gemini-headers";
|
|
28
28
|
interface ParsedGeminiCliCredentials {
|
|
29
29
|
accessToken: string;
|
|
30
30
|
projectId: string;
|
|
@@ -63,6 +63,9 @@ interface CloudCodeAssistRequest {
|
|
|
63
63
|
mode: FunctionCallingConfigMode;
|
|
64
64
|
};
|
|
65
65
|
};
|
|
66
|
+
preambleConfig?: {
|
|
67
|
+
mode: "SYSTEM_INSTRUCTION_MODE_REPLACE";
|
|
68
|
+
};
|
|
66
69
|
};
|
|
67
70
|
requestType?: string;
|
|
68
71
|
userAgent?: string;
|
|
@@ -8,11 +8,33 @@ export declare const getGeminiCliHeaders: (modelId?: string) => {
|
|
|
8
8
|
"User-Agent": string;
|
|
9
9
|
"Client-Metadata": string;
|
|
10
10
|
};
|
|
11
|
-
export declare const ANTIGRAVITY_SYSTEM_INSTRUCTION: string;
|
|
12
11
|
/**
|
|
13
|
-
* Antigravity
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
12
|
+
* Full Antigravity system instruction as observed in the real IDE binary.
|
|
13
|
+
* This is the complete prompt injected by the Antigravity language server,
|
|
14
|
+
* including BNF lexer definition for syntax highlighting, messaging system
|
|
15
|
+
* description, and reactive wakeup protocol.
|
|
16
|
+
*
|
|
17
|
+
* Wire-format fidelity note: The `%s` placeholders are literal in the real
|
|
18
|
+
* Antigravity IDE prompt. The Cloud Code Assist service either expands them
|
|
19
|
+
* server-side or the model handles them as-is. Preserved for byte-faithful
|
|
20
|
+
* emulation of the observed IDE wire format.
|
|
21
|
+
*
|
|
22
|
+
* Evidence: Disassembly of the Antigravity LS binary confirms this exact
|
|
23
|
+
* string is loaded and injected as the system instruction.
|
|
24
|
+
*/
|
|
25
|
+
export declare const ANTIGRAVITY_SYSTEM_INSTRUCTION = "You are Antigravity, a powerful agentic AI coding assistant designed by the Google Deepmind team working on Advanced Agentic Coding.\nYou are pair programming with a USER to solve their coding task. The task may require creating a new codebase, modifying or debugging an existing codebase, or simply answering a question.\nThe USER will send you requests, which you must always prioritize addressing. Along with each USER request, we will attach additional metadata about their current state, such as what files they have open and where their cursor is.\nThis information may or may not be relevant to the coding task, it is up for you to decide.<lexer>\n <config>\n <name>BNF</name>\n <alias>bnf</alias>\n <filename>*.bnf</filename>\n <mime_type>text/x-bnf</mime_type>\n </config>\n <rules>\n <state name=\"root\">\n <rule pattern=\"(<)([ -;=?-~]+)(>)\">\n <bygroups>\n <token type=\"Punctuation\"/>\n <token type=\"NameClass\"/>\n <token type=\"Punctuation\"/>\n </bygroups>\n </rule>\n <rule pattern=\"::=\">\n <token type=\"Operator\"/>\n </rule>\n <rule pattern=\"[^<>:]+\">\n <token type=\"Text\"/>\n </rule>\n <rule pattern=\".\">\n <token type=\"Text\"/>\n </rule>\n </state>\n </rules>\n</lexer>You are connected to a messaging system where you may receive messages from: %s.\n\n## Receiving Messages\n\nYou receive messages automatically at the start of each invocation. All messages are delivered in full directly into your context \u2014 no manual retrieval is needed.\n\n## Reactive Wakeup (No Polling Needed)\n\nThe system automatically resumes your execution when:\n%s\n\nThis means you do **NOT** need to poll in a loop while waiting for messages or updates. After launching anything that performs work asynchronously, you may continue other work or simply stop by calling no more tools. The system will notify you when there is something to process.\n";
|
|
26
|
+
/**
|
|
27
|
+
* Antigravity / Cloud Code Assist user agent.
|
|
28
|
+
*
|
|
29
|
+
* Disassembly-confirmed: getUserAgentName() @ 0x5ecb1dd loads "antigravity-ide"
|
|
30
|
+
* via LEA RDX, [RIP-0x284fc90] → 0x367b554 = "antigravity-ide"
|
|
31
|
+
*
|
|
32
|
+
* The LS sets HTTP headers via: fmt.Sprintf("User-Agent: %s", getUserAgentName())
|
|
33
|
+
* So the final header is: User-Agent: antigravity-ide
|
|
34
|
+
*
|
|
35
|
+
* -override_user_agent flag can override this (confirmed at 0x5ecbc37).
|
|
17
36
|
*/
|
|
18
37
|
export declare let getAntigravityUserAgent: () => string;
|
|
38
|
+
export declare const getAntigravityRequestHeaders: () => {
|
|
39
|
+
"User-Agent": string;
|
|
40
|
+
};
|
|
@@ -60,6 +60,8 @@ export type MockContent = string | {
|
|
|
60
60
|
name: string;
|
|
61
61
|
/** Object form is preferred; strings are passed through verbatim. */
|
|
62
62
|
arguments: Record<string, unknown> | string;
|
|
63
|
+
/** Simulate a provider-flagged truncated call (cut off mid-arguments). */
|
|
64
|
+
incompleteArguments?: boolean;
|
|
63
65
|
};
|
|
64
66
|
/** One scripted response. */
|
|
65
67
|
export interface MockResponse {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type OpenAI from "openai";
|
|
2
2
|
import type { ResponseInput, ResponseInputContent, ResponseOutputItem } from "openai/resources/responses/responses";
|
|
3
|
-
import { type Api, type AssistantMessage, type ImageContent, type Model, type ServiceTier, type StopReason, type StreamOptions, type TextContent, type TextSignatureV1, type ToolResultMessage } from "../types";
|
|
3
|
+
import { type Api, type AssistantMessage, type ImageContent, type Model, type ServiceTier, type StopReason, type StreamOptions, type TextContent, type TextSignatureV1, type ToolCall, type ToolResultMessage } from "../types";
|
|
4
4
|
import type { AssistantMessageEventStream } from "../utils/event-stream";
|
|
5
5
|
export declare function encodeTextSignatureV1(id: string, phase?: TextSignatureV1["phase"]): string;
|
|
6
6
|
export declare function parseTextSignature(signature: string | undefined): {
|
|
@@ -44,6 +44,21 @@ export interface ProcessResponsesStreamOptions {
|
|
|
44
44
|
onOutputItemDone?: (item: ResponseOutputItem) => void;
|
|
45
45
|
}
|
|
46
46
|
export declare function processResponsesStream<TApi extends Api>(openaiStream: AsyncIterable<OpenAI.Responses.ResponseStreamEvent>, output: AssistantMessage, stream: AssistantMessageEventStream, model: Model<TApi>, options?: ProcessResponsesStreamOptions): Promise<void>;
|
|
47
|
+
/**
|
|
48
|
+
* Mark tool-call blocks left incomplete by a length-truncated response so the
|
|
49
|
+
* agent loop rejects them instead of executing a best-effort partial parse.
|
|
50
|
+
*
|
|
51
|
+
* The universal signal is finalization: a call that never received its terminal
|
|
52
|
+
* `output_item.done` (passed in via `isFinalized`) was cut off mid-arguments.
|
|
53
|
+
* This covers both JSON function calls and raw-input custom tools without
|
|
54
|
+
* mis-flagging a *completed* custom tool whose raw input is not valid JSON. As a
|
|
55
|
+
* defensive secondary, a finalized JSON function call whose buffered arguments
|
|
56
|
+
* still don't parse (e.g. a misbehaving relay) is flagged too. No-op unless the
|
|
57
|
+
* turn stopped for length.
|
|
58
|
+
*
|
|
59
|
+
* Shared by both Responses providers (`openai-responses`, `openai-codex-responses`).
|
|
60
|
+
*/
|
|
61
|
+
export declare function flagTruncatedToolCalls(output: AssistantMessage, stopReason: StopReason, isFinalized: (block: ToolCall) => boolean): void;
|
|
47
62
|
export declare function mapOpenAIResponsesStopReason(status: OpenAI.Responses.ResponseStatus | undefined): StopReason;
|
|
48
63
|
/** Initial empty `AssistantMessage` that streaming providers accumulate into. */
|
|
49
64
|
export declare function createInitialResponsesAssistantMessage(api: Api, provider: string, modelId: string): AssistantMessage;
|
package/dist/types/types.d.ts
CHANGED
|
@@ -48,7 +48,7 @@ export interface ThinkingConfig {
|
|
|
48
48
|
/** Provider-specific transport used to encode the selected effort. */
|
|
49
49
|
mode: ThinkingControlMode;
|
|
50
50
|
}
|
|
51
|
-
export type KnownProvider = "alibaba-coding-plan" | "amazon-bedrock" | "azure-openai" | "anthropic" | "google" | "google-gemini-cli" | "google-antigravity" | "google-vertex" | "openai" | "openai-codex" | "kimi-code" | "minimax-code" | "minimax-code-cn" | "github-copilot" | "fireworks" | "firepass" | "gitlab-duo" | "cursor" | "deepseek" | "xai" | "groq" | "cerebras" | "openrouter" | "kilo" | "vercel-ai-gateway" | "zai" | "glm-zcode" | "mistral" | "minimax" | "opencode-go" | "opencode-zen" | "synthetic" | "cloudflare-ai-gateway" | "huggingface" | "litellm" | "moonshot" | "nvidia" | "nanogpt" | "ollama" | "ollama-cloud" | "qianfan" | "qwen-portal" | "together" | "venice" | "vllm" | "xiaomi" | "xiaomi-token-plan-sgp" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-cn" | "zenmux" | "lm-studio";
|
|
51
|
+
export type KnownProvider = "alibaba-coding-plan" | "amazon-bedrock" | "azure-openai" | "anthropic" | "google" | "google-gemini-cli" | "google-antigravity" | "google-vertex" | "openai" | "openai-codex" | "kimi-code" | "minimax-code" | "minimax-code-cn" | "github-copilot" | "fireworks" | "firepass" | "fugu" | "gitlab-duo" | "cursor" | "deepseek" | "xai" | "groq" | "cerebras" | "openrouter" | "kilo" | "vercel-ai-gateway" | "zai" | "glm-zcode" | "mistral" | "minimax" | "opencode-go" | "opencode-zen" | "synthetic" | "cloudflare-ai-gateway" | "huggingface" | "litellm" | "moonshot" | "nvidia" | "nanogpt" | "ollama" | "ollama-cloud" | "qianfan" | "qwen-portal" | "together" | "venice" | "vllm" | "xiaomi" | "xiaomi-token-plan-sgp" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-cn" | "zenmux" | "lm-studio";
|
|
52
52
|
export type Provider = KnownProvider | string;
|
|
53
53
|
import type { Effort } from "./model-thinking";
|
|
54
54
|
/** Token budgets for each thinking level (token-based providers only) */
|
|
@@ -338,6 +338,14 @@ export interface ToolCall {
|
|
|
338
338
|
* JSON function tools.
|
|
339
339
|
*/
|
|
340
340
|
customWireName?: string;
|
|
341
|
+
/**
|
|
342
|
+
* Set when the provider detected the argument JSON was truncated — the model
|
|
343
|
+
* hit its output-token limit (or the response was otherwise cut short) before
|
|
344
|
+
* emitting a complete arguments object. The `arguments` field then holds a
|
|
345
|
+
* best-effort partial parse and must not be executed as-is; the agent loop
|
|
346
|
+
* rejects the call with a retryable error instead.
|
|
347
|
+
*/
|
|
348
|
+
incompleteArguments?: boolean;
|
|
341
349
|
}
|
|
342
350
|
export interface Usage {
|
|
343
351
|
/** Non-cached input tokens (matches the bucket the provider bills as new input). */
|
|
@@ -8,3 +8,11 @@ export declare function parseJsonWithRepair<T>(json: string): T;
|
|
|
8
8
|
* @returns Parsed object or empty object if parsing fails
|
|
9
9
|
*/
|
|
10
10
|
export declare function parseStreamingJson<T = Record<string, unknown>>(partialJson: string | undefined): T;
|
|
11
|
+
/**
|
|
12
|
+
* Whether a string is a complete, well-formed JSON document (strict parse, no
|
|
13
|
+
* repair). Used to distinguish a tool-call argument blob that finished cleanly
|
|
14
|
+
* from one that was cut off mid-stream (truncation). An empty / whitespace-only
|
|
15
|
+
* string is treated as complete: a tool invoked with no arguments legitimately
|
|
16
|
+
* streams an empty buffer and must not be flagged as truncated.
|
|
17
|
+
*/
|
|
18
|
+
export declare function isCompleteJson(text: string | undefined): boolean;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const loginFugu: (options: import("./types").OAuthController) => Promise<string>;
|
|
@@ -32,6 +32,7 @@ export declare function getOAuthApiKey(provider: OAuthProvider, credentials: Rec
|
|
|
32
32
|
newCredentials: OAuthCredentials;
|
|
33
33
|
apiKey: string;
|
|
34
34
|
} | null>;
|
|
35
|
+
export declare function resolveOAuthStorageProvider(provider: OAuthProviderId): OAuthProviderId;
|
|
35
36
|
/**
|
|
36
37
|
* Get list of OAuth providers.
|
|
37
38
|
*/
|
|
@@ -7,7 +7,7 @@ export type OAuthCredentials = {
|
|
|
7
7
|
email?: string;
|
|
8
8
|
accountId?: string;
|
|
9
9
|
};
|
|
10
|
-
export type OAuthProvider = "alibaba-coding-plan" | "anthropic" | "cerebras" | "cloudflare-ai-gateway" | "cursor" | "deepseek" | "fireworks" | "firepass" | "github-copilot" | "google-gemini-cli" | "google-antigravity" | "gitlab-duo" | "huggingface" | "kimi-code" | "kilo" | "kagi" | "litellm" | "lm-studio" | "minimax-code" | "minimax-code-cn" | "moonshot" | "nvidia" | "nanogpt" | "ollama" | "ollama-cloud" | "openai-codex" | "openai-codex-device" | "opencode-go" | "opencode-zen" | "parallel" | "perplexity" | "qianfan" | "qwen-portal" | "synthetic" | "tavily" | "together" | "venice" | "vercel-ai-gateway" | "vllm" | "xai" | "glm-zcode" | "xiaomi" | "xiaomi-token-plan-sgp" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-cn" | "zenmux" | "zai";
|
|
10
|
+
export type OAuthProvider = "alibaba-coding-plan" | "anthropic" | "cerebras" | "cloudflare-ai-gateway" | "cursor" | "deepseek" | "fireworks" | "firepass" | "fugu" | "github-copilot" | "google-gemini-cli" | "google-antigravity" | "gitlab-duo" | "huggingface" | "kimi-code" | "kilo" | "kagi" | "litellm" | "lm-studio" | "minimax-code" | "minimax-code-cn" | "moonshot" | "nvidia" | "nanogpt" | "ollama" | "ollama-cloud" | "openai-codex" | "openai-codex-device" | "opencode-go" | "opencode-zen" | "parallel" | "perplexity" | "qianfan" | "qwen-portal" | "synthetic" | "tavily" | "together" | "venice" | "vercel-ai-gateway" | "vllm" | "xai" | "glm-zcode" | "xiaomi" | "xiaomi-token-plan-sgp" | "xiaomi-token-plan-ams" | "xiaomi-token-plan-cn" | "zenmux" | "zai";
|
|
11
11
|
export type OAuthProviderId = OAuthProvider | (string & {});
|
|
12
12
|
export type OAuthPrompt = {
|
|
13
13
|
message: string;
|
|
@@ -2,11 +2,14 @@ import type { AssistantMessage } from "../types";
|
|
|
2
2
|
/**
|
|
3
3
|
* Check if an assistant message represents a context overflow error.
|
|
4
4
|
*
|
|
5
|
-
* This handles
|
|
5
|
+
* This handles three cases:
|
|
6
6
|
* 1. Error-based overflow: Most providers return stopReason "error" with a
|
|
7
7
|
* specific error message pattern.
|
|
8
8
|
* 2. Silent overflow: Some providers accept overflow requests and return
|
|
9
9
|
* successfully. For these, we check if usage.input exceeds the context window.
|
|
10
|
+
* 3. Proxy-level overflow: Some proxies (e.g. LiteLLM) return a "successful"
|
|
11
|
+
* response with empty content and a fabricated near-zero usage when the
|
|
12
|
+
* upstream model's context window is exceeded.
|
|
10
13
|
*
|
|
11
14
|
* ## Reliability by Provider
|
|
12
15
|
*
|
|
@@ -30,6 +33,13 @@ import type { AssistantMessage } from "../types";
|
|
|
30
33
|
* - z.ai: Sometimes accepts overflow silently (detectable via usage.input > contextWindow),
|
|
31
34
|
* sometimes returns rate limit errors. Pass contextWindow param to detect silent overflow.
|
|
32
35
|
* - Ollama: Silently truncates input without error. Cannot be detected via this function.
|
|
36
|
+
* - LiteLLM proxy: Returns a "successful" response with empty content and a
|
|
37
|
+
* fabricated near-zero usage (e.g. input: 1, output: 1) when the upstream
|
|
38
|
+
* model's context window is exceeded. Detected via Case 3 (empty content +
|
|
39
|
+
* anomalously low usage). Note: the LiteLLM proxy's context limit may differ
|
|
40
|
+
* from the underlying model's advertised contextWindow (e.g. configured via
|
|
41
|
+
* `model_info.max_tokens` in LiteLLM's config.yaml), so Case 2 (which compares
|
|
42
|
+
* usage.input against contextWindow) may not catch it.
|
|
33
43
|
* The response will have usage.input < expected, but we don't know the expected value.
|
|
34
44
|
*
|
|
35
45
|
* ## Custom Providers
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "@sayknow-cli/ai",
|
|
4
|
-
"version": "0.3.
|
|
4
|
+
"version": "0.3.2",
|
|
5
5
|
"description": "Unified LLM API with automatic model discovery and provider configuration",
|
|
6
6
|
"homepage": "https://github.com/jaybeyond/Sayknow_CLI",
|
|
7
7
|
"author": "jaybeyond",
|
|
@@ -43,7 +43,7 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@anthropic-ai/sdk": "^0.94.0",
|
|
45
45
|
"@bufbuild/protobuf": "^2.12.0",
|
|
46
|
-
"@sayknow-cli/utils": "0.3.
|
|
46
|
+
"@sayknow-cli/utils": "0.3.2",
|
|
47
47
|
"openai": "^6.36.0",
|
|
48
48
|
"partial-json": "^0.1.7",
|
|
49
49
|
"zod": "4.4.3"
|
package/src/auth-gateway/http.ts
CHANGED
|
@@ -47,7 +47,7 @@ export function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean {
|
|
|
47
47
|
const TOKEN_ENCODER = new TextEncoder();
|
|
48
48
|
|
|
49
49
|
export function isAuthorized(req: Request, tokens: ReadonlySet<string>): boolean {
|
|
50
|
-
if (tokens.size === 0) return
|
|
50
|
+
if (tokens.size === 0) return !isNoAuthBrowserOriginRequest(req, tokens);
|
|
51
51
|
const header = req.headers.get("authorization");
|
|
52
52
|
if (!header) return false;
|
|
53
53
|
const match = header.match(/^Bearer\s+(.+)$/i);
|
|
@@ -63,6 +63,10 @@ export function isAuthorized(req: Request, tokens: ReadonlySet<string>): boolean
|
|
|
63
63
|
return ok;
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
+
export function isNoAuthBrowserOriginRequest(req: Request, tokens: ReadonlySet<string>): boolean {
|
|
67
|
+
return tokens.size === 0 && req.headers.has("origin");
|
|
68
|
+
}
|
|
69
|
+
|
|
66
70
|
/**
|
|
67
71
|
* Allow-list of inbound request headers that the gateway captures and forwards
|
|
68
72
|
* to the underlying parsers (which decide whether to surface them to the
|
|
@@ -27,7 +27,15 @@ import * as piNative from "../providers/pi-native-server";
|
|
|
27
27
|
import { streamSimple } from "../stream";
|
|
28
28
|
import type { Api, AssistantMessageEventStream, Context, Model, SimpleStreamOptions } from "../types";
|
|
29
29
|
import { parseBind } from "../utils/parse-bind";
|
|
30
|
-
import {
|
|
30
|
+
import {
|
|
31
|
+
captureRequestHeaders,
|
|
32
|
+
corsHeaders,
|
|
33
|
+
isAuthorized,
|
|
34
|
+
isNoAuthBrowserOriginRequest,
|
|
35
|
+
json,
|
|
36
|
+
resolvePeer,
|
|
37
|
+
withCors,
|
|
38
|
+
} from "./http";
|
|
31
39
|
import type {
|
|
32
40
|
AuthGatewayServerHandle,
|
|
33
41
|
AuthGatewayServerOptions,
|
|
@@ -640,6 +648,15 @@ export function startAuthGateway(opts: AuthGatewayBootOptions): AuthGatewayServe
|
|
|
640
648
|
const url = new URL(req.url);
|
|
641
649
|
const pathname = url.pathname;
|
|
642
650
|
const peer = resolvePeer(req);
|
|
651
|
+
if (isNoAuthBrowserOriginRequest(req, tokens)) {
|
|
652
|
+
logger.info("auth-gateway no-auth browser-origin request rejected", {
|
|
653
|
+
method: req.method,
|
|
654
|
+
path: pathname,
|
|
655
|
+
peer,
|
|
656
|
+
origin: req.headers.get("origin"),
|
|
657
|
+
});
|
|
658
|
+
return json(403, { error: "browser origin requires bearer token" });
|
|
659
|
+
}
|
|
643
660
|
// CORS preflight is always answered without auth — browsers send
|
|
644
661
|
// preflights pre-authentication and a 401 here breaks the actual
|
|
645
662
|
// request before the bearer is ever attached.
|
package/src/auth-storage.ts
CHANGED
|
@@ -31,7 +31,7 @@ import { grokCliRankingStrategy, grokCliUsageProvider } from "./usage/grok-cli";
|
|
|
31
31
|
import { kimiUsageProvider } from "./usage/kimi";
|
|
32
32
|
import { codexRankingStrategy, openaiCodexUsageProvider } from "./usage/openai-codex";
|
|
33
33
|
import { zaiUsageProvider } from "./usage/zai";
|
|
34
|
-
import { getOAuthApiKey, getOAuthProvider, refreshOAuthToken } from "./utils/oauth";
|
|
34
|
+
import { getOAuthApiKey, getOAuthProvider, refreshOAuthToken, resolveOAuthStorageProvider } from "./utils/oauth";
|
|
35
35
|
import { loginDeepSeek } from "./utils/oauth/deepseek";
|
|
36
36
|
import { loginOpenAICodexDevice } from "./utils/oauth/openai-codex";
|
|
37
37
|
import type { OAuthController, OAuthCredentials, OAuthProvider, OAuthProviderId } from "./utils/oauth/types";
|
|
@@ -918,7 +918,8 @@ export class AuthStorage {
|
|
|
918
918
|
* @returns Array of stored credentials, empty if none exist
|
|
919
919
|
*/
|
|
920
920
|
#getStoredCredentials(provider: string): StoredCredential[] {
|
|
921
|
-
|
|
921
|
+
const storageProvider = resolveOAuthStorageProvider(provider);
|
|
922
|
+
return this.#data.get(storageProvider) ?? [];
|
|
922
923
|
}
|
|
923
924
|
|
|
924
925
|
/**
|
|
@@ -1250,16 +1251,17 @@ export class AuthStorage {
|
|
|
1250
1251
|
* Set credential for a provider.
|
|
1251
1252
|
*/
|
|
1252
1253
|
async set(provider: string, credential: AuthCredentialEntry): Promise<void> {
|
|
1254
|
+
const storageProvider = resolveOAuthStorageProvider(provider);
|
|
1253
1255
|
const normalized = Array.isArray(credential) ? credential : [credential];
|
|
1254
|
-
const deduped = this.#dedupeOAuthCredentials(
|
|
1256
|
+
const deduped = this.#dedupeOAuthCredentials(storageProvider, normalized);
|
|
1255
1257
|
const stored = this.#store.replaceAuthCredentialsRemote
|
|
1256
|
-
? await this.#store.replaceAuthCredentialsRemote(
|
|
1257
|
-
: this.#store.replaceAuthCredentialsForProvider(
|
|
1258
|
+
? await this.#store.replaceAuthCredentialsRemote(storageProvider, deduped)
|
|
1259
|
+
: this.#store.replaceAuthCredentialsForProvider(storageProvider, deduped);
|
|
1258
1260
|
this.#setStoredCredentials(
|
|
1259
|
-
|
|
1261
|
+
storageProvider,
|
|
1260
1262
|
stored.map(record => ({ id: record.id, credential: record.credential })),
|
|
1261
1263
|
);
|
|
1262
|
-
this.#resetProviderAssignments(
|
|
1264
|
+
this.#resetProviderAssignments(storageProvider);
|
|
1263
1265
|
}
|
|
1264
1266
|
|
|
1265
1267
|
#toSnapshotEntries(provider: string, stored: StoredAuthCredential[]): AuthCredentialSnapshotEntry[] {
|
|
@@ -1289,26 +1291,30 @@ export class AuthStorage {
|
|
|
1289
1291
|
provider: string,
|
|
1290
1292
|
credential: AuthCredential,
|
|
1291
1293
|
): Promise<AuthCredentialIfAbsentSnapshotResult> {
|
|
1292
|
-
|
|
1293
|
-
if (this.#
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
if (this.#
|
|
1294
|
+
const storageProvider = resolveOAuthStorageProvider(provider);
|
|
1295
|
+
if (this.#runtimeOverrides.has(storageProvider))
|
|
1296
|
+
return this.#snapshotSkipResult(storageProvider, "skipped-existing-runtime");
|
|
1297
|
+
if (this.#configOverrides.has(storageProvider))
|
|
1298
|
+
return this.#snapshotSkipResult(storageProvider, "skipped-existing-config");
|
|
1299
|
+
if (this.#getCredentialsForProvider(storageProvider).length > 0)
|
|
1300
|
+
return this.#snapshotSkipResult(storageProvider, "skipped-existing");
|
|
1301
|
+
if (getEnvApiKey(storageProvider)) return this.#snapshotSkipResult(storageProvider, "skipped-existing-env");
|
|
1302
|
+
if (this.#fallbackResolver?.(storageProvider))
|
|
1303
|
+
return this.#snapshotSkipResult(storageProvider, "skipped-existing-fallback");
|
|
1298
1304
|
|
|
1299
1305
|
const result = this.#store.upsertAuthCredentialRemoteIfAbsent
|
|
1300
|
-
? await this.#store.upsertAuthCredentialRemoteIfAbsent(
|
|
1301
|
-
: this.#store.upsertAuthCredentialForProviderIfAbsent(
|
|
1306
|
+
? await this.#store.upsertAuthCredentialRemoteIfAbsent(storageProvider, credential)
|
|
1307
|
+
: this.#store.upsertAuthCredentialForProviderIfAbsent(storageProvider, credential);
|
|
1302
1308
|
this.#setStoredCredentials(
|
|
1303
|
-
|
|
1309
|
+
storageProvider,
|
|
1304
1310
|
result.entries.map(entry => ({ id: entry.id, credential: entry.credential })),
|
|
1305
1311
|
);
|
|
1306
|
-
this.#resetProviderAssignments(
|
|
1312
|
+
this.#resetProviderAssignments(storageProvider);
|
|
1307
1313
|
return {
|
|
1308
1314
|
inserted: result.inserted,
|
|
1309
1315
|
reason: result.reason,
|
|
1310
1316
|
provider: result.provider,
|
|
1311
|
-
entries: this.#toSnapshotEntries(
|
|
1317
|
+
entries: this.#toSnapshotEntries(storageProvider, result.entries),
|
|
1312
1318
|
};
|
|
1313
1319
|
}
|
|
1314
1320
|
|
|
@@ -1327,13 +1333,14 @@ export class AuthStorage {
|
|
|
1327
1333
|
* Remove credential for a provider.
|
|
1328
1334
|
*/
|
|
1329
1335
|
async remove(provider: string): Promise<void> {
|
|
1336
|
+
const storageProvider = resolveOAuthStorageProvider(provider);
|
|
1330
1337
|
if (this.#store.deleteAuthCredentialsRemote) {
|
|
1331
|
-
await this.#store.deleteAuthCredentialsRemote(
|
|
1338
|
+
await this.#store.deleteAuthCredentialsRemote(storageProvider, "deleted by user");
|
|
1332
1339
|
} else {
|
|
1333
|
-
this.#store.deleteAuthCredentialsForProvider(
|
|
1340
|
+
this.#store.deleteAuthCredentialsForProvider(storageProvider, "deleted by user");
|
|
1334
1341
|
}
|
|
1335
|
-
this.#setStoredCredentials(
|
|
1336
|
-
this.#resetProviderAssignments(
|
|
1342
|
+
this.#setStoredCredentials(storageProvider, []);
|
|
1343
|
+
this.#resetProviderAssignments(storageProvider);
|
|
1337
1344
|
}
|
|
1338
1345
|
|
|
1339
1346
|
/**
|
|
@@ -1355,11 +1362,12 @@ export class AuthStorage {
|
|
|
1355
1362
|
* Unlike getApiKey(), this doesn't refresh OAuth tokens.
|
|
1356
1363
|
*/
|
|
1357
1364
|
hasAuth(provider: string): boolean {
|
|
1358
|
-
|
|
1359
|
-
if (this.#
|
|
1360
|
-
if (this.#
|
|
1361
|
-
if (
|
|
1362
|
-
if (
|
|
1365
|
+
const storageProvider = resolveOAuthStorageProvider(provider);
|
|
1366
|
+
if (this.#runtimeOverrides.has(storageProvider)) return true;
|
|
1367
|
+
if (this.#configOverrides.has(storageProvider)) return true;
|
|
1368
|
+
if (this.#getCredentialsForProvider(storageProvider).length > 0) return true;
|
|
1369
|
+
if (getEnvApiKey(storageProvider)) return true;
|
|
1370
|
+
if (this.#fallbackResolver?.(storageProvider)) return true;
|
|
1363
1371
|
return false;
|
|
1364
1372
|
}
|
|
1365
1373
|
|
|
@@ -1613,6 +1621,12 @@ export class AuthStorage {
|
|
|
1613
1621
|
await saveApiKeyCredential(apiKey);
|
|
1614
1622
|
return;
|
|
1615
1623
|
}
|
|
1624
|
+
case "fugu": {
|
|
1625
|
+
const { loginFugu } = await import("./utils/oauth/fugu");
|
|
1626
|
+
const apiKey = await loginFugu(ctrl);
|
|
1627
|
+
await saveApiKeyCredential(apiKey);
|
|
1628
|
+
return;
|
|
1629
|
+
}
|
|
1616
1630
|
case "zai": {
|
|
1617
1631
|
const { loginZai } = await import("./utils/oauth/zai");
|
|
1618
1632
|
const apiKey = await loginZai(ctrl);
|
package/src/model-thinking.ts
CHANGED
|
@@ -197,7 +197,7 @@ export function applyGeneratedModelPolicies(models: ApiModel<Api>[]): void {
|
|
|
197
197
|
* model with a larger context window on the same provider:
|
|
198
198
|
* - `OpenAI code backend-spark` variants promote to `gpt-5.5`.
|
|
199
199
|
*
|
|
200
|
-
* `gpt-5.5` itself is a
|
|
200
|
+
* `gpt-5.5` itself is a 1M-context model and is not demoted to `gpt-5.4`
|
|
201
201
|
* (which has a smaller window), so it has no promotion target.
|
|
202
202
|
*/
|
|
203
203
|
export function linkOpenAIPromotionTargets(models: ApiModel<Api>[]): void {
|
|
@@ -453,15 +453,8 @@ function inferGeneratedApplyPatchToolType(
|
|
|
453
453
|
}
|
|
454
454
|
|
|
455
455
|
function applyGpt55ContextWindow(model: ApiModel<Api>, parsedModel: OpenAIModel): boolean {
|
|
456
|
-
// OpenAI Codex reports GPT-5.5 with a 272K prompt budget. Keep the generated
|
|
457
|
-
// bundle aligned with the backend limit so compaction fires before the prompt
|
|
458
|
-
// crosses the usable Codex window instead of trusting stale 400K snapshots.
|
|
459
|
-
if (model.provider === "openai-codex" && parsedModel.variant === "base" && semverEqual(parsedModel.version, "5.5")) {
|
|
460
|
-
model.contextWindow = 272000;
|
|
461
|
-
return true;
|
|
462
|
-
}
|
|
463
456
|
if (parsedModel.variant === "base" && semverEqual(parsedModel.version, "5.5")) {
|
|
464
|
-
model.contextWindow =
|
|
457
|
+
model.contextWindow = 1_000_000;
|
|
465
458
|
return true;
|
|
466
459
|
}
|
|
467
460
|
return false;
|
package/src/models.json
CHANGED
|
@@ -56310,7 +56310,7 @@
|
|
|
56310
56310
|
"cacheRead": 0.5,
|
|
56311
56311
|
"cacheWrite": 0
|
|
56312
56312
|
},
|
|
56313
|
-
"contextWindow":
|
|
56313
|
+
"contextWindow": 1000000,
|
|
56314
56314
|
"maxTokens": 128000,
|
|
56315
56315
|
"preferWebsockets": true,
|
|
56316
56316
|
"priority": 9,
|
|
@@ -79757,5 +79757,71 @@
|
|
|
79757
79757
|
"maxLevel": "xhigh"
|
|
79758
79758
|
}
|
|
79759
79759
|
}
|
|
79760
|
+
},
|
|
79761
|
+
"fugu": {
|
|
79762
|
+
"fugu": {
|
|
79763
|
+
"id": "fugu",
|
|
79764
|
+
"name": "Sakana Fugu",
|
|
79765
|
+
"api": "openai-completions",
|
|
79766
|
+
"provider": "fugu",
|
|
79767
|
+
"baseUrl": "https://api.sakana.ai/v1",
|
|
79768
|
+
"reasoning": true,
|
|
79769
|
+
"input": [
|
|
79770
|
+
"text"
|
|
79771
|
+
],
|
|
79772
|
+
"cost": {
|
|
79773
|
+
"input": 0,
|
|
79774
|
+
"output": 0,
|
|
79775
|
+
"cacheRead": 0,
|
|
79776
|
+
"cacheWrite": 0
|
|
79777
|
+
},
|
|
79778
|
+
"contextWindow": 200000,
|
|
79779
|
+
"maxTokens": 65536,
|
|
79780
|
+
"compat": {
|
|
79781
|
+
"supportsStore": false,
|
|
79782
|
+
"supportsDeveloperRole": false,
|
|
79783
|
+
"supportsMultipleSystemMessages": false,
|
|
79784
|
+
"supportsReasoningEffort": false,
|
|
79785
|
+
"supportsUsageInStreaming": true,
|
|
79786
|
+
"maxTokensField": "max_tokens"
|
|
79787
|
+
},
|
|
79788
|
+
"thinking": {
|
|
79789
|
+
"mode": "effort",
|
|
79790
|
+
"minLevel": "minimal",
|
|
79791
|
+
"maxLevel": "high"
|
|
79792
|
+
}
|
|
79793
|
+
},
|
|
79794
|
+
"fugu-ultra": {
|
|
79795
|
+
"id": "fugu-ultra",
|
|
79796
|
+
"name": "Sakana Fugu Ultra",
|
|
79797
|
+
"api": "openai-completions",
|
|
79798
|
+
"provider": "fugu",
|
|
79799
|
+
"baseUrl": "https://api.sakana.ai/v1",
|
|
79800
|
+
"reasoning": true,
|
|
79801
|
+
"input": [
|
|
79802
|
+
"text"
|
|
79803
|
+
],
|
|
79804
|
+
"cost": {
|
|
79805
|
+
"input": 0,
|
|
79806
|
+
"output": 0,
|
|
79807
|
+
"cacheRead": 0,
|
|
79808
|
+
"cacheWrite": 0
|
|
79809
|
+
},
|
|
79810
|
+
"contextWindow": 200000,
|
|
79811
|
+
"maxTokens": 65536,
|
|
79812
|
+
"compat": {
|
|
79813
|
+
"supportsStore": false,
|
|
79814
|
+
"supportsDeveloperRole": false,
|
|
79815
|
+
"supportsMultipleSystemMessages": false,
|
|
79816
|
+
"supportsReasoningEffort": false,
|
|
79817
|
+
"supportsUsageInStreaming": true,
|
|
79818
|
+
"maxTokensField": "max_tokens"
|
|
79819
|
+
},
|
|
79820
|
+
"thinking": {
|
|
79821
|
+
"mode": "effort",
|
|
79822
|
+
"minLevel": "minimal",
|
|
79823
|
+
"maxLevel": "high"
|
|
79824
|
+
}
|
|
79825
|
+
}
|
|
79760
79826
|
}
|
|
79761
79827
|
}
|
|
@@ -16,6 +16,7 @@ import {
|
|
|
16
16
|
deepseekModelManagerOptions,
|
|
17
17
|
firepassModelManagerOptions,
|
|
18
18
|
fireworksModelManagerOptions,
|
|
19
|
+
fuguModelManagerOptions,
|
|
19
20
|
githubCopilotModelManagerOptions,
|
|
20
21
|
groqModelManagerOptions,
|
|
21
22
|
huggingfaceModelManagerOptions,
|
|
@@ -154,6 +155,12 @@ export const PROVIDER_DESCRIPTORS: readonly ProviderDescriptor[] = [
|
|
|
154
155
|
catalog("Fireworks", ["FIREWORKS_API_KEY"]),
|
|
155
156
|
),
|
|
156
157
|
descriptor("firepass", "kimi-k2.6-turbo", config => firepassModelManagerOptions(config)),
|
|
158
|
+
catalogDescriptor(
|
|
159
|
+
"fugu",
|
|
160
|
+
"fugu",
|
|
161
|
+
config => fuguModelManagerOptions(config),
|
|
162
|
+
catalog("Sakana Fugu", ["FUGU_API_KEY"]),
|
|
163
|
+
),
|
|
157
164
|
descriptor("xai", "grok-4-fast-non-reasoning", config => xaiModelManagerOptions(config)),
|
|
158
165
|
catalogDescriptor(
|
|
159
166
|
"deepseek",
|
|
@@ -785,6 +785,15 @@ export function firepassModelManagerOptions(
|
|
|
785
785
|
// 7. Mistral
|
|
786
786
|
// ---------------------------------------------------------------------------
|
|
787
787
|
|
|
788
|
+
export interface FuguModelManagerConfig {
|
|
789
|
+
apiKey?: string;
|
|
790
|
+
baseUrl?: string;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
export function fuguModelManagerOptions(config?: FuguModelManagerConfig): ModelManagerOptions<"openai-completions"> {
|
|
794
|
+
return createSimpleOpenAICompletionsOptions("fugu", config?.baseUrl ?? "https://api.sakana.ai/v1", config);
|
|
795
|
+
}
|
|
796
|
+
|
|
788
797
|
export interface MistralModelManagerConfig {
|
|
789
798
|
apiKey?: string;
|
|
790
799
|
baseUrl?: string;
|