@opentiny/next-sdk 0.4.0 → 0.4.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.
Files changed (130) hide show
  1. package/agent/AgentModelProvider.ts +78 -81
  2. package/agent/type.ts +6 -9
  3. package/agent/utils/getAISDKTools.ts +0 -1
  4. package/agent/utils/getBuiltinMcpTools.ts +7 -7
  5. package/core.ts +0 -3
  6. package/dist/SimulatorMask-BHVXyogh-iNUb4dyF.js +361 -0
  7. package/dist/agent/type.d.ts +4 -12
  8. package/dist/agent/utils/getBuiltinMcpTools.d.ts +3 -3
  9. package/dist/core.d.ts +0 -1
  10. package/dist/core.js +16 -17
  11. package/dist/index-CXK8CcKq.js +6076 -0
  12. package/dist/index.d.ts +0 -3
  13. package/dist/index.js +57 -4969
  14. package/dist/{initialize-builtin-WebMCP-HgObT902.js → initialize-builtin-WebMCP-rALfqS-o.js} +1166 -1056
  15. package/dist/page-tools/a11y/build.d.ts +11 -0
  16. package/dist/page-tools/a11y/constants.d.ts +9 -0
  17. package/dist/page-tools/a11y/search.d.ts +17 -0
  18. package/dist/page-tools/a11y/types.d.ts +104 -0
  19. package/dist/page-tools/a11y/utils.d.ts +34 -0
  20. package/dist/page-tools/a11y/vnode.d.ts +37 -0
  21. package/dist/page-tools/a11y-tree.d.ts +8 -99
  22. package/dist/page-tools/constants.d.ts +22 -0
  23. package/dist/page-tools/context.d.ts +28 -0
  24. package/dist/page-tools/handlers/browserState.d.ts +8 -0
  25. package/dist/page-tools/handlers/click.d.ts +8 -0
  26. package/dist/page-tools/handlers/executeJavascript.d.ts +8 -0
  27. package/dist/page-tools/handlers/fill.d.ts +8 -0
  28. package/dist/page-tools/handlers/scroll.d.ts +8 -0
  29. package/dist/page-tools/handlers/searchTree.d.ts +8 -0
  30. package/dist/page-tools/handlers/select.d.ts +8 -0
  31. package/dist/page-tools/page-agent-highlight/index.d.ts +9 -0
  32. package/dist/page-tools/page-agent-mask/SimulatorMask.d.ts +14 -0
  33. package/dist/page-tools/page-agent-mask/checkDarkMode.d.ts +5 -0
  34. package/dist/page-tools/page-agent-tool.d.ts +3 -8
  35. package/dist/page-tools/schema.d.ts +43 -0
  36. package/dist/page-tools/utils/dom.d.ts +6 -0
  37. package/dist/page-tools/utils/scroll.d.ts +15 -0
  38. package/dist/runtime.d.ts +4 -0
  39. package/dist/runtime.js +744 -0
  40. package/dist/utils/builtinProxy.d.ts +1 -1
  41. package/index.ts +0 -5
  42. package/package.json +6 -17
  43. package/page-tools/a11y/build.ts +70 -0
  44. package/page-tools/a11y/constants.ts +110 -0
  45. package/page-tools/a11y/search.ts +129 -0
  46. package/page-tools/a11y/types.ts +108 -0
  47. package/page-tools/a11y/utils.ts +266 -0
  48. package/page-tools/a11y/vnode.ts +229 -0
  49. package/page-tools/a11y-tree.ts +8 -527
  50. package/page-tools/bridge.ts +1 -1
  51. package/page-tools/constants.ts +72 -0
  52. package/page-tools/context.ts +13 -0
  53. package/page-tools/handlers/browserState.ts +12 -0
  54. package/page-tools/handlers/click.ts +26 -0
  55. package/page-tools/handlers/executeJavascript.ts +22 -0
  56. package/page-tools/handlers/fill.ts +60 -0
  57. package/page-tools/handlers/scroll.ts +66 -0
  58. package/page-tools/handlers/searchTree.ts +30 -0
  59. package/page-tools/handlers/select.ts +34 -0
  60. package/page-tools/page-agent-highlight/index.ts +198 -0
  61. package/page-tools/page-agent-mask/SimulatorMask.module.css +14 -0
  62. package/page-tools/page-agent-mask/SimulatorMask.ts +279 -0
  63. package/page-tools/page-agent-mask/checkDarkMode.ts +181 -0
  64. package/page-tools/page-agent-mask/cursor-border.svg +3 -0
  65. package/page-tools/page-agent-mask/cursor-fill.svg +5 -0
  66. package/page-tools/page-agent-mask/cursor.module.css +70 -0
  67. package/page-tools/page-agent-prompt.md +34 -18
  68. package/page-tools/page-agent-tool.ts +104 -152
  69. package/page-tools/schema.ts +51 -0
  70. package/page-tools/utils/dom.ts +155 -0
  71. package/page-tools/utils/scroll.ts +58 -0
  72. package/runtime.ts +13 -0
  73. package/utils/builtinProxy.ts +45 -13
  74. package/vite.config.runtime.ts +22 -0
  75. package/vite.config.ts +52 -8
  76. package/McpSdk.ts +0 -14
  77. package/WebAgent.ts +0 -5
  78. package/WebMcp.ts +0 -26
  79. package/Zod.ts +0 -1
  80. package/dist/McpSdk.d.ts +0 -14
  81. package/dist/SimulatorMask-BHVXyogh-BFEGpD5S.js +0 -1048
  82. package/dist/SimulatorMask-BHVXyogh-CCYbrb84.js +0 -801
  83. package/dist/WebAgent.d.ts +0 -5
  84. package/dist/WebMcp.d.ts +0 -23
  85. package/dist/Zod.d.ts +0 -1
  86. package/dist/index.es.dev.js +0 -59017
  87. package/dist/index.es.js +0 -46795
  88. package/dist/index.umd.dev.js +0 -60355
  89. package/dist/index.umd.js +0 -1248
  90. package/dist/mcpsdk@1.25.3.dev.js +0 -22780
  91. package/dist/mcpsdk@1.25.3.es.dev.js +0 -22778
  92. package/dist/mcpsdk@1.25.3.es.js +0 -16960
  93. package/dist/mcpsdk@1.25.3.js +0 -48
  94. package/dist/transport/ExtensionClientTransport.d.ts +0 -24
  95. package/dist/transport/ExtensionContentServerTransport.d.ts +0 -39
  96. package/dist/transport/ExtensionPageServerTransport.d.ts +0 -36
  97. package/dist/transport/messages.d.ts +0 -9
  98. package/dist/vite.config.mcpSdk.d.ts +0 -2
  99. package/dist/vite.config.webAgent.d.ts +0 -2
  100. package/dist/vite.config.webMcp.d.ts +0 -2
  101. package/dist/vite.config.webMcpFull.d.ts +0 -2
  102. package/dist/vite.config.zod.d.ts +0 -2
  103. package/dist/webagent.dev.js +0 -49360
  104. package/dist/webagent.es.dev.js +0 -49071
  105. package/dist/webagent.es.js +0 -39219
  106. package/dist/webagent.js +0 -642
  107. package/dist/webmcp-full.dev.js +0 -31336
  108. package/dist/webmcp-full.es.dev.js +0 -30283
  109. package/dist/webmcp-full.es.js +0 -22889
  110. package/dist/webmcp-full.js +0 -645
  111. package/dist/webmcp.dev.js +0 -9572
  112. package/dist/webmcp.es.dev.js +0 -8518
  113. package/dist/webmcp.es.js +0 -6727
  114. package/dist/webmcp.js +0 -602
  115. package/dist/zod@3.25.76.dev.js +0 -4037
  116. package/dist/zod@3.25.76.es.dev.js +0 -4033
  117. package/dist/zod@3.25.76.es.js +0 -2945
  118. package/dist/zod@3.25.76.js +0 -1
  119. package/transport/ExtensionClientTransport.ts +0 -100
  120. package/transport/ExtensionContentServerTransport.ts +0 -162
  121. package/transport/ExtensionPageServerTransport.ts +0 -149
  122. package/transport/messages.ts +0 -63
  123. package/vite-build-tsc.ts +0 -63
  124. package/vite-env.d.ts +0 -10
  125. package/vite.config.mcpSdk.ts +0 -28
  126. package/vite.config.webAgent.ts +0 -19
  127. package/vite.config.webMcp.ts +0 -40
  128. package/vite.config.webMcpFull.ts +0 -19
  129. package/vite.config.zod.ts +0 -23
  130. /package/dist/{vite-build-tsc.d.ts → vite.config.runtime.d.ts} +0 -0
