@flanksource/clicky-ui 0.3.85 → 0.3.87

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 (87) hide show
  1. package/dist/components.cjs +0 -1
  2. package/dist/components.d.ts +1 -1
  3. package/dist/components.d.ts.map +1 -1
  4. package/dist/components.js +2 -2
  5. package/dist/data/ProgressBars.cjs +157 -101
  6. package/dist/data/ProgressBars.cjs.map +1 -1
  7. package/dist/data/ProgressBars.d.ts +1 -1
  8. package/dist/data/ProgressBars.d.ts.map +1 -1
  9. package/dist/data/ProgressBars.js +157 -101
  10. package/dist/data/ProgressBars.js.map +1 -1
  11. package/dist/data/ai/PromptCatalog/PromptCatalogTable.cjs +1 -0
  12. package/dist/data/ai/PromptCatalog/PromptCatalogTable.cjs.map +1 -1
  13. package/dist/data/ai/PromptCatalog/PromptCatalogTable.d.ts.map +1 -1
  14. package/dist/data/ai/PromptCatalog/PromptCatalogTable.js +1 -0
  15. package/dist/data/ai/PromptCatalog/PromptCatalogTable.js.map +1 -1
  16. package/dist/data/traces/ProcessResourceMetrics.cjs +99 -0
  17. package/dist/data/traces/ProcessResourceMetrics.cjs.map +1 -0
  18. package/dist/data/traces/ProcessResourceMetrics.d.ts +10 -0
  19. package/dist/data/traces/ProcessResourceMetrics.d.ts.map +1 -0
  20. package/dist/data/traces/ProcessResourceMetrics.js +99 -0
  21. package/dist/data/traces/ProcessResourceMetrics.js.map +1 -0
  22. package/dist/data/traces/ProcessTraceDetail.cjs +85 -108
  23. package/dist/data/traces/ProcessTraceDetail.cjs.map +1 -1
  24. package/dist/data/traces/ProcessTraceDetail.d.ts +7 -1
  25. package/dist/data/traces/ProcessTraceDetail.d.ts.map +1 -1
  26. package/dist/data/traces/ProcessTraceDetail.js +88 -111
  27. package/dist/data/traces/ProcessTraceDetail.js.map +1 -1
  28. package/dist/data/traces/ProcessUsageBars.cjs +37 -41
  29. package/dist/data/traces/ProcessUsageBars.cjs.map +1 -1
  30. package/dist/data/traces/ProcessUsageBars.d.ts.map +1 -1
  31. package/dist/data/traces/ProcessUsageBars.js +37 -41
  32. package/dist/data/traces/ProcessUsageBars.js.map +1 -1
  33. package/dist/data/traces/process-tree.cjs +35 -0
  34. package/dist/data/traces/process-tree.cjs.map +1 -1
  35. package/dist/data/traces/process-tree.d.ts +15 -0
  36. package/dist/data/traces/process-tree.d.ts.map +1 -1
  37. package/dist/data/traces/process-tree.js +35 -1
  38. package/dist/data/traces/process-tree.js.map +1 -1
  39. package/dist/data/version-info.cjs +3 -3
  40. package/dist/data/version-info.js +3 -3
  41. package/dist/expressions/playground/ExpressionPlayground.cjs +207 -156
  42. package/dist/expressions/playground/ExpressionPlayground.cjs.map +1 -1
  43. package/dist/expressions/playground/ExpressionPlayground.d.ts +1 -6
  44. package/dist/expressions/playground/ExpressionPlayground.d.ts.map +1 -1
  45. package/dist/expressions/playground/ExpressionPlayground.js +207 -156
  46. package/dist/expressions/playground/ExpressionPlayground.js.map +1 -1
  47. package/dist/expressions/playground/useEvaluator.cjs +56 -42
  48. package/dist/expressions/playground/useEvaluator.cjs.map +1 -1
  49. package/dist/expressions/playground/useEvaluator.d.ts +0 -2
  50. package/dist/expressions/playground/useEvaluator.d.ts.map +1 -1
  51. package/dist/expressions/playground/useEvaluator.js +56 -42
  52. package/dist/expressions/playground/useEvaluator.js.map +1 -1
  53. package/dist/expressions/playground/useSampleRuns.cjs +22 -18
  54. package/dist/expressions/playground/useSampleRuns.cjs.map +1 -1
  55. package/dist/expressions/playground/useSampleRuns.d.ts +2 -3
  56. package/dist/expressions/playground/useSampleRuns.d.ts.map +1 -1
  57. package/dist/expressions/playground/useSampleRuns.js +22 -18
  58. package/dist/expressions/playground/useSampleRuns.js.map +1 -1
  59. package/dist/hooks/use-latest.cjs +14 -0
  60. package/dist/hooks/use-latest.cjs.map +1 -0
  61. package/dist/hooks/use-latest.d.ts +3 -0
  62. package/dist/hooks/use-latest.d.ts.map +1 -0
  63. package/dist/hooks/use-latest.js +14 -0
  64. package/dist/hooks/use-latest.js.map +1 -0
  65. package/dist/index.cjs +0 -1
  66. package/dist/index.js +2 -2
  67. package/dist/overlay/ConfirmDialog.cjs +12 -37
  68. package/dist/overlay/ConfirmDialog.cjs.map +1 -1
  69. package/dist/overlay/ConfirmDialog.d.ts +3 -17
  70. package/dist/overlay/ConfirmDialog.d.ts.map +1 -1
  71. package/dist/overlay/ConfirmDialog.js +12 -37
  72. package/dist/overlay/ConfirmDialog.js.map +1 -1
  73. package/dist/overlay/useConfirm.cjs +1 -16
  74. package/dist/overlay/useConfirm.cjs.map +1 -1
  75. package/dist/overlay/useConfirm.d.ts +1 -9
  76. package/dist/overlay/useConfirm.d.ts.map +1 -1
  77. package/dist/overlay/useConfirm.js +3 -17
  78. package/dist/overlay/useConfirm.js.map +1 -1
  79. package/dist/runtime/barrels.json +1574 -1576
  80. package/dist/runtime/importmap.json +2184 -2183
  81. package/package.json +1 -1
  82. package/dist/overlay/confirmMemory.cjs +0 -58
  83. package/dist/overlay/confirmMemory.cjs.map +0 -1
  84. package/dist/overlay/confirmMemory.d.ts +0 -8
  85. package/dist/overlay/confirmMemory.d.ts.map +0 -1
  86. package/dist/overlay/confirmMemory.js +0 -54
  87. package/dist/overlay/confirmMemory.js.map +0 -1
@@ -1,3 +1,4 @@
1
+ const require_use_latest = require("../../hooks/use-latest.cjs");
1
2
  const require_api = require("./api.cjs");
2
3
  let react = require("react");
3
4
  // src/expressions/playground/useEvaluator.ts
@@ -15,54 +16,70 @@ var DEBOUNCE_MS = 250;
15
16
  */
