rift-ds 1.30.0 → 1.31.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 +6 -4
- package/behaviors/locale.d.ts +7 -0
- package/behaviors/locale.js +5 -0
- package/behaviors/shell.css +33 -0
- package/bin/rift-ds.mjs +35 -9
- package/bin/templates/blocks/app/planner/PlannerGrid.tsx +58 -0
- package/bin/templates/blocks/app/planner/Requests.tsx +42 -0
- package/bin/templates/blocks/app/planner/page.module.css +17 -0
- package/bin/templates/blocks/app/planner/page.tsx +32 -0
- package/bin/templates/blocks/registry.json +14 -0
- package/bin/templates/chat/app/api/chat/route.actions.ts +108 -0
- package/bin/templates/chat/components/chat/ChatMount.actions.tsx +55 -0
- package/bin/templates/chat/components/chat/ChatMount.tsx +14 -1
- package/bin/templates/chat/components/chat/proposals/ProposalCard.tsx +81 -0
- package/bin/templates/chat/components/chat/proposals/actions.ts +60 -0
- package/bin/templates/chat/components/chat/proposals/confirmation.ts +9 -0
- package/bin/templates/chat/components/chat/proposals/renderers.tsx +9 -0
- package/chat/ChatLauncher.d.ts +4 -1
- package/chat/ChatLauncher.js +6 -1
- package/chat/ChatProvider.d.ts +19 -2
- package/chat/ChatProvider.js +26 -3
- package/chat/ChatWidget.css +10 -0
- package/chat/ChatWidget.d.ts +2 -1
- package/chat/ChatWidget.js +8 -6
- package/chat/actions.d.ts +52 -6
- package/chat/actions.js +53 -5
- package/chat/contextSchema.d.ts +76 -0
- package/chat/contextSchema.js +58 -0
- package/chat/index.d.ts +2 -2
- package/chat/index.js +5 -1
- package/chat/registry.json.d.ts +2 -2
- package/chat/registry.json.js +1 -1
- package/chat/server.d.ts +18 -2
- package/chat/server.js +10 -1
- package/chat/suggestions.d.ts +7 -0
- package/chat/suggestions.js +13 -1
- package/chat/types.d.ts +23 -0
- package/chat/useChat.d.ts +2 -2
- package/chat/useChat.js +15 -1
- package/components/AppSidebar/AppSidebar.css +12 -0
- package/components/ButtonGroup/ButtonGroup.css +10 -1
- package/components/Chart/RadarChart.js +24 -10
- package/components/DataTable/DataTable.css +17 -2
- package/components/DataTable/DataTable.d.ts +14 -2
- package/components/DataTable/DataTable.js +13 -2
- package/components/DataTable/periodColumns.d.ts +37 -0
- package/components/DataTable/periodColumns.js +48 -0
- package/components/DatePicker/DatePicker.css +22 -6
- package/components/DatePicker/DatePicker.d.ts +2 -1
- package/components/DatePicker/DatePicker.js +2 -2
- package/components/DateRangePicker/DateRangePicker.js +2 -2
- package/components/DecisionRow/DecisionRow.css +46 -0
- package/components/DecisionRow/DecisionRow.d.ts +62 -0
- package/components/DecisionRow/DecisionRow.js +93 -0
- package/components/EventCalendar/EventCalendar.css +54 -0
- package/components/EventCalendar/EventCalendar.d.ts +27 -5
- package/components/EventCalendar/EventCalendar.js +50 -15
- package/components/FunnelChart/FunnelChart.js +33 -22
- package/components/Globe/Globe.css +7 -0
- package/components/Globe/Globe.js +1 -1
- package/components/Table/Table.css +7 -0
- package/components/Table/Table.js +1 -1
- package/components/Tabs/Tabs.css +21 -3
- package/components/Timeline/Timeline.css +8 -0
- package/components/Widget/Widget.css +2 -0
- package/components/WorldMap/WorldMap.css +15 -0
- package/components/WorldMap/WorldMap.js +56 -1
- package/components/registry.json +20 -3
- package/components/registry.json.d.ts +20 -3
- package/components/registry.json.js +1 -1
- package/index.d.ts +1 -0
- package/index.js +6 -0
- package/package.json +1 -1
- package/tokens/presets/blueprint.css +9 -0
- package/tokens/presets/bubblegum.css +9 -0
- package/tokens/presets/contrast.css +3 -3
- package/tokens/presets/coral.css +3 -3
- package/tokens/presets/ember.css +9 -0
- package/tokens/presets/getaway.css +9 -0
- package/tokens/presets/gold.css +3 -3
- package/tokens/presets/mono.css +3 -3
- package/tokens/presets/pink.css +3 -3
- package/tokens/presets/plum.css +3 -3
- package/tokens/presets/signal.css +9 -0
- package/tokens/presets/smoke.css +9 -0
- package/tokens/presets/velvet.css +9 -0
- package/tokens/presets/violet.css +3 -3
- package/tokens/presets/volt.css +9 -0
- package/tokens/presets/warm.css +3 -3
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The cards of the propose-and-confirm flow, as the chat's renderers.
|
|
5
|
+
*
|
|
6
|
+
* `proposal` is a change the assistant planned: an ActionCard with Confirm
|
|
7
|
+
* and Cancel. Confirm runs the action on the page (`applyPlan`, yours to
|
|
8
|
+
* write), marks the request it sends so the route knows this message was
|
|
9
|
+
* confirmed on its card (a typed "yes" never is), and sends the reader's
|
|
10
|
+
* words as their message. The card then settles to one row.
|
|
11
|
+
*
|
|
12
|
+
* `record` is what was done, which the route sends as the reply's lead card:
|
|
13
|
+
* the same ActionCard, confirmed. Each card's state is kept by its id outside
|
|
14
|
+
* React, so a redraw or a reopened panel never offers Confirm again.
|
|
15
|
+
* renderers.tsx hands both to the chat.
|
|
16
|
+
*/
|
|
17
|
+
import { useSyncExternalStore } from 'react';
|
|
18
|
+
import { ActionCard } from 'rift-ds/components/ActionCard/ActionCard';
|
|
19
|
+
import { skippedText, type ActionPlan } from 'rift-ds/chat/actions';
|
|
20
|
+
import { useChatWidget } from 'rift-ds/chat';
|
|
21
|
+
import { ACTIONS, theseItems, type ActionId } from './actions';
|
|
22
|
+
import { confirmation } from './confirmation';
|
|
23
|
+
|
|
24
|
+
/** Apply a confirmed plan to your page's data. Replace this with your own write. */
|
|
25
|
+
function applyPlan(plan: ActionPlan) {
|
|
26
|
+
console.info('Apply', plan.action, 'to', plan.ids);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
type CardStatus = 'proposed' | 'confirmed' | 'dismissed';
|
|
30
|
+
const statuses = new Map<string, CardStatus>();
|
|
31
|
+
const listeners = new Set<() => void>();
|
|
32
|
+
const setStatus = (id: string, status: CardStatus) => {
|
|
33
|
+
statuses.set(id, status);
|
|
34
|
+
listeners.forEach((listener) => listener());
|
|
35
|
+
};
|
|
36
|
+
const subscribe = (listener: () => void) => {
|
|
37
|
+
listeners.add(listener);
|
|
38
|
+
return () => listeners.delete(listener);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type ProposalData = ActionPlan & { id: string };
|
|
42
|
+
|
|
43
|
+
export function Proposal({ id, action, ids, skipped }: ProposalData) {
|
|
44
|
+
const { send, streaming } = useChatWidget();
|
|
45
|
+
const status = useSyncExternalStore<CardStatus>(subscribe, () => statuses.get(id) ?? 'proposed', () => 'proposed');
|
|
46
|
+
const definition = ACTIONS[action as ActionId];
|
|
47
|
+
if (!definition) return null;
|
|
48
|
+
const left = skippedText(skipped);
|
|
49
|
+
return (
|
|
50
|
+
<ActionCard
|
|
51
|
+
title={`${definition.label}: ${ids.length} ${ids.length === 1 ? 'item' : 'items'}`}
|
|
52
|
+
detail={[definition.effect?.(ids.length), left && `Left alone: ${left}.`].filter(Boolean).join(' ')}
|
|
53
|
+
icon={definition.icon}
|
|
54
|
+
tone={definition.destructive ? 'destructive' : 'default'}
|
|
55
|
+
status={status}
|
|
56
|
+
onConfirm={
|
|
57
|
+
streaming
|
|
58
|
+
? undefined
|
|
59
|
+
: () => {
|
|
60
|
+
applyPlan({ action, ids, skipped });
|
|
61
|
+
setStatus(id, 'confirmed');
|
|
62
|
+
confirmation.mark({ action: action as ActionId, count: ids.length, ids });
|
|
63
|
+
send(definition.confirm(theseItems(ids.length)));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
onDismiss={() => setStatus(id, 'dismissed')}
|
|
67
|
+
/>
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function RecordCard({ action, count }: { action: string; count: number }) {
|
|
72
|
+
const definition = ACTIONS[action as ActionId];
|
|
73
|
+
if (!definition) return null;
|
|
74
|
+
return (
|
|
75
|
+
<ActionCard
|
|
76
|
+
title={`${count} ${count === 1 ? 'item' : 'items'} ${definition.done}`}
|
|
77
|
+
icon={definition.icon}
|
|
78
|
+
status="confirmed"
|
|
79
|
+
/>
|
|
80
|
+
);
|
|
81
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The changes the chat may propose, defined once for the page and the route.
|
|
3
|
+
*
|
|
4
|
+
* A chat proposes and the reader confirms: the route's tool plans an action
|
|
5
|
+
* over the records the request means and sends the plan as a card; the card
|
|
6
|
+
* (ProposalCard.tsx) runs it on the reader's Confirm and sends their words as
|
|
7
|
+
* their message; the route opens the reply with the record of what was done.
|
|
8
|
+
* The page's own buttons should run these same actions, so the chat and the
|
|
9
|
+
* page can never do different things to the same records.
|
|
10
|
+
*
|
|
11
|
+
* Replace the records and the actions with your own. This module imports
|
|
12
|
+
* nothing but rift-ds/chat/actions, so the route and the browser can both
|
|
13
|
+
* import it.
|
|
14
|
+
*/
|
|
15
|
+
import { defineAction } from 'rift-ds/chat/actions';
|
|
16
|
+
|
|
17
|
+
/** One record the actions act on. Yours will have more fields. */
|
|
18
|
+
export type Item = { title: string; state: 'open' | 'done' | 'archived' };
|
|
19
|
+
|
|
20
|
+
export const ACTIONS = {
|
|
21
|
+
complete: defineAction<Item>({
|
|
22
|
+
id: 'complete',
|
|
23
|
+
label: 'Mark done',
|
|
24
|
+
done: 'marked done',
|
|
25
|
+
confirm: (these) => `Mark ${these} done`,
|
|
26
|
+
effect: (count) => (count === 1 ? 'It moves to done.' : 'They move to done.'),
|
|
27
|
+
icon: 'check_circle',
|
|
28
|
+
skip: (item) => (item.state === 'open' ? null : 'not open'),
|
|
29
|
+
apply: () => ({ state: 'done' }),
|
|
30
|
+
}),
|
|
31
|
+
archive: defineAction<Item>({
|
|
32
|
+
id: 'archive',
|
|
33
|
+
label: 'Archive',
|
|
34
|
+
done: 'archived',
|
|
35
|
+
confirm: (these) => `Archive ${these}`,
|
|
36
|
+
effect: (count) => (count === 1 ? 'It leaves the list.' : 'They leave the list.'),
|
|
37
|
+
icon: 'archive',
|
|
38
|
+
destructive: true,
|
|
39
|
+
skip: (item) => (item.state === 'done' ? null : 'not done'),
|
|
40
|
+
apply: () => ({ state: 'archived' }),
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export type ActionId = keyof typeof ACTIONS;
|
|
45
|
+
|
|
46
|
+
/** "this item" or "these 3 items", for the reader's confirming words. */
|
|
47
|
+
export const theseItems = (count: number) => (count === 1 ? 'this item' : `these ${count} items`);
|
|
48
|
+
|
|
49
|
+
/** The noun after a count, for the line that tells the model what was confirmed. */
|
|
50
|
+
export const itemNoun = (count: number) => (count === 1 ? 'item' : 'items');
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Sample records, so the example runs. Replace with a read of your own
|
|
54
|
+
* data: the route plans against it, and the page applies to it.
|
|
55
|
+
*/
|
|
56
|
+
export const SAMPLE_ITEMS: Record<string, Item> = {
|
|
57
|
+
'item-1': { title: 'Write the brief', state: 'open' },
|
|
58
|
+
'item-2': { title: 'Book the venue', state: 'done' },
|
|
59
|
+
'item-3': { title: 'Send the invites', state: 'open' },
|
|
60
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The page's one-shot confirmation mark: a card's Confirm sets it, and the
|
|
3
|
+
* next request's context takes it, once. ChatMount's getContext reads it;
|
|
4
|
+
* the route rebuilds it as untrusted input, so a typed "yes" never counts.
|
|
5
|
+
*/
|
|
6
|
+
import { createConfirmationMark } from 'rift-ds/chat/actions';
|
|
7
|
+
import type { ActionId } from './actions';
|
|
8
|
+
|
|
9
|
+
export const confirmation = createConfirmationMark<ActionId>();
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
|
|
3
|
+
/** The chat's card renderers, by kind: pass to ChatProvider's `renderers`. */
|
|
4
|
+
import { Proposal, RecordCard, type ProposalData } from './ProposalCard';
|
|
5
|
+
|
|
6
|
+
export const ACTION_RENDERERS = {
|
|
7
|
+
proposal: (data: unknown) => <Proposal {...(data as ProposalData)} />,
|
|
8
|
+
record: (data: unknown) => <RecordCard {...(data as { action: string; count: number })} />,
|
|
9
|
+
};
|
package/chat/ChatLauncher.d.ts
CHANGED
|
@@ -83,7 +83,10 @@ export interface ChatLauncherProps {
|
|
|
83
83
|
* writes the chosen dock width to `--ds-chat-width` there; a host's
|
|
84
84
|
* stylesheet pads the page beside a docked panel and hides it behind a
|
|
85
85
|
* takeover from those two facts (the package's own stylesheet does not
|
|
86
|
-
* touch the host's body).
|
|
86
|
+
* touch the host's body). An app shell needs no rule of its own: the shell
|
|
87
|
+
* recipe (src/behaviors/shell.css) gives the outermost `data-ds-shell` host,
|
|
88
|
+
* AppLayout and the app-shell block among them, the panel's width plus its
|
|
89
|
+
* edge gap on the docked side. The panel's geometry reads `--ds-chat-width`,
|
|
87
90
|
* `--ds-chat-inset` and `--ds-chat-float-height`, defaulted on `:root`.
|
|
88
91
|
*/
|
|
89
92
|
export declare function ChatLauncher({ label, icon, summary, hidden, panelId, hostAttribute, panelLabel, aiButtonLabels, widget, children, surface, fab, }: ChatLauncherProps): import("react").JSX.Element | null;
|
package/chat/ChatLauncher.js
CHANGED
|
@@ -8,6 +8,7 @@ import { useChatAgentState } from "./agentState.js";
|
|
|
8
8
|
import { useChatWidget } from "./context.js";
|
|
9
9
|
import { isFloatDragStart, FLOAT_EDGE_GAP, FLOAT_MAX_SCALE, dockWidthFromDrag, clampFloatPosition } from "./placement.js";
|
|
10
10
|
import { useDockViewport, TAKEOVER_QUERY } from "./queries.js";
|
|
11
|
+
import { keepFittingSuggestions } from "./suggestions.js";
|
|
11
12
|
import "./ChatWidget.css";
|
|
12
13
|
const CHAT_PANEL_ID = "ds-chat-panel";
|
|
13
14
|
const FLOAT_GRIPS = [
|
|
@@ -261,7 +262,11 @@ function ChatLauncher({
|
|
|
261
262
|
title: summary.title,
|
|
262
263
|
caption: summary.caption,
|
|
263
264
|
text: summary.text,
|
|
264
|
-
suggestions:
|
|
265
|
+
suggestions: keepFittingSuggestions(
|
|
266
|
+
summary.suggestions ?? summary.chips?.map(({ id, label: chipLabel }) => ({ id, label: chipLabel })) ?? [],
|
|
267
|
+
(chip) => chip.label,
|
|
268
|
+
"ChatLauncher summary chips"
|
|
269
|
+
)
|
|
265
270
|
} : void 0,
|
|
266
271
|
onSummarySuggestion: launchSuggestion,
|
|
267
272
|
onClick: (event) => {
|
package/chat/ChatProvider.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ChatArtifactRenderers, ChatCardRenderers, ChatLinkRenderer, FeedbackSin
|
|
|
3
3
|
import { FollowupsSource } from './followups';
|
|
4
4
|
import { ChatPlacement } from './placement';
|
|
5
5
|
import { ThreadsOptions } from './threads';
|
|
6
|
-
import { ChatTransport } from './types';
|
|
6
|
+
import { ChatPageAction, ChatTransport } from './types';
|
|
7
7
|
import { ChatPlaceholderCopy } from './useChat';
|
|
8
8
|
export interface ChatProviderProps {
|
|
9
9
|
/**
|
|
@@ -12,6 +12,23 @@ export interface ChatProviderProps {
|
|
|
12
12
|
* `scriptChat()` runs the widget with no backend at all.
|
|
13
13
|
*/
|
|
14
14
|
transport?: ChatTransport;
|
|
15
|
+
/**
|
|
16
|
+
* Wraps the default transport instead of replacing it: handed the fetch
|
|
17
|
+
* transport, which carries the reader's model pick and `getContext`, it
|
|
18
|
+
* returns the transport the chat uses, so a host can add to the stream (a
|
|
19
|
+
* card the reply opens with, a log) and keep that wiring. Pass a stable
|
|
20
|
+
* function, declared outside the component or memoised, that reads what it
|
|
21
|
+
* needs when it runs rather than from render. Ignored with a `transport`.
|
|
22
|
+
*/
|
|
23
|
+
wrapTransport?: (base: ChatTransport) => ChatTransport;
|
|
24
|
+
/**
|
|
25
|
+
* What the page does when a reply sends an `action` event, by the action's
|
|
26
|
+
* `name`: go to a page, set a table's view, open a tab. Each runs once, as
|
|
27
|
+
* the event arrives, never again on a redraw. The input came from the model,
|
|
28
|
+
* so a handler checks it before acting on it. A name with no handler is
|
|
29
|
+
* ignored, with a warning in development.
|
|
30
|
+
*/
|
|
31
|
+
pageActions?: Record<string, (input: unknown, action: ChatPageAction) => void>;
|
|
15
32
|
/** The endpoint the default transport posts to. */
|
|
16
33
|
endpoint?: string;
|
|
17
34
|
/**
|
|
@@ -66,4 +83,4 @@ export interface ChatProviderProps {
|
|
|
66
83
|
* clicking between pages for free. A hard reload starts clean by design:
|
|
67
84
|
* no storage, no persistence.
|
|
68
85
|
*/
|
|
69
|
-
export declare function ChatProvider({ transport, endpoint, getContext, defaultPlacement, followups, fallbackFollowups, feedback, renderers, artifactRenderers, renderLink, isInternalHref, threads: threadsOptions, placeholders, children, }: ChatProviderProps): import("react").JSX.Element;
|
|
86
|
+
export declare function ChatProvider({ transport, wrapTransport, pageActions, endpoint, getContext, defaultPlacement, followups, fallbackFollowups, feedback, renderers, artifactRenderers, renderLink, isInternalHref, threads: threadsOptions, placeholders, children, }: ChatProviderProps): import("react").JSX.Element;
|
package/chat/ChatProvider.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
import { jsx } from "react/jsx-runtime";
|
|
3
|
-
import { useState, useMemo, useRef, useEffect
|
|
3
|
+
import { useState, useMemo, useCallback, useRef, useEffect } from "react";
|
|
4
4
|
import { MOTION_EXIT_SYNC_MS } from "../tokens/motion.js";
|
|
5
5
|
import { createFeedbackPoster, isInternalPath, ChatWidgetContext } from "./context.js";
|
|
6
6
|
import { createFollowupsFetcher, followupTarget, DEFAULT_FALLBACK_FOLLOWUPS, pickFallbackFollowups } from "./followups.js";
|
|
7
7
|
import { useThreads } from "./threads.js";
|
|
8
8
|
import { createFetchTransport } from "./transport.js";
|
|
9
|
+
import { keepFittingSuggestions } from "./suggestions.js";
|
|
10
|
+
import { warnOnce } from "../behaviors/devWarning.js";
|
|
9
11
|
import { DEFAULT_PLACEHOLDER_COPY, useChat } from "./useChat.js";
|
|
10
12
|
import { useLinkComponent } from "../behaviors/LinkProvider.js";
|
|
11
13
|
function DefaultChatLink({ href, children, onNavigate }) {
|
|
@@ -20,6 +22,8 @@ function DefaultChatLink({ href, children, onNavigate }) {
|
|
|
20
22
|
const defaultRenderLink = (props) => /* @__PURE__ */ jsx(DefaultChatLink, { ...props });
|
|
21
23
|
function ChatProvider({
|
|
22
24
|
transport,
|
|
25
|
+
wrapTransport,
|
|
26
|
+
pageActions,
|
|
23
27
|
endpoint = "/api/chat",
|
|
24
28
|
getContext,
|
|
25
29
|
defaultPlacement = "right",
|
|
@@ -43,7 +47,22 @@ function ChatProvider({
|
|
|
43
47
|
() => ({ ...DEFAULT_PLACEHOLDER_COPY, ...placeholders }),
|
|
44
48
|
[placeholders]
|
|
45
49
|
);
|
|
46
|
-
const
|
|
50
|
+
const wrappedTransport = useMemo(
|
|
51
|
+
() => wrapTransport ? wrapTransport(fallbackTransport) : fallbackTransport,
|
|
52
|
+
[wrapTransport, fallbackTransport]
|
|
53
|
+
);
|
|
54
|
+
const runPageAction = useCallback(
|
|
55
|
+
(action) => {
|
|
56
|
+
const handler = pageActions?.[action.name];
|
|
57
|
+
if (!handler) {
|
|
58
|
+
warnOnce(`ChatProvider: no pageActions handler named "${action.name}", so the action was ignored.`);
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
handler(action.input, action);
|
|
62
|
+
},
|
|
63
|
+
[pageActions]
|
|
64
|
+
);
|
|
65
|
+
const chat = useChat(transport ?? wrappedTransport, placeholderCopy, runPageAction);
|
|
47
66
|
const threads = useThreads(chat, threadsOptions);
|
|
48
67
|
const followupsSource = useMemo(() => {
|
|
49
68
|
if (followups === false) return null;
|
|
@@ -57,7 +76,11 @@ function ChatProvider({
|
|
|
57
76
|
if (!target || askedRef.current.has(target.id)) return;
|
|
58
77
|
askedRef.current.add(target.id);
|
|
59
78
|
const offer = (suggestions) => {
|
|
60
|
-
const offered = suggestions.length > 0 ? suggestions : pickFallbackFollowups(
|
|
79
|
+
const offered = suggestions.length > 0 ? suggestions : pickFallbackFollowups(
|
|
80
|
+
keepFittingSuggestions(fallbackFollowups, (label) => label, "ChatProvider fallbackFollowups"),
|
|
81
|
+
target.id,
|
|
82
|
+
target.question
|
|
83
|
+
);
|
|
61
84
|
setTurnFollowups(target.id, offered);
|
|
62
85
|
setFreshFollowupsId(target.id);
|
|
63
86
|
};
|
package/chat/ChatWidget.css
CHANGED
|
@@ -329,6 +329,16 @@
|
|
|
329
329
|
flex-shrink: 0;
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
+
/* Widgets taller than the whole widget (a board stacked to one column in a
|
|
333
|
+
narrow panel) have no room to ride up into, so the welcome scrolls
|
|
334
|
+
rather than letting its host's edge cut the board off. While they fit
|
|
335
|
+
there is nothing to scroll, and the layout above is unchanged. */
|
|
336
|
+
.ds-chat-widget[data-widgets="fit"]:has(.ds-chat-widget__welcome) {
|
|
337
|
+
overflow-y: auto;
|
|
338
|
+
overscroll-behavior: contain;
|
|
339
|
+
scrollbar-width: thin;
|
|
340
|
+
}
|
|
341
|
+
|
|
332
342
|
/* The widgets claim their height first, so on a short window an even split
|
|
333
343
|
of what is left would crowd the greeting into the header. The top region
|
|
334
344
|
keeps room for the header and the greeting. A phone never centres, so it
|
package/chat/ChatWidget.d.ts
CHANGED
|
@@ -194,7 +194,8 @@ export interface ChatWidgetProps {
|
|
|
194
194
|
/** How the widget slot takes its room. `fit` keeps the welcome screen to
|
|
195
195
|
one viewport: the composer holds dead centre as it does over the
|
|
196
196
|
starters, the widgets hanging below it, and rides up only when they
|
|
197
|
-
need more than half the room
|
|
197
|
+
need more than half the room; widgets taller than the whole widget
|
|
198
|
+
scroll with the welcome rather than being cut off. `scroll` turns the welcome screen into a page: the
|
|
198
199
|
greeting and composer take the opening of the viewport, the widgets
|
|
199
200
|
run on below the fold, and the whole welcome scrolls as one. */
|
|
200
201
|
widgetsLayout?: 'fit' | 'scroll';
|
package/chat/ChatWidget.js
CHANGED
|
@@ -27,7 +27,7 @@ import { useChatWidget, ArtifactSeatContext } from "./context.js";
|
|
|
27
27
|
import { DEFAULT_GREETING_COPY, subscribeClock, greetingFor, serverGreeting } from "./greeting.js";
|
|
28
28
|
import { CHAT_PLACEMENTS, chatPlacement } from "./placement.js";
|
|
29
29
|
import { ASIDE_WITH_THREADS_MIN_WIDTH, THREADS_RAIL_MIN_WIDTH, ARTIFACT_MIN_COLUMN_WIDTH, ASIDE_RAIL_MIN_WIDTH } from "./queries.js";
|
|
30
|
-
import {
|
|
30
|
+
import { keepFittingSuggestions } from "./suggestions.js";
|
|
31
31
|
import { TurnCard } from "./TurnContent.js";
|
|
32
32
|
import { isBlockingQuestion } from "./questions.js";
|
|
33
33
|
import { ReplyBody } from "./TurnQuestion.js";
|
|
@@ -199,7 +199,7 @@ function ChatWidget({
|
|
|
199
199
|
() => greetingFor((/* @__PURE__ */ new Date()).getHours(), greetingCopy),
|
|
200
200
|
serverGreeting
|
|
201
201
|
);
|
|
202
|
-
const starters = (startersInput ?? []
|
|
202
|
+
const starters = keepFittingSuggestions(startersInput ?? [], (starter) => starter.label, "ChatWidget starters");
|
|
203
203
|
const composerRef = useRef(null);
|
|
204
204
|
const focusComposer = () => composerRef.current?.focus({ preventScroll: true });
|
|
205
205
|
useEffect(() => {
|
|
@@ -279,6 +279,7 @@ function ChatWidget({
|
|
|
279
279
|
const [artifactEmptyOpen, setArtifactEmptyOpen] = useState(false);
|
|
280
280
|
const [artifactExpandedWanted, setArtifactExpanded] = useState(false);
|
|
281
281
|
const wasAsideWideRef = useRef(true);
|
|
282
|
+
const [seatMeasured, setSeatMeasured] = useState(false);
|
|
282
283
|
useEffect(() => {
|
|
283
284
|
if (!hasRails) return;
|
|
284
285
|
const host = railsHostRef.current;
|
|
@@ -298,6 +299,7 @@ function ChatWidget({
|
|
|
298
299
|
closeArtifact();
|
|
299
300
|
}
|
|
300
301
|
wasAsideWideRef.current = asideRoom;
|
|
302
|
+
setSeatMeasured(true);
|
|
301
303
|
};
|
|
302
304
|
measure();
|
|
303
305
|
const observer = new ResizeObserver(measure);
|
|
@@ -806,7 +808,7 @@ function ChatWidget({
|
|
|
806
808
|
artifacts && !asideWide && /* @__PURE__ */ jsxs(
|
|
807
809
|
"div",
|
|
808
810
|
{
|
|
809
|
-
className: `ds-chat-widget__aside-overlay ${artifactShown ? "ds-chat-widget__aside-overlay--open" : ""}`,
|
|
811
|
+
className: `ds-chat-widget__aside-overlay ${artifactShown && seatMeasured ? "ds-chat-widget__aside-overlay--open" : ""}`,
|
|
810
812
|
children: [
|
|
811
813
|
/* @__PURE__ */ jsx(
|
|
812
814
|
"button",
|
|
@@ -814,7 +816,7 @@ function ChatWidget({
|
|
|
814
816
|
type: "button",
|
|
815
817
|
className: "ds-chat-widget__rail-scrim",
|
|
816
818
|
"aria-label": closeArtifactLabel,
|
|
817
|
-
tabIndex: artifactShown ? 0 : -1,
|
|
819
|
+
tabIndex: artifactShown && seatMeasured ? 0 : -1,
|
|
818
820
|
onClick: closeArtifactPanel
|
|
819
821
|
}
|
|
820
822
|
),
|
|
@@ -864,7 +866,7 @@ function ChatWidget({
|
|
|
864
866
|
renderAside && !asideWide && /* @__PURE__ */ jsxs(
|
|
865
867
|
"div",
|
|
866
868
|
{
|
|
867
|
-
className: `ds-chat-widget__aside-overlay ${asideShown ? "ds-chat-widget__aside-overlay--open" : ""}`,
|
|
869
|
+
className: `ds-chat-widget__aside-overlay ${asideShown && seatMeasured ? "ds-chat-widget__aside-overlay--open" : ""}`,
|
|
868
870
|
children: [
|
|
869
871
|
/* @__PURE__ */ jsx(
|
|
870
872
|
"button",
|
|
@@ -872,7 +874,7 @@ function ChatWidget({
|
|
|
872
874
|
type: "button",
|
|
873
875
|
className: "ds-chat-widget__rail-scrim",
|
|
874
876
|
"aria-label": closeAgentLabel,
|
|
875
|
-
tabIndex: asideShown ? 0 : -1,
|
|
877
|
+
tabIndex: asideShown && seatMeasured ? 0 : -1,
|
|
876
878
|
onClick: closeAside
|
|
877
879
|
}
|
|
878
880
|
),
|
package/chat/actions.d.ts
CHANGED
|
@@ -25,7 +25,16 @@
|
|
|
25
25
|
* the route rebuilds the mark from the request (`sanitiseConfirmation`), and
|
|
26
26
|
* the model is told whether this message was confirmed on its card
|
|
27
27
|
* (`confirmationLine`). A typed "yes, cancel them" carries no mark.
|
|
28
|
+
*
|
|
29
|
+
* A change can also be a sequence: several different actions in order, where
|
|
30
|
+
* each one acts on the records as the ones before it left them ("close the
|
|
31
|
+
* open items, then archive the closed ones"). `planSequence` dry-runs the
|
|
32
|
+
* steps in turn over one set of records, and the reader confirms the whole
|
|
33
|
+
* sequence once; its confirmation carries each step (`confirmSequence`), and
|
|
34
|
+
* `confirmationLine` tells the model all of them, in order. A single action
|
|
35
|
+
* is a sequence of one.
|
|
28
36
|
*/
|
|
37
|
+
export { defineContext, type ContextField, type ContextShape, type ContextValues, type ContextSchema } from './contextSchema';
|
|
29
38
|
export interface ChatAction<Values, Edit = Partial<Values>> {
|
|
30
39
|
/** A stable id the server and the page share. */
|
|
31
40
|
id: string;
|
|
@@ -61,12 +70,42 @@ export declare function planAction<Values>(action: ChatAction<Values, unknown>,
|
|
|
61
70
|
id: string;
|
|
62
71
|
values: Values;
|
|
63
72
|
}[]): ActionPlan;
|
|
64
|
-
/**
|
|
65
|
-
export interface
|
|
73
|
+
/** One step of a sequence: an action, and the records it is meant for. */
|
|
74
|
+
export interface SequenceStep<Values> {
|
|
75
|
+
action: ChatAction<Values, unknown>;
|
|
76
|
+
/** The records this step means, by id. A record the sequence was not handed is ignored. */
|
|
77
|
+
ids: readonly string[];
|
|
78
|
+
}
|
|
79
|
+
/** What a sequence would do: each step's plan, in order, worked out over the records as the steps before it left them. */
|
|
80
|
+
export interface SequencePlan {
|
|
81
|
+
steps: ActionPlan[];
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The dry run of a sequence. Each step is planned over the records as the
|
|
85
|
+
* earlier steps would leave them, so a step that depends on the one before
|
|
86
|
+
* it (archive what the first step closed) counts the right records. Changes
|
|
87
|
+
* nothing: the writes are simulated on copies.
|
|
88
|
+
*/
|
|
89
|
+
export declare function planSequence<Values extends object>(steps: readonly SequenceStep<Values>[], records: {
|
|
90
|
+
id: string;
|
|
91
|
+
values: Values;
|
|
92
|
+
}[]): SequencePlan;
|
|
93
|
+
/** One confirmed step: which action, how many records it changed, and their ids. */
|
|
94
|
+
export interface ConfirmedStep<Id extends string = string> {
|
|
66
95
|
action: Id;
|
|
67
96
|
count: number;
|
|
68
97
|
ids: string[];
|
|
69
98
|
}
|
|
99
|
+
/**
|
|
100
|
+
* A change the reader confirmed on its card: which action, how many records it
|
|
101
|
+
* changed, and their ids. A confirmed sequence carries every step in `steps`,
|
|
102
|
+
* in order, with the first step's action and the total count at the top.
|
|
103
|
+
*/
|
|
104
|
+
export interface ActionConfirmation<Id extends string = string> extends ConfirmedStep<Id> {
|
|
105
|
+
steps?: ConfirmedStep<Id>[];
|
|
106
|
+
}
|
|
107
|
+
/** The confirmation for a sequence plan, ready to mark: its steps, the first step's action, the total count. Steps that change nothing are left out; null when none change anything. */
|
|
108
|
+
export declare function confirmSequence<Id extends string = string>(plan: SequencePlan, maxIds?: number): ActionConfirmation<Id> | null;
|
|
70
109
|
/** The page's one-shot mark: set on Confirm, read once by the next request, then gone. */
|
|
71
110
|
export interface ConfirmationMark<Id extends string = string> {
|
|
72
111
|
/** Marks the request about to carry the reader's confirmation. */
|
|
@@ -85,14 +124,21 @@ export interface SanitiseConfirmationOptions {
|
|
|
85
124
|
maxCount: number;
|
|
86
125
|
/** The most ids kept. Defaults to 50. */
|
|
87
126
|
maxIds?: number;
|
|
127
|
+
/** The most steps a confirmed sequence may carry. Defaults to 10. */
|
|
128
|
+
maxSteps?: number;
|
|
88
129
|
}
|
|
89
|
-
/**
|
|
90
|
-
|
|
130
|
+
/**
|
|
131
|
+
* The confirmation a request claims, rebuilt as untrusted input: a known
|
|
132
|
+
* action, a count in range, real ids only. A sequence's steps are each held
|
|
133
|
+
* to the same rules, and one bad step drops the whole claim, so the model is
|
|
134
|
+
* never told part of a sequence was confirmed. Null for none.
|
|
135
|
+
*/
|
|
136
|
+
export declare function sanitiseConfirmation(value: unknown, { actions, isId, maxCount, maxIds, maxSteps }: SanitiseConfirmationOptions): ActionConfirmation | null;
|
|
91
137
|
export interface ConfirmationLineOptions {
|
|
92
138
|
/** The actions a confirmation may name, by id, for the words that say what was done. */
|
|
93
139
|
actions: Record<string, Pick<ChatAction<never, unknown>, 'done'>>;
|
|
94
|
-
/** The records' noun for a count: "order" for one, "orders" for more. */
|
|
95
|
-
noun: (count: number) => string;
|
|
140
|
+
/** The records' noun for a count: "order" for one, "orders" for more. Handed the step's action id too, for a sequence whose steps act on different records. */
|
|
141
|
+
noun: (count: number, action: string) => string;
|
|
96
142
|
/** The locale the count is written in. */
|
|
97
143
|
locale?: string;
|
|
98
144
|
}
|
package/chat/actions.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { DEFAULT_LOCALE } from "../behaviors/locale.js";
|
|
2
|
+
import { defineContext } from "./contextSchema.js";
|
|
2
3
|
const defineAction = (action) => action;
|
|
3
4
|
function planAction(action, records) {
|
|
4
5
|
const ids = [];
|
|
@@ -10,6 +11,33 @@ function planAction(action, records) {
|
|
|
10
11
|
}
|
|
11
12
|
return { action: action.id, ids, skipped };
|
|
12
13
|
}
|
|
14
|
+
function planSequence(steps, records) {
|
|
15
|
+
const current = new Map(records.map((record) => [record.id, record.values]));
|
|
16
|
+
return {
|
|
17
|
+
steps: steps.map(({ action, ids }) => {
|
|
18
|
+
const meant = ids.flatMap((id) => {
|
|
19
|
+
const values = current.get(id);
|
|
20
|
+
return values ? [{ id, values }] : [];
|
|
21
|
+
});
|
|
22
|
+
const plan = planAction(action, meant);
|
|
23
|
+
for (const id of plan.ids) {
|
|
24
|
+
const values = current.get(id);
|
|
25
|
+
current.set(id, { ...values, ...action.apply(values) });
|
|
26
|
+
}
|
|
27
|
+
return plan;
|
|
28
|
+
})
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function confirmSequence(plan, maxIds = 50) {
|
|
32
|
+
const steps = plan.steps.filter((step) => step.ids.length > 0).map((step) => ({ action: step.action, count: step.ids.length, ids: step.ids.slice(0, maxIds) }));
|
|
33
|
+
if (!steps.length) return null;
|
|
34
|
+
return {
|
|
35
|
+
action: steps[0].action,
|
|
36
|
+
count: steps.reduce((total, step) => total + step.count, 0),
|
|
37
|
+
ids: [...new Set(steps.flatMap((step) => step.ids))].slice(0, maxIds),
|
|
38
|
+
steps
|
|
39
|
+
};
|
|
40
|
+
}
|
|
13
41
|
function createConfirmationMark() {
|
|
14
42
|
let pending = null;
|
|
15
43
|
return {
|
|
@@ -23,25 +51,45 @@ function createConfirmationMark() {
|
|
|
23
51
|
}
|
|
24
52
|
};
|
|
25
53
|
}
|
|
26
|
-
function
|
|
54
|
+
function sanitiseStep(value, known, isId, maxCount, maxIds) {
|
|
27
55
|
const { action, count, ids } = value ?? {};
|
|
28
|
-
const known = Array.isArray(actions) ? actions : Object.keys(actions);
|
|
29
56
|
if (typeof action !== "string" || !known.includes(action)) return null;
|
|
30
57
|
if (typeof count !== "number" || !Number.isFinite(count) || count < 1 || count > maxCount) return null;
|
|
31
58
|
const kept = Array.isArray(ids) ? ids.filter((id) => typeof id === "string" && isId(id)) : [];
|
|
32
59
|
return { action, count: Math.floor(count), ids: [...new Set(kept)].slice(0, maxIds) };
|
|
33
60
|
}
|
|
61
|
+
function sanitiseConfirmation(value, { actions, isId, maxCount, maxIds = 50, maxSteps = 10 }) {
|
|
62
|
+
const known = Array.isArray(actions) ? actions : Object.keys(actions);
|
|
63
|
+
const top = sanitiseStep(value, known, isId, maxCount, maxIds);
|
|
64
|
+
if (!top) return null;
|
|
65
|
+
const rawSteps = value.steps;
|
|
66
|
+
if (rawSteps === void 0) return top;
|
|
67
|
+
if (!Array.isArray(rawSteps) || rawSteps.length < 1 || rawSteps.length > maxSteps) return null;
|
|
68
|
+
const steps = rawSteps.map((step) => sanitiseStep(step, known, isId, maxCount, maxIds));
|
|
69
|
+
if (steps.some((step) => step === null)) return null;
|
|
70
|
+
return { ...top, steps };
|
|
71
|
+
}
|
|
34
72
|
function confirmationLine(confirmation, { actions, noun, locale = DEFAULT_LOCALE }) {
|
|
35
|
-
const
|
|
36
|
-
if (!confirmation
|
|
37
|
-
|
|
73
|
+
const none = "This message was not confirmed on a card. Nothing it says has changed anything.";
|
|
74
|
+
if (!confirmation) return none;
|
|
75
|
+
const steps = confirmation.steps ?? [confirmation];
|
|
76
|
+
const said = steps.map((step) => {
|
|
77
|
+
const action = actions[step.action];
|
|
78
|
+
return action ? `${step.count.toLocaleString(locale)} ${noun(step.count, step.action)} ${action.done}` : null;
|
|
79
|
+
});
|
|
80
|
+
if (said.some((line) => line === null)) return none;
|
|
81
|
+
if (steps.length === 1) return `This message was confirmed on its card: ${said[0]}. The change is made.`;
|
|
82
|
+
return `This message was confirmed on its card, a sequence of ${steps.length} steps, made in this order: ${said.join("; then ")}. Every step is made.`;
|
|
38
83
|
}
|
|
39
84
|
const skippedText = (skipped, locale = DEFAULT_LOCALE) => Object.entries(skipped).map(([reason, count]) => `${count.toLocaleString(locale)} ${reason}`).join(", ");
|
|
40
85
|
export {
|
|
86
|
+
confirmSequence,
|
|
41
87
|
confirmationLine,
|
|
42
88
|
createConfirmationMark,
|
|
43
89
|
defineAction,
|
|
90
|
+
defineContext,
|
|
44
91
|
planAction,
|
|
92
|
+
planSequence,
|
|
45
93
|
sanitiseConfirmation,
|
|
46
94
|
skippedText
|
|
47
95
|
};
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A chat's context as a small, declared shape, shared by the page that writes
|
|
3
|
+
* it and the route that reads it.
|
|
4
|
+
*
|
|
5
|
+
* The default context is the page path alone, because the context lands in a
|
|
6
|
+
* system block and anything the browser sends is untrusted. A host whose
|
|
7
|
+
* state lives in the browser (the tab that is open, the record in view, the
|
|
8
|
+
* filter a table holds) needs to say more than a path, and the safe way to
|
|
9
|
+
* say more is to say only things that cannot carry an instruction: ids that
|
|
10
|
+
* match a pattern, words from a fixed list, numbers in a range, flags.
|
|
11
|
+
*
|
|
12
|
+
* `defineContext` takes that shape once. The page builds its context with
|
|
13
|
+
* `write` (handed to `getContext`), and the route passes `sanitise` as the
|
|
14
|
+
* handler's `sanitiseContext`. Whatever arrives, `sanitise` keeps only the
|
|
15
|
+
* fields the shape names and only the values each one allows, drops the
|
|
16
|
+
* rest without rejecting the request, and returns the kept fields as a JSON
|
|
17
|
+
* string, or null when nothing survives. `read` parses that string back for
|
|
18
|
+
* a tool. No dependencies, so a page and its route can both import it.
|
|
19
|
+
*/
|
|
20
|
+
/** What one field of a context may hold. */
|
|
21
|
+
export type ContextField =
|
|
22
|
+
/** A same-site path: lowercase slug segments only. */
|
|
23
|
+
{
|
|
24
|
+
kind: 'path';
|
|
25
|
+
}
|
|
26
|
+
/** One word from a fixed list. */
|
|
27
|
+
| {
|
|
28
|
+
kind: 'oneOf';
|
|
29
|
+
values: readonly string[];
|
|
30
|
+
}
|
|
31
|
+
/** One id matching `pattern`. */
|
|
32
|
+
| {
|
|
33
|
+
kind: 'id';
|
|
34
|
+
pattern: RegExp;
|
|
35
|
+
}
|
|
36
|
+
/** Ids matching `pattern`, at most `max` of them (default 50). */
|
|
37
|
+
| {
|
|
38
|
+
kind: 'ids';
|
|
39
|
+
pattern: RegExp;
|
|
40
|
+
max?: number;
|
|
41
|
+
}
|
|
42
|
+
/** A finite number between `min` and `max`. */
|
|
43
|
+
| {
|
|
44
|
+
kind: 'number';
|
|
45
|
+
min?: number;
|
|
46
|
+
max?: number;
|
|
47
|
+
}
|
|
48
|
+
/** True or false. */
|
|
49
|
+
| {
|
|
50
|
+
kind: 'flag';
|
|
51
|
+
};
|
|
52
|
+
export type ContextShape = Record<string, ContextField>;
|
|
53
|
+
type FieldValue<F extends ContextField> = F extends {
|
|
54
|
+
kind: 'ids';
|
|
55
|
+
} ? string[] : F extends {
|
|
56
|
+
kind: 'number';
|
|
57
|
+
} ? number : F extends {
|
|
58
|
+
kind: 'flag';
|
|
59
|
+
} ? boolean : string;
|
|
60
|
+
/** The values a shape allows, every field optional. */
|
|
61
|
+
export type ContextValues<S extends ContextShape> = {
|
|
62
|
+
[K in keyof S]?: FieldValue<S[K]>;
|
|
63
|
+
};
|
|
64
|
+
export interface ContextSchema<S extends ContextShape> {
|
|
65
|
+
/** The shape, as given. */
|
|
66
|
+
shape: S;
|
|
67
|
+
/** The context string for these values, or null when there are none. What a page returns from `getContext`. */
|
|
68
|
+
write: (values: ContextValues<S>) => string | null;
|
|
69
|
+
/** The handler's `sanitiseContext`: only the fields and values the shape allows, as a JSON string, or null. */
|
|
70
|
+
sanitise: (raw: unknown) => string | null;
|
|
71
|
+
/** The sanitised string parsed back into values, for a tool or a system block. */
|
|
72
|
+
read: (context: string | null) => ContextValues<S>;
|
|
73
|
+
}
|
|
74
|
+
/** Declares a context shape once, for the page that writes it and the route that sanitises it. */
|
|
75
|
+
export declare function defineContext<S extends ContextShape>(shape: S): ContextSchema<S>;
|
|
76
|
+
export {};
|