@getworkle/cli 0.2.7 → 0.2.8

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/cli.js CHANGED
@@ -21367,6 +21367,7 @@ var AgentService = class _AgentService {
21367
21367
  scheduler;
21368
21368
  relayClient = null;
21369
21369
  unsubscribeRelay = null;
21370
+ relaySendFn = null;
21370
21371
  /** Map of agentId -> agent stub (for name lookups). */
21371
21372
  agentMap = /* @__PURE__ */ new Map();
21372
21373
  /** Map of agentId -> pulled config (for schedule + runtime settings). */
@@ -21396,6 +21397,55 @@ var AgentService = class _AgentService {
21396
21397
  return;
21397
21398
  }
21398
21399
  console.log("[agents] Starting AgentService...");
21400
+ await this.syncConfigsAndSchedules();
21401
+ const relayUrl = deriveRelayUrl(this.apiUrl);
21402
+ this.relayClient = new RelayClient(relayUrl, this.instanceId, this.token);
21403
+ this.unsubscribeRelay = this.relayClient.onMessage((msg) => {
21404
+ this.handleRelayMessage(msg);
21405
+ });
21406
+ await this.relayClient.connect();
21407
+ console.log("[agents] Connected to relay");
21408
+ this.running = true;
21409
+ console.log("[agents] AgentService running");
21410
+ }
21411
+ /**
21412
+ * Start embedded inside SyncService — no own relay connection.
21413
+ * Relay messages are forwarded via ingestRelayMessage().
21414
+ * Progress frames are sent via the provided sendFn.
21415
+ */
21416
+ async startEmbedded(sendFn) {
21417
+ if (this.running) {
21418
+ console.log("[agents] AgentService already running");
21419
+ return;
21420
+ }
21421
+ console.log("[agents] Starting AgentService (embedded)...");
21422
+ this.relaySendFn = sendFn;
21423
+ await this.syncConfigsAndSchedules();
21424
+ this.running = true;
21425
+ console.log("[agents] AgentService running (embedded \u2014 relay via SyncService)");
21426
+ }
21427
+ /**
21428
+ * Handle a relay message forwarded from SyncService.
21429
+ */
21430
+ ingestRelayMessage(msg) {
21431
+ this.handleRelayMessage(msg);
21432
+ }
21433
+ /**
21434
+ * Send a message to the relay — uses own RelayClient in standalone mode,
21435
+ * or the injected sendFn in embedded mode.
21436
+ */
21437
+ sendRelay(msg) {
21438
+ if (this.relaySendFn) {
21439
+ this.relaySendFn(msg);
21440
+ } else if (this.relayClient?.connected) {
21441
+ this.relayClient.send(msg);
21442
+ }
21443
+ }
21444
+ isRelayConnected() {
21445
+ if (this.relaySendFn) return true;
21446
+ return this.relayClient?.connected ?? false;
21447
+ }
21448
+ async syncConfigsAndSchedules() {
21399
21449
  const stubs = await this.materializer.listAgents();
21400
21450
  console.log(`[agents] Found ${stubs.length} claude_code agent(s)`);
21401
21451
  const configs = [];
@@ -21424,16 +21474,7 @@ var AgentService = class _AgentService {
21424
21474
  `[agents] Materialized ${materialized.length} agent config(s)`
21425
21475
  );
21426
21476
  }
21427
- const relayUrl = deriveRelayUrl(this.apiUrl);
21428
- this.relayClient = new RelayClient(relayUrl, this.instanceId, this.token);
21429
- this.unsubscribeRelay = this.relayClient.onMessage((msg) => {
21430
- this.handleRelayMessage(msg);
21431
- });
21432
- await this.relayClient.connect();
21433
- console.log("[agents] Connected to relay");
21434
21477
  this.refreshSchedules();
21435
- this.running = true;
21436
- console.log("[agents] AgentService running");
21437
21478
  }
