index.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. type MouseButton = 'left' | 'right' | 'middle'
  2. type MouseAction = 'press' | 'release' | 'click'
  3. type ScrollAxis = 'vertical' | 'horizontal'
  4. export type FrontmostAppInfo = {
  5. bundleId?: string
  6. appName?: string
  7. }
  8. export type ComputerUseInputAPI = {
  9. moveMouse(x: number, y: number, smooth?: boolean): Promise<void>
  10. mouseLocation(): Promise<{ x: number; y: number }>
  11. key(key: string, action?: 'press' | 'release' | 'click'): Promise<void>
  12. keys(keys: string[]): Promise<void>
  13. leftClick(): Promise<void>
  14. rightClick(): Promise<void>
  15. doubleClick(): Promise<void>
  16. middleClick(): Promise<void>
  17. dragMouse(x: number, y: number): Promise<void>
  18. scroll(x: number, y: number): Promise<void>
  19. type(text: string): Promise<void>
  20. mouseButton(
  21. button: MouseButton,
  22. action?: MouseAction,
  23. count?: number,
  24. ): Promise<void>
  25. mouseScroll(amount: number, axis?: ScrollAxis): Promise<void>
  26. typeText(text: string): Promise<void>
  27. getFrontmostAppInfo(): FrontmostAppInfo | null
  28. }
  29. export type ComputerUseInput =
  30. | ({ isSupported: false } & Partial<ComputerUseInputAPI>)
  31. | ({ isSupported: true } & ComputerUseInputAPI)
  32. let cursor = { x: 0, y: 0 }
  33. async function noOp(): Promise<void> {}
  34. const supported: ComputerUseInput = {
  35. isSupported: process.platform === 'darwin',
  36. async moveMouse(x: number, y: number): Promise<void> {
  37. cursor = { x, y }
  38. },
  39. async mouseLocation(): Promise<{ x: number; y: number }> {
  40. return cursor
  41. },
  42. async key(_key: string, _action: 'press' | 'release' | 'click' = 'click') {
  43. await noOp()
  44. },
  45. async keys(_keys: string[]) {
  46. await noOp()
  47. },
  48. async leftClick() {
  49. await noOp()
  50. },
  51. async rightClick() {
  52. await noOp()
  53. },
  54. async doubleClick() {
  55. await noOp()
  56. },
  57. async middleClick() {
  58. await noOp()
  59. },
  60. async dragMouse(x: number, y: number) {
  61. cursor = { x, y }
  62. },
  63. async scroll(_x: number, _y: number) {
  64. await noOp()
  65. },
  66. async type(_text: string) {
  67. await noOp()
  68. },
  69. async mouseButton(
  70. _button: MouseButton,
  71. _action: MouseAction = 'click',
  72. _count = 1,
  73. ) {
  74. await noOp()
  75. },
  76. async mouseScroll(_amount: number, _axis: ScrollAxis = 'vertical') {
  77. await noOp()
  78. },
  79. async typeText(_text: string) {
  80. await noOp()
  81. },
  82. getFrontmostAppInfo(): FrontmostAppInfo | null {
  83. return null
  84. },
  85. }
  86. export default supported