jev-devtools 0.4.0 → 0.5.0

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.
Files changed (37) hide show
  1. package/README.md +20 -13
  2. package/lib/flow/compare.d.ts +61 -0
  3. package/lib/flow/compare.js +336 -0
  4. package/lib/flow/index.d.ts +6 -1
  5. package/lib/flow/index.js +78 -16
  6. package/lib/flow/replay.d.ts +5 -0
  7. package/lib/flow/replay.js +55 -0
  8. package/lib/flow/runtime.d.ts +4 -0
  9. package/lib/flow/runtime.js +74 -0
  10. package/lib/flow/step-context.d.ts +7 -0
  11. package/lib/flow/step-context.js +3 -0
  12. package/lib/server/flows.d.ts +2 -2
  13. package/lib/server/flows.js +4 -6
  14. package/lib/server/index.js +16 -4
  15. package/lib/server/recorder.js +42 -6
  16. package/lib/src/ConnectedInspector.d.ts +1 -1
  17. package/lib/src/ConnectedInspector.js +14 -3
  18. package/lib/src/CopyFixture.d.ts +22 -0
  19. package/lib/src/CopyFixture.js +67 -0
  20. package/lib/src/FlowOverview.js +4 -2
  21. package/lib/src/JevDevtools.js +1 -1
  22. package/lib/src/JevDrawer.js +2 -4
  23. package/lib/src/display-redaction.d.ts +9 -0
  24. package/lib/src/display-redaction.js +116 -0
  25. package/lib/src/flow-fixture.d.ts +19 -0
  26. package/lib/src/flow-fixture.js +40 -0
  27. package/lib/src/flow-stream.js +9 -0
  28. package/lib/src/index.d.ts +5 -0
  29. package/lib/src/index.js +1 -0
  30. package/lib/src/inspector-styles.js +1 -1
  31. package/lib/src/inspector.css +207 -267
  32. package/package.json +2 -2
  33. package/skills/setup-jev-devtools/SKILL.md +8 -5
  34. package/skills/setup-jev-devtools/references/flows.md +15 -2
  35. package/skills/setup-jev-devtools/references/nextjs.md +1 -1
  36. package/skills/setup-jev-devtools/references/react-node.md +1 -2
  37. package/skills/setup-jev-devtools/references/verify.md +2 -4
package/lib/flow/index.js CHANGED
@@ -1,5 +1,9 @@
1
1
  import { randomUUID } from "node:crypto";
2
- import { jsonSnapshot, runCallback } from "./runtime.js";
2
+ import { jsonSnapshot, observerSnapshot, observeRequest, runCallback, } from "./runtime.js";
3
+ import { stepContext } from "./step-context.js";
4
+ export { replayClient } from "./replay.js";
5
+ export { flowFixture } from "../src/flow-fixture.js";
6
+ export { compareWithFixture, formatComparison, } from "./compare.js";
3
7
  const owners = new WeakMap();
4
8
  const builderIds = new WeakMap();
