@chatroomcp/chatroom 0.1.6 → 0.2.0

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 (52) hide show
  1. package/dist/app/application.d.ts +1 -0
  2. package/dist/app/application.js +4 -0
  3. package/dist/app/event-bus.d.ts +4 -0
  4. package/dist/auth/ingress-policy.d.ts +2 -0
  5. package/dist/auth/ingress-policy.js +7 -1
  6. package/dist/infrastructure/database/app-database.js +7 -0
  7. package/dist/infrastructure/http/http-server.js +5 -1
  8. package/dist/mcp/server/plugin-mcp-registrar.d.ts +6 -1
  9. package/dist/mcp/server/plugin-mcp-registrar.js +4 -4
  10. package/dist/mcp/server/request-context.d.ts +3 -0
  11. package/dist/mcp/server/request-context.js +8 -0
  12. package/dist/mcp/server/tool-support.d.ts +1 -1
  13. package/dist/mcp/server/tool-support.js +3 -1
  14. package/dist/native/macos/ChatRoomComputerHelper.app/Contents/Info.plist +14 -0
  15. package/dist/native/macos/ChatRoomComputerHelper.app/Contents/MacOS/chatroom-computer-helper +0 -0
  16. package/dist/native/macos/ChatRoomComputerHelper.app/Contents/_CodeSignature/CodeResources +115 -0
  17. package/dist/native/windows/chatroom-computer-helper.exe +0 -0
  18. package/dist/plugins/cloud/tunnel-client.d.ts +15 -1
  19. package/dist/plugins/cloud/tunnel-client.js +69 -3
  20. package/dist/plugins/computer/audit.d.ts +34 -0
  21. package/dist/plugins/computer/audit.js +42 -0
  22. package/dist/plugins/computer/computer-native-backend.d.ts +11 -0
  23. package/dist/plugins/computer/computer-native-backend.js +58 -0
  24. package/dist/plugins/computer/computer-native-host.d.ts +26 -0
  25. package/dist/plugins/computer/computer-native-host.js +245 -0
  26. package/dist/plugins/computer/computer-protocol.d.ts +21 -0
  27. package/dist/plugins/computer/computer-protocol.js +72 -0
  28. package/dist/plugins/computer/computer-schemas.d.ts +317 -0
  29. package/dist/plugins/computer/computer-schemas.js +152 -0
  30. package/dist/plugins/computer/computer-service.d.ts +27 -0
  31. package/dist/plugins/computer/computer-service.js +108 -0
  32. package/dist/plugins/computer/computer-settings-repository.d.ts +8 -0
  33. package/dist/plugins/computer/computer-settings-repository.js +41 -0
  34. package/dist/plugins/computer/mcp.d.ts +3 -0
  35. package/dist/plugins/computer/mcp.js +126 -0
  36. package/dist/plugins/computer/plugin.d.ts +4 -0
  37. package/dist/plugins/computer/plugin.js +29 -0
  38. package/dist/plugins/computer/types.d.ts +174 -0
  39. package/dist/plugins/computer/types.js +1 -0
  40. package/dist/plugins/web/api-types.d.ts +15 -1
  41. package/dist/plugins/web/http/api-router.js +2 -0
  42. package/dist/plugins/web/http/computer-api-router.d.ts +5 -0
  43. package/dist/plugins/web/http/computer-api-router.js +68 -0
  44. package/dist/plugins/web/plugin.js +3 -1
  45. package/dist/plugins/web/runtime.d.ts +3 -1
  46. package/dist/plugins/web/runtime.js +3 -1
  47. package/dist/web/assets/index-BXlxEk8f.css +1 -0
  48. package/dist/web/assets/index-DUM6zVHS.js +26 -0
  49. package/dist/web/index.html +2 -2
  50. package/package.json +6 -3
  51. package/dist/web/assets/index-B7jL3mhD.css +0 -1
  52. package/dist/web/assets/index-L4-YdZVN.js +0 -26
