@opentiny/next-sdk 0.2.5 → 0.2.6-beta.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.
@@ -0,0 +1,114 @@
1
+ import { ZodRawShape } from 'zod';
2
+ import { RegisteredTool } from '@modelcontextprotocol/sdk/server/mcp.js';
3
+ import { ToolAnnotations } from '@modelcontextprotocol/sdk/types.js';
4
+ import { WebMcpServer } from './WebMcpServer';
5
+
6
+ /**
7
+ * 注册应用的导航函数,通常在应用入口(如 main.ts)调用一次。
8
+ * @param fn 导航函数,接收路由路径并执行跳转(如 router.push)
9
+ */
10
+ export declare function setNavigator(fn: (route: string) => void | Promise<void>): void;
11
+ /**
12
+ * registerTool 第三个参数的路由配置对象类型。
13
+ * 当传入此类型时,工具调用会自动跳转到 route 对应的页面并通过消息通信执行。
14
+ */
15
+ export type RouteConfig = {
16
+ /** 目标路由路径,如 '/comprehensive' */
17
+ route: string;
18
+ /** 等待页面响应的超时时间(ms),默认 30000 */
19
+ timeout?: number;
20
+ };
21
+ /**
22
+ * PageAwareServer 的 registerTool 配置对象类型,与 WebMcpServer.registerTool 保持一致。
23
+ */
24
+ type RegisterToolConfig<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape> = {
25
+ title?: string;
26
+ description?: string;
27
+ inputSchema?: InputArgs;
28
+ outputSchema?: OutputArgs;
29
+ annotations?: ToolAnnotations;
30
+ };
31
+ /**
32
+ * 包装 WebMcpServer 后的类型:registerTool 第三个参数额外支持 RouteConfig。
33
+ * 泛型签名与 WebMcpServer.registerTool 对齐,保持完整的类型推导能力。
34
+ * 原有的回调函数写法完全兼容,无需改动。
35
+ */
36
+ export type PageAwareServer = Omit<WebMcpServer, 'registerTool'> & {
37
+ registerTool<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(name: string, config: RegisterToolConfig<InputArgs, OutputArgs>, handlerOrRoute: ((...args: any[]) => any) | RouteConfig): RegisteredTool;
38
+ };
39
+ /**
40
+ * 包装 WebMcpServer,使 registerTool 第三个参数支持 RouteConfig。
41
+ *
42
+ * - 第三个参数为**回调函数**:与原始 registerTool 完全一致,直接透传
43
+ * - 第三个参数为 **RouteConfig 对象**:自动生成转发 handler,工具调用时
44
+ * 先导航到目标路由,再通过 postMessage 与页面通信
45
+ *
46
+ * @example
47
+ * const server = withPageTools(new WebMcpServer())
48
+ *
49
+ * // 路由模式:第三个参数传路由配置
50
+ * server.registerTool('product-guide', { title, inputSchema }, { route: '/comprehensive' })
51
+ *
52
+ * // 普通模式:第三个参数传回调(兼容原有写法)
53
+ * server.registerTool('simple-tool', { title }, async (input) => ({ content: [...] }))
54
+ */
55
+ export declare function withPageTools(server: WebMcpServer): PageAwareServer;
56
+ /**
57
+ * 在目标页面激活工具处理器(框架无关的纯 JS 函数)。
58
+ *
59
+ * 调用后立即:
60
+ * - 将路由注册到 activePages(标记页面已激活)
61
+ * - 添加 message 监听,处理来自 buildPageHandler 的工具调用
62
+ * - 广播 page-ready 信号,通知正在等待导航完成的工具
63
+ *
64
+ * 返回 cleanup 函数,页面销毁时调用。
65
+ *
66
+ * @example
67
+ * // Vue(Composition API)— 省略 route,默认读 window.location.pathname
68
+ * let cleanup: () => void
69
+ * onMounted(() => { cleanup = registerPageTool({ handlers: { ... } }) })
70
+ * onUnmounted(() => cleanup())
71
+ *
72
+ * // Vue — 当页面路由与 pathname 不一致时,手动指定 route
73
+ * onMounted(() => { cleanup = registerPageTool({ route: '/comprehensive', handlers: { ... } }) })
74
+ * onUnmounted(() => cleanup())
75
+ *
76
+ * // React(Hooks)
77
+ * useEffect(() => registerPageTool({ handlers: { ... } }), [])
78
+ * // useEffect 直接返回 cleanup 函数,React 会在组件卸载时自动调用
79
+ *
80
+ * // Angular(实现 OnInit / OnDestroy 接口)
81
+ * export class PriceProtectionComponent implements OnInit, OnDestroy {
82
+ * private cleanupPageTool!: () => void
83
+ *
84
+ * ngOnInit(): void {
85
+ * this.cleanupPageTool = registerPageTool({
86
+ * handlers: {
87
+ * 'price-protection-query': async ({ status }) => { ... },
88
+ * }
89
+ * })
90
+ * }
91
+ *
92
+ * ngOnDestroy(): void {
93
+ * this.cleanupPageTool()
94
+ * }
95
+ * }
96
+ */
97
+ export declare function registerPageTool(options: {
98
+ /**
99
+ * 目标路由路径,与 RouteConfig.route 保持一致。
100
+ * 省略时自动读取 window.location.pathname。
101
+ * 当页面路由与 pathname 不一致时(如 hash 路由、子路径前缀等),需手动传入。
102
+ */
103
+ route?: string;
104
+ /**
105
+ * 工具名 → 处理函数的映射表。
106
+ *
107
+ * 此处 handler 的 input 参数类型保留 any:
108
+ * 若改为 unknown,TypeScript 函数参数逆变规则会导致用户的具名解构写法
109
+ *(如 `async ({ productId }: { productId: string }) => ...`)无法通过类型检查,
110
+ * 破坏现有调用方代码的开发体验。运行时输入由 MCP inputSchema 保证类型安全。
111
+ */
112
+ handlers: Record<string, (input: any) => Promise<any>>;
113
+ }): () => void;
114
+ export {};
@@ -21,10 +21,12 @@ export declare function parseSkillFrontMatter(content: string): {
21
21
  } | null;
