rift-ds 1.29.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.
Files changed (194) hide show
  1. package/README.md +6 -3
  2. package/behaviors/locale.d.ts +7 -0
  3. package/behaviors/locale.js +5 -0
  4. package/behaviors/shell.css +33 -0
  5. package/bin/rift-ds.mjs +36 -10
  6. package/bin/templates/app/components/rift/ColorModeScript.tsx +7 -3
  7. package/bin/templates/blocks/app/planner/PlannerGrid.tsx +58 -0
  8. package/bin/templates/blocks/app/planner/Requests.tsx +42 -0
  9. package/bin/templates/blocks/app/planner/page.module.css +17 -0
  10. package/bin/templates/blocks/app/planner/page.tsx +32 -0
  11. package/bin/templates/blocks/app/settings/ProfileForm.tsx +98 -0
  12. package/bin/templates/blocks/app/settings/page.module.css +11 -0
  13. package/bin/templates/blocks/app/settings/page.tsx +84 -0
  14. package/bin/templates/blocks/components/blocks/AppShell.module.css +8 -41
  15. package/bin/templates/blocks/components/blocks/AppShell.tsx +9 -11
  16. package/bin/templates/blocks/registry.json +27 -0
  17. package/bin/templates/chat/app/api/chat/feedback/route.ts +0 -3
  18. package/bin/templates/chat/app/api/chat/followups/route.ts +0 -3
  19. package/bin/templates/chat/app/api/chat/route.actions.ts +108 -0
  20. package/bin/templates/chat/app/api/chat/route.ts +0 -2
  21. package/bin/templates/chat/components/chat/ChatMount.actions.tsx +55 -0
  22. package/bin/templates/chat/components/chat/ChatMount.tsx +14 -1
  23. package/bin/templates/chat/components/chat/proposals/ProposalCard.tsx +81 -0
  24. package/bin/templates/chat/components/chat/proposals/actions.ts +60 -0
  25. package/bin/templates/chat/components/chat/proposals/confirmation.ts +9 -0
  26. package/bin/templates/chat/components/chat/proposals/renderers.tsx +9 -0
  27. package/chat/ChatLauncher.d.ts +13 -2
  28. package/chat/ChatLauncher.js +9 -2
  29. package/chat/ChatProvider.d.ts +19 -2
  30. package/chat/ChatProvider.js +26 -3
  31. package/chat/ChatWidget.css +10 -0
  32. package/chat/ChatWidget.d.ts +2 -1
  33. package/chat/ChatWidget.js +8 -6
  34. package/chat/actions.d.ts +52 -6
  35. package/chat/actions.js +53 -5
  36. package/chat/contextSchema.d.ts +76 -0
  37. package/chat/contextSchema.js +58 -0
  38. package/chat/index.d.ts +2 -2
  39. package/chat/index.js +5 -1
  40. package/chat/registry.json.d.ts +2 -2
  41. package/chat/registry.json.js +1 -1
  42. package/chat/server.d.ts +18 -2
  43. package/chat/server.js +10 -1
  44. package/chat/suggestions.d.ts +7 -0
  45. package/chat/suggestions.js +13 -1
  46. package/chat/types.d.ts +23 -0
  47. package/chat/useChat.d.ts +2 -2
  48. package/chat/useChat.js +15 -1
  49. package/components/Alert/Alert.css +12 -0
  50. package/components/Alert/Alert.d.ts +2 -0
  51. package/components/Alert/Alert.js +3 -1
  52. package/components/AppSidebar/AppSidebar.css +12 -0
  53. package/components/Attachment/Attachment.css +92 -0
  54. package/components/Attachment/FileRow.d.ts +47 -0
  55. package/components/Attachment/FileRow.js +38 -0
  56. package/components/Avatar/Avatar.css +14 -0
  57. package/components/Avatar/Avatar.d.ts +3 -1
  58. package/components/Avatar/Avatar.js +3 -2
  59. package/components/ButtonGroup/ButtonGroup.css +10 -1
  60. package/components/Chart/AreaChart.d.ts +2 -0
  61. package/components/Chart/AreaChart.js +2 -1
  62. package/components/Chart/BarChart.d.ts +2 -0
  63. package/components/Chart/BarChart.js +2 -1
  64. package/components/Chart/ComboChart.d.ts +4 -0
  65. package/components/Chart/ComboChart.js +4 -2
  66. package/components/Chart/LineChart.d.ts +2 -0
  67. package/components/Chart/LineChart.js +2 -1
  68. package/components/Chart/PieChart.d.ts +4 -4
  69. package/components/Chart/PieChart.js +1 -1
  70. package/components/Chart/RadarChart.js +24 -10
  71. package/components/Chart/ScatterChart.d.ts +2 -0
  72. package/components/Chart/ScatterChart.js +2 -1
  73. package/components/Checklist/Checklist.css +123 -0
  74. package/components/Checklist/Checklist.d.ts +66 -0
  75. package/components/Checklist/Checklist.js +115 -0
  76. package/components/CircularButton/CircularButton.css +39 -0
  77. package/components/CircularButton/CircularButton.d.ts +13 -0
  78. package/components/CircularButton/CircularButton.js +18 -4
  79. package/components/ColourSwatch/ColourSwatch.css +3 -1
  80. package/components/ColourSwatch/ColourSwatch.d.ts +5 -3
  81. package/components/ColourSwatch/ColourSwatch.js +5 -0
  82. package/components/Combobox/Combobox.d.ts +4 -2
  83. package/components/Combobox/Combobox.js +7 -3
  84. package/components/DataTable/DataTable.css +52 -2
  85. package/components/DataTable/DataTable.d.ts +47 -3
  86. package/components/DataTable/DataTable.js +71 -7
  87. package/components/DataTable/columnTypes.d.ts +1 -1
  88. package/components/DataTable/columnTypes.js +1 -1
  89. package/components/DataTable/periodColumns.d.ts +37 -0
  90. package/components/DataTable/periodColumns.js +48 -0
  91. package/components/DateInput/DateInput.d.ts +4 -2
  92. package/components/DateInput/DateInput.js +7 -3
  93. package/components/DatePicker/DatePicker.css +22 -6
  94. package/components/DatePicker/DatePicker.d.ts +2 -1
  95. package/components/DatePicker/DatePicker.js +2 -2
  96. package/components/DateRangePicker/DateRangePicker.js +2 -2
  97. package/components/DecisionRow/DecisionRow.css +46 -0
  98. package/components/DecisionRow/DecisionRow.d.ts +62 -0
  99. package/components/DecisionRow/DecisionRow.js +93 -0
  100. package/components/DescriptionList/DescriptionList.css +28 -0
  101. package/components/DescriptionList/DescriptionList.d.ts +3 -1
  102. package/components/DescriptionList/DescriptionList.js +4 -3
  103. package/components/DescriptionList/DescriptionListCopy.js +2 -0
  104. package/components/Dropdown/Dropdown.d.ts +4 -2
  105. package/components/Dropdown/Dropdown.js +7 -3
  106. package/components/EmptyState/EmptyState.css +12 -0
  107. package/components/EmptyState/EmptyState.d.ts +7 -0
  108. package/components/EmptyState/EmptyState.js +2 -0
  109. package/components/EventCalendar/EventCalendar.css +113 -0
  110. package/components/EventCalendar/EventCalendar.d.ts +48 -4
  111. package/components/EventCalendar/EventCalendar.js +145 -25
  112. package/components/FormSection/FormSection.css +70 -0
  113. package/components/FormSection/FormSection.d.ts +35 -0
  114. package/components/FormSection/FormSection.js +35 -0
  115. package/components/FunnelChart/FunnelChart.js +33 -22
  116. package/components/Globe/Globe.css +7 -0
  117. package/components/Globe/Globe.js +1 -1
  118. package/components/Grid/Grid.css +52 -0
  119. package/components/Grid/Grid.d.ts +30 -0
  120. package/components/Grid/Grid.js +36 -0
  121. package/components/Icon/Icon.css +55 -0
  122. package/components/Icon/Icon.d.ts +27 -0
  123. package/components/Icon/Icon.js +42 -0
  124. package/components/NumberInput/NumberInput.d.ts +4 -2
  125. package/components/NumberInput/NumberInput.js +7 -3
  126. package/components/OrgChart/OrgChart.css +143 -0
  127. package/components/OrgChart/OrgChart.d.ts +44 -0
  128. package/components/OrgChart/OrgChart.js +26 -0
  129. package/components/PageHeader/PageHeader.css +74 -0
  130. package/components/PageHeader/PageHeader.d.ts +39 -0
  131. package/components/PageHeader/PageHeader.js +33 -0
  132. package/components/Panel/Panel.css +7 -0
  133. package/components/Panel/Panel.d.ts +5 -2
  134. package/components/Panel/Panel.js +2 -1
  135. package/components/ResourceTimeline/ResourceTimeline.css +194 -0
  136. package/components/ResourceTimeline/ResourceTimeline.d.ts +57 -0
  137. package/components/ResourceTimeline/ResourceTimeline.js +129 -0
  138. package/components/ShaderField/useShaderField.js +13 -1
  139. package/components/ShaderScene/ShaderScene.d.ts +3 -2
  140. package/components/ShaderScene/ShaderScene.js +47 -4
  141. package/components/Stack/Stack.css +68 -0
  142. package/components/Stack/Stack.d.ts +37 -0
  143. package/components/Stack/Stack.js +39 -0
  144. package/components/Stat/Stat.css +65 -0
  145. package/components/Stat/Stat.d.ts +7 -1
  146. package/components/Stat/Stat.js +16 -1
  147. package/components/Table/Table.css +18 -0
  148. package/components/Table/Table.d.ts +2 -0
  149. package/components/Table/Table.js +6 -2
  150. package/components/Tabs/Tabs.css +44 -3
  151. package/components/Tabs/Tabs.d.ts +9 -8
  152. package/components/Tabs/Tabs.js +38 -5
  153. package/components/Textarea/Textarea.d.ts +4 -2
  154. package/components/Textarea/Textarea.js +7 -3
  155. package/components/ThemeToggle/colorMode.d.ts +17 -1
  156. package/components/ThemeToggle/colorMode.js +11 -2
  157. package/components/Timeline/Timeline.css +8 -0
  158. package/components/Widget/Widget.css +23 -0
  159. package/components/Widget/Widget.d.ts +4 -0
  160. package/components/Widget/Widget.js +13 -5
  161. package/components/Wizard/Wizard.css +67 -0
  162. package/components/Wizard/Wizard.d.ts +54 -0
  163. package/components/Wizard/Wizard.js +71 -0
  164. package/components/WorldMap/WorldMap.css +15 -0
  165. package/components/WorldMap/WorldMap.js +56 -1
  166. package/components/registry.d.ts +25 -0
  167. package/components/registry.js +8 -0
  168. package/components/registry.json +251 -33
  169. package/components/registry.json.d.ts +251 -33
  170. package/components/registry.json.js +1 -1
  171. package/index.d.ts +11 -0
  172. package/index.js +26 -0
  173. package/package.json +1 -1
  174. package/tokens/motion.d.ts +2 -0
  175. package/tokens/motion.js +2 -0
  176. package/tokens/presets/blueprint.css +9 -0
  177. package/tokens/presets/bubblegum.css +9 -0
  178. package/tokens/presets/contrast.css +3 -3
  179. package/tokens/presets/coral.css +3 -3
  180. package/tokens/presets/ember.css +9 -0
  181. package/tokens/presets/getaway.css +9 -0
  182. package/tokens/presets/gold.css +3 -3
  183. package/tokens/presets/mono.css +3 -3
  184. package/tokens/presets/pink.css +3 -3
  185. package/tokens/presets/plum.css +3 -3
  186. package/tokens/presets/signal.css +9 -0
  187. package/tokens/presets/smoke.css +9 -0
  188. package/tokens/presets/velvet.css +9 -0
  189. package/tokens/presets/violet.css +3 -3
  190. package/tokens/presets/volt.css +9 -0
  191. package/tokens/presets/warm.css +3 -3
  192. package/tokens/values.d.ts +31 -0
  193. package/tokens/values.js +16 -0
  194. package/tokens/values.json.js +1 -1
