cortena-ui 1.4.2 → 1.5.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 (133) hide show
  1. package/LICENSE +7 -0
  2. package/README.md +149 -1
  3. package/dist/a2ui/views.js +2 -2
  4. package/dist/agent-chat/a2ui-block.d.ts +60 -0
  5. package/dist/agent-chat/a2ui-block.js +69 -0
  6. package/dist/agent-chat/a2ui-block.js.map +1 -0
  7. package/dist/agent-chat/agui-client.d.ts +40 -0
  8. package/dist/agent-chat/agui-client.js +251 -0
  9. package/dist/agent-chat/agui-client.js.map +1 -0
  10. package/dist/agent-chat/bridge.d.ts +109 -0
  11. package/dist/agent-chat/bridge.js +349 -0
  12. package/dist/agent-chat/bridge.js.map +1 -0
  13. package/dist/agent-chat/session.d.ts +58 -0
  14. package/dist/agent-chat/session.js +249 -0
  15. package/dist/agent-chat/session.js.map +1 -0
  16. package/dist/agent-chat/store.d.ts +67 -0
  17. package/dist/agent-chat/store.js +548 -0
  18. package/dist/agent-chat/store.js.map +1 -0
  19. package/dist/agent-chat/types.d.ts +187 -0
  20. package/dist/agent-chat/types.js +17 -0
  21. package/dist/agent-chat/types.js.map +1 -0
  22. package/dist/agent-chat.d.ts +10 -0
  23. package/dist/agent-chat.js +10 -0
  24. package/dist/components/admin-permissions/admin-permissions.d.ts +66 -0
  25. package/dist/components/admin-permissions/admin-permissions.js +101 -0
  26. package/dist/components/admin-permissions/admin-permissions.js.map +1 -0
  27. package/dist/components/admin-permissions/context.d.ts +70 -0
  28. package/dist/components/admin-permissions/context.js +258 -0
  29. package/dist/components/admin-permissions/context.js.map +1 -0
  30. package/dist/components/admin-permissions/index.d.ts +10 -0
  31. package/dist/components/admin-permissions/licence.d.ts +15 -0
  32. package/dist/components/admin-permissions/licence.js +78 -0
  33. package/dist/components/admin-permissions/licence.js.map +1 -0
  34. package/dist/components/admin-permissions/matrix.d.ts +20 -0
  35. package/dist/components/admin-permissions/matrix.js +191 -0
  36. package/dist/components/admin-permissions/matrix.js.map +1 -0
  37. package/dist/components/admin-permissions/members.d.ts +18 -0
  38. package/dist/components/admin-permissions/members.js +185 -0
  39. package/dist/components/admin-permissions/members.js.map +1 -0
  40. package/dist/components/admin-permissions/role-assignment.d.ts +35 -0
  41. package/dist/components/admin-permissions/role-assignment.js +174 -0
  42. package/dist/components/admin-permissions/role-assignment.js.map +1 -0
  43. package/dist/components/admin-permissions/roles.d.ts +25 -0
  44. package/dist/components/admin-permissions/roles.js +168 -0
  45. package/dist/components/admin-permissions/roles.js.map +1 -0
  46. package/dist/components/admin-permissions/types.d.ts +152 -0
  47. package/dist/components/admin-permissions/types.js +63 -0
  48. package/dist/components/admin-permissions/types.js.map +1 -0
  49. package/dist/components/agent-chat-popup.d.ts +29 -0
  50. package/dist/components/agent-chat-popup.js +188 -0
  51. package/dist/components/agent-chat-popup.js.map +1 -0
  52. package/dist/components/agent-chat.d.ts +143 -0
  53. package/dist/components/agent-chat.js +578 -0
  54. package/dist/components/agent-chat.js.map +1 -0
  55. package/dist/components/app-shell.d.ts +126 -0
  56. package/dist/components/app-shell.js +297 -0
  57. package/dist/components/app-shell.js.map +1 -0
  58. package/dist/components/badge.d.ts +1 -1
  59. package/dist/components/button-link.js +1 -1
  60. package/dist/components/button.d.ts +2 -2
  61. package/dist/components/checkbox.d.ts +1 -1
  62. package/dist/components/combobox.d.ts +1 -1
  63. package/dist/components/combobox.js +1 -1
  64. package/dist/components/consent-screen.d.ts +65 -0
  65. package/dist/components/consent-screen.js +123 -0
  66. package/dist/components/consent-screen.js.map +1 -0
  67. package/dist/components/data-table/data-table.d.ts +15 -1
  68. package/dist/components/data-table/data-table.js +18 -4
  69. package/dist/components/data-table/data-table.js.map +1 -1
  70. package/dist/components/data-table/index.d.ts +4 -4
  71. package/dist/components/data-table/parts.d.ts +27 -3
  72. package/dist/components/data-table/parts.js +175 -55
  73. package/dist/components/data-table/parts.js.map +1 -1
  74. package/dist/components/data-table/types.d.ts +61 -0
  75. package/dist/components/data-table/use-data-table.js +91 -6
  76. package/dist/components/data-table/use-data-table.js.map +1 -1
  77. package/dist/components/data-table/use-server-source.js +119 -28
  78. package/dist/components/data-table/use-server-source.js.map +1 -1
  79. package/dist/components/help-panel.d.ts +131 -0
  80. package/dist/components/help-panel.js +545 -0
  81. package/dist/components/help-panel.js.map +1 -0
  82. package/dist/components/login-screen.d.ts +127 -0
  83. package/dist/components/login-screen.js +339 -0
  84. package/dist/components/login-screen.js.map +1 -0
  85. package/dist/components/session-guard.d.ts +268 -0
  86. package/dist/components/session-guard.js +632 -0
  87. package/dist/components/session-guard.js.map +1 -0
  88. package/dist/components/toast.d.ts +1 -1
  89. package/dist/core.d.ts +5 -1
  90. package/dist/core.js +11 -7
  91. package/dist/data-table.d.ts +13 -4
  92. package/dist/data-table.js +10 -2
  93. package/dist/hooks/use-cortena-theme.js +49 -3
  94. package/dist/hooks/use-cortena-theme.js.map +1 -1
  95. package/dist/index.d.ts +17 -4
  96. package/dist/index.js +21 -8
  97. package/dist/markdown.d.ts +2 -1
  98. package/dist/markdown.js +2 -1
  99. package/package.json +16 -4
  100. package/src/agent-chat/a2ui-block.ts +118 -0
  101. package/src/agent-chat/agui-client.ts +405 -0
  102. package/src/agent-chat/bridge.ts +433 -0
  103. package/src/agent-chat/session.ts +392 -0
  104. package/src/agent-chat/store.ts +738 -0
  105. package/src/agent-chat/types.ts +213 -0
  106. package/src/components/admin-permissions/admin-permissions.tsx +130 -0
  107. package/src/components/admin-permissions/context.tsx +376 -0
  108. package/src/components/admin-permissions/index.tsx +32 -0
  109. package/src/components/admin-permissions/licence.tsx +84 -0
  110. package/src/components/admin-permissions/matrix.tsx +257 -0
  111. package/src/components/admin-permissions/members.tsx +204 -0
  112. package/src/components/admin-permissions/role-assignment.tsx +239 -0
  113. package/src/components/admin-permissions/roles.tsx +169 -0
  114. package/src/components/admin-permissions/types.ts +231 -0
  115. package/src/components/agent-chat-popup.tsx +289 -0
  116. package/src/components/agent-chat.tsx +843 -0
  117. package/src/components/app-shell.tsx +502 -0
  118. package/src/components/consent-screen.tsx +239 -0
  119. package/src/components/data-table/data-table.tsx +36 -0
  120. package/src/components/data-table/index.tsx +6 -1
  121. package/src/components/data-table/parts.tsx +223 -47
  122. package/src/components/data-table/types.ts +68 -0
  123. package/src/components/data-table/use-data-table.ts +152 -4
  124. package/src/components/data-table/use-server-source.ts +150 -12
  125. package/src/components/help-panel.tsx +765 -0
  126. package/src/components/login-screen.tsx +479 -0
  127. package/src/components/session-guard.tsx +1071 -0
  128. package/src/entries/agent-chat.ts +113 -0
  129. package/src/entries/core.ts +8 -0
  130. package/src/entries/data-table.ts +41 -0
  131. package/src/entries/markdown.ts +25 -0
  132. package/src/hooks/use-cortena-theme.ts +63 -4
  133. package/src/index.ts +6 -0
