cortena-ui 1.4.2 → 1.6.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 (138) hide show
  1. package/CHANGELOG.md +107 -0
  2. package/LICENSE +7 -0
  3. package/README.md +235 -3
  4. package/dist/a2ui/views.js +2 -2
  5. package/dist/agent-chat/a2ui-block.d.ts +60 -0
  6. package/dist/agent-chat/a2ui-block.js +69 -0
  7. package/dist/agent-chat/a2ui-block.js.map +1 -0
  8. package/dist/agent-chat/agui-client.d.ts +40 -0
  9. package/dist/agent-chat/agui-client.js +251 -0
  10. package/dist/agent-chat/agui-client.js.map +1 -0
  11. package/dist/agent-chat/bridge.d.ts +109 -0
  12. package/dist/agent-chat/bridge.js +353 -0
  13. package/dist/agent-chat/bridge.js.map +1 -0
  14. package/dist/agent-chat/session.d.ts +79 -0
  15. package/dist/agent-chat/session.js +391 -0
  16. package/dist/agent-chat/session.js.map +1 -0
  17. package/dist/agent-chat/step-label.d.ts +99 -0
  18. package/dist/agent-chat/step-label.js +116 -0
  19. package/dist/agent-chat/step-label.js.map +1 -0
  20. package/dist/agent-chat/store.d.ts +102 -0
  21. package/dist/agent-chat/store.js +876 -0
  22. package/dist/agent-chat/store.js.map +1 -0
  23. package/dist/agent-chat/types.d.ts +277 -0
  24. package/dist/agent-chat/types.js +17 -0
  25. package/dist/agent-chat/types.js.map +1 -0
  26. package/dist/agent-chat.d.ts +11 -0
  27. package/dist/agent-chat.js +11 -0
  28. package/dist/components/admin-permissions/admin-permissions.d.ts +66 -0
  29. package/dist/components/admin-permissions/admin-permissions.js +101 -0
  30. package/dist/components/admin-permissions/admin-permissions.js.map +1 -0
  31. package/dist/components/admin-permissions/context.d.ts +70 -0
  32. package/dist/components/admin-permissions/context.js +258 -0
  33. package/dist/components/admin-permissions/context.js.map +1 -0
  34. package/dist/components/admin-permissions/index.d.ts +10 -0
  35. package/dist/components/admin-permissions/licence.d.ts +15 -0
  36. package/dist/components/admin-permissions/licence.js +78 -0
  37. package/dist/components/admin-permissions/licence.js.map +1 -0
  38. package/dist/components/admin-permissions/matrix.d.ts +20 -0
  39. package/dist/components/admin-permissions/matrix.js +191 -0
  40. package/dist/components/admin-permissions/matrix.js.map +1 -0
  41. package/dist/components/admin-permissions/members.d.ts +18 -0
  42. package/dist/components/admin-permissions/members.js +185 -0
  43. package/dist/components/admin-permissions/members.js.map +1 -0
  44. package/dist/components/admin-permissions/role-assignment.d.ts +35 -0
  45. package/dist/components/admin-permissions/role-assignment.js +174 -0
  46. package/dist/components/admin-permissions/role-assignment.js.map +1 -0
  47. package/dist/components/admin-permissions/roles.d.ts +25 -0
  48. package/dist/components/admin-permissions/roles.js +168 -0
  49. package/dist/components/admin-permissions/roles.js.map +1 -0
  50. package/dist/components/admin-permissions/types.d.ts +152 -0
  51. package/dist/components/admin-permissions/types.js +63 -0
  52. package/dist/components/admin-permissions/types.js.map +1 -0
  53. package/dist/components/agent-chat-popup.d.ts +29 -0
  54. package/dist/components/agent-chat-popup.js +188 -0
  55. package/dist/components/agent-chat-popup.js.map +1 -0
  56. package/dist/components/agent-chat.d.ts +163 -0
  57. package/dist/components/agent-chat.js +673 -0
  58. package/dist/components/agent-chat.js.map +1 -0
  59. package/dist/components/app-shell.d.ts +126 -0
  60. package/dist/components/app-shell.js +297 -0
  61. package/dist/components/app-shell.js.map +1 -0
  62. package/dist/components/badge.d.ts +1 -1
  63. package/dist/components/button-link.js +1 -1
  64. package/dist/components/button.d.ts +1 -1
  65. package/dist/components/checkbox.d.ts +1 -1
  66. package/dist/components/combobox.d.ts +1 -1
  67. package/dist/components/combobox.js +1 -1
  68. package/dist/components/consent-screen.d.ts +65 -0
  69. package/dist/components/consent-screen.js +123 -0
  70. package/dist/components/consent-screen.js.map +1 -0
  71. package/dist/components/data-table/data-table.d.ts +15 -1
  72. package/dist/components/data-table/data-table.js +18 -4
  73. package/dist/components/data-table/data-table.js.map +1 -1
  74. package/dist/components/data-table/index.d.ts +4 -4
  75. package/dist/components/data-table/parts.d.ts +27 -3
  76. package/dist/components/data-table/parts.js +175 -55
  77. package/dist/components/data-table/parts.js.map +1 -1
  78. package/dist/components/data-table/types.d.ts +61 -0
  79. package/dist/components/data-table/use-data-table.js +91 -6
  80. package/dist/components/data-table/use-data-table.js.map +1 -1
  81. package/dist/components/data-table/use-server-source.js +119 -28
  82. package/dist/components/data-table/use-server-source.js.map +1 -1
  83. package/dist/components/help-panel.d.ts +131 -0
  84. package/dist/components/help-panel.js +545 -0
  85. package/dist/components/help-panel.js.map +1 -0
  86. package/dist/components/login-screen.d.ts +127 -0
  87. package/dist/components/login-screen.js +339 -0
  88. package/dist/components/login-screen.js.map +1 -0
  89. package/dist/components/session-guard.d.ts +268 -0
  90. package/dist/components/session-guard.js +632 -0
  91. package/dist/components/session-guard.js.map +1 -0
  92. package/dist/components/toast.d.ts +1 -1
  93. package/dist/core.d.ts +5 -1
  94. package/dist/core.js +11 -7
  95. package/dist/data-table.d.ts +13 -4
  96. package/dist/data-table.js +10 -2
  97. package/dist/hooks/use-cortena-theme.js +49 -3
  98. package/dist/hooks/use-cortena-theme.js.map +1 -1
  99. package/dist/index.d.ts +17 -4
  100. package/dist/index.js +21 -8
  101. package/dist/markdown.d.ts +2 -1
  102. package/dist/markdown.js +2 -1
  103. package/package.json +18 -5
  104. package/src/agent-chat/a2ui-block.ts +118 -0
  105. package/src/agent-chat/agui-client.ts +405 -0
  106. package/src/agent-chat/bridge.ts +445 -0
  107. package/src/agent-chat/session.ts +549 -0
  108. package/src/agent-chat/step-label.ts +177 -0
  109. package/src/agent-chat/store.ts +1234 -0
  110. package/src/agent-chat/types.ts +308 -0
  111. package/src/components/admin-permissions/admin-permissions.tsx +130 -0
  112. package/src/components/admin-permissions/context.tsx +376 -0
  113. package/src/components/admin-permissions/index.tsx +32 -0
  114. package/src/components/admin-permissions/licence.tsx +84 -0
  115. package/src/components/admin-permissions/matrix.tsx +257 -0
  116. package/src/components/admin-permissions/members.tsx +204 -0
  117. package/src/components/admin-permissions/role-assignment.tsx +239 -0
  118. package/src/components/admin-permissions/roles.tsx +169 -0
  119. package/src/components/admin-permissions/types.ts +231 -0
  120. package/src/components/agent-chat-popup.tsx +289 -0
  121. package/src/components/agent-chat.tsx +1006 -0
  122. package/src/components/app-shell.tsx +502 -0
  123. package/src/components/consent-screen.tsx +239 -0
  124. package/src/components/data-table/data-table.tsx +36 -0
  125. package/src/components/data-table/index.tsx +6 -1
  126. package/src/components/data-table/parts.tsx +223 -47
  127. package/src/components/data-table/types.ts +68 -0
  128. package/src/components/data-table/use-data-table.ts +152 -4
  129. package/src/components/data-table/use-server-source.ts +150 -12
  130. package/src/components/help-panel.tsx +765 -0
  131. package/src/components/login-screen.tsx +479 -0
  132. package/src/components/session-guard.tsx +1071 -0
  133. package/src/entries/agent-chat.ts +137 -0
  134. package/src/entries/core.ts +8 -0
  135. package/src/entries/data-table.ts +41 -0
  136. package/src/entries/markdown.ts +25 -0
  137. package/src/hooks/use-cortena-theme.ts +63 -4
  138. package/src/index.ts +6 -0
