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/dist/index.cjs CHANGED
@@ -35,7 +35,15 @@ __export(src_exports, {
35
35
  PromptOpsKit: () => PromptOpsKit,
36
36
  anthropicAdapter: () => anthropicAdapter,
37
37
  applyOverrides: () => applyOverrides,
38
+ applyUsageTapEntitlements: () => applyUsageTapEntitlements,
39
+ beginUsageTapCall: () => beginUsageTapCall,
38
40
  createPromptOpsKit: () => createPromptOpsKit,
41
+ createUsageTapClient: () => createUsageTapClient,
42
+ defaultUsageTapErrorMapper: () => defaultUsageTapErrorMapper,
43
+ endUsageTapCall: () => endUsageTapCall,
44
+ extractAnthropicUsage: () => extractAnthropicUsage,
45
+ extractGeminiUsage: () => extractGeminiUsage,
46
+ extractOpenAIUsage: () => extractOpenAIUsage,
39
47
  extractSections: () => extractSections,
40
48
  extractVariables: () => extractVariables,
41
49
  geminiAdapter: () => geminiAdapter,
@@ -47,8 +55,13 @@ __export(src_exports, {
47
55
  parsePrompt: () => parsePrompt,
48
56
  renderPrompt: () => renderPrompt,
49
57
  resolveIncludes: () => resolveIncludes,
58
+ runAnthropicWithUsageTap: () => runAnthropicWithUsageTap,
59
+ runGeminiWithUsageTap: () => runGeminiWithUsageTap,
60
+ runOpenAIWithUsageTap: () => runOpenAIWithUsageTap,
61
+ runOpenRouterWithUsageTap: () => runOpenRouterWithUsageTap,
50
62
  validateAsset: () => validateAsset,
51
- validateAssetWithIncludes: () => validateAssetWithIncludes
63
+ validateAssetWithIncludes: () => validateAssetWithIncludes,
64
+ withUsageTapCall: () => withUsageTapCall
52
65
  });
53
66
  module.exports = __toCommonJS(src_exports);
54
67
  var import_promises3 = require("fs/promises");
@@ -934,6 +947,377 @@ var PromptCache = class {
934
947
  }
935
948
  };
936
949
 
950
+ // src/usagetap/client.ts
951
+ var DEFAULT_BASE_URL = "https://api.usagetap.com";
952
+ var ACCEPT_HEADER = "application/vnd.usagetap.v1+json";
953
+ function createUsageTapHttpError(status, body) {
954
+ const error = new Error(`UsageTap request failed with status ${status}`);
955
+ error.statusCode = status;
956
+ error.body = body;
957
+ return error;
958
+ }
959
+ function createUsageTapClient(config) {
960
+ const fetchImpl = config.fetch ?? globalThis.fetch;
961
+ if (!fetchImpl) {
962
+ throw new Error("Fetch API is not available. Provide config.fetch when creating the UsageTap client.");
963
+ }
964
+ const baseUrl = (config.baseUrl ?? DEFAULT_BASE_URL).replace(/\/$/, "");
965
+ async function request(path, body, init = {}) {
966
+ const response = await fetchImpl(`${baseUrl}${path}`, {
967
+ method: "POST",
968
+ headers: {
969
+ Authorization: `Bearer ${config.apiKey}`,
970
+ Accept: ACCEPT_HEADER,
971
+ "Content-Type": "application/json"
972
+ },
973
+ body: JSON.stringify(body),
974
+ signal: init.signal
975
+ });
976
+ const text = await response.text();
977
+ const parsed = text ? JSON.parse(text) : {};
978
+ if (!response.ok) {
979
+ throw createUsageTapHttpError(response.status, parsed);
980
+ }
981
+ return parsed;
982
+ }
983
+ return {
984
+ request,
985
+ beginCall(begin, init) {
986
+ return request("/call_begin", begin, init);
987
+ },
988
+ endCall(end, init) {
989
+ return request("/call_end", end, init);
990
+ }
991
+ };
992
+ }
993
+
994
+ // src/usagetap/lifecycle.ts
995
+ function isObject(value) {
996
+ return typeof value === "object" && value !== null;
997
+ }
998
+ function isInvokeResult(value) {
999
+ return isObject(value) && "result" in value && "usage" in value;
1000
+ }
1001
+ function getErrorStatusCode(error) {
1002
+ if (!isObject(error)) {
1003
+ return void 0;
1004
+ }
1005
+ if (typeof error.statusCode === "number") {
1006
+ return error.statusCode;
1007
+ }
1008
+ if (typeof error.status === "number") {
1009
+ return error.status;
1010
+ }
1011
+ if (isObject(error.response) && typeof error.response.status === "number") {
1012
+ return error.response.status;
1013
+ }
1014
+ return void 0;
1015
+ }
1016
+ function attachEndErrorCause(thrownError, endError) {
1017
+ if (thrownError instanceof Error) {
1018
+ if (thrownError.cause === void 0) {
1019
+ Object.defineProperty(thrownError, "cause", {
1020
+ value: endError,
1021
+ configurable: true,
1022
+ writable: true
1023
+ });
1024
+ }
1025
+ throw thrownError;
1026
+ }
1027
+ throw new AggregateError([thrownError, endError], "Vendor call failed and UsageTap call_end also failed.");
1028
+ }
1029
+ function defaultUsageTapErrorMapper(error) {
1030
+ return {
1031
+ code: "VENDOR_ERROR",
1032
+ message: String(error)
1033
+ };
1034
+ }
1035
+ function beginUsageTapCall(client, begin, init) {
1036
+ return client.beginCall(begin, init);
1037
+ }
1038
+ function endUsageTapCall(client, end, init) {
1039
+ return client.endCall(end, init);
1040
+ }
1041
+ async function withUsageTapCall(client, options) {
1042
+ const begin = await beginUsageTapCall(client, options.begin, { signal: options.signal });
1043
+ const onError = options.onError ?? defaultUsageTapErrorMapper;
1044
+ let result;
1045
+ let usage;
1046
+ let thrownError;
1047
+ const setUsage = (nextUsage) => {
1048
+ usage = { ...usage, ...nextUsage };
1049
+ };
1050
+ try {
1051
+ const invoked = await options.invoke({
1052
+ begin,
1053
+ setUsage,
1054
+ signal: options.signal
1055
+ });
1056
+ if (isInvokeResult(invoked)) {
1057
+ result = invoked.result;
1058
+ setUsage(invoked.usage);
1059
+ } else {
1060
+ result = invoked;
1061
+ }
1062
+ } catch (error) {
1063
+ thrownError = error;
1064
+ }
1065
+ const effectiveUsage = {
1066
+ ...usage
1067
+ };
1068
+ if (thrownError) {
1069
+ effectiveUsage.error = effectiveUsage.error ?? onError(thrownError);
1070
+ effectiveUsage.responseStatusCode = effectiveUsage.responseStatusCode ?? getErrorStatusCode(thrownError);
1071
+ } else {
1072
+ effectiveUsage.responseStatusCode = effectiveUsage.responseStatusCode ?? 200;
1073
+ }
1074
+ let end;
1075
+ try {
1076
+ end = await endUsageTapCall(client, {
1077
+ callId: begin.data.callId,
1078
+ ...effectiveUsage
1079
+ }, { signal: options.signal });
1080
+ } catch (endError) {
1081
+ if (thrownError) {
1082
+ attachEndErrorCause(thrownError, endError);
1083
+ }
1084
+ throw endError;
1085
+ }
1086
+ if (thrownError) {
1087
+ throw thrownError;
1088
+ }
1089
+ return {
1090
+ result,
1091
+ begin,
1092
+ end,
1093
+ effectiveUsage
1094
+ };
1095
+ }
1096
+
1097
+ // src/usagetap/providers.ts
1098
+ function cloneRequest(request) {
1099
+ return structuredClone(request);
1100
+ }
1101
+ function isObject2(value) {
1102
+ return typeof value === "object" && value !== null;
1103
+ }
1104
+ function isRecordArray(value) {
1105
+ return Array.isArray(value) && value.every((item) => isObject2(item));
1106
+ }
1107
+ function isInvokeResult2(value) {
1108
+ return isObject2(value) && "result" in value && "usage" in value;
1109
+ }
1110
+ function capabilityAllowed(allowed, capability) {
1111
+ return allowed[capability] !== false;
1112
+ }
1113
+ function reasoningLevelOrder(level) {
1114
+ switch (level) {
1115
+ case "HIGH":
1116
+ return 3;
1117
+ case "MEDIUM":
1118
+ return 2;
1119
+ case "LOW":
1120
+ return 1;
1121
+ default:
1122
+ return 0;
1123
+ }
1124
+ }
1125
+ function capOpenAIReasoning(body, allowed) {
1126
+ if (typeof body.reasoning_effort !== "string") {
1127
+ return;
1128
+ }
1129
+ const current = String(body.reasoning_effort).toUpperCase();
1130
+ const allowedLevel = allowed.reasoningLevel ?? "HIGH";
1131
+ if (allowedLevel === "NONE") {
1132
+ delete body.reasoning_effort;
1133
+ return;
1134
+ }
1135
+ if (reasoningLevelOrder(current) > reasoningLevelOrder(allowedLevel)) {
1136
+ body.reasoning_effort = allowedLevel.toLowerCase();
1137
+ }
1138
+ }
1139
+ function capGeminiReasoning(body, allowed) {
1140
+ if (allowed.reasoningLevel === "NONE") {
1141
+ delete body.thinkingConfig;
1142
+ return;
1143
+ }
1144
+ const budgets = {
1145
+ LOW: 1024,
1146
+ MEDIUM: 4096,
1147
+ HIGH: 8192
1148
+ };
1149
+ const allowedBudget = allowed.reasoningLevel ? budgets[allowed.reasoningLevel] : void 0;
1150
+ if (allowedBudget === void 0) {
1151
+ return;
1152
+ }
1153
+ if (!isObject2(body.thinkingConfig)) {
1154
+ body.thinkingConfig = { thinkingBudget: allowedBudget };
1155
+ return;
1156
+ }
1157
+ const thinkingConfig = body.thinkingConfig;
1158
+ if (typeof thinkingConfig.thinkingBudget === "number") {
1159
+ thinkingConfig.thinkingBudget = Math.min(thinkingConfig.thinkingBudget, allowedBudget);
1160
+ return;
1161
+ }
1162
+ thinkingConfig.thinkingBudget = allowedBudget;
1163
+ }
1164
+ function applyModelTier(request, allowed, options) {
1165
+ const nextModel = allowed.premium && options.modelTiers?.premium ? options.modelTiers.premium : allowed.standard && options.modelTiers?.standard ? options.modelTiers.standard : void 0;
1166
+ if (!nextModel) {
1167
+ return;
1168
+ }
1169
+ request.model = nextModel;
1170
+ if (isObject2(request.body) && "model" in request.body) {
1171
+ request.body.model = nextModel;
1172
+ }
1173
+ }
1174
+ function filterOpenAITools(tools, allowed, options) {
1175
+ return tools.filter((tool) => {
1176
+ if (tool.type === "web_search" && allowed.search === false) {
1177
+ return false;
1178
+ }
1179
+ const functionDef = isObject2(tool.function) ? tool.function : void 0;
1180
+ const name = typeof functionDef?.name === "string" ? functionDef.name : typeof tool.name === "string" ? tool.name : typeof tool.type === "string" ? tool.type : void 0;
1181
+ if (!name) {
1182
+ return true;
1183
+ }
1184
+ const capability = options.toolEntitlements?.[name];
1185
+ return capability ? capabilityAllowed(allowed, capability) : true;
1186
+ });
1187
+ }
1188
+ function filterAnthropicTools(tools, allowed, options) {
1189
+ return tools.filter((tool) => {
1190
+ const name = typeof tool.name === "string" ? tool.name : void 0;
1191
+ if (!name) {
1192
+ return true;
1193
+ }
1194
+ const capability = options.toolEntitlements?.[name];
1195
+ return capability ? capabilityAllowed(allowed, capability) : true;
1196
+ });
1197
+ }
1198
+ function filterGeminiTools(tools, allowed, options) {
1199
+ return tools.map((tool) => {
1200
+ const declarations = Array.isArray(tool.functionDeclarations) ? tool.functionDeclarations.filter((declaration) => {
1201
+ if (!isObject2(declaration) || typeof declaration.name !== "string") {
1202
+ return true;
1203
+ }
1204
+ const capability = options.toolEntitlements?.[declaration.name];
1205
+ return capability ? capabilityAllowed(allowed, capability) : true;
1206
+ }) : tool.functionDeclarations;
1207
+ return {
1208
+ ...tool,
1209
+ functionDeclarations: declarations
1210
+ };
1211
+ }).filter((tool) => {
1212
+ if (!Array.isArray(tool.functionDeclarations)) {
1213
+ return true;
1214
+ }
1215
+ return tool.functionDeclarations.length > 0;
1216
+ });
1217
+ }
1218
+ function applyUsageTapEntitlements(request, begin, options = {}) {
1219
+ const nextRequest = cloneRequest(request);
1220
+ const allowed = begin.data.allowed ?? {};
1221
+ applyModelTier(nextRequest, allowed, options);
1222
+ if (!isObject2(nextRequest.body)) {
1223
+ return nextRequest;
1224
+ }
1225
+ if (nextRequest.provider === "openai" || nextRequest.provider === "openrouter") {
1226
+ capOpenAIReasoning(nextRequest.body, allowed);
1227
+ if (isRecordArray(nextRequest.body.tools)) {
1228
+ nextRequest.body.tools = filterOpenAITools(nextRequest.body.tools, allowed, options);
1229
+ }
1230
+ return nextRequest;
1231
+ }
1232
+ if (nextRequest.provider === "gemini") {
1233
+ capGeminiReasoning(nextRequest.body, allowed);
1234
+ if (isRecordArray(nextRequest.body.tools)) {
1235
+ nextRequest.body.tools = filterGeminiTools(nextRequest.body.tools, allowed, options);
1236
+ }
1237
+ return nextRequest;
1238
+ }
1239
+ if (nextRequest.provider === "anthropic" && isRecordArray(nextRequest.body.tools)) {
1240
+ nextRequest.body.tools = filterAnthropicTools(nextRequest.body.tools, allowed, options);
1241
+ }
1242
+ return nextRequest;
1243
+ }
1244
+ function extractOpenAIUsage(response, meta = {}) {
1245
+ const usage = isObject2(response) && isObject2(response.usage) ? response.usage : {};
1246
+ const promptDetails = isObject2(usage.prompt_tokens_details) ? usage.prompt_tokens_details : {};
1247
+ const completionDetails = isObject2(usage.completion_tokens_details) ? usage.completion_tokens_details : {};
1248
+ return {
1249
+ modelUsed: meta.modelUsed,
1250
+ inputTokens: typeof usage.prompt_tokens === "number" ? usage.prompt_tokens : 0,
1251
+ responseTokens: typeof usage.completion_tokens === "number" ? usage.completion_tokens : 0,
1252
+ cachedInputTokens: typeof promptDetails.cached_tokens === "number" ? promptDetails.cached_tokens : void 0,
1253
+ reasoningTokens: typeof completionDetails.reasoning_tokens === "number" ? completionDetails.reasoning_tokens : void 0,
1254
+ responseStatusCode: meta.responseStatusCode ?? 200
1255
+ };
1256
+ }
1257
+ function extractAnthropicUsage(response, meta = {}) {
1258
+ const usage = isObject2(response) && isObject2(response.usage) ? response.usage : {};
1259
+ return {
1260
+ modelUsed: meta.modelUsed,
1261
+ inputTokens: typeof usage.input_tokens === "number" ? usage.input_tokens : 0,
1262
+ responseTokens: typeof usage.output_tokens === "number" ? usage.output_tokens : 0,
1263
+ cachedInputTokens: typeof usage.cache_read_input_tokens === "number" ? usage.cache_read_input_tokens : void 0,
1264
+ responseStatusCode: meta.responseStatusCode ?? 200
1265
+ };
1266
+ }
1267
+ function extractGeminiUsage(response, meta = {}) {
1268
+ const usage = isObject2(response) && isObject2(response.usageMetadata) ? response.usageMetadata : {};
1269
+ return {
1270
+ modelUsed: meta.modelUsed,
1271
+ inputTokens: typeof usage.promptTokenCount === "number" ? usage.promptTokenCount : 0,
1272
+ responseTokens: typeof usage.candidatesTokenCount === "number" ? usage.candidatesTokenCount : 0,
1273
+ cachedInputTokens: typeof usage.cachedContentTokenCount === "number" ? usage.cachedContentTokenCount : void 0,
1274
+ reasoningTokens: typeof usage.thoughtsTokenCount === "number" ? usage.thoughtsTokenCount : void 0,
1275
+ responseStatusCode: meta.responseStatusCode ?? 200
1276
+ };
1277
+ }
1278
+ async function runProviderWithUsageTap(client, options, defaultExtractor) {
1279
+ let requestUsed = cloneRequest(options.request);
1280
+ const lifecycle = await withUsageTapCall(client, {
1281
+ begin: options.begin,
1282
+ signal: options.signal,
1283
+ onError: options.onError ?? defaultUsageTapErrorMapper,
1284
+ invoke: async ({ begin, setUsage }) => {
1285
+ requestUsed = options.entitlementMode === "apply" ? applyUsageTapEntitlements(options.request, begin, options) : cloneRequest(options.request);
1286
+ const invoked = await options.invoke(requestUsed);
1287
+ if (isInvokeResult2(invoked)) {
1288
+ return invoked;
1289
+ }
1290
+ const response = invoked;
1291
+ const usage = (options.extractUsage ?? defaultExtractor)(response, {
1292
+ modelUsed: requestUsed.model,
1293
+ responseStatusCode: 200
1294
+ });
1295
+ setUsage(usage);
1296
+ return response;
1297
+ }
1298
+ });
1299
+ return {
1300
+ response: lifecycle.result,
1301
+ begin: lifecycle.begin,
1302
+ end: lifecycle.end,
1303
+ requestUsed,
1304
+ effectiveUsage: lifecycle.effectiveUsage,
1305
+ allowed: lifecycle.begin.data.allowed ?? {}
1306
+ };
1307
+ }
1308
+ function runOpenAIWithUsageTap(client, options) {
1309
+ return runProviderWithUsageTap(client, options, extractOpenAIUsage);
1310
+ }
1311
+ function runOpenRouterWithUsageTap(client, options) {
1312
+ return runProviderWithUsageTap(client, options, extractOpenAIUsage);
1313
+ }
1314
+ function runAnthropicWithUsageTap(client, options) {
1315
+ return runProviderWithUsageTap(client, options, extractAnthropicUsage);
1316
+ }
1317
+ function runGeminiWithUsageTap(client, options) {
1318
+ return runProviderWithUsageTap(client, options, extractGeminiUsage);
1319
+ }
1320
+
937
1321
  // src/index.ts
938
1322
  function shouldIncludeContextWarningsInResult(policy) {
939
1323
  return policy === void 0 || policy === "auto" || policy === "result-only" || policy === "console-and-result";
@@ -1118,7 +1502,15 @@ async function renderPrompt(options) {
1118
1502
  PromptOpsKit,
1119
1503
  anthropicAdapter,
1120
1504
  applyOverrides,
1505
+ applyUsageTapEntitlements,
1506
+ beginUsageTapCall,
1121
1507
  createPromptOpsKit,
1508
+ createUsageTapClient,
1509
+ defaultUsageTapErrorMapper,
1510
+ endUsageTapCall,
1511
+ extractAnthropicUsage,
1512
+ extractGeminiUsage,
1513
+ extractOpenAIUsage,
1122
1514
  extractSections,
1123
1515
  extractVariables,
1124
1516
  geminiAdapter,
@@ -1130,7 +1522,12 @@ async function renderPrompt(options) {
1130
1522
  parsePrompt,
1131
1523
  renderPrompt,
1132
1524
  resolveIncludes,
1525
+ runAnthropicWithUsageTap,
1526
+ runGeminiWithUsageTap,
1527
+ runOpenAIWithUsageTap,
1528
+ runOpenRouterWithUsageTap,
1133
1529
  validateAsset,
1134
- validateAssetWithIncludes
1530
+ validateAssetWithIncludes,
1531
+ withUsageTapCall
1135
1532
  });
1136
1533
  //# sourceMappingURL=index.cjs.map