@enlitt/sdk 0.1.0 → 0.1.1

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.
Files changed (72) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +3 -1
  3. package/dist/{chunk-C76K6HLL.cjs → chunk-2BGWYTJM.cjs} +240 -49
  4. package/dist/chunk-2BGWYTJM.cjs.map +1 -0
  5. package/dist/{chunk-VNVMEU34.js → chunk-NI6WH5FI.js} +3 -3
  6. package/dist/{chunk-VNVMEU34.js.map → chunk-NI6WH5FI.js.map} +1 -1
  7. package/dist/{chunk-6DPBYLA5.js → chunk-O6ASIA3X.js} +19 -8
  8. package/dist/chunk-O6ASIA3X.js.map +1 -0
  9. package/dist/{chunk-26SW63P5.cjs → chunk-PPOUNWNA.cjs} +19 -7
  10. package/dist/chunk-PPOUNWNA.cjs.map +1 -0
  11. package/dist/{chunk-SFI4RLNN.js → chunk-UVZMKGIL.js} +198 -7
  12. package/dist/chunk-UVZMKGIL.js.map +1 -0
  13. package/dist/{chunk-G4DTTV5E.cjs → chunk-YPA4R65T.cjs} +7 -7
  14. package/dist/{chunk-G4DTTV5E.cjs.map → chunk-YPA4R65T.cjs.map} +1 -1
  15. package/dist/core/errors.d.cts +13 -5
  16. package/dist/core/errors.d.ts +13 -5
  17. package/dist/core/errors.d.ts.map +1 -1
  18. package/dist/core/index.d.cts +1 -1
  19. package/dist/core/index.d.ts +1 -1
  20. package/dist/core/index.d.ts.map +1 -1
  21. package/dist/core/session.d.cts +15 -0
  22. package/dist/core/session.d.ts +15 -0
  23. package/dist/core/session.d.ts.map +1 -1
  24. package/dist/core/transport.d.cts +1 -1
  25. package/dist/core/transport.d.ts +1 -1
  26. package/dist/core.cjs +22 -18
  27. package/dist/core.js +2 -2
  28. package/dist/openai/client.d.cts +25 -1
  29. package/dist/openai/client.d.ts +25 -1
  30. package/dist/openai/client.d.ts.map +1 -1
  31. package/dist/openai/index.d.cts +1 -0
  32. package/dist/openai/index.d.ts +1 -0
  33. package/dist/openai/index.d.ts.map +1 -1
  34. package/dist/openai/media.d.cts +108 -0
  35. package/dist/openai/media.d.ts +109 -0
  36. package/dist/openai/media.d.ts.map +1 -0
  37. package/dist/openai.cjs +191 -47
  38. package/dist/openai.cjs.map +1 -1
  39. package/dist/openai.js +145 -1
  40. package/dist/openai.js.map +1 -1
  41. package/dist/server/callback.d.cts +2 -1
  42. package/dist/server/callback.d.ts +2 -1
  43. package/dist/server/callback.d.ts.map +1 -1
  44. package/dist/server/client-sign-in.d.cts +77 -0
  45. package/dist/server/client-sign-in.d.ts +78 -0
  46. package/dist/server/client-sign-in.d.ts.map +1 -0
  47. package/dist/server/index.d.cts +2 -1
  48. package/dist/server/index.d.ts +2 -1
  49. package/dist/server/index.d.ts.map +1 -1
  50. package/dist/server.cjs +555 -29
  51. package/dist/server.cjs.map +1 -1
  52. package/dist/server.js +529 -9
  53. package/dist/server.js.map +1 -1
  54. package/dist/vercel/index.d.cts +4 -2
  55. package/dist/vercel/index.d.ts +4 -2
  56. package/dist/vercel/index.d.ts.map +1 -1
  57. package/dist/vercel/language-model.d.cts +17 -2
  58. package/dist/vercel/language-model.d.ts +17 -2
  59. package/dist/vercel/language-model.d.ts.map +1 -1
  60. package/dist/vercel/premium-language-model.d.cts +30 -1
  61. package/dist/vercel/premium-language-model.d.ts +30 -1
  62. package/dist/vercel/premium-language-model.d.ts.map +1 -1
  63. package/dist/vercel/premium-media-model.d.cts +101 -0
  64. package/dist/vercel/premium-media-model.d.ts +102 -0
  65. package/dist/vercel/premium-media-model.d.ts.map +1 -0
  66. package/dist/vercel.cjs +5 -5
  67. package/dist/vercel.js +2 -2
  68. package/package.json +12 -2
  69. package/dist/chunk-26SW63P5.cjs.map +0 -1
  70. package/dist/chunk-6DPBYLA5.js.map +0 -1
  71. package/dist/chunk-C76K6HLL.cjs.map +0 -1
  72. package/dist/chunk-SFI4RLNN.js.map +0 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.1
