@certalis/agent-smith 0.1.17 → 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 +17 -9
- package/dist/bin.js +1140 -186
- 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 +86 -12
- package/dist/edge.js +13 -2
- package/dist/https.js +13 -2
- package/dist/pool.js +1064 -156
- package/dist/post-processor.worker.js +164 -2
- package/dist/server.js +1064 -156
- 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;
|
|
@@ -1659,6 +1659,18 @@ function sanitizeTeamRouting(value) {
|
|
|
1659
1659
|
result.weekly_max_usage_by_email = Object.fromEntries(normalized);
|
|
1660
1660
|
}
|
|
1661
1661
|
}
|
|
1662
|
+
if (isRecord(value.caller_weekly_max_usage)) {
|
|
1663
|
+
const entries = Object.entries(value.caller_weekly_max_usage);
|
|
1664
|
+
if (entries.length <= MAX_CALLERS && entries.every(([caller, cap]) => CALLER_ID_PATTERN.test(caller) && isFiniteNumber(cap) && cap > 0 && cap <= 0.95)) {
|
|
1665
|
+
result.caller_weekly_max_usage = Object.fromEntries(entries);
|
|
1666
|
+
}
|
|
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
|
+
}
|
|
1662
1674
|
if (isNonNegativeFiniteNumber(value.admission_cap)) {
|
|
1663
1675
|
result.admission_cap = Math.floor(value.admission_cap);
|
|
1664
1676
|
}
|
|
@@ -1700,7 +1712,7 @@ function clientRouting(client) {
|
|
|
1700
1712
|
...client?.pool_ssh_host && { poolSshHost: client.pool_ssh_host }
|
|
1701
1713
|
};
|
|
1702
1714
|
}
|
|
1703
|
-
var log2, PROVIDER_STRATEGIES, BUILTIN_PROVIDER_CONFIGS, BUILTIN_PROVIDER_CONFIG_BY_NAME, PROVIDER_NAME_PATTERN, POOL_SSH_HOST_PATTERN, TEAM_ROUTING_DEFAULTS, MAX_ENROLLMENT_EMAILS = 200, MAX_ALERT_VALUE_LENGTH = 512, Config;
|
|
1715
|
+
var log2, PROVIDER_STRATEGIES, BUILTIN_PROVIDER_CONFIGS, BUILTIN_PROVIDER_CONFIG_BY_NAME, PROVIDER_NAME_PATTERN, POOL_SSH_HOST_PATTERN, CALLER_ID_PATTERN, MAX_CALLERS = 128, TEAM_ROUTING_DEFAULTS, MAX_ENROLLMENT_EMAILS = 200, MAX_ALERT_VALUE_LENGTH = 512, Config;
|
|
1704
1716
|
var init_src4 = __esm(() => {
|
|
1705
1717
|
init_src2();
|
|
1706
1718
|
init_src3();
|
|
@@ -1713,6 +1725,7 @@ var init_src4 = __esm(() => {
|
|
|
1713
1725
|
log2 = new Logger("Config");
|
|
1714
1726
|
PROVIDER_STRATEGIES = [
|
|
1715
1727
|
"anthropic-api",
|
|
1728
|
+
"model-studio-anthropic-api",
|
|
1716
1729
|
"openai-api",
|
|
1717
1730
|
"claude-code",
|
|
1718
1731
|
"codex"
|
|
@@ -1726,6 +1739,7 @@ var init_src4 = __esm(() => {
|
|
|
1726
1739
|
BUILTIN_PROVIDER_CONFIG_BY_NAME = new Map(BUILTIN_PROVIDER_CONFIGS.map((provider) => [provider.name, provider]));
|
|
1727
1740
|
PROVIDER_NAME_PATTERN = /^[a-z][a-z0-9-]{0,63}$/;
|
|
1728
1741
|
POOL_SSH_HOST_PATTERN = /^\w[\w.-]{0,127}(@\w[\w.-]{0,127})?$/;
|
|
1742
|
+
CALLER_ID_PATTERN = /^[-a-zA-Z0-9_]{1,64}$/;
|
|
1729
1743
|
TEAM_ROUTING_DEFAULTS = {
|
|
1730
1744
|
enabled: false,
|
|
1731
1745
|
distributionEnabled: true,
|
|
@@ -1734,6 +1748,8 @@ var init_src4 = __esm(() => {
|
|
|
1734
1748
|
maxUsage: 0.9,
|
|
1735
1749
|
weeklyMaxUsage: 0.95,
|
|
1736
1750
|
weeklyMaxUsageByEmail: {},
|
|
1751
|
+
callerWeeklyMaxUsage: {},
|
|
1752
|
+
callerProviderFallback: {},
|
|
1737
1753
|
admissionCap: 5,
|
|
1738
1754
|
admissionQueueLimit: 25,
|
|
1739
1755
|
admissionWaitMs: 30000,
|
|
@@ -1896,6 +1912,8 @@ var init_src4 = __esm(() => {
|
|
|
1896
1912
|
maxUsage: file?.max_usage ?? TEAM_ROUTING_DEFAULTS.maxUsage,
|
|
1897
1913
|
weeklyMaxUsage: file?.weekly_max_usage ?? TEAM_ROUTING_DEFAULTS.weeklyMaxUsage,
|
|
1898
1914
|
weeklyMaxUsageByEmail: { ...file?.weekly_max_usage_by_email },
|
|
1915
|
+
callerWeeklyMaxUsage: { ...file?.caller_weekly_max_usage },
|
|
1916
|
+
callerProviderFallback: { ...file?.caller_provider_fallback },
|
|
1899
1917
|
admissionCap: file?.admission_cap ?? TEAM_ROUTING_DEFAULTS.admissionCap,
|
|
1900
1918
|
admissionQueueLimit: file?.admission_queue_limit ?? TEAM_ROUTING_DEFAULTS.admissionQueueLimit,
|
|
1901
1919
|
admissionWaitMs: file?.admission_wait_ms ?? TEAM_ROUTING_DEFAULTS.admissionWaitMs,
|
|
@@ -1965,6 +1983,8 @@ var init_src4 = __esm(() => {
|
|
|
1965
1983
|
"max_usage",
|
|
1966
1984
|
"weekly_max_usage",
|
|
1967
1985
|
"weekly_max_usage_by_email",
|
|
1986
|
+
"caller_weekly_max_usage",
|
|
1987
|
+
"caller_provider_fallback",
|
|
1968
1988
|
"admission_cap",
|
|
1969
1989
|
"admission_queue_limit",
|
|
1970
1990
|
"admission_wait_ms",
|
|
@@ -1990,6 +2010,16 @@ var init_src4 = __esm(() => {
|
|
|
1990
2010
|
...oldValue.weekly_max_usage_by_email
|
|
1991
2011
|
};
|
|
1992
2012
|
}
|
|
2013
|
+
if (!("caller_weekly_max_usage" in value) && oldValue?.caller_weekly_max_usage) {
|
|
2014
|
+
sanitized.caller_weekly_max_usage = {
|
|
2015
|
+
...oldValue.caller_weekly_max_usage
|
|
2016
|
+
};
|
|
2017
|
+
}
|
|
2018
|
+
if (!("caller_provider_fallback" in value) && oldValue?.caller_provider_fallback) {
|
|
2019
|
+
sanitized.caller_provider_fallback = {
|
|
2020
|
+
...oldValue.caller_provider_fallback
|
|
2021
|
+
};
|
|
2022
|
+
}
|
|
1993
2023
|
this.persistStrictly("team_routing", sanitized);
|
|
1994
2024
|
}
|
|
1995
2025
|
allowAccountUuid(accountUuid) {
|
|
@@ -2129,9 +2159,11 @@ import {
|
|
|
2129
2159
|
accessSync,
|
|
2130
2160
|
chmodSync as chmodSync2,
|
|
2131
2161
|
constants,
|
|
2162
|
+
copyFileSync,
|
|
2132
2163
|
existsSync as existsSync3,
|
|
2133
2164
|
mkdirSync as mkdirSync3,
|
|
2134
2165
|
readFileSync as readFileSync2,
|
|
2166
|
+
rmSync as rmSync2,
|
|
2135
2167
|
statSync as statSync3,
|
|
2136
2168
|
unlinkSync as unlinkSync2,
|
|
2137
2169
|
writeFileSync as writeFileSync2
|
|
@@ -2187,7 +2219,37 @@ function claudeProxyCredential(keepClaudeLogin) {
|
|
|
2187
2219
|
export ANTHROPIC_CUSTOM_HEADERS="\${ANTHROPIC_CUSTOM_HEADERS:+$ANTHROPIC_CUSTOM_HEADERS
|
|
2188
2220
|
}x-api-key: $AGENT_SMITH_PROXY_KEY"`;
|
|
2189
2221
|
}
|
|
2190
|
-
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) {
|
|
2191
2253
|
return `#!/bin/sh
|
|
2192
2254
|
# Generated by agent-smith. This file delegates to the real Claude Code binary.
|
|
2193
2255
|
unset AGENT_SMITH_ADMIN_KEY
|
|
@@ -2197,7 +2259,7 @@ ${proxyKeySetup(proxyKeyFile)}
|
|
|
2197
2259
|
${claudeProxyCredential(keepClaudeLogin)}
|
|
2198
2260
|
unset AGENT_SMITH_PROXY_KEY
|
|
2199
2261
|
export ANTHROPIC_BASE_URL=${shellQuote(baseUrl)}
|
|
2200
|
-
exec ${shellQuote(claudePath)} "$@"
|
|
2262
|
+
exec ${shellQuote(claudePath)} --plugin-dir ${shellQuote(companionDir)} "$@"
|
|
2201
2263
|
`;
|
|
2202
2264
|
}
|
|
2203
2265
|
function codexWrapper(codexPath, baseUrl, proxyKeyFile) {
|
|
@@ -2234,9 +2296,10 @@ async function installClientWrappers(options) {
|
|
|
2234
2296
|
if (target === join5(options.binDir, tool))
|
|
2235
2297
|
throw new Error("A client launcher cannot call itself");
|
|
2236
2298
|
const wrapper = join5(options.binDir, tool);
|
|
2299
|
+
const companionDir = tool === "claude" ? installClaudeCompanion(options.binDir) : "";
|
|
2237
2300
|
writeFileSync2(wrapper, `#!/bin/sh
|
|
2238
2301
|
${CONFIG_CLIENT_MARKER}
|
|
2239
|
-
exec ${[process.execPath, launcher, options.clientConfigPath, tool, target].map(shellQuote).join(" ")} "$@"
|
|
2302
|
+
exec ${[process.execPath, launcher, options.clientConfigPath, tool, target, companionDir].map(shellQuote).join(" ")} "$@"
|
|
2240
2303
|
`, { encoding: "utf8", mode: 448 });
|
|
2241
2304
|
chmodSync2(wrapper, 448);
|
|
2242
2305
|
}
|
|
@@ -2249,7 +2312,8 @@ exec ${[process.execPath, launcher, options.clientConfigPath, tool, target].map(
|
|
|
2249
2312
|
const proxyKeyFile = options.proxyKeyFile ? validateProxyKeyFile(options.proxyKeyFile) : undefined;
|
|
2250
2313
|
if (options.claudePath) {
|
|
2251
2314
|
const claude = join5(options.binDir, "claude");
|
|
2252
|
-
|
|
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), {
|
|
2253
2317
|
encoding: "utf8",
|
|
2254
2318
|
mode: 448
|
|
2255
2319
|
});
|
|
@@ -2339,7 +2403,7 @@ function configClientWrappersInstalled(paths = {}) {
|
|
|
2339
2403
|
const files = ["claude", "codex"].map((name) => join5(binDir, name)).filter(existsSync3);
|
|
2340
2404
|
return installedRoutingStatus(paths).enabled && files.length > 0 && files.every((path) => readFileSync2(path, "utf8").includes(CONFIG_CLIENT_MARKER));
|
|
2341
2405
|
}
|
|
2342
|
-
async function launchConfiguredClient(configPath, tool, target, args) {
|
|
2406
|
+
async function launchConfiguredClient(configPath, tool, target, claudeCompanionDir, args) {
|
|
2343
2407
|
if (tool !== "claude" && tool !== "codex")
|
|
2344
2408
|
throw new Error("Unknown Agent Smith client");
|
|
2345
2409
|
const client = readClientRouting(configPath);
|
|
@@ -2353,7 +2417,7 @@ async function launchConfiguredClient(configPath, tool, target, args) {
|
|
|
2353
2417
|
const keyFile = validateProxyKeyFile(client.proxyKeyFile);
|
|
2354
2418
|
const wires = Object.keys(new Config(configPath).getProviderPools());
|
|
2355
2419
|
const base = proxyBaseUrl(8080, client.baseUrl, tool === "claude" ? "claude-code" : "codex", tool === "claude" ? "anthropic-messages" : "openai-responses", wires);
|
|
2356
|
-
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);
|
|
2357
2421
|
command = ["/bin/sh", "-c", script, "--", ...args];
|
|
2358
2422
|
}
|
|
2359
2423
|
const child = Bun.spawn(command, {
|
|
@@ -2429,6 +2493,10 @@ function disableLocalClientRouting(paths = {}) {
|
|
|
2429
2493
|
if (existsSync3(wrapper))
|
|
2430
2494
|
unlinkSync2(wrapper);
|
|
2431
2495
|
}
|
|
2496
|
+
rmSync2(join5(binDir, CLAUDE_COMPANION_DIRNAME), {
|
|
2497
|
+
force: true,
|
|
2498
|
+
recursive: true
|
|
2499
|
+
});
|
|
2432
2500
|
if (existsSync3(statePath))
|
|
2433
2501
|
unlinkSync2(statePath);
|
|
2434
2502
|
return getLocalRoutingStatus({ binDir, shellProfile, statePath });
|
|
@@ -2455,18 +2523,24 @@ export PATH=${shellDoubleQuote(`${binDir}:$PATH`)}
|
|
|
2455
2523
|
${ROUTING_END}
|
|
2456
2524
|
`;
|
|
2457
2525
|
}
|
|
2458
|
-
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";
|
|
2459
2527
|
var init_local_routing = __esm(() => {
|
|
2460
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
|
+
];
|
|
2461
2535
|
});
|
|
2462
2536
|
|
|
2463
2537
|
// src/core/client-launcher.ts
|
|
2464
2538
|
init_local_routing();
|
|
2465
2539
|
try {
|
|
2466
|
-
const [configPath, tool, target, ...args] = process.argv.slice(2);
|
|
2467
|
-
if (!configPath || !tool || !target)
|
|
2540
|
+
const [configPath, tool, target, claudeCompanionDir, ...args] = process.argv.slice(2);
|
|
2541
|
+
if (!configPath || !tool || !target || claudeCompanionDir === undefined)
|
|
2468
2542
|
throw new Error("Invalid Agent Smith client launcher");
|
|
2469
|
-
process.exitCode = await launchConfiguredClient(configPath, tool, target, args);
|
|
2543
|
+
process.exitCode = await launchConfiguredClient(configPath, tool, target, claudeCompanionDir, args);
|
|
2470
2544
|
} catch (error) {
|
|
2471
2545
|
console.error(error instanceof Error ? error.message : "Agent Smith could not start the client");
|
|
2472
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" && [
|