ctxtop 0.0.0-stage โ 4.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/.antigravity-plugin/plugin.json +36 -0
- package/.claude-plugin/plugin.json +18 -0
- package/CHECKSUMS.sha256 +166 -0
- package/LICENSE +27 -0
- package/README.md +350 -2
- package/SECURITY.md +22 -0
- package/agents/ah-flash-scout.md +25 -0
- package/assets/advanced_view_loaded_420px.png +0 -0
- package/assets/hero-demo.svg +188 -0
- package/assets/logo.svg +19 -0
- package/assets/simple_view_lean_420px.png +0 -0
- package/assets/simple_view_loaded_420px.png +0 -0
- package/assets/social-preview.svg +72 -0
- package/assets/zone_c_step3_shield_full.png +0 -0
- package/bin/ah-statusline.mjs +175 -0
- package/bin/cli.mjs +3 -0
- package/bin/ctxtop.mjs +1799 -0
- package/commands/cost-radar.md +11 -0
- package/commands/evolve.md +10 -0
- package/commands/handoff.md +10 -0
- package/commands/horizon.md +10 -0
- package/commands/inspect.md +10 -0
- package/commands/precheck.md +10 -0
- package/config/default_flightcheck.json +133 -0
- package/config/modelCatalog.json +785 -0
- package/config/platformQuirks.json +19 -0
- package/hooks/handlers/compact-lifecycle.mjs +577 -0
- package/hooks/handlers/config-change.mjs +402 -0
- package/hooks/handlers/hookIo.mjs +311 -0
- package/hooks/handlers/hookIpcClient.mjs +83 -0
- package/hooks/handlers/post-model-switch.mjs +111 -0
- package/hooks/handlers/post-tool-use.mjs +366 -0
- package/hooks/handlers/pre-model-switch.mjs +1048 -0
- package/hooks/handlers/pre-tool-use.mjs +632 -0
- package/hooks/handlers/stop-gate.mjs +526 -0
- package/hooks/handlers/user-prompt-submit.mjs +367 -0
- package/hooks/hooks.json +131 -0
- package/hooks/post_invocation.mjs +13 -0
- package/hooks/post_tool_use.mjs +25 -0
- package/hooks/pre_invocation.mjs +14 -0
- package/hooks/pre_tool_use.mjs +97 -0
- package/hooks/register.js +726 -0
- package/hooks/session_start.mjs +24 -0
- package/hooks/stop.mjs +23 -0
- package/hooks.json +56 -0
- package/package.json +110 -4
- package/packages/antigravity/package.json +14 -0
- package/packages/antigravity/src/index.mjs +1 -0
- package/packages/claude-code/package.json +14 -0
- package/packages/claude-code/src/index.mjs +2 -0
- package/packages/core/package.json +14 -0
- package/packages/core/src/index.mjs +1 -0
- package/packages/engine-node/package.json +14 -0
- package/packages/engine-node/src/index.mjs +1 -0
- package/packages/mcp/package.json +17 -0
- package/packages/mcp/src/server.mjs +396 -0
- package/packages/pro/LICENSE-COMMERCIAL.md +19 -0
- package/packages/pro/README-SIGNING.md +30 -0
- package/packages/pro/package.json +16 -0
- package/packages/pro/src/index.mjs +3 -0
- package/packages/pro/src/team_finops_pack.mjs +241 -0
- package/packages/pro/workers/polar_webhook_worker.mjs +215 -0
- package/plugin.json +36 -0
- package/rules/AGENTS.md +10 -0
- package/sidecars/horizon-daemon/ipcServer.mjs +24 -0
- package/sidecars/horizon-daemon/server.mjs +2038 -0
- package/sidecars/horizon-daemon/sidecar.json +19 -0
- package/skills/cost-radar/SKILL.md +21 -0
- package/skills/evolve/SKILL.md +21 -0
- package/skills/handoff/SKILL.md +50 -0
- package/skills/horizon/SKILL.md +17 -0
- package/skills/inspect/SKILL.md +18 -0
- package/skills/precheck/SKILL.md +44 -0
- package/src/adapters/antigravity/index.mjs +109 -0
- package/src/adapters/claude-code/index.mjs +194 -0
- package/src/cli/cost_radar.mjs +70 -0
- package/src/cli/doctor.mjs +351 -0
- package/src/cli/horizon_hud.mjs +888 -0
- package/src/cli/init.mjs +425 -0
- package/src/cli/postmortem.mjs +515 -0
- package/src/coach/cacheGravityRouter.mjs +12 -0
- package/src/config/modelCatalog.json +704 -0
- package/src/config/platformQuirks.json +19 -0
- package/src/core/action_queue.mjs +264 -0
- package/src/core/ag_bridge.mjs +346 -0
- package/src/core/atomic_io.mjs +124 -0
- package/src/core/baseline.mjs +607 -0
- package/src/core/blindspot_sensors.mjs +399 -0
- package/src/core/bucket_b_runtime.mjs +1200 -0
- package/src/core/classifier.mjs +1757 -0
- package/src/core/coach/circuit_breakers.mjs +650 -0
- package/src/core/coach/contradiction_auditor.mjs +239 -0
- package/src/core/coach/effort_advisor.mjs +143 -0
- package/src/core/coach/handoff_hydrator.mjs +483 -0
- package/src/core/coach/progressive_checkpointer.mjs +551 -0
- package/src/core/coach/prompt_decomposer_coach.mjs +141 -0
- package/src/core/coach/reflector_queue.mjs +505 -0
- package/src/core/coach/saved_quota_ledger.mjs +615 -0
- package/src/core/coach/subagent_ipi_sanitizer.mjs +200 -0
- package/src/core/coach/subagent_phantom_auditor.mjs +261 -0
- package/src/core/coach/task_quota_router.mjs +955 -0
- package/src/core/coach/turn_zero_gate.mjs +574 -0
- package/src/core/compaction_coach.mjs +1126 -0
- package/src/core/config.mjs +214 -0
- package/src/core/constants.mjs +1987 -0
- package/src/core/detectors_det01_to_16.mjs +1523 -0
- package/src/core/doc_indexer.mjs +338 -0
- package/src/core/episodic_memory.mjs +135 -0
- package/src/core/fts5_output_compressor.mjs +359 -0
- package/src/core/git_ledger.mjs +189 -0
- package/src/core/handoff_audit.mjs +558 -0
- package/src/core/history.mjs +221 -0
- package/src/core/hook_diagnostics.mjs +158 -0
- package/src/core/horizon_classifier.mjs +326 -0
- package/src/core/horizon_config.mjs +802 -0
- package/src/core/host_watchdog.mjs +200 -0
- package/src/core/layer5_hygiene.mjs +1084 -0
- package/src/core/log_resolver.mjs +231 -0
- package/src/core/mcp_auditor.mjs +138 -0
- package/src/core/micro_skills_store.mjs +91 -0
- package/src/core/offenders.mjs +161 -0
- package/src/core/parser.mjs +1997 -0
- package/src/core/path_canonicalizer.mjs +73 -0
- package/src/core/paths.mjs +311 -0
- package/src/core/prefix_cache_shield.mjs +386 -0
- package/src/core/pro_license.mjs +385 -0
- package/src/core/prompt_rubric.mjs +518 -0
- package/src/core/pruner.mjs +433 -0
- package/src/core/quota.mjs +1860 -0
- package/src/core/rsi_instinct_engine.mjs +1197 -0
- package/src/core/rsl_clash_auditor.mjs +423 -0
- package/src/core/rsl_distiller.mjs +372 -0
- package/src/core/rsl_ledger.mjs +969 -0
- package/src/core/rsl_triage.mjs +909 -0
- package/src/core/sanitizer.mjs +121 -0
- package/src/core/session_auditor.mjs +658 -0
- package/src/core/session_forensics.mjs +303 -0
- package/src/core/session_timeline.mjs +358 -0
- package/src/core/settings.mjs +99 -0
- package/src/core/shared/detectors.mjs +1342 -0
- package/src/core/shared/fnv1a64.mjs +85 -0
- package/src/core/shared/handoff_schema.mjs +123 -0
- package/src/core/shared/index.mjs +5 -0
- package/src/core/shared/ipi_sanitizer.mjs +165 -0
- package/src/core/shared/micro_skills.mjs +1 -0
- package/src/core/shared/plugin_validator.mjs +202 -0
- package/src/core/shared/prompt_hygiene_governor.mjs +1 -0
- package/src/core/shield_manager.mjs +1417 -0
- package/src/core/sidecar_store.mjs +302 -0
- package/src/core/sprint_metrics.mjs +366 -0
- package/src/core/sqlite_client.mjs +283 -0
- package/src/core/storage_doctor.mjs +358 -0
- package/src/core/subagents.mjs +609 -0
- package/src/core/swarm_topology.mjs +360 -0
- package/src/core/task_quota_router.mjs +1656 -0
- package/src/core/telemetry/cache_ttl_watchdog.mjs +284 -0
- package/src/core/telemetry/scope_lock_registry.mjs +578 -0
- package/src/core/telemetry/shadow_file_detector.mjs +291 -0
- package/src/core/telemetry/trajectory_watcher.mjs +259 -0
- package/src/core/timeline_svg.mjs +236 -0
- package/src/core/tool_surgeon.mjs +305 -0
- package/src/core/trajectory_stitcher.mjs +645 -0
- package/src/core/u_shaped_optimizer.mjs +789 -0
- package/src/core/wal_hygiene.mjs +233 -0
- package/src/core-isomorphic/catalog/detectorCatalog.mjs +558 -0
- package/src/core-isomorphic/catalog/index.mjs +1 -0
- package/src/core-isomorphic/coach/cacheGravityRouter.mjs +1547 -0
- package/src/core-isomorphic/coach/microSkills.mjs +1 -0
- package/src/core-isomorphic/coach/promptHygieneGovernor.mjs +1 -0
- package/src/core-isomorphic/coach/repoCalibration.mjs +254 -0
- package/src/core-isomorphic/coach/resumeToolsetAuditor.mjs +321 -0
- package/src/core-isomorphic/coach/savedQuotaLedger.mjs +527 -0
- package/src/core-isomorphic/coach/smartCompactAdvisor.mjs +305 -0
- package/src/core-isomorphic/coach/stateBatonHydrator.mjs +810 -0
- package/src/core-isomorphic/coach/ttlWatchdog.mjs +464 -0
- package/src/core-isomorphic/config/anthropicModelCatalog.json +606 -0
- package/src/core-isomorphic/config/catalogLoader.mjs +1268 -0
- package/src/core-isomorphic/config/horizonConfig.mjs +1055 -0
- package/src/core-isomorphic/control/callTree.mjs +338 -0
- package/src/core-isomorphic/control/coachLatches.mjs +126 -0
- package/src/core-isomorphic/control/controlStateMachine.mjs +205 -0
- package/src/core-isomorphic/control/index.mjs +49 -0
- package/src/core-isomorphic/control/swarmWorktree.mjs +109 -0
- package/src/core-isomorphic/control/toolSuspension.mjs +197 -0
- package/src/core-isomorphic/diagnostics/drift_guard.mjs +382 -0
- package/src/core-isomorphic/diagnostics/index.mjs +9 -0
- package/src/core-isomorphic/diagnostics/micro_skills.mjs +471 -0
- package/src/core-isomorphic/diagnostics/prompt_hygiene_governor.mjs +888 -0
- package/src/core-isomorphic/economics/billingBlocks.mjs +251 -0
- package/src/core-isomorphic/economics/cacheGravity.mjs +369 -0
- package/src/core-isomorphic/economics/cliffBreaker.mjs +256 -0
- package/src/core-isomorphic/economics/driftLatency.mjs +433 -0
- package/src/core-isomorphic/economics/dualTtl.mjs +198 -0
- package/src/core-isomorphic/economics/index.mjs +47 -0
- package/src/core-isomorphic/economics/retentionRadar.mjs +348 -0
- package/src/core-isomorphic/guards/bucketBAlphaBeta.mjs +860 -0
- package/src/core-isomorphic/guards/bucketBGamma.mjs +442 -0
- package/src/core-isomorphic/guards/bucketBHeuristics.mjs +37 -0
- package/src/core-isomorphic/guards/circuitBreakers.mjs +1202 -0
- package/src/core-isomorphic/guards/det01_03_shell_secret.mjs +1184 -0
- package/src/core-isomorphic/guards/det04_12_thrash_rot.mjs +572 -0
- package/src/core-isomorphic/guards/det13_20_mcp_scope.mjs +913 -0
- package/src/core-isomorphic/guards/docIndexCore.mjs +446 -0
- package/src/core-isomorphic/guards/losslessPostSanitizer.mjs +317 -0
- package/src/core-isomorphic/guards/slopsquatGate.mjs +551 -0
- package/src/core-isomorphic/index.js +27 -0
- package/src/core-isomorphic/telemetry/otelExporter.mjs +187 -0
- package/src/core-isomorphic/telemetry/scopeLockRegistry.mjs +522 -0
- package/src/core-isomorphic/telemetry/sprintMetrics.mjs +327 -0
- package/src/core-isomorphic/telemetry/swarmTopology.mjs +152 -0
- package/src/core-isomorphic/telemetry/telemetryBridge.mjs +1156 -0
- package/src/core-isomorphic/ui/brailleSparkline.mjs +142 -0
- package/src/core-isomorphic/ui/costRadarSvg.mjs +276 -0
- package/src/core-isomorphic/ui/horizonGrid.mjs +475 -0
- package/src/core-isomorphic/ui/horizonPanelHtml.mjs +1237 -0
- package/src/core-isomorphic/ui/modViews.mjs +1504 -0
- package/src/core-isomorphic/ui/pdfReceiptGenerator.mjs +329 -0
- package/src/core-isomorphic/ustf/index.mjs +7 -0
- package/src/core-isomorphic/ustf/normalizer.mjs +512 -0
- package/src/core-isomorphic/ustf/schema.mjs +186 -0
- package/src/core-isomorphic/utils/runtime.mjs +962 -0
- package/src/demo/synthetic_states.mjs +661 -0
- package/src/diagnostics/bugReportGenerator.mjs +253 -0
- package/src/diagnostics/claudeHookCapabilities.mjs +396 -0
- package/src/diagnostics/doctorEngine.mjs +295 -0
- package/src/engine-node/db/index.mjs +58 -0
- package/src/engine-node/db/jsonl_fallback.mjs +184 -0
- package/src/engine-node/db/probe.mjs +342 -0
- package/src/engine-node/db/sqlite_wrapper.mjs +122 -0
- package/src/engine-node/index.mjs +12 -0
- package/src/engine-node/ipc/client.mjs +447 -0
- package/src/engine-node/ipc/index.mjs +22 -0
- package/src/engine-node/ipc/protocol.mjs +177 -0
- package/src/engine-node/ipc/server.mjs +445 -0
- package/src/engine-node/offloader/index.mjs +6 -0
- package/src/engine-node/offloader/tool_offloader.mjs +241 -0
- package/src/engine-node/sensors/blindspots.mjs +376 -0
- package/src/engine-node/sensors/git_merge_tree.mjs +223 -0
- package/src/engine-node/sensors/index.mjs +7 -0
- package/src/hooks/antigravity_rsl_hook.mjs +250 -0
- package/src/hooks/claude_rsl_hook.mjs +288 -0
- package/src/hooks/portfolio_runtime.mjs +858 -0
- package/src/hooks/post_invocation.mjs +611 -0
- package/src/hooks/pre_invocation.mjs +1725 -0
- package/src/hooks/session_start.mjs +199 -0
- package/src/shims/agy_cli_compressor.mjs +705 -0
- package/src/shims/agy_git_guard.mjs +749 -0
- package/src/shims/ah_log_query.mjs +39 -0
- package/src/ui/costRadarSvg.mjs +6 -0
- package/src/ui/horizonGrid.mjs +1 -0
- package/src/ui/horizonPanelHtml.mjs +1 -0
- package/src/ui/horizon_panel.css +167 -0
- package/src/ui/horizon_panel.js +212 -0
- package/src/ui/modViews.mjs +1 -0
- package/src/utils/modelSwitchTelemetry.mjs +308 -0
- package/src/utils/nodeIo.mjs +59 -0
- package/src/utils/runtime.mjs +1 -0
- package/src/utils/sessionCalibration.mjs +657 -0
- package/src/webview/advanced_view.html +1808 -0
- package/src/webview/horizon_panel.html +12397 -0
- package/src/webview/registry.mjs +80 -0
- package/src/webview/simple_view.html +1370 -0
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file src/core-isomorphic/ui/pdfReceiptGenerator.mjs
|
|
3
|
+
* Pure Zero-Dependency Corporate Expense Receipt Generator (PDF 1.4 + HTML + Markdown).
|
|
4
|
+
*
|
|
5
|
+
* Strictly zero `node:*` imports (Mod Law 4 compliant).
|
|
6
|
+
* Generates standards-compliant single-page PDF 1.4 binary documents, print-ready HTML,
|
|
7
|
+
* and Markdown corporate reimbursement reports with FIPS 180-4 SHA-256 audit digests.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const SHA256_K = new Uint32Array([
|
|
11
|
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
|
12
|
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
|
13
|
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
|
14
|
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
|
15
|
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
|
16
|
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
|
17
|
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
|
18
|
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
function rotr(x, n) {
|
|
22
|
+
return (x >>> n) | (x << (32 - n));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Pure-V8 FIPS 180-4 SHA-256 hex digest (zero `node:*` imports).
|
|
27
|
+
* @param {string} input
|
|
28
|
+
* @returns {string}
|
|
29
|
+
*/
|
|
30
|
+
export function sha256HexPure(input = '') {
|
|
31
|
+
const bytes = new TextEncoder().encode(String(input));
|
|
32
|
+
const bitLen = bytes.length * 8;
|
|
33
|
+
const padLen = (((bytes.length + 8) >>> 6) + 1) << 6;
|
|
34
|
+
const buf = new Uint8Array(padLen);
|
|
35
|
+
buf.set(bytes);
|
|
36
|
+
buf[bytes.length] = 0x80;
|
|
37
|
+
const view = new DataView(buf.buffer);
|
|
38
|
+
view.setUint32(padLen - 8, Math.floor(bitLen / 0x100000000), false);
|
|
39
|
+
view.setUint32(padLen - 4, bitLen >>> 0, false);
|
|
40
|
+
|
|
41
|
+
let h0 = 0x6a09e667;
|
|
42
|
+
let h1 = 0xbb67ae85;
|
|
43
|
+
let h2 = 0x3c6ef372;
|
|
44
|
+
let h3 = 0xa54ff53a;
|
|
45
|
+
let h4 = 0x510e527f;
|
|
46
|
+
let h5 = 0x9b05688c;
|
|
47
|
+
let h6 = 0x1f83d9ab;
|
|
48
|
+
let h7 = 0x5be0cd19;
|
|
49
|
+
|
|
50
|
+
const w = new Uint32Array(64);
|
|
51
|
+
for (let offset = 0; offset < padLen; offset += 64) {
|
|
52
|
+
for (let i = 0; i < 16; i++) {
|
|
53
|
+
w[i] = view.getUint32(offset + i * 4, false);
|
|
54
|
+
}
|
|
55
|
+
for (let i = 16; i < 64; i++) {
|
|
56
|
+
const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
|
|
57
|
+
const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
|
|
58
|
+
w[i] = (w[i - 16] + s0 + w[i - 7] + s1) >>> 0;
|
|
59
|
+
}
|
|
60
|
+
let a = h0;
|
|
61
|
+
let b = h1;
|
|
62
|
+
let c = h2;
|
|
63
|
+
let d = h3;
|
|
64
|
+
let e = h4;
|
|
65
|
+
let f = h5;
|
|
66
|
+
let g = h6;
|
|
67
|
+
let h = h7;
|
|
68
|
+
|
|
69
|
+
for (let i = 0; i < 64; i++) {
|
|
70
|
+
const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
|
|
71
|
+
const ch = (e & f) ^ (~e & g);
|
|
72
|
+
const temp1 = (h + S1 + ch + SHA256_K[i] + w[i]) >>> 0;
|
|
73
|
+
const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
|
|
74
|
+
const maj = (a & b) ^ (a & c) ^ (b & c);
|
|
75
|
+
const temp2 = (S0 + maj) >>> 0;
|
|
76
|
+
|
|
77
|
+
h = g;
|
|
78
|
+
g = f;
|
|
79
|
+
f = e;
|
|
80
|
+
e = (d + temp1) >>> 0;
|
|
81
|
+
d = c;
|
|
82
|
+
c = b;
|
|
83
|
+
b = a;
|
|
84
|
+
a = (temp1 + temp2) >>> 0;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
h0 = (h0 + a) >>> 0;
|
|
88
|
+
h1 = (h1 + b) >>> 0;
|
|
89
|
+
h2 = (h2 + c) >>> 0;
|
|
90
|
+
h3 = (h3 + d) >>> 0;
|
|
91
|
+
h4 = (h4 + e) >>> 0;
|
|
92
|
+
h5 = (h5 + f) >>> 0;
|
|
93
|
+
h6 = (h6 + g) >>> 0;
|
|
94
|
+
h7 = (h7 + h) >>> 0;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return [h0, h1, h2, h3, h4, h5, h6, h7]
|
|
98
|
+
.map((v) => v.toString(16).padStart(8, '0'))
|
|
99
|
+
.join('');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function escapePdfText(str = '') {
|
|
103
|
+
return String(str)
|
|
104
|
+
.replace(/[^\x20-\x7E]/g, ' ')
|
|
105
|
+
.replace(/\\/g, '\\\\')
|
|
106
|
+
.replace(/\(/g, '\\(')
|
|
107
|
+
.replace(/\)/g, '\\)');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
function escapeHtml(str = '') {
|
|
111
|
+
return String(str)
|
|
112
|
+
.replace(/&/g, '&')
|
|
113
|
+
.replace(/</g, '<')
|
|
114
|
+
.replace(/>/g, '>')
|
|
115
|
+
.replace(/"/g, '"');
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Builds a standards-compliant PDF 1.4 single-page corporate expense receipt (`Uint8Array`).
|
|
120
|
+
*/
|
|
121
|
+
export function buildPdf14Document(lines = [], meta = {}) {
|
|
122
|
+
const streamOps = [
|
|
123
|
+
'BT',
|
|
124
|
+
'/F1 15 Tf',
|
|
125
|
+
'48 750 Td',
|
|
126
|
+
`(${escapePdfText(meta.title || 'HIGHBEAM PRO - CORPORATE FINOPS EXPENSE RECEIPT')}) Tj`,
|
|
127
|
+
'/F2 9 Tf',
|
|
128
|
+
'0 -18 Td',
|
|
129
|
+
`(${escapePdfText(`Receipt ID: ${meta.receiptId || 'AH-RCPT'} | Issued: ${meta.issuedAt || ''}`)}) Tj`,
|
|
130
|
+
'0 -14 Td',
|
|
131
|
+
`(${escapePdfText(`SHA-256 Audit Digest: ${meta.sha256 || ''}`)}) Tj`,
|
|
132
|
+
'0 -18 Td',
|
|
133
|
+
'(' + '-'.repeat(86) + ') Tj'
|
|
134
|
+
];
|
|
135
|
+
|
|
136
|
+
for (const line of lines) {
|
|
137
|
+
streamOps.push('0 -14 Td');
|
|
138
|
+
streamOps.push(`(${escapePdfText(line)}) Tj`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
streamOps.push('ET');
|
|
142
|
+
const contentStream = streamOps.join('\n');
|
|
143
|
+
|
|
144
|
+
const objects = [
|
|
145
|
+
'1 0 obj\n<< /Type /Catalog /Pages 2 0 R >>\nendobj\n',
|
|
146
|
+
'2 0 obj\n<< /Type /Pages /Kids [3 0 R] /Count 1 >>\nendobj\n',
|
|
147
|
+
'3 0 obj\n<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Resources << /Font << /F1 4 0 R /F2 5 0 R >> >> /Contents 6 0 R >>\nendobj\n',
|
|
148
|
+
'4 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica-Bold >>\nendobj\n',
|
|
149
|
+
'5 0 obj\n<< /Type /Font /Subtype /Type1 /BaseFont /Courier >>\nendobj\n',
|
|
150
|
+
`6 0 obj\n<< /Length ${contentStream.length} >>\nstream\n${contentStream}\nendstream\nendobj\n`
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
let pdf = '%PDF-1.4\n';
|
|
154
|
+
const offsets = [0];
|
|
155
|
+
for (const obj of objects) {
|
|
156
|
+
offsets.push(pdf.length);
|
|
157
|
+
pdf += obj;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const xrefStart = pdf.length;
|
|
161
|
+
pdf += `xref\n0 ${objects.length + 1}\n`;
|
|
162
|
+
pdf += '0000000000 65535 f \n';
|
|
163
|
+
for (let i = 1; i <= objects.length; i++) {
|
|
164
|
+
pdf += `${String(offsets[i]).padStart(10, '0')} 00000 n \n`;
|
|
165
|
+
}
|
|
166
|
+
pdf += `trailer\n<< /Size ${objects.length + 1} /Root 1 0 R >>\nstartxref\n${xrefStart}\n%%EOF\n`;
|
|
167
|
+
|
|
168
|
+
return new TextEncoder().encode(pdf);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Generates a signed Corporate Expense Receipt in PDF 1.4 (`Uint8Array`), HTML, and Markdown formats.
|
|
173
|
+
*
|
|
174
|
+
* @param {object} summary - Output from `getSavedQuotaReceiptSummary`
|
|
175
|
+
* @param {object} [options={}]
|
|
176
|
+
*/
|
|
177
|
+
export function generateCorporateExpenseReceipt(summary = {}, options = {}) {
|
|
178
|
+
const issuedAt = options.issuedAt || new Date().toISOString();
|
|
179
|
+
const periodLabel = options.periodLabel || issuedAt.slice(0, 7);
|
|
180
|
+
const employeeEmail = options.email || summary.email || 'developer@workspace.local';
|
|
181
|
+
const tierLabel = options.tierLabel || 'Highbeam Pro ($15.00/mo)';
|
|
182
|
+
const subscriptionCostUSD = Number(options.subscriptionCostUSD ?? 15.0);
|
|
183
|
+
|
|
184
|
+
const sessionSavedTokens = Number(summary.sessionSavedTokens || 0);
|
|
185
|
+
const sessionSavedUSD = Number(summary.sessionSavedUSD || 0);
|
|
186
|
+
const workspace30dSavedTokens = Number(summary.workspace30dSavedTokens || sessionSavedTokens || 0);
|
|
187
|
+
const workspace30dSavedUSD = Number(summary.workspace30dSavedUSD || sessionSavedUSD || 0);
|
|
188
|
+
const netRoiMultiplier =
|
|
189
|
+
subscriptionCostUSD > 0
|
|
190
|
+
? Number((Math.max(workspace30dSavedUSD, sessionSavedUSD) / subscriptionCostUSD).toFixed(1))
|
|
191
|
+
: 0;
|
|
192
|
+
const estimatedGpuWhSaved = Number(((workspace30dSavedTokens / 1_000_000) * 650).toFixed(1));
|
|
193
|
+
const estimatedCo2GramsSaved = Number(((estimatedGpuWhSaved / 1000) * 380).toFixed(1));
|
|
194
|
+
|
|
195
|
+
const byCategory = summary.byCategory || {};
|
|
196
|
+
const cacheTok = Number(byCategory.DET_09_PREFIX_FENCE?.savedTokens || 0);
|
|
197
|
+
const cacheUsd = Number(byCategory.DET_09_PREFIX_FENCE?.savedUSD || 0);
|
|
198
|
+
const sanitizeTok = Number(byCategory.POST_TOOL_USE_SANITIZER?.savedTokens || 0);
|
|
199
|
+
const sanitizeUsd = Number(byCategory.POST_TOOL_USE_SANITIZER?.savedUSD || 0);
|
|
200
|
+
const readTok = Number(byCategory.DET_11_BOUNDED_READ?.savedTokens || 0);
|
|
201
|
+
const readUsd = Number(byCategory.DET_11_BOUNDED_READ?.savedUSD || 0);
|
|
202
|
+
const breakerTok = Number(byCategory.CIRCUIT_BREAKER_INTERVENTION?.savedTokens || 0);
|
|
203
|
+
const breakerUsd = Number(byCategory.CIRCUIT_BREAKER_INTERVENTION?.savedUSD || 0);
|
|
204
|
+
|
|
205
|
+
const canonicalAuditString = JSON.stringify({
|
|
206
|
+
v: 1,
|
|
207
|
+
periodLabel,
|
|
208
|
+
employeeEmail,
|
|
209
|
+
sessionSavedTokens,
|
|
210
|
+
sessionSavedUSD: Number(sessionSavedUSD.toFixed(4)),
|
|
211
|
+
workspace30dSavedTokens,
|
|
212
|
+
workspace30dSavedUSD: Number(workspace30dSavedUSD.toFixed(4))
|
|
213
|
+
});
|
|
214
|
+
const sha256 = sha256HexPure(canonicalAuditString);
|
|
215
|
+
const receiptId = `AH-${periodLabel.replace('-', '')}-${sha256.slice(0, 8).toUpperCase()}`;
|
|
216
|
+
|
|
217
|
+
const pdfLines = [
|
|
218
|
+
`Billing Period : ${periodLabel}`,
|
|
219
|
+
`Account / Seat : ${employeeEmail}`,
|
|
220
|
+
`Product Tier : ${tierLabel}`,
|
|
221
|
+
`Subscription Fee : $${subscriptionCostUSD.toFixed(2)} USD`,
|
|
222
|
+
'-'.repeat(86),
|
|
223
|
+
'ITEMIZED LLM API & KV-CACHE SAVINGS (30-DAY VERIFIED LEDGER)',
|
|
224
|
+
'-'.repeat(86),
|
|
225
|
+
`1. KV-Cache Prefix Fences (DET-09) : +${cacheTok.toLocaleString('en-US').padStart(10)} tok ($${cacheUsd.toFixed(2)})`,
|
|
226
|
+
`2. Lossless CLI Output Offloader : +${sanitizeTok.toLocaleString('en-US').padStart(10)} tok ($${sanitizeUsd.toFixed(2)})`,
|
|
227
|
+
`3. Bounded File Reads (DET-11) : +${readTok.toLocaleString('en-US').padStart(10)} tok ($${readUsd.toFixed(2)})`,
|
|
228
|
+
`4. Circuit Breaker Interventions : +${breakerTok.toLocaleString('en-US').padStart(10)} tok ($${breakerUsd.toFixed(2)})`,
|
|
229
|
+
'-'.repeat(86),
|
|
230
|
+
`Current Session Verified Savings : +${sessionSavedTokens.toLocaleString('en-US')} tokens ($${sessionSavedUSD.toFixed(2)} USD)`,
|
|
231
|
+
`30-Day Cumulative Workspace Savings : +${workspace30dSavedTokens.toLocaleString('en-US')} tokens ($${workspace30dSavedUSD.toFixed(2)} USD)`,
|
|
232
|
+
`Net FinOps ROI vs. $${subscriptionCostUSD.toFixed(2)}/mo License : ${netRoiMultiplier}x Return on Investment`,
|
|
233
|
+
`GreenOps GPU Compute Energy Avoided : ~${estimatedGpuWhSaved} Wh (~${estimatedCo2GramsSaved}g CO2e Scope 3)`,
|
|
234
|
+
'-'.repeat(86),
|
|
235
|
+
'Zero-Trust Privacy Attestation: Computed 100% locally from token counters.',
|
|
236
|
+
'Zero prompt text, source code, or PII transmitted off-device.',
|
|
237
|
+
`Verification Hash (SHA-256): ${sha256}`
|
|
238
|
+
];
|
|
239
|
+
|
|
240
|
+
const pdfBytes = buildPdf14Document(pdfLines, {
|
|
241
|
+
title: 'HIGHBEAM PRO - CORPORATE FINOPS EXPENSE RECEIPT',
|
|
242
|
+
receiptId,
|
|
243
|
+
issuedAt,
|
|
244
|
+
sha256
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const markdown = [
|
|
248
|
+
`# ๐งพ Highbeam Pro โ Corporate FinOps Expense Receipt`,
|
|
249
|
+
``,
|
|
250
|
+
`| Field | Value |`,
|
|
251
|
+
`| :--- | :--- |`,
|
|
252
|
+
`| **Receipt ID** | \`${receiptId}\` |`,
|
|
253
|
+
`| **Billing Period** | \`${periodLabel}\` |`,
|
|
254
|
+
`| **Seat / Account** | \`${employeeEmail}\` |`,
|
|
255
|
+
`| **Product Tier** | ${tierLabel} |`,
|
|
256
|
+
`| **Subscription Cost** | **$${subscriptionCostUSD.toFixed(2)} USD** |`,
|
|
257
|
+
`| **30-Day Verified API Savings** | **+$${workspace30dSavedUSD.toFixed(2)} USD** (+${workspace30dSavedTokens.toLocaleString('en-US')} tokens) |`,
|
|
258
|
+
`| **Current Session Savings** | **+$${sessionSavedUSD.toFixed(2)} USD** (+${sessionSavedTokens.toLocaleString('en-US')} tokens) |`,
|
|
259
|
+
`| **Net FinOps ROI** | **${netRoiMultiplier}x** |`,
|
|
260
|
+
`| **GreenOps Compute Energy Avoided** | **~${estimatedGpuWhSaved} Wh** (~${estimatedCo2GramsSaved}g COโe Scope 3) |`,
|
|
261
|
+
``,
|
|
262
|
+
`## Itemized Savings Breakdown`,
|
|
263
|
+
`| Guard Category | Tokens Saved | USD Saved |`,
|
|
264
|
+
`| :--- | ---: | ---: |`,
|
|
265
|
+
`| KV-Cache Prefix Fences (\`DET-09\`) | +${cacheTok.toLocaleString('en-US')} | $${cacheUsd.toFixed(2)} |`,
|
|
266
|
+
`| Lossless CLI Output Offloader | +${sanitizeTok.toLocaleString('en-US')} | $${sanitizeUsd.toFixed(2)} |`,
|
|
267
|
+
`| Bounded File Reads (\`DET-11\`) | +${readTok.toLocaleString('en-US')} | $${readUsd.toFixed(2)} |`,
|
|
268
|
+
`| Circuit Breaker Interventions | +${breakerTok.toLocaleString('en-US')} | $${breakerUsd.toFixed(2)} |`,
|
|
269
|
+
``,
|
|
270
|
+
`> **SHA-256 Audit Digest**: \`${sha256}\` `,
|
|
271
|
+
`> *Computed 100% locally via Highbeam (\`ctxtop receipt\`) with zero network calls or PII.*`
|
|
272
|
+
].join('\n');
|
|
273
|
+
|
|
274
|
+
const html = `<!DOCTYPE html>
|
|
275
|
+
<html lang="en">
|
|
276
|
+
<head>
|
|
277
|
+
<meta charset="utf-8" />
|
|
278
|
+
<title>Expense Receipt ${escapeHtml(receiptId)} โ Highbeam Pro</title>
|
|
279
|
+
<style>
|
|
280
|
+
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; color: #111827; max-width: 720px; margin: 32px auto; padding: 24px; border: 1px solid #e5e7eb; border-radius: 8px; }
|
|
281
|
+
h1 { font-size: 18px; margin: 0 0 4px 0; }
|
|
282
|
+
.meta { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: #4b5563; margin-bottom: 16px; }
|
|
283
|
+
table { width: 100%; border-collapse: collapse; margin: 16px 0; font-size: 13px; font-variant-numeric: tabular-nums; }
|
|
284
|
+
th, td { padding: 8px 10px; border-bottom: 1px solid #e5e7eb; text-align: left; }
|
|
285
|
+
th { background: #f9fafb; font-weight: 600; }
|
|
286
|
+
td.num, th.num { text-align: right; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
|
|
287
|
+
. digest { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; color: #6b7280; margin-top: 16px; word-break: break-all; }
|
|
288
|
+
@media print { body { border: none; margin: 0; } }
|
|
289
|
+
</style>
|
|
290
|
+
</head>
|
|
291
|
+
<body>
|
|
292
|
+
<h1>Highbeam Pro โ Corporate FinOps Expense Receipt</h1>
|
|
293
|
+
<div class="meta">Receipt ID: ${escapeHtml(receiptId)} · Period: ${escapeHtml(periodLabel)} · Issued: ${escapeHtml(issuedAt)}</div>
|
|
294
|
+
<table>
|
|
295
|
+
<tr><th>Product Tier</th><td>${escapeHtml(tierLabel)}</td><th class="num">Subscription Cost</th><td class="num">$${subscriptionCostUSD.toFixed(2)} USD</td></tr>
|
|
296
|
+
<tr><th>30-Day Verified Savings</th><td class="num">+${workspace30dSavedTokens.toLocaleString('en-US')} tok</td><th class="num">30-Day USD Saved</th><td class="num">+$${workspace30dSavedUSD.toFixed(2)} USD</td></tr>
|
|
297
|
+
<tr><th>Session Verified Savings</th><td class="num">+${sessionSavedTokens.toLocaleString('en-US')} tok</td><th class="num">Net FinOps ROI</th><td class="num">${netRoiMultiplier}x</td></tr>
|
|
298
|
+
<tr><th>GreenOps Energy Avoided</th><td class="num">~${estimatedGpuWhSaved} Wh</td><th class="num">Scope 3 CO2e Avoided</th><td class="num">~${estimatedCo2GramsSaved}g CO2e</td></tr>
|
|
299
|
+
</table>
|
|
300
|
+
<table>
|
|
301
|
+
<thead><tr><th>Guard Category</th><th class="num">Tokens Saved</th><th class="num">USD Saved</th></tr></thead>
|
|
302
|
+
<tbody>
|
|
303
|
+
<tr><td>KV-Cache Prefix Fences (DET-09)</td><td class="num">+${cacheTok.toLocaleString('en-US')}</td><td class="num">$${cacheUsd.toFixed(2)}</td></tr>
|
|
304
|
+
<tr><td>Lossless CLI Output Offloader</td><td class="num">+${sanitizeTok.toLocaleString('en-US')}</td><td class="num">$${sanitizeUsd.toFixed(2)}</td></tr>
|
|
305
|
+
<tr><td>Bounded File Reads (DET-11)</td><td class="num">+${readTok.toLocaleString('en-US')}</td><td class="num">$${readUsd.toFixed(2)}</td></tr>
|
|
306
|
+
<tr><td>Circuit Breaker Interventions</td><td class="num">+${breakerTok.toLocaleString('en-US')}</td><td class="num">$${breakerUsd.toFixed(2)}</td></tr>
|
|
307
|
+
</tbody>
|
|
308
|
+
</table>
|
|
309
|
+
<div class="digest">SHA-256 Audit Digest: ${escapeHtml(sha256)}</div>
|
|
310
|
+
</body>
|
|
311
|
+
</html>`;
|
|
312
|
+
|
|
313
|
+
return {
|
|
314
|
+
receiptId,
|
|
315
|
+
issuedAt,
|
|
316
|
+
periodLabel,
|
|
317
|
+
sha256,
|
|
318
|
+
sessionSavedTokens,
|
|
319
|
+
sessionSavedUSD,
|
|
320
|
+
workspace30dSavedTokens,
|
|
321
|
+
workspace30dSavedUSD,
|
|
322
|
+
netRoiMultiplier,
|
|
323
|
+
estimatedGpuWhSaved,
|
|
324
|
+
estimatedCo2GramsSaved,
|
|
325
|
+
pdfBytes,
|
|
326
|
+
markdown,
|
|
327
|
+
html
|
|
328
|
+
};
|
|
329
|
+
}
|