promptopskit 0.2.6 → 0.3.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/README.md +66 -0
- package/dist/chunk-HWEVRBKZ.js +387 -0
- package/dist/chunk-HWEVRBKZ.js.map +1 -0
- package/dist/cli/index.js +22 -20
- package/dist/cli/index.js.map +1 -1
- package/dist/index.cjs +399 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -4
- package/dist/index.d.ts +5 -4
- package/dist/index.js +29 -1
- package/dist/index.js.map +1 -1
- package/dist/providers/anthropic.d.cts +2 -2
- package/dist/providers/anthropic.d.ts +2 -2
- package/dist/providers/gemini.d.cts +2 -2
- package/dist/providers/gemini.d.ts +2 -2
- package/dist/providers/openai.d.cts +2 -2
- package/dist/providers/openai.d.ts +2 -2
- package/dist/providers/openrouter.d.cts +2 -2
- package/dist/providers/openrouter.d.ts +2 -2
- package/dist/testing.d.cts +1 -1
- package/dist/testing.d.ts +1 -1
- package/dist/{types-BUzJB5p_.d.cts → types-1zDDD-Ij.d.cts} +1 -1
- package/dist/{types-DM0-tThJ.d.ts → types-CQhlfAnR.d.ts} +1 -1
- package/dist/usagetap/index.cjs +425 -0
- package/dist/usagetap/index.cjs.map +1 -0
- package/dist/usagetap/index.d.cts +147 -0
- package/dist/usagetap/index.d.ts +147 -0
- package/dist/usagetap/index.js +31 -0
- package/dist/usagetap/index.js.map +1 -0
- package/package.json +11 -1
- package/dist/{schema-DJ7C8Q6y.d.cts → schema-DIOA4OiO.d.cts} +42 -42
- package/dist/{schema-DJ7C8Q6y.d.ts → schema-DIOA4OiO.d.ts} +42 -42
package/README.md
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# PromptOpsKit
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/promptopskit)
|
|
4
|
+
[](https://github.com/PredictabilityAtScale/promptopskit/actions/workflows/ci.yml)
|
|
4
5
|
[](LICENSE)
|
|
5
6
|
[](https://nodejs.org)
|
|
6
7
|
|
|
@@ -181,6 +182,71 @@ import { geminiAdapter } from 'promptopskit/gemini';
|
|
|
181
182
|
import { openrouterAdapter } from 'promptopskit/openrouter';
|
|
182
183
|
```
|
|
183
184
|
|
|
185
|
+
## Optional UsageTap Tracking
|
|
186
|
+
|
|
187
|
+
PromptOpsKit can also help you track provider calls with UsageTap.com while keeping the core render API body-only.
|
|
188
|
+
|
|
189
|
+
```typescript
|
|
190
|
+
import { createPromptOpsKit } from 'promptopskit';
|
|
191
|
+
import { createUsageTapClient, runOpenAIWithUsageTap } from 'promptopskit/usagetap';
|
|
192
|
+
|
|
193
|
+
const kit = createPromptOpsKit({ sourceDir: './prompts' });
|
|
194
|
+
const usageTap = createUsageTapClient({ apiKey: process.env.USAGETAP_API_KEY! });
|
|
195
|
+
|
|
196
|
+
const { request } = await kit.renderPrompt({
|
|
197
|
+
path: 'support/reply',
|
|
198
|
+
provider: 'openai',
|
|
199
|
+
variables: {
|
|
200
|
+
user_message: 'How do I reset my password?',
|
|
201
|
+
app_context: 'Account settings page',
|
|
202
|
+
},
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const tracked = await runOpenAIWithUsageTap(usageTap, {
|
|
206
|
+
begin: {
|
|
207
|
+
customerId: 'user_123',
|
|
208
|
+
feature: 'chat.send',
|
|
209
|
+
requested: { standard: true, premium: true, search: true },
|
|
210
|
+
idempotencyKey: 'chat-send-user-123-req-456',
|
|
211
|
+
},
|
|
212
|
+
request,
|
|
213
|
+
entitlementMode: 'apply',
|
|
214
|
+
modelTiers: {
|
|
215
|
+
standard: 'gpt-5.4-mini',
|
|
216
|
+
premium: 'gpt-5.4',
|
|
217
|
+
},
|
|
218
|
+
toolEntitlements: {
|
|
219
|
+
image_tool: 'image',
|
|
220
|
+
web_lookup: 'search',
|
|
221
|
+
},
|
|
222
|
+
invoke: async (requestUsed) => {
|
|
223
|
+
const response = await fetch('https://api.openai.com/v1/chat/completions', {
|
|
224
|
+
method: 'POST',
|
|
225
|
+
headers: {
|
|
226
|
+
'Content-Type': 'application/json',
|
|
227
|
+
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
|
|
228
|
+
},
|
|
229
|
+
body: JSON.stringify(requestUsed.body),
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
return response.json();
|
|
233
|
+
},
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
// tracked.response -> vendor JSON response
|
|
237
|
+
// tracked.begin -> UsageTap call_begin payload
|
|
238
|
+
// tracked.end -> UsageTap call_end payload
|
|
239
|
+
// tracked.requestUsed -> effective request after optional entitlement changes
|
|
240
|
+
// tracked.effectiveUsage -> usage sent to UsageTap
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
Notes:
|
|
244
|
+
- `entitlementMode` defaults to `'off'`. Set it to `'apply'` only when you want UsageTap allowances to mutate a cloned provider request.
|
|
245
|
+
- `runOpenRouterWithUsageTap`, `runAnthropicWithUsageTap`, and `runGeminiWithUsageTap` follow the same pattern.
|
|
246
|
+
- `extractOpenAIUsage`, `extractAnthropicUsage`, and `extractGeminiUsage` are public if you want to manage UsageTap lifecycle yourself.
|
|
247
|
+
|
|
248
|
+
For explicit lifecycle control, use `beginUsageTapCall`, `endUsageTapCall`, or `withUsageTapCall` from `promptopskit/usagetap`. Full documentation: [docs/usagetap.md](./docs/usagetap.md).
|
|
249
|
+
|
|
184
250
|
## Overrides
|
|
185
251
|
|
|
186
252
|
Define environment and tier overrides in front matter. Precedence: **base → environment → tier → runtime**. Scalars and arrays are replaced, not merged.
|
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
// src/usagetap/client.ts
|
|
2
|
+
var DEFAULT_BASE_URL = "https://api.usagetap.com";
|
|
3
|
+
var ACCEPT_HEADER = "application/vnd.usagetap.v1+json";
|
|
4
|
+
function createUsageTapHttpError(status, body) {
|
|
5
|
+
const error = new Error(`UsageTap request failed with status ${status}`);
|
|
6
|
+
error.statusCode = status;
|
|
7
|
+
error.body = body;
|
|
8
|
+
return error;
|
|
9
|
+
}
|
|
10
|
+
function createUsageTapClient(config) {
|
|
11
|
+
const fetchImpl = config.fetch ?? globalThis.fetch;
|
|
12
|
+
if (!fetchImpl) {
|
|
13
|
+
throw new Error("Fetch API is not available. Provide config.fetch when creating the UsageTap client.");
|
|
14
|
+
}
|
|
15
|
+
const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
|
|
16
|
+
async function request(path, body, init = {}) {
|
|
17
|
+
const response = await fetchImpl(`${baseUrl}${path}`, {
|
|
18
|
+
method: "POST",
|
|
19
|
+
headers: {
|
|
20
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
21
|
+
Accept: ACCEPT_HEADER,
|
|
22
|
+
"Content-Type": "application/json"
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify(body),
|
|
25
|
+
signal: init.signal
|
|
26
|
+
});
|
|
27
|
+
const text = await response.text();
|
|
28
|
+
const parsed = text ? JSON.parse(text) : {};
|
|
29
|
+
if (!response.ok) {
|
|
30
|
+
throw createUsageTapHttpError(response.status, parsed);
|
|
31
|
+
}
|
|
32
|
+
return parsed;
|
|
33
|
+
}
|
|
34
|
+
return {
|
|
35
|
+
request,
|
|
36
|
+
beginCall(begin, init) {
|
|
37
|
+
return request("/call_begin", begin, init);
|
|
38
|
+
},
|
|
39
|
+
endCall(end, init) {
|
|
40
|
+
return request("/call_end", end, init);
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/usagetap/lifecycle.ts
|
|
46
|
+
function isObject(value) {
|
|
47
|
+
return typeof value === "object" && value !== null;
|
|
48
|
+
}
|
|
49
|
+
function isInvokeResult(value) {
|
|
50
|
+
return isObject(value) && "result" in value && "usage" in value;
|
|
51
|
+
}
|
|
52
|
+
function getErrorStatusCode(error) {
|
|
53
|
+
if (!isObject(error)) {
|
|
54
|
+
return void 0;
|
|
55
|
+
}
|
|
56
|
+
if (typeof error.statusCode === "number") {
|
|
57
|
+
return error.statusCode;
|
|
58
|
+
}
|
|
59
|
+
if (typeof error.status === "number") {
|
|
60
|
+
return error.status;
|
|
61
|
+
}
|
|
62
|
+
if (isObject(error.response) && typeof error.response.status === "number") {
|
|
63
|
+
return error.response.status;
|
|
64
|
+
}
|
|
65
|
+
return void 0;
|
|
66
|
+
}
|
|
67
|
+
function attachEndErrorCause(thrownError, endError) {
|
|
68
|
+
if (thrownError instanceof Error) {
|
|
69
|
+
if (thrownError.cause === void 0) {
|
|
70
|
+
Object.defineProperty(thrownError, "cause", {
|
|
71
|
+
value: endError,
|
|
72
|
+
configurable: true,
|
|
73
|
+
writable: true
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
throw thrownError;
|
|
77
|
+
}
|
|
78
|
+
throw new AggregateError([thrownError, endError], "Vendor call failed and UsageTap call_end also failed.");
|
|
79
|
+
}
|
|
80
|
+
function defaultUsageTapErrorMapper(error) {
|
|
81
|
+
return {
|
|
82
|
+
code: "VENDOR_ERROR",
|
|
83
|
+
message: String(error)
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function beginUsageTapCall(client, begin, init) {
|
|
87
|
+
return client.beginCall(begin, init);
|
|
88
|
+
}
|
|
89
|
+
function endUsageTapCall(client, end, init) {
|
|
90
|
+
return client.endCall(end, init);
|
|
91
|
+
}
|
|
92
|
+
async function withUsageTapCall(client, options) {
|
|
93
|
+
const begin = await beginUsageTapCall(client, options.begin, { signal: options.signal });
|
|
94
|
+
const onError = options.onError ?? defaultUsageTapErrorMapper;
|
|
95
|
+
let result;
|
|
96
|
+
let usage;
|
|
97
|
+
let thrownError;
|
|
98
|
+
const setUsage = (nextUsage) => {
|
|
99
|
+
usage = { ...usage, ...nextUsage };
|
|
100
|
+
};
|
|
101
|
+
try {
|
|
102
|
+
const invoked = await options.invoke({
|
|
103
|
+
begin,
|
|
104
|
+
setUsage,
|
|
105
|
+
signal: options.signal
|
|
106
|
+
});
|
|
107
|
+
if (isInvokeResult(invoked)) {
|
|
108
|
+
result = invoked.result;
|
|
109
|
+
setUsage(invoked.usage);
|
|
110
|
+
} else {
|
|
111
|
+
result = invoked;
|
|
112
|
+
}
|
|
113
|
+
} catch (error) {
|
|
114
|
+
thrownError = error;
|
|
115
|
+
}
|
|
116
|
+
const effectiveUsage = {
|
|
117
|
+
...usage
|
|
118
|
+
};
|
|
119
|
+
if (thrownError) {
|
|
120
|
+
effectiveUsage.error = effectiveUsage.error ?? onError(thrownError);
|
|
121
|
+
effectiveUsage.responseStatusCode = effectiveUsage.responseStatusCode ?? getErrorStatusCode(thrownError);
|
|
122
|
+
} else {
|
|
123
|
+
effectiveUsage.responseStatusCode = effectiveUsage.responseStatusCode ?? 200;
|
|
124
|
+
}
|
|
125
|
+
let end;
|
|
126
|
+
try {
|
|
127
|
+
end = await endUsageTapCall(client, {
|
|
128
|
+
callId: begin.data.callId,
|
|
129
|
+
...effectiveUsage
|
|
130
|
+
}, { signal: options.signal });
|
|
131
|
+
} catch (endError) {
|
|
132
|
+
if (thrownError) {
|
|
133
|
+
attachEndErrorCause(thrownError, endError);
|
|
134
|
+
}
|
|
135
|
+
throw endError;
|
|
136
|
+
}
|
|
137
|
+
if (thrownError) {
|
|
138
|
+
throw thrownError;
|
|
139
|
+
}
|
|
140
|
+
return {
|
|
141
|
+
result,
|
|
142
|
+
begin,
|
|
143
|
+
end,
|
|
144
|
+
effectiveUsage
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// src/usagetap/providers.ts
|
|
149
|
+
function cloneRequest(request) {
|
|
150
|
+
return structuredClone(request);
|
|
151
|
+
}
|
|
152
|
+
function isObject2(value) {
|
|
153
|
+
return typeof value === "object" && value !== null;
|
|
154
|
+
}
|
|
155
|
+
function isRecordArray(value) {
|
|
156
|
+
return Array.isArray(value) && value.every((item) => isObject2(item));
|
|
157
|
+
}
|
|
158
|
+
function isInvokeResult2(value) {
|
|
159
|
+
return isObject2(value) && "result" in value && "usage" in value;
|
|
160
|
+
}
|
|
161
|
+
function capabilityAllowed(allowed, capability) {
|
|
162
|
+
return allowed[capability] !== false;
|
|
163
|
+
}
|
|
164
|
+
function reasoningLevelOrder(level) {
|
|
165
|
+
switch (level) {
|
|
166
|
+
case "HIGH":
|
|
167
|
+
return 3;
|
|
168
|
+
case "MEDIUM":
|
|
169
|
+
return 2;
|
|
170
|
+
case "LOW":
|
|
171
|
+
return 1;
|
|
172
|
+
default:
|
|
173
|
+
return 0;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function capOpenAIReasoning(body, allowed) {
|
|
177
|
+
if (typeof body.reasoning_effort !== "string") {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
const current = String(body.reasoning_effort).toUpperCase();
|
|
181
|
+
const allowedLevel = allowed.reasoningLevel ?? "HIGH";
|
|
182
|
+
if (allowedLevel === "NONE") {
|
|
183
|
+
delete body.reasoning_effort;
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (reasoningLevelOrder(current) > reasoningLevelOrder(allowedLevel)) {
|
|
187
|
+
body.reasoning_effort = allowedLevel.toLowerCase();
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function capGeminiReasoning(body, allowed) {
|
|
191
|
+
if (allowed.reasoningLevel === "NONE") {
|
|
192
|
+
delete body.thinkingConfig;
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
const budgets = {
|
|
196
|
+
LOW: 1024,
|
|
197
|
+
MEDIUM: 4096,
|
|
198
|
+
HIGH: 8192
|
|
199
|
+
};
|
|
200
|
+
const allowedBudget = allowed.reasoningLevel ? budgets[allowed.reasoningLevel] : void 0;
|
|
201
|
+
if (allowedBudget === void 0) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (!isObject2(body.thinkingConfig)) {
|
|
205
|
+
body.thinkingConfig = { thinkingBudget: allowedBudget };
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const thinkingConfig = body.thinkingConfig;
|
|
209
|
+
if (typeof thinkingConfig.thinkingBudget === "number") {
|
|
210
|
+
thinkingConfig.thinkingBudget = Math.min(thinkingConfig.thinkingBudget, allowedBudget);
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
thinkingConfig.thinkingBudget = allowedBudget;
|
|
214
|
+
}
|
|
215
|
+
function applyModelTier(request, allowed, options) {
|
|
216
|
+
const nextModel = allowed.premium && options.modelTiers?.premium ? options.modelTiers.premium : allowed.standard && options.modelTiers?.standard ? options.modelTiers.standard : void 0;
|
|
217
|
+
if (!nextModel) {
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
request.model = nextModel;
|
|
221
|
+
if (isObject2(request.body) && "model" in request.body) {
|
|
222
|
+
request.body.model = nextModel;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
function filterOpenAITools(tools, allowed, options) {
|
|
226
|
+
return tools.filter((tool) => {
|
|
227
|
+
if (tool.type === "web_search" && allowed.search === false) {
|
|
228
|
+
return false;
|
|
229
|
+
}
|
|
230
|
+
const functionDef = isObject2(tool.function) ? tool.function : void 0;
|
|
231
|
+
const name = typeof functionDef?.name === "string" ? functionDef.name : typeof tool.name === "string" ? tool.name : typeof tool.type === "string" ? tool.type : void 0;
|
|
232
|
+
if (!name) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
const capability = options.toolEntitlements?.[name];
|
|
236
|
+
return capability ? capabilityAllowed(allowed, capability) : true;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function filterAnthropicTools(tools, allowed, options) {
|
|
240
|
+
return tools.filter((tool) => {
|
|
241
|
+
const name = typeof tool.name === "string" ? tool.name : void 0;
|
|
242
|
+
if (!name) {
|
|
243
|
+
return true;
|
|
244
|
+
}
|
|
245
|
+
const capability = options.toolEntitlements?.[name];
|
|
246
|
+
return capability ? capabilityAllowed(allowed, capability) : true;
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function filterGeminiTools(tools, allowed, options) {
|
|
250
|
+
return tools.map((tool) => {
|
|
251
|
+
const declarations = Array.isArray(tool.functionDeclarations) ? tool.functionDeclarations.filter((declaration) => {
|
|
252
|
+
if (!isObject2(declaration) || typeof declaration.name !== "string") {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
const capability = options.toolEntitlements?.[declaration.name];
|
|
256
|
+
return capability ? capabilityAllowed(allowed, capability) : true;
|
|
257
|
+
}) : tool.functionDeclarations;
|
|
258
|
+
return {
|
|
259
|
+
...tool,
|
|
260
|
+
functionDeclarations: declarations
|
|
261
|
+
};
|
|
262
|
+
}).filter((tool) => {
|
|
263
|
+
if (!Array.isArray(tool.functionDeclarations)) {
|
|
264
|
+
return true;
|
|
265
|
+
}
|
|
266
|
+
return tool.functionDeclarations.length > 0;
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
function applyUsageTapEntitlements(request, begin, options = {}) {
|
|
270
|
+
const nextRequest = cloneRequest(request);
|
|
271
|
+
const allowed = begin.data.allowed ?? {};
|
|
272
|
+
applyModelTier(nextRequest, allowed, options);
|
|
273
|
+
if (!isObject2(nextRequest.body)) {
|
|
274
|
+
return nextRequest;
|
|
275
|
+
}
|
|
276
|
+
if (nextRequest.provider === "openai" || nextRequest.provider === "openrouter") {
|
|
277
|
+
capOpenAIReasoning(nextRequest.body, allowed);
|
|
278
|
+
if (isRecordArray(nextRequest.body.tools)) {
|
|
279
|
+
nextRequest.body.tools = filterOpenAITools(nextRequest.body.tools, allowed, options);
|
|
280
|
+
}
|
|
281
|
+
return nextRequest;
|
|
282
|
+
}
|
|
283
|
+
if (nextRequest.provider === "gemini") {
|
|
284
|
+
capGeminiReasoning(nextRequest.body, allowed);
|
|
285
|
+
if (isRecordArray(nextRequest.body.tools)) {
|
|
286
|
+
nextRequest.body.tools = filterGeminiTools(nextRequest.body.tools, allowed, options);
|
|
287
|
+
}
|
|
288
|
+
return nextRequest;
|
|
289
|
+
}
|
|
290
|
+
if (nextRequest.provider === "anthropic" && isRecordArray(nextRequest.body.tools)) {
|
|
291
|
+
nextRequest.body.tools = filterAnthropicTools(nextRequest.body.tools, allowed, options);
|
|
292
|
+
}
|
|
293
|
+
return nextRequest;
|
|
294
|
+
}
|
|
295
|
+
function extractOpenAIUsage(response, meta = {}) {
|
|
296
|
+
const usage = isObject2(response) && isObject2(response.usage) ? response.usage : {};
|
|
297
|
+
const promptDetails = isObject2(usage.prompt_tokens_details) ? usage.prompt_tokens_details : {};
|
|
298
|
+
const completionDetails = isObject2(usage.completion_tokens_details) ? usage.completion_tokens_details : {};
|
|
299
|
+
return {
|
|
300
|
+
modelUsed: meta.modelUsed,
|
|
301
|
+
inputTokens: typeof usage.prompt_tokens === "number" ? usage.prompt_tokens : 0,
|
|
302
|
+
responseTokens: typeof usage.completion_tokens === "number" ? usage.completion_tokens : 0,
|
|
303
|
+
cachedInputTokens: typeof promptDetails.cached_tokens === "number" ? promptDetails.cached_tokens : void 0,
|
|
304
|
+
reasoningTokens: typeof completionDetails.reasoning_tokens === "number" ? completionDetails.reasoning_tokens : void 0,
|
|
305
|
+
responseStatusCode: meta.responseStatusCode ?? 200
|
|
306
|
+
};
|
|
307
|
+
}
|
|
308
|
+
function extractAnthropicUsage(response, meta = {}) {
|
|
309
|
+
const usage = isObject2(response) && isObject2(response.usage) ? response.usage : {};
|
|
310
|
+
return {
|
|
311
|
+
modelUsed: meta.modelUsed,
|
|
312
|
+
inputTokens: typeof usage.input_tokens === "number" ? usage.input_tokens : 0,
|
|
313
|
+
responseTokens: typeof usage.output_tokens === "number" ? usage.output_tokens : 0,
|
|
314
|
+
cachedInputTokens: typeof usage.cache_read_input_tokens === "number" ? usage.cache_read_input_tokens : void 0,
|
|
315
|
+
responseStatusCode: meta.responseStatusCode ?? 200
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
function extractGeminiUsage(response, meta = {}) {
|
|
319
|
+
const usage = isObject2(response) && isObject2(response.usageMetadata) ? response.usageMetadata : {};
|
|
320
|
+
return {
|
|
321
|
+
modelUsed: meta.modelUsed,
|
|
322
|
+
inputTokens: typeof usage.promptTokenCount === "number" ? usage.promptTokenCount : 0,
|
|
323
|
+
responseTokens: typeof usage.candidatesTokenCount === "number" ? usage.candidatesTokenCount : 0,
|
|
324
|
+
cachedInputTokens: typeof usage.cachedContentTokenCount === "number" ? usage.cachedContentTokenCount : void 0,
|
|
325
|
+
reasoningTokens: typeof usage.thoughtsTokenCount === "number" ? usage.thoughtsTokenCount : void 0,
|
|
326
|
+
responseStatusCode: meta.responseStatusCode ?? 200
|
|
327
|
+
};
|
|
328
|
+
}
|
|
329
|
+
async function runProviderWithUsageTap(client, options, defaultExtractor) {
|
|
330
|
+
let requestUsed = cloneRequest(options.request);
|
|
331
|
+
const lifecycle = await withUsageTapCall(client, {
|
|
332
|
+
begin: options.begin,
|
|
333
|
+
signal: options.signal,
|
|
334
|
+
onError: options.onError ?? defaultUsageTapErrorMapper,
|
|
335
|
+
invoke: async ({ begin, setUsage }) => {
|
|
336
|
+
requestUsed = options.entitlementMode === "apply" ? applyUsageTapEntitlements(options.request, begin, options) : cloneRequest(options.request);
|
|
337
|
+
const invoked = await options.invoke(requestUsed);
|
|
338
|
+
if (isInvokeResult2(invoked)) {
|
|
339
|
+
return invoked;
|
|
340
|
+
}
|
|
341
|
+
const response = invoked;
|
|
342
|
+
const usage = (options.extractUsage ?? defaultExtractor)(response, {
|
|
343
|
+
modelUsed: requestUsed.model,
|
|
344
|
+
responseStatusCode: 200
|
|
345
|
+
});
|
|
346
|
+
setUsage(usage);
|
|
347
|
+
return response;
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
return {
|
|
351
|
+
response: lifecycle.result,
|
|
352
|
+
begin: lifecycle.begin,
|
|
353
|
+
end: lifecycle.end,
|
|
354
|
+
requestUsed,
|
|
355
|
+
effectiveUsage: lifecycle.effectiveUsage,
|
|
356
|
+
allowed: lifecycle.begin.data.allowed ?? {}
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
function runOpenAIWithUsageTap(client, options) {
|
|
360
|
+
return runProviderWithUsageTap(client, options, extractOpenAIUsage);
|
|
361
|
+
}
|
|
362
|
+
function runOpenRouterWithUsageTap(client, options) {
|
|
363
|
+
return runProviderWithUsageTap(client, options, extractOpenAIUsage);
|
|
364
|
+
}
|
|
365
|
+
function runAnthropicWithUsageTap(client, options) {
|
|
366
|
+
return runProviderWithUsageTap(client, options, extractAnthropicUsage);
|
|
367
|
+
}
|
|
368
|
+
function runGeminiWithUsageTap(client, options) {
|
|
369
|
+
return runProviderWithUsageTap(client, options, extractGeminiUsage);
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export {
|
|
373
|
+
createUsageTapClient,
|
|
374
|
+
defaultUsageTapErrorMapper,
|
|
375
|
+
beginUsageTapCall,
|
|
376
|
+
endUsageTapCall,
|
|
377
|
+
withUsageTapCall,
|
|
378
|
+
applyUsageTapEntitlements,
|
|
379
|
+
extractOpenAIUsage,
|
|
380
|
+
extractAnthropicUsage,
|
|
381
|
+
extractGeminiUsage,
|
|
382
|
+
runOpenAIWithUsageTap,
|
|
383
|
+
runOpenRouterWithUsageTap,
|
|
384
|
+
runAnthropicWithUsageTap,
|
|
385
|
+
runGeminiWithUsageTap
|
|
386
|
+
};
|
|
387
|
+
//# sourceMappingURL=chunk-HWEVRBKZ.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/usagetap/client.ts","../src/usagetap/lifecycle.ts","../src/usagetap/providers.ts"],"sourcesContent":["import type {\n UsageTapBeginRequest,\n UsageTapBeginResponse,\n UsageTapClient,\n UsageTapClientConfig,\n UsageTapEndRequest,\n UsageTapEndResponse,\n UsageTapRequestInit,\n} from './types.js';\n\nconst DEFAULT_BASE_URL = 'https://api.usagetap.com';\nconst ACCEPT_HEADER = 'application/vnd.usagetap.v1+json';\n\nfunction createUsageTapHttpError(status: number, body: unknown): Error & { statusCode: number; body: unknown } {\n const error = new Error(`UsageTap request failed with status ${status}`) as Error & {\n statusCode: number;\n body: unknown;\n };\n error.statusCode = status;\n error.body = body;\n return error;\n}\n\nexport function createUsageTapClient(config: UsageTapClientConfig): UsageTapClient {\n const fetchImpl = config.fetch ?? globalThis.fetch;\n\n if (!fetchImpl) {\n throw new Error('Fetch API is not available. Provide config.fetch when creating the UsageTap client.');\n }\n\n const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\\/$/, '');\n\n async function request<T>(path: string, body: unknown, init: UsageTapRequestInit = {}): Promise<T> {\n const response = await fetchImpl(`${baseUrl}${path}`, {\n method: 'POST',\n headers: {\n Authorization: `Bearer ${config.apiKey}`,\n Accept: ACCEPT_HEADER,\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(body),\n signal: init.signal,\n });\n\n const text = await response.text();\n const parsed = text ? JSON.parse(text) as unknown : {};\n\n if (!response.ok) {\n throw createUsageTapHttpError(response.status, parsed);\n }\n\n return parsed as T;\n }\n\n return {\n request,\n beginCall(begin: UsageTapBeginRequest, init?: UsageTapRequestInit): Promise<UsageTapBeginResponse> {\n return request<UsageTapBeginResponse>('/call_begin', begin, init);\n },\n endCall(end: UsageTapEndRequest, init?: UsageTapRequestInit): Promise<UsageTapEndResponse> {\n return request<UsageTapEndResponse>('/call_end', end, init);\n },\n };\n}\n","import type {\n UsageTapCallOptions,\n UsageTapCallResult,\n UsageTapClient,\n UsageTapEndRequest,\n UsageTapEndUsage,\n UsageTapErrorPayload,\n UsageTapInvokeResult,\n UsageTapRequestInit,\n} from './types.js';\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nfunction isInvokeResult<TResult>(value: TResult | UsageTapInvokeResult<TResult>): value is UsageTapInvokeResult<TResult> {\n return isObject(value) && 'result' in value && 'usage' in value;\n}\n\nfunction getErrorStatusCode(error: unknown): number | undefined {\n if (!isObject(error)) {\n return undefined;\n }\n\n if (typeof error.statusCode === 'number') {\n return error.statusCode;\n }\n\n if (typeof error.status === 'number') {\n return error.status;\n }\n\n if (isObject(error.response) && typeof error.response.status === 'number') {\n return error.response.status;\n }\n\n return undefined;\n}\n\nfunction attachEndErrorCause(thrownError: unknown, endError: unknown): never {\n if (thrownError instanceof Error) {\n if (thrownError.cause === undefined) {\n Object.defineProperty(thrownError, 'cause', {\n value: endError,\n configurable: true,\n writable: true,\n });\n }\n\n throw thrownError;\n }\n\n throw new AggregateError([thrownError, endError], 'Vendor call failed and UsageTap call_end also failed.');\n}\n\nexport function defaultUsageTapErrorMapper(error: unknown): UsageTapErrorPayload {\n return {\n code: 'VENDOR_ERROR',\n message: String(error),\n };\n}\n\nexport function beginUsageTapCall(client: UsageTapClient, begin: Parameters<UsageTapClient['beginCall']>[0], init?: UsageTapRequestInit) {\n return client.beginCall(begin, init);\n}\n\nexport function endUsageTapCall(client: UsageTapClient, end: UsageTapEndRequest, init?: UsageTapRequestInit) {\n return client.endCall(end, init);\n}\n\nexport async function withUsageTapCall<TResult>(\n client: UsageTapClient,\n options: UsageTapCallOptions<TResult>,\n): Promise<UsageTapCallResult<TResult>> {\n const begin = await beginUsageTapCall(client, options.begin, { signal: options.signal });\n const onError = options.onError ?? defaultUsageTapErrorMapper;\n\n let result: TResult | undefined;\n let usage: UsageTapEndUsage | undefined;\n let thrownError: unknown;\n\n const setUsage = (nextUsage: UsageTapEndUsage): void => {\n usage = { ...usage, ...nextUsage };\n };\n\n try {\n const invoked = await options.invoke({\n begin,\n setUsage,\n signal: options.signal,\n });\n\n if (isInvokeResult(invoked)) {\n result = invoked.result;\n setUsage(invoked.usage);\n } else {\n result = invoked;\n }\n } catch (error) {\n thrownError = error;\n }\n\n const effectiveUsage: UsageTapEndUsage = {\n ...usage,\n };\n\n if (thrownError) {\n effectiveUsage.error = effectiveUsage.error ?? onError(thrownError);\n effectiveUsage.responseStatusCode = effectiveUsage.responseStatusCode ?? getErrorStatusCode(thrownError);\n } else {\n effectiveUsage.responseStatusCode = effectiveUsage.responseStatusCode ?? 200;\n }\n\n let end;\n try {\n end = await endUsageTapCall(client, {\n callId: begin.data.callId,\n ...effectiveUsage,\n }, { signal: options.signal });\n } catch (endError) {\n if (thrownError) {\n attachEndErrorCause(thrownError, endError);\n }\n throw endError;\n }\n\n if (thrownError) {\n throw thrownError;\n }\n\n return {\n result: result as TResult,\n begin,\n end,\n effectiveUsage,\n };\n}\n","import type { ProviderRequest } from '../providers/types.js';\nimport { defaultUsageTapErrorMapper, withUsageTapCall } from './lifecycle.js';\nimport type {\n UsageTapAllowed,\n UsageTapAllowedCapability,\n UsageTapBeginResponse,\n UsageTapClient,\n UsageTapEndUsage,\n UsageTapEntitlementOptions,\n UsageTapProviderRunOptions,\n UsageTapProviderRunResult,\n UsageTapReasoningLevel,\n} from './types.js';\n\nfunction cloneRequest(request: ProviderRequest): ProviderRequest {\n return structuredClone(request);\n}\n\nfunction isObject(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null;\n}\n\nfunction isRecordArray(value: unknown): value is Array<Record<string, unknown>> {\n return Array.isArray(value) && value.every((item) => isObject(item));\n}\n\nfunction isInvokeResult<TResult>(value: unknown): value is { result: TResult; usage: UsageTapEndUsage } {\n return isObject(value) && 'result' in value && 'usage' in value;\n}\n\nfunction capabilityAllowed(allowed: UsageTapAllowed, capability: UsageTapAllowedCapability): boolean {\n return allowed[capability] !== false;\n}\n\nfunction reasoningLevelOrder(level: UsageTapReasoningLevel | undefined): number {\n switch (level) {\n case 'HIGH':\n return 3;\n case 'MEDIUM':\n return 2;\n case 'LOW':\n return 1;\n default:\n return 0;\n }\n}\n\nfunction capOpenAIReasoning(body: Record<string, unknown>, allowed: UsageTapAllowed): void {\n if (typeof body.reasoning_effort !== 'string') {\n return;\n }\n\n const current = String(body.reasoning_effort).toUpperCase() as UsageTapReasoningLevel;\n const allowedLevel = allowed.reasoningLevel ?? 'HIGH';\n\n if (allowedLevel === 'NONE') {\n delete body.reasoning_effort;\n return;\n }\n\n if (reasoningLevelOrder(current) > reasoningLevelOrder(allowedLevel)) {\n body.reasoning_effort = allowedLevel.toLowerCase();\n }\n}\n\nfunction capGeminiReasoning(body: Record<string, unknown>, allowed: UsageTapAllowed): void {\n if (allowed.reasoningLevel === 'NONE') {\n delete body.thinkingConfig;\n return;\n }\n\n const budgets: Record<Exclude<UsageTapReasoningLevel, 'NONE'>, number> = {\n LOW: 1024,\n MEDIUM: 4096,\n HIGH: 8192,\n };\n\n const allowedBudget = allowed.reasoningLevel ? budgets[allowed.reasoningLevel as Exclude<UsageTapReasoningLevel, 'NONE'>] : undefined;\n\n if (allowedBudget === undefined) {\n return;\n }\n\n if (!isObject(body.thinkingConfig)) {\n body.thinkingConfig = { thinkingBudget: allowedBudget };\n return;\n }\n\n const thinkingConfig = body.thinkingConfig;\n\n if (typeof thinkingConfig.thinkingBudget === 'number') {\n thinkingConfig.thinkingBudget = Math.min(thinkingConfig.thinkingBudget, allowedBudget);\n return;\n }\n\n thinkingConfig.thinkingBudget = allowedBudget;\n}\n\nfunction applyModelTier(request: ProviderRequest, allowed: UsageTapAllowed, options: UsageTapEntitlementOptions): void {\n const nextModel = allowed.premium && options.modelTiers?.premium\n ? options.modelTiers.premium\n : allowed.standard && options.modelTiers?.standard\n ? options.modelTiers.standard\n : undefined;\n\n if (!nextModel) {\n return;\n }\n\n request.model = nextModel;\n if (isObject(request.body) && 'model' in request.body) {\n request.body.model = nextModel;\n }\n}\n\nfunction filterOpenAITools(tools: Array<Record<string, unknown>>, allowed: UsageTapAllowed, options: UsageTapEntitlementOptions): Array<Record<string, unknown>> {\n return tools.filter((tool) => {\n if (tool.type === 'web_search' && allowed.search === false) {\n return false;\n }\n\n const functionDef = isObject(tool.function) ? tool.function : undefined;\n const name = typeof functionDef?.name === 'string'\n ? functionDef.name\n : typeof tool.name === 'string'\n ? tool.name\n : typeof tool.type === 'string'\n ? tool.type\n : undefined;\n\n if (!name) {\n return true;\n }\n\n const capability = options.toolEntitlements?.[name];\n return capability ? capabilityAllowed(allowed, capability) : true;\n });\n}\n\nfunction filterAnthropicTools(tools: Array<Record<string, unknown>>, allowed: UsageTapAllowed, options: UsageTapEntitlementOptions): Array<Record<string, unknown>> {\n return tools.filter((tool) => {\n const name = typeof tool.name === 'string' ? tool.name : undefined;\n if (!name) {\n return true;\n }\n const capability = options.toolEntitlements?.[name];\n return capability ? capabilityAllowed(allowed, capability) : true;\n });\n}\n\nfunction filterGeminiTools(tools: Array<Record<string, unknown>>, allowed: UsageTapAllowed, options: UsageTapEntitlementOptions): Array<Record<string, unknown>> {\n return tools\n .map((tool) => {\n const declarations = Array.isArray(tool.functionDeclarations)\n ? tool.functionDeclarations.filter((declaration) => {\n if (!isObject(declaration) || typeof declaration.name !== 'string') {\n return true;\n }\n const capability = options.toolEntitlements?.[declaration.name];\n return capability ? capabilityAllowed(allowed, capability) : true;\n })\n : tool.functionDeclarations;\n\n return {\n ...tool,\n functionDeclarations: declarations,\n };\n })\n .filter((tool) => {\n if (!Array.isArray(tool.functionDeclarations)) {\n return true;\n }\n return tool.functionDeclarations.length > 0;\n });\n}\n\nexport function applyUsageTapEntitlements(\n request: ProviderRequest,\n begin: UsageTapBeginResponse,\n options: UsageTapEntitlementOptions = {},\n): ProviderRequest {\n const nextRequest = cloneRequest(request);\n const allowed = begin.data.allowed ?? {};\n\n applyModelTier(nextRequest, allowed, options);\n\n if (!isObject(nextRequest.body)) {\n return nextRequest;\n }\n\n if (nextRequest.provider === 'openai' || nextRequest.provider === 'openrouter') {\n capOpenAIReasoning(nextRequest.body, allowed);\n if (isRecordArray(nextRequest.body.tools)) {\n nextRequest.body.tools = filterOpenAITools(nextRequest.body.tools, allowed, options);\n }\n return nextRequest;\n }\n\n if (nextRequest.provider === 'gemini') {\n capGeminiReasoning(nextRequest.body, allowed);\n if (isRecordArray(nextRequest.body.tools)) {\n nextRequest.body.tools = filterGeminiTools(nextRequest.body.tools, allowed, options);\n }\n return nextRequest;\n }\n\n if (nextRequest.provider === 'anthropic' && isRecordArray(nextRequest.body.tools)) {\n nextRequest.body.tools = filterAnthropicTools(nextRequest.body.tools, allowed, options);\n }\n\n return nextRequest;\n}\n\ninterface UsageMeta {\n modelUsed?: string;\n responseStatusCode?: number;\n}\n\nexport function extractOpenAIUsage(response: unknown, meta: UsageMeta = {}): UsageTapEndUsage {\n const usage = isObject(response) && isObject(response.usage) ? response.usage : {};\n const promptDetails = isObject(usage.prompt_tokens_details) ? usage.prompt_tokens_details : {};\n const completionDetails = isObject(usage.completion_tokens_details) ? usage.completion_tokens_details : {};\n\n return {\n modelUsed: meta.modelUsed,\n inputTokens: typeof usage.prompt_tokens === 'number' ? usage.prompt_tokens : 0,\n responseTokens: typeof usage.completion_tokens === 'number' ? usage.completion_tokens : 0,\n cachedInputTokens: typeof promptDetails.cached_tokens === 'number' ? promptDetails.cached_tokens : undefined,\n reasoningTokens: typeof completionDetails.reasoning_tokens === 'number' ? completionDetails.reasoning_tokens : undefined,\n responseStatusCode: meta.responseStatusCode ?? 200,\n };\n}\n\nexport function extractAnthropicUsage(response: unknown, meta: UsageMeta = {}): UsageTapEndUsage {\n const usage = isObject(response) && isObject(response.usage) ? response.usage : {};\n\n return {\n modelUsed: meta.modelUsed,\n inputTokens: typeof usage.input_tokens === 'number' ? usage.input_tokens : 0,\n responseTokens: typeof usage.output_tokens === 'number' ? usage.output_tokens : 0,\n cachedInputTokens: typeof usage.cache_read_input_tokens === 'number' ? usage.cache_read_input_tokens : undefined,\n responseStatusCode: meta.responseStatusCode ?? 200,\n };\n}\n\nexport function extractGeminiUsage(response: unknown, meta: UsageMeta = {}): UsageTapEndUsage {\n const usage = isObject(response) && isObject(response.usageMetadata) ? response.usageMetadata : {};\n\n return {\n modelUsed: meta.modelUsed,\n inputTokens: typeof usage.promptTokenCount === 'number' ? usage.promptTokenCount : 0,\n responseTokens: typeof usage.candidatesTokenCount === 'number' ? usage.candidatesTokenCount : 0,\n cachedInputTokens: typeof usage.cachedContentTokenCount === 'number' ? usage.cachedContentTokenCount : undefined,\n reasoningTokens: typeof usage.thoughtsTokenCount === 'number' ? usage.thoughtsTokenCount : undefined,\n responseStatusCode: meta.responseStatusCode ?? 200,\n };\n}\n\nasync function runProviderWithUsageTap<TResult>(\n client: UsageTapClient,\n options: UsageTapProviderRunOptions<TResult>,\n defaultExtractor: (response: TResult, meta: UsageMeta) => UsageTapEndUsage,\n): Promise<UsageTapProviderRunResult<TResult>> {\n let requestUsed = cloneRequest(options.request);\n\n const lifecycle = await withUsageTapCall(client, {\n begin: options.begin,\n signal: options.signal,\n onError: options.onError ?? defaultUsageTapErrorMapper,\n invoke: async ({ begin, setUsage }) => {\n requestUsed = options.entitlementMode === 'apply'\n ? applyUsageTapEntitlements(options.request, begin, options)\n : cloneRequest(options.request);\n\n const invoked = await options.invoke(requestUsed);\n\n if (isInvokeResult<TResult>(invoked)) {\n return invoked;\n }\n\n const response = invoked as TResult;\n const usage = (options.extractUsage ?? defaultExtractor)(response, {\n modelUsed: requestUsed.model,\n responseStatusCode: 200,\n });\n setUsage(usage);\n return response;\n },\n });\n\n return {\n response: lifecycle.result,\n begin: lifecycle.begin,\n end: lifecycle.end,\n requestUsed,\n effectiveUsage: lifecycle.effectiveUsage,\n allowed: lifecycle.begin.data.allowed ?? {},\n };\n}\n\nexport function runOpenAIWithUsageTap<TResult>(client: UsageTapClient, options: UsageTapProviderRunOptions<TResult>) {\n return runProviderWithUsageTap(client, options, extractOpenAIUsage);\n}\n\nexport function runOpenRouterWithUsageTap<TResult>(client: UsageTapClient, options: UsageTapProviderRunOptions<TResult>) {\n return runProviderWithUsageTap(client, options, extractOpenAIUsage);\n}\n\nexport function runAnthropicWithUsageTap<TResult>(client: UsageTapClient, options: UsageTapProviderRunOptions<TResult>) {\n return runProviderWithUsageTap(client, options, extractAnthropicUsage);\n}\n\nexport function runGeminiWithUsageTap<TResult>(client: UsageTapClient, options: UsageTapProviderRunOptions<TResult>) {\n return runProviderWithUsageTap(client, options, extractGeminiUsage);\n}\n"],"mappings":";AAUA,IAAM,mBAAmB;AACzB,IA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isObject","isInvokeResult"]}
|
package/dist/cli/index.js
CHANGED
|
@@ -909,27 +909,29 @@ async function main() {
|
|
|
909
909
|
},
|
|
910
910
|
});
|
|
911
911
|
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
// {
|
|
916
|
-
// "model": "gpt-5.4-mini",
|
|
917
|
-
// "reasoning_effort": "low",
|
|
918
|
-
// "temperature": 0.2,
|
|
919
|
-
// "messages": [
|
|
920
|
-
// {
|
|
921
|
-
// "role": "system",
|
|
922
|
-
// "content": "You are a friendly assistant. Be helpful and concise.
|
|
923
|
-
Current app context: Welcome screen.
|
|
912
|
+
/*
|
|
913
|
+
request.body is the fully transformed OpenAI Chat Completions payload.
|
|
914
|
+
For the hello.md prompt in the dev environment it looks like:
|
|
924
915
|
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
916
|
+
{
|
|
917
|
+
"model": "gpt-5.4-mini",
|
|
918
|
+
"reasoning_effort": "low",
|
|
919
|
+
"temperature": 0.2,
|
|
920
|
+
"messages": [
|
|
921
|
+
{
|
|
922
|
+
"role": "system",
|
|
923
|
+
"content": "You are a friendly assistant. Be helpful and concise.
|
|
924
|
+
Current app context: Welcome screen.
|
|
925
|
+
|
|
926
|
+
Always be polite, professional, and concise. Avoid jargon unless the user uses it first."
|
|
927
|
+
},
|
|
928
|
+
{
|
|
929
|
+
"role": "user",
|
|
930
|
+
"content": "Say hello to World and ask how you can help them today."
|
|
931
|
+
}
|
|
932
|
+
]
|
|
933
|
+
}
|
|
934
|
+
*/
|
|
933
935
|
|
|
934
936
|
console.log('Model:', request.body.model);
|
|
935
937
|
|