@silmaril-security/sdk 0.8.0 → 0.8.2

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.js CHANGED
@@ -263,6 +263,10 @@ function record(value) {
263
263
  function validThreadId(value) {
264
264
  return typeof value === "string" && value.trim().length > 0 ? value : void 0;
265
265
  }
266
+ function threadIds(first, second) {
267
+ if (first === void 0) return second === void 0 ? NO_THREAD_IDS : Object.freeze([second]);
268
+ return Object.freeze(second === void 0 || second === first ? [first] : [first, second]);
269
+ }
266
270
  function nativeThreadId(runtime, metadata, inherited, currentConfig) {
267
271
  const explicit = record(record(runtime)?.configurable);
268
272
  const current = record(record(currentConfig)?.configurable);
@@ -278,10 +282,11 @@ function rejectConversationOptions(options) {
278
282
  throw new TypeError("conversationId/conversation_id was removed. Pass the native configurable.thread_id when invoking the agent or runnable instead.");
279
283
  }
280
284
  }
281
- var RunThreadIdentity;
285
+ var NO_THREAD_IDS, RunThreadIdentity;
282
286
  var init_thread_identity = __esm({
283
287
  "src/adapters/thread-identity.ts"() {
284
288
  "use strict";
289
+ NO_THREAD_IDS = Object.freeze([]);
285
290
  RunThreadIdentity = class {
286
291
  constructor(boundIdentity, currentConfig = () => void 0) {
287
292
  this.boundIdentity = boundIdentity;
@@ -297,10 +302,50 @@ var init_thread_identity = __esm({
297
302
  const oldestRoot = [...this.runs].find(([, run]) => run.parentRunId === void 0 || !this.runs.has(run.parentRunId))?.[0] ?? this.runs.keys().next().value;
298
303
  if (oldestRoot !== void 0) discarded.push(...this.finish(oldestRoot));
299
304
  }
300
- const inherited = parentRunId === void 0 ? void 0 : this.runs.get(parentRunId)?.threadId;
305
+ const parent = parentRunId === void 0 ? void 0 : this.runs.get(parentRunId);
306
+ const inherited = parent?.threadId;
301
307
  const exposed = record(metadata);
302
- const threadId = this.boundIdentity && (parentRunId === void 0 || !this.runs.has(parentRunId)) ? this.boundIdentity.threadId : exposed && Object.prototype.hasOwnProperty.call(exposed, "thread_id") ? typeof exposed.langgraph_node === "string" ? nativeThreadId(void 0, exposed, inherited, this.currentConfig()) : validThreadId(exposed.thread_id) : inherited;
303
- this.runs.set(runId, { threadId, parentRunId });
308
+ const hasExposedThread = exposed !== void 0 && Object.prototype.hasOwnProperty.call(exposed, "thread_id");
309
+ const candidate = hasExposedThread ? validThreadId(exposed.thread_id) : void 0;
310
+ let threadId;
311
+ let untrustedThreadIds;
312
+ let graphProven;
313
+ if (this.boundIdentity && parent === void 0) {
314
+ threadId = this.boundIdentity.threadId;
315
+ untrustedThreadIds = threadIds(threadId, candidate);
316
+ graphProven = false;
317
+ } else if (exposed && (typeof exposed.langgraph_node === "string" || typeof exposed.langgraph_step === "number")) {
318
+ threadId = nativeThreadId(void 0, exposed, inherited, this.currentConfig());
319
+ untrustedThreadIds = NO_THREAD_IDS;
320
+ graphProven = true;
321
+ } else if (hasExposedThread) {
322
+ if (candidate === void 0) {
323
+ threadId = void 0;
324
+ untrustedThreadIds = NO_THREAD_IDS;
325
+ graphProven = false;
326
+ } else if (parent?.graphProven && candidate === inherited) {
327
+ threadId = inherited;
328
+ untrustedThreadIds = NO_THREAD_IDS;
329
+ graphProven = true;
330
+ } else if (parent && (candidate === inherited || parent.untrustedThreadIds.includes(candidate))) {
331
+ threadId = void 0;
332
+ untrustedThreadIds = parent.untrustedThreadIds;
333
+ graphProven = false;
334
+ } else {
335
+ threadId = candidate;
336
+ untrustedThreadIds = threadIds(candidate);
337
+ graphProven = false;
338
+ }
339
+ } else if (parent?.graphProven) {
340
+ threadId = inherited;
341
+ untrustedThreadIds = NO_THREAD_IDS;
342
+ graphProven = true;
343
+ } else {
344
+ threadId = void 0;
345
+ untrustedThreadIds = parent?.untrustedThreadIds ?? NO_THREAD_IDS;
346
+ graphProven = false;
347
+ }
348
+ this.runs.set(runId, { threadId, untrustedThreadIds, parentRunId, graphProven });
304
349
  if (parentRunId !== void 0 && parentRunId !== runId) {
305
350
  const children = this.children.get(parentRunId) ?? /* @__PURE__ */ new Set();
306
351
  children.add(runId);
@@ -773,7 +818,34 @@ async function protectLangChainRunnable(firewall, runnable, options = {}) {
773
818
  });
774
819
  }
775
820
  }
776
- return new ProtectedBinding({ bound: runnable, config: {}, configFactories });
821
+ const bindings = /* @__PURE__ */ new Set();
822
+ const installBoundary = (candidate) => {
823
+ if (!RunnableBinding.isRunnableBinding(candidate)) {
824
+ return new ProtectedBinding({ bound: candidate, config: {}, configFactories });
825
+ }
826
+ if (bindings.has(candidate)) throw new TypeError("Cannot protect cyclic runnable bindings");
827
+ bindings.add(candidate);
828
+ const binding = candidate;
829
+ const Binding = binding.constructor;
830
+ return new Binding({ ...binding, bound: installBoundary(binding.bound) });
831
+ };
832
+ class ComposableBinding extends RunnableBinding {
833
+ withConfig(config) {
834
+ return new ComposableBinding({ bound: this.bound, config: mergeConfigs(this.config, config) });
835
+ }
836
+ withListeners(fields) {
837
+ return new RunnableBinding({ bound: this, config: {} }).withListeners(fields);
838
+ }
839
+ withRetry(fields) {
840
+ return new RunnableRetry({
841
+ bound: this,
842
+ config: {},
843
+ ...fields?.stopAfterAttempt === void 0 ? {} : { maxAttemptNumber: fields.stopAfterAttempt },
844
+ ...fields?.onFailedAttempt === void 0 ? {} : { onFailedAttempt: fields.onFailedAttempt }
845
+ });
846
+ }
847
+ }
848
+ return new ComposableBinding({ bound: installBoundary(runnable), config: {} });
777
849
  }
778
850
  var handlerCleanups, USER_ROLES;
779
851
  var init_langchain = __esm({
@@ -1151,7 +1223,7 @@ function sanitizeText(text) {
1151
1223
  }
1152
1224
 
1153
1225
  // src/firewall.ts
1154
- var SDK_VERSION = "0.8.0";
1226
+ var SDK_VERSION = "0.8.2";
1155
1227
  var DEFAULT_TIMEOUT_MS = 1e4;
1156
1228
  var DEFAULT_MAX_RETRIES = 5;
1157
1229
  var MAX_BACKOFF_SECONDS = 30;