@gajae-code/ai 0.4.5 → 0.5.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 +31 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/providers/amazon-bedrock.d.ts +29 -5
- package/dist/types/providers/composer-discipline.d.ts +27 -0
- package/dist/types/providers/cursor.d.ts +1 -1
- package/dist/types/providers/google-gemini-cli.d.ts +1 -1
- package/dist/types/providers/google-shared.d.ts +11 -1
- package/dist/types/providers/ollama.d.ts +36 -1
- package/dist/types/providers/openai-completions-compat.d.ts +3 -1
- package/dist/types/providers/register-builtins.d.ts +3 -3
- package/dist/types/types.d.ts +25 -3
- package/dist/types/usage/grok-cli.d.ts +10 -0
- package/dist/types/utils/event-stream.d.ts +6 -1
- package/dist/types/utils/oauth/xai.d.ts +10 -3
- package/dist/types/utils/tool-choice-capability.d.ts +41 -0
- package/package.json +2 -2
- package/src/auth-storage.ts +3 -0
- package/src/index.ts +2 -0
- package/src/model-thinking.ts +9 -0
- package/src/models.json +116 -0
- package/src/models.ts +33 -7
- package/src/provider-models/descriptors.ts +1 -1
- package/src/provider-models/openai-compat.ts +9 -1
- package/src/providers/amazon-bedrock.ts +145 -60
- package/src/providers/anthropic.ts +85 -32
- package/src/providers/azure-openai-responses.ts +44 -3
- package/src/providers/composer-discipline.ts +38 -0
- package/src/providers/cursor.ts +10 -3
- package/src/providers/google-gemini-cli.ts +69 -10
- package/src/providers/google-shared.ts +61 -12
- package/src/providers/ollama.ts +60 -4
- package/src/providers/openai-codex-responses.ts +151 -2
- package/src/providers/openai-completions-compat.ts +9 -1
- package/src/providers/openai-completions.ts +46 -6
- package/src/providers/openai-request-transform.ts +1 -0
- package/src/providers/openai-responses.ts +54 -5
- package/src/providers/register-builtins.ts +5 -6
- package/src/rate-limit-utils.ts +11 -2
- package/src/types.ts +37 -3
- package/src/usage/grok-cli.ts +163 -0
- package/src/utils/event-stream.ts +35 -5
- package/src/utils/oauth/xai.ts +49 -13
- package/src/utils/tool-choice-capability.ts +220 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { extractHttpStatusFromError } from "@gajae-code/utils/fetch-retry";
|
|
2
|
+
import * as logger from "@gajae-code/utils/logger";
|
|
3
|
+
import type { Api, Model, ToolChoice, ToolChoiceCompat, ToolChoiceSupport, ToolChoiceSupportSource } from "../types";
|
|
4
|
+
|
|
5
|
+
const supportRank: Record<ToolChoiceSupport, number> = {
|
|
6
|
+
none: 0,
|
|
7
|
+
auto: 1,
|
|
8
|
+
required: 2,
|
|
9
|
+
named: 3,
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const registry = new Map<string, ToolChoiceSupport>();
|
|
13
|
+
const loggedRegistryKeys = new Set<string>();
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Claude Mythos accepts tools but rejects forced tool use (Anthropic 400:
|
|
17
|
+
* "tool_choice forces tool use is not compatible with this model"). Catalog
|
|
18
|
+
* generation and dynamic discovery use this to default `toolChoiceSupport`.
|
|
19
|
+
*/
|
|
20
|
+
export function isClaudeForcedToolChoiceIncapableModelId(modelId: string): boolean {
|
|
21
|
+
return /(?:^|[/.])claude-mythos(?:-|$)/i.test(modelId);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Derives the effective static tool-choice support from compatibility flags. */
|
|
25
|
+
export function deriveToolChoiceSupport(compat: ToolChoiceCompat | undefined): {
|
|
26
|
+
support: ToolChoiceSupport;
|
|
27
|
+
source: "static" | "derived";
|
|
28
|
+
} {
|
|
29
|
+
if (compat?.toolChoiceSupport) {
|
|
30
|
+
return { support: compat.toolChoiceSupport, source: "static" };
|
|
31
|
+
}
|
|
32
|
+
if (compat?.supportsToolChoice === false) {
|
|
33
|
+
return { support: "none", source: "derived" };
|
|
34
|
+
}
|
|
35
|
+
if (compat?.supportsForcedToolChoice === false) {
|
|
36
|
+
return { support: "auto", source: "derived" };
|
|
37
|
+
}
|
|
38
|
+
return { support: "named", source: "derived" };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Returns the registry key used for runtime tool-choice capability overrides. */
|
|
42
|
+
export function toolChoiceRegistryKey(model: Model<Api>): string {
|
|
43
|
+
return [model.api, model.provider, model.baseUrl, model.wireModelId ?? model.id].join("|");
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Returns the current runtime tool-choice capability override for a model. */
|
|
47
|
+
export function getToolChoiceCapabilityOverride(model: Model<Api>): ToolChoiceSupport | undefined {
|
|
48
|
+
return registry.get(toolChoiceRegistryKey(model));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Clears runtime tool-choice capability overrides for tests. */
|
|
52
|
+
export function clearToolChoiceIncapabilityRegistryForTests(): void {
|
|
53
|
+
registry.clear();
|
|
54
|
+
loggedRegistryKeys.clear();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Records a discovered maximum supported tool-choice level for a model. */
|
|
58
|
+
export function markToolChoiceIncapability(model: Model<Api>, maxSupport: ToolChoiceSupport, reason?: string): void {
|
|
59
|
+
const key = toolChoiceRegistryKey(model);
|
|
60
|
+
const existing = registry.get(key);
|
|
61
|
+
const next = existing && supportRank[existing] < supportRank[maxSupport] ? existing : maxSupport;
|
|
62
|
+
registry.set(key, next);
|
|
63
|
+
|
|
64
|
+
if (!loggedRegistryKeys.has(key)) {
|
|
65
|
+
loggedRegistryKeys.add(key);
|
|
66
|
+
logger.debug("Discovered tool_choice incapability", {
|
|
67
|
+
api: model.api,
|
|
68
|
+
provider: model.provider,
|
|
69
|
+
baseUrlHost: safeHostname(model.baseUrl),
|
|
70
|
+
model: model.wireModelId ?? model.id,
|
|
71
|
+
maxSupport,
|
|
72
|
+
reason,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Resolves a requested tool_choice against static and runtime capability limits.
|
|
79
|
+
* `compat` overrides `model.compat` for transports that layer URL/provider
|
|
80
|
+
* detection on top of explicit model overrides (e.g. resolveOpenAICompat).
|
|
81
|
+
*/
|
|
82
|
+
export function resolveToolChoice(
|
|
83
|
+
model: Model<Api>,
|
|
84
|
+
requested: ToolChoice | undefined,
|
|
85
|
+
compat?: ToolChoiceCompat,
|
|
86
|
+
): ResolveToolChoiceResult {
|
|
87
|
+
const derived = deriveToolChoiceSupport(compat ?? model.compat);
|
|
88
|
+
const runtime = registry.get(toolChoiceRegistryKey(model));
|
|
89
|
+
const support = runtime && supportRank[runtime] < supportRank[derived.support] ? runtime : derived.support;
|
|
90
|
+
const supportSource: ToolChoiceSupportSource = support === derived.support ? derived.source : "runtime";
|
|
91
|
+
const requestedInfo = requestedToolChoiceLevel(requested);
|
|
92
|
+
const clampLevel = requestedInfo.requestedLevel === "none" ? "auto" : requestedInfo.requestedLevel;
|
|
93
|
+
const registryKey = toolChoiceRegistryKey(model);
|
|
94
|
+
|
|
95
|
+
if (requested === undefined) {
|
|
96
|
+
return {
|
|
97
|
+
requestedChoice: requested,
|
|
98
|
+
requestedLevel: requestedInfo.requestedLevel,
|
|
99
|
+
resolvedChoice: undefined,
|
|
100
|
+
resolvedLevel: "auto",
|
|
101
|
+
support,
|
|
102
|
+
supportSource,
|
|
103
|
+
degraded: false,
|
|
104
|
+
registryKey,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (support === "none") {
|
|
109
|
+
return {
|
|
110
|
+
requestedChoice: requested,
|
|
111
|
+
requestedLevel: requestedInfo.requestedLevel,
|
|
112
|
+
resolvedChoice: undefined,
|
|
113
|
+
resolvedLevel: "none",
|
|
114
|
+
support,
|
|
115
|
+
supportSource,
|
|
116
|
+
degraded: requestedInfo.requestedLevel !== "none",
|
|
117
|
+
reason: "tool_choice is not supported by this model",
|
|
118
|
+
registryKey,
|
|
119
|
+
targetToolName: requestedInfo.targetToolName,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
if (supportRank[support] >= supportRank[clampLevel]) {
|
|
124
|
+
return {
|
|
125
|
+
requestedChoice: requested,
|
|
126
|
+
requestedLevel: requestedInfo.requestedLevel,
|
|
127
|
+
resolvedChoice: requested,
|
|
128
|
+
resolvedLevel: requestedInfo.requestedLevel,
|
|
129
|
+
support,
|
|
130
|
+
supportSource,
|
|
131
|
+
degraded: false,
|
|
132
|
+
registryKey,
|
|
133
|
+
targetToolName: requestedInfo.targetToolName,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (requestedInfo.requestedLevel === "named" && support === "required") {
|
|
138
|
+
return {
|
|
139
|
+
requestedChoice: requested,
|
|
140
|
+
requestedLevel: "named",
|
|
141
|
+
resolvedChoice: "required",
|
|
142
|
+
resolvedLevel: "required",
|
|
143
|
+
support,
|
|
144
|
+
supportSource,
|
|
145
|
+
degraded: true,
|
|
146
|
+
reason: "named tool_choice degraded to required",
|
|
147
|
+
registryKey,
|
|
148
|
+
targetToolName: requestedInfo.targetToolName,
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return {
|
|
153
|
+
requestedChoice: requested,
|
|
154
|
+
requestedLevel: requestedInfo.requestedLevel,
|
|
155
|
+
resolvedChoice: undefined,
|
|
156
|
+
resolvedLevel: support === "auto" ? "auto" : "none",
|
|
157
|
+
support,
|
|
158
|
+
supportSource,
|
|
159
|
+
degraded: true,
|
|
160
|
+
reason: "forced tool_choice is not supported by this model",
|
|
161
|
+
registryKey,
|
|
162
|
+
targetToolName: requestedInfo.targetToolName,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Detects provider errors indicating forced tool_choice is unsupported. */
|
|
167
|
+
export function isForcedToolChoiceUnsupportedError(error: unknown, sentForcedToolChoice: boolean): boolean {
|
|
168
|
+
if (!sentForcedToolChoice || extractHttpStatusFromError(error) !== 400) return false;
|
|
169
|
+
const message = errorMessage(error);
|
|
170
|
+
return (
|
|
171
|
+
// `by <something>` continuations ("not supported by billing") describe a
|
|
172
|
+
// different subject than the model's tool_choice capability — reject them
|
|
173
|
+
// unless the continuation names the model itself.
|
|
174
|
+
/tool[_\s-]?choices?\b.*?(not\s+compatible|incompatible|not\s+supported)(?!\s+by\s+(?!(?:this|the)\s+model\b|model\b))/is.test(
|
|
175
|
+
message,
|
|
176
|
+
) ||
|
|
177
|
+
/forces?\s+tool\s+use.*?(not\s+compatible|incompatible|not\s+supported)/is.test(message) ||
|
|
178
|
+
/does\s+not\s+support\s+forced\s+tool[_\s-]?choices?/is.test(message)
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
export type { ToolChoiceCompat, ToolChoiceSupport, ToolChoiceSupportSource } from "../types";
|
|
183
|
+
|
|
184
|
+
export interface ResolveToolChoiceResult {
|
|
185
|
+
requestedChoice: ToolChoice | undefined;
|
|
186
|
+
requestedLevel: ToolChoiceSupport;
|
|
187
|
+
resolvedChoice: ToolChoice | undefined;
|
|
188
|
+
resolvedLevel: ToolChoiceSupport;
|
|
189
|
+
support: ToolChoiceSupport;
|
|
190
|
+
supportSource: ToolChoiceSupportSource;
|
|
191
|
+
degraded: boolean;
|
|
192
|
+
reason?: string;
|
|
193
|
+
registryKey: string;
|
|
194
|
+
targetToolName?: string;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function requestedToolChoiceLevel(requested: ToolChoice | undefined): {
|
|
198
|
+
requestedLevel: ToolChoiceSupport;
|
|
199
|
+
targetToolName?: string;
|
|
200
|
+
} {
|
|
201
|
+
if (requested === undefined || requested === "auto") return { requestedLevel: "auto" };
|
|
202
|
+
if (requested === "none") return { requestedLevel: "none" };
|
|
203
|
+
if (requested === "any" || requested === "required") return { requestedLevel: "required" };
|
|
204
|
+
if ("name" in requested) return { requestedLevel: "named", targetToolName: requested.name };
|
|
205
|
+
return { requestedLevel: "named", targetToolName: requested.function.name };
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function errorMessage(error: unknown): string {
|
|
209
|
+
if (error instanceof Error) return error.message;
|
|
210
|
+
if (typeof error === "string") return error;
|
|
211
|
+
return String(error);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
function safeHostname(baseUrl: string): string | undefined {
|
|
215
|
+
try {
|
|
216
|
+
return new URL(baseUrl).hostname;
|
|
217
|
+
} catch {
|
|
218
|
+
return undefined;
|
|
219
|
+
}
|
|
220
|
+
}
|