Skip to content

Commit 872a598

Browse files
committed
feat: 实现大模型动态切换路由功能
1 parent 97f7e13 commit 872a598

File tree

9 files changed

+92
-37
lines changed

9 files changed

+92
-37
lines changed

examples/sites/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"@docsearch/css": "^3.8.0",
2828
"@docsearch/js": "^3.8.0",
2929
"@docsearch/react": "npm:@docsearch/css",
30-
"@opentiny/next-sdk": "0.0.1-alpha.2",
30+
"@opentiny/next-sdk": "0.0.1-alpha.4",
3131
"@opentiny/tiny-robot": "0.3.0-alpha.3",
3232
"@opentiny/tiny-robot-kit": "0.3.0-alpha.3",
3333
"@opentiny/tiny-robot-svgs": "0.3.0-alpha.3",
@@ -66,7 +66,8 @@
6666
"tailwindcss": "^3.2.4",
6767
"vue": "^3.4.31",
6868
"vue-i18n": "~9.14.3",
69-
"vue-router": "4.1.5"
69+
"vue-router": "4.1.5",
70+
"zod": "^3.24.4"
7071
},
7172
"devDependencies": {
7273
"@opentiny-internal/unplugin-virtual-template": "workspace:~",

examples/sites/src/App.vue

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
</template>
1616

17-
<script setup>
17+
<script setup lang="ts">
1818
import { onMounted, provide, ref } from 'vue'
1919
import { TinyConfigProvider, TinyModal } from '@opentiny/vue'
2020
import useTheme from './tools/useTheme'
@@ -23,9 +23,10 @@ import { IconAi } from '@opentiny/tiny-robot-svgs'
2323
import { showTinyRobot } from './composable/utils'
2424
import { createServer, createInMemoryTransport } from '@opentiny/next-sdk'
2525
26+
import { createGlobalMcpTool } from './tools/globalMcpTool'
27+
2628
const previewUrl = ref(import.meta.env.VITE_PLAYGROUND_URL)
2729
const modalSHow = ref(false)
28-
2930
const server = createServer(
3031
{
3132
name: 'comprehensive-config',
@@ -41,26 +42,7 @@ const server = createServer(
4142
4243
server.use(createInMemoryTransport())
4344
44-
// 长任务示例
45-
server.registerTool(
46-
'long-task',
47-
{
48-
title: 'long-task',
49-
description: '可以帮用户订机票'
50-
},
51-
async () => {
52-
// 执行一个长任务
53-
await new Promise((resolve) => setTimeout(resolve, 10000))
54-
return {
55-
content: [
56-
{
57-
type: 'text',
58-
text: '执行一个长任务,执行完成'
59-
}
60-
]
61-
}
62-
}
63-
)
45+
createGlobalMcpTool(server)
6446
6547
onMounted(() => {
6648
server.connectTransport()

examples/sites/src/menus.jsx renamed to examples/sites/src/menus.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ function genMenus() {
5050
type: 'components'
5151
}))
5252
}))
53-
5453
return [...standaloneOptions, ...docOptions, ...cmpOptions]
5554
}
5655

