@g9tm/aiassistant-lynx 1.0.0 → 1.2.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/dist/parts.jsx CHANGED
@@ -4,28 +4,19 @@
4
4
  * every interactive element has an accessibility label; the growing message is drawn below the recycled list.
5
5
  */
6
6
  import { useEffect, useState } from "@lynx-js/react";
7
- import { blockView, formatNumber, needsCode, parseText, shares, visibleText, REVEAL_MS, windowView } from "@g9tm/aiassistant";
8
- import { blockOverride, cls, fill, override, rowOf, tok, useUi } from "./context.jsx";
7
+ import { arr, blockView, can, finished, needsCode, obj, parseText, str, visibleText, LONG_RUNNING_MS, REVEAL_MS, windowView } from "@g9tm/aiassistant";
8
+ import { saveCsv } from "@g9tm/aiassistant/pages";
9
+ import { Chart } from "./chart.jsx";
10
+ import { blockOverride, cls, fill, override, rowOf, swappable, tok, useUi } from "./context.jsx";
11
+ import { Button, field, PinPad, SecretField } from "./slots.jsx";
9
12
  const grow = { ...fill, minWidth: 0 };
10
- /** A Lynx input has no size of its own (on a device it is drawn zero high): every input of the renderer gets one. */
11
- function field(ui) {
12
- return { height: "40px", fontSize: "15px", paddingLeft: "10px", paddingRight: "10px", borderWidth: "1px", borderStyle: "solid", borderRadius: "8px",
13
- borderColor: tok(ui, "border", "#d0d0d0"), color: tok(ui, "ink", "#111"), backgroundColor: tok(ui, "field", "#fff") };
14
- }
15
- export function Button(props) {
16
- const ui = useUi();
17
- return (<view className={props.part ? cls(ui, props.part) : "g9a-button"} accessibility-element={true} accessibility-label={props.label} bindtap={props.onTap} style={{ minHeight: "38px", paddingLeft: "12px", paddingRight: "12px", borderRadius: tok(ui, "radius-s", "6px"), justifyContent: "center",
18
- backgroundColor: props.primary ? tok(ui, "accent", "#8a4b2a") : tok(ui, "surface", "#fff"), borderWidth: "1px", borderColor: tok(ui, "line", "#e4ded6") }}>
19
- <text style={{ color: props.primary ? tok(ui, "accent-ink", "#fff") : tok(ui, "ink", "#22201d") }} text-maxline="1">{props.label}</text>
20
- </view>);
21
- }
22
13
  export function TextView(props) {
23
14
  const ui = useUi();
24
15
  const line = (xs) => xs.map((x, i) => (<text key={i} style={x.kind === "bold" ? { fontWeight: "bold" } : x.kind === "code" ? { fontFamily: "monospace" } : x.kind === "link" && x.href ? { color: tok(ui, "accent", "#8a4b2a") } : {}} {...(x.kind === "link" && x.href ? { bindtap: () => ui.assistant.host.openUrl?.(x.href ?? "") } : {})}>{x.text}</text>));
25
- return (<view>
16
+ return (<view>
26
17
  {parseText(props.text, ui.assistant.host.linkHosts ?? []).map((b, i) => b.kind === "paragraph"
27
18
  ? <text key={i} style={{ color: tok(ui, "ink", "#22201d"), marginBottom: "6px" }}>{line(b.inlines)}</text>
28
- : <view key={i}>{b.items.map((it, n) => <text key={n}>{b.ordered ? String(n + 1) + ". " : "• "}{line(it)}</text>)}</view>)}
19
+ : <view key={i}>{b.items.map((it, n) => <text key={n}>{b.ordered ? String(n + 1) + ". " : "• "}{line(it)}</text>)}</view>)}
29
20
  </view>);
30
21
  }
