@sriinnu/fiche 0.0.0-stage → 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.
Files changed (196) hide show
  1. package/CHANGELOG.md +134 -0
  2. package/LICENSE +22 -0
  3. package/NOTICE +13 -0
  4. package/PRIVACY.md +41 -0
  5. package/README.md +288 -2
  6. package/SECURITY.md +53 -0
  7. package/assets/JETBRAINS_MONO_LICENSE.txt +89 -0
  8. package/assets/SPLEEN_LICENSE.txt +24 -0
  9. package/assets/UNIFONT_LICENSE.txt +476 -0
  10. package/assets/fiche-logo-dark.svg +61 -0
  11. package/assets/fiche-logo-light.svg +61 -0
  12. package/assets/fiche-logo.svg +71 -0
  13. package/assets/fiche-mark.svg +48 -0
  14. package/bin/cli.js +7 -0
  15. package/bin/mcp.js +8 -0
  16. package/dist/calibrate.d.ts +30 -0
  17. package/dist/calibrate.js +588 -0
  18. package/dist/core/anthropic-vision.d.ts +28 -0
  19. package/dist/core/anthropic-vision.js +34 -0
  20. package/dist/core/applicability.d.ts +63 -0
  21. package/dist/core/applicability.js +151 -0
  22. package/dist/core/atlas-gray-jbmono10.d.ts +26 -0
  23. package/dist/core/atlas-gray-jbmono10.js +64 -0
  24. package/dist/core/atlas-gray-jbmono12.d.ts +26 -0
  25. package/dist/core/atlas-gray-jbmono12.js +64 -0
  26. package/dist/core/atlas-gray-jbmono14.d.ts +26 -0
  27. package/dist/core/atlas-gray-jbmono14.js +64 -0
  28. package/dist/core/atlas-gray.d.ts +26 -0
  29. package/dist/core/atlas-gray.js +64 -0
  30. package/dist/core/atlas-jbmono10.d.ts +34 -0
  31. package/dist/core/atlas-jbmono10.js +71 -0
  32. package/dist/core/atlas-jbmono12.d.ts +34 -0
  33. package/dist/core/atlas-jbmono12.js +71 -0
  34. package/dist/core/atlas-jbmono14.d.ts +34 -0
  35. package/dist/core/atlas-jbmono14.js +71 -0
  36. package/dist/core/atlas.d.ts +34 -0
  37. package/dist/core/atlas.js +71 -0
  38. package/dist/core/baseline.d.ts +121 -0
  39. package/dist/core/baseline.js +200 -0
  40. package/dist/core/calibration.d.ts +86 -0
  41. package/dist/core/calibration.js +547 -0
  42. package/dist/core/casi-delta.d.ts +63 -0
  43. package/dist/core/casi-delta.js +1271 -0
  44. package/dist/core/claude-model-profiles.d.ts +53 -0
  45. package/dist/core/claude-model-profiles.js +160 -0
  46. package/dist/core/client-tool-round.d.ts +31 -0
  47. package/dist/core/client-tool-round.js +58 -0
  48. package/dist/core/codex-cache.d.ts +20 -0
  49. package/dist/core/codex-cache.js +59 -0
  50. package/dist/core/context-mmu.d.ts +350 -0
  51. package/dist/core/context-mmu.js +3028 -0
  52. package/dist/core/cpt-fit.d.ts +70 -0
  53. package/dist/core/cpt-fit.js +197 -0
  54. package/dist/core/env.d.ts +8 -0
  55. package/dist/core/env.js +15 -0
  56. package/dist/core/export.d.ts +127 -0
  57. package/dist/core/export.js +385 -0
  58. package/dist/core/factsheet.d.ts +77 -0
  59. package/dist/core/factsheet.js +494 -0
  60. package/dist/core/gemini-model-profiles.d.ts +20 -0
  61. package/dist/core/gemini-model-profiles.js +134 -0
  62. package/dist/core/google.d.ts +76 -0
  63. package/dist/core/google.js +1380 -0
  64. package/dist/core/gpt-model-profiles.d.ts +218 -0
  65. package/dist/core/gpt-model-profiles.js +570 -0
  66. package/dist/core/gpt-tokens.d.ts +14 -0
  67. package/dist/core/gpt-tokens.js +165 -0
  68. package/dist/core/history.d.ts +197 -0
  69. package/dist/core/history.js +1003 -0
  70. package/dist/core/index.d.ts +16 -0
  71. package/dist/core/index.js +16 -0
  72. package/dist/core/library.d.ts +80 -0
  73. package/dist/core/library.js +131 -0
  74. package/dist/core/measurement.d.ts +34 -0
  75. package/dist/core/measurement.js +280 -0
  76. package/dist/core/messages-chat-bridge.d.ts +23 -0
  77. package/dist/core/messages-chat-bridge.js +747 -0
  78. package/dist/core/messages-responses-bridge.d.ts +11 -0
  79. package/dist/core/messages-responses-bridge.js +893 -0
  80. package/dist/core/openai-history.d.ts +184 -0
  81. package/dist/core/openai-history.js +1279 -0
  82. package/dist/core/openai-savings.d.ts +57 -0
  83. package/dist/core/openai-savings.js +116 -0
  84. package/dist/core/openai.d.ts +68 -0
  85. package/dist/core/openai.js +2523 -0
  86. package/dist/core/pin.d.ts +199 -0
  87. package/dist/core/pin.js +1353 -0
  88. package/dist/core/png.d.ts +14 -0
  89. package/dist/core/png.js +188 -0
  90. package/dist/core/prefix-tape.d.ts +102 -0
  91. package/dist/core/prefix-tape.js +315 -0
  92. package/dist/core/price-id.d.ts +10 -0
  93. package/dist/core/price-id.js +26 -0
  94. package/dist/core/prices.generated.d.ts +36 -0
  95. package/dist/core/prices.generated.js +369 -0
  96. package/dist/core/pricing.d.ts +134 -0
  97. package/dist/core/pricing.js +335 -0
  98. package/dist/core/profile-base.d.ts +30 -0
  99. package/dist/core/profile-base.js +67 -0
  100. package/dist/core/provider-router.d.ts +51 -0
  101. package/dist/core/provider-router.js +121 -0
  102. package/dist/core/proxy.d.ts +252 -0
  103. package/dist/core/proxy.js +2929 -0
  104. package/dist/core/reader-tier.d.ts +24 -0
  105. package/dist/core/reader-tier.js +118 -0
  106. package/dist/core/render.d.ts +280 -0
  107. package/dist/core/render.js +1302 -0
  108. package/dist/core/request-guard.d.ts +31 -0
  109. package/dist/core/request-guard.js +126 -0
  110. package/dist/core/request-transform.d.ts +43 -0
  111. package/dist/core/request-transform.js +173 -0
  112. package/dist/core/schema-compiler.d.ts +64 -0
  113. package/dist/core/schema-compiler.js +1696 -0
  114. package/dist/core/schema-strip.d.ts +29 -0
  115. package/dist/core/schema-strip.js +173 -0
  116. package/dist/core/session-state.d.ts +219 -0
  117. package/dist/core/session-state.js +929 -0
  118. package/dist/core/stage-plugins.d.ts +45 -0
  119. package/dist/core/stage-plugins.js +205 -0
  120. package/dist/core/text-history.d.ts +74 -0
  121. package/dist/core/text-history.js +612 -0
  122. package/dist/core/text-hygiene.d.ts +160 -0
  123. package/dist/core/text-hygiene.js +2259 -0
  124. package/dist/core/toon.d.ts +29 -0
  125. package/dist/core/toon.js +425 -0
  126. package/dist/core/tracker.d.ts +223 -0
  127. package/dist/core/tracker.js +290 -0
  128. package/dist/core/transform.d.ts +589 -0
  129. package/dist/core/transform.js +3153 -0
  130. package/dist/core/types.d.ts +87 -0
  131. package/dist/core/types.js +8 -0
  132. package/dist/core/upstream-hosts.d.ts +127 -0
  133. package/dist/core/upstream-hosts.js +398 -0
  134. package/dist/core/vision-cost.d.ts +63 -0
  135. package/dist/core/vision-cost.js +137 -0
  136. package/dist/dashboard/fragments.d.ts +87 -0
  137. package/dist/dashboard/fragments.js +1515 -0
  138. package/dist/dashboard/types.d.ts +217 -0
  139. package/dist/dashboard/types.js +3 -0
  140. package/dist/dashboard/vendor.d.ts +3 -0
  141. package/dist/dashboard/vendor.js +6 -0
  142. package/dist/dashboard.d.ts +372 -0
  143. package/dist/dashboard.js +2731 -0
  144. package/dist/export-collect.d.ts +36 -0
  145. package/dist/export-collect.js +59 -0
  146. package/dist/file-tracker.d.ts +42 -0
  147. package/dist/file-tracker.js +234 -0
  148. package/dist/kosha-sync.d.ts +74 -0
  149. package/dist/kosha-sync.js +272 -0
  150. package/dist/launchd-socket.d.ts +22 -0
  151. package/dist/launchd-socket.js +210 -0
  152. package/dist/mcp/cli.d.ts +2 -0
  153. package/dist/mcp/cli.js +55 -0
  154. package/dist/mcp/server.d.ts +48 -0
  155. package/dist/mcp/server.js +987 -0
  156. package/dist/mmu-store.d.ts +31 -0
  157. package/dist/mmu-store.js +179 -0
  158. package/dist/node-fetch.d.ts +22 -0
  159. package/dist/node-fetch.js +489 -0
  160. package/dist/node.d.ts +9 -0
  161. package/dist/node.js +244396 -0
  162. package/dist/request-decoding.d.ts +30 -0
  163. package/dist/request-decoding.js +85 -0
  164. package/dist/sessions.d.ts +179 -0
  165. package/dist/sessions.js +837 -0
  166. package/dist/stats.d.ts +114 -0
  167. package/dist/stats.js +439 -0
  168. package/dist/turn-report.d.ts +65 -0
  169. package/dist/turn-report.js +724 -0
  170. package/dist/warp/app-launch.d.ts +26 -0
  171. package/dist/warp/app-launch.js +155 -0
  172. package/dist/warp/ca.d.ts +74 -0
  173. package/dist/warp/ca.js +416 -0
  174. package/dist/warp/connect.d.ts +42 -0
  175. package/dist/warp/connect.js +686 -0
  176. package/dist/warp/der.d.ts +57 -0
  177. package/dist/warp/der.js +151 -0
  178. package/dist/warp/index.d.ts +79 -0
  179. package/dist/warp/index.js +650 -0
  180. package/dist/warp/resolve.d.ts +12 -0
  181. package/dist/warp/resolve.js +49 -0
  182. package/dist/warp/route.d.ts +71 -0
  183. package/dist/warp/route.js +134 -0
  184. package/dist/worker.d.ts +71 -0
  185. package/dist/worker.js +174 -0
  186. package/docs/SECURITY_MODEL.md +84 -0
  187. package/package.json +122 -4
  188. package/plugins/fiche-companion/README.md +42 -0
  189. package/plugins/fiche-companion/assets/logo.svg +48 -0
  190. package/plugins/fiche-companion/hooks.json +11 -0
  191. package/plugins/fiche-companion/mcp_config.json +8 -0
  192. package/plugins/fiche-companion/plugin.json +18 -0
  193. package/plugins/fiche-companion/scripts/claude-statusline.mjs +35 -0
  194. package/plugins/fiche-companion/scripts/post-invocation.mjs +122 -0
  195. package/plugins/fiche-companion/scripts/resolve-turn-report.mjs +132 -0
  196. package/plugins/fiche-companion/skills/fiche/SKILL.md +61 -0
