@desplega.ai/agent-swarm 1.100.2 → 1.100.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.
@@ -10,6 +10,10 @@ export interface InterpolateResult {
10
10
  unresolved: string[];
11
11
  }
12
12
 
13
+ export interface DeepInterpolateOptions {
14
+ preserveRawTokens?: boolean;
15
+ }
16
+
13
17
  export function interpolate(template: string, ctx: Record<string, unknown>): InterpolateResult {
14
18
  const unresolved: string[] = [];
15
19
  const result = template.replace(/\{\{([^}]+)\}\}/g, (_match, path: string) => {
@@ -43,18 +47,62 @@ function safeStringify(value: unknown): string {
43
47
  }
44
48
  }
45
49
 
50
+ /** Matches a string that is EXACTLY one {{path}} token with no surrounding text. */
51
+ const EXACT_TOKEN_RE = /^\{\{([^}]+)\}\}$/;
52
+
53
+ /**
54
+ * Resolve a dot-separated path against a context object.
55
+ * Returns `{ found: true, value }` on success, `{ found: false }` when any
56
+ * segment is missing or the traversal hits a non-object.
57
+ */
58
+ function resolvePath(
59
+ path: string,
60
+ ctx: Record<string, unknown>,
61
+ ): { found: true; value: unknown } | { found: false } {
62
+ const keys = path.trim().split(".");
63
+ let value: unknown = ctx;
64
+ for (const key of keys) {
65
+ if (value == null || typeof value !== "object") return { found: false };
66
+ value = (value as Record<string, unknown>)[key];
67
+ }
68
+ if (value === undefined) return { found: false };
69
+ return { found: true, value };
70
+ }
71
+
46
72
  /**
47
73
  * Deep-interpolate an arbitrary value tree (objects, arrays, strings).
74
+ *
75
+ * When `preserveRawTokens` is true and a string value is **exactly** one
76
+ * `{{path}}` token with no surrounding text, the resolved value is returned
77
+ * as-is (preserving object / array / number / boolean types). This is the
78
+ * "raw injection" path used by `swarm-script` node `config.args`.
79
+ *
80
+ * When a string contains multiple tokens or surrounding text (e.g.
81
+ * `"prefix-{{x}}"`) the existing string-interpolation path is used so the
82
+ * result remains a string.
83
+ *
48
84
  * Non-string leaves are passed through unchanged.
49
85
  */
50
86
  export function deepInterpolate(
51
87
  value: unknown,
52
88
  ctx: Record<string, unknown>,
89
+ options: DeepInterpolateOptions = {},
53
90
  ): { value: unknown; unresolved: string[] } {
54
91
  const allUnresolved: string[] = [];
55
92
 
56
93
  function walk(v: unknown): unknown {
57
94
  if (typeof v === "string") {
95
+ const exactMatch = options.preserveRawTokens ? EXACT_TOKEN_RE.exec(v) : null;
96
+ if (exactMatch?.[1]) {
97
+ const path = exactMatch[1].trim();
98
+ const resolved = resolvePath(path, ctx);
99
+ if (!resolved.found) {
100
+ allUnresolved.push(path);
101
+ return "";
102
+ }
103
+ return resolved.value;
104
+ }
105
+ // Multi-token or mixed string - fall back to string interpolation.
58
106
  const { result, unresolved } = interpolate(v, ctx);
59
107
  allUnresolved.push(...unresolved);
60
108
  return result;