@@ -13,6 +13,7 @@ export interface ApplicationComponents {
13
13
  application: import("../plugins/web/runtime.js").WebRuntime;
14
14
  processes: import("../plugins/process/process-supervisor.js").ProcessSupervisor;
15
15
  cloud: import("../plugins/cloud/controller.js").CloudController;
16
+ computer: import("../plugins/computer/computer-service.js").ComputerService;
16
17
  http: HttpServer;
17
18
  }
18
19
  export declare function createApplication(config: ChatRoomConfig): Promise<ApplicationComponents>;
@@ -13,6 +13,7 @@ import { PluginManager } from "../plugins/plugin-manager.js";
13
13
  import { createWorkspacePlugin } from "../plugins/workspace/plugin.js";
14
14
  import { createProcessPlugin, ProcessService, } from "../plugins/process/plugin.js";
15
15
  import { createCloudPlugin, CloudService } from "../plugins/cloud/plugin.js";
16
+ import { createComputerPlugin, ComputerServiceToken, } from "../plugins/computer/plugin.js";
16
17
  import { createWebPlugin, WebServiceToken } from "../plugins/web/plugin.js";
17
18
  import { createChatRoomMcpHandler } from "../mcp/server/create-mcp-server.js";
18
19
  import { HttpServer } from "../infrastructure/http/http-server.js";
@@ -36,6 +37,7 @@ export async function createApplication(config) {
36
37
  }, [
37
38
  createWorkspacePlugin(),
38
39
  createProcessPlugin(),
40
+ createComputerPlugin(),
39
41
  createCloudPlugin(),
40
42
  createWebPlugin(),
41
43
  ]);
