@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/README.md +8 -2
- package/dist/admin.jsx +1 -1
- package/dist/chart.d.ts +14 -0
- package/dist/chart.jsx +75 -0
- package/dist/context.d.ts +7 -0
- package/dist/context.jsx +10 -0
- package/dist/index.d.ts +11 -3
- package/dist/index.js +6 -3
- package/dist/pages.d.ts +41 -0
- package/dist/pages.jsx +336 -0
- package/dist/parts.d.ts +20 -8
- package/dist/parts.jsx +158 -103
- package/dist/slots.d.ts +43 -0
- package/dist/slots.jsx +58 -0
- package/dist/surfaces.d.ts +24 -0
- package/dist/surfaces.jsx +124 -27
- package/package.json +2 -2
package/dist/surfaces.jsx
CHANGED
|
@@ -3,18 +3,19 @@
|
|
|
3
3
|
* layouts. Messages use a recycled list with a stable key; the answer still growing is drawn below the list so its
|
|
4
4
|
* measurement does not break.
|
|
5
5
|
*/
|
|
6
|
-
import { useEffect } from "@lynx-js/react";
|
|
7
|
-
import { pickLayout } from "@g9tm/aiassistant";
|
|
8
|
-
import { cls, fill, override, rowOf, tok, UiProvider, useSnapshot, useUi } from "./context.jsx";
|
|
6
|
+
import { useEffect, useState } from "@lynx-js/react";
|
|
7
|
+
import { formatNumber, obj, pickLayout, str } from "@g9tm/aiassistant";
|
|
8
|
+
import { cls, fill, override, rowOf, swappable, tok, UiProvider, useSnapshot, useUi } from "./context.jsx";
|
|
9
9
|
import { Composer, Message, Small, WorkCard } from "./parts.jsx";
|
|
10
|
+
import { Button, field } from "./slots.jsx";
|
|
10
11
|
/** Default tokens (light and dark); the host's theme tokens override them. */
|
|
11
12
|
const LIGHT = { "surface": "#ffffff", "surface-2": "#f6f4f1", "ink": "#22201d", "muted": "#6b6660", "line": "#e4ded6", "accent": "#8a4b2a", "accent-ink": "#ffffff", "warn": "#a35a00", "danger": "#b42318", "radius": "10px", "radius-s": "6px", "chart-bar": "#8a4b2a", "approval-card-border": "#8a4b2a" };
|
|
12
13
|
const DARK = { ...LIGHT, "surface": "#1d1b19", "surface-2": "#272421", "ink": "#efeae4", "muted": "#a39c94", "line": "#3a3632", "accent": "#e0a074", "accent-ink": "#1d1b19", "chart-bar": "#e0a074", "approval-card-border": "#e0a074" };
|
|
13
14
|
export function AssistantRoot(props) {
|
|
14
15
|
const host = props.assistant.host;
|
|
15
16
|
const theme = { ...(host.theme.mode === "dark" ? DARK : LIGHT), ...host.theme.tokens };
|
|
16
|
-
return (<UiProvider value={{ assistant: props.assistant, texts: props.assistant.texts, styles: props.styles ?? {}, unstyled: props.unstyled ?? false, components: props.components ?? {}, theme, layer: (host.sheetLayer ?? 1000) + 1 }}>
|
|
17
|
-
{props.children}
|
|
17
|
+
return (<UiProvider value={{ assistant: props.assistant, texts: props.assistant.texts, styles: props.styles ?? {}, unstyled: props.unstyled ?? false, components: props.components ?? {}, theme, layer: (host.sheetLayer ?? 1000) + 1 }}>
|
|
18
|
+
{props.children}
|
|
18
19
|
</UiProvider>);
|
|
19
20
|
}
|
|
20
21
|
export function MessageList(props) {
|
|
@@ -22,13 +23,13 @@ export function MessageList(props) {
|
|
|
22
23
|
const Item = override(ui, "Message") ?? Message;
|
|
23
24
|
const settled = props.messages.slice(0, -1);
|
|
24
25
|
const last = props.messages[props.messages.length - 1];
|
|
25
|
-
return (<view className={cls(ui, "messages")} style={{ display: "flex", flexDirection: "column", ...fill, minHeight: 0 }}>
|
|
26
|
-
<list scroll-orientation="vertical" style={{ ...fill }} list-type="single">
|
|
27
|
-
{settled.map((m) => (<list-item key={m.id} item-key={m.id}>
|
|
28
|
-
<Item message={m} plans={props.plans} runId={props.runId}/>
|
|
29
|
-
</list-item>))}
|
|
30
|
-
</list>
|
|
31
|
-
{last ? <Item key={last.id} message={last} plans={props.plans} runId={props.runId}/> : null}
|
|
26
|
+
return (<view className={cls(ui, "messages")} style={{ display: "flex", flexDirection: "column", ...fill, minHeight: 0 }}>
|
|
27
|
+
<list scroll-orientation="vertical" style={{ ...fill }} list-type="single">
|
|
28
|
+
{settled.map((m) => (<list-item key={m.id} item-key={m.id}>
|
|
29
|
+
<Item message={m} plans={props.plans} runId={props.runId}/>
|
|
30
|
+
</list-item>))}
|
|
31
|
+
</list>
|
|
32
|
+
{last ? <Item key={last.id} message={last} plans={props.plans} runId={props.runId}/> : null}
|
|
32
33
|
</view>);
|
|
33
34
|
}
|
|
34
35
|
export function AssistantChat() {
|
|
@@ -38,32 +39,128 @@ export function AssistantChat() {
|
|
|
38
39
|
const Work = override(ui, "WorkCard") ?? WorkCard;
|
|
39
40
|
const Compose = override(ui, "Composer") ?? Composer;
|
|
40
41
|
useEffect(() => { ui.assistant.stateWatch.start(); }, [ui.assistant]); // the mode, cards waiting and usage (as in Svelte)
|
|
41
|
-
return (<view className={cls(ui, "chat")} style={{ display: "flex", flexDirection: "column", ...fill, minHeight: 0, backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
42
|
-
<Small kind="mode" state={s.state}/>
|
|
43
|
-
<List messages={s.messages} plans={s.plans} runId={s.run?.runId ?? null}/>
|
|
44
|
-
{s.run ? <Work run={s.run}/> : null}
|
|
45
|
-
{s.error ? <text style={{ color: tok(ui, "danger", "#b42318") }}>{ui.texts.t(s.error)}</text> : null}
|
|
46
|
-
<Compose busy={s.busy} attachments={s.attachments}/>
|
|
42
|
+
return (<view className={cls(ui, "chat")} style={{ display: "flex", flexDirection: "column", ...fill, minHeight: 0, backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
43
|
+
<Small kind="mode" state={s.state}/>
|
|
44
|
+
<List messages={s.messages} plans={s.plans} runId={s.run?.runId ?? null}/>
|
|
45
|
+
{s.run ? <Work run={s.run}/> : null}
|
|
46
|
+
{s.error ? <text style={{ color: tok(ui, "danger", "#b42318") }}>{ui.texts.t(s.error)}</text> : null}
|
|
47
|
+
<Compose busy={s.busy} attachments={s.attachments}/>
|
|
47
48
|
</view>);
|
|
48
49
|
}
|
|
50
|
+
/**
|
|
51
|
+
* "Download PDF" of the open conversation: a signed link from the server (ten minutes), opened through the host's
|
|
52
|
+
* openUrl. Shown only when the conversation has messages and the host can open a link; busy while the link comes; a
|
|
53
|
+
* small notice when it fails. Replaceable as `PdfDownload`.
|
|
54
|
+
*/
|
|
55
|
+
export const PdfDownload = swappable("PdfDownload", () => {
|
|
56
|
+
const ui = useUi();
|
|
57
|
+
const t = ui.texts.t;
|
|
58
|
+
const host = ui.assistant.host;
|
|
59
|
+
const s = useSnapshot(ui.assistant);
|
|
60
|
+
const [busy, setBusy] = useState(false);
|
|
61
|
+
const [failed, setFailed] = useState(false);
|
|
62
|
+
const id = s.conversationId;
|
|
63
|
+
if (!id || s.messages.length === 0 || typeof host.openUrl !== "function")
|
|
64
|
+
return null;
|
|
65
|
+
const download = async () => {
|
|
66
|
+
if (busy)
|
|
67
|
+
return;
|
|
68
|
+
setBusy(true);
|
|
69
|
+
setFailed(false);
|
|
70
|
+
try {
|
|
71
|
+
const url = str(obj(await ui.assistant.api.documentLink(id))["url"]);
|
|
72
|
+
if (url.length === 0)
|
|
73
|
+
setFailed(true);
|
|
74
|
+
else
|
|
75
|
+
host.openUrl?.(url);
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
setFailed(true);
|
|
79
|
+
}
|
|
80
|
+
finally {
|
|
81
|
+
setBusy(false);
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
return (<view className="g9a-pdf" style={{ marginLeft: "12px", marginRight: "12px" }}>
|
|
85
|
+
<view accessibility-element={true} accessibility-label={t("pdf.download")} bindtap={() => void download()} style={{ opacity: busy ? 0.6 : 1 }}>
|
|
86
|
+
<text text-maxline="1">{busy ? t("pdf.busy") : t("pdf.download")}</text>
|
|
87
|
+
</view>
|
|
88
|
+
{failed ? <text className="g9a-pdf-failed" style={{ fontSize: "11px", color: tok(ui, "danger", "#b42318") }} text-maxline="2">{t("pdf.failed")}</text> : null}
|
|
89
|
+
</view>);
|
|
90
|
+
});
|
|
49
91
|
export function AssistantPanel(props) {
|
|
50
92
|
const ui = useUi();
|
|
51
93
|
const t = ui.texts.t;
|
|
52
94
|
const layout = pickLayout(props.layout ?? "auto", props.width);
|
|
53
|
-
|
|
95
|
+
// a host's own Sheet (its design kit's bottom sheet) holds the panel in the sheet layout, and opens and closes it
|
|
96
|
+
const Sheet = layout === "sheet" ? override(ui, "Sheet") : undefined;
|
|
97
|
+
if (!Sheet && !props.open && layout !== "inline" && layout !== "card")
|
|
54
98
|
return null;
|
|
99
|
+
const header = layout !== "compact" ? (<view className={cls(ui, "panel.header")} style={{ ...rowOf(ui), padding: "12px" }}>
|
|
100
|
+
{/* on a device a text does not grow in a row: a view takes the room, and the link keeps its distance */}
|
|
101
|
+
<view style={{ ...fill, minWidth: 0 }}><text style={{ fontWeight: "bold" }} text-maxline="1">{props.title ?? t("assistant.name")}</text></view>
|
|
102
|
+
<PdfDownload />
|
|
103
|
+
<view accessibility-element={true} accessibility-label={t("panel.new")} bindtap={() => ui.assistant.fresh()} style={{ marginLeft: "12px", marginRight: "12px" }}><text>{t("panel.new")}</text></view>
|
|
104
|
+
{props.onClose ? <view accessibility-element={true} accessibility-label={t("panel.close")} bindtap={props.onClose} style={{ marginLeft: "12px" }}><text>×</text></view> : null}
|
|
105
|
+
</view>) : null;
|
|
106
|
+
if (Sheet) {
|
|
107
|
+
return (<Sheet open={props.open} onClose={props.onClose} label={props.title ?? t("assistant.name")} layer={ui.layer - 1}>
|
|
108
|
+
<view className={cls(ui, "panel")} style={{ direction: "ltr", display: "flex", flexDirection: "column", ...fill, minHeight: 0, backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
109
|
+
{header}
|
|
110
|
+
<AssistantChat />
|
|
111
|
+
</view>
|
|
112
|
+
</Sheet>);
|
|
113
|
+
}
|
|
55
114
|
const frame = layout === "sheet" ? { position: "fixed", left: 0, right: 0, bottom: 0, height: "85%", borderTopLeftRadius: "16px", borderTopRightRadius: "16px" }
|
|
56
115
|
: layout === "side" ? { position: "fixed", top: 0, bottom: 0, right: 0, width: "420px" }
|
|
57
116
|
: layout === "fullscreen" ? { position: "fixed", top: 0, bottom: 0, left: 0, right: 0 }
|
|
58
117
|
: layout === "card" ? { borderRadius: tok(ui, "radius", "10px"), maxHeight: "520px" }
|
|
59
118
|
: { width: "100%", height: "100%" };
|
|
60
|
-
return (<view className={cls(ui, "panel")} style={{ ...frame, direction: "ltr", display: "flex", flexDirection: "column", zIndex: layout === "inline" || layout === "card" ? 0 : ui.layer - 1, backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
61
|
-
{
|
|
62
|
-
|
|
63
|
-
<view style={{ ...fill, minWidth: 0 }}><text style={{ fontWeight: "bold" }} text-maxline="1">{props.title ?? t("assistant.name")}</text></view>
|
|
64
|
-
<view accessibility-element={true} accessibility-label={t("panel.new")} bindtap={() => ui.assistant.fresh()} style={{ marginLeft: "12px", marginRight: "12px" }}><text>{t("panel.new")}</text></view>
|
|
65
|
-
{props.onClose ? <view accessibility-element={true} accessibility-label={t("panel.close")} bindtap={props.onClose} style={{ marginLeft: "12px" }}><text>×</text></view> : null}
|
|
66
|
-
</view>) : null}
|
|
67
|
-
<AssistantChat />
|
|
119
|
+
return (<view className={cls(ui, "panel")} style={{ ...frame, direction: "ltr", display: "flex", flexDirection: "column", zIndex: layout === "inline" || layout === "card" ? 0 : ui.layer - 1, backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
120
|
+
{header}
|
|
121
|
+
<AssistantChat />
|
|
68
122
|
</view>);
|
|
69
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* The small surfaces: the dashboard hero (greeting, one status line, three starts, a place to write), the bar button
|
|
126
|
+
* with the count of cards waiting, and "ask the assistant" about one thing (sent with the page and the subject).
|
|
127
|
+
*/
|
|
128
|
+
export const Surfaces = swappable("Surfaces", (props) => {
|
|
129
|
+
const ui = useUi();
|
|
130
|
+
const t = ui.texts.t;
|
|
131
|
+
const s = useSnapshot(ui.assistant);
|
|
132
|
+
const [text, setText] = useState("");
|
|
133
|
+
const [epoch, setEpoch] = useState(0); // a Lynx input has no value prop: a new key clears it
|
|
134
|
+
const ask = async (q) => {
|
|
135
|
+
if (!q.trim())
|
|
136
|
+
return;
|
|
137
|
+
const sub = props.subject;
|
|
138
|
+
const page = sub ? { pageId: sub.pageId, ...(sub.kind ? { subjectKind: sub.kind } : {}), ...(sub.id ? { subjectId: sub.id } : {}), subjectLabel: sub.label } : undefined;
|
|
139
|
+
await ui.assistant.send(q, page);
|
|
140
|
+
setText("");
|
|
141
|
+
setEpoch((n) => n + 1);
|
|
142
|
+
props.onOpen?.();
|
|
143
|
+
};
|
|
144
|
+
if (props.kind === "bar-button") {
|
|
145
|
+
const waiting = s.state.waiting;
|
|
146
|
+
return (<view className={cls(ui, "bar-button")} accessibility-element={true} accessibility-label={t("assistant.name")} bindtap={() => props.onOpen?.()} style={{ ...rowOf(ui, "6px"), minHeight: "38px", paddingLeft: "12px", paddingRight: "12px", borderRadius: tok(ui, "radius-s", "6px"), borderWidth: "1px", borderColor: tok(ui, "line", "#e4ded6"), backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
147
|
+
<text style={{ color: tok(ui, "ink", "#22201d") }} text-maxline="1">{t("assistant.name")}</text>
|
|
148
|
+
{waiting > 0 ? <view className="g9a-badge" style={{ paddingLeft: "6px", paddingRight: "6px", borderRadius: "9px", backgroundColor: tok(ui, "danger", "#b42318") }}><text style={{ color: "#fff", fontSize: "11px" }}>{formatNumber(waiting, ui.texts.locale)}</text></view> : null}
|
|
149
|
+
</view>);
|
|
150
|
+
}
|
|
151
|
+
if (props.kind === "ask-about")
|
|
152
|
+
return <Button part="ask-about" label={t("assistant.name") + ": " + (props.subject?.label ?? "")} onTap={() => void ask(props.subject?.label ?? "")}/>;
|
|
153
|
+
return (<view className={cls(ui, "hero")} style={{ padding: "12px", borderRadius: tok(ui, "radius", "10px"), borderWidth: "1px", borderColor: tok(ui, "line", "#e4ded6"), backgroundColor: tok(ui, "surface", "#fff") }}>
|
|
154
|
+
<text style={{ fontWeight: "bold", fontSize: "18px", color: tok(ui, "ink", "#22201d") }} text-maxline="2">{props.greeting || t("assistant.name")}</text>
|
|
155
|
+
{s.state.mode !== "on" ? <text style={{ fontSize: "12px", color: tok(ui, "muted", "#6b6660") }}>{t(s.state.mode === "off" ? "mode.off" : "mode.offline")}</text> : null}
|
|
156
|
+
<view className={cls(ui, "block.actions")} style={{ ...rowOf(ui, "6px"), flexWrap: "wrap", marginTop: "8px", marginBottom: "8px" }}>
|
|
157
|
+
{(props.starts ?? []).slice(0, 3).map((x) => <Button key={x} part="block.action" label={x} onTap={() => void ask(x)}/>)}
|
|
158
|
+
</view>
|
|
159
|
+
<view style={rowOf(ui, "8px")}>
|
|
160
|
+
<view style={{ ...fill, minWidth: 0 }}>
|
|
161
|
+
<input key={epoch} style={{ ...field(ui), width: "100%" }} placeholder={t("composer.placeholder")} accessibility-label={t("composer.placeholder")} bindinput={(e) => setText(e.detail.value)} bindconfirm={(e) => void ask(e.detail.value)}/>
|
|
162
|
+
</view>
|
|
163
|
+
<Button part="composer.send" primary label={t("composer.send")} onTap={() => void ask(text)}/>
|
|
164
|
+
</view>
|
|
165
|
+
</view>);
|
|
166
|
+
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@g9tm/aiassistant-lynx",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "ReactLynx components for the G9 AI assistant, fully customisable (layouts, tokens, per-part classes, overrides, parts).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": {
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@g9tm/aiassistant": "1.
|
|
32
|
+
"@g9tm/aiassistant": "1.2.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@lynx-js/react": "0.129.2",
|