@shipfox/api-logs 2.0.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/.turbo/turbo-build.log +2 -0
- package/.turbo/turbo-type$colon$emit.log +1 -0
- package/.turbo/turbo-type.log +1 -0
- package/CHANGELOG.md +179 -0
- package/LICENSE +21 -0
- package/README.md +151 -0
- package/dist/api/object-storage.d.ts +69 -0
- package/dist/api/object-storage.d.ts.map +1 -0
- package/dist/api/object-storage.js +199 -0
- package/dist/api/object-storage.js.map +1 -0
- package/dist/config.d.ts +20 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +108 -0
- package/dist/config.js.map +1 -0
- package/dist/core/append-logs.d.ts +28 -0
- package/dist/core/append-logs.d.ts.map +1 -0
- package/dist/core/append-logs.js +318 -0
- package/dist/core/append-logs.js.map +1 -0
- package/dist/core/budget.d.ts +12 -0
- package/dist/core/budget.d.ts.map +1 -0
- package/dist/core/budget.js +9 -0
- package/dist/core/budget.js.map +1 -0
- package/dist/core/close-stream.d.ts +27 -0
- package/dist/core/close-stream.d.ts.map +1 -0
- package/dist/core/close-stream.js +59 -0
- package/dist/core/close-stream.js.map +1 -0
- package/dist/core/compaction.d.ts +28 -0
- package/dist/core/compaction.d.ts.map +1 -0
- package/dist/core/compaction.js +49 -0
- package/dist/core/compaction.js.map +1 -0
- package/dist/core/entities/attempt-stream.d.ts +37 -0
- package/dist/core/entities/attempt-stream.d.ts.map +1 -0
- package/dist/core/entities/attempt-stream.js +15 -0
- package/dist/core/entities/attempt-stream.js.map +1 -0
- package/dist/core/entities/job-accounting.d.ts +12 -0
- package/dist/core/entities/job-accounting.d.ts.map +1 -0
- package/dist/core/entities/job-accounting.js +6 -0
- package/dist/core/entities/job-accounting.js.map +1 -0
- package/dist/core/entities/log-object.d.ts +13 -0
- package/dist/core/entities/log-object.d.ts.map +1 -0
- package/dist/core/entities/log-object.js +9 -0
- package/dist/core/entities/log-object.js.map +1 -0
- package/dist/core/errors.d.ts +26 -0
- package/dist/core/errors.d.ts.map +1 -0
- package/dist/core/errors.js +31 -0
- package/dist/core/errors.js.map +1 -0
- package/dist/core/finalize-attempt-stream.d.ts +22 -0
- package/dist/core/finalize-attempt-stream.d.ts.map +1 -0
- package/dist/core/finalize-attempt-stream.js +48 -0
- package/dist/core/finalize-attempt-stream.js.map +1 -0
- package/dist/core/read-logs.d.ts +33 -0
- package/dist/core/read-logs.d.ts.map +1 -0
- package/dist/core/read-logs.js +49 -0
- package/dist/core/read-logs.js.map +1 -0
- package/dist/core/reap-stale-open-streams.d.ts +27 -0
- package/dist/core/reap-stale-open-streams.d.ts.map +1 -0
- package/dist/core/reap-stale-open-streams.js +50 -0
- package/dist/core/reap-stale-open-streams.js.map +1 -0
- package/dist/core/retention.d.ts +32 -0
- package/dist/core/retention.d.ts.map +1 -0
- package/dist/core/retention.js +148 -0
- package/dist/core/retention.js.map +1 -0
- package/dist/core/session/claude/rows.d.ts +6 -0
- package/dist/core/session/claude/rows.d.ts.map +1 -0
- package/dist/core/session/claude/rows.js +145 -0
- package/dist/core/session/claude/rows.js.map +1 -0
- package/dist/core/session/claude-parser.d.ts +4 -0
- package/dist/core/session/claude-parser.d.ts.map +1 -0
- package/dist/core/session/claude-parser.js +42 -0
- package/dist/core/session/claude-parser.js.map +1 -0
- package/dist/core/session/entry-schema.d.ts +51 -0
- package/dist/core/session/entry-schema.d.ts.map +1 -0
- package/dist/core/session/entry-schema.js +61 -0
- package/dist/core/session/entry-schema.js.map +1 -0
- package/dist/core/session/object.d.ts +12 -0
- package/dist/core/session/object.d.ts.map +1 -0
- package/dist/core/session/object.js +50 -0
- package/dist/core/session/object.js.map +1 -0
- package/dist/core/session/parse-session.d.ts +5 -0
- package/dist/core/session/parse-session.d.ts.map +1 -0
- package/dist/core/session/parse-session.js +18 -0
- package/dist/core/session/parse-session.js.map +1 -0
- package/dist/core/session/pi/message-rows.d.ts +4 -0
- package/dist/core/session/pi/message-rows.d.ts.map +1 -0
- package/dist/core/session/pi/message-rows.js +113 -0
- package/dist/core/session/pi/message-rows.js.map +1 -0
- package/dist/core/session/pi/metadata.d.ts +18 -0
- package/dist/core/session/pi/metadata.d.ts.map +1 -0
- package/dist/core/session/pi/metadata.js +120 -0
- package/dist/core/session/pi/metadata.js.map +1 -0
- package/dist/core/session/pi/text.d.ts +12 -0
- package/dist/core/session/pi/text.d.ts.map +1 -0
- package/dist/core/session/pi/text.js +59 -0
- package/dist/core/session/pi/text.js.map +1 -0
- package/dist/core/session/pi-parser.d.ts +5 -0
- package/dist/core/session/pi-parser.d.ts.map +1 -0
- package/dist/core/session/pi-parser.js +73 -0
- package/dist/core/session/pi-parser.js.map +1 -0
- package/dist/core/session/rows.d.ts +7 -0
- package/dist/core/session/rows.d.ts.map +1 -0
- package/dist/core/session/rows.js +39 -0
- package/dist/core/session/rows.js.map +1 -0
- package/dist/core/session/session-record.d.ts +5 -0
- package/dist/core/session/session-record.d.ts.map +1 -0
- package/dist/core/session/session-record.js +3 -0
- package/dist/core/session/session-record.js.map +1 -0
- package/dist/db/accounting.d.ts +33 -0
- package/dist/db/accounting.d.ts.map +1 -0
- package/dist/db/accounting.js +52 -0
- package/dist/db/accounting.js.map +1 -0
- package/dist/db/chunks.d.ts +62 -0
- package/dist/db/chunks.d.ts.map +1 -0
- package/dist/db/chunks.js +88 -0
- package/dist/db/chunks.js.map +1 -0
- package/dist/db/db.d.ts +752 -0
- package/dist/db/db.d.ts.map +1 -0
- package/dist/db/db.js +24 -0
- package/dist/db/db.js.map +1 -0
- package/dist/db/index.d.ts +2 -0
- package/dist/db/index.d.ts.map +1 -0
- package/dist/db/index.js +5 -0
- package/dist/db/index.js.map +1 -0
- package/dist/db/schema/attempt-streams.d.ts +304 -0
- package/dist/db/schema/attempt-streams.d.ts.map +1 -0
- package/dist/db/schema/attempt-streams.js +100 -0
- package/dist/db/schema/attempt-streams.js.map +1 -0
- package/dist/db/schema/chunks.d.ts +166 -0
- package/dist/db/schema/chunks.d.ts.map +1 -0
- package/dist/db/schema/chunks.js +47 -0
- package/dist/db/schema/chunks.js.map +1 -0
- package/dist/db/schema/common.d.ts +29 -0
- package/dist/db/schema/common.d.ts.map +1 -0
- package/dist/db/schema/common.js +11 -0
- package/dist/db/schema/common.js.map +1 -0
- package/dist/db/schema/job-accounting.d.ts +143 -0
- package/dist/db/schema/job-accounting.d.ts.map +1 -0
- package/dist/db/schema/job-accounting.js +43 -0
- package/dist/db/schema/job-accounting.js.map +1 -0
- package/dist/db/schema/outbox.d.ts +195 -0
- package/dist/db/schema/outbox.d.ts.map +1 -0
- package/dist/db/schema/outbox.js +5 -0
- package/dist/db/schema/outbox.js.map +1 -0
- package/dist/db/streams.d.ts +141 -0
- package/dist/db/streams.d.ts.map +1 -0
- package/dist/db/streams.js +197 -0
- package/dist/db/streams.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +81 -0
- package/dist/index.js.map +1 -0
- package/dist/metrics/index.d.ts +2 -0
- package/dist/metrics/index.d.ts.map +1 -0
- package/dist/metrics/index.js +3 -0
- package/dist/metrics/index.js.map +1 -0
- package/dist/metrics/instance.d.ts +14 -0
- package/dist/metrics/instance.d.ts.map +1 -0
- package/dist/metrics/instance.js +16 -0
- package/dist/metrics/instance.js.map +1 -0
- package/dist/metrics/service.d.ts +2 -0
- package/dist/metrics/service.d.ts.map +1 -0
- package/dist/metrics/service.js +20 -0
- package/dist/metrics/service.js.map +1 -0
- package/dist/presentation/dto/read-logs.d.ts +4 -0
- package/dist/presentation/dto/read-logs.d.ts.map +1 -0
- package/dist/presentation/dto/read-logs.js +22 -0
- package/dist/presentation/dto/read-logs.js.map +1 -0
- package/dist/presentation/routes/append-logs.d.ts +2 -0
- package/dist/presentation/routes/append-logs.d.ts.map +1 -0
- package/dist/presentation/routes/append-logs.js +71 -0
- package/dist/presentation/routes/append-logs.js.map +1 -0
- package/dist/presentation/routes/index.d.ts +3 -0
- package/dist/presentation/routes/index.d.ts.map +1 -0
- package/dist/presentation/routes/index.js +32 -0
- package/dist/presentation/routes/index.js.map +1 -0
- package/dist/presentation/routes/read-logs.d.ts +2 -0
- package/dist/presentation/routes/read-logs.d.ts.map +1 -0
- package/dist/presentation/routes/read-logs.js +41 -0
- package/dist/presentation/routes/read-logs.js.map +1 -0
- package/dist/presentation/subscribers/on-job-terminated.d.ts +8 -0
- package/dist/presentation/subscribers/on-job-terminated.d.ts.map +1 -0
- package/dist/presentation/subscribers/on-job-terminated.js +32 -0
- package/dist/presentation/subscribers/on-job-terminated.js.map +1 -0
- package/dist/presentation/subscribers/on-log-stream-closed.d.ts +9 -0
- package/dist/presentation/subscribers/on-log-stream-closed.d.ts.map +1 -0
- package/dist/presentation/subscribers/on-log-stream-closed.js +31 -0
- package/dist/presentation/subscribers/on-log-stream-closed.js.map +1 -0
- package/dist/presentation/subscribers/on-step-attempt-terminated.d.ts +3 -0
- package/dist/presentation/subscribers/on-step-attempt-terminated.d.ts.map +1 -0
- package/dist/presentation/subscribers/on-step-attempt-terminated.js +14 -0
- package/dist/presentation/subscribers/on-step-attempt-terminated.js.map +1 -0
- package/dist/temporal/activities/close-abandoned-streams.d.ts +12 -0
- package/dist/temporal/activities/close-abandoned-streams.d.ts.map +1 -0
- package/dist/temporal/activities/close-abandoned-streams.js +33 -0
- package/dist/temporal/activities/close-abandoned-streams.js.map +1 -0
- package/dist/temporal/activities/compact-stream.d.ts +33 -0
- package/dist/temporal/activities/compact-stream.d.ts.map +1 -0
- package/dist/temporal/activities/compact-stream.js +95 -0
- package/dist/temporal/activities/compact-stream.js.map +1 -0
- package/dist/temporal/activities/compaction-reconcile.d.ts +16 -0
- package/dist/temporal/activities/compaction-reconcile.d.ts.map +1 -0
- package/dist/temporal/activities/compaction-reconcile.js +52 -0
- package/dist/temporal/activities/compaction-reconcile.js.map +1 -0
- package/dist/temporal/activities/index.d.ts +14 -0
- package/dist/temporal/activities/index.d.ts.map +1 -0
- package/dist/temporal/activities/index.js +16 -0
- package/dist/temporal/activities/index.js.map +1 -0
- package/dist/temporal/activities/reap-stale-open-streams.d.ts +4 -0
- package/dist/temporal/activities/reap-stale-open-streams.d.ts.map +1 -0
- package/dist/temporal/activities/reap-stale-open-streams.js +11 -0
- package/dist/temporal/activities/reap-stale-open-streams.js.map +1 -0
- package/dist/temporal/activities/retention-sweep.d.ts +7 -0
- package/dist/temporal/activities/retention-sweep.d.ts.map +1 -0
- package/dist/temporal/activities/retention-sweep.js +19 -0
- package/dist/temporal/activities/retention-sweep.js.map +1 -0
- package/dist/temporal/constants.d.ts +7 -0
- package/dist/temporal/constants.d.ts.map +1 -0
- package/dist/temporal/constants.js +13 -0
- package/dist/temporal/constants.js.map +1 -0
- package/dist/temporal/workflows/close-abandoned-streams.d.ts +11 -0
- package/dist/temporal/workflows/close-abandoned-streams.d.ts.map +1 -0
- package/dist/temporal/workflows/close-abandoned-streams.js +22 -0
- package/dist/temporal/workflows/close-abandoned-streams.js.map +1 -0
- package/dist/temporal/workflows/compact-stream.d.ts +6 -0
- package/dist/temporal/workflows/compact-stream.d.ts.map +1 -0
- package/dist/temporal/workflows/compact-stream.js +25 -0
- package/dist/temporal/workflows/compact-stream.js.map +1 -0
- package/dist/temporal/workflows/compaction-reconcile-cron.d.ts +3 -0
- package/dist/temporal/workflows/compaction-reconcile-cron.d.ts.map +1 -0
- package/dist/temporal/workflows/compaction-reconcile-cron.js +15 -0
- package/dist/temporal/workflows/compaction-reconcile-cron.js.map +1 -0
- package/dist/temporal/workflows/index.d.ts +6 -0
- package/dist/temporal/workflows/index.d.ts.map +1 -0
- package/dist/temporal/workflows/index.js +7 -0
- package/dist/temporal/workflows/index.js.map +1 -0
- package/dist/temporal/workflows/reap-stale-open-streams-cron.d.ts +2 -0
- package/dist/temporal/workflows/reap-stale-open-streams-cron.d.ts.map +1 -0
- package/dist/temporal/workflows/reap-stale-open-streams-cron.js +15 -0
- package/dist/temporal/workflows/reap-stale-open-streams-cron.js.map +1 -0
- package/dist/temporal/workflows/retention-sweep-cron.d.ts +2 -0
- package/dist/temporal/workflows/retention-sweep-cron.d.ts.map +1 -0
- package/dist/temporal/workflows/retention-sweep-cron.js +23 -0
- package/dist/temporal/workflows/retention-sweep-cron.js.map +1 -0
- package/dist/tsconfig.test.tsbuildinfo +1 -0
- package/drizzle/0000_initial.sql +65 -0
- package/drizzle/meta/0000_snapshot.json +529 -0
- package/drizzle/meta/_journal.json +13 -0
- package/drizzle.config.ts +7 -0
- package/package.json +84 -0
- package/src/api/object-storage-credentials.test.ts +70 -0
- package/src/api/object-storage.test.ts +64 -0
- package/src/api/object-storage.ts +226 -0
- package/src/config.test.ts +118 -0
- package/src/config.ts +130 -0
- package/src/core/append-logs.test.ts +563 -0
- package/src/core/append-logs.ts +384 -0
- package/src/core/budget.test.ts +30 -0
- package/src/core/budget.ts +19 -0
- package/src/core/close-stream.ts +72 -0
- package/src/core/compaction.ts +68 -0
- package/src/core/entities/attempt-stream.ts +38 -0
- package/src/core/entities/job-accounting.ts +11 -0
- package/src/core/entities/log-object.test.ts +25 -0
- package/src/core/entities/log-object.ts +18 -0
- package/src/core/errors.ts +37 -0
- package/src/core/finalize-attempt-stream.test.ts +90 -0
- package/src/core/finalize-attempt-stream.ts +59 -0
- package/src/core/read-logs.ts +77 -0
- package/src/core/reap-stale-open-streams.test.ts +137 -0
- package/src/core/reap-stale-open-streams.ts +60 -0
- package/src/core/retention.test.ts +388 -0
- package/src/core/retention.ts +184 -0
- package/src/core/session/claude/rows.ts +223 -0
- package/src/core/session/claude-parser.test.ts +199 -0
- package/src/core/session/claude-parser.ts +38 -0
- package/src/core/session/entry-schema.ts +82 -0
- package/src/core/session/object.ts +57 -0
- package/src/core/session/parse-session.ts +23 -0
- package/src/core/session/pi/message-rows.ts +211 -0
- package/src/core/session/pi/metadata.ts +148 -0
- package/src/core/session/pi/text.ts +83 -0
- package/src/core/session/pi-parser.test.ts +451 -0
- package/src/core/session/pi-parser.ts +107 -0
- package/src/core/session/rows.ts +38 -0
- package/src/core/session/session-record.ts +4 -0
- package/src/db/accounting.ts +81 -0
- package/src/db/chunks.ts +143 -0
- package/src/db/db.ts +22 -0
- package/src/db/index.ts +4 -0
- package/src/db/schema/attempt-streams.ts +106 -0
- package/src/db/schema/chunks.ts +43 -0
- package/src/db/schema/common.ts +12 -0
- package/src/db/schema/job-accounting.ts +38 -0
- package/src/db/schema/outbox.ts +4 -0
- package/src/db/streams.test.ts +47 -0
- package/src/db/streams.ts +368 -0
- package/src/index.ts +77 -0
- package/src/metrics/index.ts +1 -0
- package/src/metrics/instance.ts +33 -0
- package/src/metrics/service.ts +23 -0
- package/src/presentation/dto/read-logs.ts +24 -0
- package/src/presentation/routes/append-logs.test.ts +279 -0
- package/src/presentation/routes/append-logs.ts +66 -0
- package/src/presentation/routes/index.ts +27 -0
- package/src/presentation/routes/read-logs.test.ts +504 -0
- package/src/presentation/routes/read-logs.ts +38 -0
- package/src/presentation/subscribers/on-job-terminated.test.ts +64 -0
- package/src/presentation/subscribers/on-job-terminated.ts +26 -0
- package/src/presentation/subscribers/on-log-stream-closed.test.ts +63 -0
- package/src/presentation/subscribers/on-log-stream-closed.ts +26 -0
- package/src/presentation/subscribers/on-step-attempt-terminated.ts +16 -0
- package/src/temporal/activities/close-abandoned-streams.test.ts +84 -0
- package/src/temporal/activities/close-abandoned-streams.ts +30 -0
- package/src/temporal/activities/compact-stream.test.ts +221 -0
- package/src/temporal/activities/compact-stream.ts +123 -0
- package/src/temporal/activities/compaction-reconcile.test.ts +143 -0
- package/src/temporal/activities/compaction-reconcile.ts +48 -0
- package/src/temporal/activities/index.ts +15 -0
- package/src/temporal/activities/reap-stale-open-streams.test.ts +48 -0
- package/src/temporal/activities/reap-stale-open-streams.ts +14 -0
- package/src/temporal/activities/retention-sweep.test.ts +38 -0
- package/src/temporal/activities/retention-sweep.ts +23 -0
- package/src/temporal/constants.ts +14 -0
- package/src/temporal/workflows/close-abandoned-streams.ts +25 -0
- package/src/temporal/workflows/compact-stream.ts +26 -0
- package/src/temporal/workflows/compaction-reconcile-cron.ts +14 -0
- package/src/temporal/workflows/index.ts +5 -0
- package/src/temporal/workflows/reap-stale-open-streams-cron.ts +13 -0
- package/src/temporal/workflows/retention-sweep-cron.ts +23 -0
- package/test/env.ts +37 -0
- package/test/factories/job-accounting.ts +38 -0
- package/test/fixtures/closed-stream.ts +53 -0
- package/test/fixtures/lease-token.ts +42 -0
- package/test/fixtures/ndjson.ts +34 -0
- package/test/globalSetup.ts +44 -0
- package/test/index.ts +9 -0
- package/test/queries.ts +54 -0
- package/test/setup.ts +17 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +3 -0
- package/tsconfig.test.json +8 -0
- package/vitest.config.ts +13 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
SessionViewLifecycleRow,
|
|
3
|
+
SessionViewRow,
|
|
4
|
+
SessionViewRowMeta,
|
|
5
|
+
SessionViewToolCallRow,
|
|
6
|
+
SessionViewToolResultRow,
|
|
7
|
+
} from '@shipfox/api-logs-dto';
|
|
8
|
+
import {asLooseObject} from '../entry-schema.js';
|
|
9
|
+
import {
|
|
10
|
+
booleanField,
|
|
11
|
+
field,
|
|
12
|
+
formatNumber,
|
|
13
|
+
isMeta,
|
|
14
|
+
metaItem,
|
|
15
|
+
stringField,
|
|
16
|
+
stringifyValue,
|
|
17
|
+
toJson,
|
|
18
|
+
} from '../object.js';
|
|
19
|
+
import {lifecycleRow, messageRow, thinkingRow} from '../rows.js';
|
|
20
|
+
|
|
21
|
+
export function systemRow(
|
|
22
|
+
timestamp: number,
|
|
23
|
+
message: Record<string, unknown>,
|
|
24
|
+
): SessionViewLifecycleRow {
|
|
25
|
+
const subtype = stringField(message, 'subtype');
|
|
26
|
+
const label = subtype === 'init' || message.type === 'init' ? 'Session started' : 'Session event';
|
|
27
|
+
const detail = stringField(message, 'session_id') ?? stringField(message, 'sessionId') ?? null;
|
|
28
|
+
const meta = [
|
|
29
|
+
metaItem('cwd', stringField(message, 'cwd'), false),
|
|
30
|
+
metaItem('model', stringField(message, 'model')),
|
|
31
|
+
metaItem(
|
|
32
|
+
'permission',
|
|
33
|
+
stringField(message, 'permissionMode') ?? stringField(message, 'permission_mode'),
|
|
34
|
+
),
|
|
35
|
+
].filter(isMeta);
|
|
36
|
+
|
|
37
|
+
return lifecycleRow(timestamp, label, detail, 'default', false, meta);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function assistantRows(
|
|
41
|
+
timestamp: number,
|
|
42
|
+
message: Record<string, unknown>,
|
|
43
|
+
): readonly SessionViewRow[] {
|
|
44
|
+
const sdkMessage = asLooseObject(message.message) ?? message;
|
|
45
|
+
const rows: SessionViewRow[] = [];
|
|
46
|
+
const textParts: string[] = [];
|
|
47
|
+
const thinkingParts: string[] = [];
|
|
48
|
+
|
|
49
|
+
const pushText = () => {
|
|
50
|
+
if (textParts.length === 0) return;
|
|
51
|
+
rows.push(messageRow(timestamp, 'assistant', 'assistant', textParts.join('\n\n'), false));
|
|
52
|
+
textParts.length = 0;
|
|
53
|
+
};
|
|
54
|
+
const pushThinking = () => {
|
|
55
|
+
if (thinkingParts.length === 0) return;
|
|
56
|
+
rows.push(thinkingRow(timestamp, thinkingParts.join('\n\n')));
|
|
57
|
+
thinkingParts.length = 0;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
for (const block of contentBlocks(sdkMessage)) {
|
|
61
|
+
const type = stringField(block, 'type');
|
|
62
|
+
if (type === 'tool_use' || type === 'tool-use' || type === 'toolCall') {
|
|
63
|
+
pushText();
|
|
64
|
+
pushThinking();
|
|
65
|
+
rows.push(toolCallRow(timestamp, block));
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (type === 'thinking' || type === 'reasoning') {
|
|
70
|
+
pushText();
|
|
71
|
+
const text = blockText(block);
|
|
72
|
+
if (text) thinkingParts.push(text);
|
|
73
|
+
continue;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
pushThinking();
|
|
77
|
+
const text = blockText(block);
|
|
78
|
+
if (text) textParts.push(text);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
pushText();
|
|
82
|
+
pushThinking();
|
|
83
|
+
|
|
84
|
+
if (rows.length > 0) return rows;
|
|
85
|
+
|
|
86
|
+
const text = stringField(sdkMessage, 'content') ?? stringField(message, 'result');
|
|
87
|
+
return [messageRow(timestamp, 'assistant', 'assistant', text ?? toJson(message), false)];
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function userRows(
|
|
91
|
+
timestamp: number,
|
|
92
|
+
message: Record<string, unknown>,
|
|
93
|
+
): readonly SessionViewRow[] {
|
|
94
|
+
const sdkMessage = asLooseObject(message.message) ?? message;
|
|
95
|
+
const rows: SessionViewRow[] = [];
|
|
96
|
+
const textParts: string[] = [];
|
|
97
|
+
const pushText = () => {
|
|
98
|
+
if (textParts.length === 0) return;
|
|
99
|
+
rows.push(messageRow(timestamp, 'user', 'user', textParts.join('\n\n'), false));
|
|
100
|
+
textParts.length = 0;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
for (const block of contentBlocks(sdkMessage)) {
|
|
104
|
+
const type = stringField(block, 'type');
|
|
105
|
+
if (type === 'tool_result' || type === 'tool-result') {
|
|
106
|
+
pushText();
|
|
107
|
+
rows.push(toolResultRow(timestamp, block));
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const text = blockText(block);
|
|
112
|
+
if (text) textParts.push(text);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
pushText();
|
|
116
|
+
|
|
117
|
+
if (rows.length > 0) return rows;
|
|
118
|
+
|
|
119
|
+
const content = stringField(sdkMessage, 'content');
|
|
120
|
+
return [messageRow(timestamp, 'user', 'user', content ?? toJson(message), false)];
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
export function resultRow(
|
|
124
|
+
timestamp: number,
|
|
125
|
+
message: Record<string, unknown>,
|
|
126
|
+
): SessionViewLifecycleRow {
|
|
127
|
+
const isError = booleanField(message, 'is_error') || booleanField(message, 'isError');
|
|
128
|
+
const subtype = stringField(message, 'subtype');
|
|
129
|
+
const terminalFailure = isError || subtype === 'error';
|
|
130
|
+
const detail =
|
|
131
|
+
stringField(message, 'result') ??
|
|
132
|
+
stringField(message, 'error') ??
|
|
133
|
+
stringField(message, 'message') ??
|
|
134
|
+
null;
|
|
135
|
+
const meta = [
|
|
136
|
+
numberMeta(message, 'duration_ms', 'duration', 'ms'),
|
|
137
|
+
numberMeta(message, 'duration_api_ms', 'api duration', 'ms'),
|
|
138
|
+
numberMeta(message, 'num_turns', 'turns'),
|
|
139
|
+
costMeta(message),
|
|
140
|
+
].filter(isMeta);
|
|
141
|
+
|
|
142
|
+
return lifecycleRow(
|
|
143
|
+
timestamp,
|
|
144
|
+
terminalFailure ? 'Session failed' : 'Session completed',
|
|
145
|
+
detail,
|
|
146
|
+
terminalFailure ? 'error' : 'default',
|
|
147
|
+
terminalFailure,
|
|
148
|
+
meta,
|
|
149
|
+
);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function contentBlocks(message: Record<string, unknown>): Record<string, unknown>[] {
|
|
153
|
+
const content = message.content;
|
|
154
|
+
if (!Array.isArray(content)) return [];
|
|
155
|
+
|
|
156
|
+
return content.flatMap((block) => {
|
|
157
|
+
const object = asLooseObject(block);
|
|
158
|
+
return object ? [object] : [];
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function toolCallRow(timestamp: number, block: Record<string, unknown>): SessionViewToolCallRow {
|
|
163
|
+
return {
|
|
164
|
+
kind: 'tool-call',
|
|
165
|
+
timestamp,
|
|
166
|
+
id: stringField(block, 'id') ?? null,
|
|
167
|
+
name: stringField(block, 'name') ?? 'tool',
|
|
168
|
+
input: stringifyValue(field(block, 'input') ?? {}),
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function toolResultRow(
|
|
173
|
+
timestamp: number,
|
|
174
|
+
block: Record<string, unknown>,
|
|
175
|
+
): SessionViewToolResultRow {
|
|
176
|
+
return {
|
|
177
|
+
kind: 'tool-result',
|
|
178
|
+
timestamp,
|
|
179
|
+
toolCallId: stringField(block, 'tool_use_id') ?? stringField(block, 'toolUseId') ?? null,
|
|
180
|
+
toolName: stringField(block, 'name') ?? 'tool',
|
|
181
|
+
output: blockText(block) || stringifyValue(field(block, 'content') ?? ''),
|
|
182
|
+
isError: booleanField(block, 'is_error') || booleanField(block, 'isError'),
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function blockText(block: Record<string, unknown>): string {
|
|
187
|
+
const content = field(block, 'content');
|
|
188
|
+
if (Array.isArray(content)) {
|
|
189
|
+
return content
|
|
190
|
+
.map((item) => {
|
|
191
|
+
const object = asLooseObject(item);
|
|
192
|
+
return object ? blockText(object) : typeof item === 'string' ? item : '';
|
|
193
|
+
})
|
|
194
|
+
.filter(Boolean)
|
|
195
|
+
.join('\n\n');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
return (
|
|
199
|
+
stringField(block, 'text') ??
|
|
200
|
+
stringField(block, 'thinking') ??
|
|
201
|
+
(typeof content === 'string' ? content : '') ??
|
|
202
|
+
''
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function numberMeta(
|
|
207
|
+
value: unknown,
|
|
208
|
+
key: string,
|
|
209
|
+
label: string,
|
|
210
|
+
unit?: string,
|
|
211
|
+
): SessionViewRowMeta | null {
|
|
212
|
+
const raw = field(value, key);
|
|
213
|
+
if (typeof raw !== 'number' || !Number.isFinite(raw)) return null;
|
|
214
|
+
|
|
215
|
+
return metaItem(label, unit ? `${formatNumber(raw)} ${unit}` : formatNumber(raw));
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function costMeta(message: Record<string, unknown>): SessionViewRowMeta | null {
|
|
219
|
+
const value = field(message, 'total_cost_usd') ?? field(message, 'totalCostUsd');
|
|
220
|
+
if (typeof value !== 'number' || !Number.isFinite(value)) return null;
|
|
221
|
+
|
|
222
|
+
return metaItem('cost', `$${value.toFixed(value < 0.01 ? 4 : 2)}`);
|
|
223
|
+
}
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import {parseClaudeSessionRecord} from './claude-parser.js';
|
|
2
|
+
|
|
3
|
+
const record = (data: unknown, ts = 1) => ({
|
|
4
|
+
ts,
|
|
5
|
+
data: typeof data === 'string' ? data : JSON.stringify(data),
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe('parseClaudeSessionRecord', () => {
|
|
9
|
+
it('returns a raw row for malformed JSON', () => {
|
|
10
|
+
const rows = parseClaudeSessionRecord(record('{not json'));
|
|
11
|
+
|
|
12
|
+
expect(rows).toEqual([
|
|
13
|
+
{kind: 'raw', timestamp: 1, label: 'Malformed session entry', raw: '{not json'},
|
|
14
|
+
]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('returns a raw row for an unknown message type', () => {
|
|
18
|
+
const rows = parseClaudeSessionRecord(record({type: 'future_event', value: 1}));
|
|
19
|
+
|
|
20
|
+
expect(rows).toEqual([
|
|
21
|
+
{
|
|
22
|
+
kind: 'raw',
|
|
23
|
+
timestamp: 1,
|
|
24
|
+
label: 'Unknown Claude message: future_event',
|
|
25
|
+
raw: '{"type":"future_event","value":1}',
|
|
26
|
+
},
|
|
27
|
+
]);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it('maps the init message to a lifecycle row', () => {
|
|
31
|
+
const rows = parseClaudeSessionRecord(
|
|
32
|
+
record({
|
|
33
|
+
type: 'system',
|
|
34
|
+
subtype: 'init',
|
|
35
|
+
session_id: 'session-1',
|
|
36
|
+
cwd: '/workspace',
|
|
37
|
+
model: 'claude-opus-4-8',
|
|
38
|
+
}),
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
expect(rows).toEqual([
|
|
42
|
+
{
|
|
43
|
+
kind: 'lifecycle',
|
|
44
|
+
timestamp: 1,
|
|
45
|
+
label: 'Session started',
|
|
46
|
+
detail: 'session-1',
|
|
47
|
+
meta: [
|
|
48
|
+
{label: 'cwd', value: '/workspace', inline: false},
|
|
49
|
+
{label: 'model', value: 'claude-opus-4-8'},
|
|
50
|
+
],
|
|
51
|
+
tone: 'default',
|
|
52
|
+
terminalFailure: false,
|
|
53
|
+
},
|
|
54
|
+
]);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('expands assistant text, thinking, and tool-use blocks in order', () => {
|
|
58
|
+
const rows = parseClaudeSessionRecord(
|
|
59
|
+
record({
|
|
60
|
+
type: 'assistant',
|
|
61
|
+
message: {
|
|
62
|
+
role: 'assistant',
|
|
63
|
+
content: [
|
|
64
|
+
{type: 'text', text: 'I will inspect the repo.'},
|
|
65
|
+
{type: 'thinking', thinking: 'Need the failing file first.'},
|
|
66
|
+
{type: 'tool_use', id: 'tool-1', name: 'Read', input: {file_path: 'src/a.ts'}},
|
|
67
|
+
],
|
|
68
|
+
},
|
|
69
|
+
}),
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
expect(rows).toEqual([
|
|
73
|
+
{
|
|
74
|
+
kind: 'message',
|
|
75
|
+
timestamp: 1,
|
|
76
|
+
role: 'assistant',
|
|
77
|
+
label: 'assistant',
|
|
78
|
+
meta: [],
|
|
79
|
+
text: 'I will inspect the repo.',
|
|
80
|
+
terminalFailure: false,
|
|
81
|
+
},
|
|
82
|
+
{kind: 'thinking', timestamp: 1, text: 'Need the failing file first.'},
|
|
83
|
+
{
|
|
84
|
+
kind: 'tool-call',
|
|
85
|
+
timestamp: 1,
|
|
86
|
+
id: 'tool-1',
|
|
87
|
+
name: 'Read',
|
|
88
|
+
input: '{\n "file_path": "src/a.ts"\n}',
|
|
89
|
+
},
|
|
90
|
+
]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('maps user tool results to tool-result rows', () => {
|
|
94
|
+
const rows = parseClaudeSessionRecord(
|
|
95
|
+
record({
|
|
96
|
+
type: 'user',
|
|
97
|
+
message: {
|
|
98
|
+
role: 'user',
|
|
99
|
+
content: [
|
|
100
|
+
{
|
|
101
|
+
type: 'tool_result',
|
|
102
|
+
tool_use_id: 'tool-1',
|
|
103
|
+
content: [{type: 'text', text: 'file contents'}],
|
|
104
|
+
is_error: true,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
},
|
|
108
|
+
}),
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
expect(rows).toEqual([
|
|
112
|
+
{
|
|
113
|
+
kind: 'tool-result',
|
|
114
|
+
timestamp: 1,
|
|
115
|
+
toolCallId: 'tool-1',
|
|
116
|
+
toolName: 'tool',
|
|
117
|
+
output: 'file contents',
|
|
118
|
+
isError: true,
|
|
119
|
+
},
|
|
120
|
+
]);
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it('preserves mixed user text and tool-result block order', () => {
|
|
124
|
+
const rows = parseClaudeSessionRecord(
|
|
125
|
+
record({
|
|
126
|
+
type: 'user',
|
|
127
|
+
message: {
|
|
128
|
+
role: 'user',
|
|
129
|
+
content: [
|
|
130
|
+
{type: 'text', text: 'before'},
|
|
131
|
+
{
|
|
132
|
+
type: 'tool_result',
|
|
133
|
+
tool_use_id: 'tool-1',
|
|
134
|
+
content: [{type: 'text', text: 'tool output'}],
|
|
135
|
+
},
|
|
136
|
+
{type: 'text', text: 'after'},
|
|
137
|
+
],
|
|
138
|
+
},
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
|
|
142
|
+
expect(rows).toEqual([
|
|
143
|
+
{
|
|
144
|
+
kind: 'message',
|
|
145
|
+
timestamp: 1,
|
|
146
|
+
role: 'user',
|
|
147
|
+
label: 'user',
|
|
148
|
+
meta: [],
|
|
149
|
+
text: 'before',
|
|
150
|
+
terminalFailure: false,
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
kind: 'tool-result',
|
|
154
|
+
timestamp: 1,
|
|
155
|
+
toolCallId: 'tool-1',
|
|
156
|
+
toolName: 'tool',
|
|
157
|
+
output: 'tool output',
|
|
158
|
+
isError: false,
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
kind: 'message',
|
|
162
|
+
timestamp: 1,
|
|
163
|
+
role: 'user',
|
|
164
|
+
label: 'user',
|
|
165
|
+
meta: [],
|
|
166
|
+
text: 'after',
|
|
167
|
+
terminalFailure: false,
|
|
168
|
+
},
|
|
169
|
+
]);
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('maps result messages to terminal lifecycle rows', () => {
|
|
173
|
+
const rows = parseClaudeSessionRecord(
|
|
174
|
+
record({
|
|
175
|
+
type: 'result',
|
|
176
|
+
subtype: 'error',
|
|
177
|
+
is_error: true,
|
|
178
|
+
result: 'Timed out',
|
|
179
|
+
duration_ms: 1200,
|
|
180
|
+
total_cost_usd: 0.0042,
|
|
181
|
+
}),
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
expect(rows).toEqual([
|
|
185
|
+
{
|
|
186
|
+
kind: 'lifecycle',
|
|
187
|
+
timestamp: 1,
|
|
188
|
+
label: 'Session failed',
|
|
189
|
+
detail: 'Timed out',
|
|
190
|
+
meta: [
|
|
191
|
+
{label: 'duration', value: '1,200 ms'},
|
|
192
|
+
{label: 'cost', value: '$0.0042'},
|
|
193
|
+
],
|
|
194
|
+
tone: 'error',
|
|
195
|
+
terminalFailure: true,
|
|
196
|
+
},
|
|
197
|
+
]);
|
|
198
|
+
});
|
|
199
|
+
});
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type {SessionViewRow} from '@shipfox/api-logs-dto';
|
|
2
|
+
import {z} from 'zod';
|
|
3
|
+
import {assistantRows, resultRow, systemRow, userRows} from './claude/rows.js';
|
|
4
|
+
import {rawRecordRow} from './rows.js';
|
|
5
|
+
import type {AgentSessionRecord} from './session-record.js';
|
|
6
|
+
|
|
7
|
+
const claudeMessageSchema = z
|
|
8
|
+
.object({
|
|
9
|
+
type: z.string().min(1),
|
|
10
|
+
})
|
|
11
|
+
.catchall(z.unknown());
|
|
12
|
+
|
|
13
|
+
export function parseClaudeSessionRecord(record: AgentSessionRecord): readonly SessionViewRow[] {
|
|
14
|
+
let json: unknown;
|
|
15
|
+
try {
|
|
16
|
+
json = JSON.parse(record.data);
|
|
17
|
+
} catch {
|
|
18
|
+
return [rawRecordRow(record, 'Malformed session entry')];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const parsed = claudeMessageSchema.safeParse(json);
|
|
22
|
+
if (!parsed.success) return [rawRecordRow(record, 'Unsupported Claude message')];
|
|
23
|
+
|
|
24
|
+
const message = parsed.data;
|
|
25
|
+
switch (message.type) {
|
|
26
|
+
case 'system':
|
|
27
|
+
case 'init':
|
|
28
|
+
return [systemRow(record.ts, message)];
|
|
29
|
+
case 'assistant':
|
|
30
|
+
return assistantRows(record.ts, message);
|
|
31
|
+
case 'user':
|
|
32
|
+
return userRows(record.ts, message);
|
|
33
|
+
case 'result':
|
|
34
|
+
return [resultRow(record.ts, message)];
|
|
35
|
+
default:
|
|
36
|
+
return [rawRecordRow(record, `Unknown Claude message: ${message.type}`)];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {z} from 'zod';
|
|
2
|
+
|
|
3
|
+
const looseObjectSchema = z.object({}).catchall(z.unknown());
|
|
4
|
+
|
|
5
|
+
export const sessionEntrySchema = z
|
|
6
|
+
.object({
|
|
7
|
+
type: z.string().min(1),
|
|
8
|
+
})
|
|
9
|
+
.catchall(z.unknown());
|
|
10
|
+
|
|
11
|
+
export const agentMessageSchema = z
|
|
12
|
+
.object({
|
|
13
|
+
type: z.string().optional(),
|
|
14
|
+
role: z.string().optional(),
|
|
15
|
+
content: z.unknown().optional(),
|
|
16
|
+
model: z.string().optional(),
|
|
17
|
+
provider: z.string().optional(),
|
|
18
|
+
stopReason: z.string().optional(),
|
|
19
|
+
errorMessage: z.string().optional(),
|
|
20
|
+
toolCallId: z.string().optional(),
|
|
21
|
+
toolName: z.string().optional(),
|
|
22
|
+
isError: z.boolean().optional(),
|
|
23
|
+
})
|
|
24
|
+
.catchall(z.unknown());
|
|
25
|
+
|
|
26
|
+
export const sessionMessageEntrySchema = z
|
|
27
|
+
.object({
|
|
28
|
+
type: z.literal('message'),
|
|
29
|
+
message: agentMessageSchema,
|
|
30
|
+
})
|
|
31
|
+
.catchall(z.unknown());
|
|
32
|
+
|
|
33
|
+
export const contentBlockSchema = z
|
|
34
|
+
.object({
|
|
35
|
+
type: z.string().optional(),
|
|
36
|
+
})
|
|
37
|
+
.catchall(z.unknown());
|
|
38
|
+
|
|
39
|
+
export type SessionEntry = z.infer<typeof sessionEntrySchema>;
|
|
40
|
+
export type AgentMessage = z.infer<typeof agentMessageSchema>;
|
|
41
|
+
export type ContentBlock = z.infer<typeof contentBlockSchema>;
|
|
42
|
+
|
|
43
|
+
export interface ParsedSessionEntry {
|
|
44
|
+
ok: true;
|
|
45
|
+
entry: SessionEntry;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export interface MalformedSessionEntry {
|
|
49
|
+
ok: false;
|
|
50
|
+
raw: string;
|
|
51
|
+
reason: 'invalid-json' | 'invalid-entry';
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export type SessionEntryParseResult = ParsedSessionEntry | MalformedSessionEntry;
|
|
55
|
+
|
|
56
|
+
export function parseSessionEntry(data: string): SessionEntryParseResult {
|
|
57
|
+
let json: unknown;
|
|
58
|
+
try {
|
|
59
|
+
json = JSON.parse(data);
|
|
60
|
+
} catch {
|
|
61
|
+
return {ok: false, raw: data, reason: 'invalid-json'};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const parsed = sessionEntrySchema.safeParse(json);
|
|
65
|
+
if (!parsed.success) return {ok: false, raw: data, reason: 'invalid-entry'};
|
|
66
|
+
|
|
67
|
+
return {ok: true, entry: parsed.data};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export function asLooseObject(value: unknown): Record<string, unknown> | null {
|
|
71
|
+
const parsed = looseObjectSchema.safeParse(value);
|
|
72
|
+
return parsed.success ? parsed.data : null;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function asContentBlocks(value: unknown): ContentBlock[] {
|
|
76
|
+
if (!Array.isArray(value)) return [];
|
|
77
|
+
|
|
78
|
+
return value.flatMap((item) => {
|
|
79
|
+
const parsed = contentBlockSchema.safeParse(item);
|
|
80
|
+
return parsed.success ? [parsed.data] : [];
|
|
81
|
+
});
|
|
82
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type {SessionViewRowMeta} from '@shipfox/api-logs-dto';
|
|
2
|
+
import {asLooseObject} from './entry-schema.js';
|
|
3
|
+
|
|
4
|
+
const SESSION_META_NUMBER_LOCALE = 'en-US';
|
|
5
|
+
|
|
6
|
+
export function field(value: unknown, key: string): unknown {
|
|
7
|
+
const object = asLooseObject(value);
|
|
8
|
+
return object?.[key];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function stringField(value: unknown, key: string): string | undefined {
|
|
12
|
+
const fieldValue = field(value, key);
|
|
13
|
+
return typeof fieldValue === 'string' && fieldValue.length > 0 ? fieldValue : undefined;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function numberField(value: unknown, key: string): number | undefined {
|
|
17
|
+
const fieldValue = field(value, key);
|
|
18
|
+
return typeof fieldValue === 'number' && Number.isFinite(fieldValue) ? fieldValue : undefined;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function booleanField(value: unknown, key: string): boolean {
|
|
22
|
+
return field(value, key) === true;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function stringifyValue(value: unknown): string {
|
|
26
|
+
if (typeof value === 'string') return value;
|
|
27
|
+
return toJson(value);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function toJson(value: unknown): string {
|
|
31
|
+
const json = JSON.stringify(value, null, 2);
|
|
32
|
+
return json === undefined ? String(value) : json;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function formatNumber(value: number): string {
|
|
36
|
+
return new Intl.NumberFormat(SESSION_META_NUMBER_LOCALE, {
|
|
37
|
+
maximumFractionDigits: 1,
|
|
38
|
+
notation: value >= 10000 ? 'compact' : 'standard',
|
|
39
|
+
}).format(value);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function formatCount(value: number, unit: string): string {
|
|
43
|
+
return `${formatNumber(value)} ${unit}${value === 1 ? '' : 's'}`;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function metaItem(
|
|
47
|
+
label: string,
|
|
48
|
+
value: string | null | undefined,
|
|
49
|
+
inline = true,
|
|
50
|
+
): SessionViewRowMeta | null {
|
|
51
|
+
if (value == null || value.length === 0) return null;
|
|
52
|
+
return inline ? {label, value} : {label, value, inline};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function isMeta(value: SessionViewRowMeta | null | undefined): value is SessionViewRowMeta {
|
|
56
|
+
return value != null;
|
|
57
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type {Harness} from '@shipfox/api-agent-dto';
|
|
2
|
+
import type {SessionViewRow} from '@shipfox/api-logs-dto';
|
|
3
|
+
import {parseClaudeSessionRecord} from './claude-parser.js';
|
|
4
|
+
import {parsePiSessionRecord} from './pi-parser.js';
|
|
5
|
+
import type {AgentSessionRecord} from './session-record.js';
|
|
6
|
+
|
|
7
|
+
export function parseSessionRecord(
|
|
8
|
+
record: AgentSessionRecord,
|
|
9
|
+
harness: Harness,
|
|
10
|
+
): readonly SessionViewRow[] {
|
|
11
|
+
try {
|
|
12
|
+
return harness === 'claude' ? parseClaudeSessionRecord(record) : parsePiSessionRecord(record);
|
|
13
|
+
} catch {
|
|
14
|
+
return [
|
|
15
|
+
{
|
|
16
|
+
kind: 'raw',
|
|
17
|
+
timestamp: record.ts,
|
|
18
|
+
label: 'Unsupported session entry',
|
|
19
|
+
raw: record.data,
|
|
20
|
+
},
|
|
21
|
+
];
|
|
22
|
+
}
|
|
23
|
+
}
|