@powerhousedao/clint 6.4.0-next.1 → 6.4.0-next.2
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/{build-skills-XkfkDhWl.mjs → build-skills-Bpbbnin0.mjs} +2 -2
- package/dist/{build-skills-XkfkDhWl.mjs.map → build-skills-Bpbbnin0.mjs.map} +1 -1
- package/dist/{index-C3-HSfKb.d.mts → index-CfHcet4v.d.mts} +2 -2
- package/dist/{index-C3-HSfKb.d.mts.map → index-CfHcet4v.d.mts.map} +1 -1
- package/dist/index.d.mts +33 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/integrations/mastra/index.d.mts +1 -1
- package/dist/integrations/powerhouse/index.d.mts +2 -2
- package/dist/runners/cli-runner.d.mts +5 -1
- package/dist/runners/cli-runner.d.mts.map +1 -1
- package/dist/runners/cli-runner.mjs +23 -9
- package/dist/runners/cli-runner.mjs.map +1 -1
- package/dist/runners/mastra-runner.mjs +1 -1
- package/dist/{src-055YwI8s.mjs → src-BmMd0Wl4.mjs} +159 -21
- package/dist/src-BmMd0Wl4.mjs.map +1 -0
- package/dist/testing/index.d.mts +2 -2
- package/dist/{types-CbHJ-ZnH.d.mts → types-3TnDjQ1w.d.mts} +2 -2
- package/dist/{types-CbHJ-ZnH.d.mts.map → types-3TnDjQ1w.d.mts.map} +1 -1
- package/dist/{types-CF6GuPJd.d.mts → types-DbQKfvWc.d.mts} +20 -1
- package/dist/types-DbQKfvWc.d.mts.map +1 -0
- package/package.json +13 -13
- package/dist/src-055YwI8s.mjs.map +0 -1
- package/dist/types-CF6GuPJd.d.mts.map +0 -1
|
@@ -10,6 +10,7 @@ import { z } from "zod";
|
|
|
10
10
|
import { Command } from "commander";
|
|
11
11
|
import { randomBytes, randomUUID } from "node:crypto";
|
|
12
12
|
import { homedir } from "node:os";
|
|
13
|
+
import { ControlError, callControl, processControlChannel } from "@powerhousedao/host-core/control-channel";
|
|
13
14
|
import { spawn } from "node:child_process";
|
|
14
15
|
import Handlebars from "handlebars";
|
|
15
16
|
import { EventEmitter } from "node:events";
|
|
@@ -539,6 +540,145 @@ function generateConfigCommandHelp(cliName, configSchema, workdir, sensitiveKeys
|
|
|
539
540
|
return lines.join("\n");
|
|
540
541
|
}
|
|
541
542
|
//#endregion
|
|
543
|
+
//#region src/core/service-control.ts
|
|
544
|
+
/**
|
|
545
|
+
* A CLI's services over the control channel of the process that runs them.
|
|
546
|
+
* The process that keeps running answers `list`, `start`, `stop`, `restart`
|
|
547
|
+
* and `logs` ({@link serveServices}); a command typed in a second terminal
|
|
548
|
+
* reaches them through a manager that forwards ({@link connectRemoteServices}).
|
|
549
|
+
* A command with nothing to forward to cannot start a service at all
|
|
550
|
+
* ({@link refuseServiceStarts}): the service would end with the command.
|
|
551
|
+
*/
|
|
552
|
+
const startParamsSchema = z.object({
|
|
553
|
+
id: z.string().min(1),
|
|
554
|
+
options: z.object({
|
|
555
|
+
workdir: z.string().optional(),
|
|
556
|
+
cwd: z.string().optional(),
|
|
557
|
+
params: z.record(z.string(), z.unknown()).optional(),
|
|
558
|
+
name: z.string().optional()
|
|
559
|
+
}).optional()
|
|
560
|
+
});
|
|
561
|
+
const instanceParamsSchema = z.object({
|
|
562
|
+
id: z.string().min(1),
|
|
563
|
+
instanceId: z.string().optional()
|
|
564
|
+
});
|
|
565
|
+
const listParamsSchema = z.object({ serviceId: z.string().optional() });
|
|
566
|
+
const logsParamsSchema = instanceParamsSchema.extend({ lines: z.number().int().positive().optional() });
|
|
567
|
+
function parse(schema, params) {
|
|
568
|
+
const parsed = schema.safeParse(params ?? {});
|
|
569
|
+
if (!parsed.success) throw new Error(`Bad parameters: ${parsed.error.issues.map((i) => `${i.path.join(".") || "<root>"}: ${i.message}`).join("; ")}`);
|
|
570
|
+
return parsed.data;
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Stops the service's running instance, if any, and starts it again where
|
|
574
|
+
* and as it ran. Resolves with the new instance's id.
|
|
575
|
+
*/
|
|
576
|
+
async function restartService(services, id, instanceId, workdir) {
|
|
577
|
+
const [instance] = services.list(id).filter((s) => s.status === "ready" || s.status === "starting");
|
|
578
|
+
let name;
|
|
579
|
+
if (instance) {
|
|
580
|
+
if (instance.instanceId?.includes(":")) name = instance.instanceId.split(":")[1];
|
|
581
|
+
await services.stop(id, instanceId);
|
|
582
|
+
}
|
|
583
|
+
const cwd = instance?.workdir ?? workdir;
|
|
584
|
+
return services.start(id, {
|
|
585
|
+
workdir: cwd,
|
|
586
|
+
cwd,
|
|
587
|
+
params: instance?.params,
|
|
588
|
+
name
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
/** Answers service requests on `channel` from `services`. */
|
|
592
|
+
function serveServices(channel, services, cli) {
|
|
593
|
+
channel.describe({
|
|
594
|
+
cli: cli.name,
|
|
595
|
+
version: cli.version
|
|
596
|
+
});
|
|
597
|
+
channel.handle("list", (params) => ({ instances: services.list(parse(listParamsSchema, params).serviceId) }));
|
|
598
|
+
channel.handle("start", async (params) => {
|
|
599
|
+
const { id, options } = parse(startParamsSchema, params);
|
|
600
|
+
return {
|
|
601
|
+
instanceId: await services.start(id, options),
|
|
602
|
+
instances: services.list()
|
|
603
|
+
};
|
|
604
|
+
});
|
|
605
|
+
channel.handle("stop", async (params) => {
|
|
606
|
+
const { id, instanceId } = parse(instanceParamsSchema, params);
|
|
607
|
+
await services.stop(id, instanceId);
|
|
608
|
+
return { instances: services.list() };
|
|
609
|
+
});
|
|
610
|
+
channel.handle("restart", async (params) => {
|
|
611
|
+
const { id, instanceId } = parse(instanceParamsSchema, params);
|
|
612
|
+
return {
|
|
613
|
+
instanceId: await restartService(services, id, instanceId, cli.workdir),
|
|
614
|
+
instances: services.list()
|
|
615
|
+
};
|
|
616
|
+
});
|
|
617
|
+
channel.handle("logs", (params) => {
|
|
618
|
+
const { id, instanceId, lines } = parse(logsParamsSchema, params);
|
|
619
|
+
return { text: services.logs(id, instanceId, lines) };
|
|
620
|
+
});
|
|
621
|
+
}
|
|
622
|
+
const instancesOf = (result) => result?.instances ?? [];
|
|
623
|
+
/**
|
|
624
|
+
* A manager whose instances live in the process behind `control`: start,
|
|
625
|
+
* stop and list act there; logs, definitions and scans are read here (the
|
|
626
|
+
* files are the same). Undefined when that process does not answer service
|
|
627
|
+
* requests.
|
|
628
|
+
*/
|
|
629
|
+
async function connectRemoteServices(local, control) {
|
|
630
|
+
let instances;
|
|
631
|
+
try {
|
|
632
|
+
const hello = await callControl(control, "hello", void 0, { timeoutMs: 5e3 });
|
|
633
|
+
const methods = Array.isArray(hello?.methods) ? hello.methods : [];
|
|
634
|
+
if (![
|
|
635
|
+
"list",
|
|
636
|
+
"start",
|
|
637
|
+
"stop"
|
|
638
|
+
].every((m) => methods.includes(m))) return;
|
|
639
|
+
instances = instancesOf(await callControl(control, "list"));
|
|
640
|
+
} catch (error) {
|
|
641
|
+
if (error instanceof ControlError) return void 0;
|
|
642
|
+
throw error;
|
|
643
|
+
}
|
|
644
|
+
return {
|
|
645
|
+
...local,
|
|
646
|
+
async start(id, opts) {
|
|
647
|
+
const result = await callControl(control, "start", {
|
|
648
|
+
id,
|
|
649
|
+
...opts ? { options: opts } : {}
|
|
650
|
+
});
|
|
651
|
+
instances = instancesOf(result);
|
|
652
|
+
return result.instanceId;
|
|
653
|
+
},
|
|
654
|
+
async stop(id, instanceId) {
|
|
655
|
+
instances = instancesOf(await callControl(control, "stop", {
|
|
656
|
+
id,
|
|
657
|
+
...instanceId ? { instanceId } : {}
|
|
658
|
+
}));
|
|
659
|
+
},
|
|
660
|
+
stopAll: () => local.stopAll(),
|
|
661
|
+
list: (serviceId) => serviceId ? instances.filter((s) => s.serviceId === serviceId) : [...instances]
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
/** Thrown by a start that cannot outlive its command. */
|
|
665
|
+
var ServiceStartRefused = class extends Error {
|
|
666
|
+
constructor(id, serveCommand) {
|
|
667
|
+
super(`${id} was not started: a service runs as long as the process that started it, and this command ends. Start it where it keeps running: ${serveCommand ? `\`${serveCommand}\`` : "`ph run <host>`"}, then run this command from a second terminal.`);
|
|
668
|
+
this.name = "ServiceStartRefused";
|
|
669
|
+
}
|
|
670
|
+
};
|
|
671
|
+
/**
|
|
672
|
+
* The manager of a command that ends with nothing running to forward to:
|
|
673
|
+
* everything but `start`, which is refused with where to run instead.
|
|
674
|
+
*/
|
|
675
|
+
function refuseServiceStarts(local, serveCommand) {
|
|
676
|
+
return {
|
|
677
|
+
...local,
|
|
678
|
+
start: (id) => Promise.reject(new ServiceStartRefused(id, serveCommand))
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
//#endregion
|
|
542
682
|
//#region src/core/service-command.ts
|
|
543
683
|
/**
|
|
544
684
|
* Format a service instance status line for display.
|
|
@@ -609,6 +749,7 @@ function createServiceCommands(def) {
|
|
|
609
749
|
});
|
|
610
750
|
return { text: formatStatus(services.list(id).find((s) => s.instanceId === instanceId)) };
|
|
611
751
|
} catch (err) {
|
|
752
|
+
if (err instanceof ServiceStartRefused) throw err;
|
|
612
753
|
const msg = err instanceof Error ? err.message : String(err);
|
|
613
754
|
return { text: `✗ ${id}: ${msg}` };
|
|
614
755
|
}
|
|
@@ -640,26 +781,10 @@ function createServiceCommands(def) {
|
|
|
640
781
|
const services = context.services;
|
|
641
782
|
if (!services) throw new Error("No services configured");
|
|
642
783
|
try {
|
|
643
|
-
const
|
|
644
|
-
let prevWorkdir;
|
|
645
|
-
let prevParams;
|
|
646
|
-
let prevName;
|
|
647
|
-
if (running.length > 0) {
|
|
648
|
-
const instance = running[0];
|
|
649
|
-
prevWorkdir = instance.workdir;
|
|
650
|
-
prevParams = instance.params;
|
|
651
|
-
if (instance.instanceId?.includes(":")) prevName = instance.instanceId.split(":")[1];
|
|
652
|
-
await services.stop(id, input.instance);
|
|
653
|
-
}
|
|
654
|
-
const cwd = prevWorkdir ?? context.workdir;
|
|
655
|
-
const instanceId = await services.start(id, {
|
|
656
|
-
workdir: cwd,
|
|
657
|
-
cwd,
|
|
658
|
-
params: prevParams,
|
|
659
|
-
name: prevName
|
|
660
|
-
});
|
|
784
|
+
const instanceId = await restartService(services, id, input.instance, context.workdir);
|
|
661
785
|
return { text: formatStatus(services.list(id).find((s) => s.instanceId === instanceId)) };
|
|
662
786
|
} catch (err) {
|
|
787
|
+
if (err instanceof ServiceStartRefused) throw err;
|
|
663
788
|
const msg = err instanceof Error ? err.message : String(err);
|
|
664
789
|
return { text: `✗ ${id}: ${msg}` };
|
|
665
790
|
}
|
|
@@ -3037,7 +3162,9 @@ function defineCli(options) {
|
|
|
3037
3162
|
reactor: runOptions?.reactor,
|
|
3038
3163
|
onServe: runOptions?.onServe,
|
|
3039
3164
|
signal: runOptions?.signal,
|
|
3040
|
-
members: runOptions?.members
|
|
3165
|
+
members: runOptions?.members,
|
|
3166
|
+
remoteServices: runOptions?.remoteServices,
|
|
3167
|
+
serveCommand: runOptions?.serveCommand
|
|
3041
3168
|
});
|
|
3042
3169
|
}
|
|
3043
3170
|
/**
|
|
@@ -3228,6 +3355,11 @@ function defineCli(options) {
|
|
|
3228
3355
|
const mode = !interactiveFlag ? "headless" : isTTY ? "interactive-terminal" : "interactive-streaming";
|
|
3229
3356
|
const hasKeepAlive = !!effectiveRoutine;
|
|
3230
3357
|
if (isBuiltinFlag || !interactiveFlag && (isSubcommand || hasPrompt)) {
|
|
3358
|
+
if (context.services && isSubcommand && !isBuiltinFlag) {
|
|
3359
|
+
const local = context.services;
|
|
3360
|
+
context.services = (opts.remoteServices ? await connectRemoteServices(local, opts.remoteServices) : void 0) ?? refuseServiceStarts(local, opts.serveCommand);
|
|
3361
|
+
}
|
|
3362
|
+
if (opts.remoteServices && !isBuiltinFlag) context.serving = opts.remoteServices;
|
|
3231
3363
|
if (hasPrompt) {
|
|
3232
3364
|
const agentProvider = await runtime.getAgent();
|
|
3233
3365
|
if (agentProvider) {
|
|
@@ -3269,6 +3401,12 @@ function defineCli(options) {
|
|
|
3269
3401
|
exit(1);
|
|
3270
3402
|
return;
|
|
3271
3403
|
}
|
|
3404
|
+
const control = processControlChannel();
|
|
3405
|
+
if (control && context.services) serveServices(control, context.services, {
|
|
3406
|
+
name: options.name,
|
|
3407
|
+
version: options.version,
|
|
3408
|
+
workdir
|
|
3409
|
+
});
|
|
3272
3410
|
let session;
|
|
3273
3411
|
if (interactiveFlag) {
|
|
3274
3412
|
if (!options.interactive) {
|
|
@@ -3676,6 +3814,6 @@ async function getProjectMapping(scanResults, folders) {
|
|
|
3676
3814
|
return mappings;
|
|
3677
3815
|
}
|
|
3678
3816
|
//#endregion
|
|
3679
|
-
export {
|
|
3817
|
+
export { restartService as A, userConfigPath as B, installSkills as C, ServiceStartRefused as D, formatStatus as E, configKeyToEnvVar as F, createLogger as H, getMissingRequiredFields as I, localConfigPath as L, createConfigCommand as M, generateConfigCommandHelp as N, connectRemoteServices as O, resolveWorkdir as P, resolveConfig as R, readSkills as S, createServiceCommands as T, formatZodError as U, userStoreFolder as V, readPackageInfo as W, createSkillCommands as _, initLifecycle as a, registerDefaultHelpers as b, tokenizeArgs as c, createRoutine as d, IDENTITY_WRAPS as f, DEFAULT_SKILL_INSTRUCTION as g, createEventBus as h, defineCli as i, serveServices as j, refuseServiceStarts as k, createCompositeServiceManager as l, createProcessManager as m, createTypes as n, createReplSession as o, composeWraps as p, defineTrigger as r, parseReplInput as s, getProjectMapping as t, createRoutineServiceAdapter as u, isSkillInvocation as v, createHelpCommand as w, renderSkillTemplate as x, extractTemplateVars as y, toUpperSnake as z };
|
|
3680
3818
|
|
|
3681
|
-
//# sourceMappingURL=src-
|
|
3819
|
+
//# sourceMappingURL=src-BmMd0Wl4.mjs.map
|