devnonla-ui 0.17.0 → 0.17.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devnonla-ui",
3
- "version": "0.17.0",
3
+ "version": "0.17.1",
4
4
  "type": "module",
5
5
  "description": "NonlaUI — React controls for Nonla Agents",
6
6
  "author": "devlangla <devlangla0x01@gmail.com>",
@@ -1,5 +1,6 @@
1
1
  import { type ReactNode, useEffect, useLayoutEffect } from "react";
2
2
  import { cn } from "../lib/cn";
3
+ import { type MarkdownComponents, MarkdownComponentsProvider } from "../markdown-editor/MarkdownViewer";
3
4
  import { OverlayScroll, type OverlayScrollVisibility } from "../scroll/OverlayScroll";
4
5
  import { AgentMessageList, activityStatus, useStickToBottom } from "./chatMessageList";
5
6
  import type { AgentMessage, AgentToolHook } from "./common/types";
@@ -30,6 +31,8 @@ export type AgentChatboxProps = {
30
31
  placeholder?: string;
31
32
  toolbar?: ReactNode;
32
33
  toolUis?: AgentToolUI[];
34
+ /** Fence language → component for assistant markdown (`step`, `subagent`, `mindmap`). */
35
+ components?: MarkdownComponents;
33
36
  toolHooks?: AgentToolHook[];
34
37
  accessory?: ReactNode;
35
38
  emptyState?: ReactNode;
@@ -51,6 +54,7 @@ export function AgentChatbox({
51
54
  placeholder = "Message…",
52
55
  toolbar,
53
56
  toolUis,
57
+ components,
54
58
  toolHooks,
55
59
  accessory,
56
60
  emptyState,
@@ -88,25 +92,27 @@ export function AgentChatbox({
88
92
  };
89
93
 
90
94
  return (
91
- <div className={cn("nonla-agent-chatbox relative flex h-full min-h-0 flex-col overflow-hidden bg-transparent", className)}>
92
- <OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
93
- <div data-chat-scroll-content className={`max-w-200 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col" : "px-2.5 pt-4 pb-20"}`}>
94
- {messages.length === 0 ? (
95
- (emptyState ?? (
96
- <ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
97
- ))
98
- ) : (
99
- <AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
100
- )}
101
- </div>
102
- </OverlayScroll>
95
+ <MarkdownComponentsProvider components={components}>
96
+ <div className={cn("nonla-agent-chatbox relative flex h-full min-h-0 flex-col overflow-hidden bg-transparent", className)}>
97
+ <OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
98
+ <div data-chat-scroll-content className={`max-w-200 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col" : "px-2.5 pt-4 pb-20"}`}>
99
+ {messages.length === 0 ? (
100
+ (emptyState ?? (
101
+ <ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
102
+ ))
103
+ ) : (
104
+ <AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
105
+ )}
106
+ </div>
107
+ </OverlayScroll>
103
108
 
104
- <div className="relative shrink-0">
105
- {accessory}
106
- <div className="mx-auto w-full max-w-200">
107
- <ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} toolbar={toolbar} enableTypeToFocus />
109
+ <div className="relative shrink-0">
110
+ {accessory}
111
+ <div className="mx-auto w-full max-w-200">
112
+ <ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} toolbar={toolbar} enableTypeToFocus />
113
+ </div>
108
114
  </div>
109
115
  </div>
110
- </div>
116
+ </MarkdownComponentsProvider>
111
117
  );
112
118
  }
@@ -1,5 +1,6 @@
1
1
  import { type ReactNode, useEffect, useLayoutEffect, useState } from "react";
2
2
  import { cn } from "../lib/cn";
3
+ import { type MarkdownComponents, MarkdownComponentsProvider } from "../markdown-editor/MarkdownViewer";
3
4
  import { OverlayScroll, type OverlayScrollVisibility } from "../scroll/OverlayScroll";
4
5
  import { AgentMessageList, activityStatus, useStickToBottom } from "./chatMessageList";
5
6
  import type { AgentMessage, AgentPanelEndpoint, AgentToolHook } from "./common/types";
@@ -27,6 +28,8 @@ export type AgentPanelProps = {
27
28
  toolbar?: ReactNode;
28
29
  /** Extra tool cards keyed by stream `toolName`. Checked before builtins. */
29
30
  toolUis?: AgentToolUI[];
31
+ /** Fence language → component for assistant markdown (`step`, `subagent`, `mindmap`). */
32
+ components?: MarkdownComponents;
30
33
  /** Tool lifecycle hooks, matched by `name` like `toolUis`. Omit `name` to hear every tool. */
31
34
  toolHooks?: AgentToolHook[];
32
35
  accessory?: ReactNode;
@@ -51,6 +54,7 @@ export function AgentPanel({
51
54
  initialMessages,
52
55
  toolbar,
53
56
  toolUis,
57
+ components,
54
58
  toolHooks,
55
59
  accessory,
56
60
  emptyState,
@@ -95,25 +99,27 @@ export function AgentPanel({
95
99
  };
96
100
 
97
101
  return (
98
- <div className={cn("nonla-agent-panel relative flex h-full min-h-0 flex-col overflow-hidden bg-transparent", className)}>
99
- <OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
100
- <div data-chat-scroll-content className={`max-w-200 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col" : "px-2.5 pt-4 pb-28"}`}>
101
- {messages.length === 0 ? (
102
- (emptyState ?? (
103
- <ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
104
- ))
105
- ) : (
106
- <AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
107
- )}
108
- </div>
109
- </OverlayScroll>
102
+ <MarkdownComponentsProvider components={components}>
103
+ <div className={cn("nonla-agent-panel relative flex h-full min-h-0 flex-col overflow-hidden bg-transparent", className)}>
104
+ <OverlayScroll visibility={scrollbar} scrollRef={scrollRef} onScroll={onScroll} className="flex-1 min-h-0 min-w-0" innerClassName={messages.length === 0 ? "flex flex-col" : undefined}>
105
+ <div data-chat-scroll-content className={`max-w-200 mx-auto min-w-0 w-full ${messages.length === 0 ? "flex flex-1 flex-col" : "px-2.5 pt-4 pb-28"}`}>
106
+ {messages.length === 0 ? (
107
+ (emptyState ?? (
108
+ <ChatWelcome name={welcomeName} description={description} avatar={avatar} starters={starters} onStarter={sendMessage} disabled={generating} />
109
+ ))
110
+ ) : (
111
+ <AgentMessageList messages={messages} generating={generating} toolUis={toolUis} showFooter={showFooter} status={status} />
112
+ )}
113
+ </div>
114
+ </OverlayScroll>
110
115
 
111
- <div className="relative shrink-0">
112
- {accessory}
113
- <div className="mx-auto w-full max-w-200">
114
- <ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} onClear={handleNewChat} toolbar={toolbar} focusSignal={String(epoch)} enableTypeToFocus />
116
+ <div className="relative shrink-0">
117
+ {accessory}
118
+ <div className="mx-auto w-full max-w-200">
119
+ <ChatInput generating={generating} placeholder={placeholder} onSend={sendMessage} onCancel={cancel} onClear={handleNewChat} toolbar={toolbar} focusSignal={String(epoch)} enableTypeToFocus />
120
+ </div>
115
121
  </div>
116
122
  </div>
117
- </div>
123
+ </MarkdownComponentsProvider>
118
124
  );
119
125
  }
package/src/chat/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ export type { MarkdownComponents } from "../markdown-editor/MarkdownViewer";
1
2
  export type { AgentChatboxProps } from "./AgentChatbox";
2
3
  export { AgentChatbox } from "./AgentChatbox";
3
4
  export type { AgentPanelProps } from "./AgentPanel";
@@ -1,5 +1,6 @@
1
1
  import type { ReactNode } from "react";
2
2
  import { cn } from "../../lib/cn";
3
+ import type { MarkdownComponents } from "../../markdown-editor/MarkdownViewer";
3
4
  import { chatRowBoxClass, chatRowClass } from "../chatRhythm";
4
5
  import { ChatMarkdown } from "./ChatMarkdown";
5
6
  import { ChatThinking } from "./ChatThinking";
@@ -8,6 +9,8 @@ export type ChatAgentMessageProps = {
8
9
  /** Markdown body. Rendered with `MarkdownViewer`. */
9
10
  content?: string;
10
11
  streaming?: boolean;
12
+ /** Fence language → component for `content`. */
13
+ components?: MarkdownComponents;
11
14
  /** Extra row content. Replaces `content` when set. */
12
15
  children?: ReactNode;
13
16
  thinking?: string;
@@ -16,8 +19,8 @@ export type ChatAgentMessageProps = {
16
19
  className?: string;
17
20
  };
18
21
 
19
- export function ChatAgentMessage({ content, streaming = false, children, thinking, thinkingDuration, thinkingStreaming, className }: ChatAgentMessageProps) {
20
- const body = children ?? (content ? <ChatMarkdown content={content} streaming={streaming} /> : null);
22
+ export function ChatAgentMessage({ content, streaming = false, components, children, thinking, thinkingDuration, thinkingStreaming, className }: ChatAgentMessageProps) {
23
+ const body = children ?? (content ? <ChatMarkdown content={content} streaming={streaming} components={components} /> : null);
21
24
 
22
25
  return (
23
26
  <div className={cn("nonla-chat-agent", chatRowClass, className)}>
@@ -1,9 +1,11 @@
1
1
  import type { CSSProperties } from "react";
2
- import { MarkdownViewer } from "../../markdown-editor/MarkdownViewer";
2
+ import { type MarkdownComponents, MarkdownViewer, useMarkdownComponents } from "../../markdown-editor/MarkdownViewer";
3
3
 
4
4
  export type ChatMarkdownProps = {
5
5
  content: string;
6
6
  streaming?: boolean;
7
+ /** Fence language → component, such as `step`, `subagent`, or `mindmap`. */
8
+ components?: MarkdownComponents;
7
9
  };
8
10
 
9
11
  /** Reply headings are `text-lg` at the strong weight. Body stays `text-md`. Strong is 500. Tables stay `text-base`. */
@@ -40,6 +42,7 @@ const chatEdgeClass = [
40
42
  ].join(" ");
41
43
 
42
44
  /** Agent markdown. Headings are `text-lg` at the strong weight. */
43
- export function ChatMarkdown({ content, streaming = false }: ChatMarkdownProps) {
44
- return <MarkdownViewer value={content} streaming={streaming} className={chatEdgeClass} style={chatHeadingScale} />;
45
+ export function ChatMarkdown({ content, streaming = false, components }: ChatMarkdownProps) {
46
+ const inherited = useMarkdownComponents();
47
+ return <MarkdownViewer value={content} streaming={streaming} className={chatEdgeClass} style={chatHeadingScale} components={components ?? inherited} />;
45
48
  }
@@ -1,4 +1,4 @@
1
- import { type CSSProperties, type ReactNode, useMemo } from "react";
1
+ import { type ComponentType, type CSSProperties, createContext, type ReactNode, useContext, useMemo } from "react";
2
2
  import ReactMarkdown, { type Components } from "react-markdown";
3
3
  import remarkGfm from "remark-gfm";
4
4
  import { Checkbox } from "../checkbox/Checkbox";
@@ -176,14 +176,54 @@ function fenceClosed(body: string): boolean {
176
176
  return /^(?:`{3,}|~{3,})$/.test(last);
177
177
  }
178
178
 
179
+ /** Props for a component registered on a fence language. */
180
+ export type MarkdownFenceProps = {
181
+ /** Fence body, without the opening and closing lines. */
182
+ code: string;
183
+ /** Fence language, lowercased. */
184
+ lang: string;
185
+ /** True while the parent viewer is still receiving the document. */
186
+ streaming: boolean;
187
+ /** True once the closing fence line has arrived. */
188
+ closed: boolean;
189
+ };
190
+
191
+ export type MarkdownFenceComponent = ComponentType<MarkdownFenceProps>;
192
+
193
+ /** Fence language → component. Keys are languages such as `step`, `subagent`, and `mindmap`. */
194
+ export type MarkdownComponents = Record<string, MarkdownFenceComponent>;
195
+
196
+ const MarkdownComponentsContext = createContext<MarkdownComponents | undefined>(undefined);
197
+
198
+ /** Shares a fence component map with every `ChatMarkdown` under this tree. */
199
+ export function MarkdownComponentsProvider({ components, children }: { components?: MarkdownComponents; children: ReactNode }) {
200
+ return <MarkdownComponentsContext.Provider value={components}>{children}</MarkdownComponentsContext.Provider>;
201
+ }
202
+
203
+ export function useMarkdownComponents(): MarkdownComponents | undefined {
204
+ return useContext(MarkdownComponentsContext);
205
+ }
206
+
207
+ function fenceComponent(components: MarkdownComponents | undefined, lang: string): MarkdownFenceComponent | undefined {
208
+ if (!components || !lang) return undefined;
209
+ const direct = components[lang];
210
+ if (direct) return direct;
211
+ const lower = lang.toLowerCase();
212
+ for (const key of Object.keys(components)) {
213
+ if (key.toLowerCase() === lower) return components[key];
214
+ }
215
+ return undefined;
216
+ }
217
+
179
218
  type LiveReactOptions = {
180
219
  trustedModules?: Record<string, unknown>;
181
220
  sandboxSrc?: string;
182
221
  showHeader?: boolean;
183
222
  showCode?: boolean;
223
+ components?: MarkdownComponents;
184
224
  };
185
225
 
186
- export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHeader, showCode }: { block: MarkdownBlock; streaming?: boolean } & LiveReactOptions): ReactNode {
226
+ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHeader, showCode, components }: { block: MarkdownBlock; streaming?: boolean } & LiveReactOptions): ReactNode {
187
227
  if (block.kind === "mermaid") {
188
228
  const fence = readFence(block.body);
189
229
  const code = fence?.code ?? block.body;
@@ -193,11 +233,17 @@ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHe
193
233
  if (block.kind === "code") {
194
234
  const fence = readFence(block.body);
195
235
  if (!fence) return <CodeBlock code={block.body} className="my-0" />;
236
+ const closed = fenceClosed(block.body);
196
237
  // An unclosed fence is still being written, so it never runs.
197
- if (fence.lang === "live-react" && fenceClosed(block.body)) {
238
+ if (fence.lang === "live-react" && closed) {
198
239
  if (sandboxSrc) return <ReactCodeSandbox key={fence.code} code={fence.code} src={sandboxSrc} showHeader={showHeader} showCode={showCode} />;
199
240
  if (trustedModules) return <ReactCode code={fence.code} trustedModules={trustedModules} showHeader={showHeader} showCode={showCode} />;
200
241
  }
242
+ const Custom = fence.lang === "live-react" ? undefined : fenceComponent(components, fence.lang);
243
+ if (Custom) {
244
+ if (streaming && !closed) return <CodeBlock code={fence.code} language={fence.lang} className="my-0" />;
245
+ return <Custom code={fence.code} lang={fence.lang} streaming={!!streaming} closed={closed} />;
246
+ }
201
247
  return <CodeBlock code={fence.code} language={fence.lang || undefined} className="my-0" />;
202
248
  }
203
249
  const view = readInlineEdit(block.body);
@@ -213,7 +259,7 @@ export function BlockBody({ block, streaming, trustedModules, sandboxSrc, showHe
213
259
 
214
260
  export type MarkdownViewerProps = {
215
261
  value?: string;
216
- /** An open mermaid fence stays a code block until the stream closes it. */
262
+ /** An open mermaid or custom fence stays a code block until the stream closes it. */
217
263
  streaming?: boolean;
218
264
  placeholder?: string;
219
265
  className?: string;
@@ -234,10 +280,15 @@ export type MarkdownViewerProps = {
234
280
  showCode?: boolean;
235
281
  /** Overrides scale variables from `markdownScaleStyle`, such as heading size. */
236
282
  style?: CSSProperties;
283
+ /**
284
+ * Components for fenced blocks, keyed by language (`step`, `subagent`, `mindmap`).
285
+ * `mermaid` and `live-react` stay built in.
286
+ */
287
+ components?: MarkdownComponents;
237
288
  };
238
289
 
239
290
  /** Read-only markdown. Same blocks as the editor preview, without editing. */
240
- export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trustedModules, showHeader = true, showCode = true, placeholder, className, style }: MarkdownViewerProps) {
291
+ export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trustedModules, showHeader = true, showCode = true, placeholder, className, style, components }: MarkdownViewerProps) {
241
292
  const doc = useMemo(() => parseMarkdownDoc(value), [value]);
242
293
  if (doc.blocks.length === 0) {
243
294
  return placeholder ? <p className={cn("m-0 text-sm text-quaternary-foreground", className)}>{placeholder}</p> : null;
@@ -251,7 +302,7 @@ export function MarkdownViewer({ value = "", streaming = false, sandboxSrc, trus
251
302
  data-md-block={block.kind}
252
303
  className={cn("outline-none", spaceBefore(block, doc.blocks[index - 1]))}
253
304
  >
254
- <BlockBody block={block} streaming={streaming} trustedModules={trustedModules} sandboxSrc={sandboxSrc} showHeader={showHeader} showCode={showCode} />
305
+ <BlockBody block={block} streaming={streaming} trustedModules={trustedModules} sandboxSrc={sandboxSrc} showHeader={showHeader} showCode={showCode} components={components} />
255
306
  </div>
256
307
  ))}
257
308
  </div>
@@ -1,7 +1,7 @@
1
1
  export type { MarkdownDiffLayout, MarkdownEditorMode, MarkdownEditorProps } from "./MarkdownEditor";
2
2
  export { MarkdownEditor } from "./MarkdownEditor";
3
- export type { MarkdownViewerProps } from "./MarkdownViewer";
4
- export { MarkdownViewer } from "./MarkdownViewer";
3
+ export type { MarkdownComponents, MarkdownFenceComponent, MarkdownFenceProps, MarkdownViewerProps } from "./MarkdownViewer";
4
+ export { MarkdownComponentsProvider, MarkdownViewer } from "./MarkdownViewer";
5
5
  export type { MermaidBlockProps } from "./MermaidBlock";
6
6
  export { MermaidBlock } from "./MermaidBlock";
7
7
  export type { ReactCodeProps } from "./ReactCode";