@parley-im/parley 0.2.3 → 0.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -96,7 +96,7 @@ Each reply records which model answered, on which login (`auth: "none"` means yo
96
96
  | `src/mcp.ts` | `create_session`, `list_sessions`, `get_transcript`, `set_reply_limit`, `end_session` |
97
97
  | `src/db.ts` | Each project's `.parley/parley.db` (sessions with keys, messages, turns), a folder per session with screenshots and `transcript.md`, and `~/.parley/projects.json` listing the projects |
98
98
  | `web/` | Chat page, session view, control panel; parley.im serves the guest page's files from here too |
99
- | `relay/` | parley.im: Worker, a Durable Object per linked computer, D1; the front page's animation is `web/home.js` |
99
+ | `relay/` | parley.im: Worker, a Durable Object per linked computer, D1 |
100
100
 
101
101
  ## Setup
102
102
 
@@ -122,7 +122,7 @@ Conversations live with their project, in `<project>/.parley/`: a database, and
122
122
 
123
123
  ## Running parley.im
124
124
 
125
- `relay/` is a Cloudflare Worker (`wrangler.jsonc`) with a Durable Object per linked computer and a D1 database (`migrations/`). Signed out, `/` is a wordless animation; signed in, it's your dashboard (open sessions, linked computers and whether they're online). `/docs` is the install guide; `/admin` is for admins (`ADMIN_EMAILS`): invites, the waitlist and who's in. Email goes through Cloudflare Email Service (the `EMAIL` binding, from `hello@parley.im`): a note when someone joins the waitlist (to them and the admins), when they're let in, and invites. Rate limits use the `LIMITS` binding. Secrets: `SESSION_SECRET` (random), `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` (a Google OAuth web client with redirect URI `https://parley.im/auth/google/callback`).
125
+ `relay/` is a Cloudflare Worker (`wrangler.jsonc`) with a Durable Object per linked computer and a D1 database (`migrations/`). Signed out, `/` explains parley in plain words; signed in, it's your dashboard (open sessions, linked computers and whether they're online). `/docs` is the install guide; `/admin` is for admins (`ADMIN_EMAILS`): invites, the waitlist and who's in. Email goes through Cloudflare Email Service (the `EMAIL` binding, from `hello@parley.im`): a note when someone joins the waitlist (to them and the admins), when they're let in, and invites. Rate limits use the `LIMITS` binding. Secrets: `SESSION_SECRET` (random), `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` (a Google OAuth web client with redirect URI `https://parley.im/auth/google/callback`).
126
126
 
