@alpic80/rivet-ai-sdk-provider 2.0.2 → 2.0.4

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.mjs CHANGED
@@ -986,6 +986,7 @@ var rivetFailedResponseHandler = createJsonErrorResponseHandler({
986
986
  });
987
987
 
988
988
  // src/utils.ts
989
+ var isDebug = process.env.RIVET_AI_PROVIDER_DEBUG === "true";
989
990
  function printObject(obj) {
990
991
  const seen = /* @__PURE__ */ new WeakSet();
991
992
  return JSON.stringify(
@@ -1006,6 +1007,11 @@ function printObject(obj) {
1006
1007
  // Indentation level for pretty-printing
1007
1008
  );
1008
1009
  }
1010
+ var debugLog = (...args) => {
1011
+ if (isDebug) {
1012
+ console.log(...args);
1013
+ }
1014
+ };
1009
1015
 
1010
1016
  // src/rivet-prepare-tools.ts
1011
1017
  async function prepareTools({
@@ -1018,7 +1024,7 @@ async function prepareTools({
1018
1024
  if (tools == null) {
1019
1025
  return { tools: void 0, toolSchemas: void 0, toolChoice: void 0, toolWarnings };
1020
1026
  }
1021
- console.log(`
1027
+ debugLog(`
1022
1028
 
1023
1029
  prepare tools input:${printObject(tools)}`);
1024
1030
  const rivetTools = [];
@@ -1048,7 +1054,7 @@ prepare tools input:${printObject(tools)}`);
1048
1054
  }
1049
1055
  }
1050
1056
  }
1051
- console.log(`rivetSchemas:${printObject(rivetSchemas)}`);
1057
+ debugLog(`rivetSchemas:${printObject(rivetSchemas)}`);
1052
1058
  if (toolChoice == null) {
1053
1059
  return { tools: rivetTools, toolSchemas: rivetSchemas, toolChoice: void 0, toolWarnings };
1054
1060
  }
@@ -1076,7 +1082,7 @@ prepare tools input:${printObject(tools)}`);
1076
1082
  }
1077
1083
 
1078
1084
  // src/version.ts
1079
- var VERSION = true ? "2.0.2" : "0.0.0-test";
1085
+ var VERSION = true ? "2.0.4" : "0.0.0-test";
1080
1086
 
1081
1087
  // src/post-to-api.ts
1082
1088
  var getOriginalFetch = () => globalThis.fetch;
@@ -1225,7 +1231,7 @@ function parseJsonEventStream({
1225
1231
  return stream.pipeThrough(new TransformStream({
1226
1232
  transform(chunk, controller) {
1227
1233
  const decoded = new TextDecoder().decode(chunk);
1228
- console.log(`Decoded:${printObject(decoded)}`);
1234
+ debugLog(`Decoded:${printObject(decoded)}`);
1229
1235
  controller.enqueue(decoded);
1230
1236
  }
1231
1237
  })).pipeThrough(new EventSourceParserStream()).pipeThrough(
@@ -1234,7 +1240,7 @@ function parseJsonEventStream({
1234
1240
  if (data === "[DONE]") {
1235
1241
  return;
1236
1242
  }
1237
- console.log(`Data to schema:${printObject(data)}`);
1243
+ debugLog(`Data to schema:${printObject(data)}`);
1238
1244
  controller.enqueue(await safeParseJSON({ text: data, schema }));
1239
1245
  }
1240
1246
  })
@@ -1290,13 +1296,13 @@ var RivetChatLanguageModel = class {
1290
1296
  runParams: {},
1291
1297
  chatConfig: {}
1292
1298
  };
1293
- console.log(`providerOptions:${printObject(providerOptions)}`);
1299
+ debugLog(`providerOptions:${printObject(providerOptions)}`);
1294
1300
  const options = (_a15 = await parseProviderOptions({
1295
1301
  provider: "rivet",
1296
1302
  providerOptions,
1297
1303
  schema: rivetLanguageModelOptions
1298
1304
  })) != null ? _a15 : emptyOptions;
1299
- console.log(`options:${printObject(options)}`);
1305
+ debugLog(`options:${printObject(options)}`);
1300
1306
  const {
1301
1307
  tools: rivetTools,
1302
1308
  toolSchemas: rivetSchemas,
@@ -1421,7 +1427,7 @@ var RivetChatLanguageModel = class {
1421
1427
  var _a15;
1422
1428
  const { args, warnings } = await this.getArgs(options);
1423
1429
  const body = args;
1424
- console.log(`body:${printObject(body)}`);
1430
+ debugLog(`body:${printObject(body)}`);
1425
1431
  const headers = combineHeaders(this.config.headers(), options.headers);
1426
1432
  let responseId = (_a15 = headers["X-Completion-Id"]) != null ? _a15 : this.generateId();
1427
1433
  const model = this.modelId;
@@ -1723,12 +1729,12 @@ var rivetChatChunkSchema = z3.object({
1723
1729
  });
1724
1730
  function createRivetEventSourceResponseHandler(id, model) {
1725
1731
  return createEventSourceResponseHandler(z3.any().transform((data) => {
1726
- console.log(`SSE event received:${printObject(data)}`);
1732
+ debugLog(`SSE event received:${printObject(data)}`);
1727
1733
  return mapRivetEventToOpenAIChunk(data, id, model);
1728
1734
  }));
1729
1735
  }
1730
1736
  function mapRivetEventToOpenAIChunk(eventData, id, model) {
1731
- var _a15, _b, _c, _d, _e, _f, _g, _h;
1737
+ var _a15, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1732
1738
  const eventType = eventData.type;
1733
1739
  switch (eventType) {
1734
1740
  case "partialOutput":
@@ -1750,6 +1756,8 @@ function mapRivetEventToOpenAIChunk(eventData, id, model) {
1750
1756
  usage: void 0
1751
1757
  };
1752
1758
  case "nodeFinish":
1759
+ const rawContent = (_g = (_f = (_c = (_b = eventData.outputs) == null ? void 0 : _b.response) == null ? void 0 : _c.value) != null ? _f : (_e = (_d = eventData.outputs) == null ? void 0 : _d.valueOutput) == null ? void 0 : _e.value) != null ? _g : void 0;
1760
+ const delta = typeof rawContent === "string" ? rawContent : rawContent !== void 0 ? JSON.stringify(rawContent) : void 0;
1753
1761
  return {
1754
1762
  id,
1755
1763
  created: Math.floor(Date.now() / 1e3),
@@ -1758,14 +1766,14 @@ function mapRivetEventToOpenAIChunk(eventData, id, model) {
1758
1766
  {
1759
1767
  delta: {
1760
1768
  role: "assistant",
1761
- content: (_b = eventData.delta) != null ? _b : "",
1769
+ content: delta != null ? delta : "",
1762
1770
  tool_calls: void 0
1763
1771
  },
1764
1772
  finish_reason: null,
1765
1773
  index: 0
1766
1774
  }
1767
1775
  ],
1768
- usage: void 0
1776
+ usage: toOpenAIUsage((_k = (_j = (_i = (_h = eventData == null ? void 0 : eventData.output) == null ? void 0 : _h.usages) == null ? void 0 : _i.value) == null ? void 0 : _j[0]) == null ? void 0 : _k.value)
1769
1777
  };
1770
1778
  case "done":
1771
1779
  return {
@@ -1776,16 +1784,17 @@ function mapRivetEventToOpenAIChunk(eventData, id, model) {
1776
1784
  {
1777
1785
  delta: {
1778
1786
  role: "assistant",
1779
- content: (_e = (_d = (_c = eventData.graphOutput) == null ? void 0 : _c.response) == null ? void 0 : _d.value) != null ? _e : "",
1787
+ content: (_n = (_m = (_l = eventData.graphOutput) == null ? void 0 : _l.response) == null ? void 0 : _m.value) != null ? _n : "",
1780
1788
  tool_calls: void 0
1781
1789
  },
1782
1790
  finish_reason: "stop",
1783
1791
  index: 0
1784
1792
  }
1785
1793
  ],
1786
- usage: toOpenAIUsage((_h = (_g = (_f = eventData.graphOutput.usages) == null ? void 0 : _f.value) == null ? void 0 : _g[0]) == null ? void 0 : _h.value)
1794
+ usage: toOpenAIUsage((_q = (_p = (_o = eventData.graphOutput.usages) == null ? void 0 : _o.value) == null ? void 0 : _p[0]) == null ? void 0 : _q.value)
1787
1795
  };
1788
1796
  default:
1797
+ debugLog(`falling on default for type:${printObject(eventType)}`);
1789
1798
  return {
1790
1799
  id,
1791
1800
  created: Math.floor(Date.now() / 1e3),
@@ -1806,7 +1815,7 @@ function mapRivetEventToOpenAIChunk(eventData, id, model) {
1806
1815
  }
1807
1816
  }
1808
1817
  var toOpenAIUsage = (usage) => {
1809
- console.log(`Usage to convert:${printObject(usage)}`);
1818
+ debugLog(`Usage to convert:${printObject(usage)}`);
1810
1819
  return usage ? {
1811
1820
  prompt_tokens: usage.prompt_tokens,
1812
1821
  completion_tokens: usage.completion_tokens,