@songsid/agend 2.4.0-beta.20 → 2.4.0-beta.22
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/dist/agent-cli-instructions.md +1 -1
- package/dist/backend/claude-carry.d.ts +11 -0
- package/dist/backend/claude-carry.js +88 -6
- package/dist/backend/claude-carry.js.map +1 -1
- package/dist/backend/claude-code.js +3 -0
- package/dist/backend/claude-code.js.map +1 -1
- package/dist/build-provenance.json +1 -1
- package/dist/cache-ledger.d.ts +23 -21
- package/dist/cache-ledger.js +70 -6
- package/dist/cache-ledger.js.map +1 -1
- package/dist/cache-prices.d.ts +8 -6
- package/dist/cache-prices.js +12 -5
- package/dist/cache-prices.js.map +1 -1
- package/dist/cache-service.d.ts +9 -5
- package/dist/cache-service.js +202 -50
- package/dist/cache-service.js.map +1 -1
- package/dist/channel/mcp-tools.js +6 -4
- package/dist/channel/mcp-tools.js.map +1 -1
- package/dist/cli-audited.js +2 -1
- package/dist/cli-audited.js.map +1 -1
- package/dist/cli.js +4 -1
- package/dist/cli.js.map +1 -1
- package/dist/contact-auth-policy.js +1 -1
- package/dist/contact-auth-policy.js.map +1 -1
- package/dist/contact-auth-service.js +8 -2
- package/dist/contact-auth-service.js.map +1 -1
- package/dist/contact-auth-store.d.ts +3 -0
- package/dist/contact-auth-store.js +34 -8
- package/dist/contact-auth-store.js.map +1 -1
- package/dist/contact-auth-types.d.ts +3 -2
- package/dist/contact-auth-types.js.map +1 -1
- package/dist/contact-change-proposals.d.ts +47 -0
- package/dist/contact-change-proposals.js +132 -0
- package/dist/contact-change-proposals.js.map +1 -0
- package/dist/daemon.d.ts +31 -0
- package/dist/daemon.js +149 -2
- package/dist/daemon.js.map +1 -1
- package/dist/delivery-outbox.d.ts +10 -0
- package/dist/delivery-outbox.js +21 -0
- package/dist/delivery-outbox.js.map +1 -1
- package/dist/fleet-manager.d.ts +65 -1
- package/dist/fleet-manager.js +338 -20
- package/dist/fleet-manager.js.map +1 -1
- package/dist/general-knowledge/skills/concepts/SKILL.md +2 -1
- package/dist/general-knowledge/skills/hub/SKILL.md +45 -5
- package/dist/general-knowledge/skills/task-board/SKILL.md +4 -0
- package/dist/general-knowledge/skills/web-appearance/SKILL.md +1 -1
- package/dist/general-knowledge/skills/web-dashboard/SKILL.md +16 -6
- package/dist/hub/agent-pictures-model.d.ts +275 -0
- package/dist/hub/agent-pictures-model.js +103 -0
- package/dist/hub/agent-pictures-model.js.map +1 -0
- package/dist/hub/agent-pictures.d.ts +64 -0
- package/dist/hub/agent-pictures.js +255 -0
- package/dist/hub/agent-pictures.js.map +1 -0
- package/dist/hub/client.d.ts +4 -0
- package/dist/hub/client.js +23 -2
- package/dist/hub/client.js.map +1 -1
- package/dist/hub/compose/member.d.ts +14 -3
- package/dist/hub/compose/member.js +46 -10
- package/dist/hub/compose/member.js.map +1 -1
- package/dist/hub/compose/server.d.ts +6 -5
- package/dist/hub/compose/server.js +7 -3
- package/dist/hub/compose/server.js.map +1 -1
- package/dist/hub/direct-posts-model.d.ts +1 -0
- package/dist/hub/direct-posts-model.js +2 -0
- package/dist/hub/direct-posts-model.js.map +1 -0
- package/dist/hub/direct-posts.d.ts +36 -0
- package/dist/hub/direct-posts.js +145 -0
- package/dist/hub/direct-posts.js.map +1 -0
- package/dist/hub/http/compose.d.ts +5 -0
- package/dist/hub/http/compose.js +158 -20
- package/dist/hub/http/compose.js.map +1 -1
- package/dist/hub/http/core.d.ts +9 -1
- package/dist/hub/http/core.js +21 -1
- package/dist/hub/http/core.js.map +1 -1
- package/dist/hub/http/deps.d.ts +1 -0
- package/dist/hub/http/schema.js +7 -0
- package/dist/hub/http/schema.js.map +1 -1
- package/dist/hub/init/text.js +1 -1
- package/dist/hub/join/service.d.ts +2 -0
- package/dist/hub/join/service.js +7 -3
- package/dist/hub/join/service.js.map +1 -1
- package/dist/hub/member/agent-pictures.d.ts +76 -0
- package/dist/hub/member/agent-pictures.js +196 -0
- package/dist/hub/member/agent-pictures.js.map +1 -0
- package/dist/hub/member/direct-rooms.d.ts +9 -0
- package/dist/hub/member/direct-rooms.js +14 -0
- package/dist/hub/member/direct-rooms.js.map +1 -0
- package/dist/hub/member/room-ask.d.ts +22 -1
- package/dist/hub/member/room-ask.js +99 -5
- package/dist/hub/member/room-ask.js.map +1 -1
- package/dist/hub/member/room-asker-store.d.ts +48 -0
- package/dist/hub/member/room-asker-store.js +181 -0
- package/dist/hub/member/room-asker-store.js.map +1 -0
- package/dist/hub/member/room-post.d.ts +11 -10
- package/dist/hub/member/room-post.js +51 -7
- package/dist/hub/member/room-post.js.map +1 -1
- package/dist/hub/policy.d.ts +11 -2
- package/dist/hub/policy.js +60 -7
- package/dist/hub/policy.js.map +1 -1
- package/dist/hub/protocol.d.ts +254 -1
- package/dist/hub/protocol.js +20 -8
- package/dist/hub/protocol.js.map +1 -1
- package/dist/hub/representative.d.ts +38 -0
- package/dist/hub/representative.js +140 -12
- package/dist/hub/representative.js.map +1 -1
- package/dist/hub/room-asker-identity-model.d.ts +53 -0
- package/dist/hub/room-asker-identity-model.js +14 -0
- package/dist/hub/room-asker-identity-model.js.map +1 -0
- package/dist/hub/room-asker-identity.d.ts +22 -0
- package/dist/hub/room-asker-identity.js +43 -0
- package/dist/hub/room-asker-identity.js.map +1 -0
- package/dist/hub/rooms/agents.d.ts +2 -0
- package/dist/hub/rooms/agents.js +30 -3
- package/dist/hub/rooms/agents.js.map +1 -1
- package/dist/hub/rooms/asker-identity.d.ts +21 -0
- package/dist/hub/rooms/asker-identity.js +86 -0
- package/dist/hub/rooms/asker-identity.js.map +1 -0
- package/dist/hub/rooms/direct-view.d.ts +3 -2
- package/dist/hub/rooms/direct-view.js +23 -8
- package/dist/hub/rooms/direct-view.js.map +1 -1
- package/dist/hub/rooms/office.d.ts +27 -0
- package/dist/hub/rooms/office.js +12 -2
- package/dist/hub/rooms/office.js.map +1 -1
- package/dist/hub/rooms/service.d.ts +38 -1
- package/dist/hub/rooms/service.js +56 -3
- package/dist/hub/rooms/service.js.map +1 -1
- package/dist/hub/rooms/types.d.ts +5 -2
- package/dist/hub/rooms.d.ts +9 -0
- package/dist/hub/rooms.js +97 -26
- package/dist/hub/rooms.js.map +1 -1
- package/dist/hub/runtime.d.ts +1 -1
- package/dist/hub/server.js +24 -2
- package/dist/hub/server.js.map +1 -1
- package/dist/hub/session-directory-transition.d.ts +1 -0
- package/dist/hub/session-directory-transition.js +15 -9
- package/dist/hub/session-directory-transition.js.map +1 -1
- package/dist/hub/session-limits.d.ts +58 -0
- package/dist/hub/session-limits.js +121 -0
- package/dist/hub/session-limits.js.map +1 -0
- package/dist/hub/sessions.d.ts +1 -1
- package/dist/hub/sessions.js +13 -7
- package/dist/hub/sessions.js.map +1 -1
- package/dist/hub/settings-directory-decision.js +1 -1
- package/dist/hub/settings-directory-decision.js.map +1 -1
- package/dist/hub/types.d.ts +9 -2
- package/dist/hub-character-plan.d.ts +47 -0
- package/dist/hub-character-plan.js +70 -0
- package/dist/hub-character-plan.js.map +1 -0
- package/dist/hub-contact-windows.d.ts +13 -1
- package/dist/hub-contact-windows.js +35 -2
- package/dist/hub-contact-windows.js.map +1 -1
- package/dist/hub-picture-reduce.d.ts +34 -0
- package/dist/hub-picture-reduce.js +101 -0
- package/dist/hub-picture-reduce.js.map +1 -0
- package/dist/hub-picture-worker.d.ts +1 -0
- package/dist/hub-picture-worker.js +19 -0
- package/dist/hub-picture-worker.js.map +1 -0
- package/dist/hub-report-target.d.ts +6 -0
- package/dist/hub-report-target.js +6 -0
- package/dist/hub-report-target.js.map +1 -1
- package/dist/locale.js +357 -265
- package/dist/locale.js.map +1 -1
- package/dist/ls-rows.d.ts +2 -0
- package/dist/ls-rows.js.map +1 -1
- package/dist/needs-you.d.ts +6 -0
- package/dist/needs-you.js +3 -1
- package/dist/needs-you.js.map +1 -1
- package/dist/outbound-handlers.d.ts +8 -0
- package/dist/outbound-handlers.js +53 -6
- package/dist/outbound-handlers.js.map +1 -1
- package/dist/outbound-schemas.d.ts +2 -0
- package/dist/outbound-schemas.js +6 -4
- package/dist/outbound-schemas.js.map +1 -1
- package/dist/runtime-cli-install.js +4 -4
- package/dist/runtime-cli-install.js.map +1 -1
- package/dist/task-board-tool.d.ts +17 -0
- package/dist/task-board-tool.js +58 -6
- package/dist/task-board-tool.js.map +1 -1
- package/dist/tips.js +3 -3
- package/dist/tool-permissions.js +2 -2
- package/dist/tool-permissions.js.map +1 -1
- package/dist/ui/agent-export-strings.js +1 -1
- package/dist/ui/agent-page-strings.js +5 -5
- package/dist/ui/contact-auth-client.js +2 -2
- package/dist/ui/contact-tool-groups.js +1 -1
- package/dist/ui/fleet-cache.js +5 -5
- package/dist/ui/fleet-canvas.js +3 -2
- package/dist/ui/fleet-office-card.js +46 -7
- package/dist/ui/fleet-office-dialogue.d.ts +6 -3
- package/dist/ui/fleet-office-dialogue.js +13 -3
- package/dist/ui/fleet-office-layout.js +15 -15
- package/dist/ui/fleet-office-model.js +3 -2
- package/dist/ui/fleet-office-overview.js +1 -1
- package/dist/ui/fleet-office-pack.d.ts +17 -7
- package/dist/ui/fleet-office-pack.js +106 -38
- package/dist/ui/fleet-office-play.js +11 -4
- package/dist/ui/fleet-office-skin.js +41 -11
- package/dist/ui/fleet-office-store.js +10 -1
- package/dist/ui/fleet-office-talk.js +423 -0
- package/dist/ui/fleet-office-view.js +58 -17
- package/dist/ui/fleet-office-zones.js +20 -12
- package/dist/ui/fleet-office.js +250 -65
- package/dist/ui/fleet-org.js +3 -3
- package/dist/ui/fleet-task-common.js +3 -3
- package/dist/ui/fleet-task-sort.js +88 -0
- package/dist/ui/fleet-task-view.js +38 -9
- package/dist/ui/fleet-tasks.js +47 -15
- package/dist/ui/fleet-usage.js +6 -6
- package/dist/ui/hub-admin-overview.js +1 -1
- package/dist/ui/hub-chat-store.js +4 -3
- package/dist/ui/hub-chat-strings.js +3 -1
- package/dist/ui/hub-client.js +3 -0
- package/dist/ui/hub-my-agents-strings.js +2 -2
- package/dist/ui/hub-my-agents.js +3 -0
- package/dist/ui/hub-room-composer.js +24 -4
- package/dist/ui/hub-room-members.js +7 -3
- package/dist/ui/hub-room-office.js +71 -2
- package/dist/ui/hub-room-presence.js +14 -0
- package/dist/ui/hub-room-strings.js +18 -2
- package/dist/ui/hub-room-thread.js +82 -13
- package/dist/ui/hub-room-working.js +37 -0
- package/dist/ui/hub-session-limits.js +79 -0
- package/dist/ui/panel-fleet.js +5 -5
- package/dist/ui/panel-hub.js +41 -6
- package/dist/ui/panel-log.js +3 -3
- package/dist/ui/panel-needs.js +28 -2
- package/dist/ui/panel-runtimes.js +2 -2
- package/dist/ui/runtime-cli-install.js +1 -1
- package/dist/ui/runtime-update-status.js +1 -1
- package/dist/ui/settings-access-strings.js +1 -1
- package/dist/ui/settings-hub-setup-strings.js +1 -1
- package/dist/ui/settings-layout.js +2 -2
- package/dist/ui/settings-named-test.js +16 -16
- package/dist/ui/settings-public-session-strings.js +2 -2
- package/dist/ui/settings-revoke-strings.js +1 -1
- package/dist/ui/settings-shared-strings.js +2 -2
- package/dist/ui/settings-strings.js +40 -40
- package/dist/ui/shared/app-shell.js +1 -1
- package/dist/ui/shared/app.css +106 -6
- package/dist/ui/shared/chat-strings.js +9 -9
- package/dist/ui/shared/hub-owner.css +5 -0
- package/dist/ui/shared/hub-rooms-u2.css +3 -0
- package/dist/ui/shared/instance-nav.js +36 -2
- package/dist/ui/shared/office-dialogue.css +110 -0
- package/dist/ui/shared/task-board.css +103 -7
- package/dist/ui/shared/view-strings.js +27 -9
- package/dist/web-api.d.ts +5 -0
- package/dist/web-api.js +29 -0
- package/dist/web-api.js.map +1 -1
- package/dist/web-office-skins.d.ts +8 -2
- package/dist/web-office-skins.js +33 -14
- package/dist/web-office-skins.js.map +1 -1
- package/dist/web-office-store.js +5 -3
- package/dist/web-office-store.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
// Talking to an agent from the Office, without leaving it. One store (createTalk) under three ways to show it, picked
|
|
2
|
+
// per browser in the Office bar (TALK_MODES): "window" (the chat in a pop-up window, as before), "panel" (a side panel
|
|
3
|
+
// of the conversation; a bottom sheet on a phone), "box" (type under the dialogue box) and "bubble" (the reply over the
|
|
4
|
+
// agent's head). The store reads a thread's recent history (GET /ui/history), sends (POST /ui/send, as the chat does)
|
|
5
|
+
// and follows the page's live `message` events, so every mode shows the same conversation. Never on a share link.
|
|
6
|
+
import { html, useEffect, useLayoutEffect, useRef, useState } from "/assets/app-html.js";
|
|
7
|
+
import { register, t } from "/assets/app-i18n.js";
|
|
8
|
+
import "/assets/chat-render.js";
|
|
9
|
+
import { nineSlice, typed, wrap } from "./fleet-office-dialogue.js";
|
|
10
|
+
|
|
11
|
+
register("otalk", {
|
|
12
|
+
en: {
|
|
13
|
+
mode: "Talk", modeTitle: "How you talk to someone here (this browser only)",
|
|
14
|
+
window: "Pop-up window", panel: "Side panel", box: "Dialogue box", bubble: "Over their head",
|
|
15
|
+
placeholder: "Say something to {0}…", pick: "Click someone, then Talk, to talk to them here", send: "Send",
|
|
16
|
+
full: "Open full chat", reply: "Full reply", close: "Close", loading: "Loading…", failed: "Not sent — try again",
|
|
17
|
+
empty: "No messages yet. Say hello.", sending: "Sending…", talkTo: "Talking to {0}", file: "[a file]",
|
|
18
|
+
next: "Click: the rest, the next page, or close",
|
|
19
|
+
you: "You", history: "History",
|
|
20
|
+
play: "Reading", read_click: "Click", read_auto: "Auto", read_fast: "Fast", skip: "Skip",
|
|
21
|
+
readTitle: "Click: each line waits for a click. Auto: it goes on by itself. Fast: quickly. Skip: close every reply now.",
|
|
22
|
+
},
|
|
23
|
+
"zh-TW": {
|
|
24
|
+
mode: "對話方式", modeTitle: "在這裡跟成員對話的方式(只限這個瀏覽器)",
|
|
25
|
+
window: "彈出視窗", panel: "側邊面板", box: "對話框", bubble: "頭頂氣泡",
|
|
26
|
+
placeholder: "對 {0} 說…", pick: "點一個角色,再按「對話」就能在這裡跟他說話", send: "送出",
|
|
27
|
+
full: "開啟完整聊天", reply: "完整回覆", close: "關閉", loading: "載入中…", failed: "沒送出去,請再試一次",
|
|
28
|
+
empty: "還沒有訊息,打個招呼吧。", sending: "傳送中…", talkTo: "正在和 {0} 對話", file: "[檔案]",
|
|
29
|
+
next: "點一下:顯示全部、下一頁或關閉",
|
|
30
|
+
you: "你", history: "歷史紀錄",
|
|
31
|
+
play: "播放", read_click: "點擊", read_auto: "自動", read_fast: "快速", skip: "跳過",
|
|
32
|
+
readTitle: "點擊:每句等你點一下。自動:自己往下播。快速:很快地往下播。跳過:現在就關掉所有回覆。",
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
const tt = (k, ...v) => t(`otalk.${k}`, ...v);
|
|
36
|
+
/** This module's words (the Office bar's picker uses them). */
|
|
37
|
+
export const talkWord = tt;
|
|
38
|
+
|
|
39
|
+
export const TALK_MODES = ["window", "panel", "box", "bubble"];
|
|
40
|
+
/** How a reply plays in the box and the bubble, as in a visual novel (per browser): wait for a click, go on, go fast. */
|
|
41
|
+
export const READ_MODES = ["click", "auto", "fast"];
|
|
42
|
+
export const readModeOf = (v) => (READ_MODES.includes(v) ? v : "click");
|
|
43
|
+
/** Typing speed (characters a second) and how long a typed page stays before it goes on by itself (ms; ∞: a click). */
|
|
44
|
+
export function readPace(mode, chars = 0) {
|
|
45
|
+
if (mode === "fast") return { cps: 200, wait: 450 };
|
|
46
|
+
return { cps: 40, wait: mode === "auto" ? Math.min(5000, 1400 + 35 * chars) : Infinity };
|
|
47
|
+
}
|
|
48
|
+
/** A stored mode, or the default (the pop-up window: the Office as it was). */
|
|
49
|
+
export const talkModeOf = (v) => (TALK_MODES.includes(v) ? v : "window");
|
|
50
|
+
const HISTORY = 50, KEEP = 200;
|
|
51
|
+
|
|
52
|
+
/** What a message says in the Office: its text (a button's label), "[a file]" for files alone; null for anything else. */
|
|
53
|
+
export function talkTextOf(m) {
|
|
54
|
+
if (!m || (m.role !== "user" && m.role !== "agent")) return null;
|
|
55
|
+
const said = m.click && typeof m.click.label === "string" ? m.click.label : typeof m.text === "string" ? m.text : "";
|
|
56
|
+
if (said.trim()) return said;
|
|
57
|
+
return Array.isArray(m.attachments) && m.attachments.length ? tt("file") : null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The conversations: `open(name)` reads its recent history once; `send(name, text)` shows it at once (pending) and
|
|
62
|
+
* posts it; `take(m)` a live `message` event (the server's copy of a pending send replaces it, by clientId);
|
|
63
|
+
* #4406: whichever comes first — the history, the send's answer or the live copy — the server's copy settles the
|
|
64
|
+
* page's own (its messageId from the send's answer, or its clientId on the live copy), as the chat does (chat-store's
|
|
65
|
+
* settleOutgoing), so a reply never shows twice;
|
|
66
|
+
* `thread(name)` { msgs, loaded, error }; `lastReply(name)`; `subscribe(fn)`. `fetch` and `rid` are the page's.
|
|
67
|
+
*/
|
|
68
|
+
export function createTalk({ fetch: f = (...a) => fetch(...a), rid = () => `ot${Date.now().toString(36)}${Math.random().toString(36).slice(2, 10)}` } = {}) {
|
|
69
|
+
// said: name → its last live reply over the head, { text, at, page, pageAt, pages, whole, wholeAt } (the drawing
|
|
70
|
+
// fills pages / whole / wholeAt; advance() turns the page or lets it go).
|
|
71
|
+
const threads = new Map(), subs = new Set(), said = new Map();
|
|
72
|
+
const changed = (name) => { for (const fn of [...subs]) fn(name); };
|
|
73
|
+
const thread = (name) => { if (!threads.has(name)) threads.set(name, { msgs: [], loaded: false, loading: false, error: false }); return threads.get(name); };
|
|
74
|
+
const row = (m) => {
|
|
75
|
+
const text = talkTextOf(m);
|
|
76
|
+
if (text === null) return null;
|
|
77
|
+
const r = { key: `${m.boot}-${m.id}`, role: m.role, text, ts: m.ts };
|
|
78
|
+
if (typeof m.messageId === "string" && m.messageId) r.messageId = m.messageId;
|
|
79
|
+
if (m.role === "user" && typeof m.clientId === "string" && m.clientId) r.clientId = m.clientId; // the live copy's only
|
|
80
|
+
return r;
|
|
81
|
+
};
|
|
82
|
+
/** The page's own sends that the server's copy now stands for (same messageId, or same clientId): gone. Whether any went. */
|
|
83
|
+
const settle = (th) => {
|
|
84
|
+
const ids = new Set(), cids = new Set();
|
|
85
|
+
for (const x of th.msgs) if (!x.local) { if (x.messageId) ids.add(x.messageId); if (x.clientId) cids.add(x.clientId); }
|
|
86
|
+
const n = th.msgs.length;
|
|
87
|
+
th.msgs = th.msgs.filter((x) => !(x.local && ((x.messageId && ids.has(x.messageId)) || cids.has(x.clientId))));
|
|
88
|
+
return th.msgs.length !== n;
|
|
89
|
+
};
|
|
90
|
+
const keep = (th) => { if (th.msgs.length > KEEP) th.msgs.splice(0, th.msgs.length - KEEP); };
|
|
91
|
+
return {
|
|
92
|
+
thread: (name) => thread(name),
|
|
93
|
+
/** The live reply over `name`'s head, until it is read (advance) or skipped; else null. */
|
|
94
|
+
recent(name) { return said.get(name) || null; },
|
|
95
|
+
/** A click on it (or its turn to go on): the page whole while it types, else the next page, else it goes. */
|
|
96
|
+
advance(name) {
|
|
97
|
+
const r = said.get(name);
|
|
98
|
+
if (!r) return;
|
|
99
|
+
if (!r.whole) r.pageAt = -1e15;
|
|
100
|
+
else if (r.page < r.pages - 1) { r.page++; r.pageAt = Date.now(); r.whole = false; r.wholeAt = null; }
|
|
101
|
+
else said.delete(name);
|
|
102
|
+
changed(name);
|
|
103
|
+
},
|
|
104
|
+
/** Skip: every reply over a head goes now. */
|
|
105
|
+
skipAll() { const names = [...said.keys()]; said.clear(); for (const n of names) changed(n); },
|
|
106
|
+
lastReply(name) { const m = [...thread(name).msgs].reverse().find((x) => x.role === "agent"); return m ? m.text : null; },
|
|
107
|
+
subscribe(fn) { subs.add(fn); return () => subs.delete(fn); },
|
|
108
|
+
async open(name) {
|
|
109
|
+
const th = thread(name);
|
|
110
|
+
if (th.loaded || th.loading) return;
|
|
111
|
+
th.loading = true; changed(name);
|
|
112
|
+
try {
|
|
113
|
+
const r = await f(`/ui/history?instance=${encodeURIComponent(name)}&limit=${HISTORY}`, { credentials: "same-origin" });
|
|
114
|
+
const d = r && r.ok ? await r.json() : null;
|
|
115
|
+
const old = Array.isArray(d && d.messages) ? d.messages.map(row).filter(Boolean) : [];
|
|
116
|
+
const have = new Set(th.msgs.map((m) => m.key));
|
|
117
|
+
th.msgs = [...old.filter((m) => !have.has(m.key)), ...th.msgs];
|
|
118
|
+
settle(th); keep(th); th.loaded = true; th.error = !d;
|
|
119
|
+
} catch { th.error = true; }
|
|
120
|
+
th.loading = false; changed(name);
|
|
121
|
+
},
|
|
122
|
+
async send(name, text) {
|
|
123
|
+
const words = String(text || "").trim();
|
|
124
|
+
if (!words) return false;
|
|
125
|
+
const th = thread(name), clientId = rid(), mine = { key: `c-${clientId}`, role: "user", text: words, pending: true, clientId, local: true };
|
|
126
|
+
th.msgs.push(mine); keep(th); changed(name);
|
|
127
|
+
try {
|
|
128
|
+
const r = await f("/ui/send", { method: "POST", credentials: "same-origin", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ instance: name, message: words, clientId }) });
|
|
129
|
+
const d = await r.json().catch(() => ({}));
|
|
130
|
+
if (!r.ok || d.error || d.sent === false) throw new Error(d.error || "send");
|
|
131
|
+
mine.pending = false;
|
|
132
|
+
if (typeof d.messageId === "string" && d.messageId) { mine.messageId = d.messageId; settle(th); }
|
|
133
|
+
} catch { mine.pending = false; mine.failed = true; }
|
|
134
|
+
changed(name);
|
|
135
|
+
return !mine.failed;
|
|
136
|
+
},
|
|
137
|
+
take(m) {
|
|
138
|
+
if (!m || typeof m.instance !== "string" || !threads.has(m.instance)) return;
|
|
139
|
+
const r = row(m);
|
|
140
|
+
if (!r) return;
|
|
141
|
+
const th = threads.get(m.instance);
|
|
142
|
+
// Already here (the history brought it first): only the page's own copy of it may still have to go.
|
|
143
|
+
const had = th.msgs.find((x) => x.key === r.key);
|
|
144
|
+
if (had) { if (r.clientId && !had.local) had.clientId = r.clientId; if (settle(th)) changed(m.instance); return; }
|
|
145
|
+
const i = m.role === "user" && m.clientId ? th.msgs.findIndex((x) => x.local && x.clientId === m.clientId) : -1;
|
|
146
|
+
if (i >= 0) th.msgs[i] = r; else th.msgs.push(r);
|
|
147
|
+
settle(th);
|
|
148
|
+
if (r.role === "agent") said.set(m.instance, { text: r.text, at: Date.now(), page: 0, pageAt: null, pages: 1, whole: false, wholeAt: null });
|
|
149
|
+
keep(th); changed(m.instance);
|
|
150
|
+
},
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Re-render on a change to `name`'s thread. */
|
|
155
|
+
function useThread(talk, name) {
|
|
156
|
+
const [, bump] = useState(0);
|
|
157
|
+
useEffect(() => (talk && name ? talk.subscribe((n) => { if (n === name) bump((x) => x + 1); }) : undefined), [talk, name]);
|
|
158
|
+
useEffect(() => { if (talk && name) void talk.open(name); }, [talk, name]);
|
|
159
|
+
return talk && name ? talk.thread(name) : null;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Markdown as the chat renders it: escaped first, then a fixed set of tags (shared/chat-render.js). */
|
|
163
|
+
const md = (text) => {
|
|
164
|
+
const R = globalThis.AgendChatRender;
|
|
165
|
+
return R && typeof R.renderMarkdown === "function" ? R.renderMarkdown(text) : String(text).replace(/[&<>"']/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* A textarea that sends on Enter (Shift+Enter: a new line; never mid-IME composition). #4406: each draft is its
|
|
170
|
+
* target's — switching to someone else (a click, or the dialogue box's front speaker changing) shows theirs, never
|
|
171
|
+
* carries the words typed for the one before; going back finds them again.
|
|
172
|
+
*/
|
|
173
|
+
function Composer({ talk, name, label, autoFocus }) {
|
|
174
|
+
const [drafts, setDrafts] = useState(() => new Map()), ref = useRef(null);
|
|
175
|
+
const text = (name && drafts.get(name)) || "";
|
|
176
|
+
const setText = (v) => setDrafts((d) => { const n = new Map(d); if (v) n.set(name, v); else n.delete(name); return n; });
|
|
177
|
+
useEffect(() => { if (autoFocus && ref.current) ref.current.focus(); }, [name, autoFocus]);
|
|
178
|
+
const go = () => { const v = text.trim(); if (!v || !name) return; setText(""); void talk.send(name, v); };
|
|
179
|
+
const key = (e) => { if (e.key === "Enter" && !e.shiftKey && !e.isComposing && e.keyCode !== 229) { e.preventDefault(); go(); } };
|
|
180
|
+
return html`<div class="ot-compose">
|
|
181
|
+
<textarea ref=${ref} rows="1" class="ot-input" value=${text} disabled=${!name} aria-label=${name ? tt("placeholder", label) : tt("pick")}
|
|
182
|
+
placeholder=${name ? tt("placeholder", label) : tt("pick")} onInput=${(e) => setText(e.target.value)} onKeyDown=${key}></textarea>
|
|
183
|
+
<button type="button" class="btn btn-sm btn-primary ot-send" disabled=${!name || !text.trim()} onClick=${go}>${tt("send")}</button>
|
|
184
|
+
</div>`;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** Variant A: the conversation in a side panel (a bottom sheet on a phone). */
|
|
188
|
+
export function TalkPanel({ talk, name, label, state, stateLabel = null, sheet, onClose, onFull, top = null }) {
|
|
189
|
+
const th = useThread(talk, name), list = useRef(null), box = useRef(null);
|
|
190
|
+
useLayoutEffect(() => { const el = list.current; if (el) el.scrollTop = el.scrollHeight; }, [th && th.msgs.length, name]);
|
|
191
|
+
// Where the map starts (beside it, not over the bar); set as a property: the page's CSP allows no style attribute.
|
|
192
|
+
useLayoutEffect(() => {
|
|
193
|
+
const el = box.current;
|
|
194
|
+
if (el && el.style) { if (top === null || sheet) el.style.removeProperty("top"); else el.style.setProperty("top", `${top}px`); }
|
|
195
|
+
}, [top, sheet, name]);
|
|
196
|
+
if (!name) return null;
|
|
197
|
+
return html`<aside ref=${box} class=${`ot-panel${sheet ? " sheet" : ""}`} role="complementary" aria-label=${tt("talkTo", label)}>
|
|
198
|
+
<header class="ot-head">
|
|
199
|
+
<div class="ot-who"><strong>${label}</strong>${state ? html`<span class=${`ot-state st-${state}`}>${stateLabel || state}</span>` : null}</div>
|
|
200
|
+
${onFull ? html`<button type="button" class="btn btn-sm btn-ghost ot-full" onClick=${onFull}>${tt("full")}</button>` : null}
|
|
201
|
+
<button type="button" class="icon-btn ot-close" aria-label=${tt("close")} title=${tt("close")} onClick=${onClose}>×</button>
|
|
202
|
+
</header>
|
|
203
|
+
<ol class="ot-list" ref=${list} aria-live="polite">
|
|
204
|
+
${th && th.loading && !th.msgs.length ? html`<li class="ot-note">${tt("loading")}</li>` : null}
|
|
205
|
+
${th && th.loaded && !th.msgs.length ? html`<li class="ot-note">${tt("empty")}</li>` : null}
|
|
206
|
+
${(th ? th.msgs : []).map((m) => html`<li key=${m.key} class=${`ot-msg ${m.role === "user" ? "me" : "agent"}${m.pending ? " pending" : ""}${m.failed ? " failed" : ""}`}>
|
|
207
|
+
${m.role === "agent" ? html`<div class="ot-md" dangerouslySetInnerHTML=${{ __html: md(m.text) }}></div>` : html`<div class="ot-plain">${m.text}</div>`}
|
|
208
|
+
${m.pending ? html`<small class="ot-meta">${tt("sending")}</small>` : m.failed ? html`<small class="ot-meta ot-bad">${tt("failed")}</small>` : null}
|
|
209
|
+
</li>`)}
|
|
210
|
+
</ol>
|
|
211
|
+
<${Composer} talk=${talk} name=${name} label=${label} autoFocus=${true} />
|
|
212
|
+
</aside>`;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/** Variants B and C: a line to type in under the dialogue box, and the last reply in full on demand. */
|
|
216
|
+
export function TalkBar({ talk, name, label, onFull, read = null, onRead = null, onSkip = null }) {
|
|
217
|
+
const th = useThread(talk, name), [open, setOpen] = useState(false);
|
|
218
|
+
const reply = th ? talk.lastReply(name) : null;
|
|
219
|
+
useEffect(() => { setOpen(false); }, [name]);
|
|
220
|
+
return html`<div class="ot-bar">
|
|
221
|
+
${open && reply ? html`<div class="ot-reply" role="region" aria-label=${tt("reply")}>
|
|
222
|
+
<div class="ot-md" dangerouslySetInnerHTML=${{ __html: md(reply) }}></div>
|
|
223
|
+
<button type="button" class="icon-btn ot-close" aria-label=${tt("close")} onClick=${() => setOpen(false)}>×</button>
|
|
224
|
+
</div>` : null}
|
|
225
|
+
<div class="ot-bar-row">
|
|
226
|
+
${name ? html`<span class="ot-to">${label}</span>` : null}
|
|
227
|
+
<${Composer} talk=${talk} name=${name} label=${label} autoFocus=${false} />
|
|
228
|
+
${reply ? html`<button type="button" class="btn btn-sm btn-ghost ot-more" aria-expanded=${open ? "true" : "false"} onClick=${() => setOpen(!open)}>${tt("reply")}</button>` : null}
|
|
229
|
+
${name && onFull ? html`<button type="button" class="btn btn-sm btn-ghost ot-full" onClick=${onFull}>${tt("full")}</button>` : null}
|
|
230
|
+
${onRead ? html`<div class="ot-play" role="group" aria-label=${tt("play")} title=${tt("readTitle")}>
|
|
231
|
+
${READ_MODES.map((m) => html`<button key=${m} type="button" class=${`btn btn-sm ot-read${read === m ? " on" : ""}`} aria-pressed=${read === m ? "true" : "false"}
|
|
232
|
+
onClick=${() => onRead(m)}>${tt(`read_${m}`)}</button>`)}
|
|
233
|
+
<button type="button" class="btn btn-sm ot-skip" onClick=${onSkip}>${tt("skip")}</button>
|
|
234
|
+
</div>` : null}
|
|
235
|
+
</div>
|
|
236
|
+
</div>`;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// ── Variant B, on the scene: the dialogue box laid over the foot of the Office, a visual novel's ──
|
|
240
|
+
// The Office keeps its size and place: the box, the portrait and the line to type in are an overlay on the stage.
|
|
241
|
+
// Only the agent's side: what was said to it is in History (TalkLog). `peek()` (the Office's) is what it shows now:
|
|
242
|
+
// { name, label, conv, status, portrait, colours, more } or null; `ops` { now, drop(instance), skip(), target(name) }. Each conversation keeps its page and typing
|
|
243
|
+
// (page, pageAt, wholeAt, shownAt, replyShownAt) as the canvas box did.
|
|
244
|
+
const PAGE_LINES = { desk: 2, phone: 3 };
|
|
245
|
+
let measureCtx = null;
|
|
246
|
+
const measurer = (font) => {
|
|
247
|
+
if (!measureCtx && typeof document !== "undefined") measureCtx = document.createElement("canvas").getContext("2d");
|
|
248
|
+
if (!measureCtx) return (s) => [...s].length * 16;
|
|
249
|
+
measureCtx.font = font;
|
|
250
|
+
return (s) => measureCtx.measureText(s).width;
|
|
251
|
+
};
|
|
252
|
+
export function TalkStage({ peek, ops, talk, read, onRead, phone, onFull, onClose, still = false }) {
|
|
253
|
+
const [, tick] = useState(0), [log, setLog] = useState(false), reply = useRef(null), root = useRef(null), fig = useRef(null);
|
|
254
|
+
const v = peek();
|
|
255
|
+
const live = !!(v && v.conv);
|
|
256
|
+
useEffect(() => {
|
|
257
|
+
if (!v) return undefined;
|
|
258
|
+
// Reduced motion (`still`, the canvas box's own): nothing types, so a slow tick only for Auto / Fast's waits.
|
|
259
|
+
const h = setInterval(() => tick((x) => x + 1), still ? 250 : 50);
|
|
260
|
+
return () => clearInterval(h);
|
|
261
|
+
}, [!!v, still]);
|
|
262
|
+
// The text keeps clear of the portrait: its drawn width, as a property (the page's CSP allows no style attribute).
|
|
263
|
+
useLayoutEffect(() => {
|
|
264
|
+
const el = root.current, im = fig.current;
|
|
265
|
+
if (!el || !el.style) return;
|
|
266
|
+
// Beside the portrait (a desk): the words end where the drawn figure begins (object-fit: contain, at the right).
|
|
267
|
+
const box = el.querySelector(".ot-box");
|
|
268
|
+
let pad = 0;
|
|
269
|
+
if (!phone && box && im && im.complete && im.naturalWidth) {
|
|
270
|
+
const br = box.getBoundingClientRect(), fr = im.getBoundingClientRect();
|
|
271
|
+
const drawnW = Math.min(fr.width, (fr.height * im.naturalWidth) / im.naturalHeight);
|
|
272
|
+
pad = Math.max(0, Math.round(br.right - (fr.right - drawnW)));
|
|
273
|
+
}
|
|
274
|
+
el.style.setProperty("--ot-pad", `${pad}px`);
|
|
275
|
+
// The pack's dialogBox colours, else a visual novel's night blue and gold.
|
|
276
|
+
const k = (v && v.colours) || {};
|
|
277
|
+
for (const [name, val] of [["--ot-bg", k.background || "#141b2d"], ["--ot-gold", k.border || "#c8a165"], ["--ot-text", k.text || "#f4f1ea"], ["--ot-name", k.name || "#ffd27a"]]) el.style.setProperty(name, val);
|
|
278
|
+
});
|
|
279
|
+
if (!v) return null;
|
|
280
|
+
const t = ops.now(), c = v.conv;
|
|
281
|
+
// The reply, page by page (PAGE_LINES), typed at the reading mode's pace; the heard line stays whole.
|
|
282
|
+
let lines = [], page = 0, pages = 1, n = Infinity, whole = true, pageChars = 0;
|
|
283
|
+
const answered = !!(c && c.answeredAt !== null);
|
|
284
|
+
if (c) {
|
|
285
|
+
if (c.shownAt == null) c.shownAt = t;
|
|
286
|
+
if (answered && c.replyShownAt == null) c.replyShownAt = t;
|
|
287
|
+
}
|
|
288
|
+
if (answered) {
|
|
289
|
+
const el = reply.current, w = el ? el.clientWidth : 0;
|
|
290
|
+
const cs = typeof getComputedStyle === "function" && el ? getComputedStyle(el) : null;
|
|
291
|
+
const all = w > 0 ? wrap(measurer((cs && cs.font) || "16px sans-serif"), c.replyFull ?? c.reply ?? "", w, 400) : [c.replyFull ?? c.reply ?? ""];
|
|
292
|
+
const per = phone ? PAGE_LINES.phone : PAGE_LINES.desk;
|
|
293
|
+
pages = Math.max(1, Math.ceil(all.length / per)); page = Math.min(c.page || 0, pages - 1);
|
|
294
|
+
lines = all.slice(page * per, page * per + per);
|
|
295
|
+
pageChars = lines.reduce((a, l) => a + [...l].length, 0);
|
|
296
|
+
const pace = readPace(read, pageChars);
|
|
297
|
+
n = still ? Infinity : Math.floor(((t - (c.pageAt ?? c.replyShownAt)) * pace.cps) / 1000);
|
|
298
|
+
whole = n >= pageChars;
|
|
299
|
+
if (whole && c.wholeAt == null) c.wholeAt = t;
|
|
300
|
+
if (whole && pace.wait < Infinity && t - c.wholeAt >= pace.wait) setTimeout(() => step(true), 0);
|
|
301
|
+
}
|
|
302
|
+
const last = page >= pages - 1;
|
|
303
|
+
function step(auto = false) {
|
|
304
|
+
if (!auto && v.name) ops.target(v.name); // the line below talks to whoever the box shows
|
|
305
|
+
if (!c || !answered) return;
|
|
306
|
+
if (!whole) c.pageAt = -1e15;
|
|
307
|
+
else if (!last) { c.page = page + 1; c.pageAt = ops.now(); c.wholeAt = null; }
|
|
308
|
+
else ops.drop(c.instance);
|
|
309
|
+
tick((x) => x + 1);
|
|
310
|
+
}
|
|
311
|
+
const shown = answered ? typed(lines, n).join("\n") : "";
|
|
312
|
+
const keep = (e) => e.stopPropagation(); // the stage's pan, zoom keys and clicks stay off the box
|
|
313
|
+
return html`<div class=${`ot-stage${phone ? " phone" : ""}`} ref=${root} onPointerDown=${keep} onKeyDown=${keep} onWheel=${keep} onClick=${keep}>
|
|
314
|
+
${v.portrait ? html`<img class="ot-figure" ref=${fig} src=${v.portrait} alt="" draggable=${false} />` : null}
|
|
315
|
+
<div class=${`ot-box${live ? " live" : ""}`} role="group" aria-label=${tt("talkTo", v.label)}>
|
|
316
|
+
${log ? html`<${TalkLog} talk=${talk} name=${v.name} label=${v.label} onFull=${onFull} onClose=${() => setLog(false)} />` : null}
|
|
317
|
+
<div class="ot-tools">
|
|
318
|
+
${READ_MODES.map((m) => html`<button key=${m} type="button" class=${`ot-tool${read === m ? " on" : ""}`} aria-pressed=${read === m ? "true" : "false"} onClick=${() => onRead(m)}>${tt(`read_${m}`)}</button>`)}
|
|
319
|
+
<button type="button" class="ot-tool" onClick=${ops.skip}>${tt("skip")}</button>
|
|
320
|
+
<button type="button" class="ot-tool ot-x" aria-label=${tt("close")} title=${tt("close")} onClick=${onClose}>×</button>
|
|
321
|
+
</div>
|
|
322
|
+
<div class="ot-read" role="button" tabindex="0" aria-label=${tt("next")} onClick=${() => step()}
|
|
323
|
+
onKeyDown=${(e) => { if ((e.key === "Enter" || e.key === " ") && !e.repeat) { e.preventDefault(); step(); } }}>
|
|
324
|
+
<div class="ot-answer">
|
|
325
|
+
<div class="ot-words" ref=${reply} aria-live="polite">${answered ? shown : html`<span class="ot-status">${live ? v.status || "" : ""}</span>`}${answered && whole ? html`<span class="ot-cue" aria-hidden="true">▼</span>` : null}</div>
|
|
326
|
+
<span class="ot-plate ot-agent">${v.label}</span>
|
|
327
|
+
</div>
|
|
328
|
+
${v.more > 0 ? html`<span class="ot-more-n">+${v.more}</span>` : null}
|
|
329
|
+
</div>
|
|
330
|
+
<div class="ot-line">
|
|
331
|
+
<${Composer} talk=${talk} name=${v.name} label=${v.label} autoFocus=${!live} />
|
|
332
|
+
<span class="ot-sep" aria-hidden="true">|</span>
|
|
333
|
+
<button type="button" class="ot-link" aria-expanded=${log ? "true" : "false"} onClick=${() => setLog(!log)}>${tt("history")}</button>
|
|
334
|
+
</div>
|
|
335
|
+
</div>
|
|
336
|
+
</div>`;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/** The box's history (a visual novel's backlog): both sides of the conversation, over the box; the full chat from it. */
|
|
340
|
+
function TalkLog({ talk, name, label, onFull, onClose }) {
|
|
341
|
+
const th = useThread(talk, name), list = useRef(null);
|
|
342
|
+
useLayoutEffect(() => { const el = list.current; if (el) el.scrollTop = el.scrollHeight; }, [th && th.msgs.length]);
|
|
343
|
+
return html`<section class="ot-log" role="dialog" aria-label=${tt("history")}>
|
|
344
|
+
<header class="ot-log-head">
|
|
345
|
+
<strong>${tt("history")} · ${label}</strong>
|
|
346
|
+
<button type="button" class="ot-link" onClick=${onFull}>${tt("full")}</button>
|
|
347
|
+
<button type="button" class="ot-tool ot-x" aria-label=${tt("close")} title=${tt("close")} onClick=${onClose}>×</button>
|
|
348
|
+
</header>
|
|
349
|
+
<ol class="ot-log-list" ref=${list}>
|
|
350
|
+
${th && th.loading && !th.msgs.length ? html`<li class="ot-note">${tt("loading")}</li>` : null}
|
|
351
|
+
${th && th.loaded && !th.msgs.length ? html`<li class="ot-note">${tt("empty")}</li>` : null}
|
|
352
|
+
${(th ? th.msgs : []).map((m) => html`<li key=${m.key} class=${`ot-log-msg ${m.role === "user" ? "me" : "agent"}`}>
|
|
353
|
+
<span class="ot-log-who">${m.role === "user" ? tt("you") : label}</span>
|
|
354
|
+
${m.role === "agent" ? html`<div class="ot-md" dangerouslySetInnerHTML=${{ __html: md(m.text) }}></div>` : html`<div class="ot-plain">${m.text}</div>`}
|
|
355
|
+
${m.failed ? html`<small class="ot-meta ot-bad">${tt("failed")}</small>` : null}
|
|
356
|
+
</li>`)}
|
|
357
|
+
</ol>
|
|
358
|
+
</section>`;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// ── Variant C: the reply over the agent's head ──
|
|
362
|
+
const REPLY = { w: 190, lines: 6, font: 11, line: 15, pad: 8, enterMs: 240 };
|
|
363
|
+
/** The bubble's look: the pack's `dialogBox.bubble`, else light paper (as the state bubbles). */
|
|
364
|
+
export function replyStyle(b, env) {
|
|
365
|
+
const d = b || {};
|
|
366
|
+
return {
|
|
367
|
+
background: d.background || "#fffdf6", border: d.border || (env && env.mode === "light" ? "#8a8578" : "#4a4740"),
|
|
368
|
+
text: d.text || "#1d1d22", name: d.name || "#6b5bd6", enter: d.enter || "pop",
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* A reply in a bubble over a head, its tail at (x, y) (logical units: the map's transform): the name, then a page of
|
|
373
|
+
* REPLY.lines wrapped lines. `age`: ms since it arrived, for its entrance (pop / fade / none; 0: whole at once);
|
|
374
|
+
* `frame` { im, slice, k }: a 9-slice picture instead of the drawn box; `read` { page, n }: that page, its first `n`
|
|
375
|
+
* characters typed, and ▼ once it is whole (none: page 0, whole, no ▼). `fit` { l, r, t?, b? }: the box kept on screen
|
|
376
|
+
* (#4406): between l and r, its tail still at x; over the head when it fits under t, else below it (tail up) when it
|
|
377
|
+
* fits above b, else on the side with more room, clamped. Returns the box, the pages and the page's length.
|
|
378
|
+
*/
|
|
379
|
+
export function drawReply(ctx, x, y, name, text, age, style, frame, read = null, fit = null) {
|
|
380
|
+
ctx.save();
|
|
381
|
+
const font = (wt) => `${wt} ${REPLY.font}px system-ui, sans-serif`;
|
|
382
|
+
ctx.font = font(400);
|
|
383
|
+
const all = wrap((s) => ctx.measureText(s).width, text, REPLY.w, 400);
|
|
384
|
+
const pages = Math.max(1, Math.ceil(all.length / REPLY.lines)), page = Math.min(read ? read.page || 0 : 0, pages - 1);
|
|
385
|
+
const lines = all.slice(page * REPLY.lines, page * REPLY.lines + REPLY.lines);
|
|
386
|
+
const len = lines.reduce((a, l) => a + [...l].length, 0), n = read && Number.isFinite(read.n) ? read.n : Infinity;
|
|
387
|
+
const textW = Math.max(0, ...lines.map((l) => ctx.measureText(l).width));
|
|
388
|
+
ctx.font = font(700);
|
|
389
|
+
const nameW = name ? ctx.measureText(name).width : 0;
|
|
390
|
+
const w = Math.ceil(Math.max(nameW, textW) + REPLY.pad * 2), h = (name ? REPLY.line : 0) + lines.length * REPLY.line + REPLY.pad * 2 - 2;
|
|
391
|
+
let bx0 = x - Math.min(24, w / 2);
|
|
392
|
+
if (fit) bx0 = Math.max(fit.l, Math.min(bx0, fit.r - w));
|
|
393
|
+
let by0 = y - h - 10;
|
|
394
|
+
if (fit && Number.isFinite(fit.t) && Number.isFinite(fit.b) && by0 < fit.t) {
|
|
395
|
+
const under = y + 10;
|
|
396
|
+
by0 = under + h <= fit.b || fit.b - y > y - fit.t ? under : by0;
|
|
397
|
+
by0 = Math.max(fit.t, Math.min(by0, fit.b - h));
|
|
398
|
+
}
|
|
399
|
+
const bx = Math.round(bx0), by = Math.round(by0), below = by > y, over = by + h < y;
|
|
400
|
+
const f = age > 0 ? Math.min(1, age / REPLY.enterMs) : 1, ease = 1 - (1 - f) ** 3;
|
|
401
|
+
if (style.enter === "fade") ctx.globalAlpha *= ease;
|
|
402
|
+
if (style.enter === "pop" && f < 1) {
|
|
403
|
+
const k = 0.6 + 0.4 * ease + Math.sin(f * Math.PI) * 0.06;
|
|
404
|
+
ctx.translate(x, y); ctx.scale(k, k); ctx.translate(-x, -y); ctx.globalAlpha *= Math.min(1, f * 2);
|
|
405
|
+
}
|
|
406
|
+
ctx.fillStyle = style.background; ctx.strokeStyle = style.border; ctx.lineWidth = 1.5;
|
|
407
|
+
const edge = below ? by + 1 : by + h - 1; // the tail from the box's edge nearest the head (none when the box is over it)
|
|
408
|
+
if (over || below) { ctx.beginPath(); ctx.moveTo(x - 5, edge); ctx.lineTo(x, y); ctx.lineTo(x + 6, edge); ctx.closePath(); ctx.fill(); ctx.stroke(); }
|
|
409
|
+
if (!(frame && frame.im && nineSlice(ctx, frame.im, frame.slice, bx, by, w, h, frame.k))) {
|
|
410
|
+
ctx.beginPath();
|
|
411
|
+
if (typeof ctx.roundRect === "function") ctx.roundRect(bx, by, w, h, 7); else ctx.rect(bx, by, w, h);
|
|
412
|
+
ctx.fill(); ctx.stroke();
|
|
413
|
+
if (over || below) ctx.fillRect(x - 4, below ? by - 1 : by + h - 2, 9, 3); // the tail joins the box without a seam
|
|
414
|
+
}
|
|
415
|
+
ctx.textBaseline = "top"; ctx.textAlign = "left";
|
|
416
|
+
let ty = by + REPLY.pad - 1;
|
|
417
|
+
if (name) { ctx.font = font(700); ctx.fillStyle = style.name; ctx.fillText(name, bx + REPLY.pad, ty); ty += REPLY.line; }
|
|
418
|
+
ctx.font = font(400); ctx.fillStyle = style.text;
|
|
419
|
+
for (const l of typed(lines, n)) { ctx.fillText(l, bx + REPLY.pad, ty); ty += REPLY.line; }
|
|
420
|
+
if (read && n >= len) { ctx.font = font(700); ctx.fillStyle = style.name; ctx.textAlign = "right"; ctx.fillText("▼", bx + w - 4, by + h - REPLY.line + 1); }
|
|
421
|
+
ctx.restore();
|
|
422
|
+
return { x: bx, y: by, w, h, pages, len };
|
|
423
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
// #2540: the Office view's canvas controls — the pure part (fleet-office.js applies it). The office fills the Fleet
|
|
2
|
-
// panel; its scene (the zones' 1280×720
|
|
2
|
+
// panel; its scene (the zones' logical canvas: 1280×720 or a pack's own, or the desks layout) is panned and zoomed like the org chart's
|
|
3
3
|
// canvas (fleet-canvas-view.js: the same view { k, x, y }, gestures, wheel and keys), with these of its own:
|
|
4
4
|
//
|
|
5
5
|
// - Pixel levels for the steps. + and − step to the next pixel level (one logical unit a whole number of device px:
|
|
@@ -17,36 +17,45 @@
|
|
|
17
17
|
// background). It paints the whole canvas outside the scene (and the stage behind it), never the page's grey.
|
|
18
18
|
import { clampK, pinchTo, zoomTo } from "./fleet-canvas-view.js";
|
|
19
19
|
import { CANVAS_MAX } from "./fleet-office-model.js";
|
|
20
|
+
import { CANVAS, LOGICAL } from "./fleet-office-pack.js";
|
|
20
21
|
|
|
21
|
-
/** The zoom's range, in CSS px per logical unit. */
|
|
22
|
+
/** The zoom's range, in CSS px per logical unit: OK_MIN is the floor for a scene the default canvas's size (1280×720). */
|
|
22
23
|
export const OK_MIN = 0.1, OK_MAX = 8;
|
|
24
|
+
/**
|
|
25
|
+
* #4405: a scene's own zoom floor. A scene bigger than the default canvas (a pack's `canvas: [w, h]`, All offices) may
|
|
26
|
+
* shrink as far on screen as the default one at OK_MIN (5120×2880: 0.025, still 128 px wide), so Fit shows it whole on
|
|
27
|
+
* a phone and − reaches that; never below OK_FLOOR, the biggest pack canvas's.
|
|
28
|
+
*/
|
|
29
|
+
const floorOf = (w, h) => Math.min(OK_MIN, OK_MIN * LOGICAL.w / Math.max(1, w), OK_MIN * LOGICAL.h / Math.max(1, h));
|
|
30
|
+
export const OK_FLOOR = floorOf(CANVAS.max.w, CANVAS.max.h);
|
|
31
|
+
export const minK = (w, h) => Math.max(OK_FLOOR, floorOf(w, h));
|
|
23
32
|
const EPS = 1e-6;
|
|
24
|
-
const clampO = (k) => clampK(k,
|
|
33
|
+
const clampO = (k, lo = OK_MIN) => clampK(k, lo, OK_MAX);
|
|
25
34
|
/** The highest pixel level within the range at this ratio (at least 1). */
|
|
26
35
|
const topLevel = (dpr) => Math.max(1, Math.floor(OK_MAX * dpr + EPS));
|
|
27
36
|
|
|
28
37
|
/** `k` on its pixel level: k·dpr rounded to a whole number when it is 1 or more; as it is (in range) below that. */
|
|
29
|
-
export function snapK(k, dpr = 1) {
|
|
30
|
-
const c = clampO(k), n = c * dpr;
|
|
38
|
+
export function snapK(k, dpr = 1, lo = OK_MIN) {
|
|
39
|
+
const c = clampO(k, lo), n = c * dpr;
|
|
31
40
|
if (n < 1 - EPS) return c;
|
|
32
41
|
return Math.min(topLevel(dpr), Math.max(1, Math.round(n))) / dpr;
|
|
33
42
|
}
|
|
34
43
|
/** Whether `k` is a pixel level (a whole number of device px per logical unit). */
|
|
35
44
|
export const isPixelK = (k, dpr = 1) => k * dpr >= 1 - EPS && Math.abs(k * dpr - Math.round(k * dpr)) < 1e-6;
|
|
36
|
-
/** One step in (`dir` > 0) or out: the next pixel level; below one device pixel a unit, by 1.25. */
|
|
37
|
-
export function stepK(k, dir, dpr = 1) {
|
|
45
|
+
/** One step in (`dir` > 0) or out: the next pixel level; below one device pixel a unit, by 1.25 (down to `lo`). */
|
|
46
|
+
export function stepK(k, dir, dpr = 1, lo = OK_MIN) {
|
|
38
47
|
const n = k * dpr;
|
|
39
48
|
if (dir > 0) {
|
|
40
49
|
if (n >= 1 - EPS) return Math.min(topLevel(dpr), Math.floor(n + EPS) + 1) / dpr;
|
|
41
|
-
return n * 1.25 >= 1 - EPS ? 1 / dpr : clampO(k * 1.25);
|
|
50
|
+
return n * 1.25 >= 1 - EPS ? 1 / dpr : clampO(k * 1.25, lo);
|
|
42
51
|
}
|
|
43
52
|
if (n > 1 + EPS) return Math.max(1, Math.ceil(n - EPS) - 1) / dpr;
|
|
44
|
-
return clampO(k * 0.8);
|
|
53
|
+
return clampO(k * 0.8, lo);
|
|
45
54
|
}
|
|
46
55
|
/** The view at scale `k` (in range, continuous: the wheel's) about the viewport point (cx, cy). */
|
|
47
|
-
export const zoomOffice = (v, k, cx, cy) => zoomTo(v, clampO(k), cx, cy);
|
|
56
|
+
export const zoomOffice = (v, k, cx, cy, lo = OK_MIN) => zoomTo(v, clampO(k, lo), cx, cy);
|
|
48
57
|
/** A pinch step: `raw` the zoom the pinch has reached (continuous); the world point under the old midpoint follows. */
|
|
49
|
-
export const pinchOffice = (v, from, to, raw) => pinchTo(v, from, to, clampO(raw));
|
|
58
|
+
export const pinchOffice = (v, from, to, raw, lo = OK_MIN) => pinchTo(v, from, to, clampO(raw, lo));
|
|
50
59
|
|
|
51
60
|
/** The fit modes: "contain" (the whole scene shows) and "cover" (the panel is filled, the overflow cropped). */
|
|
52
61
|
export const FIT_MODES = ["contain", "cover"];
|
|
@@ -54,7 +63,7 @@ export const fitModeOk = (m) => (m === "cover" ? "cover" : "contain");
|
|
|
54
63
|
/** The scale at which a `w`×`h` scene fits a `size` viewport: contain (the whole scene) or cover (the whole panel). */
|
|
55
64
|
export function fitK(w, h, size, mode = "contain", margin = 0) {
|
|
56
65
|
const sx = (size.w - 2 * margin) / Math.max(1, w), sy = (size.h - 2 * margin) / Math.max(1, h);
|
|
57
|
-
return clampO(fitModeOk(mode) === "cover" ? Math.max(sx, sy) : Math.min(sx, sy));
|
|
66
|
+
return clampO(fitModeOk(mode) === "cover" ? Math.max(sx, sy) : Math.min(sx, sy), minK(w, h));
|
|
58
67
|
}
|
|
59
68
|
/** The scene at fitK, centred. */
|
|
60
69
|
export function fitScene(w, h, size, mode = "contain", margin = 0) {
|
|
@@ -70,6 +79,38 @@ export const desksK = (dpr = 1) => Math.max(1, Math.round(dpr)) / dpr;
|
|
|
70
79
|
export const openingView = (w, h, size, dpr, zones, mode = "contain") => (zones ? fitScene(w, h, size, mode) : { k: desksK(dpr), x: 0, y: 0 });
|
|
71
80
|
/** One axis of clampPan: offset `o` of a scene `s` long in a viewport `n` long. */
|
|
72
81
|
const clampAxis = (o, s, n) => (s >= n ? Math.min(0, Math.max(n - s, o)) : (n - s) / 2);
|
|
82
|
+
/** How much of the area labels a `cw`×`ch` box at (x, y) covers (px²). */
|
|
83
|
+
const covered = (x, y, cw, ch, avoid) => avoid.reduce((a, r) => a + Math.max(0, Math.min(x + cw, r.x + r.w) - Math.max(x, r.x)) * Math.max(0, Math.min(y + ch, r.y + r.h) - Math.max(y, r.y)), 0);
|
|
84
|
+
/**
|
|
85
|
+
* M-3633: where a person's floating card goes (stage px): beside them — right of `px` by `gap`, else left — level with
|
|
86
|
+
* their head (`py` − 24), inside the stage (`w`×`h`, 8 px clear). Clear of the areas' labels (`avoid`, rects on the
|
|
87
|
+
* stage) where it can be: the same spot when it covers none, else that side lower (just under the label it met), else
|
|
88
|
+
* the other side; else held shorter under a label (`maxH`: it scrolls; at least CARD_MIN_H); with none clear, the spot
|
|
89
|
+
* covering the least.
|
|
90
|
+
*/
|
|
91
|
+
export const CARD_MIN_H = 240;
|
|
92
|
+
export function cardSpot({ px, py, gap, cw, ch, w, h, avoid = [] }) {
|
|
93
|
+
const cx = (x) => Math.max(8, Math.min(x, w - cw - 8)), cy = (y) => Math.max(8, Math.min(y, h - ch - 8));
|
|
94
|
+
const right = px + gap, left = px - gap - cw;
|
|
95
|
+
const sides = right + cw > w - 8 ? [left, right] : [right, left];
|
|
96
|
+
const spots = [];
|
|
97
|
+
for (const s of sides) {
|
|
98
|
+
const x = cx(s), y = cy(py - 24);
|
|
99
|
+
spots.push({ x, y });
|
|
100
|
+
for (const r of avoid) if (r.y + r.h + 6 > y && r.y + r.h + 6 + ch <= h - 8) spots.push({ x, y: r.y + r.h + 6 });
|
|
101
|
+
}
|
|
102
|
+
for (const s of sides) for (const r of avoid) {
|
|
103
|
+
const y = r.y + r.h + 6, room = h - 8 - y;
|
|
104
|
+
if (room >= CARD_MIN_H && room < ch) spots.push({ x: cx(s), y, maxH: room });
|
|
105
|
+
}
|
|
106
|
+
let best = spots[0], least = Infinity;
|
|
107
|
+
for (const p of spots) {
|
|
108
|
+
const c = covered(p.x, p.y, cw, p.maxH ?? ch, avoid);
|
|
109
|
+
if (c === 0) return p;
|
|
110
|
+
if (c < least) { least = c; best = p; }
|
|
111
|
+
}
|
|
112
|
+
return best;
|
|
113
|
+
}
|
|
73
114
|
/**
|
|
74
115
|
* The view kept on the scene: along an axis where the scene is at least the viewport it covers the viewport (no gap at
|
|
75
116
|
* either edge); where it is smaller it is centred on that axis. Nothing can be scrolled off-screen.
|
|
@@ -110,17 +151,17 @@ export function wholeInView(v, size, w, h) {
|
|
|
110
151
|
|
|
111
152
|
/** What is kept of a view: { k, cx, cy }, and `fit: true` when it is a fit (it is fitted again, to the size then). */
|
|
112
153
|
export const savedOf = (v, size, fit = false) => ({ k: v.k, cx: (size.w / 2 - v.x) / v.k, cy: (size.h / 2 - v.y) / v.k, ...(fit ? { fit: true } : {}) });
|
|
113
|
-
/** A kept view, checked: { k, cx, cy } with finite numbers (k in range), else null. */
|
|
154
|
+
/** A kept view, checked: { k, cx, cy } with finite numbers (k in range: down to OK_FLOOR, a big canvas's), else null. */
|
|
114
155
|
export function savedOk(x) {
|
|
115
156
|
if (!x || typeof x !== "object") return null;
|
|
116
157
|
const { k, cx, cy } = x;
|
|
117
158
|
if (![k, cx, cy].every((n) => typeof n === "number" && Number.isFinite(n)) || k <= 0) return null;
|
|
118
159
|
if (Math.abs(cx) > 1e6 || Math.abs(cy) > 1e6) return null;
|
|
119
|
-
return { k: clampO(k), cx, cy, ...(x.fit === true ? { fit: true } : {}) };
|
|
160
|
+
return { k: clampO(k, OK_FLOOR), cx, cy, ...(x.fit === true ? { fit: true } : {}) };
|
|
120
161
|
}
|
|
121
|
-
/** The view a kept one gives in a `size` viewport: the same zoom, the same logical point in the middle. */
|
|
122
|
-
export function viewFromSaved(s, size) {
|
|
123
|
-
const k = clampO(s.k);
|
|
162
|
+
/** The view a kept one gives in a `size` viewport: the same zoom (within the scene's floor `lo`), the same logical point in the middle. */
|
|
163
|
+
export function viewFromSaved(s, size, lo = OK_MIN) {
|
|
164
|
+
const k = clampO(s.k, lo);
|
|
124
165
|
return { k, x: size.w / 2 - s.cx * k, y: size.h / 2 - s.cy * k };
|
|
125
166
|
}
|
|
126
167
|
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// fleet-office-pack.js). Each instance goes to the area for its state now and takes a seat slot there; when its state
|
|
3
3
|
// changes it walks (along the pack's waypoints) to the new area. Pure — fleet-office.js draws it. Also here: what a
|
|
4
4
|
// character says in its speech bubble, and which furniture is active.
|
|
5
|
-
import {
|
|
5
|
+
import { canvasOf, own } from "./fleet-office-pack.js";
|
|
6
6
|
import { flightMs, nameHash, pathLength, along, routeVia } from "./fleet-office-model.js";
|
|
7
7
|
import { hue } from "./fleet-traffic.js";
|
|
8
8
|
|
|
@@ -154,9 +154,10 @@ export function zonesLayout(pack, nodes, prev = new Map(), { night = false, seat
|
|
|
154
154
|
const s = seats.get(n.name);
|
|
155
155
|
return { key: `zone/${n.name}`, node: n, x: s.x - 24, y: s.y - 52, w: 48, h: 66, seat: { x: s.x, y: s.y }, wp: null, area: s.area, slot: s.slot, night: sleepers.has(n.name) };
|
|
156
156
|
});
|
|
157
|
-
|
|
157
|
+
const cv = canvasOf(pack);
|
|
158
|
+
return { mode: "zones", rooms: [{ key: "\u0000zones", name: null, lobby: false, x: 0, y: 0, w: cv.w, h: cv.h, desks }], areas: pack.areas,
|
|
158
159
|
waypoints: pack.waypoints || [], edges: pack.edges || [], straight: pack.straight === true, door: pack.door, mailDrop: pack.mailDrop || null,
|
|
159
|
-
idleVisits: pack.idleVisits || [], idleAvoid: callingAreas(pack), width:
|
|
160
|
+
idleVisits: pack.idleVisits || [], idleAvoid: callingAreas(pack), width: cv.w, height: cv.h, seats, crowd, over }; // #3685: idleVisits for walkTargets; M-3539: idleAvoid
|
|
160
161
|
}
|
|
161
162
|
|
|
162
163
|
// ── Walking ──
|
|
@@ -604,20 +605,27 @@ export function bubbleAt(name, state, ms, lines) {
|
|
|
604
605
|
* (the most typed out) — one already showing keeps its place. Each takes its spot, or one row up (`lift`) when that is
|
|
605
606
|
* free; else it waits for another round. Returns the placed ones, each with its `dy` (0 or -lift). `rank` (optional,
|
|
606
607
|
* bubbleRank): a lower one goes first (else: calling 0, the rest 2); `blocked`: boxes no bubble may cover (name tags).
|
|
608
|
+
* M-3648: `soft` (the areas' names, M-3539) blocks every bubble but a call: one calling for the person (stuck, needs
|
|
609
|
+
* you, crashed) that finds both spots taken tries a second row up, then may cover an area's name — never a tag or
|
|
610
|
+
* another bubble (no two bubbles overlap) — so a name alone never keeps a call out (on Cozy a stuck agent stands right
|
|
611
|
+
* under "Help desk", and its "Hmm, stuck?" went missing in beta.20).
|
|
607
612
|
*/
|
|
608
|
-
export function placeBubbles(items, lift = 22, blocked = []) {
|
|
613
|
+
export function placeBubbles(items, lift = 22, blocked = [], soft = []) {
|
|
609
614
|
const rank = (it) => (it.rank ?? (it.calling ? 0 : 2));
|
|
610
615
|
const order = [...items].sort((a, b) => (rank(a) - rank(b)) || (b.shown - a.shown) || (a.key < b.key ? -1 : a.key > b.key ? 1 : 0));
|
|
611
616
|
const taken = [...blocked], out = [];
|
|
612
|
-
const
|
|
617
|
+
const hit = (list, r) => list.some((t) => r.x < t.x + t.w && t.x < r.x + r.w && r.y < t.y + t.h && t.y < r.y + r.h);
|
|
618
|
+
const at = (it, dy) => {
|
|
619
|
+
const r = { x: it.box.x, y: it.box.y + dy, w: it.box.w, h: it.box.h };
|
|
620
|
+
return { r, t: it.tail ? { x: it.tail.x - 1, y: r.y + r.h, w: 9, h: Math.max(0, it.tail.y - (r.y + r.h)) } : null };
|
|
621
|
+
};
|
|
622
|
+
const first = (it, dys, lists) => dys.find((dy) => { const { r, t } = at(it, dy); return !lists.some((l) => hit(l, r) || (t && hit(l, t))); });
|
|
613
623
|
for (const it of order) {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
break;
|
|
620
|
-
}
|
|
624
|
+
let dy = first(it, [0, -lift], [taken, soft]);
|
|
625
|
+
if (dy === undefined && it.calling) dy = first(it, [-2 * lift], [taken, soft]) ?? first(it, [0, -lift, -2 * lift], [taken]);
|
|
626
|
+
if (dy === undefined) continue;
|
|
627
|
+
const { r, t } = at(it, dy);
|
|
628
|
+
taken.push(r); if (t) taken.push(t); out.push({ ...it, dy });
|
|
621
629
|
}
|
|
622
630
|
return out;
|
|
623
631
|
}
|