@ruckc/truss-chat 1.49.0 → 1.50.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.
@@ -14,6 +14,8 @@
14
14
 
15
15
  <script lang="ts">
16
16
  import { useLabels } from './labels.js';
17
+ import { parseQuestion } from './internal/question-text.js';
18
+ import QuestionBody from './internal/QuestionBody.svelte';
17
19
  import type { HTMLAttributes } from 'svelte/elements';
18
20
  import type { Question, QuestionAnswer } from './QuestionForm.svelte';
19
21
 
@@ -58,8 +60,9 @@
58
60
  <dl class="list">
59
61
  {#each questions as item, i (i)}
60
62
  {@const shown = parts(answers[i])}
63
+ {@const pq = parseQuestion(item.question)}
61
64
  <div class="item">
62
- <dt class="q">{#if item.header}<span class="header">{item.header}</span>{/if}{item.question}</dt>
65
+ <dt class="q">{#if item.header}<span class="header">{item.header}</span>{/if}{pq.title}<QuestionBody blocks={pq.body} /></dt>
63
66
  {#if shown.length}
64
67
  {#each shown as s, j (j)}<dd class="a">{s}</dd>{/each}
65
68
  {:else}
@@ -3,6 +3,7 @@
3
3
  export type Question = {
4
4
  /** A short topic shown above the question, e.g. "Auth method". */
5
5
  header?: string;
6
+ /** The question. Line breaks are kept: the first line is the heading (it names the question), later lines are body text below it, and lines starting with `- ` or `* ` form a list. Plain text; no Markdown is parsed. */
6
7
  question: string;
7
8
  options: { label: string; description?: string; /** Marks the suggested choice: " (Recommended)" is appended to its label (the answer still holds the plain label). */ recommended?: boolean }[];
8
9
  /** Several options can be chosen. Otherwise exactly one. */
@@ -75,6 +76,8 @@
75
76
 
76
77
  <script lang="ts">
77
78
  import { useLabels } from './labels.js';
79
+ import { parseQuestion } from './internal/question-text.js';
80
+ import QuestionBody from './internal/QuestionBody.svelte';
78
81
  import { tick, untrack, type Snippet } from 'svelte';
79
82
  import type { HTMLAttributes } from 'svelte/elements';
80
83
  import { Button, Checkbox, Icon, Kbd, RadioGroup, Textarea } from '@ruckc/truss-ui';
@@ -152,6 +155,7 @@
152
155
  const lastPage = $derived(page === total - 1);
153
156
  const reviewing = $derived(withReview && page === total);
154
157
  const q = $derived(qs[page]);
158
+ const parsed = $derived(parseQuestion(q.question));
155
159
  const isAnswered = (i: number) => picked[i].length > 0 || others[i].trim() !== '';
156
160
  const canNext = $derived(isAnswered(page) || !!q.optional);
157
161
 
@@ -268,9 +272,10 @@
268
272
 
269
273
  {#snippet question()}
270
274
  {#if q.header}<p class="header">{q.header}</p>{/if}
271
- <h3 class="title" id="{uid}-q" tabindex="-1" bind:this={head}>{q.question}</h3>
275
+ <h3 class="title" id="{uid}-q" tabindex="-1" bind:this={head}>{parsed.title}</h3>
276
+ <QuestionBody blocks={parsed.body} id="{uid}-qd" />
272
277
  {#if q.multiSelect}
273
- <fieldset class="opts" aria-labelledby="{uid}-q" {@attach arrows}>
278
+ <fieldset class="opts" aria-labelledby="{uid}-q" aria-describedby={parsed.body.length ? `${uid}-qd` : undefined} {@attach arrows}>
274
279
  {#each q.options as o (o.label)}
275
280
  <Checkbox label={optionLabel(o)} description={o.description} bind:checked={() => picked[page].includes(o.label), (on) => toggle(page, o.label, on)} />
276
281
  {/each}
@@ -278,6 +283,7 @@
278
283
  {:else}
279
284
  <RadioGroup
280
285
  aria-labelledby="{uid}-q"
286
+ aria-describedby={parsed.body.length ? `${uid}-qd` : undefined}
281
287
  options={q.options.map((o) => ({ value: o.label, label: optionLabel(o), description: o.description }))}
282
288
  bind:value={() => picked[page][0], (v) => setSingle(page, v as string)}
283
289
  />
@@ -304,12 +310,14 @@
304
310
  <h3 class="title" tabindex="-1" bind:this={head}>{L.reviewTitle}</h3>
305
311
  <ol class="review">
306
312
  {#each qs as item, i (i)}
313
+ {@const pq = parseQuestion(item.question)}
307
314
  <li>
308
315
  <div class="ra">
309
- <span class="rq">{item.question}</span>
316
+ <span class="rq">{pq.title}</span>
317
+ <QuestionBody blocks={pq.body} />
310
318
  <span class="rv">{summary(answers[i])}</span>
311
319
  </div>
312
- <Button size="sm" variant="minimal" onclick={() => edit(i)} aria-label={L.editAnswer(item.question)}>{L.edit}</Button>
320
+ <Button size="sm" variant="minimal" onclick={() => edit(i)} aria-label={L.editAnswer(pq.title)}>{L.edit}</Button>
313
321
  </li>
314
322
  {/each}
315
323
  </ol>
@@ -2,6 +2,7 @@
2
2
  export type Question = {
3
3
  /** A short topic shown above the question, e.g. "Auth method". */
4
4
  header?: string;
5
+ /** The question. Line breaks are kept: the first line is the heading (it names the question), later lines are body text below it, and lines starting with `- ` or `* ` form a list. Plain text; no Markdown is parsed. */
5
6
  question: string;
6
7
  options: {
7
8
  label: string;
@@ -0,0 +1,23 @@
1
+ <script lang="ts">
2
+ import type { QuestionBlock } from './question-text.js';
3
+
4
+ let { blocks, id }: { blocks: QuestionBlock[]; id?: string } = $props();
5
+ </script>
6
+
7
+ {#if blocks.length}
8
+ <div class="qbody" {id}>
9
+ {#each blocks as b, i (i)}
10
+ {#if 'ul' in b}
11
+ <ul>{#each b.ul as li, j (j)}<li>{li}</li>{/each}</ul>
12
+ {:else}
13
+ <p>{b.p}</p>
14
+ {/if}
15
+ {/each}
16
+ </div>
17
+ {/if}
18
+
19
+ <style>
20
+ .qbody { display: flex; flex-direction: column; gap: var(--tr-space-1); min-width: 0; overflow-wrap: anywhere; font-weight: 400; font-size: var(--tr-text-base, 1em); }
21
+ .qbody p, .qbody ul { margin: 0; }
22
+ .qbody ul { padding-inline-start: 1.25em; }
23
+ </style>
@@ -0,0 +1,8 @@
1
+ import type { QuestionBlock } from './question-text.js';
2
+ type $$ComponentProps = {
3
+ blocks: QuestionBlock[];
4
+ id?: string;
5
+ };
6
+ declare const QuestionBody: import("svelte").Component<$$ComponentProps, {}, "">;
7
+ type QuestionBody = ReturnType<typeof QuestionBody>;
8
+ export default QuestionBody;
@@ -0,0 +1,15 @@
1
+ /** A line-structured question: the first line is the heading, the rest is body text. */
2
+ export type QuestionBlock = {
3
+ p: string;
4
+ } | {
5
+ ul: string[];
6
+ };
7
+ export type QuestionText = {
8
+ title: string;
9
+ body: QuestionBlock[];
10
+ };
11
+ /**
12
+ * Splits a question on line breaks. The first non-blank line is the title; each following line is a paragraph,
13
+ * and a run of `- item` / `* item` lines is one list. Blank lines are dropped. A single-line question has no body.
14
+ */
15
+ export declare function parseQuestion(text: string): QuestionText;
@@ -0,0 +1,23 @@
1
+ const ITEM = /^[-*]\s+(.*)$/;
2
+ /**
3
+ * Splits a question on line breaks. The first non-blank line is the title; each following line is a paragraph,
4
+ * and a run of `- item` / `* item` lines is one list. Blank lines are dropped. A single-line question has no body.
5
+ */
6
+ export function parseQuestion(text) {
7
+ const lines = text.split(/\r\n|\r|\n/).map((l) => l.trim()).filter(Boolean);
8
+ const title = lines.shift() ?? '';
9
+ const body = [];
10
+ for (const line of lines) {
11
+ const item = ITEM.exec(line);
12
+ const last = body[body.length - 1];
13
+ if (item) {
14
+ if (last && 'ul' in last)
15
+ last.ul.push(item[1]);
16
+ else
17
+ body.push({ ul: [item[1]] });
18
+ }
19
+ else
20
+ body.push({ p: line });
21
+ }
22
+ return { title, body };
23
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ruckc/truss-chat",
3
- "version": "1.49.0",
3
+ "version": "1.50.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.49.0"
30
+ "@ruckc/truss-markdown": "^1.50.0"
31
31
  },
32
32
  "author": "Curtis Ruck",
33
33
  "homepage": "https://truss.ruck.io",