opencode-prompt-logger 0.1.0 → 0.2.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.
@@ -0,0 +1,585 @@
1
+ // @bun
2
+ // src/rpc.ts
3
+ import { Rpc } from "@opencode/plugin/rpc";
4
+
5
+ // src/core.ts
6
+ var DEFAULT_DISK_BUDGET = 256 * 1024 * 1024;
7
+ var CONTEXT_KINDS = ["context", "compaction", "generate", "title"];
8
+
9
+ // src/settings.ts
10
+ var SETTINGS_KEY = "settings";
11
+ var MAX_SETTINGS_DISK_BUDGET = 8 * 1024 * 1024 * 1024;
12
+ var MAX_SETTINGS_RETENTION_DAYS = 3650;
13
+ var MAX_SETTINGS_RETENTION_SESSIONS = 1e4;
14
+ var MAX_SETTINGS_DIRECTORY = 4096;
15
+ var DEFAULT_SETTINGS = {
16
+ enabled: true,
17
+ http: false,
18
+ diskBudget: DEFAULT_DISK_BUDGET,
19
+ retentionMaxAgeDays: 30,
20
+ retentionMaxSessions: 200,
21
+ directory: "",
22
+ maintenance: true
23
+ };
24
+ function defaultSettings() {
25
+ return { ...DEFAULT_SETTINGS };
26
+ }
27
+ function isRecord(value) {
28
+ return value !== null && typeof value === "object" && !Array.isArray(value);
29
+ }
30
+ function isBoundedInteger(value, max) {
31
+ return typeof value === "number" && Number.isFinite(value) && value >= 0 && Math.floor(value) === value && value <= max;
32
+ }
33
+ function boundedInteger(value, max, fallback) {
34
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0)
35
+ return fallback;
36
+ return Math.min(max, Math.floor(value));
37
+ }
38
+ function directoryValue(value) {
39
+ if (typeof value !== "string")
40
+ return;
41
+ return value.trim().slice(0, MAX_SETTINGS_DIRECTORY);
42
+ }
43
+ function overlay(target, value) {
44
+ if (!isRecord(value))
45
+ return target;
46
+ const next = { ...target };
47
+ if (typeof value.enabled === "boolean")
48
+ next.enabled = value.enabled;
49
+ if (typeof value.http === "boolean")
50
+ next.http = value.http;
51
+ next.diskBudget = boundedInteger(value.diskBudget, MAX_SETTINGS_DISK_BUDGET, next.diskBudget);
52
+ next.retentionMaxAgeDays = boundedInteger(value.retentionMaxAgeDays, MAX_SETTINGS_RETENTION_DAYS, next.retentionMaxAgeDays);
53
+ next.retentionMaxSessions = boundedInteger(value.retentionMaxSessions, MAX_SETTINGS_RETENTION_SESSIONS, next.retentionMaxSessions);
54
+ const directory = directoryValue(value.directory);
55
+ if (directory !== undefined)
56
+ next.directory = directory;
57
+ if (typeof value.maintenance === "boolean")
58
+ next.maintenance = value.maintenance;
59
+ return next;
60
+ }
61
+ function sanitizeSettings(value) {
62
+ return overlay(defaultSettings(), value);
63
+ }
64
+ function isValidSettings(value) {
65
+ if (!isRecord(value))
66
+ return false;
67
+ if (typeof value.enabled !== "boolean" || typeof value.http !== "boolean")
68
+ return false;
69
+ if (typeof value.maintenance !== "boolean")
70
+ return false;
71
+ if (typeof value.directory !== "string" || value.directory.length > MAX_SETTINGS_DIRECTORY)
72
+ return false;
73
+ return isBoundedInteger(value.diskBudget, MAX_SETTINGS_DISK_BUDGET) && isBoundedInteger(value.retentionMaxAgeDays, MAX_SETTINGS_RETENTION_DAYS) && isBoundedInteger(value.retentionMaxSessions, MAX_SETTINGS_RETENTION_SESSIONS);
74
+ }
75
+ function settingsFromEnvelope(value) {
76
+ return sanitizeSettings(isRecord(value) ? value.settings : undefined);
77
+ }
78
+ function pickSettings(value) {
79
+ return {
80
+ enabled: value.enabled,
81
+ http: value.http,
82
+ diskBudget: value.diskBudget,
83
+ retentionMaxAgeDays: value.retentionMaxAgeDays,
84
+ retentionMaxSessions: value.retentionMaxSessions,
85
+ directory: value.directory,
86
+ maintenance: value.maintenance
87
+ };
88
+ }
89
+
90
+ // src/rpc.ts
91
+ var CONTRACT_VERSION = 6;
92
+ var nullableString = { type: ["string", "null"] };
93
+ var modelSchema = {
94
+ anyOf: [
95
+ { type: "null" },
96
+ {
97
+ type: "object",
98
+ properties: {
99
+ providerID: { type: "string" },
100
+ id: { type: "string" },
101
+ variant: { type: "string" }
102
+ },
103
+ additionalProperties: false
104
+ }
105
+ ]
106
+ };
107
+ var summarySchema = {
108
+ type: "object",
109
+ properties: {
110
+ key: { type: "string" },
111
+ cursor: { type: "string" },
112
+ bytes: { type: "number" },
113
+ at: nullableString,
114
+ type: nullableString,
115
+ kind: nullableString,
116
+ sessionID: nullableString,
117
+ agent: nullableString,
118
+ model: modelSchema,
119
+ url: nullableString,
120
+ messages: { type: "number" },
121
+ bodyBytes: { type: "number" },
122
+ hasBody: { type: "boolean" }
123
+ },
124
+ required: ["key", "cursor", "bytes", "at", "type", "kind", "sessionID", "agent", "model", "url"],
125
+ additionalProperties: false
126
+ };
127
+ var storeStatusSchema = {
128
+ type: "object",
129
+ properties: {
130
+ path: { type: "string" },
131
+ exists: { type: "boolean" },
132
+ bytes: { type: "number" },
133
+ mtimeMs: { anyOf: [{ type: "number" }, { type: "null" }] },
134
+ directory: { type: "boolean" },
135
+ symlink: { type: "boolean" },
136
+ mode: { anyOf: [{ type: "number" }, { type: "null" }] },
137
+ directoryMode: { anyOf: [{ type: "number" }, { type: "null" }] },
138
+ warnings: { type: "array", items: { type: "string" } }
139
+ },
140
+ required: ["path", "exists", "bytes", "mtimeMs", "directory", "symlink", "mode", "directoryMode", "warnings"],
141
+ additionalProperties: false
142
+ };
143
+ var cacheSchema = {
144
+ type: "object",
145
+ properties: {
146
+ requests: { type: "number" },
147
+ requestBytes: { type: "number" },
148
+ lastScanMs: { type: "number" },
149
+ error: nullableString
150
+ },
151
+ required: ["requests", "requestBytes", "lastScanMs", "error"],
152
+ additionalProperties: false
153
+ };
154
+ var settingsSchema = {
155
+ type: "object",
156
+ properties: {
157
+ enabled: { type: "boolean" },
158
+ http: { type: "boolean" },
159
+ diskBudget: { type: "number", minimum: 0, maximum: MAX_SETTINGS_DISK_BUDGET },
160
+ retentionMaxAgeDays: { type: "number", minimum: 0, maximum: MAX_SETTINGS_RETENTION_DAYS },
161
+ retentionMaxSessions: { type: "number", minimum: 0, maximum: MAX_SETTINGS_RETENTION_SESSIONS },
162
+ directory: { type: "string", maxLength: MAX_SETTINGS_DIRECTORY },
163
+ maintenance: { type: "boolean" }
164
+ },
165
+ required: [
166
+ "enabled",
167
+ "http",
168
+ "diskBudget",
169
+ "retentionMaxAgeDays",
170
+ "retentionMaxSessions",
171
+ "directory",
172
+ "maintenance"
173
+ ],
174
+ additionalProperties: false
175
+ };
176
+ var statusSettingsSchema = {
177
+ type: "object",
178
+ properties: {
179
+ ...settingsSchema.properties,
180
+ retention: {
181
+ type: "object",
182
+ properties: {
183
+ maxTotalBytes: { anyOf: [{ type: "number" }, { type: "null" }] },
184
+ maxAgeDays: { type: "number" },
185
+ maxSessions: { anyOf: [{ type: "number" }, { type: "null" }] }
186
+ },
187
+ required: ["maxTotalBytes", "maxAgeDays", "maxSessions"],
188
+ additionalProperties: false
189
+ }
190
+ },
191
+ required: [...settingsSchema.required, "retention"],
192
+ additionalProperties: false
193
+ };
194
+ var sessionScopeSchema = {
195
+ type: "object",
196
+ properties: {
197
+ mode: { type: "string", enum: ["all", "only"] },
198
+ sessions: { type: "array", items: { type: "string", maxLength: 200 } },
199
+ max: { type: "number" }
200
+ },
201
+ required: ["mode", "sessions", "max"],
202
+ additionalProperties: false
203
+ };
204
+ var settingsResponseSchema = {
205
+ type: "object",
206
+ properties: {
207
+ settings: settingsSchema,
208
+ sessionScope: sessionScopeSchema
209
+ },
210
+ required: ["settings", "sessionScope"],
211
+ additionalProperties: false
212
+ };
213
+ var errorSchema = {
214
+ type: "object",
215
+ properties: { message: { type: "string" } },
216
+ additionalProperties: false
217
+ };
218
+ var errors = {
219
+ io: errorSchema,
220
+ not_found: errorSchema,
221
+ bad_request: errorSchema,
222
+ forbidden: errorSchema
223
+ };
224
+ var filtersSchema = {
225
+ sessionID: { type: "string", maxLength: 200 },
226
+ type: { type: "string", maxLength: 80 },
227
+ kind: { type: "string", maxLength: 80 },
228
+ agent: { type: "string", maxLength: 200 },
229
+ model: { type: "string", maxLength: 200 },
230
+ query: { type: "string", maxLength: 200 }
231
+ };
232
+ var PromptLogger = Rpc.define({
233
+ id: "prompt-logger",
234
+ methods: {
235
+ status: {
236
+ input: { type: "object", properties: {}, additionalProperties: false },
237
+ output: {
238
+ type: "object",
239
+ properties: {
240
+ version: { type: "number" },
241
+ store: storeStatusSchema,
242
+ settings: statusSettingsSchema,
243
+ sessionScope: sessionScopeSchema,
244
+ cache: cacheSchema,
245
+ lastWriteAt: { anyOf: [{ type: "string" }, { type: "null" }] },
246
+ lastWriteError: {
247
+ anyOf: [
248
+ { type: "null" },
249
+ {
250
+ type: "object",
251
+ properties: {
252
+ at: { type: "string" },
253
+ code: { type: "string" },
254
+ message: { type: "string" }
255
+ },
256
+ required: ["at", "message"],
257
+ additionalProperties: false
258
+ }
259
+ ]
260
+ }
261
+ },
262
+ required: ["version", "store", "settings", "sessionScope", "cache", "lastWriteAt", "lastWriteError"],
263
+ additionalProperties: false
264
+ },
265
+ errors
266
+ },
267
+ list: {
268
+ input: {
269
+ type: "object",
270
+ properties: {
271
+ ...filtersSchema,
272
+ limit: { type: "number" },
273
+ cursor: { type: "string" }
274
+ },
275
+ additionalProperties: false
276
+ },
277
+ output: {
278
+ type: "object",
279
+ properties: {
280
+ entries: { type: "array", items: summarySchema },
281
+ hasMore: { type: "boolean" },
282
+ records: { type: "number" },
283
+ error: nullableString
284
+ },
285
+ required: ["entries", "hasMore", "records", "error"],
286
+ additionalProperties: false
287
+ },
288
+ errors
289
+ },
290
+ read: {
291
+ input: {
292
+ type: "object",
293
+ properties: {
294
+ key: { type: "string", maxLength: 300 },
295
+ maxBytes: { type: "number" }
296
+ },
297
+ required: ["key"],
298
+ additionalProperties: false
299
+ },
300
+ output: {
301
+ type: "object",
302
+ properties: {
303
+ record: {},
304
+ bytes: { type: "number" },
305
+ truncated: { type: "boolean" },
306
+ moreAvailable: { type: "boolean" },
307
+ fullBytes: { type: "number" },
308
+ paths: {
309
+ type: "object",
310
+ properties: {
311
+ session: { type: "string" },
312
+ requests: { type: "string" },
313
+ body: { type: "string" }
314
+ },
315
+ additionalProperties: false
316
+ }
317
+ },
318
+ required: ["record", "bytes", "truncated"],
319
+ additionalProperties: false
320
+ },
321
+ errors
322
+ },
323
+ search: {
324
+ input: {
325
+ type: "object",
326
+ properties: {
327
+ ...filtersSchema,
328
+ query: { type: "string", minLength: 1, maxLength: 500 },
329
+ limit: { type: "number" },
330
+ ignoreCase: { type: "boolean" },
331
+ budgetBytes: { type: "number" },
332
+ budgetMs: { type: "number" }
333
+ },
334
+ required: ["query"],
335
+ additionalProperties: false
336
+ },
337
+ output: {
338
+ type: "object",
339
+ properties: {
340
+ matches: {
341
+ type: "array",
342
+ items: {
343
+ type: "object",
344
+ properties: { entry: summarySchema, snippet: { type: "string" } },
345
+ required: ["entry", "snippet"],
346
+ additionalProperties: false
347
+ }
348
+ },
349
+ partial: { type: "boolean" },
350
+ scannedBytes: { type: "number" },
351
+ skipped: { type: "number" },
352
+ error: nullableString
353
+ },
354
+ required: ["matches", "partial", "scannedBytes", "skipped", "error"],
355
+ additionalProperties: false
356
+ },
357
+ errors
358
+ },
359
+ truncate: {
360
+ input: {
361
+ type: "object",
362
+ properties: {
363
+ target: { type: "string", enum: ["current", "all"] },
364
+ sessionID: { type: "string", maxLength: 200 },
365
+ confirm: { type: "boolean", enum: [true] }
366
+ },
367
+ required: ["target", "confirm"],
368
+ additionalProperties: false
369
+ },
370
+ output: {
371
+ type: "object",
372
+ properties: {
373
+ target: { type: "string", enum: ["current", "all"] },
374
+ bytesFreed: { type: "number" }
375
+ },
376
+ required: ["target", "bytesFreed"],
377
+ additionalProperties: false
378
+ },
379
+ errors
380
+ },
381
+ getSettings: {
382
+ input: { type: "object", properties: {}, additionalProperties: false },
383
+ output: settingsResponseSchema,
384
+ errors
385
+ },
386
+ setSettings: {
387
+ input: {
388
+ type: "object",
389
+ properties: { settings: settingsSchema },
390
+ required: ["settings"],
391
+ additionalProperties: false
392
+ },
393
+ output: settingsResponseSchema,
394
+ errors
395
+ },
396
+ resetSettings: {
397
+ input: { type: "object", properties: {}, additionalProperties: false },
398
+ output: settingsResponseSchema,
399
+ errors
400
+ },
401
+ updateSessionScope: {
402
+ input: {
403
+ type: "object",
404
+ properties: {
405
+ mode: { type: "string", enum: ["all", "only"] },
406
+ action: { type: "string", enum: ["only", "disarm", "clear"] },
407
+ sessionID: { type: "string", maxLength: 200 }
408
+ },
409
+ additionalProperties: false
410
+ },
411
+ output: {
412
+ type: "object",
413
+ properties: {
414
+ sessionScope: sessionScopeSchema,
415
+ settings: settingsSchema
416
+ },
417
+ required: ["sessionScope", "settings"],
418
+ additionalProperties: false
419
+ },
420
+ errors
421
+ }
422
+ },
423
+ events: {
424
+ changed: {
425
+ schema: {
426
+ type: "object",
427
+ properties: {
428
+ store: { type: "string" },
429
+ reason: { type: "string", enum: ["append", "truncate", "error"] },
430
+ at: { type: "string" }
431
+ },
432
+ required: ["store", "reason", "at"],
433
+ additionalProperties: false
434
+ }
435
+ },
436
+ settingsChanged: {
437
+ schema: settingsResponseSchema
438
+ }
439
+ }
440
+ });
441
+
442
+ // src/log/limits.ts
443
+ var DEFAULT_PAGE = 50;
444
+ var MAX_PAGE = 200;
445
+ var MAX_RECORD_BYTES = 2 * 1024 * 1024;
446
+ var MAX_FULL_RECORD_BYTES = 16 * 1024 * 1024;
447
+ var MAX_SEARCH_BYTES = 128 * 1024 * 1024;
448
+ var MAX_SEARCH_MS = 2000;
449
+ var MAX_SEARCH_QUERY = 500;
450
+ var MAX_SEARCH_RESULTS = 100;
451
+ var EVENT_COALESCE_MS = 1000;
452
+ function clampInteger(value, min, max, fallback) {
453
+ if (typeof value !== "number" || !Number.isFinite(value))
454
+ return fallback;
455
+ return Math.min(max, Math.max(min, Math.round(value)));
456
+ }
457
+ function clampPage(value) {
458
+ return clampInteger(value, 1, MAX_PAGE, DEFAULT_PAGE);
459
+ }
460
+ function clampReadBytes(value) {
461
+ return clampInteger(value, 1024, MAX_FULL_RECORD_BYTES, MAX_RECORD_BYTES);
462
+ }
463
+ function clampSearchBytes(value) {
464
+ return clampInteger(value, 1024 * 1024, MAX_SEARCH_BYTES, MAX_SEARCH_BYTES);
465
+ }
466
+ function clampSearchMs(value) {
467
+ return clampInteger(value, 100, MAX_SEARCH_MS, MAX_SEARCH_MS);
468
+ }
469
+ function boundedString(value, maxLength) {
470
+ if (typeof value !== "string")
471
+ return;
472
+ const trimmed = value.trim();
473
+ if (trimmed === "")
474
+ return;
475
+ return trimmed.length > maxLength ? trimmed.slice(0, maxLength) : trimmed;
476
+ }
477
+
478
+ // src/rpc-client.ts
479
+ function asSettingsResponse(value) {
480
+ const response = value;
481
+ return {
482
+ settings: settingsFromEnvelope(value),
483
+ sessionScope: response.sessionScope
484
+ };
485
+ }
486
+ function promptLogApi(client, location) {
487
+ const rpc = client.rpc(PromptLogger);
488
+ const options = (signal) => ({
489
+ ...location?.directory === undefined ? {} : { location: { directory: location.directory } },
490
+ ...signal === undefined ? {} : { signal }
491
+ });
492
+ return {
493
+ status(signal) {
494
+ return rpc.status({}, options(signal));
495
+ },
496
+ list(input = {}, signal) {
497
+ return rpc.list(clampList(input), options(signal));
498
+ },
499
+ read(input, signal) {
500
+ return rpc.read(clampRead(input), options(signal));
501
+ },
502
+ search(input, signal) {
503
+ return rpc.search(clampSearch(input), options(signal));
504
+ },
505
+ truncate(target, sessionID) {
506
+ return rpc.truncate({
507
+ target,
508
+ confirm: true,
509
+ ...sessionID === undefined ? {} : { sessionID: boundedString(sessionID, 200) }
510
+ }, options());
511
+ },
512
+ getSettings() {
513
+ return rpc.getSettings({}, options()).then(asSettingsResponse);
514
+ },
515
+ setSettings(settings) {
516
+ const input = { settings: pickSettings(settings) };
517
+ return rpc.setSettings(input, options()).then(asSettingsResponse);
518
+ },
519
+ resetSettings() {
520
+ return rpc.resetSettings({}, options()).then(asSettingsResponse);
521
+ },
522
+ updateSessionScope(input) {
523
+ const sessionID = boundedString(input.sessionID, 200);
524
+ return rpc.updateSessionScope({
525
+ ...input.mode === undefined ? {} : { mode: input.mode },
526
+ ...input.action === undefined ? {} : { action: input.action },
527
+ ...sessionID === undefined ? {} : { sessionID }
528
+ }, options());
529
+ },
530
+ onChanged(handler, signal) {
531
+ if (typeof rpc.events?.on !== "function")
532
+ return () => {};
533
+ return rpc.events.on("changed", (event) => handler(event.data), signal === undefined ? undefined : { signal });
534
+ },
535
+ onSettingsChanged(handler, signal) {
536
+ if (typeof rpc.events?.on !== "function")
537
+ return () => {};
538
+ return rpc.events.on("settingsChanged", (event) => handler(asSettingsResponse(event.data)), signal === undefined ? undefined : { signal });
539
+ },
540
+ contractVersion: CONTRACT_VERSION
541
+ };
542
+ }
543
+ function filtersOf(input) {
544
+ const sessionID = boundedString(input.sessionID, 200);
545
+ const type = boundedString(input.type, 80);
546
+ const kind = boundedString(input.kind, 80);
547
+ const agent = boundedString(input.agent, 200);
548
+ const model = boundedString(input.model, 200);
549
+ return {
550
+ ...sessionID === undefined ? {} : { sessionID },
551
+ ...type === undefined ? {} : { type },
552
+ ...kind === undefined ? {} : { kind },
553
+ ...agent === undefined ? {} : { agent },
554
+ ...model === undefined ? {} : { model }
555
+ };
556
+ }
557
+ function clampList(input) {
558
+ return {
559
+ ...filtersOf(input),
560
+ ...boundedString(input.query, 200) === undefined ? {} : { query: boundedString(input.query, 200) },
561
+ limit: clampPage(input.limit),
562
+ ...boundedString(input.cursor, 400) === undefined ? {} : { cursor: boundedString(input.cursor, 400) }
563
+ };
564
+ }
565
+ function clampRead(input) {
566
+ return {
567
+ key: boundedString(input.key, 300) ?? "",
568
+ maxBytes: clampReadBytes(input.maxBytes)
569
+ };
570
+ }
571
+ function clampSearch(input) {
572
+ return {
573
+ ...filtersOf(input),
574
+ query: (input.query ?? "").trim().slice(0, 500),
575
+ limit: clampInteger(input.limit, 1, MAX_SEARCH_RESULTS, 50),
576
+ ignoreCase: input.ignoreCase === true,
577
+ budgetBytes: clampSearchBytes(input.budgetBytes),
578
+ budgetMs: clampSearchMs(input.budgetMs)
579
+ };
580
+ }
581
+ export {
582
+ promptLogApi
583
+ };
584
+
585
+ //# debugId=0113F8B3548B37A564756E2164756E21