@@ -0,0 +1,36 @@
1
+ /**
2
+ * Shared file-reading gate for `inkling export`. Every collection mode
3
+ * (directory walk, explicit single-file target, and `--git` untracked files)
4
+ * must apply the SAME three checks — include/exclude globs, a max size, and a
5
+ * binary sniff — so the untracked path can no longer read gigabyte or filtered
6
+ * files that directory mode would have skipped.
7
+ *
8
+ * Kept in its own import-safe module (no top-level side effects, unlike
9
+ * src/node.ts which starts the server on import) so it can be unit-tested
10
+ * directly. src/core/export.ts is deliberately fs-free, so the fs-touching gate
11
+ * lives here rather than there.
12
+ */
13
+ /** Files larger than this are skipped by export (1 MiB). A single export bundle
14
+ * is meant to be paste-sized; a multi-MB file is never intentional context and
15
+ * reading it fully into memory is a resource-safety hazard. */
16
+ export declare const MAX_FILE_BYTES = 1000000;
17
+ /** Returns true if `buf` looks like binary (contains a null byte in the first 512 bytes). */
18
+ export declare function looksLikeBinary(buf: Buffer): boolean;
19
+ /** Outcome of trying to read one file for export. `excluded` is a normal filter
20
+ * hit (callers stay quiet); the other non-ok kinds are worth a warning. */
21
+ export type ExportReadResult = {
22
+ readonly kind: 'ok';
23
+ readonly content: string;
24
+ } | {
25
+ readonly kind: 'excluded' | 'oversized' | 'binary' | 'inaccessible';
26
+ };
27
+ /**
28
+ * Read a single text file for export if it passes every gate, in this order:
29
+ * 1. include/exclude globs (relative path)
30
+ * 2. size <= MAX_FILE_BYTES
31
+ * 3. not binary
32
+ * Returns the utf8 content on success, or the reason it was skipped. Pure of
33
+ * logging so each caller can decide whether a skip is noteworthy.
34
+ */
35
+ export declare function readExportTextFile(fullPath: string, relPath: string, include: string[], exclude: string[]): ExportReadResult;
36
+ //# sourceMappingURL=export-collect.d.ts.map
@@ -0,0 +1,59 @@
1
+ /**
2
+ * Shared file-reading gate for `inkling export`. Every collection mode
3
+ * (directory walk, explicit single-file target, and `--git` untracked files)
4
+ * must apply the SAME three checks — include/exclude globs, a max size, and a
5
+ * binary sniff — so the untracked path can no longer read gigabyte or filtered
6
+ * files that directory mode would have skipped.
7
+ *
8
+ * Kept in its own import-safe module (no top-level side effects, unlike
9
+ * src/node.ts which starts the server on import) so it can be unit-tested
10
+ * directly. src/core/export.ts is deliberately fs-free, so the fs-touching gate
11
+ * lives here rather than there.
12
+ */
13
+ import * as fs from 'node:fs';
14
+ import { shouldIncludeFile } from './core/export.js';
15
+ /** Files larger than this are skipped by export (1 MiB). A single export bundle
16
+ * is meant to be paste-sized; a multi-MB file is never intentional context and
17
+ * reading it fully into memory is a resource-safety hazard. */
18
+ export const MAX_FILE_BYTES = 1_000_000;
19
+ /** Returns true if `buf` looks like binary (contains a null byte in the first 512 bytes). */
20
+ export function looksLikeBinary(buf) {
21
+ const check = Math.min(buf.byteLength, 512);
22
+ for (let i = 0; i < check; i++) {
23
+ if (buf[i] === 0)
24
+ return true;
25
+ }
26
+ return false;
27
+ }
28
+ /**
29
+ * Read a single text file for export if it passes every gate, in this order:
30
+ * 1. include/exclude globs (relative path)
31
+ * 2. size <= MAX_FILE_BYTES
32
+ * 3. not binary
33
+ * Returns the utf8 content on success, or the reason it was skipped. Pure of
34
+ * logging so each caller can decide whether a skip is noteworthy.
35
+ */
36
+ export function readExportTextFile(fullPath, relPath, include, exclude) {
37
+ if (!shouldIncludeFile(relPath, include, exclude))
38
+ return { kind: 'excluded' };
39
+ let stat;
40
+ try {
41
+ stat = fs.statSync(fullPath);
42
+ }
43
+ catch {
44
+ return { kind: 'inaccessible' };
45
+ }
46
+ if (stat.size > MAX_FILE_BYTES)
47
+ return { kind: 'oversized' };
48
+ let buf;
49
+ try {
50
+ buf = fs.readFileSync(fullPath);
51
+ }
52
+ catch {
53
+ return { kind: 'inaccessible' };
54
+ }
55
+ if (looksLikeBinary(buf))
56
+ return { kind: 'binary' };
57
+ return { kind: 'ok', content: buf.toString('utf8') };
58
+ }
59
+ //# sourceMappingURL=export-collect.js.map
@@ -0,0 +1,42 @@
1
+ import type { Tracker, TrackEvent } from './core/tracker.js';
2
+ import { type EventsFileCoordinator } from './sessions.js';
3
+ /**
4
+ * Append-only JSONL tracker with size-based rotation and prune coordination.
5
+ * One line per request.
6
+ *
7
+ * Rotation: when the current file exceeds MAX_FILE_BYTES (100 MB by default),
8
+ * it's renamed to `<path>.1` (overwriting any previous .1) and a fresh file
9
+ * is opened.
10
+ *
11
+ * Failures here NEVER propagate — the proxy must keep serving requests even
12
+ * if the disk is full or the path is unwritable.
13
+ */
14
+ export declare class FileTracker implements Tracker, EventsFileCoordinator {
15
+ private readonly filePath;
16
+ private fd;
17
+ private fdIno;
18
+ private fdDev;
19
+ private lastStatCheckAt;
20
+ private bytesWritten;
21
+ private brokenLogged;
22
+ private rewriting;
23
+ private closed;
24
+ private pendingDuringRewrite;
25
+ private pendingBytesDuringRewrite;
26
+ private readonly unregisterCoordinator;
27
+ private static readonly MAX_FILE_BYTES;
28
+ private static readonly STAT_CHECK_INTERVAL_MS;
29
+ private static readonly MAX_PENDING_REWRITE_LINES;
30
+ private static readonly MAX_PENDING_REWRITE_BYTES;
31
+ constructor(filePath: string);
32
+ private static writeAllSync;
33
+ private closeFd;
34
+ private ensureOpen;
35
+ private rotate;
36
+ private writeSerializedLine;
37
+ emit(ev: TrackEvent): void;
38
+ runDuringRewrite<T>(fn: (flushPending: (outFd: number, writeLine?: (outFd: number, line: string) => void) => void) => Promise<T>): Promise<T>;
39
+ flush(): void;
40
+ close(): void;
41
+ }
42
+ //# sourceMappingURL=file-tracker.d.ts.map
@@ -0,0 +1,234 @@
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+ import { registerEventsFileCoordinator, } from './sessions.js';
4
+ /**
5
+ * Append-only JSONL tracker with size-based rotation and prune coordination.
6
+ * One line per request.
7
+ *
8
+ * Rotation: when the current file exceeds MAX_FILE_BYTES (100 MB by default),
9
+ * it's renamed to `<path>.1` (overwriting any previous .1) and a fresh file
10
+ * is opened.
11
+ *
12
+ * Failures here NEVER propagate — the proxy must keep serving requests even
13
+ * if the disk is full or the path is unwritable.
14
+ */
15
+ export class FileTracker {
16
+ filePath;
17
+ fd = null;
18
+ fdIno = null;
19
+ fdDev = null;
20
+ lastStatCheckAt = 0;
21
+ bytesWritten = 0;
22
+ brokenLogged = false;
23
+ rewriting = false;
24
+ closed = false;
25
+ pendingDuringRewrite = [];
26
+ pendingBytesDuringRewrite = 0;
27
+ unregisterCoordinator;
28
+ static MAX_FILE_BYTES = 100 * 1024 * 1024;
29
+ static STAT_CHECK_INTERVAL_MS = 1000;
30
+ static MAX_PENDING_REWRITE_LINES = 10_000;
31
+ static MAX_PENDING_REWRITE_BYTES = 16 * 1024 * 1024;
32
+ constructor(filePath) {
33
+ this.filePath = filePath;
34
+ this.unregisterCoordinator = registerEventsFileCoordinator(this.filePath, this);
35
+ }
36
+ static writeAllSync(fd, buf) {
37
+ let offset = 0;
38
+ while (offset < buf.length) {
39
+ const written = fs.writeSync(fd, buf, offset, buf.length - offset);
40
+ if (written <= 0)
41
+ throw new Error('short write returned 0 bytes');
42
+ offset += written;
43
+ }
44
+ }
45
+ closeFd() {
46
+ if (this.fd != null) {
47
+ try {
48
+ fs.closeSync(this.fd);
49
+ }
50
+ catch {
51
+ /* ignore */
52
+ }
53
+ this.fd = null;
54
+ this.fdIno = null;
55
+ this.fdDev = null;
56
+ this.lastStatCheckAt = 0;
57
+ this.bytesWritten = 0;
58
+ }
59
+ }
60
+ ensureOpen() {
61
+ if (this.fd != null) {
62
+ const now = Date.now();
63
+ if (now - this.lastStatCheckAt < FileTracker.STAT_CHECK_INTERVAL_MS) {
64
+ return true;
65
+ }
66
+ this.lastStatCheckAt = now;
67
+ let needsReopen = false;
68
+ try {
69
+ const st = fs.statSync(this.filePath);
70
+ if (this.fdIno !== null && (st.ino !== this.fdIno || st.dev !== this.fdDev)) {
71
+ needsReopen = true;
72
+ }
73
+ }
74
+ catch {
75
+ // File unlinked or rotated externally — reopen the fresh path.
76
+ needsReopen = true;
77
+ }
78
+ if (!needsReopen)
79
+ return true;
80
+ this.closeFd();
81
+ }
82
+ try {
83
+ const parent = path.dirname(this.filePath);
84
+ const parentExists = fs.existsSync(parent);
85
+ fs.mkdirSync(parent, { recursive: true, mode: 0o700 });
86
+ if (!parentExists)
87
+ fs.chmodSync(parent, 0o700);
88
+ }
89
+ catch {
90
+ /* dir may already exist or be unmkable; openSync below will surface */
91
+ }
92
+ try {
93
+ const flags = fs.constants.O_WRONLY |
94
+ fs.constants.O_APPEND |
95
+ fs.constants.O_CREAT |
96
+ (fs.constants.O_NOFOLLOW ?? 0);
97
+ this.fd = fs.openSync(this.filePath, flags, 0o600);
98
+ const fst = fs.fstatSync(this.fd);
99
+ if (!fst.isFile()) {
100
+ this.closeFd();
101
+ return false;
102
+ }
103
+ fs.fchmodSync(this.fd, 0o600);
104
+ this.fdIno = fst.ino;
105
+ this.fdDev = fst.dev;
106
+ this.bytesWritten = fst.size;
107
+ this.lastStatCheckAt = Date.now();
108
+ return true;
109
+ }
110
+ catch (err) {
111
+ if (!this.brokenLogged) {
112
+ // eslint-disable-next-line no-console -- FileTracker error logging
113
+ console.error(`[fiche] FileTracker disabled — cannot open ${this.filePath}: ${err.message}`);
114
+ this.brokenLogged = true;
115
+ }
116
+ return false;
117
+ }
118
+ }
119
+ rotate() {
120
+ this.closeFd();
121
+ try {
122
+ fs.renameSync(this.filePath, this.filePath + '.1');
123
+ }
124
+ catch {
125
+ /* if rename fails (e.g. .1 locked) we'll just keep growing — better
126
+ than dropping events */
127
+ }
128
+ this.bytesWritten = 0;
129
+ }
130
+ writeSerializedLine(line) {
131
+ if (!this.ensureOpen())
132
+ return;
133
+ try {
134
+ const buf = Buffer.from(line, 'utf8');
135
+ FileTracker.writeAllSync(this.fd, buf);
136
+ this.bytesWritten += buf.length;
137
+ if (this.bytesWritten > FileTracker.MAX_FILE_BYTES)
138
+ this.rotate();
139
+ }
140
+ catch (err) {
141
+ if (!this.brokenLogged) {
142
+ console.error(`[fiche] FileTracker write failed: ${err.message}`);
143
+ this.brokenLogged = true;
144
+ }
145
+ }
146
+ }
147
+ emit(ev) {
148
+ if (this.closed)
149
+ return;
150
+ let line;
151
+ try {
152
+ line = JSON.stringify(ev) + '\n';
153
+ }
154
+ catch {
155
+ return;
156
+ }
157
+ if (this.rewriting) {
158
+ const lineBytes = Buffer.byteLength(line, 'utf8');
159
+ if (this.pendingDuringRewrite.length < FileTracker.MAX_PENDING_REWRITE_LINES &&
160
+ this.pendingBytesDuringRewrite + lineBytes <= FileTracker.MAX_PENDING_REWRITE_BYTES) {
161
+ this.pendingDuringRewrite.push(line);
162
+ this.pendingBytesDuringRewrite += lineBytes;
163
+ }
164
+ return;
165
+ }
166
+ this.writeSerializedLine(line);
167
+ }
168
+ async runDuringRewrite(fn) {
169
+ this.rewriting = true;
170
+ this.flush();
171
+ let flushedCount = 0;
172
+ try {
173
+ const result = await fn((outFd, writeLine) => {
174
+ if (this.pendingDuringRewrite.length <= flushedCount)
175
+ return;
176
+ const batch = this.pendingDuringRewrite.slice(flushedCount);
177
+ flushedCount += batch.length;
178
+ for (const line of batch) {
179
+ if (writeLine)
180
+ writeLine(outFd, line);
181
+ else
182
+ FileTracker.writeAllSync(outFd, Buffer.from(line, 'utf8'));
183
+ }
184
+ });
185
+ if (flushedCount > 0) {
186
+ const removed = this.pendingDuringRewrite.splice(0, flushedCount);
187
+ for (const line of removed) {
188
+ this.pendingBytesDuringRewrite = Math.max(0, this.pendingBytesDuringRewrite - Buffer.byteLength(line, 'utf8'));
189
+ }
190
+ }
191
+ return result;
192
+ }
193
+ finally {
194
+ this.closeFd();
195
+ this.rewriting = false;
196
+ if (this.pendingDuringRewrite.length > 0 && !this.closed) {
197
+ const remaining = this.pendingDuringRewrite.splice(0, this.pendingDuringRewrite.length);
198
+ this.pendingBytesDuringRewrite = 0;
199
+ for (const line of remaining) {
200
+ this.writeSerializedLine(line);
201
+ }
202
+ }
203
+ if (this.closed) {
204
+ this.pendingDuringRewrite.length = 0;
205
+ this.pendingBytesDuringRewrite = 0;
206
+ this.closeFd();
207
+ }
208
+ }
209
+ }
210
+ flush() {
211
+ if (this.fd != null) {
212
+ try {
213
+ fs.fsyncSync(this.fd);
214
+ }
215
+ catch {
216
+ /* ignore */
217
+ }
218
+ }
219
+ }
220
+ close() {
221
+ this.closed = true;
222
+ this.unregisterCoordinator();
223
+ if (this.fd != null) {
224
+ try {
225
+ fs.fsyncSync(this.fd);
226
+ }
227
+ catch {
228
+ /* ignore */
229
+ }
230
+ this.closeFd();
231
+ }
232
+ }
233
+ }
234
+ //# sourceMappingURL=file-tracker.js.map
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Syncs Fiche's `models` allowlist from a local Kosha model registry snapshot
3
+ * (`~/.kosha/registry.json`, `$KOSHA_REGISTRY`, `--file <path>`, or
4
+ * `kosha registry export` CLI fallback).
5
+ *
6
+ * Node-only module: never imported from `src/core/`.
7
+ */
8
+ import type { ProviderId } from './core/upstream-hosts.js';
9
+ export interface KoshaRegistryModel {
10
+ readonly id?: string;
11
+ readonly modelId?: string;
12
+ readonly provider?: string;
13
+ readonly providerId?: string;
14
+ readonly mode?: string | null;
15
+ readonly status?: string | null;
16
+ }
17
+ export interface KoshaRegistryProviderBlock {
18
+ readonly id?: string;
19
+ readonly providerId?: string;
20
+ readonly models?: readonly KoshaRegistryModel[];
21
+ }
22
+ export interface KoshaRegistrySnapshot {
23
+ readonly schemaVersion?: number | string;
24
+ readonly discoveredAt?: string;
25
+ readonly models?: readonly KoshaRegistryModel[];
26
+ readonly providers?: readonly KoshaRegistryProviderBlock[];
27
+ }
28
+ export interface KoshaExtractOptions {
29
+ readonly providers: readonly ProviderId[];
30
+ }
31
+ export interface KoshaExtractResult {
32
+ readonly bases: string[];
33
+ readonly scannedModels: number;
34
+ readonly matchedModels: number;
35
+ readonly providersMatched: ProviderId[];
36
+ }
37
+ /**
38
+ * Map Kosha provider identifiers (including `-cn` and billing/coding sub-feeds)
39
+ * to Fiche's canonical `ProviderId` values.
40
+ */
41
+ export declare const KOSHA_PROVIDER_TO_FICHE: Readonly<Record<string, ProviderId>>;
42
+ /**
43
+ * Normalize a raw Kosha model ID into a clean, deduplicatable model-base entry
44
+ * suitable for `FICHE_MODELS` / `~/.config/fiche/config.json` `models`.
45
+ * Returns `null` for aliases (`~...`), router endpoints (`/routers/`),
46
+ * placeholders (`auto`, `chat-latest`, `free`, `firerouter`, overly-broad `gpt`/`claude`),
47
+ * non-LLM modalities (embeddings, audio, voice, asr, ocr, image, video, moderation),
48
+ * or malformed IDs.
49
+ */
50
+ export declare function normalizeKoshaModelBase(rawId: string): string | null;
51
+ /**
52
+ * Extract normalized, deduplicated, prefix-collapsed chat model bases for the
53
+ * selected Fiche providers from a Kosha registry snapshot.
54
+ */
55
+ export declare function extractKoshaModelBases(snapshot: KoshaRegistrySnapshot, opts: KoshaExtractOptions): KoshaExtractResult;
56
+ /**
57
+ * Merge newly discovered Kosha model bases with Fiche's current `models` list.
58
+ * Preserves existing bases and skips any discovered base already covered by an
59
+ * existing shorter prefix (`deepseek` already matches `deepseek-coder`), unless
60
+ * `replace` is true.
61
+ */
62
+ export declare function mergeModelBases(existing: readonly string[], discovered: readonly string[], replace: boolean): {
63
+ merged: string[];
64
+ added: string[];
65
+ };
66
+ /**
67
+ * Load a Kosha registry snapshot from `--file`, `$KOSHA_REGISTRY`,
68
+ * `~/.kosha/registry.json`, or `kosha registry export`.
69
+ */
70
+ export declare function loadKoshaSnapshot(explicitFile?: string): {
71
+ snapshot: KoshaRegistrySnapshot;
72
+ source: string;
73
+ };
74
+ //# sourceMappingURL=kosha-sync.d.ts.map