Skip to content

Commit 2018281

Browse files
committed
feat:升级tinyrobot版本到0.4.0
1 parent 7cc98a1 commit 2018281

File tree

15 files changed

+715
-179
lines changed

15 files changed

+715
-179
lines changed

packages/plugins/robot/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929
"@opentiny/tiny-engine-common": "workspace:*",
3030
"@opentiny/tiny-engine-meta-register": "workspace:*",
3131
"@opentiny/tiny-engine-utils": "workspace:*",
32-
"@opentiny/tiny-robot": "0.3.1",
33-
"@opentiny/tiny-robot-kit": "0.3.1",
34-
"@opentiny/tiny-robot-svgs": "0.3.1",
32+
"@opentiny/tiny-robot": "0.4.0",
33+
"@opentiny/tiny-robot-kit": "0.4.0",
34+
"@opentiny/tiny-robot-svgs": "0.4.0",
3535
"@opentiny/tiny-schema-renderer": "1.0.0-beta.6",
3636
"@vueuse/core": "^9.13.0",
3737
"dompurify": "^3.0.1",

packages/plugins/robot/src/Main.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
v-model:fullscreen="fullscreen"
1919
v-model:show="robotVisible"
2020
v-model:input="inputMessage"
21-
:status="chatStatus"
21+
:status="mappedStatus"
2222
:prompt-items="promptItems"
2323
:bubble-renderers="bubbleRenderers"
2424
:allowFiles="isVisualModel && robotSettingState.chatMode === ChatMode.Agent"
@@ -147,7 +147,7 @@ const showTeleport = ref(false)
147147
const showSetting = ref(false)
148148
149149
const {
150-
chatStatus,
150+
mappedStatus,
151151
inputMessage,
152152
messages,
153153
changeChatMode,

packages/plugins/robot/src/components/chat/RobotChat.vue

Lines changed: 74 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
@item-click="handlePromptItemClick"
2323
></tr-prompts>
2424
</div>
25-
<tr-bubble-provider v-else :content-renderers="contentRenderers">
26-
<tr-bubble-list :items="messages" :roles="roles" auto-scroll class="robot-bubble-list"> </tr-bubble-list>
25+
<tr-bubble-provider v-else :box-rules="boxRules" :content-rules="contentRules">
26+
<tr-bubble-list :messages="messages" :role-configs="roleConfigs" auto-scroll class="robot-bubble-list"> </tr-bubble-list>
2727
</tr-bubble-provider>
2828
</div>
2929

@@ -39,9 +39,6 @@
3939
:showWordLimit="false"
4040
@submit="handleSendMessage"
4141
@cancel="handleAbortRequest"
42-
:allowFiles="selectedAttachments.length < 1 && props.allowFiles"
43-
uploadTooltip="支持上传1张图片"
44-
@files-selected="handleSingleFilesSelected"
4542
>
4643
<template #header v-if="selectedAttachments.length > 0">
4744
<div>
@@ -55,9 +52,23 @@
5552
</tr-attachments>
5653
</div>
5754
</template>
58-
<template #footer-left>
55+
<template #footer>
5956
<slot name="footer-left"></slot>
6057
</template>
58+
<template #footer-right>
59+
<VoiceButton
60+
:speech-config="{ lang: 'zh-CN', continuous: false }"
61+
@speech-start="handleSpeechStart"
62+
@speech-end="handleSpeechEnd"
63+
@speech-error="handleSpeechError"
64+
/>
65+
<UploadButton
66+
v-if="selectedAttachments.length < 1 && props.allowFiles"
67+
accept="image/*"
68+
:multiple="false"
69+
@select="handleSingleFilesSelected"
70+
/>
71+
</template>
6172
</tr-sender>
6273
</div>
6374
</template>
@@ -74,11 +85,14 @@ import {
7485
TrSender,
7586
TrWelcome,
7687
TrAttachments,
88+
UploadButton,
89+
VoiceButton,
7790
type BubbleRoleConfig,
7891
type PromptProps,
7992
type RawFileAttachment
8093
} from '@opentiny/tiny-robot'
81-
import { type ChatMessage, GeneratingStatus } from '@opentiny/tiny-robot-kit'
94+
import { type ChatMessage } from '@opentiny/tiny-robot-kit'
95+
import { GeneratingStatus } from '../../constants/status'
8296
import { LoadingRenderer, MarkdownRenderer, ImgRenderer } from '../renderers'
8397
import { useNotify } from '@opentiny/tiny-engine-meta-register'
8498
@@ -172,30 +186,72 @@ const handleSingleFileRetry = (file: RawFileAttachment) => {
172186
handleSingleFilesSelected([file.rawFile], true)
173187
}
174188
189+
// 语音输入处理
190+
const handleSpeechStart = () => {
191+
}
192+
193+
const handleSpeechEnd = (transcript: string) => {
194+
if (transcript) {
195+
inputMessage.value = transcript
196+
}
197+
}
198+
199+
const handleSpeechError = () => {
200+
useNotify({
201+
type: 'error',
202+
message: '语音识别失败,请重试'
203+
})
204+
}
205+
175206
const getSvgIcon = (name: string, style?: CSSProperties) => {
176207
return h(resolveComponent('svg-icon'), { name, style: { fontSize: '32px', ...style } })
177208
}
178209
const aiAvatar = getSvgIcon('AI')
179210
const welcomeIcon = getSvgIcon('AI', { fontSize: '48px' })
180211
181-
const contentRenderers = computed(() => ({
182-
markdown: MarkdownRenderer,
183-
loading: LoadingRenderer,
184-
img: ImgRenderer,
185-
...props.bubbleRenderers
186-
}))
212+
// const contentRenderers = computed(() => ({
213+
// markdown: MarkdownRenderer,
214+
// loading: LoadingRenderer,
215+
// img: ImgRenderer,
216+
// ...props.bubbleRenderers
217+
// }))
218+
219+
// 0.4.x 使用 match rules 配置渲染器
220+
const contentRules = computed(() => [
221+
{
222+
priority: 100,
223+
find: (message: any) => message?.renderContent?.[0]?.type === 'markdown' || message?.renderContent?.[0]?.type === 'text',
224+
renderer: MarkdownRenderer
225+
},
226+
{
227+
priority: 100,
228+
find: (message: any) => message?.renderContent?.[0]?.type === 'img' || message?.renderContent?.[0]?.type === 'image',
229+
renderer: ImgRenderer
230+
},
231+
{
232+
priority: 100,
233+
find: (message: any) => message?.renderContent?.[0]?.type === 'loading' || message?.renderContent?.[0]?.type === 'agent-loading',
234+
renderer: LoadingRenderer
235+
},
236+
// 支持自定义渲染器
237+
...Object.entries(props.bubbleRenderers).map(([type, renderer]) => ({
238+
priority: 50,
239+
find: (message: any) => message?.renderContent?.[0]?.type === type,
240+
renderer: renderer as any
241+
}))
242+
])
243+
244+
const boxRules = computed(() => [])
187245
188-
const roles: Record<string, BubbleRoleConfig> = {
246+
const roleConfigs: Record<string, BubbleRoleConfig> = {
189247
assistant: {
190248
placement: 'start',
191249
avatar: aiAvatar,
192-
contentRenderer: MarkdownRenderer,
193-
customContentField: 'renderContent'
250+
contentResolver: (message: any) => message.renderContent || message.content
194251
},
195252
user: {
196253
placement: 'end',
197-
contentRenderer: MarkdownRenderer,
198-
customContentField: 'renderContent'
254+
contentResolver: (message: any) => message.renderContent || message.content
199255
},
200256
system: {
201257
hidden: true

packages/plugins/robot/src/components/header-extension/History.vue

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
@click="showHistory = false"
1212
/>
1313
<tr-history
14-
:selected="props.conversationState.currentId || undefined"
14+
:selected="(('value' in props.conversationState ? props.conversationState.value : props.conversationState)?.currentId) || undefined"
1515
:search-bar="true"
1616
:data="conversationsData"
1717
@item-action="handleHistoryItemAction"
@@ -35,7 +35,14 @@ import { computed, ref } from 'vue'
3535
const showHistory = ref(false)
3636
3737
interface HistoryProps {
38+
// 0.4.x 中 conversationState 是一个 computed ref
3839
conversationState: {
40+
value: {
41+
currentId?: string | null
42+
conversations: Conversation[]
43+
}
44+
} | {
45+
// 兼容旧版本
3946
currentId?: string | null
4047
conversations: Conversation[]
4148
}
@@ -81,7 +88,9 @@ const convertFlatToGrouped = (flatData: Conversation[]): Array<{ group: string;
8188
}
8289
8390
const conversationsData = computed(() => {
84-
return convertFlatToGrouped(props.conversationState.conversations)
91+
// 兼容新旧两种 conversationState 结构
92+
const state = 'value' in props.conversationState ? props.conversationState.value : props.conversationState
93+
return convertFlatToGrouped(state.conversations)
8594
})
8695
8796
const emit = defineEmits<{

packages/plugins/robot/src/components/renderers/AgentRenderer.vue

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export default {
2727
},
2828
setup(props) {
2929
const getIconUrl = (icon: string) => {
30+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
31+
// @ts-ignore
3032
return new URL(`../../../assets/${icon}`, import.meta.url).href
3133
}
3234

0 commit comments

Comments
 (0)