@warlock.js/ai-panoptic 5.31.0 → 5.33.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.
- package/CHANGELOG.md +18 -0
- package/cjs/index.cjs +72 -67
- package/cjs/index.cjs.map +1 -1
- package/esm/collector/collector.d.mts +3 -5
- package/esm/collector/collector.d.mts.map +1 -1
- package/esm/collector/collector.mjs +2 -0
- package/esm/collector/collector.mjs.map +1 -1
- package/esm/collector/content-capture.type.d.mts +4 -5
- package/esm/collector/content-capture.type.d.mts.map +1 -1
- package/esm/collector/extract-span-attributes.d.mts +1 -3
- package/esm/collector/extract-span-attributes.d.mts.map +1 -1
- package/esm/collector/extract-span-attributes.mjs +30 -6
- package/esm/collector/extract-span-attributes.mjs.map +1 -1
- package/esm/collector/index.d.mts +3 -1
- package/esm/collector/index.mjs +2 -1
- package/esm/collector/normalize-error.d.mts +1 -3
- package/esm/collector/normalize-error.d.mts.map +1 -1
- package/esm/collector/report-to-span.d.mts +1 -3
- package/esm/collector/report-to-span.d.mts.map +1 -1
- package/esm/collector/report-to-trace.d.mts +1 -3
- package/esm/collector/report-to-trace.d.mts.map +1 -1
- package/esm/collector/trace-subscriptions.d.mts +14 -0
- package/esm/collector/trace-subscriptions.d.mts.map +1 -0
- package/esm/collector/trace-subscriptions.mjs +26 -0
- package/esm/collector/trace-subscriptions.mjs.map +1 -0
- package/esm/config/apply-panoptic-config.d.mts +2 -3
- package/esm/config/apply-panoptic-config.d.mts.map +1 -1
- package/esm/config/index.d.mts +2 -1
- package/esm/config/index.mjs +1 -1
- package/esm/config/panoptic-config.type.d.mts +2 -4
- package/esm/config/panoptic-config.type.d.mts.map +1 -1
- package/esm/contracts/collector.contract.d.mts +1 -3
- package/esm/contracts/collector.contract.d.mts.map +1 -1
- package/esm/contracts/exporter.contract.d.mts +1 -3
- package/esm/contracts/exporter.contract.d.mts.map +1 -1
- package/esm/contracts/index.d.mts +2 -1
- package/esm/contracts/trace.type.d.mts +37 -20
- package/esm/contracts/trace.type.d.mts.map +1 -1
- package/esm/dashboard/dashboard.d.mts +1 -3
- package/esm/dashboard/dashboard.d.mts.map +1 -1
- package/esm/dashboard/dashboard.mjs +3 -2
- package/esm/dashboard/dashboard.mjs.map +1 -1
- package/esm/dashboard/dashboard.type.d.mts +9 -7
- package/esm/dashboard/dashboard.type.d.mts.map +1 -1
- package/esm/dashboard/index.d.mts +4 -1
- package/esm/dashboard/index.mjs +1 -1
- package/esm/dashboard/parse-query.d.mts +1 -1
- package/esm/dashboard/serve.d.mts +2 -1
- package/esm/dashboard/serve.mjs +1 -1
- package/esm/dashboard/serve.mjs.map +1 -1
- package/esm/dashboard/trace-filter.d.mts +44 -32
- package/esm/dashboard/trace-filter.d.mts.map +1 -1
- package/esm/evaluate/evaluate-system-prompt.d.mts +1 -3
- package/esm/evaluate/evaluate-system-prompt.d.mts.map +1 -1
- package/esm/evaluate/evaluate-system-prompt.mjs +3 -1
- package/esm/evaluate/evaluate-system-prompt.mjs.map +1 -1
- package/esm/evaluate/evaluate.type.d.mts +2 -4
- package/esm/evaluate/evaluate.type.d.mts.map +1 -1
- package/esm/evaluate/extract-last-system-prompt.d.mts +1 -3
- package/esm/evaluate/extract-last-system-prompt.d.mts.map +1 -1
- package/esm/evaluate/find-span-by-id.d.mts +1 -3
- package/esm/evaluate/find-span-by-id.d.mts.map +1 -1
- package/esm/evaluate/index.d.mts +2 -1
- package/esm/evaluate/index.mjs +1 -1
- package/esm/exporters/console/console-exporter.d.mts +2 -3
- package/esm/exporters/console/console-exporter.d.mts.map +1 -1
- package/esm/exporters/console/console-exporter.type.d.mts +2 -3
- package/esm/exporters/console/console-exporter.type.d.mts.map +1 -1
- package/esm/exporters/console/format-span-io.d.mts +2 -2
- package/esm/exporters/console/format-span-io.d.mts.map +1 -1
- package/esm/exporters/console/format-span-line.d.mts +2 -2
- package/esm/exporters/console/format-span-line.d.mts.map +1 -1
- package/esm/exporters/console/index.d.mts +2 -1
- package/esm/exporters/console/index.mjs +1 -1
- package/esm/exporters/file/file-exporter.d.mts +2 -3
- package/esm/exporters/file/file-exporter.d.mts.map +1 -1
- package/esm/exporters/file/file-exporter.type.d.mts +8 -5
- package/esm/exporters/file/file-exporter.type.d.mts.map +1 -1
- package/esm/exporters/file/index.d.mts +2 -1
- package/esm/exporters/index.d.mts +7 -1
- package/esm/exporters/index.mjs +1 -1
- package/esm/exporters/langfuse/index.d.mts +2 -1
- package/esm/exporters/langfuse/index.mjs +1 -1
- package/esm/exporters/langfuse/langfuse-exporter.d.mts +2 -3
- package/esm/exporters/langfuse/langfuse-exporter.d.mts.map +1 -1
- package/esm/exporters/langfuse/langfuse-exporter.mjs +1 -1
- package/esm/exporters/langfuse/langfuse-exporter.mjs.map +1 -1
- package/esm/exporters/langfuse/langfuse-exporter.type.d.mts +23 -17
- package/esm/exporters/langfuse/langfuse-exporter.type.d.mts.map +1 -1
- package/esm/exporters/otel/index.d.mts +2 -1
- package/esm/exporters/otel/index.mjs +1 -1
- package/esm/exporters/otel/otel-api.shim.type.d.mts +6 -7
- package/esm/exporters/otel/otel-api.shim.type.d.mts.map +1 -1
- package/esm/exporters/otel/otel-exporter.d.mts +2 -3
- package/esm/exporters/otel/otel-exporter.d.mts.map +1 -1
- package/esm/exporters/otel/otel-exporter.type.d.mts +1 -3
- package/esm/exporters/otel/otel-exporter.type.d.mts.map +1 -1
- package/esm/exporters/utils/gen-ai-attributes.d.mts +9 -7
- package/esm/exporters/utils/gen-ai-attributes.d.mts.map +1 -1
- package/esm/exporters/utils/index.d.mts +2 -1
- package/esm/exporters/utils/index.mjs +1 -1
- package/esm/exporters/utils/total-cost.d.mts +1 -3
- package/esm/exporters/utils/total-cost.d.mts.map +1 -1
- package/esm/exporters/utils/walk-spans.d.mts +2 -2
- package/esm/exporters/utils/walk-spans.d.mts.map +1 -1
- package/esm/index.d.mts +10 -1
- package/esm/index.mjs +2 -1
- package/esm/panoptic/index.d.mts +2 -1
- package/esm/panoptic/index.mjs +1 -1
- package/esm/panoptic/panoptic-middleware.d.mts +1 -3
- package/esm/panoptic/panoptic-middleware.d.mts.map +1 -1
- package/esm/panoptic/panoptic-target.type.d.mts +4 -5
- package/esm/panoptic/panoptic-target.type.d.mts.map +1 -1
- package/esm/panoptic/panoptic.d.mts +1 -3
- package/esm/panoptic/panoptic.d.mts.map +1 -1
- package/esm/panoptic/panoptic.type.d.mts +8 -7
- package/esm/panoptic/panoptic.type.d.mts.map +1 -1
- package/esm/store/cache-trace-store.d.mts +4 -6
- package/esm/store/cache-trace-store.d.mts.map +1 -1
- package/esm/store/cache-trace-store.mjs +1 -4
- package/esm/store/cache-trace-store.mjs.map +1 -1
- package/esm/store/in-memory-trace-store.d.mts +2 -4
- package/esm/store/in-memory-trace-store.d.mts.map +1 -1
- package/esm/store/in-memory-trace-store.mjs +1 -4
- package/esm/store/in-memory-trace-store.mjs.map +1 -1
- package/esm/store/index.d.mts +2 -1
- package/esm/store/index.mjs +1 -1
- package/esm/store/match-trace.d.mts +1 -3
- package/esm/store/match-trace.d.mts.map +1 -1
- package/esm/store/match-trace.mjs +1 -0
- package/esm/store/match-trace.mjs.map +1 -1
- package/esm/store/sum-usage.d.mts +4 -30
- package/esm/store/sum-usage.d.mts.map +1 -1
- package/esm/store/sum-usage.mjs +6 -49
- package/esm/store/sum-usage.mjs.map +1 -1
- package/esm/store/trace-aggregate.type.d.mts +12 -9
- package/esm/store/trace-aggregate.type.d.mts.map +1 -1
- package/esm/store/trace-query.type.d.mts +7 -5
- package/esm/store/trace-query.type.d.mts.map +1 -1
- package/esm/store/trace-store.contract.d.mts +1 -3
- package/esm/store/trace-store.contract.d.mts.map +1 -1
- package/llms-full.txt +105 -0
- package/package.json +4 -4
- package/skills/observe-with-panoptic/SKILL.md +35 -0
- package/skills/query-traces/SKILL.md +49 -0
- package/skills/use-local-dashboard/SKILL.md +21 -0
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TraceSpan } from "../../contracts/trace.type.mjs";
|
|
2
|
+
import "../../contracts/index.mjs";
|
|
2
3
|
//#region ../../packages/ai-panoptic/src/exporters/utils/gen-ai-attributes.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Subset of the OpenTelemetry GenAI semantic-convention attribute keys
|
|
@@ -8,15 +9,17 @@ import { TraceSpan } from "../../contracts/trace.type.mjs";
|
|
|
8
9
|
*
|
|
9
10
|
* @see https://opentelemetry.io/docs/specs/semconv/gen-ai/
|
|
10
11
|
*/
|
|
11
|
-
declare const GEN_AI_ATTRIBUTES: {
|
|
12
|
+
export declare const GEN_AI_ATTRIBUTES: {
|
|
12
13
|
readonly operationName: "gen_ai.operation.name";
|
|
13
14
|
readonly system: "gen_ai.system";
|
|
14
15
|
readonly requestModel: "gen_ai.request.model";
|
|
15
16
|
readonly responseModel: "gen_ai.response.model";
|
|
16
17
|
readonly usageInputTokens: "gen_ai.usage.input_tokens";
|
|
17
18
|
readonly usageOutputTokens: "gen_ai.usage.output_tokens";
|
|
18
|
-
readonly conversationId: "gen_ai.conversation.id";
|
|
19
|
-
|
|
19
|
+
readonly conversationId: "gen_ai.conversation.id";
|
|
20
|
+
/** Captured prompt/input (set only under content capture). */
|
|
21
|
+
readonly prompt: "gen_ai.prompt";
|
|
22
|
+
/** Captured completion/output (set only under content capture). */
|
|
20
23
|
readonly completion: "gen_ai.completion";
|
|
21
24
|
};
|
|
22
25
|
/**
|
|
@@ -24,7 +27,7 @@ declare const GEN_AI_ATTRIBUTES: {
|
|
|
24
27
|
* equivalent. Namespaced under `warlock.*` so they never collide with a
|
|
25
28
|
* future `gen_ai.*` key the spec might add.
|
|
26
29
|
*/
|
|
27
|
-
declare const WARLOCK_ATTRIBUTES: {
|
|
30
|
+
export declare const WARLOCK_ATTRIBUTES: {
|
|
28
31
|
readonly reportType: "warlock.report.type";
|
|
29
32
|
readonly version: "warlock.version";
|
|
30
33
|
readonly durationMs: "warlock.duration_ms";
|
|
@@ -38,7 +41,7 @@ declare const WARLOCK_ATTRIBUTES: {
|
|
|
38
41
|
* GenAI attributes are scalars; the framework's free-form
|
|
39
42
|
* `TraceSpan.attributes` may also carry these.
|
|
40
43
|
*/
|
|
41
|
-
type AttributeValue = string | number | boolean;
|
|
44
|
+
export type AttributeValue = string | number | boolean;
|
|
42
45
|
/**
|
|
43
46
|
* Project a {@link TraceSpan} onto the OpenTelemetry GenAI
|
|
44
47
|
* semantic-convention attribute set.
|
|
@@ -61,7 +64,6 @@ type AttributeValue = string | number | boolean;
|
|
|
61
64
|
* const attributes = toGenAiAttributes(span);
|
|
62
65
|
* // { "gen_ai.usage.input_tokens": 150, "gen_ai.usage.output_tokens": 320, ... }
|
|
63
66
|
*/
|
|
64
|
-
declare function toGenAiAttributes(span: TraceSpan): Record<string, AttributeValue>;
|
|
67
|
+
export declare function toGenAiAttributes(span: TraceSpan): Record<string, AttributeValue>;
|
|
65
68
|
//#endregion
|
|
66
|
-
export { AttributeValue, GEN_AI_ATTRIBUTES, WARLOCK_ATTRIBUTES, toGenAiAttributes };
|
|
67
69
|
//# sourceMappingURL=gen-ai-attributes.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"gen-ai-attributes.d.mts","names":[],"sources":["../../../../../../../../../packages/ai-panoptic/src/exporters/utils/gen-ai-attributes.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"gen-ai-attributes.d.mts","names":[],"sources":["../../../../../../../../../packages/ai-panoptic/src/exporters/utils/gen-ai-attributes.ts"],"mappings":";;;;;;;;;;;qBAWa;WACX;WACA;WACA;WACA;WACA;WACA;WACA;;WAEA;;WAEA;;;;;;;qBAQW;WACX;WACA;WACA;WACA;WACA;WACA;WACA;;;;;;;YAQU;;;;;;;;;;;;;;;;;;;;;;;wBAwBI,kBAAkB,MAAM,YAAY,eAAe"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
import { AttributeValue, GEN_AI_ATTRIBUTES, WARLOCK_ATTRIBUTES, toGenAiAttributes } from "./gen-ai-attributes.mjs";
|
|
2
2
|
import { totalCostUsd } from "./total-cost.mjs";
|
|
3
|
-
import { walkSpans } from "./walk-spans.mjs";
|
|
3
|
+
import { walkSpans } from "./walk-spans.mjs";
|
|
4
|
+
export { type AttributeValue, GEN_AI_ATTRIBUTES, WARLOCK_ATTRIBUTES, toGenAiAttributes, totalCostUsd, walkSpans };
|
|
@@ -2,4 +2,4 @@ import { walkSpans } from "./walk-spans.mjs";
|
|
|
2
2
|
import { totalCostUsd } from "./total-cost.mjs";
|
|
3
3
|
import { GEN_AI_ATTRIBUTES, WARLOCK_ATTRIBUTES, toGenAiAttributes } from "./gen-ai-attributes.mjs";
|
|
4
4
|
|
|
5
|
-
export {
|
|
5
|
+
export { GEN_AI_ATTRIBUTES, WARLOCK_ATTRIBUTES, toGenAiAttributes, totalCostUsd, walkSpans };
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Usage } from "@warlock.js/ai";
|
|
2
|
-
|
|
3
2
|
//#region ../../packages/ai-panoptic/src/exporters/utils/total-cost.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Collapse a {@link Usage.cost} breakdown into a single USD scalar by
|
|
@@ -12,7 +11,6 @@ import { Usage } from "@warlock.js/ai";
|
|
|
12
11
|
* totalCostUsd({ input: 1, output: 2, total: 3, cost: { input: 0.01, output: 0.04 } });
|
|
13
12
|
* // => 0.05
|
|
14
13
|
*/
|
|
15
|
-
declare function totalCostUsd(usage: Usage): number | undefined;
|
|
14
|
+
export declare function totalCostUsd(usage: Usage): number | undefined;
|
|
16
15
|
//#endregion
|
|
17
|
-
export { totalCostUsd };
|
|
18
16
|
//# sourceMappingURL=total-cost.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"total-cost.d.mts","names":[],"sources":["../../../../../../../../../packages/ai-panoptic/src/exporters/utils/total-cost.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"total-cost.d.mts","names":[],"sources":["../../../../../../../../../packages/ai-panoptic/src/exporters/utils/total-cost.ts"],"mappings":";;;;;;;;;;;;;wBAagB,aAAa,OAAO"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { TraceSpan } from "../../contracts/trace.type.mjs";
|
|
2
|
+
import "../../contracts/index.mjs";
|
|
2
3
|
//#region ../../packages/ai-panoptic/src/exporters/utils/walk-spans.d.ts
|
|
3
4
|
/**
|
|
4
5
|
* Depth-first pre-order traversal of a {@link TraceSpan} tree, yielding
|
|
@@ -12,7 +13,6 @@ import { TraceSpan } from "../../contracts/trace.type.mjs";
|
|
|
12
13
|
* emit(span);
|
|
13
14
|
* }
|
|
14
15
|
*/
|
|
15
|
-
declare function walkSpans(root: TraceSpan): Generator<TraceSpan>;
|
|
16
|
+
export declare function walkSpans(root: TraceSpan): Generator<TraceSpan>;
|
|
16
17
|
//#endregion
|
|
17
|
-
export { walkSpans };
|
|
18
18
|
//# sourceMappingURL=walk-spans.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"walk-spans.d.mts","names":[],"sources":["../../../../../../../../../packages/ai-panoptic/src/exporters/utils/walk-spans.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"walk-spans.d.mts","names":[],"sources":["../../../../../../../../../packages/ai-panoptic/src/exporters/utils/walk-spans.ts"],"mappings":";;;;;;;;;;;;;;;wBAciB,UAAU,MAAM,YAAY,UAAU"}
|
package/esm/index.d.mts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import { Trace, TraceSpan, TraceSpanError } from "./contracts/trace.type.mjs";
|
|
2
2
|
import { ExporterContract } from "./contracts/exporter.contract.mjs";
|
|
3
3
|
import { CollectorContract } from "./contracts/collector.contract.mjs";
|
|
4
|
+
import "./contracts/index.mjs";
|
|
4
5
|
import { ContentCaptureOptions, ContentRedactor } from "./collector/content-capture.type.mjs";
|
|
5
6
|
import { CollectorOptions, ExporterErrorHandler, createCollector } from "./collector/collector.mjs";
|
|
6
7
|
import { reportToTrace } from "./collector/report-to-trace.mjs";
|
|
7
8
|
import { reportToSpan } from "./collector/report-to-span.mjs";
|
|
8
9
|
import { extractSpanAttributes } from "./collector/extract-span-attributes.mjs";
|
|
9
10
|
import { normalizeError } from "./collector/normalize-error.mjs";
|
|
11
|
+
import { TraceListener, subscribeTraces } from "./collector/trace-subscriptions.mjs";
|
|
12
|
+
import "./collector/index.mjs";
|
|
10
13
|
import { TraceAggregate } from "./store/trace-aggregate.type.mjs";
|
|
11
14
|
import { TraceQuery } from "./store/trace-query.type.mjs";
|
|
12
15
|
import { TraceStoreContract } from "./store/trace-store.contract.mjs";
|
|
@@ -14,6 +17,7 @@ import { CacheDriverInput, CacheTraceStoreHandle, CacheTraceStoreOptions, create
|
|
|
14
17
|
import { InMemoryTraceStoreOptions, createInMemoryTraceStore } from "./store/in-memory-trace-store.mjs";
|
|
15
18
|
import { matchTrace } from "./store/match-trace.mjs";
|
|
16
19
|
import { emptyUsage, sumUsage } from "./store/sum-usage.mjs";
|
|
20
|
+
import "./store/index.mjs";
|
|
17
21
|
import { ConsoleExporterOptions, ConsoleLike } from "./exporters/console/console-exporter.type.mjs";
|
|
18
22
|
import { consoleExporter } from "./exporters/console/console-exporter.mjs";
|
|
19
23
|
import { formatSpanLine } from "./exporters/console/format-span-line.mjs";
|
|
@@ -27,18 +31,23 @@ import { otelExporter } from "./exporters/otel/otel-exporter.mjs";
|
|
|
27
31
|
import { AttributeValue, GEN_AI_ATTRIBUTES, WARLOCK_ATTRIBUTES, toGenAiAttributes } from "./exporters/utils/gen-ai-attributes.mjs";
|
|
28
32
|
import { totalCostUsd } from "./exporters/utils/total-cost.mjs";
|
|
29
33
|
import { walkSpans } from "./exporters/utils/walk-spans.mjs";
|
|
34
|
+
import "./exporters/index.mjs";
|
|
30
35
|
import { CompletedEventPayload, PanopticTarget } from "./panoptic/panoptic-target.type.mjs";
|
|
31
36
|
import { Panoptic, PanopticOptions } from "./panoptic/panoptic.type.mjs";
|
|
32
37
|
import { panoptic } from "./panoptic/panoptic.mjs";
|
|
33
38
|
import { createPanopticMiddleware } from "./panoptic/panoptic-middleware.mjs";
|
|
39
|
+
import "./panoptic/index.mjs";
|
|
34
40
|
import { EvaluateConfig, EvaluateVerdict } from "./evaluate/evaluate.type.mjs";
|
|
35
41
|
import { evaluateSystemPrompt } from "./evaluate/evaluate-system-prompt.mjs";
|
|
36
42
|
import { extractLastSystemPrompt } from "./evaluate/extract-last-system-prompt.mjs";
|
|
37
43
|
import { findSpanById } from "./evaluate/find-span-by-id.mjs";
|
|
44
|
+
import "./evaluate/index.mjs";
|
|
38
45
|
import { DashboardHandle, DashboardOptions } from "./dashboard/dashboard.type.mjs";
|
|
39
46
|
import { dashboard } from "./dashboard/dashboard.mjs";
|
|
40
47
|
import { NO_PROMPT_KEY, NO_SESSION_KEY, NO_TYPE_KEY, PromptGroup, SessionGroup, TraceFilter, TypeGroup, TypeStat, aggregateByType, filterTraces, groupByPrompt, groupBySession, groupByType, heatIntensity, matchesFilter, maxNodeCost, percentile, rollupCost, traceCost, tracePromptKey } from "./dashboard/trace-filter.mjs";
|
|
48
|
+
import "./dashboard/index.mjs";
|
|
41
49
|
import { PanopticConfig } from "./config/panoptic-config.type.mjs";
|
|
42
50
|
import { applyPanopticConfig } from "./config/apply-panoptic-config.mjs";
|
|
43
|
-
|
|
51
|
+
import "./config/index.mjs";
|
|
52
|
+
export { type AttributeValue, type CacheDriverInput, type CacheTraceStoreHandle, type CacheTraceStoreOptions, type CollectorContract, type CollectorOptions, type CompletedEventPayload, type ConsoleExporterOptions, type ConsoleLike, type ContentCaptureOptions, type ContentRedactor, type DashboardHandle, type DashboardOptions, type EvaluateConfig, type EvaluateVerdict, type ExporterContract, type ExporterErrorHandler, type FileExporterOptions, GEN_AI_ATTRIBUTES, type InMemoryTraceStoreOptions, type LangfuseClientLike, type LangfuseExporterOptions, type LangfuseObservationBody, type LangfuseObservationEndBody, type LangfuseObservationLevel, type LangfuseObservationLike, type LangfuseTraceBody, type LangfuseTraceLike, type LangfuseUsageBody, NO_PROMPT_KEY, NO_SESSION_KEY, NO_TYPE_KEY, type OtelExporterOptions, type Panoptic, type PanopticConfig, type PanopticOptions, type PanopticTarget, type PromptGroup, type SessionGroup, type Trace, type TraceAggregate, type TraceFilter, type TraceListener, type TraceQuery, type TraceRecord, type TraceSpan, type TraceSpanError, type TraceStoreContract, type TypeGroup, type TypeStat, WARLOCK_ATTRIBUTES, aggregateByType, applyPanopticConfig, consoleExporter, createCacheTraceStore, createCollector, createInMemoryTraceStore, createPanopticMiddleware, dashboard, emptyUsage, evaluateSystemPrompt, extractLastSystemPrompt, extractSpanAttributes, fileExporter, filterTraces, findSpanById, formatSpanIO, formatSpanLine, groupByPrompt, groupBySession, groupByType, heatIntensity, langfuseExporter, matchTrace, matchesFilter, maxNodeCost, normalizeError, otelExporter, panoptic, percentile, reportToSpan, reportToTrace, rollupCost, subscribeTraces, sumUsage, toGenAiAttributes, totalCostUsd, traceCost, tracePromptKey, walkSpans };
|
|
44
53
|
import "./config/panoptic-config.type.mjs";
|
package/esm/index.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { normalizeError } from "./collector/normalize-error.mjs";
|
|
|
3
3
|
import { extractSpanAttributes } from "./collector/extract-span-attributes.mjs";
|
|
4
4
|
import { reportToSpan } from "./collector/report-to-span.mjs";
|
|
5
5
|
import { reportToTrace } from "./collector/report-to-trace.mjs";
|
|
6
|
+
import { subscribeTraces } from "./collector/trace-subscriptions.mjs";
|
|
6
7
|
import { createCollector } from "./collector/collector.mjs";
|
|
7
8
|
import "./collector/index.mjs";
|
|
8
9
|
import { matchTrace } from "./store/match-trace.mjs";
|
|
@@ -33,4 +34,4 @@ import { applyPanopticConfig } from "./config/apply-panoptic-config.mjs";
|
|
|
33
34
|
import "./config/index.mjs";
|
|
34
35
|
import "./register.mjs";
|
|
35
36
|
|
|
36
|
-
export { GEN_AI_ATTRIBUTES, NO_PROMPT_KEY, NO_SESSION_KEY, NO_TYPE_KEY, WARLOCK_ATTRIBUTES, aggregateByType, applyPanopticConfig, consoleExporter, createCacheTraceStore, createCollector, createInMemoryTraceStore, createPanopticMiddleware, dashboard, emptyUsage, evaluateSystemPrompt, extractLastSystemPrompt, extractSpanAttributes, fileExporter, filterTraces, findSpanById, formatSpanIO, formatSpanLine, groupByPrompt, groupBySession, groupByType, heatIntensity, langfuseExporter, matchTrace, matchesFilter, maxNodeCost, normalizeError, otelExporter, panoptic, percentile, reportToSpan, reportToTrace, rollupCost, sumUsage, toGenAiAttributes, totalCostUsd, traceCost, tracePromptKey, walkSpans };
|
|
37
|
+
export { GEN_AI_ATTRIBUTES, NO_PROMPT_KEY, NO_SESSION_KEY, NO_TYPE_KEY, WARLOCK_ATTRIBUTES, aggregateByType, applyPanopticConfig, consoleExporter, createCacheTraceStore, createCollector, createInMemoryTraceStore, createPanopticMiddleware, dashboard, emptyUsage, evaluateSystemPrompt, extractLastSystemPrompt, extractSpanAttributes, fileExporter, filterTraces, findSpanById, formatSpanIO, formatSpanLine, groupByPrompt, groupBySession, groupByType, heatIntensity, langfuseExporter, matchTrace, matchesFilter, maxNodeCost, normalizeError, otelExporter, panoptic, percentile, reportToSpan, reportToTrace, rollupCost, subscribeTraces, sumUsage, toGenAiAttributes, totalCostUsd, traceCost, tracePromptKey, walkSpans };
|
package/esm/panoptic/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { CompletedEventPayload, PanopticTarget } from "./panoptic-target.type.mjs";
|
|
2
2
|
import { Panoptic, PanopticOptions } from "./panoptic.type.mjs";
|
|
3
3
|
import { panoptic } from "./panoptic.mjs";
|
|
4
|
-
import { createPanopticMiddleware } from "./panoptic-middleware.mjs";
|
|
4
|
+
import { createPanopticMiddleware } from "./panoptic-middleware.mjs";
|
|
5
|
+
export { type CompletedEventPayload, type Panoptic, type PanopticOptions, type PanopticTarget, createPanopticMiddleware, panoptic };
|
package/esm/panoptic/index.mjs
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { CollectorContract } from "../contracts/collector.contract.mjs";
|
|
2
2
|
import { AgentMiddleware } from "@warlock.js/ai";
|
|
3
|
-
|
|
4
3
|
//#region ../../packages/ai-panoptic/src/panoptic/panoptic-middleware.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Build an {@link AgentMiddleware} that feeds a collector from the
|
|
@@ -32,7 +31,6 @@ import { AgentMiddleware } from "@warlock.js/ai";
|
|
|
32
31
|
* @param name - stable middleware name (kebab-case). Defaults to
|
|
33
32
|
* `"panoptic"`.
|
|
34
33
|
*/
|
|
35
|
-
declare function createPanopticMiddleware(collector: CollectorContract, name?: string): AgentMiddleware;
|
|
34
|
+
export declare function createPanopticMiddleware(collector: CollectorContract, name?: string): AgentMiddleware;
|
|
36
35
|
//#endregion
|
|
37
|
-
export { createPanopticMiddleware };
|
|
38
36
|
//# sourceMappingURL=panoptic-middleware.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"panoptic-middleware.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic-middleware.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"panoptic-middleware.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic-middleware.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBAiCgB,yBACd,WAAW,mBACX,gBACC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { BaseReport, BaseResult } from "@warlock.js/ai";
|
|
2
|
-
|
|
3
2
|
//#region ../../packages/ai-panoptic/src/panoptic/panoptic-target.type.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* The minimal completed-event payload Panoptic reads off any primitive's
|
|
@@ -10,8 +9,9 @@ import { BaseReport, BaseResult } from "@warlock.js/ai";
|
|
|
10
9
|
* `*.completed` still fires). Reading the report off this single event is
|
|
11
10
|
* therefore enough to capture every run regardless of outcome.
|
|
12
11
|
*/
|
|
13
|
-
type CompletedEventPayload = {
|
|
14
|
-
/** The finalized result of the run; carries the full `report` tree. */
|
|
12
|
+
export type CompletedEventPayload = {
|
|
13
|
+
/** The finalized result of the run; carries the full `report` tree. */
|
|
14
|
+
result: BaseResult & {
|
|
15
15
|
report: BaseReport;
|
|
16
16
|
};
|
|
17
17
|
};
|
|
@@ -34,7 +34,7 @@ type CompletedEventPayload = {
|
|
|
34
34
|
* const sub = panoptic({ exporters: [consoleExporter()] });
|
|
35
35
|
* const detach = sub.attach(agent); // agent is a PanopticTarget
|
|
36
36
|
*/
|
|
37
|
-
type PanopticTarget = {
|
|
37
|
+
export type PanopticTarget = {
|
|
38
38
|
/**
|
|
39
39
|
* Stable identifier of the primitive. Used only for diagnostics
|
|
40
40
|
* (Panoptic never branches behavior on it). Present on every core
|
|
@@ -49,5 +49,4 @@ type PanopticTarget = {
|
|
|
49
49
|
on(event: string, handler: (payload: unknown) => void): () => void;
|
|
50
50
|
};
|
|
51
51
|
//#endregion
|
|
52
|
-
export { CompletedEventPayload, PanopticTarget };
|
|
53
52
|
//# sourceMappingURL=panoptic-target.type.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"panoptic-target.type.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic-target.type.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"panoptic-target.type.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic-target.type.ts"],"mappings":";;;;;;;;;;;YAWY;;EAEV,QAAQ;IAAe,QAAQ;;;;;;;;;;;;;;;;;;;;;;YAsBrB;;;;;;WAMD;;;;;;EAMT,GAAG,eAAe,UAAU"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Panoptic, PanopticOptions } from "./panoptic.type.mjs";
|
|
2
|
-
|
|
3
2
|
//#region ../../packages/ai-panoptic/src/panoptic/panoptic.d.ts
|
|
4
3
|
/**
|
|
5
4
|
* Create a Panoptic subscriber — the one-call entry point that wires the
|
|
@@ -31,7 +30,6 @@ import { Panoptic, PanopticOptions } from "./panoptic.type.mjs";
|
|
|
31
30
|
* const result = await orchestrator.execute(input, { sessionId });
|
|
32
31
|
* await observe.collect(result.report);
|
|
33
32
|
*/
|
|
34
|
-
declare function panoptic(options?: PanopticOptions): Panoptic;
|
|
33
|
+
export declare function panoptic(options?: PanopticOptions): Panoptic;
|
|
35
34
|
//#endregion
|
|
36
|
-
export { panoptic };
|
|
37
35
|
//# sourceMappingURL=panoptic.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"panoptic.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"panoptic.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA8LgB,SAAS,UAAS,kBAAuB"}
|
|
@@ -5,7 +5,6 @@ import { ContentRedactor } from "../collector/content-capture.type.mjs";
|
|
|
5
5
|
import { ExporterErrorHandler } from "../collector/collector.mjs";
|
|
6
6
|
import { PanopticTarget } from "./panoptic-target.type.mjs";
|
|
7
7
|
import { AgentMiddleware, BaseReport } from "@warlock.js/ai";
|
|
8
|
-
|
|
9
8
|
//#region ../../packages/ai-panoptic/src/panoptic/panoptic.type.d.ts
|
|
10
9
|
/**
|
|
11
10
|
* Options for the {@link panoptic} subscriber factory.
|
|
@@ -15,7 +14,7 @@ import { AgentMiddleware, BaseReport } from "@warlock.js/ai";
|
|
|
15
14
|
* a pre-built collector (e.g. one that already has a store registered);
|
|
16
15
|
* any `exporters` supplied are registered onto it as well.
|
|
17
16
|
*/
|
|
18
|
-
type PanopticOptions = {
|
|
17
|
+
export type PanopticOptions = {
|
|
19
18
|
/**
|
|
20
19
|
* Exporters to register on the collector. Each is deduped by
|
|
21
20
|
* `ExporterContract.name` (registering the same exporter twice is a
|
|
@@ -92,8 +91,9 @@ type PanopticOptions = {
|
|
|
92
91
|
* All three converge on the same collector, so a trace flows to every
|
|
93
92
|
* registered exporter exactly once per run.
|
|
94
93
|
*/
|
|
95
|
-
type Panoptic = {
|
|
96
|
-
/** The underlying collector — exposed for `use()`, `toTrace()`, etc. */
|
|
94
|
+
export type Panoptic = {
|
|
95
|
+
/** The underlying collector — exposed for `use()`, `toTrace()`, etc. */
|
|
96
|
+
readonly collector: CollectorContract;
|
|
97
97
|
/**
|
|
98
98
|
* Register another exporter on the collector. Returns `this` for
|
|
99
99
|
* chaining; mirrors `CollectorContract.use`.
|
|
@@ -123,10 +123,11 @@ type Panoptic = {
|
|
|
123
123
|
* Pure projection of a report into a {@link Trace} without dispatching
|
|
124
124
|
* — passthrough to `CollectorContract.toTrace`.
|
|
125
125
|
*/
|
|
126
|
-
toTrace(report: BaseReport): Trace;
|
|
127
|
-
|
|
126
|
+
toTrace(report: BaseReport): Trace;
|
|
127
|
+
/** Flush every registered exporter that supports it. */
|
|
128
|
+
flush(): Promise<void>;
|
|
129
|
+
/** Flush and shut down every registered exporter. */
|
|
128
130
|
shutdown(): Promise<void>;
|
|
129
131
|
};
|
|
130
132
|
//#endregion
|
|
131
|
-
export { Panoptic, PanopticOptions };
|
|
132
133
|
//# sourceMappingURL=panoptic.type.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"panoptic.type.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic.type.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"panoptic.type.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/panoptic/panoptic.type.ts"],"mappings":";;;;;;;;;;;;;;;;YAgBY;;;;;;EAMV,YAAY;;;;;;;EAOZ,YAAY;;;;;;;;;EASZ;;;;;;EAMA;;;;;;;;;;EAUA;;;;;;EAMA,gBAAgB;;;;;;EAMhB;;;;;;;;EAQA,UAAU;;;;;;;;;;;;;;;;;;;YAoBA;;WAED,WAAW;;;;;EAKpB,IAAI,UAAU,mBAAmB;;;;;;;EAOjC,OAAO,QAAQ;;;;;;;EAOf,cAAc;;;;;;EAMd,QAAQ,QAAQ,aAAa;;;;;EAK7B,QAAQ,QAAQ,aAAa;;EAE7B,SAAS;;EAET,YAAY"}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ExporterContract } from "../contracts/exporter.contract.mjs";
|
|
2
2
|
import { TraceStoreContract } from "./trace-store.contract.mjs";
|
|
3
3
|
import { CacheDriver } from "@warlock.js/cache";
|
|
4
|
-
|
|
5
4
|
//#region ../../packages/ai-panoptic/src/store/cache-trace-store.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* A {@link CacheDriver} instance, or a (possibly async) factory that
|
|
@@ -14,11 +13,11 @@ import { CacheDriver } from "@warlock.js/cache";
|
|
|
14
13
|
* driver's `get` / `set` / `remove` surface and is agnostic to the
|
|
15
14
|
* concrete client + options of whichever driver backs it.
|
|
16
15
|
*/
|
|
17
|
-
type CacheDriverInput = CacheDriver<any, any> | (() => CacheDriver<any, any> | Promise<CacheDriver<any, any>>);
|
|
16
|
+
export type CacheDriverInput = CacheDriver<any, any> | (() => CacheDriver<any, any> | Promise<CacheDriver<any, any>>);
|
|
18
17
|
/**
|
|
19
18
|
* Options for {@link createCacheTraceStore}.
|
|
20
19
|
*/
|
|
21
|
-
type CacheTraceStoreOptions = {
|
|
20
|
+
export type CacheTraceStoreOptions = {
|
|
22
21
|
/**
|
|
23
22
|
* Key prefix every cache entry this store writes is namespaced under.
|
|
24
23
|
* Per-trace keys are `${prefix}:trace:${traceId}`; the newest-first
|
|
@@ -38,7 +37,7 @@ type CacheTraceStoreOptions = {
|
|
|
38
37
|
* plus a `ready()` to hydrate the in-memory mirror from the cache on
|
|
39
38
|
* startup so traces survive a process restart.
|
|
40
39
|
*/
|
|
41
|
-
type CacheTraceStoreHandle = TraceStoreContract & ExporterContract & {
|
|
40
|
+
export type CacheTraceStoreHandle = TraceStoreContract & ExporterContract & {
|
|
42
41
|
/**
|
|
43
42
|
* Hydrate the in-memory mirror from the cache. Await once at startup
|
|
44
43
|
* (the dashboard wiring does this for you) so previously-persisted
|
|
@@ -73,9 +72,8 @@ type CacheTraceStoreHandle = TraceStoreContract & ExporterContract & {
|
|
|
73
72
|
* collector.use(store); // fills as traces complete
|
|
74
73
|
* const failed = store.query({ status: "failed" });
|
|
75
74
|
*/
|
|
76
|
-
declare function createCacheTraceStore(cache: CacheDriverInput, options?: CacheTraceStoreOptions & {
|
|
75
|
+
export declare function createCacheTraceStore(cache: CacheDriverInput, options?: CacheTraceStoreOptions & {
|
|
77
76
|
onError?: (error: unknown) => void;
|
|
78
77
|
}): CacheTraceStoreHandle;
|
|
79
78
|
//#endregion
|
|
80
|
-
export { CacheDriverInput, CacheTraceStoreHandle, CacheTraceStoreOptions, createCacheTraceStore };
|
|
81
79
|
//# sourceMappingURL=cache-trace-store.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-trace-store.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/cache-trace-store.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"cache-trace-store.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/cache-trace-store.ts"],"mappings":";;;;;;;;;;;;;;;YAoBY,mBAIR,+BAEO,wBAAwB,QAAQ;;;;YAK/B;;;;;;EAMV;;;;;;EAMA;;;;;;;;YAwYU,wBAAwB,qBAClC;;;;;;EAME,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;wBA8BG,sBACd,OAAO,kBACP,UAAS;EAA2B,WAAW;IAC9C"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache-trace-store.mjs","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/cache-trace-store.ts"],"sourcesContent":["import type { CacheDriver } from \"@warlock.js/cache\";\nimport type { ExporterContract } from \"../contracts/exporter.contract\";\nimport type { Trace } from \"../contracts/trace.type\";\nimport { matchTrace } from \"./match-trace\";\nimport { emptyUsage, sumUsage } from \"./sum-usage\";\nimport type { TraceAggregate } from \"./trace-aggregate.type\";\nimport type { TraceQuery } from \"./trace-query.type\";\nimport type { TraceStoreContract } from \"./trace-store.contract\";\n\n/**\n * A {@link CacheDriver} instance, or a (possibly async) factory that\n * yields one on first use. The factory form lets production defer an\n * expensive connect (e.g. the Redis handshake) until the first trace is\n * actually written, and keeps the dashboard wiring free of a live driver\n * at module-import time.\n *\n * Typed `CacheDriver<any, any>` because the store only ever touches the\n * driver's `get` / `set` / `remove` surface and is agnostic to the\n * concrete client + options of whichever driver backs it.\n */\nexport type CacheDriverInput =\n // The store is driver-agnostic; it only uses get/set/remove, so the\n // concrete client/options generics are intentionally unconstrained.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | CacheDriver<any, any>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | (() => CacheDriver<any, any> | Promise<CacheDriver<any, any>>);\n\n/**\n * Options for {@link createCacheTraceStore}.\n */\nexport type CacheTraceStoreOptions = {\n /**\n * Key prefix every cache entry this store writes is namespaced under.\n * Per-trace keys are `${prefix}:trace:${traceId}`; the newest-first\n * index lives at `${prefix}:index`. Default `\"panoptic\"`.\n */\n prefix?: string;\n /**\n * Maximum number of traces to retain. When set and exceeded, the\n * oldest-ingested trace is evicted from both the cache and the in-memory\n * mirror (insertion-order FIFO). Absent / `0` = unbounded.\n */\n capacity?: number;\n};\n\n/** One entry in the persisted newest-first index. */\ntype IndexEntry = {\n /** The trace's `traceId` — the suffix of its `${prefix}:trace:` key. */\n id: string;\n /**\n * Monotonic insertion order from an internal counter — NOT a wall clock.\n * Used only to keep the index in stable insertion order across a restart\n * so FIFO eviction stays honest.\n */\n addedAt: number;\n};\n\nconst DEFAULT_PREFIX = \"panoptic\";\n\n/**\n * Cache-backed {@link TraceStoreContract} with a **write-through** design\n * that reconciles the synchronous store contract with an asynchronous\n * cache driver.\n *\n * **How the sync/async tension is resolved.** The contract's `get` /\n * `query` / `aggregate` / `size` are synchronous (the dashboard polls them\n * on every request and the in-memory store answers instantly). A cache\n * driver is async. So this store keeps an **in-memory read mirror** — the\n * same insertion-ordered `Map<traceId, Trace>` the in-memory store uses —\n * and serves every read from it synchronously. Writes go **through** to the\n * cache: `add` updates the mirror immediately, then asynchronously persists\n * the trace + index to the cache (errors are swallowed via an optional\n * `onError` hook so a flaky cache never throws into the collector's hot\n * path). On process restart, {@link CacheTraceStore.ready} re-hydrates the\n * mirror from the cache so traces survive the restart.\n *\n * **Durability is best-effort.** Reads never wait on the cache; the mirror\n * is the source of truth at runtime and the cache is the durable backing\n * store. A write that the cache rejects is still visible in the mirror for\n * the life of the process — it just won't survive a restart.\n *\n * **Lazy driver resolution.** The driver (or its async factory) is resolved\n * on first use and memoized, so a production deployment can defer the Redis\n * connect until the first trace is collected, and the dashboard can be\n * wired with a factory at import time without a live connection.\n *\n * Doubles as an {@link ExporterContract} (`export` ≡ `add`), so it drops\n * straight into a collector via `collector.use(store)`.\n *\n * Instantiated via {@link createCacheTraceStore}; callers never see `new`.\n */\nclass CacheTraceStore implements TraceStoreContract, ExporterContract {\n /** Stable exporter id so a collector can dedupe / log this sink. */\n public readonly name = \"cache-trace-store\";\n\n /**\n * In-memory read mirror keyed by `traceId`. A `Map` preserves insertion\n * order, which newest-first `query` ordering and FIFO eviction both rely\n * on. Every read is served from here synchronously.\n */\n private readonly mirror = new Map<string, Trace>();\n\n private readonly prefix: string;\n\n private readonly capacity: number;\n\n /**\n * Monotonic insertion counter — the source of `IndexEntry.addedAt`.\n * Deliberately NOT `Date.now()`: an internal counter guarantees a stable\n * total order for the index even when many traces land in the same\n * millisecond.\n */\n private addCounter = 0;\n\n /** The optional input — a driver, a factory, or `undefined`. */\n private readonly input: CacheDriverInput;\n\n // The store is driver-agnostic; only get/set/remove are used.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private resolvedDriver?: CacheDriver<any, any>;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private driverPending?: Promise<CacheDriver<any, any>>;\n\n private readonly onError?: (error: unknown) => void;\n\n public constructor(\n input: CacheDriverInput,\n options: CacheTraceStoreOptions & { onError?: (error: unknown) => void } = {},\n ) {\n this.input = input;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.capacity = options.capacity ?? 0;\n this.onError = options.onError;\n }\n\n public get size(): number {\n return this.mirror.size;\n }\n\n /**\n * Hydrate the in-memory mirror from the cache. Idempotent-safe to call\n * once at startup (the dashboard / config wiring awaits it). Reads the\n * persisted index, fetches each referenced trace, and replays them into\n * the mirror in insertion order so newest-first ordering + eviction stay\n * correct after a restart. A cache failure is routed to `onError` and\n * leaves the mirror empty rather than throwing.\n */\n public async ready(): Promise<void> {\n try {\n const driver = await this.resolveDriver();\n const index = await driver.get<IndexEntry[]>(this.indexKey());\n\n if (!Array.isArray(index)) {\n return;\n }\n\n // Oldest-first replay so the mirror's insertion order matches the\n // original ingestion order.\n const ordered = [...index].sort((left, right) => left.addedAt - right.addedAt);\n\n for (const entry of ordered) {\n const trace = await driver.get<Trace>(this.traceKey(entry.id));\n\n if (trace !== null && trace !== undefined) {\n this.mirror.delete(trace.traceId);\n this.mirror.set(trace.traceId, trace);\n }\n\n if (entry.addedAt >= this.addCounter) {\n this.addCounter = entry.addedAt + 1;\n }\n }\n\n this.evictOverflow();\n } catch (error) {\n this.reportError(error);\n }\n }\n\n public add(trace: Trace): void {\n // Mirror update is synchronous and authoritative for runtime reads.\n this.mirror.delete(trace.traceId);\n this.mirror.set(trace.traceId, trace);\n\n const addedAt = this.addCounter;\n this.addCounter += 1;\n\n const evicted = this.evictOverflow();\n\n // Write through to the cache fire-and-forget; reads never wait on this.\n void this.persist(trace, addedAt, evicted);\n }\n\n /**\n * `ExporterContract.export` — a collector dispatches a completed trace\n * here, which is exactly an `add`.\n */\n public export(trace: Trace): void {\n this.add(trace);\n }\n\n public get(traceId: string): Trace | undefined {\n return this.mirror.get(traceId);\n }\n\n public query(filter?: TraceQuery): Trace[] {\n const matched: Trace[] = [];\n\n for (const trace of this.mirror.values()) {\n if (matchTrace(trace, filter)) {\n matched.push(trace);\n }\n }\n\n return this.sortNewestFirst(matched);\n }\n\n public aggregate(filter?: TraceQuery): TraceAggregate {\n const aggregate: TraceAggregate = {\n traces: 0,\n completed: 0,\n failed: 0,\n cancelled: 0,\n usage: emptyUsage(),\n totalDuration: 0,\n };\n\n for (const trace of this.mirror.values()) {\n if (!matchTrace(trace, filter)) {\n continue;\n }\n\n aggregate.traces += 1;\n aggregate.totalDuration += trace.duration;\n aggregate.usage = sumUsage(aggregate.usage, trace.usage);\n\n this.countStatus(aggregate, trace);\n }\n\n if (aggregate.usage.cost !== undefined) {\n aggregate.cost = aggregate.usage.cost;\n }\n\n return aggregate;\n }\n\n public clear(): void {\n const ids = [...this.mirror.keys()];\n this.mirror.clear();\n\n void this.purge(ids);\n }\n\n /**\n * Persist one trace + the rebuilt index to the cache, optionally removing\n * a trace evicted by the capacity cap. Best-effort: any cache failure is\n * routed to `onError`, never thrown — the mirror already reflects the\n * write so runtime reads are unaffected.\n */\n private async persist(\n trace: Trace,\n addedAt: number,\n evictedId: string | undefined,\n ): Promise<void> {\n try {\n const driver = await this.resolveDriver();\n\n await driver.set(this.traceKey(trace.traceId), trace);\n\n if (evictedId !== undefined) {\n await driver.remove(this.traceKey(evictedId));\n }\n\n await driver.set(this.indexKey(), this.buildIndex(addedAt));\n } catch (error) {\n this.reportError(error);\n }\n }\n\n /** Best-effort removal of every persisted trace + the index on `clear`. */\n private async purge(ids: string[]): Promise<void> {\n try {\n const driver = await this.resolveDriver();\n\n for (const id of ids) {\n await driver.remove(this.traceKey(id));\n }\n\n await driver.remove(this.indexKey());\n } catch (error) {\n this.reportError(error);\n }\n }\n\n /**\n * Rebuild the newest-first index from the current mirror. The mirror's\n * `Map` iteration is oldest-first insertion order; we walk it and assign\n * `addedAt` from the surviving counter span so the persisted order\n * matches the in-memory one. The freshest entry uses `latestAddedAt`.\n */\n private buildIndex(latestAddedAt: number): IndexEntry[] {\n const ids = [...this.mirror.keys()];\n const base = latestAddedAt - (ids.length - 1);\n\n return ids.map((id, offset) => ({ id, addedAt: base + offset }));\n }\n\n /**\n * Resolve the driver once and memoize. Supports a bare driver, a sync\n * factory, and an async factory. Concurrent first-callers share one\n * in-flight resolution.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private async resolveDriver(): Promise<CacheDriver<any, any>> {\n if (this.resolvedDriver !== undefined) {\n return this.resolvedDriver;\n }\n\n if (this.driverPending !== undefined) {\n return this.driverPending;\n }\n\n const candidate =\n typeof this.input === \"function\"\n ? (\n this.input as () =>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | CacheDriver<any, any>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Promise<CacheDriver<any, any>>\n )()\n : this.input;\n\n this.driverPending = Promise.resolve(candidate);\n\n try {\n this.resolvedDriver = await this.driverPending;\n\n return this.resolvedDriver;\n } finally {\n this.driverPending = undefined;\n }\n }\n\n /** `${prefix}:trace:${traceId}` — the per-trace cache key. */\n private traceKey(traceId: string): string {\n return `${this.prefix}:trace:${traceId}`;\n }\n\n /** `${prefix}:index` — the newest-first index cache key. */\n private indexKey(): string {\n return `${this.prefix}:index`;\n }\n\n /** Route a swallowed cache error to the optional handler. */\n private reportError(error: unknown): void {\n if (this.onError !== undefined) {\n this.onError(error);\n }\n }\n\n /**\n * Increment the matching terminal-status counter for one trace.\n * Mirrors the in-memory store: non-terminal statuses are counted in\n * `traces` but tracked by none of the three headline counters.\n */\n private countStatus(aggregate: TraceAggregate, trace: Trace): void {\n switch (trace.root.status) {\n case \"completed\": {\n aggregate.completed += 1;\n break;\n }\n\n // An incomplete agent run (its answer was cut off) errored.\n case \"failed\":\n case \"incomplete\": {\n aggregate.failed += 1;\n break;\n }\n\n case \"cancelled\": {\n aggregate.cancelled += 1;\n break;\n }\n\n default: {\n break;\n }\n }\n }\n\n /**\n * Sort matched traces newest-started first. A copy is sorted so the\n * mirror's insertion order (which eviction depends on) is never disturbed.\n */\n private sortNewestFirst(traces: Trace[]): Trace[] {\n return traces.sort((left, right) => Date.parse(right.startedAt) - Date.parse(left.startedAt));\n }\n\n /**\n * Evict oldest-inserted traces from the mirror until within `capacity`.\n * Returns the id of the single evicted trace (the common case — one `add`\n * pushes at most one over the cap) so the caller can remove it from the\n * cache too. No-op + `undefined` when unbounded or within the cap.\n */\n private evictOverflow(): string | undefined {\n if (this.capacity <= 0) {\n return undefined;\n }\n\n let evicted: string | undefined;\n\n while (this.mirror.size > this.capacity) {\n const oldest = this.mirror.keys().next().value;\n\n if (oldest === undefined) {\n return evicted;\n }\n\n this.mirror.delete(oldest);\n evicted = oldest;\n }\n\n return evicted;\n }\n}\n\n/**\n * The concrete store type returned by {@link createCacheTraceStore} — the\n * standard {@link TraceStoreContract} + {@link ExporterContract} surface,\n * plus a `ready()` to hydrate the in-memory mirror from the cache on\n * startup so traces survive a process restart.\n */\nexport type CacheTraceStoreHandle = TraceStoreContract &\n ExporterContract & {\n /**\n * Hydrate the in-memory mirror from the cache. Await once at startup\n * (the dashboard wiring does this for you) so previously-persisted\n * traces are queryable after a restart.\n */\n ready(): Promise<void>;\n };\n\n/**\n * Create a cache-backed trace store. Reads are served synchronously from an\n * in-memory mirror; writes go through to the cache, and {@link\n * CacheTraceStoreHandle.ready} re-hydrates the mirror on startup so traces\n * survive a restart. See {@link CacheTraceStore} for the full write-through\n * design.\n *\n * @param cache a {@link CacheDriver}, or a (possibly async) factory that\n * yields one on first use — resolved lazily and memoized so a production\n * Redis connect can be deferred until the first trace is collected.\n * @param options `prefix` (default `\"panoptic\"`), `capacity` (FIFO cap),\n * and an optional `onError` hook for swallowed cache write failures.\n *\n * @example\n * import { RedisCacheDriver } from \"@warlock.js/cache\";\n *\n * // Lazy async factory — defers the Redis connect until first use.\n * const store = createCacheTraceStore(async () => {\n * const driver = new RedisCacheDriver();\n * await driver.connect();\n * return driver;\n * });\n *\n * await store.ready(); // hydrate from a prior run\n * collector.use(store); // fills as traces complete\n * const failed = store.query({ status: \"failed\" });\n */\nexport function createCacheTraceStore(\n cache: CacheDriverInput,\n options: CacheTraceStoreOptions & { onError?: (error: unknown) => void } = {},\n): CacheTraceStoreHandle {\n return new CacheTraceStore(cache, options);\n}\n"],"mappings":";;;;AA0DA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCvB,IAAM,kBAAN,MAAsE;;CAEpE,AAAgB,OAAO;;;;;;CAOvB,AAAiB,yBAAS,IAAI,IAAmB;CAEjD,AAAiB;CAEjB,AAAiB;;;;;;;CAQjB,AAAQ,aAAa;;CAGrB,AAAiB;CAIjB,AAAQ;CAGR,AAAQ;CAER,AAAiB;CAEjB,AAAO,YACL,OACA,UAA2E,CAAC,GAC5E;EACA,KAAK,QAAQ;EACb,KAAK,SAAS,QAAQ,UAAU;EAChC,KAAK,WAAW,QAAQ,YAAY;EACpC,KAAK,UAAU,QAAQ;CACzB;CAEA,IAAW,OAAe;EACxB,OAAO,KAAK,OAAO;CACrB;;;;;;;;;CAUA,MAAa,QAAuB;EAClC,IAAI;GACF,MAAM,SAAS,MAAM,KAAK,cAAc;GACxC,MAAM,QAAQ,MAAM,OAAO,IAAkB,KAAK,SAAS,CAAC;GAE5D,IAAI,CAAC,MAAM,QAAQ,KAAK,GACtB;GAKF,MAAM,UAAU,CAAC,GAAG,KAAK,CAAC,CAAC,MAAM,MAAM,UAAU,KAAK,UAAU,MAAM,OAAO;GAE7E,KAAK,MAAM,SAAS,SAAS;IAC3B,MAAM,QAAQ,MAAM,OAAO,IAAW,KAAK,SAAS,MAAM,EAAE,CAAC;IAE7D,IAAI,UAAU,QAAQ,UAAU,QAAW;KACzC,KAAK,OAAO,OAAO,MAAM,OAAO;KAChC,KAAK,OAAO,IAAI,MAAM,SAAS,KAAK;IACtC;IAEA,IAAI,MAAM,WAAW,KAAK,YACxB,KAAK,aAAa,MAAM,UAAU;GAEtC;GAEA,KAAK,cAAc;EACrB,SA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;CAEtB;;;;;;CAOA,AAAQ,YAAY,WAA2B,OAAoB;EACjE,QAAQ,MAAM,KAAK,QAAnB;GACE,KAAK;IACH,UAAU,aAAa;IACvB;GAIF,KAAK;GACL,KAAK;IACH,UAAU,UAAU;IACpB;GAGF,KAAK;IACH,UAAU,aAAa;IACvB;GAGF,SACE;EAEJ;CACF;;;;;CAMA,AAAQ,gBAAgB,QAA0B;EAChD,OAAO,OAAO,MAAM,MAAM,UAAU,KAAK,MAAM,MAAM,SAAS,IAAI,KAAK,MAAM,KAAK,SAAS,CAAC;CAC9F;;;;;;;CAQA,AAAQ,gBAAoC;EAC1C,IAAI,KAAK,YAAY,GACnB;EAGF,IAAI;EAEJ,OAAO,KAAK,OAAO,OAAO,KAAK,UAAU;GACvC,MAAM,SAAS,KAAK,OAAO,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;GAEzC,IAAI,WAAW,QACb,OAAO;GAGT,KAAK,OAAO,OAAO,MAAM;GACzB,UAAU;EACZ;EAEA,OAAO;CACT;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,SAAgB,sBACd,OACA,UAA2E,CAAC,GACrD;CACvB,OAAO,IAAI,gBAAgB,OAAO,OAAO;AAC3C"}
|
|
1
|
+
{"version":3,"file":"cache-trace-store.mjs","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/cache-trace-store.ts"],"sourcesContent":["import type { CacheDriver } from \"@warlock.js/cache\";\nimport type { ExporterContract } from \"../contracts/exporter.contract\";\nimport type { Trace } from \"../contracts/trace.type\";\nimport { matchTrace } from \"./match-trace\";\nimport { emptyUsage, sumUsage } from \"./sum-usage\";\nimport type { TraceAggregate } from \"./trace-aggregate.type\";\nimport type { TraceQuery } from \"./trace-query.type\";\nimport type { TraceStoreContract } from \"./trace-store.contract\";\n\n/**\n * A {@link CacheDriver} instance, or a (possibly async) factory that\n * yields one on first use. The factory form lets production defer an\n * expensive connect (e.g. the Redis handshake) until the first trace is\n * actually written, and keeps the dashboard wiring free of a live driver\n * at module-import time.\n *\n * Typed `CacheDriver<any, any>` because the store only ever touches the\n * driver's `get` / `set` / `remove` surface and is agnostic to the\n * concrete client + options of whichever driver backs it.\n */\nexport type CacheDriverInput =\n // The store is driver-agnostic; it only uses get/set/remove, so the\n // concrete client/options generics are intentionally unconstrained.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | CacheDriver<any, any>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | (() => CacheDriver<any, any> | Promise<CacheDriver<any, any>>);\n\n/**\n * Options for {@link createCacheTraceStore}.\n */\nexport type CacheTraceStoreOptions = {\n /**\n * Key prefix every cache entry this store writes is namespaced under.\n * Per-trace keys are `${prefix}:trace:${traceId}`; the newest-first\n * index lives at `${prefix}:index`. Default `\"panoptic\"`.\n */\n prefix?: string;\n /**\n * Maximum number of traces to retain. When set and exceeded, the\n * oldest-ingested trace is evicted from both the cache and the in-memory\n * mirror (insertion-order FIFO). Absent / `0` = unbounded.\n */\n capacity?: number;\n};\n\n/** One entry in the persisted newest-first index. */\ntype IndexEntry = {\n /** The trace's `traceId` — the suffix of its `${prefix}:trace:` key. */\n id: string;\n /**\n * Monotonic insertion order from an internal counter — NOT a wall clock.\n * Used only to keep the index in stable insertion order across a restart\n * so FIFO eviction stays honest.\n */\n addedAt: number;\n};\n\nconst DEFAULT_PREFIX = \"panoptic\";\n\n/**\n * Cache-backed {@link TraceStoreContract} with a **write-through** design\n * that reconciles the synchronous store contract with an asynchronous\n * cache driver.\n *\n * **How the sync/async tension is resolved.** The contract's `get` /\n * `query` / `aggregate` / `size` are synchronous (the dashboard polls them\n * on every request and the in-memory store answers instantly). A cache\n * driver is async. So this store keeps an **in-memory read mirror** — the\n * same insertion-ordered `Map<traceId, Trace>` the in-memory store uses —\n * and serves every read from it synchronously. Writes go **through** to the\n * cache: `add` updates the mirror immediately, then asynchronously persists\n * the trace + index to the cache (errors are swallowed via an optional\n * `onError` hook so a flaky cache never throws into the collector's hot\n * path). On process restart, {@link CacheTraceStore.ready} re-hydrates the\n * mirror from the cache so traces survive the restart.\n *\n * **Durability is best-effort.** Reads never wait on the cache; the mirror\n * is the source of truth at runtime and the cache is the durable backing\n * store. A write that the cache rejects is still visible in the mirror for\n * the life of the process — it just won't survive a restart.\n *\n * **Lazy driver resolution.** The driver (or its async factory) is resolved\n * on first use and memoized, so a production deployment can defer the Redis\n * connect until the first trace is collected, and the dashboard can be\n * wired with a factory at import time without a live connection.\n *\n * Doubles as an {@link ExporterContract} (`export` ≡ `add`), so it drops\n * straight into a collector via `collector.use(store)`.\n *\n * Instantiated via {@link createCacheTraceStore}; callers never see `new`.\n */\nclass CacheTraceStore implements TraceStoreContract, ExporterContract {\n /** Stable exporter id so a collector can dedupe / log this sink. */\n public readonly name = \"cache-trace-store\";\n\n /**\n * In-memory read mirror keyed by `traceId`. A `Map` preserves insertion\n * order, which newest-first `query` ordering and FIFO eviction both rely\n * on. Every read is served from here synchronously.\n */\n private readonly mirror = new Map<string, Trace>();\n\n private readonly prefix: string;\n\n private readonly capacity: number;\n\n /**\n * Monotonic insertion counter — the source of `IndexEntry.addedAt`.\n * Deliberately NOT `Date.now()`: an internal counter guarantees a stable\n * total order for the index even when many traces land in the same\n * millisecond.\n */\n private addCounter = 0;\n\n /** The optional input — a driver, a factory, or `undefined`. */\n private readonly input: CacheDriverInput;\n\n // The store is driver-agnostic; only get/set/remove are used.\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private resolvedDriver?: CacheDriver<any, any>;\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private driverPending?: Promise<CacheDriver<any, any>>;\n\n private readonly onError?: (error: unknown) => void;\n\n public constructor(\n input: CacheDriverInput,\n options: CacheTraceStoreOptions & { onError?: (error: unknown) => void } = {},\n ) {\n this.input = input;\n this.prefix = options.prefix ?? DEFAULT_PREFIX;\n this.capacity = options.capacity ?? 0;\n this.onError = options.onError;\n }\n\n public get size(): number {\n return this.mirror.size;\n }\n\n /**\n * Hydrate the in-memory mirror from the cache. Idempotent-safe to call\n * once at startup (the dashboard / config wiring awaits it). Reads the\n * persisted index, fetches each referenced trace, and replays them into\n * the mirror in insertion order so newest-first ordering + eviction stay\n * correct after a restart. A cache failure is routed to `onError` and\n * leaves the mirror empty rather than throwing.\n */\n public async ready(): Promise<void> {\n try {\n const driver = await this.resolveDriver();\n const index = await driver.get<IndexEntry[]>(this.indexKey());\n\n if (!Array.isArray(index)) {\n return;\n }\n\n // Oldest-first replay so the mirror's insertion order matches the\n // original ingestion order.\n const ordered = [...index].sort((left, right) => left.addedAt - right.addedAt);\n\n for (const entry of ordered) {\n const trace = await driver.get<Trace>(this.traceKey(entry.id));\n\n if (trace !== null && trace !== undefined) {\n this.mirror.delete(trace.traceId);\n this.mirror.set(trace.traceId, trace);\n }\n\n if (entry.addedAt >= this.addCounter) {\n this.addCounter = entry.addedAt + 1;\n }\n }\n\n this.evictOverflow();\n } catch (error) {\n this.reportError(error);\n }\n }\n\n public add(trace: Trace): void {\n // Mirror update is synchronous and authoritative for runtime reads.\n this.mirror.delete(trace.traceId);\n this.mirror.set(trace.traceId, trace);\n\n const addedAt = this.addCounter;\n this.addCounter += 1;\n\n const evicted = this.evictOverflow();\n\n // Write through to the cache fire-and-forget; reads never wait on this.\n void this.persist(trace, addedAt, evicted);\n }\n\n /**\n * `ExporterContract.export` — a collector dispatches a completed trace\n * here, which is exactly an `add`.\n */\n public export(trace: Trace): void {\n this.add(trace);\n }\n\n public get(traceId: string): Trace | undefined {\n return this.mirror.get(traceId);\n }\n\n public query(filter?: TraceQuery): Trace[] {\n const matched: Trace[] = [];\n\n for (const trace of this.mirror.values()) {\n if (matchTrace(trace, filter)) {\n matched.push(trace);\n }\n }\n\n return this.sortNewestFirst(matched);\n }\n\n public aggregate(filter?: TraceQuery): TraceAggregate {\n const aggregate: TraceAggregate = {\n traces: 0,\n completed: 0,\n failed: 0,\n cancelled: 0,\n usage: emptyUsage(),\n totalDuration: 0,\n };\n\n for (const trace of this.mirror.values()) {\n if (!matchTrace(trace, filter)) {\n continue;\n }\n\n aggregate.traces += 1;\n aggregate.totalDuration += trace.duration;\n aggregate.usage = sumUsage(aggregate.usage, trace.usage);\n\n this.countStatus(aggregate, trace);\n }\n\n if (aggregate.usage.cost !== undefined) {\n aggregate.cost = aggregate.usage.cost;\n }\n\n return aggregate;\n }\n\n public clear(): void {\n const ids = [...this.mirror.keys()];\n this.mirror.clear();\n\n void this.purge(ids);\n }\n\n /**\n * Persist one trace + the rebuilt index to the cache, optionally removing\n * a trace evicted by the capacity cap. Best-effort: any cache failure is\n * routed to `onError`, never thrown — the mirror already reflects the\n * write so runtime reads are unaffected.\n */\n private async persist(\n trace: Trace,\n addedAt: number,\n evictedId: string | undefined,\n ): Promise<void> {\n try {\n const driver = await this.resolveDriver();\n\n await driver.set(this.traceKey(trace.traceId), trace);\n\n if (evictedId !== undefined) {\n await driver.remove(this.traceKey(evictedId));\n }\n\n await driver.set(this.indexKey(), this.buildIndex(addedAt));\n } catch (error) {\n this.reportError(error);\n }\n }\n\n /** Best-effort removal of every persisted trace + the index on `clear`. */\n private async purge(ids: string[]): Promise<void> {\n try {\n const driver = await this.resolveDriver();\n\n for (const id of ids) {\n await driver.remove(this.traceKey(id));\n }\n\n await driver.remove(this.indexKey());\n } catch (error) {\n this.reportError(error);\n }\n }\n\n /**\n * Rebuild the newest-first index from the current mirror. The mirror's\n * `Map` iteration is oldest-first insertion order; we walk it and assign\n * `addedAt` from the surviving counter span so the persisted order\n * matches the in-memory one. The freshest entry uses `latestAddedAt`.\n */\n private buildIndex(latestAddedAt: number): IndexEntry[] {\n const ids = [...this.mirror.keys()];\n const base = latestAddedAt - (ids.length - 1);\n\n return ids.map((id, offset) => ({ id, addedAt: base + offset }));\n }\n\n /**\n * Resolve the driver once and memoize. Supports a bare driver, a sync\n * factory, and an async factory. Concurrent first-callers share one\n * in-flight resolution.\n */\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n private async resolveDriver(): Promise<CacheDriver<any, any>> {\n if (this.resolvedDriver !== undefined) {\n return this.resolvedDriver;\n }\n\n if (this.driverPending !== undefined) {\n return this.driverPending;\n }\n\n const candidate =\n typeof this.input === \"function\"\n ? (\n this.input as () =>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | CacheDriver<any, any>\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n | Promise<CacheDriver<any, any>>\n )()\n : this.input;\n\n this.driverPending = Promise.resolve(candidate);\n\n try {\n this.resolvedDriver = await this.driverPending;\n\n return this.resolvedDriver;\n } finally {\n this.driverPending = undefined;\n }\n }\n\n /** `${prefix}:trace:${traceId}` — the per-trace cache key. */\n private traceKey(traceId: string): string {\n return `${this.prefix}:trace:${traceId}`;\n }\n\n /** `${prefix}:index` — the newest-first index cache key. */\n private indexKey(): string {\n return `${this.prefix}:index`;\n }\n\n /** Route a swallowed cache error to the optional handler. */\n private reportError(error: unknown): void {\n if (this.onError !== undefined) {\n this.onError(error);\n }\n }\n\n /**\n * Increment the matching terminal-status counter for one trace.\n * Mirrors the in-memory store: non-terminal statuses are counted in\n * `traces` but tracked by none of the three headline counters.\n */\n private countStatus(aggregate: TraceAggregate, trace: Trace): void {\n switch (trace.root.status) {\n case \"completed\": {\n aggregate.completed += 1;\n break;\n }\n\n // An incomplete agent run (its answer was cut off) errored.\n case \"failed\":\n case \"incomplete\": {\n aggregate.failed += 1;\n break;\n }\n\n case \"cancelled\": {\n aggregate.cancelled += 1;\n break;\n }\n\n default: {\n break;\n }\n }\n }\n\n /**\n * Sort matched traces newest-started first. A copy is sorted so the\n * mirror's insertion order (which eviction depends on) is never disturbed.\n */\n private sortNewestFirst(traces: Trace[]): Trace[] {\n return traces.sort((left, right) => Date.parse(right.startedAt) - Date.parse(left.startedAt));\n }\n\n /**\n * Evict oldest-inserted traces from the mirror until within `capacity`.\n * Returns the id of the single evicted trace (the common case — one `add`\n * pushes at most one over the cap) so the caller can remove it from the\n * cache too. No-op + `undefined` when unbounded or within the cap.\n */\n private evictOverflow(): string | undefined {\n if (this.capacity <= 0) {\n return undefined;\n }\n\n let evicted: string | undefined;\n\n while (this.mirror.size > this.capacity) {\n const oldest = this.mirror.keys().next().value;\n\n if (oldest === undefined) {\n return evicted;\n }\n\n this.mirror.delete(oldest);\n evicted = oldest;\n }\n\n return evicted;\n }\n}\n\n/**\n * The concrete store type returned by {@link createCacheTraceStore} — the\n * standard {@link TraceStoreContract} + {@link ExporterContract} surface,\n * plus a `ready()` to hydrate the in-memory mirror from the cache on\n * startup so traces survive a process restart.\n */\nexport type CacheTraceStoreHandle = TraceStoreContract &\n ExporterContract & {\n /**\n * Hydrate the in-memory mirror from the cache. Await once at startup\n * (the dashboard wiring does this for you) so previously-persisted\n * traces are queryable after a restart.\n */\n ready(): Promise<void>;\n };\n\n/**\n * Create a cache-backed trace store. Reads are served synchronously from an\n * in-memory mirror; writes go through to the cache, and {@link\n * CacheTraceStoreHandle.ready} re-hydrates the mirror on startup so traces\n * survive a restart. See {@link CacheTraceStore} for the full write-through\n * design.\n *\n * @param cache a {@link CacheDriver}, or a (possibly async) factory that\n * yields one on first use — resolved lazily and memoized so a production\n * Redis connect can be deferred until the first trace is collected.\n * @param options `prefix` (default `\"panoptic\"`), `capacity` (FIFO cap),\n * and an optional `onError` hook for swallowed cache write failures.\n *\n * @example\n * import { RedisCacheDriver } from \"@warlock.js/cache\";\n *\n * // Lazy async factory — defers the Redis connect until first use.\n * const store = createCacheTraceStore(async () => {\n * const driver = new RedisCacheDriver();\n * await driver.connect();\n * return driver;\n * });\n *\n * await store.ready(); // hydrate from a prior run\n * collector.use(store); // fills as traces complete\n * const failed = store.query({ status: \"failed\" });\n */\nexport function createCacheTraceStore(\n cache: CacheDriverInput,\n options: CacheTraceStoreOptions & { onError?: (error: unknown) => void } = {},\n): CacheTraceStoreHandle {\n return new CacheTraceStore(cache, options);\n}\n"],"mappings":";;;;AA0DA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkCvB,IAAM,kBA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AM,UAAU,KAAK,MAAM,MAAM,SAAS,IAAI,KAAK,MAAM,KAAK,SAAS,CAAC;CAC9F;;;;;;;CAQA,AAAQ,gBAAoC;EAC1C,IAAI,KAAK,YAAY,GACnB;EAGF,IAAI;EAEJ,OAAO,KAAK,OAAO,OAAO,KAAK,UAAU;GACvC,MAAM,SAAS,KAAK,OAAO,KAAK,CAAC,CAAC,KAAK,CAAC,CAAC;GAEzC,IAAI,WAAW,QACb,OAAO;GAGT,KAAK,OAAO,OAAO,MAAM;GACzB,UAAU;EACZ;EAEA,OAAO;CACT;AACF;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6CA,SAAgB,sBACd,OACA,UAA2E,CAAC,GACrD;CACvB,OAAO,IAAI,gBAAgB,OAAO,OAAO;AAC3C"}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { ExporterContract } from "../contracts/exporter.contract.mjs";
|
|
2
2
|
import { TraceStoreContract } from "./trace-store.contract.mjs";
|
|
3
|
-
|
|
4
3
|
//#region ../../packages/ai-panoptic/src/store/in-memory-trace-store.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* Options for {@link createInMemoryTraceStore}.
|
|
7
6
|
*/
|
|
8
|
-
type InMemoryTraceStoreOptions = {
|
|
7
|
+
export type InMemoryTraceStoreOptions = {
|
|
9
8
|
/**
|
|
10
9
|
* Maximum number of traces to retain. When set and exceeded, the
|
|
11
10
|
* oldest-ingested trace is evicted (insertion-order FIFO) so the
|
|
@@ -26,7 +25,6 @@ type InMemoryTraceStoreOptions = {
|
|
|
26
25
|
* const recentFailures = store.query({ status: "failed" });
|
|
27
26
|
* const sessionSpend = store.aggregate({ sessionId });
|
|
28
27
|
*/
|
|
29
|
-
declare function createInMemoryTraceStore(options?: InMemoryTraceStoreOptions): TraceStoreContract & ExporterContract;
|
|
28
|
+
export declare function createInMemoryTraceStore(options?: InMemoryTraceStoreOptions): TraceStoreContract & ExporterContract;
|
|
30
29
|
//#endregion
|
|
31
|
-
export { InMemoryTraceStoreOptions, createInMemoryTraceStore };
|
|
32
30
|
//# sourceMappingURL=in-memory-trace-store.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory-trace-store.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/in-memory-trace-store.ts"],"mappings":";;;;;;
|
|
1
|
+
{"version":3,"file":"in-memory-trace-store.d.mts","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/in-memory-trace-store.ts"],"mappings":";;;;;;YAWY;;;;;;;EAOV;;;;;;;;;;;;;;wBAsLc,yBACd,UAAU,4BACT,qBAAqB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"in-memory-trace-store.mjs","names":[],"sources":["../../../../../../../../packages/ai-panoptic/src/store/in-memory-trace-store.ts"],"sourcesContent":["import type { ExporterContract } from \"../contracts/exporter.contract\";\nimport type { Trace } from \"../contracts/trace.type\";\nimport { matchTrace } from \"./match-trace\";\nimport { emptyUsage, sumUsage } from \"./sum-usage\";\nimport type { TraceAggregate } from \"./trace-aggregate.type\";\nimport type { TraceQuery } from \"./trace-query.type\";\nimport type { TraceStoreContract } from \"./trace-store.contract\";\n\n/**\n * Options for {@link createInMemoryTraceStore}.\n */\nexport type InMemoryTraceStoreOptions = {\n /**\n * Maximum number of traces to retain. When set and exceeded, the\n * oldest-ingested trace is evicted (insertion-order FIFO) so the\n * store stays bounded for long-lived processes. Absent / `0` =\n * unbounded (keep everything until `clear`).\n */\n capacity?: number;\n};\n\n/**\n * In-memory {@link TraceStoreContract} that doubles as an\n * {@link ExporterContract} — register it on a collector\n * (`collector.use(store)`) and it fills as traces complete, then query\n * or aggregate it after the fact.\n *\n * Backed by an insertion-ordered `Map` keyed by `traceId`, giving O(1)\n * `get` / `add` / overwrite and O(n) scans for `query` / `aggregate`\n * (the price of an in-memory store with no secondary indexes — fine for\n * the dev/test and modest-volume runtime use this targets). When a\n * `capacity` is configured, ingesting past the cap evicts the oldest\n * trace.\n *\n * Instantiated fresh per store via {@link createInMemoryTraceStore};\n * callers never see `new`.\n */\nclass InMemoryTraceStore implements TraceStoreContract, ExporterContract {\n /** Stable exporter id so a collector can dedupe / log this sink. */\n public readonly name = \"in-memory-trace-store\";\n\n /**\n * Retained traces keyed by `traceId`. A `Map` preserves insertion\n * order, which is what FIFO eviction and newest-first `query` ordering\n * both rely on.\n */\n private readonly traces = new Map<string, Trace>();\n\n private readonly capacity: number;\n\n public constructor(options?: InMemoryTraceStoreOptions) {\n this.capacity = options?.capacity ?? 0;\n }\n\n public get size(): number {\n return this.traces.size;\n }\n\n public add(trace: Trace): void {\n // Re-insert so an overwrite also refreshes insertion position —\n // keeps \"oldest\" honest for FIFO eviction.\n this.traces.delete(trace.traceId);\n this.traces.set(trace.traceId, trace);\n\n this.evictOverflow();\n }\n\n /**\n * `ExporterContract.export` — a collector dispatches a completed\n * trace here, which is exactly an `add`. Lets the store be wired into\n * a collector as a sink without an adapter.\n */\n public export(trace: Trace): void {\n this.add(trace);\n }\n\n public get(traceId: string): Trace | undefined {\n return this.traces.get(traceId);\n }\n\n public query(filter?: TraceQuery): Trace[] {\n const matched: Trace[] = [];\n\n for (const trace of this.traces.values()) {\n if (matchTrace(trace, filter)) {\n matched.push(trace);\n }\n }\n\n return this.sortNewestFirst(matched);\n }\n\n public aggregate(filter?: TraceQuery): TraceAggregate {\n const aggregate: TraceAggregate = {\n traces: 0,\n completed: 0,\n failed: 0,\n cancelled: 0,\n usage: emptyUsage(),\n totalDuration: 0,\n };\n\n for (const trace of this.traces.values()) {\n if (!matchTrace(trace, filter)) {\n continue;\n }\n\n aggregate.traces += 1;\n aggregate.totalDuration += trace.duration;\n aggregate.usage = sumUsage(aggregate.usage, trace.usage);\n\n this.countStatus(aggregate, trace);\n }\n\n if (aggregate.usage.cost !== undefined) {\n aggregate.cost = aggregate.usage.cost;\n }\n\n return aggregate;\n }\n\n public clear(): void {\n this.traces.clear();\n }\n\n /**\n * Increment the matching terminal-status counter for one trace.\n * Non-terminal statuses (`awaiting-input`, `max-iterations`) are\n * counted in `traces` but tracked by none of the three headline\n * counters — intentional, those three answer the common\n * \"succeeded / errored / aborted\" question. An `incomplete` agent run\n * (its answer was cut off) errored, so it counts as `failed`.\n */\n private countStatus(aggregate: TraceAggregate, trace: Trace): void {\n switch (trace.root.status) {\n case \"completed\": {\n aggregate.completed += 1;\n break;\n }\n\n case \"failed\":\n case \"incomplete\": {\n aggregate.failed += 1;\n break;\n }\n\n case \"cancelled\": {\n aggregate.cancelled += 1;\n break;\n }\n\n default: {\n break;\n }\n }\n }\n\n /**\n * Sort matched traces newest-started first. A copy is sorted so the\n * underlying insertion order (which eviction depends on) is never\n * disturbed.\n */\n private sortNewestFirst(traces: Trace[]): Trace[] {\n return traces.sort((left, right) => Date.parse(right.startedAt) - Date.parse(left.startedAt));\n }\n\n /**\n * Evict oldest-inserted traces until the store is within `capacity`.\n * No-op when unbounded. The `Map` iterator yields keys in insertion\n * order, so the first key is always the oldest.\n */\n private evictOverflow(): void {\n if (this.capacity <= 0) {\n return;\n }\n\n while (this.traces.size > this.capacity) {\n const oldest = this.traces.keys().next().value;\n\n if (oldest === undefined) {\n return;\n }\n\n this.traces.delete(oldest);\n }\n }\n}\n\n/**\n * Create an in-memory trace store. Optionally bound it with `capacity`\n * for long-lived processes; leave it unset for dev/test where you want\n * every trace retained.\n *\n * @example\n * const store = createInMemoryTraceStore({ capacity: 1000 });\n * collector.use(store);\n * // later:\n * const recentFailures = store.query({ status: \"failed\" });\n * const sessionSpend = store.aggregate({ sessionId });\n */\nexport function createInMemoryTraceStore(\n options?: InMemoryTraceStoreOptions,\n): TraceStoreContract & ExporterContract {\n return new InMemoryTraceStore(options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqCA,IAAM,qBAAN,MAAyE;;CAEvE,AAAgB,OAAO;;;;;;CAOvB,AAAiB,yBAAS,IAAI,IAAmB;CAEjD,AAAiB;CAEjB,AAAO,YAAY,SAAqC;EACtD,KAA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ai-panoptic/src/store/in-memory-trace-store.ts"],"sourcesContent":["import type { ExporterContract } from \"../contracts/exporter.contract\";\nimport type { Trace } from \"../contracts/trace.type\";\nimport { matchTrace } from \"./match-trace\";\nimport { emptyUsage, sumUsage } from \"./sum-usage\";\nimport type { TraceAggregate } from \"./trace-aggregate.type\";\nimport type { TraceQuery } from \"./trace-query.type\";\nimport type { TraceStoreContract } from \"./trace-store.contract\";\n\n/**\n * Options for {@link createInMemoryTraceStore}.\n */\nexport type InMemoryTraceStoreOptions = {\n /**\n * Maximum number of traces to retain. When set and exceeded, the\n * oldest-ingested trace is evicted (insertion-order FIFO) so the\n * store stays bounded for long-lived processes. Absent / `0` =\n * unbounded (keep everything until `clear`).\n */\n capacity?: number;\n};\n\n/**\n * In-memory {@link TraceStoreContract} that doubles as an\n * {@link ExporterContract} — register it on a collector\n * (`collector.use(store)`) and it fills as traces complete, then query\n * or aggregate it after the fact.\n *\n * Backed by an insertion-ordered `Map` keyed by `traceId`, giving O(1)\n * `get` / `add` / overwrite and O(n) scans for `query` / `aggregate`\n * (the price of an in-memory store with no secondary indexes — fine for\n * the dev/test and modest-volume runtime use this targets). When a\n * `capacity` is configured, ingesting past the cap evicts the oldest\n * trace.\n *\n * Instantiated fresh per store via {@link createInMemoryTraceStore};\n * callers never see `new`.\n */\nclass InMemoryTraceStore implements TraceStoreContract, ExporterContract {\n /** Stable exporter id so a collector can dedupe / log this sink. */\n public readonly name = \"in-memory-trace-store\";\n\n /**\n * Retained traces keyed by `traceId`. A `Map` preserves insertion\n * order, which is what FIFO eviction and newest-first `query` ordering\n * both rely on.\n */\n private readonly traces = new Map<string, Trace>();\n\n private readonly capacity: number;\n\n public constructor(options?: InMemoryTraceStoreOptions) {\n this.capacity = options?.capacity ?? 0;\n }\n\n public get size(): number {\n return this.traces.size;\n }\n\n public add(trace: Trace): void {\n // Re-insert so an overwrite also refreshes insertion position —\n // keeps \"oldest\" honest for FIFO eviction.\n this.traces.delete(trace.traceId);\n this.traces.set(trace.traceId, trace);\n\n this.evictOverflow();\n }\n\n /**\n * `ExporterContract.export` — a collector dispatches a completed\n * trace here, which is exactly an `add`. Lets the store be wired into\n * a collector as a sink without an adapter.\n */\n public export(trace: Trace): void {\n this.add(trace);\n }\n\n public get(traceId: string): Trace | undefined {\n return this.traces.get(traceId);\n }\n\n public query(filter?: TraceQuery): Trace[] {\n const matched: Trace[] = [];\n\n for (const trace of this.traces.values()) {\n if (matchTrace(trace, filter)) {\n matched.push(trace);\n }\n }\n\n return this.sortNewestFirst(matched);\n }\n\n public aggregate(filter?: TraceQuery): TraceAggregate {\n const aggregate: TraceAggregate = {\n traces: 0,\n completed: 0,\n failed: 0,\n cancelled: 0,\n usage: emptyUsage(),\n totalDuration: 0,\n };\n\n for (const trace of this.traces.values()) {\n if (!matchTrace(trace, filter)) {\n continue;\n }\n\n aggregate.traces += 1;\n aggregate.totalDuration += trace.duration;\n aggregate.usage = sumUsage(aggregate.usage, trace.usage);\n\n this.countStatus(aggregate, trace);\n }\n\n if (aggregate.usage.cost !== undefined) {\n aggregate.cost = aggregate.usage.cost;\n }\n\n return aggregate;\n }\n\n public clear(): void {\n this.traces.clear();\n }\n\n /**\n * Increment the matching terminal-status counter for one trace.\n * Non-terminal statuses (`awaiting-input`, `max-iterations`) are\n * counted in `traces` but tracked by none of the three headline\n * counters — intentional, those three answer the common\n * \"succeeded / errored / aborted\" question. An `incomplete` agent run\n * (its answer was cut off) errored, so it counts as `failed`.\n */\n private countStatus(aggregate: TraceAggregate, trace: Trace): void {\n switch (trace.root.status) {\n case \"completed\": {\n aggregate.completed += 1;\n break;\n }\n\n case \"failed\":\n case \"incomplete\": {\n aggregate.failed += 1;\n break;\n }\n\n case \"cancelled\": {\n aggregate.cancelled += 1;\n break;\n }\n\n default: {\n break;\n }\n }\n }\n\n /**\n * Sort matched traces newest-started first. A copy is sorted so the\n * underlying insertion order (which eviction depends on) is never\n * disturbed.\n */\n private sortNewestFirst(traces: Trace[]): Trace[] {\n return traces.sort((left, right) => Date.parse(right.startedAt) - Date.parse(left.startedAt));\n }\n\n /**\n * Evict oldest-inserted traces until the store is within `capacity`.\n * No-op when unbounded. The `Map` iterator yields keys in insertion\n * order, so the first key is always the oldest.\n */\n private evictOverflow(): void {\n if (this.capacity <= 0) {\n return;\n }\n\n while (this.traces.size > this.capacity) {\n const oldest = this.traces.keys().next().value;\n\n if (oldest === undefined) {\n return;\n }\n\n this.traces.delete(oldest);\n }\n }\n}\n\n/**\n * Create an in-memory trace store. Optionally bound it with `capacity`\n * for long-lived processes; leave it unset for dev/test where you want\n * every trace retained.\n *\n * @example\n * const store = createInMemoryTraceStore({ capacity: 1000 });\n * collector.use(store);\n * // later:\n * const recentFailures = store.query({ status: \"failed\" });\n * const sessionSpend = store.aggregate({ sessionId });\n */\nexport function createInMemoryTraceStore(\n options?: InMemoryTraceStoreOptions,\n): TraceStoreContract & ExporterContract {\n return new InMemoryTraceStore(options);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAqCA,IAAM,qBAAN,MAAyE;;CAEvE,AAAgB,OAAO;;;;;;CAOvB,AAAiB,yBAAS,IAAI,IAAmB;CAEjD,AAAiB;CAEjB,AAAO,YAAY,SAAqC;EACtD,KAAK,WAAW,SAAS,YAAY;CACvC;CAEA,IAAW,OAAe;EACxB,OAAO,KAAK,OAAO;CACrB;CAEA,AAAO,IAAI,OAAoB;EAG7B,KAAK,OAAO,OAAO,MAAM,OAAO;EAChC,KAAK,OAAO,IAAI,MAAM,SAAS,KAAK;EAEpC,KAAK,cAAc;CACrB;;;;;;CAOA,AAAO,OAAO,OAAoB;EAChC,KAAK,IAAI,KAAK;CAChB;CAEA,AAAO,IAAI,SAAoC;EAC7C,OAAO,KAAK,OAAO,IAAI,OAAO;CAChC;CAEA,AAAO,MAAM,QAA8B;EACzC,MAAM,UAAmB,CAAC;EAE1B,KAAK,MAAM,SAAS,KAAK,OAAO,OAAO,GACrC,IAAI,WAAW,OAAO,MAAM,GAC1B,QAAQ,KAA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oreContract } from "./trace-store.contract.mjs";
|
|
|
4
4
|
import { CacheDriverInput, CacheTraceStoreHandle, CacheTraceStoreOptions, createCacheTraceStore } from "./cache-trace-store.mjs";
|
|
5
5
|
import { InMemoryTraceStoreOptions, createInMemoryTraceStore } from "./in-memory-trace-store.mjs";
|
|
6
6
|
import { matchTrace } from "./match-trace.mjs";
|
|
7
|
-
import { emptyUsage, sumUsage } from "./sum-usage.mjs";
|
|
7
|
+
import { emptyUsage, sumUsage } from "./sum-usage.mjs";
|
|
8
|
+
export { type CacheDriverInput, type CacheTraceStoreHandle, type CacheTraceStoreOptions, type InMemoryTraceStoreOptions, type TraceAggregate, type TraceQuery, type TraceStoreContract, createCacheTraceStore, createInMemoryTraceStore, emptyUsage, matchTrace, sumUsage };
|
package/esm/store/index.mjs
CHANGED
|
@@ -3,4 +3,4 @@ import { emptyUsage, sumUsage } from "./sum-usage.mjs";
|
|
|
3
3
|
import { createCacheTraceStore } from "./cache-trace-store.mjs";
|
|
4
4
|
import { createInMemoryTraceStore } from "./in-memory-trace-store.mjs";
|
|
5
5
|
|
|
6
|
-
export {
|
|
6
|
+
export { createCacheTraceStore, createInMemoryTraceStore, emptyUsage, matchTrace, sumUsage };
|