troxy-cli 1.29.37 → 1.29.39
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "troxy-cli",
|
|
3
|
-
"version": "1.29.
|
|
3
|
+
"version": "1.29.39",
|
|
4
4
|
"description": "A secure control layer for AI agents: policies across payments, messages, logins, destructive actions, model usage, and secrets, all enforceable from the CLI",
|
|
5
5
|
"homepage": "https://troxy.io",
|
|
6
6
|
"bugs": {
|
|
@@ -50,7 +50,7 @@ async function failClosed($, e, next, err) {
|
|
|
50
50
|
//
|
|
51
51
|
// Ported from the proxy's scorer (troxy-tf-live model-proxy/app.py; bands match
|
|
52
52
|
// the production ROUTE_SCORER_BANDS=0.2:haiku,0.6:sonnet,0.8:opus,
|
|
53
|
-
// never-upward rule
|
|
53
|
+
// never-upward rule; the 50k context floor here covers every downward route, not just Haiku). The mod scores each human
|
|
54
54
|
// prompt once with the user's own session via $.model.complete, so there is no
|
|
55
55
|
// Troxy round trip and no credential to carry. One decision per prompt, applied
|
|
56
56
|
// to every step of its turn, so the cache prefix stays stable within a turn.
|
|
@@ -62,7 +62,7 @@ const MODELS = {
|
|
|
62
62
|
};
|
|
63
63
|
const RANK = { haiku: 0, sonnet: 1, opus: 2 };
|
|
64
64
|
const BANDS = [[0.2, 'haiku'], [0.6, 'sonnet'], [0.8, 'opus']];
|
|
65
|
-
const
|
|
65
|
+
const ROUTE_CONTEXT_FLOOR_TOKENS = 50000;
|
|
66
66
|
const SCORE_TIMEOUT_MS = 3000;
|
|
67
67
|
const SCORER_PROMPT_CHARS = 4000;
|
|
68
68
|
const SCORER_SYSTEM = 'You are a routing classifier for a coding-assistant proxy. Read the request below '
|
|
@@ -98,9 +98,9 @@ function pickModel(score, requested, contextTokens) {
|
|
|
98
98
|
for (const [limit, name] of BANDS) {
|
|
99
99
|
if (score <= limit) { if (RANK[name] < RANK[rq]) tier = name; break; }
|
|
100
100
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
101
|
+
// The cache is per model: the first prompt on a new model re-writes the whole
|
|
102
|
+
// conversation, so past the floor a switch costs more than one cheap turn saves.
|
|
103
|
+
if (tier !== rq && contextTokens > ROUTE_CONTEXT_FLOOR_TOKENS) tier = rq;
|
|
104
104
|
return tier === rq ? requested : MODELS[tier];
|
|
105
105
|
}
|
|
106
106
|
|
|
@@ -113,28 +113,24 @@ function troxyCommand(args, paused) {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
|
|
116
|
-
// Session
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
//
|
|
121
|
-
const MODEL_COST = {
|
|
122
|
-
|
|
123
|
-
'claude-sonnet-5-5': 0.6,
|
|
124
|
-
'claude-opus-5-5': 1.0,
|
|
125
|
-
};
|
|
116
|
+
// Session usage tracking, one count per human prompt (not per step).
|
|
117
|
+
// Weights relative to Opus: list prices put Haiku at 0.25 and Sonnet at 0.5; a
|
|
118
|
+
// Pro-plan meter test (30 prompts each) put Haiku at ~0.33. Haiku sits between.
|
|
119
|
+
// ponytail: turn-weighted and ignores cache writes after a model switch, so it
|
|
120
|
+
// is an estimate; weight by r.usage tokens if the number must be exact.
|
|
121
|
+
const MODEL_COST = { haiku: 0.3, sonnet: 0.5, opus: 1.0 };
|
|
122
|
+
const stats = { prompts: 0, routed: 0, cost: 0 }; // cost: actual spend in "requested-model prompts"
|
|
126
123
|
|
|
127
|
-
function
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
if (toCost >= fromCost) return 0; // no savings if routing upward
|
|
131
|
-
return Math.round(((fromCost - toCost) / fromCost) * 100);
|
|
124
|
+
function costRatio(fromModel, toModel) {
|
|
125
|
+
const f = MODEL_COST[tierOf(fromModel)], t = MODEL_COST[tierOf(toModel)];
|
|
126
|
+
return f && t ? t / f : 1;
|
|
132
127
|
}
|
|
133
128
|
|
|
134
|
-
//
|
|
135
|
-
function
|
|
136
|
-
const
|
|
137
|
-
|
|
129
|
+
// e.g. [████████░░░░░░░░░░░░] Troxy extended your usage by 87% · 2 of 3 prompts on a cheaper model
|
|
130
|
+
function usageLine({ prompts, routed, cost }) {
|
|
131
|
+
const extended = cost > 0 ? Math.round((prompts / cost - 1) * 100) : 0;
|
|
132
|
+
const filled = prompts ? Math.round((routed / prompts) * 20) : 0;
|
|
133
|
+
return `[${'█'.repeat(filled)}${'░'.repeat(20 - filled)}] Troxy extended your usage by ${extended}% · ${routed} of ${prompts} prompts on a cheaper model`;
|
|
138
134
|
}
|
|
139
135
|
|
|
140
136
|
// Where the local daemon looks for this session's routing note (see interceptor.js).
|
|
@@ -165,6 +161,10 @@ async function scorePrompt($, e, next) {
|
|
|
165
161
|
{ model: 'haiku', system: SCORER_SYSTEM, prompt: e.text.slice(0, SCORER_PROMPT_CHARS), maxTokens: 8, effort: 'low', timeoutMs: SCORE_TIMEOUT_MS },
|
|
166
162
|
);
|
|
167
163
|
const score = r.isAnswered ? parseScore(r.text) : null;
|
|
164
|
+
$.ui.log(`troxy scorer: ${r.isAnswered ? JSON.stringify(r.text) : r.reason} -> ${score}`, { to: 'debug' });
|
|
165
|
+
stats.prompts += 1;
|
|
166
|
+
stats.cost += 1; // until routeStep says otherwise
|
|
167
|
+
$.ui.status(usageLine(stats));
|
|
168
168
|
if (score !== null) {
|
|
169
169
|
const u = await $.session.usage();
|
|
170
170
|
decision = { score, tokens: estimateTokens(u.context && u.context.tokens, e.text) };
|
|
@@ -177,11 +177,11 @@ async function* routeStep($, e, next) {
|
|
|
177
177
|
if (!decision || e.agentId) return yield* next(e);
|
|
178
178
|
const model = pickModel(decision.score, e.model, decision.tokens);
|
|
179
179
|
if (model !== e.model) {
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
$.ui.status(
|
|
180
|
+
if (!decision.counted) {
|
|
181
|
+
decision.counted = true;
|
|
182
|
+
stats.routed += 1;
|
|
183
|
+
stats.cost += costRatio(e.model, model) - 1;
|
|
184
|
+
$.ui.status(usageLine(stats));
|
|
185
185
|
}
|
|
186
186
|
const sid = await $.session.id();
|
|
187
187
|
const file = modRouteFile($.plugin.root, sid);
|
|
@@ -213,4 +213,4 @@ export function register(on) {
|
|
|
213
213
|
}
|
|
214
214
|
|
|
215
215
|
// Exported for tests only - not part of the mod's runtime contract.
|
|
216
|
-
export const _internal = { toDomainAndFields, guardBash, failClosed, parseScore, pickModel, troxyCommand, modRouteFile, estimateTokens, MODELS };
|
|
216
|
+
export const _internal = { toDomainAndFields, guardBash, failClosed, parseScore, pickModel, troxyCommand, modRouteFile, estimateTokens, MODELS, usageLine, costRatio };
|
|
@@ -31,10 +31,12 @@ test('pickModel: never routes upward', () => {
|
|
|
31
31
|
assert.equal(pickModel(0.4, MODELS.haiku, 1000), MODELS.haiku);
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
test('pickModel:
|
|
35
|
-
assert.equal(pickModel(0.05, MODELS.opus, 60000), MODELS.
|
|
34
|
+
test('pickModel: past the 50k floor nothing routes down, a cold cache would cost more than it saves', () => {
|
|
35
|
+
assert.equal(pickModel(0.05, MODELS.opus, 60000), MODELS.opus);
|
|
36
|
+
assert.equal(pickModel(0.4, MODELS.opus, 60000), MODELS.opus);
|
|
36
37
|
assert.equal(pickModel(0.05, MODELS.sonnet, 60000), MODELS.sonnet);
|
|
37
38
|
assert.equal(pickModel(0.05, MODELS.haiku, 60000), MODELS.haiku);
|
|
39
|
+
assert.equal(pickModel(0.4, MODELS.opus, 50000), MODELS.sonnet);
|
|
38
40
|
});
|
|
39
41
|
|
|
40
42
|
test('pickModel: a model it does not recognise is left alone', () => {
|
|
@@ -71,6 +73,6 @@ test('estimateTokens: counts the prompt being submitted, not just the context so
|
|
|
71
73
|
assert.equal(estimateTokens(30000, 'x'.repeat(40000)), 40000);
|
|
72
74
|
assert.equal(estimateTokens(undefined, 'hi'), 1);
|
|
73
75
|
assert.equal(estimateTokens(5, ''), 5);
|
|
74
|
-
// a first-prompt of ~60k tokens must trip the
|
|
75
|
-
assert.equal(pickModel(0.05, MODELS.opus, estimateTokens(0, 'x'.repeat(240000))), MODELS.
|
|
76
|
+
// a first-prompt of ~60k tokens must trip the floor
|
|
77
|
+
assert.equal(pickModel(0.05, MODELS.opus, estimateTokens(0, 'x'.repeat(240000))), MODELS.opus);
|
|
76
78
|
});
|
|
@@ -62,3 +62,13 @@ test('real subprocess: a real Bash command still runs normally through the mod (
|
|
|
62
62
|
fs.rmSync(workdir, { recursive: true, force: true });
|
|
63
63
|
}
|
|
64
64
|
});
|
|
65
|
+
|
|
66
|
+
test('usageLine: counts every prompt, extension reflects routed share', () => {
|
|
67
|
+
const { usageLine, costRatio, MODELS } = _internal;
|
|
68
|
+
assert.equal(costRatio(MODELS.opus, MODELS.haiku), 0.3);
|
|
69
|
+
assert.equal(costRatio('some-new-model', MODELS.haiku), 1);
|
|
70
|
+
// 3 prompts, 2 routed opus->haiku: cost 1 + 0.3 + 0.3 = 1.6, 3/1.6 = +88%
|
|
71
|
+
assert.match(usageLine({ prompts: 3, routed: 2, cost: 1.6 }), /extended your usage by 88% · 2 of 3 prompts/);
|
|
72
|
+
assert.match(usageLine({ prompts: 3, routed: 0, cost: 3 }), /by 0% · 0 of 3/);
|
|
73
|
+
assert.match(usageLine({ prompts: 0, routed: 0, cost: 0 }), /by 0% · 0 of 0/);
|
|
74
|
+
});
|