@@ -145,7 +145,8 @@
145
145
  }
146
146
 
147
147
  /* The folded search: shown only where the field does not fit. */
148
- .searchIcon {
148
+ .searchIcon,
149
+ .aiIcon {
149
150
  display: none;
150
151
  }
151
152
 
@@ -164,39 +165,12 @@
164
165
  min-width: 0;
165
166
  }
166
167
 
168
+ /* PageHeader draws the breadcrumb, the title and the actions, and folds the
169
+ actions under the title when it is narrow; the shell only spaces it. */
167
170
  .pageHead {
168
- display: flex;
169
- align-items: flex-end;
170
- justify-content: space-between;
171
- flex-wrap: wrap;
172
- gap: var(--gap-400);
173
171
  padding-top: var(--padding-400);
174
172
  }
175
173
 
176
- .pageHeadText {
177
- display: flex;
178
- flex-direction: column;
179
- gap: var(--gap-100);
180
- min-width: 0;
181
- }
182
-
183
- .title {
184
- margin: 0;
185
- font-family: var(--font-heading-1-family);
186
- font-size: var(--font-heading-1-size);
187
- font-weight: var(--font-heading-1-weight);
188
- line-height: var(--font-heading-1-line-height);
189
- letter-spacing: var(--font-heading-1-letter-spacing);
190
- color: var(--color-text-primary);
191
- }
192
-
193
- .pageActions {
194
- display: flex;
195
- flex-wrap: wrap;
196
- align-items: center;
197
- gap: var(--gap-200);
198
- }
199
-
200
174
  /* -------------------------------------------------------- responsive */
