@kitn.ai/ui 0.15.1 → 0.17.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 +37 -13
- package/bin/mcp.js +22 -0
- package/dist/bash-InADTalH.js +6 -1
- package/dist/components/artifact.d.ts +58 -0
- package/dist/components/attachments.d.ts +67 -0
- package/dist/components/card-fallback.d.ts +10 -0
- package/dist/components/card-renderer.d.ts +13 -0
- package/dist/components/card.d.ts +28 -0
- package/dist/components/chain-of-thought.d.ts +29 -0
- package/dist/components/chat-container.d.ts +19 -0
- package/dist/components/chat-scope-picker.d.ts +9 -0
- package/dist/components/chat-thread.d.ts +91 -0
- package/dist/components/checkpoint.d.ts +20 -0
- package/dist/components/choice-card.d.ts +78 -0
- package/dist/components/code-block.d.ts +16 -0
- package/dist/components/confirm-card.d.ts +57 -0
- package/dist/components/context.d.ts +65 -0
- package/dist/components/conversation-item.d.ts +8 -0
- package/dist/components/conversation-list.d.ts +11 -0
- package/dist/components/dismissed-stub.d.ts +22 -0
- package/dist/components/embed.d.ts +21 -0
- package/dist/components/empty.d.ts +43 -0
- package/dist/components/feedback-bar.d.ts +45 -0
- package/dist/components/file-tree.d.ts +57 -0
- package/dist/components/file-upload.d.ts +16 -0
- package/dist/components/form-widgets.d.ts +31 -0
- package/dist/components/form.d.ts +103 -0
- package/dist/components/image.d.ts +12 -0
- package/dist/components/link-preview.d.ts +23 -0
- package/dist/components/loader.d.ts +60 -0
- package/dist/components/markdown.d.ts +8 -0
- package/dist/components/message-skills.d.ts +15 -0
- package/dist/components/message.d.ts +111 -0
- package/dist/components/model-switcher.d.ts +8 -0
- package/dist/components/prompt-input.d.ts +38 -0
- package/dist/components/prompt-suggestion.d.ts +13 -0
- package/dist/components/reasoning.d.ts +20 -0
- package/dist/components/response-compare-types.d.ts +66 -0
- package/dist/components/response-compare.d.ts +51 -0
- package/dist/components/response-stream.d.ts +14 -0
- package/dist/components/scroll-button.d.ts +7 -0
- package/dist/components/slash-command.d.ts +15 -0
- package/dist/components/source.d.ts +23 -0
- package/dist/components/tasks-card.d.ts +72 -0
- package/dist/components/text-shimmer.d.ts +9 -0
- package/dist/components/thinking-bar.d.ts +10 -0
- package/dist/components/toast.d.ts +48 -0
- package/dist/components/tool.d.ts +16 -0
- package/dist/components/use-card-resolution.d.ts +16 -0
- package/dist/components/voice-input.d.ts +7 -0
- package/dist/core-AYMC6_lb.js +5874 -12
- package/dist/css-M7EaDHN_.js +6 -1
- package/dist/custom-elements.json +260 -11
- package/dist/elements/artifact.d.ts +1 -0
- package/dist/elements/artifact.js +1 -0
- package/dist/elements/attachments.d.ts +1 -0
- package/dist/elements/attachments.js +1 -0
- package/dist/elements/autoloader.d.ts +2 -0
- package/dist/elements/autoloader.js +1 -0
- package/dist/elements/card.d.ts +1 -0
- package/dist/elements/card.js +1 -0
- package/dist/elements/cards.d.ts +1 -0
- package/dist/elements/cards.js +1 -0
- package/dist/elements/chain-of-thought.d.ts +1 -0
- package/dist/elements/chain-of-thought.js +1 -0
- package/dist/elements/chat-scope-picker.d.ts +1 -0
- package/dist/elements/chat-scope-picker.js +1 -0
- package/dist/elements/chat-workspace.d.ts +1 -0
- package/dist/elements/chat-workspace.js +1 -0
- package/dist/elements/chat.d.ts +1 -0
- package/dist/elements/chat.js +1 -0
- package/dist/elements/checkpoint.d.ts +1 -0
- package/dist/elements/checkpoint.js +1 -0
- package/dist/elements/choice.d.ts +1 -0
- package/dist/elements/choice.js +1 -0
- package/dist/elements/chunks/Icon-D_Zmwmwh.js +1 -0
- package/dist/elements/chunks/action-icons-PVfIvcuG.js +1 -0
- package/dist/elements/chunks/attachments-Dp6vqtrc.js +1 -0
- package/dist/elements/chunks/bash-InADTalH.js +1 -0
- package/dist/elements/chunks/button-CKyYD9G4.js +1 -0
- package/dist/elements/chunks/card-Cjho-PYk.js +1 -0
- package/dist/elements/chunks/card-routing-C986KuUA.js +1 -0
- package/dist/elements/chunks/chat-thread-RT808iPf.js +1 -0
- package/dist/elements/chunks/check-CDYaMYrM.js +1 -0
- package/dist/elements/chunks/chevron-down-3SBiq8HF.js +1 -0
- package/dist/elements/chunks/chevron-right-k2UFlIVK.js +1 -0
- package/dist/elements/chunks/cn-Dogu6PbV.js +1 -0
- package/dist/elements/chunks/code-block-BYUmfFSd.js +1 -0
- package/dist/elements/chunks/collapsible-BiNwdOn6.js +1 -0
- package/dist/elements/chunks/context-18g6oi4J.js +1 -0
- package/dist/elements/chunks/conversation-list-D9Y0NVdo.js +1 -0
- package/dist/elements/chunks/core-AYMC6_lb.js +12 -0
- package/dist/elements/chunks/css-M7EaDHN_.js +1 -0
- package/dist/elements/chunks/default-input-CSAoYYMd.js +1 -0
- package/dist/elements/chunks/download-D8K0Je8r.js +1 -0
- package/dist/elements/chunks/dropdown-bUs2iTia.js +1 -0
- package/dist/elements/chunks/engine-javascript-vq0WuIJl.js +141 -0
- package/dist/elements/chunks/external-link-6mA0wxab.js +1 -0
- package/dist/elements/chunks/file-text-DnaYk3Dm.js +1 -0
- package/dist/elements/chunks/file-tree-CgtRLlxK.js +1 -0
- package/dist/elements/chunks/github-dark-dimmed-DUshB20C.js +1 -0
- package/dist/elements/chunks/github-light-JYsPkUQd.js +1 -0
- package/dist/elements/chunks/hover-card-CIGw62gP.js +1 -0
- package/dist/elements/chunks/html-CPZ3oZQ7.js +1 -0
- package/dist/elements/chunks/index-CTBEJeo6.js +1 -0
- package/dist/elements/chunks/javascript-C25yR2R2.js +1 -0
- package/dist/elements/chunks/json-DxJze_jm.js +1 -0
- package/dist/elements/chunks/link-BlGSeoPy.js +1 -0
- package/dist/elements/chunks/loader-DvhR4qa0.js +1 -0
- package/dist/elements/chunks/markdown-CMTGx9r8.js +64 -0
- package/dist/elements/chunks/message-CDZwXjhL.js +1 -0
- package/dist/elements/chunks/message-feedback-CYx81QiA.js +1 -0
- package/dist/elements/chunks/model-switcher-O_tUhoOT.js +1 -0
- package/dist/elements/chunks/overlay-DydXYMQW.js +1 -0
- package/dist/elements/chunks/prompt-suggestion-ldlWV5kd.js +1 -0
- package/dist/elements/chunks/reasoning-2PUnHlmr.js +1 -0
- package/dist/elements/chunks/resizable-B5WNbSeb.js +1 -0
- package/dist/elements/chunks/star-t9tI-P0q.js +1 -0
- package/dist/elements/chunks/store-C6rIooZz.js +1 -0
- package/dist/elements/chunks/svelte--5p79yCD.js +1 -0
- package/dist/elements/chunks/text-shimmer-uh9V_KUh.js +1 -0
- package/dist/elements/chunks/textarea-3nm6Ia9U.js +1 -0
- package/dist/elements/chunks/thumbs-up-BBtguGld.js +1 -0
- package/dist/elements/chunks/toast-store-D8cL3fWl.js +1 -0
- package/dist/elements/chunks/tool-GJ3vwp1U.js +1 -0
- package/dist/elements/chunks/tooltip-DwKAz4XR.js +1 -0
- package/dist/elements/chunks/triangle-alert-Ct1zt2JL.js +1 -0
- package/dist/elements/chunks/tsx-B8rCNbgL.js +1 -0
- package/dist/elements/chunks/typescript-RycA9KXf.js +1 -0
- package/dist/elements/chunks/use-card-resolution-D-_0Alzy.js +1 -0
- package/dist/elements/chunks/video-mVYiM99-.js +1 -0
- package/dist/elements/chunks/vue-BmIZj4XD.js +1 -0
- package/dist/elements/chunks/x-CzxL27Ch.js +1 -0
- package/dist/elements/code-block.d.ts +1 -0
- package/dist/elements/code-block.js +1 -0
- package/dist/elements/compare.d.ts +1 -0
- package/dist/elements/compare.js +1 -0
- package/dist/elements/confirm-card.d.ts +1 -0
- package/dist/elements/confirm-card.js +1 -0
- package/dist/elements/context-meter.d.ts +1 -0
- package/dist/elements/context-meter.js +1 -0
- package/dist/elements/conversation-list.d.ts +1 -0
- package/dist/elements/conversation-list.js +1 -0
- package/dist/elements/embed.d.ts +1 -0
- package/dist/elements/embed.js +1 -0
- package/dist/elements/empty.d.ts +1 -0
- package/dist/elements/empty.js +1 -0
- package/dist/elements/feedback-bar.d.ts +1 -0
- package/dist/elements/feedback-bar.js +1 -0
- package/dist/elements/file-tree.d.ts +1 -0
- package/dist/elements/file-tree.js +1 -0
- package/dist/elements/file-upload.d.ts +1 -0
- package/dist/elements/file-upload.js +1 -0
- package/dist/elements/form.d.ts +1 -0
- package/dist/elements/form.js +1 -0
- package/dist/elements/image.d.ts +1 -0
- package/dist/elements/image.js +1 -0
- package/dist/elements/link-preview.d.ts +1 -0
- package/dist/elements/link-preview.js +1 -0
- package/dist/elements/loader.d.ts +1 -0
- package/dist/elements/loader.js +1 -0
- package/dist/elements/markdown.d.ts +1 -0
- package/dist/elements/markdown.js +1 -0
- package/dist/elements/message-skills.d.ts +1 -0
- package/dist/elements/message-skills.js +1 -0
- package/dist/elements/message.d.ts +1 -0
- package/dist/elements/message.js +1 -0
- package/dist/elements/model-switcher.d.ts +1 -0
- package/dist/elements/model-switcher.js +1 -0
- package/dist/elements/popover.d.ts +1 -0
- package/dist/elements/popover.js +1 -0
- package/dist/elements/prompt-input.d.ts +1 -0
- package/dist/elements/prompt-input.js +1 -0
- package/dist/elements/prompt-suggestions.d.ts +1 -0
- package/dist/elements/prompt-suggestions.js +1 -0
- package/dist/elements/reasoning.d.ts +1 -0
- package/dist/elements/reasoning.js +1 -0
- package/dist/elements/resizable.d.ts +1 -0
- package/dist/elements/resizable.js +1 -0
- package/dist/elements/response-stream.d.ts +1 -0
- package/dist/elements/response-stream.js +14 -0
- package/dist/elements/scroll-button.d.ts +1 -0
- package/dist/elements/scroll-button.js +1 -0
- package/dist/elements/source.d.ts +1 -0
- package/dist/elements/source.js +1 -0
- package/dist/elements/switch.d.ts +1 -0
- package/dist/elements/switch.js +1 -0
- package/dist/elements/tasks.d.ts +1 -0
- package/dist/elements/tasks.js +1 -0
- package/dist/elements/text-shimmer.d.ts +1 -0
- package/dist/elements/text-shimmer.js +1 -0
- package/dist/elements/thinking-bar.d.ts +1 -0
- package/dist/elements/thinking-bar.js +1 -0
- package/dist/elements/toast.d.ts +1 -0
- package/dist/elements/toast.js +1 -0
- package/dist/elements/tool.d.ts +1 -0
- package/dist/elements/tool.js +1 -0
- package/dist/elements/voice-input.d.ts +1 -0
- package/dist/elements/voice-input.js +1 -0
- package/dist/elements.d.ts +768 -0
- package/dist/engine-javascript-vq0WuIJl.js +2516 -14
- package/dist/github-dark-dimmed-DUshB20C.js +4 -1
- package/dist/github-light-JYsPkUQd.js +4 -1
- package/dist/html-CPZ3oZQ7.js +10 -1
- package/dist/index.d.ts +124 -2
- package/dist/index.js +15593 -0
- package/dist/javascript-C25yR2R2.js +6 -1
- package/dist/json-DxJze_jm.js +6 -1
- package/dist/kitn-chat.es.js +1 -77
- package/dist/llms/llms-full.txt +57 -12
- package/dist/llms/llms.txt +3 -3
- package/dist/mcp.es.js +2296 -0
- package/dist/primitives/card-contract.d.ts +119 -0
- package/dist/primitives/card-host.d.ts +12 -0
- package/dist/primitives/card-recovery.d.ts +55 -0
- package/dist/primitives/card-registry.d.ts +17 -0
- package/dist/primitives/card-resolution.d.ts +8 -0
- package/dist/primitives/card-routing.d.ts +12 -0
- package/dist/primitives/card-validate.d.ts +25 -0
- package/dist/primitives/chat-config.d.ts +31 -0
- package/dist/primitives/controllable.d.ts +13 -0
- package/dist/primitives/embed-providers.d.ts +65 -0
- package/dist/primitives/highlighter.d.ts +23 -0
- package/dist/primitives/link-preview.d.ts +47 -0
- package/dist/primitives/message-feedback.d.ts +35 -0
- package/dist/primitives/pdf-preview.d.ts +50 -0
- package/dist/primitives/toast-store.d.ts +88 -0
- package/dist/primitives/use-auto-resize.d.ts +8 -0
- package/dist/primitives/use-resize-observer.d.ts +1 -0
- package/dist/primitives/use-stick-to-bottom.d.ts +5 -0
- package/dist/primitives/use-text-stream.d.ts +19 -0
- package/dist/primitives/use-voice-recorder.d.ts +9 -0
- package/dist/react/index.d.ts +1198 -0
- package/dist/react/runtime.d.ts +15 -0
- package/dist/react.js +275 -0
- package/dist/register-JyTglR6m.js +1 -0
- package/dist/register-impl-C6wlSsts.js +77 -0
- package/dist/remote/host-embed.d.ts +33 -0
- package/dist/remote/index.d.ts +2 -0
- package/dist/remote/origin.d.ts +5 -0
- package/dist/remote/provider-runtime.d.ts +27 -0
- package/dist/remote/provider.d.ts +2 -0
- package/dist/remote/validate.d.ts +4 -0
- package/dist/remote/version.d.ts +4 -0
- package/dist/remote/wire.d.ts +53 -0
- package/dist/schemas/choice.schema.json +5 -0
- package/dist/schemas/tasks.schema.json +5 -0
- package/dist/svelte--5p79yCD.js +15 -1
- package/dist/tsx-B8rCNbgL.js +6 -1
- package/dist/types.d.ts +42 -0
- package/dist/typescript-RycA9KXf.js +6 -1
- package/dist/ui/action-icons.d.ts +11 -0
- package/dist/ui/avatar.d.ts +8 -0
- package/dist/ui/badge.d.ts +9 -0
- package/dist/ui/button.d.ts +10 -0
- package/dist/ui/collapsible.d.ts +27 -0
- package/dist/ui/dropdown.d.ts +20 -0
- package/dist/ui/hover-card.d.ts +28 -0
- package/dist/ui/overlay.d.ts +67 -0
- package/dist/ui/popover.d.ts +32 -0
- package/dist/ui/resizable.d.ts +76 -0
- package/dist/ui/scroll-area.d.ts +5 -0
- package/dist/ui/separator.d.ts +5 -0
- package/dist/ui/skeleton.d.ts +5 -0
- package/dist/ui/switch.d.ts +19 -0
- package/dist/ui/textarea.d.ts +6 -0
- package/dist/ui/tooltip.d.ts +9 -0
- package/dist/utils/cn.d.ts +2 -0
- package/dist/vue-BmIZj4XD.js +33 -1
- package/frameworks/react/index.tsx +67 -11
- package/frameworks/react/runtime.tsx +16 -2
- package/llms-full.txt +57 -12
- package/llms.txt +3 -3
- package/package.json +36 -14
- package/src/agent-tooling/archetypes.ts +46 -0
- package/src/agent-tooling/integrations/cloudflare.ts +88 -0
- package/src/agent-tooling/integrations/langgraph.ts +60 -0
- package/src/agent-tooling/integrations/mastra.ts +27 -0
- package/src/agent-tooling/integrations/mock.ts +34 -0
- package/src/agent-tooling/integrations/ollama.ts +47 -0
- package/src/agent-tooling/integrations/openrouter.ts +26 -0
- package/src/agent-tooling/integrations/pi.ts +40 -0
- package/src/agent-tooling/integrations/pydantic-ai.ts +52 -0
- package/src/agent-tooling/integrations/vercel-ai-sdk.ts +44 -0
- package/src/agent-tooling/mcp/manifest.ts +128 -0
- package/src/agent-tooling/mcp/server.ts +59 -0
- package/src/agent-tooling/mcp/stdio.ts +18 -0
- package/src/agent-tooling/mcp/tools/debug.ts +489 -0
- package/src/agent-tooling/mcp/tools/reference.ts +132 -0
- package/src/agent-tooling/mcp/tools/scaffold.ts +1759 -0
- package/src/agent-tooling/mcp/tools/theme.ts +340 -0
- package/src/agent-tooling/mcp/tools/types.ts +14 -0
- package/src/agent-tooling/registry.ts +41 -0
- package/src/agent-tooling/types.ts +30 -0
- package/src/components/chat-thread.tsx +27 -35
- package/src/components/choice-card.tsx +33 -4
- package/src/components/component-meta.json +250 -26
- package/src/components/confirm-card.tsx +15 -1
- package/src/components/dismissed-stub.tsx +80 -0
- package/src/components/form.tsx +21 -3
- package/src/components/message.tsx +151 -10
- package/src/components/response-compare-types.ts +127 -0
- package/src/components/response-compare.tsx +431 -0
- package/src/components/tasks-card.tsx +33 -3
- package/src/components/toast.tsx +398 -0
- package/src/components/use-card-resolution.ts +24 -8
- package/src/elements/autoloader.ts +89 -0
- package/src/elements/chat-types.ts +7 -0
- package/src/elements/chat-workspace.tsx +4 -2
- package/src/elements/chat.tsx +4 -2
- package/src/elements/compare.tsx +88 -0
- package/src/elements/compiled.css +1 -1
- package/src/elements/define.tsx +7 -1
- package/src/elements/element-manifest.json +182 -0
- package/src/elements/element-meta.json +191 -68
- package/src/elements/element-types.d.ts +43 -6
- package/src/elements/framework-usage.json +25 -0
- package/src/elements/message.tsx +27 -45
- package/src/elements/register-impl.ts +58 -0
- package/src/elements/register.ts +26 -51
- package/src/elements/remote.tsx +4 -0
- package/src/elements/styles.css +19 -0
- package/src/elements/toast.tsx +74 -0
- package/src/index.ts +30 -0
- package/src/primitives/card-contract.ts +14 -5
- package/src/primitives/card-recovery.ts +149 -0
- package/src/primitives/card-routing.ts +3 -0
- package/src/primitives/card-schemas/choice.schema.json +5 -0
- package/src/primitives/card-schemas/tasks.schema.json +5 -0
- package/src/primitives/message-feedback.ts +123 -0
- package/src/primitives/toast-store.ts +235 -0
- package/src/ui/tooltip.tsx +8 -2
- package/theme.css +4 -2
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// src/primitives/card-recovery.ts
|
|
2
|
+
// Recovery glue for the card dismiss/reopen flow. `dismissRecovery` builds the
|
|
3
|
+
// `{ onDismiss, onReopen }` half of a CardPolicy for a host that keeps its cards in
|
|
4
|
+
// some store: it writes a `dismissed` resolution onto the matching envelope (a NEW
|
|
5
|
+
// array reference, never an in-place mutation, so a Solid/React host re-renders),
|
|
6
|
+
// optionally shows an injected "Dismissed · Undo" toast (Undo restores the prior
|
|
7
|
+
// resolution), and on reopen decides — via `isReopenable` — whether the card comes
|
|
8
|
+
// back live (resolution cleared) or `expired`.
|
|
9
|
+
//
|
|
10
|
+
// The toast is an INJECTED ADAPTER, never an import — cards (and this helper) stay
|
|
11
|
+
// decoupled from the toast module. The re-appear rule is the host's: "the agent
|
|
12
|
+
// already proceeded" is a host fact, surfaced through `isReopenable`.
|
|
13
|
+
import type { CardEnvelope, CardPolicy, CardResolution } from './card-contract';
|
|
14
|
+
|
|
15
|
+
/** The minimal toast surface this helper needs. Inject a real adapter (e.g. one
|
|
16
|
+
* backed by `kai-toast-region`) — this module never imports the toast itself. */
|
|
17
|
+
export interface RecoveryToast {
|
|
18
|
+
show(opts: {
|
|
19
|
+
message: string;
|
|
20
|
+
action?: { label: string; onClick: () => void };
|
|
21
|
+
durationMs?: number;
|
|
22
|
+
}): { dismiss(): void };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Context handed to a custom `isReopenable` predicate. */
|
|
26
|
+
export interface ReopenEnv {
|
|
27
|
+
cardId: string;
|
|
28
|
+
/** The card's current resolution at reopen time (should be `dismissed`). */
|
|
29
|
+
resolution: CardResolution | undefined;
|
|
30
|
+
/** `Date.now()` at reopen time (injected for testability). */
|
|
31
|
+
now: number;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface DismissRecoveryOptions {
|
|
35
|
+
/** Read the current cards array. */
|
|
36
|
+
get: () => CardEnvelope[];
|
|
37
|
+
/** Write the next cards array (a NEW reference). */
|
|
38
|
+
set: (next: CardEnvelope[]) => void;
|
|
39
|
+
/** Optional injected toast adapter for the "Dismissed · Undo" affordance. */
|
|
40
|
+
toast?: RecoveryToast;
|
|
41
|
+
/** Override the default re-openable rule. Return false to expire instead. */
|
|
42
|
+
isReopenable?: (env: ReopenEnv) => boolean;
|
|
43
|
+
/** A dismissed card older than this (ms since `dismissed.at`) is no longer
|
|
44
|
+
* re-openable (→ expired). Omit/Infinity = never stale. */
|
|
45
|
+
staleAfterMs?: number;
|
|
46
|
+
/** Auto-dismiss the Undo toast after this long. Default 6000ms. */
|
|
47
|
+
undoMs?: number;
|
|
48
|
+
/** Injected clock (defaults to Date.now); keeps the helper testable. */
|
|
49
|
+
now?: () => number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The terminal resolution kinds that block a reopen (a finished card can't come back). */
|
|
53
|
+
const TERMINAL_KINDS = new Set<CardResolution['kind']>(['action', 'submit', 'expired']);
|
|
54
|
+
|
|
55
|
+
/** Default re-openable rule: a card can come back unless it carries a terminal
|
|
56
|
+
* resolution OR it has gone stale (`staleAfterMs` elapsed since `dismissed.at`).
|
|
57
|
+
* A `dismissed` card with no/invalid `at` is always re-openable. */
|
|
58
|
+
export function defaultIsReopenable(env: ReopenEnv, staleAfterMs?: number): boolean {
|
|
59
|
+
const r = env.resolution;
|
|
60
|
+
if (r && TERMINAL_KINDS.has(r.kind)) return false;
|
|
61
|
+
if (
|
|
62
|
+
staleAfterMs !== undefined &&
|
|
63
|
+
Number.isFinite(staleAfterMs) &&
|
|
64
|
+
r?.kind === 'dismissed' &&
|
|
65
|
+
typeof r.at === 'string'
|
|
66
|
+
) {
|
|
67
|
+
const at = Date.parse(r.at);
|
|
68
|
+
if (!Number.isNaN(at) && env.now - at >= staleAfterMs) return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Return a new cards array with `cardId`'s envelope stamped with `resolution`
|
|
74
|
+
* (or cleared when `resolution` is undefined). Same reference if nothing changed. */
|
|
75
|
+
function withResolution(
|
|
76
|
+
cards: CardEnvelope[],
|
|
77
|
+
cardId: string,
|
|
78
|
+
resolution: CardResolution | undefined,
|
|
79
|
+
): CardEnvelope[] {
|
|
80
|
+
let changed = false;
|
|
81
|
+
const next = cards.map((c) => {
|
|
82
|
+
if (c.id !== cardId) return c;
|
|
83
|
+
changed = true;
|
|
84
|
+
if (resolution === undefined) {
|
|
85
|
+
if (c.resolution === undefined) return c;
|
|
86
|
+
const { resolution: _drop, ...rest } = c;
|
|
87
|
+
return rest;
|
|
88
|
+
}
|
|
89
|
+
return { ...c, resolution };
|
|
90
|
+
});
|
|
91
|
+
return changed ? next : cards;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** The current resolution on `cardId`, if any. */
|
|
95
|
+
function resolutionOf(cards: CardEnvelope[], cardId: string): CardResolution | undefined {
|
|
96
|
+
return cards.find((c) => c.id === cardId)?.resolution;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Build the `{ onDismiss, onReopen }` policy handlers for the card dismiss/recovery
|
|
101
|
+
* flow over a host store (`get`/`set`).
|
|
102
|
+
*
|
|
103
|
+
* - `onDismiss(cardId)` — stamps `{ kind:'dismissed', at }` immutably and, when a
|
|
104
|
+
* `toast` is injected, shows "Dismissed" with an Undo that restores the card's
|
|
105
|
+
* prior resolution (live again when there was none).
|
|
106
|
+
* - `onReopen(cardId)` — clears the resolution (live) when `isReopenable`, else
|
|
107
|
+
* stamps `{ kind:'expired', at }`.
|
|
108
|
+
*/
|
|
109
|
+
export function dismissRecovery(
|
|
110
|
+
opts: DismissRecoveryOptions,
|
|
111
|
+
): Pick<CardPolicy, 'onDismiss' | 'onReopen'> {
|
|
112
|
+
const now = opts.now ?? (() => Date.now());
|
|
113
|
+
const reopenable = opts.isReopenable ?? ((env: ReopenEnv) => defaultIsReopenable(env, opts.staleAfterMs));
|
|
114
|
+
const undoMs = opts.undoMs ?? 6000;
|
|
115
|
+
|
|
116
|
+
const onDismiss = (cardId: string): void => {
|
|
117
|
+
const before = opts.get();
|
|
118
|
+
const prior = resolutionOf(before, cardId);
|
|
119
|
+
const dismissed: CardResolution = { kind: 'dismissed', at: new Date(now()).toISOString() };
|
|
120
|
+
opts.set(withResolution(before, cardId, dismissed));
|
|
121
|
+
|
|
122
|
+
opts.toast?.show({
|
|
123
|
+
message: 'Dismissed',
|
|
124
|
+
durationMs: undoMs,
|
|
125
|
+
action: {
|
|
126
|
+
label: 'Undo',
|
|
127
|
+
onClick: () => {
|
|
128
|
+
// Restore the prior resolution (undefined → live again). Read fresh in
|
|
129
|
+
// case the store moved on; only touch this card.
|
|
130
|
+
opts.set(withResolution(opts.get(), cardId, prior));
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const onReopen = (cardId: string): void => {
|
|
137
|
+
const before = opts.get();
|
|
138
|
+
const current = resolutionOf(before, cardId);
|
|
139
|
+
const env: ReopenEnv = { cardId, resolution: current, now: now() };
|
|
140
|
+
if (reopenable(env)) {
|
|
141
|
+
opts.set(withResolution(before, cardId, undefined));
|
|
142
|
+
} else {
|
|
143
|
+
const expired: CardResolution = { kind: 'expired', at: new Date(now()).toISOString() };
|
|
144
|
+
opts.set(withResolution(before, cardId, expired));
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
return { onDismiss, onReopen };
|
|
149
|
+
}
|
|
@@ -59,6 +59,9 @@ export function routeCardEvent(policy: CardPolicy | undefined, event: CardEvent)
|
|
|
59
59
|
case 'dismiss':
|
|
60
60
|
p.onDismiss ? p.onDismiss(event.cardId) : warnNoHandler('dismiss');
|
|
61
61
|
break;
|
|
62
|
+
case 'reopen':
|
|
63
|
+
p.onReopen ? p.onReopen(event.cardId) : warnNoHandler('reopen');
|
|
64
|
+
break;
|
|
62
65
|
case 'error':
|
|
63
66
|
p.onError ? p.onError(event.cardId, event.message) : warnNoHandler('error');
|
|
64
67
|
break;
|
|
@@ -65,6 +65,11 @@
|
|
|
65
65
|
"allowOther": {
|
|
66
66
|
"description": "Optional free-text escape ('none of these / specify'). `true` appends a final 'Other…' option; an object customizes its `label`/`placeholder`. Selecting it reveals a text input; the shared Submit button then emits `{ kind:'action', action:'__other__', payload:{ text } }`.",
|
|
67
67
|
"x-kai-control": "allow-other"
|
|
68
|
+
},
|
|
69
|
+
"dismissible": {
|
|
70
|
+
"type": "boolean",
|
|
71
|
+
"default": false,
|
|
72
|
+
"description": "Show a close affordance that emits the `dismiss` verb. The card collapses to a re-openable 'dismissed' stub; the host decides via `onReopen` whether it comes back live or `expired`."
|
|
68
73
|
}
|
|
69
74
|
}
|
|
70
75
|
}
|
|
@@ -73,6 +73,11 @@
|
|
|
73
73
|
"minimum": 1,
|
|
74
74
|
"description": "Optional maximum selectable; further toggles are blocked once reached.",
|
|
75
75
|
"x-kai-select-max": true
|
|
76
|
+
},
|
|
77
|
+
"dismissible": {
|
|
78
|
+
"type": "boolean",
|
|
79
|
+
"default": false,
|
|
80
|
+
"description": "Show a close affordance that emits the `dismiss` verb. The card collapses to a re-openable 'dismissed' stub; the host decides via `onReopen` whether it comes back live or `expired`."
|
|
76
81
|
}
|
|
77
82
|
}
|
|
78
83
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// Message action-row feedback state — copy + like/dislike — owned ABOVE the
|
|
2
|
+
// per-message `<For>` so a streaming re-render (a fresh `messages` array ref per
|
|
3
|
+
// chunk) never wipes the user's optimistic vote or the transient "copied" check.
|
|
4
|
+
//
|
|
5
|
+
// Both facades (`ChatThread` and the `<kai-message>` element) create one of
|
|
6
|
+
// these and route their action-bar clicks through `handleAction`. Keeping the
|
|
7
|
+
// logic here (not inside the un-unit-testable `defineWebComponent` body) makes
|
|
8
|
+
// the toggle/toast/clipboard behavior directly testable.
|
|
9
|
+
|
|
10
|
+
import { createSignal, onCleanup } from 'solid-js';
|
|
11
|
+
import type { ChatMessage, FeedbackVote } from '../elements/chat-types';
|
|
12
|
+
import { toast } from './toast-store';
|
|
13
|
+
|
|
14
|
+
/** Detail shape emitted by the action row. `state` is present only for the
|
|
15
|
+
* toggleable feedback votes: `'on'` when a vote is set, `'off'` when cleared.
|
|
16
|
+
* Copy / regenerate / edit / custom actions omit it. */
|
|
17
|
+
export interface MessageActionDetail {
|
|
18
|
+
messageId: string;
|
|
19
|
+
action: string;
|
|
20
|
+
state?: 'on' | 'off';
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface MessageFeedbackOptions {
|
|
24
|
+
/** Emit the `kai-message-action` detail (facade maps this to `dispatch` /
|
|
25
|
+
* `onMessageAction`). */
|
|
26
|
+
emit: (detail: MessageActionDetail) => void;
|
|
27
|
+
/** How long the copy check stays before auto-clearing. Defaults to 2000ms. */
|
|
28
|
+
copiedDuration?: number;
|
|
29
|
+
/** Container the copy/feedback toasts should be scoped to (the chat, by
|
|
30
|
+
* default) so they appear in-chat rather than at the page top. */
|
|
31
|
+
target?: () => HTMLElement | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MessageFeedback {
|
|
35
|
+
/** Resolve the active vote for a message: a controlled `m.feedback` wins,
|
|
36
|
+
* else the facade's own optimistic map. */
|
|
37
|
+
resolveFeedback: (m: Pick<ChatMessage, 'id' | 'feedback'>) => FeedbackVote | undefined;
|
|
38
|
+
/** Whether a message's copy button should currently show its check. */
|
|
39
|
+
isCopied: (id: string) => boolean;
|
|
40
|
+
/** Route a clicked action through copy / vote / passthrough handling. */
|
|
41
|
+
handleAction: (m: Pick<ChatMessage, 'id' | 'content' | 'feedback'>, action: string) => void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const DEFAULT_COPIED_DURATION = 2000;
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Create the action-row feedback controller for one message list (or one
|
|
48
|
+
* standalone message). Owns the optimistic `feedbackMap` + transient
|
|
49
|
+
* `copiedIds`, resolves the controlled-wins vote, and turns clicks into
|
|
50
|
+
* clipboard writes, toasts, and `kai-message-action` emissions.
|
|
51
|
+
*/
|
|
52
|
+
export function createMessageFeedback(opts: MessageFeedbackOptions): MessageFeedback {
|
|
53
|
+
const [feedbackMap, setFeedbackMap] = createSignal<Record<string, FeedbackVote>>({});
|
|
54
|
+
const [copiedIds, setCopiedIds] = createSignal<Set<string>>(new Set());
|
|
55
|
+
const copiedDuration = opts.copiedDuration ?? DEFAULT_COPIED_DURATION;
|
|
56
|
+
const timers = new Map<string, ReturnType<typeof setTimeout>>();
|
|
57
|
+
|
|
58
|
+
onCleanup(() => {
|
|
59
|
+
timers.forEach((t) => clearTimeout(t));
|
|
60
|
+
timers.clear();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const resolveFeedback: MessageFeedback['resolveFeedback'] = (m) =>
|
|
64
|
+
m.feedback ?? feedbackMap()[m.id];
|
|
65
|
+
|
|
66
|
+
const isCopied: MessageFeedback['isCopied'] = (id) => copiedIds().has(id);
|
|
67
|
+
|
|
68
|
+
const markCopied = (id: string) => {
|
|
69
|
+
setCopiedIds((prev) => {
|
|
70
|
+
const next = new Set(prev);
|
|
71
|
+
next.add(id);
|
|
72
|
+
return next;
|
|
73
|
+
});
|
|
74
|
+
const existing = timers.get(id);
|
|
75
|
+
if (existing) clearTimeout(existing);
|
|
76
|
+
timers.set(id, setTimeout(() => {
|
|
77
|
+
timers.delete(id);
|
|
78
|
+
setCopiedIds((prev) => {
|
|
79
|
+
if (!prev.has(id)) return prev;
|
|
80
|
+
const next = new Set(prev);
|
|
81
|
+
next.delete(id);
|
|
82
|
+
return next;
|
|
83
|
+
});
|
|
84
|
+
}, copiedDuration));
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const setVote = (id: string, vote: FeedbackVote | undefined) => {
|
|
88
|
+
setFeedbackMap((prev) => {
|
|
89
|
+
const next = { ...prev };
|
|
90
|
+
if (vote === undefined) delete next[id];
|
|
91
|
+
else next[id] = vote;
|
|
92
|
+
return next;
|
|
93
|
+
});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const handleAction: MessageFeedback['handleAction'] = (m, action) => {
|
|
97
|
+
if (action === 'copy') {
|
|
98
|
+
// Clipboard may be unavailable (insecure context / jsdom); fail soft.
|
|
99
|
+
try { navigator.clipboard?.writeText(m.content); } catch { /* ignore */ }
|
|
100
|
+
markCopied(m.id);
|
|
101
|
+
toast('Copied to clipboard', { target: opts.target?.() });
|
|
102
|
+
opts.emit({ messageId: m.id, action });
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
if (action === 'like' || action === 'dislike') {
|
|
106
|
+
const current = resolveFeedback(m);
|
|
107
|
+
if (current === action) {
|
|
108
|
+
// Re-tap clears the vote — no toast on un-vote.
|
|
109
|
+
setVote(m.id, undefined);
|
|
110
|
+
opts.emit({ messageId: m.id, action, state: 'off' });
|
|
111
|
+
} else {
|
|
112
|
+
setVote(m.id, action);
|
|
113
|
+
toast('Thanks for your feedback', { target: opts.target?.() });
|
|
114
|
+
opts.emit({ messageId: m.id, action, state: 'on' });
|
|
115
|
+
}
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
// regenerate / edit / custom — passthrough, no state.
|
|
119
|
+
opts.emit({ messageId: m.id, action });
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
return { resolveFeedback, isCopied, handleAction };
|
|
123
|
+
}
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
// Module-level toast store + the imperative `toast()` API.
|
|
2
|
+
//
|
|
3
|
+
// This is the PRIMARY way consumers raise a toast: `toast('Saved')`,
|
|
4
|
+
// `toast.success('Copied')`, `toast.dismiss(id)`. The store is a single
|
|
5
|
+
// reactive list held in a long-lived `createRoot` (so its reactivity survives
|
|
6
|
+
// outside any component), and `ensureMounted()` lazily creates exactly ONE
|
|
7
|
+
// `<kai-toast-region>` on `document.body` the first time a toast is raised,
|
|
8
|
+
// binding the list to its `toasts` property. The region is a real `kai-*`
|
|
9
|
+
// element, so it carries its own shadow root + the shared kit stylesheet — it's
|
|
10
|
+
// viewport-positioned AND fully kit-styled, never a raw div.
|
|
11
|
+
//
|
|
12
|
+
// SSR-safe: every DOM touch is guarded by `typeof document`. On the server,
|
|
13
|
+
// raising a toast is an inert no-op (the store updates, nothing mounts).
|
|
14
|
+
|
|
15
|
+
import { createRoot, createSignal } from 'solid-js';
|
|
16
|
+
import { createStore, produce } from 'solid-js/store';
|
|
17
|
+
import type { ToastPosition } from '../components/toast';
|
|
18
|
+
|
|
19
|
+
export type ToastVariant = 'neutral' | 'success';
|
|
20
|
+
|
|
21
|
+
export interface ToastConfig {
|
|
22
|
+
stack?: 'expanded' | 'collapsed';
|
|
23
|
+
position?: ToastPosition;
|
|
24
|
+
max?: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Module-level config the imperative singleton regions inherit. Defaults match
|
|
28
|
+
// the element's own defaults, so an un-configured app behaves exactly as before.
|
|
29
|
+
let toastConfig: ToastConfig = {};
|
|
30
|
+
|
|
31
|
+
/** Apply the current config to a region element (attributes for scalars). */
|
|
32
|
+
function applyConfig(el: HTMLElement): void {
|
|
33
|
+
if (toastConfig.stack) el.setAttribute('stack', toastConfig.stack);
|
|
34
|
+
if (toastConfig.position) el.setAttribute('position', toastConfig.position);
|
|
35
|
+
if (toastConfig.max !== undefined) el.setAttribute('max', String(toastConfig.max));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Configure the imperative `toast()` singleton — call once at app start.
|
|
40
|
+
* `toast.success('…')` has no element to set a prop on, so this is how you opt
|
|
41
|
+
* the auto-mounted region into collapsed stacking / a position / a max. Updates
|
|
42
|
+
* any already-mounted regions too, so call order doesn't matter.
|
|
43
|
+
*/
|
|
44
|
+
export function configureToasts(config: ToastConfig): void {
|
|
45
|
+
toastConfig = { ...toastConfig, ...config };
|
|
46
|
+
if (typeof document === 'undefined') return;
|
|
47
|
+
document.querySelectorAll('kai-toast-region').forEach((el) => applyConfig(el as HTMLElement));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** An action button rendered inside the toast. Returning `false` from
|
|
51
|
+
* `onAction` keeps the toast open (e.g. to show a follow-up); any other
|
|
52
|
+
* return value dismisses it. */
|
|
53
|
+
export interface ToastAction {
|
|
54
|
+
label: string;
|
|
55
|
+
onAction: () => void | false;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface ToastItem {
|
|
59
|
+
id: string;
|
|
60
|
+
message: string;
|
|
61
|
+
variant?: ToastVariant;
|
|
62
|
+
action?: ToastAction;
|
|
63
|
+
/** Auto-dismiss delay in ms. `0` = sticky (never auto-dismisses). When an
|
|
64
|
+
* `action` is present the effective floor is 4000ms so it stays long enough
|
|
65
|
+
* to act on. Defaults to 2000ms. */
|
|
66
|
+
duration?: number;
|
|
67
|
+
/** Whether the × close affordance is shown. Defaults to `true`. */
|
|
68
|
+
dismissible?: boolean;
|
|
69
|
+
/** Container to scope this toast WITHIN — it floats anchored to that element's
|
|
70
|
+
* bounds instead of the viewport. Omit for a global, viewport-anchored toast.
|
|
71
|
+
* The chat targets itself by default so its copy/feedback toasts stay in-chat. */
|
|
72
|
+
target?: HTMLElement;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Options accepted by `toast()` — everything but the message. Pass `id` to
|
|
76
|
+
* update an existing toast in place. */
|
|
77
|
+
export interface ToastOptions {
|
|
78
|
+
id?: string;
|
|
79
|
+
variant?: ToastVariant;
|
|
80
|
+
action?: ToastAction;
|
|
81
|
+
duration?: number;
|
|
82
|
+
dismissible?: boolean;
|
|
83
|
+
/** Scope this toast to a container's bounds (e.g. a chat) instead of the
|
|
84
|
+
* viewport. Omit for a global, viewport-anchored toast. */
|
|
85
|
+
target?: HTMLElement;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Handle returned from `toast()` for imperative control. */
|
|
89
|
+
export interface ToastHandle {
|
|
90
|
+
id: string;
|
|
91
|
+
dismiss: () => void;
|
|
92
|
+
update: (patch: Partial<Omit<ToastItem, 'id'>>) => void;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/** Default auto-dismiss delay. Long enough to read + reach before it leaves. */
|
|
96
|
+
export const DEFAULT_TOAST_DURATION = 5000;
|
|
97
|
+
/** Minimum auto-dismiss delay when the toast carries an action (e.g. Undo) — it
|
|
98
|
+
* has to stay up long enough to actually act on. */
|
|
99
|
+
export const ACTION_TOAST_FLOOR = 7000;
|
|
100
|
+
|
|
101
|
+
// ── The reactive store ──────────────────────────────────────────────────────
|
|
102
|
+
// Held in a never-disposed root so the signal/store graph stays live for the
|
|
103
|
+
// lifetime of the page, independent of any component.
|
|
104
|
+
const { toasts, setToasts, mounted, setMounted } = createRoot(() => {
|
|
105
|
+
const [list, setList] = createStore<ToastItem[]>([]);
|
|
106
|
+
const [isMounted, setIsMounted] = createSignal(false);
|
|
107
|
+
return { toasts: list, setToasts: setList, mounted: isMounted, setMounted: setIsMounted };
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
/** The live toast list (reactive). The region facade binds to this. */
|
|
111
|
+
export function getToasts(): ToastItem[] {
|
|
112
|
+
return toasts;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let counter = 0;
|
|
116
|
+
function nextId(): string {
|
|
117
|
+
counter += 1;
|
|
118
|
+
return `toast-${counter}`;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** Resolve the effective duration, applying the action floor. */
|
|
122
|
+
function resolveDuration(item: Pick<ToastItem, 'duration' | 'action'>): number {
|
|
123
|
+
const base = item.duration ?? DEFAULT_TOAST_DURATION;
|
|
124
|
+
if (base === 0) return 0; // explicit sticky wins
|
|
125
|
+
if (item.action) return Math.max(base, ACTION_TOAST_FLOOR);
|
|
126
|
+
return base;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Lazily create the single `<kai-toast-region>` host on `document.body` and
|
|
131
|
+
* bind the store to its `toasts` property. Idempotent + SSR-safe. The element
|
|
132
|
+
* upgrade (its `defineWebComponent` registration) happens when the elements
|
|
133
|
+
* bundle loads; setting the property before/after upgrade both work because
|
|
134
|
+
* `customElement` reflects late-set properties.
|
|
135
|
+
*/
|
|
136
|
+
// One region per distinct target (the `null` key = the global / viewport region).
|
|
137
|
+
// Every region binds the SAME store proxy and filters to its own target, so a
|
|
138
|
+
// toast routes to the region for its `target`.
|
|
139
|
+
const regions = new Map<HTMLElement | null, HTMLElement>();
|
|
140
|
+
|
|
141
|
+
export function ensureMounted(target: HTMLElement | null = null): HTMLElement | undefined {
|
|
142
|
+
if (typeof document === 'undefined') return undefined;
|
|
143
|
+
const cached = regions.get(target);
|
|
144
|
+
if (cached && cached.isConnected) return cached;
|
|
145
|
+
const el = document.createElement('kai-toast-region') as HTMLElement;
|
|
146
|
+
// Append FIRST so the element upgrades, THEN bind. Setting a property before
|
|
147
|
+
// upgrade loses it: component-register resets every prop in the constructor on
|
|
148
|
+
// upgrade (the same pre-upgrade clobber that bites <kai-chat>), so a pre-append
|
|
149
|
+
// `el.toasts = …` is wiped and the first toast never renders.
|
|
150
|
+
document.body.appendChild(el);
|
|
151
|
+
(el as unknown as { toasts: ToastItem[] }).toasts = toasts as ToastItem[];
|
|
152
|
+
if (target) (el as unknown as { target: HTMLElement }).target = target;
|
|
153
|
+
applyConfig(el); // inherit stack/position/max from configureToasts()
|
|
154
|
+
regions.set(target, el);
|
|
155
|
+
setMounted(true);
|
|
156
|
+
return el;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** Test-only: whether `ensureMounted` has run. */
|
|
160
|
+
export function isToastRegionMounted(): boolean {
|
|
161
|
+
return mounted();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function upsert(item: ToastItem): void {
|
|
165
|
+
const idx = toasts.findIndex((t) => t.id === item.id);
|
|
166
|
+
if (idx >= 0) {
|
|
167
|
+
setToasts(idx, item);
|
|
168
|
+
} else {
|
|
169
|
+
setToasts(produce((list) => { list.push(item); }));
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function dismiss(id: string): void {
|
|
174
|
+
setToasts((list) => list.filter((t) => t.id !== id));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function makeHandle(id: string): ToastHandle {
|
|
178
|
+
return {
|
|
179
|
+
id,
|
|
180
|
+
dismiss: () => dismiss(id),
|
|
181
|
+
update: (patch) => {
|
|
182
|
+
const idx = toasts.findIndex((t) => t.id === id);
|
|
183
|
+
if (idx >= 0) setToasts(idx, (prev) => ({ ...prev, ...patch }));
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Internal: raise/refresh a toast of a given variant. */
|
|
189
|
+
function raise(message: string, opts: ToastOptions | undefined, variant: ToastVariant): ToastHandle {
|
|
190
|
+
ensureMounted(opts?.target ?? null);
|
|
191
|
+
const id = opts?.id ?? nextId();
|
|
192
|
+
const item: ToastItem = {
|
|
193
|
+
id,
|
|
194
|
+
message,
|
|
195
|
+
variant: opts?.variant ?? variant,
|
|
196
|
+
action: opts?.action,
|
|
197
|
+
duration: opts?.duration,
|
|
198
|
+
dismissible: opts?.dismissible,
|
|
199
|
+
target: opts?.target,
|
|
200
|
+
};
|
|
201
|
+
upsert(item);
|
|
202
|
+
return makeHandle(id);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export interface ToastFn {
|
|
206
|
+
(message: string, opts?: ToastOptions): ToastHandle;
|
|
207
|
+
/** Raise a success (emerald check) toast. */
|
|
208
|
+
success: (message: string, opts?: ToastOptions) => ToastHandle;
|
|
209
|
+
/** Dismiss a toast by id. */
|
|
210
|
+
dismiss: (id: string) => void;
|
|
211
|
+
/** Dismiss every active toast. */
|
|
212
|
+
clear: () => void;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Raise a transient toast. Returns a `{ id, dismiss, update }` handle.
|
|
217
|
+
*
|
|
218
|
+
* ```ts
|
|
219
|
+
* toast('Copied to clipboard');
|
|
220
|
+
* toast.success('Saved');
|
|
221
|
+
* const t = toast('Working…', { duration: 0 });
|
|
222
|
+
* t.update({ message: 'Done', variant: 'success', duration: 2000 });
|
|
223
|
+
* ```
|
|
224
|
+
*/
|
|
225
|
+
export const toast: ToastFn = Object.assign(
|
|
226
|
+
(message: string, opts?: ToastOptions) => raise(message, opts, 'neutral'),
|
|
227
|
+
{
|
|
228
|
+
success: (message: string, opts?: ToastOptions) => raise(message, opts, 'success'),
|
|
229
|
+
dismiss,
|
|
230
|
+
clear: () => setToasts([]),
|
|
231
|
+
},
|
|
232
|
+
);
|
|
233
|
+
|
|
234
|
+
// Re-export for the region facade so the effective-duration rule lives in one place.
|
|
235
|
+
export { resolveDuration };
|
package/src/ui/tooltip.tsx
CHANGED
|
@@ -4,10 +4,10 @@ import { cn } from '../utils/cn';
|
|
|
4
4
|
import { useChatConfig } from '../primitives/chat-config';
|
|
5
5
|
import { createPresence, usePosition, useDismiss, As } from './overlay';
|
|
6
6
|
|
|
7
|
-
export interface TooltipProps { content: string; children: JSX.Element; class?: string; openDelay?: number; }
|
|
7
|
+
export interface TooltipProps { content: string; children: JSX.Element; class?: string; openDelay?: number; dismissOnClick?: boolean; }
|
|
8
8
|
|
|
9
9
|
export function Tooltip(props: TooltipProps) {
|
|
10
|
-
const [local] = splitProps(props, ['content', 'children', 'class', 'openDelay']);
|
|
10
|
+
const [local] = splitProps(props, ['content', 'children', 'class', 'openDelay', 'dismissOnClick']);
|
|
11
11
|
const config = useChatConfig();
|
|
12
12
|
const id = createUniqueId();
|
|
13
13
|
const [open, setOpen] = createSignal(false);
|
|
@@ -25,6 +25,10 @@ export function Tooltip(props: TooltipProps) {
|
|
|
25
25
|
};
|
|
26
26
|
const hide = () => { clearTimeout(timer); setOpen(false); };
|
|
27
27
|
const maybeHide = () => { if (!pointerInside() && !focusInside()) hide(); };
|
|
28
|
+
// Action-style tooltips should dismiss when their trigger is clicked: the pointer
|
|
29
|
+
// never "leaves", so reset the inside flags and force-close until the next genuine
|
|
30
|
+
// hover/focus. Opt out with dismissOnClick={false}.
|
|
31
|
+
const dismiss = () => { if (local.dismissOnClick === false) return; setPointerInside(false); setFocusInside(false); hide(); };
|
|
28
32
|
onCleanup(() => clearTimeout(timer));
|
|
29
33
|
|
|
30
34
|
const presence = createPresence(open);
|
|
@@ -39,6 +43,8 @@ export function Tooltip(props: TooltipProps) {
|
|
|
39
43
|
aria-describedby={open() ? id : undefined}
|
|
40
44
|
onPointerEnter={() => { setPointerInside(true); show(local.openDelay ?? 600); }}
|
|
41
45
|
onPointerLeave={() => { setPointerInside(false); maybeHide(); }}
|
|
46
|
+
onPointerDown={dismiss}
|
|
47
|
+
onClick={dismiss}
|
|
42
48
|
onFocusIn={() => { setFocusInside(true); show(); }}
|
|
43
49
|
onFocusOut={(e: FocusEvent) => { const t = triggerEl(); if (t && t.contains(e.relatedTarget as Node)) return; setFocusInside(false); maybeHide(); }}
|
|
44
50
|
>
|
package/theme.css
CHANGED
|
@@ -3,8 +3,10 @@
|
|
|
3
3
|
utilities, and the .chat-markdown styles. Contains NO global base/reset rules,
|
|
4
4
|
so importing it restyles nothing on its own — a host opts in via the kit's
|
|
5
5
|
components + classes. Rebrand by overriding the --color-* values (optionally
|
|
6
|
-
scoped to a subtree).
|
|
7
|
-
|
|
6
|
+
scoped to a subtree).
|
|
7
|
+
NOTE: This file is Tailwind-v4 source (requires tailwindcss as a peer). For
|
|
8
|
+
bundler-based apps that don't use Tailwind, import @kitn.ai/ui/theme.tokens.css
|
|
9
|
+
instead — it is plain compiled CSS with no toolchain requirements. */
|
|
8
10
|
|
|
9
11
|
@custom-variant dark (&:is(.dark *));
|
|
10
12
|
|