agent-stream-ui 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +220 -2
- package/dist/adapters/openai.d.ts +42 -0
- package/dist/adapters/openai.d.ts.map +1 -0
- package/dist/agent-stream-ui.css +2 -0
- package/dist/agent-stream-ui.js +1418 -0
- package/dist/agent-stream-ui.js.map +1 -0
- package/dist/core/reducer.d.ts +33 -0
- package/dist/core/reducer.d.ts.map +1 -0
- package/dist/core/scheduler.d.ts +15 -0
- package/dist/core/scheduler.d.ts.map +1 -0
- package/dist/core/store.d.ts +29 -0
- package/dist/core/store.d.ts.map +1 -0
- package/dist/core/transport.d.ts +57 -0
- package/dist/core/transport.d.ts.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/markdown/blocks.d.ts +31 -0
- package/dist/markdown/blocks.d.ts.map +1 -0
- package/dist/markdown/inline.d.ts +13 -0
- package/dist/markdown/inline.d.ts.map +1 -0
- package/dist/markdown/render.d.ts +18 -0
- package/dist/markdown/render.d.ts.map +1 -0
- package/dist/react/agent-chat.d.ts +14 -0
- package/dist/react/agent-chat.d.ts.map +1 -0
- package/dist/react/composer.d.ts +11 -0
- package/dist/react/composer.d.ts.map +1 -0
- package/dist/react/icons.d.ts +12 -0
- package/dist/react/icons.d.ts.map +1 -0
- package/dist/react/markdown-stream.d.ts +14 -0
- package/dist/react/markdown-stream.d.ts.map +1 -0
- package/dist/react/message-body.d.ts +14 -0
- package/dist/react/message-body.d.ts.map +1 -0
- package/dist/react/message-list.d.ts +8 -0
- package/dist/react/message-list.d.ts.map +1 -0
- package/dist/react/tool-call-card.d.ts +13 -0
- package/dist/react/tool-call-card.d.ts.map +1 -0
- package/dist/react/use-agent-stream.d.ts +11 -0
- package/dist/react/use-agent-stream.d.ts.map +1 -0
- package/dist/react/use-stick-to-bottom.d.ts +14 -0
- package/dist/react/use-stick-to-bottom.d.ts.map +1 -0
- package/dist/sse/client.d.ts +60 -0
- package/dist/sse/client.d.ts.map +1 -0
- package/dist/sse/frame.d.ts +42 -0
- package/dist/sse/frame.d.ts.map +1 -0
- package/dist/types.d.ts +119 -0
- package/dist/types.d.ts.map +1 -0
- package/docs/design-notes.md +145 -0
- package/package.json +84 -4
- package/src/lib/adapters/openai.ts +112 -0
- package/src/lib/core/reducer.ts +417 -0
- package/src/lib/core/scheduler.ts +40 -0
- package/src/lib/core/store.ts +214 -0
- package/src/lib/core/transport.ts +197 -0
- package/src/lib/index.ts +102 -0
- package/src/lib/markdown/blocks.ts +201 -0
- package/src/lib/markdown/inline.tsx +87 -0
- package/src/lib/markdown/render.tsx +149 -0
- package/src/lib/react/agent-chat.tsx +121 -0
- package/src/lib/react/composer.tsx +105 -0
- package/src/lib/react/icons.tsx +83 -0
- package/src/lib/react/markdown-stream.tsx +37 -0
- package/src/lib/react/message-body.tsx +66 -0
- package/src/lib/react/message-list.tsx +109 -0
- package/src/lib/react/tool-call-card.tsx +117 -0
- package/src/lib/react/use-agent-stream.ts +14 -0
- package/src/lib/react/use-stick-to-bottom.ts +60 -0
- package/src/lib/sse/client.ts +263 -0
- package/src/lib/sse/frame.ts +116 -0
- package/src/lib/styles.css +790 -0
- package/src/lib/types.ts +130 -0
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 行内解析:只认 `code`、**bold**、*italic*、[link](url)、裸 URL。
|
|
3
|
+
*
|
|
4
|
+
* 关键取舍:**不用 dangerouslySetInnerHTML**。
|
|
5
|
+
* 模型输出是不可信文本,一旦走 innerHTML 就等于把 XSS 交给模型的心情。
|
|
6
|
+
* 这里全部构造 React 节点,天然转义;链接还要过协议白名单。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Fragment, type ReactNode } from 'react'
|
|
10
|
+
|
|
11
|
+
const INLINE_PATTERN =
|
|
12
|
+
/(`[^`\n]+`)|(\*\*[^*\n]+\*\*)|(\*[^*\n]+\*)|(\[[^\]\n]+\]\([^)\s]+\))|(https?:\/\/[^\s<>"']+)/g
|
|
13
|
+
|
|
14
|
+
export function isSafeHref(href: string): boolean {
|
|
15
|
+
const trimmed = href.trim()
|
|
16
|
+
if (trimmed === '') return false
|
|
17
|
+
if (trimmed.startsWith('#')) return true
|
|
18
|
+
if (trimmed.startsWith('/')) return true
|
|
19
|
+
return /^(https?:|mailto:)/i.test(trimmed)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function renderLink(label: string, href: string, key: string): ReactNode {
|
|
23
|
+
if (!isSafeHref(href)) return <Fragment key={key}>{label}</Fragment>
|
|
24
|
+
return (
|
|
25
|
+
<a key={key} href={href} target="_blank" rel="noreferrer noopener">
|
|
26
|
+
{label}
|
|
27
|
+
</a>
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function parseInline(text: string): ReactNode[] {
|
|
32
|
+
if (text === '') return []
|
|
33
|
+
|
|
34
|
+
const nodes: ReactNode[] = []
|
|
35
|
+
let cursor = 0
|
|
36
|
+
let match: RegExpExecArray | null
|
|
37
|
+
|
|
38
|
+
INLINE_PATTERN.lastIndex = 0
|
|
39
|
+
while ((match = INLINE_PATTERN.exec(text)) !== null) {
|
|
40
|
+
if (match.index > cursor) {
|
|
41
|
+
nodes.push(text.slice(cursor, match.index))
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const token = match[0]
|
|
45
|
+
const key = `i-${nodes.length}`
|
|
46
|
+
|
|
47
|
+
if (token.startsWith('`')) {
|
|
48
|
+
nodes.push(
|
|
49
|
+
<code className="asui-inline-code" key={key}>
|
|
50
|
+
{token.slice(1, -1)}
|
|
51
|
+
</code>,
|
|
52
|
+
)
|
|
53
|
+
} else if (token.startsWith('**')) {
|
|
54
|
+
nodes.push(<strong key={key}>{token.slice(2, -2)}</strong>)
|
|
55
|
+
} else if (token.startsWith('*')) {
|
|
56
|
+
nodes.push(<em key={key}>{token.slice(1, -1)}</em>)
|
|
57
|
+
} else if (token.startsWith('[')) {
|
|
58
|
+
const linkMatch = /^\[([^\]]+)\]\(([^)]+)\)$/.exec(token)
|
|
59
|
+
if (linkMatch) {
|
|
60
|
+
nodes.push(renderLink(linkMatch[1] ?? '', linkMatch[2] ?? '', key))
|
|
61
|
+
} else {
|
|
62
|
+
nodes.push(token)
|
|
63
|
+
}
|
|
64
|
+
} else {
|
|
65
|
+
nodes.push(renderLink(token, token, key))
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
cursor = match.index + token.length
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (cursor < text.length) nodes.push(text.slice(cursor))
|
|
72
|
+
|
|
73
|
+
return nodes
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 段落里的硬换行:单换行 → `<br />`。 */
|
|
77
|
+
export function parseInlineLines(text: string): ReactNode[] {
|
|
78
|
+
const lines = text.split('\n')
|
|
79
|
+
const nodes: ReactNode[] = []
|
|
80
|
+
|
|
81
|
+
lines.forEach((line, index) => {
|
|
82
|
+
if (index > 0) nodes.push(<br key={`br-${index}`} />)
|
|
83
|
+
nodes.push(...parseInline(line))
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
return nodes
|
|
87
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 块 → React 节点。
|
|
3
|
+
*
|
|
4
|
+
* 已闭合的块按「类型 + 内容」缓存渲染结果。缓存的是一棵不可变元素树,
|
|
5
|
+
* 复用它不会跳过子组件的状态(React 只按元素类型和 key 协调),
|
|
6
|
+
* 但省掉了重复解析和重复创建元素的成本。
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { memo, useState, type ReactNode } from 'react'
|
|
10
|
+
import { languageLabel, listItemText, type MdBlock } from './blocks'
|
|
11
|
+
import { parseInline, parseInlineLines } from './inline'
|
|
12
|
+
|
|
13
|
+
const CACHE_LIMIT = 500
|
|
14
|
+
const blockCache = new Map<string, ReactNode>()
|
|
15
|
+
|
|
16
|
+
function cacheKey(block: MdBlock): string {
|
|
17
|
+
return [block.kind, block.level ?? '', block.lang ?? '', block.ordered ? '1' : '0', block.text].join(
|
|
18
|
+
'\u0000',
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function renderBlock(block: MdBlock, streaming: boolean): ReactNode {
|
|
23
|
+
const key = cacheKey(block)
|
|
24
|
+
|
|
25
|
+
if (block.closed) {
|
|
26
|
+
const cached = blockCache.get(key)
|
|
27
|
+
if (cached !== undefined) return cached
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const node = renderUncached(block, streaming)
|
|
31
|
+
|
|
32
|
+
if (block.closed) {
|
|
33
|
+
if (blockCache.size >= CACHE_LIMIT) {
|
|
34
|
+
const oldest = blockCache.keys().next()
|
|
35
|
+
if (!oldest.done) blockCache.delete(oldest.value)
|
|
36
|
+
}
|
|
37
|
+
blockCache.set(key, node)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return node
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** 测试用:确认缓存命中行为。 */
|
|
44
|
+
export function clearBlockCache(): void {
|
|
45
|
+
blockCache.clear()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function renderUncached(block: MdBlock, streaming: boolean): ReactNode {
|
|
49
|
+
switch (block.kind) {
|
|
50
|
+
case 'heading': {
|
|
51
|
+
const level = Math.min(Math.max(block.level ?? 1, 1), 6)
|
|
52
|
+
const Tag = `h${level}` as 'h1'
|
|
53
|
+
return (
|
|
54
|
+
<Tag className={`asui-h asui-h${level}`}>{parseInline(block.text)}</Tag>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
case 'code':
|
|
59
|
+
return <CodeBlock code={block.text} lang={block.lang} streaming={streaming} />
|
|
60
|
+
|
|
61
|
+
case 'list': {
|
|
62
|
+
const items = block.text
|
|
63
|
+
.split('\n')
|
|
64
|
+
.filter((line) => line.trim() !== '')
|
|
65
|
+
.map((line) => listItemText(line))
|
|
66
|
+
|
|
67
|
+
const children = items.map((item, index) => (
|
|
68
|
+
<li key={`li-${index}`}>{parseInline(item)}</li>
|
|
69
|
+
))
|
|
70
|
+
|
|
71
|
+
return block.ordered ? (
|
|
72
|
+
<ol className="asui-list">{children}</ol>
|
|
73
|
+
) : (
|
|
74
|
+
<ul className="asui-list">{children}</ul>
|
|
75
|
+
)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
case 'quote':
|
|
79
|
+
return <blockquote className="asui-quote">{parseInlineLines(block.text)}</blockquote>
|
|
80
|
+
|
|
81
|
+
case 'hr':
|
|
82
|
+
return <hr className="asui-hr" />
|
|
83
|
+
|
|
84
|
+
case 'paragraph':
|
|
85
|
+
default:
|
|
86
|
+
return <p className="asui-p">{parseInlineLines(block.text)}</p>
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function CodeBlock({
|
|
91
|
+
code,
|
|
92
|
+
lang,
|
|
93
|
+
streaming,
|
|
94
|
+
}: {
|
|
95
|
+
code: string
|
|
96
|
+
lang?: string
|
|
97
|
+
streaming: boolean
|
|
98
|
+
}) {
|
|
99
|
+
const [copied, setCopied] = useState(false)
|
|
100
|
+
|
|
101
|
+
const copy = () => {
|
|
102
|
+
const clipboard = typeof navigator === 'undefined' ? undefined : navigator.clipboard
|
|
103
|
+
if (!clipboard) return
|
|
104
|
+
void clipboard.writeText(code).then(
|
|
105
|
+
() => {
|
|
106
|
+
setCopied(true)
|
|
107
|
+
setTimeout(() => setCopied(false), 1400)
|
|
108
|
+
},
|
|
109
|
+
() => {},
|
|
110
|
+
)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return (
|
|
114
|
+
<div className="asui-code">
|
|
115
|
+
<div className="asui-code-head">
|
|
116
|
+
<span className="asui-code-lang">{languageLabel(lang)}</span>
|
|
117
|
+
{!streaming && (
|
|
118
|
+
<button type="button" className="asui-code-copy" onClick={copy}>
|
|
119
|
+
{copied ? '已复制' : '复制'}
|
|
120
|
+
</button>
|
|
121
|
+
)}
|
|
122
|
+
</div>
|
|
123
|
+
<pre>
|
|
124
|
+
<code>{code}</code>
|
|
125
|
+
</pre>
|
|
126
|
+
</div>
|
|
127
|
+
)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** 单块渲染,供 MarkdownStream 做浅比较跳过未变化的块。 */
|
|
131
|
+
export const MarkdownBlockView = memo(
|
|
132
|
+
function MarkdownBlockView({
|
|
133
|
+
block,
|
|
134
|
+
streaming,
|
|
135
|
+
}: {
|
|
136
|
+
block: MdBlock
|
|
137
|
+
streaming: boolean
|
|
138
|
+
}) {
|
|
139
|
+
return <>{renderBlock(block, streaming)}</>
|
|
140
|
+
},
|
|
141
|
+
(prev, next) =>
|
|
142
|
+
prev.streaming === next.streaming &&
|
|
143
|
+
prev.block.kind === next.block.kind &&
|
|
144
|
+
prev.block.closed === next.block.closed &&
|
|
145
|
+
prev.block.level === next.block.level &&
|
|
146
|
+
prev.block.lang === next.block.lang &&
|
|
147
|
+
prev.block.ordered === next.block.ordered &&
|
|
148
|
+
prev.block.text === next.block.text,
|
|
149
|
+
)
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import type { ReactNode } from 'react'
|
|
2
|
+
import type { AgentStreamStore } from '../core/store'
|
|
3
|
+
import type { SseStatus } from '../types'
|
|
4
|
+
import { useAgentStream } from './use-agent-stream'
|
|
5
|
+
import { MessageList } from './message-list'
|
|
6
|
+
import { Composer } from './composer'
|
|
7
|
+
import { SparkIcon } from './icons'
|
|
8
|
+
|
|
9
|
+
export interface AgentChatProps {
|
|
10
|
+
store: AgentStreamStore
|
|
11
|
+
title?: string
|
|
12
|
+
subtitle?: string
|
|
13
|
+
placeholder?: string
|
|
14
|
+
suggestions?: readonly string[]
|
|
15
|
+
footer?: ReactNode
|
|
16
|
+
emptyState?: ReactNode
|
|
17
|
+
className?: string
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const STATUS_TEXT: Record<SseStatus, string> = {
|
|
21
|
+
idle: '就绪',
|
|
22
|
+
connecting: '连接中',
|
|
23
|
+
open: '生成中',
|
|
24
|
+
reconnecting: '重连中',
|
|
25
|
+
closed: '就绪',
|
|
26
|
+
stopped: '已停止',
|
|
27
|
+
error: '连接异常',
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function AgentChat({
|
|
31
|
+
store,
|
|
32
|
+
title = 'Agent 对话',
|
|
33
|
+
subtitle,
|
|
34
|
+
placeholder,
|
|
35
|
+
suggestions = [],
|
|
36
|
+
footer,
|
|
37
|
+
emptyState,
|
|
38
|
+
className,
|
|
39
|
+
}: AgentChatProps) {
|
|
40
|
+
const snapshot = useAgentStream(store)
|
|
41
|
+
const running = snapshot.live?.status === 'running'
|
|
42
|
+
const canRetry =
|
|
43
|
+
snapshot.live?.status === 'error' ||
|
|
44
|
+
snapshot.status === 'error' ||
|
|
45
|
+
snapshot.messages.at(-1)?.status === 'error'
|
|
46
|
+
|
|
47
|
+
const tone =
|
|
48
|
+
snapshot.status === 'error'
|
|
49
|
+
? 'error'
|
|
50
|
+
: snapshot.status === 'reconnecting'
|
|
51
|
+
? 'warn'
|
|
52
|
+
: running
|
|
53
|
+
? 'busy'
|
|
54
|
+
: 'ok'
|
|
55
|
+
|
|
56
|
+
const reconnectLabel =
|
|
57
|
+
snapshot.status === 'reconnecting' && snapshot.reconnect
|
|
58
|
+
? ` · 第 ${snapshot.reconnect.attempt} 次`
|
|
59
|
+
: ''
|
|
60
|
+
|
|
61
|
+
return (
|
|
62
|
+
<div className={className ? `asui-chat ${className}` : 'asui-chat'}>
|
|
63
|
+
<header className="asui-header">
|
|
64
|
+
<div className="asui-brand">
|
|
65
|
+
<span className="asui-brand-mark">
|
|
66
|
+
<SparkIcon width={14} height={14} />
|
|
67
|
+
</span>
|
|
68
|
+
<div>
|
|
69
|
+
<h1 className="asui-title">{title}</h1>
|
|
70
|
+
{subtitle && <p className="asui-subtitle">{subtitle}</p>}
|
|
71
|
+
</div>
|
|
72
|
+
</div>
|
|
73
|
+
|
|
74
|
+
<span className={`asui-badge asui-badge--${tone}`} data-testid="status-badge">
|
|
75
|
+
<i />
|
|
76
|
+
{STATUS_TEXT[snapshot.status]}
|
|
77
|
+
{reconnectLabel}
|
|
78
|
+
</span>
|
|
79
|
+
</header>
|
|
80
|
+
|
|
81
|
+
<MessageList
|
|
82
|
+
snapshot={snapshot}
|
|
83
|
+
onRetry={store.retry}
|
|
84
|
+
emptyState={
|
|
85
|
+
emptyState ?? (
|
|
86
|
+
<div className="asui-empty-inner">
|
|
87
|
+
<SparkIcon width={22} height={22} />
|
|
88
|
+
<h2>问点需要查证的问题</h2>
|
|
89
|
+
<p>工具调用会以卡片形式展开,失败可以重试,断线会自动续传。</p>
|
|
90
|
+
{suggestions.length > 0 && (
|
|
91
|
+
<div className="asui-suggestions">
|
|
92
|
+
{suggestions.map((suggestion) => (
|
|
93
|
+
<button
|
|
94
|
+
key={suggestion}
|
|
95
|
+
type="button"
|
|
96
|
+
className="asui-suggestion"
|
|
97
|
+
onClick={() => store.send(suggestion)}
|
|
98
|
+
>
|
|
99
|
+
{suggestion}
|
|
100
|
+
</button>
|
|
101
|
+
))}
|
|
102
|
+
</div>
|
|
103
|
+
)}
|
|
104
|
+
</div>
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
/>
|
|
108
|
+
|
|
109
|
+
<Composer
|
|
110
|
+
running={running === true}
|
|
111
|
+
canRetry={canRetry === true}
|
|
112
|
+
placeholder={placeholder ?? '发消息…'}
|
|
113
|
+
onSend={store.send}
|
|
114
|
+
onStop={store.stop}
|
|
115
|
+
onRetry={store.retry}
|
|
116
|
+
/>
|
|
117
|
+
|
|
118
|
+
{footer && <div className="asui-footer">{footer}</div>}
|
|
119
|
+
</div>
|
|
120
|
+
)
|
|
121
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { useLayoutEffect, useRef, useState, type KeyboardEvent } from 'react'
|
|
2
|
+
import { RetryIcon, SendIcon, SparkIcon, StopIcon } from './icons'
|
|
3
|
+
|
|
4
|
+
export interface ComposerProps {
|
|
5
|
+
running: boolean
|
|
6
|
+
canRetry: boolean
|
|
7
|
+
placeholder?: string
|
|
8
|
+
disabled?: boolean
|
|
9
|
+
onSend: (text: string) => void
|
|
10
|
+
onStop: () => void
|
|
11
|
+
onRetry: () => void
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const MAX_HEIGHT_PX = 200
|
|
15
|
+
|
|
16
|
+
export function Composer({
|
|
17
|
+
running,
|
|
18
|
+
canRetry,
|
|
19
|
+
placeholder = '发消息…',
|
|
20
|
+
disabled = false,
|
|
21
|
+
onSend,
|
|
22
|
+
onStop,
|
|
23
|
+
onRetry,
|
|
24
|
+
}: ComposerProps) {
|
|
25
|
+
const [value, setValue] = useState('')
|
|
26
|
+
const textareaRef = useRef<HTMLTextAreaElement | null>(null)
|
|
27
|
+
|
|
28
|
+
// 自适应高度:先归零再读 scrollHeight,否则文本删短时不会回缩
|
|
29
|
+
useLayoutEffect(() => {
|
|
30
|
+
const element = textareaRef.current
|
|
31
|
+
if (!element) return
|
|
32
|
+
element.style.height = 'auto'
|
|
33
|
+
element.style.height = `${Math.min(element.scrollHeight, MAX_HEIGHT_PX)}px`
|
|
34
|
+
}, [value])
|
|
35
|
+
|
|
36
|
+
const submit = () => {
|
|
37
|
+
const text = value.trim()
|
|
38
|
+
if (text === '' || running || disabled) return
|
|
39
|
+
setValue('')
|
|
40
|
+
onSend(text)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const handleKeyDown = (event: KeyboardEvent<HTMLTextAreaElement>) => {
|
|
44
|
+
if (event.key === 'Enter' && !event.shiftKey && !event.nativeEvent.isComposing) {
|
|
45
|
+
event.preventDefault()
|
|
46
|
+
submit()
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return (
|
|
51
|
+
<div className="asui-composer">
|
|
52
|
+
<div className="asui-composer-box">
|
|
53
|
+
<SparkIcon className="asui-composer-spark" />
|
|
54
|
+
<textarea
|
|
55
|
+
ref={textareaRef}
|
|
56
|
+
className="asui-composer-input"
|
|
57
|
+
value={value}
|
|
58
|
+
rows={1}
|
|
59
|
+
placeholder={placeholder}
|
|
60
|
+
disabled={disabled}
|
|
61
|
+
onChange={(event) => setValue(event.target.value)}
|
|
62
|
+
onKeyDown={handleKeyDown}
|
|
63
|
+
aria-label="输入消息"
|
|
64
|
+
/>
|
|
65
|
+
|
|
66
|
+
{running ? (
|
|
67
|
+
<button
|
|
68
|
+
type="button"
|
|
69
|
+
className="asui-btn asui-btn--stop"
|
|
70
|
+
onClick={onStop}
|
|
71
|
+
title="停止生成"
|
|
72
|
+
aria-label="停止生成"
|
|
73
|
+
>
|
|
74
|
+
<StopIcon />
|
|
75
|
+
</button>
|
|
76
|
+
) : canRetry ? (
|
|
77
|
+
<button
|
|
78
|
+
type="button"
|
|
79
|
+
className="asui-btn asui-btn--retry"
|
|
80
|
+
onClick={onRetry}
|
|
81
|
+
title="重试"
|
|
82
|
+
aria-label="重试"
|
|
83
|
+
>
|
|
84
|
+
<RetryIcon />
|
|
85
|
+
</button>
|
|
86
|
+
) : (
|
|
87
|
+
<button
|
|
88
|
+
type="button"
|
|
89
|
+
className="asui-btn asui-btn--send"
|
|
90
|
+
onClick={submit}
|
|
91
|
+
disabled={value.trim() === '' || disabled}
|
|
92
|
+
title="发送"
|
|
93
|
+
aria-label="发送"
|
|
94
|
+
>
|
|
95
|
+
<SendIcon />
|
|
96
|
+
</button>
|
|
97
|
+
)}
|
|
98
|
+
</div>
|
|
99
|
+
|
|
100
|
+
<p className="asui-composer-hint">
|
|
101
|
+
<kbd>Enter</kbd> 发送 · <kbd>Shift</kbd> + <kbd>Enter</kbd> 换行
|
|
102
|
+
</p>
|
|
103
|
+
</div>
|
|
104
|
+
)
|
|
105
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { SVGProps } from 'react'
|
|
2
|
+
|
|
3
|
+
type IconProps = SVGProps<SVGSVGElement>
|
|
4
|
+
|
|
5
|
+
const base = {
|
|
6
|
+
width: 16,
|
|
7
|
+
height: 16,
|
|
8
|
+
viewBox: '0 0 24 24',
|
|
9
|
+
fill: 'none',
|
|
10
|
+
stroke: 'currentColor',
|
|
11
|
+
strokeWidth: 1.8,
|
|
12
|
+
strokeLinecap: 'round' as const,
|
|
13
|
+
strokeLinejoin: 'round' as const,
|
|
14
|
+
'aria-hidden': true,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function SendIcon(props: IconProps) {
|
|
18
|
+
return (
|
|
19
|
+
<svg {...base} {...props}>
|
|
20
|
+
<path d="M4 11.5 20 4l-7.5 16-1.8-6.2L4 11.5Z" />
|
|
21
|
+
</svg>
|
|
22
|
+
)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function StopIcon(props: IconProps) {
|
|
26
|
+
return (
|
|
27
|
+
<svg {...base} {...props}>
|
|
28
|
+
<rect x="6" y="6" width="12" height="12" rx="2" fill="currentColor" stroke="none" />
|
|
29
|
+
</svg>
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export function RetryIcon(props: IconProps) {
|
|
34
|
+
return (
|
|
35
|
+
<svg {...base} {...props}>
|
|
36
|
+
<path d="M20 12a8 8 0 1 1-2.4-5.7" />
|
|
37
|
+
<path d="M20 4v4h-4" />
|
|
38
|
+
</svg>
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function ToolIcon(props: IconProps) {
|
|
43
|
+
return (
|
|
44
|
+
<svg {...base} {...props}>
|
|
45
|
+
<path d="M14.7 6.3a4 4 0 0 0 5 5l-8.4 8.4a2.4 2.4 0 0 1-3.4-3.4l8.4-8.4a4 4 0 0 0-1.6-1.6Z" />
|
|
46
|
+
</svg>
|
|
47
|
+
)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function CheckIcon(props: IconProps) {
|
|
51
|
+
return (
|
|
52
|
+
<svg {...base} {...props}>
|
|
53
|
+
<path d="m5 13 4 4L19 7" />
|
|
54
|
+
</svg>
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function AlertIcon(props: IconProps) {
|
|
59
|
+
return (
|
|
60
|
+
<svg {...base} {...props}>
|
|
61
|
+
<path d="M12 8v5" />
|
|
62
|
+
<path d="M12 16.5v.5" />
|
|
63
|
+
<path d="M10.3 4.3 2.9 17.4A2 2 0 0 0 4.6 20h14.8a2 2 0 0 0 1.7-2.6L13.7 4.3a2 2 0 0 0-3.4 0Z" />
|
|
64
|
+
</svg>
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function ArrowDownIcon(props: IconProps) {
|
|
69
|
+
return (
|
|
70
|
+
<svg {...base} {...props}>
|
|
71
|
+
<path d="M12 5v14" />
|
|
72
|
+
<path d="m6 13 6 6 6-6" />
|
|
73
|
+
</svg>
|
|
74
|
+
)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function SparkIcon(props: IconProps) {
|
|
78
|
+
return (
|
|
79
|
+
<svg {...base} {...props}>
|
|
80
|
+
<path d="M12 3v4M12 17v4M3 12h4M17 12h4M6 6l2.5 2.5M15.5 15.5 18 18M18 6l-2.5 2.5M8.5 15.5 6 18" />
|
|
81
|
+
</svg>
|
|
82
|
+
)
|
|
83
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { memo, useMemo } from 'react'
|
|
2
|
+
import { splitBlocks } from '../markdown/blocks'
|
|
3
|
+
import { MarkdownBlockView } from '../markdown/render'
|
|
4
|
+
|
|
5
|
+
export interface MarkdownStreamProps {
|
|
6
|
+
text: string
|
|
7
|
+
/** 是否还在流式追加:只有尾块会重新解析,并显示光标。 */
|
|
8
|
+
streaming?: boolean
|
|
9
|
+
className?: string
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 流式 Markdown。
|
|
14
|
+
*
|
|
15
|
+
* 成本模型:每次文本变化 O(n) 切块(纯字符串扫描,无正则回溯),
|
|
16
|
+
* 解析成本只落在未闭合的尾块上;已闭合块命中缓存后只做一次元素复用。
|
|
17
|
+
*/
|
|
18
|
+
export const MarkdownStream = memo(function MarkdownStream({
|
|
19
|
+
text,
|
|
20
|
+
streaming = false,
|
|
21
|
+
className,
|
|
22
|
+
}: MarkdownStreamProps) {
|
|
23
|
+
const blocks = useMemo(() => splitBlocks(text), [text])
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className={className ? `asui-md ${className}` : 'asui-md'}>
|
|
27
|
+
{blocks.map((block) => (
|
|
28
|
+
<MarkdownBlockView
|
|
29
|
+
key={block.key}
|
|
30
|
+
block={block}
|
|
31
|
+
streaming={streaming && !block.closed}
|
|
32
|
+
/>
|
|
33
|
+
))}
|
|
34
|
+
{streaming && <span className="asui-caret" aria-hidden="true" />}
|
|
35
|
+
</div>
|
|
36
|
+
)
|
|
37
|
+
})
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { Message, MessagePart, ToolCallState } from '../types'
|
|
2
|
+
import { MarkdownStream } from './markdown-stream'
|
|
3
|
+
import { ToolCallCard } from './tool-call-card'
|
|
4
|
+
import { AlertIcon } from './icons'
|
|
5
|
+
|
|
6
|
+
export interface MessageBodyProps {
|
|
7
|
+
parts: MessagePart[]
|
|
8
|
+
toolCalls: ToolCallState[]
|
|
9
|
+
streaming: boolean
|
|
10
|
+
role: Message['role']
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** 按到达顺序渲染「文本 / 工具卡」,而不是把工具卡统一堆到末尾。 */
|
|
14
|
+
export function MessageBody({ parts, toolCalls, streaming, role }: MessageBodyProps) {
|
|
15
|
+
const byId = new Map(toolCalls.map((tool) => [tool.callId, tool]))
|
|
16
|
+
|
|
17
|
+
return (
|
|
18
|
+
<>
|
|
19
|
+
{parts.map((part, index) =>
|
|
20
|
+
part.kind === 'text' ? (
|
|
21
|
+
<MarkdownStream
|
|
22
|
+
key={`text-${index}`}
|
|
23
|
+
text={part.text}
|
|
24
|
+
streaming={streaming && index === parts.length - 1}
|
|
25
|
+
/>
|
|
26
|
+
) : (
|
|
27
|
+
<ToolCallCard key={`tool-${part.callId}`} tool={byId.get(part.callId) ?? missingTool(part.callId)} />
|
|
28
|
+
),
|
|
29
|
+
)}
|
|
30
|
+
{role === 'assistant' && streaming && parts.length === 0 && (
|
|
31
|
+
<div className="asui-typing" data-testid="typing-indicator" aria-label="正在生成">
|
|
32
|
+
<span />
|
|
33
|
+
<span />
|
|
34
|
+
<span />
|
|
35
|
+
</div>
|
|
36
|
+
)}
|
|
37
|
+
</>
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function missingTool(callId: string): ToolCallState {
|
|
42
|
+
return {
|
|
43
|
+
callId,
|
|
44
|
+
name: 'unknown_tool',
|
|
45
|
+
args: '',
|
|
46
|
+
argsComplete: true,
|
|
47
|
+
status: 'error',
|
|
48
|
+
preview: '工具调用状态缺失',
|
|
49
|
+
startedAt: 0,
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export function ErrorNote({ message, stage }: { message: string; stage: string }) {
|
|
54
|
+
return (
|
|
55
|
+
<div className="asui-error" role="alert">
|
|
56
|
+
<AlertIcon />
|
|
57
|
+
<div>
|
|
58
|
+
<strong>本轮失败</strong>
|
|
59
|
+
<p>
|
|
60
|
+
{message}
|
|
61
|
+
{stage !== 'unknown' ? `(阶段:${stage})` : ''}
|
|
62
|
+
</p>
|
|
63
|
+
</div>
|
|
64
|
+
</div>
|
|
65
|
+
)
|
|
66
|
+
}
|