127
127
  ```bash
128
128
  cd relay
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@parley-im/parley",
3
- "version": "0.2.3",
3
+ "version": "0.2.4",
4
4
  "description": "Send someone a link. They talk to a read-only Claude or Codex in your project, on your computer, and you get everything they said.",
5
5
  "keywords": [
6
6
  "claude",
package/web/app.css CHANGED
@@ -76,6 +76,8 @@ button, input, textarea, select { font: inherit; color: inherit; }
76
76
  .crumbs .slash { color: var(--faint); }
77
77
  .brand { display: inline-flex; align-items: center; gap: 10px; font-weight: 600; color: inherit; text-decoration: none !important; }
78
78
  .brand:hover .mark { filter: brightness(1.1); }
79
+ .lost .own { margin-top: 18px; font-size: 13px; color: var(--faint); }
80
+ .lost .own a { color: var(--accent); }
79
81
  .crumbs .here { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
80
82
  .crumbs a { text-decoration: none; }
81
83
  .crumbs a:hover { text-decoration: underline; }
@@ -431,15 +433,26 @@ body.panel { overflow: auto; }
431
433
  /* Long details shorten instead of pushing the buttons off a phone screen. */
432
434
  .trow.site-row .who-cell { min-width: 0; }
433
435
 
434
- /* parley.im's front page: no words, just home.js drawing on a faint grid. */
435
- body.home { overflow: hidden; }
436
- .home .topbar { position: relative; z-index: 1; background: transparent; border-bottom-color: transparent; }
437
- .stage { position: fixed; inset: 0; }
438
- .stage::before {
439
- content: ""; position: absolute; inset: 0;
440
- background-image: radial-gradient(circle, var(--line-strong) 0.9px, transparent 1.4px);
441
- background-size: 22px 22px; background-position: center;
442
- -webkit-mask-image: radial-gradient(ellipse at center, #000 15%, transparent 72%);
443
- mask-image: radial-gradient(ellipse at center, #000 15%, transparent 72%);
436
+ /* parley.im's front page: what it is, in plain words. */
437
+ .landing { max-width: 980px; }
438
+ .intro { padding: 9vh 0 8px; max-width: 640px; }
439
+ .intro h1 { font-size: clamp(32px, 6vw, 46px); line-height: 1.08; letter-spacing: -0.03em; margin: 0 0 16px; }
440
+ .intro .lede { font-size: 18px; line-height: 1.5; color: var(--muted); margin: 0; }
441
+ .intro .cta { display: flex; gap: 8px; flex-wrap: wrap; margin: 24px 0 10px; }
442
+ .intro .fine { font-size: 13px; color: var(--faint); margin: 0; }
443
+ .how { list-style: none; counter-reset: step; padding: 0; margin: 56px 0 0; display: grid; gap: 14px; grid-template-columns: repeat(3, 1fr); }
444
+ .how li { counter-increment: step; border: 1px solid var(--line); border-radius: 14px; padding: 18px; background: var(--panel); }
445
+ .how li::before {
446
+ content: counter(step); display: grid; place-items: center; width: 26px; height: 26px; margin-bottom: 12px;
447
+ border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 600; font-size: 13px;
448
+ }
449
+ .how h2 { font-size: 16px; margin: 0 0 6px; letter-spacing: -0.01em; }
450
+ .how p { margin: 0; font-size: 14px; color: var(--muted); }
451
+ .how q { color: var(--text); }
452
+ .promises { display: grid; grid-template-columns: repeat(3, 1fr); gap: 24px; margin: 44px 0 0; padding: 28px 0 40px; border-top: 1px solid var(--line); }
453
+ .promises h3 { font-size: 14px; margin: 0 0 4px; }
454
+ .promises p { font-size: 14px; color: var(--muted); margin: 0; }
455
+ @media (max-width: 720px) {
456
+ .how, .promises { grid-template-columns: 1fr; }
457
+ .intro { padding-top: 6vh; }
444
458
  }
445
- .stage canvas { position: absolute; inset: 0; display: block; }
package/web/chat.html CHANGED
@@ -17,9 +17,11 @@
17
17
  <body>
18
18
  <div class="app">
19
19
  <header class="topbar">
20
- <span class="mark" aria-hidden="true"><svg viewBox="4 4 24 24"><g fill="currentColor" stroke="none"><circle cx="8" cy="10" r="2.2"/><rect x="12" y="8" width="12" height="4" rx="2"/><rect x="6" y="14" width="20" height="4" rx="2" opacity=".7"/><rect x="6" y="20" width="13" height="4" rx="2" opacity=".7"/></g></svg></span>
21
20
  <nav class="crumbs" id="crumbs">
22
- <span class="product">parley</span><span class="slash">/</span><span class="here" id="project">&nbsp;</span>
21
+ <!-- What parley is, in a new tab so the conversation stays put. -->
22
+ <a class="brand" id="home-link" href="https://parley.im" target="_blank" rel="noopener" title="parley: let people talk to your agent">
23
+ <span class="mark" aria-hidden="true"><svg viewBox="4 4 24 24"><g fill="currentColor" stroke="none"><circle cx="8" cy="10" r="2.2"/><rect x="12" y="8" width="12" height="4" rx="2"/><rect x="6" y="14" width="20" height="4" rx="2" opacity=".7"/><rect x="6" y="20" width="13" height="4" rx="2" opacity=".7"/></g></svg></span><span class="product">parley</span>
24
+ </a><span class="slash">/</span><span class="here" id="project">&nbsp;</span>
23
25
  </nav>
24
26
  <span class="status" id="status" hidden><span class="dot" id="status-dot"></span><span id="status-text"></span></span>
25
27
  <span class="chip" id="chip" hidden></span>
package/web/chat.js CHANGED
@@ -1,5 +1,8 @@
1
1
  (function () {
2
2
  const { base, owner, maxText, relay, sender, project: knownProject } = window.PARLEY;
3
+ // parley's own homepage: this site on parley.im links, parley.im on local ones.
4
+ const homeUrl = relay ? "/" : "https://parley.im";
5
+ document.getElementById("home-link")?.setAttribute("href", homeUrl);
3
6
  const $ = (id) => document.getElementById(id);
4
7
  const els = {
5
8
  project: $("project"), crumbs: $("crumbs"), status: $("status"), statusDot: $("status-dot"), statusText: $("status-text"),
@@ -265,7 +268,8 @@
265
268
  els.status.hidden = false;
266
269
  let dot = "on";
267
270
  let label = owner ? `${agentName()} · live` : speaker();
268
- if (offline) (dot = "off"), (label = offlineWhy === "connection" ? "reconnecting" : "offline");
271
+ if (idle) (dot = ""), (label = "paused");
272
+ else if (offline) (dot = "off"), (label = offlineWhy === "connection" ? "reconnecting" : "offline");
269
273
  // A reply in progress shows as typing dots in the conversation; the
270
274
  // header stays put.
271
275
  else if (live && owner) (dot = "busy"), (label = `${agentName()} is replying`);
@@ -415,11 +419,15 @@
415
419
  }
416
420
 
417
421
  function showEnded() {
418
- showNote("This parley has ended", "Thanks for the feedback. The conversation isn't available here anymore.");
422
+ // For the guest, one quiet line about having their own.
423
+ const own = () =>
424
+ owner ? null : el("p", { className: "own" }, "Want people to talk to your agent? ", el("a", { href: homeUrl, target: "_blank", rel: "noopener", textContent: "Get parley" }));
425
+ showNote("This parley has ended", "Thanks for the feedback. The conversation isn't available here anymore.", own);
419
426
  }
420
427
 
421
- // Replaces the conversation with a note, for good.
422
- function showNote(title, text) {
428
+ // Replaces the conversation with a note, for good. `extra` makes anything
429
+ // to show under it.
430
+ function showNote(title, text, extra = () => null) {
423
431
  if (ended) return;
424
432
  ended = true;
425
433
  clearLive();
@@ -427,7 +435,7 @@
427
435
  els.status.hidden = true;
428
436
  els.chip.hidden = true;
429
437
  els.layout.textContent = "";
430
- els.layout.append(el("div", { className: "lost inline", style: "flex:1" }, el("div", {}, markEl(), el("h1", { textContent: title }), el("p", { textContent: text }))));
438
+ els.layout.append(el("div", { className: "lost inline", style: "flex:1" }, el("div", {}, markEl(), el("h1", { textContent: title }), el("p", { textContent: text }), extra())));
431
439
  }
432
440
 
433
441
  function markEl() {
@@ -716,6 +724,7 @@
716
724
  return body.ok ? p.resolve(body.result) : p.reject(problemError(body.status, body.error, body.message));
717
725
  }
718
726
  if (m.t === "evt") {
727
+ stirred = Date.now(); // a reply coming in counts as you being here
719
728
  const item = await S.open(key, token, "evt", m.d);
720
729
  if (loading) return early.push(item);
721
730
  if (item.epoch !== epoch) return load();
@@ -756,7 +765,7 @@
756
765
  for (const p of pending.values()) clearTimeout(p.timer), p.reject(connectionError());
757
766
  pending.clear();
758
767
  if (event.code === 4410) return h.ended();
759
- if (ended) return;
768
+ if (ended || paused) return;
760
769
  failures++;
761
770
  // This page lost its own connection (a phone switching networks, or
762
771
  // waking up); the sender's computer may be fine.
@@ -767,11 +776,42 @@
767
776
 
768
777
  // Back from the background or back online: reconnect now, not on a timer.
769
778
  const wake = () => {
770
- if (!ended && document.visibilityState === "visible" && retry) connect();
779
+ if (!ended && !paused && document.visibilityState === "visible" && retry) connect();
771
780
  };
772
781
  document.addEventListener("visibilitychange", wake);
773
782
  window.addEventListener("online", wake);
774
783
 
784
+ // A page left open and untouched lets its connection go (after 15
785
+ // minutes, or 5 in a background tab), so a forgotten tab doesn't hold on
786
+ // forever or show as "here" on the sender's dashboard. Anything you do
787
+ // brings it back, caught up.
788
+ const IDLE_MS = 15 * 60_000;
789
+ const HIDDEN_MS = 5 * 60_000;
790
+ let stirred = Date.now();
791
+ let hiddenAt = null;
792
+ let paused = false;
793
+ const stir = () => {
794
+ stirred = Date.now();
795
+ if (paused && !ended) {
796
+ paused = false;
797
+ h.idle(false);
798
+ connect();
799
+ }
800
+ };
801
+ for (const type of ["pointerdown", "keydown", "touchstart", "wheel", "focusin"]) addEventListener(type, stir, { passive: true, capture: true });
802
+ document.addEventListener("visibilitychange", () => {
803
+ hiddenAt = document.hidden ? Date.now() : null;
804
+ if (!document.hidden) stir();
805
+ });
806
+ setInterval(() => {
807
+ if (paused || ended || ws?.readyState !== WebSocket.OPEN || h.busy()) return;
808
+ if ((hiddenAt && Date.now() - hiddenAt > HIDDEN_MS) || Date.now() - stirred > IDLE_MS) {
809
+ paused = true;
810
+ h.idle(true);
811
+ ws.close(1000, "idle");
812
+ }
813
+ }, 30_000);
814
+
775
815
  return {
776
816
  async start(handlers) {
777
817
  h = handlers;
@@ -833,6 +873,14 @@
833
873
  toEnd();
834
874
  }
835
875
 
876
+ // Paused: this page let its connection go while nobody was using it.
877
+ let idle = false;
878
+ function setIdle(on) {
879
+ idle = on;
880
+ paintStatus();
881
+ banner(on ? "Paused while you were away. Tap anywhere to pick up where you left off." : "");
882
+ }
883
+
836
884
  // Offline means one of two things: the sender's computer (asleep, or
837
885
  // parley isn't running), or this page's own connection.
838
886
  let offlineWhy = null;
@@ -1040,6 +1088,8 @@
1040
1088
  },
1041
1089
  event: apply,
1042
1090
  offline: setOffline,
1091
+ idle: setIdle,
1092
+ busy: () => !!live,
1043
1093
  ended: showEnded,
1044
1094
  missingKey: () =>
1045
1095
  showNote("This link is incomplete", "The part after the # is missing, and it's the key to this conversation. Ask whoever sent it to send the whole link again."),
package/web/home.js DELETED
@@ -1,552 +0,0 @@
1
- // parley.im's front page, without a word on it. An agent in the middle checks
2
- // in with people, one and then another, hears them out and writes down what
3
- // it hears. When the page fills up it boils it down, sends it off and starts
4
- // again, forever. Every line is a line of text with the letters taken out.
5
- (() => {
6
- const canvas = document.getElementById("field");
7
- const ctx = canvas && canvas.getContext("2d");
8
- if (!ctx) return;
9
-
10
- const still = matchMedia("(prefers-reduced-motion: reduce)").matches;
11
- const rand = (a, b) => a + Math.random() * (b - a);
12
- const pick = (list) => list[Math.floor(Math.random() * list.length)];
13
- const clamp = (v, a, b) => Math.max(a, Math.min(b, v));
14
- const ease = (t) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2);
15
- const toward = (v, to, rate, dt) => v + (to - v) * (1 - Math.exp(-rate * dt));
16
-
17
- // Word lengths in characters, about as often as English uses them.
18
- const LENGTHS = [1, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 8, 9, 11];
19
- function sentence(max, fill) {
20
- const limit = Math.max(3, Math.round(max * fill));
21
- const ws = [];
22
- for (let n = 0; ; ) {
23
- const w = pick(LENGTHS);
24
- if (n + w > limit) break;
25
- ws.push(w);
26
- n += w + 1;
27
- }
28
- return ws.length ? ws : [3];
29
- }
30
- const span = (ws) => ws.reduce((n, w) => n + w + 1, -1);
31
-
32
- const VISIBLE = 5; // lines a person's pane shows
33
- let W = 0, H = 0, S = 1, CW = 4, BAR = 3, GAP = 10, PAD = 10;
34
- let narrow = null, clock = 0, color = {};
35
- let cx = 0, cy = 0, doc = null, people = [], packets = [], ripples = [], lifts = [], nextAsk = 0;
36
-
37
- function readColors() {
38
- const css = getComputedStyle(document.documentElement);
39
- const v = (name) => css.getPropertyValue(name).trim();
40
- color = { bg: v("--bg"), text: v("--text"), faint: v("--faint"), line: v("--line-strong"), accent: v("--accent") };
41
- }
42
-
43
- // ---------- the cast ----------
44
-
45
- function build() {
46
- const count = narrow ? 6 : W < 1000 ? 8 : 10;
47
- people = Array.from({ length: count }, (_, i) => ({
48
- angle: -Math.PI / 2 + ((i + (narrow ? 0 : 0.5)) / count) * Math.PI * 2 + rand(-0.14, 0.14),
49
- reach: rand(0.82, 1),
50
- bend: (i % 2 ? 1 : -1) * rand(0.1, 0.22),
51
- phase: rand(0, 100),
52
- chars: narrow ? 18 : 24,
53
- lines: [],
54
- scroll: 0,
55
- state: "idle",
56
- wait: rand(0.2, 3),
57
- speed: rand(14, 30),
58
- pause: 0,
59
- plan: [],
60
- busy: 0,
61
- }));
62
- doc = { chars: narrow ? 28 : 42, max: narrow ? 7 : 9, phase: "gather", t: 0, lines: [], queue: [], wait: 0, h: 0, titleY: null, title: { w: [], tw: [] } };
63
- morph(doc.title, sentence(doc.chars * 0.55, rand(0.6, 1)));
64
- doc.title.w = doc.title.tw.slice();
65
- packets = [];
66
- ripples = [];
67
- lifts = [];
68
- nextAsk = 0.4;
69
- }
70
-
71
- function place() {
72
- const dpr = Math.min(window.devicePixelRatio || 1, 2);
73
- W = window.innerWidth;
74
- H = window.innerHeight;
75
- canvas.width = Math.round(W * dpr);
76
- canvas.height = Math.round(H * dpr);
77
- canvas.style.width = `${W}px`;
78
- canvas.style.height = `${H}px`;
79
- ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
80
-
81
- const top = (document.querySelector(".topbar")?.getBoundingClientRect().bottom ?? 0) + 6;
82
- const wasNarrow = narrow;
83
- narrow = W < 600;
84
- S = narrow ? clamp(W / 400, 0.82, 1.05) : clamp(Math.min(W, H - top) / 720, 0.82, 1.3);
85
- CW = 4 * S;
86
- BAR = 3.2 * S;
87
- GAP = 10 * S;
88
- PAD = 10 * S;
89
- if (narrow !== wasNarrow || people.length !== (narrow ? 6 : W < 1000 ? 8 : 10)) build();
90
-
91
- const x0 = 12, x1 = W - 12, y0 = top, y1 = H - 18;
92
- cx = W / 2;
93
- cy = (y0 + y1) / 2;
94
- doc.w = doc.chars * CW + PAD * 2 + 9 * S;
95
- const docBox = { x: cx, y: cy, w: doc.w, h: (doc.max + 1.7) * GAP + PAD * 2 };
96
- const pw = people[0].chars * CW + PAD * 2 + 9 * S;
97
- const ph = VISIBLE * GAP + PAD * 1.2;
98
- const ry = (y1 - y0 - ph) / 2;
99
- const rx = Math.min((x1 - x0 - pw) / 2, ry * 1.9);
100
- for (const p of people) {
101
- p.w = pw;
102
- p.h = ph;
103
- p.x = cx + Math.cos(p.angle) * rx * p.reach;
104
- p.y = cy + Math.sin(p.angle) * ry * p.reach;
105
- }
106
- // Nudge panes apart where they'd overlap each other or the notes in the middle.
107
- const margin = 12 * S;
108
- for (let k = 0; k < 80; k++) {
109
- for (const p of people) {
110
- shove(p, docBox, margin, 1);
111
- for (const q of people) if (q !== p) shove(p, q, margin, 0.5);
112
- p.x = clamp(p.x, x0 + pw / 2, x1 - pw / 2);
113
- p.y = clamp(p.y, y0 + ph / 2, y1 - ph / 2);
114
- }
115
- }
116
- }
117
-
118
- function shove(p, o, margin, share) {
119
- const dx = p.x - o.x, dy = p.y - o.y;
120
- const ox = (p.w + o.w) / 2 + margin - Math.abs(dx);
121
- const oy = (p.h + o.h) / 2 + margin - Math.abs(dy);
122
- if (ox <= 0 || oy <= 0) return;
123
- if (ox < oy) p.x += (Math.sign(dx) || 1) * ox * share;
124
- else p.y += (Math.sign(dy) || 1) * oy * share;
125
- }
126
-
127
- // A line that can be rewritten in place: its words ease toward new lengths.
128
- function morph(line, ws) {
129
- const n = Math.max(line.w.length, ws.length);
130
- line.w = Array.from({ length: n }, (_, i) => line.w[i] ?? 0);
131
- line.tw = Array.from({ length: n }, (_, i) => ws[i] ?? 0);
132
- }
133
-
134
- // ---------- what happens ----------
135
-
136
- function ask(p) {
137
- p.state = "asked";
138
- const question = sentence(Math.round(p.chars * 0.85), rand(0.45, 1));
139
- ripples.push({ t: 0 });
140
- send(p, "out", [question], () => {
141
- const count = pick([1, 2, 2, 3, 3, 4]);
142
- p.plan = Array.from({ length: count }, (_, i) => sentence(p.chars, i === count - 1 ? rand(0.3, 0.8) : rand(0.8, 1)));
143
- p.lines.push({ ws: question, shown: 0, kind: "q", sent: false, dim: 0 });
144
- p.lines.push({ ws: p.plan.shift(), shown: 0, kind: "a", sent: false, dim: 0 });
145
- p.state = "reading";
146
- p.wait = rand(0.5, 1.8);
147
- });
148
- }
149
-
150
- function send(p, dir, payload, arrive) {
151
- const d = Math.hypot(p.x - cx, p.y - cy);
152
- packets.push({ p, dir, payload, arrive, t: 0, dur: clamp(d / (230 * S), 0.8, 2.2) });
153
- }
154
-
155
- function stepPerson(p, dt) {
156
- for (const l of p.lines) {
157
- if (l.kind === "q") l.shown += 80 * dt;
158
- l.dim = toward(l.dim, l.sent ? 1 : 0, 2.5, dt);
159
- }
160
- if (p.state === "idle") p.wait -= dt;
161
- else if (p.state === "reading") {
162
- p.wait -= dt;
163
- if (p.wait <= 0) p.state = "typing";
164
- } else if (p.state === "typing") {
165
- const l = p.lines[p.lines.length - 1];
166
- if (p.pause > 0) p.pause -= dt;
167
- else {
168
- l.shown += p.speed * dt;
169
- if (Math.random() < dt * 0.8) p.pause = rand(0.15, 0.9);
170
- }
171
- if (l.shown >= span(l.ws)) {
172
- l.shown = span(l.ws);
173
- if (p.plan.length) p.lines.push({ ws: p.plan.shift(), shown: 0, kind: "a", sent: false, dim: 0 });
174
- else {
175
- p.state = "sending";
176
- p.wait = 0.4;
177
- }
178
- }
179
- } else if (p.state === "sending") {
180
- p.wait -= dt;
181
- if (p.wait <= 0) {
182
- const mine = p.lines.filter((l) => !l.sent);
183
- for (const l of mine) l.sent = true;
184
- p.state = "idle";
185
- p.wait = rand(1.5, 5);
186
- send(p, "in", mine.filter((l) => l.kind === "a").map((l) => l.ws), (payload) => hear(payload));
187
- }
188
- }
189
- const talking = p.state !== "idle" || packets.some((k) => k.p === p);
190
- p.busy = toward(p.busy, talking ? 1 : 0, 4, dt);
191
- // Scroll so the newest line shows, and forget lines long gone.
192
- p.scroll = toward(p.scroll, Math.max(0, p.lines.length - VISIBLE), 6, dt);
193
- while (p.lines.length > VISIBLE + 3 && p.scroll >= 3) {
194
- p.lines.shift();
195
- p.scroll -= 1;
196
- }
197
- }
198
-
199
- // An answer reaches the middle: a line or two goes into the notes, and
200
- // something already there may get rewritten in light of it.
201
- function hear(payload) {
202
- const d = doc;
203
- if (d.phase !== "gather") return d.queue.push(payload);
204
- const adds = Math.max(1, Math.round(payload.length * rand(0.4, 0.8)));
205
- for (let i = 0; i < adds; i++) {
206
- const ws = sentence(d.chars, rand(0.5, 1));
207
- const at = Math.random() < 0.55 ? d.lines.length : Math.floor(rand(0, d.lines.length + 1));
208
- d.lines.splice(at, 0, { w: ws.slice(), tw: ws, shown: 0, a: 0, heat: 1, y: null, keep: false, into: null });
209
- }
210
- const settled = d.lines.filter((l) => l.heat < 0.3);
211
- if (settled.length && Math.random() < 0.7) {
212
- const l = pick(settled);
213
- morph(l, sentence(d.chars, rand(0.5, 1)));
214
- l.heat = 0.6;
215
- }
216
- if (d.lines.length > d.max) condense();
217
- }
218
-
219
- // Too much to hold: everything folds into three lines.
220
- function condense() {
221
- const d = doc;
222
- const n = d.lines.length;
223
- const keys = [0, Math.floor((n - 1) / 2), n - 1];
224
- keys.forEach((i) => {
225
- const l = d.lines[i];
226
- morph(l, sentence(d.chars, rand(0.85, 1)));
227
- l.keep = true;
228
- l.heat = 1;
229
- l.shown = Infinity;
230
- });
231
- d.lines.forEach((l, i) => {
232
- if (l.keep) return;
233
- const nearest = keys.reduce((best, k) => (Math.abs(k - i) < Math.abs(best - i) ? k : best));
234
- l.into = d.lines[nearest];
235
- morph(l, []);
236
- });
237
- d.phase = "condense";
238
- d.t = 0;
239
- }
240
-
241
- function stepDoc(dt) {
242
- const d = doc;
243
- d.t += dt;
244
- for (const l of [d.title, ...d.lines]) for (let i = 0; i < l.w.length; i++) l.w[i] = toward(l.w[i], l.tw[i], 5, dt);
245
- for (const l of d.lines) {
246
- l.shown += 90 * dt;
247
- l.a = toward(l.a, l.into ? 0 : 1, l.into ? 2.5 : 6, dt);
248
- if (!l.keep) l.heat = toward(l.heat, 0, 0.8, dt);
249
- }
250
- if (d.phase === "gather") {
251
- d.wait -= dt;
252
- if (d.queue.length && d.wait <= 0) {
253
- hear(d.queue.shift());
254
- d.wait = 0.5;
255
- }
256
- } else if (d.phase === "condense" && d.t > 1.6) {
257
- d.lines = d.lines.filter((l) => !l.into);
258
- d.phase = "hold";
259
- d.t = 0;
260
- } else if (d.phase === "hold" && d.t > 1.2) {
261
- // Sent off: the summary rises out of the notes, and they start over.
262
- lifts.push({ lines: d.lines.map((l) => ({ ws: l.tw.slice(), y: l.y })), t: 0 });
263
- d.lines = [];
264
- morph(d.title, []);
265
- d.phase = "rest";
266
- d.t = 0;
267
- } else if (d.phase === "rest" && d.t > 1.4) {
268
- morph(d.title, sentence(d.chars * 0.55, rand(0.6, 1)));
269
- d.phase = "gather";
270
- d.t = 0;
271
- }
272
- const live = d.lines.filter((l) => !l.into);
273
- const block = 1.7 * GAP + live.length * GAP;
274
- const top = cy - block / 2;
275
- d.titleY = d.titleY == null ? top + GAP / 2 : toward(d.titleY, top + GAP / 2, 6, dt);
276
- live.forEach((l, i) => {
277
- const ty = top + 1.7 * GAP + GAP / 2 + i * GAP;
278
- l.y = l.y == null ? ty : toward(l.y, ty, 7, dt);
279
- });
280
- for (const l of d.lines) if (l.into) l.y = toward(l.y, l.into.y, 5, dt);
281
- d.h = d.h ? toward(d.h, block, 6, dt) : block;
282
- }
283
-
284
- function step(dt) {
285
- clock += dt;
286
- nextAsk -= dt;
287
- if (nextAsk <= 0) {
288
- nextAsk = rand(0.5, 1.6);
289
- const free = people.filter((p) => p.state === "idle" && p.wait <= 0);
290
- const busy = people.filter((p) => p.state !== "idle").length;
291
- if (doc.phase === "gather" && free.length && busy < Math.ceil(people.length * 0.6)) ask(pick(free));
292
- }
293
- for (const p of people) stepPerson(p, dt);
294
- for (const k of packets) {
295
- k.t += dt / k.dur;
296
- if (k.t >= 1) k.arrive(k.payload);
297
- }
298
- packets = packets.filter((k) => k.t < 1);
299
- for (const r of ripples) r.t += dt;
300
- ripples = ripples.filter((r) => r.t < 1);
301
- for (const l of lifts) l.t += dt;
302
- lifts = lifts.filter((l) => l.t < 2.6);
303
- stepDoc(dt);
304
- }
305
-
306
- // ---------- drawing ----------
307
-
308
- function bar(x, y, w, h) {
309
- if (ctx.roundRect) ctx.roundRect(x, y, w, h, h / 2);
310
- else ctx.rect(x, y, w, h);
311
- }
312
-
313
- // A line of words from (x, y), the first `shown` characters of it. Returns
314
- // where the typing has got to.
315
- function words(ws, x, y, shown = Infinity, k = 1, tall = 1) {
316
- const c = CW * k, h = BAR * k * tall;
317
- let left = shown, end = x;
318
- ctx.beginPath();
319
- for (const w of ws) {
320
- if (left <= 0) break;
321
- const len = Math.min(w, left);
322
- if (len > 0.05) bar(x, y - h / 2, len * c, h);
323
- end = x + len * c;
324
- x += (w + Math.min(1, w)) * c;
325
- left -= w + 1;
326
- }
327
- ctx.fill();
328
- return end;
329
- }
330
-
331
- function curve(p) {
332
- const px = p.x + Math.sin(clock * 0.23 + p.phase) * 3 * S;
333
- const py = p.y + Math.cos(clock * 0.19 + p.phase * 1.3) * 3 * S;
334
- const dx = px - cx, dy = py - cy;
335
- return { ax: cx, ay: cy, bx: (cx + px) / 2 - dy * p.bend, by: (cy + py) / 2 + dx * p.bend, px, py };
336
- }
337
- const at = (c, t) => {
338
- const u = 1 - t;
339
- return [u * u * c.ax + 2 * u * t * c.bx + t * t * c.px, u * u * c.ay + 2 * u * t * c.by + t * t * c.py];
340
- };
341
-
342
- function draw() {
343
- ctx.clearRect(0, 0, W, H);
344
- const curves = new Map(people.map((p) => [p, curve(p)]));
345
-
346
- // Threads from the middle to everyone.
347
- ctx.lineWidth = Math.max(1, S);
348
- for (const p of people) {
349
- const c = curves.get(p);
350
- ctx.beginPath();
351
- ctx.moveTo(c.ax, c.ay);
352
- ctx.quadraticCurveTo(c.bx, c.by, c.px, c.py);
353
- ctx.setLineDash([1.2 * S, 5 * S]);
354
- ctx.globalAlpha = 0.9;
355
- ctx.strokeStyle = color.line;
356
- ctx.stroke();
357
- if (p.busy > 0.02) {
358
- ctx.setLineDash([]);
359
- ctx.globalAlpha = p.busy * 0.4;
360
- ctx.strokeStyle = color.faint;
361
- ctx.stroke();
362
- }
363
- }
364
- ctx.setLineDash([]);
365
-
366
- // Panes, cut out of the grid.
367
- ctx.fillStyle = color.bg;
368
- ctx.globalAlpha = 1;
369
- ctx.beginPath();
370
- for (const p of people) {
371
- const c = curves.get(p);
372
- bar(c.px - p.w / 2, c.py - p.h / 2, p.w, p.h);
373
- }
374
- ctx.fill();
375
- ctx.beginPath();
376
- ctx.roundRect ? ctx.roundRect(cx - doc.w / 2, cy - doc.h / 2 - PAD, doc.w, doc.h + PAD * 2, 8 * S) : ctx.rect(cx - doc.w / 2, cy - doc.h / 2 - PAD, doc.w, doc.h + PAD * 2);
377
- ctx.fill();
378
-
379
- drawDoc();
380
- for (const p of people) drawPerson(p, curves.get(p));
381
- for (const k of packets) drawPacket(k, curves.get(k.p));
382
- for (const l of lifts) drawLift(l);
383
- ctx.globalAlpha = 1;
384
- }
385
-
386
- function drawDoc() {
387
- const d = doc;
388
- const left = cx - d.w / 2 + PAD;
389
- const x = left + 9 * S;
390
- // The agent: a dot that pulses while it's thinking things over.
391
- const thinking = d.phase === "condense" || d.phase === "hold";
392
- const r = 2.8 * S * (thinking ? 1 + 0.25 * Math.sin(clock * 7) : 1);
393
- ctx.globalAlpha = 1;
394
- ctx.fillStyle = color.accent;
395
- ctx.beginPath();
396
- ctx.arc(left + 2.5 * S, d.titleY, r, 0, Math.PI * 2);
397
- ctx.fill();
398
- for (const ring of ripples) {
399
- ctx.globalAlpha = (1 - ring.t) * 0.55;
400
- ctx.strokeStyle = color.accent;
401
- ctx.lineWidth = S;
402
- ctx.beginPath();
403
- ctx.arc(left + 2.5 * S, d.titleY, 3 * S + ease(ring.t) * 16 * S, 0, Math.PI * 2);
404
- ctx.stroke();
405
- }
406
- ctx.globalAlpha = 0.95;
407
- words(d.title.w, x, d.titleY, Infinity, 1, 1.35);
408
-
409
- for (const l of d.lines) {
410
- if (l.y == null) continue;
411
- if (l.heat < 0.99) {
412
- ctx.globalAlpha = l.a * (1 - l.heat) * 0.82;
413
- ctx.fillStyle = color.text;
414
- words(l.w, x, l.y, l.shown);
415
- }
416
- if (l.heat > 0.01) {
417
- ctx.globalAlpha = l.a * l.heat;
418
- ctx.fillStyle = color.accent;
419
- words(l.w, x, l.y, l.shown);
420
- }
421
- }
422
- }
423
-
424
- function drawPerson(p, c) {
425
- const x = c.px - p.w / 2, y = c.py - p.h / 2;
426
- const top = y + PAD * 0.6 + GAP / 2;
427
- // Someone: a ring that fills in while they're talking with the agent.
428
- const dot = [x + PAD + 2.5 * S, top];
429
- ctx.globalAlpha = 1;
430
- ctx.strokeStyle = color.faint;
431
- ctx.lineWidth = Math.max(1, S);
432
- ctx.beginPath();
433
- ctx.arc(dot[0], dot[1], 2.4 * S, 0, Math.PI * 2);
434
- ctx.stroke();
435
- if (p.busy > 0.02) {
436
- ctx.globalAlpha = p.busy;
437
- ctx.fillStyle = color.text;
438
- ctx.fill();
439
- }
440
-
441
- ctx.save();
442
- ctx.beginPath();
443
- ctx.rect(x, y + PAD * 0.25, p.w, p.h - PAD * 0.5);
444
- ctx.clip();
445
- const lx = x + PAD + 9 * S;
446
- p.lines.forEach((l, j) => {
447
- const v = j - p.scroll;
448
- if (v < -1 || v > VISIBLE) return;
449
- const ly = top + v * GAP;
450
- const fade = clamp(v + 1, 0, 1);
451
- if (l.kind === "q") {
452
- ctx.globalAlpha = fade * (0.9 - 0.45 * l.dim);
453
- ctx.fillStyle = color.accent;
454
- } else {
455
- ctx.globalAlpha = fade * (0.78 - 0.45 * l.dim);
456
- ctx.fillStyle = color.text;
457
- }
458
- const end = words(l.ws, lx, ly, l.shown);
459
- const last = j === p.lines.length - 1;
460
- if (last && (p.state === "reading" || p.state === "typing")) {
461
- const blink = p.state === "reading" || p.pause > 0 ? (Math.sin(clock * 8) > -0.3 ? 0.85 : 0.1) : 0.85;
462
- ctx.globalAlpha = fade * blink;
463
- ctx.fillStyle = color.text;
464
- ctx.fillRect(l.shown > 0 ? end + 1.5 * S : lx, ly - BAR * 1.15, 1.4 * S, BAR * 2.3);
465
- }
466
- });
467
- ctx.restore();
468
- }
469
-
470
- function drawPacket(k, c) {
471
- const e = ease(clamp(k.t, 0, 1));
472
- const out = k.dir === "out";
473
- const head = out ? e : 1 - e;
474
- const tail = out ? Math.max(0, head - 0.24) : Math.min(1, head + 0.24);
475
- const fade = Math.min(1, k.t / 0.1, (1 - k.t) / 0.1);
476
- const ink = out ? color.accent : color.text;
477
- ctx.strokeStyle = ink;
478
- ctx.lineWidth = 1.3 * S;
479
- const steps = 12;
480
- for (let i = 0; i < steps; i++) {
481
- const [x0, y0] = at(c, tail + ((head - tail) * i) / steps);
482
- const [x1, y1] = at(c, tail + ((head - tail) * (i + 1)) / steps);
483
- ctx.globalAlpha = fade * ((i + 1) / steps) * 0.8;
484
- ctx.beginPath();
485
- ctx.moveTo(x0, y0);
486
- ctx.lineTo(x1, y1);
487
- ctx.stroke();
488
- }
489
- // What's being carried, in miniature.
490
- const [hx, hy] = at(c, head);
491
- const k2 = 0.55;
492
- const wide = Math.max(...k.payload.map(span)) * CW * k2;
493
- const tallness = k.payload.length * GAP * k2 * 0.8;
494
- const bx = hx - wide / 2, by = hy - tallness / 2;
495
- ctx.globalAlpha = fade * 0.9;
496
- ctx.fillStyle = color.bg;
497
- ctx.beginPath();
498
- bar(bx - 3 * S, by - 2.5 * S, wide + 6 * S, tallness + 5 * S);
499
- ctx.fill();
500
- ctx.globalAlpha = fade;
501
- ctx.fillStyle = ink;
502
- k.payload.forEach((ws, i) => words(ws, bx, by + (i + 0.5) * GAP * k2 * 0.8, Infinity, k2));
503
- }
504
-
505
- function drawLift(l) {
506
- const t = clamp(l.t / 2.6, 0, 1);
507
- const rise = ease(t) * 54 * S;
508
- const x = cx - doc.w / 2 + PAD + 9 * S;
509
- ctx.fillStyle = color.accent;
510
- ctx.globalAlpha = (1 - t) ** 1.6;
511
- for (const line of l.lines) words(line.ws, x, line.y - rise);
512
- }
513
-
514
- // ---------- running ----------
515
-
516
- let last = 0;
517
- function frame(now) {
518
- const dt = Math.min(0.05, last ? (now - last) / 1000 : 1 / 60);
519
- last = now;
520
- step(dt);
521
- draw();
522
- requestAnimationFrame(frame);
523
- }
524
-
525
- // Without motion: run it for a while and show one moment of it.
526
- function snapshot() {
527
- for (let i = 0; i < 30 * 24; i++) step(1 / 30);
528
- draw();
529
- }
530
-
531
- readColors();
532
- place();
533
- matchMedia("(prefers-color-scheme: dark)").addEventListener("change", () => {
534
- readColors();
535
- if (still) draw();
536
- });
537
- let pending = false;
538
- window.addEventListener("resize", () => {
539
- if (pending) return;
540
- pending = true;
541
- requestAnimationFrame(() => {
542
- pending = false;
543
- place();
544
- if (still) snapshot();
545
- });
546
- });
547
- if (still) snapshot();
548
- else {
549
- for (let i = 0; i < 45; i++) step(1 / 30); // start with something already moving
550
- requestAnimationFrame(frame);
551
- }
552
- })();