16
17
  function useEvaluator(apiBase, payload, options = {}) {
17
18
  const explicit = options.mode === "explicit";
18
- const [response, setResponse] = (0, react.useState)(null);
19
- const [pending, setPending] = (0, react.useState)(false);
20
19
  const [autoRun, setAutoRunState] = (0, react.useState)(readAutoRun);
21
- const [evaluated, setEvaluated] = (0, react.useState)(null);
22
- const abortRef = (0, react.useRef)(void 0);
23
- const runningRef = (0, react.useRef)(false);
24
- const optionsRef = (0, react.useRef)(options);
25
- optionsRef.current = options;
26
- const payloadRef = (0, react.useRef)(payload);
27
- payloadRef.current = payload;
20
+ const [evaluation, setEvaluation] = (0, react.useState)(null);
21
+ const [request, setRequest] = (0, react.useState)(null);
22
+ const [session, setSession] = (0, react.useState)(() => ({ language: payload.language }));
23
+ if (session.language !== payload.language) setSession({ language: payload.language });
24
+ const activeRequest = (0, react.useRef)(null);
25
+ const latest = require_use_latest.useLatest({
26
+ payload,
27
+ session,
28
+ evaluate: options.evaluate
29
+ });
30
+ (0, react.useEffect)(() => () => {
31
+ const active = activeRequest.current;
32
+ if (active?.session === session) active.controller.abort();
33
+ }, [session]);
28
34
  const evaluateNow = (0, react.useCallback)(async () => {
29
- const current = payloadRef.current;
30
- if (explicit && runningRef.current) return;
35
+ const { payload: current, session, evaluate: evaluateLocally } = latest.current;
36
+ const active = activeRequest.current;
37
+ if (explicit && active?.explicit && active.session === session) return;
38
+ active?.controller.abort();
31
39
  if (!current.source.trim()) {
32
- if (!explicit) abortRef.current?.abort();
33
- setResponse(null);
34
- setEvaluated(current);
35
- setPending(false);
40
+ activeRequest.current = null;
41
+ setRequest(null);
42
+ setEvaluation({
43
+ session,
44
+ payload: current,
45
+ response: null
46
+ });
36
47
  return;
37
48
  }
38
- if (!explicit) abortRef.current?.abort();
39
49
  const controller = new AbortController();
40
- abortRef.current = controller;
41
- if (explicit) runningRef.current = true;
50
+ const next = {
51
+ session,
52
+ controller,
53
+ explicit
54
+ };
55
+ activeRequest.current = next;
56
+ setRequest(next);
42
57
  try {
43
- if (optionsRef.current.beforeRun && !await optionsRef.current.beforeRun([current])) return;
44
- setPending(true);
45
- const next = await (optionsRef.current.evaluate ?? ((request, signal) => require_api.evaluate(apiBase, request, signal)))(current, controller.signal);
46
- if (!controller.signal.aborted) {
47
- setResponse(next);
48
- setEvaluated(current);
49
- }
58
+ const response = await (evaluateLocally ?? ((request, signal) => require_api.evaluate(apiBase, request, signal)))(current, controller.signal);
59
+ if (!controller.signal.aborted) setEvaluation({
60
+ session,
61
+ payload: current,
62
+ response
63
+ });
50
64
  } catch (cause) {
51
- if (!controller.signal.aborted) {
52
- setResponse({
65
+ if (!controller.signal.aborted) setEvaluation({
66
+ session,
67
+ payload: current,
68
+ response: {
53
69
  result: "",
54
70
  durationMs: 0,
55
71
  error: { message: String(cause) }
56
- });
57
- setEvaluated(current);
58
- }
72
+ }
73
+ });
59
74
  } finally {
60
- if (abortRef.current === controller) {
61
- runningRef.current = false;
62
- setPending(false);
63
- }
75
+ if (activeRequest.current === next) activeRequest.current = null;
76
+ setRequest((current) => current === next ? null : current);
64
77
  }
65
- }, [apiBase, explicit]);
78
+ }, [
79
+ apiBase,
80
+ explicit,
81
+ latest
82
+ ]);
66
83
  (0, react.useEffect)(() => {
67
84
  if (explicit || !autoRun) return;
68
85
  const timer = setTimeout(() => {
@@ -77,19 +94,16 @@ function useEvaluator(apiBase, payload, options = {}) {
77
94
  explicit,
78
95
  evaluateNow
79
96
  ]);
80
- (0, react.useEffect)(() => {
81
- setResponse(null);
82
- setEvaluated(null);
83
- }, [payload.language]);
84
97
  const setAutoRun = (0, react.useCallback)((next) => {
85
98
  setAutoRunState(next);
86
99
  window.localStorage.setItem(AUTO_RUN_STORAGE_KEY, String(next));
87
100
  if (next && !explicit) evaluateNow();
88
101
  }, [evaluateNow, explicit]);
102
+ const current = evaluation?.session === session ? evaluation : null;
89
103
  return {
90
- response,
91
- pending,
92
- stale: isStale(evaluated, payload),
104
+ response: current?.response ?? null,
105
+ pending: request?.session === session,
106
+ stale: isStale(current?.payload ?? null, payload),
93
107
  autoRun: explicit ? false : autoRun,
94
108
  explicit,
95
109
  setAutoRun,
@@ -1 +1 @@
1
- {"version":3,"file":"useEvaluator.cjs","names":["useState","useRef","useCallback","evaluate"],"sources":["../../../src/expressions/playground/useEvaluator.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { evaluate, type EvalRequest, type EvalResponse } from \"./api.ts\";\n\nconst AUTO_RUN_STORAGE_KEY = \"expressions:auto-run\";\n\n/** How long typing settles before an automatic run fires. */\nconst DEBOUNCE_MS = 250;\n\nexport interface Evaluator {\n response: EvalResponse | null;\n /** A request is in flight or a debounce is pending. */\n pending: boolean;\n /** The source or input has changed since the shown result was produced. */\n stale: boolean;\n autoRun: boolean;\n explicit: boolean;\n setAutoRun: (next: boolean) => void;\n /** Evaluates now, skipping the debounce. */\n run: () => Promise<void>;\n}\n\ntype Payload = Pick<EvalRequest, \"language\" | \"source\" | \"input\">;\n\nexport interface EvaluatorOptions {\n mode?: \"automatic\" | \"explicit\";\n evaluate?: (request: EvalRequest, signal: AbortSignal) => Promise<EvalResponse>;\n /** Every evaluation a run is about to make; one call covers a whole batch. */\n beforeRun?: (requests: EvalRequest[]) => boolean | Promise<boolean>;\n}\n\n/**\n * Runs an expression against the Go evaluator.\n *\n * Automatic evaluation is convenient for a one-line expression and a nuisance\n * for anything longer: a debounce fires mid-keystroke and reports errors for\n * half-written input. So it is a toggle, and an explicit run is always\n * available -- which is also the only way to re-run an expression whose value\n * changes on its own (`time.Now()`, `uuid.V4()`, `random.*`).\n */\nexport function useEvaluator(apiBase: string, payload: Payload, options: EvaluatorOptions = {}): Evaluator {\n const explicit = options.mode === \"explicit\";\n const [response, setResponse] = useState<EvalResponse | null>(null);\n const [pending, setPending] = useState(false);\n const [autoRun, setAutoRunState] = useState(readAutoRun);\n const [evaluated, setEvaluated] = useState<Payload | null>(null);\n\n const abortRef = useRef<AbortController | undefined>(undefined);\n const runningRef = useRef(false);\n const optionsRef = useRef(options);\n optionsRef.current = options;\n // The current payload, so `run` stays referentially stable: it is wired into\n // a Monaco action registered once at mount, which would otherwise capture the\n // payload from the first render forever.\n const payloadRef = useRef(payload);\n payloadRef.current = payload;\n\n const evaluateNow = useCallback(async () => {\n const current = payloadRef.current;\n if (explicit && runningRef.current) return;\n if (!current.source.trim()) {\n if (!explicit) abortRef.current?.abort();\n setResponse(null);\n setEvaluated(current);\n setPending(false);\n return;\n }\n\n if (!explicit) abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n if (explicit) runningRef.current = true;\n try {\n // Pending starts once the run is confirmed: while a confirmation is on\n // screen nothing is evaluating, and the result panel must not say so.\n if (optionsRef.current.beforeRun && !(await optionsRef.current.beforeRun([current]))) return;\n setPending(true);\n const next = await (optionsRef.current.evaluate ?? ((request, signal) => evaluate(apiBase, request, signal)))(current, controller.signal);\n if (!controller.signal.aborted) {\n setResponse(next);\n setEvaluated(current);\n }\n } catch (cause) {\n if (!controller.signal.aborted) {\n setResponse({ result: \"\", durationMs: 0, error: { message: String(cause) } });\n setEvaluated(current);\n }\n } finally {\n if (abortRef.current === controller) {\n runningRef.current = false;\n setPending(false);\n }\n }\n }, [apiBase, explicit]);\n\n useEffect(() => {\n if (explicit || !autoRun) return;\n const timer = setTimeout(() => { void evaluateNow(); }, DEBOUNCE_MS);\n return () => clearTimeout(timer);\n }, [payload.language, payload.source, payload.input, autoRun, explicit, evaluateNow]);\n\n // Switching language changes what the source even means, so show nothing\n // rather than the previous language's result.\n useEffect(() => {\n setResponse(null);\n setEvaluated(null);\n }, [payload.language]);\n\n const setAutoRun = useCallback(\n (next: boolean) => {\n setAutoRunState(next);\n window.localStorage.setItem(AUTO_RUN_STORAGE_KEY, String(next));\n if (next && !explicit) void evaluateNow();\n },\n [evaluateNow, explicit],\n );\n\n return {\n response,\n pending,\n stale: isStale(evaluated, payload),\n autoRun: explicit ? false : autoRun,\n explicit,\n setAutoRun,\n run: evaluateNow,\n };\n}\n\nfunction isStale(evaluated: Payload | null, current: Payload): boolean {\n if (!current.source.trim()) return false;\n if (!evaluated) return true;\n return (\n evaluated.source !== current.source ||\n evaluated.input !== current.input ||\n evaluated.language !== current.language\n );\n}\n\nfunction readAutoRun(): boolean {\n if (typeof window === \"undefined\") return true;\n // Default on: the playground should evaluate as soon as it opens.\n return window.localStorage.getItem(AUTO_RUN_STORAGE_KEY) !== \"false\";\n}\n"],"mappings":";;;AAG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AAQ,SAC5B,UAAU,aAAa,QAAQ;AAEnC;AAEA,SAAS,cAAuB;CAC9B,IAAI,OAAO,WAAW,aAAa,OAAO;CAE1C,OAAO,OAAO,aAAa,QAAQ,oBAAoB,MAAM;AAC/D"}
1
+ {"version":3,"file":"useEvaluator.cjs","names":["useState","useRef","useLatest","useCallback","evaluate"],"sources":["../../../src/expressions/playground/useEvaluator.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useLatest } from \"../../hooks/use-latest\";\nimport { evaluate, type EvalRequest, type EvalResponse } from \"./api.ts\";\n\nconst AUTO_RUN_STORAGE_KEY = \"expressions:auto-run\";\n\n/** How long typing settles before an automatic run fires. */\nconst DEBOUNCE_MS = 250;\n\nexport interface Evaluator {\n response: EvalResponse | null;\n /** A request is in flight or a debounce is pending. */\n pending: boolean;\n /** The source or input has changed since the shown result was produced. */\n stale: boolean;\n autoRun: boolean;\n explicit: boolean;\n setAutoRun: (next: boolean) => void;\n /** Evaluates now, skipping the debounce. */\n run: () => Promise<void>;\n}\n\ntype Payload = Pick<EvalRequest, \"language\" | \"source\" | \"input\">;\n\ntype LanguageSession = { language: Payload[\"language\"] };\ntype Evaluation = { session: LanguageSession; payload: Payload; response: EvalResponse | null };\ntype EvaluationRequest = { session: LanguageSession; controller: AbortController; explicit: boolean };\n\nexport interface EvaluatorOptions {\n mode?: \"automatic\" | \"explicit\";\n evaluate?: (request: EvalRequest, signal: AbortSignal) => Promise<EvalResponse>;\n}\n\n/**\n * Runs an expression against the Go evaluator.\n *\n * Automatic evaluation is convenient for a one-line expression and a nuisance\n * for anything longer: a debounce fires mid-keystroke and reports errors for\n * half-written input. So it is a toggle, and an explicit run is always\n * available -- which is also the only way to re-run an expression whose value\n * changes on its own (`time.Now()`, `uuid.V4()`, `random.*`).\n */\nexport function useEvaluator(apiBase: string, payload: Payload, options: EvaluatorOptions = {}): Evaluator {\n const explicit = options.mode === \"explicit\";\n const [autoRun, setAutoRunState] = useState(readAutoRun);\n const [evaluation, setEvaluation] = useState<Evaluation | null>(null);\n const [request, setRequest] = useState<EvaluationRequest | null>(null);\n const [session, setSession] = useState<LanguageSession>(() => ({ language: payload.language }));\n if (session.language !== payload.language) setSession({ language: payload.language });\n\n const activeRequest = useRef<EvaluationRequest | null>(null);\n const latest = useLatest({ payload, session, evaluate: options.evaluate });\n\n useEffect(() => () => {\n const active = activeRequest.current;\n if (active?.session === session) active.controller.abort();\n }, [session]);\n\n const evaluateNow = useCallback(async () => {\n const { payload: current, session, evaluate: evaluateLocally } = latest.current;\n const active = activeRequest.current;\n if (explicit && active?.explicit && active.session === session) return;\n active?.controller.abort();\n if (!current.source.trim()) {\n activeRequest.current = null;\n setRequest(null);\n setEvaluation({ session, payload: current, response: null });\n return;\n }\n\n const controller = new AbortController();\n const next = { session, controller, explicit };\n activeRequest.current = next;\n setRequest(next);\n try {\n const response = await (evaluateLocally ?? ((request, signal) => evaluate(apiBase, request, signal)))(current, controller.signal);\n if (!controller.signal.aborted) {\n setEvaluation({ session, payload: current, response });\n }\n } catch (cause) {\n if (!controller.signal.aborted) {\n setEvaluation({ session, payload: current, response: { result: \"\", durationMs: 0, error: { message: String(cause) } } });\n }\n } finally {\n if (activeRequest.current === next) activeRequest.current = null;\n setRequest((current) => current === next ? null : current);\n }\n }, [apiBase, explicit, latest]);\n\n useEffect(() => {\n if (explicit || !autoRun) return;\n const timer = setTimeout(() => { void evaluateNow(); }, DEBOUNCE_MS);\n return () => clearTimeout(timer);\n }, [payload.language, payload.source, payload.input, autoRun, explicit, evaluateNow]);\n\n const setAutoRun = useCallback(\n (next: boolean) => {\n setAutoRunState(next);\n window.localStorage.setItem(AUTO_RUN_STORAGE_KEY, String(next));\n if (next && !explicit) void evaluateNow();\n },\n [evaluateNow, explicit],\n );\n\n const current = evaluation?.session === session ? evaluation : null;\n return {\n response: current?.response ?? null,\n pending: request?.session === session,\n stale: isStale(current?.payload ?? null, payload),\n autoRun: explicit ? false : autoRun,\n explicit,\n setAutoRun,\n run: evaluateNow,\n };\n}\n\nfunction isStale(evaluated: Payload | null, current: Payload): boolean {\n if (!current.source.trim()) return false;\n if (!evaluated) return true;\n return (\n evaluated.source !== current.source ||\n evaluated.input !== current.input ||\n evaluated.language !== current.language\n );\n}\n\nfunction readAutoRun(): boolean {\n if (typeof window === \"undefined\") return true;\n // Default on: the playground should evaluate as soon as it opens.\n return window.localStorage.getItem(AUTO_RUN_STORAGE_KEY) !== \"false\";\n}\n"],"mappings":";;;;AAIA,IAAM,uBAAuB;;AAG7B,IAAM,cAAc;;;;;;;;;;AAmCpB,SAAgB,aAAa,SAAiB,SAAkB,UAA4B,CAAC,GAAc;CACzG,MAAM,WAAW,QAAQ,SAAS;CAClC,MAAM,CAAC,SAAS,oBAAA,GAAmBA,MAAAA,SAAAA,CAAS,WAAW;CACvD,MAAM,CAAC,YAAY,kBAAA,GAAiBA,MAAAA,SAAAA,CAA4B,IAAI;CACpE,MAAM,CAAC,SAAS,eAAA,GAAcA,MAAAA,SAAAA,CAAmC,IAAI;CACrE,MAAM,CAAC,SAAS,eAAA,GAAcA,MAAAA,SAAAA,QAAiC,EAAE,UAAU,QAAQ,SAAS,EAAE;CAC9F,IAAI,QAAQ,aAAa,QAAQ,UAAU,WAAW,EAAE,UAAU,QAAQ,SAAS,CAAC;CAEpF,MAAM,iBAAA,GAAgBC,MAAAA,OAAAA,CAAiC,IAAI;CAC3D,MAAM,SAASC,mBAAAA,UAAU;EAAE;EAAS;EAAS,UAAU,QAAQ;CAAS,CAAC;CAEzE,CAAA,GAAA,MAAA,UAAA,aAAsB;EACpB,MAAM,SAAS,cAAc;EAC7B,IAAI,QAAQ,YAAY,SAAS,OAAO,WAAW,MAAM;CAC3D,GAAG,CAAC,OAAO,CAAC;CAEZ,MAAM,eAAA,GAAcC,MAAAA,YAAAA,CAAY,YAAY;EAC1C,MAAM,EAAE,SAAS,SAAS,SAAS,UAAU,oBAAoB,OAAO;EACxE,MAAM,SAAS,cAAc;EAC7B,IAAI,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AiB;CAC1C,GACA,CAAC,aAAa,QAAQ,CACxB;CAEA,MAAM,UAAU,YAAY,YAAY,UAAU,aAAa;CAC/D,OAAO;EACL,UAAU,SAAS,YAAY;EAC/B,SAAS,SAAS,YAAY;EAC9B,OAAO,QAAQ,SAAS,WAAW,MAAM,OAAO;EAChD,SAAS,WAAW,QAAQ;EAC5B;EACA;EACA,KAAK;CACP;AACF;AAEA,SAAS,QAAQ,WAA2B,SAA2B;CACrE,IAAI,CAAC,QAAQ,OAAO,KAAK,GAAG,OAAO;CACnC,IAAI,CAAC,WAAW,OAAO;CACvB,OACE,UAAU,WAAW,QAAQ,UAC7B,UAAU,UAAU,QAAQ,SAC5B,UAAU,aAAa,QAAQ;AAEnC;AAEA,SAAS,cAAuB;CAC9B,IAAI,OAAO,WAAW,aAAa,OAAO;CAE1C,OAAO,OAAO,aAAa,QAAQ,oBAAoB,MAAM;AAC/D"}
@@ -15,8 +15,6 @@ type Payload = Pick<EvalRequest, "language" | "source" | "input">;
15
15
  export interface EvaluatorOptions {
16
16
  mode?: "automatic" | "explicit";
17
17
  evaluate?: (request: EvalRequest, signal: AbortSignal) => Promise<EvalResponse>;
18
- /** Every evaluation a run is about to make; one call covers a whole batch. */
19
- beforeRun?: (requests: EvalRequest[]) => boolean | Promise<boolean>;
20
18
  }
21
19
  /**
22
20
  * Runs an expression against the Go evaluator.
@@ -1 +1 @@
1
- {"version":3,"file":"useEvaluator.d.ts","sourceRoot":"","sources":["../../../src/expressions/playground/useEvaluator.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,KAAK,WAAW,EAAE,KAAK,YAAY,EAAE,MAAM,UAAU,CAAC;AAOzE,MAAM,WAAW,SAAS;IACxB,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAC;IAC9B,uDAAuD;IACvD,OAAO,EAAE,OAAO,CAAC;IACjB,2EAA2E;IAC3E,KAAK,EAAE,OAAO,CAAC;IACf,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE,OAAO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,WAAW,EAAE,UAAU,GAAG,QAAQ,GAAG,OAAO,CAAC,CAAC;AAMlE,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,WAAW,GAAG,UAAU,CAAC;IAChC,QAAQ,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,WAAW,KAAK,OAAO,CAAC,YAAY,CAAC,CAAC;CACjF;AAED;;;;;;;;GAQG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,GAAE,gBAAqB,GAAG,SAAS,CAwEzG"}
@@ -1,3 +1,4 @@
1
+ import { useLatest } from "../../hooks/use-latest.js";
1
2
  import { evaluate } from "./api.js";
2
3
  import { useCallback, useEffect, useRef, useState } from "react";
3
4
  // src/expressions/playground/useEvaluator.ts
@@ -15,54 +16,70 @@ var DEBOUNCE_MS = 250;
15
16
  */
16
17
  function useEvaluator(apiBase, payload, options = {}) {
17
18
  const explicit = options.mode === "explicit";
18
- const [response, setResponse] = useState(null);
19
- const [pending, setPending] = useState(false);
20
19
  const [autoRun, setAutoRunState] = useState(readAutoRun);
21
- const [evaluated, setEvaluated] = useState(null);
22
- const abortRef = useRef(void 0);
23
- const runningRef = useRef(false);
24
- const optionsRef = useRef(options);
25
- optionsRef.current = options;
26
- const payloadRef = useRef(payload);
27
- payloadRef.current = payload;
20
+ const [evaluation, setEvaluation] = useState(null);
21
+ const [request, setRequest] = useState(null);
22
+ const [session, setSession] = useState(() => ({ language: payload.language }));
23
+ if (session.language !== payload.language) setSession({ language: payload.language });
24
+ const activeRequest = useRef(null);
25
+ const latest = useLatest({
26
+ payload,
27
+ session,
28
+ evaluate: options.evaluate
29
+ });
30
+ useEffect(() => () => {
31
+ const active = activeRequest.current;
32
+ if (active?.session === session) active.controller.abort();
33
+ }, [session]);
28
34
  const evaluateNow = useCallback(async () => {
29
- const current = payloadRef.current;
30
- if (explicit && runningRef.current) return;
35
+ const { payload: current, session, evaluate: evaluateLocally } = latest.current;
36
+ const active = activeRequest.current;
37
+ if (explicit && active?.explicit && active.session === session) return;
38
+ active?.controller.abort();
31
39
  if (!current.source.trim()) {
32
- if (!explicit) abortRef.current?.abort();
33
- setResponse(null);
34
- setEvaluated(current);
35
- setPending(false);
40
+ activeRequest.current = null;
41
+ setRequest(null);
42
+ setEvaluation({
43
+ session,
44
+ payload: current,
45
+ response: null
46
+ });
36
47
  return;
37
48
  }
38
- if (!explicit) abortRef.current?.abort();
39
49
  const controller = new AbortController();
40
- abortRef.current = controller;
41
- if (explicit) runningRef.current = true;
50
+ const next = {
51
+ session,
52
+ controller,
53
+ explicit
54
+ };
55
+ activeRequest.current = next;
56
+ setRequest(next);
42
57
  try {
43
- if (optionsRef.current.beforeRun && !await optionsRef.current.beforeRun([current])) return;
44
- setPending(true);
45
- const next = await (optionsRef.current.evaluate ?? ((request, signal) => evaluate(apiBase, request, signal)))(current, controller.signal);
46
- if (!controller.signal.aborted) {
47
- setResponse(next);
48
- setEvaluated(current);
49
- }
58
+ const response = await (evaluateLocally ?? ((request, signal) => evaluate(apiBase, request, signal)))(current, controller.signal);
59
+ if (!controller.signal.aborted) setEvaluation({
60
+ session,
61
+ payload: current,
62
+ response
63
+ });
50
64
  } catch (cause) {
51
- if (!controller.signal.aborted) {
52
- setResponse({
65
+ if (!controller.signal.aborted) setEvaluation({
66
+ session,
67
+ payload: current,
68
+ response: {
53
69
  result: "",
54
70
  durationMs: 0,
55
71
  error: { message: String(cause) }
56
- });
57
- setEvaluated(current);
58
- }
72
+ }
73
+ });
59
74
  } finally {
60
- if (abortRef.current === controller) {
61
- runningRef.current = false;
62
- setPending(false);
63
- }
75
+ if (activeRequest.current === next) activeRequest.current = null;
76
+ setRequest((current) => current === next ? null : current);
64
77
  }
65
- }, [apiBase, explicit]);
78
+ }, [
79
+ apiBase,
80
+ explicit,
81
+ latest
82
+ ]);
66
83
  useEffect(() => {
67
84
  if (explicit || !autoRun) return;
68
85
  const timer = setTimeout(() => {
@@ -77,19 +94,16 @@ function useEvaluator(apiBase, payload, options = {}) {
77
94
  explicit,
78
95
  evaluateNow
79
96
  ]);
80
- useEffect(() => {
81
- setResponse(null);
82
- setEvaluated(null);
83
- }, [payload.language]);
84
97
  const setAutoRun = useCallback((next) => {
85
98
  setAutoRunState(next);
86
99
  window.localStorage.setItem(AUTO_RUN_STORAGE_KEY, String(next));
87
100
  if (next && !explicit) evaluateNow();
88
101
  }, [evaluateNow, explicit]);
102
+ const current = evaluation?.session === session ? evaluation : null;
89
103
  return {
90
- response,
91
- pending,
92
- stale: isStale(evaluated, payload),
104
+ response: current?.response ?? null,
105
+ pending: request?.session === session,
106
+ stale: isStale(current?.payload ?? null, payload),
93
107
  autoRun: explicit ? false : autoRun,
94
108
  explicit,
95
109
  setAutoRun,
@@ -1 +1 @@
1
- {"version":3,"file":"useEvaluator.js","names":[],"sources":["../../../src/expressions/playground/useEvaluator.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { evaluate, type EvalRequest, type EvalResponse } from \"./api.ts\";\n\nconst AUTO_RUN_STORAGE_KEY = \"expressions:auto-run\";\n\n/** How long typing settles before an automatic run fires. */\nconst DEBOUNCE_MS = 250;\n\nexport interface Evaluator {\n response: EvalResponse | null;\n /** A request is in flight or a debounce is pending. */\n pending: boolean;\n /** The source or input has changed since the shown result was produced. */\n stale: boolean;\n autoRun: boolean;\n explicit: boolean;\n setAutoRun: (next: boolean) => void;\n /** Evaluates now, skipping the debounce. */\n run: () => Promise<void>;\n}\n\ntype Payload = Pick<EvalRequest, \"language\" | \"source\" | \"input\">;\n\nexport interface EvaluatorOptions {\n mode?: \"automatic\" | \"explicit\";\n evaluate?: (request: EvalRequest, signal: AbortSignal) => Promise<EvalResponse>;\n /** Every evaluation a run is about to make; one call covers a whole batch. */\n beforeRun?: (requests: EvalRequest[]) => boolean | Promise<boolean>;\n}\n\n/**\n * Runs an expression against the Go evaluator.\n *\n * Automatic evaluation is convenient for a one-line expression and a nuisance\n * for anything longer: a debounce fires mid-keystroke and reports errors for\n * half-written input. So it is a toggle, and an explicit run is always\n * available -- which is also the only way to re-run an expression whose value\n * changes on its own (`time.Now()`, `uuid.V4()`, `random.*`).\n */\nexport function useEvaluator(apiBase: string, payload: Payload, options: EvaluatorOptions = {}): Evaluator {\n const explicit = options.mode === \"explicit\";\n const [response, setResponse] = useState<EvalResponse | null>(null);\n const [pending, setPending] = useState(false);\n const [autoRun, setAutoRunState] = useState(readAutoRun);\n const [evaluated, setEvaluated] = useState<Payload | null>(null);\n\n const abortRef = useRef<AbortController | undefined>(undefined);\n const runningRef = useRef(false);\n const optionsRef = useRef(options);\n optionsRef.current = options;\n // The current payload, so `run` stays referentially stable: it is wired into\n // a Monaco action registered once at mount, which would otherwise capture the\n // payload from the first render forever.\n const payloadRef = useRef(payload);\n payloadRef.current = payload;\n\n const evaluateNow = useCallback(async () => {\n const current = payloadRef.current;\n if (explicit && runningRef.current) return;\n if (!current.source.trim()) {\n if (!explicit) abortRef.current?.abort();\n setResponse(null);\n setEvaluated(current);\n setPending(false);\n return;\n }\n\n if (!explicit) abortRef.current?.abort();\n const controller = new AbortController();\n abortRef.current = controller;\n if (explicit) runningRef.current = true;\n try {\n // Pending starts once the run is confirmed: while a confirmation is on\n // screen nothing is evaluating, and the result panel must not say so.\n if (optionsRef.current.beforeRun && !(await optionsRef.current.beforeRun([current]))) return;\n setPending(true);\n const next = await (optionsRef.current.evaluate ?? ((request, signal) => evaluate(apiBase, request, signal)))(current, controller.signal);\n if (!controller.signal.aborted) {\n setResponse(next);\n setEvaluated(current);\n }\n } catch (cause) {\n if (!controller.signal.aborted) {\n setResponse({ result: \"\", durationMs: 0, error: { message: String(cause) } });\n setEvaluated(current);\n }\n } finally {\n if (abortRef.current === controller) {\n runningRef.current = false;\n setPending(false);\n }\n }\n }, [apiBase, explicit]);\n\n useEffect(() => {\n if (explicit || !autoRun) return;\n const timer = setTimeout(() => { void evaluateNow(); }, DEBOUNCE_MS);\n return () => clearTimeout(timer);\n }, [payload.language, payload.source, payload.input, autoRun, explicit, evaluateNow]);\n\n // Switching language changes what the source even means, so show nothing\n // rather than the previous language's result.\n useEffect(() => {\n setResponse(null);\n setEvaluated(null);\n }, [payload.language]);\n\n const setAutoRun = useCallback(\n (next: boolean) => {\n setAutoRunState(next);\n window.localStorage.setItem(AUTO_RUN_STORAGE_KEY, String(next));\n if (next && !explicit) void evaluateNow();\n },\n [evaluateNow, explicit],\n );\n\n return {\n response,\n pending,\n stale: isStale(evaluated, payload),\n autoRun: explicit ? false : autoRun,\n explicit,\n setAutoRun,\n run: evaluateNow,\n };\n}\n\nfunction isStale(evaluated: Payload | null, current: Payload): boolean {\n if (!current.source.trim()) return false;\n if (!evaluated) return true;\n return (\n evaluated.source !== current.source ||\n evaluated.input !== current.input ||\n evaluated.language !== current.language\n );\n}\n\nfunction readAutoRun(): boolean {\n if (typeof window === \"undefined\") return true;\n // Default on: the playground should evaluate as soon as it opens.\n return window.localStorage.getItem(AUTO_RUN_STORAGE_KEY) !== \"false\";\n}\n"],"mappings":";;;AAGA,IAAM,uBAAuB;;AAG7B,IAAM,cAAc;;;;;;;;;;AAiCpB,SAAgB,aAAa,SAAiB,SAAkB,UAA4B,CAAC,GAAc;CACzG,MAAM,WAAW,QAAQ,SAAS;CAClC,MAAM,CAAC,UAAU,eAAe,SAA8B,IAAI;CAClE,MAAM,CAAC,SAAS,cAAc,SAAS,KAAK;CAC5C,MAAM,CAAC,SAAS,mBAAmB,SAAS,WAAW;CACvD,MAAM,CAAC,WAAW,gBAAgB,SAAyB,IAAI;CAE/D,MAAM,WAAW,OAAoC,KAAA,CAAS;CAC9D,MAAM,aAAa,OAAO,KAAK;CAC/B,MAAM,aAAa,OAAO,OAAO;CACjC,WAAW,UAAU;CAIrB,MAAM,aAAa,OAAO,OAAO;CACjC,WAAW,UAAU;CAErB,MAAM,cAAc,YAAY,YAAY;EAC1C,MAAM,UAAU,WAAW;EAC3B,IAAI,YAAY,W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I;EACpB,OAAO,aAAa,QAAQ,sBAAsB,OAAO,IAAI,CAAC;EAC9D,IAAI,QAAQ,CAAC,UAAU,YAAiB;CAC1C,GACA,CAAC,aAAa,QAAQ,CACxB;CAEA,OAAO;EACL;EACA;EACA,OAAO,QAAQ,WAAW,OAAO;EACjC,SAAS,WAAW,QAAQ;EAC5B;EACA;EACA,KAAK;CACP;AACF;AAEA,SAAS,QAAQ,WAA2B,SAA2B;CACrE,IAAI,CAAC,QAAQ,OAAO,KAAK,GAAG,OAAO;CACnC,IAAI,CAAC,WAAW,OAAO;CACvB,OACE,UAAU,WAAW,QAAQ,UAC7B,UAAU,UAAU,QAAQ,SAC5B,UAAU,aAAa,QAAQ;AAEnC;AAEA,SAAS,cAAuB;CAC9B,IAAI,OAAO,WAAW,aAAa,OAAO;CAE1C,OAAO,OAAO,aAAa,QAAQ,oBAAoB,MAAM;AAC/D"}
1
+ {"version":3,"file":"useEvaluator.js","names":[],"sources":["../../../src/expressions/playground/useEvaluator.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useLatest } from \"../../hooks/use-latest\";\nimport { evaluate, type EvalRequest, type EvalResponse } from \"./api.ts\";\n\nconst AUTO_RUN_STORAGE_KEY = \"expressions:auto-run\";\n\n/** How long typing settles before an automatic run fires. */\nconst DEBOUNCE_MS = 250;\n\nexport interface Evaluator {\n response: EvalResponse | null;\n /** A request is in flight or a debounce is pending. */\n pending: boolean;\n /** The source or input has changed since the shown result was produced. */\n stale: boolean;\n autoRun: boolean;\n explicit: boolean;\n setAutoRun: (next: boolean) => void;\n /** Evaluates now, skipping the debounce. */\n run: () => Promise<void>;\n}\n\ntype Payload = Pick<EvalRequest, \"language\" | \"source\" | \"input\">;\n\ntype LanguageSession = { language: Payload[\"language\"] };\ntype Evaluation = { session: LanguageSession; payload: Payload; response: EvalResponse | null };\ntype EvaluationRequest = { session: LanguageSession; controller: AbortController; explicit: boolean };\n\nexport interface EvaluatorOptions {\n mode?: \"automatic\" | \"explicit\";\n evaluate?: (request: EvalRequest, signal: AbortSignal) => Promise<EvalResponse>;\n}\n\n/**\n * Runs an expression against the Go evaluator.\n *\n * Automatic evaluation is convenient for a one-line expression and a nuisance\n * for anything longer: a debounce fires mid-keystroke and reports errors for\n * half-written input. So it is a toggle, and an explicit run is always\n * available -- which is also the only way to re-run an expression whose value\n * changes on its own (`time.Now()`, `uuid.V4()`, `random.*`).\n */\nexport function useEvaluator(apiBase: string, payload: Payload, options: EvaluatorOptions = {}): Evaluator {\n const explicit = options.mode === \"explicit\";\n const [autoRun, setAutoRunState] = useState(readAutoRun);\n const [evaluation, setEvaluation] = useState<Evaluation | null>(null);\n const [request, setRequest] = useState<EvaluationRequest | null>(null);\n const [session, setSession] = useState<LanguageSession>(() => ({ language: payload.language }));\n if (session.language !== payload.language) setSession({ language: payload.language });\n\n const activeRequest = useRef<EvaluationRequest | null>(null);\n const latest = useLatest({ payload, session, evaluate: options.evaluate });\n\n useEffect(() => () => {\n const active = activeRequest.current;\n if (active?.session === session) active.controller.abort();\n }, [session]);\n\n const evaluateNow = useCallback(async () => {\n const { payload: current, session, evaluate: evaluateLocally } = latest.current;\n const active = activeRequest.current;\n if (explicit && active?.explicit && active.session === session) return;\n active?.controller.abort();\n if (!current.source.trim()) {\n activeRequest.current = null;\n setRequest(null);\n setEvaluation({ session, payload: current, response: null });\n return;\n }\n\n const controller = new AbortController();\n const next = { session, controller, explicit };\n activeRequest.current = next;\n setRequest(next);\n try {\n const response = await (evaluateLocally ?? ((request, signal) => evaluate(apiBase, request, signal)))(current, controller.signal);\n if (!controller.signal.aborted) {\n setEvaluation({ session, payload: current, response });\n }\n } catch (cause) {\n if (!controller.signal.aborted) {\n setEvaluation({ session, payload: current, response: { result: \"\", durationMs: 0, error: { message: String(cause) } } });\n }\n } finally {\n if (activeRequest.current === next) activeRequest.current = null;\n setRequest((current) => current === next ? null : current);\n }\n }, [apiBase, explicit, latest]);\n\n useEffect(() => {\n if (explicit || !autoRun) return;\n const timer = setTimeout(() => { void evaluateNow(); }, DEBOUNCE_MS);\n return () => clearTimeout(timer);\n }, [payload.language, payload.source, payload.input, autoRun, explicit, evaluateNow]);\n\n const setAutoRun = useCallback(\n (next: boolean) => {\n setAutoRunState(next);\n window.localStorage.setItem(AUTO_RUN_STORAGE_KEY, String(next));\n if (next && !explicit) void evaluateNow();\n },\n [evaluateNow, explicit],\n );\n\n const current = evaluation?.session === session ? evaluation : null;\n return {\n response: current?.response ?? null,\n pending: request?.session === session,\n stale: isStale(current?.payload ?? null, payload),\n autoRun: explicit ? false : autoRun,\n explicit,\n setAutoRun,\n run: evaluateNow,\n };\n}\n\nfunction isStale(evaluated: Payload | null, current: Payload): boolean {\n if (!current.source.trim()) return false;\n if (!evaluated) return true;\n return (\n evaluated.source !== current.source ||\n evaluated.input !== current.input ||\n evaluated.language !== current.language\n );\n}\n\nfunction readAutoRun(): boolean {\n if (typeof window === \"undefined\") return true;\n // Default on: the playground should evaluate as soon as it opens.\n return window.localStorage.getItem(AUTO_RUN_STORAGE_KEY) !== \"false\";\n}\n"],"mappings":";;;;AAIA,IAAM,uBAAuB;;AAG7B,IAAM,cAAc;;;;;;;;;;AAmCpB,SAAgB,aAAa,SAAiB,SAAkB,UAA4B,CAAC,GAAc;CACzG,MAAM,WAAW,QAAQ,SAAS;CAClC,MAAM,CAAC,SAAS,mBAAmB,SAAS,WAAW;CACvD,MAAM,CAAC,YAAY,iBAAiB,SAA4B,IAAI;CACpE,MAAM,CAAC,SAAS,cAAc,SAAmC,IAAI;CACrE,MAAM,CAAC,SAAS,cAAc,gBAAiC,EAAE,UAAU,QAAQ,SAAS,EAAE;CAC9F,IAAI,QAAQ,aAAa,QAAQ,UAAU,WAAW,EAAE,UAAU,QAAQ,SAAS,CAAC;CAEpF,MAAM,gBAAgB,OAAiC,IAAI;CAC3D,MAAM,SAAS,UAAU;EAAE;EAAS;EAAS,UAAU,QAAQ;CAAS,CAAC;CAEzE,sBAAsB;EACpB,MAAM,SAAS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AAS;EAAU;CAAW,CAAC;CAEpF,MAAM,aAAa,aAChB,SAAkB;EACjB,gBAAgB,IAAI;EACpB,OAAO,aAAa,QAAQ,sBAAsB,OAAO,IAAI,CAAC;EAC9D,IAAI,QAAQ,CAAC,UAAU,YAAiB;CAC1C,GACA,CAAC,aAAa,QAAQ,CACxB;CAEA,MAAM,UAAU,YAAY,YAAY,UAAU,aAAa;CAC/D,OAAO;EACL,UAAU,SAAS,YAAY;EAC/B,SAAS,SAAS,YAAY;EAC9B,OAAO,QAAQ,SAAS,WAAW,MAAM,OAAO;EAChD,SAAS,WAAW,QAAQ;EAC5B;EACA;EACA,KAAK;CACP;AACF;AAEA,SAAS,QAAQ,WAA2B,SAA2B;CACrE,IAAI,CAAC,QAAQ,OAAO,KAAK,GAAG,OAAO;CACnC,IAAI,CAAC,WAAW,OAAO;CACvB,OACE,UAAU,WAAW,QAAQ,UAC7B,UAAU,UAAU,QAAQ,SAC5B,UAAU,aAAa,QAAQ;AAEnC;AAEA,SAAS,cAAuB;CAC9B,IAAI,OAAO,WAAW,aAAa,OAAO;CAE1C,OAAO,OAAO,aAAa,QAAQ,oBAAoB,MAAM;AAC/D"}
@@ -1,3 +1,4 @@
1
+ const require_use_latest = require("../../hooks/use-latest.cjs");
1
2
  const require_api = require("./api.cjs");
2
3
  let react = require("react");
3
4
  // src/expressions/playground/useSampleRuns.ts
@@ -6,23 +7,22 @@ let react = require("react");
6
7
  *
7
8
  * Samples run one after another, never in parallel: a host evaluator may call
8
9
  * functions with side effects or that hand out generated values, and those
9
- * must see the samples in the order the author reads them. `beforeRun` sees the
10
- * whole batch, so a confirmation is asked once rather than per sample.
10
+ * must see the samples in the order the author reads them.
11
11
  */
12
12
  function useSampleRuns(apiBase, payload, options = {}) {
13
13
  const [results, setResults] = (0, react.useState)([]);
14
14
  const [progress, setProgress] = (0, react.useState)(null);
15
15
  const [evaluated, setEvaluated] = (0, react.useState)(null);
16
- const payloadRef = (0, react.useRef)(payload);
17
- payloadRef.current = payload;
18
- const optionsRef = (0, react.useRef)(options);
19
- optionsRef.current = options;
16
+ const latest = require_use_latest.useLatest({
17
+ payload,
18
+ evaluate: options.evaluate
19
+ });
20
20
  const runningRef = (0, react.useRef)(false);
21
21
  const abortRef = (0, react.useRef)(void 0);
22
22
  (0, react.useEffect)(() => () => abortRef.current?.abort(), []);
23
23
  const run = (0, react.useCallback)(async () => {
24
24
  if (runningRef.current) return;
25
- const current = payloadRef.current;
25
+ const { payload: current, evaluate: evaluate$1 } = latest.current;
26
26
  if (!current.source.trim() || current.samples.length === 0) {
27
27
  setResults([]);
28
28
  setEvaluated(fingerprint(current));
@@ -32,22 +32,25 @@ function useSampleRuns(apiBase, payload, options = {}) {
32
32
  const controller = new AbortController();
33
33
  abortRef.current = controller;
34
34
  const requests = current.samples.map((sample) => ({
35
- language: current.language,
36
- source: current.source,
37
- input: sample.input
35
+ sample,
36
+ request: {
37
+ language: current.language,
38
+ source: current.source,
39
+ input: sample.input
40
+ }
38
41
  }));
42
+ const total = requests.length;
39
43
  try {
40
- const { beforeRun, evaluate: evaluate$1 } = optionsRef.current;
41
- if (beforeRun && !await beforeRun(requests)) return;
42
44
  const evaluateOne = evaluate$1 ?? ((request, signal) => require_api.evaluate(apiBase, request, signal));
43
45
  const collected = [];
44
46
  setResults([]);
45
47
  setEvaluated(fingerprint(current));
46
48
  setProgress({
47
49
  done: 0,
48
- total: requests.length
50
+ total
49
51
  });
50
- for (const [index, request] of requests.entries()) {
52
+ const runSample = async ({ sample, request }, index) => {
53
+ if (controller.signal.aborted) return;
51
54
  let response;
52
55
  try {
53
56
  response = await evaluateOne(request, controller.signal);
@@ -61,20 +64,21 @@ function useSampleRuns(apiBase, payload, options = {}) {
61
64
  }
62
65
  if (controller.signal.aborted) return;
63
66
  collected.push({
64
- sample: current.samples[index],
67
+ sample,
65
68
  response
66
69
  });
67
70
  setResults([...collected]);
68
71
  setProgress({
69
72
  done: index + 1,
70
- total: requests.length
73
+ total
71
74
  });
72
- }
75
+ };
76
+ await requests.reduce((previous, entry, index) => previous.then(() => runSample(entry, index)), Promise.resolve());
73
77
  } finally {
74
78
  runningRef.current = false;
75
79
  if (!controller.signal.aborted) setProgress(null);
76
80
  }
77
- }, [apiBase]);
81
+ }, [apiBase, latest]);
78
82
  return {
79
83
  results,
80
84
  pending: progress !== null,
@@ -1 +1 @@
1
- {"version":3,"file":"useSampleRuns.cjs","names":["useState","useRef","useCallback","evaluate","evaluateRemote"],"sources":["../../../src/expressions/playground/useSampleRuns.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { evaluate as evaluateRemote, type EvalRequest, type EvalResponse, type PlaygroundSample } from \"./api.ts\";\nimport type { EvalLanguage } from \"./languages.ts\";\nimport type { EvaluatorOptions } from \"./useEvaluator.ts\";\n\nexport interface SampleRunsPayload {\n language: EvalLanguage;\n source: string;\n samples: PlaygroundSample[];\n}\n\nexport interface SampleResult {\n sample: PlaygroundSample;\n response: EvalResponse;\n}\n\nexport interface SampleRuns {\n /** In sample order, growing as each evaluation lands. */\n results: SampleResult[];\n pending: boolean;\n progress: { done: number; total: number } | null;\n /** The source, language or samples changed since the shown results. */\n stale: boolean;\n run: () => Promise<void>;\n}\n\n/**\n * Evaluates one expression against every sample, only when asked.\n *\n * Samples run one after another, never in parallel: a host evaluator may call\n * functions with side effects or that hand out generated values, and those\n * must see the samples in the order the author reads them. `beforeRun` sees the\n * whole batch, so a confirmation is asked once rather than per sample.\n */\nexport function useSampleRuns(\n apiBase: string,\n payload: SampleRunsPayload,\n options: Pick<EvaluatorOptions, \"evaluate\" | \"beforeRun\"> = {},\n): SampleRuns {\n const [results, setResults] = useState<SampleResult[]>([]);\n const [progress, setProgress] = useState<SampleRuns[\"progress\"]>(null);\n const [evaluated, setEvaluated] = useState<string | null>(null);\n\n const payloadRef = useRef(payload);\n payloadRef.current = payload;\n const optionsRef = useRef(options);\n optionsRef.current = options;\n const runningRef = useRef(false);\n const abortRef = useRef<AbortController | undefined>(undefined);\n\n useEffect(() => () => abortRef.current?.abort(), []);\n\n const run = useCallback(async () => {\n if (runningRef.current) return;\n const current = payloadRef.current;\n if (!current.source.trim() || current.samples.length === 0) {\n setResults([]);\n setEvaluated(fingerprint(current));\n return;\n }\n\n runningRef.current = true;\n const controller = new AbortController();\n abortRef.current = controller;\n const requests: EvalRequest[] = current.samples.map((sample) => ({\n language: current.language,\n source: current.source,\n input: sample.input,\n }));\n try {\n const { beforeRun, evaluate } = optionsRef.current;\n if (beforeRun && !(await beforeRun(requests))) return;\n const evaluateOne = evaluate ?? ((request: EvalRequest, signal: AbortSignal) => evaluateRemote(apiBase, request, signal));\n\n // The results shown from here on belong to this payload, so they are\n // current while they arrive and go stale only once it is edited.\n const collected: SampleResult[] = [];\n setResults([]);\n setEvaluated(fingerprint(current));\n setProgress({ done: 0, total: requests.length });\n for (const [index, request] of requests.entries()) {\n let response: EvalResponse;\n try {\n response = await evaluateOne(request, controller.signal);\n } catch (cause) {\n if (controller.signal.aborted) return;\n response = { result: \"\", durationMs: 0, error: { message: String(cause) } };\n }\n if (controller.signal.aborted) return;\n collected.push({ sample: current.samples[index]!, response });\n setResults([...collected]);\n setProgress({ done: index + 1, total: requests.length });\n }\n } finally {\n runningRef.current = false;\n if (!controller.signal.aborted) setProgress(null);\n }\n }, [apiBase]);\n\n return {\n results,\n pending: progress !== null,\n progress,\n stale: payload.source.trim() !== \"\" && evaluated !== fingerprint(payload),\n run,\n };\n}\n\nfunction fingerprint({ language, source, samples }: SampleRunsPayload): string {\n return JSON.stringify([language, source, samples.map((sample) => [sample.id, sample.input])]);\n}\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,cACd,SACA,SACA,UAA4D,CAAC,GACjD;CACZ,MAAM,CAAC,SAAS,eAAA,GAAcA,MAAAA,SAAAA,CAAyB,CAAC,CAAC;CACzD,MAAM,CAAC,UAAU,gBAAA,GAAeA,MAAAA,SAAAA,CAAiC,IAAI;CACrE,MAAM,CAAC,WAAW,iBAAA,GAAg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ACd,IAAI,WAAW,OAAO,SAAS;KAC/B,WAAW;MAAE,QAAQ;MAAI,YAAY;MAAG,OAAO,EAAE,SAAS,OAAO,KAAK,EAAE;KAAE;IAC5E;IACA,IAAI,WAAW,OAAO,SAAS;IAC/B,UAAU,KAAK;KAAE,QAAQ,QAAQ,QAAQ;KAAS;IAAS,CAAC;IAC5D,WAAW,CAAC,GAAG,SAAS,CAAC;IACzB,YAAY;KAAE,MAAM,QAAQ;KAAG,OAAO,SAAS;IAAO,CAAC;GACzD;EACF,UAAU;GACR,WAAW,UAAU;GACrB,IAAI,CAAC,WAAW,OAAO,SAAS,YAAY,IAAI;EAClD;CACF,GAAG,CAAC,OAAO,CAAC;CAEZ,OAAO;EACL;EACA,SAAS,aAAa;EACtB;EACA,OAAO,QAAQ,OAAO,KAAK,MAAM,MAAM,cAAc,YAAY,OAAO;EACxE;CACF;AACF;AAEA,SAAS,YAAY,EAAE,UAAU,QAAQ,WAAsC;CAC7E,OAAO,KAAK,UAAU;EAAC;EAAU;EAAQ,QAAQ,KAAK,WAAW,CAAC,OAAO,IAAI,OAAO,KAAK,CAAC;CAAC,CAAC;AAC9F"}
1
+ {"version":3,"file":"useSampleRuns.cjs","names":["useState","useLatest","useRef","useCallback","evaluate","evaluateRemote"],"sources":["../../../src/expressions/playground/useSampleRuns.ts"],"sourcesContent":["import { useCallback, useEffect, useRef, useState } from \"react\";\nimport { useLatest } from \"../../hooks/use-latest\";\nimport { evaluate as evaluateRemote, type EvalRequest, type EvalResponse, type PlaygroundSample } from \"./api.ts\";\nimport type { EvalLanguage } from \"./languages.ts\";\nimport type { EvaluatorOptions } from \"./useEvaluator.ts\";\n\nexport interface SampleRunsPayload {\n language: EvalLanguage;\n source: string;\n samples: PlaygroundSample[];\n}\n\nexport interface SampleResult {\n sample: PlaygroundSample;\n response: EvalResponse;\n}\n\nexport interface SampleRuns {\n /** In sample order, growing as each evaluation lands. */\n results: SampleResult[];\n pending: boolean;\n progress: { done: number; total: number } | null;\n /** The source, language or samples changed since the shown results. */\n stale: boolean;\n run: () => Promise<void>;\n}\n\n/**\n * Evaluates one expression against every sample, only when asked.\n *\n * Samples run one after another, never in parallel: a host evaluator may call\n * functions with side effects or that hand out generated values, and those\n * must see the samples in the order the author reads them.\n */\nexport function useSampleRuns(\n apiBase: string,\n payload: SampleRunsPayload,\n options: Pick<EvaluatorOptions, \"evaluate\"> = {},\n): SampleRuns {\n const [results, setResults] = useState<SampleResult[]>([]);\n const [progress, setProgress] = useState<SampleRuns[\"progress\"]>(null);\n const [evaluated, setEvaluated] = useState<string | null>(null);\n\n const latest = useLatest({ payload, evaluate: options.evaluate });\n const runningRef = useRef(false);\n const abortRef = useRef<AbortController | undefined>(undefined);\n\n useEffect(() => () => abortRef.current?.abort(), []);\n\n const run = useCallback(async () => {\n if (runningRef.current) return;\n const { payload: current, evaluate } = latest.current;\n if (!current.source.trim() || current.samples.length === 0) {\n setResults([]);\n setEvaluated(fingerprint(current));\n return;\n }\n\n runningRef.current = true;\n const controller = new AbortController();\n abortRef.current = controller;\n const requests = current.samples.map((sample) => ({\n sample,\n request: { language: current.language, source: current.source, input: sample.input },\n }));\n const total = requests.length;\n try {\n const evaluateOne = evaluate ?? ((request: EvalRequest, signal: AbortSignal) => evaluateRemote(apiBase, request, signal));\n\n // The results shown from here on belong to this payload, so they are\n // current while they arrive and go stale only once it is edited.\n const collected: SampleResult[] = [];\n setResults([]);\n setEvaluated(fingerprint(current));\n setProgress({ done: 0, total });\n const runSample = async ({ sample, request }: (typeof requests)[number], index: number) => {\n if (controller.signal.aborted) return;\n let response: EvalResponse;\n try {\n response = await evaluateOne(request, controller.signal);\n } catch (cause) {\n if (controller.signal.aborted) return;\n response = { result: \"\", durationMs: 0, error: { message: String(cause) } };\n }\n if (controller.signal.aborted) return;\n collected.push({ sample, response });\n setResults([...collected]);\n setProgress({ done: index + 1, total });\n };\n await requests.reduce(\n (previous, entry, index) => previous.then(() => runSample(entry, index)),\n Promise.resolve(),\n );\n } finally {\n runningRef.current = false;\n if (!controller.signal.aborted) setProgress(null);\n }\n }, [apiBase, latest]);\n\n return {\n results,\n pending: progress !== null,\n progress,\n stale: payload.source.trim() !== \"\" && evaluated !== fingerprint(payload),\n run,\n };\n}\n\nfunction fingerprint({ language, source, samples }: SampleRunsPayload): string {\n return JSON.stringify([language, source, samples.map((sample) => [sample.id, sample.input])]);\n}\n"],"mappings":";;;;;;;;;;;AAkCA,SAAgB,cACd,SACA,SACA,UAA8C,CAAC,GACnC;CACZ,MAAM,CAAC,SAAS,eAAA,GAAcA,MAAAA,SAAAA,CAAyB,CAAC,CAAC;CACzD,MAAM,CAAC,UAAU,gBAAA,GAAeA,MAAAA,SAAAA,CAAiC,IAAI;CACrE,MAAM,CAAC,WAAW,iBAAA,GAAgBA,MAAAA,SAAAA,CAAwB,IAAI;CAE9D,MAAM,SAASC,mBAAAA,UAAU;EAAE;EAAS,UAAU,QAAQ;CAAS,CAAC;CAChE,MAAM,cAAA,GAAaC,MAAAA,OAAAA,CAAO,KAAK;CAC/B,MAAM,YAAA,GAAWA,MAAAA,OAAAA,CAAoC,KAAA,CAAS;CAE9D,CAAA,GAAA,MAAA,UAAA,aAAsB,SAAS,SAAS,MAAM,GAAG,CAAC,CAAC;CAEnD,MAAM,OAAA,GAAMC,MAAAA,YAAAA,CAAY,YAAY;EAClC,IAAI,WAAW,SAAS;EACxB,MAAM,EAAE,SAAS,SAAS,UAAA,eAAa,OAAO;EAC9C,IAAI,CAAC,QAAQ,OAAO,KAAK,KAAK,QAAQ,QAAQ,WAAW,GAAG;GAC1D,WAAW,CAAC,CAAC;GACb,aAAa,YAAY,OAAO,CAAC;GACjC;EACF;EAEA,WAAW,UAAU;EACrB,MAAM,aAAa,IAAI,gBAAgB;EACvC,SAAS,UAAU;EACnB,MAAM,WAAW,QAAQ,QAAQ,KAAK,YAAY;GAChD;GACA,SAAS;IAAE,UAAU,QAAQ;IAAU,QAAQ,QAAQ;IAAQ,OAAO,OAAO;GAAM;EACrF,EAA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
28
  * Samples run one after another, never in parallel: a host evaluator may call
29
29
  * functions with side effects or that hand out generated values, and those
30
- * must see the samples in the order the author reads them. `beforeRun` sees the
31
- * whole batch, so a confirmation is asked once rather than per sample.
30
+ * must see the samples in the order the author reads them.
32
31
  */
33
- export declare function useSampleRuns(apiBase: string, payload: SampleRunsPayload, options?: Pick<EvaluatorOptions, "evaluate" | "beforeRun">): SampleRuns;
32
+ export declare function useSampleRuns(apiBase: string, payload: SampleRunsPayload, options?: Pick<EvaluatorOptions, "evaluate">): SampleRuns;
34
33
  //# sourceMappingURL=useSampleRuns.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"useSampleRuns.d.ts","sourceRoot":"","sources":["../../../src/expressions/playground/useSampleRuns.ts"],"names":[],"mappings":"AACA,OAAO,EAAgD,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAClH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,YAAY,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,gBAAgB,EAAE,CAAC;CAC7B;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,gBAAgB,CAAC;IACzB,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED,MAAM,WAAW,UAAU;IACzB,yDAAyD;IACzD,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACjD,uEAAuE;IACvE,KAAK,EAAE,OAAO,CAAC;IACf,GAAG,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED;;;;;;;GAOG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,iBAAiB,EAC1B,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,UAAU,GAAG,WAAW,CAAM,GAC7D,UAAU,CAoEZ"}
1
+ {"version":3,"file":"useSampleRuns.d.ts","sourceRoot":"","sources":["../../../src/expressions/playground/useSampleRuns.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgD,KAAK,YAAY,EAAE,KAAK,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAClH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AACnD,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC;AAE1D,MAAM,WAAW,iBAAiB;IAChC,QAAQ,EAAE,YAAY,CAAC;IACvB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,gBAAgB,EAAE,CAAC;CAC7B;AAED,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,gBAAgB,CAAC;IACzB,QAAQ,EAAE,YAAY,CAAC;CACxB;AAED,MAAM,WAAW,UAAU;IACzB,yDAAyD;IACzD,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,OAAO,EAAE,OAAO,CAAC;IACjB,QAAQ,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,GAAG,IAAI,CAAC;IACjD,uEAAuE;IACvE,KAAK,EAAE,OAAO,CAAC;IACf,GAAG,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,iBAAiB,EAC1B,OAAO,GAAE,IAAI,CAAC,gBAAgB,EAAE,UAAU,CAAM,GAC/C,UAAU,CAoEZ"}
@@ -1,3 +1,4 @@
1
+ import { useLatest } from "../../hooks/use-latest.js";
1
2
  import { evaluate } from "./api.js";
2
3
  import { useCallback, useEffect, useRef, useState } from "react";
3
4
  // src/expressions/playground/useSampleRuns.ts
@@ -6,23 +7,22 @@ import { useCallback, useEffect, useRef, useState } from "react";
6
7
  *
7
8
  * Samples run one after another, never in parallel: a host evaluator may call
8
9
  * functions with side effects or that hand out generated values, and those
9
- * must see the samples in the order the author reads them. `beforeRun` sees the
10
- * whole batch, so a confirmation is asked once rather than per sample.
10
+ * must see the samples in the order the author reads them.
11
11
  */
12
12
  function useSampleRuns(apiBase, payload, options = {}) {
13
13
  const [results, setResults] = useState([]);
14
14
  const [progress, setProgress] = useState(null);
15
15
  const [evaluated, setEvaluated] = useState(null);
16
- const payloadRef = useRef(payload);
17
- payloadRef.current = payload;
18
- const optionsRef = useRef(options);
19
- optionsRef.current = options;
16
+ const latest = useLatest({
17
+ payload,
18
+ evaluate: options.evaluate
19
+ });
20
20
  const runningRef = useRef(false);
21
21
  const abortRef = useRef(void 0);
22
22
  useEffect(() => () => abortRef.current?.abort(), []);
23
23
  const run = useCallback(async () => {
24
24
  if (runningRef.current) return;
25
- const current = payloadRef.current;
25
+ const { payload: current, evaluate: evaluate$1 } = latest.current;
26
26
  if (!current.source.trim() || current.samples.length === 0) {
27
27
  setResults([]);
28
28
  setEvaluated(fingerprint(current));
@@ -32,22 +32,25 @@ function useSampleRuns(apiBase, payload, options = {}) {
32
32
  const controller = new AbortController();
33
33
  abortRef.current = controller;
34
34
  const requests = current.samples.map((sample) => ({
35
- language: current.language,
36
- source: current.source,
37
- input: sample.input
35
+ sample,
36
+ request: {
37
+ language: current.language,
38
+ source: current.source,
39
+ input: sample.input
40
+ }
38
41
  }));
42
+ const total = requests.length;
39
43
  try {
40
- const { beforeRun, evaluate: evaluate$1 } = optionsRef.current;
41
- if (beforeRun && !await beforeRun(requests)) return;
42
44
  const evaluateOne = evaluate$1 ?? ((request, signal) => evaluate(apiBase, request, signal));
43
45
  const collected = [];
44
46
  setResults([]);
45
47
  setEvaluated(fingerprint(current));
46
48
  setProgress({
47
49
  done: 0,
48
- total: requests.length
50
+ total
49
51
  });
50
- for (const [index, request] of requests.entries()) {
52
+ const runSample = async ({ sample, request }, index) => {
53
+ if (controller.signal.aborted) return;
51
54
  let response;
52
55
  try {
53
56
  response = await evaluateOne(request, controller.signal);
@@ -61,20 +64,21 @@ function useSampleRuns(apiBase, payload, options = {}) {
61
64
  }
62
65
  if (controller.signal.aborted) return;
63
66
  collected.push({
64
- sample: current.samples[index],
67
+ sample,
65
68
  response
66
69
  });
67
70
  setResults([...collected]);
68
71
  setProgress({
69
72
  done: index + 1,
70
- total: requests.length
73
+ total
71
74
  });
72
- }
75
+ };
76
+ await requests.reduce((previous, entry, index) => previous.then(() => runSample(entry, index)), Promise.resolve());
73
77
  } finally {
74
78
  runningRef.current = false;
75
79
  if (!controller.signal.aborted) setProgress(null);
76
80
  }
77
- }, [apiBase]);
81
+ }, [apiBase, latest]);
78
82
  return {
79
83
  results,
80
84
  pending: progress !== null,