@enlitt/sdk 0.0.0-stage → 0.1.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/CHANGELOG.md +8 -0
- package/LICENSE +21 -0
- package/README.md +61 -2
- package/dist/browser/auth.d.cts +44 -0
- package/dist/browser/auth.d.ts +45 -0
- package/dist/browser/auth.d.ts.map +1 -0
- package/dist/browser.cjs +35 -0
- package/dist/browser.cjs.map +1 -0
- package/dist/browser.d.cts +12 -0
- package/dist/browser.d.ts +13 -0
- package/dist/browser.d.ts.map +1 -0
- package/dist/browser.js +32 -0
- package/dist/browser.js.map +1 -0
- package/dist/chunk-26SW63P5.cjs +883 -0
- package/dist/chunk-26SW63P5.cjs.map +1 -0
- package/dist/chunk-6DPBYLA5.js +855 -0
- package/dist/chunk-6DPBYLA5.js.map +1 -0
- package/dist/chunk-6OWXYBBT.cjs +146 -0
- package/dist/chunk-6OWXYBBT.cjs.map +1 -0
- package/dist/chunk-C76K6HLL.cjs +906 -0
- package/dist/chunk-C76K6HLL.cjs.map +1 -0
- package/dist/chunk-G4DTTV5E.cjs +35 -0
- package/dist/chunk-G4DTTV5E.cjs.map +1 -0
- package/dist/chunk-SFI4RLNN.js +900 -0
- package/dist/chunk-SFI4RLNN.js.map +1 -0
- package/dist/chunk-V5TCSQ7C.js +143 -0
- package/dist/chunk-V5TCSQ7C.js.map +1 -0
- package/dist/chunk-VNVMEU34.js +33 -0
- package/dist/chunk-VNVMEU34.js.map +1 -0
- package/dist/core/errors.d.cts +254 -0
- package/dist/core/errors.d.ts +255 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/index.d.cts +24 -0
- package/dist/core/index.d.ts +25 -0
- package/dist/core/index.d.ts.map +1 -0
- package/dist/core/premium.d.cts +230 -0
- package/dist/core/premium.d.ts +231 -0
- package/dist/core/premium.d.ts.map +1 -0
- package/dist/core/session.d.cts +107 -0
- package/dist/core/session.d.ts +108 -0
- package/dist/core/session.d.ts.map +1 -0
- package/dist/core/sse.d.cts +79 -0
- package/dist/core/sse.d.ts +80 -0
- package/dist/core/sse.d.ts.map +1 -0
- package/dist/core/transport.d.cts +223 -0
- package/dist/core/transport.d.ts +224 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/core/usage.d.cts +35 -0
- package/dist/core/usage.d.ts +36 -0
- package/dist/core/usage.d.ts.map +1 -0
- package/dist/core.cjs +73 -0
- package/dist/core.cjs.map +1 -0
- package/dist/core.js +4 -0
- package/dist/core.js.map +1 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +16 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/openai/client.d.cts +48 -0
- package/dist/openai/client.d.ts +49 -0
- package/dist/openai/client.d.ts.map +1 -0
- package/dist/openai/index.d.cts +1 -0
- package/dist/openai/index.d.ts +2 -0
- package/dist/openai/index.d.ts.map +1 -0
- package/dist/openai.cjs +479 -0
- package/dist/openai.cjs.map +1 -0
- package/dist/openai.js +473 -0
- package/dist/openai.js.map +1 -0
- package/dist/server/callback.d.cts +106 -0
- package/dist/server/callback.d.ts +107 -0
- package/dist/server/callback.d.ts.map +1 -0
- package/dist/server/index.d.cts +19 -0
- package/dist/server/index.d.ts +20 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/secrets.d.cts +46 -0
- package/dist/server/secrets.d.ts +47 -0
- package/dist/server/secrets.d.ts.map +1 -0
- package/dist/server-unavailable.cjs +8 -0
- package/dist/server-unavailable.cjs.map +1 -0
- package/dist/server-unavailable.d.cts +10 -0
- package/dist/server-unavailable.d.ts +11 -0
- package/dist/server-unavailable.d.ts.map +1 -0
- package/dist/server-unavailable.js +6 -0
- package/dist/server-unavailable.js.map +1 -0
- package/dist/server.cjs +354 -0
- package/dist/server.cjs.map +1 -0
- package/dist/server.js +275 -0
- package/dist/server.js.map +1 -0
- package/dist/vercel/index.d.cts +15 -0
- package/dist/vercel/index.d.ts +16 -0
- package/dist/vercel/index.d.ts.map +1 -0
- package/dist/vercel/language-model.d.cts +131 -0
- package/dist/vercel/language-model.d.ts +132 -0
- package/dist/vercel/language-model.d.ts.map +1 -0
- package/dist/vercel/premium-language-model.d.cts +98 -0
- package/dist/vercel/premium-language-model.d.ts +99 -0
- package/dist/vercel/premium-language-model.d.ts.map +1 -0
- package/dist/vercel/standard-language-model.d.cts +104 -0
- package/dist/vercel/standard-language-model.d.ts +105 -0
- package/dist/vercel/standard-language-model.d.ts.map +1 -0
- package/dist/vercel.cjs +22 -0
- package/dist/vercel.cjs.map +1 -0
- package/dist/vercel.js +5 -0
- package/dist/vercel.js.map +1 -0
- package/package.json +129 -3
|
@@ -0,0 +1,906 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk6OWXYBBT_cjs = require('./chunk-6OWXYBBT.cjs');
|
|
4
|
+
var chunk26SW63P5_cjs = require('./chunk-26SW63P5.cjs');
|
|
5
|
+
var protocol = require('@enlitt/protocol');
|
|
6
|
+
|
|
7
|
+
var RESPONSES_PATH = "/v1/responses";
|
|
8
|
+
var ENLITT_METADATA_KEYS = {
|
|
9
|
+
operationId: "enlitt_operation_id",
|
|
10
|
+
routedProvider: "enlitt_routed_provider",
|
|
11
|
+
routedModel: "enlitt_routed_model",
|
|
12
|
+
policyVersion: "enlitt_policy_version"
|
|
13
|
+
};
|
|
14
|
+
function createStandardLanguageModel(client) {
|
|
15
|
+
return {
|
|
16
|
+
specificationVersion: "v3",
|
|
17
|
+
provider: "enlitt",
|
|
18
|
+
modelId: protocol.ENLITT_STANDARD_MODEL,
|
|
19
|
+
// Every URL is downloaded by the caller or sent as an image URL rather than
|
|
20
|
+
// referenced natively: no pattern is claimed because none can be verified.
|
|
21
|
+
supportedUrls: {},
|
|
22
|
+
async doGenerate(params) {
|
|
23
|
+
const options = readEnlittCallOptions(params.providerOptions, "standard");
|
|
24
|
+
const callId = resolveCallId(options, params);
|
|
25
|
+
const body = buildStandardRequest(params, options);
|
|
26
|
+
const response = await chunk26SW63P5_cjs.requestEnlitt(client, {
|
|
27
|
+
path: RESPONSES_PATH,
|
|
28
|
+
body,
|
|
29
|
+
callId,
|
|
30
|
+
accept: "application/json",
|
|
31
|
+
...params.abortSignal !== void 0 ? { signal: params.abortSignal } : {}
|
|
32
|
+
});
|
|
33
|
+
const validated = protocol.validateOpenAIResponse(await chunk26SW63P5_cjs.readEnlittJson(response), { mode: "tolerant" });
|
|
34
|
+
if (!validated.ok) {
|
|
35
|
+
throw new chunk26SW63P5_cjs.EnlittError(
|
|
36
|
+
"malformed_response",
|
|
37
|
+
`Enlitt's response could not be validated: ${validated.error.message} (${validated.error.param}).`
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
return mapStandardGenerateResponse(validated.value);
|
|
41
|
+
},
|
|
42
|
+
async doStream(params) {
|
|
43
|
+
const options = readEnlittCallOptions(params.providerOptions, "standard");
|
|
44
|
+
const callId = resolveCallId(options, params);
|
|
45
|
+
const body = buildStandardRequest(params, options, callId, true);
|
|
46
|
+
const response = await chunk26SW63P5_cjs.requestEnlitt(client, {
|
|
47
|
+
path: RESPONSES_PATH,
|
|
48
|
+
body,
|
|
49
|
+
callId,
|
|
50
|
+
accept: "text/event-stream",
|
|
51
|
+
...params.abortSignal !== void 0 ? { signal: params.abortSignal } : {}
|
|
52
|
+
});
|
|
53
|
+
if (response.body === null) {
|
|
54
|
+
throw new chunk26SW63P5_cjs.EnlittError("malformed_response", "Enlitt's streaming response carried no body.");
|
|
55
|
+
}
|
|
56
|
+
const events = chunk6OWXYBBT_cjs.createSseEventStream(response.body);
|
|
57
|
+
return { stream: mapStandardStream(events, params.abortSignal) };
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function hasProviderOptions(value) {
|
|
62
|
+
return typeof value === "object" && value !== null && Object.keys(value).length > 0;
|
|
63
|
+
}
|
|
64
|
+
function rejectItemProviderOptions(value, where) {
|
|
65
|
+
if (hasProviderOptions(value)) {
|
|
66
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
67
|
+
`Enlitt's standard route does not support provider-specific options on ${where}; remove its providerOptions.`
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function buildStandardRequest(params, options, callId, stream = false) {
|
|
72
|
+
rejectUnsupportedCallControls(params);
|
|
73
|
+
const draft = {
|
|
74
|
+
model: protocol.ENLITT_STANDARD_MODEL,
|
|
75
|
+
messages: mapPromptToMessages(params.prompt)
|
|
76
|
+
};
|
|
77
|
+
const choice = params.toolChoice?.type;
|
|
78
|
+
if (choice === "required" || choice === "tool") {
|
|
79
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
80
|
+
"Enlitt's standard route supports only automatic tool choice; remove `toolChoice` or set it to auto or none."
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
if (choice !== "none" && params.tools !== void 0 && params.tools.length > 0) {
|
|
84
|
+
draft.tools = mapTools(params.tools);
|
|
85
|
+
}
|
|
86
|
+
if (params.maxOutputTokens !== void 0) {
|
|
87
|
+
draft.max_output_tokens = params.maxOutputTokens;
|
|
88
|
+
}
|
|
89
|
+
if (stream) {
|
|
90
|
+
draft.stream = true;
|
|
91
|
+
}
|
|
92
|
+
if (options.metadata !== void 0) {
|
|
93
|
+
draft.metadata = options.metadata;
|
|
94
|
+
}
|
|
95
|
+
const validated = protocol.validateEnlittStandardRequest(draft);
|
|
96
|
+
if (!validated.ok) {
|
|
97
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
98
|
+
`The call could not be mapped to Enlitt's standard request shape: ${validated.error.message} (${validated.error.param}).`
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return validated.value;
|
|
102
|
+
}
|
|
103
|
+
function rejectUnsupportedCallControls(params) {
|
|
104
|
+
const unsupported = [
|
|
105
|
+
["temperature", params.temperature !== void 0],
|
|
106
|
+
["topP", params.topP !== void 0],
|
|
107
|
+
["topK", params.topK !== void 0],
|
|
108
|
+
["presencePenalty", params.presencePenalty !== void 0],
|
|
109
|
+
["frequencyPenalty", params.frequencyPenalty !== void 0],
|
|
110
|
+
["seed", params.seed !== void 0],
|
|
111
|
+
["stopSequences", params.stopSequences !== void 0 && params.stopSequences.length > 0],
|
|
112
|
+
// A "text" response format is the default and carries nothing; "json" is structured output.
|
|
113
|
+
["responseFormat", params.responseFormat !== void 0 && params.responseFormat.type !== "text"]
|
|
114
|
+
];
|
|
115
|
+
for (const [name, present] of unsupported) {
|
|
116
|
+
if (present) {
|
|
117
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
118
|
+
`Enlitt's standard route does not support \`${name}\`; remove it from the call.`
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
for (const namespace of Object.keys(params.providerOptions ?? {})) {
|
|
123
|
+
if (namespace !== "enlitt") {
|
|
124
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
125
|
+
`Enlitt's standard route does not support provider-specific options (providerOptions.${namespace}); remove them.`
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
function mapPromptToMessages(prompt) {
|
|
131
|
+
const messages = [];
|
|
132
|
+
for (const message of prompt) {
|
|
133
|
+
rejectItemProviderOptions(message.providerOptions, `a ${message.role} message`);
|
|
134
|
+
switch (message.role) {
|
|
135
|
+
case "system": {
|
|
136
|
+
messages.push({ role: "system", content: message.content });
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "user": {
|
|
140
|
+
messages.push({ role: "user", content: message.content.map(mapUserPart) });
|
|
141
|
+
break;
|
|
142
|
+
}
|
|
143
|
+
case "assistant": {
|
|
144
|
+
messages.push(mapAssistantMessage(message.content));
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "tool": {
|
|
148
|
+
for (const part of message.content) {
|
|
149
|
+
if (part.type === "tool-approval-response") {
|
|
150
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
151
|
+
"Enlitt's standard route does not support tool approval responses; remove the approval response."
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
rejectItemProviderOptions(part.providerOptions, "a tool result");
|
|
155
|
+
messages.push({ role: "tool", tool_call_id: part.toolCallId, content: mapToolResultOutput(part.output) });
|
|
156
|
+
}
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return messages;
|
|
162
|
+
}
|
|
163
|
+
function mapUserPart(part) {
|
|
164
|
+
rejectItemProviderOptions(part.providerOptions, `a user ${part.type} part`);
|
|
165
|
+
if (part.type === "text") {
|
|
166
|
+
return { type: "text", text: part.text };
|
|
167
|
+
}
|
|
168
|
+
if (!part.mediaType.startsWith("image/")) {
|
|
169
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
170
|
+
`Enlitt's standard route supports only text and image parts; "${part.mediaType}" files are not supported.`
|
|
171
|
+
);
|
|
172
|
+
}
|
|
173
|
+
if (part.data instanceof URL) {
|
|
174
|
+
return { type: "image", source: { type: "url", url: part.data.toString() } };
|
|
175
|
+
}
|
|
176
|
+
const data = typeof part.data === "string" ? part.data : Buffer.from(part.data).toString("base64");
|
|
177
|
+
return { type: "image", source: { type: "base64", media_type: part.mediaType, data } };
|
|
178
|
+
}
|
|
179
|
+
function mapAssistantMessage(content) {
|
|
180
|
+
let text = "";
|
|
181
|
+
const toolCalls = [];
|
|
182
|
+
for (const part of content) {
|
|
183
|
+
rejectItemProviderOptions(part.providerOptions, `an assistant ${part.type} part`);
|
|
184
|
+
switch (part.type) {
|
|
185
|
+
case "text":
|
|
186
|
+
text += part.text;
|
|
187
|
+
break;
|
|
188
|
+
case "tool-call": {
|
|
189
|
+
if (part.providerExecuted === true) {
|
|
190
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
191
|
+
"Enlitt's standard route does not support provider-executed tool calls in history."
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
toolCalls.push({
|
|
195
|
+
id: part.toolCallId,
|
|
196
|
+
name: part.toolName,
|
|
197
|
+
arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input)
|
|
198
|
+
});
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case "file":
|
|
202
|
+
case "reasoning":
|
|
203
|
+
case "tool-result":
|
|
204
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
205
|
+
`Enlitt's standard route does not support an assistant "${part.type}" part in history.`
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
role: "assistant",
|
|
211
|
+
...text.length > 0 ? { content: text } : {},
|
|
212
|
+
...toolCalls.length > 0 ? { tool_calls: toolCalls } : {}
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
function mapToolResultOutput(output) {
|
|
216
|
+
switch (output.type) {
|
|
217
|
+
case "text":
|
|
218
|
+
case "error-text":
|
|
219
|
+
return String(output.value);
|
|
220
|
+
case "json":
|
|
221
|
+
case "error-json":
|
|
222
|
+
return JSON.stringify(output.value);
|
|
223
|
+
default:
|
|
224
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
225
|
+
`Enlitt's standard route does not support a tool result output of type "${output.type}".`
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
function mapTools(tools) {
|
|
230
|
+
return tools.map((tool) => {
|
|
231
|
+
if (tool.type !== "function") {
|
|
232
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
233
|
+
"Enlitt's standard route accepts only application-defined function tools, not provider-defined tools."
|
|
234
|
+
);
|
|
235
|
+
}
|
|
236
|
+
rejectItemProviderOptions(tool.providerOptions, `the tool "${tool.name}"`);
|
|
237
|
+
if (tool.strict !== void 0) {
|
|
238
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
239
|
+
`Enlitt's standard route does not support strict tool schemas; remove \`strict\` from the tool "${tool.name}".`
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
return {
|
|
243
|
+
name: tool.name,
|
|
244
|
+
...tool.description !== void 0 ? { description: tool.description } : {},
|
|
245
|
+
input_schema: tool.inputSchema
|
|
246
|
+
};
|
|
247
|
+
});
|
|
248
|
+
}
|
|
249
|
+
function isUnknownVariant(value) {
|
|
250
|
+
return "unknown" in value && value.unknown === true;
|
|
251
|
+
}
|
|
252
|
+
function mapStandardGenerateResponse(response) {
|
|
253
|
+
const content = [];
|
|
254
|
+
const skippedTypes = /* @__PURE__ */ new Set();
|
|
255
|
+
for (const item of response.output) {
|
|
256
|
+
if (isUnknownVariant(item)) {
|
|
257
|
+
skippedTypes.add(item.type);
|
|
258
|
+
} else if (item.type === "message") {
|
|
259
|
+
for (const part of item.content) {
|
|
260
|
+
if (isUnknownVariant(part)) {
|
|
261
|
+
skippedTypes.add(part.type);
|
|
262
|
+
} else {
|
|
263
|
+
content.push({ type: "text", text: part.text });
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
} else {
|
|
267
|
+
content.push({ type: "tool-call", toolCallId: item.call_id, toolName: item.name, input: item.arguments });
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
const warnings = [...skippedTypes].map((type) => ({
|
|
271
|
+
type: "other",
|
|
272
|
+
message: `Enlitt returned an output item of type "${type}" that this version of @enlitt/sdk does not recognize and skipped it. Upgrade @enlitt/sdk to receive it.`
|
|
273
|
+
}));
|
|
274
|
+
return {
|
|
275
|
+
content,
|
|
276
|
+
finishReason: mapFinishReason(response),
|
|
277
|
+
usage: mapUsage(response),
|
|
278
|
+
warnings,
|
|
279
|
+
...hasRoutingMetadata(response) ? { providerMetadata: { enlitt: routingMetadata(response) } } : {}
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
function mapFinishReason(response) {
|
|
283
|
+
switch (response.status) {
|
|
284
|
+
case "other":
|
|
285
|
+
return { unified: "other", raw: void 0 };
|
|
286
|
+
case "failed":
|
|
287
|
+
return { unified: "error", raw: response.error?.code };
|
|
288
|
+
case "incomplete": {
|
|
289
|
+
const reason = response.incomplete_details?.reason;
|
|
290
|
+
if (reason === "max_output_tokens") {
|
|
291
|
+
return { unified: "length", raw: reason };
|
|
292
|
+
}
|
|
293
|
+
return { unified: "other", raw: reason === "other" ? void 0 : reason };
|
|
294
|
+
}
|
|
295
|
+
case "completed":
|
|
296
|
+
return response.output.some((item) => !isUnknownVariant(item) && item.type === "function_call") ? { unified: "tool-calls", raw: response.status } : { unified: "stop", raw: response.status };
|
|
297
|
+
case "in_progress":
|
|
298
|
+
case "queued":
|
|
299
|
+
case "cancelled":
|
|
300
|
+
return { unified: "other", raw: response.status };
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
function mapUsage(response) {
|
|
304
|
+
const usage = response.usage;
|
|
305
|
+
const cachedTokens = usage?.input_tokens_details?.cached_tokens;
|
|
306
|
+
const reasoningTokens = usage?.output_tokens_details?.reasoning_tokens;
|
|
307
|
+
return {
|
|
308
|
+
inputTokens: {
|
|
309
|
+
total: usage?.input_tokens,
|
|
310
|
+
noCache: usage !== void 0 && cachedTokens !== void 0 ? usage.input_tokens - cachedTokens : void 0,
|
|
311
|
+
cacheRead: cachedTokens,
|
|
312
|
+
cacheWrite: void 0
|
|
313
|
+
},
|
|
314
|
+
outputTokens: {
|
|
315
|
+
total: usage?.output_tokens,
|
|
316
|
+
text: usage?.output_tokens !== void 0 && reasoningTokens !== void 0 ? usage.output_tokens - reasoningTokens : usage?.output_tokens,
|
|
317
|
+
reasoning: reasoningTokens
|
|
318
|
+
}
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
function hasRoutingMetadata(response) {
|
|
322
|
+
return Object.values(ENLITT_METADATA_KEYS).some((key) => response.metadata[key] !== void 0);
|
|
323
|
+
}
|
|
324
|
+
function routingMetadata(response) {
|
|
325
|
+
const metadata = response.metadata;
|
|
326
|
+
const pick = (name) => {
|
|
327
|
+
const value = metadata[ENLITT_METADATA_KEYS[name]];
|
|
328
|
+
return value !== void 0 ? { [name]: value } : {};
|
|
329
|
+
};
|
|
330
|
+
return {
|
|
331
|
+
...pick("operationId"),
|
|
332
|
+
...pick("routedProvider"),
|
|
333
|
+
...pick("routedModel"),
|
|
334
|
+
...pick("policyVersion")
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
function isKnownEvent(event) {
|
|
338
|
+
return !isUnknownVariant(event);
|
|
339
|
+
}
|
|
340
|
+
function isTerminalEvent(event) {
|
|
341
|
+
return isKnownEvent(event) && protocol.isTerminalResponseStreamEventType(event.type);
|
|
342
|
+
}
|
|
343
|
+
function sameEvent(a, b) {
|
|
344
|
+
return JSON.stringify(a) === JSON.stringify(b);
|
|
345
|
+
}
|
|
346
|
+
function mapStandardStream(events, signal) {
|
|
347
|
+
const reader = events.getReader();
|
|
348
|
+
let lastEvent;
|
|
349
|
+
let terminated = false;
|
|
350
|
+
let sawTerminal = false;
|
|
351
|
+
const openText = /* @__PURE__ */ new Set();
|
|
352
|
+
const toolItems = /* @__PURE__ */ new Map();
|
|
353
|
+
function onAbort(controller) {
|
|
354
|
+
if (!terminated) {
|
|
355
|
+
terminated = true;
|
|
356
|
+
controller.error(signal?.reason);
|
|
357
|
+
void reader.cancel(signal?.reason).catch(() => void 0);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function reject(controller, message) {
|
|
361
|
+
terminated = true;
|
|
362
|
+
controller.enqueue({ type: "error", error: new chunk26SW63P5_cjs.EnlittError("malformed_response", message) });
|
|
363
|
+
controller.close();
|
|
364
|
+
void reader.cancel().catch(() => void 0);
|
|
365
|
+
}
|
|
366
|
+
return new ReadableStream({
|
|
367
|
+
start(controller) {
|
|
368
|
+
if (signal !== void 0) {
|
|
369
|
+
if (signal.aborted) {
|
|
370
|
+
onAbort(controller);
|
|
371
|
+
} else {
|
|
372
|
+
signal.addEventListener("abort", () => onAbort(controller), { once: true });
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
},
|
|
376
|
+
async pull(controller) {
|
|
377
|
+
for (; ; ) {
|
|
378
|
+
if (terminated) {
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
let raw;
|
|
382
|
+
try {
|
|
383
|
+
raw = await reader.read();
|
|
384
|
+
} catch (thrown) {
|
|
385
|
+
if (thrown instanceof chunk6OWXYBBT_cjs.SseFramingError) {
|
|
386
|
+
reject(controller, thrown.message);
|
|
387
|
+
} else {
|
|
388
|
+
terminated = true;
|
|
389
|
+
controller.error(thrown);
|
|
390
|
+
}
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
if (sawTerminal) {
|
|
394
|
+
if (raw.done) {
|
|
395
|
+
terminated = true;
|
|
396
|
+
controller.close();
|
|
397
|
+
} else {
|
|
398
|
+
reject(controller, "A stream event was received after the terminal event.");
|
|
399
|
+
}
|
|
400
|
+
return;
|
|
401
|
+
}
|
|
402
|
+
if (raw.done) {
|
|
403
|
+
reject(controller, "The stream ended before a terminal event was received.");
|
|
404
|
+
return;
|
|
405
|
+
}
|
|
406
|
+
let json;
|
|
407
|
+
try {
|
|
408
|
+
json = JSON.parse(raw.value.data);
|
|
409
|
+
} catch {
|
|
410
|
+
reject(controller, "A stream event's data payload was not valid JSON.");
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
const validated = protocol.validateResponseStreamEvent(json, { mode: "tolerant" });
|
|
414
|
+
if (!validated.ok) {
|
|
415
|
+
reject(controller, `A stream event failed validation: ${validated.error.message} (${validated.error.param}).`);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
const event = validated.value;
|
|
419
|
+
const orderingError = checkIncrementalOrdering(lastEvent, event);
|
|
420
|
+
if (orderingError !== void 0) {
|
|
421
|
+
reject(controller, orderingError);
|
|
422
|
+
return;
|
|
423
|
+
}
|
|
424
|
+
const isIdenticalRepeat = lastEvent !== void 0 && sameEvent(lastEvent, event);
|
|
425
|
+
lastEvent = event;
|
|
426
|
+
if (isIdenticalRepeat) {
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
const parts = isKnownEvent(event) ? translateEvent(event, openText, toolItems) : [];
|
|
430
|
+
if (isTerminalEvent(event)) {
|
|
431
|
+
sawTerminal = true;
|
|
432
|
+
}
|
|
433
|
+
if (parts.length === 0) {
|
|
434
|
+
continue;
|
|
435
|
+
}
|
|
436
|
+
for (const part of parts) {
|
|
437
|
+
controller.enqueue(part);
|
|
438
|
+
}
|
|
439
|
+
return;
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
async cancel(reason) {
|
|
443
|
+
terminated = true;
|
|
444
|
+
await reader.cancel(reason);
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
function checkIncrementalOrdering(previous, next) {
|
|
449
|
+
if (previous === void 0) {
|
|
450
|
+
return next.type === "response.created" ? void 0 : `The first stream event must be "response.created", got "${next.type}".`;
|
|
451
|
+
}
|
|
452
|
+
if (isTerminalEvent(previous)) {
|
|
453
|
+
return "A stream event was received after the terminal event.";
|
|
454
|
+
}
|
|
455
|
+
if (next.sequence_number === previous.sequence_number) {
|
|
456
|
+
return sameEvent(previous, next) ? void 0 : "Two stream events carried the same sequence_number with conflicting payloads.";
|
|
457
|
+
}
|
|
458
|
+
if (next.sequence_number < previous.sequence_number) {
|
|
459
|
+
return "A stream event arrived out of order (its sequence_number was lower than a prior event's).";
|
|
460
|
+
}
|
|
461
|
+
if (next.sequence_number !== previous.sequence_number + 1) {
|
|
462
|
+
return "A stream event's sequence_number skipped a value (a gap in the sequence).";
|
|
463
|
+
}
|
|
464
|
+
return void 0;
|
|
465
|
+
}
|
|
466
|
+
function translateEvent(event, openText, toolItems) {
|
|
467
|
+
switch (event.type) {
|
|
468
|
+
case "response.created":
|
|
469
|
+
return [{ type: "stream-start", warnings: [] }];
|
|
470
|
+
case "response.output_item.added": {
|
|
471
|
+
const item = event.item;
|
|
472
|
+
if (isUnknownVariant(item) || item.type !== "function_call") {
|
|
473
|
+
return [];
|
|
474
|
+
}
|
|
475
|
+
toolItems.set(item.id, { callId: item.call_id, name: item.name });
|
|
476
|
+
return [{ type: "tool-input-start", id: item.call_id, toolName: item.name }];
|
|
477
|
+
}
|
|
478
|
+
case "response.output_text.delta": {
|
|
479
|
+
const parts = [];
|
|
480
|
+
if (!openText.has(event.item_id)) {
|
|
481
|
+
openText.add(event.item_id);
|
|
482
|
+
parts.push({ type: "text-start", id: event.item_id });
|
|
483
|
+
}
|
|
484
|
+
parts.push({ type: "text-delta", id: event.item_id, delta: event.delta });
|
|
485
|
+
return parts;
|
|
486
|
+
}
|
|
487
|
+
case "response.output_text.done": {
|
|
488
|
+
const parts = [];
|
|
489
|
+
if (!openText.has(event.item_id)) {
|
|
490
|
+
parts.push({ type: "text-start", id: event.item_id });
|
|
491
|
+
}
|
|
492
|
+
openText.delete(event.item_id);
|
|
493
|
+
parts.push({ type: "text-end", id: event.item_id });
|
|
494
|
+
return parts;
|
|
495
|
+
}
|
|
496
|
+
case "response.function_call_arguments.delta": {
|
|
497
|
+
const tool = toolItems.get(event.item_id);
|
|
498
|
+
return [{ type: "tool-input-delta", id: tool?.callId ?? event.item_id, delta: event.delta }];
|
|
499
|
+
}
|
|
500
|
+
case "response.function_call_arguments.done": {
|
|
501
|
+
const tool = toolItems.get(event.item_id);
|
|
502
|
+
const callId = tool?.callId ?? event.item_id;
|
|
503
|
+
return [
|
|
504
|
+
{ type: "tool-input-end", id: callId },
|
|
505
|
+
{ type: "tool-call", toolCallId: callId, toolName: tool?.name ?? event.item_id, input: event.arguments }
|
|
506
|
+
];
|
|
507
|
+
}
|
|
508
|
+
case "response.completed":
|
|
509
|
+
case "response.incomplete":
|
|
510
|
+
case "response.failed":
|
|
511
|
+
return [
|
|
512
|
+
{
|
|
513
|
+
type: "finish",
|
|
514
|
+
finishReason: event.type === "response.completed" && toolItems.size > 0 && event.response.status === "completed" ? { unified: "tool-calls", raw: event.response.status } : mapFinishReason(event.response),
|
|
515
|
+
usage: mapUsage(event.response),
|
|
516
|
+
...hasRoutingMetadata(event.response) ? { providerMetadata: { enlitt: routingMetadata(event.response) } } : {}
|
|
517
|
+
}
|
|
518
|
+
];
|
|
519
|
+
// Bookkeeping events carry nothing a consumer needs.
|
|
520
|
+
case "response.in_progress":
|
|
521
|
+
case "response.output_item.done":
|
|
522
|
+
case "response.content_part.added":
|
|
523
|
+
case "response.content_part.done":
|
|
524
|
+
return [];
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
// src/vercel/premium-language-model.ts
|
|
529
|
+
function perToken(rates) {
|
|
530
|
+
const inputRate = BigInt(chunk26SW63P5_cjs.assertNanoUsd(rates.input, "perToken input rate"));
|
|
531
|
+
const outputRate = BigInt(chunk26SW63P5_cjs.assertNanoUsd(rates.output, "perToken output rate"));
|
|
532
|
+
return (context) => {
|
|
533
|
+
const inputTokens = BigInt(context.usage?.inputTokens.total ?? 0);
|
|
534
|
+
const outputTokens = BigInt(context.usage?.outputTokens.total ?? 0);
|
|
535
|
+
return (inputRate * inputTokens + outputRate * outputTokens).toString();
|
|
536
|
+
};
|
|
537
|
+
}
|
|
538
|
+
function createPremiumLanguageModel(client, model) {
|
|
539
|
+
return {
|
|
540
|
+
specificationVersion: "v3",
|
|
541
|
+
provider: model.provider,
|
|
542
|
+
modelId: model.modelId,
|
|
543
|
+
supportedUrls: model.supportedUrls,
|
|
544
|
+
doGenerate(params) {
|
|
545
|
+
return invoke(
|
|
546
|
+
params,
|
|
547
|
+
(request, price) => wrapPremiumGenerate(client, request, price, () => Promise.resolve(model.doGenerate(params)))
|
|
548
|
+
);
|
|
549
|
+
},
|
|
550
|
+
doStream(params) {
|
|
551
|
+
return invoke(
|
|
552
|
+
params,
|
|
553
|
+
(request, price) => wrapPremiumStream(client, request, price, () => Promise.resolve(model.doStream(params)))
|
|
554
|
+
);
|
|
555
|
+
}
|
|
556
|
+
};
|
|
557
|
+
async function invoke(params, wrap) {
|
|
558
|
+
const options = readEnlittCallOptions(params.providerOptions, "premium");
|
|
559
|
+
const callId = resolveCallId(options, params);
|
|
560
|
+
const request = {
|
|
561
|
+
maxCharge: options.maxCharge,
|
|
562
|
+
provider: { id: model.provider, model: model.modelId },
|
|
563
|
+
callId,
|
|
564
|
+
...options.reportingDeadlineMs !== void 0 ? { reportingDeadlineMs: options.reportingDeadlineMs } : {},
|
|
565
|
+
...options.metadata !== void 0 ? { metadata: options.metadata } : {},
|
|
566
|
+
...params.abortSignal !== void 0 ? { signal: params.abortSignal } : {}
|
|
567
|
+
};
|
|
568
|
+
return wrap(request, options.price);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
async function evaluatePrice(client, price, context) {
|
|
572
|
+
if (price === void 0) {
|
|
573
|
+
return "0";
|
|
574
|
+
}
|
|
575
|
+
if (typeof price === "string") {
|
|
576
|
+
return price;
|
|
577
|
+
}
|
|
578
|
+
try {
|
|
579
|
+
return chunk26SW63P5_cjs.assertNanoUsd(await price(context), "price() result");
|
|
580
|
+
} catch (thrown) {
|
|
581
|
+
const detail = thrown instanceof Error ? thrown.message : "the price function failed";
|
|
582
|
+
client.onTerminalReportError?.(
|
|
583
|
+
thrown instanceof chunk26SW63P5_cjs.EnlittConfigurationError ? thrown : new chunk26SW63P5_cjs.EnlittConfigurationError(`The price function failed, so the call was charged "0": ${detail}`)
|
|
584
|
+
);
|
|
585
|
+
return "0";
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
function wrapPremiumGenerate(client, request, price, run) {
|
|
589
|
+
return chunk26SW63P5_cjs.runPremiumCharge(client, request, async (charge, operation) => {
|
|
590
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
591
|
+
let result;
|
|
592
|
+
try {
|
|
593
|
+
result = await run();
|
|
594
|
+
} catch (thrown) {
|
|
595
|
+
if (chunk26SW63P5_cjs.isCancellation(thrown, request.signal)) {
|
|
596
|
+
const endedAt2 = /* @__PURE__ */ new Date();
|
|
597
|
+
charge(
|
|
598
|
+
await evaluatePrice(client, price, {
|
|
599
|
+
status: "canceled",
|
|
600
|
+
output: { kind: "generate", content: [] },
|
|
601
|
+
startedAt,
|
|
602
|
+
endedAt: endedAt2,
|
|
603
|
+
durationMs: endedAt2.getTime() - startedAt.getTime(),
|
|
604
|
+
effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
|
|
605
|
+
})
|
|
606
|
+
);
|
|
607
|
+
operation.describe({
|
|
608
|
+
timing: timingFacts(startedAt, endedAt2),
|
|
609
|
+
delivery: { outputStarted: false, outputCompleted: false },
|
|
610
|
+
errorCategory: "canceled"
|
|
611
|
+
});
|
|
612
|
+
} else {
|
|
613
|
+
operation.describe({
|
|
614
|
+
timing: timingFacts(startedAt, /* @__PURE__ */ new Date()),
|
|
615
|
+
delivery: { outputStarted: false, outputCompleted: false },
|
|
616
|
+
errorCategory: "provider"
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
throw thrown;
|
|
620
|
+
}
|
|
621
|
+
const endedAt = /* @__PURE__ */ new Date();
|
|
622
|
+
charge(
|
|
623
|
+
await evaluatePrice(client, price, {
|
|
624
|
+
status: "completed",
|
|
625
|
+
output: { kind: "generate", content: result.content },
|
|
626
|
+
usage: result.usage,
|
|
627
|
+
finishReason: result.finishReason,
|
|
628
|
+
...result.providerMetadata !== void 0 ? { providerMetadata: result.providerMetadata } : {},
|
|
629
|
+
startedAt,
|
|
630
|
+
endedAt,
|
|
631
|
+
durationMs: endedAt.getTime() - startedAt.getTime(),
|
|
632
|
+
effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
|
|
633
|
+
})
|
|
634
|
+
);
|
|
635
|
+
operation.describe({
|
|
636
|
+
timing: timingFacts(startedAt, endedAt),
|
|
637
|
+
delivery: { outputStarted: true, outputCompleted: true },
|
|
638
|
+
finishReason: result.finishReason.unified,
|
|
639
|
+
...result.response?.id !== void 0 ? { providerResponseId: result.response.id } : {},
|
|
640
|
+
...usageFacts(result.usage)
|
|
641
|
+
});
|
|
642
|
+
return attachEnlittMetadataToGenerateResult(result, operation);
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
var CONTENT_PART_TYPES = /* @__PURE__ */ new Set([
|
|
646
|
+
"text-delta",
|
|
647
|
+
"reasoning-delta",
|
|
648
|
+
"tool-input-delta",
|
|
649
|
+
"tool-call",
|
|
650
|
+
"file",
|
|
651
|
+
"source"
|
|
652
|
+
]);
|
|
653
|
+
function wrapPremiumStream(client, request, price, run) {
|
|
654
|
+
return new Promise((resolveStreamResult, rejectStreamResult) => {
|
|
655
|
+
const settler = {};
|
|
656
|
+
const lifecycle = chunk26SW63P5_cjs.runPremiumCharge(client, request, async (charge, operation) => {
|
|
657
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
658
|
+
let result;
|
|
659
|
+
try {
|
|
660
|
+
result = await run();
|
|
661
|
+
} catch (thrown) {
|
|
662
|
+
operation.describe({
|
|
663
|
+
timing: timingFacts(startedAt, /* @__PURE__ */ new Date()),
|
|
664
|
+
delivery: { outputStarted: false, outputCompleted: false },
|
|
665
|
+
errorCategory: chunk26SW63P5_cjs.isCancellation(thrown, request.signal) ? "canceled" : "provider"
|
|
666
|
+
});
|
|
667
|
+
throw thrown;
|
|
668
|
+
}
|
|
669
|
+
const parked = new Promise((resolve, reject) => {
|
|
670
|
+
settler.resolve = resolve;
|
|
671
|
+
settler.reject = reject;
|
|
672
|
+
});
|
|
673
|
+
resolveStreamResult(wrapStream(client, request, price, result, charge, operation, startedAt, settler, lifecycle));
|
|
674
|
+
return parked;
|
|
675
|
+
});
|
|
676
|
+
lifecycle.catch(rejectStreamResult);
|
|
677
|
+
});
|
|
678
|
+
}
|
|
679
|
+
function wrapStream(client, request, price, result, charge, operation, startedAt, settler, lifecycle) {
|
|
680
|
+
const collectParts = typeof price === "function";
|
|
681
|
+
const parts = [];
|
|
682
|
+
const reader = result.stream.getReader();
|
|
683
|
+
let outputStarted = false;
|
|
684
|
+
let firstContentAt;
|
|
685
|
+
let finish;
|
|
686
|
+
let sawErrorPart = false;
|
|
687
|
+
let responseId;
|
|
688
|
+
let settlement;
|
|
689
|
+
const settle = (outcome) => {
|
|
690
|
+
settlement ??= (async () => {
|
|
691
|
+
const endedAt = /* @__PURE__ */ new Date();
|
|
692
|
+
const facts = {
|
|
693
|
+
timing: timingFacts(startedAt, endedAt),
|
|
694
|
+
delivery: { outputStarted, outputCompleted: outcome === "completed" }
|
|
695
|
+
};
|
|
696
|
+
if (outcome === "failed") {
|
|
697
|
+
operation.describe({ ...facts, errorCategory: "provider" });
|
|
698
|
+
settler.reject(new Error("The provider stream failed."));
|
|
699
|
+
} else {
|
|
700
|
+
charge(
|
|
701
|
+
await evaluatePrice(client, price, {
|
|
702
|
+
status: outcome,
|
|
703
|
+
output: { kind: "stream", parts },
|
|
704
|
+
...finish !== void 0 ? { usage: finish.usage, finishReason: finish.finishReason } : {},
|
|
705
|
+
...finish?.providerMetadata !== void 0 ? { providerMetadata: finish.providerMetadata } : {},
|
|
706
|
+
startedAt,
|
|
707
|
+
endedAt,
|
|
708
|
+
durationMs: endedAt.getTime() - startedAt.getTime(),
|
|
709
|
+
...firstContentAt !== void 0 ? { timeToFirstTokenMs: firstContentAt.getTime() - startedAt.getTime() } : {},
|
|
710
|
+
effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
|
|
711
|
+
})
|
|
712
|
+
);
|
|
713
|
+
operation.describe({
|
|
714
|
+
...facts,
|
|
715
|
+
...finish !== void 0 ? { finishReason: finish.finishReason.unified, ...usageFacts(finish.usage) } : {},
|
|
716
|
+
...responseId !== void 0 ? { providerResponseId: responseId } : {},
|
|
717
|
+
...outcome === "canceled" ? { errorCategory: "canceled" } : {}
|
|
718
|
+
});
|
|
719
|
+
if (outcome === "completed") {
|
|
720
|
+
settler.resolve();
|
|
721
|
+
} else {
|
|
722
|
+
settler.reject(Object.assign(new Error("The stream was canceled."), { name: "AbortError" }));
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
await lifecycle.catch(() => void 0);
|
|
726
|
+
})();
|
|
727
|
+
return settlement;
|
|
728
|
+
};
|
|
729
|
+
const stream = new ReadableStream({
|
|
730
|
+
async pull(controller) {
|
|
731
|
+
let chunk;
|
|
732
|
+
try {
|
|
733
|
+
chunk = await reader.read();
|
|
734
|
+
} catch (thrown) {
|
|
735
|
+
await settle(chunk26SW63P5_cjs.isCancellation(thrown, request.signal) ? "canceled" : "failed");
|
|
736
|
+
controller.error(thrown);
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
if (chunk.done) {
|
|
740
|
+
await settle(finish !== void 0 && !sawErrorPart ? "completed" : "failed");
|
|
741
|
+
controller.close();
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
const part = chunk.value;
|
|
745
|
+
if (collectParts) parts.push(part);
|
|
746
|
+
if (part.type === "response-metadata" && part.id !== void 0) responseId = part.id;
|
|
747
|
+
if (CONTENT_PART_TYPES.has(part.type)) {
|
|
748
|
+
outputStarted = true;
|
|
749
|
+
firstContentAt ??= /* @__PURE__ */ new Date();
|
|
750
|
+
}
|
|
751
|
+
if (part.type === "error") sawErrorPart = true;
|
|
752
|
+
if (part.type === "finish") {
|
|
753
|
+
finish = part;
|
|
754
|
+
controller.enqueue({
|
|
755
|
+
...part,
|
|
756
|
+
providerMetadata: {
|
|
757
|
+
...part.providerMetadata,
|
|
758
|
+
enlitt: {
|
|
759
|
+
operationId: operation.operationId,
|
|
760
|
+
effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
|
|
761
|
+
}
|
|
762
|
+
}
|
|
763
|
+
});
|
|
764
|
+
} else {
|
|
765
|
+
controller.enqueue(part);
|
|
766
|
+
}
|
|
767
|
+
},
|
|
768
|
+
async cancel(reason) {
|
|
769
|
+
await reader.cancel(reason).catch(() => void 0);
|
|
770
|
+
await settle("canceled");
|
|
771
|
+
}
|
|
772
|
+
});
|
|
773
|
+
return { ...result, stream };
|
|
774
|
+
}
|
|
775
|
+
function timingFacts(startedAt, endedAt) {
|
|
776
|
+
return { startedAt: startedAt.toISOString(), endedAt: endedAt.toISOString() };
|
|
777
|
+
}
|
|
778
|
+
function usageFacts(usage) {
|
|
779
|
+
const counts = {
|
|
780
|
+
...usage.inputTokens.total !== void 0 ? { inputTokens: usage.inputTokens.total } : {},
|
|
781
|
+
...usage.outputTokens.total !== void 0 ? { outputTokens: usage.outputTokens.total } : {},
|
|
782
|
+
...usage.inputTokens.cacheRead !== void 0 ? { cachedInputTokens: usage.inputTokens.cacheRead } : {},
|
|
783
|
+
...usage.outputTokens.reasoning !== void 0 ? { reasoningTokens: usage.outputTokens.reasoning } : {}
|
|
784
|
+
};
|
|
785
|
+
return Object.keys(counts).length > 0 ? { usage: counts } : {};
|
|
786
|
+
}
|
|
787
|
+
function attachEnlittMetadataToGenerateResult(result, operation) {
|
|
788
|
+
return {
|
|
789
|
+
...result,
|
|
790
|
+
providerMetadata: {
|
|
791
|
+
...result.providerMetadata,
|
|
792
|
+
enlitt: {
|
|
793
|
+
operationId: operation.operationId,
|
|
794
|
+
effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// src/vercel/language-model.ts
|
|
801
|
+
function enlitt(target, options = {}) {
|
|
802
|
+
const client = chunk26SW63P5_cjs.createInternalClient(options);
|
|
803
|
+
if (target === protocol.ENLITT_STANDARD_MODEL) {
|
|
804
|
+
return createStandardLanguageModel(client);
|
|
805
|
+
}
|
|
806
|
+
return createPremiumLanguageModel(client, target);
|
|
807
|
+
}
|
|
808
|
+
var KNOWN_ENLITT_OPTION_KEYS = /* @__PURE__ */ new Set([
|
|
809
|
+
"idempotencyKey",
|
|
810
|
+
"maxCharge",
|
|
811
|
+
"price",
|
|
812
|
+
"reportingDeadlineMs",
|
|
813
|
+
"metadata"
|
|
814
|
+
]);
|
|
815
|
+
function readEnlittCallOptions(providerOptions, mode) {
|
|
816
|
+
const raw = providerOptions?.enlitt;
|
|
817
|
+
if (raw === void 0) {
|
|
818
|
+
if (mode === "premium") {
|
|
819
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
820
|
+
"A premium call requires providerOptions.enlitt.maxCharge, or a constant providerOptions.enlitt.price."
|
|
821
|
+
);
|
|
822
|
+
}
|
|
823
|
+
return {};
|
|
824
|
+
}
|
|
825
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
826
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError("providerOptions.enlitt must be an object.");
|
|
827
|
+
}
|
|
828
|
+
const input = raw;
|
|
829
|
+
for (const key of Object.keys(input)) {
|
|
830
|
+
if (!KNOWN_ENLITT_OPTION_KEYS.has(key)) {
|
|
831
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(`providerOptions.enlitt.${key} is not a recognized Enlitt option.`);
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
if (mode === "standard") {
|
|
835
|
+
for (const key of ["maxCharge", "price", "reportingDeadlineMs"]) {
|
|
836
|
+
if (input[key] !== void 0) {
|
|
837
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
838
|
+
`providerOptions.enlitt.${key} is a premium-only option; Enlitt's standard route chooses its own model and charges nothing the business declares.`
|
|
839
|
+
);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
const idempotencyKey = input.idempotencyKey !== void 0 ? chunk26SW63P5_cjs.assertIdempotencyKeyBase(input.idempotencyKey, "providerOptions.enlitt.idempotencyKey") : void 0;
|
|
844
|
+
const metadata = input.metadata !== void 0 ? chunk26SW63P5_cjs.assertCallerMetadata(input.metadata, "providerOptions.enlitt.metadata") : void 0;
|
|
845
|
+
let maxCharge;
|
|
846
|
+
let price;
|
|
847
|
+
let reportingDeadlineMs;
|
|
848
|
+
if (mode === "premium") {
|
|
849
|
+
({ maxCharge, price } = readPremiumPricing(input));
|
|
850
|
+
if (input.reportingDeadlineMs !== void 0) {
|
|
851
|
+
reportingDeadlineMs = chunk26SW63P5_cjs.assertReportingDeadlineMs(
|
|
852
|
+
input.reportingDeadlineMs,
|
|
853
|
+
"providerOptions.enlitt.reportingDeadlineMs"
|
|
854
|
+
);
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
return {
|
|
858
|
+
...idempotencyKey !== void 0 ? { idempotencyKey } : {},
|
|
859
|
+
...maxCharge !== void 0 ? { maxCharge } : {},
|
|
860
|
+
...price !== void 0 ? { price } : {},
|
|
861
|
+
...reportingDeadlineMs !== void 0 ? { reportingDeadlineMs } : {},
|
|
862
|
+
...metadata !== void 0 ? { metadata } : {}
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
function readPremiumPricing(input) {
|
|
866
|
+
const rawPrice = input.price;
|
|
867
|
+
let price;
|
|
868
|
+
if (rawPrice !== void 0) {
|
|
869
|
+
price = typeof rawPrice === "function" ? rawPrice : chunk26SW63P5_cjs.assertNanoUsd(rawPrice, "providerOptions.enlitt.price");
|
|
870
|
+
}
|
|
871
|
+
let maxCharge;
|
|
872
|
+
if (input.maxCharge !== void 0) {
|
|
873
|
+
maxCharge = chunk26SW63P5_cjs.assertNanoUsd(input.maxCharge, "providerOptions.enlitt.maxCharge");
|
|
874
|
+
}
|
|
875
|
+
if (maxCharge === void 0) {
|
|
876
|
+
if (typeof price === "string") {
|
|
877
|
+
maxCharge = price;
|
|
878
|
+
} else if (typeof price === "function") {
|
|
879
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
880
|
+
"providerOptions.enlitt.maxCharge is required when providerOptions.enlitt.price is a function."
|
|
881
|
+
);
|
|
882
|
+
} else {
|
|
883
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
884
|
+
"A premium call requires providerOptions.enlitt.maxCharge, or a constant providerOptions.enlitt.price."
|
|
885
|
+
);
|
|
886
|
+
}
|
|
887
|
+
} else if (typeof price === "string" && BigInt(price) > BigInt(maxCharge)) {
|
|
888
|
+
throw new chunk26SW63P5_cjs.EnlittConfigurationError(
|
|
889
|
+
"providerOptions.enlitt.price is above providerOptions.enlitt.maxCharge and could never be charged in full."
|
|
890
|
+
);
|
|
891
|
+
}
|
|
892
|
+
return { maxCharge, price };
|
|
893
|
+
}
|
|
894
|
+
function resolveCallId(options, params) {
|
|
895
|
+
const base = options.idempotencyKey ?? chunk26SW63P5_cjs.generateRandomCallId();
|
|
896
|
+
return chunk26SW63P5_cjs.assertCallId(`${base}.${params.prompt.length}`);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
Object.defineProperty(exports, "ENLITT_STANDARD_MODEL", {
|
|
900
|
+
enumerable: true,
|
|
901
|
+
get: function () { return protocol.ENLITT_STANDARD_MODEL; }
|
|
902
|
+
});
|
|
903
|
+
exports.enlitt = enlitt;
|
|
904
|
+
exports.perToken = perToken;
|
|
905
|
+
//# sourceMappingURL=chunk-C76K6HLL.cjs.map
|
|
906
|
+
//# sourceMappingURL=chunk-C76K6HLL.cjs.map
|