pi-provider-cursor-ask 0.1.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 +9 -0
- package/LICENSE +21 -0
- package/README.md +87 -0
- package/README.zh-CN.md +87 -0
- package/UPSTREAM_CHANGELOG.md +368 -0
- package/UPSTREAM_SOURCE.md +23 -0
- package/dist/index.js +54 -0
- package/package.json +97 -0
- package/src/auth/cli-credentials.ts +275 -0
- package/src/auth/consent.ts +25 -0
- package/src/auth/index.ts +23 -0
- package/src/auth/oauth.ts +282 -0
- package/src/auth/refresh-guard.ts +93 -0
- package/src/client/bridge.ts +673 -0
- package/src/client/cursor-wire.ts +213 -0
- package/src/client/h2-unary.ts +142 -0
- package/src/client/index.ts +18 -0
- package/src/config/index.ts +69 -0
- package/src/diagnostics/diagnostics.ts +116 -0
- package/src/diagnostics/index.ts +1 -0
- package/src/extension/auth.ts +99 -0
- package/src/extension/commands.ts +163 -0
- package/src/extension/compaction-guard.ts +86 -0
- package/src/extension/debug-hooks.ts +359 -0
- package/src/extension/index.ts +8 -0
- package/src/extension/provider.ts +277 -0
- package/src/extension/quota-adapter.ts +175 -0
- package/src/extension/report-dashboard.ts +133 -0
- package/src/identity.ts +16 -0
- package/src/index.ts +186 -0
- package/src/models/ask-catalog.ts +384 -0
- package/src/models/catalog.json +1163 -0
- package/src/models/cost.ts +126 -0
- package/src/models/index.ts +6 -0
- package/src/models/limits.ts +36 -0
- package/src/models/parameterized.ts +416 -0
- package/src/models/processing.ts +313 -0
- package/src/proto/agent_pb.ts +14577 -0
- package/src/stream/bridge-session.ts +215 -0
- package/src/stream/client-transcript.ts +51 -0
- package/src/stream/config.ts +5 -0
- package/src/stream/context-normalize.ts +308 -0
- package/src/stream/context-usage.ts +168 -0
- package/src/stream/debug-log.ts +316 -0
- package/src/stream/drift.ts +122 -0
- package/src/stream/images.ts +201 -0
- package/src/stream/index.ts +68 -0
- package/src/stream/interaction-query.ts +369 -0
- package/src/stream/message-parsing.ts +402 -0
- package/src/stream/model-cache.ts +100 -0
- package/src/stream/model-discovery.ts +242 -0
- package/src/stream/model-routing.ts +100 -0
- package/src/stream/native-core.ts +2121 -0
- package/src/stream/pi-adapter.ts +414 -0
- package/src/stream/protocol.ts +63 -0
- package/src/stream/recovery.ts +494 -0
- package/src/stream/request-build.ts +668 -0
- package/src/stream/root-prompt.ts +184 -0
- package/src/stream/run-journal.ts +474 -0
- package/src/stream/run-usage.ts +107 -0
- package/src/stream/server-messages.ts +777 -0
- package/src/stream/session-state.ts +499 -0
- package/src/stream/stream-writer.ts +211 -0
- package/src/stream/thinking-filter.ts +63 -0
- package/src/stream/tool-schema.ts +185 -0
- package/src/stream/transport-errors.ts +150 -0
- package/src/stream/tuning.ts +250 -0
- package/src/stream/types.ts +330 -0
- package/src/types/enums.ts +103 -0
- package/src/types/index.ts +4 -0
- package/src/usage.ts +262 -0
- package/src/utils/cache-dir.ts +39 -0
- package/src/utils/index.ts +2 -0
- package/src/utils/security.ts +68 -0
- package/src/utils/util.ts +43 -0
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/** Receipt ownership follows an upstream Run, not each local Pi tool response. */
|
|
2
|
+
import { lifecycleLog, reportCursorAnomaly } from "./debug-log.js";
|
|
3
|
+
import type {
|
|
4
|
+
CursorBilledUsage,
|
|
5
|
+
CursorRunUsage,
|
|
6
|
+
StoredConversation,
|
|
7
|
+
StreamState,
|
|
8
|
+
} from "./types.js";
|
|
9
|
+
|
|
10
|
+
export interface CursorBillingInfo {
|
|
11
|
+
status: "reported" | "partial" | "already-reported" | "pending" | "unavailable" | "not-started";
|
|
12
|
+
missingFields?: Array<keyof CursorBilledUsage>;
|
|
13
|
+
carriedReceipts?: number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function recordRunReceipt(
|
|
17
|
+
state: StreamState,
|
|
18
|
+
billed: CursorBilledUsage,
|
|
19
|
+
raw: {
|
|
20
|
+
inputTokens?: bigint | number;
|
|
21
|
+
outputTokens?: bigint | number;
|
|
22
|
+
cacheReadTokens?: bigint | number;
|
|
23
|
+
cacheWriteTokens?: bigint | number;
|
|
24
|
+
},
|
|
25
|
+
): void {
|
|
26
|
+
const run = (state.runUsage ??= {});
|
|
27
|
+
// A repeated terminal frame must never turn the same receipt into a second charge.
|
|
28
|
+
if (run.billedUsage) return;
|
|
29
|
+
const pairs = [
|
|
30
|
+
["input", raw.inputTokens],
|
|
31
|
+
["output", raw.outputTokens],
|
|
32
|
+
["cacheRead", raw.cacheReadTokens],
|
|
33
|
+
["cacheWrite", raw.cacheWriteTokens],
|
|
34
|
+
] as const;
|
|
35
|
+
const missingFields = pairs
|
|
36
|
+
.filter(
|
|
37
|
+
([, value]) =>
|
|
38
|
+
value === undefined || !Number.isSafeInteger(Number(value)) || Number(value) < 0,
|
|
39
|
+
)
|
|
40
|
+
.map(([key]) => key);
|
|
41
|
+
if (billed.cacheRead + billed.cacheWrite > billed.input && !missingFields.includes("input"))
|
|
42
|
+
missingFields.push("input");
|
|
43
|
+
state.billedUsage = billed;
|
|
44
|
+
run.billedUsage = billed;
|
|
45
|
+
run.missingFields = missingFields;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function takeRunReceipt(state?: StreamState): {
|
|
49
|
+
billed?: CursorBilledUsage;
|
|
50
|
+
info: CursorBillingInfo;
|
|
51
|
+
} {
|
|
52
|
+
if (!state) return { info: { status: "not-started" } };
|
|
53
|
+
const run = (state.runUsage ??= {});
|
|
54
|
+
// Preserve the small standalone StreamState seam used by older callers/tests.
|
|
55
|
+
run.billedUsage ??= state.billedUsage;
|
|
56
|
+
if (run.reported) return { info: { status: "already-reported" } };
|
|
57
|
+
if (!run.billedUsage) return { info: { status: state.turnEnded ? "unavailable" : "pending" } };
|
|
58
|
+
run.reported = true;
|
|
59
|
+
const missingFields = run.missingFields ?? [];
|
|
60
|
+
const raw = run.billedUsage;
|
|
61
|
+
const canPriceInput = !missingFields.some(
|
|
62
|
+
(key) => key === "input" || key === "cacheRead" || key === "cacheWrite",
|
|
63
|
+
);
|
|
64
|
+
return {
|
|
65
|
+
billed: {
|
|
66
|
+
...raw,
|
|
67
|
+
// When the cache split is incomplete we cannot price the cache-inclusive input as uncached.
|
|
68
|
+
// Only explicitly known output/cache buckets are charged; metadata marks the missing remainder.
|
|
69
|
+
input: canPriceInput ? raw.input : raw.cacheRead + raw.cacheWrite,
|
|
70
|
+
},
|
|
71
|
+
info: missingFields.length ? { status: "partial", missingFields } : { status: "reported" },
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function retainRunReceipt(
|
|
76
|
+
stored: StoredConversation | undefined,
|
|
77
|
+
run: CursorRunUsage,
|
|
78
|
+
): void {
|
|
79
|
+
if (!stored || !run.billedUsage || run.reported) return;
|
|
80
|
+
const pending = (stored.unreportedUsage ??= []);
|
|
81
|
+
if (!pending.includes(run)) pending.push(run);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function takePendingRunReceipts(
|
|
85
|
+
stored: StoredConversation | undefined,
|
|
86
|
+
modelId: string,
|
|
87
|
+
): CursorRunUsage[] {
|
|
88
|
+
if (!stored?.unreportedUsage) return [];
|
|
89
|
+
const receipts = stored.unreportedUsage.filter((run) => !run.reported && run.modelId === modelId);
|
|
90
|
+
stored.unreportedUsage = stored.unreportedUsage.filter(
|
|
91
|
+
(run) => !run.reported && run.modelId !== modelId,
|
|
92
|
+
);
|
|
93
|
+
return receipts;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export function reportRunUsageBoundary(run: CursorRunUsage, reason: string): void {
|
|
97
|
+
if (run.boundaryLogged || run.reported) return;
|
|
98
|
+
run.boundaryLogged = true;
|
|
99
|
+
const status = run.billedUsage ? "receipt_unreported" : "receipt_missing";
|
|
100
|
+
lifecycleLog("usage_run_unsettled", { reason, status });
|
|
101
|
+
reportCursorAnomaly(
|
|
102
|
+
"usage_incomplete",
|
|
103
|
+
"Cursor billing is not fully reported; displayed costs may omit usage.",
|
|
104
|
+
{ reason, status },
|
|
105
|
+
{ level: "warning" },
|
|
106
|
+
);
|
|
107
|
+
}
|