31
22
  export function RevealText(props) {
@@ -52,13 +43,29 @@ export function Small(props) {
52
43
  return <view className={cls(ui, "mode-banner")} style={{ padding: "6px 12px", backgroundColor: tok(ui, "surface-2", "#f6f4f1") }}><text style={{ color: tok(ui, "warn", "#a35a00") }}>{s.mode === "off" ? t("mode.off") : t("mode.offline")}</text></view>;
53
44
  const w = s?.usage[0] ? windowView(s.usage[0], Date.now()) : null;
54
45
  if (props.kind === "usage" && w) {
55
- return (<view className={cls(ui, "usage-chip")} style={{ ...rowOf(ui), height: "22px", borderRadius: "11px", overflow: "hidden", backgroundColor: tok(ui, "surface-2", "#f6f4f1") }}>
56
- <view style={{ position: "absolute", left: 0, top: 0, bottom: 0, width: String(Math.round(w.left * 100)) + "%", backgroundColor: tok(ui, "accent", "#8a4b2a"), opacity: 0.25 }}/>
57
- <text style={{ fontSize: "11px", paddingLeft: "8px", paddingRight: "8px" }}>{t("usage.left", { percent: Math.round(w.left * 100) + "%" })}</text>
46
+ return (<view className={cls(ui, "usage-chip")} style={{ ...rowOf(ui), height: "22px", borderRadius: "11px", overflow: "hidden", backgroundColor: tok(ui, "surface-2", "#f6f4f1") }}>
47
+ <view style={{ position: "absolute", left: 0, top: 0, bottom: 0, width: String(Math.round(w.left * 100)) + "%", backgroundColor: tok(ui, "accent", "#8a4b2a"), opacity: 0.25 }}/>
48
+ <text style={{ fontSize: "11px", paddingLeft: "8px", paddingRight: "8px" }}>{t("usage.left", { percent: Math.round(w.left * 100) + "%" })}</text>
58
49
  </view>);
59
50
  }
60
51
  return null;
61
52
  }
53
+ /**
54
+ * "Save as CSV" for a table or an export block, through the host's saveFile; a small notice says saved or not saved.
55
+ * Hidden when the host cannot save files. Replaceable as `CsvButton`.
56
+ */
57
+ export const CsvButton = swappable("CsvButton", (props) => {
58
+ const ui = useUi();
59
+ const t = ui.texts.t;
60
+ const [notice, setNotice] = useState(null);
61
+ if (!can(ui.assistant.host, "save"))
62
+ return null;
63
+ const save = () => { void saveCsv(ui.assistant.host, props.title, props.labels, props.rows).then((ok) => setNotice(ok ? "csv.saved" : "csv.not_saved")); };
64
+ return (<view className="g9a-csv" style={{ ...rowOf(ui, "8px"), marginTop: "6px" }}>
65
+ <Button label={t("csv.save")} onTap={save}/>
66
+ {notice ? <view style={grow}><text className="g9a-csv-notice" style={{ fontSize: "12px", color: notice === "csv.saved" ? tok(ui, "muted", "#6b6660") : tok(ui, "danger", "#b42318") }} text-maxline="1">{t(notice)}</text></view> : null}
67
+ </view>);
68
+ });
62
69
  export function BlockView(props) {
63
70
  const ui = useUi();
64
71
  const t = ui.texts.t;
@@ -71,31 +78,29 @@ export function BlockView(props) {
71
78
  case "text":
72
79
  case "callout": return <view className={cls(ui, v.type === "text" ? "block.text" : "block.callout")}><TextView text={v.text}/></view>;
73
80
  case "table":
74
- return (<view className={cls(ui, "block.table")}>
75
- {v.title ? <text style={{ fontWeight: "bold" }}>{v.title}</text> : null}
76
- {v.asCards || props.compact ? null : <view style={rowOf(ui)}>{v.columns.map((c) => <text key={c.name} style={{ ...grow, fontWeight: "bold" }} text-maxline="1">{c.label}</text>)}</view>}
81
+ return (<view className={cls(ui, "block.table")}>
82
+ {v.title ? <text style={{ fontWeight: "bold" }}>{v.title}</text> : null}
83
+ {v.asCards || props.compact ? null : <view style={rowOf(ui)}>{v.columns.map((c) => <text key={c.name} style={{ ...grow, fontWeight: "bold" }} text-maxline="1">{c.label}</text>)}</view>}
77
84
  {v.rows.slice(0, open ? v.rows.length : v.shown).map((r, i) => v.asCards || props.compact
78
- ? <view key={i} className={cls(ui, "block.table.card")} style={{ padding: "6px 8px", marginBottom: "6px", borderRadius: "6px", backgroundColor: tok(ui, "surface", "#fff") }}>
79
- {v.columns.map((c, j) => <view key={c.name} style={rowOf(ui)}><text style={{ ...grow, color: tok(ui, "muted", "#6b6660") }} text-maxline="1">{c.label}</text><text text-maxline="2">{r.at(j)}</text></view>)}
85
+ ? <view key={i} className={cls(ui, "block.table.card")} style={{ padding: "6px 8px", marginBottom: "6px", borderRadius: "6px", backgroundColor: tok(ui, "surface", "#fff") }}>
86
+ {v.columns.map((c, j) => <view key={c.name} style={rowOf(ui)}><text style={{ ...grow, color: tok(ui, "muted", "#6b6660") }} text-maxline="1">{c.label}</text><text text-maxline="2">{r.at(j)}</text></view>)}
80
87
  </view>
81
- : <view key={i} style={rowOf(ui)}>{r.map((c, j) => <text key={j} style={grow} text-maxline="1">{c}</text>)}</view>)}
82
- {v.more > 0 && !open ? <Button label={t("block.more_rows", { count: v.more })} onTap={() => setOpen(true)}/> : null}
88
+ : <view key={i} style={rowOf(ui)}>{r.map((c, j) => <text key={j} style={grow} text-maxline="1">{c}</text>)}</view>)}
89
+ {v.more > 0 && !open ? <Button label={t("block.more_rows", { count: v.more })} onTap={() => setOpen(true)}/> : null}
90
+ <CsvButton title={v.title} labels={v.columns.map((c) => c.label)} rows={v.rows}/>
83
91
  </view>);
84
- case "chart": {
85
- const first = v.series[0]?.values ?? [];
86
- const sh = shares(first);
87
- return (<view className={cls(ui, "block.chart")} accessibility-label={v.fallbackText}>
88
- {v.title ? <text style={{ fontWeight: "bold" }}>{v.title}</text> : null}
89
- {v.labels.map((l, i) => (<view key={i} className={cls(ui, "block.chart.bar")} style={{ marginBottom: "6px" }}>
90
- <text text-maxline="1">{l}</text>
91
- <view style={{ height: "10px", borderRadius: "5px", backgroundColor: tok(ui, "surface", "#fff") }}>
92
- <view style={{ height: "10px", borderRadius: "5px", width: String(Math.round((sh.at(i) ?? 0) * 100)) + "%", backgroundColor: tok(ui, "chart-bar", "#8a4b2a") }}/>
93
- </view>
94
- <text style={{ fontSize: "12px" }}>{formatNumber(first.at(i) ?? 0, ui.texts.locale)}</text>
95
- </view>))}
96
- {v.fewData ? <text style={{ fontSize: "12px" }}>{t("block.few_data")}</text> : null}
92
+ case "export":
93
+ return (<view className="g9a-block-export">
94
+ {v.title ? <text style={{ fontWeight: "bold" }} text-maxline="2">{v.title}</text> : null}
95
+ <text style={{ fontSize: "12px", color: tok(ui, "muted", "#6b6660") }} text-maxline="3">{v.fallbackText}</text>
96
+ <CsvButton title={v.title} labels={v.columns.map((c) => c.label)} rows={v.rows}/>
97
+ </view>);
98
+ case "chart":
99
+ return (<view className={cls(ui, "block.chart")} accessibility-label={v.fallbackText}>
100
+ {v.title ? <text style={{ fontWeight: "bold" }}>{v.title}</text> : null}
101
+ <Chart view={v}/>
102
+ {v.fewData ? <text style={{ fontSize: "12px" }}>{t("block.few_data")}</text> : null}
97
103
  </view>);
98
- }
99
104
  case "kpi": return <view className={cls(ui, "block.kpi")} style={{ ...rowOf(ui), flexWrap: "wrap" }}>{v.items.map((k, i) => <view key={i} style={{ marginRight: "8px" }}><text>{k.label}</text><text style={{ fontWeight: "bold" }}>{k.value}</text></view>)}</view>;
100
105
  case "steps": return <view className={cls(ui, "block.steps")}>{v.items.map((s, i) => <text key={i}>{s.text}</text>)}</view>;
101
106
  case "actions": return <view className={cls(ui, "block.actions")} style={{ ...rowOf(ui), flexWrap: "wrap" }}>{v.items.map((a, i) => <Button key={i} part="block.action" label={a.label + (a.suggested ? " · " + t("block.suggested") : "")} onTap={() => void ui.assistant.send(a.send)}/>)}</view>;
@@ -110,24 +115,32 @@ export function ApprovalCard(props) {
110
115
  const t = ui.texts.t;
111
116
  const p = props.plan;
112
117
  const [code, setCode] = useState("");
118
+ const [epoch, setEpoch] = useState(0); // a new key clears the code field after a try
113
119
  // A fact without a text of its own shows its values (as in Svelte), never its key.
114
120
  const say = (key, args) => { const text = t(key, args); return text === key ? Object.values(args).join(" ") || key : text; };
115
- return (<view className={cls(ui, "card.approval")} style={{ padding: "12px", borderRadius: "10px", borderWidth: "1px", borderColor: tok(ui, "approval-card-border", "#8a4b2a") }}>
116
- <text style={{ fontWeight: "bold" }}>{p.readOnly ? t("card.readonly_routine") : p.title}</text>
117
- {p.warning ? <text style={{ color: tok(ui, "warn", "#a35a00") }}>{t("card.tainted")}</text> : null}
118
- {p.steps.map((s) => (<view key={s.index} className={cls(ui, "card.approval.step")} accessibility-element={true} accessibility-label={s.text} bindtap={() => ui.assistant.tick(p.id, s.index)}>
119
- <view style={rowOf(ui)}><text>{s.ticked ? "☑ " : "☐ "}</text><text style={grow}>{s.text}{s.undoable ? "" : " (" + t("card.irreversible") + ")"}</text></view>
120
- {s.facts.map((f, i) => <text key={i} className={cls(ui, "card.approval.fact")} style={{ fontSize: "12px" }}>{say(f.key, f.args)}</text>)}
121
- {s.diff.map((d, i) => <text key={"d" + String(i)} className={cls(ui, "card.approval.fact")} style={{ fontSize: "12px" }}>{say(d.name, d.args) + ": " + (d.before ?? "—") + " ← " + (d.after ?? "—")}</text>)}
122
- </view>))}
123
- {p.answered === null ? (<view>
124
- {needsCode(p.method) ? <input className={cls(ui, "card.approval.code")} style={{ ...field(ui), marginTop: "8px" }} type="password" accessibility-label={t("card.code")} placeholder={t("card.code")} bindinput={(e) => setCode(e.detail.value)}/> : null}
125
- {p.failCode === "confirm.waiting" ? <text>{t("card.waiting")}</text> : p.failCode ? <text>{p.failCode}{p.triesLeft !== null ? " · " + t("card.tries_left", { count: p.triesLeft }) : ""}</text> : null}
126
- <view style={rowOf(ui)}>
127
- <Button part="card.approval.confirm" primary label={t("card.confirm")} onTap={() => void ui.assistant.confirm(p.id, needsCode(p.method) ? code : null)}/>
128
- <Button part="card.approval.refuse" label={t("card.refuse")} onTap={() => void ui.assistant.refuse(p.id)}/>
129
- </view>
130
- </view>) : <text>{t("card.answered")}</text>}
121
+ const confirm = async () => {
122
+ if (needsCode(p.method) && code.length === 0)
123
+ return;
124
+ await ui.assistant.confirm(p.id, needsCode(p.method) ? code : null);
125
+ setCode("");
126
+ setEpoch((n) => n + 1);
127
+ };
128
+ return (<view className={cls(ui, "card.approval")} style={{ padding: "12px", borderRadius: "10px", borderWidth: "1px", borderColor: tok(ui, "approval-card-border", "#8a4b2a") }}>
129
+ <text style={{ fontWeight: "bold" }}>{p.readOnly ? t("card.readonly_routine") : p.title}</text>
130
+ {p.warning ? <text style={{ color: tok(ui, "warn", "#a35a00") }}>{t("card.tainted")}</text> : null}
131
+ {p.steps.map((s) => (<view key={s.index} className={cls(ui, "card.approval.step")} accessibility-element={true} accessibility-label={s.text} bindtap={() => ui.assistant.tick(p.id, s.index)}>
132
+ <view style={rowOf(ui)}><text>{s.ticked ? "☑ " : "☐ "}</text><text style={grow}>{s.text}{s.undoable ? "" : " (" + t("card.irreversible") + ")"}</text></view>
133
+ {s.facts.map((f, i) => <text key={i} className={cls(ui, "card.approval.fact")} style={{ fontSize: "12px" }}>{say(f.key, f.args)}</text>)}
134
+ {s.diff.map((d, i) => <text key={"d" + String(i)} className={cls(ui, "card.approval.fact")} style={{ fontSize: "12px" }}>{say(d.name, d.args) + ": " + (d.before ?? "—") + " ← " + (d.after ?? "—")}</text>)}
135
+ </view>))}
136
+ {p.answered === null ? (<view>
137
+ {needsCode(p.method) ? <view className={cls(ui, "card.approval.code")}><PinPad key={epoch} label={t("card.code")} onInput={setCode}/></view> : null}
138
+ {p.failCode === "confirm.waiting" ? <text>{t("card.waiting")}</text> : p.failCode ? <text>{p.failCode}{p.triesLeft !== null ? " · " + t("card.tries_left", { count: p.triesLeft }) : ""}</text> : null}
139
+ <view style={rowOf(ui)}>
140
+ <Button part="card.approval.confirm" primary label={t("card.confirm")} onTap={() => void confirm()}/>
141
+ <Button part="card.approval.refuse" label={t("card.refuse")} onTap={() => void ui.assistant.refuse(p.id)}/>
142
+ </view>
143
+ </view>) : <text>{t("card.answered")}</text>}
131
144
  </view>);
132
145
  }
133
146
  export function Message(props) {
@@ -136,68 +149,111 @@ export function Message(props) {
136
149
  const m = props.message;
137
150
  const [reveal] = useState(() => ui.assistant.watched.reveal(m.id) && m.role === "assistant" && !m.pending);
138
151
  const Approval = override(ui, "ApprovalCard") ?? ApprovalCard;
152
+ const Other = override(ui, "Card") ?? Cards;
139
153
  const plan = m.card?.kind === "plan" ? props.plans.get(m.card.id) : undefined;
140
154
  return (<view className={cls(ui, "message") + " " + cls(ui, m.role === "user" ? "message.user" : "message.assistant")} style={{ padding: "8px 12px", borderRadius: "10px", marginBottom: "8px", alignSelf: m.role === "user" ? "flex-start" : "flex-end", maxWidth: "88%",
141
- backgroundColor: tok(ui, "surface-2", "#f6f4f1"), opacity: m.pending ? 0.7 : 1 }}>
142
- {plan ? <Approval plan={plan}/> : m.card ? <Cards card={m.card} runId={props.runId}/> : (<view>
143
- {m.text.length > 0 ? <RevealText text={m.text} reveal={reveal}/> : null}
144
- {m.blocks.map((b, i) => <BlockView key={i} block={b}/>)}
145
- {m.next.length > 0 ? <view className={cls(ui, "block.actions")} style={{ ...rowOf(ui), flexWrap: "wrap" }}>{m.next.map((n) => <Button key={n} part="block.action" label={n} onTap={() => void ui.assistant.send(n)}/>)}</view> : null}
146
- {m.role === "assistant" ? (<view className={cls(ui, "message.feedback")} style={rowOf(ui)}>
147
- <Small kind="provenance" provenance={m.provenance}/>
148
- {ui.assistant.host.copy ? <Button label={t("feedback.copy")} onTap={() => void ui.assistant.host.copy?.(m.text)}/> : null}
149
- {m.provenance === "learned" ? <Button label={t("feedback.not_right")} onTap={() => void ui.assistant.notRight(m.id)}/> : null}
150
- </view>) : null}
151
- </view>)}
155
+ backgroundColor: tok(ui, "surface-2", "#f6f4f1"), opacity: m.pending ? 0.7 : 1 }}>
156
+ {plan ? <Approval plan={plan}/> : m.card ? <Other card={m.card} runId={props.runId}/> : (<view>
157
+ {m.text.length > 0 ? <RevealText text={m.text} reveal={reveal}/> : null}
158
+ {m.blocks.map((b, i) => <BlockView key={i} block={b}/>)}
159
+ {m.next.length > 0 ? <view className={cls(ui, "block.actions")} style={{ ...rowOf(ui), flexWrap: "wrap" }}>{m.next.map((n) => <Button key={n} part="block.action" label={n} onTap={() => void ui.assistant.send(n)}/>)}</view> : null}
160
+ {m.role === "assistant" ? (<view className={cls(ui, "message.feedback")} style={rowOf(ui)}>
161
+ <Small kind="provenance" provenance={m.provenance}/>
162
+ {ui.assistant.host.copy ? <Button label={t("feedback.copy")} onTap={() => void ui.assistant.host.copy?.(m.text)}/> : null}
163
+ {m.provenance === "learned" ? <Button label={t("feedback.not_right")} onTap={() => void ui.assistant.notRight(m.id)}/> : null}
164
+ </view>) : null}
165
+ </view>)}
152
166
  </view>);
153
167
  }
168
+ /** The other cards: undo all (its steps, then what the undo said), a question (options, a field or a secret field), "learn this?" and "make it a routine?". */
154
169
  export function Cards(props) {
155
170
  const ui = useUi();
156
171
  const t = ui.texts.t;
157
172
  const c = props.card;
173
+ const b = c.body;
158
174
  const answered = ui.assistant.answered.of(c.id);
159
175
  const [text, setText] = useState("");
160
- const options = Array.isArray(c.body["options"]) ? c.body["options"].map((o) => String(o.label ?? "")).filter((l) => l.length > 0) : [];
161
- if (c.kind === "undo")
162
- return <view className={cls(ui, "card.undo")}><Button label={t("undo.all")} onTap={() => void ui.assistant.undo(c.id)}/></view>;
176
+ const [undone, setUndone] = useState(null);
177
+ const options = arr(b["options"]).map(obj).map((o) => str(o["label"])).filter((l) => l.length > 0);
178
+ const steps = arr(b["steps"]).map(obj).map((s) => str(s["text"], str(s["title"], str(s["tool"]))));
179
+ const list = steps.map((s, i) => <text key={i} text-maxline="2">{String(i + 1) + ". " + s}</text>);
180
+ if (c.kind === "undo") {
181
+ return (<view className={cls(ui, "card.undo")}>
182
+ {list}
183
+ {undone === null ? <Button label={t("undo.all")} onTap={() => { ui.assistant.undo(c.id).then((r) => setUndone(str(obj(r)["text"], t("card.answered"))), () => setUndone(t("card.answered"))); }}/>
184
+ : <text style={{ fontSize: "12px", color: tok(ui, "muted", "#6b6660") }}>{undone}</text>}
185
+ </view>);
186
+ }
163
187
  if (c.kind === "ask" && props.runId) {
164
188
  const runId = props.runId;
165
- return (<view className={cls(ui, "card.ask")}>
166
- <text>{String(c.body["question"] ?? "")}</text>
167
- {answered ? <text>{t("card.answered")}</text> : (<view>
168
- {options.map((o) => <Button key={o} label={o} onTap={() => void ui.assistant.answer(runId, c.id, [o], null)}/>)}
169
- <input style={{ ...field(ui), ...grow }} type={c.body["secret"] === true ? "password" : "text"} accessibility-label={t("ask.send")} bindinput={(e) => setText(e.detail.value)}/>
170
- <Button label={t("ask.send")} onTap={() => { const v = text; setText(""); void ui.assistant.answer(runId, c.id, [], v); }}/>
171
- </view>)}
189
+ return (<view className={cls(ui, "card.ask")}>
190
+ <text>{str(b["question"])}</text>
191
+ {answered ? <text>{t("card.answered")}</text> : (<view>
192
+ {options.map((o) => <Button key={o} label={o} onTap={() => void ui.assistant.answer(runId, c.id, [o], null)}/>)}
193
+ {b["secret"] === true ? <SecretField onSubmit={(v) => void ui.assistant.answer(runId, c.id, [], v)}/> : (<view style={rowOf(ui, "8px")}>
194
+ <view style={grow}><input style={{ ...field(ui), width: "100%" }} type="text" accessibility-label={t("ask.send")} bindinput={(e) => setText(e.detail.value)}/></view>
195
+ <Button label={t("ask.send")} onTap={() => { const v = text; setText(""); void ui.assistant.answer(runId, c.id, [], v); }}/>
196
+ </view>)}
197
+ </view>)}
172
198
  </view>);
173
199
  }
174
- const kind = c.kind === "learn" ? "knowledge" : "learned";
175
- const id = typeof c.body["id"] === "string" ? c.body["id"] : c.id;
176
- return (<view className={cls(ui, "card.choice")}>
177
- <text>{c.kind === "learn" ? t("learn.ask") : t("routine.ask")}</text>
178
- {answered ? <text>{t("card.answered")}</text> : (<view style={rowOf(ui)}>
179
- <Button label={t("yes")} onTap={() => void ui.assistant.choose(kind, id, c.kind === "learn" ? "accept" : "approve")}/>
180
- <Button label={t("no")} onTap={() => void ui.assistant.choose(kind, id, c.kind === "learn" ? "refuse" : "retire")}/>
181
- </view>)}
200
+ if (c.kind !== "learn" && c.kind !== "routine" && c.kind !== "routine_revision")
201
+ return null;
202
+ const learn = c.kind === "learn";
203
+ const id = str(b["id"], c.id);
204
+ return (<view className={cls(ui, "card.choice")}>
205
+ <text>{learn ? t("learn.ask") : t("routine.ask")}</text>
206
+ {learn ? <text style={{ color: tok(ui, "muted", "#6b6660") }} text-maxline="6">{str(b["text"])}</text> : list}
207
+ {answered ? <text>{t("card.answered")}</text> : (<view style={rowOf(ui)}>
208
+ <Button label={t("yes")} onTap={() => void ui.assistant.choose(learn ? "knowledge" : "learned", id, learn ? "accept" : "approve")}/>
209
+ <Button label={t("no")} onTap={() => void ui.assistant.choose(learn ? "knowledge" : "learned", id, learn ? "refuse" : "retire")}/>
210
+ </view>)}
182
211
  </view>);
183
212
  }
213
+ /** The thinking orb: only transform and opacity change (a slow pulse); still with reduced motion. */
214
+ export const ThinkingOrb = swappable("ThinkingOrb", () => {
215
+ const ui = useUi();
216
+ const still = ui.assistant.host.reducedMotion;
217
+ const [up, setUp] = useState(false);
218
+ useEffect(() => {
219
+ if (still)
220
+ return undefined;
221
+ const h = setInterval(() => setUp((x) => !x), 600);
222
+ return () => clearInterval(h);
223
+ }, [still]);
224
+ return (<view className={cls(ui, "thinking-orb")} style={{ width: "18px", height: "18px", borderRadius: "9px", flexShrink: 0, backgroundColor: tok(ui, "accent", "#8a4b2a"),
225
+ ...(still ? {} : { transform: up ? "scale(1)" : "scale(0.8)", opacity: up ? 1 : 0.6, transition: "transform 0.6s ease-in-out, opacity 0.6s ease-in-out" }) }}/>);
226
+ });
227
+ /** Named stages lit by the run's real stage events (a pipeline's roles too, by the names given); never a show of thinking. */
228
+ export const StagesThinking = swappable("StagesThinking", (props) => {
229
+ const ui = useUi();
230
+ const names = new Map(Object.entries(props.names ?? {}));
231
+ const name = (s) => (s.role ? (names.get(s.role) ?? s.role) : ui.texts.t("stage." + s.id));
232
+ return (<view className={cls(ui, "stages-thinking")} style={{ ...rowOf(ui, "6px"), flexWrap: "wrap" }}>
233
+ {props.run.stages.filter((s) => s.id === "role" || names.has(s.id) || s.id !== "waiting").map((s, i) => (<text key={i} className={cls(ui, "work.stage")} style={{ fontSize: "12px", opacity: s.state === "on" ? 1 : 0.5 }} text-maxline="1">{name(s)}</text>))}
234
+ </view>);
235
+ });
184
236
  export function WorkCard(props) {
185
237
  const ui = useUi();
186
238
  const t = ui.texts.t;
187
239
  const r = props.run;
188
240
  const [now, setNow] = useState(Date.now());
189
241
  useEffect(() => { const h = setInterval(() => setNow(Date.now()), 1000); return () => clearInterval(h); }, []);
190
- if (r.state === "done" || r.state === "failed" || r.state === "cancelled" || r.state === "waiting-confirm" || r.state === "waiting-answer")
242
+ if (finished(r) || r.state === "waiting-confirm" || r.state === "waiting-answer")
191
243
  return null;
192
- const seconds = Math.max(0, Math.round((now - (r.startedAt ?? now)) / 1000));
193
- return (<view className={cls(ui, "work")} style={{ ...rowOf(ui), padding: "12px" }}>
194
- <view className={cls(ui, "thinking-orb")} style={{ width: "18px", height: "18px", borderRadius: "9px", backgroundColor: tok(ui, "accent", "#8a4b2a") }}/>
195
- <view style={grow}>
196
- <view style={rowOf(ui)}><text style={grow}>{r.longRunning ? t("work.long") : t("work.title")}</text><text>{t("work.elapsed", { seconds })}</text></view>
197
- <view style={{ ...rowOf(ui), flexWrap: "wrap" }}>{r.stages.filter((s) => s.id !== "waiting").map((s, i) => <text key={i} className={cls(ui, "work.stage")} style={{ marginRight: "6px", opacity: s.state === "on" ? 1 : 0.5 }}>{s.id === "role" ? (s.role ?? "") : t("stage." + s.id)}</text>)}</view>
198
- {r.progress ? <text className={cls(ui, "work.progress")} text-maxline="2">{t(r.progress.key, r.progress.args)}</text> : null}
199
- </view>
200
- <Button part="composer.stop" label={t("composer.stop")} onTap={() => void ui.assistant.stop()}/>
244
+ const started = r.startedAt ?? now;
245
+ const seconds = Math.max(0, Math.round((now - started) / 1000));
246
+ const long = r.longRunning || now - started >= LONG_RUNNING_MS;
247
+ return (<view className={cls(ui, "work")} style={{ ...rowOf(ui, "8px"), alignItems: "flex-start", padding: "12px" }}>
248
+ <ThinkingOrb />
249
+ <view style={grow}>
250
+ <view style={rowOf(ui)}><text style={grow}>{long ? t("work.long") : t("work.title")}</text><text className={cls(ui, "work.elapsed")}>{t("work.elapsed", { seconds })}</text></view>
251
+ <StagesThinking run={r}/>
252
+ {r.progress ? <text className={cls(ui, "work.progress")} text-maxline="2">{t(r.progress.key, r.progress.args)}</text> : null}
253
+ {r.tools.slice(-3).map((x, i) => <text key={i} className={cls(ui, "work.tool")} style={{ fontSize: "12px", color: tok(ui, "muted", "#6b6660") }} text-maxline="1">{t(x.title.key) === x.title.key ? x.name : t(x.title.key)}</text>)}
254
+ {r.streamingText ? <text className="g9a-stream" text-maxline="4">{r.streamingText}</text> : null}
255
+ </view>
256
+ <Button part="composer.stop" label={t("composer.stop")} onTap={() => void ui.assistant.stop()}/>
201
257
  </view>);
202
258
  }
203
259
  export function Composer(props) {
@@ -206,18 +262,17 @@ export function Composer(props) {
206
262
  const host = ui.assistant.host;
207
263
  const [text, setText] = useState("");
208
264
  const [epoch, setEpoch] = useState(0); // a Lynx input has no value prop: a new key clears it
209
- return (<view className={cls(ui, "composer")} style={{ padding: "12px" }}>
210
- {props.attachments.length > 0 ? <view className={cls(ui, "file-chips")} style={{ ...rowOf(ui), flexWrap: "wrap" }}>{props.attachments.map((a) => <Button key={a.id} part="file-chip" label={a.name + " ×"} onTap={() => ui.assistant.detach(a.id)}/>)}</view> : null}
211
- <view style={rowOf(ui)}>
212
- {host.pickFile ? <Button part="composer.attach" label={t("composer.attach")} onTap={async () => { const f = await host.pickFile?.([".pdf", ".xlsx", ".csv", "image/*"]); if (f)
213
- await ui.assistant.attach(f.name, f.bytes); }}/> : null}
214
- <view style={{ ...grow, marginLeft: "8px", marginRight: "8px" }}><input key={epoch} className={cls(ui, "composer.input")} style={{ ...field(ui), width: "100%" }} placeholder={t("composer.placeholder")} accessibility-label={t("composer.placeholder")} bindinput={(e) => setText(e.detail.value)}/></view>
265
+ return (<view className={cls(ui, "composer")} style={{ padding: "12px" }}>
266
+ {props.attachments.length > 0 ? <view className={cls(ui, "file-chips")} style={{ ...rowOf(ui), flexWrap: "wrap" }}>{props.attachments.map((a) => <Button key={a.id} part="file-chip" label={a.name + " ×"} onTap={() => ui.assistant.detach(a.id)}/>)}</view> : null}
267
+ <view style={rowOf(ui)}>
268
+ {host.pickFile || host.uploadFile ? <Button part="composer.attach" label={t("composer.attach")} onTap={() => ui.assistant.attachFromDevice()}/> : null}
269
+ <view style={{ ...grow, marginLeft: "8px", marginRight: "8px" }}><input key={epoch} className={cls(ui, "composer.input")} style={{ ...field(ui), width: "100%" }} placeholder={t("composer.placeholder")} accessibility-label={t("composer.placeholder")} bindinput={(e) => setText(e.detail.value)}/></view>
215
270
  <Button part={props.busy ? "composer.stop" : "composer.send"} primary label={props.busy ? t("composer.stop") : t("composer.send")} onTap={async () => { if (props.busy)
216
271
  await ui.assistant.stop();
217
272
  else if (await ui.assistant.send(text)) {
218
273
  setText("");
219
274
  setEpoch((n) => n + 1);
220
- } }}/>
221
- </view>
275
+ } }}/>
276
+ </view>
222
277
  </view>);
223
278
  }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The slots: the library's own simple Button, PinPad, SecretField and Dialog, each replaceable by name (`components`
3
+ * of AssistantRoot), so a host gives its design kit's pieces and the library draws with them everywhere.
4
+ */
5
+ import type { ReactNode } from "@lynx-js/react";
6
+ import type { PartName } from "@g9tm/aiassistant";
7
+ import type { LynxUi } from "./context.jsx";
8
+ /** A Lynx input has no size of its own (on a device it is drawn zero high): every input of the renderer gets one. */
9
+ export declare function field(ui: LynxUi): {
10
+ readonly height: "40px";
11
+ readonly fontSize: "15px";
12
+ readonly paddingLeft: "10px";
13
+ readonly paddingRight: "10px";
14
+ readonly borderWidth: "1px";
15
+ readonly borderStyle: "solid";
16
+ readonly borderRadius: "8px";
17
+ readonly borderColor: string;
18
+ readonly color: string;
19
+ readonly backgroundColor: string;
20
+ };
21
+ export interface ButtonProps {
22
+ label: string;
23
+ onTap: () => void;
24
+ primary?: boolean;
25
+ part?: PartName;
26
+ }
27
+ export declare const Button: import("./context.jsx").Part<ButtonProps>;
28
+ /** The code field of an approval: what is typed goes to onInput; a new key clears it (a Lynx input has no value prop). */
29
+ export declare const PinPad: import("./context.jsx").Part<{
30
+ label: string;
31
+ onInput: (value: string) => void;
32
+ }>;
33
+ /** A secret the person types: sent once as the answer, then cleared; read from the field, never kept in state. */
34
+ export declare const SecretField: import("./context.jsx").Part<{
35
+ onSubmit: (value: string) => void;
36
+ }>;
37
+ /** A dialog above the panel (sheetLayer + 1); closes from its × when onClose is given. */
38
+ export declare const Dialog: import("./context.jsx").Part<{
39
+ open: boolean;
40
+ label: string;
41
+ onClose?: () => void;
42
+ children?: ReactNode;
43
+ }>;
package/dist/slots.jsx ADDED
@@ -0,0 +1,58 @@
1
+ /**
2
+ * The slots: the library's own simple Button, PinPad, SecretField and Dialog, each replaceable by name (`components`
3
+ * of AssistantRoot), so a host gives its design kit's pieces and the library draws with them everywhere.
4
+ */
5
+ import { useRef, useState } from "@lynx-js/react";
6
+ import { cls, fill, rowOf, swappable, tok, useUi } from "./context.jsx";
7
+ /** A Lynx input has no size of its own (on a device it is drawn zero high): every input of the renderer gets one. */
8
+ export function field(ui) {
9
+ return { height: "40px", fontSize: "15px", paddingLeft: "10px", paddingRight: "10px", borderWidth: "1px", borderStyle: "solid", borderRadius: "8px",
10
+ borderColor: tok(ui, "border", "#d0d0d0"), color: tok(ui, "ink", "#111"), backgroundColor: tok(ui, "field", "#fff") };
11
+ }
12
+ export const Button = swappable("Button", (props) => {
13
+ const ui = useUi();
14
+ return (<view className={props.part ? cls(ui, props.part) : "g9a-button"} accessibility-element={true} accessibility-label={props.label} bindtap={props.onTap} style={{ minHeight: "38px", paddingLeft: "12px", paddingRight: "12px", borderRadius: tok(ui, "radius-s", "6px"), justifyContent: "center",
15
+ backgroundColor: props.primary ? tok(ui, "accent", "#8a4b2a") : tok(ui, "surface", "#fff"), borderWidth: "1px", borderColor: tok(ui, "line", "#e4ded6") }}>
16
+ <text style={{ color: props.primary ? tok(ui, "accent-ink", "#fff") : tok(ui, "ink", "#22201d") }} text-maxline="1">{props.label}</text>
17
+ </view>);
18
+ });
19
+ /** The code field of an approval: what is typed goes to onInput; a new key clears it (a Lynx input has no value prop). */
20
+ export const PinPad = swappable("PinPad", (props) => {
21
+ const ui = useUi();
22
+ return <input className="g9a-pin" style={{ ...field(ui), marginTop: "8px" }} type="password" accessibility-label={props.label} placeholder={props.label} bindinput={(e) => props.onInput(e.detail.value)}/>;
23
+ });
24
+ /** A secret the person types: sent once as the answer, then cleared; read from the field, never kept in state. */
25
+ export const SecretField = swappable("SecretField", (props) => {
26
+ const ui = useUi();
27
+ const t = ui.texts.t;
28
+ const value = useRef("");
29
+ const [epoch, setEpoch] = useState(0);
30
+ const send = () => {
31
+ const v = value.current;
32
+ if (v.length === 0)
33
+ return;
34
+ value.current = "";
35
+ setEpoch((n) => n + 1);
36
+ props.onSubmit(v);
37
+ };
38
+ return (<view className={cls(ui, "secret-field")} style={rowOf(ui, "8px")}>
39
+ <view style={{ ...fill, minWidth: 0 }}>
40
+ <input key={epoch} style={{ ...field(ui), width: "100%" }} type="password" accessibility-label={t("secret.hint")} placeholder={t("secret.hint")} bindinput={(e) => { value.current = e.detail.value; }} bindconfirm={send}/>
41
+ </view>
42
+ <Button label={t("ask.send")} onTap={send}/>
43
+ </view>);
44
+ });
45
+ /** A dialog above the panel (sheetLayer + 1); closes from its × when onClose is given. */
46
+ export const Dialog = swappable("Dialog", (props) => {
47
+ const ui = useUi();
48
+ if (!props.open)
49
+ return null;
50
+ return (<view className="g9a-dialog" accessibility-label={props.label} style={{ position: "fixed", top: "10%", bottom: "10%", left: "8%", right: "8%", zIndex: ui.layer + 1, display: "flex", flexDirection: "column", padding: "12px",
51
+ borderRadius: tok(ui, "radius", "10px"), backgroundColor: tok(ui, "surface", "#fff"), borderWidth: "1px", borderColor: tok(ui, "line", "#e4ded6") }}>
52
+ {props.onClose ? (<view style={rowOf(ui)}>
53
+ <view style={{ ...fill, minWidth: 0 }}><text style={{ fontWeight: "bold" }} text-maxline="1">{props.label}</text></view>
54
+ <view accessibility-element={true} accessibility-label={ui.texts.t("panel.close")} bindtap={props.onClose} style={{ paddingLeft: "8px", paddingRight: "8px" }}><text>×</text></view>
55
+ </view>) : null}
56
+ <scroll-view scroll-orientation="vertical" style={{ ...fill }}>{props.children}</scroll-view>
57
+ </view>);
58
+ });
@@ -19,6 +19,12 @@ export declare function MessageList(props: {
19
19
  runId: string | null;
20
20
  }): import("@lynx-js/react").JSX.Element;
21
21
  export declare function AssistantChat(): import("@lynx-js/react").JSX.Element;
22
+ /**
23
+ * "Download PDF" of the open conversation: a signed link from the server (ten minutes), opened through the host's
24
+ * openUrl. Shown only when the conversation has messages and the host can open a link; busy while the link comes; a
25
+ * small notice when it fails. Replaceable as `PdfDownload`.
26
+ */
27
+ export declare const PdfDownload: import("./context.jsx").Part<object>;
22
28
  export declare function AssistantPanel(props: {
23
29
  open: boolean;
24
30
  layout?: Layout;
@@ -26,3 +32,21 @@ export declare function AssistantPanel(props: {
26
32
  onClose?: () => void;
27
33
  title?: string;
28
34
  }): import("@lynx-js/react").JSX.Element | null;
35
+ export type SurfaceKind = "hero" | "bar-button" | "ask-about";
36
+ export interface SurfaceProps {
37
+ kind: SurfaceKind;
38
+ greeting?: string;
39
+ starts?: string[];
40
+ subject?: {
41
+ label: string;
42
+ pageId: string;
43
+ kind?: string;
44
+ id?: string;
45
+ };
46
+ onOpen?: () => void;
47
+ }
48
+ /**
49
+ * The small surfaces: the dashboard hero (greeting, one status line, three starts, a place to write), the bar button
50
+ * with the count of cards waiting, and "ask the assistant" about one thing (sent with the page and the subject).
51
+ */
52
+ export declare const Surfaces: import("./context.jsx").Part<SurfaceProps>;