@@ -1,17 +1,45 @@
1
1
  import type { Transport } from '@modelcontextprotocol/sdk/shared/transport.js'
2
2
 
3
+ /** 浏览器内置 WebMCP 原生上下文接口 */
4
+ interface NativeModelContext {
5
+ getTools(): Promise<NativeTool[]> | NativeTool[]
6
+ executeTool(tool: NativeTool, argsStr: string): Promise<unknown>
7
+ }
8
+
9
+ /** 原生工具描述 */
10
+ interface NativeTool {
11
+ name: string
12
+ description?: string
13
+ title?: string
14
+ inputSchema?: unknown
15
+ }
16
+
17
+ /** JSON-RPC 消息基础结构 */
18
+ interface JsonRpcMessage {
19
+ jsonrpc: string
20
+ id?: string | number
21
+ method?: string
22
+ params?: {
23
+ name?: string
24
+ arguments?: Record<string, unknown>
25
+ [key: string]: unknown
26
+ }
27
+ }
28
+
3
29
  /**
4
30
  * 建立原生的 JSON-RPC 拦截层:
5
- * 将发向 remote 的 MCP 请求拦截,代理给所在浏览器的 navigator.modelContextTesting 上执行。
31
+ * 将发向 remote 的 MCP 请求拦截,代理给所在浏览器的 document.modelContext 上执行。
6
32
  */
