@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
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
A chat between the reader and you: the reader types (Enter sends,
|
|
2
|
+
Shift+Enter starts a new line), you answer, the reader's messages on one side
|
|
3
|
+
and yours on the other. Every form has one under it, for questions about the
|
|
4
|
+
question; a `chat` block puts one anywhere, for talk that isn't about a form;
|
|
5
|
+
and every page ends with one, the chat `page`, for whatever is about none of
|
|
6
|
+
its questions. A page turns that one off with `plugins: { chat: false }` in
|
|
7
|
+
its frontmatter, a project with `chat({ page: false })` in its config, and a
|
|
8
|
+
page with a chat named `page` of its own keeps just that.
|
|
9
|
+
|
|
10
|
+
Your messages are small pages: write them as you write a page, with any
|
|
11
|
+
block (a form, a details, tabs, a diagram). The reader's are plain markdown:
|
|
12
|
+
what they type as `:::…` or `:button` shows as written.
|
|
13
|
+
|
|
14
|
+
A form in your message is a question like one on the page, named in its
|
|
15
|
+
chat: `:::form{name=scope}` in the chat `logger` is the form `logger/scope`,
|
|
16
|
+
its answers are events of that type, and a link to it is
|
|
17
|
+
`#form:logger/scope`. One without a name is numbered in the chat:
|
|
18
|
+
`logger/1`, `logger/2`. It has no chat of its own: the chat it is in is its
|
|
19
|
+
chat. A form on the page can't have a `/` in its name.
|
|
20
|
+
|
|
21
|
+
To rework a message (an option more, a typo), edit it in place:
|
|
22
|
+
`workpad edit <its id> --data '{"text":"…"}'`. It updates in the reader's
|
|
23
|
+
tab, marked new until they've seen it, and a form in it that keeps its name
|
|
24
|
+
keeps its answers.
|
|
25
|
+
|
|
26
|
+
```markdown
|
|
27
|
+
:::chat{name=notes}
|
|
28
|
+
Anything about the plan that isn't a decision: write it here.
|
|
29
|
+
:::
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
:::chat{name=notes}
|
|
33
|
+
Anything about the plan that isn't a decision: write it here.
|
|
34
|
+
:::
|
|
35
|
+
|
|
36
|
+
What the block says above the chat is ordinary markdown, and optional:
|
|
37
|
+
`::chat{name=page}` alone is a bare chat. Give each chat on a page its own
|
|
38
|
+
name, other than the page's forms' names: a form's chat has the form's name.
|
|
39
|
+
A link to a chat is `#chat:<name>`, to a form's chat `#chat:<form name>`, and to one
|
|
40
|
+
message `#message:<its event id>`; a folded form's chat opens for it.
|
|
41
|
+
|
|
42
|
+
A message is a `chat:message` event, `{ "chat": "<name>", "text": "…" }`,
|
|
43
|
+
never an answer: under a form it never makes a choice (only the reader can,
|
|
44
|
+
with a message of yours marked `answer`, below). Answer in the same chat:
|
|
45
|
+
|
|
46
|
+
```sh
|
|
47
|
+
workpad append --page <file> --type chat:message --data '{"chat":"<name>","text":"your answer"}'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Two more fields, both optional:
|
|
51
|
+
|
|
52
|
+
- `replaces`: the id of an earlier message of yours. Yours takes its place,
|
|
53
|
+
to correct it; the log itself only grows.
|
|
54
|
+
- `replyTo` (in a form's chat): the id of a submit of the form whose written
|
|
55
|
+
text you answer (a remark typed into one of its text fields). That text
|
|
56
|
+
shows as the reader's message, right before yours.
|
|
57
|
+
|
|
58
|
+
When you settle on an answer in a chat that is none of the form's options,
|
|
59
|
+
put it to the reader as a form in your message: one with a single
|
|
60
|
+
`:button[…]` is a choice they can make with one press, and it is a question
|
|
61
|
+
of the page like any other.
|
|
62
|
+
|
|
63
|
+
Your messages show as the agent's, and your own `listen` skips what you
|
|
64
|
+
append, while other agents listening on the page get it. A listen on the
|
|
65
|
+
whole page (no `--type`) hands out chat messages too; one with `--type
|
|
66
|
+
<form name>` does not. So don't add a free-text field
|
|
67
|
+
for questions or remarks to a decision form: its chat is for that, and a
|
|
68
|
+
submit stays a choice.
|
|
69
|
+
|
|
70
|
+
A chat is found by its name. Remove or rename a form or a chat block and its
|
|
71
|
+
chat moves to the end of the page, read-only, folded under "n chats no longer
|
|
72
|
+
on this page" (you can still answer there); bring it back under its name and
|
|
73
|
+
the chat is in its place again. So rework a question in its form rather than
|
|
74
|
+
replacing the form.
|
|
75
|
+
|
|
76
|
+
When you take a question or a chat off a page for good, remove its talk too,
|
|
77
|
+
so it doesn't stay behind in that list:
|
|
78
|
+
|
|
79
|
+
```sh
|
|
80
|
+
workpad chat prune <file> <name>
|
|
81
|
+
```
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
// A textarea that grows with its text, between a minimum and a maximum number of rows. Inlined
|
|
2
|
+
// from solid-textarea-autosize (MIT, © 2022 bigmistqke), itself a port of react-textarea-autosize,
|
|
3
|
+
// and updated for Solid 2: a ref directive, set up in `onSettled` with its cleanup returned.
|
|
4
|
+
//
|
|
5
|
+
// <textarea ref={autosize({ maxRows: 8 })} />
|
|
6
|
+
//
|
|
7
|
+
// The height is measured on a hidden textarea with the same sizing styles. It is measured again on
|
|
8
|
+
// every input, and whenever the textarea's own box changes (a narrower pane wraps more lines).
|
|
9
|
+
// After changing the value from code, dispatch an `input` event to measure again.
|
|
10
|
+
import { onSettled } from 'solid-js'
|
|
11
|
+
|
|
12
|
+
export interface AutosizeOptions {
|
|
13
|
+
/** rows shown at the least (default 1) */
|
|
14
|
+
minRows?: number
|
|
15
|
+
/** rows it grows to at the most, then it scrolls (default: no limit) */
|
|
16
|
+
maxRows?: number
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const SIZING_STYLE = [
|
|
20
|
+
'border-bottom-width',
|
|
21
|
+
'border-left-width',
|
|
22
|
+
'border-right-width',
|
|
23
|
+
'border-top-width',
|
|
24
|
+
'box-sizing',
|
|
25
|
+
'font-family',
|
|
26
|
+
'font-size',
|
|
27
|
+
'font-style',
|
|
28
|
+
'font-weight',
|
|
29
|
+
'letter-spacing',
|
|
30
|
+
'line-height',
|
|
31
|
+
'padding-bottom',
|
|
32
|
+
'padding-left',
|
|
33
|
+
'padding-right',
|
|
34
|
+
'padding-top',
|
|
35
|
+
'tab-size',
|
|
36
|
+
'text-indent',
|
|
37
|
+
'text-rendering',
|
|
38
|
+
'text-transform',
|
|
39
|
+
'width',
|
|
40
|
+
'word-break',
|
|
41
|
+
] as const
|
|
42
|
+
|
|
43
|
+
// forced on the measuring textarea, over anything a page's stylesheet gives textareas (a page's
|
|
44
|
+
// `max-width: 100%` resolves against the body here, and would squeeze it to nothing)
|
|
45
|
+
const HIDDEN_STYLE: Record<string, string> = {
|
|
46
|
+
'min-height': '0',
|
|
47
|
+
'max-height': 'none',
|
|
48
|
+
'min-width': '0',
|
|
49
|
+
'max-width': 'none',
|
|
50
|
+
height: '0',
|
|
51
|
+
visibility: 'hidden',
|
|
52
|
+
overflow: 'hidden',
|
|
53
|
+
position: 'absolute',
|
|
54
|
+
'z-index': '-1000',
|
|
55
|
+
top: '0',
|
|
56
|
+
right: '0',
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
interface Sizing {
|
|
60
|
+
style: Record<string, string>
|
|
61
|
+
padding: number
|
|
62
|
+
border: number
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function sizingOf(node: HTMLElement): Sizing | null {
|
|
66
|
+
const computed = getComputedStyle(node)
|
|
67
|
+
const style: Record<string, string> = {}
|
|
68
|
+
for (const key of SIZING_STYLE) style[key] = computed.getPropertyValue(key)
|
|
69
|
+
// a node that is not in the document has no computed box
|
|
70
|
+
if (!style['box-sizing']) return null
|
|
71
|
+
return {
|
|
72
|
+
style,
|
|
73
|
+
padding: parseFloat(style['padding-bottom']) + parseFloat(style['padding-top']),
|
|
74
|
+
border: parseFloat(style['border-bottom-width']) + parseFloat(style['border-top-width']),
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let hidden: HTMLTextAreaElement | null = null
|
|
79
|
+
|
|
80
|
+
/** The height the textarea needs for `value`, within the rows allowed, and whether that is the most it may have. */
|
|
81
|
+
function heightFor(sizing: Sizing, value: string, minRows: number, maxRows: number) {
|
|
82
|
+
if (!hidden) {
|
|
83
|
+
hidden = document.createElement('textarea')
|
|
84
|
+
hidden.setAttribute('tabindex', '-1')
|
|
85
|
+
hidden.setAttribute('aria-hidden', 'true')
|
|
86
|
+
}
|
|
87
|
+
if (!hidden.isConnected) document.body.append(hidden)
|
|
88
|
+
for (const [key, value] of Object.entries(sizing.style)) hidden.style.setProperty(key, value)
|
|
89
|
+
for (const [key, value] of Object.entries(HIDDEN_STYLE)) hidden.style.setProperty(key, value, 'important')
|
|
90
|
+
|
|
91
|
+
const borderBox = sizing.style['box-sizing'] === 'border-box'
|
|
92
|
+
// border-box: the height includes padding and border; content-box: only the content
|
|
93
|
+
const outer = (content: number) => (borderBox ? content + sizing.padding + sizing.border : content)
|
|
94
|
+
hidden.value = value
|
|
95
|
+
const height = borderBox ? hidden.scrollHeight + sizing.border : hidden.scrollHeight - sizing.padding
|
|
96
|
+
hidden.value = 'x'
|
|
97
|
+
const row = hidden.scrollHeight - sizing.padding
|
|
98
|
+
const most = outer(row * maxRows)
|
|
99
|
+
return { height: Math.min(most, Math.max(outer(row * minRows), height)), full: height >= most }
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A ref directive that keeps a textarea as tall as its text. */
|
|
103
|
+
export function autosize(options: AutosizeOptions = {}) {
|
|
104
|
+
let node: HTMLTextAreaElement | undefined
|
|
105
|
+
let last = 0
|
|
106
|
+
const resize = () => {
|
|
107
|
+
if (!node) return
|
|
108
|
+
const sizing = sizingOf(node)
|
|
109
|
+
if (!sizing) return
|
|
110
|
+
const { height, full } = heightFor(
|
|
111
|
+
sizing,
|
|
112
|
+
node.value || node.placeholder || 'x',
|
|
113
|
+
options.minRows ?? 1,
|
|
114
|
+
options.maxRows ?? Infinity,
|
|
115
|
+
)
|
|
116
|
+
// a scrollbar only once it is as tall as it may be: below that its text fits, and a height a
|
|
117
|
+
// fraction short of the text (rounding) would show one for nothing
|
|
118
|
+
node.style.setProperty('overflow-y', full ? 'auto' : 'hidden', 'important')
|
|
119
|
+
if (height === last) return
|
|
120
|
+
last = height
|
|
121
|
+
node.style.setProperty('height', `${height}px`, 'important')
|
|
122
|
+
}
|
|
123
|
+
// the ref runs before the element is in the document: measure once it is
|
|
124
|
+
onSettled(() => {
|
|
125
|
+
const el = node
|
|
126
|
+
if (!el) return
|
|
127
|
+
resize()
|
|
128
|
+
el.addEventListener('input', resize)
|
|
129
|
+
const observer = new ResizeObserver(() => resize())
|
|
130
|
+
observer.observe(el)
|
|
131
|
+
return () => {
|
|
132
|
+
el.removeEventListener('input', resize)
|
|
133
|
+
observer.disconnect()
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
return (el: HTMLTextAreaElement) => {
|
|
137
|
+
node = el
|
|
138
|
+
}
|
|
139
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// A chat (rendered by the chat block, on its own or under a form): the reader writes, the agent
|
|
2
|
+
// answers (`workpad append --type chat:message --data '{"chat":"<name>","text":"…"}'`). A message
|
|
3
|
+
// is a `chat:message` event, never an answer, so talking about a form's choice never makes it. But
|
|
4
|
+
// under a form, an agent's message marked `answer` has a button that answers the form with its
|
|
5
|
+
// text, for an answer the two settled on in the chat that is none of the form's options.
|
|
6
|
+
import { createEffect, createMemo, For, Loading } from 'solid-js'
|
|
7
|
+
import { solid, useWorkpad } from '@bigmistqke/workpad/solid'
|
|
8
|
+
import type { ChatProps } from './index'
|
|
9
|
+
import type { WpEvent } from '@bigmistqke/workpad'
|
|
10
|
+
import { autosize } from './autosize'
|
|
11
|
+
|
|
12
|
+
/** A message in a chat: the chat block's own event type. */
|
|
13
|
+
const MESSAGE = 'chat:message'
|
|
14
|
+
|
|
15
|
+
interface Message {
|
|
16
|
+
id: string
|
|
17
|
+
from: 'you' | 'agent'
|
|
18
|
+
text: string
|
|
19
|
+
/** shown at once, but the server hasn't confirmed it yet */
|
|
20
|
+
sending?: boolean
|
|
21
|
+
/** the agent's message as it is in the log: it shows as a small page (Api.message) */
|
|
22
|
+
event?: WpEvent
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* What the reader wrote in a submit of a form: every text in it that is not one of the form's
|
|
27
|
+
* choices (also from a field the form no longer has).
|
|
28
|
+
*/
|
|
29
|
+
function written(submit: WpEvent, choiceNames: string[]) {
|
|
30
|
+
const choices = new Set(choiceNames)
|
|
31
|
+
return Object.entries(submit.data)
|
|
32
|
+
.filter(([k, v]) => !choices.has(k) && typeof v === 'string' && v.trim())
|
|
33
|
+
.map(([, v]) => String(v).trim())
|
|
34
|
+
.join('\n\n')
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function Chat(props: ChatProps) {
|
|
38
|
+
const wp = useWorkpad()
|
|
39
|
+
// The chat's messages in order. A message can take the place of an earlier one (`replaces` its
|
|
40
|
+
// id), to correct it. Under a form, a message can answer something the reader wrote in a submit
|
|
41
|
+
// of the form (`replyTo` its id): that text shows as theirs, right before the answer.
|
|
42
|
+
const messages = createMemo((): Message[] => {
|
|
43
|
+
const events = wp.events()
|
|
44
|
+
const own = events.filter(e => e.type === MESSAGE && e.data.chat === props.chat)
|
|
45
|
+
const replaced = new Set(
|
|
46
|
+
own
|
|
47
|
+
.map(e => e.data.replaces)
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.map(String),
|
|
50
|
+
)
|
|
51
|
+
const out: Message[] = []
|
|
52
|
+
for (const e of own) {
|
|
53
|
+
if (replaced.has(e.id)) continue
|
|
54
|
+
const to =
|
|
55
|
+
props.form && e.data.replyTo ? events.find(x => x.id === e.data.replyTo && x.type === props.form) : undefined
|
|
56
|
+
const asked = to ? written(to, props.choices) : ''
|
|
57
|
+
if (to && asked && !out.some(m => m.id === to.id)) out.push({ id: to.id, from: 'you', text: asked })
|
|
58
|
+
out.push({
|
|
59
|
+
id: e.id,
|
|
60
|
+
from: e.from === 'cli' ? 'agent' : 'you',
|
|
61
|
+
text: String(e.data.text ?? ''),
|
|
62
|
+
sending: !!e.sending,
|
|
63
|
+
event: e.from === 'cli' ? e : undefined,
|
|
64
|
+
})
|
|
65
|
+
}
|
|
66
|
+
return out
|
|
67
|
+
})
|
|
68
|
+
let box: HTMLTextAreaElement | undefined
|
|
69
|
+
// what the reader is typing, kept until it is sent: a reload, the page's tab sent elsewhere or the
|
|
70
|
+
// chat drawn again loses nothing; per page and chat, as forms keep theirs (the path tells pages,
|
|
71
|
+
// and on a shared server projects, apart)
|
|
72
|
+
const draftKey = `wp-chat-draft:${location.pathname}|${props.chat}`
|
|
73
|
+
const keep = (text: string) => {
|
|
74
|
+
try {
|
|
75
|
+
if (text.trim()) localStorage.setItem(draftKey, text)
|
|
76
|
+
else localStorage.removeItem(draftKey)
|
|
77
|
+
} catch {
|
|
78
|
+
/* storage unavailable */
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
// on the element itself: sending empties the box with an input event that doesn't bubble
|
|
82
|
+
const restore = (el: HTMLTextAreaElement) => {
|
|
83
|
+
el.addEventListener('input', () => keep(el.value))
|
|
84
|
+
try {
|
|
85
|
+
const text = localStorage.getItem(draftKey)
|
|
86
|
+
if (text && !el.value) {
|
|
87
|
+
el.value = text
|
|
88
|
+
queueMicrotask(() => el.dispatchEvent(new Event('input')))
|
|
89
|
+
}
|
|
90
|
+
} catch {
|
|
91
|
+
/* storage unavailable */
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const post = async () => {
|
|
95
|
+
const text = box?.value.trim()
|
|
96
|
+
if (!text) return
|
|
97
|
+
// optimistic: the message is in the chat at once, grey until the server has it, and the box is
|
|
98
|
+
// empty, so a second Send has nothing to send again
|
|
99
|
+
const write = (value: string) => {
|
|
100
|
+
box!.value = value
|
|
101
|
+
// measure again: a value set from code fires no input event
|
|
102
|
+
box!.dispatchEvent(new Event('input'))
|
|
103
|
+
}
|
|
104
|
+
write('')
|
|
105
|
+
try {
|
|
106
|
+
await wp.append(MESSAGE, { chat: props.chat, text })
|
|
107
|
+
} catch (err) {
|
|
108
|
+
console.error(err)
|
|
109
|
+
// it never got there: back into the box, unless something else was written meanwhile
|
|
110
|
+
if (!box!.value.trim()) write(text)
|
|
111
|
+
wp.toast("Couldn't send the message", {
|
|
112
|
+
kind: 'alert',
|
|
113
|
+
name: 'chat-failed',
|
|
114
|
+
details: "It's back in the box: send it again.",
|
|
115
|
+
})
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return (
|
|
119
|
+
// a div, not a form: forms don't nest, and the box has no name, so it is never part of an answer
|
|
120
|
+
<div class="wp-chat" data-under-form={props.form ? '' : undefined}>
|
|
121
|
+
{/* keyed by id: a new message adds its own element and leaves the others be (the list is
|
|
122
|
+
built anew on every event, so by identity each message would be drawn again, and its
|
|
123
|
+
markdown with it, moving the page under the reader) */}
|
|
124
|
+
<For each={messages()} keyed={m => m.id}>
|
|
125
|
+
{m => (
|
|
126
|
+
// the reader's messages on the right, the agent's on the left
|
|
127
|
+
// the agent's: read once in view (the core marks it, see EventSpec.forReader)
|
|
128
|
+
// #message:<id> goes to it (src/anchors.ts); one still sending has no id in the log yet
|
|
129
|
+
<div
|
|
130
|
+
id={m().sending ? undefined : `message:${m().id}`}
|
|
131
|
+
class="wp-chat-message"
|
|
132
|
+
data-from={m().from}
|
|
133
|
+
data-sending={m().sending ? '' : undefined}
|
|
134
|
+
data-wp-message={m().from === 'agent' ? m().id : undefined}
|
|
135
|
+
>
|
|
136
|
+
<span class="wp-chat-who">{m().from}</span>
|
|
137
|
+
<Bubble text={m().text} event={m().event} />
|
|
138
|
+
</div>
|
|
139
|
+
)}
|
|
140
|
+
</For>
|
|
141
|
+
<div class="wp-chat-new">
|
|
142
|
+
<textarea
|
|
143
|
+
ref={[el => (box = el), autosize({ maxRows: 10 }), restore]}
|
|
144
|
+
rows={1}
|
|
145
|
+
placeholder={props.form ? "Ask about this question (doesn't choose anything)" : 'Write a message'}
|
|
146
|
+
// Enter sends, Shift+Enter starts a new line; not while an input method is composing
|
|
147
|
+
onKeyDown={e => {
|
|
148
|
+
if (e.key === 'Enter' && !e.shiftKey && !e.isComposing) {
|
|
149
|
+
e.preventDefault()
|
|
150
|
+
post()
|
|
151
|
+
}
|
|
152
|
+
}}
|
|
153
|
+
/>
|
|
154
|
+
<button type="button" onClick={post}>
|
|
155
|
+
{props.form ? 'Ask' : 'Send'}
|
|
156
|
+
</button>
|
|
157
|
+
</div>
|
|
158
|
+
</div>
|
|
159
|
+
)
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** A message as markdown, rendered by the server; its plain text until then. */
|
|
163
|
+
/** A message: the reader's as markdown; the agent's as a small page, every block and all (Api.message). */
|
|
164
|
+
function Bubble(props: { text: string; event?: WpEvent }) {
|
|
165
|
+
const wp = useWorkpad()
|
|
166
|
+
const md = createMemo(() => (props.event ? wp.message(props.event) : wp.markdown(props.text)))
|
|
167
|
+
// the agent edited it (`workpad edit`: the same message, new text): the page marks it new until
|
|
168
|
+
// the reader has seen it, as a changed block on the page. Compared with the text it showed, not
|
|
169
|
+
// `defer`: the effect runs again as what it reads settles after mount, with the same text, and
|
|
170
|
+
// that is no edit (it marked every message new, the reader's own too)
|
|
171
|
+
let el: HTMLElement | undefined
|
|
172
|
+
let shown: string | undefined
|
|
173
|
+
createEffect(
|
|
174
|
+
() => props.text,
|
|
175
|
+
text => {
|
|
176
|
+
if (shown !== undefined && text !== shown) el?.dispatchEvent(new CustomEvent('wp-changed', { bubbles: true }))
|
|
177
|
+
shown = text
|
|
178
|
+
},
|
|
179
|
+
)
|
|
180
|
+
return (
|
|
181
|
+
<Loading fallback={<div class="wp-chat-bubble">{props.text}</div>}>
|
|
182
|
+
<div ref={e => (el = e)} class="wp-chat-bubble" data-md="" innerHTML={md()} />
|
|
183
|
+
</Loading>
|
|
184
|
+
)
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export default solid(Chat)
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// :::chat{name=…} — a chat between the reader and the agent, anywhere on a page; and the chat under
|
|
2
|
+
// every form, which the form plugin places with the form's name. See README.md.
|
|
3
|
+
//
|
|
4
|
+
// A message is a `chat:message` event, `{ chat, text }`, from the reader (the page) or the agent
|
|
5
|
+
// (`workpad append`); the chat's name says which chat it belongs to. Messages are never answers:
|
|
6
|
+
// a form's chat is talk about the question, never a choice.
|
|
7
|
+
import fs from 'node:fs'
|
|
8
|
+
import { prune } from '@bigmistqke/workpad/log'
|
|
9
|
+
import { definePlugin, type Plugin, type WpEvent } from '@bigmistqke/workpad'
|
|
10
|
+
import { anchorTaken, html, raw, type Block, type DirectiveContext } from '@bigmistqke/workpad'
|
|
11
|
+
import { builtinPublic } from '../../src/package.ts'
|
|
12
|
+
import { isQuestionName } from '../../src/status.ts'
|
|
13
|
+
import { chatNames } from '../../src/pages/markdown.ts'
|
|
14
|
+
import { item, when } from '../../src/archive.ts'
|
|
15
|
+
|
|
16
|
+
const style = 'style.css'
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* What the chat needs to know. `form`: the form it sits under, if any, with the names of its choices,
|
|
20
|
+
* whose values are never a remark (a message can answer text the reader typed into a submit).
|
|
21
|
+
*/
|
|
22
|
+
export interface ChatProps {
|
|
23
|
+
chat: string
|
|
24
|
+
form: string | null
|
|
25
|
+
choices: string[]
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** The chat itself: a client island, so the server renders nothing for it. */
|
|
29
|
+
const thread: Block<ChatProps> = {
|
|
30
|
+
style,
|
|
31
|
+
props: ctx => ({
|
|
32
|
+
chat: (ctx as DirectiveContext).attrs.chat ?? '',
|
|
33
|
+
form: (ctx as DirectiveContext).attrs.form || null,
|
|
34
|
+
choices: ((ctx as DirectiveContext).attrs.choices ?? '').split(' ').filter(Boolean),
|
|
35
|
+
}),
|
|
36
|
+
html: () => '',
|
|
37
|
+
client: 'client.js',
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const quote = (s: string) => s.replace(/"/g, '"')
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* The chat thread for `chat`, as markup: for the chat block, and for the form plugin under a form,
|
|
44
|
+
* which gives it its id (`anchor`: `chat:<name>`, so `#chat:<name>` reaches a form's chat too).
|
|
45
|
+
*/
|
|
46
|
+
export const chatThread = (c: DirectiveContext, chat: string, form?: { choices: string[]; anchor?: string | null }) =>
|
|
47
|
+
c.markdown(
|
|
48
|
+
`::chat-thread{chat="${quote(chat)}"${form ? ` form="${quote(chat)}" choices="${form.choices.join(' ')}"` : ''}${form?.anchor ? ` anchor="${quote(form.anchor)}"` : ''}}`,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
/** `:::chat{name=…}`: a chat on its own, after whatever the block says above it. */
|
|
52
|
+
const chat: Block = {
|
|
53
|
+
style,
|
|
54
|
+
async html(ctx) {
|
|
55
|
+
const c = ctx as DirectiveContext
|
|
56
|
+
const name = c.attrs.name || c.attrs.id || 'chat'
|
|
57
|
+
// a chat's name shares the forms' names: a colon marks events, never a chat
|
|
58
|
+
if (!isQuestionName(name))
|
|
59
|
+
throw new Error(`chat name "${name}" has a colon: those names are reserved for events of workpad and plugins`)
|
|
60
|
+
const body = c.children.length ? await c.toHtml(c.children) : ''
|
|
61
|
+
// #chat:<name> goes to it (src/anchors.ts)
|
|
62
|
+
const id = c.anchor('chat', name)
|
|
63
|
+
return html`<section class="wp-chat-block" data-chat="${name}" ${id ? html` id="${id}"` : ''}>
|
|
64
|
+
${!id ? anchorTaken('chat', name) : ''}${raw(body)}${raw(await chatThread(c, name))}
|
|
65
|
+
</section>`
|
|
66
|
+
},
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** The chat at the bottom of every page (see `end`): for what is about none of its questions. */
|
|
70
|
+
export const PAGE_CHAT = 'page'
|
|
71
|
+
|
|
72
|
+
/** `page: false`: no chat at the bottom of every page. */
|
|
73
|
+
export interface ChatOptions {
|
|
74
|
+
page?: boolean
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export default function chatPlugin({ page: pageChat = true }: ChatOptions = {}): Plugin {
|
|
78
|
+
return definePlugin({
|
|
79
|
+
name: 'chat',
|
|
80
|
+
// a chat at the bottom of every page, for questions that are about none of its forms: not on a
|
|
81
|
+
// repository's own files, nor on a page that turns it off (`plugins: { chat: false }` in its
|
|
82
|
+
// frontmatter) or has a chat of that name already
|
|
83
|
+
end(page) {
|
|
84
|
+
if (!pageChat || page.id.startsWith('-/')) return null
|
|
85
|
+
const own = (page.frontmatter.plugins as Record<string, unknown> | undefined)?.chat
|
|
86
|
+
if (own === false || chatNames(page.body).includes(PAGE_CHAT)) return null
|
|
87
|
+
return `:::chat{name=${PAGE_CHAT}}\nQuestions or remarks about this page:\n:::`
|
|
88
|
+
},
|
|
89
|
+
public: builtinPublic('chat'),
|
|
90
|
+
blocks: { chat: { ...chat, menu: { group: 'ask', summary: 'a conversation, on the page or under a form' } }, 'chat-thread': thread },
|
|
91
|
+
events: {
|
|
92
|
+
'chat:message': {
|
|
93
|
+
handOut: true,
|
|
94
|
+
forReader: true,
|
|
95
|
+
description: 'a message in a chat: `{ chat, text }`, and `replaces` or `replyTo` (see below)',
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
commands: {
|
|
99
|
+
prune: {
|
|
100
|
+
description:
|
|
101
|
+
"remove one chat's messages from the log, and the answers to the forms in them, the page kept: for a question taken off a page, so its talk doesn't stay behind",
|
|
102
|
+
args: '<page|file> <chat>',
|
|
103
|
+
run(ctx) {
|
|
104
|
+
const [target, chat] = ctx.positionals
|
|
105
|
+
if (!target || !chat) throw new Error('usage: workpad chat prune <page|file> <chat>')
|
|
106
|
+
const page = ctx.page(target)
|
|
107
|
+
const removed = prune(ctx.eventsFile, page, e => e.type === 'chat:message' && e.data?.chat === chat)
|
|
108
|
+
// the forms in the agent's messages there go with them: their answers (`<chat>/<name>`)
|
|
109
|
+
const answers = prune(ctx.eventsFile, page, e => e.type.startsWith(`${chat}/`))
|
|
110
|
+
console.log(
|
|
111
|
+
`pruned ${removed} message${removed === 1 ? '' : 's'} of chat ${chat} on ${page}${answers ? `, and ${answers} answer${answers === 1 ? '' : 's'} to the forms in them` : ''}`,
|
|
112
|
+
)
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
docs: () => fs.readFileSync(new URL('./README.md', import.meta.url), 'utf8'),
|
|
117
|
+
archive: archiveChats,
|
|
118
|
+
})
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** An archived page's chats: each one's messages as the page showed them, a correction in its place. */
|
|
122
|
+
function archiveChats(events: WpEvent[]) {
|
|
123
|
+
const messages = events.filter(e => e.type === 'chat:message')
|
|
124
|
+
const replaced = new Set(
|
|
125
|
+
messages
|
|
126
|
+
.map(e => e.data.replaces)
|
|
127
|
+
.filter(Boolean)
|
|
128
|
+
.map(String),
|
|
129
|
+
)
|
|
130
|
+
const byChat = new Map<string, WpEvent[]>()
|
|
131
|
+
for (const e of messages) {
|
|
132
|
+
if (replaced.has(e.id)) continue
|
|
133
|
+
const chat = String(e.data.chat ?? '')
|
|
134
|
+
byChat.set(chat, [...(byChat.get(chat) ?? []), e])
|
|
135
|
+
}
|
|
136
|
+
if (!byChat.size) return
|
|
137
|
+
return [
|
|
138
|
+
'## Chats',
|
|
139
|
+
...[...byChat].map(
|
|
140
|
+
([chat, list]) =>
|
|
141
|
+
`### ${chat}\n\n${list
|
|
142
|
+
.map(e => item(`${e.from === 'cli' ? 'agent' : 'you'}, ${when(e)}`, String(e.data.text ?? '')))
|
|
143
|
+
.join('\n')}`,
|
|
144
|
+
),
|
|
145
|
+
].join('\n\n')
|
|
146
|
+
}
|