21438
21479
  /**
21439
21480
  * Trigger an agent run. Prevents concurrent runs of the same agent.
@@ -21446,8 +21487,8 @@ var AgentService = class _AgentService {
21446
21487
  console.log(
21447
21488
  `[agents] Agent ${agentId} is already running \u2014 skipping trigger`
21448
21489
  );
21449
- if (executionId && this.relayClient?.connected) {
21450
- this.relayClient.send({
21490
+ if (executionId && this.isRelayConnected()) {
21491
+ this.sendRelay({
21451
21492
  type: "agent.result",
21452
21493
  payload: {
21453
21494
  agentId,
@@ -21466,8 +21507,8 @@ var AgentService = class _AgentService {
21466
21507
  }
21467
21508
  const runExecutionId = executionId ?? randomUUID();
21468
21509
  const sendProgressPart = (part, sessionId) => {
21469
- if (this.relayClient?.connected) {
21470
- this.relayClient.send({
21510
+ if (this.isRelayConnected()) {
21511
+ this.sendRelay({
21471
21512
  type: "agent.progress",
21472
21513
  payload: {
21473
21514
  agentId,
@@ -21673,8 +21714,8 @@ var AgentService = class _AgentService {
21673
21714
  this.activeRuns.set(agentId, runPromise);
21674
21715
  try {
21675
21716
  const result = await runPromise;
21676
- if (this.relayClient?.connected) {
21677
- this.relayClient.send({
21717
+ if (this.isRelayConnected()) {
21718
+ this.sendRelay({
21678
21719
  type: "agent.result",
21679
21720
  payload: {
21680
21721
  agentId,
@@ -21691,8 +21732,8 @@ var AgentService = class _AgentService {
21691
21732
  } catch (err) {
21692
21733
  const error46 = err instanceof Error ? err.message : String(err);
21693
21734
  sendProgressPart({ type: "error", message: error46 });
21694
- if (this.relayClient?.connected) {
21695
- this.relayClient.send({
21735
+ if (this.isRelayConnected()) {
21736
+ this.sendRelay({
21696
21737
  type: "agent.result",
21697
21738
  payload: {
21698
21739
  agentId,
@@ -23086,6 +23127,22 @@ var SyncService = class {
23086
23127
  unsubscribeEvents = null;
23087
23128
  unsubscribeRelayMessages = null;
23088
23129
  running = false;
23130
+ externalMessageHandler = null;
23131
+ /**
23132
+ * Register a handler for relay messages that SyncService doesn't handle
23133
+ * (e.g. agent.trigger, agent.cancel, agent.config.updated).
23134
+ * Must be called before start().
23135
+ */
23136
+ onRelayMessage(handler) {
23137
+ this.externalMessageHandler = handler;
23138
+ }
23139
+ /**
23140
+ * Send a message through the relay WebSocket connection.
23141
+ * Used by AgentService in embedded mode to send progress/result frames.
23142
+ */
23143
+ sendRelayMessage(msg) {
23144
+ this.relayClient?.send(msg);
23145
+ }
23089
23146
  get isRunning() {
23090
23147
  return this.running;
23091
23148
  }
@@ -23181,7 +23238,12 @@ var SyncService = class {
23181
23238
  );
23182
23239
  }
23183
23240
  this.unsubscribeRelayMessages = this.relayClient.onMessage((msg) => {
23184
- if (msg.type !== "rpc.request") return;
23241
+ if (msg.type !== "rpc.request") {
23242
+ if (this.externalMessageHandler) {
23243
+ this.externalMessageHandler(msg);
23244
+ }
23245
+ return;
23246
+ }
23185
23247
  const rpcId = typeof msg.id === "string" ? msg.id : null;
23186
23248
  const method = typeof msg.method === "string" ? msg.method : null;
23187
23249
  const params = msg.params && typeof msg.params === "object" ? msg.params : {};
@@ -23681,9 +23743,11 @@ program2.command("start").description("Start the Workle Claw sync service").opti
23681
23743
  console.log(color.dim("Starting sync service... (Ctrl+C to stop)\n"));
23682
23744
  const processManager = new ProcessManager();
23683
23745
  const syncService = new SyncService();
23746
+ let agentService = null;
23684
23747
  let weSpawnedGateway = false;
23685
23748
  const shutdown = () => {
23686
23749
  console.log("\n" + color.dim("Shutting down..."));
23750
+ agentService?.stop();
23687
23751
  syncService.stop();
23688
23752
  if (weSpawnedGateway) {
23689
23753
  void processManager.stop().then(() => {
@@ -23706,6 +23770,15 @@ program2.command("start").description("Start the Workle Claw sync service").opti
23706
23770
  weSpawnedGateway = true;
23707
23771
  }
23708
23772
  await syncService.start();
23773
+ try {
23774
+ agentService = await createAgentService();
23775
+ syncService.onRelayMessage((msg) => agentService.ingestRelayMessage(msg));
23776
+ await agentService.startEmbedded((msg) => syncService.sendRelayMessage(msg));
23777
+ } catch (err) {
23778
+ console.error(
23779
+ color.dim(`[agents] AgentService failed to start: ${err instanceof Error ? err.message : String(err)}`)
23780
+ );
23781
+ }
23709
23782
  console.log(color.green("\u2713 Running"));
23710
23783
  console.log(color.dim(" Press Ctrl+C to stop\n"));
23711
23784
  } catch (err) {