examples/sites/src/router.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,5 @@ router.afterEach((to, from) => {
5757
if (to.meta.title) {
5858
document.title = to.meta.title
5959
}
60-
// tiny-robot 通过路由,确定浮动区,是否显示AI按钮
61-
appData.hasFloatRobot = to.path.endsWith('components/grid')
6260
})
6361
export { router }
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import { genMenus } from '../menus'
2+
import { z } from 'zod'
3+
import { useRouter } from 'vue-router'
4+
5+
export const createGlobalMcpTool = (server) => {
6+
const router = useRouter()
7+
server.registerResource(
8+
'site-menus',
9+
'site-menus://app',
10+
{
11+
title: 'TinyVue官网的菜单数据',
12+
description: 'TinyVue官网的菜单数据,其中"key"为路由路径,"name"为菜单名称,"children"为子菜单',
13+
mimeType: 'text/plain'
14+
},
15+
async (uri) => ({
16+
contents: [
17+
{
18+
uri: uri.href,
19+
text: JSON.stringify(genMenus())
20+
}
21+
]
22+
})
23+
)
24+
// 帮我查看button组件的API
25+
server.registerTool(
26+
'swtich-router',
27+
{
28+
title: 'router',
29+
description: '可以帮用户跳转页面,比如:跳转到组件文档页面和API文档页面',
30+
inputSchema: {
31+
key: z.string().describe('跳转页面路径'),
32+
type: z.enum(['components', 'docs', 'overview', 'features']).describe('跳转页面类型'),
33+
isOpenApi: z.boolean().describe('是否打开API文档')
34+
}
35+
},
36+
async ({ key, type, isOpenApi }) => {
37+
const { params, fullPath } = router.currentRoute.value
38+
const { theme } = params
39+
const themeIndex = fullPath.indexOf(theme)
40+
const linkUrl =
41+
fullPath.slice(0, themeIndex) + `${theme}/${type}/${key === 'overview' ? '' : key}${isOpenApi ? '#api' : ''}`
42+
router.push(linkUrl)
43+
return {
44+
content: [
45+
{
46+
type: 'text',
47+
text: `跳转页面成功: ${key}`
48+
}
49+
]
50+
}
51+
}
52+
)
53+
54+
// 长任务示例
55+
server.registerTool(
56+
'long-task',
57+
{
58+
title: 'long-task',
59+
description: '可以帮用户订机票'
60+
},
61+
async () => {
62+
// 执行一个长任务
63+
await new Promise((resolve) => setTimeout(resolve, 10000))
64+
return {
65+
content: [
66+
{
67+
type: 'text',
68+
text: '执行一个长任务,执行完成'
69+
}
70+
]
71+
}
72+
}
73+
)
74+
}

examples/sites/src/tools/useAllTaskFinish.ts

Whitespace-only changes.

examples/sites/src/tools/useBulletin.jsx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,16 @@ export function useBulletin() {
2222
onMounted(() => {
2323
Modal.confirm({
2424
title: '公告',
25-
message: (<div style="font-size:16px;line-height:1.5;">
26-
<div>尊敬的 TinyVue 用户:</div>
27-
<p style="text-indent: 2em;" v-html={lastBulletin.content}>
28-
</p>
29-
<div style="text-align:right;margin-top:20px">TinyVue 团队</div>
30-
<div style="text-align:right;">{ lastBulletin.time }</div>
31-
</div>),
25+
message: (
26+
<div style="font-size:16px;line-height:1.5;">
27+
<div>尊敬的 TinyVue 用户:</div>
28+
<p style="text-indent: 2em;" v-html={lastBulletin.content}></p>
29+
<div style="text-align:right;margin-top:20px">TinyVue 团队</div>
30+
<div style="text-align:right;">{lastBulletin.time}</div>
31+
</div>
32+
),
3233
status: null,
33-
width:'760',
34+
width: '760',
3435
confirmContent: '我知道了',
3536
cancelContent: '关闭'
3637
}).then((res) => {

examples/sites/src/tools/useTemplateMode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { reactive, computed, watch } from 'vue'
22
import { router } from '@/router.js'
3-
import { getAllComponents } from '@/menus.jsx'
3+
import { getAllComponents } from '@/menus'
44
import demoConfig from '@demos/config.js'
55
import { staticDemoPath } from '../views/components-doc/cmp-config'
66

examples/sites/src/views/layout/layout.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
import { useRoute } from 'vue-router'
6060
import { defineComponent, reactive, computed, toRefs, watch, onMounted, onUnmounted } from 'vue'
6161
import { TreeMenu, Dropdown, DropdownMenu, Tooltip, Tag, Radio, RadioGroup, Button } from '@opentiny/vue'
62-
import { genMenus, getMenuIcons } from '@/menus.jsx'
62+
import { genMenus, getMenuIcons } from '@/menus'
6363
import { router } from '@/router.js'
6464
import { getWord, i18nByKey, appData, appFn, useApiMode, useTemplateMode } from '@/tools'
6565
import useTheme from '@/tools/useTheme'

0 commit comments

Comments
 (0)