@ruckc/truss-chat 1.34.0 → 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # @ruckc/truss-chat
2
2
 
3
- Chat building blocks for [Truss UI](https://truss.ruck.io): thread, messages, reasoning, tool calls, composer, status line and a question form.
3
+ Chat building blocks for [Truss UI](https://truss.ruck.io): thread, messages, reasoning, tool calls, composer, status line, a question form, a conversation list (`ChatSessions`) and a conversation title row (`ChatTitle`).
4
4
 
5
5
  Docs: <https://truss.ruck.io/docs/packages>
6
6
 
@@ -27,6 +27,23 @@ npm install @ruckc/truss-chat @ruckc/truss-ui @ruckc/truss-markdown
27
27
  <ChatComposer bind:value onsend={(text) => console.log(text)} />
28
28
  ```
29
29
 
30
+ A conversation list with select, rename and archive (the component never edits `sessions`; apply the change in the callbacks):
31
+
32
+ ```svelte
33
+ <script lang="ts">
34
+ import { ChatSessions, type ChatSession } from '@ruckc/truss-chat';
35
+ let sessions = $state<ChatSession[]>([{ id: 'a', title: 'Trip plan' }, { id: 'b', title: 'Budget' }]);
36
+ let selected = $state('a');
37
+ </script>
38
+
39
+ <ChatSessions
40
+ {sessions}
41
+ bind:selected
42
+ onrename={(id, title) => (sessions = sessions.map((s) => (s.id === id ? { ...s, title } : s)))}
43
+ onarchive={(id, archived) => (sessions = sessions.map((s) => (s.id === id ? { ...s, archived } : s)))}
44
+ />
45
+ ```
46
+
30
47
  ## License
31
48
 
32
49
  MIT
@@ -1,20 +1,25 @@
1
1
  <script lang="ts">
2
2
  import type { Snippet } from 'svelte';
3
- import type { HTMLAttributes } from 'svelte/elements';
3
+ import type { HTMLAttributes, HTMLButtonAttributes, HTMLTextareaAttributes } from 'svelte/elements';
4
4
  import { Button, Textarea } from '@ruckc/truss-ui';
5
5
 
6
6
  let {
7
7
  value = $bindable(''),
8
8
  onsend,
9
9
  onkeydown: hook,
10
+ onstop,
10
11
  trim = true,
11
12
  busy = false,
12
13
  sendOn = 'enter',
13
14
  placeholder = 'Message',
15
+ minRows = 1,
14
16
  maxRows = 8,
15
17
  disabled = false,
16
18
  label = 'Message',
17
19
  sendLabel = 'Send',
20
+ stopLabel = 'Stop',
21
+ inputAttrs,
22
+ sendAttrs,
18
23
  actions,
19
24
  class: klass = '',
20
25
  ...rest
@@ -27,11 +32,15 @@
27
32
  onkeydown?: (e: KeyboardEvent) => void;
28
33
  /** Trim the text passed to `onsend`. Set false to keep leading/trailing whitespace and newlines (the blank check still ignores whitespace-only drafts). */
29
34
  trim?: boolean;
30
- /** A reply is in progress: the send button shows a spinner and is disabled, and sending does nothing. The text stays editable. */
35
+ /** Called when the user presses the Stop button, which replaces the send button while `busy` (see `onstop`). */
36
+ onstop?: () => void;
37
+ /** A reply is in progress: nothing is sent and the text stays editable. The send button shows a spinner and is disabled, or, when `onstop` is set, is replaced by an enabled Stop button. */
31
38
  busy?: boolean;
32
39
  /** `enter`: Enter sends and Shift+Enter adds a newline. `mod-enter`: Ctrl/Cmd+Enter sends and Enter adds a newline. Enter never sends while an IME composition is in progress. */
33
40
  sendOn?: 'enter' | 'mod-enter';
34
41
  placeholder?: string;
42
+ /** The field is at least this many rows tall (default 1) and grows with its content up to `maxRows`. */
43
+ minRows?: number;
35
44
  /** The field grows with its content up to this many rows, then scrolls. */
36
45
  maxRows?: number;
37
46
  /** Disables the field and the send button. */
@@ -40,6 +49,12 @@
40
49
  label?: string;
41
50
  /** Accessible name of the send button. */
42
51
  sendLabel?: string;
52
+ /** Accessible name of the Stop button. */
53
+ stopLabel?: string;
54
+ /** Attributes for the `<textarea>` (`data-testid`, `title`, `aria-keyshortcuts`, `name`, ...). `aria-label` here replaces `label`; `class` is added to the field's wrapper. */
55
+ inputAttrs?: Omit<HTMLTextareaAttributes, 'value' | 'defaultValue' | 'rows' | 'children'>;
56
+ /** Attributes for the send button (`data-testid`, `title`, `aria-keyshortcuts`, ...). `aria-label` here replaces `sendLabel`. They also apply to the Stop button, except `onclick`, `disabled` and `aria-label`, which are owned by it. */
57
+ sendAttrs?: Omit<HTMLButtonAttributes, 'children'>;
43
58
  /** Extra buttons placed between the field and the send button (attach, stop, voice, ...). */
44
59
  actions?: Snippet;
45
60
  /** Other attributes (`data-testid`, `id`, ...) go to the root `<div>`. `class` is added to it. */
@@ -82,9 +97,13 @@
82
97
  </script>
83
98
 
84
99
  <div {...rest} bind:this={root} class={['composer', klass]} aria-busy={busy}>
85
- <Textarea bind:value minRows={1} {maxRows} autosize {placeholder} {disabled} aria-label={label} {onkeydown} class="field" />
100
+ <Textarea {...inputAttrs} bind:value {minRows} {maxRows} autosize {placeholder} {disabled} aria-label={inputAttrs?.['aria-label'] ?? label} {onkeydown} class={['field', inputAttrs?.class]} />
86
101
  {#if actions}<div class="actions">{@render actions()}</div>{/if}
87
- <Button icon="send" aria-label={sendLabel} loading={busy} disabled={disabled || busy || empty} onclick={send} />
102
+ {#if busy && onstop}
103
+ <Button {...sendAttrs} icon="stop" aria-label={stopLabel} {disabled} onclick={() => onstop()} />
104
+ {:else}
105
+ <Button icon="send" aria-label={sendLabel} {...sendAttrs} loading={busy} disabled={disabled || busy || empty} onclick={send} />
106
+ {/if}
88
107
  </div>
89
108
 
90
109
  <style>
@@ -1,5 +1,5 @@
1
1
  import type { Snippet } from 'svelte';
2
- import type { HTMLAttributes } from 'svelte/elements';
2
+ import type { HTMLAttributes, HTMLButtonAttributes, HTMLTextareaAttributes } from 'svelte/elements';
3
3
  type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class'> & {
4
4
  /** The draft text. Bindable; cleared after a send. */
5
5
  value?: string;
@@ -9,11 +9,15 @@ type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class
9
9
  onkeydown?: (e: KeyboardEvent) => void;
10
10
  /** Trim the text passed to `onsend`. Set false to keep leading/trailing whitespace and newlines (the blank check still ignores whitespace-only drafts). */
11
11
  trim?: boolean;
12
- /** A reply is in progress: the send button shows a spinner and is disabled, and sending does nothing. The text stays editable. */
12
+ /** Called when the user presses the Stop button, which replaces the send button while `busy` (see `onstop`). */
13
+ onstop?: () => void;
14
+ /** A reply is in progress: nothing is sent and the text stays editable. The send button shows a spinner and is disabled, or, when `onstop` is set, is replaced by an enabled Stop button. */
13
15
  busy?: boolean;
14
16
  /** `enter`: Enter sends and Shift+Enter adds a newline. `mod-enter`: Ctrl/Cmd+Enter sends and Enter adds a newline. Enter never sends while an IME composition is in progress. */
15
17
  sendOn?: 'enter' | 'mod-enter';
16
18
  placeholder?: string;
19
+ /** The field is at least this many rows tall (default 1) and grows with its content up to `maxRows`. */
20
+ minRows?: number;
17
21
  /** The field grows with its content up to this many rows, then scrolls. */
18
22
  maxRows?: number;
19
23
  /** Disables the field and the send button. */
@@ -22,6 +26,12 @@ type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class
22
26
  label?: string;
23
27
  /** Accessible name of the send button. */
24
28
  sendLabel?: string;
29
+ /** Accessible name of the Stop button. */
30
+ stopLabel?: string;
31
+ /** Attributes for the `<textarea>` (`data-testid`, `title`, `aria-keyshortcuts`, `name`, ...). `aria-label` here replaces `label`; `class` is added to the field's wrapper. */
32
+ inputAttrs?: Omit<HTMLTextareaAttributes, 'value' | 'defaultValue' | 'rows' | 'children'>;
33
+ /** Attributes for the send button (`data-testid`, `title`, `aria-keyshortcuts`, ...). `aria-label` here replaces `sendLabel`. They also apply to the Stop button, except `onclick`, `disabled` and `aria-label`, which are owned by it. */
34
+ sendAttrs?: Omit<HTMLButtonAttributes, 'children'>;
25
35
  /** Extra buttons placed between the field and the send button (attach, stop, voice, ...). */
26
36
  actions?: Snippet;
27
37
  /** Other attributes (`data-testid`, `id`, ...) go to the root `<div>`. `class` is added to it. */
@@ -19,6 +19,8 @@
19
19
  tools,
20
20
  reasoning,
21
21
  showReasoning = $bindable(),
22
+ code,
23
+ link,
22
24
  class: klass = '',
23
25
  children,
24
26
  ...rest
@@ -51,6 +53,10 @@
51
53
  * Set it to `true` or `false` to control this message alone: the button then changes only this value. Precedence: this prop, then the thread's setting, then hidden.
52
54
  */
53
55
  showReasoning?: boolean;
56
+ /** Renders a fenced code block of the Markdown (`text`, `reasoning`) in place of the default `<pre><code>`, e.g. with `truss-code`. Receives the language (empty when none) and the code. Defaults to the thread's `code`. */
57
+ code?: Snippet<[{ lang: string; text: string }]>;
58
+ /** Renders a Markdown link in place of the default `<a>`, e.g. for client-side routing. Only http, https and mailto links reach it; `children` is the link text. Defaults to the thread's `link`. */
59
+ link?: Snippet<[{ href: string; children: Snippet }]>;
54
60
  /** Other attributes (`data-testid`, `id`, ...) go to the root `<article>`. `class` is added to it. */
55
61
  class?: string;
56
62
  /** Show `text` as plain text even for an assistant message. */
@@ -63,6 +69,8 @@
63
69
 
64
70
  const thread = useThread();
65
71
  let body: HTMLDivElement;
72
+ const codeSnippet = $derived(code ?? thread?.code);
73
+ const linkSnippet = $derived(link ?? thread?.link);
66
74
  const rich = $derived(!plain && (markdown ?? role === 'assistant'));
67
75
  let localShow = $state(false);
68
76
  const shown = $derived(showReasoning ?? (thread ? thread.showReasoning : localShow));
@@ -105,11 +113,11 @@
105
113
  {shown ? 'Hide reasoning' : 'Show reasoning'}
106
114
  {#if streaming && !shown}<span class="thinking">Thinking…</span>{/if}
107
115
  </button>
108
- <div class="rbody" id="{rid}-reasoning" hidden={!shown}>{#if shown}<Markdown text={reasoning} />{/if}</div>
116
+ <div class="rbody" id="{rid}-reasoning" hidden={!shown}>{#if shown}<Markdown text={reasoning} code={codeSnippet} link={linkSnippet} />{/if}</div>
109
117
  </div>
110
118
  {/if}
111
119
  <div class="body" bind:this={body}>
112
- {#if content}{@render content()}{:else if children}{@render children()}{:else if text}{#if rich}<Markdown {text} />{:else}<div class="plain">{text}</div>{/if}{/if}
120
+ {#if content}{@render content()}{:else if children}{@render children()}{:else if text}{#if rich}<Markdown {text} code={codeSnippet} link={linkSnippet} />{:else}<div class="plain">{text}</div>{/if}{/if}
113
121
  {#if streaming && typing}<span class="typing" aria-hidden="true"><i></i><i></i><i></i></span>{/if}
114
122
  </div>
115
123
  {#if tools}<div class="tools">{@render tools()}</div>{/if}
@@ -29,6 +29,16 @@ type $$ComponentProps = Omit<HTMLAttributes<HTMLElement>, 'children' | 'class'>
29
29
  * Set it to `true` or `false` to control this message alone: the button then changes only this value. Precedence: this prop, then the thread's setting, then hidden.
30
30
  */
31
31
  showReasoning?: boolean;
32
+ /** Renders a fenced code block of the Markdown (`text`, `reasoning`) in place of the default `<pre><code>`, e.g. with `truss-code`. Receives the language (empty when none) and the code. Defaults to the thread's `code`. */
33
+ code?: Snippet<[{
34
+ lang: string;
35
+ text: string;
36
+ }]>;
37
+ /** Renders a Markdown link in place of the default `<a>`, e.g. for client-side routing. Only http, https and mailto links reach it; `children` is the link text. Defaults to the thread's `link`. */
38
+ link?: Snippet<[{
39
+ href: string;
40
+ children: Snippet;
41
+ }]>;
32
42
  /** Other attributes (`data-testid`, `id`, ...) go to the root `<article>`. `class` is added to it. */
33
43
  class?: string;
34
44
  /** Show `text` as plain text even for an assistant message. */
@@ -0,0 +1,148 @@
1
+ <script lang="ts" module>
2
+ export type ChatSession = {
3
+ /** A stable identifier, passed back to every callback and used for `selected`. */
4
+ id: string;
5
+ title: string;
6
+ /** Archived conversations are hidden until `showArchived` is on. */
7
+ archived?: boolean;
8
+ /** A muted second line, e.g. "2 hours ago" or the last message. */
9
+ meta?: string;
10
+ };
11
+ </script>
12
+
13
+ <script lang="ts">
14
+ import { tick } from 'svelte';
15
+ import type { HTMLAttributes } from 'svelte/elements';
16
+ import { MenuButton, Switch } from '@ruckc/truss-ui';
17
+ import RenameField from './internal/RenameField.svelte';
18
+
19
+ let {
20
+ sessions,
21
+ selected = $bindable(),
22
+ showArchived = $bindable(false),
23
+ onselect,
24
+ onrename,
25
+ onarchive,
26
+ label = 'Conversations',
27
+ archivedLabel = 'Show archived',
28
+ archivedText = 'Archived',
29
+ renameLabel = 'Rename',
30
+ archiveLabel = 'Archive',
31
+ unarchiveLabel = 'Unarchive',
32
+ actionsLabel = 'Actions',
33
+ empty = 'No conversations',
34
+ class: klass = '',
35
+ ...rest
36
+ }: Omit<HTMLAttributes<HTMLElement>, 'children' | 'class'> & {
37
+ /** The conversations, in the order to show them. */
38
+ sessions: ChatSession[];
39
+ /** The `id` of the open conversation (`aria-current`). Bindable; set when the reader picks one. */
40
+ selected?: string;
41
+ /** Whether archived conversations are listed. Bindable, driven by the "Show archived" switch; the switch only appears when something is archived. */
42
+ showArchived?: boolean;
43
+ /** The reader picked a conversation. */
44
+ onselect?: (id: string) => void;
45
+ /** The reader renamed a conversation, inline (double-click, F2) or from its menu. Receives the trimmed new title; a blank or unchanged title is dropped. You update `sessions`. */
46
+ onrename?: (id: string, title: string) => void;
47
+ /** The reader archived (`true`) or unarchived (`false`) a conversation from its menu. You update `sessions`; an archived one disappears unless `showArchived`. */
48
+ onarchive?: (id: string, archived: boolean) => void;
49
+ /** Accessible name of the list and its heading. */
50
+ label?: string;
51
+ /** Text of the switch that includes archived conversations. */
52
+ archivedLabel?: string;
53
+ /** Badge on an archived conversation. */
54
+ archivedText?: string;
55
+ /** Text of the Rename menu entry (and the name of the rename field). */
56
+ renameLabel?: string;
57
+ /** Text of the Archive menu entry. */
58
+ archiveLabel?: string;
59
+ /** Text of the Unarchive menu entry. */
60
+ unarchiveLabel?: string;
61
+ /** Accessible name of each row's menu button, followed by the title. */
62
+ actionsLabel?: string;
63
+ /** Shown when nothing is listed. */
64
+ empty?: string;
65
+ /** Other attributes (`data-testid`, `id`, ...) go to the root `<section>`. `class` is added to it. */
66
+ class?: string;
67
+ } = $props();
68
+
69
+ let editing = $state<string | undefined>();
70
+ const shown = $derived(sessions.filter((s) => showArchived || !s.archived));
71
+ const hasArchived = $derived(sessions.some((s) => s.archived));
72
+ const uid = $props.id();
73
+
74
+ let root: HTMLElement;
75
+ /** Start renaming a conversation in place, e.g. from your own shortcut. */
76
+ export function rename(id: string) {
77
+ editing = id;
78
+ }
79
+ async function stop(id: string, title?: string) {
80
+ if (title !== undefined) onrename?.(id, title);
81
+ editing = undefined;
82
+ await tick();
83
+ root.querySelector<HTMLElement>(`[data-id="${CSS.escape(id)}"] .pick`)?.focus();
84
+ }
85
+ function pick(id: string) {
86
+ selected = id;
87
+ onselect?.(id);
88
+ }
89
+ function key(e: KeyboardEvent, id: string) {
90
+ if (e.key !== 'F2') return;
91
+ e.preventDefault();
92
+ editing = id;
93
+ }
94
+ </script>
95
+
96
+ <section {...rest} bind:this={root} class={['sessions', klass]} aria-labelledby="{uid}-h">
97
+ <header class="head">
98
+ <h2 id="{uid}-h">{label}</h2>
99
+ {#if hasArchived}<Switch bind:checked={showArchived} label={archivedLabel} />{/if}
100
+ </header>
101
+ {#if shown.length}
102
+ <ul>
103
+ {#each shown as s (s.id)}
104
+ <li data-id={s.id} data-selected={selected === s.id} data-archived={!!s.archived}>
105
+ {#if editing === s.id}
106
+ <RenameField value={s.title} label={renameLabel} oncommit={(t) => stop(s.id, t)} oncancel={() => stop(s.id)} />
107
+ {:else}
108
+ <button type="button" class="pick" aria-current={selected === s.id ? 'true' : undefined} onclick={() => pick(s.id)} ondblclick={() => (editing = s.id)} onkeydown={(e) => key(e, s.id)}>
109
+ <span class="title">{s.title}</span>
110
+ {#if s.meta || s.archived}<span class="meta">{[s.archived && archivedText, s.meta].filter(Boolean).join(' · ')}</span>{/if}
111
+ </button>
112
+ <MenuButton
113
+ label="{actionsLabel}, {s.title}"
114
+ icon="more"
115
+ iconOnly
116
+ variant="minimal"
117
+ size="sm"
118
+ align="end"
119
+ items={[
120
+ { label: renameLabel, icon: 'edit', onselect: () => (editing = s.id) },
121
+ { label: s.archived ? unarchiveLabel : archiveLabel, icon: s.archived ? 'inbox' : 'archive', onselect: () => onarchive?.(s.id, !s.archived) }
122
+ ]}
123
+ />
124
+ {/if}
125
+ </li>
126
+ {/each}
127
+ </ul>
128
+ {:else}
129
+ <p class="empty">{empty}</p>
130
+ {/if}
131
+ </section>
132
+
133
+ <style>
134
+ .sessions { display: flex; flex-direction: column; gap: var(--tr-space-2); min-width: 0; }
135
+ .head { display: flex; align-items: center; justify-content: space-between; gap: var(--tr-space-2); flex-wrap: wrap; }
136
+ h2 { margin: 0; font-size: var(--tr-text-sm); font-weight: 600; color: var(--tr-on-surface-variant); }
137
+ ul { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 2px; }
138
+ li { display: flex; align-items: center; gap: var(--tr-space-1); min-width: 0; border-radius: var(--tr-radius-md); }
139
+ li:hover { background: var(--tr-surface-container); }
140
+ li[data-selected='true'] { background: var(--tr-primary-container); color: var(--tr-on-primary-container); }
141
+ .pick { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; padding: var(--tr-space-1) var(--tr-space-2); border: 0; border-radius: var(--tr-radius-md); background: none; color: inherit; font: inherit; text-align: start; cursor: pointer; }
142
+ .pick:focus-visible { outline: none; box-shadow: var(--tr-focus-ring); }
143
+ .title, .meta { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
144
+ .meta { font-size: var(--tr-text-sm); color: var(--tr-on-surface-variant); }
145
+ li[data-selected='true'] .meta { color: inherit; }
146
+ li[data-archived='true'] .title { font-style: italic; }
147
+ .empty { margin: 0; color: var(--tr-on-surface-variant); font-size: var(--tr-text-sm); }
148
+ </style>
@@ -0,0 +1,47 @@
1
+ export type ChatSession = {
2
+ /** A stable identifier, passed back to every callback and used for `selected`. */
3
+ id: string;
4
+ title: string;
5
+ /** Archived conversations are hidden until `showArchived` is on. */
6
+ archived?: boolean;
7
+ /** A muted second line, e.g. "2 hours ago" or the last message. */
8
+ meta?: string;
9
+ };
10
+ import type { HTMLAttributes } from 'svelte/elements';
11
+ type $$ComponentProps = Omit<HTMLAttributes<HTMLElement>, 'children' | 'class'> & {
12
+ /** The conversations, in the order to show them. */
13
+ sessions: ChatSession[];
14
+ /** The `id` of the open conversation (`aria-current`). Bindable; set when the reader picks one. */
15
+ selected?: string;
16
+ /** Whether archived conversations are listed. Bindable, driven by the "Show archived" switch; the switch only appears when something is archived. */
17
+ showArchived?: boolean;
18
+ /** The reader picked a conversation. */
19
+ onselect?: (id: string) => void;
20
+ /** The reader renamed a conversation, inline (double-click, F2) or from its menu. Receives the trimmed new title; a blank or unchanged title is dropped. You update `sessions`. */
21
+ onrename?: (id: string, title: string) => void;
22
+ /** The reader archived (`true`) or unarchived (`false`) a conversation from its menu. You update `sessions`; an archived one disappears unless `showArchived`. */
23
+ onarchive?: (id: string, archived: boolean) => void;
24
+ /** Accessible name of the list and its heading. */
25
+ label?: string;
26
+ /** Text of the switch that includes archived conversations. */
27
+ archivedLabel?: string;
28
+ /** Badge on an archived conversation. */
29
+ archivedText?: string;
30
+ /** Text of the Rename menu entry (and the name of the rename field). */
31
+ renameLabel?: string;
32
+ /** Text of the Archive menu entry. */
33
+ archiveLabel?: string;
34
+ /** Text of the Unarchive menu entry. */
35
+ unarchiveLabel?: string;
36
+ /** Accessible name of each row's menu button, followed by the title. */
37
+ actionsLabel?: string;
38
+ /** Shown when nothing is listed. */
39
+ empty?: string;
40
+ /** Other attributes (`data-testid`, `id`, ...) go to the root `<section>`. `class` is added to it. */
41
+ class?: string;
42
+ };
43
+ declare const ChatSessions: import("svelte").Component<$$ComponentProps, {
44
+ rename: (id: string) => void;
45
+ }, "selected" | "showArchived">;
46
+ type ChatSessions = ReturnType<typeof ChatSessions>;
47
+ export default ChatSessions;
@@ -10,6 +10,8 @@
10
10
  follow = $bindable(true),
11
11
  showReasoning = $bindable(false),
12
12
  reasoningStorageKey,
13
+ code,
14
+ link,
13
15
  class: klass = '',
14
16
  children,
15
17
  ...rest
@@ -30,6 +32,10 @@
30
32
  * Unavailable, blocked or throwing storage and unrecognised stored values are ignored. Changing `showReasoning` from code does not write to storage.
31
33
  */
32
34
  reasoningStorageKey?: string;
35
+ /** The default for every `ChatMessage`'s `code` snippet: renders a fenced code block in its Markdown (assistant text, reasoning), e.g. with `truss-code` or a copy button. A message's own `code` wins. */
36
+ code?: Snippet<[{ lang: string; text: string }]>;
37
+ /** The default for every `ChatMessage`'s `link` snippet: renders a Markdown link, e.g. for client-side routing. A message's own `link` wins. */
38
+ link?: Snippet<[{ href: string; children: Snippet }]>;
33
39
  /** Other attributes (`data-testid`, `id`, `style`, ...) go to the root `<div>`. `class` is added to it. */
34
40
  class?: string;
35
41
  /** The messages: `ChatMessage`, `ChatStatus` and anything else. A `ChatMessage` inside reports its finished reply to the thread's live region. */
@@ -48,6 +54,12 @@
48
54
  clearTimeout(timer);
49
55
  timer = setTimeout(() => (live = text), 50);
50
56
  },
57
+ get code() {
58
+ return code;
59
+ },
60
+ get link() {
61
+ return link;
62
+ },
51
63
  get showReasoning() {
52
64
  return showReasoning;
53
65
  },
@@ -17,6 +17,16 @@ type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class
17
17
  * Unavailable, blocked or throwing storage and unrecognised stored values are ignored. Changing `showReasoning` from code does not write to storage.
18
18
  */
19
19
  reasoningStorageKey?: string;
20
+ /** The default for every `ChatMessage`'s `code` snippet: renders a fenced code block in its Markdown (assistant text, reasoning), e.g. with `truss-code` or a copy button. A message's own `code` wins. */
21
+ code?: Snippet<[{
22
+ lang: string;
23
+ text: string;
24
+ }]>;
25
+ /** The default for every `ChatMessage`'s `link` snippet: renders a Markdown link, e.g. for client-side routing. A message's own `link` wins. */
26
+ link?: Snippet<[{
27
+ href: string;
28
+ children: Snippet;
29
+ }]>;
20
30
  /** Other attributes (`data-testid`, `id`, `style`, ...) go to the root `<div>`. `class` is added to it. */
21
31
  class?: string;
22
32
  /** The messages: `ChatMessage`, `ChatStatus` and anything else. A `ChatMessage` inside reports its finished reply to the thread's live region. */
@@ -0,0 +1,55 @@
1
+ <script lang="ts">
2
+ import { tick, type Snippet } from 'svelte';
3
+ import type { HTMLAttributes } from 'svelte/elements';
4
+ import { Button } from '@ruckc/truss-ui';
5
+ import RenameField from './internal/RenameField.svelte';
6
+
7
+ let {
8
+ title,
9
+ onrename,
10
+ renameLabel = 'Rename',
11
+ actions,
12
+ class: klass = '',
13
+ ...rest
14
+ }: Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'title'> & {
15
+ /** The conversation's title. */
16
+ title: string;
17
+ /** The reader renamed it (the pencil button, a double-click on the title, or `rename()`). Receives the trimmed title; blank or unchanged is dropped. Without it the title is read-only and has no rename button. */
18
+ onrename?: (title: string) => void;
19
+ /** Accessible name of the rename button and field. */
20
+ renameLabel?: string;
21
+ /** Buttons or a menu placed after the title (archive, share, ...). */
22
+ actions?: Snippet;
23
+ /** Other attributes (`data-testid`, `id`, ...) go to the root `<div>`. `class` is added to it. */
24
+ class?: string;
25
+ } = $props();
26
+
27
+ let editing = $state(false);
28
+ let root: HTMLDivElement;
29
+ /** Start renaming in place. Does nothing without `onrename`. */
30
+ export function rename() {
31
+ if (onrename) editing = true;
32
+ }
33
+ async function stop(text?: string) {
34
+ if (text !== undefined) onrename?.(text);
35
+ editing = false;
36
+ await tick();
37
+ root.querySelector<HTMLElement>('.edit')?.focus();
38
+ }
39
+ </script>
40
+
41
+ <div {...rest} bind:this={root} class={['row', klass]}>
42
+ {#if editing}
43
+ <RenameField value={title} label={renameLabel} oncommit={stop} oncancel={() => stop()} />
44
+ {:else}
45
+ <span class="text" role="heading" aria-level="2" ondblclick={rename}>{title}</span>
46
+ {#if onrename}<Button class="edit" variant="minimal" size="sm" icon="edit" aria-label={renameLabel} onclick={rename} />{/if}
47
+ {/if}
48
+ {#if actions}<div class="actions">{@render actions()}</div>{/if}
49
+ </div>
50
+
51
+ <style>
52
+ .row { display: flex; align-items: center; gap: var(--tr-space-1); min-width: 0; }
53
+ .text { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
54
+ .actions { display: flex; align-items: center; gap: var(--tr-space-1); margin-inline-start: auto; flex: none; }
55
+ </style>
@@ -0,0 +1,19 @@
1
+ import { type Snippet } from 'svelte';
2
+ import type { HTMLAttributes } from 'svelte/elements';
3
+ type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class' | 'title'> & {
4
+ /** The conversation's title. */
5
+ title: string;
6
+ /** The reader renamed it (the pencil button, a double-click on the title, or `rename()`). Receives the trimmed title; blank or unchanged is dropped. Without it the title is read-only and has no rename button. */
7
+ onrename?: (title: string) => void;
8
+ /** Accessible name of the rename button and field. */
9
+ renameLabel?: string;
10
+ /** Buttons or a menu placed after the title (archive, share, ...). */
11
+ actions?: Snippet;
12
+ /** Other attributes (`data-testid`, `id`, ...) go to the root `<div>`. `class` is added to it. */
13
+ class?: string;
14
+ };
15
+ declare const ChatTitle: import("svelte").Component<$$ComponentProps, {
16
+ rename: () => void;
17
+ }, "">;
18
+ type ChatTitle = ReturnType<typeof ChatTitle>;
19
+ export default ChatTitle;
@@ -4,7 +4,7 @@
4
4
  import { Button, Dialog, Icon, Tooltip } from '@ruckc/truss-ui';
5
5
  import { formatValue, previewInput } from './chat.js';
6
6
 
7
- type Status = 'pending' | 'in_progress' | 'completed' | 'failed';
7
+ type Status = 'pending' | 'awaiting' | 'in_progress' | 'completed' | 'failed';
8
8
 
9
9
  let {
10
10
  name,
@@ -12,6 +12,7 @@
12
12
  variant = 'row',
13
13
  formatName,
14
14
  labels,
15
+ summary,
15
16
  input,
16
17
  output,
17
18
  error,
@@ -25,14 +26,16 @@
25
26
  }: Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class'> & {
26
27
  /** The tool's display name. Pass a raw identifier together with `formatName` to tidy it. */
27
28
  name: string;
28
- /** Where the call is. Shown as an icon (a spinner while `in_progress`) and always as text (the row's label, the chip's accessible name and tooltip), never by colour alone. */
29
+ /** Where the call is: `pending` (queued), `awaiting` (waiting for the user to confirm or approve it), `in_progress`, `completed` or `failed`. Shown as an icon (a spinner while `in_progress`, a question mark while `awaiting`) and always as text (the row's label, the chip's accessible name and tooltip), never by colour alone. */
29
30
  status?: Status;
30
31
  /** `row` (default) is a full-width line with the status text, for a list of calls. `chip` is a compact pill coloured by status with a tooltip, for many calls side by side (wrap them in `ToolCalls`). */
31
32
  variant?: 'row' | 'chip';
32
33
  /** Turns `name` into what is displayed, e.g. `formatToolName`, which strips an `mcp__server__` prefix and replaces underscores with spaces (`mcp__github__create_issue` becomes `create issue`). */
33
34
  formatName?: (name: string) => string;
34
- /** Replaces the status texts, e.g. for another language. Missing keys keep the defaults: Pending, Running, Done, Failed. */
35
+ /** Replaces the status texts, e.g. for another language. Missing keys keep the defaults: Pending, Awaiting confirmation, Running, Done, Failed. */
35
36
  labels?: Partial<Record<Status, string>>;
37
+ /** A one-line result for the row, e.g. "3 rows" or the error message: shown between the name and the status, muted and cut with an ellipsis (the full text is its tooltip and is repeated in the details). In a chip it is added to the tooltip and the accessible name. */
38
+ summary?: string;
36
39
  /** What the tool was called with: a string or any JSON-able value. Shown as a one-line preview in the chip's tooltip (see `previewInput`) and in full, pretty-printed, in the details. */
37
40
  input?: unknown;
38
41
  /** What the tool returned: a string or any JSON-able value. Pretty-printed in the details. */
@@ -59,8 +62,8 @@
59
62
  } = $props();
60
63
 
61
64
  const uid = $props.id();
62
- const DEFAULTS: Record<Status, string> = { pending: 'Pending', in_progress: 'Running', completed: 'Done', failed: 'Failed' };
63
- const ICONS = { pending: 'clock', in_progress: 'refresh', completed: 'success', failed: 'error' } as const;
65
+ const DEFAULTS: Record<Status, string> = { pending: 'Pending', awaiting: 'Awaiting confirmation', in_progress: 'Running', completed: 'Done', failed: 'Failed' };
66
+ const ICONS = { pending: 'clock', awaiting: 'help', in_progress: 'refresh', completed: 'success', failed: 'error' } as const;
64
67
  const shown = $derived(formatName ? formatName(name) : name);
65
68
  const statusText = $derived(labels?.[status] ?? DEFAULTS[status]);
66
69
 
@@ -85,7 +88,8 @@
85
88
  };
86
89
  const hasBody = $derived(!!custom || sections.length > 0);
87
90
  const preview = $derived(previewInput(input));
88
- const tip = $derived(preview ? `${statusText}: ${preview}` : statusText);
91
+ const tip = $derived(summary ? `${statusText}: ${summary}` : preview ? `${statusText}: ${preview}` : statusText);
92
+ const spoken = $derived(summary ? `${shown}, ${statusText}, ${summary}` : `${shown}, ${statusText}`);
89
93
 
90
94
  // the dialog is created on first use, so a long list of calls does not render every call's details up front
91
95
  let mounted = $state(false);
@@ -122,6 +126,7 @@
122
126
 
123
127
  {#snippet body()}
124
128
  <p class="line">Status: <strong>{statusText}</strong></p>
129
+ {#if summary}<p class="line">Result: <strong>{summary}</strong></p>{/if}
125
130
  {#if custom}{@render custom()}{/if}
126
131
  {#each sections as s (s.key)}
127
132
  {@const c = cut(s.text, s.key)}
@@ -154,7 +159,7 @@
154
159
  <button
155
160
  type="button"
156
161
  class="chip"
157
- aria-label="{shown}, {statusText}"
162
+ aria-label={spoken}
158
163
  aria-haspopup={mode === 'dialog' ? 'dialog' : undefined}
159
164
  aria-expanded={mode === 'inline' ? open : undefined}
160
165
  aria-controls={mode === 'inline' ? `${uid}-details` : undefined}
@@ -163,7 +168,7 @@
163
168
  {@render lead()}<span class="name">{shown}</span>
164
169
  </button>
165
170
  {:else}
166
- <span class="chip" role="group" aria-label="{shown}, {statusText}">{@render lead()}<span class="name">{shown}</span></span>
171
+ <span class="chip" role="group" aria-label={spoken}>{@render lead()}<span class="name">{shown}</span></span>
167
172
  {/if}
168
173
  </Tooltip>
169
174
  {:else if hasBody}
@@ -175,11 +180,11 @@
175
180
  aria-controls={mode === 'inline' ? `${uid}-details` : undefined}
176
181
  onclick={() => (open = !open)}
177
182
  >
178
- {@render lead()}<span class="name">{shown}</span><span class="status">{statusText}</span>
183
+ {@render lead()}<span class="name">{shown}</span>{#if summary}<span class="summary" title={summary}>{summary}</span>{/if}<span class="status">{statusText}</span>
179
184
  {#if mode === 'inline'}<span class="chev" data-open={open}><Icon name="chevron-down" /></span>{/if}
180
185
  </button>
181
186
  {:else}
182
- <div class="row">{@render lead()}<span class="name">{shown}</span><span class="status">{statusText}</span></div>
187
+ <div class="row">{@render lead()}<span class="name">{shown}</span>{#if summary}<span class="summary" title={summary}>{summary}</span>{/if}<span class="status">{statusText}</span></div>
183
188
  {/if}
184
189
  {#if hasBody && mode === 'inline' && open}
185
190
  <div class="details" id="{uid}-details">{@render body()}</div>
@@ -205,8 +210,11 @@
205
210
  .icon { display: inline-flex; flex: none; color: var(--tr-on-surface-variant); }
206
211
  [data-status='completed'] .icon { color: var(--tr-success); }
207
212
  [data-status='failed'] .icon { color: var(--tr-danger); }
213
+ [data-status='awaiting'] .icon { color: var(--tr-warning); }
208
214
  [data-status='in_progress'] .icon { color: var(--tr-primary); }
209
215
  .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
216
+ .row .name:has(+ .summary) { flex: 0 1 auto; }
217
+ .summary { flex: 1 1 0; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--tr-on-surface-variant); }
210
218
  .status { flex: none; color: var(--tr-on-surface-variant); }
211
219
  [data-status='failed'] .status { color: var(--tr-danger); font-weight: 600; }
212
220
  .chev { display: inline-flex; flex: none; color: var(--tr-on-surface-variant); transition: transform var(--tr-dur) var(--tr-ease); }
@@ -224,6 +232,7 @@
224
232
  [data-status='in_progress'] .chip { background: var(--tr-primary-container); color: var(--tr-on-primary-container); border-color: transparent; }
225
233
  [data-status='completed'] .chip { background: var(--tr-success-container); color: var(--tr-on-success-container); border-color: transparent; }
226
234
  [data-status='failed'] .chip { background: var(--tr-danger-container); color: var(--tr-on-danger-container); border-color: transparent; }
235
+ [data-status='awaiting'] .chip { background: var(--tr-warning-container); color: var(--tr-on-warning-container); border-color: transparent; }
227
236
  [data-status='pending'] .chip { color: var(--tr-on-surface-variant); border-style: dashed; }
228
237
 
229
238
  .line { margin: 0 0 var(--tr-space-2); color: var(--tr-on-surface-variant); }
@@ -1,17 +1,19 @@
1
1
  import { type Snippet } from 'svelte';
2
2
  import type { HTMLAttributes } from 'svelte/elements';
3
- type Status = 'pending' | 'in_progress' | 'completed' | 'failed';
3
+ type Status = 'pending' | 'awaiting' | 'in_progress' | 'completed' | 'failed';
4
4
  type $$ComponentProps = Omit<HTMLAttributes<HTMLDivElement>, 'children' | 'class'> & {
5
5
  /** The tool's display name. Pass a raw identifier together with `formatName` to tidy it. */
6
6
  name: string;
7
- /** Where the call is. Shown as an icon (a spinner while `in_progress`) and always as text (the row's label, the chip's accessible name and tooltip), never by colour alone. */
7
+ /** Where the call is: `pending` (queued), `awaiting` (waiting for the user to confirm or approve it), `in_progress`, `completed` or `failed`. Shown as an icon (a spinner while `in_progress`, a question mark while `awaiting`) and always as text (the row's label, the chip's accessible name and tooltip), never by colour alone. */
8
8
  status?: Status;
9
9
  /** `row` (default) is a full-width line with the status text, for a list of calls. `chip` is a compact pill coloured by status with a tooltip, for many calls side by side (wrap them in `ToolCalls`). */
10
10
  variant?: 'row' | 'chip';
11
11
  /** Turns `name` into what is displayed, e.g. `formatToolName`, which strips an `mcp__server__` prefix and replaces underscores with spaces (`mcp__github__create_issue` becomes `create issue`). */
12
12
  formatName?: (name: string) => string;
13
- /** Replaces the status texts, e.g. for another language. Missing keys keep the defaults: Pending, Running, Done, Failed. */
13
+ /** Replaces the status texts, e.g. for another language. Missing keys keep the defaults: Pending, Awaiting confirmation, Running, Done, Failed. */
14
14
  labels?: Partial<Record<Status, string>>;
15
+ /** A one-line result for the row, e.g. "3 rows" or the error message: shown between the name and the status, muted and cut with an ellipsis (the full text is its tooltip and is repeated in the details). In a chip it is added to the tooltip and the accessible name. */
16
+ summary?: string;
15
17
  /** What the tool was called with: a string or any JSON-able value. Shown as a one-line preview in the chip's tooltip (see `previewInput`) and in full, pretty-printed, in the details. */
16
18
  input?: unknown;
17
19
  /** What the tool returned: a string or any JSON-able value. Pretty-printed in the details. */
package/dist/index.d.ts CHANGED
@@ -4,6 +4,9 @@ export { default as ToolCall } from './ToolCall.svelte';
4
4
  export { default as ToolCalls } from './ToolCalls.svelte';
5
5
  export { default as ChatComposer } from './ChatComposer.svelte';
6
6
  export { default as ChatStatus } from './ChatStatus.svelte';
7
+ export { default as ChatSessions } from './ChatSessions.svelte';
8
+ export type { ChatSession } from './ChatSessions.svelte';
9
+ export { default as ChatTitle } from './ChatTitle.svelte';
7
10
  export { default as QuestionForm } from './QuestionForm.svelte';
8
11
  export { default as QuestionAnswers } from './QuestionAnswers.svelte';
9
12
  export type { Question, QuestionAnswer } from './QuestionForm.svelte';
package/dist/index.js CHANGED
@@ -4,6 +4,8 @@ export { default as ToolCall } from './ToolCall.svelte';
4
4
  export { default as ToolCalls } from './ToolCalls.svelte';
5
5
  export { default as ChatComposer } from './ChatComposer.svelte';
6
6
  export { default as ChatStatus } from './ChatStatus.svelte';
7
+ export { default as ChatSessions } from './ChatSessions.svelte';
8
+ export { default as ChatTitle } from './ChatTitle.svelte';
7
9
  export { default as QuestionForm } from './QuestionForm.svelte';
8
10
  export { default as QuestionAnswers } from './QuestionAnswers.svelte';
9
11
  export { formatToolName, previewInput, formatValue } from './chat.js';
@@ -0,0 +1,55 @@
1
+ <script lang="ts">
2
+ import { untrack } from 'svelte';
3
+ import { Input } from '@ruckc/truss-ui';
4
+
5
+ let {
6
+ value,
7
+ label,
8
+ oncommit,
9
+ oncancel
10
+ }: {
11
+ /** The text to start from. */
12
+ value: string;
13
+ /** Accessible name of the field. */
14
+ label: string;
15
+ /** Enter or leaving the field with a changed, non-blank text. */
16
+ oncommit: (text: string) => void;
17
+ /** Escape, or leaving the field with nothing new (blank or unchanged). */
18
+ oncancel: () => void;
19
+ } = $props();
20
+
21
+ const start = untrack(() => value);
22
+ let draft = $state(start);
23
+ let done = false;
24
+ function finish(commit: boolean) {
25
+ if (done) return;
26
+ done = true;
27
+ const text = draft.trim();
28
+ if (commit && text && text !== start) oncommit(text);
29
+ else oncancel();
30
+ }
31
+ const focus = (el: HTMLElement) => {
32
+ const input = el.querySelector('input')!;
33
+ input.focus();
34
+ input.select();
35
+ };
36
+ function onkeydown(e: KeyboardEvent) {
37
+ if (e.isComposing || e.keyCode === 229) return;
38
+ if (e.key === 'Enter') {
39
+ e.preventDefault();
40
+ finish(true);
41
+ } else if (e.key === 'Escape') {
42
+ e.preventDefault();
43
+ e.stopPropagation();
44
+ finish(false);
45
+ }
46
+ }
47
+ </script>
48
+
49
+ <div class="rename" {@attach focus}>
50
+ <Input bind:value={draft} size="sm" aria-label={label} {onkeydown} onblur={() => finish(true)} />
51
+ </div>
52
+
53
+ <style>
54
+ .rename { flex: 1; min-width: 0; }
55
+ </style>
@@ -0,0 +1,13 @@
1
+ type $$ComponentProps = {
2
+ /** The text to start from. */
3
+ value: string;
4
+ /** Accessible name of the field. */
5
+ label: string;
6
+ /** Enter or leaving the field with a changed, non-blank text. */
7
+ oncommit: (text: string) => void;
8
+ /** Escape, or leaving the field with nothing new (blank or unchanged). */
9
+ oncancel: () => void;
10
+ };
11
+ declare const RenameField: import("svelte").Component<$$ComponentProps, {}, "">;
12
+ type RenameField = ReturnType<typeof RenameField>;
13
+ export default RenameField;
@@ -1,3 +1,4 @@
1
+ import { type Snippet } from 'svelte';
1
2
  /** What a ChatThread offers the messages inside it. */
2
3
  export type ChatThreadContext = {
3
4
  /** Queue text for the thread's polite live region. */
@@ -6,6 +7,16 @@ export type ChatThreadContext = {
6
7
  readonly showReasoning: boolean;
7
8
  /** The user chose to show or hide reasoning: update the thread setting (and its storage). */
8
9
  setShowReasoning: (show: boolean) => void;
10
+ /** The thread-wide default for rendering fenced code blocks in Markdown. */
11
+ readonly code?: Snippet<[{
12
+ lang: string;
13
+ text: string;
14
+ }]>;
15
+ /** The thread-wide default for rendering Markdown links. */
16
+ readonly link?: Snippet<[{
17
+ href: string;
18
+ children: Snippet;
19
+ }]>;
9
20
  };
10
21
  export declare const provideThread: (c: ChatThreadContext) => ChatThreadContext;
11
22
  export declare const useThread: () => ChatThreadContext | undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-chat",
3
- "version": "1.34.0",
3
+ "version": "1.36.0",
4
4
  "description": "Chat building blocks for Truss UI: thread, messages, tool calls, composer, status line and a question form",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -27,7 +27,7 @@
27
27
  "@ruckc/truss-ui": "^1.0.0"
28
28
  },
29
29
  "dependencies": {
30
- "@ruckc/truss-markdown": "^1.34.0"
30
+ "@ruckc/truss-markdown": "^1.36.0"
31
31
  },
32
32
  "author": "Curtis Ruck",
33
33
  "homepage": "https://truss.ruck.io",