opencode-tokscope 0.1.0 → 0.3.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/README.md +36 -78
- package/dist/tui.js +2005 -471
- package/package.json +23 -5
package/dist/tui.js
CHANGED
|
@@ -1,17 +1,616 @@
|
|
|
1
1
|
// src/tui.tsx
|
|
2
2
|
import {createComponent as _$createComponent5} from "@opentui/solid";
|
|
3
3
|
import {Plugin} from "@opencode/plugin/tui";
|
|
4
|
-
import {createSignal as
|
|
4
|
+
import {createSignal as createSignal5} from "solid-js";
|
|
5
5
|
|
|
6
|
-
// src/
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
6
|
+
// src/history/records.ts
|
|
7
|
+
var fieldsOf = function(value) {
|
|
8
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
9
|
+
return;
|
|
10
|
+
return value;
|
|
11
|
+
};
|
|
12
|
+
var stringValue = function(value) {
|
|
13
|
+
return typeof value === "string" ? value : undefined;
|
|
14
|
+
};
|
|
15
|
+
var nonEmptyString = function(value) {
|
|
16
|
+
return typeof value === "string" && value.length > 0 ? value : undefined;
|
|
17
|
+
};
|
|
18
|
+
var sessionIDFrom = function(value) {
|
|
19
|
+
return typeof value === "string" && SESSION_ID_PATTERN.test(value) ? value : undefined;
|
|
20
|
+
};
|
|
21
|
+
var finiteNumber = function(value) {
|
|
22
|
+
return typeof value === "number" && Number.isFinite(value) ? value : undefined;
|
|
23
|
+
};
|
|
24
|
+
var nonNegativeNumber = function(value) {
|
|
25
|
+
const parsed = finiteNumber(value);
|
|
26
|
+
return parsed !== undefined && parsed >= 0 ? parsed : undefined;
|
|
27
|
+
};
|
|
28
|
+
var optionalField = function(value, parse) {
|
|
29
|
+
if (value === undefined)
|
|
30
|
+
return { ok: true, value: undefined };
|
|
31
|
+
const parsed = parse(value);
|
|
32
|
+
return parsed === undefined ? { ok: false } : { ok: true, value: parsed };
|
|
33
|
+
};
|
|
34
|
+
var parseRows = function(raw, parse) {
|
|
35
|
+
if (!Array.isArray(raw))
|
|
36
|
+
return;
|
|
37
|
+
const rows = [];
|
|
38
|
+
for (const entry of raw) {
|
|
39
|
+
const row = parse(entry);
|
|
40
|
+
if (row === undefined)
|
|
41
|
+
return;
|
|
42
|
+
rows.push(row);
|
|
43
|
+
}
|
|
44
|
+
return rows;
|
|
45
|
+
};
|
|
46
|
+
var parseSample = function(raw) {
|
|
47
|
+
const source = fieldsOf(raw);
|
|
48
|
+
if (source === undefined)
|
|
49
|
+
return;
|
|
50
|
+
const providerID = nonEmptyString(source.providerID);
|
|
51
|
+
const modelID = nonEmptyString(source.modelID);
|
|
52
|
+
const variant = optionalField(source.variant, stringValue);
|
|
53
|
+
const completedAt = finiteNumber(source.completedAt);
|
|
54
|
+
const rate = nonNegativeNumber(source.rate);
|
|
55
|
+
if (providerID === undefined || modelID === undefined || !variant.ok || completedAt === undefined || rate === undefined) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
return { providerID, modelID, variant: variant.value, completedAt, rate };
|
|
59
|
+
};
|
|
60
|
+
var parseUnavailableSample = function(raw) {
|
|
61
|
+
const source = fieldsOf(raw);
|
|
62
|
+
if (source === undefined)
|
|
63
|
+
return;
|
|
64
|
+
const providerID = nonEmptyString(source.providerID);
|
|
65
|
+
const modelID = nonEmptyString(source.modelID);
|
|
66
|
+
const variant = optionalField(source.variant, stringValue);
|
|
67
|
+
const completedAt = finiteNumber(source.completedAt);
|
|
68
|
+
if (providerID === undefined || modelID === undefined || !variant.ok || completedAt === undefined) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
return { providerID, modelID, variant: variant.value, completedAt };
|
|
72
|
+
};
|
|
73
|
+
var parseSessionRecord = function(raw) {
|
|
74
|
+
const source = fieldsOf(raw);
|
|
75
|
+
if (source === undefined)
|
|
76
|
+
return;
|
|
77
|
+
const sessionID = sessionIDFrom(source.sessionID);
|
|
78
|
+
const directory = optionalField(source.directory, stringValue);
|
|
79
|
+
const createdAt = finiteNumber(source.createdAt);
|
|
80
|
+
const updatedAt = finiteNumber(source.updatedAt);
|
|
81
|
+
const scannedAtMs = finiteNumber(source.scannedAtMs);
|
|
82
|
+
const signature = stringValue(source.signature);
|
|
83
|
+
const fork = source.fork;
|
|
84
|
+
const complete = source.complete;
|
|
85
|
+
if (sessionID === undefined || !directory.ok || createdAt === undefined || updatedAt === undefined || scannedAtMs === undefined || signature === undefined || typeof fork !== "boolean" || typeof complete !== "boolean") {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const samples = parseRows(source.samples, parseSample);
|
|
89
|
+
const unavailable = parseRows(source.unavailable, parseUnavailableSample);
|
|
90
|
+
if (samples === undefined || unavailable === undefined)
|
|
91
|
+
return;
|
|
92
|
+
return {
|
|
93
|
+
sessionID,
|
|
94
|
+
directory: directory.value,
|
|
95
|
+
createdAt,
|
|
96
|
+
updatedAt,
|
|
97
|
+
fork,
|
|
98
|
+
signature,
|
|
99
|
+
scannedAtMs,
|
|
100
|
+
complete,
|
|
101
|
+
samples,
|
|
102
|
+
unavailable
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
var parseIndexEntry = function(raw) {
|
|
106
|
+
const source = fieldsOf(raw);
|
|
107
|
+
if (source === undefined)
|
|
108
|
+
return;
|
|
109
|
+
const sessionID = sessionIDFrom(source.sessionID);
|
|
110
|
+
const directory = optionalField(source.directory, stringValue);
|
|
111
|
+
const updatedAt = finiteNumber(source.updatedAt);
|
|
112
|
+
if (sessionID === undefined || !directory.ok || updatedAt === undefined)
|
|
113
|
+
return;
|
|
114
|
+
return { sessionID, directory: directory.value, updatedAt };
|
|
115
|
+
};
|
|
116
|
+
var parseComparisonRow = function(raw) {
|
|
117
|
+
const source = fieldsOf(raw);
|
|
118
|
+
if (source === undefined)
|
|
119
|
+
return;
|
|
120
|
+
const key = nonEmptyString(source.key);
|
|
121
|
+
const providerID = nonEmptyString(source.providerID);
|
|
122
|
+
const modelID = nonEmptyString(source.modelID);
|
|
123
|
+
const calls = nonNegativeNumber(source.calls);
|
|
124
|
+
const unavailable = source.unavailable;
|
|
125
|
+
if (key === undefined || providerID === undefined || modelID === undefined || calls === undefined || typeof unavailable !== "boolean") {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
let median;
|
|
129
|
+
if (source.median === null)
|
|
130
|
+
median = null;
|
|
131
|
+
else {
|
|
132
|
+
const parsed = finiteNumber(source.median);
|
|
133
|
+
if (parsed === undefined)
|
|
134
|
+
return;
|
|
135
|
+
median = parsed;
|
|
136
|
+
}
|
|
137
|
+
return { key, providerID, modelID, median, calls, unavailable };
|
|
138
|
+
};
|
|
139
|
+
var parseWidgetRow = function(raw) {
|
|
140
|
+
const source = fieldsOf(raw);
|
|
141
|
+
if (source === undefined)
|
|
142
|
+
return;
|
|
143
|
+
const key = nonEmptyString(source.key);
|
|
144
|
+
const providerID = nonEmptyString(source.providerID);
|
|
145
|
+
const modelID = nonEmptyString(source.modelID);
|
|
146
|
+
const median = nonNegativeNumber(source.median);
|
|
147
|
+
const calls = nonNegativeNumber(source.calls);
|
|
148
|
+
if (key === undefined || providerID === undefined || modelID === undefined || median === undefined || calls === undefined) {
|
|
149
|
+
return;
|
|
150
|
+
}
|
|
151
|
+
return { key, providerID, modelID, median, calls };
|
|
152
|
+
};
|
|
153
|
+
function signatureFor(meta) {
|
|
154
|
+
return JSON.stringify([
|
|
155
|
+
meta.id,
|
|
156
|
+
meta.time.created,
|
|
157
|
+
meta.time.updated,
|
|
158
|
+
meta.location?.directory,
|
|
159
|
+
meta.cost,
|
|
160
|
+
meta.tokens,
|
|
161
|
+
meta.fork?.sessionID,
|
|
162
|
+
meta.fork?.boundary
|
|
163
|
+
]);
|
|
164
|
+
}
|
|
165
|
+
function emptyShard() {
|
|
166
|
+
return {
|
|
167
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
168
|
+
metricVersion: METRIC_VERSION,
|
|
169
|
+
records: {},
|
|
170
|
+
index: {}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
function parseShard(raw) {
|
|
174
|
+
const source = fieldsOf(raw);
|
|
175
|
+
if (source === undefined)
|
|
176
|
+
return;
|
|
177
|
+
if (source.schemaVersion !== CACHE_SCHEMA_VERSION)
|
|
178
|
+
return;
|
|
179
|
+
if (source.metricVersion !== METRIC_VERSION)
|
|
180
|
+
return;
|
|
181
|
+
const recordsSource = fieldsOf(source.records);
|
|
182
|
+
const indexSource = fieldsOf(source.index);
|
|
183
|
+
if (recordsSource === undefined || indexSource === undefined)
|
|
184
|
+
return;
|
|
185
|
+
const records = {};
|
|
186
|
+
for (const [key, value] of Object.entries(recordsSource)) {
|
|
187
|
+
const record = parseSessionRecord(value);
|
|
188
|
+
if (record === undefined || record.sessionID !== key)
|
|
189
|
+
continue;
|
|
190
|
+
records[key] = record;
|
|
191
|
+
}
|
|
192
|
+
const index = {};
|
|
193
|
+
for (const [key, value] of Object.entries(indexSource)) {
|
|
194
|
+
const entry = parseIndexEntry(value);
|
|
195
|
+
if (entry === undefined || entry.sessionID !== key)
|
|
196
|
+
continue;
|
|
197
|
+
index[key] = entry;
|
|
198
|
+
}
|
|
199
|
+
return { schemaVersion: CACHE_SCHEMA_VERSION, metricVersion: METRIC_VERSION, records, index };
|
|
200
|
+
}
|
|
201
|
+
function parseCoverage(raw) {
|
|
202
|
+
const source = fieldsOf(raw);
|
|
203
|
+
if (source === undefined)
|
|
204
|
+
return;
|
|
205
|
+
if (source.schemaVersion !== CACHE_SCHEMA_VERSION)
|
|
206
|
+
return;
|
|
207
|
+
const directoryFilter = optionalField(source.directoryFilter, stringValue);
|
|
208
|
+
const fullInventoryCompletedAt = optionalField(source.fullInventoryCompletedAt, finiteNumber);
|
|
209
|
+
const recentCatchUpAt = optionalField(source.recentCatchUpAt, finiteNumber);
|
|
210
|
+
if (!directoryFilter.ok || !fullInventoryCompletedAt.ok || !recentCatchUpAt.ok)
|
|
211
|
+
return;
|
|
212
|
+
return {
|
|
213
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
214
|
+
directoryFilter: directoryFilter.value,
|
|
215
|
+
fullInventoryCompletedAt: fullInventoryCompletedAt.value,
|
|
216
|
+
recentCatchUpAt: recentCatchUpAt.value
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function parseViewSnapshot(raw) {
|
|
220
|
+
const source = fieldsOf(raw);
|
|
221
|
+
if (source === undefined)
|
|
222
|
+
return;
|
|
223
|
+
if (source.schemaVersion !== CACHE_SCHEMA_VERSION)
|
|
224
|
+
return;
|
|
225
|
+
if (source.metricVersion !== METRIC_VERSION)
|
|
226
|
+
return;
|
|
227
|
+
if (!RANGE_FILTERS.includes(source.range))
|
|
228
|
+
return;
|
|
229
|
+
const directoryFilter = optionalField(source.directoryFilter, stringValue);
|
|
230
|
+
const lastCheckedAt = optionalField(source.lastCheckedAt, finiteNumber);
|
|
231
|
+
const savedAtMs = finiteNumber(source.savedAtMs);
|
|
232
|
+
const rows = parseRows(source.rows, parseComparisonRow);
|
|
233
|
+
if (!directoryFilter.ok || !lastCheckedAt.ok || savedAtMs === undefined || rows === undefined)
|
|
234
|
+
return;
|
|
235
|
+
return {
|
|
236
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
237
|
+
metricVersion: METRIC_VERSION,
|
|
238
|
+
directoryFilter: directoryFilter.value,
|
|
239
|
+
range: source.range,
|
|
240
|
+
rows,
|
|
241
|
+
lastCheckedAt: lastCheckedAt.value,
|
|
242
|
+
savedAtMs
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
function parseWidgetSnapshot(raw) {
|
|
246
|
+
const source = fieldsOf(raw);
|
|
247
|
+
if (source === undefined)
|
|
248
|
+
return;
|
|
249
|
+
if (source.schemaVersion !== CACHE_SCHEMA_VERSION)
|
|
250
|
+
return;
|
|
251
|
+
if (source.metricVersion !== METRIC_VERSION)
|
|
252
|
+
return;
|
|
253
|
+
const rows = parseRows(source.rows, parseWidgetRow);
|
|
254
|
+
const savedAtMs = finiteNumber(source.savedAtMs);
|
|
255
|
+
if (rows === undefined || savedAtMs === undefined)
|
|
256
|
+
return;
|
|
257
|
+
return { schemaVersion: CACHE_SCHEMA_VERSION, metricVersion: METRIC_VERSION, rows, savedAtMs };
|
|
258
|
+
}
|
|
259
|
+
function parseTombstones(raw) {
|
|
260
|
+
const source = fieldsOf(raw);
|
|
261
|
+
if (source === undefined)
|
|
262
|
+
return;
|
|
263
|
+
if (!Array.isArray(source.sessionIDs))
|
|
264
|
+
return;
|
|
265
|
+
const sessionIDs = [];
|
|
266
|
+
for (const entry of source.sessionIDs) {
|
|
267
|
+
const sessionID = sessionIDFrom(entry);
|
|
268
|
+
if (sessionID !== undefined)
|
|
269
|
+
sessionIDs.push(sessionID);
|
|
270
|
+
}
|
|
271
|
+
return { sessionIDs };
|
|
272
|
+
}
|
|
273
|
+
function recordFrom(meta, scan, nowMs) {
|
|
274
|
+
return {
|
|
275
|
+
sessionID: meta.id,
|
|
276
|
+
directory: meta.location?.directory,
|
|
277
|
+
createdAt: meta.time.created,
|
|
278
|
+
updatedAt: meta.time.updated,
|
|
279
|
+
fork: meta.fork !== undefined,
|
|
280
|
+
signature: signatureFor(meta),
|
|
281
|
+
scannedAtMs: nowMs,
|
|
282
|
+
complete: true,
|
|
283
|
+
samples: scan.samples,
|
|
284
|
+
unavailable: scan.unavailable
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
var CACHE_SCHEMA_VERSION = 1;
|
|
288
|
+
var METRIC_VERSION = 1;
|
|
289
|
+
var SESSION_ID_PATTERN = /^[a-zA-Z0-9][a-zA-Z0-9._-]*$/;
|
|
290
|
+
var RANGE_FILTERS = ["last7", "last30", "last90", "all"];
|
|
291
|
+
|
|
292
|
+
// src/history/durable.ts
|
|
293
|
+
function fnv1a(value) {
|
|
294
|
+
let hash = 2166136261;
|
|
295
|
+
for (let index = 0;index < value.length; index++) {
|
|
296
|
+
hash = Math.imul(hash ^ value.charCodeAt(index), 16777619) >>> 0;
|
|
297
|
+
}
|
|
298
|
+
return hash;
|
|
299
|
+
}
|
|
300
|
+
var shardIndex = function(sessionID) {
|
|
301
|
+
return fnv1a(sessionID) % SHARD_COUNT;
|
|
302
|
+
};
|
|
303
|
+
var shardName = function(index) {
|
|
304
|
+
return `shard:${index}`;
|
|
305
|
+
};
|
|
306
|
+
var shardStorageKey = function(index) {
|
|
307
|
+
return `${SHARD_PREFIX}${index}`;
|
|
308
|
+
};
|
|
309
|
+
var coverageKey = function(directoryFilter) {
|
|
310
|
+
if (directoryFilter === undefined)
|
|
311
|
+
return COVERAGE_ALL_KEY;
|
|
312
|
+
return `${COVERAGE_PREFIX}d${fnv1a(directoryFilter).toString(16).padStart(8, "0")}`;
|
|
313
|
+
};
|
|
314
|
+
var isCoverageKey = function(key) {
|
|
315
|
+
return key === COVERAGE_ALL_KEY || key.startsWith(`${COVERAGE_PREFIX}d`);
|
|
316
|
+
};
|
|
317
|
+
var viewSnapshotKey = function(slot) {
|
|
318
|
+
return `${VIEW_SNAPSHOT_PREFIX}${slot}`;
|
|
319
|
+
};
|
|
320
|
+
var snapshotSlot = function(key) {
|
|
321
|
+
if (!key.startsWith(VIEW_SNAPSHOT_PREFIX))
|
|
322
|
+
return;
|
|
323
|
+
const suffix = key.slice(VIEW_SNAPSHOT_PREFIX.length);
|
|
324
|
+
if (!/^(?:0|[1-9][0-9]*)$/.test(suffix))
|
|
325
|
+
return;
|
|
326
|
+
const slot = Number(suffix);
|
|
327
|
+
return slot < VIEW_SNAPSHOT_LIMIT ? slot : undefined;
|
|
328
|
+
};
|
|
329
|
+
var isSnapshotIndexKey = function(key) {
|
|
330
|
+
return snapshotSlot(key) !== undefined;
|
|
331
|
+
};
|
|
332
|
+
var sameView = function(a, b) {
|
|
333
|
+
return a.directoryFilter === b.directoryFilter && a.range === b.range;
|
|
334
|
+
};
|
|
335
|
+
var replaceDraft = function(draft, value) {
|
|
336
|
+
for (const key of Object.keys(draft))
|
|
337
|
+
delete draft[key];
|
|
338
|
+
Object.assign(draft, value);
|
|
339
|
+
};
|
|
340
|
+
var parseKeyIndex = function(payload, accept) {
|
|
341
|
+
const keys = payload === undefined ? undefined : payload.keys;
|
|
342
|
+
if (!Array.isArray(keys))
|
|
343
|
+
return [];
|
|
344
|
+
const result = [];
|
|
345
|
+
for (const entry of keys) {
|
|
346
|
+
if (typeof entry !== "string" || !accept(entry) || result.includes(entry))
|
|
347
|
+
continue;
|
|
348
|
+
result.push(entry);
|
|
349
|
+
}
|
|
350
|
+
return result;
|
|
351
|
+
};
|
|
352
|
+
var readKeyIndex = function(store, key, accept) {
|
|
353
|
+
return parseKeyIndex(store.read(key), accept);
|
|
354
|
+
};
|
|
355
|
+
var mergeTombstones = function(existing, added) {
|
|
356
|
+
const incoming = new Set(added);
|
|
357
|
+
const merged = existing.filter((sessionID) => !incoming.has(sessionID));
|
|
358
|
+
const seen = new Set;
|
|
359
|
+
for (const sessionID of added) {
|
|
360
|
+
if (seen.has(sessionID))
|
|
361
|
+
continue;
|
|
362
|
+
seen.add(sessionID);
|
|
363
|
+
merged.push(sessionID);
|
|
364
|
+
}
|
|
365
|
+
return merged.slice(-TOMBSTONE_LIMIT);
|
|
366
|
+
};
|
|
367
|
+
function createRepository(store) {
|
|
368
|
+
const readTombstones = () => parseTombstones(store.read(TOMBSTONES_KEY)) ?? { sessionIDs: [] };
|
|
369
|
+
const readSnapshotIndex = () => readKeyIndex(store, SNAPSHOTS_INDEX_KEY, isSnapshotIndexKey);
|
|
370
|
+
const liveSnapshots = (keys) => {
|
|
371
|
+
const live = [];
|
|
372
|
+
for (const key of keys) {
|
|
373
|
+
const slot = snapshotSlot(key);
|
|
374
|
+
if (slot === undefined)
|
|
375
|
+
continue;
|
|
376
|
+
const parsed = parseViewSnapshot(store.read(key));
|
|
377
|
+
if (parsed !== undefined)
|
|
378
|
+
live.push({ key, slot, snapshot: parsed });
|
|
379
|
+
}
|
|
380
|
+
return live;
|
|
381
|
+
};
|
|
382
|
+
return {
|
|
383
|
+
loadAllShards() {
|
|
384
|
+
const shards = new Map;
|
|
385
|
+
for (let index = 0;index < SHARD_COUNT; index++) {
|
|
386
|
+
const shard = parseShard(store.read(shardStorageKey(index)));
|
|
387
|
+
if (shard !== undefined)
|
|
388
|
+
shards.set(shardName(index), shard);
|
|
389
|
+
}
|
|
390
|
+
return shards;
|
|
391
|
+
},
|
|
392
|
+
async putRecords(records2, nowMs) {
|
|
393
|
+
const grouped = new Map;
|
|
394
|
+
for (const record of records2) {
|
|
395
|
+
const index = shardIndex(record.sessionID);
|
|
396
|
+
const group = grouped.get(index);
|
|
397
|
+
if (group === undefined)
|
|
398
|
+
grouped.set(index, [record]);
|
|
399
|
+
else
|
|
400
|
+
group.push(record);
|
|
401
|
+
}
|
|
402
|
+
for (const [index, group] of [...grouped].sort((a, b) => a[0] - b[0])) {
|
|
403
|
+
await store.write(shardStorageKey(index), (draft) => {
|
|
404
|
+
const shard = parseShard(draft) ?? emptyShard();
|
|
405
|
+
for (const record of group) {
|
|
406
|
+
shard.records[record.sessionID] = record;
|
|
407
|
+
shard.index[record.sessionID] = {
|
|
408
|
+
sessionID: record.sessionID,
|
|
409
|
+
directory: record.directory,
|
|
410
|
+
updatedAt: record.updatedAt
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
replaceDraft(draft, shard);
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
},
|
|
417
|
+
async removeSessions(sessionIDs, nowMs) {
|
|
418
|
+
if (sessionIDs.length === 0)
|
|
419
|
+
return;
|
|
420
|
+
await store.write(TOMBSTONES_KEY, (draft) => replaceDraft(draft, { sessionIDs: mergeTombstones(readTombstones().sessionIDs, sessionIDs) }));
|
|
421
|
+
const grouped = new Map;
|
|
422
|
+
for (const sessionID of sessionIDs) {
|
|
423
|
+
const index = shardIndex(sessionID);
|
|
424
|
+
const group = grouped.get(index);
|
|
425
|
+
if (group === undefined)
|
|
426
|
+
grouped.set(index, [sessionID]);
|
|
427
|
+
else
|
|
428
|
+
group.push(sessionID);
|
|
429
|
+
}
|
|
430
|
+
for (const [index, group] of [...grouped].sort((a, b) => a[0] - b[0])) {
|
|
431
|
+
const key = shardStorageKey(index);
|
|
432
|
+
if (store.read(key) === undefined)
|
|
433
|
+
continue;
|
|
434
|
+
await store.write(key, (draft) => {
|
|
435
|
+
const shard = parseShard(draft) ?? emptyShard();
|
|
436
|
+
for (const sessionID of group) {
|
|
437
|
+
delete shard.records[sessionID];
|
|
438
|
+
delete shard.index[sessionID];
|
|
439
|
+
}
|
|
440
|
+
replaceDraft(draft, shard);
|
|
441
|
+
});
|
|
442
|
+
}
|
|
443
|
+
},
|
|
444
|
+
loadTombstones() {
|
|
445
|
+
return readTombstones();
|
|
446
|
+
},
|
|
447
|
+
async clearTombstones(nowMs) {
|
|
448
|
+
await store.write(TOMBSTONES_KEY, (draft) => replaceDraft(draft, { sessionIDs: [] }));
|
|
449
|
+
},
|
|
450
|
+
readCoverage(directoryFilter) {
|
|
451
|
+
const record = parseCoverage(store.read(coverageKey(directoryFilter)));
|
|
452
|
+
if (record === undefined)
|
|
453
|
+
return;
|
|
454
|
+
return record.directoryFilter === directoryFilter ? record : undefined;
|
|
455
|
+
},
|
|
456
|
+
async writeCoverage(record) {
|
|
457
|
+
const key = coverageKey(record.directoryFilter);
|
|
458
|
+
await store.write(COVERAGE_INDEX_KEY, (draft) => {
|
|
459
|
+
const indexed = parseKeyIndex(draft, isCoverageKey);
|
|
460
|
+
replaceDraft(draft, { keys: indexed.includes(key) ? indexed : [...indexed, key] });
|
|
461
|
+
});
|
|
462
|
+
await store.write(key, (draft) => replaceDraft(draft, record));
|
|
463
|
+
},
|
|
464
|
+
readViewSnapshot(directoryFilter, range) {
|
|
465
|
+
const index = readSnapshotIndex();
|
|
466
|
+
for (let position = index.length - 1;position >= 0; position--) {
|
|
467
|
+
const snapshot = parseViewSnapshot(store.read(index[position]));
|
|
468
|
+
if (snapshot === undefined)
|
|
469
|
+
continue;
|
|
470
|
+
if (snapshot.directoryFilter === directoryFilter && snapshot.range === range)
|
|
471
|
+
return snapshot;
|
|
472
|
+
}
|
|
473
|
+
return;
|
|
474
|
+
},
|
|
475
|
+
async writeViewSnapshot(snapshot) {
|
|
476
|
+
const live = liveSnapshots(readSnapshotIndex());
|
|
477
|
+
const updated = live.filter((entry) => sameView(entry.snapshot, snapshot)).at(-1);
|
|
478
|
+
const kept = live.filter((entry) => !sameView(entry.snapshot, snapshot));
|
|
479
|
+
let slot;
|
|
480
|
+
if (updated !== undefined) {
|
|
481
|
+
slot = updated.slot;
|
|
482
|
+
} else {
|
|
483
|
+
const used = new Set(kept.map((entry) => entry.slot));
|
|
484
|
+
const free = Array.from({ length: VIEW_SNAPSHOT_LIMIT }, (_, candidate) => candidate).find((candidate) => !used.has(candidate));
|
|
485
|
+
if (free !== undefined) {
|
|
486
|
+
slot = free;
|
|
487
|
+
} else {
|
|
488
|
+
slot = kept[0].slot;
|
|
489
|
+
kept.shift();
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
const key = viewSnapshotKey(slot);
|
|
493
|
+
await store.write(key, (draft) => replaceDraft(draft, snapshot));
|
|
494
|
+
await store.write(SNAPSHOTS_INDEX_KEY, (draft) => {
|
|
495
|
+
const current = liveSnapshots(parseKeyIndex(draft, isSnapshotIndexKey)).filter((entry) => entry.key !== key && !sameView(entry.snapshot, snapshot)).map((entry) => entry.key);
|
|
496
|
+
replaceDraft(draft, { keys: [...current, key].slice(-VIEW_SNAPSHOT_LIMIT) });
|
|
497
|
+
});
|
|
498
|
+
},
|
|
499
|
+
readWidgetSnapshot() {
|
|
500
|
+
return parseWidgetSnapshot(store.read(WIDGET_SNAPSHOT_KEY));
|
|
501
|
+
},
|
|
502
|
+
async writeWidgetSnapshot(rows, nowMs) {
|
|
503
|
+
const snapshot = {
|
|
504
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
505
|
+
metricVersion: METRIC_VERSION,
|
|
506
|
+
rows,
|
|
507
|
+
savedAtMs: nowMs
|
|
508
|
+
};
|
|
509
|
+
await store.write(WIDGET_SNAPSHOT_KEY, (draft) => replaceDraft(draft, snapshot));
|
|
510
|
+
},
|
|
511
|
+
async clear() {
|
|
512
|
+
const keys = [
|
|
513
|
+
...Array.from({ length: SHARD_COUNT }, (_, index) => shardStorageKey(index)),
|
|
514
|
+
TOMBSTONES_KEY,
|
|
515
|
+
COVERAGE_INDEX_KEY,
|
|
516
|
+
...readKeyIndex(store, COVERAGE_INDEX_KEY, isCoverageKey),
|
|
517
|
+
SNAPSHOTS_INDEX_KEY,
|
|
518
|
+
...Array.from({ length: VIEW_SNAPSHOT_LIMIT }, (_, slot) => viewSnapshotKey(slot)),
|
|
519
|
+
WIDGET_SNAPSHOT_KEY
|
|
520
|
+
];
|
|
521
|
+
for (const key of keys) {
|
|
522
|
+
if (store.read(key) === undefined)
|
|
523
|
+
continue;
|
|
524
|
+
await store.write(key, (draft) => replaceDraft(draft, {}));
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
var SHARD_COUNT = 64;
|
|
530
|
+
var TOMBSTONE_LIMIT = 1000;
|
|
531
|
+
var VIEW_SNAPSHOT_LIMIT = 8;
|
|
532
|
+
var SHARD_PREFIX = "tks.shard.";
|
|
533
|
+
var COVERAGE_PREFIX = "tks.coverage.";
|
|
534
|
+
var COVERAGE_ALL_KEY = "tks.coverage.all";
|
|
535
|
+
var COVERAGE_INDEX_KEY = "tks.coverage-index";
|
|
536
|
+
var TOMBSTONES_KEY = "tks.tombstones";
|
|
537
|
+
var SNAPSHOTS_INDEX_KEY = "tks.snapshots";
|
|
538
|
+
var VIEW_SNAPSHOT_PREFIX = "tks.snapshot.view.";
|
|
539
|
+
var WIDGET_SNAPSHOT_KEY = "tks.snapshot.widget24h";
|
|
540
|
+
|
|
541
|
+
// src/history/plugin-storage.ts
|
|
542
|
+
import {unwrap} from "solid-js/store";
|
|
543
|
+
function createPluginDurableStore(storage) {
|
|
544
|
+
const entries = new Map;
|
|
545
|
+
const entryOf = (key) => {
|
|
546
|
+
const existing = entries.get(key);
|
|
547
|
+
if (existing !== undefined)
|
|
548
|
+
return existing;
|
|
549
|
+
const [store2, update] = storage.store(`${KEY_PREFIX}${key}`, {
|
|
550
|
+
initial: {}
|
|
551
|
+
});
|
|
552
|
+
const entry = {
|
|
553
|
+
read: () => unwrap(store2),
|
|
554
|
+
write: (mutation) => update(mutation)
|
|
555
|
+
};
|
|
556
|
+
entries.set(key, entry);
|
|
557
|
+
return entry;
|
|
558
|
+
};
|
|
559
|
+
return {
|
|
560
|
+
read(key) {
|
|
561
|
+
return entryOf(key).read();
|
|
562
|
+
},
|
|
563
|
+
write(key, mutate) {
|
|
564
|
+
return entryOf(key).write(mutate);
|
|
565
|
+
}
|
|
566
|
+
};
|
|
567
|
+
}
|
|
568
|
+
var KEY_PREFIX = "history.";
|
|
569
|
+
|
|
570
|
+
// src/history/log-file.ts
|
|
571
|
+
import {randomUUID} from "node:crypto";
|
|
572
|
+
import {appendFile, mkdir, rename, stat} from "node:fs/promises";
|
|
573
|
+
import {homedir} from "node:os";
|
|
574
|
+
import {dirname, join} from "node:path";
|
|
575
|
+
function historyLogPath(env = process.env, home = homedir()) {
|
|
576
|
+
return join(env.XDG_DATA_HOME || join(home, ".local", "share"), "opencode", "log", "tokscope.log");
|
|
577
|
+
}
|
|
578
|
+
function createHistoryFileLogger(path = historyLogPath()) {
|
|
579
|
+
const run = randomUUID();
|
|
580
|
+
let pending = Promise.resolve();
|
|
581
|
+
return {
|
|
582
|
+
log(event) {
|
|
583
|
+
const line = JSON.stringify({
|
|
584
|
+
event: "tokscope.history",
|
|
585
|
+
timestamp: new Date().toISOString(),
|
|
586
|
+
run,
|
|
587
|
+
operation: event.operation,
|
|
588
|
+
outcome: event.outcome,
|
|
589
|
+
durationMs: event.durationMs,
|
|
590
|
+
attempt: event.attempt,
|
|
591
|
+
sessionsRead: event.sessionsRead,
|
|
592
|
+
messagesRead: event.messagesRead
|
|
593
|
+
}) + "\n";
|
|
594
|
+
pending = pending.then(async () => {
|
|
595
|
+
await mkdir(dirname(path), { recursive: true });
|
|
596
|
+
const size = await stat(path).then((info) => info.size, (error) => {
|
|
597
|
+
if (error.code === "ENOENT")
|
|
598
|
+
return 0;
|
|
599
|
+
throw error;
|
|
600
|
+
});
|
|
601
|
+
if (size >= MAX_LOG_BYTES)
|
|
602
|
+
await rename(path, path + ".previous");
|
|
603
|
+
await appendFile(path, line, { encoding: "utf8", mode: 384 });
|
|
604
|
+
}).catch(() => {
|
|
605
|
+
});
|
|
606
|
+
},
|
|
607
|
+
flush: () => pending
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
var MAX_LOG_BYTES = 1048576;
|
|
611
|
+
|
|
612
|
+
// src/history/service.ts
|
|
613
|
+
import {createSignal} from "solid-js";
|
|
15
614
|
|
|
16
615
|
// src/filters.ts
|
|
17
616
|
function rangeStartMs(range, nowMs) {
|
|
@@ -29,22 +628,6 @@ function rangeStartMs(range, nowMs) {
|
|
|
29
628
|
var DEFAULT_FILTERS = { scope: "all", range: "last30", reasoning: "all" };
|
|
30
629
|
var DAY_MS = 86400000;
|
|
31
630
|
|
|
32
|
-
// src/format.ts
|
|
33
|
-
function formatRate(median) {
|
|
34
|
-
return `${median.toFixed(1)} tok/s`;
|
|
35
|
-
}
|
|
36
|
-
function formatDayLabel(date) {
|
|
37
|
-
const [year, month, day] = date.split("-").map(Number);
|
|
38
|
-
return new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric" }).format(new Date(year, month - 1, day));
|
|
39
|
-
}
|
|
40
|
-
function formatCoverage(measured, unavailable) {
|
|
41
|
-
return unavailable > 0 ? `${measured} measured calls; ${unavailable} unavailable` : `${measured} measured calls`;
|
|
42
|
-
}
|
|
43
|
-
function modelDisplayName(models, providerID, modelID) {
|
|
44
|
-
const match = models?.find((model) => model.id === modelID && model.providerID === providerID);
|
|
45
|
-
return match?.name ?? `${providerID}/${modelID}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
631
|
// src/metric.ts
|
|
49
632
|
function classifyAssistant(message) {
|
|
50
633
|
if (message.error !== undefined)
|
|
@@ -85,8 +668,10 @@ function classifyAssistant(message) {
|
|
|
85
668
|
}
|
|
86
669
|
|
|
87
670
|
// src/harvest.ts
|
|
88
|
-
async function
|
|
671
|
+
async function listSessions(client, options) {
|
|
89
672
|
const sessions = [];
|
|
673
|
+
const fromMs = options.fromMs;
|
|
674
|
+
const seenCursors = new Set;
|
|
90
675
|
let sessionCursor;
|
|
91
676
|
do {
|
|
92
677
|
const page = await client.session.list(sessionCursor === undefined ? {
|
|
@@ -95,42 +680,27 @@ async function collectMeasurements(client, options = {}) {
|
|
|
95
680
|
...options.directory === undefined ? {} : { directory: options.directory }
|
|
96
681
|
} : { limit: SESSION_PAGE_SIZE, cursor: sessionCursor });
|
|
97
682
|
sessions.push(...page.data);
|
|
683
|
+
const stalePage = fromMs !== undefined && page.data.length > 0 && page.data.every((session) => session.time.updated < fromMs);
|
|
684
|
+
if (stalePage)
|
|
685
|
+
return { sessions, boundaryReached: true };
|
|
98
686
|
sessionCursor = page.cursor.next ?? undefined;
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const inRangeAt = (completedAt) => options.fromMs === undefined || completedAt >= options.fromMs;
|
|
104
|
-
let scanned = 0;
|
|
105
|
-
let nextSession = 0;
|
|
106
|
-
const concurrency = Math.max(1, Math.min(options.concurrency ?? DEFAULT_CONCURRENCY, inRange.length));
|
|
107
|
-
const worker = async () => {
|
|
108
|
-
while (true) {
|
|
109
|
-
const index = nextSession++;
|
|
110
|
-
if (index >= inRange.length)
|
|
111
|
-
return;
|
|
112
|
-
const session = inRange[index];
|
|
113
|
-
let messageCursor;
|
|
114
|
-
do {
|
|
115
|
-
const page = await client.message.list(messageCursor === undefined ? { sessionID: session.id, limit: MESSAGE_PAGE_SIZE, order: "desc", type: "assistant" } : { sessionID: session.id, limit: MESSAGE_PAGE_SIZE, type: "assistant", cursor: messageCursor });
|
|
116
|
-
for (const message of page.data) {
|
|
117
|
-
if (message.type !== "assistant")
|
|
118
|
-
continue;
|
|
119
|
-
if (session.fork !== undefined && message.time.created < session.time.created)
|
|
120
|
-
continue;
|
|
121
|
-
const result = classifyAssistant(message);
|
|
122
|
-
if (result.kind === "measured" && inRangeAt(result.sample.completedAt))
|
|
123
|
-
samples.push(result.sample);
|
|
124
|
-
else if (result.kind === "unavailable" && inRangeAt(result.sample.completedAt))
|
|
125
|
-
unavailable.push(result.sample);
|
|
126
|
-
}
|
|
127
|
-
messageCursor = page.cursor.next ?? undefined;
|
|
128
|
-
} while (messageCursor !== undefined);
|
|
129
|
-
scanned++;
|
|
130
|
-
options.onProgress?.({ sessionsScanned: scanned });
|
|
687
|
+
if (sessionCursor !== undefined) {
|
|
688
|
+
if (seenCursors.has(sessionCursor))
|
|
689
|
+
throw new Error("repeated cursor");
|
|
690
|
+
seenCursors.add(sessionCursor);
|
|
131
691
|
}
|
|
692
|
+
} while (sessionCursor !== undefined);
|
|
693
|
+
return { sessions, boundaryReached: true };
|
|
694
|
+
}
|
|
695
|
+
function inRangeSamples(result, fromMs) {
|
|
696
|
+
if (fromMs === undefined)
|
|
697
|
+
return result;
|
|
698
|
+
return {
|
|
699
|
+
samples: result.samples.filter((sample) => sample.completedAt >= fromMs),
|
|
700
|
+
unavailable: result.unavailable.filter((sample) => sample.completedAt >= fromMs)
|
|
132
701
|
};
|
|
133
|
-
|
|
702
|
+
}
|
|
703
|
+
function collectVariants(samples, unavailable) {
|
|
134
704
|
const variantSet = new Set;
|
|
135
705
|
for (const sample of samples)
|
|
136
706
|
if (sample.variant !== undefined)
|
|
@@ -138,11 +708,37 @@ async function collectMeasurements(client, options = {}) {
|
|
|
138
708
|
for (const sample of unavailable)
|
|
139
709
|
if (sample.variant !== undefined)
|
|
140
710
|
variantSet.add(sample.variant);
|
|
141
|
-
return
|
|
711
|
+
return [...variantSet].sort();
|
|
712
|
+
}
|
|
713
|
+
async function scanMessages(client, session) {
|
|
714
|
+
const samples = [];
|
|
715
|
+
const unavailable = [];
|
|
716
|
+
const seenCursors = new Set;
|
|
717
|
+
let messageCursor;
|
|
718
|
+
do {
|
|
719
|
+
const page = await client.message.list(messageCursor === undefined ? { sessionID: session.id, limit: MESSAGE_PAGE_SIZE, order: "desc", type: "assistant" } : { sessionID: session.id, limit: MESSAGE_PAGE_SIZE, type: "assistant", cursor: messageCursor });
|
|
720
|
+
for (const message of page.data) {
|
|
721
|
+
if (message.type !== "assistant")
|
|
722
|
+
continue;
|
|
723
|
+
if (session.fork !== undefined && message.time.created < session.time.created)
|
|
724
|
+
continue;
|
|
725
|
+
const result = classifyAssistant(message);
|
|
726
|
+
if (result.kind === "measured")
|
|
727
|
+
samples.push(result.sample);
|
|
728
|
+
else if (result.kind === "unavailable")
|
|
729
|
+
unavailable.push(result.sample);
|
|
730
|
+
}
|
|
731
|
+
messageCursor = page.cursor.next ?? undefined;
|
|
732
|
+
if (messageCursor !== undefined) {
|
|
733
|
+
if (seenCursors.has(messageCursor))
|
|
734
|
+
throw new Error("repeated cursor");
|
|
735
|
+
seenCursors.add(messageCursor);
|
|
736
|
+
}
|
|
737
|
+
} while (messageCursor !== undefined);
|
|
738
|
+
return { samples, unavailable };
|
|
142
739
|
}
|
|
143
740
|
var SESSION_PAGE_SIZE = 100;
|
|
144
741
|
var MESSAGE_PAGE_SIZE = 200;
|
|
145
|
-
var DEFAULT_CONCURRENCY = 8;
|
|
146
742
|
|
|
147
743
|
// src/stats.ts
|
|
148
744
|
function median(values) {
|
|
@@ -226,46 +822,1109 @@ function dailySeries(samples, key, fromMs, nowMs) {
|
|
|
226
822
|
else
|
|
227
823
|
rates.push(sample.rate);
|
|
228
824
|
}
|
|
229
|
-
const points = [];
|
|
230
|
-
const end = dayStart(nowMs);
|
|
231
|
-
for (const cursor = dayStart(startMs);cursor.getTime() <= end.getTime(); cursor.setDate(cursor.getDate() + 1)) {
|
|
232
|
-
const date = localDayKey(cursor.getTime());
|
|
233
|
-
const rates = byDay.get(date);
|
|
234
|
-
points.push({
|
|
235
|
-
date,
|
|
236
|
-
median: rates === undefined ? null : median(rates),
|
|
237
|
-
calls: rates?.length ?? 0
|
|
825
|
+
const points = [];
|
|
826
|
+
const end = dayStart(nowMs);
|
|
827
|
+
for (const cursor = dayStart(startMs);cursor.getTime() <= end.getTime(); cursor.setDate(cursor.getDate() + 1)) {
|
|
828
|
+
const date = localDayKey(cursor.getTime());
|
|
829
|
+
const rates = byDay.get(date);
|
|
830
|
+
points.push({
|
|
831
|
+
date,
|
|
832
|
+
median: rates === undefined ? null : median(rates),
|
|
833
|
+
calls: rates?.length ?? 0
|
|
834
|
+
});
|
|
835
|
+
}
|
|
836
|
+
return points;
|
|
837
|
+
}
|
|
838
|
+
function windowRange(length, selected, size) {
|
|
839
|
+
const span = Math.max(0, Math.min(size, length));
|
|
840
|
+
if (span === 0)
|
|
841
|
+
return { start: 0, end: 0 };
|
|
842
|
+
const clamped = Math.max(0, Math.min(selected, length - 1));
|
|
843
|
+
const start = Math.max(0, Math.min(clamped - Math.floor(span / 2), length - span));
|
|
844
|
+
return { start, end: start + span };
|
|
845
|
+
}
|
|
846
|
+
function dayWindow(series, selected, width) {
|
|
847
|
+
return windowRange(series.length, selected, width);
|
|
848
|
+
}
|
|
849
|
+
function barLevels(series, rows) {
|
|
850
|
+
const max = Math.max(0, ...series.map((point) => point.median ?? 0));
|
|
851
|
+
return series.map((point) => {
|
|
852
|
+
if (point.median === null || max <= 0)
|
|
853
|
+
return null;
|
|
854
|
+
return Math.max(1, Math.round(point.median / max * rows));
|
|
855
|
+
});
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
// src/sidebar/rank.ts
|
|
859
|
+
function widgetRows(samples, nowMs) {
|
|
860
|
+
const since = nowMs - WIDGET_WINDOW_MS;
|
|
861
|
+
const groups = new Map;
|
|
862
|
+
for (const sample of samples) {
|
|
863
|
+
if (sample.completedAt <= since || sample.completedAt > nowMs)
|
|
864
|
+
continue;
|
|
865
|
+
const key = rowKey(sample.providerID, sample.modelID);
|
|
866
|
+
let group = groups.get(key);
|
|
867
|
+
if (group === undefined) {
|
|
868
|
+
group = { providerID: sample.providerID, modelID: sample.modelID, rates: [] };
|
|
869
|
+
groups.set(key, group);
|
|
870
|
+
}
|
|
871
|
+
group.rates.push(sample.rate);
|
|
872
|
+
}
|
|
873
|
+
const rows = [];
|
|
874
|
+
for (const [key, group] of groups) {
|
|
875
|
+
if (group.rates.length < WIDGET_MIN_CALLS)
|
|
876
|
+
continue;
|
|
877
|
+
const value = median(group.rates);
|
|
878
|
+
if (value === null)
|
|
879
|
+
continue;
|
|
880
|
+
rows.push({
|
|
881
|
+
key,
|
|
882
|
+
providerID: group.providerID,
|
|
883
|
+
modelID: group.modelID,
|
|
884
|
+
median: value,
|
|
885
|
+
calls: group.rates.length
|
|
886
|
+
});
|
|
887
|
+
}
|
|
888
|
+
rows.sort((a, b) => {
|
|
889
|
+
const medianOrder = b.median - a.median;
|
|
890
|
+
if (medianOrder !== 0)
|
|
891
|
+
return medianOrder;
|
|
892
|
+
if (b.calls !== a.calls)
|
|
893
|
+
return b.calls - a.calls;
|
|
894
|
+
return a.key.localeCompare(b.key);
|
|
895
|
+
});
|
|
896
|
+
return rows.slice(0, WIDGET_MAX_ENTRIES);
|
|
897
|
+
}
|
|
898
|
+
var WIDGET_WINDOW_MS = 86400000;
|
|
899
|
+
var WIDGET_MIN_CALLS = 3;
|
|
900
|
+
var WIDGET_MAX_ENTRIES = 3;
|
|
901
|
+
|
|
902
|
+
// src/history/service.ts
|
|
903
|
+
var readScope = function(base) {
|
|
904
|
+
const get = base.session.get;
|
|
905
|
+
const scope = {
|
|
906
|
+
sessionsRead: 0,
|
|
907
|
+
messagesRead: 0,
|
|
908
|
+
client: {
|
|
909
|
+
session: {
|
|
910
|
+
list: async (input) => {
|
|
911
|
+
const page = await base.session.list(input);
|
|
912
|
+
scope.sessionsRead += page.data.length;
|
|
913
|
+
return page;
|
|
914
|
+
},
|
|
915
|
+
...get === undefined ? {} : {
|
|
916
|
+
get: async (input) => {
|
|
917
|
+
const meta = await get.call(base.session, input);
|
|
918
|
+
scope.sessionsRead += 1;
|
|
919
|
+
return meta;
|
|
920
|
+
}
|
|
921
|
+
}
|
|
922
|
+
},
|
|
923
|
+
message: {
|
|
924
|
+
list: async (input) => {
|
|
925
|
+
const page = await base.message.list(input);
|
|
926
|
+
scope.messagesRead += page.data.length;
|
|
927
|
+
return page;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
}
|
|
931
|
+
};
|
|
932
|
+
return scope;
|
|
933
|
+
};
|
|
934
|
+
var matchesScope = function(sessionDirectory, directoryFilter) {
|
|
935
|
+
if (directoryFilter === undefined)
|
|
936
|
+
return true;
|
|
937
|
+
return sessionDirectory === undefined || sessionDirectory === directoryFilter;
|
|
938
|
+
};
|
|
939
|
+
var isNotFound = function(error) {
|
|
940
|
+
if (typeof error !== "object" || error === null)
|
|
941
|
+
return false;
|
|
942
|
+
const fields = error;
|
|
943
|
+
if (fields.status === 404 || fields.statusCode === 404)
|
|
944
|
+
return true;
|
|
945
|
+
if (fields.cause?.status === 404)
|
|
946
|
+
return true;
|
|
947
|
+
return typeof fields.message === "string" && /(?:^|\D)404(?:\D|$)/.test(fields.message);
|
|
948
|
+
};
|
|
949
|
+
function createHistoryService(deps) {
|
|
950
|
+
const client = deps.client;
|
|
951
|
+
const now = deps.now;
|
|
952
|
+
const log = deps.log;
|
|
953
|
+
const concurrency = Math.max(1, deps.concurrency ?? 8);
|
|
954
|
+
const records3 = new Map;
|
|
955
|
+
const index = new Map;
|
|
956
|
+
const tombstones = new Set;
|
|
957
|
+
const coverage = new Map;
|
|
958
|
+
const views = new Map;
|
|
959
|
+
const passes = new Map;
|
|
960
|
+
const statuses = new Map;
|
|
961
|
+
const work = new Map;
|
|
962
|
+
const dirty = new Set;
|
|
963
|
+
const scanTokens = new Map;
|
|
964
|
+
let holds = 0;
|
|
965
|
+
let timerCancel;
|
|
966
|
+
let auditCancel;
|
|
967
|
+
let burstCancel;
|
|
968
|
+
let burstStartedAt;
|
|
969
|
+
let flushInFlight;
|
|
970
|
+
let auditInFlight = false;
|
|
971
|
+
let resetGeneration = 0;
|
|
972
|
+
let pendingRecords = [];
|
|
973
|
+
let persistence = "ok";
|
|
974
|
+
let widgetPublished = false;
|
|
975
|
+
let hydrated = false;
|
|
976
|
+
const retryWaits = new Set;
|
|
977
|
+
const storedWidget = deps.repository.readWidgetSnapshot();
|
|
978
|
+
const [widgetState, setWidgetState] = createSignal(storedWidget === undefined ? { status: "loading", rows: [], stale: false } : { status: "ready", rows: storedWidget.rows, stale: true });
|
|
979
|
+
if (storedWidget !== undefined)
|
|
980
|
+
widgetPublished = true;
|
|
981
|
+
rememberTombstones();
|
|
982
|
+
function rememberTombstones() {
|
|
983
|
+
for (const sessionID of deps.repository.loadTombstones().sessionIDs)
|
|
984
|
+
tombstones.add(sessionID);
|
|
985
|
+
}
|
|
986
|
+
function emit(event) {
|
|
987
|
+
if (log === undefined)
|
|
988
|
+
return;
|
|
989
|
+
try {
|
|
990
|
+
log(event);
|
|
991
|
+
} catch {
|
|
992
|
+
}
|
|
993
|
+
}
|
|
994
|
+
async function acquire(operation, guard, watching, attempt) {
|
|
995
|
+
const scope = readScope(client);
|
|
996
|
+
const operationStartedAt = now();
|
|
997
|
+
const report = (outcome, attemptNumber) => emit({
|
|
998
|
+
operation,
|
|
999
|
+
outcome,
|
|
1000
|
+
durationMs: now() - operationStartedAt,
|
|
1001
|
+
attempt: attemptNumber,
|
|
1002
|
+
sessionsRead: scope.sessionsRead,
|
|
1003
|
+
messagesRead: scope.messagesRead
|
|
1004
|
+
});
|
|
1005
|
+
for (let attemptNumber = 1;; attemptNumber++) {
|
|
1006
|
+
try {
|
|
1007
|
+
const value = await attempt(scope);
|
|
1008
|
+
if (value === CANCELLED || !guard()) {
|
|
1009
|
+
report("cancelled", attemptNumber);
|
|
1010
|
+
return CANCELLED;
|
|
1011
|
+
}
|
|
1012
|
+
report("success", attemptNumber);
|
|
1013
|
+
return value;
|
|
1014
|
+
} catch (error) {
|
|
1015
|
+
if (!guard()) {
|
|
1016
|
+
report("cancelled", attemptNumber);
|
|
1017
|
+
return CANCELLED;
|
|
1018
|
+
}
|
|
1019
|
+
const waitMs = ACQUISITION_RETRY_DELAYS_MS[attemptNumber - 1];
|
|
1020
|
+
if (waitMs === undefined || !watching()) {
|
|
1021
|
+
report("failure", attemptNumber);
|
|
1022
|
+
throw error;
|
|
1023
|
+
}
|
|
1024
|
+
report("retry", attemptNumber);
|
|
1025
|
+
const waited = await waitForRetry(waitMs);
|
|
1026
|
+
if (!waited || !guard() || !watching()) {
|
|
1027
|
+
report("cancelled", attemptNumber);
|
|
1028
|
+
return CANCELLED;
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
function waitForRetry(ms) {
|
|
1034
|
+
return new Promise((resolve) => {
|
|
1035
|
+
let settled = false;
|
|
1036
|
+
let cancelDelay;
|
|
1037
|
+
const settle = (waited) => {
|
|
1038
|
+
if (settled)
|
|
1039
|
+
return;
|
|
1040
|
+
settled = true;
|
|
1041
|
+
retryWaits.delete(abort);
|
|
1042
|
+
cancelDelay?.();
|
|
1043
|
+
resolve(waited);
|
|
1044
|
+
};
|
|
1045
|
+
const abort = () => settle(false);
|
|
1046
|
+
retryWaits.add(abort);
|
|
1047
|
+
const cancel = deps.delay(() => settle(true), ms);
|
|
1048
|
+
if (settled)
|
|
1049
|
+
cancel();
|
|
1050
|
+
else
|
|
1051
|
+
cancelDelay = cancel;
|
|
1052
|
+
});
|
|
1053
|
+
}
|
|
1054
|
+
function cancelRetries() {
|
|
1055
|
+
for (const abort of [...retryWaits])
|
|
1056
|
+
abort();
|
|
1057
|
+
}
|
|
1058
|
+
async function persisted(write) {
|
|
1059
|
+
const startedAt = now();
|
|
1060
|
+
try {
|
|
1061
|
+
await write();
|
|
1062
|
+
return true;
|
|
1063
|
+
} catch {
|
|
1064
|
+
persistence = "unavailable";
|
|
1065
|
+
emit({
|
|
1066
|
+
operation: "persistence",
|
|
1067
|
+
outcome: "failure",
|
|
1068
|
+
durationMs: now() - startedAt,
|
|
1069
|
+
attempt: 1,
|
|
1070
|
+
sessionsRead: 0,
|
|
1071
|
+
messagesRead: 0
|
|
1072
|
+
});
|
|
1073
|
+
return false;
|
|
1074
|
+
}
|
|
1075
|
+
}
|
|
1076
|
+
function hydrateShards() {
|
|
1077
|
+
if (hydrated)
|
|
1078
|
+
return;
|
|
1079
|
+
hydrated = true;
|
|
1080
|
+
const startedAt = now();
|
|
1081
|
+
try {
|
|
1082
|
+
for (const shard of deps.repository.loadAllShards().values()) {
|
|
1083
|
+
for (const entry of Object.values(shard.index)) {
|
|
1084
|
+
if (!tombstones.has(entry.sessionID))
|
|
1085
|
+
index.set(entry.sessionID, entry);
|
|
1086
|
+
}
|
|
1087
|
+
for (const record of Object.values(shard.records)) {
|
|
1088
|
+
if (!tombstones.has(record.sessionID))
|
|
1089
|
+
records3.set(record.sessionID, record);
|
|
1090
|
+
}
|
|
1091
|
+
}
|
|
1092
|
+
emit({
|
|
1093
|
+
operation: "hydrate",
|
|
1094
|
+
outcome: "success",
|
|
1095
|
+
durationMs: now() - startedAt,
|
|
1096
|
+
attempt: 1,
|
|
1097
|
+
sessionsRead: 0,
|
|
1098
|
+
messagesRead: 0
|
|
1099
|
+
});
|
|
1100
|
+
} catch {
|
|
1101
|
+
emit({
|
|
1102
|
+
operation: "hydrate",
|
|
1103
|
+
outcome: "failure",
|
|
1104
|
+
durationMs: now() - startedAt,
|
|
1105
|
+
attempt: 1,
|
|
1106
|
+
sessionsRead: 0,
|
|
1107
|
+
messagesRead: 0
|
|
1108
|
+
});
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
function forgetSession(sessionID) {
|
|
1112
|
+
records3.delete(sessionID);
|
|
1113
|
+
index.delete(sessionID);
|
|
1114
|
+
dirty.delete(sessionID);
|
|
1115
|
+
pendingRecords = pendingRecords.filter((record) => record.sessionID !== sessionID);
|
|
1116
|
+
}
|
|
1117
|
+
function workOf(sessionID) {
|
|
1118
|
+
const existing = work.get(sessionID);
|
|
1119
|
+
if (existing !== undefined)
|
|
1120
|
+
return existing;
|
|
1121
|
+
const fresh = { revision: 0, covered: 0, inFlight: false, pending: false, followUp: false };
|
|
1122
|
+
work.set(sessionID, fresh);
|
|
1123
|
+
return fresh;
|
|
1124
|
+
}
|
|
1125
|
+
function scanTokenOf(sessionID) {
|
|
1126
|
+
return scanTokens.get(sessionID) ?? 0;
|
|
1127
|
+
}
|
|
1128
|
+
function coverageFor(directory) {
|
|
1129
|
+
const key = directoryKeyOf(directory);
|
|
1130
|
+
const cached = coverage.get(key);
|
|
1131
|
+
if (cached !== undefined)
|
|
1132
|
+
return cached;
|
|
1133
|
+
const stored = deps.repository.readCoverage(directory);
|
|
1134
|
+
if (stored !== undefined)
|
|
1135
|
+
coverage.set(key, stored);
|
|
1136
|
+
return stored;
|
|
1137
|
+
}
|
|
1138
|
+
function statusFor(directory) {
|
|
1139
|
+
const key = directoryKeyOf(directory);
|
|
1140
|
+
let status = statuses.get(key);
|
|
1141
|
+
if (status === undefined) {
|
|
1142
|
+
status = { refreshing: false, failed: false, publishedAt: undefined, publishCancel: undefined };
|
|
1143
|
+
statuses.set(key, status);
|
|
1144
|
+
}
|
|
1145
|
+
return status;
|
|
1146
|
+
}
|
|
1147
|
+
function viewsOf(directory) {
|
|
1148
|
+
const matching = [];
|
|
1149
|
+
for (const entry of views.values()) {
|
|
1150
|
+
if (entry.request.directory === directory)
|
|
1151
|
+
matching.push(entry);
|
|
1152
|
+
}
|
|
1153
|
+
return matching;
|
|
1154
|
+
}
|
|
1155
|
+
function requestedDirectories() {
|
|
1156
|
+
const directories = [];
|
|
1157
|
+
for (const entry of views.values()) {
|
|
1158
|
+
if (!directories.some((directory) => directory === entry.request.directory)) {
|
|
1159
|
+
directories.push(entry.request.directory);
|
|
1160
|
+
}
|
|
1161
|
+
}
|
|
1162
|
+
if (directories.length === 0 && holds > 0)
|
|
1163
|
+
directories.push(undefined);
|
|
1164
|
+
return directories;
|
|
1165
|
+
}
|
|
1166
|
+
function affectedDirectories(sessionDirectory) {
|
|
1167
|
+
return requestedDirectories().filter((directory) => matchesScope(sessionDirectory, directory));
|
|
1168
|
+
}
|
|
1169
|
+
function countIndex(directory) {
|
|
1170
|
+
let count = 0;
|
|
1171
|
+
for (const entry of index.values())
|
|
1172
|
+
if (matchesScope(entry.directory, directory))
|
|
1173
|
+
count += 1;
|
|
1174
|
+
return count;
|
|
1175
|
+
}
|
|
1176
|
+
function liveView(request) {
|
|
1177
|
+
const fromMs = rangeStartMs(request.range, now());
|
|
1178
|
+
const samples = [];
|
|
1179
|
+
const unavailable = [];
|
|
1180
|
+
for (const entry of index.values()) {
|
|
1181
|
+
if (!matchesScope(entry.directory, request.directory))
|
|
1182
|
+
continue;
|
|
1183
|
+
if (fromMs !== undefined && entry.updatedAt < fromMs)
|
|
1184
|
+
continue;
|
|
1185
|
+
const record = records3.get(entry.sessionID);
|
|
1186
|
+
if (record === undefined)
|
|
1187
|
+
continue;
|
|
1188
|
+
const inRange = inRangeSamples(record, fromMs);
|
|
1189
|
+
samples.push(...inRange.samples);
|
|
1190
|
+
unavailable.push(...inRange.unavailable);
|
|
1191
|
+
}
|
|
1192
|
+
return { samples, unavailable };
|
|
1193
|
+
}
|
|
1194
|
+
function buildState(request, kind, lastCheckedAt, refreshing, refreshFailed) {
|
|
1195
|
+
const data = liveView(request);
|
|
1196
|
+
const hasData = data.samples.length > 0 || data.unavailable.length > 0;
|
|
1197
|
+
return {
|
|
1198
|
+
kind: kind === "partial" && !hasData ? "loading" : kind,
|
|
1199
|
+
samples: data.samples,
|
|
1200
|
+
unavailable: data.unavailable,
|
|
1201
|
+
variants: collectVariants(data.samples, data.unavailable),
|
|
1202
|
+
scanned: countIndex(request.directory),
|
|
1203
|
+
lastCheckedAt,
|
|
1204
|
+
refreshing,
|
|
1205
|
+
refreshFailed,
|
|
1206
|
+
persistence
|
|
1207
|
+
};
|
|
1208
|
+
}
|
|
1209
|
+
function publishViews(directory, mode) {
|
|
1210
|
+
const status = statusFor(directory);
|
|
1211
|
+
for (const entry of viewsOf(directory)) {
|
|
1212
|
+
const kind = mode === "ready" ? "ready" : mode === "failure" ? "error" : "partial";
|
|
1213
|
+
entry.publish(buildState(entry.request, kind, entry.lastCheckedAt, mode !== "ready", mode === "failure" || status.failed));
|
|
1214
|
+
}
|
|
1215
|
+
if (mode === "failure")
|
|
1216
|
+
publishWidgetFailure();
|
|
1217
|
+
else
|
|
1218
|
+
publishWidget(mode === "ready");
|
|
1219
|
+
}
|
|
1220
|
+
function publishWidget(persist) {
|
|
1221
|
+
const nowMs = now();
|
|
1222
|
+
const rows = widgetRows(allSamples(), nowMs);
|
|
1223
|
+
widgetPublished = true;
|
|
1224
|
+
setWidgetState({ status: "ready", rows, stale: false });
|
|
1225
|
+
if (persist)
|
|
1226
|
+
persistWidget(rows, nowMs);
|
|
1227
|
+
}
|
|
1228
|
+
function publishWidgetFailure() {
|
|
1229
|
+
const current = widgetState();
|
|
1230
|
+
setWidgetState(widgetPublished ? { status: "ready", rows: current.rows, stale: true } : { status: "error", rows: [], stale: false });
|
|
1231
|
+
}
|
|
1232
|
+
async function persistWidget(rows, nowMs) {
|
|
1233
|
+
await persisted(() => deps.repository.writeWidgetSnapshot(rows, nowMs));
|
|
1234
|
+
}
|
|
1235
|
+
function allSamples() {
|
|
1236
|
+
const samples = [];
|
|
1237
|
+
for (const record of records3.values())
|
|
1238
|
+
samples.push(...record.samples);
|
|
1239
|
+
return samples;
|
|
1240
|
+
}
|
|
1241
|
+
async function flushRecords() {
|
|
1242
|
+
if (pendingRecords.length === 0)
|
|
1243
|
+
return;
|
|
1244
|
+
const generation = resetGeneration;
|
|
1245
|
+
const batch = pendingRecords;
|
|
1246
|
+
pendingRecords = [];
|
|
1247
|
+
const flush = (async () => {
|
|
1248
|
+
const written = await persisted(() => deps.repository.putRecords(batch, now()));
|
|
1249
|
+
if (written)
|
|
1250
|
+
persistence = "ok";
|
|
1251
|
+
else if (generation === resetGeneration)
|
|
1252
|
+
pendingRecords = [...batch, ...pendingRecords];
|
|
1253
|
+
})();
|
|
1254
|
+
flushInFlight = flush;
|
|
1255
|
+
try {
|
|
1256
|
+
await flush;
|
|
1257
|
+
} finally {
|
|
1258
|
+
if (flushInFlight === flush)
|
|
1259
|
+
flushInFlight = undefined;
|
|
1260
|
+
}
|
|
1261
|
+
}
|
|
1262
|
+
async function readMeta(scope, sessionID) {
|
|
1263
|
+
const get = scope.client.session.get;
|
|
1264
|
+
if (get === undefined)
|
|
1265
|
+
return;
|
|
1266
|
+
try {
|
|
1267
|
+
return await get.call(scope.client.session, { sessionID });
|
|
1268
|
+
} catch (error) {
|
|
1269
|
+
if (isNotFound(error))
|
|
1270
|
+
return;
|
|
1271
|
+
throw error;
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
async function scanSession(scope, meta) {
|
|
1275
|
+
if (tombstones.has(meta.id)) {
|
|
1276
|
+
forgetSession(meta.id);
|
|
1277
|
+
return { meta: undefined, requeue: false, discarded: true };
|
|
1278
|
+
}
|
|
1279
|
+
const state = workOf(meta.id);
|
|
1280
|
+
if (state.inFlight) {
|
|
1281
|
+
state.followUp = true;
|
|
1282
|
+
return;
|
|
1283
|
+
}
|
|
1284
|
+
state.inFlight = true;
|
|
1285
|
+
const revision = state.revision;
|
|
1286
|
+
const generation = resetGeneration;
|
|
1287
|
+
const token = scanTokenOf(meta.id);
|
|
1288
|
+
const current = () => generation === resetGeneration && scanTokenOf(meta.id) === token;
|
|
1289
|
+
try {
|
|
1290
|
+
const fresh = await readMeta(scope, meta.id) ?? meta;
|
|
1291
|
+
if (!current())
|
|
1292
|
+
return { meta: undefined, requeue: false, discarded: true };
|
|
1293
|
+
const scan = await scanMessages(scope.client, fresh);
|
|
1294
|
+
if (!current())
|
|
1295
|
+
return { meta: undefined, requeue: false, discarded: true };
|
|
1296
|
+
const after = await readMeta(scope, fresh.id) ?? fresh;
|
|
1297
|
+
if (!current())
|
|
1298
|
+
return { meta: undefined, requeue: false, discarded: true };
|
|
1299
|
+
if (tombstones.has(fresh.id)) {
|
|
1300
|
+
forgetSession(fresh.id);
|
|
1301
|
+
return { meta: after, requeue: false, discarded: true };
|
|
1302
|
+
}
|
|
1303
|
+
const record = recordFrom(fresh, scan, now());
|
|
1304
|
+
records3.set(record.sessionID, record);
|
|
1305
|
+
index.set(record.sessionID, {
|
|
1306
|
+
sessionID: record.sessionID,
|
|
1307
|
+
directory: record.directory,
|
|
1308
|
+
updatedAt: record.updatedAt
|
|
1309
|
+
});
|
|
1310
|
+
pendingRecords.push(record);
|
|
1311
|
+
return { meta: after, requeue: signatureFor(after) !== signatureFor(fresh), discarded: false };
|
|
1312
|
+
} finally {
|
|
1313
|
+
state.inFlight = false;
|
|
1314
|
+
state.covered = Math.max(state.covered, revision);
|
|
1315
|
+
if (state.followUp || state.revision > state.covered) {
|
|
1316
|
+
state.followUp = false;
|
|
1317
|
+
scheduleSessionScan(meta.id);
|
|
1318
|
+
}
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
function cancelPublish(directory) {
|
|
1322
|
+
const status = statuses.get(directoryKeyOf(directory));
|
|
1323
|
+
if (status?.publishCancel === undefined)
|
|
1324
|
+
return;
|
|
1325
|
+
status.publishCancel();
|
|
1326
|
+
status.publishCancel = undefined;
|
|
1327
|
+
}
|
|
1328
|
+
function cancelPublications() {
|
|
1329
|
+
for (const status of statuses.values()) {
|
|
1330
|
+
if (status.publishCancel === undefined)
|
|
1331
|
+
continue;
|
|
1332
|
+
status.publishCancel();
|
|
1333
|
+
status.publishCancel = undefined;
|
|
1334
|
+
}
|
|
1335
|
+
}
|
|
1336
|
+
function schedulePublish(directory) {
|
|
1337
|
+
const status = statusFor(directory);
|
|
1338
|
+
if (status.publishCancel !== undefined)
|
|
1339
|
+
return;
|
|
1340
|
+
const elapsed = now() - (status.publishedAt ?? now());
|
|
1341
|
+
status.publishCancel = deps.delay(() => {
|
|
1342
|
+
status.publishCancel = undefined;
|
|
1343
|
+
publishDeferred(directory);
|
|
1344
|
+
}, Math.max(0, PUBLISH_INTERVAL_MS - elapsed));
|
|
1345
|
+
}
|
|
1346
|
+
async function publishDeferred(directory) {
|
|
1347
|
+
try {
|
|
1348
|
+
await flushRecords();
|
|
1349
|
+
publishViews(directory, "progress");
|
|
1350
|
+
statusFor(directory).publishedAt = now();
|
|
1351
|
+
} catch {
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
async function publishProgress(directory) {
|
|
1355
|
+
const status = statusFor(directory);
|
|
1356
|
+
if (status.publishedAt !== undefined && now() - status.publishedAt < PUBLISH_INTERVAL_MS) {
|
|
1357
|
+
schedulePublish(directory);
|
|
1358
|
+
return;
|
|
1359
|
+
}
|
|
1360
|
+
await flushRecords();
|
|
1361
|
+
publishViews(directory, "progress");
|
|
1362
|
+
status.publishedAt = now();
|
|
1363
|
+
}
|
|
1364
|
+
async function drain(scope, directory, items, onRequeue) {
|
|
1365
|
+
let cursor = 0;
|
|
1366
|
+
let abandoned = false;
|
|
1367
|
+
let failed = false;
|
|
1368
|
+
const worker = async () => {
|
|
1369
|
+
try {
|
|
1370
|
+
for (;; ) {
|
|
1371
|
+
if (holds === 0) {
|
|
1372
|
+
abandoned = true;
|
|
1373
|
+
return;
|
|
1374
|
+
}
|
|
1375
|
+
if (failed)
|
|
1376
|
+
return;
|
|
1377
|
+
const position = cursor;
|
|
1378
|
+
cursor += 1;
|
|
1379
|
+
if (position >= items.length)
|
|
1380
|
+
return;
|
|
1381
|
+
const outcome = await scanSession(scope, items[position]);
|
|
1382
|
+
if (outcome?.requeue === true && outcome.meta !== undefined)
|
|
1383
|
+
onRequeue(outcome.meta);
|
|
1384
|
+
await publishProgress(directory);
|
|
1385
|
+
}
|
|
1386
|
+
} catch (error) {
|
|
1387
|
+
failed = true;
|
|
1388
|
+
throw error;
|
|
1389
|
+
}
|
|
1390
|
+
};
|
|
1391
|
+
const workers = Math.min(concurrency, Math.max(1, items.length));
|
|
1392
|
+
const settled = await Promise.allSettled(Array.from({ length: workers }, () => worker()));
|
|
1393
|
+
const rejection = settled.find((result) => result.status === "rejected");
|
|
1394
|
+
if (rejection?.status === "rejected")
|
|
1395
|
+
throw rejection.reason;
|
|
1396
|
+
return abandoned;
|
|
1397
|
+
}
|
|
1398
|
+
async function commitCoverage(directory, startedAt, inventory, previous) {
|
|
1399
|
+
const next = {
|
|
1400
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
1401
|
+
directoryFilter: directory,
|
|
1402
|
+
fullInventoryCompletedAt: inventory ? startedAt : previous?.fullInventoryCompletedAt,
|
|
1403
|
+
recentCatchUpAt: startedAt
|
|
1404
|
+
};
|
|
1405
|
+
if (await persisted(() => deps.repository.writeCoverage(next)))
|
|
1406
|
+
coverage.set(directoryKeyOf(directory), next);
|
|
1407
|
+
}
|
|
1408
|
+
async function publishReady(directory) {
|
|
1409
|
+
cancelPublish(directory);
|
|
1410
|
+
await flushRecords();
|
|
1411
|
+
const checkedAt = now();
|
|
1412
|
+
for (const entry of viewsOf(directory)) {
|
|
1413
|
+
const data = liveView(entry.request);
|
|
1414
|
+
entry.lastCheckedAt = checkedAt;
|
|
1415
|
+
const snapshot = {
|
|
1416
|
+
schemaVersion: CACHE_SCHEMA_VERSION,
|
|
1417
|
+
metricVersion: METRIC_VERSION,
|
|
1418
|
+
directoryFilter: entry.request.directory,
|
|
1419
|
+
range: entry.request.range,
|
|
1420
|
+
rows: comparisonRows(data.samples, data.unavailable),
|
|
1421
|
+
lastCheckedAt: checkedAt,
|
|
1422
|
+
savedAtMs: checkedAt
|
|
1423
|
+
};
|
|
1424
|
+
await persisted(() => deps.repository.writeViewSnapshot(snapshot));
|
|
1425
|
+
}
|
|
1426
|
+
statusFor(directory).publishedAt = checkedAt;
|
|
1427
|
+
publishViews(directory, "ready");
|
|
1428
|
+
}
|
|
1429
|
+
async function probeSession(scope, sessionID) {
|
|
1430
|
+
const get = scope.client.session.get;
|
|
1431
|
+
if (get === undefined)
|
|
1432
|
+
return "unknown";
|
|
1433
|
+
try {
|
|
1434
|
+
await get.call(scope.client.session, { sessionID });
|
|
1435
|
+
return "present";
|
|
1436
|
+
} catch (error) {
|
|
1437
|
+
if (isNotFound(error))
|
|
1438
|
+
return "missing";
|
|
1439
|
+
throw error;
|
|
1440
|
+
}
|
|
1441
|
+
}
|
|
1442
|
+
async function verifyInventory(scope, listed, known) {
|
|
1443
|
+
if (scope.client.session.get === undefined)
|
|
1444
|
+
return;
|
|
1445
|
+
const tombstoned = [...tombstones];
|
|
1446
|
+
let removed = false;
|
|
1447
|
+
let verified = true;
|
|
1448
|
+
for (const sessionID of known) {
|
|
1449
|
+
if (listed.has(sessionID) || tombstones.has(sessionID))
|
|
1450
|
+
continue;
|
|
1451
|
+
const state = await probeSession(scope, sessionID);
|
|
1452
|
+
if (state === "missing") {
|
|
1453
|
+
await deleteSession(sessionID);
|
|
1454
|
+
removed = true;
|
|
1455
|
+
} else if (state === "unknown") {
|
|
1456
|
+
verified = false;
|
|
1457
|
+
}
|
|
1458
|
+
}
|
|
1459
|
+
if (removed || !verified)
|
|
1460
|
+
return;
|
|
1461
|
+
for (const sessionID of tombstoned) {
|
|
1462
|
+
if (await probeSession(scope, sessionID) !== "missing") {
|
|
1463
|
+
verified = false;
|
|
1464
|
+
break;
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
if (!verified || tombstoned.length === 0)
|
|
1468
|
+
return;
|
|
1469
|
+
if (await persisted(() => deps.repository.clearTombstones(now())))
|
|
1470
|
+
tombstones.clear();
|
|
1471
|
+
}
|
|
1472
|
+
async function deleteSession(sessionID) {
|
|
1473
|
+
scanTokens.set(sessionID, scanTokenOf(sessionID) + 1);
|
|
1474
|
+
tombstones.add(sessionID);
|
|
1475
|
+
const directory = records3.get(sessionID)?.directory ?? index.get(sessionID)?.directory;
|
|
1476
|
+
forgetSession(sessionID);
|
|
1477
|
+
await persisted(() => deps.repository.removeSessions([sessionID], now()));
|
|
1478
|
+
await publishSessionWork([directory], "settled");
|
|
1479
|
+
}
|
|
1480
|
+
function unsettled() {
|
|
1481
|
+
for (const state of work.values()) {
|
|
1482
|
+
if (state.inFlight || state.pending || state.followUp || state.revision > state.covered)
|
|
1483
|
+
return true;
|
|
1484
|
+
}
|
|
1485
|
+
return false;
|
|
1486
|
+
}
|
|
1487
|
+
async function publishSessionWork(sessionDirectories, mode) {
|
|
1488
|
+
const targets = new Set;
|
|
1489
|
+
for (const sessionDirectory of sessionDirectories) {
|
|
1490
|
+
for (const directory of affectedDirectories(sessionDirectory))
|
|
1491
|
+
targets.add(directory);
|
|
1492
|
+
}
|
|
1493
|
+
if (targets.size === 0) {
|
|
1494
|
+
await flushRecords();
|
|
1495
|
+
publishWidget(false);
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
for (const directory of targets) {
|
|
1499
|
+
if (mode === "settled" && !statusFor(directory).refreshing && !unsettled()) {
|
|
1500
|
+
await publishReady(directory);
|
|
1501
|
+
} else {
|
|
1502
|
+
await publishProgress(directory);
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
function publishSessionFailure(sessionDirectory) {
|
|
1507
|
+
const targets = affectedDirectories(sessionDirectory);
|
|
1508
|
+
if (targets.length === 0) {
|
|
1509
|
+
publishWidgetFailure();
|
|
1510
|
+
return;
|
|
1511
|
+
}
|
|
1512
|
+
for (const directory of targets) {
|
|
1513
|
+
const status = statusFor(directory);
|
|
1514
|
+
status.refreshing = true;
|
|
1515
|
+
status.failed = true;
|
|
1516
|
+
publishViews(directory, "failure");
|
|
1517
|
+
}
|
|
1518
|
+
}
|
|
1519
|
+
async function scanOne(operation, sessionID) {
|
|
1520
|
+
const generation = resetGeneration;
|
|
1521
|
+
let directory;
|
|
1522
|
+
try {
|
|
1523
|
+
const outcome = await acquire(operation, () => generation === resetGeneration, () => holds > 0, async (scope) => {
|
|
1524
|
+
const meta = await readMeta(scope, sessionID);
|
|
1525
|
+
if (meta === undefined)
|
|
1526
|
+
return false;
|
|
1527
|
+
directory = meta.location?.directory;
|
|
1528
|
+
if (tombstones.has(sessionID)) {
|
|
1529
|
+
forgetSession(sessionID);
|
|
1530
|
+
return false;
|
|
1531
|
+
}
|
|
1532
|
+
await scanSession(scope, meta);
|
|
1533
|
+
return true;
|
|
1534
|
+
});
|
|
1535
|
+
if (outcome === CANCELLED)
|
|
1536
|
+
return { scanned: false, directory: undefined };
|
|
1537
|
+
return { scanned: outcome, directory };
|
|
1538
|
+
} catch {
|
|
1539
|
+
publishSessionFailure(directory ?? records3.get(sessionID)?.directory ?? index.get(sessionID)?.directory);
|
|
1540
|
+
return { scanned: false, directory };
|
|
1541
|
+
}
|
|
1542
|
+
}
|
|
1543
|
+
function scheduleSessionScan(sessionID) {
|
|
1544
|
+
const state = workOf(sessionID);
|
|
1545
|
+
if (state.pending || state.inFlight) {
|
|
1546
|
+
if (state.revision > state.covered)
|
|
1547
|
+
state.followUp = true;
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
if (state.revision <= state.covered)
|
|
1551
|
+
return;
|
|
1552
|
+
state.pending = true;
|
|
1553
|
+
Promise.resolve().then(async () => {
|
|
1554
|
+
state.pending = false;
|
|
1555
|
+
const result = await scanOne("event", sessionID);
|
|
1556
|
+
if (!result.scanned)
|
|
1557
|
+
return;
|
|
1558
|
+
await publishSessionWork([result.directory], "progress");
|
|
1559
|
+
}).catch(() => {
|
|
1560
|
+
}).finally(() => {
|
|
1561
|
+
state.pending = false;
|
|
1562
|
+
});
|
|
1563
|
+
}
|
|
1564
|
+
function scheduleDirtyFlush() {
|
|
1565
|
+
if (holds === 0 || dirty.size === 0)
|
|
1566
|
+
return;
|
|
1567
|
+
const at = now();
|
|
1568
|
+
if (burstStartedAt === undefined)
|
|
1569
|
+
burstStartedAt = at;
|
|
1570
|
+
burstCancel?.();
|
|
1571
|
+
burstCancel = undefined;
|
|
1572
|
+
const remaining = Math.max(0, burstStartedAt + MAX_SCHEDULE_DELAY_MS - at);
|
|
1573
|
+
burstCancel = deps.delay(() => {
|
|
1574
|
+
burstCancel = undefined;
|
|
1575
|
+
flushDirty();
|
|
1576
|
+
}, Math.min(DIRTY_DEBOUNCE_MS, remaining));
|
|
1577
|
+
}
|
|
1578
|
+
function flushDirty() {
|
|
1579
|
+
burstStartedAt = undefined;
|
|
1580
|
+
if (holds === 0)
|
|
1581
|
+
return;
|
|
1582
|
+
const sessions = [...dirty];
|
|
1583
|
+
dirty.clear();
|
|
1584
|
+
for (const sessionID of sessions) {
|
|
1585
|
+
const state = workOf(sessionID);
|
|
1586
|
+
state.revision += 1;
|
|
1587
|
+
scheduleSessionScan(sessionID);
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
function markDirty(sessionID) {
|
|
1591
|
+
if (tombstones.has(sessionID))
|
|
1592
|
+
return;
|
|
1593
|
+
dirty.add(sessionID);
|
|
1594
|
+
scheduleDirtyFlush();
|
|
1595
|
+
}
|
|
1596
|
+
function auditCandidates() {
|
|
1597
|
+
return [...index.values()].filter((entry) => !tombstones.has(entry.sessionID)).map((entry) => ({ entry, scannedAt: records3.get(entry.sessionID)?.scannedAtMs ?? 0 })).sort((left, right) => left.scannedAt - right.scannedAt || left.entry.sessionID.localeCompare(right.entry.sessionID)).slice(0, AUDIT_BATCH_SESSIONS).map((candidate) => candidate.entry.sessionID);
|
|
1598
|
+
}
|
|
1599
|
+
async function runAudit() {
|
|
1600
|
+
if (holds === 0)
|
|
1601
|
+
return;
|
|
1602
|
+
const directories = [];
|
|
1603
|
+
for (const sessionID of auditCandidates()) {
|
|
1604
|
+
if (holds === 0)
|
|
1605
|
+
break;
|
|
1606
|
+
const result = await scanOne("audit", sessionID);
|
|
1607
|
+
if (result.scanned)
|
|
1608
|
+
directories.push(result.directory);
|
|
1609
|
+
}
|
|
1610
|
+
if (directories.length > 0)
|
|
1611
|
+
await publishSessionWork(directories, "settled");
|
|
1612
|
+
}
|
|
1613
|
+
async function runPass(directory) {
|
|
1614
|
+
hydrateShards();
|
|
1615
|
+
const status = statusFor(directory);
|
|
1616
|
+
status.refreshing = true;
|
|
1617
|
+
const startedAt = now();
|
|
1618
|
+
const generation = resetGeneration;
|
|
1619
|
+
const previous = coverageFor(directory);
|
|
1620
|
+
const inventory = previous?.fullInventoryCompletedAt === undefined || startedAt - previous.fullInventoryCompletedAt > INVENTORY_MIN_INTERVAL_MS;
|
|
1621
|
+
try {
|
|
1622
|
+
const outcome = await acquire(inventory ? "inventory" : "catch-up", () => generation === resetGeneration, () => holds > 0, async (scope) => {
|
|
1623
|
+
const listing = await listSessions(scope.client, inventory || previous?.recentCatchUpAt === undefined ? { directory } : { directory, fromMs: previous.recentCatchUpAt });
|
|
1624
|
+
if (generation !== resetGeneration)
|
|
1625
|
+
return CANCELLED;
|
|
1626
|
+
rememberTombstones();
|
|
1627
|
+
const known = [];
|
|
1628
|
+
if (inventory) {
|
|
1629
|
+
for (const entry of index.values()) {
|
|
1630
|
+
if (tombstones.has(entry.sessionID))
|
|
1631
|
+
continue;
|
|
1632
|
+
if (!matchesScope(entry.directory, directory))
|
|
1633
|
+
continue;
|
|
1634
|
+
known.push(entry.sessionID);
|
|
1635
|
+
}
|
|
1636
|
+
}
|
|
1637
|
+
const listed = new Set;
|
|
1638
|
+
const queue = [];
|
|
1639
|
+
for (const meta of listing.sessions) {
|
|
1640
|
+
listed.add(meta.id);
|
|
1641
|
+
if (tombstones.has(meta.id)) {
|
|
1642
|
+
forgetSession(meta.id);
|
|
1643
|
+
continue;
|
|
1644
|
+
}
|
|
1645
|
+
const existing = records3.get(meta.id);
|
|
1646
|
+
if (existing !== undefined && existing.signature === signatureFor(meta))
|
|
1647
|
+
continue;
|
|
1648
|
+
queue.push(meta);
|
|
1649
|
+
}
|
|
1650
|
+
if (inventory)
|
|
1651
|
+
await verifyInventory(scope, listed, known);
|
|
1652
|
+
const requeue = [];
|
|
1653
|
+
const abandoned = await drain(scope, directory, queue, (meta) => {
|
|
1654
|
+
if (!requeue.some((item) => item.id === meta.id))
|
|
1655
|
+
requeue.push(meta);
|
|
1656
|
+
});
|
|
1657
|
+
if (abandoned) {
|
|
1658
|
+
cancelPublish(directory);
|
|
1659
|
+
return CANCELLED;
|
|
1660
|
+
}
|
|
1661
|
+
const abandonedAgain = await drain(scope, directory, requeue, () => {
|
|
1662
|
+
});
|
|
1663
|
+
if (abandonedAgain) {
|
|
1664
|
+
cancelPublish(directory);
|
|
1665
|
+
return CANCELLED;
|
|
1666
|
+
}
|
|
1667
|
+
if (generation !== resetGeneration)
|
|
1668
|
+
return CANCELLED;
|
|
1669
|
+
await flushRecords();
|
|
1670
|
+
if (generation !== resetGeneration)
|
|
1671
|
+
return CANCELLED;
|
|
1672
|
+
await commitCoverage(directory, startedAt, inventory, previous);
|
|
1673
|
+
return generation === resetGeneration ? "settled" : CANCELLED;
|
|
1674
|
+
});
|
|
1675
|
+
if (outcome !== "settled") {
|
|
1676
|
+
status.refreshing = true;
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
status.refreshing = false;
|
|
1680
|
+
status.failed = false;
|
|
1681
|
+
await publishReady(directory);
|
|
1682
|
+
} catch {
|
|
1683
|
+
if (generation !== resetGeneration) {
|
|
1684
|
+
status.refreshing = true;
|
|
1685
|
+
return;
|
|
1686
|
+
}
|
|
1687
|
+
status.refreshing = true;
|
|
1688
|
+
status.failed = true;
|
|
1689
|
+
cancelPublish(directory);
|
|
1690
|
+
publishViews(directory, "failure");
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
function ensurePass(directory) {
|
|
1694
|
+
const key = directoryKeyOf(directory);
|
|
1695
|
+
const slot = passes.get(key);
|
|
1696
|
+
if (slot?.inFlight === true) {
|
|
1697
|
+
slot.queued = true;
|
|
1698
|
+
return;
|
|
1699
|
+
}
|
|
1700
|
+
const next = { inFlight: true, queued: false };
|
|
1701
|
+
passes.set(key, next);
|
|
1702
|
+
runPass(directory).finally(() => {
|
|
1703
|
+
next.inFlight = false;
|
|
1704
|
+
if (next.queued) {
|
|
1705
|
+
next.queued = false;
|
|
1706
|
+
ensurePass(directory);
|
|
1707
|
+
}
|
|
1708
|
+
});
|
|
1709
|
+
}
|
|
1710
|
+
function schedulePass(directory) {
|
|
1711
|
+
if (holds === 0)
|
|
1712
|
+
return;
|
|
1713
|
+
Promise.resolve().then(() => {
|
|
1714
|
+
if (holds === 0 || !requestedDirectories().some((requested) => requested === directory))
|
|
1715
|
+
return;
|
|
1716
|
+
ensurePass(directory);
|
|
1717
|
+
});
|
|
1718
|
+
}
|
|
1719
|
+
function startTimers() {
|
|
1720
|
+
if (timerCancel !== undefined)
|
|
1721
|
+
return;
|
|
1722
|
+
timerCancel = deps.setTimer(() => {
|
|
1723
|
+
if (holds === 0)
|
|
1724
|
+
return;
|
|
1725
|
+
for (const directory of requestedDirectories())
|
|
1726
|
+
ensurePass(directory);
|
|
1727
|
+
}, RECENT_CATCHUP_MS);
|
|
1728
|
+
auditCancel = deps.setTimer(() => {
|
|
1729
|
+
if (auditInFlight)
|
|
1730
|
+
return;
|
|
1731
|
+
auditInFlight = true;
|
|
1732
|
+
runAudit().catch(() => {
|
|
1733
|
+
}).finally(() => {
|
|
1734
|
+
auditInFlight = false;
|
|
1735
|
+
});
|
|
1736
|
+
}, AUDIT_INTERVAL_MS);
|
|
1737
|
+
scheduleDirtyFlush();
|
|
1738
|
+
for (const directory of requestedDirectories())
|
|
1739
|
+
schedulePass(directory);
|
|
1740
|
+
}
|
|
1741
|
+
function stopTimers() {
|
|
1742
|
+
cancelPublications();
|
|
1743
|
+
cancelRetries();
|
|
1744
|
+
burstCancel?.();
|
|
1745
|
+
burstCancel = undefined;
|
|
1746
|
+
burstStartedAt = undefined;
|
|
1747
|
+
if (auditCancel !== undefined) {
|
|
1748
|
+
auditCancel();
|
|
1749
|
+
auditCancel = undefined;
|
|
1750
|
+
}
|
|
1751
|
+
if (timerCancel !== undefined) {
|
|
1752
|
+
timerCancel();
|
|
1753
|
+
timerCancel = undefined;
|
|
1754
|
+
}
|
|
1755
|
+
}
|
|
1756
|
+
function view(request) {
|
|
1757
|
+
const key = viewKeyOf(request);
|
|
1758
|
+
const existing = views.get(key);
|
|
1759
|
+
if (existing !== undefined)
|
|
1760
|
+
return existing.accessor;
|
|
1761
|
+
const snapshot = deps.repository.readViewSnapshot(request.directory, request.range);
|
|
1762
|
+
const lastCheckedAt = snapshot?.lastCheckedAt;
|
|
1763
|
+
hydrateShards();
|
|
1764
|
+
const [state, setState] = createSignal(buildState(request, snapshot === undefined ? "loading" : "cached", lastCheckedAt, true, false));
|
|
1765
|
+
views.set(key, { request, accessor: state, publish: setState, lastCheckedAt });
|
|
1766
|
+
schedulePass(request.directory);
|
|
1767
|
+
return state;
|
|
1768
|
+
}
|
|
1769
|
+
function hold() {
|
|
1770
|
+
holds += 1;
|
|
1771
|
+
if (holds === 1)
|
|
1772
|
+
startTimers();
|
|
1773
|
+
let released = false;
|
|
1774
|
+
return () => {
|
|
1775
|
+
if (released)
|
|
1776
|
+
return;
|
|
1777
|
+
released = true;
|
|
1778
|
+
holds -= 1;
|
|
1779
|
+
if (holds === 0)
|
|
1780
|
+
stopTimers();
|
|
1781
|
+
};
|
|
1782
|
+
}
|
|
1783
|
+
function refreshNow() {
|
|
1784
|
+
for (const directory of requestedDirectories())
|
|
1785
|
+
ensurePass(directory);
|
|
1786
|
+
}
|
|
1787
|
+
async function reset() {
|
|
1788
|
+
resetGeneration += 1;
|
|
1789
|
+
cancelRetries();
|
|
1790
|
+
cancelPublications();
|
|
1791
|
+
burstCancel?.();
|
|
1792
|
+
burstCancel = undefined;
|
|
1793
|
+
burstStartedAt = undefined;
|
|
1794
|
+
dirty.clear();
|
|
1795
|
+
records3.clear();
|
|
1796
|
+
index.clear();
|
|
1797
|
+
tombstones.clear();
|
|
1798
|
+
coverage.clear();
|
|
1799
|
+
pendingRecords = [];
|
|
1800
|
+
widgetPublished = false;
|
|
1801
|
+
setWidgetState({ status: "loading", rows: [], stale: false });
|
|
1802
|
+
for (const status of statuses.values()) {
|
|
1803
|
+
status.refreshing = true;
|
|
1804
|
+
status.failed = false;
|
|
1805
|
+
status.publishedAt = undefined;
|
|
1806
|
+
}
|
|
1807
|
+
for (const entry of views.values()) {
|
|
1808
|
+
entry.lastCheckedAt = undefined;
|
|
1809
|
+
entry.publish(buildState(entry.request, "loading", undefined, true, false));
|
|
1810
|
+
}
|
|
1811
|
+
await flushInFlight;
|
|
1812
|
+
await persisted(() => deps.repository.clear());
|
|
1813
|
+
for (const directory of requestedDirectories())
|
|
1814
|
+
ensurePass(directory);
|
|
1815
|
+
}
|
|
1816
|
+
function subscribeToEvents() {
|
|
1817
|
+
deps.subscribeEvents((event) => {
|
|
1818
|
+
const sessionID = event.data?.sessionID;
|
|
1819
|
+
if (typeof sessionID !== "string" || sessionID.length === 0)
|
|
1820
|
+
return;
|
|
1821
|
+
if (event.type === DELETED_EVENT_TYPE) {
|
|
1822
|
+
deleteSession(sessionID).catch(() => {
|
|
1823
|
+
});
|
|
1824
|
+
return;
|
|
1825
|
+
}
|
|
1826
|
+
if (!DIRTY_EVENT_TYPES.has(event.type))
|
|
1827
|
+
return;
|
|
1828
|
+
markDirty(sessionID);
|
|
238
1829
|
});
|
|
239
1830
|
}
|
|
240
|
-
|
|
1831
|
+
subscribeToEvents();
|
|
1832
|
+
return { view, widget: () => widgetState, hold, refreshNow, reset };
|
|
241
1833
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
1834
|
+
var DIRTY_DEBOUNCE_MS = 250;
|
|
1835
|
+
var MAX_SCHEDULE_DELAY_MS = 1000;
|
|
1836
|
+
var PUBLISH_INTERVAL_MS = 250;
|
|
1837
|
+
var RECENT_CATCHUP_MS = 60000;
|
|
1838
|
+
var INVENTORY_MIN_INTERVAL_MS = 1800000;
|
|
1839
|
+
var ACQUISITION_RETRY_DELAYS_MS = [1000, 3000];
|
|
1840
|
+
var AUDIT_INTERVAL_MS = 60000;
|
|
1841
|
+
var AUDIT_BATCH_SESSIONS = 20;
|
|
1842
|
+
var DIRTY_EVENT_TYPES = new Set([
|
|
1843
|
+
"session.step.ended",
|
|
1844
|
+
"session.step.failed",
|
|
1845
|
+
"session.execution.succeeded",
|
|
1846
|
+
"session.execution.failed",
|
|
1847
|
+
"session.execution.interrupted",
|
|
1848
|
+
"session.created",
|
|
1849
|
+
"session.forked",
|
|
1850
|
+
"session.moved",
|
|
1851
|
+
"session.revert.staged",
|
|
1852
|
+
"session.revert.committed",
|
|
1853
|
+
"session.compaction.ended",
|
|
1854
|
+
"session.compaction.failed",
|
|
1855
|
+
"session.usage.recorded"
|
|
1856
|
+
]);
|
|
1857
|
+
var DELETED_EVENT_TYPE = "session.deleted";
|
|
1858
|
+
var CANCELLED = Symbol("cancelled");
|
|
1859
|
+
var viewKeyOf = (request) => JSON.stringify([request.directory, request.range]);
|
|
1860
|
+
var directoryKeyOf = (directory) => JSON.stringify(directory ?? null);
|
|
1861
|
+
|
|
1862
|
+
// src/page/tokscope.tsx
|
|
1863
|
+
import {effect as _$effect6} from "@opentui/solid";
|
|
1864
|
+
import {insertNode as _$insertNode5} from "@opentui/solid";
|
|
1865
|
+
import {insert as _$insert6} from "@opentui/solid";
|
|
1866
|
+
import {createComponent as _$createComponent3} from "@opentui/solid";
|
|
1867
|
+
import {setProp as _$setProp6} from "@opentui/solid";
|
|
1868
|
+
import {createElement as _$createElement6} from "@opentui/solid";
|
|
1869
|
+
import {useTerminalDimensions} from "@opentui/solid";
|
|
1870
|
+
import {createEffect, createMemo as createMemo2, createSignal as createSignal3, Match, Switch} from "solid-js";
|
|
1871
|
+
|
|
1872
|
+
// src/format.ts
|
|
1873
|
+
function formatRate(median2) {
|
|
1874
|
+
return `${median2.toFixed(1)} tok/s`;
|
|
249
1875
|
}
|
|
250
|
-
function
|
|
251
|
-
|
|
1876
|
+
function formatDayLabel(date) {
|
|
1877
|
+
const [year, month, day] = date.split("-").map(Number);
|
|
1878
|
+
return new Intl.DateTimeFormat("en-US", { month: "short", day: "numeric" }).format(new Date(year, month - 1, day));
|
|
252
1879
|
}
|
|
253
|
-
function
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
});
|
|
1880
|
+
function formatCoverage(measured, unavailable) {
|
|
1881
|
+
return unavailable > 0 ? `${measured} measured calls; ${unavailable} unavailable` : `${measured} measured calls`;
|
|
1882
|
+
}
|
|
1883
|
+
function modelDisplayName(models, providerID, modelID) {
|
|
1884
|
+
const match = models?.find((model) => model.id === modelID && model.providerID === providerID);
|
|
1885
|
+
return match?.name ?? `${providerID}/${modelID}`;
|
|
260
1886
|
}
|
|
261
1887
|
|
|
262
|
-
// src/
|
|
1888
|
+
// src/spinner.tsx
|
|
1889
|
+
import {setProp as _$setProp} from "@opentui/solid";
|
|
263
1890
|
import {effect as _$effect} from "@opentui/solid";
|
|
264
1891
|
import {insert as _$insert} from "@opentui/solid";
|
|
1892
|
+
import {createElement as _$createElement} from "@opentui/solid";
|
|
1893
|
+
|
|
1894
|
+
// src/spinner-ticker.ts
|
|
1895
|
+
import {createSignal as createSignal2, onCleanup} from "solid-js";
|
|
1896
|
+
function createSpinnerTicker(clock = systemClock) {
|
|
1897
|
+
const [frame, setFrame] = createSignal2(SPINNER_FRAMES[0]);
|
|
1898
|
+
let index = 0;
|
|
1899
|
+
const timer = clock.setInterval(() => {
|
|
1900
|
+
index = (index + 1) % SPINNER_FRAMES.length;
|
|
1901
|
+
setFrame(SPINNER_FRAMES[index]);
|
|
1902
|
+
}, SPINNER_INTERVAL_MS);
|
|
1903
|
+
onCleanup(() => clock.clearInterval(timer));
|
|
1904
|
+
return frame;
|
|
1905
|
+
}
|
|
1906
|
+
var SPINNER_FRAMES = ["\u280B", "\u2819", "\u2839", "\u2838", "\u283C", "\u2834", "\u2826", "\u2827", "\u2807", "\u280F"];
|
|
1907
|
+
var SPINNER_INTERVAL_MS = 80;
|
|
1908
|
+
var systemClock = { setInterval, clearInterval };
|
|
1909
|
+
|
|
1910
|
+
// src/spinner.tsx
|
|
1911
|
+
function Spinner(props) {
|
|
1912
|
+
const frame = createSpinnerTicker();
|
|
1913
|
+
return (() => {
|
|
1914
|
+
var _el$ = _$createElement("text");
|
|
1915
|
+
_$insert(_el$, frame);
|
|
1916
|
+
_$effect((_$p) => _$setProp(_el$, "fg", props.context.theme.text.muted, _$p));
|
|
1917
|
+
return _el$;
|
|
1918
|
+
})();
|
|
1919
|
+
}
|
|
1920
|
+
|
|
1921
|
+
// src/page/header.tsx
|
|
1922
|
+
import {effect as _$effect2} from "@opentui/solid";
|
|
1923
|
+
import {insert as _$insert2} from "@opentui/solid";
|
|
265
1924
|
import {createTextNode as _$createTextNode} from "@opentui/solid";
|
|
266
1925
|
import {insertNode as _$insertNode} from "@opentui/solid";
|
|
267
|
-
import {setProp as _$
|
|
268
|
-
import {createElement as _$
|
|
1926
|
+
import {setProp as _$setProp2} from "@opentui/solid";
|
|
1927
|
+
import {createElement as _$createElement2} from "@opentui/solid";
|
|
269
1928
|
import {TextAttributes} from "@opentui/core";
|
|
270
1929
|
function scopeLabel(scope) {
|
|
271
1930
|
return scope === "project" ? "current project" : "all projects";
|
|
@@ -292,23 +1951,23 @@ function reasoningLabel(reasoning) {
|
|
|
292
1951
|
function Header(props) {
|
|
293
1952
|
const theme = props.context.theme;
|
|
294
1953
|
return (() => {
|
|
295
|
-
var _el$ = _$
|
|
1954
|
+
var _el$ = _$createElement2("box"), _el$2 = _$createElement2("text"), _el$4 = _$createElement2("text"), _el$5 = _$createTextNode(`\xB7 `), _el$6 = _$createTextNode(` \xB7 `), _el$7 = _$createTextNode(` \xB7 reasoning: `);
|
|
296
1955
|
_$insertNode(_el$, _el$2);
|
|
297
1956
|
_$insertNode(_el$, _el$4);
|
|
298
|
-
_$
|
|
299
|
-
_$
|
|
1957
|
+
_$setProp2(_el$, "flexDirection", "row");
|
|
1958
|
+
_$setProp2(_el$, "gap", 1);
|
|
300
1959
|
_$insertNode(_el$2, _$createTextNode(`Tokscope`));
|
|
301
1960
|
_$insertNode(_el$4, _el$5);
|
|
302
1961
|
_$insertNode(_el$4, _el$6);
|
|
303
1962
|
_$insertNode(_el$4, _el$7);
|
|
304
|
-
_$
|
|
305
|
-
_$
|
|
306
|
-
_$
|
|
307
|
-
_$
|
|
1963
|
+
_$insert2(_el$4, () => scopeLabel(props.filters.scope), _el$6);
|
|
1964
|
+
_$insert2(_el$4, () => rangeLabel(props.filters.range), _el$7);
|
|
1965
|
+
_$insert2(_el$4, () => reasoningLabel(props.filters.reasoning), null);
|
|
1966
|
+
_$effect2((_p$) => {
|
|
308
1967
|
var _v$ = theme.text.base, _v$2 = TextAttributes.BOLD, _v$3 = theme.text.muted;
|
|
309
|
-
_v$ !== _p$.e && (_p$.e = _$
|
|
310
|
-
_v$2 !== _p$.t && (_p$.t = _$
|
|
311
|
-
_v$3 !== _p$.a && (_p$.a = _$
|
|
1968
|
+
_v$ !== _p$.e && (_p$.e = _$setProp2(_el$2, "fg", _v$, _p$.e));
|
|
1969
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp2(_el$2, "attributes", _v$2, _p$.t));
|
|
1970
|
+
_v$3 !== _p$.a && (_p$.a = _$setProp2(_el$4, "fg", _v$3, _p$.a));
|
|
312
1971
|
return _p$;
|
|
313
1972
|
}, {
|
|
314
1973
|
e: undefined,
|
|
@@ -319,6 +1978,9 @@ function Header(props) {
|
|
|
319
1978
|
})();
|
|
320
1979
|
}
|
|
321
1980
|
|
|
1981
|
+
// src/page/help.ts
|
|
1982
|
+
var HELP_LINE = "\u2191\u2193 select \xB7 \u2190\u2192 day \xB7 s scope \xB7 t range \xB7 r reasoning \xB7 esc back \xB7 reset history in palette";
|
|
1983
|
+
|
|
322
1984
|
// src/page/selection.ts
|
|
323
1985
|
function nextSelection(keys, current, delta) {
|
|
324
1986
|
if (keys.length === 0)
|
|
@@ -333,109 +1995,79 @@ function nextSelection(keys, current, delta) {
|
|
|
333
1995
|
}
|
|
334
1996
|
|
|
335
1997
|
// src/page/state.ts
|
|
336
|
-
|
|
337
|
-
if (
|
|
338
|
-
return "loading";
|
|
339
|
-
if (result.error !== undefined)
|
|
340
|
-
return "error";
|
|
341
|
-
if (samples.length > 0)
|
|
1998
|
+
var contentState = function(view) {
|
|
1999
|
+
if (view.samples.length > 0)
|
|
342
2000
|
return "ready";
|
|
343
|
-
if (unavailable.length > 0)
|
|
2001
|
+
if (view.unavailable.length > 0)
|
|
344
2002
|
return "unmeasurable";
|
|
345
2003
|
return "empty";
|
|
2004
|
+
};
|
|
2005
|
+
function pickViewPageState(view) {
|
|
2006
|
+
const content = contentState(view);
|
|
2007
|
+
if (content !== "empty")
|
|
2008
|
+
return content;
|
|
2009
|
+
return view.kind === "loading" || view.kind === "partial" || view.kind === "cached" ? "loading" : "empty";
|
|
346
2010
|
}
|
|
347
2011
|
|
|
348
2012
|
// src/page/states.tsx
|
|
2013
|
+
import {effect as _$effect3} from "@opentui/solid";
|
|
349
2014
|
import {createTextNode as _$createTextNode2} from "@opentui/solid";
|
|
350
2015
|
import {insertNode as _$insertNode2} from "@opentui/solid";
|
|
351
|
-
import {
|
|
352
|
-
import {
|
|
353
|
-
import {
|
|
354
|
-
import {createElement as _$createElement2} from "@opentui/solid";
|
|
355
|
-
function LoadingState(props) {
|
|
356
|
-
const theme = props.context.theme;
|
|
357
|
-
const detail = () => props.sessions > 0 ? ` ${props.sessions} sessions` : "";
|
|
358
|
-
return (() => {
|
|
359
|
-
var _el$ = _$createElement2("text");
|
|
360
|
-
_$insert2(_el$, () => `Scanning history\u2026${detail()}`);
|
|
361
|
-
_$effect2((_$p) => _$setProp2(_el$, "fg", theme.text.muted, _$p));
|
|
362
|
-
return _el$;
|
|
363
|
-
})();
|
|
364
|
-
}
|
|
365
|
-
function ErrorState(props) {
|
|
366
|
-
const theme = props.context.theme;
|
|
367
|
-
return (() => {
|
|
368
|
-
var _el$2 = _$createElement2("box"), _el$3 = _$createElement2("text"), _el$5 = _$createElement2("text");
|
|
369
|
-
_$insertNode2(_el$2, _el$3);
|
|
370
|
-
_$insertNode2(_el$2, _el$5);
|
|
371
|
-
_$setProp2(_el$2, "flexDirection", "column");
|
|
372
|
-
_$setProp2(_el$2, "gap", 1);
|
|
373
|
-
_$insertNode2(_el$3, _$createTextNode2(`Could not read history`));
|
|
374
|
-
_$insertNode2(_el$5, _$createTextNode2(`Press enter to retry.`));
|
|
375
|
-
_$effect2((_p$) => {
|
|
376
|
-
var _v$ = theme.text.feedback.error.base, _v$2 = theme.text.muted;
|
|
377
|
-
_v$ !== _p$.e && (_p$.e = _$setProp2(_el$3, "fg", _v$, _p$.e));
|
|
378
|
-
_v$2 !== _p$.t && (_p$.t = _$setProp2(_el$5, "fg", _v$2, _p$.t));
|
|
379
|
-
return _p$;
|
|
380
|
-
}, {
|
|
381
|
-
e: undefined,
|
|
382
|
-
t: undefined
|
|
383
|
-
});
|
|
384
|
-
return _el$2;
|
|
385
|
-
})();
|
|
386
|
-
}
|
|
2016
|
+
import {insert as _$insert3} from "@opentui/solid";
|
|
2017
|
+
import {setProp as _$setProp3} from "@opentui/solid";
|
|
2018
|
+
import {createElement as _$createElement3} from "@opentui/solid";
|
|
387
2019
|
function EmptyState(props) {
|
|
388
2020
|
const theme = props.context.theme;
|
|
389
2021
|
return (() => {
|
|
390
|
-
var _el$
|
|
391
|
-
_$insertNode2(_el
|
|
392
|
-
_$insertNode2(_el
|
|
393
|
-
_$
|
|
394
|
-
_$
|
|
395
|
-
_$
|
|
396
|
-
_$insertNode2(_el$
|
|
397
|
-
_$
|
|
398
|
-
var _v$
|
|
399
|
-
_v$
|
|
400
|
-
_v$
|
|
2022
|
+
var _el$ = _$createElement3("box"), _el$2 = _$createElement3("text"), _el$3 = _$createElement3("text");
|
|
2023
|
+
_$insertNode2(_el$, _el$2);
|
|
2024
|
+
_$insertNode2(_el$, _el$3);
|
|
2025
|
+
_$setProp3(_el$, "flexDirection", "column");
|
|
2026
|
+
_$setProp3(_el$, "gap", 1);
|
|
2027
|
+
_$insert3(_el$2, () => `No history in ${scopeLabel(props.filters.scope)} \xB7 ${rangeLabel(props.filters.range)} \xB7 reasoning: ${reasoningLabel(props.filters.reasoning)}.`);
|
|
2028
|
+
_$insertNode2(_el$3, _$createTextNode2(`Try a wider time range or all projects.`));
|
|
2029
|
+
_$effect3((_p$) => {
|
|
2030
|
+
var _v$ = theme.text.muted, _v$2 = theme.text.muted;
|
|
2031
|
+
_v$ !== _p$.e && (_p$.e = _$setProp3(_el$2, "fg", _v$, _p$.e));
|
|
2032
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp3(_el$3, "fg", _v$2, _p$.t));
|
|
401
2033
|
return _p$;
|
|
402
2034
|
}, {
|
|
403
2035
|
e: undefined,
|
|
404
2036
|
t: undefined
|
|
405
2037
|
});
|
|
406
|
-
return _el
|
|
2038
|
+
return _el$;
|
|
407
2039
|
})();
|
|
408
2040
|
}
|
|
409
2041
|
function UnmeasurableState(props) {
|
|
410
2042
|
const theme = props.context.theme;
|
|
411
2043
|
return (() => {
|
|
412
|
-
var _el$
|
|
413
|
-
_$insertNode2(_el$
|
|
414
|
-
_$insertNode2(_el$
|
|
415
|
-
_$
|
|
416
|
-
_$
|
|
417
|
-
_$insertNode2(_el$
|
|
418
|
-
_$insertNode2(_el$
|
|
419
|
-
_$
|
|
420
|
-
var _v$
|
|
421
|
-
_v$
|
|
422
|
-
_v$
|
|
2044
|
+
var _el$5 = _$createElement3("box"), _el$6 = _$createElement3("text"), _el$8 = _$createElement3("text");
|
|
2045
|
+
_$insertNode2(_el$5, _el$6);
|
|
2046
|
+
_$insertNode2(_el$5, _el$8);
|
|
2047
|
+
_$setProp3(_el$5, "flexDirection", "column");
|
|
2048
|
+
_$setProp3(_el$5, "gap", 1);
|
|
2049
|
+
_$insertNode2(_el$6, _$createTextNode2(`History exists, but none of it is measurable.`));
|
|
2050
|
+
_$insertNode2(_el$8, _$createTextNode2(`Completed requests are missing the historical timing or token data needed for speed.`));
|
|
2051
|
+
_$effect3((_p$) => {
|
|
2052
|
+
var _v$3 = theme.text.muted, _v$4 = theme.text.muted;
|
|
2053
|
+
_v$3 !== _p$.e && (_p$.e = _$setProp3(_el$6, "fg", _v$3, _p$.e));
|
|
2054
|
+
_v$4 !== _p$.t && (_p$.t = _$setProp3(_el$8, "fg", _v$4, _p$.t));
|
|
423
2055
|
return _p$;
|
|
424
2056
|
}, {
|
|
425
2057
|
e: undefined,
|
|
426
2058
|
t: undefined
|
|
427
2059
|
});
|
|
428
|
-
return _el$
|
|
2060
|
+
return _el$5;
|
|
429
2061
|
})();
|
|
430
2062
|
}
|
|
431
2063
|
|
|
432
2064
|
// src/page/table.tsx
|
|
433
2065
|
import {insertNode as _$insertNode3} from "@opentui/solid";
|
|
434
|
-
import {effect as _$
|
|
2066
|
+
import {effect as _$effect4} from "@opentui/solid";
|
|
435
2067
|
import {createComponent as _$createComponent} from "@opentui/solid";
|
|
436
|
-
import {insert as _$
|
|
437
|
-
import {setProp as _$
|
|
438
|
-
import {createElement as _$
|
|
2068
|
+
import {insert as _$insert4} from "@opentui/solid";
|
|
2069
|
+
import {setProp as _$setProp4} from "@opentui/solid";
|
|
2070
|
+
import {createElement as _$createElement4} from "@opentui/solid";
|
|
439
2071
|
import {TextAttributes as TextAttributes2} from "@opentui/core";
|
|
440
2072
|
import {For, Show} from "solid-js";
|
|
441
2073
|
var pad = function(value, width) {
|
|
@@ -466,22 +2098,22 @@ function ComparisonTable(props) {
|
|
|
466
2098
|
return ` ${cells.join(" ".repeat(GAP))}`;
|
|
467
2099
|
};
|
|
468
2100
|
return (() => {
|
|
469
|
-
var _el$ = _$
|
|
2101
|
+
var _el$ = _$createElement4("box"), _el$2 = _$createElement4("text");
|
|
470
2102
|
_$insertNode3(_el$, _el$2);
|
|
471
|
-
_$
|
|
472
|
-
_$
|
|
473
|
-
_$
|
|
2103
|
+
_$setProp4(_el$, "flexDirection", "column");
|
|
2104
|
+
_$insert4(_el$2, header2);
|
|
2105
|
+
_$insert4(_el$, _$createComponent(For, {
|
|
474
2106
|
get each() {
|
|
475
2107
|
return visible();
|
|
476
2108
|
},
|
|
477
2109
|
children: (row) => (() => {
|
|
478
|
-
var _el$4 = _$
|
|
479
|
-
_$
|
|
480
|
-
_$
|
|
481
|
-
_$
|
|
2110
|
+
var _el$4 = _$createElement4("text");
|
|
2111
|
+
_$setProp4(_el$4, "selectable", false);
|
|
2112
|
+
_$insert4(_el$4, () => line(row, row.key === props.selectedKey));
|
|
2113
|
+
_$effect4((_p$) => {
|
|
482
2114
|
var _v$3 = row.key === props.selectedKey ? theme.text.base : theme.text.muted, _v$4 = row.key === props.selectedKey ? TextAttributes2.BOLD : undefined;
|
|
483
|
-
_v$3 !== _p$.e && (_p$.e = _$
|
|
484
|
-
_v$4 !== _p$.t && (_p$.t = _$
|
|
2115
|
+
_v$3 !== _p$.e && (_p$.e = _$setProp4(_el$4, "fg", _v$3, _p$.e));
|
|
2116
|
+
_v$4 !== _p$.t && (_p$.t = _$setProp4(_el$4, "attributes", _v$4, _p$.t));
|
|
485
2117
|
return _p$;
|
|
486
2118
|
}, {
|
|
487
2119
|
e: undefined,
|
|
@@ -490,21 +2122,21 @@ function ComparisonTable(props) {
|
|
|
490
2122
|
return _el$4;
|
|
491
2123
|
})()
|
|
492
2124
|
}), null);
|
|
493
|
-
_$
|
|
2125
|
+
_$insert4(_el$, _$createComponent(Show, {
|
|
494
2126
|
get when() {
|
|
495
2127
|
return props.rows.length > visible().length;
|
|
496
2128
|
},
|
|
497
2129
|
get children() {
|
|
498
|
-
var _el$3 = _$
|
|
499
|
-
_$
|
|
500
|
-
_$
|
|
2130
|
+
var _el$3 = _$createElement4("text");
|
|
2131
|
+
_$insert4(_el$3, () => ` \u2026 ${props.rows.length - visible().length} more rows`);
|
|
2132
|
+
_$effect4((_$p) => _$setProp4(_el$3, "fg", theme.text.muted, _$p));
|
|
501
2133
|
return _el$3;
|
|
502
2134
|
}
|
|
503
2135
|
}), null);
|
|
504
|
-
_$
|
|
2136
|
+
_$effect4((_p$) => {
|
|
505
2137
|
var _v$ = theme.text.muted, _v$2 = TextAttributes2.BOLD;
|
|
506
|
-
_v$ !== _p$.e && (_p$.e = _$
|
|
507
|
-
_v$2 !== _p$.t && (_p$.t = _$
|
|
2138
|
+
_v$ !== _p$.e && (_p$.e = _$setProp4(_el$2, "fg", _v$, _p$.e));
|
|
2139
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp4(_el$2, "attributes", _v$2, _p$.t));
|
|
508
2140
|
return _p$;
|
|
509
2141
|
}, {
|
|
510
2142
|
e: undefined,
|
|
@@ -521,12 +2153,12 @@ var GAP = 2;
|
|
|
521
2153
|
|
|
522
2154
|
// src/page/trend.tsx
|
|
523
2155
|
import {createTextNode as _$createTextNode3} from "@opentui/solid";
|
|
524
|
-
import {effect as _$
|
|
2156
|
+
import {effect as _$effect5} from "@opentui/solid";
|
|
525
2157
|
import {insertNode as _$insertNode4} from "@opentui/solid";
|
|
526
2158
|
import {createComponent as _$createComponent2} from "@opentui/solid";
|
|
527
|
-
import {insert as _$
|
|
528
|
-
import {setProp as _$
|
|
529
|
-
import {createElement as _$
|
|
2159
|
+
import {insert as _$insert5} from "@opentui/solid";
|
|
2160
|
+
import {setProp as _$setProp5} from "@opentui/solid";
|
|
2161
|
+
import {createElement as _$createElement5} from "@opentui/solid";
|
|
530
2162
|
import {TextAttributes as TextAttributes3} from "@opentui/core";
|
|
531
2163
|
import {For as For2, Show as Show2, createMemo} from "solid-js";
|
|
532
2164
|
|
|
@@ -589,65 +2221,65 @@ function TrendPanel(props) {
|
|
|
589
2221
|
return `${formatDayLabel(point.date)} \xB7 ${point.median.toFixed(1)} tok/s \xB7 ${point.calls} calls`;
|
|
590
2222
|
};
|
|
591
2223
|
return (() => {
|
|
592
|
-
var _el$ = _$
|
|
2224
|
+
var _el$ = _$createElement5("box"), _el$2 = _$createElement5("text");
|
|
593
2225
|
_$insertNode4(_el$, _el$2);
|
|
594
|
-
_$
|
|
595
|
-
_$
|
|
596
|
-
_$
|
|
597
|
-
_$
|
|
2226
|
+
_$setProp5(_el$, "flexDirection", "column");
|
|
2227
|
+
_$setProp5(_el$, "gap", 1);
|
|
2228
|
+
_$insert5(_el$2, () => `Trend: ${props.rowLabel} \xB7 ${props.rangeLabel} \xB7 ${props.reasoningLabel}`);
|
|
2229
|
+
_$insert5(_el$, _$createComponent2(Show2, {
|
|
598
2230
|
get when() {
|
|
599
2231
|
return measurable();
|
|
600
2232
|
},
|
|
601
2233
|
get fallback() {
|
|
602
2234
|
return (() => {
|
|
603
|
-
var _el$6 = _$
|
|
2235
|
+
var _el$6 = _$createElement5("text");
|
|
604
2236
|
_$insertNode4(_el$6, _$createTextNode3(`No measurable trend for this model \u2014 historical timing or token data is unavailable.`));
|
|
605
|
-
_$
|
|
2237
|
+
_$effect5((_$p) => _$setProp5(_el$6, "fg", theme.text.muted, _$p));
|
|
606
2238
|
return _el$6;
|
|
607
2239
|
})();
|
|
608
2240
|
},
|
|
609
2241
|
get children() {
|
|
610
2242
|
return [(() => {
|
|
611
|
-
var _el$3 = _$
|
|
2243
|
+
var _el$3 = _$createElement5("box"), _el$4 = _$createElement5("text");
|
|
612
2244
|
_$insertNode4(_el$3, _el$4);
|
|
613
|
-
_$
|
|
614
|
-
_$
|
|
2245
|
+
_$setProp5(_el$3, "flexDirection", "column");
|
|
2246
|
+
_$insert5(_el$3, _$createComponent2(For2, {
|
|
615
2247
|
get each() {
|
|
616
2248
|
return plotRows();
|
|
617
2249
|
},
|
|
618
2250
|
children: (row) => (() => {
|
|
619
|
-
var _el$8 = _$
|
|
620
|
-
_$
|
|
621
|
-
_$
|
|
2251
|
+
var _el$8 = _$createElement5("box");
|
|
2252
|
+
_$setProp5(_el$8, "flexDirection", "row");
|
|
2253
|
+
_$insert5(_el$8, _$createComponent2(For2, {
|
|
622
2254
|
get each() {
|
|
623
2255
|
return visible();
|
|
624
2256
|
},
|
|
625
2257
|
children: (_, index) => (() => {
|
|
626
|
-
var _el$9 = _$
|
|
627
|
-
_$
|
|
628
|
-
_$
|
|
629
|
-
_$
|
|
2258
|
+
var _el$9 = _$createElement5("text");
|
|
2259
|
+
_$setProp5(_el$9, "selectable", false);
|
|
2260
|
+
_$insert5(_el$9, () => barChar(levels()[index()], row, PLOT_ROWS) + " ");
|
|
2261
|
+
_$effect5((_$p) => _$setProp5(_el$9, "fg", index() === selectedIndex() ? theme.text.base : theme.text.muted, _$p));
|
|
630
2262
|
return _el$9;
|
|
631
2263
|
})()
|
|
632
2264
|
}));
|
|
633
2265
|
return _el$8;
|
|
634
2266
|
})()
|
|
635
2267
|
}), _el$4);
|
|
636
|
-
_$
|
|
637
|
-
_$
|
|
2268
|
+
_$insert5(_el$4, () => axisLine(visible(), selectedIndex()));
|
|
2269
|
+
_$effect5((_$p) => _$setProp5(_el$4, "fg", theme.text.muted, _$p));
|
|
638
2270
|
return _el$3;
|
|
639
2271
|
})(), (() => {
|
|
640
|
-
var _el$5 = _$
|
|
641
|
-
_$
|
|
642
|
-
_$
|
|
2272
|
+
var _el$5 = _$createElement5("text");
|
|
2273
|
+
_$insert5(_el$5, detail);
|
|
2274
|
+
_$effect5((_$p) => _$setProp5(_el$5, "fg", theme.text.base, _$p));
|
|
643
2275
|
return _el$5;
|
|
644
2276
|
})()];
|
|
645
2277
|
}
|
|
646
2278
|
}), null);
|
|
647
|
-
_$
|
|
2279
|
+
_$effect5((_p$) => {
|
|
648
2280
|
var _v$ = theme.text.muted, _v$2 = TextAttributes3.BOLD;
|
|
649
|
-
_v$ !== _p$.e && (_p$.e = _$
|
|
650
|
-
_v$2 !== _p$.t && (_p$.t = _$
|
|
2281
|
+
_v$ !== _p$.e && (_p$.e = _$setProp5(_el$2, "fg", _v$, _p$.e));
|
|
2282
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp5(_el$2, "attributes", _v$2, _p$.t));
|
|
651
2283
|
return _p$;
|
|
652
2284
|
}, {
|
|
653
2285
|
e: undefined,
|
|
@@ -661,46 +2293,26 @@ function TrendPanel(props) {
|
|
|
661
2293
|
function TokscopePage(props) {
|
|
662
2294
|
const theme = props.context.theme;
|
|
663
2295
|
const dimensions = useTerminalDimensions();
|
|
664
|
-
const openedAt = Date.now();
|
|
665
2296
|
props.context.data.location.model.sync();
|
|
666
2297
|
props.context.data.location.provider.sync();
|
|
667
|
-
const [
|
|
2298
|
+
const [filters3, setFilters] = createSignal3({
|
|
668
2299
|
...DEFAULT_FILTERS
|
|
669
2300
|
});
|
|
670
|
-
const [
|
|
671
|
-
const [
|
|
672
|
-
const [selectedDay, setSelectedDay] = createSignal(0);
|
|
2301
|
+
const [selectedKey, setSelectedKey] = createSignal3(undefined);
|
|
2302
|
+
const [selectedDay, setSelectedDay] = createSignal3(0);
|
|
673
2303
|
const directory = () => props.context.location?.directory ?? props.context.data.location.default().directory;
|
|
674
|
-
const
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
fromMs: rangeStartMs(range, openedAt),
|
|
682
|
-
onProgress: (info) => setScanned(info.sessionsScanned)
|
|
683
|
-
});
|
|
684
|
-
});
|
|
685
|
-
const filtered = createMemo2(() => {
|
|
686
|
-
const value = result();
|
|
687
|
-
if (value === undefined)
|
|
688
|
-
return {
|
|
689
|
-
samples: [],
|
|
690
|
-
unavailable: []
|
|
691
|
-
};
|
|
692
|
-
return applyReasoningFilter(value.samples, value.unavailable, filters2().reasoning);
|
|
693
|
-
});
|
|
694
|
-
const pageState = createMemo2(() => {
|
|
695
|
-
if (result.loading)
|
|
696
|
-
return "loading";
|
|
697
|
-
return pickPageState({
|
|
698
|
-
error: result.error
|
|
699
|
-
}, filtered().samples, filtered().unavailable);
|
|
700
|
-
});
|
|
2304
|
+
const state2 = () => props.service.view({
|
|
2305
|
+
directory: filters3().scope === "project" ? directory() : undefined,
|
|
2306
|
+
range: filters3().range
|
|
2307
|
+
})();
|
|
2308
|
+
const referenceNow = () => state2().lastCheckedAt ?? Date.now();
|
|
2309
|
+
const filtered = createMemo2(() => applyReasoningFilter(state2().samples, state2().unavailable, filters3().reasoning));
|
|
2310
|
+
const pageState = createMemo2(() => pickViewPageState(state2()));
|
|
701
2311
|
const rows = createMemo2(() => comparisonRows(filtered().samples, filtered().unavailable));
|
|
702
2312
|
createEffect(() => {
|
|
703
2313
|
const list = rows();
|
|
2314
|
+
if (list.some((row) => row.key === selectedKey()))
|
|
2315
|
+
return;
|
|
704
2316
|
setSelectedKey(list[0]?.key);
|
|
705
2317
|
});
|
|
706
2318
|
const moveSelection = (delta) => setSelectedKey(nextSelection(rows().map((row) => row.key), selectedKey(), delta));
|
|
@@ -709,13 +2321,19 @@ function TokscopePage(props) {
|
|
|
709
2321
|
const key = selectedKey();
|
|
710
2322
|
if (key === undefined)
|
|
711
2323
|
return [];
|
|
712
|
-
|
|
2324
|
+
const nowMs = referenceNow();
|
|
2325
|
+
return dailySeries(filtered().samples, key, rangeStartMs(filters3().range, nowMs), nowMs);
|
|
713
2326
|
});
|
|
2327
|
+
let anchoredKey;
|
|
714
2328
|
createEffect(() => {
|
|
715
|
-
selectedKey();
|
|
2329
|
+
const key = selectedKey();
|
|
716
2330
|
const points = series();
|
|
2331
|
+
const current = selectedDay();
|
|
2332
|
+
if (key === anchoredKey && current < points.length)
|
|
2333
|
+
return;
|
|
717
2334
|
const last = points.findLastIndex((point) => point.calls > 0);
|
|
718
2335
|
setSelectedDay(last === -1 ? Math.max(0, points.length - 1) : last);
|
|
2336
|
+
anchoredKey = key;
|
|
719
2337
|
});
|
|
720
2338
|
const moveDay = (delta) => setSelectedDay((current) => Math.max(0, Math.min(current + delta, Math.max(0, series().length - 1))));
|
|
721
2339
|
const rowLabel = () => {
|
|
@@ -731,13 +2349,13 @@ function TokscopePage(props) {
|
|
|
731
2349
|
title: "All settings",
|
|
732
2350
|
value: "all"
|
|
733
2351
|
}];
|
|
734
|
-
const value =
|
|
735
|
-
for (const variant of value
|
|
2352
|
+
const value = state2();
|
|
2353
|
+
for (const variant of value.variants)
|
|
736
2354
|
options.push({
|
|
737
2355
|
title: variant,
|
|
738
2356
|
value: variant
|
|
739
2357
|
});
|
|
740
|
-
const hasUnknown =
|
|
2358
|
+
const hasUnknown = [...value.samples, ...value.unavailable].some((item) => item.variant === undefined);
|
|
741
2359
|
if (hasUnknown)
|
|
742
2360
|
options.push({
|
|
743
2361
|
title: "unknown",
|
|
@@ -748,7 +2366,7 @@ function TokscopePage(props) {
|
|
|
748
2366
|
const pickScope = async () => {
|
|
749
2367
|
const chosen = await props.context.ui.dialog.select({
|
|
750
2368
|
title: "Scope",
|
|
751
|
-
current:
|
|
2369
|
+
current: filters3().scope,
|
|
752
2370
|
options: SCOPE_OPTIONS
|
|
753
2371
|
});
|
|
754
2372
|
if (chosen !== undefined)
|
|
@@ -760,7 +2378,7 @@ function TokscopePage(props) {
|
|
|
760
2378
|
const pickRange = async () => {
|
|
761
2379
|
const chosen = await props.context.ui.dialog.select({
|
|
762
2380
|
title: "Time range",
|
|
763
|
-
current:
|
|
2381
|
+
current: filters3().range,
|
|
764
2382
|
options: RANGE_OPTIONS
|
|
765
2383
|
});
|
|
766
2384
|
if (chosen !== undefined)
|
|
@@ -772,7 +2390,7 @@ function TokscopePage(props) {
|
|
|
772
2390
|
const pickReasoning = async () => {
|
|
773
2391
|
const chosen = await props.context.ui.dialog.select({
|
|
774
2392
|
title: "Reasoning setting",
|
|
775
|
-
current:
|
|
2393
|
+
current: filters3().reasoning,
|
|
776
2394
|
options: reasoningOptions()
|
|
777
2395
|
});
|
|
778
2396
|
if (chosen !== undefined)
|
|
@@ -822,58 +2440,37 @@ function TokscopePage(props) {
|
|
|
822
2440
|
bind: "r",
|
|
823
2441
|
title: "reasoning",
|
|
824
2442
|
run: pickReasoning
|
|
825
|
-
}, {
|
|
826
|
-
bind: "enter",
|
|
827
|
-
title: "retry",
|
|
828
|
-
run: () => {
|
|
829
|
-
if (pageState() === "error")
|
|
830
|
-
refetch();
|
|
831
|
-
}
|
|
832
2443
|
}]
|
|
833
2444
|
}));
|
|
834
2445
|
const coverage = () => pageState() === "loading" ? "" : ` \xB7 ${formatCoverage(filtered().samples.length, filtered().unavailable.length)}`;
|
|
835
2446
|
return (() => {
|
|
836
|
-
var _el$ = _$
|
|
2447
|
+
var _el$ = _$createElement6("box"), _el$2 = _$createElement6("box"), _el$4 = _$createElement6("text");
|
|
837
2448
|
_$insertNode5(_el$, _el$2);
|
|
838
2449
|
_$insertNode5(_el$, _el$4);
|
|
839
|
-
_$
|
|
840
|
-
_$
|
|
841
|
-
_$
|
|
842
|
-
_$
|
|
843
|
-
_$
|
|
844
|
-
_$
|
|
2450
|
+
_$setProp6(_el$, "width", "100%");
|
|
2451
|
+
_$setProp6(_el$, "height", "100%");
|
|
2452
|
+
_$setProp6(_el$, "flexDirection", "column");
|
|
2453
|
+
_$setProp6(_el$, "padding", 1);
|
|
2454
|
+
_$setProp6(_el$, "gap", 1);
|
|
2455
|
+
_$insert6(_el$, _$createComponent3(Header, {
|
|
845
2456
|
get context() {
|
|
846
2457
|
return props.context;
|
|
847
2458
|
},
|
|
848
2459
|
get filters() {
|
|
849
|
-
return
|
|
2460
|
+
return filters3();
|
|
850
2461
|
}
|
|
851
2462
|
}), _el$2);
|
|
852
|
-
_$
|
|
853
|
-
_$
|
|
854
|
-
_$
|
|
855
|
-
_$
|
|
2463
|
+
_$setProp6(_el$2, "flexGrow", 1);
|
|
2464
|
+
_$setProp6(_el$2, "flexDirection", "column");
|
|
2465
|
+
_$setProp6(_el$2, "gap", 1);
|
|
2466
|
+
_$insert6(_el$2, _$createComponent3(Switch, {
|
|
856
2467
|
get children() {
|
|
857
2468
|
return [_$createComponent3(Match, {
|
|
858
2469
|
get when() {
|
|
859
2470
|
return pageState() === "loading";
|
|
860
2471
|
},
|
|
861
2472
|
get children() {
|
|
862
|
-
return _$createComponent3(
|
|
863
|
-
get context() {
|
|
864
|
-
return props.context;
|
|
865
|
-
},
|
|
866
|
-
get sessions() {
|
|
867
|
-
return scanned();
|
|
868
|
-
}
|
|
869
|
-
});
|
|
870
|
-
}
|
|
871
|
-
}), _$createComponent3(Match, {
|
|
872
|
-
get when() {
|
|
873
|
-
return pageState() === "error";
|
|
874
|
-
},
|
|
875
|
-
get children() {
|
|
876
|
-
return _$createComponent3(ErrorState, {
|
|
2473
|
+
return _$createComponent3(Spinner, {
|
|
877
2474
|
get context() {
|
|
878
2475
|
return props.context;
|
|
879
2476
|
}
|
|
@@ -889,7 +2486,7 @@ function TokscopePage(props) {
|
|
|
889
2486
|
return props.context;
|
|
890
2487
|
},
|
|
891
2488
|
get filters() {
|
|
892
|
-
return
|
|
2489
|
+
return filters3();
|
|
893
2490
|
}
|
|
894
2491
|
});
|
|
895
2492
|
}
|
|
@@ -909,11 +2506,11 @@ function TokscopePage(props) {
|
|
|
909
2506
|
return pageState() === "ready";
|
|
910
2507
|
},
|
|
911
2508
|
get children() {
|
|
912
|
-
var _el$3 = _$
|
|
913
|
-
_$
|
|
914
|
-
_$
|
|
915
|
-
_$
|
|
916
|
-
_$
|
|
2509
|
+
var _el$3 = _$createElement6("box");
|
|
2510
|
+
_$setProp6(_el$3, "flexDirection", "column");
|
|
2511
|
+
_$setProp6(_el$3, "flexGrow", 1);
|
|
2512
|
+
_$setProp6(_el$3, "gap", 1);
|
|
2513
|
+
_$insert6(_el$3, _$createComponent3(ComparisonTable, {
|
|
917
2514
|
get context() {
|
|
918
2515
|
return props.context;
|
|
919
2516
|
},
|
|
@@ -930,7 +2527,7 @@ function TokscopePage(props) {
|
|
|
930
2527
|
return Math.max(3, dimensions().height - 27);
|
|
931
2528
|
}
|
|
932
2529
|
}), null);
|
|
933
|
-
_$
|
|
2530
|
+
_$insert6(_el$3, _$createComponent3(TrendPanel, {
|
|
934
2531
|
get context() {
|
|
935
2532
|
return props.context;
|
|
936
2533
|
},
|
|
@@ -944,10 +2541,10 @@ function TokscopePage(props) {
|
|
|
944
2541
|
return rowLabel();
|
|
945
2542
|
},
|
|
946
2543
|
get rangeLabel() {
|
|
947
|
-
return rangeLabel(
|
|
2544
|
+
return rangeLabel(filters3().range);
|
|
948
2545
|
},
|
|
949
2546
|
get reasoningLabel() {
|
|
950
|
-
return reasoningLabel(
|
|
2547
|
+
return reasoningLabel(filters3().reasoning);
|
|
951
2548
|
},
|
|
952
2549
|
get width() {
|
|
953
2550
|
return Math.max(20, dimensions().width - 2);
|
|
@@ -958,11 +2555,11 @@ function TokscopePage(props) {
|
|
|
958
2555
|
})];
|
|
959
2556
|
}
|
|
960
2557
|
}));
|
|
961
|
-
_$
|
|
962
|
-
_$
|
|
2558
|
+
_$insert6(_el$4, () => HELP_LINE + coverage());
|
|
2559
|
+
_$effect6((_p$) => {
|
|
963
2560
|
var _v$ = theme.background.base, _v$2 = theme.text.muted;
|
|
964
|
-
_v$ !== _p$.e && (_p$.e = _$
|
|
965
|
-
_v$2 !== _p$.t && (_p$.t = _$
|
|
2561
|
+
_v$ !== _p$.e && (_p$.e = _$setProp6(_el$, "backgroundColor", _v$, _p$.e));
|
|
2562
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp6(_el$4, "fg", _v$2, _p$.t));
|
|
966
2563
|
return _p$;
|
|
967
2564
|
}, {
|
|
968
2565
|
e: undefined,
|
|
@@ -993,128 +2590,35 @@ var RANGE_OPTIONS = [{
|
|
|
993
2590
|
}];
|
|
994
2591
|
|
|
995
2592
|
// src/sidebar/store.ts
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
for (const sample of samples) {
|
|
1003
|
-
if (sample.completedAt <= since || sample.completedAt > nowMs)
|
|
1004
|
-
continue;
|
|
1005
|
-
const key = rowKey(sample.providerID, sample.modelID);
|
|
1006
|
-
let group = groups.get(key);
|
|
1007
|
-
if (group === undefined) {
|
|
1008
|
-
group = { providerID: sample.providerID, modelID: sample.modelID, rates: [] };
|
|
1009
|
-
groups.set(key, group);
|
|
1010
|
-
}
|
|
1011
|
-
group.rates.push(sample.rate);
|
|
1012
|
-
}
|
|
1013
|
-
const rows = [];
|
|
1014
|
-
for (const [key, group] of groups) {
|
|
1015
|
-
if (group.rates.length < WIDGET_MIN_CALLS)
|
|
1016
|
-
continue;
|
|
1017
|
-
const value = median(group.rates);
|
|
1018
|
-
if (value === null)
|
|
1019
|
-
continue;
|
|
1020
|
-
rows.push({
|
|
1021
|
-
key,
|
|
1022
|
-
providerID: group.providerID,
|
|
1023
|
-
modelID: group.modelID,
|
|
1024
|
-
median: value,
|
|
1025
|
-
calls: group.rates.length
|
|
1026
|
-
});
|
|
1027
|
-
}
|
|
1028
|
-
rows.sort((a, b) => {
|
|
1029
|
-
const medianOrder = b.median - a.median;
|
|
1030
|
-
if (medianOrder !== 0)
|
|
1031
|
-
return medianOrder;
|
|
1032
|
-
if (b.calls !== a.calls)
|
|
1033
|
-
return b.calls - a.calls;
|
|
1034
|
-
return a.key.localeCompare(b.key);
|
|
1035
|
-
});
|
|
1036
|
-
return rows.slice(0, WIDGET_MAX_ENTRIES);
|
|
1037
|
-
}
|
|
1038
|
-
var WIDGET_WINDOW_MS = 86400000;
|
|
1039
|
-
var WIDGET_MIN_CALLS = 3;
|
|
1040
|
-
var WIDGET_MAX_ENTRIES = 3;
|
|
1041
|
-
|
|
1042
|
-
// src/sidebar/store.ts
|
|
1043
|
-
function createWidgetStore(deps) {
|
|
1044
|
-
const [state2, setState] = createSignal2({ status: "loading", rows: [], stale: false });
|
|
1045
|
-
let inFlight = false;
|
|
1046
|
-
let pending = false;
|
|
1047
|
-
let resolvers = [];
|
|
1048
|
-
async function runOnce() {
|
|
1049
|
-
const nowMs = deps.now();
|
|
1050
|
-
try {
|
|
1051
|
-
const result = await collectMeasurements(deps.client, { fromMs: nowMs - WIDGET_WINDOW_MS });
|
|
1052
|
-
setState({ status: "ready", rows: widgetRows(result.samples, nowMs), stale: false });
|
|
1053
|
-
} catch {
|
|
1054
|
-
const previous = state2();
|
|
1055
|
-
setState(previous.rows.length > 0 ? { status: "ready", rows: previous.rows, stale: true } : { status: "error", rows: [], stale: false });
|
|
1056
|
-
}
|
|
1057
|
-
}
|
|
1058
|
-
async function drain() {
|
|
1059
|
-
inFlight = true;
|
|
1060
|
-
do {
|
|
1061
|
-
pending = false;
|
|
1062
|
-
await runOnce();
|
|
1063
|
-
} while (pending);
|
|
1064
|
-
inFlight = false;
|
|
1065
|
-
const waiting = resolvers;
|
|
1066
|
-
resolvers = [];
|
|
1067
|
-
for (const resolve of waiting)
|
|
1068
|
-
resolve();
|
|
1069
|
-
}
|
|
1070
|
-
function refresh() {
|
|
1071
|
-
return new Promise((resolve) => {
|
|
1072
|
-
resolvers.push(resolve);
|
|
1073
|
-
if (inFlight) {
|
|
1074
|
-
pending = true;
|
|
1075
|
-
return;
|
|
1076
|
-
}
|
|
1077
|
-
drain();
|
|
1078
|
-
});
|
|
1079
|
-
}
|
|
1080
|
-
function attach() {
|
|
1081
|
-
const detachEvent = deps.onEvent(() => void refresh());
|
|
1082
|
-
const detachTimer = deps.setTimer(() => void refresh(), WIDGET_REFRESH_MS);
|
|
1083
|
-
refresh();
|
|
1084
|
-
return () => {
|
|
1085
|
-
detachEvent();
|
|
1086
|
-
detachTimer();
|
|
1087
|
-
};
|
|
1088
|
-
}
|
|
1089
|
-
return { state: state2, refresh, attach };
|
|
2593
|
+
function createWidgetStore(service) {
|
|
2594
|
+
return {
|
|
2595
|
+
state: () => service.widget()(),
|
|
2596
|
+
refresh: () => service.refreshNow(),
|
|
2597
|
+
attach: () => service.hold()
|
|
2598
|
+
};
|
|
1090
2599
|
}
|
|
1091
|
-
var WIDGET_REFRESH_MS = 60000;
|
|
1092
2600
|
|
|
1093
2601
|
// src/sidebar/widget.tsx
|
|
1094
2602
|
import {insertNode as _$insertNode6} from "@opentui/solid";
|
|
1095
|
-
import {effect as _$
|
|
1096
|
-
import {insert as _$
|
|
2603
|
+
import {effect as _$effect7} from "@opentui/solid";
|
|
2604
|
+
import {insert as _$insert7} from "@opentui/solid";
|
|
1097
2605
|
import {createComponent as _$createComponent4} from "@opentui/solid";
|
|
1098
|
-
import {setProp as _$
|
|
2606
|
+
import {setProp as _$setProp7} from "@opentui/solid";
|
|
1099
2607
|
import {use as _$use} from "@opentui/solid";
|
|
1100
|
-
import {createElement as _$
|
|
2608
|
+
import {createElement as _$createElement7} from "@opentui/solid";
|
|
1101
2609
|
import {TextAttributes as TextAttributes4} from "@opentui/core";
|
|
1102
2610
|
import {useTerminalDimensions as useTerminalDimensions2} from "@opentui/solid";
|
|
1103
|
-
import {createEffect as createEffect2, createMemo as createMemo3, createSignal as
|
|
2611
|
+
import {createEffect as createEffect2, createMemo as createMemo3, createSignal as createSignal4, For as For3, onCleanup as onCleanup2, onMount, Show as Show3} from "solid-js";
|
|
1104
2612
|
|
|
1105
2613
|
// src/sidebar/layout.ts
|
|
1106
2614
|
var unitLine = function(width) {
|
|
1107
2615
|
return " ".repeat(Math.max(0, width - UNIT.length)) + UNIT;
|
|
1108
2616
|
};
|
|
1109
|
-
function
|
|
1110
|
-
|
|
1111
|
-
}
|
|
1112
|
-
function headingLine(width, stale) {
|
|
1113
|
-
const title = headingTitle(stale);
|
|
1114
|
-
const gap = width - title.length - UNIT.length;
|
|
2617
|
+
function headingLine(width) {
|
|
2618
|
+
const gap = width - HEADING_TITLE.length - UNIT.length;
|
|
1115
2619
|
if (gap < 1)
|
|
1116
|
-
return [
|
|
1117
|
-
return [
|
|
2620
|
+
return [HEADING_TITLE, unitLine(width)];
|
|
2621
|
+
return [HEADING_TITLE + " ".repeat(gap) + UNIT];
|
|
1118
2622
|
}
|
|
1119
2623
|
var abbreviate = function(provider, budget) {
|
|
1120
2624
|
if (provider.length <= budget)
|
|
@@ -1182,21 +2686,20 @@ function entryLines(entries, width) {
|
|
|
1182
2686
|
return lines;
|
|
1183
2687
|
}
|
|
1184
2688
|
var NO_QUALIFYING_MESSAGE = "No models with 3+ calls in 24h.";
|
|
1185
|
-
var LOADING_MESSAGE = "Loading\u2026";
|
|
1186
|
-
var READ_ERROR_MESSAGE = "Could not read history.";
|
|
1187
2689
|
var UNIT = "tok/s";
|
|
1188
2690
|
var SEPARATOR = " \xB7 ";
|
|
1189
2691
|
var HEADING_BRAND = "TokScope";
|
|
2692
|
+
var HEADING_TITLE = `${HEADING_BRAND} \xB7 24h`;
|
|
1190
2693
|
|
|
1191
2694
|
// src/sidebar/view.ts
|
|
1192
2695
|
function widgetView(state2, width, entries) {
|
|
1193
2696
|
if (width === undefined)
|
|
1194
2697
|
return;
|
|
2698
|
+
const hasRows = state2.rows.length > 0;
|
|
1195
2699
|
return {
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
lines: state2.status === "ready" ? entryLines(entries, width) : []
|
|
2700
|
+
heading: headingLine(width),
|
|
2701
|
+
pending: !hasRows && state2.status === "loading",
|
|
2702
|
+
lines: hasRows ? entryLines(entries, width) : []
|
|
1200
2703
|
};
|
|
1201
2704
|
}
|
|
1202
2705
|
|
|
@@ -1204,7 +2707,7 @@ function widgetView(state2, width, entries) {
|
|
|
1204
2707
|
function TokscopeSidebar(props) {
|
|
1205
2708
|
const theme = props.context.theme;
|
|
1206
2709
|
const dimensions = useTerminalDimensions2();
|
|
1207
|
-
const [width, setWidth] =
|
|
2710
|
+
const [width, setWidth] = createSignal4();
|
|
1208
2711
|
let target;
|
|
1209
2712
|
let timer;
|
|
1210
2713
|
const measure = (remaining = MEASURE_TICKS) => {
|
|
@@ -1232,7 +2735,7 @@ function TokscopeSidebar(props) {
|
|
|
1232
2735
|
dimensions();
|
|
1233
2736
|
measure();
|
|
1234
2737
|
});
|
|
1235
|
-
|
|
2738
|
+
onCleanup2(() => {
|
|
1236
2739
|
if (timer !== undefined)
|
|
1237
2740
|
clearTimeout(timer);
|
|
1238
2741
|
});
|
|
@@ -1240,7 +2743,7 @@ function TokscopeSidebar(props) {
|
|
|
1240
2743
|
props.context.data.location.model.sync();
|
|
1241
2744
|
props.context.data.location.provider.sync();
|
|
1242
2745
|
const detach = props.store.attach();
|
|
1243
|
-
|
|
2746
|
+
onCleanup2(detach);
|
|
1244
2747
|
});
|
|
1245
2748
|
const entries = createMemo3(() => {
|
|
1246
2749
|
const models = props.context.data.location.model.list();
|
|
@@ -1253,14 +2756,14 @@ function TokscopeSidebar(props) {
|
|
|
1253
2756
|
});
|
|
1254
2757
|
const view2 = () => widgetView(props.store.state(), width(), entries());
|
|
1255
2758
|
return (() => {
|
|
1256
|
-
var _el$ = _$
|
|
2759
|
+
var _el$ = _$createElement7("box");
|
|
1257
2760
|
_$use((element) => {
|
|
1258
2761
|
target = element;
|
|
1259
2762
|
measure();
|
|
1260
2763
|
}, _el$);
|
|
1261
|
-
_$
|
|
1262
|
-
_$
|
|
1263
|
-
_$
|
|
2764
|
+
_$setProp7(_el$, "flexDirection", "column");
|
|
2765
|
+
_$setProp7(_el$, "width", "100%");
|
|
2766
|
+
_$insert7(_el$, _$createComponent4(Show3, {
|
|
1264
2767
|
get when() {
|
|
1265
2768
|
return view2();
|
|
1266
2769
|
},
|
|
@@ -1270,53 +2773,44 @@ function TokscopeSidebar(props) {
|
|
|
1270
2773
|
return current.heading;
|
|
1271
2774
|
},
|
|
1272
2775
|
children: (line, index) => index() === 0 ? (() => {
|
|
1273
|
-
var _el$
|
|
1274
|
-
_$insertNode6(_el$
|
|
1275
|
-
_$insertNode6(_el$
|
|
1276
|
-
_$
|
|
1277
|
-
_$
|
|
1278
|
-
_$
|
|
1279
|
-
_$
|
|
2776
|
+
var _el$2 = _$createElement7("box"), _el$3 = _$createElement7("text"), _el$4 = _$createElement7("text");
|
|
2777
|
+
_$insertNode6(_el$2, _el$3);
|
|
2778
|
+
_$insertNode6(_el$2, _el$4);
|
|
2779
|
+
_$setProp7(_el$2, "flexDirection", "row");
|
|
2780
|
+
_$insert7(_el$3, () => line.slice(0, HEADING_BRAND.length));
|
|
2781
|
+
_$insert7(_el$4, () => line.slice(HEADING_BRAND.length));
|
|
2782
|
+
_$effect7((_p$) => {
|
|
1280
2783
|
var _v$ = theme.text.base, _v$2 = TextAttributes4.BOLD, _v$3 = theme.text.muted;
|
|
1281
|
-
_v$ !== _p$.e && (_p$.e = _$
|
|
1282
|
-
_v$2 !== _p$.t && (_p$.t = _$
|
|
1283
|
-
_v$3 !== _p$.a && (_p$.a = _$
|
|
2784
|
+
_v$ !== _p$.e && (_p$.e = _$setProp7(_el$3, "fg", _v$, _p$.e));
|
|
2785
|
+
_v$2 !== _p$.t && (_p$.t = _$setProp7(_el$3, "attributes", _v$2, _p$.t));
|
|
2786
|
+
_v$3 !== _p$.a && (_p$.a = _$setProp7(_el$4, "fg", _v$3, _p$.a));
|
|
1284
2787
|
return _p$;
|
|
1285
2788
|
}, {
|
|
1286
2789
|
e: undefined,
|
|
1287
2790
|
t: undefined,
|
|
1288
2791
|
a: undefined
|
|
1289
2792
|
});
|
|
1290
|
-
return _el$
|
|
2793
|
+
return _el$2;
|
|
1291
2794
|
})() : (() => {
|
|
1292
|
-
var _el$
|
|
1293
|
-
_$
|
|
1294
|
-
_$
|
|
1295
|
-
return _el$
|
|
2795
|
+
var _el$5 = _$createElement7("text");
|
|
2796
|
+
_$insert7(_el$5, line);
|
|
2797
|
+
_$effect7((_$p) => _$setProp7(_el$5, "fg", theme.text.muted, _$p));
|
|
2798
|
+
return _el$5;
|
|
1296
2799
|
})()
|
|
1297
2800
|
}), _$createComponent4(Show3, {
|
|
1298
2801
|
get when() {
|
|
1299
|
-
return current.
|
|
1300
|
-
},
|
|
1301
|
-
get children() {
|
|
1302
|
-
var _el$2 = _$createElement6("text");
|
|
1303
|
-
_$insert6(_el$2, LOADING_MESSAGE);
|
|
1304
|
-
_$effect6((_$p) => _$setProp6(_el$2, "fg", theme.text.muted, _$p));
|
|
1305
|
-
return _el$2;
|
|
1306
|
-
}
|
|
1307
|
-
}), _$createComponent4(Show3, {
|
|
1308
|
-
get when() {
|
|
1309
|
-
return current.status === "error";
|
|
2802
|
+
return current.pending;
|
|
1310
2803
|
},
|
|
1311
2804
|
get children() {
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
2805
|
+
return _$createComponent4(Spinner, {
|
|
2806
|
+
get context() {
|
|
2807
|
+
return props.context;
|
|
2808
|
+
}
|
|
2809
|
+
});
|
|
1316
2810
|
}
|
|
1317
2811
|
}), _$createComponent4(Show3, {
|
|
1318
2812
|
get when() {
|
|
1319
|
-
return current.
|
|
2813
|
+
return !current.pending;
|
|
1320
2814
|
},
|
|
1321
2815
|
get children() {
|
|
1322
2816
|
return _$createComponent4(Show3, {
|
|
@@ -1325,10 +2819,10 @@ function TokscopeSidebar(props) {
|
|
|
1325
2819
|
},
|
|
1326
2820
|
get fallback() {
|
|
1327
2821
|
return (() => {
|
|
1328
|
-
var _el$
|
|
1329
|
-
_$
|
|
1330
|
-
_$
|
|
1331
|
-
return _el$
|
|
2822
|
+
var _el$6 = _$createElement7("text");
|
|
2823
|
+
_$insert7(_el$6, NO_QUALIFYING_MESSAGE);
|
|
2824
|
+
_$effect7((_$p) => _$setProp7(_el$6, "fg", theme.text.muted, _$p));
|
|
2825
|
+
return _el$6;
|
|
1332
2826
|
})();
|
|
1333
2827
|
},
|
|
1334
2828
|
get children() {
|
|
@@ -1340,10 +2834,10 @@ function TokscopeSidebar(props) {
|
|
|
1340
2834
|
}));
|
|
1341
2835
|
},
|
|
1342
2836
|
children: (line) => (() => {
|
|
1343
|
-
var _el$
|
|
1344
|
-
_$
|
|
1345
|
-
_$
|
|
1346
|
-
return _el$
|
|
2837
|
+
var _el$7 = _$createElement7("text");
|
|
2838
|
+
_$insert7(_el$7, () => line.text);
|
|
2839
|
+
_$effect7((_$p) => _$setProp7(_el$7, "fg", theme.text.muted, _$p));
|
|
2840
|
+
return _el$7;
|
|
1347
2841
|
})()
|
|
1348
2842
|
});
|
|
1349
2843
|
}
|
|
@@ -1351,7 +2845,7 @@ function TokscopeSidebar(props) {
|
|
|
1351
2845
|
}
|
|
1352
2846
|
})]
|
|
1353
2847
|
}));
|
|
1354
|
-
_$
|
|
2848
|
+
_$effect7((_$p) => _$setProp7(_el$, "onMouseDown", props.open, _$p));
|
|
1355
2849
|
return _el$;
|
|
1356
2850
|
})();
|
|
1357
2851
|
}
|
|
@@ -1361,7 +2855,7 @@ var MEASURE_TICKS = 10;
|
|
|
1361
2855
|
var tui_default = Plugin.define({
|
|
1362
2856
|
id: "tokscope",
|
|
1363
2857
|
setup(context) {
|
|
1364
|
-
const [previous, setPrevious] =
|
|
2858
|
+
const [previous, setPrevious] = createSignal5({
|
|
1365
2859
|
...context.ui.router.current()
|
|
1366
2860
|
});
|
|
1367
2861
|
const openTokscope = () => {
|
|
@@ -1377,19 +2871,38 @@ var tui_default = Plugin.define({
|
|
|
1377
2871
|
name: "tokscope"
|
|
1378
2872
|
});
|
|
1379
2873
|
};
|
|
1380
|
-
|
|
2874
|
+
let unsubscribeEvents;
|
|
2875
|
+
const historyLogger = createHistoryFileLogger();
|
|
2876
|
+
const service2 = createHistoryService({
|
|
1381
2877
|
client: context.client,
|
|
1382
|
-
|
|
2878
|
+
repository: createRepository(createPluginDurableStore(context.storage)),
|
|
2879
|
+
subscribeEvents: (handler) => {
|
|
2880
|
+
unsubscribeEvents = context.data.listen(({
|
|
2881
|
+
details
|
|
2882
|
+
}) => handler({
|
|
2883
|
+
type: details.type,
|
|
2884
|
+
data: details.data
|
|
2885
|
+
}));
|
|
2886
|
+
return unsubscribeEvents;
|
|
2887
|
+
},
|
|
1383
2888
|
setTimer: (callback, ms) => {
|
|
1384
2889
|
const id = setInterval(callback, ms);
|
|
1385
2890
|
return () => clearInterval(id);
|
|
1386
2891
|
},
|
|
1387
|
-
|
|
2892
|
+
delay: (callback, ms) => {
|
|
2893
|
+
const id = setTimeout(callback, ms);
|
|
2894
|
+
return () => clearTimeout(id);
|
|
2895
|
+
},
|
|
2896
|
+
now: () => Date.now(),
|
|
2897
|
+
log: historyLogger.log
|
|
1388
2898
|
});
|
|
2899
|
+
const release = service2.hold();
|
|
2900
|
+
const store3 = createWidgetStore(service2);
|
|
1389
2901
|
context.ui.router.register({
|
|
1390
2902
|
name: "tokscope",
|
|
1391
2903
|
render: () => _$createComponent5(TokscopePage, {
|
|
1392
2904
|
context,
|
|
2905
|
+
service: service2,
|
|
1393
2906
|
onClose: () => context.ui.router.navigate(previous())
|
|
1394
2907
|
})
|
|
1395
2908
|
});
|
|
@@ -1397,7 +2910,7 @@ var tui_default = Plugin.define({
|
|
|
1397
2910
|
append: "sidebar.content",
|
|
1398
2911
|
render: () => _$createComponent5(TokscopeSidebar, {
|
|
1399
2912
|
context,
|
|
1400
|
-
store:
|
|
2913
|
+
store: store3,
|
|
1401
2914
|
open: openTokscope
|
|
1402
2915
|
})
|
|
1403
2916
|
});
|
|
@@ -1415,11 +2928,32 @@ var tui_default = Plugin.define({
|
|
|
1415
2928
|
},
|
|
1416
2929
|
palette: true,
|
|
1417
2930
|
run: openTokscope
|
|
2931
|
+
}, {
|
|
2932
|
+
id: "tokscope.resetHistory",
|
|
2933
|
+
title: "Tokscope: reset history cache",
|
|
2934
|
+
group: "System",
|
|
2935
|
+
palette: true,
|
|
2936
|
+
run: async () => {
|
|
2937
|
+
const confirmed = await context.ui.dialog.confirm({
|
|
2938
|
+
title: "Reset Tokscope history cache",
|
|
2939
|
+
message: "Delete the shared Tokscope history cache and rebuild it from the server?",
|
|
2940
|
+
label: {
|
|
2941
|
+
confirm: "Reset",
|
|
2942
|
+
cancel: "Cancel"
|
|
2943
|
+
}
|
|
2944
|
+
});
|
|
2945
|
+
if (confirmed === true)
|
|
2946
|
+
await service2.reset();
|
|
2947
|
+
}
|
|
1418
2948
|
}]
|
|
1419
2949
|
}));
|
|
1420
2950
|
return null;
|
|
1421
2951
|
}
|
|
1422
2952
|
});
|
|
2953
|
+
return () => {
|
|
2954
|
+
unsubscribeEvents?.();
|
|
2955
|
+
release();
|
|
2956
|
+
};
|
|
1423
2957
|
}
|
|
1424
2958
|
});
|
|
1425
2959
|
export {
|