5
9
  function freezeAst(ast, depth = 0) {
@@ -64,8 +68,10 @@ export const any = (...conditions) => operation("any", conditions);
64
68
  export const not = (condition) => operation("not", [condition]);
65
69
  // Nested programs share a reservation budget without exposing execution internals in the API.
66
70
  const executionBudgets = new WeakMap();
67
- function withBudget(options, budget) {
71
+ const itemScopes = new WeakMap();
72
+ function withBudget(options, budget, scope) {
68
73
  executionBudgets.set(options, budget);
74
+ itemScopes.set(options, scope);
69
75
  return options;
70
76
  }
71
77
  function definitionSize(definition) {
@@ -650,6 +656,11 @@ export function defineJevFlow(name, build) {
650
656
  };
651
657
  const started = performance.now(), values = new Map();
652
658
  const budget = executionBudgets.get(options) ?? { used: nodes.length };
659
+ const scope = itemScopes.get(options) ?? {
660
+ runId: run.id,
661
+ flow: name,
662
+ prefix: "",
663
+ };
653
664
  const reserve = (count) => {
654
665
  if (budget.used + count > 1024)
655
666
  throw new RangeError("Flow exceeds 1024 expanded steps");
@@ -662,16 +673,56 @@ export function defineJevFlow(name, build) {
662
673
  catch {
663
674
  /* Recording cannot stop execution. */
664
675
  }
676
+ // Data objects are replaced, never mutated by execution. Keep detached
677
+ // copies stable so the recorder can reuse its captures on unrelated updates.
678
+ const observerData = new WeakMap();
679
+ const snapshotData = (value) => {
680
+ if (value && typeof value === "object" && observerData.has(value))
681
+ return observerData.get(value);
682
+ let snapshot;
683
+ try {
684
+ snapshot = observerSnapshot(value);
685
+ }
686
+ catch {
687
+ // Direct flow outputs may be non-JSON; only their capture is unavailable.
688
+ snapshot = "[capture unavailable]";
689
+ }
690
+ if (value && typeof value === "object")
691
+ observerData.set(value, snapshot);
692
+ return snapshot;
693
+ };
665
694
  const emit = () => {
666
695
  try {
667
696
  if (observer) {
668
697
  const { value, steps, ...metadata } = run;
669
- // Stable data snapshots are captured once by the recorder, not
670
- // cloned/redacted again on every unrelated step update.
671
698
  observer.update({
672
699
  ...structuredClone(metadata),
673
- steps: Object.fromEntries(Object.entries(steps).map(([id, step]) => [id, { ...step }])),
674
- ...(value !== undefined ? { value } : {}),
700
+ steps: Object.fromEntries(Object.entries(steps).map(([id, step]) => [
701
+ id,
702
+ {
703
+ ...step,
704
+ ...(step.data !== undefined
705
+ ? { data: snapshotData(step.data) }
706
+ : {}),
707
+ ...(step.loop ? { loop: { ...step.loop } } : {}),
708
+ ...(step.captureWarnings
709
+ ? { captureWarnings: [...step.captureWarnings] }
710
+ : {}),
711
+ ...(step.collection
712
+ ? {
713
+ collection: {
714
+ ...step.collection,
715
+ // Child runs have already crossed this snapshot boundary.
716
+ // Copy mutable collection metadata, reuse detached children.
717
+ items: step.collection.items.map((item) => ({
718
+ ...item,
719
+ })),
720
+ },
721
+ }
722
+ : {}),
723
+ },
724
+ ])),
725
+ ...(value !== undefined ? { value: snapshotData(value) } : {}),
675
726
  });
676
727
  }
677
728
  }
@@ -728,23 +779,31 @@ export function defineJevFlow(name, build) {
728
779
  return evaluate(e.ast, data, values);
729
780
  });
730
781
  abortCheck();
731
- const call = () => options.client.systemOne(request, {
732
- signal: controller.signal,
733
- });
782
+ const call = () => {
783
+ abortCheck();
784
+ return stepContext.run({
785
+ runId: scope.runId,
786
+ stepId: scope.prefix + step.id,
787
+ executionId: state.executionId,
788
+ flow: scope.flow,
789
+ }, () => options.client.systemOne(request, {
790
+ signal: controller.signal,
791
+ }));
792
+ };
734
793
  const link = {
735
794
  runId: run.id,
736
795
  stepId: step.id,
737
796
  executionId: state.executionId,
738
797
  };
739
- value = await runCallback(() => observer
740
- ? observer.request(link, (id) => {
798
+ value = await runCallback(() => observeRequest(call, observer
799
+ ? (callOnce) => observer.request(link, (id) => {
741
800
  if (controller.signal.aborted ||
742
801
  state.status !== "running")
743
802
  return;
744
803
  state.traceId = id;
745
804
  emit();
746
- }, call)
747
- : call(), controller.signal);
805
+ }, callOnce)
806
+ : undefined), controller.signal);
748
807
  abortCheck();
749
808
  }
750
809
  else if (step.kind === "task") {
@@ -828,7 +887,7 @@ export function defineJevFlow(name, build) {
828
887
  };
829
888
  },
830
889
  },
831
- }, budget));
890
+ }, budget, { ...scope, prefix: `${scope.prefix}${step.id}/${index}/` }));
832
891
  abortCheck();
833
892
  current = jsonSnapshot(result.value);
834
893
  state.data = { input: initial, output: current };
@@ -897,7 +956,10 @@ export function defineJevFlow(name, build) {
897
956
  };
