mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-11-30 09:05:15 +01:00
Preserve unknown Pi receipt cache and cost fields
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
ea9eb3f85e
commit
14e1e09a79
3 files changed
+55
-27
No files matched your search
@@ -77,6 +77,18 @@ describe("Pi ACP bridge", () => {
|
||||
usage.reset(); expect(usage.response()).toEqual({});
|
||||
});
|
||||
|
||||
it("does not invent missing cache totals or zero cost", () => {
|
||||
const usage = new PiTurnUsage();
|
||||
usage.accept({ role: "assistant", timestamp: 1, stopReason: "stop", usage: { input: 4, output: 2 } });
|
||||
expect(usage.response()).toEqual({ usage: {
|
||||
inputTokens: 4, outputTokens: 2, _meta: { paperclipPi: { provenance: "assistant_message_receipts" } },
|
||||
} });
|
||||
usage.accept({ role: "assistant", timestamp: 2, stopReason: "stop", usage: { input: 4, output: 2, cacheRead: 0, cacheWrite: 0, cost: { total: 0 } } });
|
||||
expect(usage.response()).toEqual({ usage: {
|
||||
inputTokens: 8, outputTokens: 4, _meta: { paperclipPi: { provenance: "assistant_message_receipts" } },
|
||||
} });
|
||||
});
|
||||
|
||||
it("launches verified paths only and rejects session escapes", async () => {
|
||||
const root = await mkdtemp(join(tmpdir(), "paperclip-pi-launch-")); temporary.push(root);
|
||||
await mkdir(join(root, "sessions"));
|
||||
|
||||
@@ -213,9 +213,12 @@ export class PiTurnUsage {
|
||||
private seen = new Set<string>();
|
||||
private total = { inputTokens: 0, outputTokens: 0, cachedReadTokens: 0, cachedWriteTokens: 0, totalTokens: 0 };
|
||||
private cost = 0;
|
||||
private unknown = new Set<keyof typeof this.total>();
|
||||
private costObserved = false;
|
||||
private costIncomplete = false;
|
||||
private failed = false;
|
||||
private observed = false;
|
||||
reset(): void { this.seen.clear(); this.total = { inputTokens: 0, outputTokens: 0, cachedReadTokens: 0, cachedWriteTokens: 0, totalTokens: 0 }; this.cost = 0; this.failed = false; this.observed = false; }
|
||||
reset(): void { this.seen.clear(); this.total = { inputTokens: 0, outputTokens: 0, cachedReadTokens: 0, cachedWriteTokens: 0, totalTokens: 0 }; this.cost = 0; this.unknown.clear(); this.costObserved = false; this.costIncomplete = false; this.failed = false; this.observed = false; }
|
||||
accept(value: unknown): void {
|
||||
const message = record(value);
|
||||
if (message.role !== "assistant") return;
|
||||
@@ -226,22 +229,27 @@ export class PiTurnUsage {
|
||||
if (this.seen.size >= 8192) throw new Error("Pi usage receipts exceed their bound");
|
||||
this.seen.add(key);
|
||||
const usage = record(message.usage);
|
||||
const numbers = ["input", "output", "cacheRead", "cacheWrite"].map((name) => usage[name] ?? 0);
|
||||
if (numbers.some((value) => typeof value !== "number" || !Number.isSafeInteger(value) || value < 0)) return;
|
||||
this.observed = true;
|
||||
for (const [index, field] of ["inputTokens", "outputTokens", "cachedReadTokens", "cachedWriteTokens"].entries()) {
|
||||
const name = field as keyof typeof this.total;
|
||||
this.total[name] += numbers[index] as number;
|
||||
}
|
||||
this.total.totalTokens = this.total.inputTokens + this.total.outputTokens + this.total.cachedReadTokens + this.total.cachedWriteTokens;
|
||||
if (usage.cost && typeof usage.cost === "object") {
|
||||
const cost = record(usage.cost).total;
|
||||
if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0) this.cost += cost;
|
||||
const valid = (value: unknown): value is number => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
||||
const fields = ["inputTokens", "outputTokens", "cachedReadTokens", "cachedWriteTokens"] as const;
|
||||
const numbers = ["input", "output", "cacheRead", "cacheWrite"].map((name) => usage[name]);
|
||||
for (const [index, name] of fields.entries()) {
|
||||
const value = numbers[index];
|
||||
if (valid(value)) { this.total[name] += value; this.observed = true; }
|
||||
else this.unknown.add(name);
|
||||
}
|
||||
const total = valid(usage.totalTokens) ? usage.totalTokens
|
||||
: numbers.every(valid) ? (numbers as number[]).reduce((sum, value) => sum + value, 0) : undefined;
|
||||
if (valid(total)) this.total.totalTokens += total;
|
||||
else this.unknown.add("totalTokens");
|
||||
const cost = usage.cost && typeof usage.cost === "object" ? record(usage.cost).total : undefined;
|
||||
if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0) {
|
||||
this.cost += cost; this.costObserved = true;
|
||||
} else this.costIncomplete = true;
|
||||
}
|
||||
|
||||
response(): RecordValue {
|
||||
return {
|
||||
...(this.observed ? { usage: { ...this.total, _meta: { paperclipPi: { provenance: "assistant_message_receipts", costUsd: this.cost } } } } : {}),
|
||||
...(this.observed ? { usage: { ...Object.fromEntries(Object.entries(this.total).filter(([name]) => !this.unknown.has(name as keyof typeof this.total))), _meta: { paperclipPi: { provenance: "assistant_message_receipts", ...(this.costObserved && !this.costIncomplete ? { costUsd: this.cost } : {}) } } } } : {}),
|
||||
...(this.failed ? { _meta: { jetbrains: { air: { version: 1, sessionFailure: { severity: "error", category: "service", title: "Pi provider request failed" } } } } } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
+22
-14
@@ -426,7 +426,7 @@ diff --git a/dist/paperclip-runtime.js b/dist/paperclip-runtime.js
|
||||
new file mode 100644
|
||||
--- /dev/null
|
||||
+++ b/dist/paperclip-runtime.js
|
||||
@@ -0,0 +1,248 @@
|
||||
@@ -0,0 +1,256 @@
|
||||
+/** Source for the helper embedded in patches/pi-acp@0.0.33.patch. */
|
||||
+import { StringDecoder } from "node:string_decoder";
|
||||
+import { isAbsolute, relative, resolve, sep } from "node:path";
|
||||
@@ -642,9 +642,12 @@ new file mode 100644
|
||||
+ seen = new Set ();
|
||||
+ total = { inputTokens: 0, outputTokens: 0, cachedReadTokens: 0, cachedWriteTokens: 0, totalTokens: 0 };
|
||||
+ cost = 0;
|
||||
+ unknown = new Set ();
|
||||
+ costObserved = false;
|
||||
+ costIncomplete = false;
|
||||
+ failed = false;
|
||||
+ observed = false;
|
||||
+ reset() { this.seen.clear(); this.total = { inputTokens: 0, outputTokens: 0, cachedReadTokens: 0, cachedWriteTokens: 0, totalTokens: 0 }; this.cost = 0; this.failed = false; this.observed = false; }
|
||||
+ reset() { this.seen.clear(); this.total = { inputTokens: 0, outputTokens: 0, cachedReadTokens: 0, cachedWriteTokens: 0, totalTokens: 0 }; this.cost = 0; this.unknown.clear(); this.costObserved = false; this.costIncomplete = false; this.failed = false; this.observed = false; }
|
||||
+ accept(value ) {
|
||||
+ const message = record(value);
|
||||
+ if (message.role !== "assistant") return;
|
||||
@@ -655,22 +658,27 @@ new file mode 100644
|
||||
+ if (this.seen.size >= 8192) throw new Error("Pi usage receipts exceed their bound");
|
||||
+ this.seen.add(key);
|
||||
+ const usage = record(message.usage);
|
||||
+ const numbers = ["input", "output", "cacheRead", "cacheWrite"].map((name) => usage[name] ?? 0);
|
||||
+ if (numbers.some((value) => typeof value !== "number" || !Number.isSafeInteger(value) || value < 0)) return;
|
||||
+ this.observed = true;
|
||||
+ for (const [index, field] of ["inputTokens", "outputTokens", "cachedReadTokens", "cachedWriteTokens"].entries()) {
|
||||
+ const name = field ;
|
||||
+ this.total[name] += numbers[index] ;
|
||||
+ }
|
||||
+ this.total.totalTokens = this.total.inputTokens + this.total.outputTokens + this.total.cachedReadTokens + this.total.cachedWriteTokens;
|
||||
+ if (usage.cost && typeof usage.cost === "object") {
|
||||
+ const cost = record(usage.cost).total;
|
||||
+ if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0) this.cost += cost;
|
||||
+ const valid = (value ) => typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
|
||||
+ const fields = ["inputTokens", "outputTokens", "cachedReadTokens", "cachedWriteTokens"] ;
|
||||
+ const numbers = ["input", "output", "cacheRead", "cacheWrite"].map((name) => usage[name]);
|
||||
+ for (const [index, name] of fields.entries()) {
|
||||
+ const value = numbers[index];
|
||||
+ if (valid(value)) { this.total[name] += value; this.observed = true; }
|
||||
+ else this.unknown.add(name);
|
||||
+ }
|
||||
+ const total = valid(usage.totalTokens) ? usage.totalTokens
|
||||
+ : numbers.every(valid) ? (numbers ).reduce((sum, value) => sum + value, 0) : undefined;
|
||||
+ if (valid(total)) this.total.totalTokens += total;
|
||||
+ else this.unknown.add("totalTokens");
|
||||
+ const cost = usage.cost && typeof usage.cost === "object" ? record(usage.cost).total : undefined;
|
||||
+ if (typeof cost === "number" && Number.isFinite(cost) && cost >= 0) {
|
||||
+ this.cost += cost; this.costObserved = true;
|
||||
+ } else this.costIncomplete = true;
|
||||
+ }
|
||||
+
|
||||
+ response() {
|
||||
+ return {
|
||||
+ ...(this.observed ? { usage: { ...this.total, _meta: { paperclipPi: { provenance: "assistant_message_receipts", costUsd: this.cost } } } } : {}),
|
||||
+ ...(this.observed ? { usage: { ...Object.fromEntries(Object.entries(this.total).filter(([name]) => !this.unknown.has(name ))), _meta: { paperclipPi: { provenance: "assistant_message_receipts", ...(this.costObserved && !this.costIncomplete ? { costUsd: this.cost } : {}) } } } } : {}),
|
||||
+ ...(this.failed ? { _meta: { jetbrains: { air: { version: 1, sessionFailure: { severity: "error", category: "service", title: "Pi provider request failed" } } } } } : {}),
|
||||
+ };
|
||||
+ }
|
||||
|
||||
Reference in new issue
Block a user