agent-director 0.4.3 → 0.4.4

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.
package/dist/index.js CHANGED
@@ -13,12 +13,13 @@ function expandTilde(p) {
13
13
  }
14
14
 
15
15
  // src/internal/bootstrapFfi.ts
16
- import { dlopen, FFIType, CString, ptr } from "bun:ffi";
16
+ import { dlopen as dlopen2, FFIType as FFIType2, CString, ptr } from "bun:ffi";
17
17
 
18
18
  // src/platform.ts
19
19
  import { existsSync } from "fs";
20
20
  import { fileURLToPath } from "url";
21
21
  import { join, dirname } from "path";
22
+ import { dlopen } from "bun:ffi";
22
23
 
23
24
  // src/errors.ts
24
25
  class AgentDirectorError extends Error {
@@ -214,6 +215,9 @@ function errorFromEnvelope(verb, err_name, err_description) {
214
215
  return new AgentDirectorError(verb, err_name, err_description);
215
216
  }
216
217
 
218
+ // src/internal/bindingSpec.ts
219
+ import { FFIType } from "bun:ffi";
220
+
217
221
  // src/internal/verbs.ts
218
222
  var VERBS = [
219
223
  "spawn",
@@ -236,6 +240,9 @@ function kebabToUnderscore(v) {
236
240
  return v.replace(/-/g, "_");
237
241
  }
238
242
  var HANDLE_FREE_VERBS = new Set(["version"]);
243
+ function isHandleFree(verb) {
244
+ return HANDLE_FREE_VERBS.has(verb);
245
+ }
239
246
 
240
247
  // src/internal/bindingSpec.ts
241
248
  var BINDING_SYMBOL_NAMES = [
@@ -244,6 +251,20 @@ var BINDING_SYMBOL_NAMES = [
244
251
  "ad_free_cstring",
245
252
  ...VERBS.map((v) => "ad_" + kebabToUnderscore(v))
246
253
  ];
254
+ function buildBindingSpec() {
255
+ const spec = {
256
+ ad_open: { args: [FFIType.cstring], returns: FFIType.ptr },
257
+ ad_close: { args: [FFIType.cstring], returns: FFIType.ptr },
258
+ ad_free_cstring: { args: [FFIType.ptr], returns: FFIType.void }
259
+ };
260
+ for (const verb of VERBS) {
261
+ spec["ad_" + kebabToUnderscore(verb)] = {
262
+ args: [FFIType.cstring],
263
+ returns: FFIType.ptr
264
+ };
265
+ }
266
+ return spec;
267
+ }
247
268
 
248
269
  // src/platform.ts
249
270
  var MIN_BUN_VERSION = "1.0.21";
@@ -297,21 +318,30 @@ function resolveNativePath(opts = {}) {
297
318
  }
298
319
  return libPath;
299
320
  }
321
+ function _loadNativeInternal(opts = {}) {
322
+ const libPath = resolveNativePath(opts);
323
+ const bindingSpec = buildBindingSpec();
324
+ const loaded = dlopen(libPath, bindingSpec);
325
+ return { lib: loaded.symbols, libPath };
326
+ }
327
+ function loadNative() {
328
+ return _loadNativeInternal();
329
+ }
300
330
 
301
331
  // src/internal/bootstrapFfi.ts
302
332
  var _soPath = resolveNativePath();
303
- var _lib = dlopen(_soPath, {
333
+ var _lib = dlopen2(_soPath, {
304
334
  ad_open: {
305
- args: [FFIType.cstring],
306
- returns: FFIType.ptr
335
+ args: [FFIType2.cstring],
336
+ returns: FFIType2.ptr
307
337
  },
308
338
  ad_close: {
309
- args: [FFIType.cstring],
310
- returns: FFIType.ptr
339
+ args: [FFIType2.cstring],
340
+ returns: FFIType2.ptr
311
341
  },
312
342
  ad_free_cstring: {
313
- args: [FFIType.ptr],
314
- returns: FFIType.void
343
+ args: [FFIType2.ptr],
344
+ returns: FFIType2.void
315
345
  }
316
346
  });
317
347
  function callOpen(params) {
@@ -350,8 +380,11 @@ function _rejectAll(reason) {
350
380
  }
351
381
  _pending.clear();
352
382
  }
383
+ function _resolveWorkerURL() {
384
+ return import.meta.url.endsWith("workerProxy.ts") ? new URL("./worker.ts", import.meta.url) : new URL("./internal/worker.js", import.meta.url);
385
+ }
353
386
  function _spawnWorker() {
354
- const worker = new Worker(new URL("./worker.ts", import.meta.url));
387
+ const worker = new Worker(_resolveWorkerURL());
355
388
  if (typeof worker.unref === "function") {
356
389
  worker.unref();
357
390
  }
@@ -0,0 +1,415 @@
1
+ // @bun
2
+ // src/internal/worker.ts
3
+ import { parentPort } from "worker_threads";
4
+ import { CString, ptr } from "bun:ffi";
5
+
6
+ // src/internal/verbs.ts
7
+ var VERBS = [
8
+ "spawn",
9
+ "status",
10
+ "get",
11
+ "send-keys",
12
+ "read-pane",
13
+ "kill",
14
+ "decide",
15
+ "resume",
16
+ "find-missing",
17
+ "expire",
18
+ "delete",
19
+ "make-template",
20
+ "list",
21
+ "pause",
22
+ "version"
23
+ ];
24
+ function kebabToUnderscore(v) {
25
+ return v.replace(/-/g, "_");
26
+ }
27
+ var HANDLE_FREE_VERBS = new Set(["version"]);
28
+ function isHandleFree(verb) {
29
+ return HANDLE_FREE_VERBS.has(verb);
30
+ }
31
+
32
+ // src/internal/freeGuard.ts
33
+ class FreeGuard {
34
+ _ptr;
35
+ _free;
36
+ constructor(ptr, free) {
37
+ this._ptr = ptr;
38
+ this._free = free;
39
+ }
40
+ get ptr() {
41
+ return this._ptr;
42
+ }
43
+ free() {
44
+ if (this._ptr === null)
45
+ return;
46
+ const p = this._ptr;
47
+ this._ptr = null;
48
+ this._free(p);
49
+ }
50
+ }
51
+
52
+ // src/platform.ts
53
+ import { existsSync } from "fs";
54
+ import { fileURLToPath } from "url";
55
+ import { join, dirname } from "path";
56
+ import { dlopen } from "bun:ffi";
57
+
58
+ // src/errors.ts
59
+ class AgentDirectorError extends Error {
60
+ verb;
61
+ errName;
62
+ errDescription;
63
+ constructor(verb, err_name, err_description) {
64
+ super(`${err_name}: ${err_description}`);
65
+ this.verb = verb;
66
+ this.errName = err_name;
67
+ this.errDescription = err_description;
68
+ this.name = this.constructor.name;
69
+ Object.setPrototypeOf(this, new.target.prototype);
70
+ }
71
+ }
72
+
73
+ class ErrClientClosed extends AgentDirectorError {
74
+ constructor() {
75
+ super("", "ErrClientClosed", "client is closed: call new Client() to obtain a fresh handle");
76
+ this.name = "ErrClientClosed";
77
+ Object.setPrototypeOf(this, new.target.prototype);
78
+ }
79
+ }
80
+
81
+ class ErrUnsupportedPlatform extends AgentDirectorError {
82
+ constructor(tuple) {
83
+ super("", "ErrUnsupportedPlatform", `platform/arch tuple "${tuple}" is not supported; supported: linux-x64, darwin-arm64`);
84
+ this.name = "ErrUnsupportedPlatform";
85
+ Object.setPrototypeOf(this, new.target.prototype);
86
+ }
87
+ }
88
+
89
+ class ErrPlatformPackageMissing extends AgentDirectorError {
90
+ constructor(pkgName, detail) {
91
+ const desc = detail ? `native sub-package "${pkgName}" is not usable: ${detail}` : `native sub-package "${pkgName}" is not installed or its binary is absent`;
92
+ super("", "ErrPlatformPackageMissing", desc);
93
+ this.name = "ErrPlatformPackageMissing";
94
+ Object.setPrototypeOf(this, new.target.prototype);
95
+ }
96
+ }
97
+
98
+ class ErrBunVersionTooOld extends AgentDirectorError {
99
+ constructor(actual, minimum) {
100
+ super("", "ErrBunVersionTooOld", `Bun ${actual} is below the minimum required version ${minimum}; upgrade Bun to continue`);
101
+ this.name = "ErrBunVersionTooOld";
102
+ Object.setPrototypeOf(this, new.target.prototype);
103
+ }
104
+ }
105
+
106
+ class ErrCwdMissing extends AgentDirectorError {
107
+ }
108
+
109
+ class ErrCwdNotAPath extends AgentDirectorError {
110
+ }
111
+
112
+ class ErrCwdNotFound extends AgentDirectorError {
113
+ }
114
+
115
+ class ErrCwdNotADirectory extends AgentDirectorError {
116
+ }
117
+
118
+ class ErrRelayModeInvalid extends AgentDirectorError {
119
+ }
120
+
121
+ class ErrSpawnDeniedFlag extends AgentDirectorError {
122
+ }
123
+
124
+ class ErrReservedEnvKey extends AgentDirectorError {
125
+ }
126
+
127
+ class ErrInstanceIdCollision extends AgentDirectorError {
128
+ }
129
+
130
+ class ErrTmuxSessionNameEmpty extends AgentDirectorError {
131
+ }
132
+
133
+ class ErrTmuxSessionNameInvalid extends AgentDirectorError {
134
+ }
135
+
136
+ class ErrTmuxSessionNameTooLong extends AgentDirectorError {
137
+ }
138
+
139
+ class ErrSpawnNotFound extends AgentDirectorError {
140
+ }
141
+
142
+ class ErrTmuxNotAvailable extends AgentDirectorError {
143
+ }
144
+
145
+ class ErrTmuxSessionCreate extends AgentDirectorError {
146
+ }
147
+
148
+ class ErrTmuxSendKeys extends AgentDirectorError {
149
+ }
150
+
151
+ class ErrTmuxCaptureFailed extends AgentDirectorError {
152
+ }
153
+
154
+ class ErrSpawnNotInteractive extends AgentDirectorError {
155
+ }
156
+
157
+ class ErrSendKeysWhileRelayed extends AgentDirectorError {
158
+ }
159
+
160
+ class ErrSpawnNotPausable extends AgentDirectorError {
161
+ }
162
+
163
+ class ErrPauseTimeout extends AgentDirectorError {
164
+ }
165
+
166
+ class ErrListInvalidLabel extends AgentDirectorError {
167
+ }
168
+
169
+ class ErrTemplateNameUnsafe extends AgentDirectorError {
170
+ }
171
+
172
+ class ErrTemplateNotFound extends AgentDirectorError {
173
+ }
174
+
175
+ class ErrTemplateMalformed extends AgentDirectorError {
176
+ }
177
+
178
+ class ErrTemplateExists extends AgentDirectorError {
179
+ }
180
+
181
+ class ErrProbeUnsupported extends AgentDirectorError {
182
+ }
183
+
184
+ class ErrSpawnNotResumable extends AgentDirectorError {
185
+ }
186
+
187
+ class ErrNoSessionId extends AgentDirectorError {
188
+ }
189
+
190
+ class ErrJsonlMissing extends AgentDirectorError {
191
+ }
192
+
193
+ class ErrRelayModeOff extends AgentDirectorError {
194
+ }
195
+
196
+ class ErrInvalidDecision extends AgentDirectorError {
197
+ }
198
+
199
+ class ErrNoOpenPermissionRequest extends AgentDirectorError {
200
+ }
201
+
202
+ class ErrAlreadyDecided extends AgentDirectorError {
203
+ }
204
+
205
+ class ErrUnknownHandle extends AgentDirectorError {
206
+ }
207
+ var ERROR_TABLE = {
208
+ ErrCwdMissing,
209
+ ErrCwdNotAPath,
210
+ ErrCwdNotFound,
211
+ ErrCwdNotADirectory,
212
+ ErrRelayModeInvalid,
213
+ ErrSpawnDeniedFlag,
214
+ ErrReservedEnvKey,
215
+ ErrInstanceIdCollision,
216
+ ErrTmuxSessionNameEmpty,
217
+ ErrTmuxSessionNameInvalid,
218
+ ErrTmuxSessionNameTooLong,
219
+ ErrSpawnNotFound,
220
+ ErrNoOpenPermissionRequest,
221
+ ErrAlreadyDecided,
222
+ ErrTmuxNotAvailable,
223
+ ErrTmuxSessionCreate,
224
+ ErrTmuxSendKeys,
225
+ ErrTmuxCaptureFailed,
226
+ ErrSpawnNotInteractive,
227
+ ErrSendKeysWhileRelayed,
228
+ ErrSpawnNotPausable,
229
+ ErrPauseTimeout,
230
+ ErrListInvalidLabel,
231
+ ErrSpawnNotResumable,
232
+ ErrNoSessionId,
233
+ ErrJsonlMissing,
234
+ ErrRelayModeOff,
235
+ ErrInvalidDecision,
236
+ ErrTemplateNameUnsafe,
237
+ ErrTemplateNotFound,
238
+ ErrTemplateMalformed,
239
+ ErrTemplateExists,
240
+ ErrProbeUnsupported,
241
+ ErrUnknownHandle
242
+ };
243
+ function errorFromEnvelope(verb, err_name, err_description) {
244
+ const Ctor = ERROR_TABLE[err_name];
245
+ if (Ctor) {
246
+ return new Ctor(verb, err_name, err_description);
247
+ }
248
+ console.warn(`agent-director: unknown err_name "${err_name}" (verb=${verb}); returning base AgentDirectorError`);
249
+ return new AgentDirectorError(verb, err_name, err_description);
250
+ }
251
+
252
+ // src/internal/bindingSpec.ts
253
+ import { FFIType } from "bun:ffi";
254
+ var BINDING_SYMBOL_NAMES = [
255
+ "ad_open",
256
+ "ad_close",
257
+ "ad_free_cstring",
258
+ ...VERBS.map((v) => "ad_" + kebabToUnderscore(v))
259
+ ];
260
+ function buildBindingSpec() {
261
+ const spec = {
262
+ ad_open: { args: [FFIType.cstring], returns: FFIType.ptr },
263
+ ad_close: { args: [FFIType.cstring], returns: FFIType.ptr },
264
+ ad_free_cstring: { args: [FFIType.ptr], returns: FFIType.void }
265
+ };
266
+ for (const verb of VERBS) {
267
+ spec["ad_" + kebabToUnderscore(verb)] = {
268
+ args: [FFIType.cstring],
269
+ returns: FFIType.ptr
270
+ };
271
+ }
272
+ return spec;
273
+ }
274
+
275
+ // src/platform.ts
276
+ var MIN_BUN_VERSION = "1.0.21";
277
+ var SUPPORTED_TUPLES = new Map([
278
+ ["linux-x64", "@agent-director/linux-x64"],
279
+ ["darwin-arm64", "@agent-director/darwin-arm64"]
280
+ ]);
281
+ function compareSemver(a, b) {
282
+ const parse = (s) => s.split(".").map((p) => parseInt(p, 10) || 0);
283
+ const pa = parse(a);
284
+ const pb = parse(b);
285
+ for (let i = 0;i < 3; i++) {
286
+ const diff = (pa[i] ?? 0) - (pb[i] ?? 0);
287
+ if (diff !== 0)
288
+ return diff;
289
+ }
290
+ return 0;
291
+ }
292
+ function resolveNativePath(opts = {}) {
293
+ const platform = opts.platform ?? process.platform;
294
+ const arch = opts.arch ?? process.arch;
295
+ const bunVersion = opts.bunVersion ?? Bun.version;
296
+ if (compareSemver(bunVersion, MIN_BUN_VERSION) < 0) {
297
+ throw new ErrBunVersionTooOld(bunVersion, MIN_BUN_VERSION);
298
+ }
299
+ const tuple = `${platform}-${arch}`;
300
+ const subpkgName = SUPPORTED_TUPLES.get(tuple);
301
+ if (!subpkgName) {
302
+ throw new ErrUnsupportedPlatform(tuple);
303
+ }
304
+ const binaryExt = platform === "darwin" ? "dylib" : "so";
305
+ const binaryName = `libagent_director.${binaryExt}`;
306
+ const cabiDir = process.env.AD_CABI_DIR;
307
+ if (cabiDir) {
308
+ const cabiPath = join(cabiDir, binaryName);
309
+ if (!existsSync(cabiPath)) {
310
+ throw new ErrPlatformPackageMissing(subpkgName, `AD_CABI_DIR is set but "${binaryName}" not found in ${cabiDir}`);
311
+ }
312
+ return cabiPath;
313
+ }
314
+ let pkgDir;
315
+ try {
316
+ const resolved = import.meta.resolve(`${subpkgName}/package.json`);
317
+ pkgDir = dirname(fileURLToPath(resolved));
318
+ } catch {
319
+ throw new ErrPlatformPackageMissing(subpkgName);
320
+ }
321
+ const libPath = join(pkgDir, binaryName);
322
+ if (!existsSync(libPath)) {
323
+ throw new ErrPlatformPackageMissing(subpkgName, `binary "${binaryName}" not found in package directory ${pkgDir}`);
324
+ }
325
+ return libPath;
326
+ }
327
+ function _loadNativeInternal(opts = {}) {
328
+ const libPath = resolveNativePath(opts);
329
+ const bindingSpec = buildBindingSpec();
330
+ const loaded = dlopen(libPath, bindingSpec);
331
+ return { lib: loaded.symbols, libPath };
332
+ }
333
+ function loadNative() {
334
+ return _loadNativeInternal();
335
+ }
336
+
337
+ // src/internal/worker.ts
338
+ if (parentPort === null) {
339
+ throw new Error("worker.ts must run inside a node:worker_threads Worker");
340
+ }
341
+ var port = parentPort;
342
+ function fatalStartup(message) {
343
+ port.postMessage({ id: -1, type: "startup-error", message });
344
+ process.exit(1);
345
+ }
346
+ function doLoadNative() {
347
+ try {
348
+ const { lib } = loadNative();
349
+ return lib;
350
+ } catch (e) {
351
+ const msg = e instanceof Error ? e.message : String(e);
352
+ fatalStartup(`Failed to load native library: ${msg}`);
353
+ }
354
+ }
355
+ var lib = doLoadNative();
356
+ port.postMessage({ id: -1, type: "ready" });
357
+ function getSymbol(name) {
358
+ const sym = lib[name];
359
+ if (typeof sym !== "function") {
360
+ throw new Error(`symbol not found in loaded library: ${name}`);
361
+ }
362
+ return sym;
363
+ }
364
+ function callSymbol(fn, jsonArg) {
365
+ const inputBuf = Buffer.from(jsonArg + "\x00", "utf8");
366
+ const rawPtr = fn(ptr(inputBuf));
367
+ if (rawPtr === null || rawPtr === 0) {
368
+ throw new Error("C-ABI function returned a null pointer");
369
+ }
370
+ const freeSymbol = getSymbol("ad_free_cstring");
371
+ const guard = new FreeGuard(rawPtr, (p) => {
372
+ freeSymbol(p);
373
+ });
374
+ try {
375
+ return new CString(guard.ptr).toString();
376
+ } finally {
377
+ guard.free();
378
+ }
379
+ }
380
+ port.on("message", (msg) => {
381
+ const { id, op, handle, paramsJSON } = msg;
382
+ let jsonString;
383
+ try {
384
+ if (op === "open") {
385
+ jsonString = callSymbol(getSymbol("ad_open"), paramsJSON);
386
+ } else if (op === "close") {
387
+ const closeJSON = handle !== null ? JSON.stringify({ handle }) : "{}";
388
+ jsonString = callSymbol(getSymbol("ad_close"), closeJSON);
389
+ } else {
390
+ const symbolName = "ad_" + kebabToUnderscore(op);
391
+ const verbFn = getSymbol(symbolName);
392
+ let inputJSON;
393
+ if (handle !== null && !isHandleFree(op)) {
394
+ const parsedParams = JSON.parse(paramsJSON);
395
+ parsedParams.handle = handle;
396
+ inputJSON = JSON.stringify(parsedParams);
397
+ } else {
398
+ inputJSON = paramsJSON;
399
+ }
400
+ jsonString = callSymbol(verbFn, inputJSON);
401
+ }
402
+ } catch (e) {
403
+ port.postMessage({
404
+ id,
405
+ type: "ffi-error",
406
+ message: e instanceof Error ? e.message : String(e)
407
+ });
408
+ return;
409
+ }
410
+ port.postMessage({
411
+ id,
412
+ type: "result",
413
+ jsonString
414
+ });
415
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-director",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "type": "module",
5
5
  "os": [
6
6
  "linux",
@@ -42,8 +42,8 @@
42
42
  "prepublishOnly": "bun run scripts/prepublish-guards.ts"
43
43
  },
44
44
  "optionalDependencies": {
45
- "@agent-director/linux-x64": "^0.4.3",
46
- "@agent-director/darwin-arm64": "^0.4.3"
45
+ "@agent-director/linux-x64": "^0.4.4",
46
+ "@agent-director/darwin-arm64": "^0.4.4"
47
47
  },
48
48
  "devDependencies": {
49
49
  "typescript": ">=5.2.0",
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  name: install-agent-director
3
3
  description: Install (or upgrade) agent-director on this machine. Runs the bundled install.sh against the user's ~/ — creates ~/.agent-director/ with the binary, warms up state.db, and injects two persistent `agent-director help` hooks into ~/.claude/settings.json (SessionStart + SessionEnd reason=compact). Use this skill when the user says "install agent-director", "set up agent-director", or "upgrade agent-director on this machine".
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  ---
6
6
 
7
7
  ## When to invoke