898
957
  },
899
958
  },
900
- }, budget));
959
+ }, budget, {
960
+ ...scope,
961
+ prefix: `${scope.prefix}${step.id}/${index}/`,
962
+ }));
901
963
  results[index] = result.value;
902
964
  }
903
965
  catch (error) {
@@ -1004,7 +1066,7 @@ export function defineJevFlow(name, build) {
1004
1066
  throw new Error("Flow must return a named output");
1005
1067
  run.status = "success";
1006
1068
  run.outcome = result.outcome;
1007
- // Only the recorder snapshots output data; runtime keeps the actual typed value.
1069
+ // emit() detaches the observer's output; callers keep the actual typed value.
1008
1070
  if (result.value !== undefined)
1009
1071
  run.value = result.value;
1010
1072
  return { runId: run.id, ...result };
@@ -0,0 +1,5 @@
1
+ import { TypeSafeClient } from "@typesafe-ai/sdk";
2
+ import type { FlowFixture } from "../src/flow-fixture.js";
3
+ export type { FlowFixture } from "../src/flow-fixture.js";
4
+ /** Answers each jev step from fixture.steps[stepId]; no network or API key. Use only as a flow run's client. */
5
+ export declare function replayClient(fixture: FlowFixture): TypeSafeClient;
@@ -0,0 +1,55 @@
1
+ import { APIPromise, TypeSafeClient, } from "@typesafe-ai/sdk";
2
+ import { stepContext } from "./step-context.js";
3
+ const record = (v) => !!v && typeof v === "object" && !Array.isArray(v);
4
+ function replayError(fixture, questions) {
5
+ const step = stepContext.getStore();
6
+ if (!step)
7
+ return new Error("Replay client was called outside a flow run. Pass it as the client of flow.run().");
8
+ if (fixture.flow !== step.flow)
9
+ return new Error(`Fixture was recorded from flow "${fixture.flow}" but is replaying flow "${step.flow}".`);
10
+ if (!Object.hasOwn(fixture.steps, step.stepId))
11
+ return new Error(`Fixture has no response for step "${step.stepId}". The run took a route the fixture didn't record, or that step's capture was missing when the fixture was copied.`);
12
+ const response = fixture.steps[step.stepId];
13
+ const answers = record(response) && record(response.answers) ? response.answers : {};
14
+ const absent = Object.keys(record(questions) ? questions : {}).filter((name) => !Object.hasOwn(answers, name));
15
+ if (absent.length)
16
+ return new Error(`Fixture step "${step.stepId}" has no answer for ${absent.join(", ")}. Re-record the fixture.`);
17
+ return undefined;
18
+ }
19
+ // Checks run before the SDK so replay failures are not reported as connection errors.
20
+ class ReplayClient extends TypeSafeClient {
21
+ fixture;
22
+ constructor(fixture) {
23
+ super({
24
+ apiKey: "replay-fixture",
25
+ fetch: async () => {
26
+ const step = stepContext.getStore();
27
+ if (!step || !Object.hasOwn(fixture.steps, step.stepId))
28
+ throw new Error("Replay fetch outside a checked flow step");
29
+ return Response.json(fixture.steps[step.stepId]);
30
+ },
31
+ // Bodies are discarded, so skip compressing them.
32
+ compression: false,
33
+ logLevel: "off",
34
+ retry: { maxRetries: 0 },
35
+ });
36
+ this.fixture = fixture;
37
+ }
38
+ systemOne(request, options) {
39
+ const error = replayError(this.fixture, request.questions);
40
+ if (!error)
41
+ return super.systemOne(request, options);
42
+ // Same shape as the SDK's own early rejections.
43
+ const response = Promise.reject(error);
44
+ return new APIPromise(response, () => response);
45
+ }
46
+ }
47
+ /** Answers each jev step from fixture.steps[stepId]; no network or API key. Use only as a flow run's client. */
48
+ export function replayClient(fixture) {
49
+ if (fixture?.version !== 1 ||
50
+ typeof fixture.flow !== "string" ||
51
+ !record(fixture.steps))
52
+ throw new TypeError("Expected a version 1 flow fixture with flow and steps");
53
+ // A snapshot, so later edits to the caller's fixture don't change replay.
54
+ return new ReplayClient(structuredClone(fixture));
55
+ }
@@ -1,5 +1,9 @@
1
1
  import type { Json } from "../src/protocol.js";
2
2
  /** Detached JSON snapshot, without capture truncation or lossy JSON.stringify. */
3
3
  export declare function jsonSnapshot(value: unknown, seen?: Set<object>, depth?: number): Json;
4
+ /** Observer-only copy: preserve aliases inside the snapshot, never to execution data. */
5
+ export declare function observerSnapshot(value: unknown): Json;
6
+ /** Recording may establish synchronous context, but cannot replace or repeat execution. */
7
+ export declare function observeRequest<T>(callback: () => T, observe?: (call: () => T) => unknown): T;
4
8
  /** Cancellation ends awaiting, even when user code ignores the cooperative signal. */
5
9
  export declare function runCallback(callback: (signal: AbortSignal) => unknown, parent: AbortSignal, timeoutMs?: number): Promise<unknown>;
@@ -29,6 +29,80 @@ export function jsonSnapshot(value, seen = new Set(), depth = 0) {
29
29
  seen.delete(value);
30
30
  }
31
31
  }
32
+ /** Observer-only copy: preserve aliases inside the snapshot, never to execution data. */
33
+ export function observerSnapshot(value) {
34
+ const copies = new WeakMap();
35
+ const ancestors = new Set();
36
+ const visit = (input, depth) => {
37
+ if (depth > 40)
38
+ throw new TypeError("Observer data nesting exceeds 40");
39
+ if (input === null || typeof input !== "object")
40
+ return jsonSnapshot(input);
41
+ const previous = copies.get(input);
42
+ if (previous) {
43
+ // A shared subtree reached through a deeper path still obeys the depth limit.
44
+ if (depth + previous.height > 40)
45
+ throw new TypeError("Observer data nesting exceeds 40");
46
+ return previous.value;
47
+ }
48
+ if ((!Array.isArray(input) &&
49
+ Object.getPrototypeOf(input) !== Object.prototype &&
50
+ Object.getPrototypeOf(input) !== null) ||
51
+ Object.getOwnPropertySymbols(input).length)
52
+ throw new TypeError("Observer data must be JSON-compatible");
53
+ if (ancestors.has(input))
54
+ throw new TypeError("Observer data cannot contain cycles");
55
+ ancestors.add(input);
56
+ try {
57
+ let height = 0;
58
+ const child = (value) => {
59
+ const copy = visit(value, depth + 1);
60
+ const childHeight = value && typeof value === "object" ? copies.get(value).height : 0;
61
+ height = Math.max(height, childHeight + 1);
62
+ return copy;
63
+ };
64
+ const value = Array.isArray(input)
65
+ ? Array.from(input, child)
66
+ : Object.fromEntries(Object.entries(input).map(([key, value]) => [key, child(value)]));
67
+ const copy = { value, height };
68
+ copies.set(input, copy);
69
+ return value;
70
+ }
71
+ finally {
72
+ ancestors.delete(input);
73
+ }
74
+ };
75
+ return visit(value, 0);
76
+ }
77
+ /** Recording may establish synchronous context, but cannot replace or repeat execution. */
78
+ export function observeRequest(callback, observe) {
79
+ let called = false;
80
+ let result;
81
+ let failure;
82
+ const callOnce = () => {
83
+ if (!called) {
84
+ called = true;
85
+ try {
86
+ result = callback();
87
+ }
88
+ catch (error) {
89
+ failure = { error };
90
+ }
91
+ }
92
+ if (failure)
93
+ throw failure.error;
94
+ return result;
95
+ };
96
+ try {
97
+ // Observe rejected hook promises without awaiting them or changing the SDK result.
98
+ if (observe)
99
+ void Promise.resolve(observe(callOnce)).catch(() => { });
100
+ }
101
+ catch {
102
+ // A hook can fail before or after calling through. Reuse that invocation below.
103
+ }
104
+ return callOnce();
105
+ }
32
106
  /** Cancellation ends awaiting, even when user code ignores the cooperative signal. */
33
107
  export function runCallback(callback, parent, timeoutMs) {
34
108
  const controller = new AbortController();
@@ -0,0 +1,7 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ import type { Trace } from "../src/protocol.js";
3
+ export type StepLink = NonNullable<Trace["flow"]> & {
4
+ flow: string;
5
+ };
6
+ /** The jev step whose SDK call is in progress, read by the replay client. */
7
+ export declare const stepContext: AsyncLocalStorage<StepLink>;
@@ -0,0 +1,3 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ /** The jev step whose SDK call is in progress, read by the replay client. */
3
+ export const stepContext = new AsyncLocalStorage();
@@ -6,7 +6,6 @@ export declare class FlowRecorder {
6
6
  private runs;
7
7
  private definitions;
8
8
  private sessions;
9
- private definitionRevisions;
10
9
  private revisions;
11
10
  private revision;
12
11
  private listeners;
@@ -21,8 +20,9 @@ export declare class FlowRecorder {
21
20
  private remove;
22
21
  private prune;
23
22
  clear(session: string): void;
24
- changes(session: string, cursor?: number): {
23
+ changes(session: string, cursor?: number, knownDefinitions?: ReadonlySet<string>): {
25
24
  revision: number;
25
+ retainedDefinitionIds: Set<string>;
26
26
  data: FlowUpdate;
27
27
  };
28
28
  }
@@ -5,7 +5,6 @@ export class FlowRecorder {
5
5
  runs = new Map();
6
6
  definitions = new Map();
7
7
  sessions = new Map();
8
- definitionRevisions = new Map();
9
8
  revisions = new Map();
10
9
  revision = 0;
11
10
  listeners = new Set();
@@ -49,7 +48,6 @@ export class FlowRecorder {
49
48
  };
50
49
  redactRules(stored);
51
50
  this.definitions.set(definition.id, stored);
52
- this.definitionRevisions.set(definition.id, ++this.revision);
53
51
  }
54
52
  this.runs.set(run.id, run);
55
53
  this.sessions.set(run.id, session);
@@ -155,7 +153,6 @@ export class FlowRecorder {
155
153
  for (const id of this.definitions.keys())
156
154
  if (!used.has(id)) {
157
155
  this.definitions.delete(id);
158
- this.definitionRevisions.delete(id);
159
156
  }
160
157
  }
161
158
  clear(session) {
@@ -166,18 +163,19 @@ export class FlowRecorder {
166
163
  this.revision++;
167
164
  this.notify(new Set([session]));
168
165
  }
169
- changes(session, cursor) {
166
+ changes(session, cursor, knownDefinitions = new Set()) {
170
167
  const current = [...this.runs.values()]
171
168
  .filter((r) => this.sessions.get(r.id) === session)
172
169
  .reverse();
173
170
  const used = new Set(current.map((r) => r.definitionId));
174
171
  return {
175
172
  revision: this.revision,
173
+ retainedDefinitionIds: used,
176
174
  data: structuredClone({
177
175
  version: 1,
178
176
  definitions: [...used]
179
- .filter((id) => cursor === undefined ||
180
- this.definitionRevisions.get(id) > cursor)
177
+ // Creation time says nothing about what this particular reader has.
178
+ .filter((id) => cursor === undefined || !knownDefinitions.has(id))
181
179
  .map((id) => this.definitions.get(id)),
182
180
  upserts: current.filter((r) => cursor === undefined || this.revisions.get(r.id) > cursor),
183
181
  order: current.map((r) => r.id),
@@ -113,14 +113,25 @@ export function createJevDevtools(options) {
113
113
  const body = new ReadableStream({
114
114
  start(controller) {
115
115
  let closed = false, dirty = true, cursor, timer;
116
+ let knownDefinitions = new Set();
116
117
  const flush = () => {
117
118
  timer = undefined;
118
119
  if (closed || !dirty || (controller.desiredSize ?? 0) <= 0)
119
120
  return;
120
- dirty = false;
121
- const update = flows.changes(session, cursor);
122
- cursor = update.revision;
123
- controller.enqueue(encoder.encode(`event: flows\ndata: ${JSON.stringify(update.data)}\n\n`));
121
+ try {
122
+ const update = flows.changes(session, cursor, knownDefinitions);
123
+ controller.enqueue(encoder.encode(`event: flows\ndata: ${JSON.stringify(update.data)}\n\n`));
124
+ // Match the browser's pruning after this delivered order. A clear
125
+ // coalesced away under backpressure must not discard reader state.
126
+ knownDefinitions = update.retainedDefinitionIds;
127
+ cursor = update.revision;
128
+ dirty = false;
129
+ }
130
+ catch {
131
+ // A failed delivery must not advance the cursor or escape into
132
+ // application execution; reconnect starts with a full snapshot.
133
+ stop();
134
+ }
124
135
  };
125
136
  const wake = () => {
126
137
  if (!closed && dirty && !timer)
@@ -140,6 +151,7 @@ export function createJevDevtools(options) {
140
151
  if (closed)
141
152
  return;
142
153
  closed = true;
154
+ knownDefinitions.clear();
143
155
  unsubscribe();
144
156
  clearImmediate(timer);
145
157
  clearInterval(heartbeat);
@@ -1,7 +1,33 @@
1
1
  import { AsyncLocalStorage } from "node:async_hooks";
2
2
  import { randomUUID } from "node:crypto";
3
3
  import { Image } from "@typesafe-ai/sdk";
4
- const secretKey = /^(authorization|cookie|set-cookie|api[-_]?key|password|secret|access[-_]?token|refresh[-_]?token)$/i;
4
+ // Whole field names only: tokenCount, sessionId, apiKeyName, etc. stay useful.
5
+ // Keep the documented lists in docs/integration.md aligned with these policies.
6
+ const credentialKeys = new Set([
7
+ "authorization",
8
+ "proxyauthorization",
9
+ "cookie",
10
+ "cookies",
11
+ "setcookie",
12
+ "apikey",
13
+ "xapikey",
14
+ "password",
15
+ "passwd",
16
+ "passphrase",
17
+ "secret",
18
+ "clientsecret",
19
+ "token",
20
+ "accesstoken",
21
+ "refreshtoken",
22
+ "sessiontoken",
23
+ "authtoken",
24
+ "xauthtoken",
25
+ "xaccesstoken",
26
+ "idtoken",
27
+ "bearertoken",
28
+ "privatekey",
29
+ "secretkey",
30
+ ]);
5
31
  const safeMessage = (value) => value.replace(/Bearer\s+\S+/gi, "Bearer [redacted]").slice(0, 500);
6
32
  // Snapshot before the caller can mutate its request. Bounded and cycle-safe;
7
33
  // image handles become explicit previews/references instead of opaque SDK objects.
@@ -9,7 +35,8 @@ export function capture(value, warnings, redact) {
9
35
  const seen = new WeakSet();
10
36
  let budget = 96_000;
11
37
  const visit = (input, path, depth) => {
12
- if (secretKey.test(path.at(-1) || ""))
38
+ const key = (path.at(-1) ?? "").replace(/[-_]/g, "").toLowerCase();
39
+ if (credentialKeys.has(key))
13
40
  return "[redacted]";
14
41
  if (redact)
15
42
  input = redact(path, input);
@@ -243,18 +270,27 @@ export class JevRecorder {
243
270
  void promise
244
271
  .asResponse()
245
272
  .then(async (response) => {
246
- const data = await response.clone().json();
273
+ let result;
274
+ try {
275
+ const data = await response.clone().json();
276
+ result = capture(data, warnings, this.options.redact);
277
+ }
278
+ catch {
279
+ // Capture failures are not SDK failures, and redactor errors
280
+ // may contain the sensitive values they were meant to hide.
281
+ warnings.add("Response snapshot failed; result unavailable.");
282
+ }
247
283
  this.update(id, (t) => {
248
284
  t.status = "success";
249
285
  t.durationMs = performance.now() - started;
250
286
  t.requestId =
251
287
  response.headers.get("x-typesafe-request-id") ?? undefined;
252
- t.result = capture(data, warnings, this.options.redact);
288
+ t.result = result;
253
289
  t.captureWarnings = [...warnings];
254
290
  });
255
291
  }, fail)
256
- .catch((error) => {
257
- fail(error);
292
+ .catch(() => {
293
+ /* Recording errors must not affect inference or expose data. */
258
294
  });
259
295
  return promise;
260
296
  }
@@ -1,2 +1,2 @@
1
1
  import type { JevDevtoolsProps } from "./index.js";
2
- export default function ConnectedInspector({ endpoint, defaultOpen, }: Omit<JevDevtoolsProps, "enabled">): import("react").JSX.Element;
2
+ export default function ConnectedInspector({ endpoint, defaultOpen, redactAdditionalSensitiveData, }: Omit<JevDevtoolsProps, "enabled">): import("react").JSX.Element;
@@ -1,5 +1,6 @@
1
1
  import { jsx as _jsx } from "react/jsx-runtime";
2
- import { memo, useCallback, useEffect, useRef, useState } from "react";
2
+ import { memo, useCallback, useEffect, useMemo, useRef, useState } from "react";
3
+ import { createDisplayRedactor } from "./display-redaction.js";
3
4
  import { JevPanel } from "./JevDevtools.js";
4
5
  import { JevDrawer } from "./JevDrawer.js";
5
6
  import { connectTraceStream } from "./trace-stream.js";
@@ -15,7 +16,7 @@ function endpointUrl(endpoint) {
15
16
  throw new Error("Use a same-origin inspector endpoint without a query or fragment.");
16
17
  return url.href.replace(/\/$/, "");
17
18
  }
18
- export default function ConnectedInspector({ endpoint = "/api/jev-devtools", defaultOpen = true, }) {
19
+ export default function ConnectedInspector({ endpoint = "/api/jev-devtools", defaultOpen = true, redactAdditionalSensitiveData = true, }) {
19
20
  const [open, setOpen] = useState(defaultOpen);
20
21
  const [visited, setVisited] = useState(defaultOpen);
21
22
  const visible = useRef(defaultOpen);
@@ -25,6 +26,16 @@ export default function ConnectedInspector({ endpoint = "/api/jev-devtools", def
25
26
  flows: emptyFlows,
26
27
  });
27
28
  const [snapshot, setSnapshot] = useState(latest.current);
29
+ const [redactor] = useState(createDisplayRedactor);
30
+ const traces = useMemo(() => redactAdditionalSensitiveData
31
+ ? snapshot.traces.map(redactor.trace)
32
+ : snapshot.traces, [snapshot.traces, redactor, redactAdditionalSensitiveData]);
33
+ const flows = useMemo(() => redactAdditionalSensitiveData
34
+ ? {
35
+ runs: snapshot.flows.runs.map(redactor.run),
36
+ definitions: snapshot.flows.definitions.map(redactor.definition),
37
+ }
38
+ : snapshot.flows, [snapshot.flows, redactor, redactAdditionalSensitiveData]);
28
39
  const toggle = (value) => {
29
40
  visible.current = value;
30
41
  setOpen(value);
@@ -91,5 +102,5 @@ export default function ConnectedInspector({ endpoint = "/api/jev-devtools", def
91
102
  if (!response.ok)
92
103
  throw new Error(`Cannot clear history (${response.status})`);
93
104
  }, [endpoint]);
94
- return (_jsx(JevDrawer, { open: open, onOpenChange: toggle, children: visited && _jsx(RetainedPanel, { ...snapshot, onClear: clear }) }));
105
+ return (_jsx(JevDrawer, { open: open, onOpenChange: toggle, children: visited && (_jsx(RetainedPanel, { traces: traces, flows: flows, connection: snapshot.connection, onClear: clear })) }));
95
106
  }
@@ -0,0 +1,22 @@
1
+ import type { FlowDefinition, FlowRun } from "./flow-protocol.js";
2
+ import type { Trace } from "./protocol.js";
3
+ import { type BuiltFlowFixture } from "./flow-fixture.js";
4
+ type CopyResult = {
5
+ built: BuiltFlowFixture;
6
+ json: string;
7
+ copied: boolean;
8
+ status: FlowRun["status"];
9
+ };
10
+ export type FixtureCopy = {
11
+ copy: () => void;
12
+ disabled: boolean;
13
+ result?: CopyResult;
14
+ };
15
+ export declare function useFixtureCopy(definition: FlowDefinition, run: FlowRun, traces: ReadonlyMap<string, Trace>): FixtureCopy;
16
+ export declare function CopyFixtureButton({ control }: {
17
+ control: FixtureCopy;
18
+ }): import("react").JSX.Element;
19
+ export declare function CopyFixtureReport({ control }: {
20
+ control: FixtureCopy;
21
+ }): import("react").JSX.Element | null;
22
+ export {};
@@ -0,0 +1,67 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { useEffect, useMemo, useRef, useState } from "react";
3
+ import { flowFixture, hasCompletedJev, } from "./flow-fixture.js";
4
+ const plural = (count, word) => `${count} ${word}${count === 1 ? "" : "s"}`;
5
+ function partial(status) {
6
+ return status === "error"
7
+ ? "Run failed. The fixture has completed Jev responses only."
8
+ : status === "cancelled"
9
+ ? "Run cancelled. The fixture has completed Jev responses only."
10
+ : undefined;
11
+ }
12
+ export function useFixtureCopy(definition, run, traces) {
13
+ const source = useMemo(() => ({ definition, run, traces }), [definition, run, traces]);
14
+ const [attempt, setAttempt] = useState();
15
+ const pending = useRef(undefined);
16
+ // Masking replaces the display projection. Hide its old JSON during render,
17
+ // including results from clipboard promises that settle after that change.
18
+ const current = attempt?.source === source ? attempt : undefined;
19
+ const completed = useMemo(() => hasCompletedJev(definition, run), [definition, run]);
20
+ const copy = async () => {
21
+ if (pending.current === source)
22
+ return;
23
+ pending.current = source;
24
+ setAttempt({ source });
25
+ const built = flowFixture(definition, run, traces);
26
+ const json = JSON.stringify(built.fixture, null, 2);
27
+ let copied = false;
28
+ try {
29
+ await navigator.clipboard.writeText(json);
30
+ copied = true;
31
+ }
32
+ catch {
33
+ // Clipboard access can be refused; show the JSON for manual copying instead.
34
+ }
35
+ setAttempt((latest) => latest?.source === source
36
+ ? { source, result: { built, json, copied, status: run.status } }
37
+ : latest);
38
+ if (pending.current === source)
39
+ pending.current = undefined;
40
+ };
41
+ return {
42
+ copy: () => void copy(),
43
+ disabled: (!!current && !current.result) || run.status === "pending" || !completed,
44
+ result: current?.result,
45
+ };
46
+ }
47
+ export function CopyFixtureButton({ control }) {
48
+ const [confirmation, setConfirmation] = useState();
49
+ useEffect(() => {
50
+ setConfirmation(control.result?.copied ? control.result : undefined);
51
+ if (!control.result?.copied)
52
+ return;
53
+ const timer = setTimeout(() => setConfirmation(undefined), 2000);
54
+ return () => clearTimeout(timer);
55
+ }, [control.result]);
56
+ const copied = !!control.result?.copied && confirmation === control.result;
57
+ return (_jsx("button", { className: "flow-fixture-button", "data-copied": copied || undefined, "aria-live": "polite", "aria-atomic": "true", disabled: control.disabled, onClick: control.copy, children: copied ? "Copied" : "Copy fixture" }));
58
+ }
59
+ export function CopyFixtureReport({ control }) {
60
+ const { result } = control;
61
+ const note = result && partial(result.status);
62
+ if (!result ||
63
+ (result.copied && !note && !result.built.missing.length && !result.built.warnings.length)) {
64
+ return null;
65
+ }
66
+ return (_jsxs("div", { className: "flow-fixture", children: [_jsxs("div", { role: "status", children: [!result.copied && (_jsx("p", { className: "muted", children: "Clipboard unavailable. Select the JSON below." })), result.built.missing.length > 0 && (_jsxs("p", { className: "warning", children: [plural(result.built.missing.length, "Jev response"), " missing from the fixture."] })), note && _jsx("p", { className: "warning", children: note }), result.built.warnings.length > 0 && (_jsxs("p", { className: "warning", children: ["Capture warnings: ", result.built.warnings.join(" ")] }))] }), result.built.missing.length > 0 && (_jsxs("details", { children: [_jsx("summary", { children: "Missing steps" }), _jsx("ul", { tabIndex: 0, "aria-label": "Steps missing from the fixture", children: result.built.missing.map((id) => (_jsx("li", { children: id }, id))) })] })), !result.copied && (_jsx("textarea", { "aria-label": "Fixture JSON", readOnly: true, value: result.json, onFocus: (e) => e.currentTarget.select() }))] }));
67
+ }