-
-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathdaemon-client.ts
More file actions
205 lines (181 loc) · 5.16 KB
/
daemon-client.ts
File metadata and controls
205 lines (181 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import net from 'node:net';
import { randomUUID } from 'node:crypto';
import { writeFrame, createFrameReader } from '../daemon/framing.ts';
import {
DAEMON_PROTOCOL_VERSION,
type DaemonRequest,
type DaemonResponse,
type DaemonMethod,
type DaemonToolResult,
type ToolInvokeParams,
type ToolInvokeResult,
type DaemonStatusResult,
type ToolListItem,
type XcodeIdeListParams,
type XcodeIdeListResult,
type XcodeIdeToolListItem,
type XcodeIdeInvokeParams,
type XcodeIdeInvokeResult,
} from '../daemon/protocol.ts';
import { getSocketPath } from '../daemon/socket-path.ts';
export class DaemonVersionMismatchError extends Error {
constructor(message: string) {
super(message);
this.name = 'DaemonVersionMismatchError';
}
}
export interface DaemonClientOptions {
socketPath?: string;
timeout?: number;
}
export class DaemonClient {
private socketPath: string;
private timeout: number;
constructor(opts: DaemonClientOptions = {}) {
this.socketPath = opts.socketPath ?? getSocketPath();
this.timeout = opts.timeout ?? 30000;
}
/**
* Send a request to the daemon and wait for a response.
*/
async request<TResult>(method: DaemonMethod, params?: unknown): Promise<TResult> {
const id = randomUUID();
const req: DaemonRequest = {
v: DAEMON_PROTOCOL_VERSION,
id,
method,
params,
};
return new Promise<TResult>((resolve, reject) => {
const socket = net.createConnection(this.socketPath);
let resolved = false;
const cleanup = (): void => {
if (!resolved) {
resolved = true;
socket.destroy();
}
};
const timeoutId = setTimeout(() => {
cleanup();
reject(new Error(`Daemon request timed out after ${this.timeout}ms`));
}, this.timeout);
socket.on('error', (err) => {
clearTimeout(timeoutId);
cleanup();
if (err.message.includes('ECONNREFUSED') || err.message.includes('ENOENT')) {
reject(new Error('Daemon is not running. Start it with: xcodebuildmcp daemon start'));
} else {
reject(err);
}
});
const onData = createFrameReader(
(msg) => {
const res = msg as DaemonResponse<TResult>;
if (res.id !== id) return;
clearTimeout(timeoutId);
resolved = true;
socket.end();
if (res.error) {
if (
res.error.code === 'BAD_REQUEST' &&
res.error.message.startsWith('Unsupported protocol version')
) {
reject(new DaemonVersionMismatchError(res.error.message));
} else {
reject(new Error(`${res.error.code}: ${res.error.message}`));
}
} else {
resolve(res.result as TResult);
}
},
(err) => {
clearTimeout(timeoutId);
cleanup();
reject(err);
},
);
socket.on('data', onData);
socket.on('connect', () => {
writeFrame(socket, req);
});
});
}
/**
* Get daemon status.
*/
async status(): Promise<DaemonStatusResult> {
return this.request<DaemonStatusResult>('daemon.status');
}
/**
* Stop the daemon.
*/
async stop(): Promise<void> {
await this.request<{ ok: boolean }>('daemon.stop');
}
/**
* List available tools.
*/
async listTools(): Promise<ToolListItem[]> {
return this.request<ToolListItem[]>('tool.list');
}
/**
* Invoke a tool.
*/
async invokeTool(tool: string, args: Record<string, unknown>): Promise<DaemonToolResult> {
const result = await this.request<ToolInvokeResult>('tool.invoke', {
tool,
args,
} satisfies ToolInvokeParams);
return result.result;
}
/**
* List dynamic xcode-ide bridge tools from the daemon-managed bridge session.
*/
async listXcodeIdeTools(params?: XcodeIdeListParams): Promise<XcodeIdeToolListItem[]> {
const result = await this.request<XcodeIdeListResult>('xcode-ide.list', params);
return result.tools;
}
/**
* Invoke a dynamic xcode-ide bridge tool through the daemon-managed bridge session.
*/
async invokeXcodeIdeTool(
remoteTool: string,
args: Record<string, unknown>,
): Promise<DaemonToolResult> {
const result = await this.request<XcodeIdeInvokeResult>('xcode-ide.invoke', {
remoteTool,
args,
} satisfies XcodeIdeInvokeParams);
return result.result;
}
/**
* Check if daemon is running by attempting to connect.
*/
async isRunning(): Promise<boolean> {
return new Promise<boolean>((resolve) => {
const socket = net.createConnection(this.socketPath);
let settled = false;
const finish = (value: boolean): void => {
if (settled) return;
settled = true;
try {
socket.destroy();
} catch {
// ignore
}
resolve(value);
};
const timeoutId = setTimeout(() => {
finish(false);
}, this.timeout);
socket.on('connect', () => {
clearTimeout(timeoutId);
finish(true);
});
socket.on('error', () => {
clearTimeout(timeoutId);
finish(false);
});
});
}
}