@anuma/sdk 10.5.4 → 10.6.1

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.
@@ -6818,6 +6818,7 @@ function count(value) {
6818
6818
 
6819
6819
  // src/lib/chat/resumeStream.ts
6820
6820
  init_clientConfig();
6821
+ init_redactor();
6821
6822
 
6822
6823
  // src/lib/chat/toolLoop.ts
6823
6824
  init_clientConfig();
@@ -11164,16 +11165,34 @@ async function resumeStream(options) {
11164
11165
  onData,
11165
11166
  onThinking,
11166
11167
  onFinish,
11167
- onError
11168
+ onError,
11169
+ piiRedactor
11168
11170
  } = options;
11169
11171
  const strategy = getStrategy(handle.apiType);
11170
11172
  const accumulator = createStreamAccumulator(handle.model || void 0);
11173
+ const contentDeAnon = piiRedactor && onData ? createStreamingDeAnonymizer(piiRedactor, onData) : null;
11174
+ const thinkingDeAnon = piiRedactor && onThinking ? createStreamingDeAnonymizer(piiRedactor, onThinking) : null;
11171
11175
  const contentSmoother = new StreamSmoother((text16) => {
11172
- if (onData) onData(text16);
11176
+ if (contentDeAnon) contentDeAnon.push(text16);
11177
+ else if (onData) onData(text16);
11173
11178
  }, smoothing);
11174
11179
  const thinkingSmoother = new StreamSmoother((text16) => {
11175
- if (onThinking) onThinking(text16);
11180
+ if (thinkingDeAnon) thinkingDeAnon.push(text16);
11181
+ else if (onThinking) onThinking(text16);
11176
11182
  }, smoothing);
11183
+ const flushOutput = () => {
11184
+ contentSmoother.flush();
11185
+ thinkingSmoother.flush();
11186
+ contentDeAnon?.flush();
11187
+ thinkingDeAnon?.flush();
11188
+ };
11189
+ const buildFinal = () => strategy.buildFinalResponse(
11190
+ piiRedactor ? {
11191
+ ...accumulator,
11192
+ content: piiRedactor.deAnonymize(accumulator.content),
11193
+ thinking: piiRedactor.deAnonymize(accumulator.thinking)
11194
+ } : accumulator
11195
+ );
11177
11196
  const idleController = new AbortController();
11178
11197
  let watchdog;
11179
11198
  let idleFired = false;
@@ -11218,9 +11237,8 @@ async function resumeStream(options) {
11218
11237
  if (signal) signal.removeEventListener("abort", onCallerAbort);
11219
11238
  };
11220
11239
  const buildInterrupted = (message) => {
11221
- contentSmoother.flush();
11222
- thinkingSmoother.flush();
11223
- const data = accumulator.toolCalls.size > 0 ? stripToolCalls(strategy.buildFinalResponse(accumulator)) : strategy.buildFinalResponse(accumulator);
11240
+ flushOutput();
11241
+ const data = accumulator.toolCalls.size > 0 ? stripToolCalls(buildFinal()) : buildFinal();
11224
11242
  return { data, error: message, interrupted: true };
11225
11243
  };
11226
11244
  if (combinedSignal.aborted) {
@@ -11268,18 +11286,16 @@ async function resumeStream(options) {
11268
11286
  const err = replayErr instanceof Error ? replayErr : new Error(String(replayErr));
11269
11287
  const statusCode = parseSseStatusCode(err);
11270
11288
  if (statusCode === 410) {
11271
- contentSmoother.flush();
11272
- thinkingSmoother.flush();
11289
+ flushOutput();
11273
11290
  throw new StreamExpiredError(handle.inferenceId);
11274
11291
  }
11275
11292
  if (err.name === "AbortError" || combinedSignal.aborted) {
11276
11293
  return buildInterrupted(abortMessage());
11277
11294
  }
11278
- contentSmoother.flush();
11279
- thinkingSmoother.flush();
11295
+ flushOutput();
11280
11296
  if (onError) onError(err);
11281
11297
  return {
11282
- data: strategy.buildFinalResponse(accumulator),
11298
+ data: buildFinal(),
11283
11299
  error: err.message,
11284
11300
  interrupted: false,
11285
11301
  statusCode
@@ -11295,7 +11311,9 @@ async function resumeStream(options) {
11295
11311
  draining = true;
11296
11312
  await Promise.all([contentSmoother.drain(), thinkingSmoother.drain()]);
11297
11313
  if (playbackAborted) return buildInterrupted(abortMessage());
11298
- const response = strategy.buildFinalResponse(accumulator);
11314
+ contentDeAnon?.flush();
11315
+ thinkingDeAnon?.flush();
11316
+ const response = buildFinal();
11299
11317
  const deliveredOutput = emittedOutput || (accumulator.toolCallEvents?.length ?? 0) > 0;
11300
11318
  if (onFinish && deliveredOutput) onFinish(response);
11301
11319
  return { data: response, error: null, interrupted: false, empty: !deliveredOutput };
@@ -18519,6 +18537,52 @@ async function getDecayCandidatesRawOp(ctx) {
18519
18537
  trustTier: raw.trust_tier ?? null
18520
18538
  }));
18521
18539
  }
18540
+ async function applyDecayTransitionsOp(ctx, candidates, opts) {
18541
+ assertVaultScopeForSweep(ctx);
18542
+ const byId = new Map(candidates.map((candidate) => [candidate.uniqueId, candidate]));
18543
+ const ids = Array.from(byId.keys());
18544
+ const changed = /* @__PURE__ */ new Set();
18545
+ const CHUNK = 500;
18546
+ for (let i = 0; i < ids.length; i += CHUNK) {
18547
+ let applied = [];
18548
+ try {
18549
+ await ctx.database.write(async () => {
18550
+ await assertCanWrite2(ctx);
18551
+ const records = await ctx.vaultMemoryCollection.query(
18552
+ import_watermelondb21.Q.where("id", import_watermelondb21.Q.oneOf(ids.slice(i, i + CHUNK))),
18553
+ ...ctx.userId !== void 0 ? [import_watermelondb21.Q.where("user_id", ctx.userId)] : []
18554
+ ).fetch();
18555
+ const eligible = records.filter((record2) => {
18556
+ if (record2.isDeleted || !isOwnedByCtxUser(ctx, record2)) return false;
18557
+ if (opts.transition === "delete") {
18558
+ return record2.archivedAt !== null && opts.now - record2.archivedAt > opts.hardDeleteWindowMs;
18559
+ }
18560
+ const candidate = byId.get(record2.id);
18561
+ return record2.archivedAt === null && record2.updatedAt.getTime() === candidate.updatedAt && (record2.lastObservedAt ?? null) === (candidate.lastObservedAt ?? null);
18562
+ });
18563
+ const prepared = eligible.map(
18564
+ (record2) => record2.prepareUpdate((row) => {
18565
+ if (opts.transition === "archive") row._setRaw("archived_at", opts.now);
18566
+ else row._setRaw("is_deleted", true);
18567
+ })
18568
+ );
18569
+ if (prepared.length > 0) await ctx.database.batch(prepared);
18570
+ applied = eligible.map((record2) => record2.id);
18571
+ });
18572
+ } catch {
18573
+ continue;
18574
+ }
18575
+ for (const id2 of applied) changed.add(id2);
18576
+ if (opts.transition === "delete" && ctx.entityCtx) {
18577
+ try {
18578
+ await unlinkMemoryEntitiesOp(ctx.entityCtx, applied);
18579
+ } catch {
18580
+ continue;
18581
+ }
18582
+ }
18583
+ }
18584
+ return changed;
18585
+ }
18522
18586
  async function archiveVaultMemoryOp(ctx, id2, opts) {
18523
18587
  try {
18524
18588
  const record2 = await ctx.vaultMemoryCollection.find(id2);
@@ -23119,25 +23183,8 @@ function isBorderline(input) {
23119
23183
  function vaultDecayBackend(vaultCtx) {
23120
23184
  return {
23121
23185
  candidates: () => getDecayCandidatesRawOp(vaultCtx),
23122
- archive: async (rows, now) => {
23123
- const archived = /* @__PURE__ */ new Set();
23124
- for (const c of rows) {
23125
- const ok = await archiveVaultMemoryOp(vaultCtx, c.uniqueId, {
23126
- now,
23127
- expectedUpdatedAt: c.updatedAt,
23128
- expectedLastObservedAt: c.lastObservedAt ?? null
23129
- });
23130
- if (ok) archived.add(c.uniqueId);
23131
- }
23132
- return archived;
23133
- },
23134
- hardDelete: async (rows, deleteOptions) => {
23135
- const deleted = /* @__PURE__ */ new Set();
23136
- for (const c of rows) {
23137
- if (await hardDeleteDecayedOp(vaultCtx, c.uniqueId, deleteOptions)) deleted.add(c.uniqueId);
23138
- }
23139
- return deleted;
23140
- }
23186
+ archive: (rows, now) => applyDecayTransitionsOp(vaultCtx, rows, { transition: "archive", now }),
23187
+ hardDelete: (rows, deleteOptions) => applyDecayTransitionsOp(vaultCtx, rows, { transition: "delete", ...deleteOptions })
23141
23188
  };
23142
23189
  }
23143
23190
  function createDecaySweeper(options) {
@@ -25139,6 +25186,7 @@ function createLocalMemoryStore(options) {
25139
25186
  "trust_tier",
25140
25187
  "superseded_by",
25141
25188
  "visibility",
25189
+ "twin_opt_in",
25142
25190
  "published_at",
25143
25191
  "geohash",
25144
25192
  "topics",
@@ -26330,6 +26378,7 @@ function subscriptionState({ memory, version }) {
26330
26378
  memory.event_time_kind,
26331
26379
  memory.trust_tier,
26332
26380
  memory.visibility,
26381
+ memory.twin_opt_in,
26333
26382
  memory.published_at,
26334
26383
  memory.geohash,
26335
26384
  memory.topics,
@@ -2753,6 +2753,8 @@ interface ResumeStreamOptions {
2753
2753
  onData?: (chunk: string) => void;
2754
2754
  /** Thinking/reasoning deltas as they replay. */
2755
2755
  onThinking?: (chunk: string) => void;
2756
+ /** Restores the placeholders this redactor assigned when the original request was sent. */
2757
+ piiRedactor?: PiiRedactor;
2756
2758
  /** Called once on a clean completion. Never called for 410 nor interrupted terminals. */
2757
2759
  onFinish?: (response: ApiResponse) => void;
2758
2760
  /** Called on a transient/unexpected failure. Never called for 410 (throws) nor interrupted terminals. */
@@ -2753,6 +2753,8 @@ interface ResumeStreamOptions {
2753
2753
  onData?: (chunk: string) => void;
2754
2754
  /** Thinking/reasoning deltas as they replay. */
2755
2755
  onThinking?: (chunk: string) => void;
2756
+ /** Restores the placeholders this redactor assigned when the original request was sent. */
2757
+ piiRedactor?: PiiRedactor;
2756
2758
  /** Called once on a clean completion. Never called for 410 nor interrupted terminals. */
2757
2759
  onFinish?: (response: ApiResponse) => void;
2758
2760
  /** Called on a transient/unexpected failure. Never called for 410 (throws) nor interrupted terminals. */
@@ -9497,16 +9497,34 @@ async function resumeStream(options) {
9497
9497
  onData,
9498
9498
  onThinking,
9499
9499
  onFinish,
9500
- onError
9500
+ onError,
9501
+ piiRedactor
9501
9502
  } = options;
9502
9503
  const strategy = getStrategy(handle.apiType);
9503
9504
  const accumulator = createStreamAccumulator(handle.model || void 0);
9505
+ const contentDeAnon = piiRedactor && onData ? createStreamingDeAnonymizer(piiRedactor, onData) : null;
9506
+ const thinkingDeAnon = piiRedactor && onThinking ? createStreamingDeAnonymizer(piiRedactor, onThinking) : null;
9504
9507
  const contentSmoother = new StreamSmoother((text16) => {
9505
- if (onData) onData(text16);
9508
+ if (contentDeAnon) contentDeAnon.push(text16);
9509
+ else if (onData) onData(text16);
9506
9510
  }, smoothing);
9507
9511
  const thinkingSmoother = new StreamSmoother((text16) => {
9508
- if (onThinking) onThinking(text16);
9512
+ if (thinkingDeAnon) thinkingDeAnon.push(text16);
9513
+ else if (onThinking) onThinking(text16);
9509
9514
  }, smoothing);
9515
+ const flushOutput = () => {
9516
+ contentSmoother.flush();
9517
+ thinkingSmoother.flush();
9518
+ contentDeAnon?.flush();
9519
+ thinkingDeAnon?.flush();
9520
+ };
9521
+ const buildFinal = () => strategy.buildFinalResponse(
9522
+ piiRedactor ? {
9523
+ ...accumulator,
9524
+ content: piiRedactor.deAnonymize(accumulator.content),
9525
+ thinking: piiRedactor.deAnonymize(accumulator.thinking)
9526
+ } : accumulator
9527
+ );
9510
9528
  const idleController = new AbortController();
9511
9529
  let watchdog;
9512
9530
  let idleFired = false;
@@ -9551,9 +9569,8 @@ async function resumeStream(options) {
9551
9569
  if (signal) signal.removeEventListener("abort", onCallerAbort);
9552
9570
  };
9553
9571
  const buildInterrupted = (message) => {
9554
- contentSmoother.flush();
9555
- thinkingSmoother.flush();
9556
- const data = accumulator.toolCalls.size > 0 ? stripToolCalls(strategy.buildFinalResponse(accumulator)) : strategy.buildFinalResponse(accumulator);
9572
+ flushOutput();
9573
+ const data = accumulator.toolCalls.size > 0 ? stripToolCalls(buildFinal()) : buildFinal();
9557
9574
  return { data, error: message, interrupted: true };
9558
9575
  };
9559
9576
  if (combinedSignal.aborted) {
@@ -9601,18 +9618,16 @@ async function resumeStream(options) {
9601
9618
  const err = replayErr instanceof Error ? replayErr : new Error(String(replayErr));
9602
9619
  const statusCode = parseSseStatusCode(err);
9603
9620
  if (statusCode === 410) {
9604
- contentSmoother.flush();
9605
- thinkingSmoother.flush();
9621
+ flushOutput();
9606
9622
  throw new StreamExpiredError(handle.inferenceId);
9607
9623
  }
9608
9624
  if (err.name === "AbortError" || combinedSignal.aborted) {
9609
9625
  return buildInterrupted(abortMessage());
9610
9626
  }
9611
- contentSmoother.flush();
9612
- thinkingSmoother.flush();
9627
+ flushOutput();
9613
9628
  if (onError) onError(err);
9614
9629
  return {
9615
- data: strategy.buildFinalResponse(accumulator),
9630
+ data: buildFinal(),
9616
9631
  error: err.message,
9617
9632
  interrupted: false,
9618
9633
  statusCode
@@ -9628,7 +9643,9 @@ async function resumeStream(options) {
9628
9643
  draining = true;
9629
9644
  await Promise.all([contentSmoother.drain(), thinkingSmoother.drain()]);
9630
9645
  if (playbackAborted) return buildInterrupted(abortMessage());
9631
- const response = strategy.buildFinalResponse(accumulator);
9646
+ contentDeAnon?.flush();
9647
+ thinkingDeAnon?.flush();
9648
+ const response = buildFinal();
9632
9649
  const deliveredOutput = emittedOutput || (accumulator.toolCallEvents?.length ?? 0) > 0;
9633
9650
  if (onFinish && deliveredOutput) onFinish(response);
9634
9651
  return { data: response, error: null, interrupted: false, empty: !deliveredOutput };
@@ -16850,6 +16867,52 @@ async function getDecayCandidatesRawOp(ctx) {
16850
16867
  trustTier: raw.trust_tier ?? null
16851
16868
  }));
16852
16869
  }
16870
+ async function applyDecayTransitionsOp(ctx, candidates, opts) {
16871
+ assertVaultScopeForSweep(ctx);
16872
+ const byId = new Map(candidates.map((candidate) => [candidate.uniqueId, candidate]));
16873
+ const ids = Array.from(byId.keys());
16874
+ const changed = /* @__PURE__ */ new Set();
16875
+ const CHUNK = 500;
16876
+ for (let i = 0; i < ids.length; i += CHUNK) {
16877
+ let applied = [];
16878
+ try {
16879
+ await ctx.database.write(async () => {
16880
+ await assertCanWrite2(ctx);
16881
+ const records = await ctx.vaultMemoryCollection.query(
16882
+ Q6.where("id", Q6.oneOf(ids.slice(i, i + CHUNK))),
16883
+ ...ctx.userId !== void 0 ? [Q6.where("user_id", ctx.userId)] : []
16884
+ ).fetch();
16885
+ const eligible = records.filter((record2) => {
16886
+ if (record2.isDeleted || !isOwnedByCtxUser(ctx, record2)) return false;
16887
+ if (opts.transition === "delete") {
16888
+ return record2.archivedAt !== null && opts.now - record2.archivedAt > opts.hardDeleteWindowMs;
16889
+ }
16890
+ const candidate = byId.get(record2.id);
16891
+ return record2.archivedAt === null && record2.updatedAt.getTime() === candidate.updatedAt && (record2.lastObservedAt ?? null) === (candidate.lastObservedAt ?? null);
16892
+ });
16893
+ const prepared = eligible.map(
16894
+ (record2) => record2.prepareUpdate((row) => {
16895
+ if (opts.transition === "archive") row._setRaw("archived_at", opts.now);
16896
+ else row._setRaw("is_deleted", true);
16897
+ })
16898
+ );
16899
+ if (prepared.length > 0) await ctx.database.batch(prepared);
16900
+ applied = eligible.map((record2) => record2.id);
16901
+ });
16902
+ } catch {
16903
+ continue;
16904
+ }
16905
+ for (const id2 of applied) changed.add(id2);
16906
+ if (opts.transition === "delete" && ctx.entityCtx) {
16907
+ try {
16908
+ await unlinkMemoryEntitiesOp(ctx.entityCtx, applied);
16909
+ } catch {
16910
+ continue;
16911
+ }
16912
+ }
16913
+ }
16914
+ return changed;
16915
+ }
16853
16916
  async function archiveVaultMemoryOp(ctx, id2, opts) {
16854
16917
  try {
16855
16918
  const record2 = await ctx.vaultMemoryCollection.find(id2);
@@ -21419,25 +21482,8 @@ function isBorderline(input) {
21419
21482
  function vaultDecayBackend(vaultCtx) {
21420
21483
  return {
21421
21484
  candidates: () => getDecayCandidatesRawOp(vaultCtx),
21422
- archive: async (rows, now) => {
21423
- const archived = /* @__PURE__ */ new Set();
21424
- for (const c of rows) {
21425
- const ok = await archiveVaultMemoryOp(vaultCtx, c.uniqueId, {
21426
- now,
21427
- expectedUpdatedAt: c.updatedAt,
21428
- expectedLastObservedAt: c.lastObservedAt ?? null
21429
- });
21430
- if (ok) archived.add(c.uniqueId);
21431
- }
21432
- return archived;
21433
- },
21434
- hardDelete: async (rows, deleteOptions) => {
21435
- const deleted = /* @__PURE__ */ new Set();
21436
- for (const c of rows) {
21437
- if (await hardDeleteDecayedOp(vaultCtx, c.uniqueId, deleteOptions)) deleted.add(c.uniqueId);
21438
- }
21439
- return deleted;
21440
- }
21485
+ archive: (rows, now) => applyDecayTransitionsOp(vaultCtx, rows, { transition: "archive", now }),
21486
+ hardDelete: (rows, deleteOptions) => applyDecayTransitionsOp(vaultCtx, rows, { transition: "delete", ...deleteOptions })
21441
21487
  };
21442
21488
  }
21443
21489
  function createDecaySweeper(options) {
@@ -23424,6 +23470,7 @@ function createLocalMemoryStore(options) {
23424
23470
  "trust_tier",
23425
23471
  "superseded_by",
23426
23472
  "visibility",
23473
+ "twin_opt_in",
23427
23474
  "published_at",
23428
23475
  "geohash",
23429
23476
  "topics",
@@ -24610,6 +24657,7 @@ function subscriptionState({ memory, version }) {
24610
24657
  memory.event_time_kind,
24611
24658
  memory.trust_tier,
24612
24659
  memory.visibility,
24660
+ memory.twin_opt_in,
24613
24661
  memory.published_at,
24614
24662
  memory.geohash,
24615
24663
  memory.topics,