201
175
 
202
176
  /* Laptop: a tighter side gutter. */
@@ -241,24 +215,17 @@
241
215
  justify-content: flex-end;
242
216
  }
243
217
 
244
- .search {
218
+ .search,
219
+ .ai {
245
220
  display: none;
246
221
  }
247
222
 
248
- .searchIcon {
223
+ .searchIcon,
224
+ .aiIcon {
249
225
  display: inline-flex;
250
226
  }
251
227
 
252
228
  .pageHead {
253
229
  padding-top: 0;
254
230
  }
255
-
256
- /* The actions take the full row under the title, sharing it evenly. */
257
- .pageActions {
258
- width: 100%;
259
- }
260
-
261
- .pageActions > * {
262
- flex: 1;
263
- }
264
231
  }
@@ -25,7 +25,8 @@ import {
25
25
  type AppSidebarProfile,
26
26
  type AppSidebarSection,
27
27
  } from 'rift-ds/components/AppSidebar/AppSidebar';
28
- import { Breadcrumb, type BreadcrumbItem } from 'rift-ds/components/Breadcrumb/Breadcrumb';
28
+ import type { BreadcrumbItem } from 'rift-ds/components/Breadcrumb/Breadcrumb';
29
+ import { PageHeader } from 'rift-ds/components/PageHeader/PageHeader';
29
30
  import { CommandPaletteTrigger } from 'rift-ds/components/CommandPaletteTrigger/CommandPaletteTrigger';
30
31
  import { ProgressiveBlur } from 'rift-ds/components/ProgressiveBlur/ProgressiveBlur';
31
32
  import { ThemeToggle } from 'rift-ds/components/ThemeToggle/ThemeToggle';
@@ -122,21 +123,18 @@ export function AppShell({ activeKey, breadcrumb, title, actions, children }: Ap
122
123
  <CommandPaletteTrigger className={styles.search} />
123
124
  <div className={styles.topBarActions}>
124
125
  <CommandPaletteTrigger iconOnly className={styles.searchIcon} />
125
- {/* The AI entry. Point it at your chat: `npx rift-ds init chat`
126
- scaffolds one, and ChatLauncher opens it. */}
127
- <AiButton />
126
+ {/* The AI entry. Point both at your chat: `npx rift-ds init chat`
127
+ scaffolds one, and ChatLauncher opens it. On a phone the
128
+ labelled button gives way to the icon, as search does, so
129
+ the bar keeps room for the actions you add. */}
130
+ <AiButton className={styles.ai} />
131
+ <AiButton iconOnly className={styles.aiIcon} />
128
132
  <ThemeToggle showLabels={false} />
129
133
  </div>
130
134
  </header>
131
135
 
132
136
  <main className={styles.page}>
133
- <div className={styles.pageHead}>
134
- <div className={styles.pageHeadText}>
135
- {breadcrumb && <Breadcrumb items={breadcrumb} />}
136
- <h1 className={styles.title}>{title}</h1>
137
- </div>
138
- {actions && <div className={styles.pageActions}>{actions}</div>}
139
- </div>
137
+ <PageHeader className={styles.pageHead} breadcrumb={breadcrumb} title={title} actions={actions} />
140
138
  {children}
141
139
  </main>
142
140
  </div>
@@ -25,6 +25,33 @@
25
25
  "app/dashboard/page.module.css"
26
26
  ]
27
27
  },
