@sriinnu/fiche 0.18.0-bootstrap.0 → 0.19.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 +134 -0
- package/LICENSE +22 -0
- package/NOTICE +13 -0
- package/PRIVACY.md +41 -0
- package/README.md +289 -0
- package/SECURITY.md +53 -0
- package/assets/JETBRAINS_MONO_LICENSE.txt +89 -0
- package/assets/SPLEEN_LICENSE.txt +24 -0
- package/assets/UNIFONT_LICENSE.txt +476 -0
- package/assets/fiche-logo-dark.svg +61 -0
- package/assets/fiche-logo-light.svg +61 -0
- package/assets/fiche-logo.svg +71 -0
- package/assets/fiche-mark.svg +48 -0
- package/bin/cli.js +7 -0
- package/bin/mcp.js +8 -0
- package/dist/calibrate.d.ts +30 -0
- package/dist/calibrate.js +588 -0
- package/dist/core/anthropic-vision.d.ts +28 -0
- package/dist/core/anthropic-vision.js +34 -0
- package/dist/core/applicability.d.ts +63 -0
- package/dist/core/applicability.js +151 -0
- package/dist/core/atlas-gray-jbmono10.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono10.js +64 -0
- package/dist/core/atlas-gray-jbmono12.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono12.js +64 -0
- package/dist/core/atlas-gray-jbmono14.d.ts +26 -0
- package/dist/core/atlas-gray-jbmono14.js +64 -0
- package/dist/core/atlas-gray.d.ts +26 -0
- package/dist/core/atlas-gray.js +64 -0
- package/dist/core/atlas-jbmono10.d.ts +34 -0
- package/dist/core/atlas-jbmono10.js +71 -0
- package/dist/core/atlas-jbmono12.d.ts +34 -0
- package/dist/core/atlas-jbmono12.js +71 -0
- package/dist/core/atlas-jbmono14.d.ts +34 -0
- package/dist/core/atlas-jbmono14.js +71 -0
- package/dist/core/atlas.d.ts +34 -0
- package/dist/core/atlas.js +71 -0
- package/dist/core/baseline.d.ts +121 -0
- package/dist/core/baseline.js +200 -0
- package/dist/core/calibration.d.ts +86 -0
- package/dist/core/calibration.js +547 -0
- package/dist/core/casi-delta.d.ts +63 -0
- package/dist/core/casi-delta.js +1271 -0
- package/dist/core/claude-model-profiles.d.ts +53 -0
- package/dist/core/claude-model-profiles.js +160 -0
- package/dist/core/client-tool-round.d.ts +31 -0
- package/dist/core/client-tool-round.js +58 -0
- package/dist/core/codex-cache.d.ts +20 -0
- package/dist/core/codex-cache.js +59 -0
- package/dist/core/context-mmu.d.ts +350 -0
- package/dist/core/context-mmu.js +3028 -0
- package/dist/core/cpt-fit.d.ts +70 -0
- package/dist/core/cpt-fit.js +197 -0
- package/dist/core/env.d.ts +8 -0
- package/dist/core/env.js +15 -0
- package/dist/core/export.d.ts +127 -0
- package/dist/core/export.js +385 -0
- package/dist/core/factsheet.d.ts +77 -0
- package/dist/core/factsheet.js +494 -0
- package/dist/core/gemini-model-profiles.d.ts +20 -0
- package/dist/core/gemini-model-profiles.js +134 -0
- package/dist/core/google.d.ts +76 -0
- package/dist/core/google.js +1380 -0
- package/dist/core/gpt-model-profiles.d.ts +218 -0
- package/dist/core/gpt-model-profiles.js +570 -0
- package/dist/core/gpt-tokens.d.ts +14 -0
- package/dist/core/gpt-tokens.js +165 -0
- package/dist/core/history.d.ts +197 -0
- package/dist/core/history.js +1003 -0
- package/dist/core/index.d.ts +16 -0
- package/dist/core/index.js +16 -0
- package/dist/core/library.d.ts +80 -0
- package/dist/core/library.js +131 -0
- package/dist/core/measurement.d.ts +34 -0
- package/dist/core/measurement.js +280 -0
- package/dist/core/messages-chat-bridge.d.ts +23 -0
- package/dist/core/messages-chat-bridge.js +747 -0
- package/dist/core/messages-responses-bridge.d.ts +11 -0
- package/dist/core/messages-responses-bridge.js +893 -0
- package/dist/core/openai-history.d.ts +184 -0
- package/dist/core/openai-history.js +1279 -0
- package/dist/core/openai-savings.d.ts +57 -0
- package/dist/core/openai-savings.js +116 -0
- package/dist/core/openai.d.ts +68 -0
- package/dist/core/openai.js +2523 -0
- package/dist/core/pin.d.ts +199 -0
- package/dist/core/pin.js +1353 -0
- package/dist/core/png.d.ts +14 -0
- package/dist/core/png.js +188 -0
- package/dist/core/prefix-tape.d.ts +102 -0
- package/dist/core/prefix-tape.js +315 -0
- package/dist/core/price-id.d.ts +10 -0
- package/dist/core/price-id.js +26 -0
- package/dist/core/prices.generated.d.ts +36 -0
- package/dist/core/prices.generated.js +369 -0
- package/dist/core/pricing.d.ts +134 -0
- package/dist/core/pricing.js +335 -0
- package/dist/core/profile-base.d.ts +30 -0
- package/dist/core/profile-base.js +67 -0
- package/dist/core/provider-router.d.ts +51 -0
- package/dist/core/provider-router.js +121 -0
- package/dist/core/proxy.d.ts +252 -0
- package/dist/core/proxy.js +2929 -0
- package/dist/core/reader-tier.d.ts +24 -0
- package/dist/core/reader-tier.js +118 -0
- package/dist/core/render.d.ts +280 -0
- package/dist/core/render.js +1302 -0
- package/dist/core/request-guard.d.ts +31 -0
- package/dist/core/request-guard.js +126 -0
- package/dist/core/request-transform.d.ts +43 -0
- package/dist/core/request-transform.js +173 -0
- package/dist/core/schema-compiler.d.ts +64 -0
- package/dist/core/schema-compiler.js +1696 -0
- package/dist/core/schema-strip.d.ts +29 -0
- package/dist/core/schema-strip.js +173 -0
- package/dist/core/session-state.d.ts +219 -0
- package/dist/core/session-state.js +929 -0
- package/dist/core/stage-plugins.d.ts +45 -0
- package/dist/core/stage-plugins.js +205 -0
- package/dist/core/text-history.d.ts +74 -0
- package/dist/core/text-history.js +612 -0
- package/dist/core/text-hygiene.d.ts +160 -0
- package/dist/core/text-hygiene.js +2259 -0
- package/dist/core/toon.d.ts +29 -0
- package/dist/core/toon.js +425 -0
- package/dist/core/tracker.d.ts +223 -0
- package/dist/core/tracker.js +290 -0
- package/dist/core/transform.d.ts +589 -0
- package/dist/core/transform.js +3153 -0
- package/dist/core/types.d.ts +87 -0
- package/dist/core/types.js +8 -0
- package/dist/core/upstream-hosts.d.ts +127 -0
- package/dist/core/upstream-hosts.js +398 -0
- package/dist/core/vision-cost.d.ts +63 -0
- package/dist/core/vision-cost.js +137 -0
- package/dist/dashboard/fragments.d.ts +87 -0
- package/dist/dashboard/fragments.js +1515 -0
- package/dist/dashboard/types.d.ts +217 -0
- package/dist/dashboard/types.js +3 -0
- package/dist/dashboard/vendor.d.ts +3 -0
- package/dist/dashboard/vendor.js +6 -0
- package/dist/dashboard.d.ts +372 -0
- package/dist/dashboard.js +2731 -0
- package/dist/export-collect.d.ts +36 -0
- package/dist/export-collect.js +59 -0
- package/dist/file-tracker.d.ts +42 -0
- package/dist/file-tracker.js +234 -0
- package/dist/kosha-sync.d.ts +74 -0
- package/dist/kosha-sync.js +272 -0
- package/dist/launchd-socket.d.ts +22 -0
- package/dist/launchd-socket.js +210 -0
- package/dist/mcp/cli.d.ts +2 -0
- package/dist/mcp/cli.js +55 -0
- package/dist/mcp/server.d.ts +48 -0
- package/dist/mcp/server.js +987 -0
- package/dist/mmu-store.d.ts +31 -0
- package/dist/mmu-store.js +179 -0
- package/dist/node-fetch.d.ts +22 -0
- package/dist/node-fetch.js +489 -0
- package/dist/node.d.ts +9 -0
- package/dist/node.js +244396 -0
- package/dist/request-decoding.d.ts +30 -0
- package/dist/request-decoding.js +85 -0
- package/dist/sessions.d.ts +179 -0
- package/dist/sessions.js +837 -0
- package/dist/stats.d.ts +114 -0
- package/dist/stats.js +439 -0
- package/dist/turn-report.d.ts +65 -0
- package/dist/turn-report.js +724 -0
- package/dist/warp/app-launch.d.ts +26 -0
- package/dist/warp/app-launch.js +155 -0
- package/dist/warp/ca.d.ts +74 -0
- package/dist/warp/ca.js +416 -0
- package/dist/warp/connect.d.ts +42 -0
- package/dist/warp/connect.js +686 -0
- package/dist/warp/der.d.ts +57 -0
- package/dist/warp/der.js +151 -0
- package/dist/warp/index.d.ts +79 -0
- package/dist/warp/index.js +650 -0
- package/dist/warp/resolve.d.ts +12 -0
- package/dist/warp/resolve.js +49 -0
- package/dist/warp/route.d.ts +71 -0
- package/dist/warp/route.js +134 -0
- package/dist/worker.d.ts +71 -0
- package/dist/worker.js +174 -0
- package/docs/SECURITY_MODEL.md +84 -0
- package/package.json +115 -3
- package/plugins/fiche-companion/README.md +42 -0
- package/plugins/fiche-companion/assets/logo.svg +48 -0
- package/plugins/fiche-companion/hooks.json +11 -0
- package/plugins/fiche-companion/mcp_config.json +8 -0
- package/plugins/fiche-companion/plugin.json +18 -0
- package/plugins/fiche-companion/scripts/claude-statusline.mjs +35 -0
- package/plugins/fiche-companion/scripts/post-invocation.mjs +122 -0
- package/plugins/fiche-companion/scripts/resolve-turn-report.mjs +132 -0
- package/plugins/fiche-companion/skills/fiche/SKILL.md +61 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adaptive chars-per-token (CPT) — the fit.
|
|
3
|
+
*
|
|
4
|
+
* The profitability gate compares `imageTokens` (exact, from pixel area) against
|
|
5
|
+
* `textTokens = chars / CPT`. CPT was a hand-tuned constant per call site (4 for
|
|
6
|
+
* reminders/tool_results, 2.0 for slab/history). Measured logs showed the
|
|
7
|
+
* real marginal density is ~1.5 for dense content, so the constant under-counted
|
|
8
|
+
* text cost and biased the gate toward passthrough — leaving savings on the table.
|
|
9
|
+
*
|
|
10
|
+
* This module learns CPT from the events inkling already logs. Model: a request's
|
|
11
|
+
* TEXT token cost decomposes into a per-bucket marginal rate times that bucket's
|
|
12
|
+
* char count,
|
|
13
|
+
*
|
|
14
|
+
* textTokens ≈ Σ_b α_b · chars_b with CPT_b = 1 / α_b
|
|
15
|
+
*
|
|
16
|
+
* where `textTokens` is the observed `baseline_tokens` (a free count_tokens probe
|
|
17
|
+
* on the ORIGINAL uncompressed body) minus the image cost, priced off Anthropic's
|
|
18
|
+
* 28×28-patch grid. Samples are built offline from events.jsonl by the caller
|
|
19
|
+
* (see the CHARS_PER_TOKEN provenance note in transform.ts). Solved by ordinary
|
|
20
|
+
* least squares
|
|
21
|
+
* via the normal equations `α = (XᵀX)⁻¹ Xᵀy`. XᵀX is at most 6×6, so a hand-rolled
|
|
22
|
+
* Gauss-Jordan inverse is microseconds and needs no dependency.
|
|
23
|
+
*
|
|
24
|
+
* Pure math — no `fs`, no `process`, no `Buffer`. Safe to import from the Workers
|
|
25
|
+
* build.
|
|
26
|
+
*
|
|
27
|
+
* The fit is deliberately conservative: every guard below fails a bucket CLOSED
|
|
28
|
+
* (back to the baked constant) rather than open. A wrong learned CPT would make
|
|
29
|
+
* the gate image unprofitably, which costs real money; a missing one just returns
|
|
30
|
+
* today's behavior.
|
|
31
|
+
*/
|
|
32
|
+
import type { BucketName } from './transform.js';
|
|
33
|
+
/** Column order for the design matrix. Stable + explicit so a fit is reproducible. */
|
|
34
|
+
export declare const CPT_BUCKETS: readonly BucketName[];
|
|
35
|
+
/** Minimum events before any fit is trusted. Below this, a slope is noise. */
|
|
36
|
+
export declare const MIN_SAMPLES = 20;
|
|
37
|
+
/** A bucket must actually appear this many times to get its own column;
|
|
38
|
+
* an all-but-empty column makes XᵀX singular and the slope meaningless. */
|
|
39
|
+
export declare const MIN_BUCKET_PRESENCE = 8;
|
|
40
|
+
/** Plausible CPT band. Real content runs ~1.2 (dense JSON) to ~4 (prose);
|
|
41
|
+
* anything outside this is a fit artifact, not a measurement. */
|
|
42
|
+
export declare const CPT_PLAUSIBLE_MIN = 0.8;
|
|
43
|
+
export declare const CPT_PLAUSIBLE_MAX = 6;
|
|
44
|
+
/** Reject the whole fit above this pivot ratio — the buckets are too collinear
|
|
45
|
+
* to separate (e.g. reminders that always grow with tool_results). */
|
|
46
|
+
export declare const MAX_CONDITION = 100000000;
|
|
47
|
+
/** One request's regressors + target. */
|
|
48
|
+
export interface CptSample {
|
|
49
|
+
/** Pre-compression TEXT chars per bucket (the logged `bucket_chars`). */
|
|
50
|
+
bucketChars: Partial<Record<BucketName, number>>;
|
|
51
|
+
/** Observed text tokens: `baseline_tokens − imagePixels / 750`. */
|
|
52
|
+
textTokens: number;
|
|
53
|
+
}
|
|
54
|
+
export interface CptFitResult {
|
|
55
|
+
/** Learned chars-per-token, per bucket. Absent = use the baked default. */
|
|
56
|
+
cpt: Partial<Record<BucketName, number>>;
|
|
57
|
+
nSamples: number;
|
|
58
|
+
/** Why each bucket was NOT learned. Surfaced so a null result is explainable. */
|
|
59
|
+
rejected: Partial<Record<BucketName, string>>;
|
|
60
|
+
/** Pivot ratio of XᵀX. Higher = less trustworthy; > MAX_CONDITION rejects. */
|
|
61
|
+
conditionEstimate: number;
|
|
62
|
+
/** Buckets that got a column in this fit. */
|
|
63
|
+
active: BucketName[];
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Fit per-bucket CPT from samples. Never throws; a fit it cannot trust comes back
|
|
67
|
+
* with an empty `cpt` and a populated `rejected` explaining why.
|
|
68
|
+
*/
|
|
69
|
+
export declare function fitCpt(samples: readonly CptSample[]): CptFitResult;
|
|
70
|
+
//# sourceMappingURL=cpt-fit.d.ts.map
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Adaptive chars-per-token (CPT) — the fit.
|
|
3
|
+
*
|
|
4
|
+
* The profitability gate compares `imageTokens` (exact, from pixel area) against
|
|
5
|
+
* `textTokens = chars / CPT`. CPT was a hand-tuned constant per call site (4 for
|
|
6
|
+
* reminders/tool_results, 2.0 for slab/history). Measured logs showed the
|
|
7
|
+
* real marginal density is ~1.5 for dense content, so the constant under-counted
|
|
8
|
+
* text cost and biased the gate toward passthrough — leaving savings on the table.
|
|
9
|
+
*
|
|
10
|
+
* This module learns CPT from the events inkling already logs. Model: a request's
|
|
11
|
+
* TEXT token cost decomposes into a per-bucket marginal rate times that bucket's
|
|
12
|
+
* char count,
|
|
13
|
+
*
|
|
14
|
+
* textTokens ≈ Σ_b α_b · chars_b with CPT_b = 1 / α_b
|
|
15
|
+
*
|
|
16
|
+
* where `textTokens` is the observed `baseline_tokens` (a free count_tokens probe
|
|
17
|
+
* on the ORIGINAL uncompressed body) minus the image cost, priced off Anthropic's
|
|
18
|
+
* 28×28-patch grid. Samples are built offline from events.jsonl by the caller
|
|
19
|
+
* (see the CHARS_PER_TOKEN provenance note in transform.ts). Solved by ordinary
|
|
20
|
+
* least squares
|
|
21
|
+
* via the normal equations `α = (XᵀX)⁻¹ Xᵀy`. XᵀX is at most 6×6, so a hand-rolled
|
|
22
|
+
* Gauss-Jordan inverse is microseconds and needs no dependency.
|
|
23
|
+
*
|
|
24
|
+
* Pure math — no `fs`, no `process`, no `Buffer`. Safe to import from the Workers
|
|
25
|
+
* build.
|
|
26
|
+
*
|
|
27
|
+
* The fit is deliberately conservative: every guard below fails a bucket CLOSED
|
|
28
|
+
* (back to the baked constant) rather than open. A wrong learned CPT would make
|
|
29
|
+
* the gate image unprofitably, which costs real money; a missing one just returns
|
|
30
|
+
* today's behavior.
|
|
31
|
+
*/
|
|
32
|
+
/** Column order for the design matrix. Stable + explicit so a fit is reproducible. */
|
|
33
|
+
export const CPT_BUCKETS = [
|
|
34
|
+
'static_slab',
|
|
35
|
+
'reminder',
|
|
36
|
+
'tool_result_json',
|
|
37
|
+
'tool_result_log',
|
|
38
|
+
'tool_result_prose',
|
|
39
|
+
'history',
|
|
40
|
+
];
|
|
41
|
+
/** Minimum events before any fit is trusted. Below this, a slope is noise. */
|
|
42
|
+
export const MIN_SAMPLES = 20;
|
|
43
|
+
/** A bucket must actually appear this many times to get its own column;
|
|
44
|
+
* an all-but-empty column makes XᵀX singular and the slope meaningless. */
|
|
45
|
+
export const MIN_BUCKET_PRESENCE = 8;
|
|
46
|
+
/** Plausible CPT band. Real content runs ~1.2 (dense JSON) to ~4 (prose);
|
|
47
|
+
* anything outside this is a fit artifact, not a measurement. */
|
|
48
|
+
export const CPT_PLAUSIBLE_MIN = 0.8;
|
|
49
|
+
export const CPT_PLAUSIBLE_MAX = 6.0;
|
|
50
|
+
/** Reject the whole fit above this pivot ratio — the buckets are too collinear
|
|
51
|
+
* to separate (e.g. reminders that always grow with tool_results). */
|
|
52
|
+
export const MAX_CONDITION = 1e8;
|
|
53
|
+
/**
|
|
54
|
+
* Invert a square matrix by Gauss-Jordan elimination with partial pivoting.
|
|
55
|
+
* Returns `null` when the matrix is singular. `condition` is the ratio of the
|
|
56
|
+
* largest to smallest pivot — a cheap stand-in for the true condition number,
|
|
57
|
+
* good enough to detect "these columns are not independent".
|
|
58
|
+
*/
|
|
59
|
+
function invertMatrix(src) {
|
|
60
|
+
const n = src.length;
|
|
61
|
+
if (n === 0)
|
|
62
|
+
return null;
|
|
63
|
+
// Augment [A | I] and reduce the left half to identity.
|
|
64
|
+
const a = src.map((row, i) => {
|
|
65
|
+
const aug = new Array(2 * n).fill(0);
|
|
66
|
+
for (let j = 0; j < n; j++)
|
|
67
|
+
aug[j] = row[j] ?? 0;
|
|
68
|
+
aug[n + i] = 1;
|
|
69
|
+
return aug;
|
|
70
|
+
});
|
|
71
|
+
let minPivot = Infinity;
|
|
72
|
+
let maxPivot = 0;
|
|
73
|
+
for (let col = 0; col < n; col++) {
|
|
74
|
+
let best = col;
|
|
75
|
+
for (let r = col + 1; r < n; r++) {
|
|
76
|
+
if (Math.abs(a[r][col]) > Math.abs(a[best][col]))
|
|
77
|
+
best = r;
|
|
78
|
+
}
|
|
79
|
+
const pivot = Math.abs(a[best][col]);
|
|
80
|
+
if (!Number.isFinite(pivot) || pivot === 0)
|
|
81
|
+
return null;
|
|
82
|
+
minPivot = Math.min(minPivot, pivot);
|
|
83
|
+
maxPivot = Math.max(maxPivot, pivot);
|
|
84
|
+
if (best !== col) {
|
|
85
|
+
const tmp = a[best];
|
|
86
|
+
a[best] = a[col];
|
|
87
|
+
a[col] = tmp;
|
|
88
|
+
}
|
|
89
|
+
const d = a[col][col];
|
|
90
|
+
for (let j = 0; j < 2 * n; j++)
|
|
91
|
+
a[col][j] = a[col][j] / d;
|
|
92
|
+
for (let r = 0; r < n; r++) {
|
|
93
|
+
if (r === col)
|
|
94
|
+
continue;
|
|
95
|
+
const f = a[r][col];
|
|
96
|
+
if (f === 0)
|
|
97
|
+
continue;
|
|
98
|
+
for (let j = 0; j < 2 * n; j++)
|
|
99
|
+
a[r][j] = a[r][j] - f * a[col][j];
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const inv = a.map((row) => row.slice(n));
|
|
103
|
+
return { inv, condition: minPivot > 0 ? maxPivot / minPivot : Infinity };
|
|
104
|
+
}
|
|
105
|
+
/** Reject every bucket with one reason. Used for the whole-fit bailouts. */
|
|
106
|
+
function rejectAll(reason) {
|
|
107
|
+
const out = {};
|
|
108
|
+
for (const b of CPT_BUCKETS)
|
|
109
|
+
out[b] = reason;
|
|
110
|
+
return out;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Fit per-bucket CPT from samples. Never throws; a fit it cannot trust comes back
|
|
114
|
+
* with an empty `cpt` and a populated `rejected` explaining why.
|
|
115
|
+
*/
|
|
116
|
+
export function fitCpt(samples) {
|
|
117
|
+
const n = samples.length;
|
|
118
|
+
if (n < MIN_SAMPLES) {
|
|
119
|
+
return {
|
|
120
|
+
cpt: {},
|
|
121
|
+
nSamples: n,
|
|
122
|
+
rejected: rejectAll(`n=${n} < MIN_SAMPLES=${MIN_SAMPLES}`),
|
|
123
|
+
conditionEstimate: Infinity,
|
|
124
|
+
active: [],
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
const rejected = {};
|
|
128
|
+
// Only give a column to buckets that actually show up. An all-zero (or
|
|
129
|
+
// nearly-all-zero) column is exactly collinear with nothing and makes the
|
|
130
|
+
// normal equations singular.
|
|
131
|
+
const active = [];
|
|
132
|
+
for (const b of CPT_BUCKETS) {
|
|
133
|
+
let present = 0;
|
|
134
|
+
for (const s of samples)
|
|
135
|
+
if ((s.bucketChars[b] ?? 0) > 0)
|
|
136
|
+
present++;
|
|
137
|
+
if (present < MIN_BUCKET_PRESENCE) {
|
|
138
|
+
rejected[b] = `present in ${present}/${n} samples < ${MIN_BUCKET_PRESENCE}`;
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
active.push(b);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
if (active.length === 0) {
|
|
145
|
+
return { cpt: {}, nSamples: n, rejected, conditionEstimate: Infinity, active };
|
|
146
|
+
}
|
|
147
|
+
// Accumulate XᵀX and Xᵀy in one pass.
|
|
148
|
+
const k = active.length;
|
|
149
|
+
const xtx = Array.from({ length: k }, () => new Array(k).fill(0));
|
|
150
|
+
const xty = new Array(k).fill(0);
|
|
151
|
+
for (const s of samples) {
|
|
152
|
+
if (!Number.isFinite(s.textTokens))
|
|
153
|
+
continue;
|
|
154
|
+
const row = new Array(k);
|
|
155
|
+
for (let i = 0; i < k; i++)
|
|
156
|
+
row[i] = s.bucketChars[active[i]] ?? 0;
|
|
157
|
+
for (let i = 0; i < k; i++) {
|
|
158
|
+
const ri = row[i];
|
|
159
|
+
xty[i] = xty[i] + ri * s.textTokens;
|
|
160
|
+
for (let j = 0; j < k; j++)
|
|
161
|
+
xtx[i][j] = xtx[i][j] + ri * row[j];
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
const inverted = invertMatrix(xtx);
|
|
165
|
+
if (!inverted) {
|
|
166
|
+
for (const b of active)
|
|
167
|
+
rejected[b] = 'singular XᵀX (collinear buckets)';
|
|
168
|
+
return { cpt: {}, nSamples: n, rejected, conditionEstimate: Infinity, active };
|
|
169
|
+
}
|
|
170
|
+
if (inverted.condition > MAX_CONDITION) {
|
|
171
|
+
for (const b of active) {
|
|
172
|
+
rejected[b] = `ill-conditioned (${inverted.condition.toExponential(1)} > ${MAX_CONDITION.toExponential(0)})`;
|
|
173
|
+
}
|
|
174
|
+
return { cpt: {}, nSamples: n, rejected, conditionEstimate: inverted.condition, active };
|
|
175
|
+
}
|
|
176
|
+
// α = (XᵀX)⁻¹ Xᵀy, then CPT = 1/α with a plausibility band.
|
|
177
|
+
const cpt = {};
|
|
178
|
+
for (let i = 0; i < k; i++) {
|
|
179
|
+
const bucket = active[i];
|
|
180
|
+
let alpha = 0;
|
|
181
|
+
for (let j = 0; j < k; j++)
|
|
182
|
+
alpha += inverted.inv[i][j] * xty[j];
|
|
183
|
+
if (!Number.isFinite(alpha) || alpha <= 0) {
|
|
184
|
+
rejected[bucket] = `non-positive slope (${Number.isFinite(alpha) ? alpha.toExponential(2) : 'NaN'})`;
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const value = 1 / alpha;
|
|
188
|
+
if (value < CPT_PLAUSIBLE_MIN || value > CPT_PLAUSIBLE_MAX) {
|
|
189
|
+
rejected[bucket] =
|
|
190
|
+
`cpt ${value.toFixed(2)} outside [${CPT_PLAUSIBLE_MIN}, ${CPT_PLAUSIBLE_MAX}]`;
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
cpt[bucket] = value;
|
|
194
|
+
}
|
|
195
|
+
return { cpt, nSamples: n, rejected, conditionEstimate: inverted.condition, active };
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=cpt-fit.js.map
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workers-safe brand environment variable lookup.
|
|
3
|
+
* Prefers `FICHE_<SUFFIX>` and falls back to legacy `INKLING_<SUFFIX>` so
|
|
4
|
+
* existing deployments and test harnesses keep working without breakage.
|
|
5
|
+
* Zero `node:` imports.
|
|
6
|
+
*/
|
|
7
|
+
export declare function readBrandEnv(suffix: string): string | undefined;
|
|
8
|
+
//# sourceMappingURL=env.d.ts.map
|
package/dist/core/env.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Workers-safe brand environment variable lookup.
|
|
3
|
+
* Prefers `FICHE_<SUFFIX>` and falls back to legacy `INKLING_<SUFFIX>` so
|
|
4
|
+
* existing deployments and test harnesses keep working without breakage.
|
|
5
|
+
* Zero `node:` imports.
|
|
6
|
+
*/
|
|
7
|
+
export function readBrandEnv(suffix) {
|
|
8
|
+
if (typeof process === 'undefined' || !process.env)
|
|
9
|
+
return undefined;
|
|
10
|
+
const ficheVal = process.env[`FICHE_${suffix}`];
|
|
11
|
+
if (ficheVal !== undefined)
|
|
12
|
+
return ficheVal;
|
|
13
|
+
return process.env[`INKLING_${suffix}`];
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=env.js.map
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core logic for `inkling export` — renders a source text to PNG pages, extracts
|
|
3
|
+
* a verbatim factsheet, builds a manifest and paste-ready prompt, and returns a
|
|
4
|
+
* token-cost report + list of artifacts to write.
|
|
5
|
+
*
|
|
6
|
+
* Pure-ish: no argv, no stdout, no process.exit, no fs calls — all I/O is
|
|
7
|
+
* delegated to the thin CLI runner in src/node.ts.
|
|
8
|
+
*/
|
|
9
|
+
export declare const DEFAULT_EXPORT_MODEL = "claude-sonnet-4-5";
|
|
10
|
+
/** Default column width (312 cols, the spaced 5x8 Claude profile). */
|
|
11
|
+
export declare const DEFAULT_EXPORT_COLS: number;
|
|
12
|
+
/**
|
|
13
|
+
* Match a relative file path against a glob pattern.
|
|
14
|
+
* Supported wildcards: `*` (non-separator), `**` (any including `/`), `?` (single non-sep).
|
|
15
|
+
* When the pattern contains no `/`, matching is against the basename only
|
|
16
|
+
* (e.g. `*.ts` matches `src/foo.ts`).
|
|
17
|
+
*/
|
|
18
|
+
export declare function matchGlob(pattern: string, filePath: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Decide whether a relative file path should be included given include/exclude glob lists.
|
|
21
|
+
* - Exclude patterns are checked first; any match → exclude.
|
|
22
|
+
* - If include patterns are given, at least one must match.
|
|
23
|
+
* - If no include patterns are given, everything passes (unless excluded).
|
|
24
|
+
*/
|
|
25
|
+
export declare function shouldIncludeFile(filePath: string, include: string[], exclude: string[]): boolean;
|
|
26
|
+
export interface ExportParsed {
|
|
27
|
+
targets: string[];
|
|
28
|
+
include: string[];
|
|
29
|
+
exclude: string[];
|
|
30
|
+
git: boolean;
|
|
31
|
+
diff: string | undefined;
|
|
32
|
+
stdin: boolean;
|
|
33
|
+
cols: number;
|
|
34
|
+
out: string;
|
|
35
|
+
model: string;
|
|
36
|
+
json: boolean;
|
|
37
|
+
open: boolean;
|
|
38
|
+
}
|
|
39
|
+
export type ExportArgvResult = {
|
|
40
|
+
kind: 'opts';
|
|
41
|
+
parsed: ExportParsed;
|
|
42
|
+
} | {
|
|
43
|
+
kind: 'help';
|
|
44
|
+
} | {
|
|
45
|
+
kind: 'error';
|
|
46
|
+
message: string;
|
|
47
|
+
};
|
|
48
|
+
/**
|
|
49
|
+
* Parse the argv array for the `export` subcommand.
|
|
50
|
+
* Returns a discriminated union so the caller (node.ts) decides
|
|
51
|
+
* whether to exit, print help, or proceed.
|
|
52
|
+
*
|
|
53
|
+
* @param defaultOut Base output directory (default: $TMPDIR or /tmp).
|
|
54
|
+
*/
|
|
55
|
+
export declare function parseExportArgv(argv: string[], defaultOut?: string): ExportArgvResult;
|
|
56
|
+
/**
|
|
57
|
+
* Per-image vision-token cost for a rendered PNG at the given pixel dimensions.
|
|
58
|
+
*
|
|
59
|
+
* - **Claude / Anthropic models** (`model.startsWith('claude')` or
|
|
60
|
+
* `model.includes('anthropic')`): Anthropic's documented 28-px patch model
|
|
61
|
+
* `⌈w/28⌉×⌈h/28⌉` (tier-aware downscale), via `visionTokensForModel`. This is
|
|
62
|
+
* the exact provider cost — no gate margin — for reporting.
|
|
63
|
+
* - **OpenAI-shaped models**: delegates to the same exact-model router as the
|
|
64
|
+
* proxy (Grok measured pixel rate; GPT tile/patch rate).
|
|
65
|
+
*/
|
|
66
|
+
export declare function exportImageTokens(model: string, width: number, height: number): number;
|
|
67
|
+
export interface ExportTokenReport {
|
|
68
|
+
textTokens: number;
|
|
69
|
+
imageTokens: number;
|
|
70
|
+
percentSaved: number;
|
|
71
|
+
/** Count of unique identifier strings extracted across all pages (paths, SHAs, ids, …).
|
|
72
|
+
* This is a count of items, not an LLM token count. */
|
|
73
|
+
factsheetItemCount: number;
|
|
74
|
+
/** Identifiers extracted across all pages that did not fit within the 64-item
|
|
75
|
+
* factsheet budget. Zero when all extracted items were kept. */
|
|
76
|
+
factsheetDropped: number;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Estimate text vs image token cost for `sourceText` without rendering.
|
|
80
|
+
* Uses the same formula as the internal gate:
|
|
81
|
+
* stripW = 2·PAD_X + cols·CELL_W
|
|
82
|
+
* imageTokens = estimateImageCount(text, cols) × exportImageTokens(model, stripW, MAX_HEIGHT_PX)
|
|
83
|
+
* textTokens = sourceText.length / REPORT_CHARS_PER_TOKEN
|
|
84
|
+
*
|
|
85
|
+
* `exportImageTokens` routes to the Anthropic 28-px patch model (⌈w/28⌉×⌈h/28⌉)
|
|
86
|
+
* for Claude models, and to the GPT tile-pricing formula for GPT/o-series models.
|
|
87
|
+
*
|
|
88
|
+
* `factsheetItemCount` is the number of unique precision-critical identifier strings
|
|
89
|
+
* extracted across all pages (paths, SHAs, ids, …); it is NOT an LLM token count.
|
|
90
|
+
* `factsheetDropped` is the count of extracted identifiers that did not fit within
|
|
91
|
+
* the 64-item per-export budget.
|
|
92
|
+
*/
|
|
93
|
+
export declare function computeTokenReport(sourceText: string, cols: number, model: string): ExportTokenReport;
|
|
94
|
+
export interface ExportPageInfo {
|
|
95
|
+
filename: string;
|
|
96
|
+
bytes: number;
|
|
97
|
+
width: number;
|
|
98
|
+
height: number;
|
|
99
|
+
}
|
|
100
|
+
export interface ExportManifest {
|
|
101
|
+
sourceChars: number;
|
|
102
|
+
files: string[];
|
|
103
|
+
pages: ExportPageInfo[];
|
|
104
|
+
cols: number;
|
|
105
|
+
model: string;
|
|
106
|
+
generatedAt: string;
|
|
107
|
+
tokenReport: ExportTokenReport;
|
|
108
|
+
}
|
|
109
|
+
export interface ExportArtifact {
|
|
110
|
+
filename: string;
|
|
111
|
+
data: Uint8Array;
|
|
112
|
+
}
|
|
113
|
+
export interface ExportResult {
|
|
114
|
+
manifest: ExportManifest;
|
|
115
|
+
artifacts: ExportArtifact[];
|
|
116
|
+
}
|
|
117
|
+
export interface ExportCoreOptions {
|
|
118
|
+
/** Relative file paths listed in the manifest and prompt (display only). */
|
|
119
|
+
sourceFiles: string[];
|
|
120
|
+
cols: number;
|
|
121
|
+
model: string;
|
|
122
|
+
}
|
|
123
|
+
export declare function buildPromptText(pageCount: number, factsheet: string, files: string[], droppedItems?: number): string;
|
|
124
|
+
/** 8-char hex hash of the first 512 chars + length of `text`. */
|
|
125
|
+
export declare function sourceShortHash(text: string): string;
|
|
126
|
+
export declare function runExportCore(sourceText: string, opts: ExportCoreOptions): Promise<ExportResult>;
|
|
127
|
+
//# sourceMappingURL=export.d.ts.map
|