4
+
5
+ - The Vercel adapter's premium wrappers accept Vercel AI SDK v4 models (`ai@7`, for example
6
+ `@ai-sdk/openai` 4.x) as well as v3 models (`ai@6`); a wrapped model keeps its version. Streaming
7
+ speech and transcription are not exposed through Enlitt.
8
+
9
+ - Premium image generation, text-to-speech, and speech-to-text: `enlitt()` from
10
+ `@enlitt/sdk/vercel` wraps Vercel AI SDK image, speech, and transcription models, and the
11
+ `@enlitt/sdk/openai` client adds premium-only `images.generate()`, `audio.speech.create()`, and
12
+ `audio.transcriptions.create()`. A media call is priced with a constant `price` or the
13
+ business's own price function, which receives the image, speech, or transcription result.
14
+
15
+ - `premium_not_enabled` (the application's premium switch is off) surfaces as
16
+ `EnlittAuthorizationError` with that code, from a premium start (no provider runs) or a terminal
17
+ report (the call is not charged).
18
+
19
+ - Client sign-in handoff for games and native apps: `createClientSignInHandler()` and
20
+ `createMemoryClientSignInStore()` from `@enlitt/sdk/server` hand a signed-in session to a client
21
+ with no cookie jar (loopback, redirect, or pairing).
22
+
23
+ - `enlittSessionMiddleware({ header: true })` also reads the session from the `Enlitt-Session`
24
+ header (`ENLITT_SESSION_HEADER_NAME`) for those clients. It is off by default; when on, a
25
+ non-empty header wins over the cookie.
26
+
27
+ - The login callback sends every outcome after a valid `state`, signed out included, to the stored
28
+ `returnTo` page instead of `defaultReturnTo`. A signed-out arrival carries no cookie.
29
+
3
30
  ## 0.1.0
4
31
 
5
32
  - First public release: the `@enlitt/sdk/vercel` and `@enlitt/sdk/openai` model adapters, the
package/README.md CHANGED
@@ -46,7 +46,7 @@ existing callers can omit the hook, and older SDKs ignore the added field.
46
46
  | Import | Purpose |
47
47
  | -------------------- | ------------------------------------------------------- |
48
48
  | `@enlitt/sdk/vercel` | Vercel AI SDK model wrapping and pricing. |
49
- | `@enlitt/sdk/openai` | Official OpenAI `responses.create()` adapter. |
49
+ | `@enlitt/sdk/openai` | Official OpenAI Responses, images, and audio adapter. |
50
50
  | `@enlitt/sdk/core` | Sessions, errors, `usage()`, and `withPremiumCharge()`. |
51
51
  | `@enlitt/sdk/server` | Login handlers and core/Vercel compatibility exports. |
52
52
 
@@ -54,6 +54,8 @@ All four entries are server-only. The root and browser entries currently
54
54
  export no integration APIs. Never put a business key in browser code.
55
55
 
56
56
  Premium pricing uses exact NANOUSD strings with `maxCharge` and `price`.
57
+ Premium also wraps image, speech, and transcription models, priced with a constant
58
+ or your own price function; there is no standard media model.
57
59
  Test applications consume real credits.
58
60
 
59
61
  Read the [developer guide](https://enlitt.com/docs), starting with
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var chunk6OWXYBBT_cjs = require('./chunk-6OWXYBBT.cjs');
4
- var chunk26SW63P5_cjs = require('./chunk-26SW63P5.cjs');
4
+ var chunkPPOUNWNA_cjs = require('./chunk-PPOUNWNA.cjs');
5
5
  var protocol = require('@enlitt/protocol');
6
6
 
7
7
  var RESPONSES_PATH = "/v1/responses";
@@ -23,16 +23,16 @@ function createStandardLanguageModel(client) {
23
23
  const options = readEnlittCallOptions(params.providerOptions, "standard");
24
24
  const callId = resolveCallId(options, params);
25
25
  const body = buildStandardRequest(params, options);
26
- const response = await chunk26SW63P5_cjs.requestEnlitt(client, {
26
+ const response = await chunkPPOUNWNA_cjs.requestEnlitt(client, {
27
27
  path: RESPONSES_PATH,
28
28
  body,
29
29
  callId,
30
30
  accept: "application/json",
31
31
  ...params.abortSignal !== void 0 ? { signal: params.abortSignal } : {}
32
32
  });
33
- const validated = protocol.validateOpenAIResponse(await chunk26SW63P5_cjs.readEnlittJson(response), { mode: "tolerant" });
33
+ const validated = protocol.validateOpenAIResponse(await chunkPPOUNWNA_cjs.readEnlittJson(response), { mode: "tolerant" });
34
34
  if (!validated.ok) {
35
- throw new chunk26SW63P5_cjs.EnlittError(
35
+ throw new chunkPPOUNWNA_cjs.EnlittError(
36
36
  "malformed_response",
37
37
  `Enlitt's response could not be validated: ${validated.error.message} (${validated.error.param}).`
38
38
  );
@@ -43,7 +43,7 @@ function createStandardLanguageModel(client) {
43
43
  const options = readEnlittCallOptions(params.providerOptions, "standard");
44
44
  const callId = resolveCallId(options, params);
45
45
  const body = buildStandardRequest(params, options, callId, true);
46
- const response = await chunk26SW63P5_cjs.requestEnlitt(client, {
46
+ const response = await chunkPPOUNWNA_cjs.requestEnlitt(client, {
47
47
  path: RESPONSES_PATH,
48
48
  body,
49
49
  callId,
@@ -51,7 +51,7 @@ function createStandardLanguageModel(client) {
51
51
  ...params.abortSignal !== void 0 ? { signal: params.abortSignal } : {}
52
52
  });
53
53
  if (response.body === null) {
54
- throw new chunk26SW63P5_cjs.EnlittError("malformed_response", "Enlitt's streaming response carried no body.");
54
+ throw new chunkPPOUNWNA_cjs.EnlittError("malformed_response", "Enlitt's streaming response carried no body.");
55
55
  }
56
56
  const events = chunk6OWXYBBT_cjs.createSseEventStream(response.body);
57
57
  return { stream: mapStandardStream(events, params.abortSignal) };
@@ -63,7 +63,7 @@ function hasProviderOptions(value) {
63
63
  }
64
64
  function rejectItemProviderOptions(value, where) {
65
65
  if (hasProviderOptions(value)) {
66
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
66
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
67
67
  `Enlitt's standard route does not support provider-specific options on ${where}; remove its providerOptions.`
68
68
  );
69
69
  }
@@ -76,7 +76,7 @@ function buildStandardRequest(params, options, callId, stream = false) {
76
76
  };
77
77
  const choice = params.toolChoice?.type;
78
78
  if (choice === "required" || choice === "tool") {
79
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
79
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
80
80
  "Enlitt's standard route supports only automatic tool choice; remove `toolChoice` or set it to auto or none."
81
81
  );
82
82
  }
@@ -94,7 +94,7 @@ function buildStandardRequest(params, options, callId, stream = false) {
94
94
  }
95
95
  const validated = protocol.validateEnlittStandardRequest(draft);
96
96
  if (!validated.ok) {
97
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
97
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
98
98
  `The call could not be mapped to Enlitt's standard request shape: ${validated.error.message} (${validated.error.param}).`
99
99
  );
100
100
  }
@@ -114,14 +114,14 @@ function rejectUnsupportedCallControls(params) {
114
114
  ];
115
115
  for (const [name, present] of unsupported) {
116
116
  if (present) {
117
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
117
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
118
118
  `Enlitt's standard route does not support \`${name}\`; remove it from the call.`
119
119
  );
120
120
  }
121
121
  }
122
122
  for (const namespace of Object.keys(params.providerOptions ?? {})) {
123
123
  if (namespace !== "enlitt") {
124
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
124
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
125
125
  `Enlitt's standard route does not support provider-specific options (providerOptions.${namespace}); remove them.`
126
126
  );
127
127
  }
@@ -147,7 +147,7 @@ function mapPromptToMessages(prompt) {
147
147
  case "tool": {
148
148
  for (const part of message.content) {
149
149
  if (part.type === "tool-approval-response") {
150
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
150
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
151
151
  "Enlitt's standard route does not support tool approval responses; remove the approval response."
152
152
  );
153
153
  }
@@ -166,7 +166,7 @@ function mapUserPart(part) {
166
166
  return { type: "text", text: part.text };
167
167
  }
168
168
  if (!part.mediaType.startsWith("image/")) {
169
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
169
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
170
170
  `Enlitt's standard route supports only text and image parts; "${part.mediaType}" files are not supported.`
171
171
  );
172
172
  }
@@ -187,7 +187,7 @@ function mapAssistantMessage(content) {
187
187
  break;
188
188
  case "tool-call": {
189
189
  if (part.providerExecuted === true) {
190
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
190
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
191
191
  "Enlitt's standard route does not support provider-executed tool calls in history."
192
192
  );
193
193
  }
@@ -201,7 +201,7 @@ function mapAssistantMessage(content) {
201
201
  case "file":
202
202
  case "reasoning":
203
203
  case "tool-result":
204
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
204
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
205
205
  `Enlitt's standard route does not support an assistant "${part.type}" part in history.`
206
206
  );
207
207
  }
@@ -221,7 +221,7 @@ function mapToolResultOutput(output) {
221
221
  case "error-json":
222
222
  return JSON.stringify(output.value);
223
223
  default:
224
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
224
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
225
225
  `Enlitt's standard route does not support a tool result output of type "${output.type}".`
226
226
  );
227
227
  }
@@ -229,13 +229,13 @@ function mapToolResultOutput(output) {
229
229
  function mapTools(tools) {
230
230
  return tools.map((tool) => {
231
231
  if (tool.type !== "function") {
232
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
232
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
233
233
  "Enlitt's standard route accepts only application-defined function tools, not provider-defined tools."
234
234
  );
235
235
  }
236
236
  rejectItemProviderOptions(tool.providerOptions, `the tool "${tool.name}"`);
237
237
  if (tool.strict !== void 0) {
238
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
238
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
239
239
  `Enlitt's standard route does not support strict tool schemas; remove \`strict\` from the tool "${tool.name}".`
240
240
  );
241
241
  }
@@ -359,7 +359,7 @@ function mapStandardStream(events, signal) {
359
359
  }
360
360
  function reject(controller, message) {
361
361
  terminated = true;
362
- controller.enqueue({ type: "error", error: new chunk26SW63P5_cjs.EnlittError("malformed_response", message) });
362
+ controller.enqueue({ type: "error", error: new chunkPPOUNWNA_cjs.EnlittError("malformed_response", message) });
363
363
  controller.close();
364
364
  void reader.cancel().catch(() => void 0);
365
365
  }
@@ -527,17 +527,18 @@ function translateEvent(event, openText, toolItems) {
527
527
 
528
528
  // src/vercel/premium-language-model.ts
529
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"));
530
+ const inputRate = BigInt(chunkPPOUNWNA_cjs.assertNanoUsd(rates.input, "perToken input rate"));
531
+ const outputRate = BigInt(chunkPPOUNWNA_cjs.assertNanoUsd(rates.output, "perToken output rate"));
532
532
  return (context) => {
533
533
  const inputTokens = BigInt(context.usage?.inputTokens.total ?? 0);
534
534
  const outputTokens = BigInt(context.usage?.outputTokens.total ?? 0);
535
535
  return (inputRate * inputTokens + outputRate * outputTokens).toString();
536
536
  };
537
537
  }
538
- function createPremiumLanguageModel(client, model) {
539
- return {
540
- specificationVersion: "v3",
538
+ function createPremiumLanguageModel(client, wrappedModel) {
539
+ const model = wrappedModel;
540
+ const wrapper = {
541
+ specificationVersion: wrappedModel.specificationVersion,
541
542
  provider: model.provider,
542
543
  modelId: model.modelId,
543
544
  supportedUrls: model.supportedUrls,
@@ -554,6 +555,7 @@ function createPremiumLanguageModel(client, model) {
554
555
  );
555
556
  }
556
557
  };
558
+ return wrapper;
557
559
  async function invoke(params, wrap) {
558
560
  const options = readEnlittCallOptions(params.providerOptions, "premium");
559
561
  const callId = resolveCallId(options, params);
@@ -576,23 +578,23 @@ async function evaluatePrice(client, price, context) {
576
578
  return price;
577
579
  }
578
580
  try {
579
- return chunk26SW63P5_cjs.assertNanoUsd(await price(context), "price() result");
581
+ return chunkPPOUNWNA_cjs.assertNanoUsd(await price(context), "price() result");
580
582
  } catch (thrown) {
581
583
  const detail = thrown instanceof Error ? thrown.message : "the price function failed";
582
584
  client.onTerminalReportError?.(
583
- thrown instanceof chunk26SW63P5_cjs.EnlittConfigurationError ? thrown : new chunk26SW63P5_cjs.EnlittConfigurationError(`The price function failed, so the call was charged "0": ${detail}`)
585
+ thrown instanceof chunkPPOUNWNA_cjs.EnlittConfigurationError ? thrown : new chunkPPOUNWNA_cjs.EnlittConfigurationError(`The price function failed, so the call was charged "0": ${detail}`)
584
586
  );
585
587
  return "0";
586
588
  }
587
589
  }
588
590
  function wrapPremiumGenerate(client, request, price, run) {
589
- return chunk26SW63P5_cjs.runPremiumCharge(client, request, async (charge, operation) => {
591
+ return chunkPPOUNWNA_cjs.runPremiumCharge(client, request, async (charge, operation) => {
590
592
  const startedAt = /* @__PURE__ */ new Date();
591
593
  let result;
592
594
  try {
593
595
  result = await run();
594
596
  } catch (thrown) {
595
- if (chunk26SW63P5_cjs.isCancellation(thrown, request.signal)) {
597
+ if (chunkPPOUNWNA_cjs.isCancellation(thrown, request.signal)) {
596
598
  const endedAt2 = /* @__PURE__ */ new Date();
597
599
  charge(
598
600
  await evaluatePrice(client, price, {
@@ -653,7 +655,7 @@ var CONTENT_PART_TYPES = /* @__PURE__ */ new Set([
653
655
  function wrapPremiumStream(client, request, price, run) {
654
656
  return new Promise((resolveStreamResult, rejectStreamResult) => {
655
657
  const settler = {};
656
- const lifecycle = chunk26SW63P5_cjs.runPremiumCharge(client, request, async (charge, operation) => {
658
+ const lifecycle = chunkPPOUNWNA_cjs.runPremiumCharge(client, request, async (charge, operation) => {
657
659
  const startedAt = /* @__PURE__ */ new Date();
658
660
  let result;
659
661
  try {
@@ -662,7 +664,7 @@ function wrapPremiumStream(client, request, price, run) {
662
664
  operation.describe({
663
665
  timing: timingFacts(startedAt, /* @__PURE__ */ new Date()),
664
666
  delivery: { outputStarted: false, outputCompleted: false },
665
- errorCategory: chunk26SW63P5_cjs.isCancellation(thrown, request.signal) ? "canceled" : "provider"
667
+ errorCategory: chunkPPOUNWNA_cjs.isCancellation(thrown, request.signal) ? "canceled" : "provider"
666
668
  });
667
669
  throw thrown;
668
670
  }
@@ -732,7 +734,7 @@ function wrapStream(client, request, price, result, charge, operation, startedAt
732
734
  try {
733
735
  chunk = await reader.read();
734
736
  } catch (thrown) {
735
- await settle(chunk26SW63P5_cjs.isCancellation(thrown, request.signal) ? "canceled" : "failed");
737
+ await settle(chunkPPOUNWNA_cjs.isCancellation(thrown, request.signal) ? "canceled" : "failed");
736
738
  controller.error(thrown);
737
739
  return;
738
740
  }
@@ -797,13 +799,202 @@ function attachEnlittMetadataToGenerateResult(result, operation) {
797
799
  };
798
800
  }
799
801
 
802
+ // src/vercel/premium-media-model.ts
803
+ function createPremiumMediaModel(client, model, kind) {
804
+ const labels = { id: model.provider, model: model.modelId };
805
+ if (kind === "image") {
806
+ const image = model;
807
+ const maxImagesPerCall = image.maxImagesPerCall;
808
+ const wrapped2 = {
809
+ specificationVersion: model.specificationVersion,
810
+ provider: image.provider,
811
+ modelId: image.modelId,
812
+ // Forwarded by reference. A function is bound so that a provider
813
+ // implementation reading `this` still sees its own model.
814
+ maxImagesPerCall: typeof maxImagesPerCall === "function" ? maxImagesPerCall.bind(image) : maxImagesPerCall,
815
+ doGenerate: (options) => runMediaCall(client, labels, options.providerOptions, options.abortSignal, {
816
+ kind: "image",
817
+ request: {
818
+ n: options.n,
819
+ ...options.size !== void 0 ? { size: options.size } : {},
820
+ ...options.aspectRatio !== void 0 ? { aspectRatio: options.aspectRatio } : {}
821
+ },
822
+ invoke: () => Promise.resolve(image.doGenerate(options)),
823
+ // `generateImage` keeps only an `images` array from each
824
+ // `providerMetadata` namespace, so an `enlitt` namespace would be
825
+ // dropped and, lacking `images`, would break its merge. The image
826
+ // result is therefore returned by identity.
827
+ mapResult: (result) => result,
828
+ facts: (result) => imageUsageFacts(result)
829
+ }),
830
+ // v4 only: whether the model accepts input files and masks. Forwarded
831
+ // by reference when present, like `maxImagesPerCall`.
832
+ ...forwardOptional(model, "supportsFileInputs"),
833
+ ...forwardOptional(model, "supportsMaskInputs")
834
+ };
835
+ return wrapped2;
836
+ }
837
+ const audio = model;
838
+ const wrapped = {
839
+ specificationVersion: model.specificationVersion,
840
+ provider: audio.provider,
841
+ modelId: audio.modelId,
842
+ doGenerate(options) {
843
+ if (isSpeechOptions(options)) {
844
+ return runMediaCall(client, labels, options.providerOptions, options.abortSignal, {
845
+ kind: "speech",
846
+ request: {
847
+ characters: [...options.text].length,
848
+ ...options.voice !== void 0 ? { voice: options.voice } : {},
849
+ ...options.outputFormat !== void 0 ? { outputFormat: options.outputFormat } : {}
850
+ },
851
+ invoke: () => Promise.resolve(audio.doGenerate(options)),
852
+ mapResult: attachEnlittMetadata,
853
+ facts: () => ({})
854
+ });
855
+ }
856
+ if (isTranscriptionOptions(options)) {
857
+ return runMediaCall(client, labels, options.providerOptions, options.abortSignal, {
858
+ kind: "transcription",
859
+ request: { mediaType: options.mediaType },
860
+ invoke: () => Promise.resolve(audio.doGenerate(options)),
861
+ mapResult: attachEnlittMetadata,
862
+ facts: () => ({})
863
+ });
864
+ }
865
+ return Promise.reject(
866
+ new chunkPPOUNWNA_cjs.EnlittConfigurationError(
867
+ "A premium audio call needs either a string `text` (speech) or `audio` with a string `mediaType` (transcription)."
868
+ )
869
+ );
870
+ }
871
+ };
872
+ return wrapped;
873
+ }
874
+ function forwardOptional(model, key) {
875
+ return key in model ? { [key]: model[key] } : {};
876
+ }
877
+ function isSpeechOptions(options) {
878
+ return typeof options.text === "string";
879
+ }
880
+ function isTranscriptionOptions(options) {
881
+ const audio = options.audio;
882
+ return (typeof audio === "string" || audio instanceof Uint8Array) && typeof options.mediaType === "string";
883
+ }
884
+ async function runMediaCall(client, labels, providerOptions, signal, spec) {
885
+ const options = readEnlittCallOptions(providerOptions, "premium");
886
+ const price = options.price;
887
+ const request = {
888
+ // Always present for a premium call: `readEnlittCallOptions` resolves it
889
+ // from the declared value or a constant price.
890
+ maxCharge: options.maxCharge,
891
+ provider: labels,
892
+ // One media call is one provider invocation, so a supplied key is the
893
+ // call ID itself (the OpenAI adapter's rule), and otherwise a fresh one.
894
+ callId: chunkPPOUNWNA_cjs.assertCallId(options.idempotencyKey ?? chunkPPOUNWNA_cjs.generateRandomCallId()),
895
+ ...options.reportingDeadlineMs !== void 0 ? { reportingDeadlineMs: options.reportingDeadlineMs } : {},
896
+ ...options.metadata !== void 0 ? { metadata: options.metadata } : {},
897
+ ...signal !== void 0 ? { signal } : {}
898
+ };
899
+ return chunkPPOUNWNA_cjs.runPremiumCharge(client, request, async (charge, operation) => {
900
+ const startedAt = /* @__PURE__ */ new Date();
901
+ let result;
902
+ try {
903
+ result = await spec.invoke();
904
+ } catch (thrown) {
905
+ const endedAt2 = /* @__PURE__ */ new Date();
906
+ if (chunkPPOUNWNA_cjs.isCancellation(thrown, signal)) {
907
+ charge(
908
+ await evaluatePrice(client, price, buildContext(spec, "canceled", void 0, startedAt, endedAt2, operation))
909
+ );
910
+ operation.describe({
911
+ timing: timingFacts(startedAt, endedAt2),
912
+ delivery: { outputStarted: false, outputCompleted: false },
913
+ errorCategory: "canceled"
914
+ });
915
+ } else {
916
+ operation.describe({
917
+ timing: timingFacts(startedAt, endedAt2),
918
+ delivery: { outputStarted: false, outputCompleted: false },
919
+ errorCategory: "provider"
920
+ });
921
+ }
922
+ throw thrown;
923
+ }
924
+ const endedAt = /* @__PURE__ */ new Date();
925
+ charge(await evaluatePrice(client, price, buildContext(spec, "completed", result, startedAt, endedAt, operation)));
926
+ operation.describe({
927
+ timing: timingFacts(startedAt, endedAt),
928
+ delivery: { outputStarted: true, outputCompleted: true },
929
+ ...spec.facts(result)
930
+ });
931
+ return spec.mapResult(result, operation);
932
+ });
933
+ }
934
+ function buildContext(spec, status, result, startedAt, endedAt, operation) {
935
+ const base = {
936
+ status,
937
+ startedAt,
938
+ endedAt,
939
+ durationMs: endedAt.getTime() - startedAt.getTime(),
940
+ effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
941
+ };
942
+ return {
943
+ ...base,
944
+ kind: spec.kind,
945
+ request: spec.request,
946
+ ...result !== void 0 ? { result } : {}
947
+ };
948
+ }
949
+ function imageUsageFacts(result) {
950
+ const usage = result.usage;
951
+ if (usage === void 0) return {};
952
+ const counts = {
953
+ ...usage.inputTokens !== void 0 ? { inputTokens: usage.inputTokens } : {},
954
+ ...usage.outputTokens !== void 0 ? { outputTokens: usage.outputTokens } : {}
955
+ };
956
+ return Object.keys(counts).length > 0 ? { usage: counts } : {};
957
+ }
958
+ function attachEnlittMetadata(result, operation) {
959
+ const withMetadata = result;
960
+ return {
961
+ ...withMetadata,
962
+ providerMetadata: {
963
+ ...withMetadata.providerMetadata,
964
+ enlitt: {
965
+ operationId: operation.operationId,
966
+ effectiveMaxChargeNanoUsd: operation.effectiveMaxChargeNanoUsd
967
+ }
968
+ }
969
+ };
970
+ }
971
+
800
972
  // src/vercel/language-model.ts
801
973
  function enlitt(target, options = {}) {
802
- const client = chunk26SW63P5_cjs.createInternalClient(options);
974
+ const client = chunkPPOUNWNA_cjs.createInternalClient(options);
803
975
  if (target === protocol.ENLITT_STANDARD_MODEL) {
804
976
  return createStandardLanguageModel(client);
805
977
  }
806
- return createPremiumLanguageModel(client, target);
978
+ const model = target;
979
+ if (!isSupportedModel(model)) {
980
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
981
+ 'enlitt() accepts "enlitt-standard" or a Vercel AI SDK v3 or v4 language, image, speech, or transcription model.'
982
+ );
983
+ }
984
+ if (typeof model.doStream === "function" && "supportedUrls" in model) {
985
+ return createPremiumLanguageModel(client, target);
986
+ }
987
+ if ("maxImagesPerCall" in model) {
988
+ return createPremiumMediaModel(client, target, "image");
989
+ }
990
+ return createPremiumMediaModel(
991
+ client,
992
+ target,
993
+ "audio"
994
+ );
995
+ }
996
+ function isSupportedModel(model) {
997
+ return (model.specificationVersion === "v3" || model.specificationVersion === "v4") && typeof model.provider === "string" && typeof model.modelId === "string" && typeof model.doGenerate === "function";
807
998
  }
808
999
  var KNOWN_ENLITT_OPTION_KEYS = /* @__PURE__ */ new Set([
809
1000
  "idempotencyKey",
@@ -816,39 +1007,39 @@ function readEnlittCallOptions(providerOptions, mode) {
816
1007
  const raw = providerOptions?.enlitt;
817
1008
  if (raw === void 0) {
818
1009
  if (mode === "premium") {
819
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
1010
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
820
1011
  "A premium call requires providerOptions.enlitt.maxCharge, or a constant providerOptions.enlitt.price."
821
1012
  );
822
1013
  }
823
1014
  return {};
824
1015
  }
825
1016
  if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
826
- throw new chunk26SW63P5_cjs.EnlittConfigurationError("providerOptions.enlitt must be an object.");
1017
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError("providerOptions.enlitt must be an object.");
827
1018
  }
828
1019
  const input = raw;
829
1020
  for (const key of Object.keys(input)) {
830
1021
  if (!KNOWN_ENLITT_OPTION_KEYS.has(key)) {
831
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(`providerOptions.enlitt.${key} is not a recognized Enlitt option.`);
1022
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(`providerOptions.enlitt.${key} is not a recognized Enlitt option.`);
832
1023
  }
833
1024
  }
834
1025
  if (mode === "standard") {
835
1026
  for (const key of ["maxCharge", "price", "reportingDeadlineMs"]) {
836
1027
  if (input[key] !== void 0) {
837
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
1028
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
838
1029
  `providerOptions.enlitt.${key} is a premium-only option; Enlitt's standard route chooses its own model and charges nothing the business declares.`
839
1030
  );
840
1031
  }
841
1032
  }
842
1033
  }
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;
1034
+ const idempotencyKey = input.idempotencyKey !== void 0 ? chunkPPOUNWNA_cjs.assertIdempotencyKeyBase(input.idempotencyKey, "providerOptions.enlitt.idempotencyKey") : void 0;
1035
+ const metadata = input.metadata !== void 0 ? chunkPPOUNWNA_cjs.assertCallerMetadata(input.metadata, "providerOptions.enlitt.metadata") : void 0;
845
1036
  let maxCharge;
846
1037
  let price;
847
1038
  let reportingDeadlineMs;
848
1039
  if (mode === "premium") {
849
1040
  ({ maxCharge, price } = readPremiumPricing(input));
850
1041
  if (input.reportingDeadlineMs !== void 0) {
851
- reportingDeadlineMs = chunk26SW63P5_cjs.assertReportingDeadlineMs(
1042
+ reportingDeadlineMs = chunkPPOUNWNA_cjs.assertReportingDeadlineMs(
852
1043
  input.reportingDeadlineMs,
853
1044
  "providerOptions.enlitt.reportingDeadlineMs"
854
1045
  );
@@ -866,34 +1057,34 @@ function readPremiumPricing(input) {
866
1057
  const rawPrice = input.price;
867
1058
  let price;
868
1059
  if (rawPrice !== void 0) {
869
- price = typeof rawPrice === "function" ? rawPrice : chunk26SW63P5_cjs.assertNanoUsd(rawPrice, "providerOptions.enlitt.price");
1060
+ price = typeof rawPrice === "function" ? rawPrice : chunkPPOUNWNA_cjs.assertNanoUsd(rawPrice, "providerOptions.enlitt.price");
870
1061
  }
871
1062
  let maxCharge;
872
1063
  if (input.maxCharge !== void 0) {
873
- maxCharge = chunk26SW63P5_cjs.assertNanoUsd(input.maxCharge, "providerOptions.enlitt.maxCharge");
1064
+ maxCharge = chunkPPOUNWNA_cjs.assertNanoUsd(input.maxCharge, "providerOptions.enlitt.maxCharge");
874
1065
  }
875
1066
  if (maxCharge === void 0) {
876
1067
  if (typeof price === "string") {
877
1068
  maxCharge = price;
878
1069
  } else if (typeof price === "function") {
879
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
1070
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
880
1071
  "providerOptions.enlitt.maxCharge is required when providerOptions.enlitt.price is a function."
881
1072
  );
882
1073
  } else {
883
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
1074
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
884
1075
  "A premium call requires providerOptions.enlitt.maxCharge, or a constant providerOptions.enlitt.price."
885
1076
  );
886
1077
  }
887
1078
  } else if (typeof price === "string" && BigInt(price) > BigInt(maxCharge)) {
888
- throw new chunk26SW63P5_cjs.EnlittConfigurationError(
1079
+ throw new chunkPPOUNWNA_cjs.EnlittConfigurationError(
889
1080
  "providerOptions.enlitt.price is above providerOptions.enlitt.maxCharge and could never be charged in full."
890
1081
  );
891
1082
  }
892
1083
  return { maxCharge, price };
893
1084
  }
894
1085
  function resolveCallId(options, params) {
895
- const base = options.idempotencyKey ?? chunk26SW63P5_cjs.generateRandomCallId();
896
- return chunk26SW63P5_cjs.assertCallId(`${base}.${params.prompt.length}`);
1086
+ const base = options.idempotencyKey ?? chunkPPOUNWNA_cjs.generateRandomCallId();
1087
+ return chunkPPOUNWNA_cjs.assertCallId(`${base}.${params.prompt.length}`);
897
1088
  }
898
1089
 
899
1090
  Object.defineProperty(exports, "ENLITT_STANDARD_MODEL", {
@@ -902,5 +1093,5 @@ Object.defineProperty(exports, "ENLITT_STANDARD_MODEL", {
902
1093
  });
903
1094
  exports.enlitt = enlitt;
904
1095
  exports.perToken = perToken;
905
- //# sourceMappingURL=chunk-C76K6HLL.cjs.map
906
- //# sourceMappingURL=chunk-C76K6HLL.cjs.map
1096
+ //# sourceMappingURL=chunk-2BGWYTJM.cjs.map
1097
+ //# sourceMappingURL=chunk-2BGWYTJM.cjs.map