28
+ {
29
+ "name": "settings",
30
+ "label": "Settings",
31
+ "description": "The app shell with a column of settings sections, each saving on its own, and a profile form checked in one pass.",
32
+ "route": "/settings",
33
+ "files": [
34
+ "components/blocks/AppShell.tsx",
35
+ "components/blocks/AppShell.module.css",
36
+ "app/settings/page.tsx",
37
+ "app/settings/page.module.css",
38
+ "app/settings/ProfileForm.tsx"
39
+ ]
40
+ },
41
+ {
42
+ "name": "planner",
43
+ "label": "Planner",
44
+ "description": "The app shell with a plan across weeks, each cell editable with totals down and across, over a queue of requests to decide.",
45
+ "route": "/planner",
46
+ "files": [
47
+ "components/blocks/AppShell.tsx",
48
+ "components/blocks/AppShell.module.css",
49
+ "app/planner/page.tsx",
50
+ "app/planner/page.module.css",
51
+ "app/planner/PlannerGrid.tsx",
52
+ "app/planner/Requests.tsx"
53
+ ]
54
+ },
28
55
  {
29
56
  "name": "sign-in",
30
57
  "label": "Sign in",
@@ -1,9 +1,6 @@
1
1
  import { createFeedbackHandler } from 'rift-ds/chat/server';
2
2
  import { guardrails } from '../guardrails';
3
3
 
4
- export const runtime = 'nodejs';
5
- export const dynamic = 'force-dynamic';
6
-
7
4
  /** Thumbs verdicts on answers, stored against the exchange the chat route logged. */
8
5
  const handler = createFeedbackHandler({ guardrails });
9
6
 
@@ -2,9 +2,6 @@ import { createFollowupsHandler } from 'rift-ds/chat/server';
2
2
  import { FOLLOWUP_MODEL } from '../../../../components/chat/models';
3
3
  import { guardrails } from '../guardrails';
4
4
 
5
- export const runtime = 'nodejs';
6
- export const dynamic = 'force-dynamic';
7
-
8
5
  /**
9
6
  * The three follow-up questions offered under a finished answer, written by
10
7
  * a small model from the question-and-answer pair. Say what the chat is
@@ -0,0 +1,108 @@
1
+ import {
2
+ confirmationLine,
3
+ createChatHandler,
4
+ planAction,
5
+ sanitiseConfirmation,
6
+ sanitisePath,
7
+ type ActionConfirmation,
8
+ type ChatTool,
9
+ } from 'rift-ds/chat/server';
10
+ import { CHAT_MODELS, DEFAULT_CHAT_MODEL } from '../../../components/chat/models';
11
+ import { ACTIONS, SAMPLE_ITEMS, itemNoun, type ActionId } from '../../../components/chat/proposals/actions';
12
+ import { guardrails } from './guardrails';
13
+
14
+ export const maxDuration = 60;
15
+
16
+ /**
17
+ * The propose-and-confirm chat. The model can only propose: its tool plans
18
+ * an action over the records the request means and sends the plan as a
19
+ * `proposal` card, which the reader confirms on the page (ProposalCard.tsx).
20
+ * Nothing here writes. A confirmed message arrives with the page's mark in
21
+ * its context; the route rebuilds it as untrusted input, tells the model
22
+ * whether this message was confirmed on its card, and opens the reply with
23
+ * the record of what was done as a `lead` card.
24
+ */
25
+ const SYSTEM = `You are the assistant on this page. You can propose changes to the items with the propose_change tool, but you never make a change yourself: the reader confirms it on the card. Say a change is made only when you are told this message was confirmed on its card.`;
26
+
27
+ /** The context the page sends: its path, and the confirmation the page marked, if any. */
28
+ type RequestContext = { path: string | null; confirmation: ActionConfirmation | null };
29
+
30
+ const isItem = (id: string) => id in SAMPLE_ITEMS;
31
+
32
+ /* The browser's context is untrusted: keep a slug path and a confirmation
33
+ rebuilt against the real actions and records, and nothing else. */
34
+ function sanitiseContext(raw: unknown): string | null {
35
+ if (typeof raw !== 'string' || raw.length > 4000) return null;
36
+ try {
37
+ const value = JSON.parse(raw) as Record<string, unknown>;
38
+ const context: RequestContext = {
39
+ path: sanitisePath(value.path),
40
+ confirmation: sanitiseConfirmation(value.confirmation, { actions: ACTIONS, isId: isItem, maxCount: 500 }),
41
+ };
42
+ return JSON.stringify(context);
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ const readContext = (context: string | null): RequestContext =>
49
+ context ? (JSON.parse(context) as RequestContext) : { path: null, confirmation: null };
50
+
51
+ const proposeChange: ChatTool = {
52
+ definition: {
53
+ name: 'propose_change',
54
+ description: `Propose a change for the reader to confirm. Actions: ${Object.keys(ACTIONS).join(', ')}. Items: ${Object.entries(SAMPLE_ITEMS)
55
+ .map(([id, item]) => `${id} "${item.title}" (${item.state})`)
56
+ .join('; ')}.`,
57
+ input_schema: {
58
+ type: 'object',
59
+ properties: {
60
+ action: { type: 'string', enum: Object.keys(ACTIONS) },
61
+ ids: { type: 'array', items: { type: 'string' } },
62
+ },
63
+ required: ['action', 'ids'],
64
+ },
65
+ },
66
+ tracePoint: () => 'Planned a change',
67
+ execute: (input) => {
68
+ const action = ACTIONS[input.action as ActionId];
69
+ const ids = Array.isArray(input.ids) ? input.ids.filter((id): id is string => typeof id === 'string' && isItem(id)) : [];
70
+ if (!action) return JSON.stringify({ error: 'Unknown action.' });
71
+ const plan = planAction(action, ids.map((id) => ({ id, values: SAMPLE_ITEMS[id] })));
72
+ if (plan.ids.length === 0) return JSON.stringify({ error: 'None of those items can take that change.', skipped: plan.skipped });
73
+ return {
74
+ text: `Proposed on a card for the reader to confirm: ${plan.ids.length} ${itemNoun(plan.ids.length)}. Nothing has changed yet.`,
75
+ card: { kind: 'proposal', data: { ...plan, id: crypto.randomUUID() } },
76
+ };
77
+ },
78
+ };
79
+
80
+ const handler = createChatHandler({
81
+ apiKey: process.env.ANTHROPIC_API_KEY,
82
+ enabled: process.env.CHAT_ENABLED === 'true' && Boolean(process.env.ANTHROPIC_API_KEY),
83
+ system: [{ type: 'text', text: SYSTEM, cache_control: { type: 'ephemeral' } }],
84
+ models: CHAT_MODELS,
85
+ defaultModel: DEFAULT_CHAT_MODEL,
86
+ tools: [proposeChange],
87
+ sanitiseContext,
88
+ /* What the model reads about this request, after the cached block. */
89
+ contextBlock: (context) => {
90
+ const { path, confirmation } = readContext(context);
91
+ return [
92
+ path ? `The reader is on the page ${path}.` : null,
93
+ confirmationLine(confirmation, { actions: ACTIONS, noun: itemNoun }),
94
+ ]
95
+ .filter(Boolean)
96
+ .join('\n\n');
97
+ },
98
+ /* A confirmed message's reply opens with the record of what was done. */
99
+ lead: ({ context }) => {
100
+ const { confirmation } = readContext(context);
101
+ return confirmation ? [{ kind: 'record', data: { action: confirmation.action, count: confirmation.count } }] : [];
102
+ },
103
+ guardrails,
104
+ });
105
+
106
+ export async function POST(request: Request): Promise<Response> {
107
+ return handler(request);
108
+ }
@@ -2,9 +2,7 @@ import { createChatHandler } from 'rift-ds/chat/server';
2
2
  import { CHAT_MODELS, DEFAULT_CHAT_MODEL } from '../../../components/chat/models';
3
3
  import { guardrails } from './guardrails';
4
4
 
5
- export const runtime = 'nodejs';
6
5
  export const maxDuration = 60;
7
- export const dynamic = 'force-dynamic';
8
6
 
9
7
  /**
10
8
  * What the assistant knows and how it behaves. Replace this with your own
@@ -0,0 +1,55 @@
1
+ 'use client';
2
+
3
+ /**
4
+ * The chat, mounted once, with the propose-and-confirm flow wired in.
5
+ * Render <ChatMount /> at the end of your root layout's body, after the page
6
+ * content, so the panel sits last in the tab order and survives client-side
7
+ * navigation. It posts to /api/chat: set ANTHROPIC_API_KEY and
8
+ * CHAT_ENABLED=true in .env.local.
9
+ *
10
+ * Each request carries the page's path and, once, the confirmation a card's
11
+ * Confirm marked (proposals/confirmation.ts), which the route rebuilds as untrusted
12
+ * input. The cards are drawn by ACTION_RENDERERS.
13
+ */
14
+ import { useCallback } from 'react';
15
+ import { usePathname } from 'next/navigation';
16
+ import { ChatLauncher, ChatProvider } from 'rift-ds/chat';
17
+ import 'rift-ds/chat/chat.css';
18
+ import { CHAT_MODELS, DEFAULT_CHAT_MODEL } from './models';
19
+ import { confirmation } from './proposals/confirmation';
20
+ import { ACTION_RENDERERS } from './proposals/renderers';
21
+
22
+ /** What the widget says about itself. Edit freely. */
23
+ const WIDGET = {
24
+ title: 'Assistant',
25
+ tagline: 'What should change?',
26
+ /* Each starter is one chip, and a chip never wraps: a label longer than 30
27
+ characters (SUGGESTION_MAX_CHARS) is dropped, with a warning in
28
+ development. Write a month or weekday short ("Oct", "Mon"). */
29
+ starters: [
30
+ { id: 'open', label: 'What is still open?' },
31
+ { id: 'done', label: 'Mark the open items done' },
32
+ { id: 'tidy', label: 'Archive what is done' },
33
+ ],
34
+ models: CHAT_MODELS,
35
+ defaultModel: DEFAULT_CHAT_MODEL,
36
+ disclaimer: 'Answers may be wrong. Changes wait for your Confirm.',
37
+ };
38
+
39
+ export function ChatMount() {
40
+ const pathname = usePathname();
41
+ /* The note over the composer says what the chat is looking at. */
42
+ const context = { label: pathname === '/' ? 'Home' : pathname };
43
+ /* Read at each send, so the mark is taken by the one request it belongs
44
+ to. A stable function: a new one would make a new transport. */
45
+ const getContext = useCallback(
46
+ () => JSON.stringify({ path: window.location.pathname, confirmation: confirmation.take() }),
47
+ []
48
+ );
49
+
50
+ return (
51
+ <ChatProvider getContext={getContext} renderers={ACTION_RENDERERS} followups="/api/chat/followups" feedback="/api/chat/feedback">
52
+ <ChatLauncher label={`Ask ${WIDGET.title}`} widget={{ ...WIDGET, context }} />
53
+ </ChatProvider>
54
+ );
55
+ }
@@ -15,6 +15,7 @@
15
15
  * peer for its chart cards. Once you are live for good, delete the demo
16
16
  * import and the peer with it.
17
17
  */
18
+ import { usePathname } from 'next/navigation';
18
19
  import { ChatLauncher, ChatProvider } from 'rift-ds/chat';
19
20
  import { DEMO_RENDERERS, DEMO_STARTERS, createDemoTransport } from 'rift-ds/chat/demo';
20
21
  import 'rift-ds/chat/chat.css';
@@ -26,6 +27,9 @@ const DEMO = process.env.NEXT_PUBLIC_CHAT_DEMO === 'true';
26
27
  const WIDGET = {
27
28
  title: 'Assistant',
28
29
  tagline: 'How can I help you today?',
30
+ /* Each starter is one chip, and a chip never wraps: a label longer than 30
31
+ characters (SUGGESTION_MAX_CHARS) is dropped, with a warning in
32
+ development. Write a month or weekday short ("Oct", "Mon"). */
29
33
  starters: DEMO
30
34
  ? DEMO_STARTERS
31
35
  : [
@@ -39,6 +43,15 @@ const WIDGET = {
39
43
  };
40
44
 
41
45
  export function ChatMount() {
46
+ /* The note over the composer says what the chat is looking at. Here it is
47
+ the page; a host whose chat follows more (a highlighted passage, the
48
+ rows ticked in a table) names that instead, and sends the same through
49
+ ChatProvider's getContext. useSelectionFocus and useHeldHighlight, from
50
+ rift-ds/chat, follow a highlight and keep it painted while the reader
51
+ types. */
52
+ const pathname = usePathname();
53
+ const context = { label: pathname === '/' ? 'Home' : pathname };
54
+
42
55
  return (
43
56
  <ChatProvider
44
57
  transport={DEMO ? createDemoTransport() : undefined}
@@ -46,7 +59,7 @@ export function ChatMount() {
46
59
  followups={DEMO ? false : '/api/chat/followups'}
47
60
  feedback={DEMO ? false : '/api/chat/feedback'}
48
61
  >
49
- <ChatLauncher label={`Ask ${WIDGET.title}`} widget={WIDGET} />
62
+ <ChatLauncher label={`Ask ${WIDGET.title}`} widget={{ ...WIDGET, context }} />
50
63
  </ChatProvider>
51
64
  );
52
65
  }
@@ -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
+ };
@@ -28,6 +28,14 @@ export interface ChatLauncherProps {
28
28
  * Note: not the native `hidden` attribute, which keeps an element mounted and only hides it; the launcher unmounts its button and panel, and spreads no native attributes.
29
29
  */
30
30
  hidden?: boolean;
31
+ /**
32
+ * Whether the closed launcher shows its floating button. Defaults to true.
33
+ * `false` keeps the panel and drops only the button, for a host that opens
34
+ * the chat from a control of its own, such as an AiButton in its top bar:
35
+ * that control calls `setOpen(true)` from `useChatWidget()` and sets
36
+ * `returnFocusRef.current` to itself, so closing the panel hands focus back.
37
+ */
38
+ fab?: boolean;
31
39
  /** The panel's DOM id, for a host control's `aria-controls`. */
32
40
  panelId?: string;
33
41
  /**
@@ -75,7 +83,10 @@ export interface ChatLauncherProps {
75
83
  * writes the chosen dock width to `--ds-chat-width` there; a host's
76
84
  * stylesheet pads the page beside a docked panel and hides it behind a
77
85
  * takeover from those two facts (the package's own stylesheet does not
78
- * touch the host's body). The panel's geometry reads `--ds-chat-width`,
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`,
79
90
  * `--ds-chat-inset` and `--ds-chat-float-height`, defaulted on `:root`.
80
91
  */
81
- export declare function ChatLauncher({ label, icon, summary, hidden, panelId, hostAttribute, panelLabel, aiButtonLabels, widget, children, surface, }: ChatLauncherProps): import("react").JSX.Element | null;
92
+ export declare function ChatLauncher({ label, icon, summary, hidden, panelId, hostAttribute, panelLabel, aiButtonLabels, widget, children, surface, fab, }: ChatLauncherProps): import("react").JSX.Element | null;
@@ -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 = [
@@ -44,7 +45,8 @@ function ChatLauncher({
44
45
  aiButtonLabels,
45
46
  widget,
46
47
  children,
47
- surface = "raised"
48
+ surface = "raised",
49
+ fab = true
48
50
  }) {
49
51
  const { open, setOpen, panelPhase, view, side, floating, returnFocusRef, send } = useChatWidget();
50
52
  const agentState = useChatAgentState();
@@ -245,6 +247,7 @@ function ChatLauncher({
245
247
  }, [isFloating, floatPos]);
246
248
  if (hidden) return null;
247
249
  if (!renderPanel) {
250
+ if (!fab) return null;
248
251
  return /* @__PURE__ */ jsx("div", { className: "ds-chat-launcher__fab", children: /* @__PURE__ */ jsx(
249
252
  AiButton,
250
253
  {
@@ -259,7 +262,11 @@ function ChatLauncher({
259
262
  title: summary.title,
260
263
  caption: summary.caption,
261
264
  text: summary.text,
262
- suggestions: summary.suggestions ?? summary.chips?.map(({ id, label: chipLabel }) => ({ id, label: chipLabel }))
265
+ suggestions: keepFittingSuggestions(
266
+ summary.suggestions ?? summary.chips?.map(({ id, label: chipLabel }) => ({ id, label: chipLabel })) ?? [],
267
+ (chip) => chip.label,
268
+ "ChatLauncher summary chips"
269
+ )
263
270
  } : void 0,
264
271
  onSummarySuggestion: launchSuggestion,
265
272
  onClick: (event) => {
@@ -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;