@certalis/agent-smith 0.1.18 → 0.1.19
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/README.md +14 -8
- package/dist/bin.js +1060 -144
- package/dist/claude-companion/.claude-plugin/plugin.json +8 -0
- package/dist/claude-companion/hooks/hooks.json +4 -0
- package/dist/claude-companion/hooks/provider-choice.js +320 -0
- package/dist/claude-companion/hooks/register.js +74 -0
- package/dist/client-launcher.js +70 -11
- package/dist/edge.js +13 -2
- package/dist/https.js +13 -2
- package/dist/pool.js +985 -114
- package/dist/post-processor.worker.js +148 -1
- package/dist/server.js +985 -114
- package/package.json +1 -1
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
const CAPACITY_HEADER = "x-agent-smith-provider-capacity";
|
|
2
|
+
const CAPACITY_DETAILS_HEADER = "x-agent-smith-provider-capacity-details";
|
|
3
|
+
const INTERACTIVE_CHOICE_HEADER = "x-agent-smith-interactive-provider-choice";
|
|
4
|
+
const PROVIDER_CHOICE_HEADER = "x-agent-smith-provider-choice";
|
|
5
|
+
const PROVIDER_CHOICE_MENU_HEADER = "x-agent-smith-provider-menu";
|
|
6
|
+
const PROVIDER_WAIT_HEADER = "x-agent-smith-provider-wait";
|
|
7
|
+
const PROMOTION_DECLINED_HEADER = "x-agent-smith-provider-promotion-declined";
|
|
8
|
+
const PROMOTION_DETAILS_HEADER = "x-agent-smith-provider-promotion-details";
|
|
9
|
+
const SERVED_PROVIDER_HEADER = "x-agent-smith-served-provider";
|
|
10
|
+
const SERVED_MODEL_HEADER = "x-agent-smith-served-model";
|
|
11
|
+
const WAIT = "Wait";
|
|
12
|
+
|
|
13
|
+
const CATEGORIES = new Set([
|
|
14
|
+
"five-hour-limit",
|
|
15
|
+
"weekly-limit",
|
|
16
|
+
"request-rate",
|
|
17
|
+
"token-allocation",
|
|
18
|
+
"burst",
|
|
19
|
+
"dynamic-throughput",
|
|
20
|
+
"temporary-capacity",
|
|
21
|
+
"unknown",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
function headerValue(headers, name) {
|
|
25
|
+
if (headers && typeof headers.get === "function") return headers.get(name);
|
|
26
|
+
if (!headers || typeof headers !== "object") return null;
|
|
27
|
+
const found = Object.entries(headers).find(
|
|
28
|
+
([key]) => key.toLowerCase() === name,
|
|
29
|
+
);
|
|
30
|
+
return typeof found?.[1] === "string" ? found[1] : null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function classifyCapacityResponse(status, headers) {
|
|
34
|
+
return (
|
|
35
|
+
(status === 409 || status === 429) &&
|
|
36
|
+
headerValue(headers, CAPACITY_HEADER) === "v1"
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function isChoice(value) {
|
|
41
|
+
return (
|
|
42
|
+
typeof value === "object" &&
|
|
43
|
+
value !== null &&
|
|
44
|
+
typeof value.provider === "string" &&
|
|
45
|
+
value.provider.length > 0 &&
|
|
46
|
+
typeof value.model === "string" &&
|
|
47
|
+
value.model.length > 0
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function isWait(value) {
|
|
52
|
+
return (
|
|
53
|
+
value === null ||
|
|
54
|
+
(typeof value === "object" &&
|
|
55
|
+
value !== null &&
|
|
56
|
+
Number.isSafeInteger(value.retryAt) &&
|
|
57
|
+
value.retryAt > 0)
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function decodeHeaderJson(headers, name) {
|
|
62
|
+
const encoded = headerValue(headers, name);
|
|
63
|
+
if (!encoded || encoded.length > 16_384) return null;
|
|
64
|
+
try {
|
|
65
|
+
const base64 = encoded.replaceAll("-", "+").replaceAll("_", "/");
|
|
66
|
+
const padded = base64.padEnd(Math.ceil(base64.length / 4) * 4, "=");
|
|
67
|
+
return JSON.parse(
|
|
68
|
+
new TextDecoder().decode(
|
|
69
|
+
Uint8Array.from(atob(padded), (character) => character.charCodeAt(0)),
|
|
70
|
+
),
|
|
71
|
+
);
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function readCapacityDetails(headers) {
|
|
78
|
+
const value = decodeHeaderJson(headers, CAPACITY_DETAILS_HEADER);
|
|
79
|
+
try {
|
|
80
|
+
if (
|
|
81
|
+
typeof value !== "object" ||
|
|
82
|
+
value === null ||
|
|
83
|
+
typeof value.blockedProvider !== "string" ||
|
|
84
|
+
value.blockedProvider.length === 0 ||
|
|
85
|
+
typeof value.category !== "string" ||
|
|
86
|
+
!CATEGORIES.has(value.category) ||
|
|
87
|
+
!Array.isArray(value.choices) ||
|
|
88
|
+
!value.choices.every(isChoice) ||
|
|
89
|
+
!isWait(value.wait)
|
|
90
|
+
) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
if (
|
|
94
|
+
value.kind !== undefined &&
|
|
95
|
+
value.kind !== "capacity" &&
|
|
96
|
+
value.kind !== "manual"
|
|
97
|
+
) {
|
|
98
|
+
return null;
|
|
99
|
+
}
|
|
100
|
+
return value;
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function readPromotionDetails(headers) {
|
|
107
|
+
const value = decodeHeaderJson(headers, PROMOTION_DETAILS_HEADER);
|
|
108
|
+
return typeof value === "object" &&
|
|
109
|
+
value !== null &&
|
|
110
|
+
Array.isArray(value.choices) &&
|
|
111
|
+
value.choices.length > 0 &&
|
|
112
|
+
value.choices.every(isChoice)
|
|
113
|
+
? value
|
|
114
|
+
: null;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function withoutHeader(headers, name) {
|
|
118
|
+
return Object.fromEntries(
|
|
119
|
+
Object.entries(headers ?? {}).filter(([key]) => key.toLowerCase() !== name),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function choiceHeaders(
|
|
124
|
+
init,
|
|
125
|
+
provider,
|
|
126
|
+
declinePromotion = false,
|
|
127
|
+
openChoiceMenu = false,
|
|
128
|
+
waitForProvider = false,
|
|
129
|
+
) {
|
|
130
|
+
const headers = withoutHeader(
|
|
131
|
+
withoutHeader(
|
|
132
|
+
withoutHeader(init?.headers, PROVIDER_CHOICE_HEADER),
|
|
133
|
+
PROVIDER_CHOICE_MENU_HEADER,
|
|
134
|
+
),
|
|
135
|
+
PROMOTION_DECLINED_HEADER,
|
|
136
|
+
);
|
|
137
|
+
headers[INTERACTIVE_CHOICE_HEADER] = "v1";
|
|
138
|
+
if (provider) headers[PROVIDER_CHOICE_HEADER] = provider;
|
|
139
|
+
if (declinePromotion) headers[PROMOTION_DECLINED_HEADER] = "v1";
|
|
140
|
+
if (openChoiceMenu) headers[PROVIDER_CHOICE_MENU_HEADER] = "v1";
|
|
141
|
+
if (waitForProvider) headers[PROVIDER_WAIT_HEADER] = "v1";
|
|
142
|
+
return { ...init, headers };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function displayProvider(provider) {
|
|
146
|
+
return provider
|
|
147
|
+
.replace(/-claude$/, "")
|
|
148
|
+
.split(/[-_]/)
|
|
149
|
+
.filter(Boolean)
|
|
150
|
+
.map((part) => part.slice(0, 1).toUpperCase() + part.slice(1))
|
|
151
|
+
.join(" ");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function choiceLabel(choice) {
|
|
155
|
+
return `${displayProvider(choice.provider)} · ${choice.model}`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function categoryLabel(category) {
|
|
159
|
+
return (
|
|
160
|
+
{
|
|
161
|
+
"five-hour-limit": "a five-hour session limit",
|
|
162
|
+
"weekly-limit": "a weekly limit",
|
|
163
|
+
"request-rate": "request rate",
|
|
164
|
+
"token-allocation": "token allocation",
|
|
165
|
+
burst: "traffic burst",
|
|
166
|
+
"dynamic-throughput": "temporary provider capacity",
|
|
167
|
+
"temporary-capacity": "temporary provider capacity",
|
|
168
|
+
unknown: "an unknown provider limit",
|
|
169
|
+
}[category] ?? "an unknown provider limit"
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function servedStatus(response) {
|
|
174
|
+
const provider = headerValue(response.headers, SERVED_PROVIDER_HEADER);
|
|
175
|
+
const model = headerValue(response.headers, SERVED_MODEL_HEADER);
|
|
176
|
+
return provider && model ? `${displayProvider(provider)} · ${model}` : null;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function fetchResponse(result) {
|
|
180
|
+
if (typeof result !== "object" || result === null || !("value" in result)) {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
const response = result.value;
|
|
184
|
+
return typeof response === "object" && response !== null ? response : null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async function chooseProvider(details, ask) {
|
|
188
|
+
return await chooseChoices(
|
|
189
|
+
details.kind === "manual"
|
|
190
|
+
? "Choose provider for this conversation."
|
|
191
|
+
: `${displayProvider(details.blockedProvider)} cannot serve this turn because of ${categoryLabel(details.category)}. Switch provider?`,
|
|
192
|
+
details.choices,
|
|
193
|
+
ask,
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async function chooseChoices(question, choices, ask) {
|
|
198
|
+
let start = 0;
|
|
199
|
+
while (start < choices.length) {
|
|
200
|
+
const remaining = choices.length - start;
|
|
201
|
+
const take = remaining > 3 ? 2 : 3;
|
|
202
|
+
const page = choices.slice(start, start + take);
|
|
203
|
+
const hasMore = start + page.length < choices.length;
|
|
204
|
+
const labels = page.map(choiceLabel);
|
|
205
|
+
const answer = await ask(question, [
|
|
206
|
+
...labels,
|
|
207
|
+
...(hasMore ? ["More providers"] : []),
|
|
208
|
+
WAIT,
|
|
209
|
+
]);
|
|
210
|
+
if (answer === WAIT) return null;
|
|
211
|
+
if (answer === "More providers" && hasMore) {
|
|
212
|
+
start += page.length;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const choice = page.find((candidate) => choiceLabel(candidate) === answer);
|
|
216
|
+
if (choice) return choice;
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
return undefined;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export async function choosePromotion(choices, ask) {
|
|
223
|
+
return await chooseChoices(
|
|
224
|
+
"A higher-ranked provider has an eligible Agent-smith account. Switch this conversation?",
|
|
225
|
+
choices,
|
|
226
|
+
ask,
|
|
227
|
+
);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* The transport-neutral core of the Claude Code hook. It starts every eligible
|
|
232
|
+
* pool request in interactive mode, asks only after a versioned capacity
|
|
233
|
+
* response, then sends the unchanged request once more with the chosen provider.
|
|
234
|
+
*/
|
|
235
|
+
export async function retryCapacityBlockedRequest({
|
|
236
|
+
init,
|
|
237
|
+
send,
|
|
238
|
+
ask,
|
|
239
|
+
setStatus,
|
|
240
|
+
initialChoice,
|
|
241
|
+
declinePromotion = false,
|
|
242
|
+
openChoiceMenu = false,
|
|
243
|
+
onPromotion,
|
|
244
|
+
}) {
|
|
245
|
+
let result = await send(
|
|
246
|
+
choiceHeaders(init, initialChoice, declinePromotion, openChoiceMenu),
|
|
247
|
+
);
|
|
248
|
+
let response = fetchResponse(result);
|
|
249
|
+
if (!response) return result;
|
|
250
|
+
const initialStatus = servedStatus(response);
|
|
251
|
+
if (initialStatus) setStatus(initialStatus);
|
|
252
|
+
if (!classifyCapacityResponse(response.status, response.headers)) {
|
|
253
|
+
const promotion = response.ok
|
|
254
|
+
? readPromotionDetails(response.headers)
|
|
255
|
+
: null;
|
|
256
|
+
if (promotion) onPromotion?.(promotion.choices);
|
|
257
|
+
return result;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
const details = readCapacityDetails(response.headers);
|
|
261
|
+
if (!details) return result;
|
|
262
|
+
if (details.choices.length === 0) {
|
|
263
|
+
return details.kind === "manual" ? await send(choiceHeaders(init)) : result;
|
|
264
|
+
}
|
|
265
|
+
let choice;
|
|
266
|
+
try {
|
|
267
|
+
choice = await chooseProvider(details, ask);
|
|
268
|
+
} catch {
|
|
269
|
+
setStatus(
|
|
270
|
+
"Agent-smith: provider choice dismissed; this turn remains blocked.",
|
|
271
|
+
);
|
|
272
|
+
return result;
|
|
273
|
+
}
|
|
274
|
+
if (choice === undefined) {
|
|
275
|
+
setStatus(
|
|
276
|
+
"Agent-smith: choose one listed provider or Wait; this turn remains blocked.",
|
|
277
|
+
);
|
|
278
|
+
return result;
|
|
279
|
+
}
|
|
280
|
+
if (choice === null && details.kind === "manual") {
|
|
281
|
+
setStatus("Agent-smith: provider choice left unchanged.");
|
|
282
|
+
return result;
|
|
283
|
+
}
|
|
284
|
+
if (choice === null && details.wait === null) {
|
|
285
|
+
setStatus(
|
|
286
|
+
"Agent-smith: the provider supplied no retry time, so this turn remains on its current provider.",
|
|
287
|
+
);
|
|
288
|
+
return result;
|
|
289
|
+
}
|
|
290
|
+
result = await send(
|
|
291
|
+
choiceHeaders(
|
|
292
|
+
init,
|
|
293
|
+
choice?.provider,
|
|
294
|
+
false,
|
|
295
|
+
false,
|
|
296
|
+
!choice && details.kind !== "manual",
|
|
297
|
+
),
|
|
298
|
+
);
|
|
299
|
+
response = fetchResponse(result);
|
|
300
|
+
if (!response) return result;
|
|
301
|
+
const status = servedStatus(response);
|
|
302
|
+
if (status) setStatus(status);
|
|
303
|
+
if (!choice) {
|
|
304
|
+
const waitResult = readCapacityDetails(response.headers);
|
|
305
|
+
if (waitResult) {
|
|
306
|
+
setStatus(
|
|
307
|
+
"Agent-smith: it cannot hold this request until the provider retry time, so this turn remains blocked.",
|
|
308
|
+
);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
return result;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
export function isAnthropicPoolRequest(url) {
|
|
315
|
+
try {
|
|
316
|
+
return new URL(url).pathname.startsWith("/v1/pool/anthropic-messages/");
|
|
317
|
+
} catch {
|
|
318
|
+
return false;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import {
|
|
2
|
+
choosePromotion,
|
|
3
|
+
isAnthropicPoolRequest,
|
|
4
|
+
retryCapacityBlockedRequest,
|
|
5
|
+
} from "./provider-choice.js";
|
|
6
|
+
|
|
7
|
+
export function register(on) {
|
|
8
|
+
let promotion = null;
|
|
9
|
+
let promotionDecision = null;
|
|
10
|
+
let promotionReadyAfterTurn = 0;
|
|
11
|
+
let completedMainTurns = 0;
|
|
12
|
+
let openChoiceMenu = false;
|
|
13
|
+
const providerChoiceDecisions = new Map();
|
|
14
|
+
on("session.start", async ($, event, next) => {
|
|
15
|
+
await $.command.register({
|
|
16
|
+
name: "agent-smith-provider",
|
|
17
|
+
description: "Choose a provider for this conversation",
|
|
18
|
+
});
|
|
19
|
+
return await next(event);
|
|
20
|
+
});
|
|
21
|
+
on("command.run", { command: "agent-smith-provider" }, ($) => {
|
|
22
|
+
openChoiceMenu = true;
|
|
23
|
+
$.ui.status("Agent-smith: choose a provider on your next turn.");
|
|
24
|
+
return { isTextDisplayOnly: true };
|
|
25
|
+
});
|
|
26
|
+
on("turn.complete", async (_$, event, next) => {
|
|
27
|
+
if (!event.agentId) completedMainTurns += 1;
|
|
28
|
+
return await next(event);
|
|
29
|
+
});
|
|
30
|
+
on("http.fetch", async ($, event, next) => {
|
|
31
|
+
if (!isAnthropicPoolRequest(event.url)) return next(event);
|
|
32
|
+
let initialChoice;
|
|
33
|
+
const chooseNow = openChoiceMenu;
|
|
34
|
+
openChoiceMenu = false;
|
|
35
|
+
let declinePromotion = false;
|
|
36
|
+
if (
|
|
37
|
+
promotionDecision ||
|
|
38
|
+
(promotion && completedMainTurns >= promotionReadyAfterTurn)
|
|
39
|
+
) {
|
|
40
|
+
promotionDecision ??= choosePromotion(promotion, (question, options) =>
|
|
41
|
+
$.ui.ask(question, { header: "Agent-smith", options }),
|
|
42
|
+
)
|
|
43
|
+
.then((choice) => choice?.provider)
|
|
44
|
+
.catch(() => undefined);
|
|
45
|
+
initialChoice = await promotionDecision;
|
|
46
|
+
declinePromotion = !initialChoice;
|
|
47
|
+
promotion = null;
|
|
48
|
+
promotionDecision = null;
|
|
49
|
+
}
|
|
50
|
+
return await retryCapacityBlockedRequest({
|
|
51
|
+
init: event.init,
|
|
52
|
+
initialChoice,
|
|
53
|
+
declinePromotion,
|
|
54
|
+
openChoiceMenu: chooseNow,
|
|
55
|
+
ask: (question, options) => {
|
|
56
|
+
const key = JSON.stringify([question, options]);
|
|
57
|
+
const existing = providerChoiceDecisions.get(key);
|
|
58
|
+
if (existing) return existing;
|
|
59
|
+
const decision = $.ui
|
|
60
|
+
.ask(question, { header: "Agent-smith", options })
|
|
61
|
+
.finally(() => providerChoiceDecisions.delete(key));
|
|
62
|
+
providerChoiceDecisions.set(key, decision);
|
|
63
|
+
return decision;
|
|
64
|
+
},
|
|
65
|
+
setStatus: (status) => $.ui.status(status),
|
|
66
|
+
onPromotion: (choices) => {
|
|
67
|
+
if (promotion) return;
|
|
68
|
+
promotion = choices;
|
|
69
|
+
promotionReadyAfterTurn = completedMainTurns + 1;
|
|
70
|
+
},
|
|
71
|
+
send: async (init) => await next({ ...event, init }),
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
package/dist/client-launcher.js
CHANGED
|
@@ -1626,7 +1626,7 @@ function sanitizeProviderPools(value) {
|
|
|
1626
1626
|
return pools;
|
|
1627
1627
|
}
|
|
1628
1628
|
function wireForStrategy(strategy3) {
|
|
1629
|
-
return strategy3 === "anthropic-api" || strategy3 === "claude-code" ? "anthropic-messages" : "openai-responses";
|
|
1629
|
+
return strategy3 === "anthropic-api" || strategy3 === "model-studio-anthropic-api" || strategy3 === "claude-code" ? "anthropic-messages" : "openai-responses";
|
|
1630
1630
|
}
|
|
1631
1631
|
function isNonNegativeFiniteNumber(value) {
|
|
1632
1632
|
return isFiniteNumber(value) && value >= 0;
|
|
@@ -1665,6 +1665,12 @@ function sanitizeTeamRouting(value) {
|
|
|
1665
1665
|
result.caller_weekly_max_usage = Object.fromEntries(entries);
|
|
1666
1666
|
}
|
|
1667
1667
|
}
|
|
1668
|
+
if (isRecord(value.caller_provider_fallback)) {
|
|
1669
|
+
const entries = Object.entries(value.caller_provider_fallback);
|
|
1670
|
+
if (entries.length <= MAX_CALLERS && entries.every(([caller, fallback]) => CALLER_ID_PATTERN.test(caller) && fallback === "ordered")) {
|
|
1671
|
+
result.caller_provider_fallback = Object.fromEntries(entries);
|
|
1672
|
+
}
|
|
1673
|
+
}
|
|
1668
1674
|
if (isNonNegativeFiniteNumber(value.admission_cap)) {
|
|
1669
1675
|
result.admission_cap = Math.floor(value.admission_cap);
|
|
1670
1676
|
}
|
|
@@ -1719,6 +1725,7 @@ var init_src4 = __esm(() => {
|
|
|
1719
1725
|
log2 = new Logger("Config");
|
|
1720
1726
|
PROVIDER_STRATEGIES = [
|
|
1721
1727
|
"anthropic-api",
|
|
1728
|
+
"model-studio-anthropic-api",
|
|
1722
1729
|
"openai-api",
|
|
1723
1730
|
"claude-code",
|
|
1724
1731
|
"codex"
|
|
@@ -1742,6 +1749,7 @@ var init_src4 = __esm(() => {
|
|
|
1742
1749
|
weeklyMaxUsage: 0.95,
|
|
1743
1750
|
weeklyMaxUsageByEmail: {},
|
|
1744
1751
|
callerWeeklyMaxUsage: {},
|
|
1752
|
+
callerProviderFallback: {},
|
|
1745
1753
|
admissionCap: 5,
|
|
1746
1754
|
admissionQueueLimit: 25,
|
|
1747
1755
|
admissionWaitMs: 30000,
|
|
@@ -1905,6 +1913,7 @@ var init_src4 = __esm(() => {
|
|
|
1905
1913
|
weeklyMaxUsage: file?.weekly_max_usage ?? TEAM_ROUTING_DEFAULTS.weeklyMaxUsage,
|
|
1906
1914
|
weeklyMaxUsageByEmail: { ...file?.weekly_max_usage_by_email },
|
|
1907
1915
|
callerWeeklyMaxUsage: { ...file?.caller_weekly_max_usage },
|
|
1916
|
+
callerProviderFallback: { ...file?.caller_provider_fallback },
|
|
1908
1917
|
admissionCap: file?.admission_cap ?? TEAM_ROUTING_DEFAULTS.admissionCap,
|
|
1909
1918
|
admissionQueueLimit: file?.admission_queue_limit ?? TEAM_ROUTING_DEFAULTS.admissionQueueLimit,
|
|
1910
1919
|
admissionWaitMs: file?.admission_wait_ms ?? TEAM_ROUTING_DEFAULTS.admissionWaitMs,
|
|
@@ -1975,6 +1984,7 @@ var init_src4 = __esm(() => {
|
|
|
1975
1984
|
"weekly_max_usage",
|
|
1976
1985
|
"weekly_max_usage_by_email",
|
|
1977
1986
|
"caller_weekly_max_usage",
|
|
1987
|
+
"caller_provider_fallback",
|
|
1978
1988
|
"admission_cap",
|
|
1979
1989
|
"admission_queue_limit",
|
|
1980
1990
|
"admission_wait_ms",
|
|
@@ -2005,6 +2015,11 @@ var init_src4 = __esm(() => {
|
|
|
2005
2015
|
...oldValue.caller_weekly_max_usage
|
|
2006
2016
|
};
|
|
2007
2017
|
}
|
|
2018
|
+
if (!("caller_provider_fallback" in value) && oldValue?.caller_provider_fallback) {
|
|
2019
|
+
sanitized.caller_provider_fallback = {
|
|
2020
|
+
...oldValue.caller_provider_fallback
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
2008
2023
|
this.persistStrictly("team_routing", sanitized);
|
|
2009
2024
|
}
|
|
2010
2025
|
allowAccountUuid(accountUuid) {
|
|
@@ -2144,9 +2159,11 @@ import {
|
|
|
2144
2159
|
accessSync,
|
|
2145
2160
|
chmodSync as chmodSync2,
|
|
2146
2161
|
constants,
|
|
2162
|
+
copyFileSync,
|
|
2147
2163
|
existsSync as existsSync3,
|
|
2148
2164
|
mkdirSync as mkdirSync3,
|
|
2149
2165
|
readFileSync as readFileSync2,
|
|
2166
|
+
rmSync as rmSync2,
|
|
2150
2167
|
statSync as statSync3,
|
|
2151
2168
|
unlinkSync as unlinkSync2,
|
|
2152
2169
|
writeFileSync as writeFileSync2
|
|
@@ -2202,7 +2219,37 @@ function claudeProxyCredential(keepClaudeLogin) {
|
|
|
2202
2219
|
export ANTHROPIC_CUSTOM_HEADERS="\${ANTHROPIC_CUSTOM_HEADERS:+$ANTHROPIC_CUSTOM_HEADERS
|
|
2203
2220
|
}x-api-key: $AGENT_SMITH_PROXY_KEY"`;
|
|
2204
2221
|
}
|
|
2205
|
-
function
|
|
2222
|
+
function claudeCompanionSourceDir() {
|
|
2223
|
+
const bundled = join5(import.meta.dir, "claude-companion");
|
|
2224
|
+
if (existsSync3(join5(bundled, ".claude-plugin", "plugin.json"))) {
|
|
2225
|
+
return bundled;
|
|
2226
|
+
}
|
|
2227
|
+
const source = join5(import.meta.dir, "..", "claude-companion");
|
|
2228
|
+
if (existsSync3(join5(source, ".claude-plugin", "plugin.json"))) {
|
|
2229
|
+
return source;
|
|
2230
|
+
}
|
|
2231
|
+
throw new Error("Agent-smith Claude companion files are missing");
|
|
2232
|
+
}
|
|
2233
|
+
function installClaudeCompanion(binDir) {
|
|
2234
|
+
const sourceDir = claudeCompanionSourceDir();
|
|
2235
|
+
const targetDir = join5(binDir, CLAUDE_COMPANION_DIRNAME);
|
|
2236
|
+
rmSync2(targetDir, { force: true, recursive: true });
|
|
2237
|
+
for (const relativePath of CLAUDE_COMPANION_FILES) {
|
|
2238
|
+
const target = join5(targetDir, relativePath);
|
|
2239
|
+
mkdirSync3(dirname2(target), { recursive: true, mode: 448 });
|
|
2240
|
+
copyFileSync(join5(sourceDir, relativePath), target, constants.COPYFILE_FICLONE);
|
|
2241
|
+
chmodSync2(target, 384);
|
|
2242
|
+
}
|
|
2243
|
+
for (const directory of [
|
|
2244
|
+
targetDir,
|
|
2245
|
+
join5(targetDir, ".claude-plugin"),
|
|
2246
|
+
join5(targetDir, "hooks")
|
|
2247
|
+
]) {
|
|
2248
|
+
chmodSync2(directory, 448);
|
|
2249
|
+
}
|
|
2250
|
+
return targetDir;
|
|
2251
|
+
}
|
|
2252
|
+
function claudeWrapper(claudePath, baseUrl, proxyKeyFile, keepClaudeLogin, companionDir) {
|
|
2206
2253
|
return `#!/bin/sh
|
|
2207
2254
|
# Generated by agent-smith. This file delegates to the real Claude Code binary.
|
|
2208
2255
|
unset AGENT_SMITH_ADMIN_KEY
|
|
@@ -2212,7 +2259,7 @@ ${proxyKeySetup(proxyKeyFile)}
|
|
|
2212
2259
|
${claudeProxyCredential(keepClaudeLogin)}
|
|
2213
2260
|
unset AGENT_SMITH_PROXY_KEY
|
|
2214
2261
|
export ANTHROPIC_BASE_URL=${shellQuote(baseUrl)}
|
|
2215
|
-
exec ${shellQuote(claudePath)} "$@"
|
|
2262
|
+
exec ${shellQuote(claudePath)} --plugin-dir ${shellQuote(companionDir)} "$@"
|
|
2216
2263
|
`;
|
|
2217
2264
|
}
|
|
2218
2265
|
function codexWrapper(codexPath, baseUrl, proxyKeyFile) {
|
|
@@ -2249,9 +2296,10 @@ async function installClientWrappers(options) {
|
|
|
2249
2296
|
if (target === join5(options.binDir, tool))
|
|
2250
2297
|
throw new Error("A client launcher cannot call itself");
|
|
2251
2298
|
const wrapper = join5(options.binDir, tool);
|
|
2299
|
+
const companionDir = tool === "claude" ? installClaudeCompanion(options.binDir) : "";
|
|
2252
2300
|
writeFileSync2(wrapper, `#!/bin/sh
|
|
2253
2301
|
${CONFIG_CLIENT_MARKER}
|
|
2254
|
-
exec ${[process.execPath, launcher, options.clientConfigPath, tool, target].map(shellQuote).join(" ")} "$@"
|
|
2302
|
+
exec ${[process.execPath, launcher, options.clientConfigPath, tool, target, companionDir].map(shellQuote).join(" ")} "$@"
|
|
2255
2303
|
`, { encoding: "utf8", mode: 448 });
|
|
2256
2304
|
chmodSync2(wrapper, 448);
|
|
2257
2305
|
}
|
|
@@ -2264,7 +2312,8 @@ exec ${[process.execPath, launcher, options.clientConfigPath, tool, target].map(
|
|
|
2264
2312
|
const proxyKeyFile = options.proxyKeyFile ? validateProxyKeyFile(options.proxyKeyFile) : undefined;
|
|
2265
2313
|
if (options.claudePath) {
|
|
2266
2314
|
const claude = join5(options.binDir, "claude");
|
|
2267
|
-
|
|
2315
|
+
const companionDir = installClaudeCompanion(options.binDir);
|
|
2316
|
+
writeFileSync2(claude, claudeWrapper(options.claudePath, proxyBaseUrl(options.port, options.baseUrl, "claude-code", "anthropic-messages", options.providerPoolWires), proxyKeyFile, options.keepClaudeLogin === true, companionDir), {
|
|
2268
2317
|
encoding: "utf8",
|
|
2269
2318
|
mode: 448
|
|
2270
2319
|
});
|
|
@@ -2354,7 +2403,7 @@ function configClientWrappersInstalled(paths = {}) {
|
|
|
2354
2403
|
const files = ["claude", "codex"].map((name) => join5(binDir, name)).filter(existsSync3);
|
|
2355
2404
|
return installedRoutingStatus(paths).enabled && files.length > 0 && files.every((path) => readFileSync2(path, "utf8").includes(CONFIG_CLIENT_MARKER));
|
|
2356
2405
|
}
|
|
2357
|
-
async function launchConfiguredClient(configPath, tool, target, args) {
|
|
2406
|
+
async function launchConfiguredClient(configPath, tool, target, claudeCompanionDir, args) {
|
|
2358
2407
|
if (tool !== "claude" && tool !== "codex")
|
|
2359
2408
|
throw new Error("Unknown Agent Smith client");
|
|
2360
2409
|
const client = readClientRouting(configPath);
|
|
@@ -2368,7 +2417,7 @@ async function launchConfiguredClient(configPath, tool, target, args) {
|
|
|
2368
2417
|
const keyFile = validateProxyKeyFile(client.proxyKeyFile);
|
|
2369
2418
|
const wires = Object.keys(new Config(configPath).getProviderPools());
|
|
2370
2419
|
const base = proxyBaseUrl(8080, client.baseUrl, tool === "claude" ? "claude-code" : "codex", tool === "claude" ? "anthropic-messages" : "openai-responses", wires);
|
|
2371
|
-
const script = tool === "claude" ? claudeWrapper(target, base, keyFile, client.keepClaudeLogin === true) : codexWrapper(target, base, keyFile);
|
|
2420
|
+
const script = tool === "claude" ? claudeWrapper(target, base, keyFile, client.keepClaudeLogin === true, claudeCompanionDir) : codexWrapper(target, base, keyFile);
|
|
2372
2421
|
command = ["/bin/sh", "-c", script, "--", ...args];
|
|
2373
2422
|
}
|
|
2374
2423
|
const child = Bun.spawn(command, {
|
|
@@ -2444,6 +2493,10 @@ function disableLocalClientRouting(paths = {}) {
|
|
|
2444
2493
|
if (existsSync3(wrapper))
|
|
2445
2494
|
unlinkSync2(wrapper);
|
|
2446
2495
|
}
|
|
2496
|
+
rmSync2(join5(binDir, CLAUDE_COMPANION_DIRNAME), {
|
|
2497
|
+
force: true,
|
|
2498
|
+
recursive: true
|
|
2499
|
+
});
|
|
2447
2500
|
if (existsSync3(statePath))
|
|
2448
2501
|
unlinkSync2(statePath);
|
|
2449
2502
|
return getLocalRoutingStatus({ binDir, shellProfile, statePath });
|
|
@@ -2470,18 +2523,24 @@ export PATH=${shellDoubleQuote(`${binDir}:$PATH`)}
|
|
|
2470
2523
|
${ROUTING_END}
|
|
2471
2524
|
`;
|
|
2472
2525
|
}
|
|
2473
|
-
var ROUTING_BEGIN = "# >>> agent-smith local routing >>>", ROUTING_END = "# <<< agent-smith local routing <<<", LOCAL_CLIENT_KEY = "agent-smith-local", CONFIG_CLIENT_MARKER = "# agent-smith config-driven client";
|
|
2526
|
+
var ROUTING_BEGIN = "# >>> agent-smith local routing >>>", ROUTING_END = "# <<< agent-smith local routing <<<", LOCAL_CLIENT_KEY = "agent-smith-local", CLAUDE_COMPANION_DIRNAME = "agent-smith-claude-companion", CLAUDE_COMPANION_FILES, CONFIG_CLIENT_MARKER = "# agent-smith config-driven client";
|
|
2474
2527
|
var init_local_routing = __esm(() => {
|
|
2475
2528
|
init_src4();
|
|
2529
|
+
CLAUDE_COMPANION_FILES = [
|
|
2530
|
+
".claude-plugin/plugin.json",
|
|
2531
|
+
"hooks/hooks.json",
|
|
2532
|
+
"hooks/provider-choice.js",
|
|
2533
|
+
"hooks/register.js"
|
|
2534
|
+
];
|
|
2476
2535
|
});
|
|
2477
2536
|
|
|
2478
2537
|
// src/core/client-launcher.ts
|
|
2479
2538
|
init_local_routing();
|
|
2480
2539
|
try {
|
|
2481
|
-
const [configPath, tool, target, ...args] = process.argv.slice(2);
|
|
2482
|
-
if (!configPath || !tool || !target)
|
|
2540
|
+
const [configPath, tool, target, claudeCompanionDir, ...args] = process.argv.slice(2);
|
|
2541
|
+
if (!configPath || !tool || !target || claudeCompanionDir === undefined)
|
|
2483
2542
|
throw new Error("Invalid Agent Smith client launcher");
|
|
2484
|
-
process.exitCode = await launchConfiguredClient(configPath, tool, target, args);
|
|
2543
|
+
process.exitCode = await launchConfiguredClient(configPath, tool, target, claudeCompanionDir, args);
|
|
2485
2544
|
} catch (error) {
|
|
2486
2545
|
console.error(error instanceof Error ? error.message : "Agent Smith could not start the client");
|
|
2487
2546
|
process.exitCode = 1;
|
package/dist/edge.js
CHANGED
|
@@ -36,7 +36,12 @@ var REQUEST_HEADERS = [
|
|
|
36
36
|
"openai-beta",
|
|
37
37
|
"x-client-request-id",
|
|
38
38
|
"x-stainless-retry-count",
|
|
39
|
-
"user-agent"
|
|
39
|
+
"user-agent",
|
|
40
|
+
"x-agent-smith-interactive-provider-choice",
|
|
41
|
+
"x-agent-smith-provider-choice",
|
|
42
|
+
"x-agent-smith-provider-menu",
|
|
43
|
+
"x-agent-smith-provider-wait",
|
|
44
|
+
"x-agent-smith-provider-promotion-declined"
|
|
40
45
|
];
|
|
41
46
|
var RESPONSE_HEADERS = [
|
|
42
47
|
"content-type",
|
|
@@ -52,7 +57,13 @@ var RESPONSE_HEADERS = [
|
|
|
52
57
|
"anthropic-ratelimit-tokens-reset",
|
|
53
58
|
"content-security-policy",
|
|
54
59
|
"referrer-policy",
|
|
55
|
-
"x-frame-options"
|
|
60
|
+
"x-frame-options",
|
|
61
|
+
"x-should-retry",
|
|
62
|
+
"x-agent-smith-provider-capacity",
|
|
63
|
+
"x-agent-smith-provider-capacity-details",
|
|
64
|
+
"x-agent-smith-provider-promotion-details",
|
|
65
|
+
"x-agent-smith-served-provider",
|
|
66
|
+
"x-agent-smith-served-model"
|
|
56
67
|
];
|
|
57
68
|
function isEnrollmentRoute(method, path) {
|
|
58
69
|
return method === "GET" && path === "/pool-enroll" || method === "POST" && [
|
package/dist/https.js
CHANGED
|
@@ -132,7 +132,12 @@ var REQUEST_HEADERS = [
|
|
|
132
132
|
"openai-beta",
|
|
133
133
|
"x-client-request-id",
|
|
134
134
|
"x-stainless-retry-count",
|
|
135
|
-
"user-agent"
|
|
135
|
+
"user-agent",
|
|
136
|
+
"x-agent-smith-interactive-provider-choice",
|
|
137
|
+
"x-agent-smith-provider-choice",
|
|
138
|
+
"x-agent-smith-provider-menu",
|
|
139
|
+
"x-agent-smith-provider-wait",
|
|
140
|
+
"x-agent-smith-provider-promotion-declined"
|
|
136
141
|
];
|
|
137
142
|
var RESPONSE_HEADERS = [
|
|
138
143
|
"content-type",
|
|
@@ -148,7 +153,13 @@ var RESPONSE_HEADERS = [
|
|
|
148
153
|
"anthropic-ratelimit-tokens-reset",
|
|
149
154
|
"content-security-policy",
|
|
150
155
|
"referrer-policy",
|
|
151
|
-
"x-frame-options"
|
|
156
|
+
"x-frame-options",
|
|
157
|
+
"x-should-retry",
|
|
158
|
+
"x-agent-smith-provider-capacity",
|
|
159
|
+
"x-agent-smith-provider-capacity-details",
|
|
160
|
+
"x-agent-smith-provider-promotion-details",
|
|
161
|
+
"x-agent-smith-served-provider",
|
|
162
|
+
"x-agent-smith-served-model"
|
|
152
163
|
];
|
|
153
164
|
function isEnrollmentRoute(method, path) {
|
|
154
165
|
return method === "GET" && path === "/pool-enroll" || method === "POST" && [
|