22
22
  /**
23
23
  * 获取所有「主 SKILL.md」的路径(一级子目录下的 SKILL.md)
24
+ * - 对传入的 modules 先做 normalize,兼容任意 import.meta.glob 写法
24
25
  */
25
26
  export declare function getMainSkillPaths(modules: Record<string, string>): string[];
26
27
  /**
27
28
  * 获取所有技能的概况列表(name、description、path),用于 systemPrompt 或列表展示
29
+ * - 内部统一对 modules 做 normalize,避免调用方关心路径细节
28
30
  */
29
31
  export declare function getSkillOverviews(modules: Record<string, string>): SkillMeta[];
30
32
  /**
@@ -33,15 +35,18 @@ export declare function getSkillOverviews(modules: Record<string, string>): Skil
33
35
  */
34
36
  export declare function formatSkillsForSystemPrompt(skills: SkillMeta[]): string;
35
37
  /**
36
- * 获取所有已加载的 md 文件路径(含主 SKILL.md 与 reference 等)
38
+ * 获取所有已加载的技能文件路径(含主 SKILL.md 与 reference 下的 .md/.json/.xml 等)
39
+ * - 对 modules 做 normalize 后再返回 key 列表
37
40
  */
38
41
  export declare function getSkillMdPaths(modules: Record<string, string>): string[];
39
42
  /**
40
- * 根据相对路径获取某个 md 文档的原始内容
43
+ * 根据相对路径获取某个技能文档的原始内容(支持 .md、.json、.xml 等文本格式)
44
+ * - 自动对 modules 做 normalize,再按 path 查找
41
45
  */
42
46
  export declare function getSkillMdContent(modules: Record<string, string>, path: string): string | undefined;
43
47
  /**
44
48
  * 根据技能 name 查找其主 SKILL.md 的路径(name 与目录名一致)
49
+ * - 依赖 getMainSkillPaths,内部已做 normalize
45
50
  */
46
51
  export declare function getMainSkillPathByName(modules: Record<string, string>, name: string): string | undefined;
47
52
  /** AI SDK Tool 类型,用于 extraTools 合并,不写死泛型避免与 ai 包版本强绑定 */