pi-cloudflare-agent-tracing 0.0.0-stage → 0.2.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.
@@ -0,0 +1,92 @@
1
+ #!/usr/bin/env node
2
+ import { readFile } from "node:fs/promises";
3
+ import { pathToFileURL } from "node:url";
4
+
5
+ if (import.meta.url === pathToFileURL(process.argv[1] ?? "").href) {
6
+ const path = process.argv[2];
7
+ if (!path) {
8
+ console.error(
9
+ "Usage: node .pi/agent-tracing/scripts/summarize-session-context.mjs <session-context.json>",
10
+ );
11
+ process.exitCode = 2;
12
+ } else {
13
+ const input = JSON.parse(await readFile(path, "utf8"));
14
+ console.log(JSON.stringify(summarizeSessionContext(input), null, 2));
15
+ }
16
+ }
17
+
18
+ export function summarizeSessionContext(input) {
19
+ const entries = Array.isArray(input?.contextEntries) ? input.contextEntries : [];
20
+ return {
21
+ query: contentFreeQuery(input?.query),
22
+ entriesScanned: finiteNumber(input?.entriesScanned),
23
+ historyPagesLoaded: finiteNumber(input?.historyPagesLoaded),
24
+ entries: entries.map(summarizeEntry),
25
+ };
26
+ }
27
+
28
+ function summarizeEntry(entry) {
29
+ const messages = Array.isArray(entry?.model) ? entry.model : [];
30
+ return {
31
+ id: finiteNumber(entry?.id),
32
+ kind: safeString(entry?.kind),
33
+ byTaskId: finiteNumber(entry?.byTaskId),
34
+ messages: messages.map(summarizeMessage).filter(Boolean),
35
+ };
36
+ }
37
+
38
+ function summarizeMessage(message) {
39
+ if (!message || typeof message !== "object") return undefined;
40
+ const base = { role: safeString(message.role) };
41
+ if (message.role === "assistant") {
42
+ const content = Array.isArray(message.content) ? message.content : [];
43
+ return {
44
+ ...base,
45
+ stopReason: safeString(message.stopReason),
46
+ usage: summarizeUsage(message.usage),
47
+ toolCalls: content
48
+ .filter((item) => item && typeof item === "object" && item.type === "toolCall")
49
+ .map((item) => ({ id: safeString(item.id), name: safeString(item.name) })),
50
+ textBlocks: content.filter((item) => item?.type === "text").length,
51
+ thinkingBlocks: content.filter((item) => item?.type === "thinking").length,
52
+ };
53
+ }
54
+ if (message.role === "toolResult") {
55
+ return {
56
+ ...base,
57
+ toolCallId: safeString(message.toolCallId),
58
+ toolName: safeString(message.toolName),
59
+ isError: message.isError === true,
60
+ };
61
+ }
62
+ return base;
63
+ }
64
+
65
+ function summarizeUsage(usage) {
66
+ if (!usage || typeof usage !== "object") return undefined;
67
+ return {
68
+ input: finiteNumber(usage.input),
69
+ output: finiteNumber(usage.output),
70
+ cacheRead: finiteNumber(usage.cacheRead),
71
+ cacheWrite: finiteNumber(usage.cacheWrite),
72
+ costTotal: finiteNumber(usage.cost?.total),
73
+ };
74
+ }
75
+
76
+ function contentFreeQuery(query) {
77
+ if (!query || typeof query !== "object") return undefined;
78
+ return {
79
+ workspaceId: safeString(query.workspaceId),
80
+ conversationId: finiteNumber(query.conversationId),
81
+ taskId: finiteNumber(query.taskId),
82
+ callId: safeString(query.callId),
83
+ };
84
+ }
85
+
86
+ function safeString(value) {
87
+ return typeof value === "string" ? value.slice(0, 200) : undefined;
88
+ }
89
+
90
+ function finiteNumber(value) {
91
+ return typeof value === "number" && Number.isFinite(value) ? value : undefined;
92
+ }
@@ -0,0 +1,46 @@
1
+ import assert from "node:assert/strict";
2
+ import test from "node:test";
3
+ import { summarizeSessionContext } from "./summarize-session-context.mjs";
4
+
5
+ test("removes prompts, answers, tool arguments, and tool results", () => {
6
+ const summary = summarizeSessionContext({
7
+ query: { workspaceId: "space-1", conversationId: 2, taskId: 3 },
8
+ entriesScanned: 4,
9
+ historyPagesLoaded: 1,
10
+ contextEntries: [
11
+ {
12
+ id: 8,
13
+ kind: "pi.assistant",
14
+ byTaskId: 3,
15
+ model: [
16
+ {
17
+ role: "assistant",
18
+ stopReason: "toolUse",
19
+ usage: { input: 10, output: 2, cost: { total: 0.01 } },
20
+ content: [
21
+ { type: "text", text: "private answer" },
22
+ { type: "thinking", thinking: "private reasoning" },
23
+ { type: "toolCall", id: "call-1", name: "search", arguments: { secret: "x" } },
24
+ ],
25
+ },
26
+ {
27
+ role: "toolResult",
28
+ toolCallId: "call-1",
29
+ toolName: "search",
30
+ isError: false,
31
+ content: [{ type: "text", text: "private result" }],
32
+ },
33
+ ],
34
+ },
35
+ ],
36
+ });
37
+
38
+ const encoded = JSON.stringify(summary);
39
+ assert.equal(encoded.includes("private"), false);
40
+ assert.equal(encoded.includes("secret"), false);
41
+ assert.deepEqual(summary.entries[0].messages[0].toolCalls, [
42
+ { id: "call-1", name: "search" },
43
+ ]);
44
+ assert.equal(summary.entries[0].messages[0].textBlocks, 1);
45
+ assert.equal(summary.entries[0].messages[1].isError, false);
46
+ });