@kitnai/chat 0.6.0 → 0.8.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 +9 -9
- package/dist/custom-elements.json +1676 -881
- package/dist/kitn-chat.es.js +36 -36
- package/dist/llms/llms-full.txt +316 -155
- package/dist/llms/llms.txt +18 -18
- package/dist/schemas/card-envelope.schema.json +14 -0
- package/dist/schemas/card-event.schema.json +12 -0
- package/dist/schemas/confirm.schema.json +65 -0
- package/dist/schemas/embed.schema.json +65 -0
- package/dist/schemas/form.result.schema.json +7 -0
- package/dist/schemas/form.schema.json +33 -0
- package/dist/schemas/link.schema.json +56 -0
- package/dist/schemas/task-list.result.schema.json +16 -0
- package/dist/schemas/task-list.schema.json +78 -0
- package/dist/theme.tokens.css +65 -65
- package/dist/tsx-B8rCNbgL.js +1 -0
- package/dist/typescript-RycA9KXf.js +1 -0
- package/frameworks/react/index.tsx +382 -193
- package/frameworks/react/runtime.tsx +2 -2
- package/llms-full.txt +316 -155
- package/llms.txt +18 -18
- package/package.json +5 -2
- package/src/components/artifact.stories.tsx +138 -0
- package/src/components/artifact.tsx +581 -0
- package/src/components/attachments.stories.tsx +7 -8
- package/src/components/attachments.tsx +2 -2
- package/src/components/card.tsx +110 -0
- package/src/components/chain-of-thought.stories.tsx +7 -8
- package/src/components/chat-container.stories.tsx +7 -8
- package/src/components/chat-container.tsx +4 -0
- package/src/components/checkpoint.stories.tsx +7 -8
- package/src/components/code-block.stories.tsx +8 -9
- package/src/components/component-meta.json +3411 -0
- package/src/components/confirm-card.stories.tsx +74 -0
- package/src/components/confirm-card.tsx +299 -0
- package/src/components/context.stories.tsx +7 -8
- package/src/components/conversation-item.stories.tsx +7 -8
- package/src/components/conversation-item.tsx +2 -2
- package/src/components/conversation-list.stories.tsx +7 -8
- package/src/components/conversation-list.tsx +1 -1
- package/src/components/embed.tsx +196 -0
- package/src/components/empty.stories.tsx +8 -9
- package/src/components/feedback-bar.stories.tsx +7 -8
- package/src/components/file-tree.stories.tsx +73 -0
- package/src/components/file-tree.tsx +383 -0
- package/src/components/file-upload.stories.tsx +7 -8
- package/src/components/form-widgets.tsx +461 -0
- package/src/components/form.tsx +796 -0
- package/src/components/image.stories.tsx +7 -8
- package/src/components/link-card.tsx +194 -0
- package/src/components/loader.stories.tsx +7 -8
- package/src/components/markdown.stories.tsx +7 -8
- package/src/components/message-narrow.stories.tsx +12 -13
- package/src/components/message-skills.stories.tsx +16 -17
- package/src/components/message.stories.tsx +17 -18
- package/src/components/model-switcher.stories.tsx +7 -8
- package/src/components/prompt-input.stories.tsx +8 -9
- package/src/components/prompt-suggestion.stories.tsx +7 -8
- package/src/components/prompt-suggestion.tsx +3 -3
- package/src/components/reasoning.stories.tsx +7 -8
- package/src/components/scroll-button.stories.tsx +7 -8
- package/src/components/slash-command.stories.tsx +8 -9
- package/src/components/slash-command.tsx +2 -2
- package/src/components/source.stories.tsx +7 -8
- package/src/components/source.tsx +1 -1
- package/src/components/task-list-card.stories.tsx +78 -0
- package/src/components/task-list-card.tsx +388 -0
- package/src/components/text-shimmer.stories.tsx +7 -8
- package/src/components/thinking-bar.stories.tsx +7 -8
- package/src/components/tool.stories.tsx +7 -8
- package/src/components/tool.tsx +2 -2
- package/src/components/voice-input.stories.tsx +7 -8
- package/src/elements/artifact.stories.tsx +291 -0
- package/src/elements/artifact.tsx +72 -0
- package/src/elements/{kitn-attachments.stories.tsx → attachments.stories.tsx} +11 -11
- package/src/elements/attachments.tsx +4 -4
- package/src/elements/card.stories.tsx +118 -0
- package/src/elements/card.tsx +40 -0
- package/src/elements/catalog.stories.tsx +491 -0
- package/src/elements/{kitn-chain-of-thought.stories.tsx → chain-of-thought.stories.tsx} +13 -13
- package/src/elements/chain-of-thought.tsx +3 -3
- package/src/elements/{kitn-chat-scope-picker.stories.tsx → chat-scope-picker.stories.tsx} +10 -10
- package/src/elements/chat-scope-picker.tsx +4 -4
- package/src/elements/{kitn-chat-workspace.stories.tsx → chat-workspace.stories.tsx} +71 -29
- package/src/elements/chat-workspace.tsx +29 -3
- package/src/elements/{kitn-chat.stories.tsx → chat.stories.tsx} +61 -16
- package/src/elements/chat.tsx +23 -2
- package/src/elements/{kitn-checkpoint.stories.tsx → checkpoint.stories.tsx} +11 -11
- package/src/elements/checkpoint.tsx +4 -4
- package/src/elements/{kitn-code-block.stories.tsx → code-block.stories.tsx} +10 -10
- package/src/elements/code-block.tsx +3 -3
- package/src/elements/compiled.css +1 -1
- package/src/elements/composed-shell.stories.tsx +316 -0
- package/src/elements/confirm-card.stories.tsx +186 -0
- package/src/elements/confirm-card.tsx +45 -0
- package/src/elements/{kitn-context-meter.stories.tsx → context-meter.stories.tsx} +10 -10
- package/src/elements/context-meter.tsx +3 -3
- package/src/elements/{kitn-conversation-list.stories.tsx → conversation-list.stories.tsx} +35 -22
- package/src/elements/conversation-list.tsx +11 -2
- package/src/elements/css.ts +1 -1
- package/src/elements/define.tsx +10 -10
- package/src/elements/element-meta.json +2649 -0
- package/src/elements/element-types.d.ts +251 -125
- package/src/elements/embed.stories.tsx +197 -0
- package/src/elements/embed.tsx +35 -0
- package/src/elements/{kitn-empty.stories.tsx → empty.stories.tsx} +12 -12
- package/src/elements/empty.tsx +3 -3
- package/src/elements/{kitn-feedback-bar.stories.tsx → feedback-bar.stories.tsx} +11 -11
- package/src/elements/feedback-bar.tsx +4 -4
- package/src/elements/file-tree.stories.tsx +133 -0
- package/src/elements/file-tree.tsx +52 -0
- package/src/elements/{kitn-file-upload.stories.tsx → file-upload.stories.tsx} +12 -12
- package/src/elements/file-upload.tsx +4 -4
- package/src/elements/form.stories.tsx +204 -0
- package/src/elements/form.tsx +37 -0
- package/src/elements/{kitn-image.stories.tsx → image.stories.tsx} +10 -10
- package/src/elements/image.tsx +3 -3
- package/src/elements/link-card.stories.tsx +193 -0
- package/src/elements/link-card.tsx +34 -0
- package/src/elements/{kitn-loader.stories.tsx → loader.stories.tsx} +11 -11
- package/src/elements/loader.tsx +3 -3
- package/src/elements/{kitn-markdown.stories.tsx → markdown.stories.tsx} +10 -10
- package/src/elements/markdown.tsx +3 -3
- package/src/elements/{kitn-message-skills.stories.tsx → message-skills.stories.tsx} +10 -10
- package/src/elements/message-skills.tsx +3 -3
- package/src/elements/{kitn-message.stories.tsx → message.stories.tsx} +12 -12
- package/src/elements/message.tsx +5 -5
- package/src/elements/{kitn-model-switcher.stories.tsx → model-switcher.stories.tsx} +10 -10
- package/src/elements/model-switcher.tsx +5 -5
- package/src/elements/{kitn-prompt-input.stories.tsx → prompt-input.stories.tsx} +41 -19
- package/src/elements/prompt-input.tsx +5 -5
- package/src/elements/{kitn-prompt-suggestions.stories.tsx → prompt-suggestions.stories.tsx} +13 -13
- package/src/elements/prompt-suggestions.tsx +4 -4
- package/src/elements/{kitn-reasoning.stories.tsx → reasoning.stories.tsx} +10 -10
- package/src/elements/reasoning.tsx +4 -4
- package/src/elements/register.ts +11 -1
- package/src/elements/resizable.stories.tsx +200 -0
- package/src/elements/resizable.tsx +264 -0
- package/src/elements/{kitn-response-stream.stories.tsx → response-stream.stories.tsx} +10 -10
- package/src/elements/response-stream.tsx +4 -4
- package/src/elements/{kitn-source-list.stories.tsx → source-list.stories.tsx} +11 -11
- package/src/elements/{kitn-source.stories.tsx → source.stories.tsx} +12 -12
- package/src/elements/source.tsx +5 -5
- package/src/elements/styles.css +140 -1
- package/src/elements/task-list-card.stories.tsx +194 -0
- package/src/elements/task-list-card.tsx +40 -0
- package/src/elements/{kitn-text-shimmer.stories.tsx → text-shimmer.stories.tsx} +10 -10
- package/src/elements/text-shimmer.tsx +3 -3
- package/src/elements/{kitn-thinking-bar.stories.tsx → thinking-bar.stories.tsx} +11 -11
- package/src/elements/thinking-bar.tsx +5 -5
- package/src/elements/{kitn-tool.stories.tsx → tool.stories.tsx} +10 -10
- package/src/elements/tool.tsx +3 -3
- package/src/elements/{kitn-voice-input.stories.tsx → voice-input.stories.tsx} +10 -10
- package/src/elements/voice-input.tsx +4 -4
- package/src/index.ts +94 -2
- package/src/primitives/card-contract.ts +60 -0
- package/src/primitives/card-host.tsx +35 -0
- package/src/primitives/card-routing.ts +79 -0
- package/src/primitives/card-schemas/card-envelope.schema.json +14 -0
- package/src/primitives/card-schemas/card-event.schema.json +12 -0
- package/src/primitives/card-schemas/confirm.schema.json +65 -0
- package/src/primitives/card-schemas/embed.schema.json +65 -0
- package/src/primitives/card-schemas/form.result.schema.json +7 -0
- package/src/primitives/card-schemas/form.schema.json +33 -0
- package/src/primitives/card-schemas/link.schema.json +56 -0
- package/src/primitives/card-schemas/task-list.result.schema.json +16 -0
- package/src/primitives/card-schemas/task-list.schema.json +78 -0
- package/src/primitives/card-validate.ts +95 -0
- package/src/primitives/embed-providers.ts +254 -0
- package/src/primitives/highlighter.ts +4 -0
- package/src/primitives/link-preview.ts +87 -0
- package/src/primitives/pdf-preview.ts +121 -0
- package/src/stories/chat-panel-layout.stories.tsx +2 -1
- package/src/stories/chat-scene.tsx +22 -21
- package/src/stories/checkpoint-restore.stories.tsx +10 -10
- package/src/stories/conversation-with-reasoning.stories.tsx +4 -4
- package/src/stories/conversation-with-sources.stories.tsx +7 -7
- package/src/stories/docs/Accessibility.mdx +2 -2
- package/src/stories/docs/ForAIAgents.mdx +3 -3
- package/src/stories/docs/GettingStarted.mdx +2 -2
- package/src/stories/docs/Installation.mdx +2 -2
- package/src/stories/docs/Integrations.mdx +29 -29
- package/src/stories/docs/Introduction.mdx +3 -3
- package/src/stories/docs/Theming.mdx +2 -2
- package/src/stories/docs/element-controls.ts +60 -0
- package/src/stories/docs/theme-editor/theme-editor.tsx +1 -0
- package/src/stories/examples/ChoosingComponents.mdx +94 -0
- package/src/stories/examples/sample-data.ts +79 -0
- package/src/stories/message-actions.stories.tsx +13 -13
- package/src/stories/pattern-centered-conversation.stories.tsx +3 -3
- package/src/stories/pattern-docked-widget.stories.tsx +1 -1
- package/src/stories/pattern-empty-state.stories.tsx +3 -3
- package/src/stories/prompt-input-variants.stories.tsx +13 -13
- package/src/stories/streaming-response.stories.tsx +3 -3
- package/src/stories/typography.stories.tsx +4 -4
- package/src/ui/avatar.stories.tsx +7 -8
- package/src/ui/badge.stories.tsx +7 -8
- package/src/ui/button.stories.tsx +8 -9
- package/src/ui/button.tsx +1 -0
- package/src/ui/collapsible.stories.tsx +6 -7
- package/src/ui/dropdown.stories.tsx +6 -7
- package/src/ui/hover-card.stories.tsx +6 -7
- package/src/ui/resizable.stories.tsx +74 -9
- package/src/ui/resizable.tsx +351 -71
- package/src/ui/scroll-area.stories.tsx +6 -7
- package/src/ui/scroll-area.tsx +3 -1
- package/src/ui/separator.stories.tsx +7 -8
- package/src/ui/skeleton.stories.tsx +7 -8
- package/src/ui/textarea.stories.tsx +6 -7
- package/src/ui/tooltip.stories.tsx +8 -9
- package/theme.css +65 -65
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from 'storybook-solidjs-vite';
|
|
2
2
|
import './register'; // side effect: registers the custom elements
|
|
3
|
+
import { argTypesFor, specDescription } from '../stories/docs/element-controls';
|
|
3
4
|
|
|
4
5
|
// The web components are custom DOM elements, so declare the tags for JSX.
|
|
5
6
|
declare module 'solid-js' {
|
|
6
7
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
7
8
|
namespace JSX {
|
|
8
9
|
interface IntrinsicElements {
|
|
9
|
-
'
|
|
10
|
+
'kc-file-upload': JSX.HTMLAttributes<HTMLElement> & {
|
|
10
11
|
multiple?: boolean | string;
|
|
11
12
|
accept?: string;
|
|
12
13
|
disabled?: boolean | string;
|
|
@@ -18,30 +19,29 @@ declare module 'solid-js' {
|
|
|
18
19
|
}
|
|
19
20
|
|
|
20
21
|
const HTML_SNIPPET = `<!-- Works in any framework or plain HTML -->
|
|
21
|
-
<
|
|
22
|
+
<kc-file-upload accept="image/*" label="Drop images here"></kc-file-upload>
|
|
22
23
|
|
|
23
24
|
<script type="module">
|
|
24
25
|
import '@kitnai/chat/elements'; // registers the custom elements
|
|
25
26
|
|
|
26
|
-
document.querySelector('
|
|
27
|
+
document.querySelector('kc-file-upload')
|
|
27
28
|
.addEventListener('filesadded', (e) =>
|
|
28
29
|
console.log(e.detail.files.map((f) => f.name)));
|
|
29
30
|
</script>`;
|
|
30
31
|
|
|
31
32
|
const meta = {
|
|
32
|
-
title: 'Web Components/
|
|
33
|
+
title: 'Web Components/kc-file-upload',
|
|
33
34
|
tags: ['autodocs'],
|
|
35
|
+
argTypes: argTypesFor('kc-file-upload'),
|
|
34
36
|
parameters: {
|
|
35
37
|
layout: 'fullscreen',
|
|
36
38
|
docs: {
|
|
37
|
-
description:
|
|
38
|
-
|
|
39
|
-
'`<kitn-file-upload>` is the framework-agnostic **web component** for a click / drag-and-drop file dropzone — isolated in **Shadow DOM**.',
|
|
39
|
+
description: specDescription('kc-file-upload', [
|
|
40
|
+
'`<kc-file-upload>` is the framework-agnostic **web component** for a click / drag-and-drop file dropzone — isolated in **Shadow DOM**.',
|
|
40
41
|
'**When to use:** accepting file or image uploads in a non-Solid app. In SolidJS, compose the `FileUpload` primitives.',
|
|
41
42
|
"**How to use:** register once with `import '@kitnai/chat/elements'`, set the `accept` / `multiple` / `label` attributes, and listen for the `filesadded` **CustomEvent** (`e.detail.files` is a `File[]`). The default dropzone label can be replaced with your own markup via the default `<slot>`.",
|
|
42
43
|
'See the **Code** tab for HTML usage.',
|
|
43
|
-
]
|
|
44
|
-
},
|
|
44
|
+
]),
|
|
45
45
|
},
|
|
46
46
|
},
|
|
47
47
|
} satisfies Meta;
|
|
@@ -53,7 +53,7 @@ type Story = StoryObj;
|
|
|
53
53
|
export const Default: Story = {
|
|
54
54
|
render: () => (
|
|
55
55
|
<div style={{ padding: '24px', 'max-width': '480px' }}>
|
|
56
|
-
<
|
|
56
|
+
<kc-file-upload
|
|
57
57
|
on:filesadded={(e: CustomEvent<{ files: File[] }>) =>
|
|
58
58
|
console.log(e.detail.files.map((f) => f.name))}
|
|
59
59
|
/>
|
|
@@ -66,7 +66,7 @@ export const Default: Story = {
|
|
|
66
66
|
export const ImagesOnly: Story = {
|
|
67
67
|
render: () => (
|
|
68
68
|
<div style={{ padding: '24px', 'max-width': '480px' }}>
|
|
69
|
-
<
|
|
69
|
+
<kc-file-upload accept="image/*" multiple={false} label="Click or drop an image" />
|
|
70
70
|
</div>
|
|
71
71
|
),
|
|
72
72
|
};
|
|
@@ -75,7 +75,7 @@ export const ImagesOnly: Story = {
|
|
|
75
75
|
export const Disabled: Story = {
|
|
76
76
|
render: () => (
|
|
77
77
|
<div style={{ padding: '24px', 'max-width': '480px' }}>
|
|
78
|
-
<
|
|
78
|
+
<kc-file-upload disabled label="Uploads are disabled" />
|
|
79
79
|
</div>
|
|
80
80
|
),
|
|
81
81
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineWebComponent } from './define';
|
|
2
2
|
import { FileUpload, FileUploadTrigger } from '../components/file-upload';
|
|
3
3
|
import { Upload } from 'lucide-solid';
|
|
4
4
|
|
|
@@ -13,18 +13,18 @@ interface Props extends Record<string, unknown> {
|
|
|
13
13
|
label?: string;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
/** Events fired by `<
|
|
16
|
+
/** Events fired by `<kc-file-upload>`. */
|
|
17
17
|
interface Events {
|
|
18
18
|
/** Files were picked or dropped. */
|
|
19
19
|
filesadded: { files: File[] };
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* `<
|
|
23
|
+
* `<kc-file-upload>` — a click/drag-drop dropzone. Emits `filesadded`. The
|
|
24
24
|
* default dropzone label can be replaced with your own markup via the default
|
|
25
25
|
* `<slot>` (a "Route 2" custom-content slot).
|
|
26
26
|
*/
|
|
27
|
-
|
|
27
|
+
defineWebComponent<Props, Events>('kc-file-upload', {
|
|
28
28
|
multiple: true,
|
|
29
29
|
accept: undefined,
|
|
30
30
|
disabled: false,
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from 'storybook-solidjs-vite';
|
|
2
|
+
import { createSignal, onMount, type JSX } from 'solid-js';
|
|
3
|
+
import './form';
|
|
4
|
+
import { argTypesFor, specDescription } from '../stories/docs/element-controls';
|
|
5
|
+
import type { FormDefinition } from '../components/form';
|
|
6
|
+
import type { CardEvent } from '../primitives/card-contract';
|
|
7
|
+
|
|
8
|
+
declare module 'solid-js' {
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
10
|
+
namespace JSX {
|
|
11
|
+
interface IntrinsicElements {
|
|
12
|
+
'kc-form': JSX.HTMLAttributes<HTMLElement> & {
|
|
13
|
+
heading?: string;
|
|
14
|
+
'card-id'?: string;
|
|
15
|
+
ref?: (el: HTMLElement) => void;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type FormEl = HTMLElement & { data?: FormDefinition };
|
|
22
|
+
|
|
23
|
+
/** A bordered box the form sits inside. */
|
|
24
|
+
function Frame(props: { children: JSX.Element }) {
|
|
25
|
+
return <div style={{ 'max-width': '460px' }}>{props.children}</div>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Mounts a <kc-form>, sets `.data`, logs the emitted CardEvent under the render. */
|
|
29
|
+
function FormDemo(props: { def: FormDefinition; cardId: string }) {
|
|
30
|
+
const [log, setLog] = createSignal<CardEvent[]>([]);
|
|
31
|
+
let el: FormEl | undefined;
|
|
32
|
+
onMount(() => {
|
|
33
|
+
if (!el) return;
|
|
34
|
+
el.data = props.def;
|
|
35
|
+
el.addEventListener('kc-card', (e) => {
|
|
36
|
+
const detail = (e as CustomEvent<CardEvent>).detail;
|
|
37
|
+
setLog((prev) => [...prev, detail]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
return (
|
|
41
|
+
<Frame>
|
|
42
|
+
<div style={{ display: 'flex', 'flex-direction': 'column', gap: '12px' }}>
|
|
43
|
+
<kc-form ref={(e) => (el = e as FormEl)} card-id={props.cardId} />
|
|
44
|
+
<pre
|
|
45
|
+
style={{
|
|
46
|
+
margin: 0,
|
|
47
|
+
'max-height': '180px',
|
|
48
|
+
overflow: 'auto',
|
|
49
|
+
background: 'var(--color-muted, #f4f4f5)',
|
|
50
|
+
'border-radius': '8px',
|
|
51
|
+
padding: '8px',
|
|
52
|
+
'font-size': '12px',
|
|
53
|
+
}}
|
|
54
|
+
>
|
|
55
|
+
{log().length === 0 ? '// emitted CardEvents appear here' : JSON.stringify(log(), null, 2)}
|
|
56
|
+
</pre>
|
|
57
|
+
</div>
|
|
58
|
+
</Frame>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const FEEDBACK: FormDefinition = {
|
|
63
|
+
type: 'object',
|
|
64
|
+
title: 'How did we do?',
|
|
65
|
+
description: 'Two quick questions.',
|
|
66
|
+
required: ['rating', 'contactOk'],
|
|
67
|
+
'x-kc-order': ['rating', 'comments', 'plan', 'contactOk'],
|
|
68
|
+
'x-kc-submitLabel': 'Send feedback',
|
|
69
|
+
'x-kc-actions': [{ id: 'skip', label: 'Skip', variant: 'ghost' }],
|
|
70
|
+
properties: {
|
|
71
|
+
rating: { type: 'integer', title: 'Overall rating', minimum: 1, maximum: 5, 'x-kc-widget': 'rating' },
|
|
72
|
+
comments: {
|
|
73
|
+
type: 'string',
|
|
74
|
+
title: 'Comments',
|
|
75
|
+
maxLength: 500,
|
|
76
|
+
'x-kc-widget': 'textarea',
|
|
77
|
+
'x-kc-placeholder': "What worked, what didn't…",
|
|
78
|
+
},
|
|
79
|
+
plan: { type: 'string', title: 'Your plan', enum: ['free', 'pro', 'team'], default: 'free' },
|
|
80
|
+
contactOk: { type: 'boolean', title: 'OK to contact me about this', default: false },
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const ALL_WIDGETS: FormDefinition = {
|
|
85
|
+
type: 'object',
|
|
86
|
+
title: 'Every widget',
|
|
87
|
+
'x-kc-submitLabel': 'Submit all',
|
|
88
|
+
properties: {
|
|
89
|
+
name: { type: 'string', title: 'Name' },
|
|
90
|
+
bio: { type: 'string', title: 'Bio', maxLength: 300 },
|
|
91
|
+
email: { type: 'string', title: 'Email', format: 'email' },
|
|
92
|
+
website: { type: 'string', title: 'Website', format: 'uri' },
|
|
93
|
+
birthday: { type: 'string', title: 'Birthday', format: 'date' },
|
|
94
|
+
secret: { type: 'string', title: 'Password', 'x-kc-widget': 'password' },
|
|
95
|
+
size: { type: 'string', title: 'Size', enum: ['S', 'M', 'L'] },
|
|
96
|
+
country: { type: 'string', title: 'Country', enum: ['US', 'UK', 'DE', 'FR', 'JP'] },
|
|
97
|
+
age: { type: 'integer', title: 'Age', minimum: 0, maximum: 120 },
|
|
98
|
+
volume: { type: 'integer', title: 'Volume', minimum: 0, maximum: 11, 'x-kc-widget': 'slider' },
|
|
99
|
+
stars: { type: 'integer', title: 'Stars', minimum: 1, maximum: 5, 'x-kc-widget': 'rating' },
|
|
100
|
+
notify: { type: 'boolean', title: 'Email me updates' },
|
|
101
|
+
agree: { type: 'boolean', title: 'I agree', 'x-kc-widget': 'checkbox' },
|
|
102
|
+
tags: { type: 'array', title: 'Tags', items: { type: 'string' } },
|
|
103
|
+
topics: { type: 'array', title: 'Topics', items: { enum: ['news', 'sports', 'tech'] } },
|
|
104
|
+
contacts: {
|
|
105
|
+
type: 'array',
|
|
106
|
+
title: 'Contacts',
|
|
107
|
+
items: {
|
|
108
|
+
type: 'object',
|
|
109
|
+
properties: { label: { type: 'string', title: 'Label' }, phone: { type: 'string', title: 'Phone' } },
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
address: {
|
|
113
|
+
type: 'object',
|
|
114
|
+
title: 'Address',
|
|
115
|
+
properties: { street: { type: 'string', title: 'Street' }, city: { type: 'string', title: 'City' } },
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const VALIDATION: FormDefinition = {
|
|
121
|
+
type: 'object',
|
|
122
|
+
title: 'Create account',
|
|
123
|
+
required: ['username', 'email', 'age'],
|
|
124
|
+
properties: {
|
|
125
|
+
username: { type: 'string', title: 'Username', minLength: 3, maxLength: 12 },
|
|
126
|
+
email: { type: 'string', title: 'Email', format: 'email' },
|
|
127
|
+
age: { type: 'integer', title: 'Age', minimum: 13, maximum: 120 },
|
|
128
|
+
},
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const HTML_SNIPPET = (def: FormDefinition) => `<kc-form></kc-form>
|
|
132
|
+
<script type="module">
|
|
133
|
+
import '@kitnai/chat/elements'; // registers the custom elements
|
|
134
|
+
|
|
135
|
+
const form = document.querySelector('kc-form');
|
|
136
|
+
// \`data\` is the CardEnvelope.data — a JSON Schema + x-kc-* UI hints (set as a property).
|
|
137
|
+
form.data = ${JSON.stringify(def, null, 2)};
|
|
138
|
+
|
|
139
|
+
// Cards bubble ONE \`kc-card\` CustomEvent carrying a typed CardEvent.
|
|
140
|
+
form.addEventListener('kc-card', (e) => {
|
|
141
|
+
const ev = e.detail; // { kind:'submit-data', cardId, data } | { kind:'action', ... } | ...
|
|
142
|
+
if (ev.kind === 'submit-data') console.log('submission', ev.data);
|
|
143
|
+
});
|
|
144
|
+
</script>`;
|
|
145
|
+
|
|
146
|
+
const meta = {
|
|
147
|
+
title: 'Generative UI/Cards/kc-form',
|
|
148
|
+
tags: ['autodocs'],
|
|
149
|
+
argTypes: argTypesFor('kc-form'),
|
|
150
|
+
parameters: {
|
|
151
|
+
layout: 'padded',
|
|
152
|
+
docs: {
|
|
153
|
+
description: specDescription('kc-form', [
|
|
154
|
+
'`<kc-form>` turns an agent\'s **JSON Schema** "shape" (set via the `data` **property**) into a themed, accessible, validated form inside `<kc-card>` chrome. A valid submission is emitted **up the Card contract** as a bubbling **`kc-card`** CustomEvent of `{ kind: \'submit-data\', cardId, data }`.',
|
|
155
|
+
'**The mapping is deterministic:** `string`→text, `string`+`enum`→radio/select, `string`+`format`→typed inputs, `number`/`integer`→number (or `slider`/`rating` via `x-kc-widget`), `boolean`→switch, `array`→checkbox-group / multi-select / repeater / tag-list, nested `object`→fieldset. `x-kc-*` hints (`x-kc-widget`, `x-kc-order`, `x-kc-submitLabel`, `x-kc-actions`, `x-kc-dismissible`, …) refine the UI and live **inside** the schema, so one source of truth drives both the form and validation.',
|
|
156
|
+
"**Events** (all frozen Card-contract verbs): `ready` on mount, `submit-data` on a valid submit, `action` for secondary buttons (`x-kc-actions`), `dismiss` when dismissible, `error` for a malformed definition (renders the inline `kc-card` error). It **never invents events**.",
|
|
157
|
+
'**The same `CardEnvelope`/`CardEvent` shapes flow over the remote iframe transport unchanged** — this is the *native* card. See the **Code** tab for the full envelope JSON + the HTML wiring.',
|
|
158
|
+
]),
|
|
159
|
+
},
|
|
160
|
+
},
|
|
161
|
+
} satisfies Meta;
|
|
162
|
+
|
|
163
|
+
export default meta;
|
|
164
|
+
type Story = StoryObj;
|
|
165
|
+
|
|
166
|
+
/** The worked example: a rating + textarea + enum radio + switch, with a Skip action. */
|
|
167
|
+
export const Feedback: Story = {
|
|
168
|
+
render: () => <FormDemo def={FEEDBACK} cardId="card-feedback-7f3" />,
|
|
169
|
+
parameters: { docs: { source: { code: HTML_SNIPPET(FEEDBACK), language: 'html' } } },
|
|
170
|
+
};
|
|
171
|
+
|
|
172
|
+
/** One form exercising every row of the mapping table. */
|
|
173
|
+
export const AllWidgets: Story = {
|
|
174
|
+
render: () => <FormDemo def={ALL_WIDGETS} cardId="card-all" />,
|
|
175
|
+
parameters: { docs: { source: { code: HTML_SNIPPET(ALL_WIDGETS), language: 'html' } } },
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
/** Required + min/max + format: try submitting empty (inline errors), then fill it in. */
|
|
179
|
+
export const Validation: Story = {
|
|
180
|
+
render: () => <FormDemo def={VALIDATION} cardId="card-signup" />,
|
|
181
|
+
parameters: { docs: { source: { code: HTML_SNIPPET(VALIDATION), language: 'html' } } },
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
/** A malformed `data` → the inline error state + an `error` event (no form rendered). */
|
|
185
|
+
export const InvalidEnvelope: Story = {
|
|
186
|
+
render: () => <FormDemo def={{ type: 'array' } as unknown as FormDefinition} cardId="card-bad" />,
|
|
187
|
+
parameters: {
|
|
188
|
+
docs: {
|
|
189
|
+
source: {
|
|
190
|
+
code: `<kc-form></kc-form>
|
|
191
|
+
<script type="module">
|
|
192
|
+
import '@kitnai/chat/elements';
|
|
193
|
+
const form = document.querySelector('kc-form');
|
|
194
|
+
// A definition that isn't a JSON-Schema object → inline error + an \`error\` event.
|
|
195
|
+
form.data = { type: 'array' };
|
|
196
|
+
form.addEventListener('kc-card', (e) => {
|
|
197
|
+
if (e.detail.kind === 'error') console.warn('form error:', e.detail.message);
|
|
198
|
+
});
|
|
199
|
+
</script>`,
|
|
200
|
+
language: 'html',
|
|
201
|
+
},
|
|
202
|
+
},
|
|
203
|
+
},
|
|
204
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { defineWebComponent } from './define';
|
|
2
|
+
import { Form, type FormDefinition } from '../components/form';
|
|
3
|
+
|
|
4
|
+
interface Props extends Record<string, unknown> {
|
|
5
|
+
/** The form definition — a JSON Schema (`type:'object'`) + `x-kc-*` UI hints
|
|
6
|
+
* (the CardEnvelope.data). Set as a JS PROPERTY: `el.data = { type:'object',
|
|
7
|
+
* properties:{…} }`. Import the `FormDefinition` type from `@kitnai/chat` for
|
|
8
|
+
* the full shape (it is self-referential, so the element types it loosely). */
|
|
9
|
+
data?: Record<string, unknown>;
|
|
10
|
+
/** Stable card id correlating every emitted CardEvent. Attribute: `card-id`. */
|
|
11
|
+
cardId?: string;
|
|
12
|
+
/** Heading rendered in the card chrome (= CardEnvelope.title). Attribute: `heading`. */
|
|
13
|
+
heading?: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* `<kc-form>` — renders a JSON-Schema **form definition** (set via the `data`
|
|
18
|
+
* property) into themed, accessible widgets inside `<kc-card>` chrome, validates
|
|
19
|
+
* input client-side, and emits the collected, coerced, validated object up the
|
|
20
|
+
* Card contract as a bubbling **`kc-card`** CustomEvent of `{ kind:'submit-data' }`.
|
|
21
|
+
* It also emits `ready` on mount, `action`/`dismiss` for secondary affordances, and
|
|
22
|
+
* `error` for a malformed definition. Routes through a `CardProvider` when present,
|
|
23
|
+
* else the bubbling `kc-card` event (so a bare `<kc-form>` works without a host).
|
|
24
|
+
* Isolated in Shadow DOM; theme-aware via the shared kit tokens.
|
|
25
|
+
*/
|
|
26
|
+
defineWebComponent<Props>('kc-form', {
|
|
27
|
+
data: undefined,
|
|
28
|
+
cardId: undefined,
|
|
29
|
+
heading: undefined,
|
|
30
|
+
}, (props, { element }) => (
|
|
31
|
+
<Form
|
|
32
|
+
data={props.data as FormDefinition | undefined}
|
|
33
|
+
cardId={props.cardId ?? (element.id || 'kc-form')}
|
|
34
|
+
heading={props.heading}
|
|
35
|
+
hostElement={element}
|
|
36
|
+
/>
|
|
37
|
+
));
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import type { Meta, StoryObj } from 'storybook-solidjs-vite';
|
|
2
2
|
import { onMount } from 'solid-js';
|
|
3
3
|
import './register'; // side effect: registers the custom elements
|
|
4
|
+
import { argTypesFor, specDescription } from '../stories/docs/element-controls';
|
|
4
5
|
|
|
5
6
|
// The web components are custom DOM elements, so declare the tags for JSX.
|
|
6
7
|
declare module 'solid-js' {
|
|
7
8
|
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
8
9
|
namespace JSX {
|
|
9
10
|
interface IntrinsicElements {
|
|
10
|
-
'
|
|
11
|
+
'kc-image': JSX.HTMLAttributes<HTMLElement>;
|
|
11
12
|
}
|
|
12
13
|
}
|
|
13
14
|
}
|
|
@@ -17,7 +18,7 @@ const sampleSvg =
|
|
|
17
18
|
'<svg xmlns="http://www.w3.org/2000/svg" width="96" height="96"><rect width="96" height="96" rx="16" fill="#7c3aed"/><text x="48" y="62" font-size="44" text-anchor="middle" fill="white">★</text></svg>';
|
|
18
19
|
const sampleBase64 = btoa(unescape(encodeURIComponent(sampleSvg)));
|
|
19
20
|
|
|
20
|
-
/** Render the actual `<
|
|
21
|
+
/** Render the actual `<kc-image>` custom element with base64 + media-type. */
|
|
21
22
|
function ImageElement() {
|
|
22
23
|
let el: (HTMLElement & { base64?: string; alt?: string }) | undefined;
|
|
23
24
|
onMount(() => {
|
|
@@ -27,11 +28,11 @@ function ImageElement() {
|
|
|
27
28
|
el.setAttribute('media-type', 'image/svg+xml');
|
|
28
29
|
}
|
|
29
30
|
});
|
|
30
|
-
return <
|
|
31
|
+
return <kc-image ref={(e) => (el = e as HTMLElement)} style={{ display: 'inline-block', padding: '16px' }} />;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
const HTML_SNIPPET = `<!-- Works in any framework or plain HTML -->
|
|
34
|
-
<
|
|
35
|
+
<kc-image id="img" media-type="image/png" alt="A chart"></kc-image>
|
|
35
36
|
|
|
36
37
|
<script type="module">
|
|
37
38
|
import '@kitnai/chat/elements'; // registers the custom elements
|
|
@@ -43,19 +44,18 @@ const HTML_SNIPPET = `<!-- Works in any framework or plain HTML -->
|
|
|
43
44
|
</script>`;
|
|
44
45
|
|
|
45
46
|
const meta = {
|
|
46
|
-
title: 'Web Components/
|
|
47
|
+
title: 'Web Components/kc-image',
|
|
47
48
|
tags: ['autodocs'],
|
|
49
|
+
argTypes: argTypesFor('kc-image'),
|
|
48
50
|
parameters: {
|
|
49
51
|
layout: 'fullscreen',
|
|
50
52
|
docs: {
|
|
51
|
-
description:
|
|
52
|
-
|
|
53
|
-
'`<kitn-image>` is the framework-agnostic **web component** that renders a base64 or byte-array image, showing a skeleton fallback while it resolves, isolated in **Shadow DOM**.',
|
|
53
|
+
description: specDescription('kc-image', [
|
|
54
|
+
'`<kc-image>` is the framework-agnostic **web component** that renders a base64 or byte-array image, showing a skeleton fallback while it resolves, isolated in **Shadow DOM**.',
|
|
54
55
|
'**When to use:** displaying model-generated or in-memory images (without a hosted URL) in a non-Solid app. In SolidJS, use the `Image` primitive directly.',
|
|
55
56
|
"**How to use:** register once with `import '@kitnai/chat/elements'`, set `base64` (paired with the `media-type` attribute) or set raw `bytes` as a **property**, and add `alt` text.",
|
|
56
57
|
'See the **Code** tab for HTML usage.',
|
|
57
|
-
]
|
|
58
|
-
},
|
|
58
|
+
]),
|
|
59
59
|
},
|
|
60
60
|
},
|
|
61
61
|
} satisfies Meta;
|
package/src/elements/image.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { defineWebComponent } from './define';
|
|
2
2
|
import { Image } from '../components/image';
|
|
3
3
|
|
|
4
4
|
interface Props extends Record<string, unknown> {
|
|
@@ -13,11 +13,11 @@ interface Props extends Record<string, unknown> {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
* `<
|
|
16
|
+
* `<kc-image>` — renders a base64 or byte-array image with a skeleton
|
|
17
17
|
* fallback while it resolves. `base64`/`alt`/`media-type` via attributes;
|
|
18
18
|
* `bytes` via property.
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
defineWebComponent<Props>('kc-image', {
|
|
21
21
|
base64: undefined,
|
|
22
22
|
bytes: undefined,
|
|
23
23
|
alt: '',
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from 'storybook-solidjs-vite';
|
|
2
|
+
import { onMount, onCleanup, type JSX } from 'solid-js';
|
|
3
|
+
import './link-card'; // side effect: registers <kc-link-card>
|
|
4
|
+
import { argTypesFor, specDescription } from '../stories/docs/element-controls';
|
|
5
|
+
import type { LinkCardData } from '../primitives/link-preview';
|
|
6
|
+
import { configureLinkPreview } from '../primitives/link-preview';
|
|
7
|
+
|
|
8
|
+
// Custom DOM element — declare the tag for JSX.
|
|
9
|
+
declare module 'solid-js' {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-namespace
|
|
11
|
+
namespace JSX {
|
|
12
|
+
interface IntrinsicElements {
|
|
13
|
+
'kc-link-card': JSX.HTMLAttributes<HTMLElement> & {
|
|
14
|
+
'card-id'?: string;
|
|
15
|
+
ref?: (el: HTMLElement) => void;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** A sized box the card sits in (cards expand to their container width). */
|
|
22
|
+
function Frame(props: { children: JSX.Element }) {
|
|
23
|
+
return <div style={{ width: '100%', 'max-width': '420px' }}>{props.children}</div>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Mount a kc-link-card, set its `data` property, and route `open` to a console log. */
|
|
27
|
+
function Card(props: { cardId: string; data: LinkCardData }) {
|
|
28
|
+
let el: HTMLElement & { cardId?: string; data?: LinkCardData };
|
|
29
|
+
const onCard = (e: Event) => {
|
|
30
|
+
const ev = (e as CustomEvent).detail;
|
|
31
|
+
// eslint-disable-next-line no-console
|
|
32
|
+
console.log('[kc-card]', ev);
|
|
33
|
+
if (ev.kind === 'open' && ev.target === 'tab') {
|
|
34
|
+
window.open(ev.url, '_blank', 'noopener,noreferrer');
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
onMount(() => {
|
|
38
|
+
if (el) {
|
|
39
|
+
el.cardId = props.cardId;
|
|
40
|
+
el.data = props.data;
|
|
41
|
+
}
|
|
42
|
+
document.addEventListener('kc-card', onCard);
|
|
43
|
+
onCleanup(() => document.removeEventListener('kc-card', onCard));
|
|
44
|
+
});
|
|
45
|
+
return (
|
|
46
|
+
<Frame>
|
|
47
|
+
<kc-link-card ref={(e) => (el = e as HTMLElement & { cardId?: string; data?: LinkCardData })} />
|
|
48
|
+
</Frame>
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const FULL_ENVELOPE = {
|
|
53
|
+
type: 'link',
|
|
54
|
+
id: 'card-link-1',
|
|
55
|
+
title: 'Shared link',
|
|
56
|
+
data: {
|
|
57
|
+
url: 'https://example.com/blog/generative-ui',
|
|
58
|
+
title: 'Generative UI, explained',
|
|
59
|
+
description:
|
|
60
|
+
'How agents render typed, themed cards in the chat — across native components and provider iframes.',
|
|
61
|
+
image: 'https://placehold.co/1200x630/6366f1/ffffff/png?text=Generative+UI',
|
|
62
|
+
imageAlt: 'Diagram of the card contract',
|
|
63
|
+
favicon: 'https://example.com/favicon.ico',
|
|
64
|
+
siteName: 'Example Blog',
|
|
65
|
+
domain: 'example.com',
|
|
66
|
+
},
|
|
67
|
+
} satisfies { type: string; id: string; title: string; data: LinkCardData };
|
|
68
|
+
|
|
69
|
+
const HTML_SNIPPET = `<!-- Works in any framework or plain HTML -->
|
|
70
|
+
<kc-link-card id="lc"></kc-link-card>
|
|
71
|
+
|
|
72
|
+
<script type="module">
|
|
73
|
+
import '@kitnai/chat/elements'; // registers the custom elements
|
|
74
|
+
|
|
75
|
+
const lc = document.getElementById('lc');
|
|
76
|
+
lc.cardId = 'card-link-1';
|
|
77
|
+
// \`data\` is a JS property (the CardEnvelope \`data\`):
|
|
78
|
+
lc.data = ${JSON.stringify(FULL_ENVELOPE.data, null, 2).replace(/\n/g, '\n ')};
|
|
79
|
+
|
|
80
|
+
// Route the card's \`open\` verb (it bubbles as the composed \`kc-card\` event):
|
|
81
|
+
document.addEventListener('kc-card', (e) => {
|
|
82
|
+
const ev = e.detail; // CardEvent
|
|
83
|
+
if (ev.kind === 'open' && ev.target === 'tab') {
|
|
84
|
+
window.open(ev.url, '_blank', 'noopener,noreferrer');
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
</script>`;
|
|
88
|
+
|
|
89
|
+
const meta = {
|
|
90
|
+
title: 'Generative UI/Cards/kc-link-card',
|
|
91
|
+
tags: ['autodocs'],
|
|
92
|
+
argTypes: argTypesFor('kc-link-card'),
|
|
93
|
+
parameters: {
|
|
94
|
+
layout: 'padded',
|
|
95
|
+
docs: {
|
|
96
|
+
description: specDescription('kc-link-card', [
|
|
97
|
+
'`<kc-link-card>` is a themed, accessible **rich link / Open-Graph preview** card for the generative-UI feature. It speaks the **Card Contract**: data down (a `link` `CardEnvelope`), events up (only the `open` verb, plus lifecycle `ready` / failure `error`).',
|
|
98
|
+
'**Pure by default:** the card renders from the metadata you supply (`title`, `description`, `image`, `favicon`, `siteName`, `domain`) — it **never fetches**. For the bare-`{ url }` case, an app may opt in to a resolver with `configureLinkPreview({ fetchMetadata })` (CORS means OG scraping needs YOUR backend; there is no built-in network call).',
|
|
99
|
+
'**Interaction:** the whole card is one link target. Activating it (click / Enter / Space) dispatches the bubbling, composed **`kc-card`** event with `{ kind: \'open\', url, target: \'tab\' }` so a host-level listener routes it through `CardPolicy` (which performs the navigation, after scheme validation).',
|
|
100
|
+
'**Graceful degradation:** a missing/broken image drops the image region (not an error); an invalid url renders a non-clickable "Invalid link" chip and emits one `error`.',
|
|
101
|
+
'See the **Code** tab for the `CardEnvelope` JSON + HTML wiring.',
|
|
102
|
+
]),
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
} satisfies Meta;
|
|
106
|
+
|
|
107
|
+
export default meta;
|
|
108
|
+
type Story = StoryObj;
|
|
109
|
+
|
|
110
|
+
/** Full OG metadata — image, site name, title, description. */
|
|
111
|
+
export const FullPreview: Story = {
|
|
112
|
+
name: 'Full preview',
|
|
113
|
+
render: () => <Card cardId={FULL_ENVELOPE.id} data={FULL_ENVELOPE.data} />,
|
|
114
|
+
parameters: { docs: { source: { code: HTML_SNIPPET, language: 'html' } } },
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
/** No image — the card degrades gracefully to the text + domain layout. */
|
|
118
|
+
export const NoImage: Story = {
|
|
119
|
+
name: 'No image',
|
|
120
|
+
render: () => (
|
|
121
|
+
<Card
|
|
122
|
+
cardId="card-link-2"
|
|
123
|
+
data={{
|
|
124
|
+
url: 'https://docs.example.com/guide',
|
|
125
|
+
title: 'API Guide',
|
|
126
|
+
description: 'Reference for the public endpoints, auth, and rate limits.',
|
|
127
|
+
siteName: 'Example Docs',
|
|
128
|
+
}}
|
|
129
|
+
/>
|
|
130
|
+
),
|
|
131
|
+
parameters: {
|
|
132
|
+
docs: {
|
|
133
|
+
source: {
|
|
134
|
+
code: JSON.stringify(
|
|
135
|
+
{
|
|
136
|
+
type: 'link',
|
|
137
|
+
id: 'card-link-2',
|
|
138
|
+
data: {
|
|
139
|
+
url: 'https://docs.example.com/guide',
|
|
140
|
+
title: 'API Guide',
|
|
141
|
+
description: 'Reference for the public endpoints, auth, and rate limits.',
|
|
142
|
+
siteName: 'Example Docs',
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
null,
|
|
146
|
+
2,
|
|
147
|
+
),
|
|
148
|
+
language: 'json',
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/** Bare URL + a configured fetcher — shows the skeleton → merged render path. */
|
|
155
|
+
export const BareUrlWithFetcher: Story = {
|
|
156
|
+
name: 'Bare URL + fetcher',
|
|
157
|
+
render: () => {
|
|
158
|
+
// Demo only: an app would point this at its own backend/proxy.
|
|
159
|
+
configureLinkPreview({
|
|
160
|
+
fetchMetadata: async (url) => {
|
|
161
|
+
await new Promise((r) => setTimeout(r, 900)); // show the skeleton
|
|
162
|
+
return {
|
|
163
|
+
title: 'Resolved by your backend',
|
|
164
|
+
description: `Metadata for ${url} fetched via configureLinkPreview.`,
|
|
165
|
+
siteName: 'example.com',
|
|
166
|
+
};
|
|
167
|
+
},
|
|
168
|
+
});
|
|
169
|
+
return <Card cardId="card-link-3" data={{ url: 'https://example.com/bare' }} />;
|
|
170
|
+
},
|
|
171
|
+
parameters: {
|
|
172
|
+
docs: {
|
|
173
|
+
source: {
|
|
174
|
+
code: `import { configureLinkPreview } from '@kitnai/chat';
|
|
175
|
+
|
|
176
|
+
// Opt in once (points at YOUR backend/proxy — there is no built-in network call):
|
|
177
|
+
configureLinkPreview({
|
|
178
|
+
fetchMetadata: (url) => fetch('/api/og?url=' + encodeURIComponent(url)).then((r) => r.json()),
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Then a bare { url } envelope resolves to a full preview:
|
|
182
|
+
lc.data = { url: 'https://example.com/bare' };`,
|
|
183
|
+
language: 'ts',
|
|
184
|
+
},
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/** Invalid link — a non-clickable error chip; one \`error\` event is emitted. */
|
|
190
|
+
export const InvalidLink: Story = {
|
|
191
|
+
name: 'Invalid link',
|
|
192
|
+
render: () => <Card cardId="card-link-4" data={{ url: 'not-a-valid-url' }} />,
|
|
193
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { defineWebComponent } from './define';
|
|
2
|
+
import { LinkCard } from '../components/link-card';
|
|
3
|
+
import type { LinkCardData } from '../primitives/link-preview';
|
|
4
|
+
import { emitCardEvent } from '../primitives/card-routing';
|
|
5
|
+
|
|
6
|
+
interface Props extends Record<string, unknown> {
|
|
7
|
+
/** Stable card id correlating every emitted event. Set as an attribute or property. */
|
|
8
|
+
cardId?: string;
|
|
9
|
+
/** The link payload (OG metadata). Set as a JS **property** (object). */
|
|
10
|
+
data?: LinkCardData;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `<kc-link-card>` — a themed, accessible rich link / Open-Graph preview card. It
|
|
15
|
+
* renders from the supplied `data` (it never fetches; an app may register a
|
|
16
|
+
* `configureLinkPreview` fetcher for the bare-`{ url }` path). Activating the card
|
|
17
|
+
* dispatches the bubbling, composed **`kc-card`** CustomEvent with the contract
|
|
18
|
+
* `open` verb (`{ kind:'open', url, target:'tab' }`) so a host-level listener
|
|
19
|
+
* routes it through CardPolicy. Set `data` as a JS property; `card-id` via attribute.
|
|
20
|
+
*/
|
|
21
|
+
defineWebComponent<Props>(
|
|
22
|
+
'kc-link-card',
|
|
23
|
+
{
|
|
24
|
+
cardId: undefined,
|
|
25
|
+
data: undefined,
|
|
26
|
+
},
|
|
27
|
+
(props, { element }) => (
|
|
28
|
+
<LinkCard
|
|
29
|
+
cardId={props.cardId ?? ''}
|
|
30
|
+
data={props.data ?? ({ url: '' } as LinkCardData)}
|
|
31
|
+
onEmit={(event) => emitCardEvent(element, event)}
|
|
32
|
+
/>
|
|
33
|
+
),
|
|
34
|
+
);
|