7
33
  export const setupBuiltinProxy = (transport: Transport) => {
8
- const getNativeCtx = () => {
9
- if (typeof navigator === 'undefined') return null
10
- const nav = navigator as any
11
- return nav.modelContextTesting || null
34
+ const getNativeCtx = (): NativeModelContext | null => {
35
+ let ctx: NativeModelContext | null = null
36
+ if (typeof document !== 'undefined') ctx = ctx || ((document as unknown as Record<string, unknown>).modelContext as NativeModelContext | null)
37
+ if (typeof navigator !== 'undefined') ctx = ctx || ((navigator as unknown as Record<string, unknown>).modelContext as NativeModelContext | null)
38
+ ctx = ctx || ((globalThis as unknown as Record<string, unknown>).modelContext as NativeModelContext | null)
39
+ return ctx || null
12
40
  }
13
41
 
14
- transport.onmessage = async (message: any) => {
42
+ transport.onmessage = async (message: JsonRpcMessage) => {
15
43
  if (!message || typeof message !== 'object') return
16
44
  const id = message.id
17
45
  const method = message.method
@@ -36,8 +64,8 @@ export const setupBuiltinProxy = (transport: Transport) => {
36
64
  await transport.send({ jsonrpc: '2.0', id, result: {} })
37
65
  } else if (method === 'tools/list') {
38
66
  const nativeCtx = getNativeCtx()
39
- if (nativeCtx && nativeCtx.listTools) {
40
- const rawTools = await nativeCtx.listTools()
67
+ if (nativeCtx && nativeCtx.getTools) {
68
+ const rawTools = await nativeCtx.getTools()
41
69
  const tools = rawTools.map((t: any) => {
42
70
  let schemaObj: any = {}
43
71
  if (typeof t.inputSchema === 'string') {
@@ -67,19 +95,22 @@ export const setupBuiltinProxy = (transport: Transport) => {
67
95
  const nativeCtx = getNativeCtx()
68
96
  if (nativeCtx && nativeCtx.executeTool) {
69
97
  if (!message.params || typeof message.params !== 'object' || !message.params.name) {
70
- const error: any = new Error('Invalid params: "name" is required and params must be an object')
98
+ const error: Error & { code?: number } = new Error('Invalid params: "name" is required and params must be an object')
71
99
  error.code = -32602 // Invalid params
72
100
  throw error
73
101
  }
74
102
  const { name, arguments: args } = message.params
75
- const result = await nativeCtx.executeTool(name, JSON.stringify(args || {}))
103
+ const tools = await (nativeCtx.getTools ? nativeCtx.getTools() : [])
104
+ const toolObj = tools.find((t: any) => t.name === name)
105
+ if (!toolObj) throw new Error(`Tool ${name} not found`)
106
+ const result = await nativeCtx.executeTool(toolObj, JSON.stringify(args || {}))
76
107
  const finalResult =
77
108
  result && typeof result === 'object' && 'content' in result
78
109
  ? result
79
110
  : { content: [{ type: 'text', text: typeof result === 'string' ? result : JSON.stringify(result) }] }
80
111
  await transport.send({ jsonrpc: '2.0', id, result: finalResult })
81
112
  } else {
82
- const error: any = new Error('executeTool not implemented in Browser built-in WebMCP')
113
+ const error: Error & { code?: number } = new Error('executeTool not implemented in Browser built-in WebMCP')
83
114
  error.code = -32601 // Method not found
84
115
  throw error
85
116
  }
@@ -99,12 +130,13 @@ export const setupBuiltinProxy = (transport: Transport) => {
99
130
  error: { code: -32601, message: `Method not found: ${method}` }
100
131
  })
101
132
  }
102
- } catch (err: any) {
133
+ } catch (err: unknown) {
103
134
  if (id !== undefined) {
135
+ const errObj = err as { code?: number; message?: string }
104
136
  await transport.send({
105
137
  jsonrpc: '2.0',
106
138
  id,
107
- error: { code: err.code || -32000, message: err.message || String(err) }
139
+ error: { code: errObj.code || -32000, message: errObj.message || String(err) }
108
140
  })
109
141
  }
110
142
  }
@@ -0,0 +1,22 @@
1
+ import { defineConfig } from 'vite'
2
+
3
+ export default defineConfig(() => {
4
+ return {
5
+ define: {
6
+ 'process.env.NODE_ENV': JSON.stringify('production')
7
+ },
8
+ build: {
9
+ emptyOutDir: false,
10
+ lib: {
11
+ entry: 'runtime.ts',
12
+ name: 'NextSDK',
13
+ formats: ['iife'] as any,
14
+ fileName: () => 'runtime.js'
15
+ },
16
+ rollupOptions: {
17
+ // 全量打包第三方依赖,便于浏览器中直接注入或运行
18
+ external: []
19
+ }
20
+ }
21
+ }
22
+ })
package/vite.config.ts CHANGED
@@ -1,18 +1,62 @@
1
1
  import { defineConfig } from 'vite'
2
+ import dts from 'vite-plugin-dts'
2
3
 
3
4
  // https://vitejs.dev/config/
4
- export default defineConfig(({ mode }) => {
5
- const shouldMinify = mode !== 'dev'
5
+ export default defineConfig(() => {
6
+ // 需要排除的第三方依赖列表
7
+ const externalDependencies = [
8
+ '@modelcontextprotocol/sdk',
9
+ '@opentiny/next',
10
+ '@ai-sdk/openai',
11
+ '@ai-sdk/deepseek',
12
+ '@ai-sdk/provider',
13
+ 'qrcode',
14
+ 'zod',
15
+ 'ajv',
16
+ 'ai'
17
+ ]
6
18
 
7
19
  return {
20
+ plugins: [
21
+ // 配置 dts 插件生成类型声明文件
22
+ dts({
23
+ // 指定 TypeScript 配置文件路径
24
+ tsconfigPath: './tsconfig.json',
25
+ outDir: 'dist',
26
+ // 包含所有 TypeScript 文件,确保所有被引用的文件都被处理
27
+ include: ['**/*.ts'],
28
+ exclude: ['node_modules/**', 'dist/**', '**/*.test.ts', '**/*.spec.ts'],
29
+ // 不合并类型文件,保持文件结构以便相对路径引用正常工作
30
+ rollupTypes: false,
31
+ // 插入类型入口文件引用
32
+ insertTypesEntry: true
33
+ })
34
+ ],
8
35
  build: {
9
- emptyOutDir: shouldMinify,
10
- minify: shouldMinify,
36
+ emptyOutDir: false,
11
37
  lib: {
12
- entry: 'index.ts',
13
- name: 'WebMCP',
14
- formats: ['es', 'umd'],
15
- fileName: (format) => `index.${format}${shouldMinify ? '' : '.dev'}.js`
38
+ entry: {
39
+ index: 'index.ts',
40
+ core: 'core.ts'
41
+ },
42
+ name: 'NEXT-SDK',
43
+ formats: ['es'],
44
+ fileName: (format, entryName) => `${entryName}.js`
45
+ },
46
+ rollupOptions: {
47
+ // 排除第三方依赖,保留本地文件
48
+ external: (id) => {
49
+ // 如果是相对路径导入(本地文件),不排除
50
+ if (id.startsWith('.') || id.startsWith('/')) {
51
+ return false
52
+ }
53
+ // 排除 node_modules 中的第三方依赖
54
+ if (id.includes('node_modules')) {
55
+ return true
56
+ }
57
+ // 排除指定的第三方依赖包
58
+ return externalDependencies.some((dep) => id === dep || id.startsWith(`${dep}/`))
59
+ }
16
60
  }
17
61
  }
18
62
  }
package/McpSdk.ts DELETED
@@ -1,14 +0,0 @@
1
- import Ajv from 'ajv'
2
- export { Ajv }
3
- export * from '@modelcontextprotocol/sdk/types.js'
4
- export * from '@modelcontextprotocol/sdk/shared/uriTemplate.js'
5
- export * from '@modelcontextprotocol/sdk/server/completable.js'
6
- export * from '@modelcontextprotocol/sdk/shared/metadataUtils.js'
7
- export * from '@modelcontextprotocol/sdk/client/index.js'
8
- export * from '@modelcontextprotocol/sdk/client/auth.js'
9
- export * from '@modelcontextprotocol/sdk/client/sse.js'
10
- export * from '@modelcontextprotocol/sdk/client/streamableHttp.js'
11
- // @ts-ignore
12
- export * from '@modelcontextprotocol/sdk/server/mcp.js'
13
- export * from '@modelcontextprotocol/sdk/shared/transport.js'
14
- export * from '@modelcontextprotocol/sdk/shared/protocol.js'
package/WebAgent.ts DELETED
@@ -1,5 +0,0 @@
1
- export { AgentModelProvider } from './agent/AgentModelProvider'
2
- export { createRemoter } from './remoter/createRemoter'
3
- export { getAISDKTools } from './agent/utils/getAISDKTools'
4
- export { QrCode, type QrCodeOption } from './remoter/QrCode'
5
- export type * from './agent/type'
package/WebMcp.ts DELETED
@@ -1,26 +0,0 @@
1
- import Ajv from 'ajv'
2
- export * from './WebMcpServer'
3
- export * from './WebMcpClient'
4
- export { Ajv }
5
- export { z } from 'zod'
6
- export { AuthClientProvider } from '@opentiny/next'
7
- export { ResourceTemplate } from '@modelcontextprotocol/sdk/server/mcp.js'
8
- export { UriTemplate } from '@modelcontextprotocol/sdk/shared/uriTemplate.js'
9
- export { completable } from '@modelcontextprotocol/sdk/server/completable.js'
10
- export { getDisplayName } from '@modelcontextprotocol/sdk/shared/metadataUtils.js'
11
- export type * from 'zod'
12
- export type * from '@opentiny/next'
13
- export type * from '@modelcontextprotocol/sdk/types.js'
14
- export type * from '@modelcontextprotocol/sdk/shared/protocol.js'
15
- export type * from '@modelcontextprotocol/sdk/shared/transport.js'
16
- export type * from '@modelcontextprotocol/sdk/client/sse.js'
17
- export type * from '@modelcontextprotocol/sdk/client/streamableHttp.js'
18
- export type * from '@modelcontextprotocol/sdk/server/mcp.js'
19
-
20
- export * from './transport/ExtensionPageServerTransport'
21
-
22
- // page-tools:供浏览器插件通过 scripting.executeScript 注入到第三方页面后初始化 WebMCP
23
- export { initializeBuiltinWebMCP } from './page-tools/initialize-builtin-WebMCP'
24
- export { setupModelContextBridge } from './page-tools/bridge'
25
- export { registerPageAgentTool } from './page-tools/page-agent-tool'
26
-
package/Zod.ts DELETED
@@ -1 +0,0 @@
1
- export { z } from 'zod'
package/dist/McpSdk.d.ts DELETED
@@ -1,14 +0,0 @@
1
- import { default as Ajv } from 'ajv';
2
-
3
- export { Ajv };
4
- export * from '@modelcontextprotocol/sdk/types.js';
5
- export * from '@modelcontextprotocol/sdk/shared/uriTemplate.js';
6
- export * from '@modelcontextprotocol/sdk/server/completable.js';
7
- export * from '@modelcontextprotocol/sdk/shared/metadataUtils.js';
8
- export * from '@modelcontextprotocol/sdk/client/index.js';
9
- export * from '@modelcontextprotocol/sdk/client/auth.js';
10
- export * from '@modelcontextprotocol/sdk/client/sse.js';
11
- export * from '@modelcontextprotocol/sdk/client/streamableHttp.js';
12
- export * from '@modelcontextprotocol/sdk/server/mcp.js';
13
- export * from '@modelcontextprotocol/sdk/shared/transport.js';
14
- export * from '@modelcontextprotocol/sdk/shared/protocol.js';