@@ -0,0 +1,673 @@
1
+ "use client";
2
+ "use client";
3
+ import { cn } from "../lib/cn.js";
4
+ import { Button } from "./button.js";
5
+ import { Spinner } from "./spinner.js";
6
+ import { Badge } from "./badge.js";
7
+ import { ErrorBanner } from "./error-banner.js";
8
+ import { Textarea } from "./textarea.js";
9
+ import { Markdown } from "./markdown.js";
10
+ import { A2UIErrorCard } from "../a2ui/views.js";
11
+ import { A2UIRenderer } from "../a2ui/renderer.js";
12
+ import { isGenericLabel, truncate } from "../agent-chat/step-label.js";
13
+ import { isHiddenMessage } from "../agent-chat/session.js";
14
+ import { useAgentChat } from "../agent-chat/store.js";
15
+ import { sessionTitle } from "../agent-chat/types.js";
16
+ import { Check, ChevronDown, ChevronRight, MessageSquarePlus, Send, ShieldCheck, ShieldX, Sparkles, Square, X } from "lucide-react";
17
+ import { jsx, jsxs } from "react/jsx-runtime";
18
+ import * as React from "react";
19
+ //#region src/components/agent-chat.tsx
20
+ /**
21
+ * AgentChat — the chat surface an extension's own agent runs in.
22
+ *
23
+ * §17 of how-to-create-a-cortena-extension, audit rule P-25. Moved here from
24
+ * `cortenaweb/components/chat/**` so there is one chat and not two: a cut-down
25
+ * widget diverges on approvals, A2UI and MCP Apps within one release, and every
26
+ * fix then has to be made twice. cortenaweb adopts this component as a
27
+ * follow-up; until then the two are kept in step by hand.
28
+ *
29
+ * Everything comes from the `AgentChatClient` prop — nothing here knows about
30
+ * HTTP, AG-UI or a store — which is what lets `test/agent-chat.test.tsx` drive
31
+ * the whole surface from a fake that replays scripted event sequences.
32
+ *
33
+ * Not in this package, on purpose: the MCP App frame. Rendering an extension's
34
+ * own screen means a sandboxed iframe, a `srcdoc` CSP, a JSON-RPC postMessage
35
+ * bridge and a `tools/invoke` proxy holding a credential — a host concern, not
36
+ * a component one. `renderToolCall` is the seam: return a node for a tool call
37
+ * and it replaces the card, return nothing and the default card is drawn.
38
+ */
39
+ /**
40
+ * Should the list jump back to the bottom after its content grew?
41
+ *
42
+ * Judged against the height BEFORE the growth. Measuring afterwards asks "is
43
+ * the newest content already visible", which is false by construction for any
44
+ * append taller than the threshold — a paragraph, a code block, an image — so
45
+ * the re-pin skipped exactly when it was needed.
46
+ */
47
+ function shouldStickToBottom(params) {
48
+ const { prevScrollHeight, scrollTop, clientHeight, threshold = 150 } = params;
49
+ if (prevScrollHeight <= 0) return true;
50
+ return prevScrollHeight - scrollTop - clientHeight < threshold;
51
+ }
52
+ function AgentThinking({ text, defaultOpen, streaming }) {
53
+ const [open, setOpen] = React.useState(defaultOpen ?? false);
54
+ if (!text) return null;
55
+ return /* @__PURE__ */ jsxs("div", {
56
+ "data-slot": "agent-chat-thinking",
57
+ className: "mt-1",
58
+ children: [/* @__PURE__ */ jsxs("button", {
59
+ type: "button",
60
+ "aria-expanded": open,
61
+ onClick: () => setOpen((v) => !v),
62
+ className: cn("inline-flex items-center gap-1 rounded-[var(--ds-radius-sm)] px-1 py-0.5", "text-[length:var(--ds-text-caption)] text-[color:var(--ds-muted-foreground)]", "transition-colors duration-[var(--ds-duration-fast)] hover:text-[color:var(--ds-foreground)]"),
63
+ children: [open ? /* @__PURE__ */ jsx(ChevronDown, {
64
+ className: "size-3",
65
+ "aria-hidden": true
66
+ }) : /* @__PURE__ */ jsx(ChevronRight, {
67
+ className: "size-3",
68
+ "aria-hidden": true
69
+ }), streaming ? "Thinking…" : "Chain of thought"]
70
+ }), open ? /* @__PURE__ */ jsx("div", {
71
+ "data-slot": "agent-chat-thinking-body",
72
+ className: cn("mt-1 max-h-64 overflow-auto rounded-[var(--ds-radius-sm)] border border-[var(--ds-border-subtle)]", "bg-[var(--ds-muted)] px-3 py-2 whitespace-pre-wrap", "text-[length:var(--ds-text-caption)] text-[color:var(--ds-muted-foreground)]"),
73
+ children: text
74
+ }) : null]
75
+ });
76
+ }
77
+ /**
78
+ * How long a step runs before it starts saying how long it has been running.
79
+ *
80
+ * Under this, a timer is noise: every call takes a second or two and a number
81
+ * ticking beside each one is a fidget spinner. Over it, the user has begun to
82
+ * wonder whether anything is happening at all, and the number is the answer.
83
+ */
84
+ const ELAPSED_AFTER_MS = 8e3;
85
+ /**
86
+ * `Date.now()`, re-read once a second while `active`.
87
+ *
88
+ * The interval only exists while something is running, so a finished chat sits
89
+ * idle rather than re-rendering the whole transcript once a second for ever.
90
+ */
91
+ function useTickingNow(active) {
92
+ const [now, setNow] = React.useState(() => Date.now());
93
+ React.useEffect(() => {
94
+ if (!active) return;
95
+ setNow(Date.now());
96
+ const id = setInterval(() => setNow(Date.now()), 1e3);
97
+ return () => clearInterval(id);
98
+ }, [active]);
99
+ return now;
100
+ }
101
+ /**
102
+ * What the agent is doing, one plain sentence per tool call (SKILLS-9).
103
+ *
104
+ * This is the surface a person watches while they wait, and it is deliberately
105
+ * not the tool strip: the strip is the record — names, arguments, output, the
106
+ * MCP App seam — and reading it requires knowing what `broker_invoke` is. The
107
+ * strip stays at the foot for anyone who wants the detail.
108
+ *
109
+ * NOT a live region of its own. The strip is drawn inside the transcript, and
110
+ * the transcript is already `role="log" aria-live="polite"` — two nested live
111
+ * regions is one announcement per change from the inner one and another from
112
+ * the outer, which is how a screen reader ends up saying every step twice.
113
+ * `aria-label` stays: it names the group for anyone who navigates to it.
114
+ */
115
+ function AgentSteps({ steps }) {
116
+ const now = useTickingNow(steps.some((step) => step.status === "running"));
117
+ if (steps.length === 0) return null;
118
+ return /* @__PURE__ */ jsx("div", {
119
+ "data-slot": "agent-chat-steps",
120
+ "aria-label": "Progress",
121
+ className: "mt-1 flex flex-col gap-0.5",
122
+ children: steps.map((step) => {
123
+ const elapsed = step.status === "running" ? now - step.startedAt : 0;
124
+ const seconds = elapsed >= ELAPSED_AFTER_MS ? Math.floor(elapsed / 1e3) : 0;
125
+ const held = step.status === "running" && isGenericLabel(step.toolName, step.label);
126
+ return /* @__PURE__ */ jsxs("p", {
127
+ "data-slot": "agent-chat-step",
128
+ "data-status": step.status,
129
+ "data-tool": truncate(step.toolName, 80),
130
+ ...held ? { "aria-hidden": true } : {},
131
+ className: cn("flex items-start gap-2 text-[length:var(--ds-text-caption-lg)]", step.status === "error" ? "text-[color:var(--ds-destructive)]" : "text-[color:var(--ds-muted-foreground)]"),
132
+ children: [/* @__PURE__ */ jsx("span", {
133
+ "aria-hidden": true,
134
+ className: "mt-0.5 inline-flex size-3.5 shrink-0 items-center justify-center",
135
+ children: step.status === "running" ? /* @__PURE__ */ jsx(Spinner, {
136
+ size: 12,
137
+ "aria-hidden": true
138
+ }) : step.status === "error" ? /* @__PURE__ */ jsx(X, { className: "size-3" }) : step.status === "stopped" ? /* @__PURE__ */ jsx(Square, { className: "size-2.5" }) : step.status === "paused" ? /* @__PURE__ */ jsx("span", { className: "size-1.5 rounded-full bg-[color:var(--ds-muted-foreground)]" }) : /* @__PURE__ */ jsx(Check, { className: "size-3 text-[color:var(--ds-success)]" })
139
+ }), /* @__PURE__ */ jsxs("span", {
140
+ className: "min-w-0",
141
+ children: [
142
+ /* @__PURE__ */ jsx("span", {
143
+ "data-slot": "agent-chat-step-label",
144
+ children: step.label
145
+ }, step.label),
146
+ seconds > 0 ? /* @__PURE__ */ jsx("span", {
147
+ "aria-hidden": true,
148
+ "data-slot": "agent-chat-step-elapsed",
149
+ children: ` · ${seconds}s`
150
+ }) : null,
151
+ step.status === "stopped" ? /* @__PURE__ */ jsx("span", {
152
+ "data-slot": "agent-chat-step-note",
153
+ children: " — Stopped"
154
+ }) : null,
155
+ step.status === "paused" ? /* @__PURE__ */ jsx("span", {
156
+ "data-slot": "agent-chat-step-note",
157
+ children: " — Waiting for your approval"
158
+ }) : null,
159
+ step.status === "error" && step.errorMessage ? /* @__PURE__ */ jsx("span", {
160
+ "data-slot": "agent-chat-step-error",
161
+ children: ` — ${step.errorMessage}`
162
+ }) : null
163
+ ]
164
+ })]
165
+ }, step.id);
166
+ })
167
+ });
168
+ }
169
+ /**
170
+ * Every block of one message, in arrival order.
171
+ *
172
+ * A block carrying a transport `error` — an unclosed fence, a body that did not
173
+ * parse — gets an error card rather than being dropped. A block that is merely
174
+ * malformed inside is the renderer's error to draw, and it draws the parts that
175
+ * were fine.
176
+ */
177
+ function AgentA2UIBlocks({ blocks, streaming, onAction }) {
178
+ if (!blocks || blocks.length === 0) return null;
179
+ return /* @__PURE__ */ jsx("div", {
180
+ className: "mt-2 flex flex-col gap-3",
181
+ "data-slot": "agent-chat-a2ui",
182
+ children: blocks.map((block, index) => /* @__PURE__ */ jsxs("div", { children: [block.error ? /* @__PURE__ */ jsx(A2UIErrorCard, {
183
+ title: "The agent's UI did not arrive complete",
184
+ detail: block.error
185
+ }) : null, block.messages.length > 0 ? /* @__PURE__ */ jsx(A2UIRenderer, {
186
+ message: block.messages,
187
+ ...streaming || !onAction ? {} : { onAction }
188
+ }) : null] }, `${block.surfaceId}:${index}`))
189
+ });
190
+ }
191
+ function AssistantAvatar() {
192
+ return /* @__PURE__ */ jsx("span", {
193
+ "data-slot": "agent-chat-avatar",
194
+ "aria-hidden": true,
195
+ className: cn("grid size-7 shrink-0 place-items-center rounded-[var(--ds-radius-sm)]", "border border-[var(--ds-ring)] bg-[var(--ds-primary-soft)] text-[color:var(--ds-primary)]"),
196
+ children: /* @__PURE__ */ jsx(Sparkles, {
197
+ className: "size-3.5",
198
+ "aria-hidden": true
199
+ })
200
+ });
201
+ }
202
+ function AgentMessageBubble({ message, agentName, streaming, onAction }) {
203
+ if (isHiddenMessage(message)) return null;
204
+ if (message.isError) return /* @__PURE__ */ jsx("div", {
205
+ "data-slot": "agent-chat-message",
206
+ "data-role": "error",
207
+ children: /* @__PURE__ */ jsx(ErrorBanner, { message: message.text })
208
+ });
209
+ if (message.role === "user") return /* @__PURE__ */ jsx("div", {
210
+ "data-slot": "agent-chat-message",
211
+ "data-role": "user",
212
+ className: "flex w-full justify-end",
213
+ children: /* @__PURE__ */ jsx("div", {
214
+ className: cn("max-w-[85%] rounded-[var(--ds-radius-lg)] border border-[var(--ds-border-subtle)]", "bg-[var(--ds-popover)] px-3.5 py-2 text-[color:var(--ds-foreground)]"),
215
+ children: /* @__PURE__ */ jsx(Markdown, {
216
+ compact: true,
217
+ children: message.text
218
+ })
219
+ })
220
+ });
221
+ return /* @__PURE__ */ jsxs("div", {
222
+ "data-slot": "agent-chat-message",
223
+ "data-role": "assistant",
224
+ className: "flex w-full gap-3",
225
+ children: [/* @__PURE__ */ jsx(AssistantAvatar, {}), /* @__PURE__ */ jsxs("div", {
226
+ className: "min-w-0 flex-1",
227
+ children: [
228
+ /* @__PURE__ */ jsx("span", {
229
+ className: cn("text-[length:var(--ds-text-caption)] font-semibold", "text-[color:var(--ds-foreground)]"),
230
+ children: agentName
231
+ }),
232
+ /* @__PURE__ */ jsx(AgentThinking, { text: message.thinkingText ?? "" }),
233
+ message.text ? /* @__PURE__ */ jsx("div", {
234
+ className: "mt-1",
235
+ children: /* @__PURE__ */ jsx(Markdown, {
236
+ compact: true,
237
+ children: message.text
238
+ })
239
+ }) : null,
240
+ /* @__PURE__ */ jsx(AgentA2UIBlocks, {
241
+ blocks: message.a2ui,
242
+ ...streaming ? { streaming } : {},
243
+ ...onAction ? { onAction } : {}
244
+ })
245
+ ]
246
+ })]
247
+ });
248
+ }
249
+ /**
250
+ * The assistant bubble while a turn is live.
251
+ *
252
+ * The filter chain is deliberately not applied: streaming text is
253
+ * agent-authored prose in progress, and the filters are about finished
254
+ * messages. Markdown is not processed either — partially arrived source
255
+ * (`**bo`) looks worse than its raw form — so the streaming view is plain text
256
+ * under `whitespace-pre-wrap` and `Markdown` takes over on `final`.
257
+ */
258
+ function AgentStreamingBubble({ agentName, text, thinkingText, a2ui, steps }) {
259
+ return /* @__PURE__ */ jsxs("div", {
260
+ "data-slot": "agent-chat-streaming",
261
+ className: "flex w-full gap-3",
262
+ children: [/* @__PURE__ */ jsx(AssistantAvatar, {}), /* @__PURE__ */ jsxs("div", {
263
+ className: "min-w-0 flex-1",
264
+ children: [
265
+ /* @__PURE__ */ jsx("span", {
266
+ className: cn("text-[length:var(--ds-text-caption)] font-semibold", "text-[color:var(--ds-foreground)]"),
267
+ children: agentName
268
+ }),
269
+ /* @__PURE__ */ jsx(AgentSteps, { steps: steps ?? [] }),
270
+ thinkingText ? /* @__PURE__ */ jsx(AgentThinking, {
271
+ text: thinkingText,
272
+ defaultOpen: true,
273
+ streaming: true
274
+ }) : null,
275
+ text ? /* @__PURE__ */ jsx("div", {
276
+ "data-slot": "agent-chat-streaming-text",
277
+ className: "mt-1 break-words whitespace-pre-wrap text-[color:var(--ds-foreground)]",
278
+ children: text
279
+ }) : null,
280
+ /* @__PURE__ */ jsx(AgentA2UIBlocks, {
281
+ blocks: a2ui,
282
+ streaming: true
283
+ })
284
+ ]
285
+ })]
286
+ });
287
+ }
288
+ /** A `ui://` resource in a tool result is an MCP App: the extension's own screen. */
289
+ function readMcpAppUri(output) {
290
+ return (typeof output === "string" ? output : output === void 0 ? "" : JSON.stringify(output)).match(/"uri"\s*:\s*"(ui:\/\/[^"]+)"/)?.[1] ?? null;
291
+ }
292
+ function summarise(input) {
293
+ if (input === void 0 || input === null) return "";
294
+ if (typeof input === "string") return input;
295
+ const record = input;
296
+ for (const key of [
297
+ "command",
298
+ "cmd",
299
+ "query",
300
+ "path",
301
+ "url",
302
+ "name"
303
+ ]) {
304
+ const value = record[key];
305
+ if (typeof value === "string" && value.trim()) return value;
306
+ }
307
+ const json = JSON.stringify(input);
308
+ return json.length > 120 ? `${json.slice(0, 117)}…` : json;
309
+ }
310
+ /**
311
+ * The default card. It shows the tool's name, its input and its output — the
312
+ * input being the part that was missing for the whole of the AG-UI path,
313
+ * because the bridge had no `TOOL_CALL_ARGS` case.
314
+ */
315
+ function AgentToolCard({ entry }) {
316
+ const [open, setOpen] = React.useState(false);
317
+ const summary = summarise(entry.input);
318
+ return /* @__PURE__ */ jsxs("div", {
319
+ "data-slot": "agent-tool-card",
320
+ "data-tool": entry.name,
321
+ "data-status": entry.status,
322
+ className: cn("overflow-hidden rounded-[var(--ds-radius-sm)] border border-[var(--ds-border-subtle)]", "bg-[var(--ds-muted)]"),
323
+ children: [/* @__PURE__ */ jsxs("button", {
324
+ type: "button",
325
+ "aria-expanded": open,
326
+ onClick: () => setOpen((v) => !v),
327
+ className: cn("flex w-full items-center gap-2.5 px-3 py-2 text-left", "transition-colors duration-[var(--ds-duration-fast)] hover:bg-[var(--ds-hover)]"),
328
+ children: [
329
+ /* @__PURE__ */ jsx("span", {
330
+ className: "inline-flex size-3.5 shrink-0 items-center justify-center",
331
+ children: entry.status === "running" ? /* @__PURE__ */ jsx(Spinner, { size: 14 }) : /* @__PURE__ */ jsx("span", {
332
+ "aria-hidden": true,
333
+ className: cn("size-2 rounded-[var(--ds-radius-full)]", entry.status === "error" ? "bg-[var(--ds-destructive)]" : "bg-[var(--ds-success)]")
334
+ })
335
+ }),
336
+ /* @__PURE__ */ jsx("span", {
337
+ className: "font-mono text-[length:var(--ds-text-caption)] text-[color:var(--ds-primary)]",
338
+ children: entry.name
339
+ }),
340
+ summary ? /* @__PURE__ */ jsx("span", {
341
+ "data-slot": "agent-tool-card-input",
342
+ className: "min-w-0 truncate text-[length:var(--ds-text-caption)] text-[color:var(--ds-muted-foreground)]",
343
+ children: summary
344
+ }) : null,
345
+ /* @__PURE__ */ jsx("span", {
346
+ className: "ml-auto shrink-0 text-[color:var(--ds-text-tertiary)]",
347
+ children: open ? /* @__PURE__ */ jsx(ChevronDown, { className: "size-3" }) : /* @__PURE__ */ jsx(ChevronRight, { className: "size-3" })
348
+ })
349
+ ]
350
+ }), open ? /* @__PURE__ */ jsxs("div", {
351
+ className: "border-t border-[var(--ds-border-subtle)] bg-[var(--ds-background)] px-3 py-2",
352
+ children: [entry.input !== void 0 ? /* @__PURE__ */ jsx("pre", {
353
+ "data-slot": "agent-tool-card-args",
354
+ className: "max-h-40 overflow-auto font-mono text-[length:var(--ds-text-caption-sm)] whitespace-pre-wrap text-[color:var(--ds-muted-foreground)]",
355
+ children: typeof entry.input === "string" ? entry.input : JSON.stringify(entry.input, null, 2)
356
+ }) : null, entry.output !== void 0 && entry.output !== null ? /* @__PURE__ */ jsx("pre", {
357
+ className: "mt-2 max-h-40 overflow-auto font-mono text-[length:var(--ds-text-caption-sm)] whitespace-pre-wrap text-[color:var(--ds-muted-foreground)]",
358
+ children: typeof entry.output === "string" ? entry.output : JSON.stringify(entry.output, null, 2)
359
+ }) : null]
360
+ }) : null]
361
+ });
362
+ }
363
+ /**
364
+ * `exec` as a render-and-wait card: the run is paused on this decision.
365
+ *
366
+ * The three answers are the three cortenacore accepts. "Always allow" is not a
367
+ * nicety — without it the same command is asked about on every turn, and a user
368
+ * who has to answer the same question repeatedly stops reading the question.
369
+ */
370
+ function AgentApprovalCard({ request, onResolve }) {
371
+ const command = typeof request.args.command === "string" ? request.args.command : request.toolName;
372
+ return /* @__PURE__ */ jsxs("div", {
373
+ "data-slot": "agent-approval-card",
374
+ role: "group",
375
+ "aria-label": "Command approval required",
376
+ className: cn("flex flex-col gap-3 rounded-[var(--ds-radius-md)] border border-[var(--ds-border)]", "bg-[var(--ds-card)] p-3"),
377
+ children: [
378
+ /* @__PURE__ */ jsxs("div", {
379
+ className: "flex items-center gap-2",
380
+ children: [/* @__PURE__ */ jsx(ShieldCheck, {
381
+ className: "size-4 text-[color:var(--ds-primary)]",
382
+ "aria-hidden": true
383
+ }), /* @__PURE__ */ jsx("span", {
384
+ className: "text-[length:var(--ds-text-body-sm)] font-medium text-[color:var(--ds-foreground)]",
385
+ children: "Approval required"
386
+ })]
387
+ }),
388
+ /* @__PURE__ */ jsx("pre", {
389
+ className: "max-h-32 overflow-auto rounded-[var(--ds-radius-sm)] bg-[var(--ds-muted)] p-2 font-mono text-[length:var(--ds-text-caption)] whitespace-pre-wrap text-[color:var(--ds-foreground)]",
390
+ children: command
391
+ }),
392
+ request.error ? /* @__PURE__ */ jsx("p", {
393
+ "data-slot": "agent-approval-error",
394
+ role: "alert",
395
+ className: "text-[length:var(--ds-text-caption)] text-[color:var(--ds-destructive)]",
396
+ children: request.error
397
+ }) : null,
398
+ /* @__PURE__ */ jsxs("div", {
399
+ className: "flex flex-wrap items-center gap-2",
400
+ children: [
401
+ /* @__PURE__ */ jsxs(Button, {
402
+ size: "sm",
403
+ onClick: () => onResolve(request.id, "allow-once"),
404
+ children: [/* @__PURE__ */ jsx(ShieldCheck, { "aria-hidden": true }), "Approve"]
405
+ }),
406
+ /* @__PURE__ */ jsx(Button, {
407
+ size: "sm",
408
+ variant: "outline",
409
+ onClick: () => onResolve(request.id, "allow-always"),
410
+ children: "Always allow"
411
+ }),
412
+ /* @__PURE__ */ jsxs(Button, {
413
+ size: "sm",
414
+ variant: "destructive",
415
+ onClick: () => onResolve(request.id, "deny"),
416
+ children: [/* @__PURE__ */ jsx(ShieldX, { "aria-hidden": true }), "Deny"]
417
+ })
418
+ ]
419
+ })
420
+ ]
421
+ });
422
+ }
423
+ function AgentChat({ client, agentName, storageScope, ephemeral, thinking, attachmentsSlot, renderToolCall, headerSlot, className, ...props }) {
424
+ const chat = useAgentChat({
425
+ client,
426
+ storageScope,
427
+ ...ephemeral === void 0 ? {} : { ephemeral },
428
+ ...thinking === void 0 ? {} : { thinking }
429
+ });
430
+ const [drawerOpen, setDrawerOpen] = React.useState(false);
431
+ const [value, setValue] = React.useState("");
432
+ const scrollRef = React.useRef(null);
433
+ const contentRef = React.useRef(null);
434
+ const prevHeightRef = React.useRef(0);
435
+ /**
436
+ * Re-pin to the bottom whenever the content grows or the viewport shrinks.
437
+ * A message keeps growing after it mounts — markdown settles, the tool strip
438
+ * appears between the list and the composer — and every one of those leaves
439
+ * the newest content under the composer until the reader scrolls by hand.
440
+ *
441
+ * Jumping rather than smooth-scrolling: this fires many times a second during
442
+ * streaming, and queued smooth animations fight each other and lose.
443
+ */
444
+ React.useEffect(() => {
445
+ const scrollEl = scrollRef.current;
446
+ const contentEl = contentRef.current;
447
+ if (!scrollEl || !contentEl || typeof ResizeObserver === "undefined") return;
448
+ const observer = new ResizeObserver(() => {
449
+ const { scrollTop, scrollHeight, clientHeight } = scrollEl;
450
+ const stick = shouldStickToBottom({
451
+ prevScrollHeight: prevHeightRef.current,
452
+ scrollTop,
453
+ clientHeight
454
+ });
455
+ prevHeightRef.current = scrollHeight;
456
+ if (stick) scrollEl.scrollTop = scrollEl.scrollHeight;
457
+ });
458
+ observer.observe(contentEl);
459
+ observer.observe(scrollEl);
460
+ return () => observer.disconnect();
461
+ }, []);
462
+ const submit = React.useCallback(() => {
463
+ const trimmed = value.trim();
464
+ if (!trimmed || chat.streaming) return;
465
+ chat.send(trimmed);
466
+ setValue("");
467
+ }, [chat, value]);
468
+ const onKeyDown = React.useCallback((event) => {
469
+ if (event.key === "Escape" && chat.streaming) {
470
+ event.preventDefault();
471
+ event.stopPropagation();
472
+ chat.abort();
473
+ return;
474
+ }
475
+ if (event.key === "Enter" && !event.shiftKey) {
476
+ event.preventDefault();
477
+ submit();
478
+ }
479
+ }, [chat, submit]);
480
+ const toolEntries = chat.toolOrder.map((id) => chat.tools[id]).filter((entry) => entry !== void 0);
481
+ const showTools = toolEntries.length > 0 || chat.approvals.length > 0;
482
+ const visible = chat.messages.filter((message) => !isHiddenMessage(message));
483
+ return /* @__PURE__ */ jsxs("div", {
484
+ "data-slot": "agent-chat",
485
+ className: cn("flex h-full min-h-0 w-full flex-col bg-[var(--ds-background)]", className),
486
+ ...props,
487
+ children: [
488
+ /* @__PURE__ */ jsxs("div", {
489
+ "data-slot": "agent-chat-header",
490
+ className: cn("flex shrink-0 items-center gap-2 border-b border-[var(--ds-border-subtle)] px-3 py-2"),
491
+ children: [
492
+ /* @__PURE__ */ jsx(Button, {
493
+ size: "sm",
494
+ variant: "ghost",
495
+ "data-slot": "agent-chat-sessions-trigger",
496
+ "aria-expanded": drawerOpen,
497
+ onClick: () => {
498
+ setDrawerOpen((open) => !open);
499
+ chat.refreshSessions();
500
+ },
501
+ children: chat.sessions.length === 1 ? "1 session" : `${chat.sessions.length} sessions`
502
+ }),
503
+ /* @__PURE__ */ jsxs(Button, {
504
+ size: "sm",
505
+ variant: "ghost",
506
+ "data-slot": "agent-chat-new-session",
507
+ "aria-label": "New session",
508
+ onClick: () => {
509
+ chat.newSession();
510
+ setDrawerOpen(false);
511
+ },
512
+ children: [/* @__PURE__ */ jsx(MessageSquarePlus, { "aria-hidden": true }), "New"]
513
+ }),
514
+ /* @__PURE__ */ jsx("div", {
515
+ className: "ml-auto flex items-center gap-1",
516
+ children: headerSlot
517
+ })
518
+ ]
519
+ }),
520
+ drawerOpen ? /* @__PURE__ */ jsx(AgentSessionDrawer, {
521
+ sessions: chat.sessions,
522
+ current: chat.sessionKey,
523
+ loading: chat.loadingSessions,
524
+ onResume: (key) => {
525
+ chat.resumeSession(key);
526
+ setDrawerOpen(false);
527
+ }
528
+ }) : null,
529
+ /* @__PURE__ */ jsx("div", {
530
+ ref: scrollRef,
531
+ "data-slot": "agent-chat-scroll",
532
+ className: "min-h-0 flex-1 overflow-x-hidden overflow-y-auto",
533
+ children: /* @__PURE__ */ jsxs("div", {
534
+ ref: contentRef,
535
+ role: "log",
536
+ "aria-live": "polite",
537
+ "aria-label": `${agentName} chat`,
538
+ className: "flex min-w-0 flex-col gap-5 px-4 py-4",
539
+ children: [
540
+ chat.error ? /* @__PURE__ */ jsx(ErrorBanner, { message: chat.error }) : null,
541
+ chat.hasMoreHistory ? /* @__PURE__ */ jsx("div", {
542
+ className: "flex justify-center",
543
+ children: /* @__PURE__ */ jsx(Button, {
544
+ size: "sm",
545
+ variant: "ghost",
546
+ "data-slot": "agent-chat-load-older",
547
+ disabled: chat.loadingHistory,
548
+ onClick: chat.loadOlder,
549
+ children: chat.loadingHistory ? "Loading…" : "Load older messages"
550
+ })
551
+ }) : null,
552
+ visible.length === 0 && !chat.streaming ? /* @__PURE__ */ jsxs("p", {
553
+ "data-slot": "agent-chat-empty",
554
+ className: "text-[length:var(--ds-text-caption-lg)] text-[color:var(--ds-muted-foreground)]",
555
+ children: [
556
+ "Ask ",
557
+ agentName,
558
+ " anything about this screen."
559
+ ]
560
+ }) : null,
561
+ visible.map((message) => /* @__PURE__ */ jsx(AgentMessageBubble, {
562
+ message,
563
+ agentName,
564
+ streaming: chat.streaming,
565
+ onAction: chat.sendAction
566
+ }, message.id)),
567
+ chat.streaming ? /* @__PURE__ */ jsx(AgentStreamingBubble, {
568
+ agentName,
569
+ text: chat.streamText,
570
+ thinkingText: chat.streamThinkingText,
571
+ a2ui: chat.streamA2ui,
572
+ steps: chat.steps
573
+ }) : chat.steps.length > 0 ? /* @__PURE__ */ jsx(AgentSteps, { steps: chat.steps }) : null
574
+ ]
575
+ })
576
+ }),
577
+ showTools ? /* @__PURE__ */ jsxs("div", {
578
+ "data-slot": "agent-chat-tools",
579
+ className: "flex shrink-0 flex-col gap-1.5 border-t border-[var(--ds-border-subtle)] px-3 py-2",
580
+ children: [
581
+ toolEntries.length > 0 ? /* @__PURE__ */ jsx(Badge, {
582
+ variant: "neutral",
583
+ className: "w-fit",
584
+ children: toolEntries.length === 1 ? "1 step" : `${toolEntries.length} steps`
585
+ }) : null,
586
+ toolEntries.map((entry) => {
587
+ const custom = renderToolCall?.({
588
+ entry,
589
+ mcpAppUri: readMcpAppUri(entry.output)
590
+ });
591
+ return /* @__PURE__ */ jsx(React.Fragment, { children: custom ?? /* @__PURE__ */ jsx(AgentToolCard, { entry }) }, entry.id);
592
+ }),
593
+ chat.approvals.map((request) => /* @__PURE__ */ jsx(AgentApprovalCard, {
594
+ request,
595
+ onResolve: chat.resolveApproval
596
+ }, request.id))
597
+ ]
598
+ }) : null,
599
+ attachmentsSlot ? /* @__PURE__ */ jsx("div", {
600
+ "data-slot": "agent-chat-attachments",
601
+ className: "shrink-0 px-3",
602
+ children: attachmentsSlot
603
+ }) : null,
604
+ /* @__PURE__ */ jsxs("div", {
605
+ "data-slot": "agent-chat-composer",
606
+ className: "flex shrink-0 items-end gap-2 border-t border-[var(--ds-border-subtle)] p-3",
607
+ children: [/* @__PURE__ */ jsx(Textarea, {
608
+ "data-slot": "agent-chat-input",
609
+ "aria-label": `Message ${agentName}`,
610
+ placeholder: `Message ${agentName}…`,
611
+ rows: 1,
612
+ className: "max-h-40 min-h-9 resize-none",
613
+ value,
614
+ onChange: (event) => setValue(event.target.value),
615
+ onKeyDown
616
+ }), chat.streaming ? /* @__PURE__ */ jsx(Button, {
617
+ size: "icon",
618
+ variant: "outline",
619
+ "data-slot": "agent-chat-stop",
620
+ "aria-label": "Stop the run",
621
+ onClick: chat.abort,
622
+ children: /* @__PURE__ */ jsx(Square, { "aria-hidden": true })
623
+ }) : /* @__PURE__ */ jsx(Button, {
624
+ size: "icon",
625
+ "data-slot": "agent-chat-send",
626
+ "aria-label": "Send",
627
+ disabled: !value.trim(),
628
+ onClick: submit,
629
+ children: /* @__PURE__ */ jsx(Send, { "aria-hidden": true })
630
+ })]
631
+ })
632
+ ]
633
+ });
634
+ }
635
+ /**
636
+ * Every session of this agent, resumable from any window.
637
+ *
638
+ * The list is not this window's history: a session started in another window is
639
+ * here and can be resumed, which is the difference between "sessions are per
640
+ * window" (which key a window starts on) and "sessions are private to a window"
641
+ * (which they are not).
642
+ */
643
+ function AgentSessionDrawer({ sessions, current, loading, onResume }) {
644
+ return /* @__PURE__ */ jsxs("div", {
645
+ "data-slot": "agent-chat-sessions",
646
+ className: cn("max-h-56 shrink-0 overflow-y-auto border-b border-[var(--ds-border-subtle)]", "bg-[var(--ds-card)] px-2 py-1.5"),
647
+ children: [
648
+ loading && sessions.length === 0 ? /* @__PURE__ */ jsx("p", {
649
+ className: "px-2 py-1 text-[length:var(--ds-text-caption)] text-[color:var(--ds-muted-foreground)]",
650
+ children: "Loading sessions…"
651
+ }) : null,
652
+ !loading && sessions.length === 0 ? /* @__PURE__ */ jsx("p", {
653
+ className: "px-2 py-1 text-[length:var(--ds-text-caption)] text-[color:var(--ds-muted-foreground)]",
654
+ children: "No earlier sessions."
655
+ }) : null,
656
+ sessions.map((session) => /* @__PURE__ */ jsx("button", {
657
+ type: "button",
658
+ "data-slot": "agent-chat-session",
659
+ "data-current": session.key === current ? "" : void 0,
660
+ onClick: () => onResume(session.key),
661
+ className: cn("flex w-full items-center gap-2 rounded-[var(--ds-radius-sm)] px-2 py-1.5 text-left", "text-[length:var(--ds-text-caption-lg)] text-[color:var(--ds-foreground)]", "transition-colors duration-[var(--ds-duration-fast)] hover:bg-[var(--ds-hover)]", session.key === current && "bg-[var(--ds-primary-soft)]"),
662
+ children: /* @__PURE__ */ jsx("span", {
663
+ className: "min-w-0 truncate",
664
+ children: sessionTitle(session)
665
+ })
666
+ }, session.key))
667
+ ]
668
+ });
669
+ }
670
+ //#endregion
671
+ export { AgentA2UIBlocks, AgentApprovalCard, AgentChat, AgentMessageBubble, AgentSessionDrawer, AgentSteps, AgentStreamingBubble, AgentThinking, AgentToolCard, readMcpAppUri, shouldStickToBottom };
672
+
673
+ //# sourceMappingURL=agent-chat.js.map