broker.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { randomUUID } from "node:crypto";
  2. import { tmpdir } from "node:os";
  3. import { join } from "node:path";
  4. // 公共 Host 通过 broker 入口复用参数契约;占位包仍拒绝真实能力调用。
  5. export { HELPER_GHOST_CURSOR_OVERLAY_ARG } from "./broker-helper-constants.js";
  6. export const BROKER_SOCKET_ENV = "ZCODE_CUA_PERMISSION_BROKER_SOCKET";
  7. export const BROKER_UNAVAILABLE_ENV = "ZCODE_CUA_PERMISSION_BROKER_UNAVAILABLE";
  8. export class BrokerError extends Error {
  9. constructor(message, options = {}) {
  10. super(message ?? "Computer Use broker is unavailable.");
  11. this.name = "BrokerError";
  12. this.code = options.code ?? "unavailable";
  13. if (options.details !== undefined) this.details = options.details;
  14. }
  15. }
  16. export class CuaHelperError extends Error {
  17. constructor(message, options = {}) {
  18. super(message ?? "Computer Use Helper is unavailable.");
  19. this.name = "CuaHelperError";
  20. this.code = options.code ?? "helper_unavailable";
  21. }
  22. }
  23. export function isCuaHelperError(value) {
  24. return value instanceof CuaHelperError;
  25. }
  26. const brokerErrorFactory = (code) => (message, details) =>
  27. new BrokerError(message ?? code, { code, details });
  28. export const notAuthorized = brokerErrorFactory("not_authorized");
  29. export const notSelectable = brokerErrorFactory("not_selectable");
  30. export const notSettable = brokerErrorFactory("not_settable");
  31. export const elementUnavailable = brokerErrorFactory("element_unavailable");
  32. export const actionUnavailable = brokerErrorFactory("action_unavailable");
  33. export const foregroundRequired = brokerErrorFactory("foreground_required");
  34. export async function callBrokerMethod(_args) {
  35. throw new BrokerError("Computer Use is not available in this build.");
  36. }
  37. export async function probeHelperHealth(_socketPath, _options) {
  38. return { bundleId: null, pid: null };
  39. }
  40. export function mintBrokerSocketPath(options = {}) {
  41. const dir = typeof options.dir === "string" ? options.dir : tmpdir();
  42. return join(dir, `zcode-cua-broker-${randomUUID()}.sock`);
  43. }
  44. export function resolveBrokerSocketPath(options = {}) {
  45. const env = options.env ?? process.env;
  46. const fromEnv = env[BROKER_SOCKET_ENV];
  47. if (typeof fromEnv === "string" && fromEnv.trim()) return fromEnv;
  48. return mintBrokerSocketPath(options);
  49. }
  50. export function parseRequestLine(_line) {
  51. return undefined;
  52. }
  53. export function okResponse(result) {
  54. return { ok: true, result };
  55. }
  56. export function errorResponse(message, options = {}) {
  57. return {
  58. ok: false,
  59. error: { message, ...(options.code ? { code: options.code } : {}) },
  60. };
  61. }
  62. export function errorResponseFromException(error) {
  63. return errorResponse(error instanceof Error ? error.message : String(error));
  64. }
  65. export function serializeResponse(response) {
  66. return `${JSON.stringify(response)}\n`;
  67. }
  68. export async function dispatchRequest(_backend, _request) {
  69. throw new CuaHelperError("Computer Use is not available in this build.");
  70. }
  71. export async function handleRequestLine(_backend, _line) {
  72. throw new CuaHelperError("Computer Use is not available in this build.");
  73. }
  74. export function isBrokerMethod(_method) {
  75. return false;
  76. }
  77. export function isReadOnlyBrokerMethod(_method) {
  78. return false;
  79. }