@@ -0,0 +1,392 @@
1
+ /**
2
+ * Sessions, history and the filter chain.
3
+ *
4
+ * Ported from `cortena-shared/src/stores/use-chat.ts` and
5
+ * `cortenaweb/components/chat/message-bubble.tsx` (Cortena monorepo).
6
+ * `cortena-shared` is not published, so the rules the two transports share
7
+ * live here; keep them in step.
8
+ *
9
+ * The one deliberate difference is where the current session key is kept.
10
+ * cortenaweb puts it in `localStorage`, which is shared by every tab of an
11
+ * origin, so a second window adopted the first window's session: it inherited
12
+ * a stream that was not its own and both windows then showed the same content.
13
+ * Sessions here are **per window** — the key lives in `sessionStorage`, under a
14
+ * name scoped to the extension — so opening the extension twice starts two
15
+ * sessions. Both stay listed and either window can resume either one; two
16
+ * windows on one session may both send, and cortenacore serialises the runs.
17
+ */
18
+
19
+ import type { AgentChatMessage } from "./types";
20
+
21
+ /* ── minting and remembering a key ───────────────────────────────────────── */
22
+
23
+ /**
24
+ * A new session key, minted client-side.
25
+ *
26
+ * `agent:<agentId>:new-<Date.now()>-<random36>`. cortenacore reads the
27
+ * `agent:<id>:` prefix to bind the session to that agent's template, and the
28
+ * key is the AG-UI `threadId` verbatim, prefix included. Minting it rather than
29
+ * asking for one is what lets the first message go out immediately.
30
+ */
31
+ export function mintSessionKey(agentId: string): string {
32
+ return `agent:${agentId}:new-${Date.now()}-${Math.random().toString(36).slice(2, 7)}`;
33
+ }
34
+
35
+ /** Was this key minted locally for a chat that had no session yet? */
36
+ export function isProvisionalSessionKey(key: string): boolean {
37
+ return /(^|:)new-\d+-[a-z0-9]+$/.test(key);
38
+ }
39
+
40
+ /** Does this session belong to this agent? `sessions.list` is filtered by it. */
41
+ export function isSessionOfAgent(key: string, agentId: string): boolean {
42
+ return key.startsWith(`agent:${agentId}:`);
43
+ }
44
+
45
+ /** The `sessionStorage` name the current key is kept under, per extension. */
46
+ export function sessionStorageKey(scope: string): string {
47
+ return `cortena.agent-chat.${scope}.session`;
48
+ }
49
+
50
+ /** The `sessionStorage` name the open/collapsed state is kept under. */
51
+ export function viewStorageKey(scope: string): string {
52
+ return `cortena.agent-chat.${scope}.view`;
53
+ }
54
+
55
+ /**
56
+ * `sessionStorage`, or nothing.
57
+ *
58
+ * Absent during SSR and refused outright when a browser blocks storage for the
59
+ * origin, and neither is a reason for the chat not to work: without it a window
60
+ * simply forgets its session on reload.
61
+ */
62
+ export function windowSessionStorage(): Storage | null {
63
+ try {
64
+ return typeof window === "undefined" ? null : window.sessionStorage;
65
+ } catch {
66
+ return null;
67
+ }
68
+ }
69
+
70
+ export function readStoredSessionKey(scope: string): string | null {
71
+ try {
72
+ return windowSessionStorage()?.getItem(sessionStorageKey(scope)) ?? null;
73
+ } catch {
74
+ return null;
75
+ }
76
+ }
77
+
78
+ export function writeStoredSessionKey(scope: string, key: string | null): void {
79
+ try {
80
+ const storage = windowSessionStorage();
81
+ if (!storage) {
82
+ return;
83
+ }
84
+ if (key === null) {
85
+ storage.removeItem(sessionStorageKey(scope));
86
+ } else {
87
+ storage.setItem(sessionStorageKey(scope), key);
88
+ }
89
+ } catch {
90
+ // A blocked storage is not a reason to lose the run in flight.
91
+ }
92
+ }
93
+
94
+ /* ── history ─────────────────────────────────────────────────────────────── */
95
+
96
+ function generateId(): string {
97
+ return `msg-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
98
+ }
99
+
100
+ function isToolRelatedMessage(msg: Record<string, unknown>): boolean {
101
+ const role = typeof msg.role === "string" ? msg.role.toLowerCase() : "";
102
+ if (role === "tool" || role === "toolresult" || role === "tool_result") {
103
+ return true;
104
+ }
105
+ const content = msg.content as Array<{ type: string }> | undefined;
106
+ if (Array.isArray(content) && content.length > 0) {
107
+ const toolTypes = new Set([
108
+ "tool_use",
109
+ "tool_call",
110
+ "tool_result",
111
+ "toolresult",
112
+ "server_tool_use",
113
+ ]);
114
+ const hasText = content.some((c) => c.type === "text" || c.type === "thinking");
115
+ const hasToolBlocks = content.some((c) => toolTypes.has(c.type));
116
+ if (hasToolBlocks && !hasText) {
117
+ return true;
118
+ }
119
+ }
120
+ return false;
121
+ }
122
+
123
+ /**
124
+ * Strip `System: [timestamp] …` lines and `[System Message]` prefixes the
125
+ * runtime prepends to a user turn as agent context. They are not user text.
126
+ */
127
+ function stripSystemPrefixes(text: string): string {
128
+ return text
129
+ .split("\n")
130
+ .filter((line) => !/^System:\s*\[/.test(line) && !line.startsWith("[System Message]"))
131
+ .join("\n")
132
+ .replace(/^\n+/, "");
133
+ }
134
+
135
+ /** Lift `<thinking>…</thinking>` out of plain text. */
136
+ export function separateThinking(text: string): { thinkingText?: string; mainText: string } {
137
+ const match = text.match(/<thinking>([\s\S]*?)<\/thinking>/);
138
+ if (!match?.[1]) {
139
+ return { mainText: text };
140
+ }
141
+ const thinkingText = match[1].trim();
142
+ const mainText = text.replace(/<thinking>[\s\S]*?<\/thinking>\s*/, "").trim();
143
+ return { ...(thinkingText ? { thinkingText } : {}), mainText };
144
+ }
145
+
146
+ function parseOneHistoryMessage(raw: unknown): AgentChatMessage {
147
+ const msg = raw as Record<string, unknown>;
148
+ const content = msg.content as
149
+ | Array<{ type: string; text?: string; thinking?: string; a2ui?: unknown }>
150
+ | undefined;
151
+ let text = Array.isArray(content)
152
+ ? content
153
+ .filter((c) => c.type === "text" && c.text)
154
+ .map((c) => c.text!)
155
+ .join("")
156
+ : typeof msg.content === "string"
157
+ ? msg.content
158
+ : typeof msg.text === "string"
159
+ ? msg.text
160
+ : "";
161
+
162
+ const isToolMessage = isToolRelatedMessage(msg);
163
+ const role = ((msg.role as string) === "user" ? "user" : "assistant") as "user" | "assistant";
164
+ if (role === "user") {
165
+ text = stripSystemPrefixes(text);
166
+ }
167
+
168
+ let thinkingText = "";
169
+ if (typeof msg.thinking === "string" && msg.thinking.trim()) {
170
+ thinkingText = msg.thinking.trim();
171
+ } else if (Array.isArray(content)) {
172
+ thinkingText = content
173
+ .filter((c) => c.type === "thinking")
174
+ .map((c) => c.thinking ?? c.text ?? "")
175
+ .filter(Boolean)
176
+ .join("\n\n");
177
+ }
178
+ if (!thinkingText) {
179
+ const { thinkingText: tagThinking, mainText } = separateThinking(text);
180
+ if (tagThinking) {
181
+ return {
182
+ id: typeof msg.id === "string" ? msg.id : generateId(),
183
+ role,
184
+ text: mainText,
185
+ timestamp: (msg.timestamp as number) ?? Date.now(),
186
+ thinkingText: tagThinking,
187
+ ...(isToolMessage ? { isToolMessage } : {}),
188
+ };
189
+ }
190
+ }
191
+
192
+ return {
193
+ id: typeof msg.id === "string" ? msg.id : generateId(),
194
+ role,
195
+ text,
196
+ timestamp: (msg.timestamp as number) ?? Date.now(),
197
+ ...(thinkingText ? { thinkingText } : {}),
198
+ ...(isToolMessage ? { isToolMessage } : {}),
199
+ };
200
+ }
201
+
202
+ /**
203
+ * Merge an agent turn — assistant messages plus tool call and result messages —
204
+ * into one visible message. Everything except the final substantive response is
205
+ * folded into the chain of thought. Only a genuine user message breaks the turn.
206
+ */
207
+ function mergeConsecutiveAssistantMessages(messages: AgentChatMessage[]): AgentChatMessage[] {
208
+ const result: AgentChatMessage[] = [];
209
+ let run: AgentChatMessage[] = [];
210
+
211
+ const flush = () => {
212
+ const first = run[0];
213
+ if (!first) {
214
+ return;
215
+ }
216
+ if (run.length === 1) {
217
+ result.push(first);
218
+ run = [];
219
+ return;
220
+ }
221
+ let visibleIdx = run.length - 1;
222
+ while (visibleIdx > 0 && !(run[visibleIdx]?.text ?? "").trim()) {
223
+ visibleIdx--;
224
+ }
225
+ const visible = run[visibleIdx] ?? first;
226
+ const cot: string[] = [];
227
+ for (let i = 0; i < run.length; i++) {
228
+ const entry = run[i];
229
+ if (!entry) {
230
+ continue;
231
+ }
232
+ if (i === visibleIdx) {
233
+ if (visible.thinkingText) {
234
+ cot.push(visible.thinkingText);
235
+ }
236
+ continue;
237
+ }
238
+ if (entry.thinkingText) {
239
+ cot.push(entry.thinkingText);
240
+ }
241
+ if (entry.text.trim()) {
242
+ cot.push(entry.text.trim());
243
+ }
244
+ }
245
+ const combined = cot.filter(Boolean).join("\n\n");
246
+ result.push({ ...visible, ...(combined ? { thinkingText: combined } : {}) });
247
+ run = [];
248
+ };
249
+
250
+ for (const msg of messages) {
251
+ const isRealUser = msg.role === "user" && !msg.isToolMessage && msg.text.trim().length > 0;
252
+ if (isRealUser) {
253
+ flush();
254
+ result.push(msg);
255
+ } else {
256
+ run.push(msg);
257
+ }
258
+ }
259
+ flush();
260
+ return result;
261
+ }
262
+
263
+ /** Raw transcript records to the messages the list renders. */
264
+ export function parseHistoryMessages(raw: readonly unknown[]): AgentChatMessage[] {
265
+ return mergeConsecutiveAssistantMessages(raw.map(parseOneHistoryMessage));
266
+ }
267
+
268
+ /**
269
+ * The history shrink guard.
270
+ *
271
+ * A history load must not shrink the session already on screen. The first
272
+ * message of a new chat loads history while the turn is still in flight and the
273
+ * user's message has not been persisted yet; replacing wholesale threw that
274
+ * message away, and the reply survived, so the symptom was a conversation with
275
+ * the answer but not the question.
276
+ *
277
+ * Only the same session, and only against losing messages: a longer or equal
278
+ * history still replaces, so an edit, a deletion made elsewhere and a compaction
279
+ * all land normally.
280
+ */
281
+ export function wouldShrinkHistory(params: {
282
+ currentSessionKey: string | null;
283
+ loadedSessionKey: string;
284
+ currentCount: number;
285
+ loadedCount: number;
286
+ }): boolean {
287
+ return (
288
+ params.currentSessionKey === params.loadedSessionKey &&
289
+ params.loadedCount < params.currentCount
290
+ );
291
+ }
292
+
293
+ /* ── the filter chain ────────────────────────────────────────────────────── */
294
+
295
+ /** A JSON blob a tool or an extension API returned, echoed as a message. */
296
+ function isToolResult(text: string): boolean {
297
+ const trimmed = text.trim();
298
+ const wrapped =
299
+ (trimmed.startsWith("[") && trimmed.endsWith("]")) ||
300
+ (trimmed.startsWith("{") && trimmed.endsWith("}"));
301
+ if (!wrapped) {
302
+ return false;
303
+ }
304
+ try {
305
+ const parsed = JSON.parse(trimmed);
306
+ return typeof parsed === "object" && parsed !== null;
307
+ } catch {
308
+ return false;
309
+ }
310
+ }
311
+
312
+ /** Raw skill-file content the model echoed instead of acting on. */
313
+ function isSkillContent(text: string): boolean {
314
+ const trimmed = text.trim();
315
+ return (
316
+ /^name:\s*\S+.*\bdescription:.*\btype:\s*skill\b/i.test(trimmed) ||
317
+ /^---\s*\n[\s\S]*?\btype:\s*skill\b[\s\S]*?\n---/m.test(trimmed)
318
+ );
319
+ }
320
+
321
+ /** Runtime diagnostics: token counters, a leaked api-key hint. */
322
+ function isAgentDiagnostics(text: string): boolean {
323
+ const trimmed = text.trim();
324
+ return /Tokens:\s*\d+.*Cache:.*Context:/s.test(trimmed) || /\bapi-key\s+sk-/.test(trimmed);
325
+ }
326
+
327
+ /** Tool execution output that leaked into history as its own message. */
328
+ function isToolOutput(text: string): boolean {
329
+ const trimmed = text.trim();
330
+ if (!trimmed) {
331
+ return false;
332
+ }
333
+ if (/^Tool\s+\S+\s+not found$/i.test(trimmed)) {
334
+ return true;
335
+ }
336
+ if (trimmed === "(no output)") {
337
+ return true;
338
+ }
339
+ if (/^total\s+\d+\s+[d-][rwx-]{9}/.test(trimmed)) {
340
+ return true;
341
+ }
342
+ if (/^[d-][rwx-]{9}[@+]?\s+\d+\s+\S+\s+\S+/.test(trimmed)) {
343
+ return true;
344
+ }
345
+ if (/(?:^|\n)[A-Z_]{2,}=\S/m.test(trimmed)) {
346
+ if (
347
+ /\b(?:PATH|PWD|HOME|USER|INIT_CWD|PNPM_|NODE_|npm_|CORTENA_|CORTENACORE_|CORTENABOT_)/m.test(
348
+ trimmed,
349
+ )
350
+ ) {
351
+ return true;
352
+ }
353
+ if ((trimmed.match(/(?:^|\n)[A-Z_]{3,}=\S/gm) ?? []).length >= 3) {
354
+ return true;
355
+ }
356
+ }
357
+ return false;
358
+ }
359
+
360
+ /**
361
+ * Should this message be hidden?
362
+ *
363
+ * The same chain cortenaweb's bubble applies, in the same order. It exists
364
+ * because tool plumbing leaks into a transcript in several recognisable shapes,
365
+ * and a chat that renders them reads as broken. Tool detail is not lost: it is
366
+ * in the tool strip and in the chain of thought.
367
+ *
368
+ * A reply that is only UI the agent drew has no text at all, so an A2UI block
369
+ * counts as content — without that, the surface the user asked for is filtered
370
+ * away and nothing appears.
371
+ */
372
+ export function isHiddenMessage(message: AgentChatMessage): boolean {
373
+ if (message.isError) {
374
+ return false;
375
+ }
376
+ if (message.isToolMessage) {
377
+ return true;
378
+ }
379
+ if (
380
+ message.role === "assistant" &&
381
+ !message.text.trim() &&
382
+ !message.thinkingText &&
383
+ !message.a2ui?.length
384
+ ) {
385
+ return true;
386
+ }
387
+ if (message.role === "user") {
388
+ return !message.text.trim() && !message.attachments?.length;
389
+ }
390
+ const text = message.text;
391
+ return isToolResult(text) || isSkillContent(text) || isAgentDiagnostics(text) || isToolOutput(text);
392
+ }