@bigmistqke/workpad 0.0.0-stage → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +781 -2
- package/bin/workpad.ts +1509 -0
- package/dist/_virtual/_@oxc-project_runtime@0.156.0/helpers/esm/taggedTemplateLiteral.js +5 -0
- package/dist/bin/workpad.js +1190 -0
- package/dist/client/runtime.js +1194 -0
- package/dist/define.js +27 -0
- package/dist/plugins/alert/README.md +18 -0
- package/dist/plugins/alert/index.js +40 -0
- package/dist/plugins/alert/public/style.css +40 -0
- package/dist/plugins/ansi/README.md +36 -0
- package/dist/plugins/ansi/index.js +63 -0
- package/dist/plugins/ansi/public/style.css +28 -0
- package/dist/plugins/badge/README.md +32 -0
- package/dist/plugins/badge/index.js +28 -0
- package/dist/plugins/badge/public/style.css +30 -0
- package/dist/plugins/chat/README.md +81 -0
- package/dist/plugins/chat/index.js +96 -0
- package/dist/plugins/chat/public/client.js +3 -0
- package/dist/plugins/chat/public/style.css +143 -0
- package/dist/plugins/claude/README.md +116 -0
- package/dist/plugins/claude/channel.js +130 -0
- package/dist/plugins/claude/doctor.js +316 -0
- package/dist/plugins/claude/index.js +199 -0
- package/dist/plugins/claude/session.js +68 -0
- package/dist/plugins/code/README.md +54 -0
- package/dist/plugins/code/diff.js +75 -0
- package/dist/plugins/code/index.js +105 -0
- package/dist/plugins/code/public/client.js +1 -0
- package/dist/plugins/code/public/style.css +99 -0
- package/dist/plugins/comments/README.md +30 -0
- package/dist/plugins/comments/index.js +39 -0
- package/dist/plugins/comments/public/client.js +24 -0
- package/dist/plugins/comments/public/style.css +224 -0
- package/dist/plugins/details/README.md +24 -0
- package/dist/plugins/details/index.js +28 -0
- package/dist/plugins/details/public/style.css +7 -0
- package/dist/plugins/draft/README.md +14 -0
- package/dist/plugins/draft/index.js +27 -0
- package/dist/plugins/draft/public/style.css +21 -0
- package/dist/plugins/forge/README.md +22 -0
- package/dist/plugins/forge/index.js +83 -0
- package/dist/plugins/form/README.md +126 -0
- package/dist/plugins/form/index.js +179 -0
- package/dist/plugins/form/public/style.css +172 -0
- package/dist/plugins/grid/README.md +45 -0
- package/dist/plugins/grid/index.js +35 -0
- package/dist/plugins/grid/public/style.css +98 -0
- package/dist/plugins/iframe/README.md +18 -0
- package/dist/plugins/iframe/index.js +49 -0
- package/dist/plugins/iframe/public/style.css +28 -0
- package/dist/plugins/index.js +44 -0
- package/dist/plugins/lightbox/README.md +24 -0
- package/dist/plugins/lightbox/index.js +20 -0
- package/dist/plugins/lightbox/public/client.js +4 -0
- package/dist/plugins/lightbox/public/style.css +66 -0
- package/dist/plugins/mermaid/README.md +21 -0
- package/dist/plugins/mermaid/index.js +42 -0
- package/dist/plugins/mermaid/public/style.css +21 -0
- package/dist/plugins/opencode/README.md +48 -0
- package/dist/plugins/opencode/channel.js +92 -0
- package/dist/plugins/opencode/index.js +64 -0
- package/dist/plugins/opencode/shim.js +150 -0
- package/dist/plugins/quiet/README.md +26 -0
- package/dist/plugins/quiet/index.js +23 -0
- package/dist/plugins/tabs/README.md +44 -0
- package/dist/plugins/tabs/index.js +75 -0
- package/dist/plugins/tabs/public/client.js +1 -0
- package/dist/plugins/tabs/public/style.css +98 -0
- package/dist/plugins/terminal/README.md +63 -0
- package/dist/plugins/terminal/host.js +338 -0
- package/dist/plugins/terminal/index.js +170 -0
- package/dist/plugins/terminal/modes.js +32 -0
- package/dist/plugins/terminal/public/client.js +41 -0
- package/dist/plugins/terminal/public/style.css +511 -0
- package/dist/plugins/terminal/server.js +256 -0
- package/dist/plugins/terminal/sizes.js +58 -0
- package/dist/plugins/timeline/README.md +48 -0
- package/dist/plugins/timeline/index.js +83 -0
- package/dist/plugins/timeline/public/style.css +138 -0
- package/dist/plugins/toc/README.md +40 -0
- package/dist/plugins/toc/index.js +29 -0
- package/dist/plugins/toc/public/client.js +1 -0
- package/dist/plugins/toc/public/style.css +117 -0
- package/dist/plugins/window/README.md +51 -0
- package/dist/plugins/window/index.js +91 -0
- package/dist/solid/index.js +41 -0
- package/dist/src/agents.js +81 -0
- package/dist/src/anchors.js +52 -0
- package/dist/src/api.js +282 -0
- package/dist/src/archive.js +49 -0
- package/dist/src/auth.js +67 -0
- package/dist/src/build.js +159 -0
- package/dist/src/client/page/base.css +1762 -0
- package/dist/src/client/page/main.js +2098 -0
- package/dist/src/config.js +76 -0
- package/dist/src/document.js +93 -0
- package/dist/src/entries.js +264 -0
- package/dist/src/events.js +322 -0
- package/dist/src/files.js +47 -0
- package/dist/src/formstate.js +65 -0
- package/dist/src/help.js +211 -0
- package/dist/src/html.js +34 -0
- package/dist/src/hub.js +341 -0
- package/dist/src/icons.js +6 -0
- package/dist/src/listen.js +132 -0
- package/dist/src/listeners.js +64 -0
- package/dist/src/load.js +81 -0
- package/dist/src/loader.js +179 -0
- package/dist/src/migrate.js +314 -0
- package/dist/src/package.js +52 -0
- package/dist/src/pages/blocks.js +67 -0
- package/dist/src/pages/board.js +59 -0
- package/dist/src/pages/index.js +27 -0
- package/dist/src/pages/markdown.js +863 -0
- package/dist/src/pages/render.js +665 -0
- package/dist/src/pages/window.js +29 -0
- package/dist/src/plugins.js +283 -0
- package/dist/src/public.js +24 -0
- package/dist/src/rename.js +130 -0
- package/dist/src/running.js +146 -0
- package/dist/src/serve.js +188 -0
- package/dist/src/server.js +1105 -0
- package/dist/src/site.js +179 -0
- package/dist/src/state.js +155 -0
- package/dist/src/status.js +323 -0
- package/dist/src/thread.js +68 -0
- package/dist/src/watchdog.js +38 -0
- package/dist/src/ws.js +128 -0
- package/package.json +154 -4
- package/plugins/README.md +304 -0
- package/plugins/alert/README.md +18 -0
- package/plugins/alert/index.ts +50 -0
- package/plugins/alert/public/style.css +40 -0
- package/plugins/ansi/README.md +36 -0
- package/plugins/ansi/index.ts +95 -0
- package/plugins/ansi/public/style.css +28 -0
- package/plugins/badge/README.md +32 -0
- package/plugins/badge/index.ts +36 -0
- package/plugins/badge/public/style.css +30 -0
- package/plugins/chat/README.md +81 -0
- package/plugins/chat/autosize.ts +139 -0
- package/plugins/chat/client.tsx +187 -0
- package/plugins/chat/index.ts +146 -0
- package/plugins/chat/public/style.css +143 -0
- package/plugins/claude/README.md +116 -0
- package/plugins/claude/channel.ts +136 -0
- package/plugins/claude/doctor.ts +274 -0
- package/plugins/claude/index.ts +248 -0
- package/plugins/claude/session.ts +70 -0
- package/plugins/code/README.md +54 -0
- package/plugins/code/client.ts +73 -0
- package/plugins/code/diff.ts +96 -0
- package/plugins/code/index.ts +136 -0
- package/plugins/code/public/style.css +99 -0
- package/plugins/comments/README.md +30 -0
- package/plugins/comments/client.ts +723 -0
- package/plugins/comments/index.ts +48 -0
- package/plugins/comments/public/style.css +224 -0
- package/plugins/details/README.md +24 -0
- package/plugins/details/index.ts +30 -0
- package/plugins/details/public/style.css +7 -0
- package/plugins/draft/README.md +14 -0
- package/plugins/draft/index.ts +23 -0
- package/plugins/draft/public/style.css +21 -0
- package/plugins/forge/README.md +22 -0
- package/plugins/forge/index.ts +84 -0
- package/plugins/form/README.md +126 -0
- package/plugins/form/index.ts +199 -0
- package/plugins/form/public/style.css +172 -0
- package/plugins/grid/README.md +45 -0
- package/plugins/grid/index.ts +41 -0
- package/plugins/grid/public/style.css +98 -0
- package/plugins/iframe/README.md +18 -0
- package/plugins/iframe/index.ts +53 -0
- package/plugins/iframe/public/style.css +28 -0
- package/plugins/index.ts +49 -0
- package/plugins/lightbox/README.md +24 -0
- package/plugins/lightbox/client.ts +109 -0
- package/plugins/lightbox/index.ts +25 -0
- package/plugins/lightbox/public/style.css +66 -0
- package/plugins/mermaid/README.md +21 -0
- package/plugins/mermaid/index.ts +46 -0
- package/plugins/mermaid/public/style.css +21 -0
- package/plugins/opencode/README.md +48 -0
- package/plugins/opencode/channel.ts +112 -0
- package/plugins/opencode/index.ts +91 -0
- package/plugins/opencode/shim.ts +166 -0
- package/plugins/quiet/README.md +26 -0
- package/plugins/quiet/index.ts +28 -0
- package/plugins/tabs/README.md +44 -0
- package/plugins/tabs/client.ts +96 -0
- package/plugins/tabs/index.ts +103 -0
- package/plugins/tabs/public/style.css +98 -0
- package/plugins/terminal/README.md +63 -0
- package/plugins/terminal/client.ts +391 -0
- package/plugins/terminal/host.ts +434 -0
- package/plugins/terminal/index.ts +199 -0
- package/plugins/terminal/modes.ts +30 -0
- package/plugins/terminal/public/style.css +225 -0
- package/plugins/terminal/server.ts +266 -0
- package/plugins/terminal/sizes.ts +72 -0
- package/plugins/timeline/README.md +48 -0
- package/plugins/timeline/index.ts +82 -0
- package/plugins/timeline/public/style.css +138 -0
- package/plugins/toc/README.md +40 -0
- package/plugins/toc/client.ts +236 -0
- package/plugins/toc/index.ts +55 -0
- package/plugins/toc/public/style.css +117 -0
- package/plugins/window/README.md +51 -0
- package/plugins/window/index.ts +92 -0
- package/skills/workpad/SKILL.md +588 -0
- package/solid/index.tsx +80 -0
- package/src/agents.ts +133 -0
- package/src/anchors.ts +85 -0
- package/src/api.ts +444 -0
- package/src/archive.ts +75 -0
- package/src/auth.ts +99 -0
- package/src/build.ts +213 -0
- package/src/client/base.ts +19 -0
- package/src/client/forms.ts +420 -0
- package/src/client/markdown.ts +147 -0
- package/src/client/page/base.css +1762 -0
- package/src/client/page/board.ts +137 -0
- package/src/client/page/elsewhere.ts +153 -0
- package/src/client/page/fresh.ts +343 -0
- package/src/client/page/heading-link.ts +32 -0
- package/src/client/page/live.ts +134 -0
- package/src/client/page/main.ts +398 -0
- package/src/client/page/orphans.ts +64 -0
- package/src/client/page/place.ts +260 -0
- package/src/client/page/sort.ts +49 -0
- package/src/client/page/sticky.ts +58 -0
- package/src/client/page/theme.ts +36 -0
- package/src/client/page/view.ts +9 -0
- package/src/client/page/waiting.ts +278 -0
- package/src/client/page/window.ts +40 -0
- package/src/client/reactive.ts +19 -0
- package/src/client/read.ts +111 -0
- package/src/client/runtime.ts +499 -0
- package/src/client/scroll.ts +90 -0
- package/src/client/toast.ts +99 -0
- package/src/config.ts +111 -0
- package/src/define.ts +997 -0
- package/src/document.ts +191 -0
- package/src/entries.ts +352 -0
- package/src/events.ts +463 -0
- package/src/files.ts +74 -0
- package/src/formstate.ts +72 -0
- package/src/help.ts +238 -0
- package/src/html.ts +44 -0
- package/src/hub.ts +426 -0
- package/src/icons.ts +18 -0
- package/src/listen.ts +171 -0
- package/src/listeners.ts +89 -0
- package/src/load.ts +113 -0
- package/src/loader.ts +227 -0
- package/src/migrate.ts +352 -0
- package/src/package.ts +62 -0
- package/src/pages/blocks.ts +106 -0
- package/src/pages/board.ts +89 -0
- package/src/pages/index.ts +79 -0
- package/src/pages/markdown.ts +976 -0
- package/src/pages/render.ts +748 -0
- package/src/pages/window.ts +69 -0
- package/src/plugins.ts +319 -0
- package/src/public.ts +39 -0
- package/src/rename.ts +147 -0
- package/src/running.ts +171 -0
- package/src/serve.ts +221 -0
- package/src/server.ts +1385 -0
- package/src/site.ts +170 -0
- package/src/state.ts +327 -0
- package/src/status.ts +466 -0
- package/src/thread.ts +98 -0
- package/src/watchdog.ts +53 -0
- package/src/ws.ts +155 -0
package/src/status.ts
ADDED
|
@@ -0,0 +1,466 @@
|
|
|
1
|
+
// What waits for the reader on a page, and how to say what is open on it: one definition for the
|
|
2
|
+
// server (`workpad status`, the hooks, the header and overview) and the page in the browser
|
|
3
|
+
// alike. No Node in it, so the browser runtime uses the same module.
|
|
4
|
+
//
|
|
5
|
+
// What waits for the reader is their to-do, not whether an agent's process runs: a question they
|
|
6
|
+
// haven't answered, and a message from the agent they haven't read.
|
|
7
|
+
//
|
|
8
|
+
// A question is a named form; its answers are the reader's events of its name. It stands:
|
|
9
|
+
// open no answer yet, or the agent asks again: a wait or watch asks for it by name after its
|
|
10
|
+
// answer was handled
|
|
11
|
+
// pending answered, but no agent has received the answer yet
|
|
12
|
+
// received an agent has the answer, and hasn't handled it yet
|
|
13
|
+
// handled an agent is done with its latest answer
|
|
14
|
+
// held an agent put it on hold, with a reason ("waits for !2145"): it doesn't wait for the
|
|
15
|
+
// reader now, and it isn't settled either: released, it is open again (HOLD, RELEASE);
|
|
16
|
+
// an answer after all ends the hold, as it does a settle
|
|
17
|
+
// settled answered elsewhere: an agent settled it (SETTLE)
|
|
18
|
+
//
|
|
19
|
+
// An answer (any event of the reader's an agent is handed) is received once a wait or watch printed
|
|
20
|
+
// it, or the agent said it has it (`workpad:received`), so no other one takes it too; it is
|
|
21
|
+
// handled once the agent is done with it: it said so (`workpad handled`, a `workpad:handled`
|
|
22
|
+
// naming it), or replied to it, after it:
|
|
23
|
+
// an event of the agent's whose `replyTo` is the answer or the answer's own `replyTo` (a reply in
|
|
24
|
+
// the same thread), or whose `chat` is the answer's chat or, for a form's answer, the form's name.
|
|
25
|
+
// A received answer not handled is handed out again by the next wait on its page.
|
|
26
|
+
// Whether an agent listens is told apart where it helps when something goes wrong: the toast after
|
|
27
|
+
// a submit, and `workpad status` (`unheard`: open, and no wait or watch takes it).
|
|
28
|
+
//
|
|
29
|
+
// A message is an agent's event of a type a plugin declares `forReader` (a reply in a comment
|
|
30
|
+
// thread, a chat message). It is unread until a `workpad:read` event from the reader names it,
|
|
31
|
+
// which the page appends once the message has been in view; one the agent replaced (`replaces`)
|
|
32
|
+
// no longer counts, nor one in a chat no longer on the page (its form or block taken off): the page
|
|
33
|
+
// keeps those folded after its content, as a record, not as something to read.
|
|
34
|
+
import type { FormState, Listener, Marks, PageStatus, WpEvent } from './define.ts'
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Can this be a question's name, the type of its answers? Not with a colon: types with one belong
|
|
38
|
+
* to workpad (`workpad:handled`) or a plugin (`comments:reply`, `chat:message`),
|
|
39
|
+
* named after its owner, so a form's answers never mix with their events.
|
|
40
|
+
*/
|
|
41
|
+
export const isQuestionName = (name: string): boolean => !!name && !name.includes(':')
|
|
42
|
+
|
|
43
|
+
/** The states, in the order a reader should see them: what waits on them first. */
|
|
44
|
+
export const STATES: FormState[] = ['open', 'pending', 'received', 'handled', 'held', 'settled']
|
|
45
|
+
|
|
46
|
+
/** Why a question is held, who held it and when (HOLD). */
|
|
47
|
+
export interface Hold {
|
|
48
|
+
reason: string
|
|
49
|
+
/** when it was held: the hold's time */
|
|
50
|
+
since: string
|
|
51
|
+
/** who held it, when the command line knew */
|
|
52
|
+
by: string | null
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Does this event of an agent's name `name` among its `forms`? (SETTLE, HOLD, RELEASE) */
|
|
56
|
+
const names = (e: WpEvent, type: string, name: string): boolean =>
|
|
57
|
+
e.type === type && !fromReader(e) && Array.isArray(e.data?.forms) && (e.data.forms as unknown[]).includes(name)
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* The hold on question `name` in force, or null: the latest HOLD naming it, unless a RELEASE or a
|
|
61
|
+
* SETTLE came after it. Only the command line's count, as with SETTLE.
|
|
62
|
+
*/
|
|
63
|
+
export function holdOf(name: string, events: WpEvent[]): Hold | null {
|
|
64
|
+
for (let i = events.length - 1; i >= 0; i--) {
|
|
65
|
+
const e = events[i]!
|
|
66
|
+
if (names(e, RELEASE, name) || names(e, SETTLE, name)) return null
|
|
67
|
+
if (names(e, HOLD, name)) return { reason: String(e.data.reason ?? ''), since: e.ts, by: e.by ?? null }
|
|
68
|
+
}
|
|
69
|
+
return null
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The questions of a page with the nested ones settled: a form in a message in the chat under a form
|
|
74
|
+
* (`<form>/<name>`) that is still open once that form is decided (handled) asks nothing any more.
|
|
75
|
+
* The server and the page both go by it, so they never disagree.
|
|
76
|
+
*/
|
|
77
|
+
export function settleNested<T extends { name: string; state: FormState }>(questions: T[]): T[] {
|
|
78
|
+
const handled = new Set(questions.filter(q => q.state === 'handled').map(q => q.name))
|
|
79
|
+
return questions.map(q => {
|
|
80
|
+
const outer = q.name.includes('/') ? q.name.slice(0, q.name.indexOf('/')) : null
|
|
81
|
+
return outer && q.state === 'open' && handled.has(outer) ? { ...q, state: 'settled' as const } : q
|
|
82
|
+
})
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Was this event written by the reader (a form submit, a comment), not by an agent? Agents write
|
|
87
|
+
* through the command line, which marks its events `from: 'cli'`: an agent's reply, a claim, a mark.
|
|
88
|
+
*/
|
|
89
|
+
export const fromReader = (event: WpEvent): boolean => event.from !== 'cli'
|
|
90
|
+
|
|
91
|
+
/** Does a listener's page filter cover `page`? An empty filter covers every page. */
|
|
92
|
+
export const covers = (l: Listener, page: string): boolean =>
|
|
93
|
+
!l.page || page === l.page || page.startsWith(l.page + '/')
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Where the question `name` stands. `events` are the page's, `listeners` the running waits and
|
|
97
|
+
* watches that cover the page (only one asking for it by name counts: it asks again), `marks`
|
|
98
|
+
* what became of each answer. `last` is its latest answer: the reader's latest event of its name.
|
|
99
|
+
* Open, and settled (SETTLE) since that answer or without one: settled.
|
|
100
|
+
*/
|
|
101
|
+
export function questionState(
|
|
102
|
+
name: string,
|
|
103
|
+
events: WpEvent[],
|
|
104
|
+
listeners: Listener[],
|
|
105
|
+
marks: Marks,
|
|
106
|
+
): { state: FormState; last: WpEvent | undefined; hold: Hold | null } {
|
|
107
|
+
const last = events.findLast(e => e.type === name && fromReader(e))
|
|
108
|
+
const askedAgain = listeners.some(l => l.type === name)
|
|
109
|
+
// settled by an agent (`workpad settle`) since its last answer: an answer after it opens it again
|
|
110
|
+
const settled = events.findLastIndex(e => names(e, SETTLE, name))
|
|
111
|
+
const shut = settled >= 0 && settled > (last ? events.lastIndexOf(last) : -1)
|
|
112
|
+
const state: FormState = !last
|
|
113
|
+
? 'open'
|
|
114
|
+
: marks.handled(last.id)
|
|
115
|
+
? askedAgain
|
|
116
|
+
? 'open'
|
|
117
|
+
: 'handled'
|
|
118
|
+
: marks.received(last.id)
|
|
119
|
+
? 'received'
|
|
120
|
+
: 'pending'
|
|
121
|
+
// held by an agent since its last answer, and not released: an answer after it ends it, as it
|
|
122
|
+
// does a settle
|
|
123
|
+
const hold = holdOf(name, last ? events.slice(events.lastIndexOf(last) + 1) : events)
|
|
124
|
+
// only a question that waits for the reader settles or is held: an answer in stays the agent's to see to
|
|
125
|
+
return { state: state !== 'open' ? state : hold ? 'held' : shut ? 'settled' : 'open', last, hold }
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** Does anyone listen for the answer to question `name`? Not: it is unheard. */
|
|
129
|
+
export const heard = (name: string, listeners: Listener[]): boolean =>
|
|
130
|
+
listeners.some(l => l.type === null || l.type === name)
|
|
131
|
+
|
|
132
|
+
/** The type of the event the page appends once messages were in view: `{ ids }`. */
|
|
133
|
+
export const READ = 'workpad:read'
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The type of the event `workpad scroll` appends: show the reader a place on a page, `{ to }`. A
|
|
137
|
+
* page applies the latest one that came after it loaded, as it comes or once its live channel is
|
|
138
|
+
* back.
|
|
139
|
+
*/
|
|
140
|
+
export const SCROLL = 'workpad:scroll'
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* The agent's messages on a page the reader hasn't read: events from the agent of a type
|
|
144
|
+
* `isMessage` says is for the reader, not named by a `workpad:read` from the reader, not
|
|
145
|
+
* replaced by a later one (`replaces`), and, given the page's `chats`, not in a chat (`data.chat`)
|
|
146
|
+
* that is no longer among them.
|
|
147
|
+
*/
|
|
148
|
+
export function unreadMessages(
|
|
149
|
+
events: WpEvent[],
|
|
150
|
+
isMessage: (type: string) => boolean,
|
|
151
|
+
chats?: ReadonlySet<string> | null,
|
|
152
|
+
): WpEvent[] {
|
|
153
|
+
const read = new Set<string>()
|
|
154
|
+
const replaced = new Set<string>()
|
|
155
|
+
for (const e of events) {
|
|
156
|
+
if (e.type === READ && fromReader(e))
|
|
157
|
+
for (const id of (e.data?.ids as unknown[] | undefined) ?? []) read.add(String(id))
|
|
158
|
+
if (e.data?.replaces) replaced.add(String(e.data.replaces))
|
|
159
|
+
}
|
|
160
|
+
const offPage = (e: WpEvent) => !!chats && typeof e.data?.chat === 'string' && !chats.has(e.data.chat)
|
|
161
|
+
return events.filter(
|
|
162
|
+
e => !fromReader(e) && isMessage(e.type) && !read.has(e.id) && !replaced.has(e.id) && !offPage(e),
|
|
163
|
+
)
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** How many things on a page wait for the reader: its open questions and unread messages. */
|
|
167
|
+
export const awaiting = (p: Pick<PageStatus, 'open' | 'unread'>): number => p.open.length + p.unread.length
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* The type of the event that says the agent handled another (`workpad handled`): `{ event: <its id> }`.
|
|
171
|
+
* Named for picking an answer up; older logs wrote it when a wait printed one, so those stay handled.
|
|
172
|
+
*/
|
|
173
|
+
export const HANDLED = 'workpad:handled'
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* The type of the event that settles questions (`workpad settle`): `{ forms: [<name>…] }`. An
|
|
177
|
+
* agent settles those a conclusion reached elsewhere (in a chat, the design moving on) answered:
|
|
178
|
+
* open, they'd stay "unanswered" for good. Only the command line's count, as with RECEIVED.
|
|
179
|
+
*/
|
|
180
|
+
export const SETTLE = 'workpad:settle'
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* The type of the event that holds questions (`workpad hold`): `{ forms: [<name>…], reason }`. A
|
|
184
|
+
* held question waits on something other than the reader (another merge request, a decision
|
|
185
|
+
* elsewhere): it doesn't count as waiting for them, and it isn't settled: once released (RELEASE)
|
|
186
|
+
* it is open again. The page shows it held, with the reason. Only the command line's count.
|
|
187
|
+
*/
|
|
188
|
+
export const HOLD = 'workpad:hold'
|
|
189
|
+
|
|
190
|
+
/** The type of the event that releases held questions (`workpad release`): `{ forms: [<name>…] }`. */
|
|
191
|
+
export const RELEASE = 'workpad:release'
|
|
192
|
+
|
|
193
|
+
/** The type of the event a wait or watch appends when it prints an answer: `{ event: <its id> }`. */
|
|
194
|
+
export const RECEIVED = 'workpad:received'
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* The type of the event that subscribes an identity to a page (its `by`): `workpad new` for the
|
|
198
|
+
* page it makes, `workpad listen --page` for the pages it listens on (and `listen --channel` without
|
|
199
|
+
* a page for every page, on the page `''`; a subscription covers the pages below it), when it is someone (`--agent`,
|
|
200
|
+
* `WORKPAD_AGENT`, a plugin's session). A subscription outlasts the command: what is left on the page
|
|
201
|
+
* is the subscriber's to see to (the turn-end check), and a channel follows it. The colon keeps it
|
|
202
|
+
* apart from form names, which are event types too.
|
|
203
|
+
*/
|
|
204
|
+
export const SUBSCRIBE = 'workpad:subscribe'
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* The type of the event that takes an agent's subscriptions off (`workpad agent unsubscribe`):
|
|
208
|
+
* `{ agent, page? }`, on no page; without a page, all of them. Its `by` is whoever ran it.
|
|
209
|
+
*/
|
|
210
|
+
export const UNSUBSCRIBE = 'workpad:unsubscribe'
|
|
211
|
+
|
|
212
|
+
/** The subscriptions in force, in the order they were made: each not taken off by a later UNSUBSCRIBE. */
|
|
213
|
+
export function subscriptions(events: WpEvent[]): WpEvent[] {
|
|
214
|
+
let live: WpEvent[] = []
|
|
215
|
+
for (const e of events) {
|
|
216
|
+
if (e.type === SUBSCRIBE && e.by) live.push(e)
|
|
217
|
+
else if (e.type === UNSUBSCRIBE && typeof e.data?.agent === 'string') {
|
|
218
|
+
const { agent, page } = e.data as { agent: string; page?: string }
|
|
219
|
+
live = live.filter(s => s.by !== agent || (page !== undefined && s.page !== page))
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
return live
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* The type of the event that names an agent (its `by`): `{ name }`, on no page, so a page deleted
|
|
227
|
+
* keeps it. `workpad agent name` keeps one per agent: setting one drops the one before.
|
|
228
|
+
*/
|
|
229
|
+
export const NAME = 'workpad:name'
|
|
230
|
+
|
|
231
|
+
/** Each agent's name, by its id: the latest each gave itself. */
|
|
232
|
+
export function namesOf(events: WpEvent[]): Map<string, string> {
|
|
233
|
+
const names = new Map<string, string>()
|
|
234
|
+
for (const e of events)
|
|
235
|
+
if (e.type === NAME && e.from === 'cli' && e.by && typeof e.data?.name === 'string') names.set(e.by, e.data.name)
|
|
236
|
+
return names
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** The ids of the answers a wait or watch printed. Only the command line's marks count. */
|
|
240
|
+
export function receivedIds(events: WpEvent[]): Set<string> {
|
|
241
|
+
return new Set(events.filter(e => e.type === RECEIVED && !fromReader(e)).map(e => String(e.data?.event)))
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Where a channel catches up from, for `agent` on `page`: the subscription it took the page on with
|
|
246
|
+
* through its channel (`workpad listen --channel`, data `{ channel: true }`, on the page, one above
|
|
247
|
+
* it, or every page `''`). Before it, what someone already received is the past, and is skipped
|
|
248
|
+
* (handOut's skipReceivedBefore); the reader's answers never are. Null when the page is the agent's
|
|
249
|
+
* otherwise too (it made it, or listened on it by name): then it has all of it.
|
|
250
|
+
*/
|
|
251
|
+
export function channelSince(events: WpEvent[], agent: string, page: string): string | null {
|
|
252
|
+
let since: string | null = null
|
|
253
|
+
for (const e of subscriptions(events)) {
|
|
254
|
+
if (e.by !== agent) continue
|
|
255
|
+
if (!(e.page === '' || e.page === page || page.startsWith(`${e.page}/`))) continue
|
|
256
|
+
if (!e.data?.channel) return null
|
|
257
|
+
since ??= e.id
|
|
258
|
+
}
|
|
259
|
+
return since
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* The ids of the reader's events the agent handled: acknowledged (`workpad:handled`), or replied
|
|
264
|
+
* to after them (see the top of this file). The log is gone through once: each of the reader's
|
|
265
|
+
* events is filed under what a reply would name (its id, its thread, its chat, its form), and an
|
|
266
|
+
* agent's event takes out all filed under what it names.
|
|
267
|
+
*/
|
|
268
|
+
export function handledIds(events: WpEvent[]): Set<string> {
|
|
269
|
+
const done = new Set<string>()
|
|
270
|
+
const filed = new Map<string, string[]>()
|
|
271
|
+
const file = (key: string, id: string) => filed.set(key, [...(filed.get(key) ?? []), id])
|
|
272
|
+
const take = (key: string) => {
|
|
273
|
+
for (const id of filed.get(key) ?? []) done.add(id)
|
|
274
|
+
filed.delete(key)
|
|
275
|
+
}
|
|
276
|
+
for (const e of events) {
|
|
277
|
+
const d = (e.data ?? {}) as Record<string, unknown>
|
|
278
|
+
if (e.type === HANDLED) {
|
|
279
|
+
if (!fromReader(e)) done.add(String(d.event))
|
|
280
|
+
continue
|
|
281
|
+
}
|
|
282
|
+
if (e.type.startsWith('workpad:')) continue
|
|
283
|
+
if (fromReader(e)) {
|
|
284
|
+
file(`id:${e.id}`, e.id)
|
|
285
|
+
if (d.replyTo) file(`id:${d.replyTo}`, e.id)
|
|
286
|
+
if (typeof d.chat === 'string') file(`chat:${d.chat}`, e.id)
|
|
287
|
+
// a form's answer: its chat is the form's name; a form in a message (`<chat>/<name>`) has no
|
|
288
|
+
// chat of its own, the chat it is in is its chat
|
|
289
|
+
else file(`chat:${e.type.includes('/') ? e.type.slice(0, e.type.indexOf('/')) : e.type}`, e.id)
|
|
290
|
+
continue
|
|
291
|
+
}
|
|
292
|
+
if (d.replyTo) take(`id:${d.replyTo}`)
|
|
293
|
+
if (typeof d.chat === 'string') take(`chat:${d.chat}`)
|
|
294
|
+
}
|
|
295
|
+
return done
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* The chats on a page waiting for the agent: those whose latest message is the reader's. A chat is
|
|
300
|
+
* what events share a `chat` name.
|
|
301
|
+
*/
|
|
302
|
+
export function awaitingReply(events: WpEvent[]): string[] {
|
|
303
|
+
const lastBy = new Map<string, WpEvent>()
|
|
304
|
+
for (const e of events)
|
|
305
|
+
if (typeof e.data?.chat === 'string' && !e.type.startsWith('workpad:')) lastBy.set(e.data.chat, e)
|
|
306
|
+
return [...lastBy].filter(([, e]) => fromReader(e)).map(([chat]) => chat)
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** A page as the snapshot needs it; its questions (`asks`) are its named forms. */
|
|
310
|
+
export interface StatusPage {
|
|
311
|
+
id: string
|
|
312
|
+
file: string
|
|
313
|
+
title: string
|
|
314
|
+
asks: string[]
|
|
315
|
+
/** the chats it shows (a form's, a chat block's); without them, every chat's messages count */
|
|
316
|
+
chats?: string[]
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
/** What a project's status is made of: no disk in it, the server reads and hands it over. */
|
|
320
|
+
export interface StatusInput {
|
|
321
|
+
pages: StatusPage[]
|
|
322
|
+
/** the whole log */
|
|
323
|
+
events: WpEvent[]
|
|
324
|
+
/** the running waits and watches covering a page */
|
|
325
|
+
listeners: (page: string) => Listener[]
|
|
326
|
+
/** the event types handed to an agent (a form's answers; plugins' events declared handOut) */
|
|
327
|
+
forAgent: (type: string) => boolean
|
|
328
|
+
/** the event types that are messages for the reader (declared forReader) */
|
|
329
|
+
forReader: (type: string) => boolean
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/** A project's status at one moment: what `workpad status`, the page lists and the log's marks take. */
|
|
333
|
+
export interface ProjectStatus {
|
|
334
|
+
/** per page, in the order given */
|
|
335
|
+
pages: PageStatus[]
|
|
336
|
+
/** the answers a wait or watch printed, by id */
|
|
337
|
+
received: Set<string>
|
|
338
|
+
/** the answers the agent handled, by id */
|
|
339
|
+
handled: Set<string>
|
|
340
|
+
/** when the reader last sent something on a page (what the agent appended doesn't count) */
|
|
341
|
+
lastAnswered: (page: string) => string | null
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* A project's status from its pages, its log and who listens: per page its questions and where each
|
|
346
|
+
* stands, what is open and unheard, the agent's messages unread, the reader's events not received,
|
|
347
|
+
* and who listens; which events were received; when each page was last answered. The log is
|
|
348
|
+
* gone through once.
|
|
349
|
+
*/
|
|
350
|
+
/**
|
|
351
|
+
* Who is subscribed to page `id`: to it, to a page above it (`design` covers `design/items`, as a
|
|
352
|
+
* listen on it does), or to every page (`''`, `workpad listen --channel` without a page), each once.
|
|
353
|
+
*/
|
|
354
|
+
function subscribersOf(subscribers: Map<string, string[]>, id: string): string[] {
|
|
355
|
+
const all = new Set<string>()
|
|
356
|
+
for (const [page, who] of subscribers)
|
|
357
|
+
if (page === '' || page === id || id.startsWith(`${page}/`)) for (const s of who) all.add(s)
|
|
358
|
+
return [...all]
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
export function projectStatus({ pages, events, listeners, forAgent, forReader }: StatusInput): ProjectStatus {
|
|
362
|
+
const received = receivedIds(events)
|
|
363
|
+
const handled = handledIds(events)
|
|
364
|
+
const marks: Marks = { received: id => received.has(id), handled: id => handled.has(id) }
|
|
365
|
+
const byPage = new Map<string, WpEvent[]>()
|
|
366
|
+
const subscribers = new Map<string, string[]>()
|
|
367
|
+
const last = new Map<string, string>()
|
|
368
|
+
for (const e of events) {
|
|
369
|
+
let list = byPage.get(e.page)
|
|
370
|
+
if (!list) byPage.set(e.page, (list = []))
|
|
371
|
+
list.push(e)
|
|
372
|
+
if (fromReader(e)) last.set(e.page, e.ts)
|
|
373
|
+
}
|
|
374
|
+
for (const e of subscriptions(events)) {
|
|
375
|
+
const now = subscribers.get(e.page) ?? []
|
|
376
|
+
if (!now.includes(e.by!)) subscribers.set(e.page, [...now, e.by!])
|
|
377
|
+
}
|
|
378
|
+
return {
|
|
379
|
+
received,
|
|
380
|
+
handled,
|
|
381
|
+
lastAnswered: page => last.get(page) ?? null,
|
|
382
|
+
pages: pages.map(p => {
|
|
383
|
+
const own = byPage.get(p.id) ?? []
|
|
384
|
+
const listening = listeners(p.id)
|
|
385
|
+
// where each question stands, as the page shows it
|
|
386
|
+
const states = p.asks.map(name => ({ name, ...questionState(name, own, listening, marks) }))
|
|
387
|
+
const questions = settleNested(states.map(({ name, state }) => ({ name, state })))
|
|
388
|
+
const open = questions.filter(q => q.state === 'open').map(q => q.name)
|
|
389
|
+
const held = states.flatMap(q => (q.state === 'held' && q.hold ? [{ name: q.name, ...q.hold }] : []))
|
|
390
|
+
return {
|
|
391
|
+
page: p.id,
|
|
392
|
+
title: p.title,
|
|
393
|
+
file: p.file,
|
|
394
|
+
subscribers: subscribersOf(subscribers, p.id),
|
|
395
|
+
asks: p.asks,
|
|
396
|
+
questions,
|
|
397
|
+
open,
|
|
398
|
+
held,
|
|
399
|
+
unheard: open.filter(name => !heard(name, listening)),
|
|
400
|
+
unread: unreadMessages(own, forReader, p.chats ? new Set(p.chats) : null),
|
|
401
|
+
// the reader's events for an agent (a form's answers, plugin events declared handOut): those
|
|
402
|
+
// no agent received, and those received but not handled
|
|
403
|
+
unreceived: own.filter(e => fromReader(e) && forAgent(e.type) && !received.has(e.id) && !handled.has(e.id)),
|
|
404
|
+
unhandled: own.filter(e => fromReader(e) && forAgent(e.type) && received.has(e.id) && !handled.has(e.id)),
|
|
405
|
+
awaitingReply: awaitingReply(own),
|
|
406
|
+
listeners: listening,
|
|
407
|
+
}
|
|
408
|
+
}),
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* What is open on a page, one short line each; the command that picks it up comes last, once.
|
|
414
|
+
* `file` is how to name the page's file to the reader (relative to where they are). Without
|
|
415
|
+
* `waiting`, the questions an agent is already waiting on are left out.
|
|
416
|
+
*/
|
|
417
|
+
export function statusLines(p: PageStatus, file: string, { waiting = true } = {}): string[] {
|
|
418
|
+
const lines: string[] = []
|
|
419
|
+
if (p.unreceived.length)
|
|
420
|
+
lines.push(
|
|
421
|
+
`${p.unreceived.length} answer${p.unreceived.length > 1 ? 's' : ''} no agent received (${[...new Set(p.unreceived.map(e => e.type))].join(', ')})`,
|
|
422
|
+
)
|
|
423
|
+
if (p.unheard.length) lines.push(`nobody listens for ${p.unheard.join(', ')}`)
|
|
424
|
+
if (lines.length) lines.push(`→ workpad listen --wait --page ${file}`)
|
|
425
|
+
// handed out, not yet acted on: the agent replies, or acks them; a wait hands them out again
|
|
426
|
+
if (p.unhandled.length)
|
|
427
|
+
lines.push(
|
|
428
|
+
`${p.unhandled.length} answer${p.unhandled.length > 1 ? 's' : ''} handed out and not handled → reply in its chat, or workpad handled ${p.unhandled.map(e => e.id).join(' ')}`,
|
|
429
|
+
)
|
|
430
|
+
if (p.awaitingReply.length)
|
|
431
|
+
lines.push(`waiting for a reply: the chat${p.awaitingReply.length > 1 ? 's' : ''} ${p.awaitingReply.join(', ')}`)
|
|
432
|
+
const listened = p.open.filter(t => !p.unheard.includes(t))
|
|
433
|
+
if (waiting && listened.length) lines.push(`waiting for the reader: ${listened.join(', ')}`)
|
|
434
|
+
return lines
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Who is subscribed to a page, said to `me`: `you` among them, the others by the name they gave
|
|
439
|
+
* themselves (`names`), else their id, a long one (a session's) cut short.
|
|
440
|
+
*/
|
|
441
|
+
export const whose = (p: PageStatus, me: string | null, names: Map<string, string> = new Map()): string =>
|
|
442
|
+
p.subscribers.length
|
|
443
|
+
? p.subscribers.map(s => (s === me ? 'you' : (names.get(s) ?? (s.length > 16 ? s.slice(0, 8) : s)))).join(', ')
|
|
444
|
+
: 'no one subscribed'
|
|
445
|
+
|
|
446
|
+
/** The type of a chat message: `{ chat, text }` (the chat plugin's; the core shows the agent's as small pages). */
|
|
447
|
+
export const CHAT_MESSAGE = 'chat:message'
|
|
448
|
+
|
|
449
|
+
/** Is this a chat message of the agent's, which shows as a small page (forms and all)? */
|
|
450
|
+
export const isAgentMessage = (e: WpEvent): boolean =>
|
|
451
|
+
e.type === CHAT_MESSAGE && !fromReader(e) && typeof e.data?.chat === 'string'
|
|
452
|
+
|
|
453
|
+
/**
|
|
454
|
+
* The agent's messages in one chat of a page as they show, in order: those a later one replaced
|
|
455
|
+
* (`replaces`) left out.
|
|
456
|
+
*/
|
|
457
|
+
export function agentMessagesIn(events: WpEvent[], page: string, chat: string): WpEvent[] {
|
|
458
|
+
const own = events.filter(e => e.page === page && isAgentMessage(e) && e.data.chat === chat)
|
|
459
|
+
const replaced = new Set(
|
|
460
|
+
own
|
|
461
|
+
.map(e => e.data.replaces)
|
|
462
|
+
.filter(Boolean)
|
|
463
|
+
.map(String),
|
|
464
|
+
)
|
|
465
|
+
return own.filter(e => !replaced.has(e.id))
|
|
466
|
+
}
|
package/src/thread.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// `workpad thread <event id>`: an answer with its question. What an agent is handed (a wait's
|
|
2
|
+
// line, the channel's message) is the answer alone; to act on it, the agent needs what was asked and
|
|
3
|
+
// what was said around it. For a form's answer or a chat message: the page, the form as written
|
|
4
|
+
// under its heading (its options, the chosen ones marked), the answers given to it, and its chat, in
|
|
5
|
+
// order. Read from the page's markdown and the log, so it needs no server.
|
|
6
|
+
import type { WpEvent } from './define.ts'
|
|
7
|
+
import { fromReader, isQuestionName } from './status.ts'
|
|
8
|
+
import { chatForms, formSource } from './pages/markdown.ts'
|
|
9
|
+
|
|
10
|
+
const who = (e: WpEvent) => (fromReader(e) ? 'reader' : 'agent')
|
|
11
|
+
const when = (e: WpEvent) => e.ts.replace('T', ' ').slice(0, 16)
|
|
12
|
+
const indent = (text: string) =>
|
|
13
|
+
text
|
|
14
|
+
.split('\n')
|
|
15
|
+
.map(l => ` ${l}`)
|
|
16
|
+
.join('\n')
|
|
17
|
+
|
|
18
|
+
/** What was chosen or written in an answer, as the reader gave it. */
|
|
19
|
+
function said(e: WpEvent): string {
|
|
20
|
+
const d = (e.data ?? {}) as Record<string, unknown>
|
|
21
|
+
const fields = Object.entries(d).filter(([, v]) => v !== '' && v != null && !(Array.isArray(v) && !v.length))
|
|
22
|
+
const parts = fields.map(([k, v]) => `${k}: ${JSON.stringify(v)}`)
|
|
23
|
+
if (e.action && e.action !== 'send') parts.unshift(`pressed ${e.action}`)
|
|
24
|
+
return parts.join(', ') || `pressed ${e.action ?? 'send'}`
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* The form as written, each option the answer chose marked: `:radio[…]{name=x value=a}` is chosen
|
|
29
|
+
* when the answer's `x` is `a` (or holds it, for a check).
|
|
30
|
+
*/
|
|
31
|
+
function marked(markdown: string, answer: WpEvent | undefined): string {
|
|
32
|
+
if (!answer) return markdown
|
|
33
|
+
const d = (answer.data ?? {}) as Record<string, unknown>
|
|
34
|
+
return markdown
|
|
35
|
+
.split('\n')
|
|
36
|
+
.map(line => {
|
|
37
|
+
const chosen = [...line.matchAll(/:(?:radio|check)\[[^\]]*\]\{([^}]*)\}/g)].some(m => {
|
|
38
|
+
const name = /\bname=("([^"]*)"|(\S+))/.exec(m[1])
|
|
39
|
+
const value = /\bvalue=("([^"]*)"|(\S+))/.exec(m[1])
|
|
40
|
+
if (!name || !value) return false
|
|
41
|
+
const got = d[name[2] ?? name[3]]
|
|
42
|
+
const want = value[2] ?? value[3]
|
|
43
|
+
return Array.isArray(got) ? got.map(String).includes(want) : String(got) === want
|
|
44
|
+
})
|
|
45
|
+
return chosen ? `${line} ← chosen` : line
|
|
46
|
+
})
|
|
47
|
+
.join('\n')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The thread of `id` on its page, as lines to print: the event, its form and its chat. `source` is
|
|
52
|
+
* the page's markdown ('' when it is gone); `events` the page's events.
|
|
53
|
+
*/
|
|
54
|
+
export function thread(id: string, events: WpEvent[], source: string): string {
|
|
55
|
+
const event = events.find(e => e.id === id)
|
|
56
|
+
if (!event) throw new Error(`no event ${id}`)
|
|
57
|
+
const d = (event.data ?? {}) as Record<string, unknown>
|
|
58
|
+
// the form and chat it belongs to: a form's answer is its form's, whose chat is named after it; a
|
|
59
|
+
// chat message is its chat's, which may be a form's
|
|
60
|
+
const name = isQuestionName(event.type) ? event.type : typeof d.chat === 'string' ? d.chat : null
|
|
61
|
+
const out: string[] = [`page ${event.page}`]
|
|
62
|
+
if (!name) {
|
|
63
|
+
out.push('', `${who(event)} ${when(event)} ${event.type} (${event.id}):`, indent(JSON.stringify(d)))
|
|
64
|
+
return out.join('\n')
|
|
65
|
+
}
|
|
66
|
+
// a form in an agent's chat message (`<chat>/<name>`) is written there, not in the page; its chat is
|
|
67
|
+
// the one that message is in
|
|
68
|
+
const chat = name.includes('/') ? name.slice(0, name.indexOf('/')) : name
|
|
69
|
+
const inChat = chat !== name ? chatForms(events, event.page, chat).find(f => f.name === name) : undefined
|
|
70
|
+
const form = inChat ? { heading: null, markdown: inChat.markdown } : formSource(source, name)
|
|
71
|
+
const answers = events.filter(e => e.type === name && fromReader(e))
|
|
72
|
+
if (form) {
|
|
73
|
+
out.push(
|
|
74
|
+
'',
|
|
75
|
+
`form ${name}${inChat ? `, in a message of the chat ${chat}` : form.heading ? `, under "${form.heading}"` : ''}:`,
|
|
76
|
+
indent(marked(form.markdown, answers.includes(event) ? event : answers.at(-1))),
|
|
77
|
+
)
|
|
78
|
+
} else if (isQuestionName(event.type)) out.push('', `form ${name}: no longer on the page`)
|
|
79
|
+
if (answers.length) {
|
|
80
|
+
out.push('', answers.length > 1 ? 'answers:' : 'answer:')
|
|
81
|
+
for (const a of answers) out.push(` ${when(a)}${a.id === id ? ' (this one)' : ''}: ${said(a)}`)
|
|
82
|
+
}
|
|
83
|
+
// the chat, a correction in place of what it replaced
|
|
84
|
+
const messages = events.filter(e => e.type === 'chat:message' && e.data?.chat === chat)
|
|
85
|
+
const replaced = new Set(
|
|
86
|
+
messages
|
|
87
|
+
.map(m => m.data?.replaces)
|
|
88
|
+
.filter(Boolean)
|
|
89
|
+
.map(String),
|
|
90
|
+
)
|
|
91
|
+
const shown = messages.filter(m => !replaced.has(m.id))
|
|
92
|
+
if (shown.length) {
|
|
93
|
+
out.push('', `chat ${chat}:`)
|
|
94
|
+
for (const m of shown)
|
|
95
|
+
out.push(` ${who(m)} ${when(m)}${m.id === id ? ' (this one)' : ''}:`, indent(indent(String(m.data?.text ?? ''))))
|
|
96
|
+
}
|
|
97
|
+
return out.join('\n')
|
|
98
|
+
}
|
package/src/watchdog.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// What a running server says about its own health, in its log (standard error: server.log for a
|
|
2
|
+
// project's server, the login service's log for the shared one). A server that accepts connections
|
|
3
|
+
// but stops answering left no trace before: these lines are what to look for when it happens.
|
|
4
|
+
//
|
|
5
|
+
// - the event loop blocked: nothing is answered while it is, so a stall over a second is logged
|
|
6
|
+
// with how long it lasted;
|
|
7
|
+
// - a request that took long: its method and path, so what blocked can be found;
|
|
8
|
+
// - the listening socket's errors, such as running out of file descriptors (EMFILE), which stop it
|
|
9
|
+
// accepting without the process ending.
|
|
10
|
+
import type http from 'node:http'
|
|
11
|
+
import { performance } from 'node:perf_hooks'
|
|
12
|
+
|
|
13
|
+
/** How often the loop is checked, and from how late on a check counts as a stall. */
|
|
14
|
+
const TICK_MS = 500
|
|
15
|
+
const STALL_MS = 1000
|
|
16
|
+
/** A request answered slower than this is logged (streams, such as server-sent events, aren't). */
|
|
17
|
+
const SLOW_MS = 2000
|
|
18
|
+
|
|
19
|
+
export type Log = (message: string) => void
|
|
20
|
+
const stderr: Log = message => console.error(`${new Date().toISOString()} workpad: ${message}`)
|
|
21
|
+
|
|
22
|
+
/** Watch the event loop: a check that comes late by more than STALL_MS is logged. */
|
|
23
|
+
export function watchStalls(log: Log = stderr, { tick = TICK_MS, stall = STALL_MS } = {}): () => void {
|
|
24
|
+
let last = performance.now()
|
|
25
|
+
const timer = setInterval(() => {
|
|
26
|
+
const now = performance.now()
|
|
27
|
+
const late = now - last - tick
|
|
28
|
+
if (late > stall) log(`the event loop was blocked for ${Math.round(late)}ms: nothing was answered meanwhile`)
|
|
29
|
+
last = now
|
|
30
|
+
}, tick)
|
|
31
|
+
timer.unref()
|
|
32
|
+
return () => clearInterval(timer)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Log a server's slow requests and its socket's errors. */
|
|
36
|
+
export function watchServer(server: http.Server, log: Log = stderr, { slow = SLOW_MS } = {}) {
|
|
37
|
+
server.on('request', (req: http.IncomingMessage, res: http.ServerResponse) => {
|
|
38
|
+
const start = performance.now()
|
|
39
|
+
res.on('finish', () => {
|
|
40
|
+
const took = performance.now() - start
|
|
41
|
+
// headers given to writeHead aren't in getHeader: the head as sent says what it was
|
|
42
|
+
const head = String(res.getHeader('content-type') ?? '') + String((res as unknown as { _header?: string })._header ?? '')
|
|
43
|
+
if (took <= slow || head.includes('text/event-stream')) return
|
|
44
|
+
log(`slow request: ${req.method} ${req.url} took ${Math.round(took)}ms`)
|
|
45
|
+
})
|
|
46
|
+
})
|
|
47
|
+
server.on('error', err => log(`the server's socket: ${(err as Error).message}`))
|
|
48
|
+
server.on('clientError', (err: NodeJS.ErrnoException, socket) => {
|
|
49
|
+
// a client that went away is no news; anything else is
|
|
50
|
+
if (err.code !== 'ECONNRESET' && err.code !== 'EPIPE') log(`a client's request: ${err.message}`)
|
|
51
|
+
if (socket.writable) socket.end('HTTP/1.1 400 Bad Request\r\n\r\n')
|
|
52
|
+
})
|
|
53
|
+
}
|