@opentiny/next-sdk 0.2.6 → 0.2.7

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,132 @@
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
+ /** 页面卸载广播,供 pageToolsOnDemand 模式监听 */
7
+ export declare const MSG_PAGE_LEAVE = "next-sdk:page-leave";
8
+ /** iframe 内 Remoter 就绪后向父窗口发送,父窗口回传 route-state-initial */
9
+ export declare const MSG_REMOTER_READY = "next-sdk:remoter-ready";
10
+ /** 父窗口向 iframe Remoter 回传的初始路由状态(toolRouteMap + activeRoutes) */
11
+ export declare const MSG_ROUTE_STATE_INITIAL = "next-sdk:route-state-initial";
12
+ /**
13
+ * 获取通过 withPageTools + RouteConfig 注册的全部工具路由映射。
14
+ * 返回的是内部 Map 的只读快照,可安全遍历。
15
+ * @returns toolName → route 的只读 Map
16
+ */
17
+ export declare function getToolRouteMap(): ReadonlyMap<string, string>;
18
+ /**
19
+ * 获取当前已激活(已挂载)的路由集合。
20
+ * 即调用了 registerPageTool 且尚未执行 cleanup 的页面路由。
21
+ * @returns 当前激活路由的 Set 快照
22
+ */
23
+ export declare function getActiveRoutes(): Set<string>;
24
+ /**
25
+ * 注册应用的导航函数,通常在应用入口(如 main.ts)调用一次。
26
+ * @param fn 导航函数,接收路由路径并执行跳转(如 router.push)
27
+ */
28
+ export declare function setNavigator(fn: (route: string) => void | Promise<void>): void;
29
+ /**
30
+ * registerTool 第三个参数的路由配置对象类型。
31
+ * 当传入此类型时,工具调用会自动跳转到 route 对应的页面并通过消息通信执行。
32
+ */
33
+ export type RouteConfig = {
34
+ /** 目标路由路径,如 '/comprehensive' */
35
+ route: string;
36
+ /** 等待页面响应的超时时间(ms),默认 30000 */
37
+ timeout?: number;
38
+ };
39
+ /**
40
+ * PageAwareServer 的 registerTool 配置对象类型,与 WebMcpServer.registerTool 保持一致。
41
+ */
42
+ type RegisterToolConfig<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape> = {
43
+ title?: string;
44
+ description?: string;
45
+ inputSchema?: InputArgs;
46
+ outputSchema?: OutputArgs;
47
+ annotations?: ToolAnnotations;
48
+ };
49
+ /**
50
+ * 包装 WebMcpServer 后的类型:registerTool 第三个参数额外支持 RouteConfig。
51
+ * 泛型签名与 WebMcpServer.registerTool 对齐,保持完整的类型推导能力。
52
+ * 原有的回调函数写法完全兼容,无需改动。
53
+ */
54
+ export type PageAwareServer = Omit<WebMcpServer, 'registerTool'> & {
55
+ registerTool<InputArgs extends ZodRawShape, OutputArgs extends ZodRawShape>(name: string, config: RegisterToolConfig<InputArgs, OutputArgs>, handlerOrRoute: ((...args: any[]) => any) | RouteConfig): RegisteredTool;
56
+ };
57
+ /**
58
+ * 包装 WebMcpServer,使 registerTool 第三个参数支持 RouteConfig。
59
+ *
60
+ * - 第三个参数为**回调函数**:与原始 registerTool 完全一致,直接透传
61
+ * - 第三个参数为 **RouteConfig 对象**:自动生成转发 handler,工具调用时
62
+ * 先导航到目标路由,再通过 postMessage 与页面通信
63
+ *
64
+ * @example
65
+ * const server = withPageTools(new WebMcpServer())
66
+ *
67
+ * // 路由模式:第三个参数传路由配置(route 必填,timeout 可选,单位 ms,默认 30000)
68
+ * server.registerTool('product-guide', { title, inputSchema }, { route: '/comprehensive', timeout: 15000 })
69
+ *
70
+ * // 普通模式:第三个参数传回调(兼容原有写法)
71
+ * server.registerTool('simple-tool', { title }, async (input) => ({ content: [...] }))
72
+ */
73
+ export declare function withPageTools(server: WebMcpServer): PageAwareServer;
74
+ /**
75
+ * 在目标页面激活工具处理器(框架无关的纯 JS 函数)。
76
+ *
77
+ * 调用后立即:
78
+ * - 将路由注册到 activePages(标记页面已激活)
79
+ * - 添加 message 监听,处理来自 buildPageHandler 的工具调用
80
+ * - 广播 page-ready 信号,通知正在等待导航完成的工具
81
+ *
82
+ * 返回 cleanup 函数,页面销毁时调用。
83
+ *
84
+ * @example
85
+ * // Vue(Composition API)— 省略 route,默认读 window.location.pathname
86
+ * let cleanup: () => void
87
+ * onMounted(() => { cleanup = registerPageTool({ handlers: { ... } }) })
88
+ * onUnmounted(() => cleanup())
89
+ *
90
+ * // Vue — 当页面路由与 pathname 不一致时,手动指定 route
91
+ * onMounted(() => { cleanup = registerPageTool({ route: '/comprehensive', handlers: { ... } }) })
92
+ * onUnmounted(() => cleanup())
93
+ *
94
+ * // React(Hooks)
95
+ * useEffect(() => registerPageTool({ handlers: { ... } }), [])
96
+ * // useEffect 直接返回 cleanup 函数,React 会在组件卸载时自动调用
97
+ *
98
+ * // Angular(实现 OnInit / OnDestroy 接口)
99
+ * export class PriceProtectionComponent implements OnInit, OnDestroy {
100
+ * private cleanupPageTool!: () => void
101
+ *
102
+ * ngOnInit(): void {
103
+ * this.cleanupPageTool = registerPageTool({
104
+ * handlers: {
105
+ * 'price-protection-query': async ({ status }) => { ... },
106
+ * }
107
+ * })
108
+ * }
109
+ *
110
+ * ngOnDestroy(): void {
111
+ * this.cleanupPageTool()
112
+ * }
113
+ * }
114
+ */
115
+ export declare function registerPageTool(options: {
116
+ /**
117
+ * 目标路由路径,与 RouteConfig.route 保持一致。
118
+ * 省略时自动读取 window.location.pathname。
119
+ * 当页面路由与 pathname 不一致时(如 hash 路由、子路径前缀等),需手动传入。
120
+ */
121
+ route?: string;
122
+ /**
123
+ * 工具名 → 处理函数的映射表。
124
+ *
125
+ * 此处 handler 的 input 参数类型保留 any:
126
+ * 若改为 unknown,TypeScript 函数参数逆变规则会导致用户的具名解构写法
127
+ *(如 `async ({ productId }: { productId: string }) => ...`)无法通过类型检查,
128
+ * 破坏现有调用方代码的开发体验。运行时输入由 MCP inputSchema 保证类型安全。
129
+ */
130
+ handlers: Record<string, (input: any) => Promise<any>>;
131
+ }): () => void;
132
+ export {};
@@ -25,8 +25,8 @@ export interface FloatingBlockOptions {
25
25
  onShowAIChat?: () => void;
26
26
  /** 遥控端页面地址,默认为: https://ai.opentiny.design/next-remoter */
27
27
  qrCodeUrl?: string;
28
- /** 被遥控页面的 sessionId, 必填 */
29
- sessionId: string;
28
+ /** 被遥控页面的 sessionId;无 sessionId 时仅显示「打开对话框」菜单,不显示二维码、识别码、遥控器链接 */
29
+ sessionId?: string;
30
30
  /** 菜单项配置 */
31
31
  menuItems?: MenuItemConfig[];
32
32
  /** 遥控端页面地址,默认为: https://chat.opentiny.design */
@@ -48,9 +48,9 @@ declare class FloatingBlock {
48
48
  private getImageUrl;
49
49
  private renderItem;
50
50
  /**
51
- * 合并菜单项配置
52
- * @param userMenuItems 用户自定义菜单项配置
53
- * @returns 合并后的菜单项配置
51
+ * 合并菜单项配置。
52
+ * - sessionId:使用默认菜单 + 用户配置(可定制每一项的 show/text/icon 等)
53
+ * - 无 sessionId:不渲染任何下拉菜单,仅保留点击浮标打开对话框的能力
54
54
  */
55
55
  private mergeMenuItems;
56
56
  private init;
@@ -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
  /**
@@ -34,14 +36,17 @@ export declare function getSkillOverviews(modules: Record<string, string>): Skil
34
36
  export declare function formatSkillsForSystemPrompt(skills: SkillMeta[]): string;
35
37
  /**
36
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
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 包版本强绑定 */