@alpic80/rivet-core 1.24.0-aidon.3 → 1.24.0-aidon.5

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.
@@ -21882,12 +21882,16 @@ var dotProductSimilarity = (a, b) => {
21882
21882
  };
21883
21883
 
21884
21884
  // src/api/streaming.ts
21885
+ function nodeMatches(spec, event) {
21886
+ var _a;
21887
+ return (spec == null ? void 0 : spec.includes(event.node.id)) || (spec == null ? void 0 : spec.includes((_a = event.node.data) == null ? void 0 : _a.id)) || (spec == null ? void 0 : spec.includes(event.node.title));
21888
+ }
21885
21889
  async function* getProcessorEvents(processor, spec) {
21886
- var _a, _b, _c, _d, _e, _f;
21887
21890
  const previousIndexes = /* @__PURE__ */ new Map();
21891
+ const usages = [];
21888
21892
  for await (const event of processor.events()) {
21889
21893
  if (event.type === "partialOutput") {
21890
- if (spec.partialOutputs === true || ((_a = spec.partialOutputs) == null ? void 0 : _a.includes(event.node.id)) || ((_b = spec.partialOutputs) == null ? void 0 : _b.includes(event.node.title))) {
21894
+ if (spec.partialOutputs === true || nodeMatches(spec.partialOutputs, event)) {
21891
21895
  const currentOutput = coerceType(event.outputs["response"], "string");
21892
21896
  const delta = currentOutput.slice(previousIndexes.get(event.node.id) ?? 0);
21893
21897
  yield {
@@ -21900,9 +21904,26 @@ async function* getProcessorEvents(processor, spec) {
21900
21904
  }
21901
21905
  } else if (event.type === "done") {
21902
21906
  if (spec.done) {
21907
+ const results = event.results;
21908
+ if (!spec.exposeCost) {
21909
+ delete results.cost;
21910
+ }
21911
+ if (!spec.exposeUsage) {
21912
+ delete results.requestTokens;
21913
+ delete results.responseTokens;
21914
+ } else if (usages.length) {
21915
+ const usageOutput = {
21916
+ type: "any[]",
21917
+ value: usages
21918
+ };
21919
+ results["usages"] = usageOutput;
21920
+ }
21921
+ if (spec.removeFinalOutput) {
21922
+ delete results.output;
21923
+ }
21903
21924
  yield {
21904
21925
  type: "done",
21905
- graphOutput: event.results
21926
+ graphOutput: results
21906
21927
  };
21907
21928
  }
21908
21929
  } else if (event.type === "error") {
@@ -21913,7 +21934,7 @@ async function* getProcessorEvents(processor, spec) {
21913
21934
  };
21914
21935
  }
21915
21936
  } else if (event.type === "nodeStart") {
21916
- if (spec.nodeStart === true || ((_c = spec.nodeStart) == null ? void 0 : _c.includes(event.node.id)) || ((_d = spec.nodeStart) == null ? void 0 : _d.includes(event.node.title))) {
21937
+ if (spec.nodeStart === true || nodeMatches(spec.nodeStart, event)) {
21917
21938
  yield {
21918
21939
  type: "nodeStart",
21919
21940
  inputs: event.inputs,
@@ -21922,7 +21943,13 @@ async function* getProcessorEvents(processor, spec) {
21922
21943
  };
21923
21944
  }
21924
21945
  } else if (event.type === "nodeFinish") {
21925
- if (spec.nodeFinish === true || ((_e = spec.nodeFinish) == null ? void 0 : _e.includes(event.node.id)) || ((_f = spec.nodeFinish) == null ? void 0 : _f.includes(event.node.title))) {
21946
+ if (spec.exposeUsage) {
21947
+ const usage = event.outputs["usage"];
21948
+ if (usage !== void 0) {
21949
+ usages.push(usage);
21950
+ }
21951
+ }
21952
+ if (spec.nodeFinish === true || nodeMatches(spec.nodeFinish, event)) {
21926
21953
  yield {
21927
21954
  type: "nodeFinish",
21928
21955
  outputs: event.outputs,
@@ -21955,19 +21982,53 @@ data: ${JSON.stringify(data)}
21955
21982
  }
21956
21983
  });
21957
21984
  }
21958
- function getSingleNodeStream(processor, nodeIdOrTitle) {
21985
+ function getSingleNodeStream(processor, arg) {
21986
+ let spec;
21987
+ if (typeof arg === "string") {
21988
+ const nodeIdOrTitle = arg;
21989
+ spec = {
21990
+ partialOutputs: [nodeIdOrTitle],
21991
+ nodeFinish: [nodeIdOrTitle]
21992
+ };
21993
+ } else {
21994
+ spec = arg;
21995
+ }
21959
21996
  return new ReadableStream({
21960
21997
  async start(controller) {
21998
+ var _a;
21961
21999
  try {
21962
- for await (const event of getProcessorEvents(processor, {
21963
- partialOutputs: [nodeIdOrTitle],
21964
- nodeFinish: [nodeIdOrTitle]
21965
- })) {
21966
- if (event.type === "partialOutput" && (event.nodeId === nodeIdOrTitle || event.nodeTitle === nodeIdOrTitle)) {
22000
+ for await (const event of getProcessorEvents(processor, spec)) {
22001
+ if (event.type === "partialOutput") {
21967
22002
  controller.enqueue(`data: ${JSON.stringify(event.delta)}
21968
22003
 
21969
22004
  `);
21970
- } else if (event.type === "nodeFinish" && (event.nodeId === nodeIdOrTitle || event.nodeTitle === nodeIdOrTitle)) {
22005
+ } else if (event.type === "nodeFinish") {
22006
+ const value = (_a = event.outputs["valueOutput"]) == null ? void 0 : _a.value;
22007
+ controller.enqueue(`data: ${JSON.stringify(value)}
22008
+
22009
+ `);
22010
+ } else if (event.type === "error") {
22011
+ controller.enqueue(`error: ${JSON.stringify(event.error)}
22012
+
22013
+ `);
22014
+ } else if (event.type === "done") {
22015
+ if (spec.done) {
22016
+ const results = event.graphOutput;
22017
+ if (!spec.exposeCost) {
22018
+ delete results.cost;
22019
+ }
22020
+ if (!spec.exposeUsage) {
22021
+ delete results.requestTokens;
22022
+ delete results.responseTokens;
22023
+ delete results.usages;
22024
+ }
22025
+ if (spec.removeFinalOutput) {
22026
+ delete results.output;
22027
+ }
22028
+ controller.enqueue(`graphOutput: ${JSON.stringify(results)}
22029
+
22030
+ `);
22031
+ }
21971
22032
  controller.close();
21972
22033
  }
21973
22034
  }