@tangle-network/agent-app 3.0.30 → 3.0.31

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.
@@ -0,0 +1,5 @@
1
+ export type ValueRedactor = <T>(value: T) => T;
2
+ /** A redactor for these values, or undefined when none qualifies. Longest first, so a value that contains another wins. */
3
+ export declare function valueRedactor(values: readonly string[] | undefined): ValueRedactor | undefined;
4
+ /** Every event of a stream, redacted. A value split across two text deltas is caught where the turn is stored, not live. */
5
+ export declare function redactEvents<T>(source: AsyncIterable<T>, redact: ValueRedactor): AsyncGenerator<T>;
@@ -724,6 +724,14 @@ export interface CreateChatTurnRoutesOptions<TContext = void, TResource = unknow
724
724
  /** Pre-persist transform of the final text (e.g. `/redact`'s `redactPII`).
725
725
  * Live stream is never altered. */
726
726
  transformFinalText?(text: string): string | Promise<string>;
727
+ /** Secret values this turn could expose (a credential the product placed in its box's environment). Every
728
+ * occurrence becomes `[redacted]` in the live stream, the turn buffer, the draft and the stored row, matched by value,
729
+ * never by pattern; values under 8 characters are ignored. The product redacts its own history reads the same way. */
730
+ redactValues?(turn: {
731
+ tenantId: string;
732
+ userId: string;
733
+ threadId: string;
734
+ }): readonly string[] | Promise<readonly string[]>;
727
735
  /** Post-processing after a turn settles (billing, titles, audit). Fires with
728
736
  * `failed:true` + `failureReason` when the turn carried a terminal error
729
737
  * event (model 402 / rate-limit / server error) instead of a clean
@@ -9,6 +9,9 @@ import {
9
9
  currentD1QueryCounter,
10
10
  withD1QueryCount
11
11
  } from "./chunk-MM6RLOXF.js";
12
+ import {
13
+ REDACTED
14
+ } from "./chunk-M4QITAQV.js";
12
15
  import {
13
16
  SandboxRunError,
14
17
  createComputerStep,
@@ -134,6 +137,19 @@ import {
134
137
  isWorkspaceFileExportable
135
138
  } from "./chunk-TXD5HXLE.js";
136
139
 
140
+ // src/chat-routes/redact-values.ts
141
+ var MIN_SECRET_LENGTH = 8;
142
+ function valueRedactor(values) {
143
+ const secrets = [...new Set((values ?? []).filter((v) => typeof v === "string" && v.length >= MIN_SECRET_LENGTH))].sort((a, b) => b.length - a.length);
144
+ if (secrets.length === 0) return void 0;
145
+ const text = (s) => secrets.some((v) => s.includes(v)) ? secrets.reduce((acc, v) => acc.split(v).join(REDACTED), s) : s;
146
+ const walk = (v) => typeof v === "string" ? text(v) : Array.isArray(v) ? v.map(walk) : v !== null && typeof v === "object" ? Object.fromEntries(Object.entries(v).map(([k, x]) => [k, walk(x)])) : v;
147
+ return walk;
148
+ }
149
+ async function* redactEvents(source, redact) {
150
+ for await (const event of source) yield redact(event);
151
+ }
152
+
137
153
  // src/chat-routes/upload.ts
138
154
  var UPLOAD_INLINE_MAX_BYTES = 700 * 1024;
139
155
  var UPLOAD_MAX_FILE_BYTES = 8 * 1024 * 1024;
@@ -2908,6 +2924,7 @@ async function runAdmittedTurn({
2908
2924
  const preparedBatch = await prepareAdmittedTurn(options, trace, log, tenantId, userId, payload, context, request, progress, content, turnId, draftTuning, ctx?.waitUntil);
2909
2925
  if (preparedBatch instanceof Response) return preparedBatch;
2910
2926
  const { turnStreamId, identity, executionId, chatTurn, readHistory, historyBeforeLock } = preparedBatch;
2927
+ const redact = options.redactValues ? valueRedactor(await options.redactValues({ tenantId, userId, threadId: payload.threadId })) : void 0;
2911
2928
  const prompt = fileParts.length === 0 ? content : content ? [{ type: "text", text: content }, ...fileParts] : [...fileParts];
2912
2929
  const state = createAdmittedTurnState();
2913
2930
  if (options.cancel) state.cancellationAbort = new AbortController();
@@ -3060,12 +3077,13 @@ async function runAdmittedTurn({
3060
3077
  messageId: assistantRowIdForTurn(executionId),
3061
3078
  snapshot: () => {
3062
3079
  if (!state.producer || state.cancellationAbort?.signal.aborted) return null;
3063
- return {
3080
+ const snapshot = {
3064
3081
  content: state.producer.finalText(),
3065
3082
  ...state.producer.draftParts ? { parts: state.producer.draftParts() } : {},
3066
3083
  ...state.producer.usage ? { usage: state.producer.usage() } : {},
3067
3084
  ...state.producer.model ? { model: state.producer.model } : {}
3068
3085
  };
3086
+ return redact ? redact(snapshot) : snapshot;
3069
3087
  },
3070
3088
  ...options.transformFinalText ? { transformText: options.transformFinalText } : {},
3071
3089
  log
@@ -3093,7 +3111,7 @@ async function runAdmittedTurn({
3093
3111
  }
3094
3112
  }
3095
3113
  streamSetup.end();
3096
- const { result } = createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, selected.produce, request, context, payload, assistantRowId);
3114
+ const { result } = createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, selected.produce, request, context, payload, assistantRowId, redact);
3097
3115
  const { clientBody } = startTurnDrain(result, log, turnStreamId, state, endTrace, exportTrace, trace, ctx, request, fireTerminalLifecycle, releaseLock, runAfterSettle, early.settle);
3098
3116
  early.handOff();
3099
3117
  return createTurnReplayResponse(result.contentType, clientBody, turnMarker, trace);
@@ -3329,7 +3347,7 @@ function createTurnSettlement(state, trace, options, executionId, traceSink, ide
3329
3347
  };
3330
3348
  return { endTrace, exportTraceInBackground, fireTerminalLifecycle, assistantRowId, exportTrace, runAfterSettle };
3331
3349
  }
3332
- function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, produce, request, context, payload, assistantRowId) {
3350
+ function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, trace, options, produce, request, context, payload, assistantRowId, redact) {
3333
3351
  const result = handleChatTurn({
3334
3352
  identity,
3335
3353
  waitUntil: ctx?.waitUntil,
@@ -3350,7 +3368,7 @@ function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, tr
3350
3368
  state.producer = await started;
3351
3369
  yield* state.producer.stream;
3352
3370
  };
3353
- let source = producerStream();
3371
+ let source = redact ? redactEvents(producerStream(), redact) : producerStream();
3354
3372
  if (options.cancel && state.cancellationAbort && !state.closeCancellation) {
3355
3373
  source = observeTurnCancellation(source, produceArgs, options.cancel, state.cancellationAbort);
3356
3374
  }
@@ -3418,13 +3436,14 @@ function createTurnEngine(identity, ctx, log, state, dispatched, produceArgs, tr
3418
3436
  }
3419
3437
  const usage = state.producer?.usage?.() ?? {};
3420
3438
  const attribution = state.producer?.modelAttribution?.();
3421
- const values = {
3439
+ const unredacted = {
3422
3440
  content: finalText,
3423
3441
  ...parts && parts.length > 0 ? { parts } : {},
3424
3442
  ...state.producer?.model ? { model: state.producer.model } : {},
3425
3443
  ...modelAttributionFields(attribution),
3426
3444
  ...measuredUsage(usage)
3427
3445
  };
3446
+ const values = redact ? redact(unredacted) : unredacted;
3428
3447
  if (state.draft) {
3429
3448
  await state.draft.finalize(values);
3430
3449
  state.assistantMessageId = state.draft.rowId() ?? null;
@@ -4157,6 +4176,8 @@ function toSandboxEvent(event) {
4157
4176
  }
4158
4177
 
4159
4178
  export {
4179
+ valueRedactor,
4180
+ redactEvents,
4160
4181
  UPLOAD_INLINE_MAX_BYTES,
4161
4182
  UPLOAD_MAX_FILE_BYTES,
4162
4183
  sanitizeUploadFilename,
@@ -4199,4 +4220,4 @@ export {
4199
4220
  createChatTurnRoutes,
4200
4221
  streamChatRouteAsSandboxEvents
4201
4222
  };
4202
- //# sourceMappingURL=chunk-LRUH36LD.js.map
4223
+ //# sourceMappingURL=chunk-HP5KD7ZI.js.map