@trazum/cli 1.64.0 → 1.66.0
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/dist/gateway-server.d.ts +13 -1
- package/dist/gateway-server.d.ts.map +1 -1
- package/dist/gateway-server.js +30 -3
- package/dist/gateway-server.js.map +1 -1
- package/dist/i18n/en.d.ts.map +1 -1
- package/dist/i18n/en.js +23 -0
- package/dist/i18n/en.js.map +1 -1
- package/dist/i18n/es.d.ts.map +1 -1
- package/dist/i18n/es.js +24 -0
- package/dist/i18n/es.js.map +1 -1
- package/dist/i18n/types.d.ts +13 -0
- package/dist/i18n/types.d.ts.map +1 -1
- package/dist/index.js +74 -3
- package/dist/index.js.map +1 -1
- package/dist/serve.d.ts +13 -1
- package/dist/serve.d.ts.map +1 -1
- package/dist/serve.js +51 -6
- package/dist/serve.js.map +1 -1
- package/package.json +2 -2
- package/src/gateway-server.ts +50 -4
- package/src/i18n/en.ts +27 -0
- package/src/i18n/es.ts +28 -0
- package/src/i18n/types.ts +14 -0
- package/src/index.ts +84 -2
- package/src/serve.ts +71 -13
package/src/serve.ts
CHANGED
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
|
|
26
26
|
import { createServer } from 'node:http';
|
|
27
27
|
import type { IncomingMessage, Server, ServerResponse } from 'node:http';
|
|
28
|
-
import { answerCost } from '@trazum/core';
|
|
29
|
-
import type { CostAnswer, PricingCatalogue } from '@trazum/core';
|
|
28
|
+
import { answerCost, judgeLimits } from '@trazum/core';
|
|
29
|
+
import type { CostAnswer, LimitsConfig, MeasuredPosition, PricingCatalogue, WaiveEntry } from '@trazum/core';
|
|
30
30
|
|
|
31
31
|
/** Compiled in. See the module note: this is the inbound SSRF posture. */
|
|
32
32
|
export const BIND_HOST = '127.0.0.1';
|
|
@@ -54,6 +54,15 @@ export interface ServeContext {
|
|
|
54
54
|
* rather than implying it is current to the second.
|
|
55
55
|
*/
|
|
56
56
|
position: () => { consumedUsd?: number; limitUsd?: number; window?: { fromMs: number; toMs: number } | null };
|
|
57
|
+
/** The `limits` block, when the config carries one. */
|
|
58
|
+
limits?: LimitsConfig;
|
|
59
|
+
/**
|
|
60
|
+
* The measured position for one call's scopes — from an index built once
|
|
61
|
+
* at start, same staleness posture as `position`.
|
|
62
|
+
*/
|
|
63
|
+
measured?: (call: { label?: string; session?: string }) => MeasuredPosition;
|
|
64
|
+
/** The config's `waive` list — a silenced limit answers within, on the record. */
|
|
65
|
+
waivers?: readonly WaiveEntry[];
|
|
57
66
|
}
|
|
58
67
|
|
|
59
68
|
async function readBody(request: IncomingMessage): Promise<string> {
|
|
@@ -112,17 +121,66 @@ export function buildServer(context: ServeContext): Server {
|
|
|
112
121
|
}
|
|
113
122
|
|
|
114
123
|
const position = context.position();
|
|
115
|
-
const
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
124
|
+
const call = {
|
|
125
|
+
model: typeof payload.model === 'string' ? payload.model : undefined,
|
|
126
|
+
inputTokens: typeof payload.inputTokens === 'number' ? payload.inputTokens : undefined,
|
|
127
|
+
outputTokens: typeof payload.outputTokens === 'number' ? payload.outputTokens : undefined,
|
|
128
|
+
basis: (payload.basis === 'heuristic' ? 'heuristic' : 'token-count') as 'heuristic' | 'token-count',
|
|
129
|
+
/**
|
|
130
|
+
* `label` and `session` scope the limits judgement and nothing else.
|
|
131
|
+
* The session value is used to look up measured spend and is never
|
|
132
|
+
* echoed back — the answer's judgement names the scope, not the key.
|
|
133
|
+
*/
|
|
134
|
+
label: typeof payload.label === 'string' && payload.label !== '' ? payload.label : undefined,
|
|
135
|
+
session: typeof payload.session === 'string' && payload.session !== '' ? payload.session : undefined,
|
|
136
|
+
};
|
|
137
|
+
let answer: CostAnswer;
|
|
138
|
+
let policy;
|
|
139
|
+
try {
|
|
140
|
+
answer = answerCost(
|
|
141
|
+
{
|
|
142
|
+
model: call.model,
|
|
143
|
+
inputTokens: call.inputTokens,
|
|
144
|
+
outputTokens: call.outputTokens,
|
|
145
|
+
basis: call.basis,
|
|
146
|
+
...position,
|
|
147
|
+
},
|
|
148
|
+
{ catalogue: context.catalogue },
|
|
149
|
+
);
|
|
150
|
+
/**
|
|
151
|
+
* The limits policy, judged by the same function the gateway and the
|
|
152
|
+
* spend guard call — one judge, three doors. Always present: absent
|
|
153
|
+
* config answers `no-policy` rather than omitting the field, because
|
|
154
|
+
* a missing field and a judged absence are different answers.
|
|
155
|
+
*/
|
|
156
|
+
policy = judgeLimits(
|
|
157
|
+
context.limits,
|
|
158
|
+
context.measured?.({
|
|
159
|
+
...(call.label === undefined ? {} : { label: call.label }),
|
|
160
|
+
...(call.session === undefined ? {} : { session: call.session }),
|
|
161
|
+
}) ?? { dayUsd: null, sessionUsd: null, labelUsd: null },
|
|
162
|
+
call,
|
|
163
|
+
{
|
|
164
|
+
catalogue: context.catalogue,
|
|
165
|
+
...(context.waivers === undefined ? {} : { waivers: context.waivers }),
|
|
166
|
+
},
|
|
167
|
+
);
|
|
168
|
+
} catch (error) {
|
|
169
|
+
/*
|
|
170
|
+
The core refuses a figure that cannot mean what it says — a
|
|
171
|
+
negative token count, most importantly, because a negative estimate
|
|
172
|
+
lowers the projected spend and buys an approval. Before this catch
|
|
173
|
+
existed the refusal was an uncaught throw inside the request
|
|
174
|
+
handler: `{"inputTokens": -5}` took the whole oracle down. A bad
|
|
175
|
+
figure is the caller's error, and it gets the caller's status code.
|
|
176
|
+
*/
|
|
177
|
+
send(response, 400, {
|
|
178
|
+
error: 'bad-request',
|
|
179
|
+
detail: error instanceof Error ? error.message : 'unjudgeable figures',
|
|
180
|
+
});
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
send(response, 200, { ...answer, policy });
|
|
126
184
|
})();
|
|
127
185
|
});
|
|
128
186
|
}
|