samagotchi 0.180.0 → 0.181.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 +46 -1
- data/bin/chi +10 -0
- data/docs/architecture.md +1 -1
- data/docs/bundles.md +7 -2
- data/docs/cli.md +16 -9
- data/docs/configuration.md +24 -4
- data/docs/internals/anthropic.md +4 -3
- data/docs/internals/background-tasks.md +2 -0
- data/docs/internals/block-renderers.md +9 -6
- data/docs/internals/objectives.md +9 -4
- data/docs/internals/web-frontend.md +67 -7
- data/docs/internals/working-set.md +24 -5
- data/docs/memory.md +3 -0
- data/docs/plugins.md +11 -4
- data/docs/sessions.md +1 -1
- data/docs/testing.md +1 -1
- data/lib/samagotchi/bundle_command.rb +4 -1
- data/lib/samagotchi/bundles/known-names/manifest.yml +2 -2
- data/lib/samagotchi/bundles/known-names/plugin.rb +4 -3
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +3 -2
- data/lib/samagotchi/bundles/system/manifest.yml +2 -2
- data/lib/samagotchi/client.rb +8 -1
- data/lib/samagotchi/config.rb +6 -1
- data/lib/samagotchi/core_notice.rb +44 -0
- data/lib/samagotchi/host_address.rb +7 -0
- data/lib/samagotchi/host_config.rb +8 -5
- data/lib/samagotchi/host_registry.rb +26 -8
- data/lib/samagotchi/kernel_loop.rb +3 -2
- data/lib/samagotchi/llm/adapter_common.rb +3 -2
- data/lib/samagotchi/llm/adapters.rb +2 -1
- data/lib/samagotchi/llm/anthropic_assembly.rb +5 -0
- data/lib/samagotchi/llm/anthropic_messages.rb +6 -2
- data/lib/samagotchi/llm/chat_loop.rb +6 -5
- data/lib/samagotchi/llm/errors.rb +41 -3
- data/lib/samagotchi/llm/http.rb +85 -22
- data/lib/samagotchi/llm/openai_chat.rb +20 -3
- data/lib/samagotchi/memory_bundle/status.rb +2 -1
- data/lib/samagotchi/objectives.rb +7 -2
- data/lib/samagotchi/owner_lock.rb +2 -0
- data/lib/samagotchi/plugin/api.rb +4 -1
- data/lib/samagotchi/plugin/context.rb +9 -0
- data/lib/samagotchi/plugin/peer.rb +32 -15
- data/lib/samagotchi/plugin/tool_spec.rb +13 -2
- data/lib/samagotchi/row_refs.rb +14 -3
- data/lib/samagotchi/session_chain/delete.rb +6 -6
- data/lib/samagotchi/session_manager/removal.rb +2 -2
- data/lib/samagotchi/tool_ids.rb +19 -0
- data/lib/samagotchi/tool_runner.rb +8 -4
- data/lib/samagotchi/tools/registry.rb +3 -2
- data/lib/samagotchi/tools/task_runtime.rb +34 -0
- data/lib/samagotchi/turn_stages/objectives.rb +10 -5
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/public/app.js +10 -14
- data/lib/samagotchi/web/public/block_host.js +20 -20
- data/lib/samagotchi/web/public/block_renderers.css +5 -5
- data/lib/samagotchi/web/public/context_chips.js +3 -4
- data/lib/samagotchi/web/public/images.js +1 -1
- data/lib/samagotchi/web/public/index.html +24 -5
- data/lib/samagotchi/web/public/objectives_strip.js +16 -6
- data/lib/samagotchi/web/public/pan_zoom.js +121 -0
- data/lib/samagotchi/web/public/peer_chip.js +3 -3
- data/lib/samagotchi/web/public/peer_data.js +27 -15
- data/lib/samagotchi/web/public/picture_viewer.js +304 -0
- data/lib/samagotchi/web/public/popover_modal.js +3 -2
- data/lib/samagotchi/web/public/session_view.js +4 -0
- data/lib/samagotchi/web/public/working_set.js +9 -3
- data/lib/samagotchi/web/public/working_set_view.js +28 -11
- data/lib/samagotchi/web/public/ws_cards.js +36 -10
- data/lib/samagotchi/web/public/ws_markdown.js +129 -0
- data/lib/samagotchi/web/public/ws_order.js +48 -0
- data/lib/samagotchi/web/public/ws_peek.js +22 -5
- data/lib/samagotchi/web/public/ws_preview.css +41 -1
- data/lib/samagotchi/web/public/ws_reader.js +35 -6
- data/lib/samagotchi/web/public/ws_sections.js +24 -15
- metadata +6 -1
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// The picture viewer's maths (picture_viewer.js), pure: no DOM, `node --test`.
|
|
2
|
+
//
|
|
3
|
+
// A view is {scale, x, y}: the picture drawn at scale × its natural size, its
|
|
4
|
+
// top-left corner at (x, y) in the stage's px. A stage is {width, height,
|
|
5
|
+
// insets: {t, r, b, l}, bar}: the viewer's box, the safe areas and the bar's
|
|
6
|
+
// room at the bottom; the picture is fitted and centred in what they leave.
|
|
7
|
+
// A picture is {width, height}, its natural size.
|
|
8
|
+
//
|
|
9
|
+
// A module of its own: its exports are new (docs/internals/web-frontend.md).
|
|
10
|
+
|
|
11
|
+
// Never closer than 8 × natural.
|
|
12
|
+
export const MAX_ZOOM = 8;
|
|
13
|
+
// A fitted picture is never blown up past this: a tiny diagram stays small.
|
|
14
|
+
export const MAX_FIT = 3;
|
|
15
|
+
// A side of the shown picture past this isn't asked of the browser.
|
|
16
|
+
export const MAX_SHOWN_SIDE = 16000;
|
|
17
|
+
// Nor a shown area past this: 4096², iOS Safari's own canvas cap, as the budget
|
|
18
|
+
// for an image drawn at its layout size on an iPhone (block_host.js's
|
|
19
|
+
// MAX_PICTURE_PIXELS, 100M, is what the page holds at all, far over what a phone draws).
|
|
20
|
+
export const MAX_SHOWN_PIXELS = 4096 * 4096;
|
|
21
|
+
// The room kept around a fitted picture.
|
|
22
|
+
export const FIT_PAD = 24;
|
|
23
|
+
// The bar's room at the bottom (its height and its offset).
|
|
24
|
+
export const BAR_ROOM = 56;
|
|
25
|
+
// A larger picture keeps at least this much of itself on screen.
|
|
26
|
+
export const KEEP = 60;
|
|
27
|
+
// A trackpad sends many small deltas: the zoom follows them, not fixed steps.
|
|
28
|
+
const WHEEL_ZOOM = 0.01;
|
|
29
|
+
// A mouse's one notch with Ctrl held is about 100 px: no more than this per event.
|
|
30
|
+
const WHEEL_ZOOM_MAX_DELTA = 60;
|
|
31
|
+
const LINE_PX = 16;
|
|
32
|
+
const PAGE_PX = 400;
|
|
33
|
+
|
|
34
|
+
const insetsOf = (stage) => ({ t: 0, r: 0, b: 0, l: 0, ...(stage.insets || {}) });
|
|
35
|
+
|
|
36
|
+
// The part of the stage the picture is fitted and centred in.
|
|
37
|
+
export function freeBox(stage) {
|
|
38
|
+
const i = insetsOf(stage);
|
|
39
|
+
const bar = stage.bar ?? 0;
|
|
40
|
+
return { left: i.l, top: i.t, width: Math.max(0, stage.width - i.l - i.r), height: Math.max(0, stage.height - i.t - i.b - bar) };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// The scale at which the whole picture shows in the stage, minus its padding,
|
|
44
|
+
// the safe areas and the bar; never above +max+.
|
|
45
|
+
export function fitScale(picture, stage, { pad = FIT_PAD, max = MAX_FIT } = {}) {
|
|
46
|
+
if (!(picture?.width > 0 && picture?.height > 0)) return 1;
|
|
47
|
+
const box = freeBox(stage);
|
|
48
|
+
const w = Math.max(1, box.width - pad * 2);
|
|
49
|
+
const h = Math.max(1, box.height - pad * 2);
|
|
50
|
+
return Math.min(w / picture.width, h / picture.height, max);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// The closest a picture goes: 8 × natural, less when a side would pass
|
|
54
|
+
// MAX_SHOWN_SIDE or the area MAX_SHOWN_PIXELS; never under +fit+.
|
|
55
|
+
export function maxScale(picture, fit = 0) {
|
|
56
|
+
const side = Math.max(picture.width, picture.height);
|
|
57
|
+
const byArea = Math.sqrt(MAX_SHOWN_PIXELS / (picture.width * picture.height));
|
|
58
|
+
return Math.max(Math.min(MAX_ZOOM, MAX_SHOWN_SIDE / side, byArea), fit);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// +scale+ within the limits: half of min(fit, 1) at the farthest, maxScale at
|
|
62
|
+
// the closest. +limits+: {fit, picture}.
|
|
63
|
+
export function clampScale(scale, { fit, picture }) {
|
|
64
|
+
const min = Math.min(fit, 1) / 2;
|
|
65
|
+
return Math.min(Math.max(scale, min), maxScale(picture, fit));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// The view at scale +next+ that keeps the picture's point under +point+ still.
|
|
69
|
+
export function zoomAt(view, next, point) {
|
|
70
|
+
const k = next / view.scale;
|
|
71
|
+
return { scale: next, x: point.x - (point.x - view.x) * k, y: point.y - (point.y - view.y) * k };
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function clampAxis(at, size, start, room, keep) {
|
|
75
|
+
if (size <= room) return start + (room - size) / 2;
|
|
76
|
+
return Math.min(start + room - keep, Math.max(start + keep - size, at));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// A picture smaller than the free box on an axis is centred on it; a larger one
|
|
80
|
+
// keeps at least +keep+ px of itself on screen.
|
|
81
|
+
export function clampView(view, picture, stage, keep = KEEP) {
|
|
82
|
+
const box = freeBox(stage);
|
|
83
|
+
const w = picture.width * view.scale;
|
|
84
|
+
const h = picture.height * view.scale;
|
|
85
|
+
const x = w <= box.width ? box.left + (box.width - w) / 2 : clampAxis(view.x, w, 0, stage.width, keep);
|
|
86
|
+
const y = h <= box.height ? box.top + (box.height - h) / 2 : clampAxis(view.y, h, 0, stage.height, keep);
|
|
87
|
+
return { scale: view.scale, x, y };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// The fitted view: the whole picture, centred.
|
|
91
|
+
export function fitView(picture, stage) {
|
|
92
|
+
return clampView({ scale: fitScale(picture, stage), x: 0, y: 0 }, picture, stage);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const distance = (a, b) => Math.hypot(a.x - b.x, a.y - b.y);
|
|
96
|
+
const midpoint = (a, b) => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 });
|
|
97
|
+
|
|
98
|
+
// Two pointers against the pinch's +start+ ({a, b, view}): the view whose scale
|
|
99
|
+
// follows their spread (through +limit+, a scale => scale) and whose picture
|
|
100
|
+
// point under the start's midpoint stays under the new midpoint.
|
|
101
|
+
export function pinch(start, a, b, limit = (s) => s) {
|
|
102
|
+
const from = distance(start.a, start.b);
|
|
103
|
+
const ratio = from > 0 ? distance(a, b) / from : 1;
|
|
104
|
+
const scale = limit(start.view.scale * ratio);
|
|
105
|
+
const was = midpoint(start.a, start.b);
|
|
106
|
+
const now = midpoint(a, b);
|
|
107
|
+
const k = scale / start.view.scale;
|
|
108
|
+
return { scale, x: now.x - (was.x - start.view.x) * k, y: now.y - (was.y - start.view.y) * k };
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// A wheel event's move in px and its zoom factor: lines (deltaMode 1) are 16 px,
|
|
112
|
+
// pages (2) 400; the factor is exp(-deltaY × 0.01), each event's capped.
|
|
113
|
+
export function wheelStep(event) {
|
|
114
|
+
const unit = event.deltaMode === 1 ? LINE_PX : event.deltaMode === 2 ? PAGE_PX : 1;
|
|
115
|
+
let dx = (event.deltaX || 0) * unit;
|
|
116
|
+
let dy = (event.deltaY || 0) * unit;
|
|
117
|
+
// Shift + a mouse wheel moves sideways.
|
|
118
|
+
if (event.shiftKey && !dx) [dx, dy] = [dy, 0];
|
|
119
|
+
const capped = Math.max(-WHEEL_ZOOM_MAX_DELTA, Math.min(WHEEL_ZOOM_MAX_DELTA, (event.deltaY || 0) * unit));
|
|
120
|
+
return { dx, dy, zoom: Math.exp(-capped * WHEEL_ZOOM) };
|
|
121
|
+
}
|
|
@@ -67,7 +67,7 @@ const since = (iso, now) => {
|
|
|
67
67
|
};
|
|
68
68
|
|
|
69
69
|
// The chip of +group+: the first state that applies.
|
|
70
|
-
// - answers-only: no inbox leg (
|
|
70
|
+
// - answers-only: no inbox leg (chi can't send);
|
|
71
71
|
// - cant-send: the newest send failed (the transport refused it);
|
|
72
72
|
// - waiting: the newest validate or question send has no answer (its age the send's);
|
|
73
73
|
// - answered: the outbox has a text the agent hasn't read (its age the text's), with the dot;
|
|
@@ -86,7 +86,7 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
|
|
|
86
86
|
if (!inbox) {
|
|
87
87
|
state = "answers-only";
|
|
88
88
|
words = "answers only";
|
|
89
|
-
why = "
|
|
89
|
+
why = "no inbox attached here: it answers, chi can't send to it";
|
|
90
90
|
} else if (cantSend(group, exchanges)) {
|
|
91
91
|
state = "cant-send";
|
|
92
92
|
words = "can't send";
|
|
@@ -135,7 +135,7 @@ export function peerPopoverTemplate(group, exchanges = [], { text = null, status
|
|
|
135
135
|
const all = sends(exchanges);
|
|
136
136
|
const lastSent = all.find((ex) => ex.state !== "failed");
|
|
137
137
|
const sendsLine = !inbox
|
|
138
|
-
? "can't:
|
|
138
|
+
? "can't: no inbox attached here"
|
|
139
139
|
: cantSend(group, exchanges)
|
|
140
140
|
? `the last send didn't go through: pair again from ${group.label}`
|
|
141
141
|
: [`to its inbox (${group.inbox.name})`, lastSent ? `last sent ${wallClock(lastSent.at, now)}` : ""].filter(Boolean).join(" · ");
|
|
@@ -37,6 +37,7 @@ export function createPeers({ sessionId, hasPeer = () => false, onChange = () =>
|
|
|
37
37
|
win = globalThis, tickMs = WAITING_TICK_MS } = {}) {
|
|
38
38
|
// The last read: {id: the session it is for, peers}.
|
|
39
39
|
let data = null;
|
|
40
|
+
let inFlight = null;
|
|
40
41
|
let tick = null;
|
|
41
42
|
// Only the newest read for the open session is kept (latest.js).
|
|
42
43
|
const latest = createLatest(() => sessionId());
|
|
@@ -55,25 +56,35 @@ export function createPeers({ sessionId, hasPeer = () => false, onChange = () =>
|
|
|
55
56
|
else if (tick === null && win.setInterval) tick = win.setInterval(() => refresh(), tickMs);
|
|
56
57
|
}
|
|
57
58
|
|
|
58
|
-
|
|
59
|
+
function refresh({ reuse = false } = {}) {
|
|
59
60
|
const id = sessionId();
|
|
60
|
-
if (!id || !hasPeer()) return false;
|
|
61
|
+
if (!id || !hasPeer()) return Promise.resolve(false);
|
|
62
|
+
if (reuse && inFlight?.id === id) return inFlight.promise;
|
|
63
|
+
if (reuse && data?.id === id) return Promise.resolve(true);
|
|
61
64
|
const fresh = latest.begin();
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
65
|
+
const request = (async () => {
|
|
66
|
+
try {
|
|
67
|
+
const answer = await list(id);
|
|
68
|
+
if (!fresh()) return false;
|
|
69
|
+
data = { id, peers: Array.isArray(answer?.peers) ? answer.peers : [] };
|
|
70
|
+
} catch {
|
|
71
|
+
// Unreachable for now: what was read stays.
|
|
72
|
+
if (!fresh()) return false;
|
|
73
|
+
}
|
|
74
|
+
syncTick();
|
|
75
|
+
onChange();
|
|
76
|
+
return true;
|
|
77
|
+
})();
|
|
78
|
+
const flight = { id, promise: request };
|
|
79
|
+
inFlight = flight;
|
|
80
|
+
const clearFlight = () => { if (inFlight === flight) inFlight = null; };
|
|
81
|
+
request.then(clearFlight, clearFlight);
|
|
82
|
+
return request;
|
|
73
83
|
}
|
|
74
84
|
|
|
75
|
-
// The newest exchange of send +id
|
|
76
|
-
//
|
|
85
|
+
// The newest exchange of send +id+, with its peer's name; null when
|
|
86
|
+
// none is read. Each send has its own id now; an older chi's resend
|
|
87
|
+
// shared one, and the newest of them wins.
|
|
77
88
|
function exchange(id) {
|
|
78
89
|
if (!id) return null;
|
|
79
90
|
let found = null;
|
|
@@ -103,6 +114,7 @@ export function createPeers({ sessionId, hasPeer = () => false, onChange = () =>
|
|
|
103
114
|
function clear() {
|
|
104
115
|
latest.drop();
|
|
105
116
|
data = null;
|
|
117
|
+
inFlight = null;
|
|
106
118
|
stopTick();
|
|
107
119
|
}
|
|
108
120
|
|
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
// The picture viewer (#lightbox): an attached image, or a drawn diagram
|
|
2
|
+
// (block_host.js), over the whole page, fitted at first, with zoom and pan.
|
|
3
|
+
// The maths is pan_zoom.js's; this is the DOM and the gestures
|
|
4
|
+
// (docs/internals/web-frontend.md, Picture viewer).
|
|
5
|
+
//
|
|
6
|
+
// - Zoom is the picture's layout size (its box's width and height in px) and a
|
|
7
|
+
// 2D translate its place: an SVG <img> is drawn again at its layout size, a
|
|
8
|
+
// scaled one can be a blurred bitmap. No will-change, no 3D transform, no
|
|
9
|
+
// animation: a composited layer at the largest size is hundreds of MB.
|
|
10
|
+
// - One pointer drags, two pinch; plain wheel moves, Ctrl/Cmd + wheel (a
|
|
11
|
+
// Chromium trackpad pinch) zooms at the cursor. Safari's gesture events are
|
|
12
|
+
// always prevented and applied only with no touch pointer down (a Mac
|
|
13
|
+
// trackpad): iOS fires them for a finger pinch too, beside the pointers.
|
|
14
|
+
// - A double tap or click goes close, or back to fit when already closer.
|
|
15
|
+
// - Closing is ✕ and Esc only; the keys are on the viewer's element.
|
|
16
|
+
//
|
|
17
|
+
// The picture and its place are one positioned box (.pv-pic), so a later
|
|
18
|
+
// overlay on the picture can share it.
|
|
19
|
+
//
|
|
20
|
+
// +picture+: {url, width, height, alt}; width and height may be absent.
|
|
21
|
+
// A module of its own: its exports are new (docs/internals/web-frontend.md,
|
|
22
|
+
// "New exports go in new modules").
|
|
23
|
+
|
|
24
|
+
import { html, render } from "./vendor/lit-html.js";
|
|
25
|
+
import { focusBack, modalDialog } from "./popover_modal.js";
|
|
26
|
+
import { BAR_ROOM, clampScale, clampView, fitScale, fitView, pinch, wheelStep, zoomAt } from "./pan_zoom.js";
|
|
27
|
+
|
|
28
|
+
const LABEL = "Picture viewer";
|
|
29
|
+
const STEP = 1.25;
|
|
30
|
+
const ARROW_PX = 60;
|
|
31
|
+
const TAP_MS = 300;
|
|
32
|
+
const TAP_SLOP = 4;
|
|
33
|
+
// A double tap goes this close to the fit, or to natural size if that is closer.
|
|
34
|
+
const CLOSE_FACTOR = 2.5;
|
|
35
|
+
|
|
36
|
+
const icon = (d) => html`<svg viewBox="0 0 16 16" width="16" height="16" aria-hidden="true"><path d=${d} fill="none" stroke="currentColor" stroke-width="1.6" stroke-linecap="round"/></svg>`;
|
|
37
|
+
|
|
38
|
+
// A thumbnail's picture: its size once loaded, so most opens don't wait for
|
|
39
|
+
// a decode.
|
|
40
|
+
export function thumbPicture(img) {
|
|
41
|
+
const sized = img.complete && img.naturalWidth > 0;
|
|
42
|
+
return { url: img.src, alt: img.alt, width: sized ? img.naturalWidth : null, height: sized ? img.naturalHeight : null };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// The safe areas, as #lightbox's padding resolves them (index.html: --safe-*).
|
|
46
|
+
function paddingInsets(el, win) {
|
|
47
|
+
const s = win.getComputedStyle?.(el);
|
|
48
|
+
const px = (v) => parseFloat(v) || 0;
|
|
49
|
+
return s ? { t: px(s.paddingTop), r: px(s.paddingRight), b: px(s.paddingBottom), l: px(s.paddingLeft) } : { t: 0, r: 0, b: 0, l: 0 };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// @param el the #lightbox element (index.html keeps its <img> for an older
|
|
53
|
+
// cached app.js, which reads it; one is added when it is missing)
|
|
54
|
+
// The rest are seams for the specs: +measure+ the viewer's box, +insets+ its
|
|
55
|
+
// safe areas, +decode+ an image of unknown size, +now+ the clock of a double tap.
|
|
56
|
+
// Nothing here touches layout, PointerEvent or ResizeObserver until an open.
|
|
57
|
+
// @return {open(picture, {opener}), close(), isOpen()}
|
|
58
|
+
export function createPictureViewer({
|
|
59
|
+
el, doc = document, win = window, measure = () => el.getBoundingClientRect(), insets = () => paddingInsets(el, win),
|
|
60
|
+
decode = (img) => img.decode(), now = Date.now,
|
|
61
|
+
}) {
|
|
62
|
+
const img = el.querySelector("img") || el.appendChild(doc.createElement("img"));
|
|
63
|
+
img.draggable = false;
|
|
64
|
+
el.tabIndex = -1;
|
|
65
|
+
modalDialog(el, { label: LABEL, isOpen: () => isOpen(), doc });
|
|
66
|
+
|
|
67
|
+
let drawn = false;
|
|
68
|
+
let box = null;
|
|
69
|
+
let pct = null;
|
|
70
|
+
let picture = null;
|
|
71
|
+
let view = { scale: 1, x: 0, y: 0 };
|
|
72
|
+
let atFit = true;
|
|
73
|
+
let opener = null;
|
|
74
|
+
let opening = 0;
|
|
75
|
+
const pointers = new Map();
|
|
76
|
+
let pinchStart = null;
|
|
77
|
+
let gesture = null;
|
|
78
|
+
let down = null;
|
|
79
|
+
let lastTap = null;
|
|
80
|
+
|
|
81
|
+
const isOpen = () => !el.hidden;
|
|
82
|
+
|
|
83
|
+
function stage() {
|
|
84
|
+
const rect = measure();
|
|
85
|
+
return { width: rect.width, height: rect.height, insets: insets(), bar: BAR_ROOM };
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const fit = () => fitScale(picture, stage());
|
|
89
|
+
const limit = (scale) => clampScale(scale, { fit: fit(), picture });
|
|
90
|
+
|
|
91
|
+
function paint() {
|
|
92
|
+
if (!picture) return;
|
|
93
|
+
view = clampView(view, picture, stage());
|
|
94
|
+
box.style.width = `${picture.width * view.scale}px`;
|
|
95
|
+
box.style.height = `${picture.height * view.scale}px`;
|
|
96
|
+
box.style.transform = `translate(${view.x}px, ${view.y}px)`;
|
|
97
|
+
pct.textContent = `${Math.round(view.scale * 100)}%`;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function toFit() {
|
|
101
|
+
if (!picture) return;
|
|
102
|
+
view = fitView(picture, stage());
|
|
103
|
+
atFit = true;
|
|
104
|
+
paint();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function zoomTo(scale, point) {
|
|
108
|
+
if (!picture) return;
|
|
109
|
+
view = zoomAt(view, limit(scale), point);
|
|
110
|
+
atFit = false;
|
|
111
|
+
paint();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function moveBy(dx, dy) {
|
|
115
|
+
if (!picture) return;
|
|
116
|
+
view = { ...view, x: view.x + dx, y: view.y + dy };
|
|
117
|
+
atFit = false;
|
|
118
|
+
paint();
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function centre() {
|
|
122
|
+
const rect = measure();
|
|
123
|
+
return { x: rect.width / 2, y: rect.height / 2 };
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function local(e) {
|
|
127
|
+
const rect = measure();
|
|
128
|
+
return { x: e.clientX - (rect.left || 0), y: e.clientY - (rect.top || 0) };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// A double tap at +point+: close in, or back to fit when already closer.
|
|
132
|
+
function toggleClose(point) {
|
|
133
|
+
const f = fit();
|
|
134
|
+
if (view.scale > f * 1.05) toFit();
|
|
135
|
+
else zoomTo(Math.max(f * CLOSE_FACTOR, 1), point);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function draw() {
|
|
139
|
+
if (drawn) return;
|
|
140
|
+
drawn = true;
|
|
141
|
+
render(html`<div class="pv-pic">${img}</div>
|
|
142
|
+
<button type="button" class="pv-btn pv-close" aria-label="Close" title="Close (Esc)" @click=${() => close()}>${icon("M4 4l8 8M12 4l-8 8")}</button>
|
|
143
|
+
<div class="pv-bar">
|
|
144
|
+
<button type="button" class="pv-btn pv-out" aria-label="Zoom out" title="Zoom out (-)" @click=${() => zoomTo(view.scale / STEP, centre())}>${icon("M3.5 8h9")}</button>
|
|
145
|
+
<span class="pv-pct" aria-live="polite"></span>
|
|
146
|
+
<button type="button" class="pv-btn pv-in" aria-label="Zoom in" title="Zoom in (+)" @click=${() => zoomTo(view.scale * STEP, centre())}>${icon("M3.5 8h9M8 3.5v9")}</button>
|
|
147
|
+
<span class="pv-sep" aria-hidden="true"></span>
|
|
148
|
+
<button type="button" class="pv-btn pv-fit" aria-label="Fit to the screen" title="Fit (0)" @click=${() => toFit()}>Fit</button>
|
|
149
|
+
</div>`, el);
|
|
150
|
+
box = el.querySelector(".pv-pic");
|
|
151
|
+
pct = el.querySelector(".pv-pct");
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function onResize() {
|
|
155
|
+
if (atFit) toFit();
|
|
156
|
+
else paint();
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async function open(next, { opener: from = null } = {}) {
|
|
160
|
+
draw();
|
|
161
|
+
const token = ++opening;
|
|
162
|
+
opener = from;
|
|
163
|
+
pointers.clear();
|
|
164
|
+
pinchStart = null;
|
|
165
|
+
gesture = null;
|
|
166
|
+
lastTap = null;
|
|
167
|
+
picture = null;
|
|
168
|
+
box.classList.add("waiting");
|
|
169
|
+
img.alt = next.alt || "";
|
|
170
|
+
img.src = next.url;
|
|
171
|
+
if (!isOpen()) {
|
|
172
|
+
el.hidden = false;
|
|
173
|
+
win.addEventListener?.("resize", onResize);
|
|
174
|
+
}
|
|
175
|
+
el.focus({ preventScroll: true });
|
|
176
|
+
let size = next.width > 0 && next.height > 0 ? { width: next.width, height: next.height } : null;
|
|
177
|
+
if (!size) {
|
|
178
|
+
try { await decode(img); } catch (_) { /* its natural size, if any, all the same */ }
|
|
179
|
+
if (token !== opening || !isOpen()) return;
|
|
180
|
+
size = { width: img.naturalWidth || 300, height: img.naturalHeight || 150 };
|
|
181
|
+
}
|
|
182
|
+
picture = size;
|
|
183
|
+
toFit();
|
|
184
|
+
// Placed already; shown once there is something to show (WebKit draws a
|
|
185
|
+
// broken-image box while even a cached URL loads).
|
|
186
|
+
if (!img.complete) {
|
|
187
|
+
try { await decode(img); } catch (_) { /* shown all the same */ }
|
|
188
|
+
if (token !== opening || !isOpen()) return;
|
|
189
|
+
}
|
|
190
|
+
box.classList.remove("waiting");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
function close() {
|
|
194
|
+
if (!isOpen()) return;
|
|
195
|
+
opening++;
|
|
196
|
+
const back = focusBack(el, { doc, preventScroll: true });
|
|
197
|
+
el.hidden = true;
|
|
198
|
+
el.classList.remove("dragging");
|
|
199
|
+
win.removeEventListener?.("resize", onResize);
|
|
200
|
+
pointers.clear();
|
|
201
|
+
pinchStart = null;
|
|
202
|
+
gesture = null;
|
|
203
|
+
back(opener);
|
|
204
|
+
opener = null;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const onControls = (e) => e.target.closest?.(".pv-close, .pv-bar");
|
|
208
|
+
const touchDown = () => [...pointers.values()].some((p) => p.type === "touch");
|
|
209
|
+
|
|
210
|
+
el.addEventListener("pointerdown", (e) => {
|
|
211
|
+
if (onControls(e) || !picture) return;
|
|
212
|
+
if (e.pointerType === "mouse" && e.button !== 0) return;
|
|
213
|
+
try { el.setPointerCapture?.(e.pointerId); } catch (_) { /* a pointer already gone */ }
|
|
214
|
+
pointers.set(e.pointerId, { ...local(e), type: e.pointerType });
|
|
215
|
+
el.classList.add("dragging");
|
|
216
|
+
if (pointers.size === 1) down = { ...local(e), moved: false };
|
|
217
|
+
if (pointers.size === 2) {
|
|
218
|
+
const [a, b] = [...pointers.values()];
|
|
219
|
+
pinchStart = { a, b, view };
|
|
220
|
+
down.moved = true;
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
el.addEventListener("pointermove", (e) => {
|
|
225
|
+
const was = pointers.get(e.pointerId);
|
|
226
|
+
if (!was) return;
|
|
227
|
+
const at = { ...local(e), type: was.type };
|
|
228
|
+
pointers.set(e.pointerId, at);
|
|
229
|
+
if (pointers.size === 1) {
|
|
230
|
+
if (Math.abs(at.x - down.x) + Math.abs(at.y - down.y) > TAP_SLOP) down.moved = true;
|
|
231
|
+
moveBy(at.x - was.x, at.y - was.y);
|
|
232
|
+
} else if (pointers.size === 2 && pinchStart) {
|
|
233
|
+
const [a, b] = [...pointers.values()];
|
|
234
|
+
view = pinch(pinchStart, a, b, limit);
|
|
235
|
+
atFit = false;
|
|
236
|
+
paint();
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
function up(e) {
|
|
241
|
+
if (!pointers.delete(e.pointerId)) return;
|
|
242
|
+
if (pointers.size < 2) pinchStart = null;
|
|
243
|
+
if (pointers.size) return;
|
|
244
|
+
el.classList.remove("dragging");
|
|
245
|
+
if (e.type !== "pointerup" || down?.moved) { lastTap = null; return; }
|
|
246
|
+
const t = now();
|
|
247
|
+
const at = local(e);
|
|
248
|
+
if (lastTap && t - lastTap.t < TAP_MS && Math.abs(at.x - lastTap.x) + Math.abs(at.y - lastTap.y) < 30) {
|
|
249
|
+
lastTap = null;
|
|
250
|
+
toggleClose(at);
|
|
251
|
+
} else {
|
|
252
|
+
lastTap = { t, ...at };
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
el.addEventListener("pointerup", up);
|
|
256
|
+
el.addEventListener("pointercancel", up);
|
|
257
|
+
el.addEventListener("dragstart", (e) => e.preventDefault());
|
|
258
|
+
|
|
259
|
+
el.addEventListener("wheel", (e) => {
|
|
260
|
+
e.preventDefault();
|
|
261
|
+
if (!picture) return;
|
|
262
|
+
const step = wheelStep(e);
|
|
263
|
+
if (e.ctrlKey || e.metaKey) zoomTo(view.scale * step.zoom, local(e));
|
|
264
|
+
else moveBy(-step.dx, -step.dy);
|
|
265
|
+
}, { passive: false });
|
|
266
|
+
|
|
267
|
+
// Safari's gestures: always prevented (they would zoom the page), applied
|
|
268
|
+
// only for a Mac trackpad (no touch pointer down).
|
|
269
|
+
el.addEventListener("gesturestart", (e) => {
|
|
270
|
+
e.preventDefault();
|
|
271
|
+
gesture = picture && !touchDown() ? { view, point: local(e) } : null;
|
|
272
|
+
});
|
|
273
|
+
el.addEventListener("gesturechange", (e) => {
|
|
274
|
+
e.preventDefault();
|
|
275
|
+
if (!gesture || touchDown() || !picture) return;
|
|
276
|
+
view = zoomAt(gesture.view, limit(gesture.view.scale * e.scale), gesture.point);
|
|
277
|
+
atFit = false;
|
|
278
|
+
paint();
|
|
279
|
+
});
|
|
280
|
+
el.addEventListener("gestureend", (e) => {
|
|
281
|
+
e.preventDefault();
|
|
282
|
+
gesture = null;
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
el.addEventListener("keydown", (e) => {
|
|
286
|
+
const k = e.key;
|
|
287
|
+
const plain = !e.ctrlKey && !e.metaKey && !e.altKey;
|
|
288
|
+
let done = true;
|
|
289
|
+
if (k === "Escape") close();
|
|
290
|
+
else if (plain && (k === "+" || k === "=")) zoomTo(view.scale * STEP, centre());
|
|
291
|
+
else if (plain && (k === "-" || k === "_")) zoomTo(view.scale / STEP, centre());
|
|
292
|
+
else if (plain && k === "0") toFit();
|
|
293
|
+
else if (plain && k === "ArrowLeft") moveBy(ARROW_PX, 0);
|
|
294
|
+
else if (plain && k === "ArrowRight") moveBy(-ARROW_PX, 0);
|
|
295
|
+
else if (plain && k === "ArrowUp") moveBy(0, ARROW_PX);
|
|
296
|
+
else if (plain && k === "ArrowDown") moveBy(0, -ARROW_PX);
|
|
297
|
+
else done = false;
|
|
298
|
+
if (!done) return;
|
|
299
|
+
e.preventDefault();
|
|
300
|
+
e.stopPropagation();
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
return { open, close, isOpen };
|
|
304
|
+
}
|
|
@@ -50,8 +50,9 @@ export function modalDialog(pop, { label = null, isOpen, doc = document }) {
|
|
|
50
50
|
|
|
51
51
|
// Called before +pop+ closes: whether the focus was in it (or +refocus+:
|
|
52
52
|
// an Esc), and so goes back(to) its opener once it has closed.
|
|
53
|
+
// +preventScroll+: the opener takes the focus without scrolling to it.
|
|
53
54
|
// @return (to) => focuses +to+ when it should and still can
|
|
54
|
-
export function focusBack(pop, { refocus = false, doc = document } = {}) {
|
|
55
|
+
export function focusBack(pop, { refocus = false, doc = document, preventScroll = false } = {}) {
|
|
55
56
|
const had = refocus || pop.contains(doc.activeElement);
|
|
56
|
-
return (to) => { if (had && to?.isConnected) to.focus?.(); };
|
|
57
|
+
return (to) => { if (had && to?.isConnected) to.focus?.(preventScroll ? { preventScroll: true } : undefined); };
|
|
57
58
|
}
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
// peerLinks the peer exchange's slots (peer_links.js): filled after a
|
|
27
27
|
// read is drawn, reset with the session; null: none
|
|
28
28
|
// markdown markdown_place.js (linkTextRefs, applyFinalMarkdown)
|
|
29
|
+
// pictures the picture viewer (picture_viewer.js), closed with the session; null: none
|
|
29
30
|
// capabilities() the page's capabilities (card.js shownElsewhere, the history's cards)
|
|
30
31
|
// page: {setReviseAnchor(anchor), renderInitRow(), setMarkdownWarning(warning),
|
|
31
32
|
// setCapabilities(capabilities, editorUrl, viewer), findInTurnRoots(selector), appendToHistory(el),
|
|
@@ -59,6 +60,7 @@ export function createSessionView(deps) {
|
|
|
59
60
|
store, historyEl, getSession,
|
|
60
61
|
liveTurn, turnDom, questionCards, shownTexts, objectives, featureGates, markdownTip, workingSet = null, wsPreview = null, gapClock,
|
|
61
62
|
bubbles, cards, markdown, capabilities, page, later, cardRegistry = null, stageMissing = null, peerLinks = null,
|
|
63
|
+
pictures = null,
|
|
62
64
|
} = deps;
|
|
63
65
|
const { makeUserBubble, addEndBubble, showGuardrailWarning, showRecap } = bubbles;
|
|
64
66
|
const { placeSnapshotCards, upsertCard } = cards;
|
|
@@ -111,6 +113,8 @@ export function createSessionView(deps) {
|
|
|
111
113
|
// The reader and the peek are the session's: another one (or none, the
|
|
112
114
|
// start page) never finds them over #center, the composer inert under it.
|
|
113
115
|
if (!keepVerify) wsPreview?.reset();
|
|
116
|
+
// The picture viewer holds a picture of the session left; the same one again keeps it.
|
|
117
|
+
if (!keepVerify) pictures?.close();
|
|
114
118
|
applyPrefs();
|
|
115
119
|
}
|
|
116
120
|
|
|
@@ -10,9 +10,11 @@
|
|
|
10
10
|
// write: a write of a file is an edit), at the first row that brought it in
|
|
11
11
|
// as ordinals ({turn, step, row}: the turn's place in the page's turns, the
|
|
12
12
|
// generation's in the turn, the row's in the generation; never a row key,
|
|
13
|
-
// which differs live and reloaded),
|
|
13
|
+
// which differs live and reloaded), last the newest row that touched it
|
|
14
|
+
// (the same ordinals: the list's order and its session step tag,
|
|
15
|
+
// ws_order.js), count the rows that touched it, runs
|
|
14
16
|
// each row's tool id with its at ([{id, at}], rows with an id only). A later
|
|
15
|
-
// touch unions its ops and
|
|
17
|
+
// touch unions its ops, bumps its count and moves its last, never its at: within a turn
|
|
16
18
|
// the set is append-only, which keeps the fold stable. state is "live" from
|
|
17
19
|
// the rows; the server's projection (GET working_set, :working_set_updated:
|
|
18
20
|
// WorkingSet) overlays its state (live, stale, forgotten, dropped) and
|
|
@@ -147,6 +149,8 @@ export function itemsOfTurn(turn, turnOrdinal) {
|
|
|
147
149
|
byKey.set(id, item);
|
|
148
150
|
items.push(item);
|
|
149
151
|
}
|
|
152
|
+
// Its newest touch so far (the turn's last, once the walk ends).
|
|
153
|
+
item.last = at;
|
|
150
154
|
// The row's tool id and place: the timeline's step tags and "go to
|
|
151
155
|
// the call" (ws_timeline.js placeOf); a legacy row has none.
|
|
152
156
|
if (row.tool_id) item.runs = [...(item.runs || []), { id: row.tool_id, at }];
|
|
@@ -157,7 +161,7 @@ export function itemsOfTurn(turn, turnOrdinal) {
|
|
|
157
161
|
}
|
|
158
162
|
|
|
159
163
|
// The session's set: every turn's items merged (the first entry's at kept,
|
|
160
|
-
// ops unioned, counts summed), the preloaded memories first (tag
|
|
164
|
+
// the latest list's last, ops unioned, counts summed), the preloaded memories first (tag
|
|
161
165
|
// "prompt"), the used memories no row explains (a turn without its saved
|
|
162
166
|
// rows) after them; the muted memories listed apart, never counted.
|
|
163
167
|
// @param turns the finished turns' item lists, in order
|
|
@@ -184,6 +188,8 @@ export function sessionItems({ turns = [], live = [], preloaded = [], notes = []
|
|
|
184
188
|
found.count += it.count;
|
|
185
189
|
if (it.runs) found.runs = [...(found.runs || []), ...it.runs];
|
|
186
190
|
if (!found.at) found.at = it.at;
|
|
191
|
+
// Lists come in turn order, the live turn's last: the latest wins.
|
|
192
|
+
if (it.last) found.last = it.last;
|
|
187
193
|
}
|
|
188
194
|
}
|
|
189
195
|
}
|