@agentage/cli 0.51.1 → 0.52.1
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/README.md +26 -15
- package/dist/commands/auth/setup.d.ts +2 -0
- package/dist/commands/auth/setup.js +4 -1
- package/dist/commands/daemon/daemon.d.ts +38 -0
- package/dist/commands/daemon/daemon.js +75 -0
- package/dist/commands/daemon/installer.d.ts +11 -0
- package/dist/commands/daemon/installer.js +87 -0
- package/dist/commands/daemon/move-plan.d.ts +17 -0
- package/dist/commands/daemon/move-plan.js +67 -0
- package/dist/commands/daemon/move-settings.d.ts +5 -0
- package/dist/commands/daemon/move-settings.js +50 -0
- package/dist/commands/daemon/move.d.ts +17 -0
- package/dist/commands/daemon/move.js +106 -0
- package/dist/commands/daemon/run.d.ts +6 -0
- package/dist/commands/daemon/run.js +36 -0
- package/dist/commands/extension/build.d.ts +13 -4
- package/dist/commands/extension/build.js +67 -25
- package/dist/commands/extension/configure.d.ts +26 -0
- package/dist/commands/extension/configure.js +100 -0
- package/dist/commands/extension/create-install.d.ts +26 -0
- package/dist/commands/extension/create-install.js +81 -0
- package/dist/commands/extension/create.d.ts +6 -1
- package/dist/commands/extension/create.js +33 -12
- package/dist/commands/extension/delegate.js +3 -1
- package/dist/commands/extension/doctor.d.ts +1 -1
- package/dist/commands/extension/doctor.js +8 -7
- package/dist/commands/extension/install-once.d.ts +17 -0
- package/dist/commands/extension/install-once.js +57 -0
- package/dist/commands/extension/kit-tool.d.ts +1 -1
- package/dist/commands/extension/lifecycle-commands.d.ts +19 -0
- package/dist/commands/extension/lifecycle-commands.js +80 -0
- package/dist/commands/extension/lifecycle-list.d.ts +26 -0
- package/dist/commands/extension/lifecycle-list.js +92 -0
- package/dist/commands/extension/lifecycle.d.ts +41 -0
- package/dist/commands/extension/lifecycle.js +142 -0
- package/dist/commands/extension/line.d.ts +1 -0
- package/dist/commands/extension/line.js +2 -0
- package/dist/commands/extension/list.d.ts +4 -14
- package/dist/commands/extension/list.js +11 -96
- package/dist/commands/extension/package-build.d.ts +15 -0
- package/dist/commands/extension/package-build.js +51 -0
- package/dist/commands/extension/plan.d.ts +4 -2
- package/dist/commands/extension/plan.js +37 -37
- package/dist/commands/extension/profile-plan.d.ts +16 -0
- package/dist/commands/extension/profile-plan.js +48 -0
- package/dist/commands/extension/release.d.ts +2 -1
- package/dist/commands/extension/release.js +10 -9
- package/dist/commands/extension/row-lines.d.ts +1 -2
- package/dist/commands/extension/row-lines.js +5 -6
- package/dist/commands/extension/rows.js +6 -4
- package/dist/commands/extension/setup-cloud.d.ts +2 -2
- package/dist/commands/extension/setup-cloud.js +6 -5
- package/dist/commands/extension/setup-install.d.ts +8 -0
- package/dist/commands/extension/setup-install.js +56 -0
- package/dist/commands/extension/setup.d.ts +4 -1
- package/dist/commands/extension/setup.js +56 -18
- package/dist/commands/extension/sync.d.ts +6 -11
- package/dist/commands/extension/sync.js +8 -50
- package/dist/commands/extension/up-commands.d.ts +9 -0
- package/dist/commands/extension/up-commands.js +74 -0
- package/dist/commands/extension/up-local.d.ts +21 -0
- package/dist/commands/extension/up-local.js +94 -0
- package/dist/commands/extension/up-run.d.ts +23 -0
- package/dist/commands/extension/up-run.js +75 -0
- package/dist/commands/extension/up.d.ts +10 -10
- package/dist/commands/extension/up.js +62 -74
- package/dist/commands/update/update.d.ts +9 -1
- package/dist/commands/update/update.js +20 -6
- package/dist/core/installer/compare-lock.d.ts +0 -2
- package/dist/core/installer/compare-lock.js +0 -12
- package/dist/core/installer/compat.js +5 -3
- package/dist/core/installer/npm.d.ts +1 -0
- package/dist/core/installer/npm.js +6 -0
- package/dist/core/installer/old-tree.d.ts +3 -0
- package/dist/core/installer/old-tree.js +45 -0
- package/dist/core/installer/package-dir.d.ts +11 -0
- package/dist/core/installer/package-dir.js +53 -0
- package/dist/core/installer/resolve.js +1 -2
- package/dist/core/installer/tree.d.ts +0 -5
- package/dist/core/installer/tree.js +1 -25
- package/dist/core/kit-id.d.ts +1 -0
- package/dist/core/kit-id.js +20 -0
- package/dist/core/loader/discover.d.ts +7 -1
- package/dist/core/loader/discover.js +5 -4
- package/dist/core/loader/installed.d.ts +9 -0
- package/dist/core/loader/installed.js +51 -0
- package/dist/core/loader/manifest.d.ts +4 -0
- package/dist/core/loader/manifest.js +25 -3
- package/dist/core/loader/scan.d.ts +2 -1
- package/dist/core/loader/scan.js +17 -4
- package/dist/core/loader/secrets.d.ts +0 -7
- package/dist/core/loader/secrets.js +0 -11
- package/dist/core/loader/verbs.d.ts +0 -1
- package/dist/core/loader/verbs.js +1 -13
- package/dist/core/profile/machine.d.ts +0 -1
- package/dist/core/profile/machine.js +0 -5
- package/dist/lib/auth/introspect.js +1 -2
- package/dist/lib/auth/sign-in.js +3 -0
- package/dist/lib/child-env.d.ts +1 -0
- package/dist/lib/child-env.js +6 -0
- package/dist/lib/daemon/daemon-answer.d.ts +8 -0
- package/dist/lib/daemon/daemon-answer.js +38 -0
- package/dist/lib/daemon/daemon-client.d.ts +19 -0
- package/dist/lib/daemon/daemon-client.js +125 -0
- package/dist/lib/daemon/daemon-file.d.ts +16 -0
- package/dist/lib/daemon/daemon-file.js +30 -0
- package/dist/lib/daemon/daemon-health.d.ts +16 -0
- package/dist/lib/daemon/daemon-health.js +36 -0
- package/dist/lib/daemon/daemon-probe.d.ts +34 -0
- package/dist/lib/daemon/daemon-probe.js +50 -0
- package/dist/lib/daemon/daemon-stop.d.ts +13 -0
- package/dist/lib/daemon/daemon-stop.js +42 -0
- package/dist/lib/daemon/host-client.d.ts +40 -0
- package/dist/lib/daemon/host-client.js +45 -0
- package/dist/lib/exit-code.d.ts +2 -0
- package/dist/lib/exit-code.js +8 -0
- package/dist/lib/fs/json-file.d.ts +1 -0
- package/dist/lib/fs/json-file.js +1 -1
- package/dist/lib/json-output.d.ts +36 -0
- package/dist/lib/json-output.js +163 -0
- package/dist/lib/net/market-error.d.ts +12 -0
- package/dist/lib/net/market-error.js +28 -0
- package/dist/lib/net/market.d.ts +17 -2
- package/dist/lib/net/market.js +92 -18
- package/dist/lib/wait.d.ts +6 -0
- package/dist/lib/wait.js +17 -0
- package/dist/lib/with-action.d.ts +12 -0
- package/dist/lib/with-action.js +33 -0
- package/dist/program-deps.d.ts +2 -0
- package/dist/program-deps.js +95 -0
- package/dist/program.d.ts +29 -21
- package/dist/program.js +41 -75
- package/package.json +8 -6
- package/CHANGELOG.md +0 -68
- package/dist/commands/extension/install-load.d.ts +0 -7
- package/dist/commands/extension/install-load.js +0 -42
- package/dist/commands/extension/install.d.ts +0 -12
- package/dist/commands/extension/install.js +0 -49
- package/dist/commands/extension/latest.d.ts +0 -2
- package/dist/commands/extension/latest.js +0 -71
- package/dist/commands/extension/remove.d.ts +0 -15
- package/dist/commands/extension/remove.js +0 -86
- package/dist/commands/extension/secrets-prompt.d.ts +0 -6
- package/dist/commands/extension/secrets-prompt.js +0 -47
- package/dist/core/installer/installer.d.ts +0 -28
- package/dist/core/installer/installer.js +0 -87
- package/dist/core/installer/latest.d.ts +0 -27
- package/dist/core/installer/latest.js +0 -90
- package/dist/core/profile/edit.d.ts +0 -4
- package/dist/core/profile/edit.js +0 -27
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Npm } from '../../core/installer/npm.js';
|
|
2
|
-
import { type Account
|
|
2
|
+
import { type Account } from '../../core/profile/cloud.js';
|
|
3
3
|
import { type MachineFile } from '../../core/profile/machine.js';
|
|
4
|
-
import { type Apply
|
|
4
|
+
import { type Apply } from '../../core/profile/push.js';
|
|
5
5
|
import { type Profile } from '../../core/profile/schema.js';
|
|
6
6
|
import { type AccountOf } from './account.js';
|
|
7
7
|
export interface EditDeps {
|
|
@@ -9,14 +9,9 @@ export interface EditDeps {
|
|
|
9
9
|
npmMajor: () => Promise<number>;
|
|
10
10
|
account: AccountOf;
|
|
11
11
|
}
|
|
12
|
-
export
|
|
13
|
-
|
|
14
|
-
fetch: CloudFetch;
|
|
15
|
-
base: CloudProfile;
|
|
12
|
+
export interface ProfileDeps extends EditDeps {
|
|
13
|
+
installProfile: (profile: Profile) => Promise<void>;
|
|
16
14
|
}
|
|
15
|
+
export type Change = (profile: Profile) => Profile;
|
|
17
16
|
export declare const checkAccount: (account: Account, machine: MachineFile) => Promise<string | null>;
|
|
18
|
-
export declare const
|
|
19
|
-
export declare const localLock: (deps: EditDeps) => Promise<LockPair>;
|
|
20
|
-
export declare const applying: (change: Change, deps: EditDeps, note?: string | null) => Apply;
|
|
21
|
-
export declare const pushApplied: (synced: Synced, apply: Apply, opts?: ReplayOptions) => Promise<Pushed>;
|
|
22
|
-
export declare const finishEdit: (synced: Synced | null, change: Change, deps: EditDeps, note?: string | null) => Promise<void>;
|
|
17
|
+
export declare const applying: (change: Change, deps: ProfileDeps, note?: string | null) => Apply;
|
|
@@ -3,15 +3,13 @@ import { parseLock } from '../../core/installer/compare-lock.js';
|
|
|
3
3
|
import {} from '../../core/installer/npm.js';
|
|
4
4
|
import { LOCK_FILE, readText } from '../../core/installer/tree.js';
|
|
5
5
|
import { extensionsDir } from '../../core/paths.js';
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { pushBody, pushWithReplay, } from '../../core/profile/push.js';
|
|
6
|
+
import {} from '../../core/profile/cloud.js';
|
|
7
|
+
import { accountMismatch } from '../../core/profile/machine.js';
|
|
8
|
+
import { mutateProfile } from '../../core/profile/profile.js';
|
|
9
|
+
import { pushBody } from '../../core/profile/push.js';
|
|
11
10
|
import {} from '../../core/profile/schema.js';
|
|
12
11
|
import {} from './account.js';
|
|
13
|
-
import {
|
|
14
|
-
const NOT_SIGNED_IN = 'not signed in - run: agentage setup (this machine syncs its profile with your account)';
|
|
12
|
+
import { line } from './line.js';
|
|
15
13
|
const REPLAY = 'changed in your account meanwhile, replaying this change on it';
|
|
16
14
|
// The signed-in account's id, refused when this machine is connected to another account.
|
|
17
15
|
export const checkAccount = async (account, machine) => {
|
|
@@ -21,27 +19,12 @@ export const checkAccount = async (account, machine) => {
|
|
|
21
19
|
throw mismatch;
|
|
22
20
|
return userId;
|
|
23
21
|
};
|
|
24
|
-
// Pulled whole, before anything changes, so the edit applies to the account's newest document.
|
|
25
|
-
export const pullForEdit = async (deps) => {
|
|
26
|
-
const machine = await loadMachine();
|
|
27
|
-
if (machine.profileSource !== 'cloud')
|
|
28
|
-
return null;
|
|
29
|
-
const account = await deps.account();
|
|
30
|
-
if (account === null)
|
|
31
|
-
throw new Error(NOT_SIGNED_IN);
|
|
32
|
-
await checkAccount(account, machine);
|
|
33
|
-
const pulled = await pullProfile(account.fetch, null);
|
|
34
|
-
if (pulled.kind === 'unavailable')
|
|
35
|
-
throw new Error(`cloud profile unavailable (${pulled.reason}); nothing changed`);
|
|
36
|
-
const base = pulled.kind === 'found' ? pulled.doc : localProfile(editableProfile());
|
|
37
|
-
return { fetch: account.fetch, base };
|
|
38
|
-
};
|
|
39
22
|
// extensions/package-lock.json and the npm major that wrote it, pushed with the profile.
|
|
40
|
-
|
|
23
|
+
const localLock = async (deps) => {
|
|
41
24
|
const lock = parseLock(readText(join(extensionsDir(), LOCK_FILE)));
|
|
42
25
|
return { lock, npmMajor: lock === null ? null : await deps.npmMajor() };
|
|
43
26
|
};
|
|
44
|
-
// Nothing is pushed unless the
|
|
27
|
+
// Nothing is pushed unless the install worked: installProfile throws otherwise.
|
|
45
28
|
export const applying = (change, deps, note = null) => {
|
|
46
29
|
let calls = 0;
|
|
47
30
|
return async (base) => {
|
|
@@ -50,32 +33,7 @@ export const applying = (change, deps, note = null) => {
|
|
|
50
33
|
const text = calls++ === 0 ? note : line('profile', REPLAY);
|
|
51
34
|
if (text !== null)
|
|
52
35
|
console.log(text);
|
|
53
|
-
await
|
|
36
|
+
await deps.installProfile(next);
|
|
54
37
|
return pushBody(next, base, await localLock(deps));
|
|
55
38
|
};
|
|
56
39
|
};
|
|
57
|
-
const onThisMachine = async (change, deps, note) => {
|
|
58
|
-
await changeProfile(change);
|
|
59
|
-
if (note !== null)
|
|
60
|
-
console.log(note);
|
|
61
|
-
await installAndLoad(editableProfile(), deps.npm);
|
|
62
|
-
};
|
|
63
|
-
const installedHere = (err) => {
|
|
64
|
-
if (!(err instanceof NotPushedError))
|
|
65
|
-
throw err;
|
|
66
|
-
throw new Error(`${err.message}; installed here only, run the command again to push it`);
|
|
67
|
-
};
|
|
68
|
-
// `apply` installs and loads before it returns the body: a failed load pushes nothing.
|
|
69
|
-
export const pushApplied = async (synced, apply, opts = {}) => {
|
|
70
|
-
const done = await pushWithReplay(synced.fetch, synced.base, apply, opts).catch(installedHere);
|
|
71
|
-
await setCloud(done.etag);
|
|
72
|
-
return done;
|
|
73
|
-
};
|
|
74
|
-
// install, remove and --off end here; the account gets the change only after the tree loaded.
|
|
75
|
-
export const finishEdit = async (synced, change, deps, note = null) => {
|
|
76
|
-
if (synced === null)
|
|
77
|
-
return onThisMachine(change, deps, note);
|
|
78
|
-
const done = await pushApplied(synced, applying(change, deps, note));
|
|
79
|
-
const where = done.pushed ? 'pushed to' : 'already in';
|
|
80
|
-
console.log(line('profile', `${where} your account (etag ${done.etag})`));
|
|
81
|
-
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Command } from 'commander';
|
|
2
|
+
import { type ExtensionDownOptions, type ExtensionTokenOptions, type ExtensionUpFlags } from './up.js';
|
|
3
|
+
export declare const issuerUrl: (value: string) => string;
|
|
4
|
+
export interface KitActions {
|
|
5
|
+
up: (opts: ExtensionUpFlags) => Promise<void>;
|
|
6
|
+
down: (opts: ExtensionDownOptions) => Promise<void>;
|
|
7
|
+
token: (opts: ExtensionTokenOptions) => Promise<void>;
|
|
8
|
+
}
|
|
9
|
+
export declare const addKitTools: (group: Command, actions: KitActions) => void;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { InvalidArgumentError } from 'commander';
|
|
2
|
+
import { addJson } from '../../lib/json-output.js';
|
|
3
|
+
import {} from './up.js';
|
|
4
|
+
// A usage error (exit 2) before the kit is asked: the issuer is an http: or https: URL.
|
|
5
|
+
export const issuerUrl = (value) => {
|
|
6
|
+
let protocol;
|
|
7
|
+
try {
|
|
8
|
+
protocol = new URL(value).protocol;
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
throw new InvalidArgumentError('not a URL.');
|
|
12
|
+
}
|
|
13
|
+
if (protocol === 'http:' || protocol === 'https:')
|
|
14
|
+
return value;
|
|
15
|
+
throw new InvalidArgumentError('not an http: or https: URL.');
|
|
16
|
+
};
|
|
17
|
+
const UP_HELP = `
|
|
18
|
+
With no Docker (the default): builds the package in --dir (default: the current folder) as
|
|
19
|
+
extension build does, prints what check refuses without stopping, and runs it in this terminal
|
|
20
|
+
with the kit host on the first free port from 4244 (127.0.0.1). It prints the URL and runs until
|
|
21
|
+
Ctrl-C. It does not use the daemon and does not change installation.json; its store is in
|
|
22
|
+
.agentage/data of the folder.
|
|
23
|
+
|
|
24
|
+
--docker runs the package the way a deployment runs it: the part images built with the release
|
|
25
|
+
recipe, the store with its migrations and seeds, a dev token issuer and a local gateway. Each
|
|
26
|
+
working copy is its own compose project; up refuses while another one holds localhost:4280 or
|
|
27
|
+
4281 and names it. Prints one line per step as the kit reports it. Exits 1 when a step fails.`;
|
|
28
|
+
const DOWN_HELP = `
|
|
29
|
+
Stops what agentage extension up started for the package in --dir, and nothing else: the local
|
|
30
|
+
run of this folder (only when its port still answers as it), and the Docker stack up --docker
|
|
31
|
+
started there (its containers, network and the store's volume; --keep-data keeps the volume).
|
|
32
|
+
It says so when nothing runs. Exits 1 when that fails.`;
|
|
33
|
+
const TOKEN_HELP = `
|
|
34
|
+
Prints a token from the dev issuer agentage extension up started for the package in --dir,
|
|
35
|
+
alone on stdout, for calling /api/v1/ and /mcp by hand or from tests. Only up trusts it. Exits 1
|
|
36
|
+
when the stack on localhost:4281 is not this package's (it names the one that is), or when the
|
|
37
|
+
issuer refuses or does not answer within 5 s. --issuer asks that issuer and skips the check.`;
|
|
38
|
+
const DIR = '--dir <path>';
|
|
39
|
+
const DIR_TEXT = 'the extension package folder (default: the current folder)';
|
|
40
|
+
const addUp = (group, up) => {
|
|
41
|
+
addJson(group
|
|
42
|
+
.command('up')
|
|
43
|
+
.description('Run the working copy in this terminal until Ctrl-C; --docker = on Docker')
|
|
44
|
+
.option(DIR, DIR_TEXT)
|
|
45
|
+
.option('--docker', 'the images, store, issuer and gateway on Docker')
|
|
46
|
+
.option('--local', 'the run with no Docker, asked for on purpose (no notice; for CI)'))
|
|
47
|
+
.addHelpText('after', UP_HELP)
|
|
48
|
+
.action(up);
|
|
49
|
+
};
|
|
50
|
+
const addDown = (group, down) => {
|
|
51
|
+
addJson(group
|
|
52
|
+
.command('down')
|
|
53
|
+
.description('Stop what extension up started here; --keep-data keeps the Docker store')
|
|
54
|
+
.option(DIR, DIR_TEXT)
|
|
55
|
+
.option('--keep-data', "keep the store's volume"))
|
|
56
|
+
.addHelpText('after', DOWN_HELP)
|
|
57
|
+
.action(down);
|
|
58
|
+
};
|
|
59
|
+
const addToken = (group, token) => {
|
|
60
|
+
addJson(group
|
|
61
|
+
.command('token')
|
|
62
|
+
.description('Print a token from the dev issuer extension up started')
|
|
63
|
+
.requiredOption('--scope <scope>', 'the scope the token carries, e.g. probe:write')
|
|
64
|
+
.option(DIR, DIR_TEXT)
|
|
65
|
+
.option('--issuer <url>', 'the dev issuer (default: http://localhost:4281)', issuerUrl))
|
|
66
|
+
.addHelpText('after', TOKEN_HELP)
|
|
67
|
+
.action(token);
|
|
68
|
+
};
|
|
69
|
+
// up, down and token: thin commands over the kit's tool library, for extension authors.
|
|
70
|
+
export const addKitTools = (group, actions) => {
|
|
71
|
+
addUp(group, actions.up);
|
|
72
|
+
addDown(group, actions.down);
|
|
73
|
+
addToken(group, actions.token);
|
|
74
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { type RequestListener, type Server } from 'node:http';
|
|
2
|
+
export declare const UP_FROM = 4244;
|
|
3
|
+
export interface UpRecord {
|
|
4
|
+
pid: number;
|
|
5
|
+
port: number;
|
|
6
|
+
id: string;
|
|
7
|
+
instance: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const recordPath: (dir: string) => string;
|
|
10
|
+
export declare const dataDirOf: (dir: string) => string;
|
|
11
|
+
export declare const readRecord: (dir: string) => UpRecord | null;
|
|
12
|
+
export declare const ignoreLocal: (dir: string) => void;
|
|
13
|
+
export declare const writeRecord: (dir: string, record: UpRecord) => void;
|
|
14
|
+
export declare const removeRecord: (dir: string) => void;
|
|
15
|
+
export declare const listenFrom: (handler: RequestListener, from?: number) => Promise<Server>;
|
|
16
|
+
export declare const portOf: (server: Server) => number;
|
|
17
|
+
export type Answer = 'ours' | 'other' | 'none';
|
|
18
|
+
export declare const answerOf: (record: UpRecord, timeoutMs?: number) => Promise<Answer>;
|
|
19
|
+
export declare const untilSignal: () => Promise<string>;
|
|
20
|
+
export type LocalDown = 'stopped' | 'stale' | 'none';
|
|
21
|
+
export declare const stopLocal: (dir: string, timeoutMs?: number) => Promise<LocalDown>;
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { createServer } from 'node:http';
|
|
2
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync, rmSync } from 'node:fs';
|
|
3
|
+
import { join, resolve } from 'node:path';
|
|
4
|
+
import { isPid } from '../../lib/daemon/daemon-file.js';
|
|
5
|
+
import { isRecord, readJson, writeTextAtomic } from '../../lib/fs/json-file.js';
|
|
6
|
+
import { CliError, messageOf } from '../../lib/json-output.js';
|
|
7
|
+
import { waitFor } from '../../lib/wait.js';
|
|
8
|
+
// D7: a local up takes the first free port from here; the daemon keeps 4243.
|
|
9
|
+
export const UP_FROM = 4244;
|
|
10
|
+
// The kit's own folder in a working copy (`.agentage/`, ignored by the template).
|
|
11
|
+
const LOCAL = '.agentage';
|
|
12
|
+
export const recordPath = (dir) => join(resolve(dir), LOCAL, 'up.json');
|
|
13
|
+
export const dataDirOf = (dir) => join(resolve(dir), LOCAL, 'data');
|
|
14
|
+
const isUpRecord = (value) => isRecord(value) &&
|
|
15
|
+
isPid(value['pid']) &&
|
|
16
|
+
Number.isInteger(value['port']) &&
|
|
17
|
+
value['port'] >= 1024 &&
|
|
18
|
+
value['port'] <= 65535 &&
|
|
19
|
+
typeof value['id'] === 'string' &&
|
|
20
|
+
typeof value['instance'] === 'string';
|
|
21
|
+
export const readRecord = (dir) => readJson(recordPath(dir), isUpRecord);
|
|
22
|
+
// The run state stays out of git: `.agentage/` goes into the folder's .gitignore when missing.
|
|
23
|
+
export const ignoreLocal = (dir) => {
|
|
24
|
+
if (!existsSync(resolve(dir)))
|
|
25
|
+
return;
|
|
26
|
+
const file = join(resolve(dir), '.gitignore');
|
|
27
|
+
const text = existsSync(file) ? readFileSync(file, 'utf8') : '';
|
|
28
|
+
if (text.split('\n').some((line) => /^\/?\.agentage\/?$/.test(line.trim())))
|
|
29
|
+
return;
|
|
30
|
+
appendFileSync(file, `${text === '' || text.endsWith('\n') ? '' : '\n'}${LOCAL}/\n`);
|
|
31
|
+
console.error(`gitignore added ${LOCAL}/ to ${file}`);
|
|
32
|
+
};
|
|
33
|
+
export const writeRecord = (dir, record) => {
|
|
34
|
+
mkdirSync(join(resolve(dir), LOCAL), { recursive: true });
|
|
35
|
+
ignoreLocal(dir);
|
|
36
|
+
writeTextAtomic(recordPath(dir), `${JSON.stringify(record)}\n`);
|
|
37
|
+
};
|
|
38
|
+
export const removeRecord = (dir) => rmSync(recordPath(dir), { force: true });
|
|
39
|
+
const bind = (handler, port) => new Promise((done, fail) => {
|
|
40
|
+
const server = createServer(handler);
|
|
41
|
+
server.once('error', (err) => err.code === 'EADDRINUSE' ? done(null) : fail(err));
|
|
42
|
+
server.listen(port, '127.0.0.1', () => done(server));
|
|
43
|
+
});
|
|
44
|
+
// D7: the first port from 4244 that binds; a port taken meanwhile is skipped, not fatal.
|
|
45
|
+
export const listenFrom = async (handler, from = UP_FROM) => {
|
|
46
|
+
for (let port = from; port < from + 100; port += 1) {
|
|
47
|
+
const server = await bind(handler, port);
|
|
48
|
+
if (server)
|
|
49
|
+
return server;
|
|
50
|
+
}
|
|
51
|
+
throw new CliError('port_in_use', `no free port from ${from} to ${from + 99}`);
|
|
52
|
+
};
|
|
53
|
+
export const portOf = (server) => server.address().port;
|
|
54
|
+
// The record's extension answers on its port when /health names the same id and instance.
|
|
55
|
+
export const answerOf = async (record, timeoutMs = 2_000) => {
|
|
56
|
+
const res = await fetch(`http://127.0.0.1:${record.port}/health`, {
|
|
57
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
58
|
+
}).catch(() => null);
|
|
59
|
+
if (!res)
|
|
60
|
+
return 'none';
|
|
61
|
+
const body = (await res.json().catch(() => null));
|
|
62
|
+
const same = body?.data?.extension === record.id && body.data.instance === record.instance;
|
|
63
|
+
return same ? 'ours' : 'other';
|
|
64
|
+
};
|
|
65
|
+
// Resolves on the first SIGINT or SIGTERM, then releases both.
|
|
66
|
+
export const untilSignal = () => new Promise((done) => {
|
|
67
|
+
const on = (signal) => {
|
|
68
|
+
process.off('SIGINT', on);
|
|
69
|
+
process.off('SIGTERM', on);
|
|
70
|
+
done(signal);
|
|
71
|
+
};
|
|
72
|
+
process.once('SIGINT', on);
|
|
73
|
+
process.once('SIGTERM', on);
|
|
74
|
+
});
|
|
75
|
+
const gone = (record, timeoutMs) => waitFor(async () => (await answerOf(record, 500)) === 'none', timeoutMs);
|
|
76
|
+
// Signals the recorded pid only when its port answers as that extension; else the record is stale.
|
|
77
|
+
export const stopLocal = async (dir, timeoutMs = 10_000) => {
|
|
78
|
+
const record = readRecord(dir);
|
|
79
|
+
if (!record)
|
|
80
|
+
return 'none';
|
|
81
|
+
if ((await answerOf(record)) !== 'ours')
|
|
82
|
+
return (removeRecord(dir), 'stale');
|
|
83
|
+
try {
|
|
84
|
+
process.kill(record.pid, 'SIGTERM');
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
const why = messageOf(err);
|
|
88
|
+
throw new CliError('stop_failed', `cannot stop extension up (pid ${record.pid}): ${why}`);
|
|
89
|
+
}
|
|
90
|
+
if (!(await gone(record, timeoutMs)))
|
|
91
|
+
throw new CliError('stop_failed', `extension up (pid ${record.pid}) did not stop`);
|
|
92
|
+
removeRecord(dir);
|
|
93
|
+
return 'stopped';
|
|
94
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type * as KitHostModule from '@agentage/extension/host';
|
|
2
|
+
import type { HostedExtension, HostOptions } from '@agentage/extension/host';
|
|
3
|
+
import { type PackageBuild } from './package-build.js';
|
|
4
|
+
export interface KitHost {
|
|
5
|
+
host: (dir: string, opts: HostOptions) => Promise<HostedExtension>;
|
|
6
|
+
}
|
|
7
|
+
type KitModule = Pick<typeof KitHostModule, 'hostExtension'>;
|
|
8
|
+
export declare const loadKitHost: (load?: () => Promise<KitModule>) => Promise<KitHost>;
|
|
9
|
+
export interface LocalUpDeps {
|
|
10
|
+
build: (dir: string) => Promise<PackageBuild>;
|
|
11
|
+
kit: () => Promise<KitHost>;
|
|
12
|
+
stopped: () => Promise<string>;
|
|
13
|
+
}
|
|
14
|
+
export declare const localUpDeps: LocalUpDeps;
|
|
15
|
+
export interface LocalUp {
|
|
16
|
+
id: string;
|
|
17
|
+
version: string;
|
|
18
|
+
url: string;
|
|
19
|
+
port: number;
|
|
20
|
+
stoppedBy: string;
|
|
21
|
+
}
|
|
22
|
+
export declare const runLocalUp: (folder?: string, deps?: LocalUpDeps, json?: boolean) => Promise<LocalUp>;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import {} from 'node:http';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { setExit } from '../../lib/exit-code.js';
|
|
4
|
+
import { CliError } from '../../lib/json-output.js';
|
|
5
|
+
import { loadKitTool } from './kit-tool.js';
|
|
6
|
+
import { buildPackage } from './package-build.js';
|
|
7
|
+
import { answerOf, dataDirOf, listenFrom, portOf, readRecord, removeRecord, untilSignal, writeRecord, } from './up-local.js';
|
|
8
|
+
export const loadKitHost = async (load = () => import('@agentage/extension/host')) => {
|
|
9
|
+
const kit = await load();
|
|
10
|
+
return { host: (dir, opts) => kit.hostExtension(dir, { ...opts, localOnly: true }) };
|
|
11
|
+
};
|
|
12
|
+
export const localUpDeps = {
|
|
13
|
+
build: (dir) => buildPackage(dir),
|
|
14
|
+
kit: () => loadKitHost(),
|
|
15
|
+
stopped: untilSignal,
|
|
16
|
+
};
|
|
17
|
+
const refuseRunning = async (dir) => {
|
|
18
|
+
const record = readRecord(dir);
|
|
19
|
+
if (!record)
|
|
20
|
+
return;
|
|
21
|
+
if ((await answerOf(record)) !== 'ours')
|
|
22
|
+
return removeRecord(dir);
|
|
23
|
+
const at = `http://127.0.0.1:${record.port}`;
|
|
24
|
+
throw new CliError('already_running', `extension up runs for ${dir} on ${at}: extension down`);
|
|
25
|
+
};
|
|
26
|
+
// Problems do not stop a local run; they are printed, as `extension build` would refuse them.
|
|
27
|
+
const warnProblems = async ({ report }) => {
|
|
28
|
+
if (report.conforms)
|
|
29
|
+
return;
|
|
30
|
+
for (const text of (await loadKitTool()).checkLines(report))
|
|
31
|
+
console.error(`check ${text}`);
|
|
32
|
+
};
|
|
33
|
+
const serve = (hosted) => listenFrom(hosted.app);
|
|
34
|
+
const close = async (server, hosted, dir) => {
|
|
35
|
+
server?.closeAllConnections();
|
|
36
|
+
await Promise.all([server && new Promise((done) => server.close(done)), hosted.stop()]);
|
|
37
|
+
removeRecord(dir);
|
|
38
|
+
};
|
|
39
|
+
// Ready: the line, and with --json one JSON line on stdout before the envelope at exit.
|
|
40
|
+
const ready = (hosted, url, port, json) => {
|
|
41
|
+
console.log(`up ${hosted.id} ${hosted.version} on ${url} (Ctrl-C stops it)`);
|
|
42
|
+
const line = { ready: true, id: hosted.id, version: hosted.version, url, port };
|
|
43
|
+
if (json)
|
|
44
|
+
process.stdout.write(`${JSON.stringify(line)}\n`);
|
|
45
|
+
};
|
|
46
|
+
const runHosted = async (dir, hosted, deps, json) => {
|
|
47
|
+
let server = null;
|
|
48
|
+
try {
|
|
49
|
+
server = await serve(hosted);
|
|
50
|
+
const port = portOf(server);
|
|
51
|
+
const { instance } = (await hosted.health()).data;
|
|
52
|
+
writeRecord(dir, { pid: process.pid, port, id: hosted.id, instance });
|
|
53
|
+
const url = `http://127.0.0.1:${port}/api/${hosted.id}/`;
|
|
54
|
+
ready(hosted, url, port, json);
|
|
55
|
+
const stoppedBy = await deps.stopped();
|
|
56
|
+
return { id: hosted.id, version: hosted.version, url, port, stoppedBy };
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
await close(server, hosted, dir);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
// D7: the working copy in this terminal, on the next free port from 4244, until Ctrl-C (exit 130).
|
|
63
|
+
export const runLocalUp = async (folder = '.', deps = localUpDeps, json = false) => {
|
|
64
|
+
const dir = resolve(folder);
|
|
65
|
+
await refuseRunning(dir);
|
|
66
|
+
await warnProblems(await deps.build(dir));
|
|
67
|
+
delete process.env['AGENTAGE_TOKEN'];
|
|
68
|
+
const kit = await deps.kit();
|
|
69
|
+
const hosted = await kit.host(dir, { dataDir: dataDirOf(dir) });
|
|
70
|
+
const done = await runHosted(dir, hosted, deps, json);
|
|
71
|
+
console.log(`stopped ${done.id} (${done.stoppedBy})`);
|
|
72
|
+
if (done.stoppedBy === 'SIGINT')
|
|
73
|
+
setExit(130);
|
|
74
|
+
return done;
|
|
75
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { type Command } from 'commander';
|
|
2
1
|
import { type LoadKitTool } from './kit-tool.js';
|
|
3
2
|
import { type Exec } from './release-guard.js';
|
|
4
3
|
export interface ExtensionUpOptions {
|
|
5
4
|
dir?: string;
|
|
5
|
+
json?: boolean;
|
|
6
6
|
}
|
|
7
7
|
export interface ExtensionDownOptions extends ExtensionUpOptions {
|
|
8
8
|
keepData?: boolean;
|
|
@@ -11,13 +11,13 @@ export interface ExtensionTokenOptions extends ExtensionUpOptions {
|
|
|
11
11
|
scope: string;
|
|
12
12
|
issuer?: string;
|
|
13
13
|
}
|
|
14
|
-
export
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
export declare const issuerUrl: (value: string) => string;
|
|
18
|
-
export interface KitActions {
|
|
19
|
-
up: (opts: ExtensionUpOptions) => Promise<void>;
|
|
20
|
-
down: (opts: ExtensionDownOptions) => Promise<void>;
|
|
21
|
-
token: (opts: ExtensionTokenOptions) => Promise<void>;
|
|
14
|
+
export interface ExtensionUpFlags extends ExtensionUpOptions {
|
|
15
|
+
docker?: boolean;
|
|
16
|
+
local?: boolean;
|
|
22
17
|
}
|
|
23
|
-
export declare const
|
|
18
|
+
export declare const UP_CHANGED = "extension up now runs with no Docker; the Docker stack is `extension up --docker`";
|
|
19
|
+
type LocalRun = (dir: string | undefined, json: boolean) => Promise<unknown>;
|
|
20
|
+
export declare const runExtensionUp: ({ docker, local: asked, ...opts }?: ExtensionUpFlags, tool?: LoadKitTool, local?: LocalRun, env?: NodeJS.ProcessEnv, tty?: boolean) => Promise<unknown>;
|
|
21
|
+
export declare const runExtensionDown: (opts?: ExtensionDownOptions, tool?: LoadKitTool) => Promise<unknown>;
|
|
22
|
+
export declare const runExtensionToken: ({ dir, json, ...opts }: ExtensionTokenOptions, tool?: LoadKitTool, exec?: Exec) => Promise<unknown>;
|
|
23
|
+
export {};
|
|
@@ -1,86 +1,74 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { join, resolve } from 'node:path';
|
|
3
|
+
import { CliError, omitJson } from '../../lib/json-output.js';
|
|
2
4
|
import { loadKitTool } from './kit-tool.js';
|
|
3
5
|
import { execIn } from './release-guard.js';
|
|
4
6
|
import { assertOwnStack } from './stack-owner.js';
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
import { stopLocal } from './up-local.js';
|
|
8
|
+
import { runLocalUp } from './up-run.js';
|
|
9
|
+
// Prints each line as the kit reports it and keeps them for the --json answer.
|
|
10
|
+
const printed = () => {
|
|
11
|
+
const lines = [];
|
|
12
|
+
return { lines, onLine: (line) => void (console.log(line), lines.push(line)) };
|
|
13
|
+
};
|
|
14
|
+
export const UP_CHANGED = 'extension up now runs with no Docker; the Docker stack is `extension up --docker`';
|
|
15
|
+
// CI=false, 0 or empty is not CI.
|
|
16
|
+
const inCi = (env) => !['', 'false', '0', undefined].includes(env['CI']);
|
|
17
|
+
// 0.51's up was Docker: a CI script with neither flag asked for that, so it stops instead.
|
|
18
|
+
const noticeChange = ({ local }, env, tty) => {
|
|
19
|
+
if (local)
|
|
20
|
+
return;
|
|
21
|
+
if (inCi(env) && !tty)
|
|
22
|
+
throw new CliError('invalid_argument', `${UP_CHANGED}; or --local`);
|
|
23
|
+
console.error(UP_CHANGED);
|
|
24
|
+
};
|
|
25
|
+
// No Docker: the working copy in this terminal (D7). --docker: the kit's path, a line per step.
|
|
26
|
+
export const runExtensionUp = async ({ docker, local: asked, ...opts } = {}, tool = loadKitTool, local = (dir, json) => runLocalUp(dir, undefined, json), env = process.env, tty = process.stdin.isTTY === true) => {
|
|
27
|
+
if (docker && asked)
|
|
28
|
+
throw new CliError('invalid_argument', '--docker and --local: pick one');
|
|
29
|
+
if (!docker) {
|
|
30
|
+
noticeChange({ local: asked }, env, tty);
|
|
31
|
+
return local(opts.dir, opts.json === true);
|
|
32
|
+
}
|
|
8
33
|
const { up } = await tool();
|
|
9
|
-
|
|
34
|
+
const { lines, onLine } = printed();
|
|
35
|
+
await up({ ...omitJson(opts), onLine });
|
|
36
|
+
return { lines };
|
|
10
37
|
};
|
|
11
|
-
|
|
38
|
+
const dockerDown = async (opts, tool) => {
|
|
12
39
|
const { down } = await tool();
|
|
13
|
-
|
|
40
|
+
const { lines, onLine } = printed();
|
|
41
|
+
await down({ ...omitJson(opts), onLine });
|
|
42
|
+
return lines;
|
|
43
|
+
};
|
|
44
|
+
const LOCAL_LINES = {
|
|
45
|
+
stopped: 'stopped extension up in this folder',
|
|
46
|
+
stale: 'removed a stale extension up record (its port no longer answers as it)',
|
|
47
|
+
none: '',
|
|
48
|
+
};
|
|
49
|
+
// A local up of the folder stops; a Docker stack up --docker started there goes too.
|
|
50
|
+
export const runExtensionDown = async (opts = {}, tool = loadKitTool) => {
|
|
51
|
+
const dir = opts.dir ?? '.';
|
|
52
|
+
const local = await stopLocal(dir);
|
|
53
|
+
const lines = local === 'none' ? [] : [LOCAL_LINES[local]];
|
|
54
|
+
for (const line of lines)
|
|
55
|
+
console.log(line);
|
|
56
|
+
if (existsSync(join(dir, '.agentage', 'compose.json')))
|
|
57
|
+
lines.push(...(await dockerDown(opts, tool)));
|
|
58
|
+
if (lines.length === 0) {
|
|
59
|
+
lines.push(`nothing runs for ${resolve(dir)}`);
|
|
60
|
+
console.log(lines[0]);
|
|
61
|
+
}
|
|
62
|
+
return { local, lines };
|
|
14
63
|
};
|
|
15
64
|
// The token alone on stdout, so a script can take it as it is.
|
|
16
|
-
export const runExtensionToken = async ({ dir = '.', ...opts }, tool = loadKitTool, exec = execIn('.', 10)) => {
|
|
65
|
+
export const runExtensionToken = async ({ dir = '.', json, ...opts }, tool = loadKitTool, exec = execIn('.', 10)) => {
|
|
17
66
|
if (!opts.issuer)
|
|
18
67
|
await assertOwnStack(dir, exec, tool);
|
|
19
68
|
const { token } = await tool();
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
try {
|
|
26
|
-
protocol = new URL(value).protocol;
|
|
27
|
-
}
|
|
28
|
-
catch {
|
|
29
|
-
throw new InvalidArgumentError('not a URL.');
|
|
30
|
-
}
|
|
31
|
-
if (protocol === 'http:' || protocol === 'https:')
|
|
32
|
-
return value;
|
|
33
|
-
throw new InvalidArgumentError('not an http: or https: URL.');
|
|
34
|
-
};
|
|
35
|
-
const UP_HELP = `
|
|
36
|
-
Runs the extension package in --dir (default: the current folder) on this machine with Docker
|
|
37
|
-
only, the way a deployment runs it: the part images built with the release recipe, the store
|
|
38
|
-
with its migrations and seeds, a dev token issuer and a local gateway. Each working copy is its
|
|
39
|
-
own compose project; up refuses while another one holds localhost:4280 or 4281 and names it, and
|
|
40
|
-
on a kept store says what was already applied. Prints one line per step as the kit reports it,
|
|
41
|
-
the last one once every part is healthy. Needs docker; every docker call has a timeout. Exits 1
|
|
42
|
-
when a step fails, with the kit's message.`;
|
|
43
|
-
const DOWN_HELP = `
|
|
44
|
-
Removes what agentage extension up started for the package in --dir, and nothing else: its
|
|
45
|
-
containers, network and the store's volume; --keep-data keeps the volume. The line it prints
|
|
46
|
-
names the compose project it removed. Exits 1 when that fails.`;
|
|
47
|
-
const TOKEN_HELP = `
|
|
48
|
-
Prints a token from the dev issuer agentage extension up started for the package in --dir,
|
|
49
|
-
alone on stdout, for calling /api/v1/ and /mcp by hand or from tests. Only up trusts it. Exits 1
|
|
50
|
-
when the stack on localhost:4281 is not this package's (it names the one that is), or when the
|
|
51
|
-
issuer refuses or does not answer within 5 s. --issuer asks that issuer and skips the check.`;
|
|
52
|
-
const DIR = '--dir <path>';
|
|
53
|
-
const DIR_TEXT = 'the extension package folder (default: the current folder)';
|
|
54
|
-
const addUp = (group, up) => {
|
|
55
|
-
group
|
|
56
|
-
.command('up')
|
|
57
|
-
.description('Run the extension package on this machine: images, store, issuer, gateway')
|
|
58
|
-
.option(DIR, DIR_TEXT)
|
|
59
|
-
.addHelpText('after', UP_HELP)
|
|
60
|
-
.action(up);
|
|
61
|
-
};
|
|
62
|
-
const addDown = (group, down) => {
|
|
63
|
-
group
|
|
64
|
-
.command('down')
|
|
65
|
-
.description('Remove what extension up started; --keep-data keeps the store')
|
|
66
|
-
.option(DIR, DIR_TEXT)
|
|
67
|
-
.option('--keep-data', "keep the store's volume")
|
|
68
|
-
.addHelpText('after', DOWN_HELP)
|
|
69
|
-
.action(down);
|
|
70
|
-
};
|
|
71
|
-
const addToken = (group, token) => {
|
|
72
|
-
group
|
|
73
|
-
.command('token')
|
|
74
|
-
.description('Print a token from the dev issuer extension up started')
|
|
75
|
-
.requiredOption('--scope <scope>', 'the scope the token carries, e.g. probe:write')
|
|
76
|
-
.option(DIR, DIR_TEXT)
|
|
77
|
-
.option('--issuer <url>', 'the dev issuer (default: http://localhost:4281)', issuerUrl)
|
|
78
|
-
.addHelpText('after', TOKEN_HELP)
|
|
79
|
-
.action(token);
|
|
80
|
-
};
|
|
81
|
-
// up, down and token: thin commands over the kit's tool library, for extension authors.
|
|
82
|
-
export const addKitTools = (group, actions) => {
|
|
83
|
-
addUp(group, actions.up);
|
|
84
|
-
addDown(group, actions.down);
|
|
85
|
-
addToken(group, actions.token);
|
|
69
|
+
const value = await token(opts);
|
|
70
|
+
if (json)
|
|
71
|
+
return { token: value };
|
|
72
|
+
console.log(value);
|
|
73
|
+
return undefined;
|
|
86
74
|
};
|
|
@@ -7,10 +7,18 @@ export interface UpdateDeps {
|
|
|
7
7
|
install: () => Promise<void>;
|
|
8
8
|
acquireLock: () => boolean;
|
|
9
9
|
releaseLock: () => void;
|
|
10
|
+
restartDaemon: () => Promise<unknown>;
|
|
10
11
|
log: (msg: string) => void;
|
|
11
12
|
}
|
|
12
13
|
export interface UpdateOptions {
|
|
13
14
|
check?: boolean;
|
|
15
|
+
json?: boolean;
|
|
14
16
|
}
|
|
15
|
-
export
|
|
17
|
+
export interface UpdateResult {
|
|
18
|
+
installed: string;
|
|
19
|
+
status: UpdateInfo['status']['kind'];
|
|
20
|
+
latest: string | null;
|
|
21
|
+
updated: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare const runUpdate: (opts: UpdateOptions, deps?: UpdateDeps) => Promise<UpdateResult>;
|
|
16
24
|
export declare const registerUpdate: (program: Command, action: (opts: UpdateOptions) => Promise<void>) => void;
|