samagotchi 0.169.0 → 0.171.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +44 -1
- data/docs/architecture.md +2 -2
- data/docs/cli.md +37 -23
- data/docs/configuration.md +1 -1
- data/docs/internals/block-renderers.md +2 -2
- data/docs/internals/bridge.md +9 -5
- data/docs/internals/context-telemetry.md +13 -1
- data/docs/internals/engine.md +4 -4
- data/docs/internals/inline-widgets.md +1 -1
- data/docs/internals/objectives.md +1 -1
- data/docs/internals/participants.md +235 -119
- data/docs/internals/web-frontend.md +173 -39
- data/docs/internals/working-set.md +71 -6
- data/docs/releasing.md +5 -2
- data/docs/sessions.md +6 -3
- data/docs/testing.md +10 -5
- data/lib/samagotchi/bridge/turn_accumulator.rb +8 -4
- data/lib/samagotchi/bridge.rb +53 -13
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/delivery.rb +13 -0
- data/lib/samagotchi/engine/events.rb +9 -0
- data/lib/samagotchi/engine.rb +6 -2
- data/lib/samagotchi/event_fanout.rb +25 -7
- data/lib/samagotchi/grants.rb +5 -0
- data/lib/samagotchi/guardrails/review_paths.rb +2 -2
- data/lib/samagotchi/image_store.rb +51 -12
- data/lib/samagotchi/kernel_loop.rb +7 -1
- data/lib/samagotchi/llm/stray_tool_call.rb +7 -4
- data/lib/samagotchi/llm_context_stale.rb +4 -1
- data/lib/samagotchi/participant_messages.rb +39 -0
- data/lib/samagotchi/participant_revise.rb +61 -0
- data/lib/samagotchi/participant_uploads.rb +98 -0
- data/lib/samagotchi/reply_wait/poll.rb +5 -1
- data/lib/samagotchi/revise_anchor.rb +9 -0
- data/lib/samagotchi/send_command.rb +5 -0
- data/lib/samagotchi/session_inbox.rb +20 -12
- data/lib/samagotchi/session_manager/delivery.rb +7 -0
- data/lib/samagotchi/session_stores.rb +2 -0
- data/lib/samagotchi/tool_runner.rb +39 -5
- data/lib/samagotchi/tools/memory.rb +26 -7
- data/lib/samagotchi/turn_anchors.rb +0 -3
- data/lib/samagotchi/turn_stages/verify_anchors.rb +13 -1
- data/lib/samagotchi/verify_anchor.rb +6 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +42 -62
- data/lib/samagotchi/web/participant_access.rb +129 -26
- data/lib/samagotchi/web/participant_images.rb +185 -0
- data/lib/samagotchi/web/participant_review.rb +19 -138
- data/lib/samagotchi/web/participant_show.rb +240 -0
- data/lib/samagotchi/web/participant_stream_filter.rb +25 -2
- data/lib/samagotchi/web/participant_view.rb +331 -24
- data/lib/samagotchi/web/public/annotate_bar.css +1 -2
- data/lib/samagotchi/web/public/annotate_bar.js +1 -2
- data/lib/samagotchi/web/public/app.js +125 -88
- data/lib/samagotchi/web/public/block_host.js +7 -4
- data/lib/samagotchi/web/public/bubbles.js +9 -1
- data/lib/samagotchi/web/public/cards_offscreen.js +69 -0
- data/lib/samagotchi/web/public/chip_popover.js +5 -4
- data/lib/samagotchi/web/public/claim_link.js +4 -4
- data/lib/samagotchi/web/public/composer.css +4 -6
- data/lib/samagotchi/web/public/composer_mode.js +11 -6
- data/lib/samagotchi/web/public/composer_mount.js +4 -4
- data/lib/samagotchi/web/public/ctx.js +12 -3
- data/lib/samagotchi/web/public/delivery_line.js +34 -5
- data/lib/samagotchi/web/public/history_shape.js +5 -1
- data/lib/samagotchi/web/public/images.js +7 -1
- data/lib/samagotchi/web/public/index.html +49 -4
- data/lib/samagotchi/web/public/info_bar_model.js +2 -1
- data/lib/samagotchi/web/public/live_timing.js +5 -3
- data/lib/samagotchi/web/public/markdown_place.js +5 -4
- data/lib/samagotchi/web/public/owner_claim.js +3 -3
- data/lib/samagotchi/web/public/owner_verify.js +0 -2
- data/lib/samagotchi/web/public/participant_bar.js +108 -0
- data/lib/samagotchi/web/public/participant_main.js +289 -0
- data/lib/samagotchi/web/public/participant_model.js +5 -292
- data/lib/samagotchi/web/public/participant_names.js +6 -1
- data/lib/samagotchi/web/public/participant_revise.js +66 -0
- data/lib/samagotchi/web/public/participants_panel.js +3 -12
- data/lib/samagotchi/web/public/question_cards.js +24 -4
- data/lib/samagotchi/web/public/refs.js +53 -36
- data/lib/samagotchi/web/public/reload_check.js +68 -11
- data/lib/samagotchi/web/public/revise_stack.js +21 -10
- data/lib/samagotchi/web/public/send_turn.js +24 -6
- data/lib/samagotchi/web/public/session_fields.js +1 -1
- data/lib/samagotchi/web/public/session_state.js +12 -4
- data/lib/samagotchi/web/public/session_sync.js +9 -2
- data/lib/samagotchi/web/public/session_view.js +17 -7
- data/lib/samagotchi/web/public/stage_asking.js +6 -4
- data/lib/samagotchi/web/public/stage_frame.js +3 -3
- data/lib/samagotchi/web/public/stage_model.js +12 -5
- data/lib/samagotchi/web/public/stage_opened.js +8 -5
- data/lib/samagotchi/web/public/stage_owner_wait.js +35 -0
- data/lib/samagotchi/web/public/stage_view.js +54 -17
- data/lib/samagotchi/web/public/stage_waiting.js +74 -6
- data/lib/samagotchi/web/public/storage.js +22 -0
- data/lib/samagotchi/web/public/stream_handlers.js +2 -1
- data/lib/samagotchi/web/public/tokens.css +1 -1
- data/lib/samagotchi/web/public/turn_block.js +3 -2
- data/lib/samagotchi/web/public/turn_events.js +15 -6
- data/lib/samagotchi/web/public/turn_html.js +9 -2
- data/lib/samagotchi/web/public/turn_range.js +1 -2
- data/lib/samagotchi/web/public/verify_target.js +2 -4
- data/lib/samagotchi/web/public/viewer.js +59 -0
- data/lib/samagotchi/web/public/viewer_self.js +53 -0
- data/lib/samagotchi/web/public/working_set.js +11 -5
- data/lib/samagotchi/web/public/working_set_view.js +53 -8
- data/lib/samagotchi/web/public/ws_cards.js +89 -0
- data/lib/samagotchi/web/public/ws_peek.js +106 -0
- data/lib/samagotchi/web/public/ws_preview.css +126 -0
- data/lib/samagotchi/web/public/ws_reader.js +319 -0
- data/lib/samagotchi/web/public/ws_timeline.js +200 -0
- data/lib/samagotchi/web/public/ws_way_in.js +25 -0
- data/lib/samagotchi/web/session_control.rb +42 -15
- data/lib/samagotchi/web/session_create.rb +1 -1
- data/lib/samagotchi/web/session_hub.rb +16 -0
- data/lib/samagotchi/web/session_show.rb +75 -28
- data/lib/samagotchi/web/viewer.rb +70 -0
- data/lib/samagotchi/web/working_set_routes.rb +132 -0
- data/lib/samagotchi/worker.rb +19 -14
- data/lib/samagotchi/working_set.rb +260 -8
- metadata +20 -6
- data/lib/samagotchi/web/public/participant.html +0 -218
- data/lib/samagotchi/web/public/participant.js +0 -719
- data/lib/samagotchi/web/public/participant_annotate.js +0 -76
- data/lib/samagotchi/web/public/review_history.js +0 -72
- data/lib/samagotchi/web/public/tools_overview.js +0 -42
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/* What chi has of a working-set item (ws_reader.js, ws_peek.js, ws_cards.js):
|
|
2
|
+
the popover's peek under a row and the reader over the stage. Colours are
|
|
3
|
+
tokens.css's. */
|
|
4
|
+
|
|
5
|
+
/* The popover's ▸: a row's peek toggle, before its glyph. */
|
|
6
|
+
.ws-peek-toggle{flex:none;width:16px;height:22px;margin:0 -4px 0 -2px;padding:0;border:0;border-radius:4px;background:transparent;color:var(--ws-faint);font-size:10px;line-height:22px;cursor:pointer}
|
|
7
|
+
.ws-peek-toggle:hover:not(:disabled){filter:none}@media(hover:hover){.ws-peek-toggle:hover{color:var(--accent);background:var(--accent-bg)}}
|
|
8
|
+
.ws-row.peeked{background:var(--bg-hover)}
|
|
9
|
+
.ws-row.peeked .ws-peek-toggle{color:var(--text-2)}
|
|
10
|
+
.ws-row.member .ws-peek-toggle{margin-left:-20px}
|
|
11
|
+
.ws-popover.sheet .ws-peek-toggle{width:28px;height:30px;margin-left:-8px}
|
|
12
|
+
|
|
13
|
+
/* The peek: the latest event, its footer. */
|
|
14
|
+
.ws-peek{flex:none;margin:4px 0 8px 6px;border:1px solid var(--border-strong);border-radius:8px;background:var(--bg-inset);overflow:hidden;font-family:ui-sans-serif,system-ui,sans-serif}
|
|
15
|
+
.ws-peek-h{display:flex;align-items:center;gap:8px;flex-wrap:wrap;padding:5px 10px;border-bottom:1px solid var(--border);font-size:12px;color:var(--ws-muted)}
|
|
16
|
+
.ws-peek-t{color:var(--text-2)}
|
|
17
|
+
.ws-peek-c{font-variant-numeric:tabular-nums}
|
|
18
|
+
.ws-peek-after{margin-left:auto;color:var(--ws-faint)}
|
|
19
|
+
.ws-peek-note{padding:8px 10px;font-size:12px;color:var(--ws-faint)}
|
|
20
|
+
.ws-peek-b .diff,.ws-peek-b .read-lines,.ws-peek-b .wsr-text{margin:0;border:0;border-radius:0;max-height:220px;font-size:11.5px}
|
|
21
|
+
.ws-peek-b .diff-note{padding:6px 10px}
|
|
22
|
+
.ws-peek-f{display:flex;align-items:center;gap:16px;padding:6px 10px;border-top:1px solid var(--border);font-size:12.5px}
|
|
23
|
+
.ws-peek-open,.wsr-call{padding:0;border:0;background:transparent;color:var(--accent);font:inherit;font-size:12.5px;cursor:pointer;text-align:left}
|
|
24
|
+
.ws-peek-open:hover:not(:disabled),.wsr-call:hover:not(:disabled){filter:none}@media(hover:hover){.ws-peek-open:hover,.wsr-call:hover{text-decoration:underline}}
|
|
25
|
+
.ws-popover.sheet .ws-peek-f{padding:0;gap:0}
|
|
26
|
+
.ws-popover.sheet .ws-peek-n{display:none}
|
|
27
|
+
.ws-popover.sheet .ws-peek-f>button{flex:1;height:44px;text-align:center}
|
|
28
|
+
.ws-popover.sheet .ws-peek-f>button+button{border-left:1px solid var(--border)}
|
|
29
|
+
|
|
30
|
+
/* A tool row's way in. */
|
|
31
|
+
.activity-ws{flex:none;padding:0 6px;border:1px solid var(--ws-act-border);border-radius:4px;background:transparent;font-size:11px;line-height:16px;color:var(--ws-muted);cursor:pointer;opacity:0}
|
|
32
|
+
.activity-row:hover .activity-ws,.activity-row:focus-within .activity-ws{opacity:1}
|
|
33
|
+
@media (hover:none){.activity-ws{opacity:1}}
|
|
34
|
+
.activity-ws:hover:not(:disabled){filter:none}@media(hover:hover){.activity-ws:hover{color:var(--accent);border-color:var(--accent);background:var(--accent-bg)}}
|
|
35
|
+
|
|
36
|
+
/* The reader: over the stage (#center: the history and the composer stay under it, inert). */
|
|
37
|
+
.ws-reader{position:absolute;inset:0;z-index:6;display:flex;background:var(--bg);border-radius:inherit;overflow:hidden}
|
|
38
|
+
.ws-reader[hidden]{display:none}
|
|
39
|
+
.wsr-main{flex:1;min-width:0;display:flex;flex-direction:column;border-right:1px solid var(--border)}
|
|
40
|
+
.wsr-head{flex:none;display:flex;align-items:center;gap:12px;padding:10px 20px;border-bottom:1px solid var(--border);background:var(--bg-panel)}
|
|
41
|
+
.wsr-back{flex:none;padding:4px 10px;border:1px solid var(--border-strong);border-radius:6px;background:transparent;color:var(--text-2);font-size:12px;cursor:pointer}
|
|
42
|
+
.wsr-title{min-width:0;display:flex;align-items:baseline;gap:12px}
|
|
43
|
+
.wsr-name{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:13px;color:var(--text)}
|
|
44
|
+
.wsr-meta{flex:none;font-size:12px;color:var(--ws-faint)}
|
|
45
|
+
.wsr-head-pill{flex:none;margin-left:auto;padding:1px 8px;border:1px solid var(--border-strong);border-radius:999px;font-size:11.5px;color:var(--ws-muted);white-space:nowrap}
|
|
46
|
+
.wsr-head-pill.warn{border-color:var(--warn-border);background:var(--warn-bg);color:var(--warn-text)}
|
|
47
|
+
.wsr-narrow{display:none}
|
|
48
|
+
.wsr-nav button{width:44px;height:44px;border:0;background:transparent;color:var(--text-2);font-size:18px;cursor:pointer}
|
|
49
|
+
.wsr-nav button:disabled{opacity:.35;cursor:default}
|
|
50
|
+
|
|
51
|
+
/* The file strip: the reads over the file's length. */
|
|
52
|
+
.wsr-strip{flex:none;display:flex;flex-direction:column;gap:6px;padding:10px 20px 8px;border-bottom:1px solid var(--border)}
|
|
53
|
+
.wsr-strip-k{display:flex;justify-content:space-between;gap:8px;font-size:11px;color:var(--ws-faint);font-variant-numeric:tabular-nums}
|
|
54
|
+
.wsr-bar{position:relative;height:10px;border-radius:3px;background:var(--bg-inset)}
|
|
55
|
+
.wsr-band{position:absolute;top:0;bottom:0;padding:0;border:0;border-radius:2px;cursor:pointer;background:color-mix(in srgb,var(--accent) 55%,transparent)}
|
|
56
|
+
.wsr-band.dropped,.wsr-band.forgotten{background:color-mix(in srgb,var(--text-faint) 75%,transparent)}
|
|
57
|
+
.wsr-band.latest.sent{background:var(--accent)}
|
|
58
|
+
.wsr-band.latest:not(.sent){background:var(--text-muted)}
|
|
59
|
+
.wsr-chips{display:flex;flex-wrap:wrap;gap:6px}
|
|
60
|
+
.wsr-chip{padding:2px 9px;border:0;border-radius:999px;background:var(--bg-inset);color:var(--text-2);font-size:11.5px;font-variant-numeric:tabular-nums;cursor:pointer}
|
|
61
|
+
.wsr-chip.dropped,.wsr-chip.forgotten{color:var(--text-faint);text-decoration:line-through}
|
|
62
|
+
|
|
63
|
+
/* The cards, newest first. */
|
|
64
|
+
.wsr-body{flex:1;min-height:0;overflow:auto;padding:14px 20px;display:flex;flex-direction:column;gap:14px;outline:none}
|
|
65
|
+
.wsr-note{color:var(--ws-faint);font-size:13px}
|
|
66
|
+
.wsr-card{flex:none;border:1px solid var(--border);border-radius:8px;background:var(--bg-panel);overflow:hidden}
|
|
67
|
+
.wsr-card.latest{border-color:var(--border-strong)}
|
|
68
|
+
.wsr-card.gone{opacity:.55}
|
|
69
|
+
.wsr-card-h{display:flex;align-items:center;gap:10px;flex-wrap:wrap;padding:6px 12px;border-bottom:1px solid var(--border);background:var(--bg-raised);font-size:12px}
|
|
70
|
+
.wsr-card-t{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;color:var(--text)}
|
|
71
|
+
.wsr-card-m{color:var(--ws-faint)}
|
|
72
|
+
.wsr-card-h .wsr-call{margin-left:auto;font-size:12px}
|
|
73
|
+
.wsr-pill{padding:0 6px;border-radius:4px;font-size:11px}
|
|
74
|
+
.wsr-pill.latest{background:var(--accent-bg);color:var(--accent)}
|
|
75
|
+
.wsr-pill.gone{background:var(--badge-bg);color:var(--text-muted)}
|
|
76
|
+
.wsr-card .read-lines,.wsr-card .diff,.wsr-card .wsr-text{margin:0;border:0;border-radius:0;max-height:none;background:transparent}
|
|
77
|
+
.wsr-lines{font-size:12.5px;line-height:19px}
|
|
78
|
+
.wsr-text{margin:0;padding:8px 12px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;line-height:17px;white-space:pre-wrap;word-break:break-word;color:var(--text-2)}
|
|
79
|
+
.wsr-gone{padding:8px 12px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px;color:var(--text-muted)}
|
|
80
|
+
.wsr-had{padding:0 12px 8px;font-size:12px;color:var(--ws-faint)}
|
|
81
|
+
.wsr-had>summary{cursor:pointer}
|
|
82
|
+
.wsr-overlay{margin:0 10px 10px;padding:6px 0 0;border:1px dashed var(--border-strong);border-radius:6px}
|
|
83
|
+
.wsr-overlay-h{padding:0 10px;font-size:11.5px;color:var(--ws-muted)}
|
|
84
|
+
.wsr-banner{flex:none;margin:10px 20px 0;padding:8px 10px;border:1px solid var(--warn-border);border-radius:8px;background:var(--warn-bg);color:var(--warn-text);font-size:12.5px;line-height:18px}
|
|
85
|
+
.wsr-tabs{flex:none;display:grid;grid-template-columns:repeat(2,minmax(0,1fr));margin:10px 20px 0;padding:2px;border-radius:8px;background:var(--bg-inset);max-width:420px}
|
|
86
|
+
.wsr-tabs button{height:32px;border:0;border-radius:6px;background:transparent;color:var(--text-muted);font-size:13px;cursor:pointer}
|
|
87
|
+
.wsr-tabs button.on{background:var(--bg-raised);color:var(--text)}
|
|
88
|
+
.wsr-from{flex:none;padding:8px 20px;font-size:12px;color:var(--ws-faint);overflow-wrap:anywhere}
|
|
89
|
+
|
|
90
|
+
/* The set beside it, the open item marked. */
|
|
91
|
+
.wsr-set{flex:none;width:300px;display:flex;flex-direction:column;background:var(--bg-panel);min-height:0}
|
|
92
|
+
.wsr-set-h{display:flex;align-items:center;gap:8px;padding:14px 14px 4px;font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:14px}
|
|
93
|
+
.wsr-set-m{font-family:ui-sans-serif,system-ui,sans-serif;font-size:12px;color:var(--ws-muted)}
|
|
94
|
+
.wsr-set-n{padding:0 14px 8px;font-size:12px;color:var(--ws-muted)}
|
|
95
|
+
.wsr-set-rows{flex:1;min-height:0;overflow:auto;padding:0 8px;display:flex;flex-direction:column;gap:1px}
|
|
96
|
+
.wsr-item{cursor:pointer}
|
|
97
|
+
.wsr-item.on{background:var(--accent-bg);color:var(--text);box-shadow:inset 0 0 0 1px var(--accent-border)}
|
|
98
|
+
.wsr-set-hint{padding:12px 14px;font-size:11.5px;line-height:17px;color:var(--ws-faint);border-top:1px solid var(--border)}
|
|
99
|
+
|
|
100
|
+
/* A phone: full screen, ‹ Set and ‹ ›, no set beside it. */
|
|
101
|
+
@media (max-width:599px){
|
|
102
|
+
.ws-reader{position:fixed;z-index:60;border-radius:0}
|
|
103
|
+
.wsr-main{border-right:0}
|
|
104
|
+
.wsr-set{display:none}
|
|
105
|
+
.wsr-wide{display:none}
|
|
106
|
+
.wsr-narrow{display:inline}
|
|
107
|
+
.wsr-head{gap:8px;padding:calc(8px + var(--safe-t,0px)) 8px 6px}
|
|
108
|
+
.wsr-back{min-width:44px;height:44px;border:0;padding:0 6px;color:var(--accent);font-size:15px;text-align:left}
|
|
109
|
+
.wsr-title{flex:1;flex-direction:column;align-items:center;gap:0;text-align:center}
|
|
110
|
+
.wsr-name{max-width:100%;font-size:14px}
|
|
111
|
+
.wsr-meta{font-size:11.5px}
|
|
112
|
+
.wsr-head-pill{display:none}
|
|
113
|
+
.wsr-nav{display:flex}
|
|
114
|
+
.wsr-strip{padding:10px 14px}
|
|
115
|
+
.wsr-strip-k{display:none}
|
|
116
|
+
.wsr-bar{height:8px}
|
|
117
|
+
.wsr-body{padding:10px 0;gap:10px}
|
|
118
|
+
.wsr-card{border-left:0;border-right:0;border-radius:0}
|
|
119
|
+
.wsr-lines{font-size:11.5px;line-height:18px}
|
|
120
|
+
.wsr-card .read-lines code{min-width:max-content}
|
|
121
|
+
.wsr-banner,.wsr-tabs{margin:10px 12px 0}
|
|
122
|
+
.wsr-tabs{max-width:none}
|
|
123
|
+
.wsr-from{padding:8px 14px}
|
|
124
|
+
.wsr-foot{display:block;flex:none;padding:8px 14px calc(10px + var(--safe-b,0px));border-top:1px solid var(--border);background:var(--bg-panel);font-size:12px;color:var(--ws-faint);text-align:center}
|
|
125
|
+
}
|
|
126
|
+
@media (min-width:600px){.wsr-chips{display:none}}
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
// The working set's reader (scene A of the working-set preview): what chi
|
|
2
|
+
// has of one item, as its timeline, newest first (ws_timeline.js cards,
|
|
3
|
+
// ws_cards.js templates): a file's reads as merged line slices over a strip
|
|
4
|
+
// of the whole file, its edits as diffs, a memory's reads with their
|
|
5
|
+
// overlay and, for a changed one, "Now · diff"; a slice chi no longer has
|
|
6
|
+
// greyed. It replaces the stage (#center: the history and the composer,
|
|
7
|
+
// kept under it, inert, so their scroll place stays) until "← Stage" or
|
|
8
|
+
// Esc; the session's set stays beside it with the open item marked, ↑↓
|
|
9
|
+
// step through it. On a phone it is full screen: ‹ Set back to the list's
|
|
10
|
+
// sheet, ‹ › the other items.
|
|
11
|
+
//
|
|
12
|
+
// Ways in: the popover peek's "Open the timeline" (ws_peek.js), a tool
|
|
13
|
+
// row's "timeline" (ws_way_in.js), the ref ▾ menu's "What chi has" on a
|
|
14
|
+
// file in the set (refs.js REF_ACTIONS.file, its env's +whatChiHas+). All
|
|
15
|
+
// the owner's: app.js makes this only for a viewer with working_set_items.
|
|
16
|
+
//
|
|
17
|
+
// The timeline comes from the item route (getWorkingSetItem); an answer for
|
|
18
|
+
// an item no longer open, or another session, is dropped (latest.js). While
|
|
19
|
+
// open, each new server snapshot asks again (the route builds from the
|
|
20
|
+
// session's messages, which lag a running turn's events). Leaving the
|
|
21
|
+
// session (another one, the start page) resets it: session_view.js
|
|
22
|
+
// forgetSession calls reset(), so #center is never left inert under it.
|
|
23
|
+
|
|
24
|
+
import { html, nothing, render } from "./vendor/lit-html.js";
|
|
25
|
+
import { api } from "./data.js";
|
|
26
|
+
import { createLatest } from "./latest.js";
|
|
27
|
+
import { diffTemplate } from "./diff_view.js";
|
|
28
|
+
import { createPeek } from "./ws_peek.js";
|
|
29
|
+
import { setTimelineWayIn } from "./ws_way_in.js";
|
|
30
|
+
import { cardTemplate } from "./ws_cards.js";
|
|
31
|
+
import { bandBox, countsText, eventTag, fileStrip, itemPath, linesText, timelineCards } from "./ws_timeline.js";
|
|
32
|
+
import { GLYPHS, leafText, opsText, steerId, steerLine, tokensText } from "./working_set.js";
|
|
33
|
+
|
|
34
|
+
// The item route's answer for session +id+ (+now+: a memory's text now).
|
|
35
|
+
export function getWorkingSetItem(id, kind, key, { now = false } = {}) {
|
|
36
|
+
return api(itemPath(id, kind, key, { now }));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const PHONE_MAX = 600;
|
|
40
|
+
|
|
41
|
+
// The head's pill: what is true of the item as a whole.
|
|
42
|
+
function itemPill(item, events) {
|
|
43
|
+
if (item.state === "stale") return { text: item.kind === "memory" ? "changed since chi read it" : "the file changed since chi saw it", warn: true };
|
|
44
|
+
if (item.state === "dropped") return { text: "dropped by you: chi no longer has it", warn: false };
|
|
45
|
+
if (item.state === "forgotten") return { text: "chi no longer has it", warn: false };
|
|
46
|
+
const edits = events.filter((e) => e.op === "edit" || e.op === "write").length;
|
|
47
|
+
return edits ? { text: `chi edited it ${edits}× · newest first`, warn: true } : null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// What an item is called in the head: a file by its path in the session's
|
|
51
|
+
// folder, a memory by its name.
|
|
52
|
+
function itemName(item) {
|
|
53
|
+
if (item.kind === "file") return item.path || item.key;
|
|
54
|
+
if (item.kind === "url") return item.url || item.key;
|
|
55
|
+
return `${GLYPHS.memory} ${item.key}`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// +centerEl+: what the reader covers (#center); +session()+, +state()+: the
|
|
59
|
+
// open session's id and fields (its folder: the undrop line); +getItem+: the
|
|
60
|
+
// item route (tests pass a stand-in); +sendLine(line)+: a steer line
|
|
61
|
+
// (undrop; null: none).
|
|
62
|
+
export function createWorkingSetPreview({ centerEl, session, state = () => ({}), getItem = getWorkingSetItem, sendLine = null,
|
|
63
|
+
doc = document, win = window }) {
|
|
64
|
+
let workingSet = null;
|
|
65
|
+
// The open item ({kind, key}), its answer, the memory's now, the tab.
|
|
66
|
+
let open = null;
|
|
67
|
+
let data = null;
|
|
68
|
+
let failed = false;
|
|
69
|
+
let tab = "has";
|
|
70
|
+
let openFor = null;
|
|
71
|
+
const latest = createLatest(() => `${session()}|${open ? `${open.kind}:${open.key}` : ""}`);
|
|
72
|
+
const peek = createPeek({ session, getItem: (id, kind, key) => getItem(id, kind, key), onChange: () => workingSet?.peekChanged() });
|
|
73
|
+
|
|
74
|
+
const el = doc.createElement("section");
|
|
75
|
+
el.className = "ws-reader";
|
|
76
|
+
el.id = "wsReader";
|
|
77
|
+
el.setAttribute("aria-label", "What chi has");
|
|
78
|
+
el.hidden = true;
|
|
79
|
+
centerEl?.appendChild(el);
|
|
80
|
+
const covered = () => [...(centerEl?.children || [])].filter((child) => child !== el);
|
|
81
|
+
|
|
82
|
+
const items = () => workingSet?.items() || [];
|
|
83
|
+
// The rows the set beside it draws (and ↑↓, ‹ › step over): not the muted.
|
|
84
|
+
const shown = () => items().filter((it) => !it.muted);
|
|
85
|
+
// Where focus was when it opened: it goes back there on close.
|
|
86
|
+
let returnTo = null;
|
|
87
|
+
const idOf = (it) => steerId(it);
|
|
88
|
+
// The open item as the set has it (its runs, state, tokens); a stand-in
|
|
89
|
+
// when the set doesn't hold it (yet).
|
|
90
|
+
const current = () => items().find((it) => it.kind === open?.kind && it.key === open?.key)
|
|
91
|
+
|| (open ? { kind: open.kind, key: open.key, label: open.key, ops: [], state: "live", runs: [] } : null);
|
|
92
|
+
|
|
93
|
+
function load() {
|
|
94
|
+
const id = session();
|
|
95
|
+
if (!id || !open) return;
|
|
96
|
+
const fresh = latest.begin();
|
|
97
|
+
const { kind, key } = open;
|
|
98
|
+
Promise.resolve()
|
|
99
|
+
.then(() => getItem(id, kind, key, { now: kind === "memory" }))
|
|
100
|
+
.then((answer) => {
|
|
101
|
+
if (!fresh()) return;
|
|
102
|
+
data = answer;
|
|
103
|
+
failed = false;
|
|
104
|
+
draw();
|
|
105
|
+
})
|
|
106
|
+
.catch(() => {
|
|
107
|
+
if (!fresh()) return;
|
|
108
|
+
failed = true;
|
|
109
|
+
draw();
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function stripTemplate(events) {
|
|
114
|
+
const strip = fileStrip(events);
|
|
115
|
+
if (!strip) return nothing;
|
|
116
|
+
const merged = strip.bands.filter((b) => !b.latest).sort((a, b) => a.first - b.first);
|
|
117
|
+
const ranges = merged.map((b) => `${b.first}–${b.last}`).join(", ");
|
|
118
|
+
const bands = strip.bands.map((band) => {
|
|
119
|
+
const box = bandBox(band, strip.length);
|
|
120
|
+
return html`<button type="button" class=${`wsr-band ${band.state}${band.latest ? " latest" : ""}`} data-wsr="band" data-first=${band.first} style=${`left:${box.left};width:${box.width}`} title=${`${linesText(band)}${band.state === "sent" ? "" : ` (${band.state})`}: go to its lines`}></button>`;
|
|
121
|
+
});
|
|
122
|
+
const chips = merged.map((band) => html`<button type="button" class=${`wsr-chip ${band.state}`} data-wsr="band" data-first=${band.first}>${band.first}–${band.last}</button>`);
|
|
123
|
+
return html`<div class="wsr-strip"><div class="wsr-strip-k"><span>1</span><span>read: ${ranges} of ${strip.length} lines known · tap a band to jump</span><span>${strip.length}</span></div><div class="wsr-bar" aria-label=${`read ${ranges} of ${strip.length} lines`}>${bands}</div><div class="wsr-chips">${chips}</div></div>`;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function memoryTemplate(item, events) {
|
|
127
|
+
const now = data?.now;
|
|
128
|
+
const changed = item.state === "stale" && now;
|
|
129
|
+
const lastRead = events.filter((e) => e.op === "memory_read").at(-1);
|
|
130
|
+
const memory = lastRead?.memory;
|
|
131
|
+
const banner = changed ? html`<div class="wsr-banner">Changed since chi's last read${lastRead ? ` (${eventTag(lastRead, item)})` : ""}: what chi has is not what the memory says now.</div>` : nothing;
|
|
132
|
+
const tabs = changed ? html`<div class="wsr-tabs" role="tablist"><button type="button" role="tab" aria-selected=${String(tab === "has")} class=${tab === "has" ? "on" : ""} data-wsr="tab" data-tab="has">What chi has</button><button type="button" role="tab" aria-selected=${String(tab === "now")} class=${tab === "now" ? "on" : ""} data-wsr="tab" data-tab="now">Now · diff</button></div>` : nothing;
|
|
133
|
+
const from = memory?.path ? html`<div class="wsr-from">${memory.scope} memory · from ${memory.path.replace(/[^/]*$/, "")}</div>` : nothing;
|
|
134
|
+
return { banner, tabs, from, nowBody: changed && tab === "now" ? (now.gone ? html`<div class="diff-note warn">the memory's file is gone</div>` : html`<article class="wsr-card now"><header class="wsr-card-h"><span class="wsr-card-t">what chi has → now</span></header>${diffTemplate(now.diff)}</article>`) : null };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function setTemplate(item) {
|
|
138
|
+
const rows = shown().map((it) => {
|
|
139
|
+
const on = it.kind === item.kind && it.key === item.key;
|
|
140
|
+
const state = it.state && it.state !== "live" ? ` ${it.state}` : "";
|
|
141
|
+
return html`<button type="button" class=${`ws-row wsr-item ${it.kind}${state}${on ? " on" : ""}`} data-wsr="item" data-ws-id=${idOf(it)} aria-current=${on ? "true" : "false"}><span class="k">${GLYPHS[it.kind] || ""}</span><span class="t">${leafText(it)}</span><span class="st">${opsText(it)}</span></button>`;
|
|
142
|
+
});
|
|
143
|
+
const files = items().filter((it) => it.kind === "file").length;
|
|
144
|
+
const memories = items().filter((it) => it.kind === "memory").length;
|
|
145
|
+
const pct = Number.isFinite(state().ctxPct) ? ` · ctx ${Math.round(state().ctxPct)} %` : "";
|
|
146
|
+
return html`<aside class="wsr-set" aria-label="Working set"><div class="wsr-set-h"><strong>working set</strong><span class="wsr-set-m">session</span></div><div class="wsr-set-n">${items().length} items${pct} · files · ${files} · memories · ${memories}</div><div class="wsr-set-rows">${rows}</div><div class="wsr-set-hint">↑↓ steps through the set · Esc back to the stage (same scroll place)</div></aside>`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function draw() {
|
|
150
|
+
if (!open) return;
|
|
151
|
+
const item = current();
|
|
152
|
+
const events = data?.events || [];
|
|
153
|
+
const cards = timelineCards(events);
|
|
154
|
+
const tokens = tokensText(item.tokens);
|
|
155
|
+
const counts = countsText(events);
|
|
156
|
+
const pill = data ? itemPill(item, events) : null;
|
|
157
|
+
const memory = item.kind === "memory" ? memoryTemplate(item, events) : null;
|
|
158
|
+
const scope = item.kind === "memory" ? events.find((e) => e.memory)?.memory?.scope : null;
|
|
159
|
+
const meta = [scope ? `${scope} memory` : "", "timeline, newest first", counts, tokens ? `${tokens} tokens` : ""].filter(Boolean).join(" · ");
|
|
160
|
+
const phoneMeta = [scope ? `${scope} memory` : "", counts, tokens ? `${tokens} tokens` : ""].filter(Boolean).join(" · ");
|
|
161
|
+
const undrop = sendLine && (item.state === "dropped" || item.hasDrop) ? html`<button type="button" class="ws-act-b" data-wsr="undrop">undrop</button>` : nothing;
|
|
162
|
+
let body;
|
|
163
|
+
if (failed) body = html`<div class="wsr-note">Couldn't load what chi has of it.</div>`;
|
|
164
|
+
else if (!data) body = html`<div class="wsr-note">Loading…</div>`;
|
|
165
|
+
else if (!cards.length) body = html`<div class="wsr-note">Nothing of it in the conversation yet: a running turn's reads show at its next step.</div>`;
|
|
166
|
+
else if (memory?.nowBody) body = memory.nowBody;
|
|
167
|
+
else body = cards.map((card) => cardTemplate(card, { item, undrop }));
|
|
168
|
+
const list = shown();
|
|
169
|
+
const at = list.findIndex((it) => it.kind === item.kind && it.key === item.key);
|
|
170
|
+
const tpl = html`<div class="wsr-main">
|
|
171
|
+
<div class="wsr-head">
|
|
172
|
+
<button type="button" class="wsr-back" data-wsr="back"><span class="wsr-wide">← Stage</span><span class="wsr-narrow">‹ Set</span></button>
|
|
173
|
+
<span class="wsr-title"><span class="wsr-name" title=${item.key}>${itemName(item)}</span><span class="wsr-meta wsr-wide">${meta}</span><span class="wsr-meta wsr-narrow">${phoneMeta}</span></span>
|
|
174
|
+
${pill ? html`<span class=${`wsr-head-pill${pill.warn ? " warn" : ""}`}>${pill.text}</span>` : nothing}
|
|
175
|
+
<span class="wsr-nav wsr-narrow"><button type="button" data-wsr="prev" aria-label="Previous item" ?disabled=${at <= 0}>‹</button><button type="button" data-wsr="next" aria-label="Next item" ?disabled=${at < 0 || at >= list.length - 1}>›</button></span>
|
|
176
|
+
</div>
|
|
177
|
+
${memory ? memory.banner : nothing}${memory ? memory.tabs : nothing}
|
|
178
|
+
${item.kind === "file" ? stripTemplate(events) : nothing}
|
|
179
|
+
<div class="wsr-body" tabindex="-1">${body}</div>
|
|
180
|
+
${memory ? memory.from : nothing}
|
|
181
|
+
<div class="wsr-foot wsr-narrow">‹ › the next item · lines don't wrap, scroll sideways in the code</div>
|
|
182
|
+
</div>${setTemplate(item)}`;
|
|
183
|
+
render(tpl, el);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// Opens +target+ ({kind, key}): the stage stays under it, inert.
|
|
187
|
+
function openItem(target) {
|
|
188
|
+
if (!target?.kind || !target.key) return;
|
|
189
|
+
const again = open && open.kind === target.kind && open.key === target.key;
|
|
190
|
+
open = { kind: target.kind, key: target.key };
|
|
191
|
+
openFor = session();
|
|
192
|
+
if (!again) {
|
|
193
|
+
data = null;
|
|
194
|
+
failed = false;
|
|
195
|
+
tab = "has";
|
|
196
|
+
}
|
|
197
|
+
if (el.hidden) {
|
|
198
|
+
const active = doc.activeElement;
|
|
199
|
+
returnTo = active && active !== doc.body && !el.contains(active) ? active : null;
|
|
200
|
+
for (const child of covered()) child.inert = true;
|
|
201
|
+
el.hidden = false;
|
|
202
|
+
doc.body.classList.add("ws-reading");
|
|
203
|
+
}
|
|
204
|
+
draw();
|
|
205
|
+
el.querySelector(".wsr-body")?.scrollTo?.(0, 0);
|
|
206
|
+
el.querySelector(".wsr-back")?.focus({ preventScroll: true });
|
|
207
|
+
load();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// +refocus+: focus goes back where it was at the open (not for a jump,
|
|
211
|
+
// which scrolls to its row, nor a reset).
|
|
212
|
+
function close({ refocus = true } = {}) {
|
|
213
|
+
if (el.hidden) return;
|
|
214
|
+
open = null;
|
|
215
|
+
data = null;
|
|
216
|
+
latest.drop();
|
|
217
|
+
el.hidden = true;
|
|
218
|
+
for (const child of covered()) child.inert = false;
|
|
219
|
+
doc.body.classList.remove("ws-reading");
|
|
220
|
+
render(nothing, el);
|
|
221
|
+
const back = returnTo;
|
|
222
|
+
returnTo = null;
|
|
223
|
+
if (refocus && back?.isConnected && !back.closest?.("[inert]")) back.focus?.({ preventScroll: true });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// The item +step+ places from the open one in the set.
|
|
227
|
+
function stepItem(step) {
|
|
228
|
+
const list = shown();
|
|
229
|
+
const at = list.findIndex((it) => it.kind === open?.kind && it.key === open?.key);
|
|
230
|
+
const next = list[at + step];
|
|
231
|
+
if (next) openItem(next);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
const phone = () => win.innerWidth < PHONE_MAX;
|
|
235
|
+
|
|
236
|
+
el.addEventListener("click", (e) => {
|
|
237
|
+
const act = e.target.closest("[data-wsr], [data-ws='jump']");
|
|
238
|
+
if (!act) return;
|
|
239
|
+
if (act.dataset.ws === "jump") {
|
|
240
|
+
const [turn, step, row] = String(act.dataset.at).split(".").map(Number);
|
|
241
|
+
close({ refocus: false });
|
|
242
|
+
workingSet?.jump({ turn, step, row });
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
const what = act.dataset.wsr;
|
|
246
|
+
if (what === "back") {
|
|
247
|
+
close();
|
|
248
|
+
// After this click has passed the list's outside-click close.
|
|
249
|
+
if (phone()) setTimeout(() => workingSet?.openList(), 0);
|
|
250
|
+
} else if (what === "item") {
|
|
251
|
+
const found = items().find((it) => idOf(it) === act.dataset.wsId);
|
|
252
|
+
if (found) openItem(found);
|
|
253
|
+
} else if (what === "prev") stepItem(-1);
|
|
254
|
+
else if (what === "next") stepItem(1);
|
|
255
|
+
else if (what === "tab") {
|
|
256
|
+
tab = act.dataset.tab === "now" ? "now" : "has";
|
|
257
|
+
draw();
|
|
258
|
+
} else if (what === "band") {
|
|
259
|
+
const first = Number(act.dataset.first);
|
|
260
|
+
const card = [...el.querySelectorAll(".wsr-card.slice")].find((c) => c.querySelector(`.read-line[data-line="${first}"]`));
|
|
261
|
+
card?.scrollIntoView?.({ block: "start" });
|
|
262
|
+
} else if (what === "undrop" && sendLine) {
|
|
263
|
+
const item = current();
|
|
264
|
+
Promise.resolve(sendLine(steerLine("undrop", [item], state().dir))).catch(() => {});
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
doc.addEventListener("keydown", (e) => {
|
|
269
|
+
if (el.hidden || e.defaultPrevented || e.altKey || e.ctrlKey || e.metaKey) return;
|
|
270
|
+
if (e.target?.closest?.("input, textarea, [contenteditable]")) return;
|
|
271
|
+
// A popover over the reader takes its own Esc.
|
|
272
|
+
if (doc.querySelector(".ctx-popover:not([hidden])")) return;
|
|
273
|
+
if (e.key === "Escape") {
|
|
274
|
+
e.preventDefault();
|
|
275
|
+
close();
|
|
276
|
+
} else if (e.key === "ArrowDown" || e.key === "ArrowUp") {
|
|
277
|
+
e.preventDefault();
|
|
278
|
+
stepItem(e.key === "ArrowDown" ? 1 : -1);
|
|
279
|
+
}
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
// A tool row's "timeline" (ws_way_in.js).
|
|
283
|
+
doc.addEventListener("click", (e) => {
|
|
284
|
+
const button = e.target.closest?.(".activity-ws[data-ws-kind]");
|
|
285
|
+
if (!button) return;
|
|
286
|
+
e.preventDefault();
|
|
287
|
+
openItem({ kind: button.dataset.wsKind, key: button.dataset.wsKey });
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
return {
|
|
291
|
+
peek,
|
|
292
|
+
// The working set's view (working_set_view.js), once made: the set,
|
|
293
|
+
// its jumps and its list; rows get their way in from now on.
|
|
294
|
+
attach(view) {
|
|
295
|
+
workingSet = view;
|
|
296
|
+
if (!view) return;
|
|
297
|
+
setTimelineWayIn(true);
|
|
298
|
+
// A new snapshot: asked again (a snapshot of another session: closed,
|
|
299
|
+
// though forgetSession's reset has done it already).
|
|
300
|
+
view.onServer(() => {
|
|
301
|
+
if (el.hidden) return;
|
|
302
|
+
if (session() !== openFor) close({ refocus: false });
|
|
303
|
+
else load();
|
|
304
|
+
});
|
|
305
|
+
},
|
|
306
|
+
open: openItem,
|
|
307
|
+
close,
|
|
308
|
+
isOpen: () => !el.hidden,
|
|
309
|
+
// The ref ▾ menu's "What chi has" (refs.js): a file ref whose path is
|
|
310
|
+
// an item of the set.
|
|
311
|
+
whatChiHas: {
|
|
312
|
+
has: (path) => items().some((it) => it.kind === "file" && it.key === path),
|
|
313
|
+
open: (path) => openItem({ kind: "file", key: path }),
|
|
314
|
+
},
|
|
315
|
+
// The session forgotten (another one, the start page: session_view.js
|
|
316
|
+
// forgetSession): closed, the stage no longer inert.
|
|
317
|
+
reset() { close({ refocus: false }); peek.close(); },
|
|
318
|
+
};
|
|
319
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
// What the model has of one working-set item, as the reader (ws_reader.js)
|
|
2
|
+
// and the popover's peek (ws_peek.js) show it: the pure part. No DOM, so
|
|
3
|
+
// `node --test` covers it. The server's timeline (GET …/working_set/item,
|
|
4
|
+
// WorkingSet.timeline_of: docs/internals/working-set.md, Timeline) is every
|
|
5
|
+
// run of the item in conversation order; here it becomes cards, newest
|
|
6
|
+
// first:
|
|
7
|
+
// - a slice: reads whose lines overlap or touch, merged (each line from the
|
|
8
|
+
// newest read holding it), its header listing the reads; reads merge only
|
|
9
|
+
// with reads in the same state (still sent, dropped, forgotten), so a
|
|
10
|
+
// slice the model no longer has is greyed whole, and only between the same
|
|
11
|
+
// edits or writes (an edit moves the lines after it);
|
|
12
|
+
// - a partial read (a head/tail preview, a cut output), shown as it is;
|
|
13
|
+
// - an edit or a write, its saved diff (diff_view.js);
|
|
14
|
+
// - a memory read (its text, its overlay apart), a memory write, a fetch.
|
|
15
|
+
// The file strip draws the merged ranges over the file's length (the
|
|
16
|
+
// largest last line any read knows: a whole read's is the file's).
|
|
17
|
+
//
|
|
18
|
+
// An event's place on the page (its step tag, "go to the call") is the
|
|
19
|
+
// item's row with its tool id (working_set.js runs); a legacy row has
|
|
20
|
+
// none, so the server's global step names it.
|
|
21
|
+
|
|
22
|
+
import { stepTag } from "./working_set.js";
|
|
23
|
+
|
|
24
|
+
// A slice longer than this shows its first lines and a "show all" fold.
|
|
25
|
+
export const SLICE_FOLD_LINES = 200;
|
|
26
|
+
// The peek's lines of a read or a text.
|
|
27
|
+
export const PEEK_LINES = 12;
|
|
28
|
+
|
|
29
|
+
const STATES = ["sent", "dropped", "forgotten"];
|
|
30
|
+
|
|
31
|
+
// An event's state: sent, dropped (the user's) or forgotten.
|
|
32
|
+
export function eventState(event) {
|
|
33
|
+
if (event?.dropped) return "dropped";
|
|
34
|
+
if (event && event.sent === false) return "forgotten";
|
|
35
|
+
return "sent";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// The path of the item route for session +id+ (+now+: a memory's text now).
|
|
39
|
+
export function itemPath(id, kind, key, { now = false } = {}) {
|
|
40
|
+
const query = new URLSearchParams({ kind, key });
|
|
41
|
+
if (now) query.set("now", "1");
|
|
42
|
+
return `/api/sessions/${encodeURIComponent(id)}/working_set/item?${query}`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// A read's lines as numbered rows ([{n, text}]), its text split as the
|
|
46
|
+
// file's (a last newline ends a line), at most up to its last line.
|
|
47
|
+
export function readRows(event) {
|
|
48
|
+
const first = Number(event?.lines?.first) || 1;
|
|
49
|
+
const lines = String(event?.text ?? "").split("\n");
|
|
50
|
+
if (lines.at(-1) === "") lines.pop();
|
|
51
|
+
const last = Number(event?.lines?.last);
|
|
52
|
+
const count = Number.isFinite(last) && last >= first ? Math.min(lines.length, last - first + 1) : lines.length;
|
|
53
|
+
return lines.slice(0, count).map((text, i) => ({ n: first + i, text }));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const mergeable = (event) => event.op === "read" && !event.lines?.partial && Number.isFinite(event.lines?.first) && Number.isFinite(event.lines?.last);
|
|
57
|
+
|
|
58
|
+
// The reads of +events+ merged into slices: [{type: "slice", first, last,
|
|
59
|
+
// state, reads, rows, newest}], +newest+ the place of its newest read.
|
|
60
|
+
function slices(events) {
|
|
61
|
+
const out = [];
|
|
62
|
+
// An edit or a write starts a new version of the file: its lines move, so
|
|
63
|
+
// only reads between the same edits merge (line numbers are one file's).
|
|
64
|
+
let version = 0;
|
|
65
|
+
const versions = events.map((event) => {
|
|
66
|
+
if (event.op === "edit" || event.op === "write") version += 1;
|
|
67
|
+
return version;
|
|
68
|
+
});
|
|
69
|
+
for (let v = 0; v <= version; v += 1) for (const state of STATES) {
|
|
70
|
+
const reads = events.map((event, place) => ({ event, place }))
|
|
71
|
+
.filter(({ event, place }) => versions[place] === v && mergeable(event) && eventState(event) === state)
|
|
72
|
+
.sort((a, b) => a.event.lines.first - b.event.lines.first || a.place - b.place);
|
|
73
|
+
let open = null;
|
|
74
|
+
for (const read of reads) {
|
|
75
|
+
const { first, last } = read.event.lines;
|
|
76
|
+
if (open && first <= open.last + 1) {
|
|
77
|
+
open.last = Math.max(open.last, last);
|
|
78
|
+
open.reads.push(read);
|
|
79
|
+
} else {
|
|
80
|
+
open = { type: "slice", first, last, state, reads: [read] };
|
|
81
|
+
out.push(open);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return out.map((slice) => {
|
|
86
|
+
// Each line from the newest read that holds it.
|
|
87
|
+
const lines = new Map();
|
|
88
|
+
for (const { event } of [...slice.reads].sort((a, b) => a.place - b.place)) {
|
|
89
|
+
for (const row of readRows(event)) lines.set(row.n, row.text);
|
|
90
|
+
}
|
|
91
|
+
const rows = [...lines.keys()].sort((a, b) => a - b).map((n) => ({ n, text: lines.get(n) }));
|
|
92
|
+
const reads = slice.reads.sort((a, b) => b.place - a.place).map(({ event }) => event);
|
|
93
|
+
return { ...slice, reads, rows, newest: Math.max(...slice.reads.map((r) => r.place)) };
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const TYPES = { edit: "edit", write: "edit", memory_read: "memory", memory_write: "written", fetch: "fetch" };
|
|
98
|
+
|
|
99
|
+
// The timeline's cards, newest first: [{type, newest, ...}] — slices
|
|
100
|
+
// (above), and {type: "partial" | "edit" | "memory" | "written" | "fetch",
|
|
101
|
+
// event, state}. The newest card is +latest+.
|
|
102
|
+
export function timelineCards(events = []) {
|
|
103
|
+
const list = Array.isArray(events) ? events : [];
|
|
104
|
+
const cards = slices(list);
|
|
105
|
+
list.forEach((event, place) => {
|
|
106
|
+
if (event.op === "read" && !mergeable(event)) cards.push({ type: "partial", event, state: eventState(event), newest: place });
|
|
107
|
+
else if (TYPES[event.op]) cards.push({ type: TYPES[event.op], event, state: eventState(event), newest: place });
|
|
108
|
+
});
|
|
109
|
+
cards.sort((a, b) => b.newest - a.newest);
|
|
110
|
+
if (cards.length) cards[0] = { ...cards[0], latest: true };
|
|
111
|
+
return cards;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// The file strip: the merged ranges over the file's length (+length+: the
|
|
115
|
+
// largest last line known), the newest read's range +latest+.
|
|
116
|
+
// @return {length, bands: [{first, last, state, latest?}]} or null without one
|
|
117
|
+
export function fileStrip(events = []) {
|
|
118
|
+
const reads = (events || []).filter(mergeable);
|
|
119
|
+
if (!reads.length) return null;
|
|
120
|
+
const length = Math.max(...reads.map((e) => e.lines.last));
|
|
121
|
+
const bands = slices(events).map(({ first, last, state }) => ({ first, last, state }));
|
|
122
|
+
const newest = reads.at(-1);
|
|
123
|
+
bands.push({ first: newest.lines.first, last: newest.lines.last, state: eventState(newest), latest: true });
|
|
124
|
+
return { length, bands };
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// A band's place on the strip as percentages ({left, width}).
|
|
128
|
+
export function bandBox(band, length) {
|
|
129
|
+
const total = Math.max(1, Number(length) || 1);
|
|
130
|
+
const left = ((band.first - 1) / total) * 100;
|
|
131
|
+
const width = Math.max(0.5, ((band.last - band.first + 1) / total) * 100);
|
|
132
|
+
return { left: `${left.toFixed(2)}%`, width: `${Math.min(width, 100 - left).toFixed(2)}%` };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// "3 reads, 1 edit" (the peek's link and the reader's head).
|
|
136
|
+
export function countsText(events = []) {
|
|
137
|
+
const reads = events.filter((e) => e.op === "read" || e.op === "memory_read" || e.op === "fetch").length;
|
|
138
|
+
const edits = events.filter((e) => e.op === "edit" || e.op === "write" || e.op === "memory_write").length;
|
|
139
|
+
const word = (n, one) => `${n} ${one}${n === 1 ? "" : "s"}`;
|
|
140
|
+
return [reads ? word(reads, "read") : "", edits ? word(edits, "edit") : ""].filter(Boolean).join(", ");
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// The place of +event+ on the page: its row's at, from the item's runs
|
|
144
|
+
// (working_set.js), or null (a legacy row, a row not drawn).
|
|
145
|
+
export function placeOf(event, item) {
|
|
146
|
+
const run = (item?.runs || []).find((r) => r.id === event?.tool_id);
|
|
147
|
+
return run ? run.at : null;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// An event's step tag: the row's "t3·s14", else the server's "step 14".
|
|
151
|
+
export function eventTag(event, item) {
|
|
152
|
+
const at = placeOf(event, item);
|
|
153
|
+
if (at) return stepTag({ at }, { session: true });
|
|
154
|
+
return Number.isFinite(event?.step) ? `step ${event.step}` : "";
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// A slice's or a card's steps for its header ("t3·s13, t3·s162").
|
|
158
|
+
export function tagsText(events, item) {
|
|
159
|
+
return events.map((event) => eventTag(event, item)).filter(Boolean).join(", ");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// A read's lines in words: "lines 204–265", "line 3", "whole file".
|
|
163
|
+
export function linesText(lines) {
|
|
164
|
+
if (!lines) return "";
|
|
165
|
+
if (lines.partial) return lines.whole ? "a preview of the file" : `from line ${lines.first}, cut`;
|
|
166
|
+
return lines.first === lines.last ? `line ${lines.first}` : `lines ${lines.first}–${lines.last}`;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const NO_LONGER = { dropped: "dropped by you", forgotten: "forgotten" };
|
|
170
|
+
|
|
171
|
+
// Why chi no longer has a card's text, or "" while it does.
|
|
172
|
+
export function goneText(state) {
|
|
173
|
+
return NO_LONGER[state] || "";
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// What the peek shows of an item: the latest edit's diff when it has one,
|
|
177
|
+
// else the newest read's (or text's) first lines; +after+ the newest event
|
|
178
|
+
// after the one shown (a read after the edit).
|
|
179
|
+
// @return {event, kind: "diff" | "lines" | "text", rows?, after} or null
|
|
180
|
+
export function peekOf(events = []) {
|
|
181
|
+
const list = Array.isArray(events) ? events : [];
|
|
182
|
+
if (!list.length) return null;
|
|
183
|
+
const editAt = list.findLastIndex((e) => (e.op === "edit" || e.op === "write") && e.diff);
|
|
184
|
+
if (editAt >= 0) {
|
|
185
|
+
const after = list.slice(editAt + 1).at(-1) || null;
|
|
186
|
+
return { event: list[editAt], kind: "diff", after };
|
|
187
|
+
}
|
|
188
|
+
// An edit without a diff (a no-op) shows nothing: the newest other run.
|
|
189
|
+
const event = list.findLast((e) => e.op !== "edit" && e.op !== "write") || list.at(-1);
|
|
190
|
+
if (event.op === "read") return { event, kind: "lines", rows: readRows(event).slice(0, PEEK_LINES), after: null };
|
|
191
|
+
const rows = String(event.text ?? "").split("\n").slice(0, PEEK_LINES).map((text, i) => ({ n: i + 1, text }));
|
|
192
|
+
return { event, kind: "text", rows, after: null };
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// What an event is, in a header's words ("edit", "read 236–250", "memory read").
|
|
196
|
+
export function eventWords(event) {
|
|
197
|
+
if (!event) return "";
|
|
198
|
+
if (event.op === "read") return `read ${linesText(event.lines).replace(/^lines? /, "")}`.trim();
|
|
199
|
+
return { edit: "edit", write: "write", memory_read: "memory read", memory_write: "memory write", fetch: "fetch" }[event.op] || event.op;
|
|
200
|
+
}
|