broker.js 3.0 KB

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