@abelxiaoxing/cadence 2.0.29 → 2.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "schema": "cadence-web-assets/1",
3
- "sourceHash": "56d61dbb96ce4ef1dd8f83d6ca150105d8417059a1f2f5f9bfe4762f05151a88",
4
- "contractHash": "70b7d387e5677a43ff0a80f82aad39684362496916c796150a4e326cd7756b65",
5
- "buildId": "4a07fe3e72c45001d5e3589fc580f60780a434069f353573bbe8de3a4b94674b",
3
+ "sourceHash": "51621747d5c49a6fc60e28d4c5b75b265b4c540debc382164db4566b10d0fdc8",
4
+ "contractHash": "3305384b10bb0c658565426ec2291d0237533cb9ae7aeb1d63f42ac8824b0c82",
5
+ "buildId": "307d83f0f2817b57a5f8df492dac832b2823f18bde7b84f1868b4f87dbda00de",
6
6
  "assets": [
7
7
  {
8
8
  "name": "assets/cadence.css",
9
- "bytes": 110832,
10
- "sha256": "070e31a05b62f3071c5ab59532784d6954cf56be34b56a3250e1bfe4707d3160",
9
+ "bytes": 112035,
10
+ "sha256": "6ab6ebe03d4b6e8bfddddd7239dee0aa16fe4d472b67399067636cb100b7c147",
11
11
  "references": [
12
12
  "assets/KaTeX_AMS-Regular.ttf",
13
13
  "assets/KaTeX_AMS-Regular.woff",
@@ -73,8 +73,8 @@
73
73
  },
74
74
  {
75
75
  "name": "assets/cadence.js",
76
- "bytes": 322394,
77
- "sha256": "992e22c9197f19ef1f1d235e939a618366c65ace9772f81a5864970445096275",
76
+ "bytes": 333960,
77
+ "sha256": "48b75a6e603d3e0dec7b35a4b89f7661b63c97f587163a26318ccec0defe90df",
78
78
  "references": [
79
79
  "assets/markdown.js",
80
80
  "assets/react.js",
@@ -1,225 +0,0 @@
1
- /** Private ordinary-chat context. No Pi session import, hidden thinking or provider envelopes. */
2
- import { createHash } from "node:crypto";
3
- import {
4
- closeSync,
5
- constants,
6
- fstatSync,
7
- lstatSync,
8
- openSync,
9
- readSync,
10
- } from "node:fs";
11
- import { dirname, join } from "node:path";
12
- import {
13
- canonicalJson,
14
- closed,
15
- integer,
16
- LIMITS,
17
- list,
18
- oneOf,
19
- parseWireJson,
20
- text,
21
- } from "./worker-public.mjs";
22
- import { DetailSanitizer, sanitizeText } from "./worker-redaction.mjs";
23
-
24
- export const CONTEXT_BYTES = 2 * 1024 * 1024;
25
- // One message/pipe record, not the lifetime context. Same logical body bound as capture.
26
- export const CONTEXT_RECORD_BYTES = LIMITS.detailBytes;
27
- const clean = (s, secrets, max = CONTEXT_RECORD_BYTES) =>
28
- sanitizeText(text(s, max), secrets);
29
- function argumentsData(value, secrets) {
30
- const sanitizer = new DetailSanitizer("canonical-json", secrets);
31
- const source =
32
- sanitizer.write(canonicalJson(value, CONTEXT_RECORD_BYTES)) +
33
- sanitizer.end();
34
- if (sanitizer.limited) throw new Error("Unsupported context arguments");
35
- return parseWireJson(source, CONTEXT_RECORD_BYTES);
36
- }
37
- function own(value, key) {
38
- const d =
39
- value && typeof value === "object"
40
- ? Object.getOwnPropertyDescriptor(value, key)
41
- : undefined;
42
- if (!d || !("value" in d)) throw new Error("Invalid usage");
43
- return d.value;
44
- }
45
- // Pi adds optional usage breakdowns per provider and release (reasoning, cacheWrite1h);
46
- // retain only the counts every assistant message carries, read as own data properties.
47
- function counts(value, keys) {
48
- const out = Object.create(null);
49
- for (const key of keys) {
50
- const n = own(value, key);
51
- if (typeof n !== "number" || !Number.isFinite(n) || n < 0)
52
- throw new Error("Invalid usage");
53
- out[key] = n;
54
- }
55
- return out;
56
- }
57
- /** Deliberately retain text and tool exchanges, not just last assistant prose. */
58
- export function conversationMessages(messages, secrets = []) {
59
- return list(messages, 4096, (message) => {
60
- if (!message || typeof message !== "object")
61
- throw new Error("Invalid context");
62
- const role = oneOf(message.role, ["user", "assistant", "toolResult"]);
63
- const timestamp = integer(message.timestamp, 0);
64
- const content =
65
- typeof message.content === "string"
66
- ? [{ type: "text", text: clean(message.content, secrets) }]
67
- : list(message.content, 256, (block) => {
68
- if (block.type === "text")
69
- return { type: "text", text: clean(block.text, secrets) };
70
- if (role === "assistant" && block.type === "toolCall")
71
- return {
72
- type: "toolCall",
73
- id: clean(block.id, secrets, 512),
74
- name: clean(block.name, secrets, 128),
75
- arguments: argumentsData(block.arguments, secrets),
76
- };
77
- // Images, hidden thinking and opaque provider signatures never become retained context.
78
- return null;
79
- }).filter(Boolean);
80
- if (role === "user") return { role, timestamp, content };
81
- if (role === "toolResult")
82
- return {
83
- role,
84
- timestamp,
85
- content,
86
- toolCallId: clean(message.toolCallId, secrets, 512),
87
- toolName: clean(message.toolName, secrets, 128),
88
- isError: message.isError === true,
89
- };
90
- const usage = counts(message.usage, [
91
- "input",
92
- "output",
93
- "cacheRead",
94
- "cacheWrite",
95
- "totalTokens",
96
- ]);
97
- usage.cost = counts(own(message.usage, "cost"), [
98
- "input",
99
- "output",
100
- "cacheRead",
101
- "cacheWrite",
102
- "total",
103
- ]);
104
- return {
105
- role,
106
- timestamp,
107
- content,
108
- api: clean(message.api, secrets, 128),
109
- provider: clean(message.provider, secrets, 128),
110
- model: clean(message.model, secrets, 256),
111
- usage,
112
- stopReason: oneOf(message.stopReason, [
113
- "stop",
114
- "length",
115
- "toolUse",
116
- "error",
117
- "aborted",
118
- ]),
119
- };
120
- });
121
- }
122
- function readPrivateContext(path, maximum) {
123
- const fd = openSync(path, constants.O_RDONLY | (constants.O_NOFOLLOW ?? 0));
124
- try {
125
- const st = fstatSync(fd);
126
- if (
127
- !st.isFile() ||
128
- st.nlink !== 1 ||
129
- st.size > maximum ||
130
- (process.platform !== "win32" &&
131
- (st.mode & 0o077 || st.uid !== process.getuid()))
132
- )
133
- throw new Error("Invalid context file");
134
- const buffer = Buffer.alloc(st.size + 1);
135
- let size = 0;
136
- while (size < buffer.length) {
137
- const n = readSync(fd, buffer, size, buffer.length - size, null);
138
- if (!n) break;
139
- size += n;
140
- }
141
- const after = fstatSync(fd);
142
- if (
143
- size !== st.size ||
144
- after.size !== st.size ||
145
- after.mtimeMs !== st.mtimeMs ||
146
- after.ctimeMs !== st.ctimeMs ||
147
- lstatSync(path).ino !== st.ino
148
- )
149
- throw new Error("Changed context file");
150
- return new TextDecoder("utf-8", { fatal: true }).decode(
151
- buffer.subarray(0, size),
152
- );
153
- } finally {
154
- closeSync(fd);
155
- }
156
- }
157
- /** Old checkpoints remain readable without migration. New checkpoints hash-chain
158
- * private per-message records, so no aggregate JSON/pipe size is required. */
159
- export function* iterateConversationContext(path) {
160
- if (path === null) return;
161
- const source = readPrivateContext(path, CONTEXT_BYTES);
162
- const value = parseWireJson(source, CONTEXT_BYTES);
163
- if (value.schema === "cadence-conversation-context/1") {
164
- const o = closed(value, ["schema", "messages"]);
165
- yield* conversationMessages(o.messages);
166
- return;
167
- }
168
- const o = closed(value, ["schema", "messages", "head"]);
169
- if (o.schema !== "cadence-conversation-context/2")
170
- throw new Error("Invalid context format");
171
- const count = integer(o.messages, 0);
172
- let head = null;
173
- for (let index = 0; index < count; index++) {
174
- const body = readPrivateContext(
175
- join(dirname(path), `context-${index}.json`),
176
- CONTEXT_RECORD_BYTES,
177
- );
178
- const record = closed(parseWireJson(body, CONTEXT_RECORD_BYTES), [
179
- "schema",
180
- "index",
181
- "previous",
182
- "message",
183
- ]);
184
- if (
185
- record.schema !== "cadence-conversation-message/1" ||
186
- record.index !== index ||
187
- record.previous !== head
188
- )
189
- throw new Error("Invalid context chain");
190
- head = createHash("sha256").update(body).digest("hex");
191
- yield conversationMessages([record.message])[0];
192
- }
193
- if (o.head !== head) throw new Error("Changed context chain");
194
- }
195
- export function readConversationContext(path) {
196
- return [...iterateConversationContext(path)];
197
- }
198
- /** Anonymous pipe framing: each message is bounded, the stream is not. The
199
- * owner sanitizes each record again before persisting it and commits only on
200
- * the end marker plus original successful process closure. */
201
- export function* conversationContextRecords(messages, secrets = []) {
202
- if (!Array.isArray(messages)) throw new Error("Invalid context");
203
- let count = 0;
204
- for (const message of messages) {
205
- yield `${canonicalJson(
206
- {
207
- schema: "cadence-conversation-stream/1",
208
- message: conversationMessages([message], secrets)[0],
209
- },
210
- CONTEXT_RECORD_BYTES - 4096,
211
- )}\n`;
212
- count++;
213
- }
214
- yield canonicalJson({ schema: "cadence-conversation-stream-end/1", count }) +
215
- "\n";
216
- }
217
- export function encodeConversationContext(messages, secrets) {
218
- return canonicalJson(
219
- {
220
- schema: "cadence-conversation-context/1",
221
- messages: conversationMessages(messages, secrets),
222
- },
223
- CONTEXT_BYTES,
224
- );
225
- }