index.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /**
  2. * @ant/computer-use-mcp — Stub 实现
  3. *
  4. * 提供类型安全的 stub,所有函数返回合理的默认值。
  5. * 在 feature('CHICAGO_MCP') = false 时不会被实际调用,
  6. * 但确保 import 不报错且类型正确。
  7. */
  8. import type {
  9. ComputerUseHostAdapter,
  10. CoordinateMode,
  11. GrantFlags,
  12. Logger,
  13. } from './types'
  14. // Re-export types from types.ts
  15. export type { CoordinateMode, Logger } from './types'
  16. export type {
  17. ComputerUseConfig,
  18. ComputerUseHostAdapter,
  19. CuPermissionRequest,
  20. CuPermissionResponse,
  21. CuSubGates,
  22. } from './types'
  23. export { DEFAULT_GRANT_FLAGS } from './types'
  24. // ---------------------------------------------------------------------------
  25. // Types (defined here for callers that import from the main entry)
  26. // ---------------------------------------------------------------------------
  27. export interface DisplayGeometry {
  28. width: number
  29. height: number
  30. displayId?: number
  31. originX?: number
  32. originY?: number
  33. }
  34. export interface FrontmostApp {
  35. bundleId: string
  36. displayName: string
  37. }
  38. export interface InstalledApp {
  39. bundleId: string
  40. displayName: string
  41. path: string
  42. }
  43. export interface RunningApp {
  44. bundleId: string
  45. displayName: string
  46. }
  47. export interface ScreenshotResult {
  48. base64: string
  49. width: number
  50. height: number
  51. }
  52. export type ResolvePrepareCaptureResult = ScreenshotResult
  53. export interface ScreenshotDims {
  54. width: number
  55. height: number
  56. displayWidth: number
  57. displayHeight: number
  58. displayId: number
  59. originX: number
  60. originY: number
  61. }
  62. export interface CuCallToolResultContent {
  63. type: 'image' | 'text'
  64. data?: string
  65. mimeType?: string
  66. text?: string
  67. }
  68. export interface CuCallToolResult {
  69. content: CuCallToolResultContent[]
  70. telemetry: {
  71. error_kind?: string
  72. [key: string]: unknown
  73. }
  74. }
  75. export type ComputerUseSessionContext = Record<string, unknown>
  76. // ---------------------------------------------------------------------------
  77. // API_RESIZE_PARAMS — 默认的截图缩放参数
  78. // ---------------------------------------------------------------------------
  79. export const API_RESIZE_PARAMS = {
  80. maxWidth: 1280,
  81. maxHeight: 800,
  82. maxPixels: 1280 * 800,
  83. }
  84. // ---------------------------------------------------------------------------
  85. // ComputerExecutor — stub class
  86. // ---------------------------------------------------------------------------
  87. export class ComputerExecutor {
  88. capabilities: Record<string, boolean> = {}
  89. }
  90. // ---------------------------------------------------------------------------
  91. // Functions — 返回合理默认值的 stub
  92. // ---------------------------------------------------------------------------
  93. /**
  94. * 计算目标截图尺寸。
  95. * 在物理宽高和 API 限制之间取最优尺寸。
  96. */
  97. export function targetImageSize(
  98. physW: number,
  99. physH: number,
  100. _params?: typeof API_RESIZE_PARAMS,
  101. ): [number, number] {
  102. const maxW = _params?.maxWidth ?? 1280
  103. const maxH = _params?.maxHeight ?? 800
  104. const scale = Math.min(1, maxW / physW, maxH / physH)
  105. return [Math.round(physW * scale), Math.round(physH * scale)]
  106. }
  107. /**
  108. * 绑定会话上下文,返回工具调度函数。
  109. * Stub 返回一个始终返回空结果的调度器。
  110. */
  111. export function bindSessionContext(
  112. _adapter: ComputerUseHostAdapter,
  113. _coordinateMode: CoordinateMode,
  114. _ctx: ComputerUseSessionContext,
  115. ): (name: string, args: unknown) => Promise<CuCallToolResult> {
  116. return async (_name: string, _args: unknown) => ({
  117. content: [],
  118. telemetry: {},
  119. })
  120. }
  121. /**
  122. * 构建 Computer Use 工具定义列表。
  123. * Stub 返回空数组(无工具)。
  124. */
  125. export function buildComputerUseTools(
  126. _capabilities?: Record<string, boolean>,
  127. _coordinateMode?: CoordinateMode,
  128. _installedAppNames?: string[],
  129. ): Array<{ name: string; description: string; inputSchema: Record<string, unknown> }> {
  130. return []
  131. }
  132. /**
  133. * 创建 Computer Use MCP server。
  134. * Stub 返回 null(服务未启用)。
  135. */
  136. export function createComputerUseMcpServer(
  137. _adapter?: ComputerUseHostAdapter,
  138. _coordinateMode?: CoordinateMode,
  139. ): null {
  140. return null
  141. }