@movk/nuxt-docs 1.6.0 → 1.6.2

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.
@@ -0,0 +1,9 @@
1
+ export function useToolCall() {
2
+ const tools: Record<string, string | ((args: any) => string)> = {
3
+ 'list-pages': '列出所有文档页面',
4
+ 'get-page': (args: any) => `检索 ${args?.path || '页面'}`
5
+ }
6
+ return {
7
+ tools
8
+ }
9
+ }
@@ -1,3 +1,5 @@
1
+ import { existsSync } from 'node:fs'
2
+ import { join } from 'node:path'
1
3
  import { addComponentsDir, addImportsDir, addServerHandler, createResolver, defineNuxtModule } from '@nuxt/kit'
2
4
 
3
5
  export interface AiChatModuleOptions {
@@ -65,11 +67,28 @@ export default defineNuxtModule<AiChatModuleOptions>({
65
67
 
66
68
  addImportsDir(resolve('runtime/composables'))
67
69
 
70
+ /**
71
+ * 检查用户项目中是否存在自定义 handler
72
+ * '/api/ai-chat' -> 'api/ai-chat','/custom' -> 'custom'
73
+ */
68
74
  const routePath = options.apiPath!.replace(/^\//, '')
69
- addServerHandler({
70
- route: `/${routePath}`,
71
- handler: resolve('./runtime/server/api/search')
72
- })
75
+ const extensions = ['ts', 'js', 'mjs']
76
+
77
+ const possibleHandlerPaths = extensions.flatMap(ext => [
78
+ join(nuxt.options.serverDir, `${routePath}.${ext}`),
79
+ join(nuxt.options.serverDir, routePath, `index.${ext}`)
80
+ ])
81
+
82
+ const hasCustomHandler = possibleHandlerPaths.some(path => existsSync(path))
83
+
84
+ if (!hasCustomHandler) {
85
+ addServerHandler({
86
+ route: options.apiPath!,
87
+ handler: resolve('./runtime/server/api/ai-chat')
88
+ })
89
+ } else {
90
+ console.info(`[ai-chat] Using custom handler, skipping default handler registration`)
91
+ }
73
92
  }
74
93
  })
75
94
 
@@ -40,7 +40,17 @@ const components = {
40
40
  const { messages, isOpen, pendingMessage, clearPending } = useAIChat()
41
41
  const { apiPath } = useRuntimeConfig().public.aiChat
42
42
 
43
- const { getToolLabel } = useTools()
43
+ const { tools } = useToolCall()
44
+ function getToolLabel(toolName: string, args?: any): string {
45
+ const label = tools[toolName]
46
+
47
+ if (!label) {
48
+ return toolName
49
+ }
50
+
51
+ return typeof label === 'function' ? label(args) : label
52
+ }
53
+
44
54
  const { model } = useModels()
45
55
 
46
56
  const input = ref('')
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@movk/nuxt-docs",
3
3
  "type": "module",
4
- "version": "1.6.0",
4
+ "version": "1.6.2",
5
5
  "private": false,
6
6
  "description": "Modern Nuxt 4 documentation theme with auto-generated component docs, AI chat assistant, MCP server, and complete developer experience optimization.",
7
7
  "author": "YiXuan <mhaibaraai@gmail.com>",
@@ -1,31 +0,0 @@
1
- export interface ToolLabelConfig {
2
- toolName: string
3
- label: string | ((args: any) => string)
4
- }
5
-
6
- export function useTools() {
7
- const defaultLabels: Record<string, string | ((args: any) => string)> = {
8
- 'list-pages': '列出所有文档页面',
9
- 'get-page': (args: any) => `检索 ${args?.path || '页面'}`
10
- }
11
-
12
- /**
13
- * 获取工具的显示标签
14
- * @param toolName - 工具名称
15
- * @param args - 工具参数
16
- * @returns 工具的显示标签
17
- */
18
- function getToolLabel(toolName: string, args?: any): string {
19
- const label = defaultLabels[toolName]
20
-
21
- if (!label) {
22
- return toolName
23
- }
24
-
25
- return typeof label === 'function' ? label(args) : label
26
- }
27
-
28
- return {
29
- getToolLabel
30
- }
31
- }