@@ -43,6 +45,7 @@ export async function createApplication(config) {
43
45
  const web = services.require(WebServiceToken);
44
46
  const processes = services.require(ProcessService);
45
47
  const cloud = services.require(CloudService);
48
+ const computer = services.require(ComputerServiceToken);
46
49
  const mcp = createChatRoomMcpHandler(plugins);
47
50
  const http = new HttpServer(config, web.application, eventBus, auth, passkeys, mcp, externalAccess, cloud);
48
51
  return {
@@ -53,6 +56,7 @@ export async function createApplication(config) {
53
56
  application: web.application,
54
57
  processes,
55
58
  cloud,
59
+ computer,
56
60
  http,
57
61
  };
58
62
  }
@@ -1,5 +1,6 @@
1
1
  import type { Operation } from "../core/operations/types.js";
2
2
  import type { ProcessSnapshot } from "../plugins/process/types.js";
3
+ import type { ComputerSettings } from "../plugins/computer/types.js";
3
4
  export type RuntimeEvent = {
4
5
  type: "operation";
5
6
  operation: Operation;
@@ -10,6 +11,9 @@ export type RuntimeEvent = {
10
11
  } | {
11
12
  type: "process";
12
13
  process: ProcessSnapshot;
14
+ } | {
15
+ type: "computer-settings";
16
+ settings: ComputerSettings;
13
17
  } | {
14
18
  type: "process-output";
15
19
  processId: string;
@@ -12,7 +12,9 @@ export declare class IngressPolicy {
12
12
  constructor(config: ChatRoomConfig, externalAccess: ExternalAccessRegistry);
13
13
  allowedHosts(): ReadonlySet<string>;
14
14
  requiresWebAuth(req: Request): boolean;
15
+ isExternalWeb(req: Request): boolean;
15
16
  requiresMcpAuth(req: Request): boolean;
17
+ isExternalMcp(req: Request): boolean;
16
18
  secureWebCookie(req: Request): boolean;
17
19
  expectedWebOrigin(req: Request): string | null;
18
20
  webAuthnOrigin(req: Request): WebAuthnOrigin | null;
@@ -16,11 +16,17 @@ export class IngressPolicy {
16
16
  return allowed;
17
17
  }
18
18
  requiresWebAuth(req) {
19
- if (this.externalAccess.matches("web", req.hostname))
19
+ if (this.isExternalWeb(req))
20
20
  return true;
21
21
  return this.config.auth.localWebAuth;
22
22
  }
23
+ isExternalWeb(req) {
24
+ return this.externalAccess.matches("web", req.hostname);
25
+ }
23
26
  requiresMcpAuth(req) {
27
+ return this.isExternalMcp(req);
28
+ }
29
+ isExternalMcp(req) {
24
30
  return this.externalAccess.matches("mcp", req.hostname);
25
31
  }
26
32
  secureWebCookie(req) {
@@ -89,6 +89,13 @@ const SCHEMA = `
89
89
  );
90
90
  CREATE INDEX IF NOT EXISTS passkeys_last_used_idx ON passkeys(last_used_at DESC);
91
91
 
92
+ CREATE TABLE IF NOT EXISTS computer_settings (
93
+ id INTEGER PRIMARY KEY CHECK(id = 1),
94
+ enabled INTEGER NOT NULL DEFAULT 0,
95
+ remote_access INTEGER NOT NULL DEFAULT 1,
96
+ updated_at TEXT NOT NULL
97
+ );
98
+
92
99
  `;
93
100
  export class AppDatabase {
94
101
  raw;
@@ -10,6 +10,7 @@ import { createOAuthRouter } from "../../presentation/http/oauth-router.js";
10
10
  import { errorMiddleware } from "../../presentation/http/http-utils.js";
11
11
  import { IngressPolicy } from "../../auth/ingress-policy.js";
12
12
  import { CHATROOM_VERSION } from "../../core/runtime/identity.js";
13
+ import { runWithMcpAccessScope } from "../../mcp/server/request-context.js";
13
14
  const WEB_UI_RESERVED_PREFIXES = [
14
15
  "/api",
15
16
  "/mcp",
@@ -60,7 +61,10 @@ export class HttpServer {
60
61
  this.mcpRequestCount += 1;
61
62
  next();
62
63
  }, mcpAuthentication(this.auth, this.ingress), (req, res) => {
63
- void nodeMcp(req, res, req.body);
64
+ const scope = this.ingress.isExternalMcp(req) ? "remote" : "local";
65
+ runWithMcpAccessScope(scope, () => {
66
+ void nodeMcp(req, res, req.body);
67
+ });
64
68
  });
65
69
  const webRoot = fileURLToPath(new URL("../../web/", import.meta.url));
66
70
  const webIndexPath = path.join(webRoot, "index.html");
@@ -1,4 +1,4 @@
1
- import type { McpServer, StandardSchemaWithJSON, ToolAnnotations } from "@modelcontextprotocol/server";
1
+ import type { McpServer, StandardSchemaWithJSON, ToolAnnotations, CallToolResult } from "@modelcontextprotocol/server";
2
2
  import type { OperationLog } from "../../operations/operation-log.js";
3
3
  type PluginToolInput<Schema extends StandardSchemaWithJSON> = StandardSchemaWithJSON.InferOutput<Schema>;
4
4
  type PluginToolAction<Input> = string | ((input: Input) => string);
@@ -14,6 +14,11 @@ export interface PluginToolConfig<InputSchema extends StandardSchemaWithJSON, Ou
14
14
  outputSchema: OutputSchema;
15
15
  annotations: ToolAnnotations;
16
16
  action: PluginToolAction<PluginToolInput<InputSchema>>;
17
+ audit?: {
18
+ input?: (input: PluginToolInput<InputSchema>) => unknown;
19
+ output?: (output: unknown) => unknown;
20
+ };
21
+ present?: (output: unknown) => CallToolResult;
17
22
  }
18
23
  /** Framework-owned MCP registration boundary that guarantees every plugin tool is audited. */
19
24
  export declare class PluginMcpRegistrar {
@@ -11,13 +11,13 @@ export class PluginMcpRegistrar {
11
11
  this.pluginId = pluginId;
12
12
  }
13
13
  registerTool(name, config, handler) {
14
- const { action, ...toolConfig } = config;
14
+ const { action, audit, present, ...toolConfig } = config;
15
15
  const callback = mcpTool(async (input) => {
16
16
  const operation = this.operations.start({
17
17
  pluginId: this.pluginId,
18
18
  source: "mcp",
19
19
  action: typeof action === "function" ? action(input) : action,
20
- input,
20
+ input: audit?.input ? audit.input(input) : input,
21
21
  ...operationReferences(input),
22
22
  });
23
23
  let deferred = false;
@@ -30,7 +30,7 @@ export class PluginMcpRegistrar {
30
30
  try {
31
31
  const result = await handler(input, execution);
32
32
  if (!deferred)
33
- this.operations.finish(operation.operationId, "success", result);
33
+ this.operations.finish(operation.operationId, "success", audit?.output ? audit.output(result) : result);
34
34
  return result;
35
35
  }
36
36
  catch (error) {
@@ -44,7 +44,7 @@ export class PluginMcpRegistrar {
44
44
  }
45
45
  throw error;
46
46
  }
47
- });
47
+ }, present);
48
48
  this.server.registerTool(name, toolConfig, callback);
49
49
  }
50
50
  }
@@ -0,0 +1,3 @@
1
+ import type { ComputerAccessScope } from "../../plugins/computer/types.js";
2
+ export declare function runWithMcpAccessScope<T>(scope: ComputerAccessScope, action: () => T): T;
3
+ export declare function currentMcpAccessScope(): ComputerAccessScope;
@@ -0,0 +1,8 @@
1
+ import { AsyncLocalStorage } from "node:async_hooks";
2
+ const storage = new AsyncLocalStorage();
3
+ export function runWithMcpAccessScope(scope, action) {
4
+ return storage.run(scope, action);
5
+ }
6
+ export function currentMcpAccessScope() {
7
+ return storage.getStore() ?? "local";
8
+ }
@@ -102,4 +102,4 @@ export declare const processSnapshotSchema: z.ZodObject<{
102
102
  timedOut: z.ZodBoolean;
103
103
  operationId: z.ZodString;
104
104
  }, z.core.$strip>;
105
- export declare function mcpTool<T>(operation: (input: T) => Promise<unknown> | unknown): (input: T) => Promise<CallToolResult>;
105
+ export declare function mcpTool<T>(operation: (input: T) => Promise<unknown> | unknown, present?: (value: unknown) => CallToolResult): (input: T) => Promise<CallToolResult>;
@@ -90,10 +90,12 @@ export const processSnapshotSchema = z.object({
90
90
  timedOut: z.boolean(),
91
91
  operationId: z.string(),
92
92
  });
93
- export function mcpTool(operation) {
93
+ export function mcpTool(operation, present) {
94
94
  return async (input) => {
95
95
  try {
96
96
  const value = await operation(input);
97
+ if (present)
98
+ return present(value);
97
99
  return {
98
100
  content: [{ type: "text", text: JSON.stringify(value, null, 2) }],
99
101
  structuredContent: asRecord(value),
@@ -0,0 +1,14 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0"><dict>
4
+ <key>CFBundleIdentifier</key><string>com.chatroomcp.computer</string>
5
+ <key>CFBundleName</key><string>ChatRoom Computer Helper</string>
6
+ <key>CFBundleDisplayName</key><string>ChatRoom Computer Helper</string>
7
+ <key>CFBundleExecutable</key><string>chatroom-computer-helper</string>
8
+ <key>CFBundlePackageType</key><string>APPL</string>
9
+ <key>CFBundleVersion</key><string>1</string>
10
+ <key>CFBundleShortVersionString</key><string>1.0</string>
11
+ <key>LSMinimumSystemVersion</key><string>15.2</string>
12
+ <key>LSUIElement</key><true/>
13
+ <key>NSHumanReadableCopyright</key><string>ChatRoom</string>
14
+ </dict></plist>
@@ -0,0 +1,115 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
+ <plist version="1.0">
4
+ <dict>
5
+ <key>files</key>
6
+ <dict/>
7
+ <key>files2</key>
8
+ <dict/>
9
+ <key>rules</key>
10
+ <dict>
11
+ <key>^Resources/</key>
12
+ <true/>
13
+ <key>^Resources/.*\.lproj/</key>
14
+ <dict>
15
+ <key>optional</key>
16
+ <true/>
17
+ <key>weight</key>
18
+ <real>1000</real>
19
+ </dict>
20
+ <key>^Resources/.*\.lproj/locversion.plist$</key>
21
+ <dict>
22
+ <key>omit</key>
23
+ <true/>
24
+ <key>weight</key>
25
+ <real>1100</real>
26
+ </dict>
27
+ <key>^Resources/Base\.lproj/</key>
28
+ <dict>
29
+ <key>weight</key>
30
+ <real>1010</real>
31
+ </dict>
32
+ <key>^version.plist$</key>
33
+ <true/>
34
+ </dict>
35
+ <key>rules2</key>
36
+ <dict>
37
+ <key>.*\.dSYM($|/)</key>
38
+ <dict>
39
+ <key>weight</key>
40
+ <real>11</real>
41
+ </dict>
42
+ <key>^(.*/)?\.DS_Store$</key>
43
+ <dict>
44
+ <key>omit</key>
45
+ <true/>
46
+ <key>weight</key>
47
+ <real>2000</real>
48
+ </dict>
49
+ <key>^(Frameworks|SharedFrameworks|PlugIns|Plug-ins|XPCServices|Helpers|MacOS|Library/(Automator|Spotlight|LoginItems))/</key>
50
+ <dict>
51
+ <key>nested</key>
52
+ <true/>
53
+ <key>weight</key>
54
+ <real>10</real>
55
+ </dict>
56
+ <key>^.*</key>
57
+ <true/>
58
+ <key>^Info\.plist$</key>
59
+ <dict>
60
+ <key>omit</key>
61
+ <true/>
62
+ <key>weight</key>
63
+ <real>20</real>
64
+ </dict>
65
+ <key>^PkgInfo$</key>
66
+ <dict>
67
+ <key>omit</key>
68
+ <true/>
69
+ <key>weight</key>
70
+ <real>20</real>
71
+ </dict>
72
+ <key>^Resources/</key>
73
+ <dict>
74
+ <key>weight</key>
75
+ <real>20</real>
76
+ </dict>
77
+ <key>^Resources/.*\.lproj/</key>
78
+ <dict>
79
+ <key>optional</key>
80
+ <true/>
81
+ <key>weight</key>
82
+ <real>1000</real>
83
+ </dict>
84
+ <key>^Resources/.*\.lproj/locversion.plist$</key>
85
+ <dict>
86
+ <key>omit</key>
87
+ <true/>
88
+ <key>weight</key>
89
+ <real>1100</real>
90
+ </dict>
91
+ <key>^Resources/Base\.lproj/</key>
92
+ <dict>
93
+ <key>weight</key>
94
+ <real>1010</real>
95
+ </dict>
96
+ <key>^[^/]+$</key>
97
+ <dict>
98
+ <key>nested</key>
99
+ <true/>
100
+ <key>weight</key>
101
+ <real>10</real>
102
+ </dict>
103
+ <key>^embedded\.provisionprofile$</key>
104
+ <dict>
105
+ <key>weight</key>
106
+ <real>20</real>
107
+ </dict>
108
+ <key>^version\.plist$</key>
109
+ <dict>
110
+ <key>weight</key>
111
+ <real>20</real>
112
+ </dict>
113
+ </dict>
114
+ </dict>
115
+ </plist>
@@ -1,4 +1,8 @@
1
1
  import { type CloudLeaseState } from "./types.js";
2
+ interface TunnelTimings {
3
+ readyTimeoutMs: number;
4
+ heartbeatIntervalMs: number;
5
+ }
2
6
  export declare class CloudTunnelClient {
3
7
  private lease;
4
8
  private readonly identity;
@@ -6,11 +10,15 @@ export declare class CloudTunnelClient {
6
10
  private readonly callbacks;
7
11
  private socket;
8
12
  private reconnectTimer;
13
+ private readyTimer;
14
+ private heartbeatTimer;
15
+ private awaitingPong;
9
16
  private stopped;
10
17
  private attempts;
11
18
  private closeWhenIdle;
12
19
  private acceptingServices;
13
20
  private readonly streams;
21
+ private readonly timings;
14
22
  constructor(lease: CloudLeaseState, identity: {
15
23
  devicePrivateKey: string;
16
24
  }, local: {
@@ -20,7 +28,7 @@ export declare class CloudTunnelClient {
20
28
  onConnected(): void;
21
29
  onDisconnected(): void;
22
30
  onError(error: Error): void;
23
- });
31
+ }, timings?: Partial<TunnelTimings>);
24
32
  start(): void;
25
33
  stop(): void;
26
34
  drainAndStop(): void;
@@ -32,5 +40,11 @@ export declare class CloudTunnelClient {
32
40
  private handleBinary;
33
41
  private send;
34
42
  private sendBinary;
43
+ private startReadyTimeout;
44
+ private clearReadyTimeout;
45
+ private startHeartbeat;
46
+ private clearSocketTimers;
47
+ private failSocket;
35
48
  private scheduleReconnect;
36
49
  }
50
+ export {};
@@ -5,6 +5,8 @@ import { cloudServiceForPublicService, } from "./types.js";
5
5
  const PROTOCOL = "chatroom-tunnel-v1";
6
6
  const HIGH_WATER = 8 * 1024 * 1024;
7
7
  const LOW_WATER = 2 * 1024 * 1024;
8
+ const READY_TIMEOUT_MS = 10_000;
9
+ const HEARTBEAT_INTERVAL_MS = 20_000;
8
10
  export class CloudTunnelClient {
9
11
  lease;
10
12
  identity;
@@ -12,17 +14,25 @@ export class CloudTunnelClient {
12
14
  callbacks;
13
15
  socket = null;
14
16
  reconnectTimer = null;
17
+ readyTimer = null;
18
+ heartbeatTimer = null;
19
+ awaitingPong = false;
15
20
  stopped = true;
16
21
  attempts = 0;
17
22
  closeWhenIdle = null;
18
23
  acceptingServices;
19
24
  streams = new Map();
20
- constructor(lease, identity, local, callbacks) {
25
+ timings;
26
+ constructor(lease, identity, local, callbacks, timings = {}) {
21
27
  this.lease = lease;
22
28
  this.identity = identity;
23
29
  this.local = local;
24
30
  this.callbacks = callbacks;
25
31
  this.acceptingServices = new Set(lease.services);
32
+ this.timings = {
33
+ readyTimeoutMs: timings.readyTimeoutMs ?? READY_TIMEOUT_MS,
34
+ heartbeatIntervalMs: timings.heartbeatIntervalMs ?? HEARTBEAT_INTERVAL_MS,
35
+ };
26
36
  }
27
37
  start() {
28
38
  if (!this.stopped)
@@ -39,6 +49,7 @@ export class CloudTunnelClient {
39
49
  if (this.reconnectTimer)
40
50
  clearTimeout(this.reconnectTimer);
41
51
  this.reconnectTimer = null;
52
+ this.clearSocketTimers();
42
53
  this.socket?.close();
43
54
  this.socket = null;
44
55
  for (const stream of this.streams.values())
@@ -51,6 +62,7 @@ export class CloudTunnelClient {
51
62
  if (this.reconnectTimer)
52
63
  clearTimeout(this.reconnectTimer);
53
64
  this.reconnectTimer = null;
65
+ this.clearSocketTimers();
54
66
  if (this.streams.size === 0) {
55
67
  this.closeWhenIdle = null;
56
68
  this.socket?.close();
@@ -86,6 +98,7 @@ export class CloudTunnelClient {
86
98
  const socket = new WebSocket(this.lease.tunnelUrl, PROTOCOL);
87
99
  this.socket = socket;
88
100
  socket.binaryType = "nodebuffer";
101
+ this.startReadyTimeout(socket);
89
102
  socket.on("message", (data, isBinary) => {
90
103
  try {
91
104
  if (isBinary)
@@ -98,9 +111,15 @@ export class CloudTunnelClient {
98
111
  socket.close();
99
112
  }
100
113
  });
101
- socket.on("close", () => {
114
+ socket.on("pong", () => {
102
115
  if (this.socket === socket)
116
+ this.awaitingPong = false;
117
+ });
118
+ socket.on("close", () => {
119
+ if (this.socket === socket) {
120
+ this.clearSocketTimers();
103
121
  this.socket = null;
122
+ }
104
123
  this.closeWhenIdle = null;
105
124
  for (const stream of this.streams.values())
106
125
  stream.request.destroy();
@@ -109,7 +128,7 @@ export class CloudTunnelClient {
109
128
  if (!this.stopped)
110
129
  this.scheduleReconnect();
111
130
  });
112
- socket.on("error", (error) => this.callbacks.onError(error));
131
+ socket.on("error", (error) => this.failSocket(socket, error));
113
132
  }
114
133
  handleControl(message) {
115
134
  if (message.type === "challenge") {
@@ -122,6 +141,9 @@ export class CloudTunnelClient {
122
141
  }
123
142
  if (message.type === "ready") {
124
143
  this.attempts = 0;
144
+ this.clearReadyTimeout();
145
+ if (this.socket)
146
+ this.startHeartbeat(this.socket);
125
147
  this.callbacks.onConnected();
126
148
  return;
127
149
  }
@@ -248,6 +270,50 @@ export class CloudTunnelClient {
248
270
  socket.send(frame, { binary: true });
249
271
  return socket.bufferedAmount < HIGH_WATER;
250
272
  }
273
+ startReadyTimeout(socket) {
274
+ this.clearReadyTimeout();
275
+ this.readyTimer = setTimeout(() => {
276
+ this.readyTimer = null;
277
+ this.failSocket(socket, new Error("Tunnel connection timed out"));
278
+ }, this.timings.readyTimeoutMs);
279
+ this.readyTimer.unref();
280
+ }
281
+ clearReadyTimeout() {
282
+ if (this.readyTimer)
283
+ clearTimeout(this.readyTimer);
284
+ this.readyTimer = null;
285
+ }
286
+ startHeartbeat(socket) {
287
+ if (this.heartbeatTimer)
288
+ clearInterval(this.heartbeatTimer);
289
+ this.awaitingPong = false;
290
+ this.heartbeatTimer = setInterval(() => {
291
+ if (this.socket !== socket || socket.readyState !== WebSocket.OPEN)
292
+ return;
293
+ if (this.awaitingPong) {
294
+ this.failSocket(socket, new Error("Tunnel heartbeat timed out"));
295
+ return;
296
+ }
297
+ this.awaitingPong = true;
298
+ socket.ping();
299
+ }, this.timings.heartbeatIntervalMs);
300
+ this.heartbeatTimer.unref();
301
+ }
302
+ clearSocketTimers() {
303
+ this.clearReadyTimeout();
304
+ if (this.heartbeatTimer)
305
+ clearInterval(this.heartbeatTimer);
306
+ this.heartbeatTimer = null;
307
+ this.awaitingPong = false;
308
+ }
309
+ failSocket(socket, error) {
310
+ if (this.socket !== socket)
311
+ return;
312
+ this.callbacks.onError(error);
313
+ this.clearSocketTimers();
314
+ this.socket = null;
315
+ socket.terminate();
316
+ }
251
317
  scheduleReconnect() {
252
318
  if (this.reconnectTimer || this.stopped)
253
319
  return;
@@ -0,0 +1,34 @@
1
+ import type { ComputerAction, ComputerActionResult, ComputerSnapshot } from "./types.js";
2
+ export declare function auditComputerActions(input: {
3
+ snapshotId?: string;
4
+ actions: ComputerAction[];
5
+ observeAfter?: boolean;
6
+ }): {
7
+ snapshotId: string | null;
8
+ observeAfter: boolean;
9
+ actions: unknown[];
10
+ };
11
+ export declare function auditSnapshotOutput(value: ComputerSnapshot): {
12
+ snapshotId: string;
13
+ revision: number;
14
+ display: import("./types.js").ComputerDisplay | null;
15
+ activeApp: string | null;
16
+ activeWindow: string | null;
17
+ elementCount: number;
18
+ screenshot: boolean;
19
+ };
20
+ export declare function auditActionOutput(value: ComputerActionResult): {
21
+ success: true;
22
+ revision: number;
23
+ executionMode: "semantic" | "background" | "foreground" | "mixed";
24
+ focusChanged: boolean;
25
+ snapshot: {
26
+ snapshotId: string;
27
+ revision: number;
28
+ display: import("./types.js").ComputerDisplay | null;
29
+ activeApp: string | null;
30
+ activeWindow: string | null;
31
+ elementCount: number;
32
+ screenshot: boolean;
33
+ } | null;
34
+ };
@@ -0,0 +1,42 @@
1
+ export function auditComputerActions(input) {
2
+ return {
3
+ snapshotId: input.snapshotId ?? null,
4
+ observeAfter: input.observeAfter ?? true,
5
+ actions: input.actions.map((action) => sanitizeAction(action)),
6
+ };
7
+ }
8
+ function sanitizeAction(action) {
9
+ if (action.type === "type_text")
10
+ return {
11
+ type: action.type,
12
+ characters: action.text.length,
13
+ elementId: action.elementId ?? null,
14
+ };
15
+ if (action.type === "set_value")
16
+ return {
17
+ type: action.type,
18
+ characters: action.value.length,
19
+ elementId: action.elementId,
20
+ };
21
+ return action;
22
+ }
23
+ export function auditSnapshotOutput(value) {
24
+ return {
25
+ snapshotId: value.snapshotId,
26
+ revision: value.revision,
27
+ display: value.display,
28
+ activeApp: value.activeApp,
29
+ activeWindow: value.activeWindow,
30
+ elementCount: value.elements.length,
31
+ screenshot: Boolean(value.screenshot),
32
+ };
33
+ }
34
+ export function auditActionOutput(value) {
35
+ return {
36
+ success: value.success,
37
+ revision: value.revision,
38
+ executionMode: value.executionMode,
39
+ focusChanged: value.focusChanged,
40
+ snapshot: value.snapshot ? auditSnapshotOutput(value.snapshot) : null,
41
+ };
42
+ }
@@ -0,0 +1,11 @@
1
+ import type { ComputerActionRequest, ComputerActionResult, ComputerBackend, ComputerPermission, ComputerSnapshot, ComputerSnapshotRequest, ComputerStatus } from "./types.js";
2
+ export declare class NativeComputerBackend implements ComputerBackend {
3
+ private readonly host;
4
+ private readonly restartWhenGranted;
5
+ status(): Promise<Omit<ComputerStatus, "settings">>;
6
+ requestPermission(permission: ComputerPermission): Promise<Omit<ComputerStatus, "settings">>;
7
+ snapshot(request: ComputerSnapshotRequest, revision: number): Promise<ComputerSnapshot>;
8
+ action(request: ComputerActionRequest, revision: number): Promise<ComputerActionResult>;
9
+ dispose(): Promise<void>;
10
+ private readStatus;
11
+ }