@valerypopoff/rivet2-core 2.0.3 → 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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @valerypopoff/rivet2-core
2
2
 
3
- Shared graph model, execution engine, built-in nodes, serialization, provider integrations, plugin assembly, and runtime contracts for Rivet 2.0.
3
+ Shared graph model, execution engine, built-in nodes, serialization, provider integrations, plugin assembly, and runtime contracts for Rivet 2.
4
4
 
5
5
  This package is the foundation used by the Rivet app, Node runtime, CLI, app executor, and Trivet tooling.
6
6
 
@@ -11306,7 +11306,7 @@ var CodeNodeImpl = class extends NodeImpl {
11306
11306
  const missingOutputs = this.getOutputDefinitions().filter((output) => !(output.id in outputs));
11307
11307
  if (missingOutputs.length > 0) {
11308
11308
  throw new Error(
11309
- `Code node must return an object with output values for all outputs. To not run an output, return { "type": "control-flow-excluded", "value": undefiend }. To return undefined, return { "type": "any", "value": undefined }. Missing: ${missingOutputs.map((output) => output.id).join(", ")}`
11309
+ `Code node must return an object with output values for all outputs. To not run an output, return { "type": "control-flow-excluded", "value": undefined }. To return undefined, return { "type": "any", "value": undefined }. Missing: ${missingOutputs.map((output) => output.id).join(", ")}`
11310
11310
  );
11311
11311
  }
11312
11312
  return outputs;
@@ -14357,13 +14357,19 @@ var contextNode = nodeDefinition(ContextNodeImpl, "Context");
14357
14357
  // src/model/nodes/CoalesceNode.ts
14358
14358
  var import_non_secure32 = require("nanoid/non-secure");
14359
14359
  var import_ts_dedent30 = require("ts-dedent");
14360
+ var INPUT_PORT_ID_PATTERN = /^input(\d+)$/;
14361
+ var IGNORE_NULL_LABEL = "Ignore 'null'";
14362
+ var IGNORE_UNDEFINED_LABEL = "Ignore 'undefined'";
14360
14363
  var CoalesceNodeImpl = class extends NodeImpl {
14361
14364
  static create = () => {
14362
14365
  const chartNode = {
14363
14366
  type: "coalesce",
14364
14367
  title: "Coalesce",
14365
14368
  id: (0, import_non_secure32.nanoid)(),
14366
- data: {},
14369
+ data: {
14370
+ ignoreNull: false,
14371
+ ignoreUndefined: false
14372
+ },
14367
14373
  visualData: {
14368
14374
  x: 0,
14369
14375
  y: 0,
@@ -14398,10 +14404,36 @@ var CoalesceNodeImpl = class extends NodeImpl {
14398
14404
  }
14399
14405
  ];
14400
14406
  }
14407
+ getEditors() {
14408
+ return [
14409
+ {
14410
+ type: "toggle",
14411
+ label: IGNORE_NULL_LABEL,
14412
+ dataKey: "ignoreNull"
14413
+ },
14414
+ {
14415
+ type: "toggle",
14416
+ label: IGNORE_UNDEFINED_LABEL,
14417
+ dataKey: "ignoreUndefined"
14418
+ }
14419
+ ];
14420
+ }
14421
+ getBody() {
14422
+ const ignoredValues = [];
14423
+ if (this.data.ignoreNull) {
14424
+ ignoredValues.push(IGNORE_NULL_LABEL);
14425
+ }
14426
+ if (this.data.ignoreUndefined) {
14427
+ ignoredValues.push(IGNORE_UNDEFINED_LABEL);
14428
+ }
14429
+ return ignoredValues.length > 0 ? ignoredValues.join("\n") : void 0;
14430
+ }
14401
14431
  static getUIData() {
14402
14432
  return {
14403
14433
  infoBoxBody: import_ts_dedent30.dedent`
14404
- Takes in any number of inputs and outputs the first value that exists. Useful for consolidating branches after a Match node. This node can also "consume" the "Not Ran" value.
14434
+ Takes in any number of inputs and outputs the first value that is not "Not Ran". Useful for consolidating branches after a Match node.
14435
+
14436
+ Null and undefined input values are emitted by default, but the node can be configured to skip either value and continue checking later inputs.
14405
14437
  `,
14406
14438
  infoBoxTitle: "Coalesce Node",
14407
14439
  contextMenuTitle: "Coalesce",
@@ -14410,14 +14442,14 @@ var CoalesceNodeImpl = class extends NodeImpl {
14410
14442
  }
14411
14443
  #getInputPortCount(connections) {
14412
14444
  const inputNodeId = this.chartNode.id;
14413
- const inputConnections = connections.filter(
14414
- (connection) => connection.inputNodeId === inputNodeId && connection.inputId.startsWith("input")
14415
- );
14416
14445
  let maxInputNumber = 0;
14417
- for (const connection of inputConnections) {
14418
- const messageNumber = parseInt(connection.inputId.replace("input", ""), 10);
14419
- if (messageNumber > maxInputNumber) {
14420
- maxInputNumber = messageNumber;
14446
+ for (const connection of connections) {
14447
+ if (connection.inputNodeId !== inputNodeId) {
14448
+ continue;
14449
+ }
14450
+ const inputNumber = this.#getInputPortNumber(connection.inputId);
14451
+ if (inputNumber && inputNumber > maxInputNumber) {
14452
+ maxInputNumber = inputNumber;
14421
14453
  }
14422
14454
  }
14423
14455
  return maxInputNumber + 1;
@@ -14432,26 +14464,44 @@ var CoalesceNodeImpl = class extends NodeImpl {
14432
14464
  }
14433
14465
  };
14434
14466
  }
14435
- const inputCount = Object.keys(inputData).filter((key) => key.startsWith("input")).length;
14436
- const okInputValues = [];
14467
+ const inputCount = this.#getInputCountFromValues(inputData);
14437
14468
  for (let i = 1; i <= inputCount; i++) {
14438
14469
  const inputValue = inputData[`input${i}`];
14439
- if (inputValue && inputValue.type !== "control-flow-excluded" && unwrapDataValue(inputValue) != null) {
14440
- okInputValues.push(inputValue);
14470
+ if (inputValue && inputValue.type !== "control-flow-excluded" && !this.#shouldSkipInputValue(inputValue)) {
14471
+ return {
14472
+ ["output"]: inputValue
14473
+ };
14441
14474
  }
14442
14475
  }
14443
- if (okInputValues.length === 0) {
14444
- return {
14445
- ["output"]: {
14446
- type: "control-flow-excluded",
14447
- value: void 0
14448
- }
14449
- };
14450
- }
14451
14476
  return {
14452
- ["output"]: okInputValues[0]
14477
+ ["output"]: {
14478
+ type: "control-flow-excluded",
14479
+ value: void 0
14480
+ }
14453
14481
  };
14454
14482
  }
14483
+ #getInputCountFromValues(inputData) {
14484
+ let maxInputNumber = 0;
14485
+ for (const inputId of Object.keys(inputData)) {
14486
+ const inputNumber = this.#getInputPortNumber(inputId);
14487
+ if (inputNumber && inputNumber > maxInputNumber) {
14488
+ maxInputNumber = inputNumber;
14489
+ }
14490
+ }
14491
+ return maxInputNumber;
14492
+ }
14493
+ #getInputPortNumber(inputId) {
14494
+ const match10 = INPUT_PORT_ID_PATTERN.exec(inputId);
14495
+ if (!match10) {
14496
+ return void 0;
14497
+ }
14498
+ const inputNumber = Number(match10[1]);
14499
+ return Number.isSafeInteger(inputNumber) && inputNumber > 0 ? inputNumber : void 0;
14500
+ }
14501
+ #shouldSkipInputValue(inputValue) {
14502
+ const value = unwrapDataValue(inputValue).value;
14503
+ return this.data.ignoreNull === true && value === null || this.data.ignoreUndefined === true && value === void 0;
14504
+ }
14455
14505
  };
14456
14506
  var coalesceNode = nodeDefinition(CoalesceNodeImpl, "Coalesce");
14457
14507
 
@@ -16523,6 +16573,44 @@ var import_ts_dedent51 = require("ts-dedent");
16523
16573
  var DEFAULT_JSON_TEMPLATE = `{
16524
16574
  "key": "{{input}}"
16525
16575
  }`;
16576
+ function isEscapedCharacter(value, index) {
16577
+ let backslashCount = 0;
16578
+ for (let i = index - 1; i >= 0 && value[i] === "\\"; i--) {
16579
+ backslashCount++;
16580
+ }
16581
+ return backslashCount % 2 === 1;
16582
+ }
16583
+ function isUnescapedQuoteAt(value, index) {
16584
+ return index >= 0 && index < value.length && value[index] === '"' && !isEscapedCharacter(value, index);
16585
+ }
16586
+ function isInsideJsonString(value, index) {
16587
+ let insideString = false;
16588
+ for (let i = 0; i < index; i++) {
16589
+ if (isUnescapedQuoteAt(value, i)) {
16590
+ insideString = !insideString;
16591
+ }
16592
+ }
16593
+ return insideString;
16594
+ }
16595
+ function stringifyJsonValue(value) {
16596
+ if (value == null) {
16597
+ return "null";
16598
+ }
16599
+ return JSON.stringify(value);
16600
+ }
16601
+ function stringifyWholeQuotedJsonValue(value) {
16602
+ if (value == null) {
16603
+ return "null";
16604
+ }
16605
+ if (typeof value === "string") {
16606
+ return JSON.stringify(value);
16607
+ }
16608
+ return JSON.stringify(JSON.stringify(value));
16609
+ }
16610
+ function stringifyEmbeddedJsonStringFragment(value) {
16611
+ const fragment = value == null ? "null" : typeof value === "string" ? value : JSON.stringify(value) ?? "null";
16612
+ return JSON.stringify(fragment).slice(1, -1);
16613
+ }
16526
16614
  var ObjectNodeImpl = class extends NodeImpl {
16527
16615
  static create() {
16528
16616
  const chartNode = {
@@ -16599,12 +16687,11 @@ var ObjectNodeImpl = class extends NodeImpl {
16599
16687
  let result = "";
16600
16688
  let cursor = 0;
16601
16689
  for (const tokenSpan of tokenSpans) {
16602
- const hasLeadingQuote = tokenSpan.start > 0 && protectedBaseString[tokenSpan.start - 1] === '"';
16603
- const hasTrailingQuote = tokenSpan.end < protectedBaseString.length && protectedBaseString[tokenSpan.end] === '"';
16604
- const replacementStart = hasLeadingQuote ? tokenSpan.start - 1 : tokenSpan.start;
16605
- const replacementEnd = hasTrailingQuote ? tokenSpan.end + 1 : tokenSpan.end;
16690
+ const isInsideString = isInsideJsonString(protectedBaseString, tokenSpan.start);
16691
+ const isWholeQuotedToken = isInsideString && isUnescapedQuoteAt(protectedBaseString, tokenSpan.start - 1) && isUnescapedQuoteAt(protectedBaseString, tokenSpan.end);
16692
+ const replacementStart = isWholeQuotedToken ? tokenSpan.start - 1 : tokenSpan.start;
16693
+ const replacementEnd = isWholeQuotedToken ? tokenSpan.end + 1 : tokenSpan.end;
16606
16694
  const trimmedKey = getInterpolationTokenName(tokenSpan.rawInner) ?? tokenSpan.rawInner.trim();
16607
- const isQuoted = hasLeadingQuote;
16608
16695
  let value;
16609
16696
  const graphInputPrefix = "@graphInputs.";
16610
16697
  const contextPrefix = "@context.";
@@ -16615,23 +16702,17 @@ var ObjectNodeImpl = class extends NodeImpl {
16615
16702
  "graphInputs"
16616
16703
  );
16617
16704
  } else if (trimmedKey.startsWith(contextPrefix) && contextValues) {
16618
- value = resolveExpressionRawValue(
16619
- contextValues,
16620
- trimmedKey.substring(contextPrefix.length),
16621
- "context"
16622
- );
16705
+ value = resolveExpressionRawValue(contextValues, trimmedKey.substring(contextPrefix.length), "context");
16623
16706
  } else {
16624
16707
  value = values2[trimmedKey];
16625
16708
  }
16626
16709
  result += protectedBaseString.slice(cursor, replacementStart);
16627
- if (value == null) {
16628
- result += "null";
16629
- } else if (isQuoted && typeof value === "string") {
16630
- result += JSON.stringify(value);
16631
- } else if (isQuoted) {
16632
- result += JSON.stringify(JSON.stringify(value));
16710
+ if (isInsideString && !isWholeQuotedToken) {
16711
+ result += stringifyEmbeddedJsonStringFragment(value);
16712
+ } else if (isWholeQuotedToken) {
16713
+ result += stringifyWholeQuotedJsonValue(value);
16633
16714
  } else {
16634
- result += JSON.stringify(value);
16715
+ result += stringifyJsonValue(value);
16635
16716
  }
16636
16717
  cursor = replacementEnd;
16637
16718
  }