troxy-cli 1.29.36 → 1.29.38
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.38",
|
|
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": {
|
|
@@ -113,22 +113,24 @@ function troxyCommand(args, paused) {
|
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
|
|
116
|
-
// Session
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
//
|
|
121
|
-
const MODEL_COST = {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
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"
|
|
123
|
+
|
|
124
|
+
function costRatio(fromModel, toModel) {
|
|
125
|
+
const f = MODEL_COST[tierOf(fromModel)], t = MODEL_COST[tierOf(toModel)];
|
|
126
|
+
return f && t ? t / f : 1;
|
|
127
|
+
}
|
|
126
128
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const
|
|
130
|
-
|
|
131
|
-
return
|
|
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`;
|
|
132
134
|
}
|
|
133
135
|
|
|
134
136
|
// Where the local daemon looks for this session's routing note (see interceptor.js).
|
|
@@ -159,6 +161,10 @@ async function scorePrompt($, e, next) {
|
|
|
159
161
|
{ model: 'haiku', system: SCORER_SYSTEM, prompt: e.text.slice(0, SCORER_PROMPT_CHARS), maxTokens: 8, effort: 'low', timeoutMs: SCORE_TIMEOUT_MS },
|
|
160
162
|
);
|
|
161
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));
|
|
162
168
|
if (score !== null) {
|
|
163
169
|
const u = await $.session.usage();
|
|
164
170
|
decision = { score, tokens: estimateTokens(u.context && u.context.tokens, e.text) };
|
|
@@ -171,11 +177,11 @@ async function* routeStep($, e, next) {
|
|
|
171
177
|
if (!decision || e.agentId) return yield* next(e);
|
|
172
178
|
const model = pickModel(decision.score, e.model, decision.tokens);
|
|
173
179
|
if (model !== e.model) {
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
$.ui.
|
|
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));
|
|
179
185
|
}
|
|
180
186
|
const sid = await $.session.id();
|
|
181
187
|
const file = modRouteFile($.plugin.root, sid);
|
|
@@ -197,22 +203,9 @@ export function register(on) {
|
|
|
197
203
|
on('command.run', { command: 'troxy' }, async ($, e) => {
|
|
198
204
|
const r = troxyCommand(e.args, Boolean(await $.store.get('paused')));
|
|
199
205
|
await $.store.set('paused', r.paused);
|
|
206
|
+
if (r.paused) $.ui.status(undefined);
|
|
200
207
|
return { text: r.text };
|
|
201
208
|
});
|
|
202
|
-
// Show session savings bar on every turn
|
|
203
|
-
on('turn.start', async ($, e, next) => {
|
|
204
|
-
const paused = Boolean(await $.store.get('paused'));
|
|
205
|
-
|
|
206
|
-
if (!paused && routingCount > 0) {
|
|
207
|
-
const barLength = 20;
|
|
208
|
-
const filledLength = Math.round((sessionSavingsPercent / 100) * barLength);
|
|
209
|
-
const emptyLength = barLength - filledLength;
|
|
210
|
-
const bar = '█'.repeat(filledLength) + '░'.repeat(emptyLength);
|
|
211
|
-
$.ui.toast(`[${bar}] Session: -${sessionSavingsPercent}% saved`);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
return next(e);
|
|
215
|
-
});
|
|
216
209
|
// Routing fails open: a scorer or hook error must never break the session,
|
|
217
210
|
// it just means the requested model is used as-is.
|
|
218
211
|
on('prompt.submit', scorePrompt).catch(($, e, next) => { decision = null; return next(e); });
|
|
@@ -220,4 +213,4 @@ export function register(on) {
|
|
|
220
213
|
}
|
|
221
214
|
|
|
222
215
|
// Exported for tests only - not part of the mod's runtime contract.
|
|
223
|
-
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 };
|
|
@@ -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
|
+
});
|