@agentage/extension 0.14.0 → 0.15.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 +3 -1
- package/dist/contracts/index.d.ts +1 -1
- package/dist/contracts/index.js +1 -1
- package/dist/discovery/discovery.d.ts +10 -25
- package/dist/discovery/discovery.js +7 -1
- package/dist/discovery/discovery.schema.d.ts +45 -0
- package/dist/discovery/discovery.schema.js +33 -4
- package/dist/discovery/served-extras.d.ts +8 -0
- package/dist/discovery/served-extras.js +10 -0
- package/dist/health/health.d.ts +2 -0
- package/dist/health/health.js +4 -2
- package/dist/health/health.schema.d.ts +9 -0
- package/dist/health/health.schema.js +3 -0
- package/dist/host/clock-rate.d.ts +1 -0
- package/dist/host/clock-rate.js +12 -0
- package/dist/host/daemon/add.js +29 -4
- package/dist/host/daemon/child-files.d.ts +20 -0
- package/dist/host/daemon/child-files.js +45 -0
- package/dist/host/daemon/child.d.ts +10 -4
- package/dist/host/daemon/child.js +68 -43
- package/dist/host/daemon/daemon-files.d.ts +3 -0
- package/dist/host/daemon/daemon-files.js +5 -1
- package/dist/host/daemon/daemon.d.ts +1 -0
- package/dist/host/daemon/daemon.js +21 -6
- package/dist/host/daemon/gate.d.ts +3 -0
- package/dist/host/daemon/gate.js +19 -0
- package/dist/host/daemon/guard.d.ts +2 -1
- package/dist/host/daemon/guard.js +16 -4
- package/dist/host/daemon/health-routes.d.ts +17 -0
- package/dist/host/daemon/health-routes.js +61 -0
- package/dist/host/daemon/host-token.d.ts +6 -0
- package/dist/host/daemon/host-token.js +28 -0
- package/dist/host/daemon/last-words.d.ts +2 -0
- package/dist/host/daemon/last-words.js +28 -0
- package/dist/host/daemon/limits.d.ts +4 -0
- package/dist/host/daemon/limits.js +7 -0
- package/dist/host/daemon/ops.js +4 -1
- package/dist/host/daemon/pending.d.ts +1 -1
- package/dist/host/daemon/proxy.d.ts +1 -1
- package/dist/host/daemon/proxy.js +16 -8
- package/dist/host/daemon/redact-stream.d.ts +4 -0
- package/dist/host/daemon/redact-stream.js +24 -0
- package/dist/host/daemon/reload-marker.d.ts +8 -0
- package/dist/host/daemon/reload-marker.js +56 -0
- package/dist/host/daemon/reload-route.d.ts +4 -0
- package/dist/host/daemon/reload-route.js +21 -0
- package/dist/host/daemon/reload.d.ts +10 -0
- package/dist/host/daemon/reload.js +73 -0
- package/dist/host/daemon/restart.d.ts +14 -0
- package/dist/host/daemon/restart.js +59 -0
- package/dist/host/daemon/routes.d.ts +2 -10
- package/dist/host/daemon/routes.js +13 -31
- package/dist/host/daemon/run-lock.d.ts +1 -0
- package/dist/host/daemon/run-lock.js +15 -0
- package/dist/host/daemon/seal-reach.d.ts +2 -0
- package/dist/host/daemon/seal-reach.js +38 -0
- package/dist/host/daemon/seal.d.ts +16 -0
- package/dist/host/daemon/seal.js +71 -0
- package/dist/host/daemon/start-checks.d.ts +7 -0
- package/dist/host/daemon/start-checks.js +29 -0
- package/dist/host/daemon/supervisor.d.ts +9 -3
- package/dist/host/daemon/supervisor.js +33 -40
- package/dist/host/data-lock.d.ts +2 -1
- package/dist/host/data-lock.js +25 -6
- package/dist/host/host-env.d.ts +1 -0
- package/dist/host/host-env.js +2 -0
- package/dist/host/host-extension.d.ts +6 -0
- package/dist/host/host-extension.js +28 -44
- package/dist/host/host-parts.d.ts +25 -0
- package/dist/host/host-parts.js +82 -0
- package/dist/host/host-store.d.ts +3 -0
- package/dist/host/host-store.js +21 -2
- package/dist/host/main.js +2 -0
- package/dist/host/part-checks.d.ts +2 -0
- package/dist/host/part-checks.js +19 -0
- package/dist/host/serve-host.js +10 -6
- package/dist/manifest/parts.d.ts +3 -0
- package/dist/manifest/parts.js +3 -0
- package/dist/mcp/mcp-app.d.ts +4 -0
- package/dist/mcp/mcp-app.js +11 -11
- package/dist/runtime/part-mounts.d.ts +31 -0
- package/dist/runtime/part-mounts.js +57 -0
- package/dist/runtime/part-runners.d.ts +2 -15
- package/dist/runtime/part-runners.js +13 -58
- package/dist/server/app.d.ts +5 -3
- package/dist/server/app.js +16 -17
- package/dist/tool/build.d.ts +1 -0
- package/dist/tool/build.js +3 -2
- package/dist/tool/check.d.ts +3 -1
- package/dist/tool/check.js +25 -9
- package/dist/tool/container-parts.d.ts +1 -1
- package/dist/tool/container-parts.js +6 -6
- package/dist/tool/format.d.ts +1 -0
- package/dist/tool/format.js +17 -8
- package/dist/tool/kit-version.d.ts +2 -0
- package/dist/tool/kit-version.js +12 -0
- package/dist/tool/record-extras.js +5 -12
- package/dist/worker/clock.d.ts +6 -0
- package/dist/worker/clock.js +21 -0
- package/dist/worker/scheduler.d.ts +1 -0
- package/dist/worker/scheduler.js +6 -2
- package/package.json +1 -1
- package/schemas/extension-discovery.schema.json +61 -34
- package/schemas/extension-record.schema.json +1 -1
- package/schemas/health.schema.json +17 -0
|
@@ -3,19 +3,24 @@ import { type BuildInfo } from '../discovery/discovery.js';
|
|
|
3
3
|
import { type HealthBody } from '../health/health.schema.js';
|
|
4
4
|
import { type Log } from '../log/log.js';
|
|
5
5
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
6
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
7
|
+
import { type Env } from '../runtime/build-info.js';
|
|
6
8
|
import { type PartAppBase } from '../server/app.js';
|
|
7
9
|
export interface HostOptions {
|
|
8
10
|
dataDir: string;
|
|
11
|
+
runDir?: string;
|
|
9
12
|
settings?: Record<string, unknown>;
|
|
10
13
|
secrets?: Record<string, string>;
|
|
11
14
|
onExit?: () => void;
|
|
12
15
|
origin?: string;
|
|
13
16
|
localOnly?: boolean;
|
|
17
|
+
localPorts?: number[];
|
|
14
18
|
storeOpenMs?: number;
|
|
15
19
|
}
|
|
16
20
|
export interface HostedExtension {
|
|
17
21
|
id: string;
|
|
18
22
|
version: string;
|
|
23
|
+
parts: HostPartKind[];
|
|
19
24
|
app: Express;
|
|
20
25
|
stop(): Promise<void>;
|
|
21
26
|
health(): Promise<HealthBody>;
|
|
@@ -25,5 +30,6 @@ export interface HostExtras {
|
|
|
25
30
|
guard?: (pkg: ExtensionPackage, log: Log) => Promise<Partial<PartAppBase>>;
|
|
26
31
|
build?: BuildInfo;
|
|
27
32
|
}
|
|
33
|
+
export declare const hostBuildInfo: (env: Env, dir: string) => Promise<BuildInfo>;
|
|
28
34
|
export declare const hostWith: (dir: string, opts: HostOptions, extras: HostExtras) => Promise<HostedExtension>;
|
|
29
35
|
export declare const hostExtension: (dir: string, opts: HostOptions) => Promise<HostedExtension>;
|
|
@@ -1,22 +1,20 @@
|
|
|
1
1
|
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { stat } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
2
4
|
import {} from 'express';
|
|
3
|
-
import { createContext } from '../context/context.js';
|
|
4
|
-
import { readSecrets, secretsFrom } from '../context/secrets.js';
|
|
5
|
-
import { settingsOf } from '../context/settings.js';
|
|
6
5
|
import {} from '../discovery/discovery.js';
|
|
7
|
-
import { readOpenapi } from '../discovery/openapi.js';
|
|
8
6
|
import { healthBody } from '../health/health.js';
|
|
9
7
|
import {} from '../health/health.schema.js';
|
|
10
8
|
import { createLog } from '../log/log.js';
|
|
11
9
|
import {} from '../manifest/manifest.schema.js';
|
|
12
|
-
import {
|
|
10
|
+
import {} from '../manifest/parts.js';
|
|
13
11
|
import { readManifest } from '../manifest/read-manifest.js';
|
|
14
12
|
import { buildInfoFromEnv } from '../runtime/build-info.js';
|
|
15
|
-
import {
|
|
16
|
-
import { createPartApp } from '../server/app.js';
|
|
13
|
+
import {} from '../server/app.js';
|
|
17
14
|
import { storeCheck } from '../store/store.js';
|
|
18
|
-
import {} from '../
|
|
15
|
+
import { hostClockRate } from '../worker/clock.js';
|
|
19
16
|
import { localOnly } from './daemon/guard.js';
|
|
17
|
+
import { hostPartsOf } from './host-parts.js';
|
|
20
18
|
import { openHostStore, STORE_OPEN_MS } from './host-store.js';
|
|
21
19
|
const once = (fn) => {
|
|
22
20
|
let done = false;
|
|
@@ -27,65 +25,51 @@ const once = (fn) => {
|
|
|
27
25
|
fn();
|
|
28
26
|
};
|
|
29
27
|
};
|
|
30
|
-
const secretsOf = ({ pkg, opts, extras }) => extras.secretsDir
|
|
31
|
-
? readSecrets(pkg, 'api', extras.secretsDir)
|
|
32
|
-
: secretsFrom(pkg, 'api', async (name) => opts.secrets?.[name] ?? '');
|
|
33
|
-
// createContext with the host's values: settings and secrets as given, no platform.
|
|
34
|
-
const contextOf = async (h, checks) => createContext({
|
|
35
|
-
pkg: h.pkg,
|
|
36
|
-
part: 'api',
|
|
37
|
-
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
38
|
-
commit: h.build.commit,
|
|
39
|
-
log: h.log,
|
|
40
|
-
settings: settingsOf(h.pkg, h.opts.settings ?? {}, h.log),
|
|
41
|
-
store: h.store,
|
|
42
|
-
secrets: await secretsOf(h),
|
|
43
|
-
checks,
|
|
44
|
-
onExit: h.onExit,
|
|
45
|
-
});
|
|
46
|
-
// The api part of the package; a package without one serves /health only.
|
|
47
|
-
const appOf = async (h, base) => {
|
|
48
|
-
if (!normalizeParts(h.pkg.agentage.parts).code.api)
|
|
49
|
-
return createPartApp({ ...base, part: 'api', mount: () => undefined });
|
|
50
|
-
const ctx = await contextOf(h, base.checks ?? {});
|
|
51
|
-
const openapi = await readOpenapi(h.dir);
|
|
52
|
-
return (await RUNNERS.api({ dir: h.dir, pkg: h.pkg, env: {}, ctx: () => ctx, base, openapi }))
|
|
53
|
-
.app;
|
|
54
|
-
};
|
|
55
28
|
const baseOf = async (h) => {
|
|
56
29
|
const guard = (await h.extras.guard?.(h.pkg, h.log)) ?? {};
|
|
57
30
|
const checks = { ...(h.store && { store: storeCheck(h.store) }), ...guard.checks };
|
|
58
31
|
const resolved = { instance: randomBytes(4).toString('hex'), startedAt: Date.now() };
|
|
59
|
-
const
|
|
32
|
+
const clockRate = hostClockRate();
|
|
33
|
+
const guards = h.opts.localOnly ? [localOnly(undefined, h.opts.localPorts)] : undefined;
|
|
60
34
|
const { pkg, log, build, onExit } = h;
|
|
61
|
-
return { pkg, log, build, onExit, guards, ...guard, checks, ...resolved };
|
|
35
|
+
return { pkg, log, build, onExit, guards, ...guard, checks, clockRate, ...resolved };
|
|
62
36
|
};
|
|
63
|
-
|
|
37
|
+
// The jobs stop before the store closes: a tick never writes to a closed store.
|
|
38
|
+
const hostedOf = (h, hosted, base) => {
|
|
64
39
|
const { id } = h.pkg.agentage;
|
|
65
40
|
const { version } = h.pkg;
|
|
66
|
-
const {
|
|
67
|
-
const
|
|
41
|
+
const { app, parts } = hosted;
|
|
42
|
+
const { instance = '', startedAt = 0, build, checks, clockRate } = base;
|
|
43
|
+
const at = { instance, startedAt, clockRate };
|
|
44
|
+
const health = () => healthBody({ extension: id, part: 'api', parts, version, build, checks, ...at });
|
|
68
45
|
let stopped;
|
|
69
|
-
const
|
|
70
|
-
|
|
46
|
+
const close = () => (hosted.stop(), h.store ? h.store.close() : Promise.resolve());
|
|
47
|
+
const stop = () => (stopped ??= close());
|
|
48
|
+
return { id, version, parts, app, health, stop };
|
|
71
49
|
};
|
|
50
|
+
// BUILD_TIME of an image, else the installed package's time: the same across restarts.
|
|
51
|
+
export const hostBuildInfo = async (env, dir) => buildInfoFromEnv(env, (await stat(join(dir, 'package.json'))).mtime);
|
|
72
52
|
// hostExtension with the extras of the entry (dist/host/main.js).
|
|
73
53
|
export const hostWith = async (dir, opts, extras) => {
|
|
74
54
|
const pkg = await readManifest(dir);
|
|
75
55
|
const log = createLog({ extension: pkg.agentage.id, part: 'api', version: pkg.version });
|
|
76
56
|
const openMs = opts.storeOpenMs ?? STORE_OPEN_MS;
|
|
77
|
-
const
|
|
57
|
+
const at = { dir, dataDir: opts.dataDir, runDir: opts.runDir, openMs };
|
|
58
|
+
const store = await openHostStore(pkg, at, log);
|
|
78
59
|
const onExit = once(opts.onExit ?? (() => undefined));
|
|
79
|
-
const build = extras.build ??
|
|
60
|
+
const build = extras.build ?? (await hostBuildInfo({}, dir));
|
|
80
61
|
const h = { dir, pkg, log, opts, extras, store, onExit, build };
|
|
81
62
|
try {
|
|
82
63
|
const base = await baseOf(h);
|
|
83
|
-
|
|
64
|
+
const hosted = await hostPartsOf(h, base);
|
|
65
|
+
// After every create and the store open: the migrations ran or a job's first query waits on them.
|
|
66
|
+
hosted.start();
|
|
67
|
+
return hostedOf(h, hosted, base);
|
|
84
68
|
}
|
|
85
69
|
catch (err) {
|
|
86
70
|
await store?.close();
|
|
87
71
|
throw err;
|
|
88
72
|
}
|
|
89
73
|
};
|
|
90
|
-
// One extension in this process:
|
|
74
|
+
// One extension in this process: api at /api/{id}/*, mcp at /mcp/{id}, the worker jobs, one store, no token check.
|
|
91
75
|
export const hostExtension = (dir, opts) => hostWith(dir, opts, {});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { type Express } from 'express';
|
|
2
|
+
import { type BuildInfo } from '../discovery/discovery.js';
|
|
3
|
+
import { type Log } from '../log/log.js';
|
|
4
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
5
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
6
|
+
import { type PartAppBase } from '../server/app.js';
|
|
7
|
+
import { type PgliteStore } from '../store/pglite-store.js';
|
|
8
|
+
import { type HostExtras, type HostOptions } from './host-extension.js';
|
|
9
|
+
export interface Hosting {
|
|
10
|
+
dir: string;
|
|
11
|
+
pkg: ExtensionPackage;
|
|
12
|
+
log: Log;
|
|
13
|
+
opts: HostOptions;
|
|
14
|
+
extras: HostExtras;
|
|
15
|
+
store: PgliteStore | undefined;
|
|
16
|
+
onExit: () => void;
|
|
17
|
+
build: BuildInfo;
|
|
18
|
+
}
|
|
19
|
+
export interface HostedParts {
|
|
20
|
+
app: Express;
|
|
21
|
+
parts: HostPartKind[];
|
|
22
|
+
start(): void;
|
|
23
|
+
stop(): void;
|
|
24
|
+
}
|
|
25
|
+
export declare const hostPartsOf: (h: Hosting, base: PartAppBase) => Promise<HostedParts>;
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {} from 'express';
|
|
2
|
+
import { createContext } from '../context/context.js';
|
|
3
|
+
import { readSecrets, secretsFrom } from '../context/secrets.js';
|
|
4
|
+
import { settingsOf } from '../context/settings.js';
|
|
5
|
+
import { hostedRecordOf } from '../discovery/discovery.js';
|
|
6
|
+
import { readOpenapi } from '../discovery/openapi.js';
|
|
7
|
+
import { migrationsOf } from '../discovery/served-extras.js';
|
|
8
|
+
import { createLog } from '../log/log.js';
|
|
9
|
+
import {} from '../manifest/manifest.schema.js';
|
|
10
|
+
import { hostParts } from '../manifest/parts.js';
|
|
11
|
+
import { mcpPathOf } from '../mcp/mcp-app.js';
|
|
12
|
+
import { MOUNTS } from '../runtime/part-mounts.js';
|
|
13
|
+
import { createPartApp } from '../server/app.js';
|
|
14
|
+
import {} from '../store/pglite-store.js';
|
|
15
|
+
import {} from '../health/health.js';
|
|
16
|
+
import {} from './host-extension.js';
|
|
17
|
+
import { partChecks } from './part-checks.js';
|
|
18
|
+
const secretsOf = ({ pkg, opts, extras }, part) => extras.secretsDir
|
|
19
|
+
? readSecrets(pkg, part, extras.secretsDir)
|
|
20
|
+
: secretsFrom(pkg, part, async (name) => opts.secrets?.[name] ?? '');
|
|
21
|
+
// A ctx for each part: its own log and mounted secrets, the one store and the one map of checks.
|
|
22
|
+
const contextOf = async (h, part, checks) => createContext({
|
|
23
|
+
pkg: h.pkg,
|
|
24
|
+
part,
|
|
25
|
+
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
26
|
+
commit: h.build.commit,
|
|
27
|
+
log: part === 'api'
|
|
28
|
+
? h.log
|
|
29
|
+
: createLog({ extension: h.pkg.agentage.id, part, version: h.pkg.version }),
|
|
30
|
+
settings: settingsOf(h.pkg, h.opts.settings ?? {}, h.log),
|
|
31
|
+
store: h.store,
|
|
32
|
+
secrets: await secretsOf(h, part),
|
|
33
|
+
checks,
|
|
34
|
+
onExit: h.onExit,
|
|
35
|
+
});
|
|
36
|
+
const inputOf = async (h, part, base, reserved) => {
|
|
37
|
+
const ctx = await contextOf(h, part, partChecks(base.checks ?? {}, reserved));
|
|
38
|
+
const openapi = await readOpenapi(h.dir);
|
|
39
|
+
const mcpPaths = [mcpPathOf(h.pkg.agentage.id)];
|
|
40
|
+
return {
|
|
41
|
+
dir: h.dir,
|
|
42
|
+
pkg: h.pkg,
|
|
43
|
+
env: {},
|
|
44
|
+
ctx: () => ctx,
|
|
45
|
+
base,
|
|
46
|
+
openapi,
|
|
47
|
+
mcpPaths,
|
|
48
|
+
openListing: !base.tokenCheck,
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
// In order api, mcp, worker; a create that throws stops what the earlier ones started.
|
|
52
|
+
const mountsOf = async (h, parts, base) => {
|
|
53
|
+
const mounts = [];
|
|
54
|
+
// The host's own checks (store, token) stay refused names, as in a part image.
|
|
55
|
+
const reserved = new Set(Object.keys(base.checks ?? {}));
|
|
56
|
+
try {
|
|
57
|
+
for (const part of parts)
|
|
58
|
+
mounts.push(await MOUNTS[part](await inputOf(h, part, base, reserved)));
|
|
59
|
+
}
|
|
60
|
+
catch (err) {
|
|
61
|
+
for (const m of mounts)
|
|
62
|
+
m.stop?.();
|
|
63
|
+
throw err;
|
|
64
|
+
}
|
|
65
|
+
return mounts;
|
|
66
|
+
};
|
|
67
|
+
// The record a part image reads from its build, here from what each part serves.
|
|
68
|
+
const recordOf = async (h, parts, mounts) => {
|
|
69
|
+
const extras = Object.assign(await migrationsOf(h.dir, h.pkg), ...mounts.map((m) => m.record));
|
|
70
|
+
return hostedRecordOf(h.pkg, h.build, parts, extras);
|
|
71
|
+
};
|
|
72
|
+
// The api at /api/{id}/*, the mcp at /mcp/{id}, the worker jobs; one /health with every check.
|
|
73
|
+
export const hostPartsOf = async (h, base) => {
|
|
74
|
+
const parts = hostParts(h.pkg);
|
|
75
|
+
const mounts = await mountsOf(h, parts, base);
|
|
76
|
+
const mount = (app) => mounts.forEach((m) => m.mount(app));
|
|
77
|
+
const record = await recordOf(h, parts, mounts);
|
|
78
|
+
const app = createPartApp({ ...base, parts, part: 'api', mount, record });
|
|
79
|
+
const start = () => mounts.forEach((m) => m.start?.());
|
|
80
|
+
const stop = () => mounts.forEach((m) => m.stop?.());
|
|
81
|
+
return { app, parts, start, stop };
|
|
82
|
+
};
|
|
@@ -2,9 +2,12 @@ import { type Log } from '../log/log.js';
|
|
|
2
2
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
3
3
|
import { type PgliteStore } from '../store/pglite-store.js';
|
|
4
4
|
export declare const STORE_OPEN_MS = 120000;
|
|
5
|
+
export declare const lockWithin: (dataDir: string, openMs: number, lock?: string) => Promise<() => Promise<void>>;
|
|
6
|
+
export declare const lockOf: ({ dataDir, runDir }: Pick<StoreAt, "dataDir" | "runDir">) => string;
|
|
5
7
|
interface StoreAt {
|
|
6
8
|
dir: string;
|
|
7
9
|
dataDir: string;
|
|
10
|
+
runDir?: string;
|
|
8
11
|
openMs: number;
|
|
9
12
|
}
|
|
10
13
|
export declare const openHostStore: (pkg: ExtensionPackage, at: StoreAt, log: Log) => Promise<PgliteStore | undefined>;
|
package/dist/host/host-store.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { mkdir } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
2
4
|
import {} from '../log/log.js';
|
|
3
5
|
import {} from '../manifest/manifest.schema.js';
|
|
4
6
|
import { hasStore } from '../manifest/parts.js';
|
|
5
7
|
import { migrate } from '../store/migrations.js';
|
|
6
8
|
import { createPgliteStore } from '../store/pglite-store.js';
|
|
7
|
-
import { lockDataDir } from './data-lock.js';
|
|
9
|
+
import { DATA_DIR_LOCKED, lockDataDir } from './data-lock.js';
|
|
8
10
|
const unlocked = async () => undefined;
|
|
9
11
|
// How long the host waits for the store to open (and to close); the daemon sets its healthMs.
|
|
10
12
|
export const STORE_OPEN_MS = 120_000;
|
|
@@ -12,18 +14,35 @@ const within = (work, ms) => Promise.race([
|
|
|
12
14
|
work,
|
|
13
15
|
new Promise((resolve) => setTimeout(() => resolve('late'), ms).unref()),
|
|
14
16
|
]);
|
|
17
|
+
const LOCK_RETRY_MS = 100;
|
|
18
|
+
// A holder that is still draining (an old child, an orphan) lets go soon: retry until openMs.
|
|
19
|
+
export const lockWithin = async (dataDir, openMs, lock = `${dataDir}.lock`) => {
|
|
20
|
+
const until = Date.now() + openMs;
|
|
21
|
+
for (;;) {
|
|
22
|
+
try {
|
|
23
|
+
return await lockDataDir(dataDir, lock);
|
|
24
|
+
}
|
|
25
|
+
catch (err) {
|
|
26
|
+
if (err.code !== DATA_DIR_LOCKED || Date.now() >= until)
|
|
27
|
+
throw err;
|
|
28
|
+
await sleep(LOCK_RETRY_MS);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
};
|
|
15
32
|
// A close past openMs keeps the data lock: PGlite may still write there until the process exits.
|
|
16
33
|
const closer = (store, release, openMs) => async () => {
|
|
17
34
|
const closed = await within(store.close().catch(() => undefined), openMs);
|
|
18
35
|
if (closed !== 'late')
|
|
19
36
|
await release();
|
|
20
37
|
};
|
|
38
|
+
// C2: in run/{id} when the daemon gives it; an old <dataDir>.lock is then not read (the daemon cleared it).
|
|
39
|
+
export const lockOf = ({ dataDir, runDir }) => runDir ? join(runDir, 'store.lock') : `${dataDir}.lock`;
|
|
21
40
|
// Locks dataDir, then opens PGlite in it and migrates; past openMs the part starts, /health says not ready.
|
|
22
41
|
const open = async (at, log) => {
|
|
23
42
|
const { dir, dataDir, openMs } = at;
|
|
24
43
|
if (dataDir)
|
|
25
44
|
await mkdir(dataDir, { recursive: true });
|
|
26
|
-
const release = dataDir ? await
|
|
45
|
+
const release = dataDir ? await lockWithin(dataDir, openMs, lockOf(at)) : unlocked;
|
|
27
46
|
const store = createPgliteStore((s) => migrate(s, dir, log), {
|
|
28
47
|
dataDir: dataDir || undefined,
|
|
29
48
|
from: dir,
|
package/dist/host/main.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { createLog } from '../log/log.js';
|
|
3
|
+
import { startHostClock } from './clock-rate.js';
|
|
3
4
|
import { serveHost } from './serve-host.js';
|
|
4
5
|
// The daemon's child and the one container: `node .../dist/host/main.js`, settings from env.
|
|
5
6
|
const crash = (err) => {
|
|
@@ -7,4 +8,5 @@ const crash = (err) => {
|
|
|
7
8
|
process.exit(1);
|
|
8
9
|
};
|
|
9
10
|
process.on('uncaughtException', crash);
|
|
11
|
+
startHostClock(process.env);
|
|
10
12
|
await serveHost(process.env).catch(crash);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { worstOf } from '../health/health.js';
|
|
2
|
+
import {} from '../health/health.schema.js';
|
|
3
|
+
const statusOf = (check) => check().catch(() => 'unavailable');
|
|
4
|
+
// Both checks of one name run; /health shows the worse status.
|
|
5
|
+
const bothOf = (first, second) => async () => worstOf(await Promise.all([statusOf(first), statusOf(second)]));
|
|
6
|
+
// One part's view of the host's one map: its own names and the host's stay unique, a name of another part joins.
|
|
7
|
+
export const partChecks = (shared, reserved) => {
|
|
8
|
+
const own = new Set();
|
|
9
|
+
return new Proxy(shared, {
|
|
10
|
+
has: (_target, name) => own.has(String(name)) || reserved.has(String(name)),
|
|
11
|
+
set: (target, name, check) => {
|
|
12
|
+
const key = String(name);
|
|
13
|
+
own.add(key);
|
|
14
|
+
const before = target[key];
|
|
15
|
+
target[key] = before ? bothOf(before, check) : check;
|
|
16
|
+
return true;
|
|
17
|
+
},
|
|
18
|
+
});
|
|
19
|
+
};
|
package/dist/host/serve-host.js
CHANGED
|
@@ -2,10 +2,11 @@ import {} from 'node:http';
|
|
|
2
2
|
import {} from 'node:net';
|
|
3
3
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
4
4
|
import { createLog } from '../log/log.js';
|
|
5
|
-
import {
|
|
5
|
+
import {} from '../runtime/build-info.js';
|
|
6
6
|
import { listen, shutdownOn } from '../server/serve.js';
|
|
7
|
+
import { loopbackPorts } from './daemon/guard.js';
|
|
7
8
|
import { guardOf, hostEnvOf } from './host-env.js';
|
|
8
|
-
import { hostWith } from './host-extension.js';
|
|
9
|
+
import { hostBuildInfo, hostWith } from './host-extension.js';
|
|
9
10
|
export const ORPHAN_MS = 10_000;
|
|
10
11
|
// The port to the daemon; its IPC channel closed: drain, and exit within ORPHAN_MS in any case.
|
|
11
12
|
export const announce = (port, stop, exit = process.exit) => {
|
|
@@ -51,13 +52,16 @@ export const serveHost = async (env, deps = {}) => {
|
|
|
51
52
|
const at = hostEnvOf(env);
|
|
52
53
|
const guard = guardOf(env, at);
|
|
53
54
|
const hook = exitHook();
|
|
54
|
-
const { dataDir, settings, origin, secretsDir, storeOpenMs } = at;
|
|
55
|
-
|
|
56
|
-
const
|
|
55
|
+
const { dataDir, runDir, settings, origin, secretsDir, storeOpenMs } = at;
|
|
56
|
+
// C2: on loopback the child checks Host and Origin itself, for its own port and the daemon's.
|
|
57
|
+
const local = { localOnly: at.bind === '127.0.0.1', localPorts: loopbackPorts(origin) };
|
|
58
|
+
const opts = { dataDir, runDir, settings, origin, storeOpenMs, onExit: hook.onExit, ...local };
|
|
59
|
+
const build = await hostBuildInfo(env, at.dir);
|
|
60
|
+
const host = await hostWith(at.dir, opts, { secretsDir, guard, build });
|
|
57
61
|
const server = await listen(host.app, at.port, at.bind);
|
|
58
62
|
const { log, stop } = drainOn(server, host, deps, at.drainMs);
|
|
59
63
|
const { port } = server.address();
|
|
60
|
-
log.info('listening', { port, bind: at.bind });
|
|
64
|
+
log.info('listening', { port, bind: at.bind, parts: host.parts.join(',') });
|
|
61
65
|
(deps.ready ?? announce)(port, stop);
|
|
62
66
|
hook.arm(stop);
|
|
63
67
|
return { server, host, stop };
|
package/dist/manifest/parts.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ export interface NormalizedParts {
|
|
|
8
8
|
}
|
|
9
9
|
export declare const normalizeParts: (parts: Parts) => NormalizedParts;
|
|
10
10
|
export declare const imageParts: (pkg: ExtensionPackage) => ImagePartKind[];
|
|
11
|
+
export declare const HOST_PARTS: readonly ["api", "mcp", "worker"];
|
|
12
|
+
export type HostPartKind = (typeof HOST_PARTS)[number];
|
|
13
|
+
export declare const hostParts: (pkg: ExtensionPackage) => HostPartKind[];
|
|
11
14
|
export declare const entryOf: (pkg: ExtensionPackage, part: ImagePartKind | "cli") => string;
|
|
12
15
|
export declare const secretNames: (pkg: ExtensionPackage) => string[];
|
|
13
16
|
export declare const scopedSecrets: (pkg: ExtensionPackage) => {
|
package/dist/manifest/parts.js
CHANGED
|
@@ -21,6 +21,9 @@ export const imageParts = (pkg) => {
|
|
|
21
21
|
const { code } = normalizeParts(pkg.agentage.parts);
|
|
22
22
|
return IMAGE_PART_KINDS.filter((kind) => code[kind] !== undefined);
|
|
23
23
|
};
|
|
24
|
+
// The parts one host process runs (the daemon's child, the one container); pages and ui wait for phase 2.
|
|
25
|
+
export const HOST_PARTS = ['api', 'mcp', 'worker'];
|
|
26
|
+
export const hostParts = (pkg) => HOST_PARTS.filter((kind) => imageParts(pkg).includes(kind));
|
|
24
27
|
export const entryOf = (pkg, part) => normalizeParts(pkg.agentage.parts).code[part]?.entry ?? `./dist/${part}/index.js`;
|
|
25
28
|
export const secretNames = (pkg) => (pkg.agentage.secrets ?? []).map((s) => (typeof s === 'string' ? s : s.name));
|
|
26
29
|
const scopedOf = (pkg) => (pkg.agentage.secrets ?? []).flatMap((s) => (typeof s === 'string' ? [] : [s]));
|
package/dist/mcp/mcp-app.d.ts
CHANGED
|
@@ -4,5 +4,9 @@ import { type ToolEntry } from './tools.js';
|
|
|
4
4
|
export interface McpAppOptions extends PartAppBase {
|
|
5
5
|
tools: ToolEntry[];
|
|
6
6
|
listing: 'open' | 'token';
|
|
7
|
+
paths?: string[];
|
|
7
8
|
}
|
|
9
|
+
export declare const MCP_PATH = "/mcp";
|
|
10
|
+
export declare const mcpPathOf: (id: string) => string;
|
|
11
|
+
export declare const mountMcp: (app: Express, options: McpAppOptions) => void;
|
|
8
12
|
export declare const createMcpApp: (options: McpAppOptions) => Express;
|
package/dist/mcp/mcp-app.js
CHANGED
|
@@ -10,6 +10,9 @@ import { declaredType, printable } from '../log/who.js';
|
|
|
10
10
|
import { BODY_LIMIT, createPartApp, PUBLIC_CORS } from '../server/app.js';
|
|
11
11
|
import { serverFactory } from './server.js';
|
|
12
12
|
import { prefixed } from './tools.js';
|
|
13
|
+
export const MCP_PATH = '/mcp';
|
|
14
|
+
// The endpoint of an extension on a host that runs many: the daemon routes it to its child.
|
|
15
|
+
export const mcpPathOf = (id) => `${MCP_PATH}/${id}`;
|
|
13
16
|
const MCP_HEADERS = ['Mcp-Protocol-Version', 'Mcp-Method', 'Mcp-Name', 'Accept'];
|
|
14
17
|
const MCP_CORS = {
|
|
15
18
|
...PUBLIC_CORS,
|
|
@@ -103,21 +106,18 @@ const methodNotAllowed = (_req, res) => {
|
|
|
103
106
|
.status(405)
|
|
104
107
|
.json({ jsonrpc: '2.0', error: { code: -32000, message: 'Method not allowed.' }, id: null });
|
|
105
108
|
};
|
|
106
|
-
// Stateless Streamable HTTP on SDK v2: POST
|
|
107
|
-
export const
|
|
109
|
+
// Stateless Streamable HTTP on SDK v2: POST only, GET and DELETE (sessions) answer 405.
|
|
110
|
+
export const mountMcp = (app, options) => {
|
|
108
111
|
const identity = { id: options.pkg.agentage.id, version: options.pkg.version };
|
|
109
112
|
const handler = createMcpHandler(serverFactory(identity, options.tools, options.log), {
|
|
110
113
|
legacy: 'stateless',
|
|
111
114
|
onerror: (err) => options.log.warn('mcp request refused', { reason: err.message }),
|
|
112
115
|
});
|
|
113
116
|
const raw = express.raw({ type: () => true, limit: BODY_LIMIT });
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
app.post('/mcp', raw, handle(handler, options));
|
|
120
|
-
app.all('/mcp', methodNotAllowed);
|
|
121
|
-
},
|
|
122
|
-
});
|
|
117
|
+
for (const path of options.paths ?? [MCP_PATH]) {
|
|
118
|
+
app.use(path, cors(MCP_CORS));
|
|
119
|
+
app.post(path, raw, handle(handler, options));
|
|
120
|
+
app.all(path, methodNotAllowed);
|
|
121
|
+
}
|
|
123
122
|
};
|
|
123
|
+
export const createMcpApp = (options) => createPartApp({ ...options, part: 'mcp', mount: (app) => mountMcp(app, options) });
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type Express } from 'express';
|
|
2
|
+
import { type PartContext } from '../context/context.js';
|
|
3
|
+
import { type RecordExtras } from '../discovery/discovery.js';
|
|
4
|
+
import { type HealthCheck } from '../health/health.js';
|
|
5
|
+
import { type ImportModule } from '../manifest/entry.js';
|
|
6
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
7
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
8
|
+
import { type ImagePartKind } from '../manifest/primitives.schema.js';
|
|
9
|
+
import { type PartAppBase } from '../server/app.js';
|
|
10
|
+
import { type Env } from './build-info.js';
|
|
11
|
+
export interface RunnerInput {
|
|
12
|
+
dir: string;
|
|
13
|
+
pkg: ExtensionPackage;
|
|
14
|
+
env: Env;
|
|
15
|
+
ctx: () => PartContext;
|
|
16
|
+
base: PartAppBase;
|
|
17
|
+
openapi: Record<string, unknown> | null;
|
|
18
|
+
importModule?: ImportModule;
|
|
19
|
+
apiAliases?: string[];
|
|
20
|
+
mcpPaths?: string[];
|
|
21
|
+
openListing?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface PartMount {
|
|
24
|
+
mount: (app: Express) => void;
|
|
25
|
+
checks?: Record<string, HealthCheck>;
|
|
26
|
+
start?: () => void;
|
|
27
|
+
stop?: () => void;
|
|
28
|
+
record?: RecordExtras;
|
|
29
|
+
}
|
|
30
|
+
export declare const callCreate: <C>(input: RunnerInput, part: ImagePartKind, ctx: C) => Promise<unknown>;
|
|
31
|
+
export declare const MOUNTS: Record<HostPartKind, (input: RunnerInput) => Promise<PartMount>>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {} from 'express';
|
|
2
|
+
import { generatedOpenapi } from '../api/openapi-doc.js';
|
|
3
|
+
import {} from '../context/context.js';
|
|
4
|
+
import {} from '../discovery/discovery.js';
|
|
5
|
+
import { openapiPath } from '../discovery/openapi.js';
|
|
6
|
+
import { toolRecords } from '../discovery/served-extras.js';
|
|
7
|
+
import {} from '../health/health.js';
|
|
8
|
+
import { loadCreate } from '../manifest/entry.js';
|
|
9
|
+
import {} from '../manifest/manifest.schema.js';
|
|
10
|
+
import { hasStore } from '../manifest/parts.js';
|
|
11
|
+
import {} from '../manifest/primitives.schema.js';
|
|
12
|
+
import { createSpan } from '../log/span.js';
|
|
13
|
+
import { mountMcp } from '../mcp/mcp-app.js';
|
|
14
|
+
import { createToolRegistry } from '../mcp/tools.js';
|
|
15
|
+
import { mountApiRoutes } from '../server/app.js';
|
|
16
|
+
import { mountApi } from '../server/api-routes.js';
|
|
17
|
+
import { runLog } from '../worker/run-log.js';
|
|
18
|
+
import { memoryRuns, storeRuns } from '../worker/runs.js';
|
|
19
|
+
import { createScheduler } from '../worker/scheduler.js';
|
|
20
|
+
import {} from './build-info.js';
|
|
21
|
+
// ui has no code to run; every other part is its create(ctx).
|
|
22
|
+
export const callCreate = async (input, part, ctx) => (await loadCreate(input.dir, input.pkg, part, input.importModule))(ctx);
|
|
23
|
+
const api = async (input) => {
|
|
24
|
+
const ctx = input.ctx();
|
|
25
|
+
const { router, routes, documents } = await mountApi(ctx, (api) => callCreate(input, 'api', api));
|
|
26
|
+
// Declared routes guard at their own scope, whatever the committed file says.
|
|
27
|
+
const openapi = routes.length > 0 ? generatedOpenapi(input.pkg, routes) : input.openapi;
|
|
28
|
+
const options = { ...input.base, router, routes, openapi, wellKnown: documents };
|
|
29
|
+
const record = openapi ? { openapi: openapiPath(input.pkg.agentage.id) } : {};
|
|
30
|
+
return { mount: (app) => mountApiRoutes(app, { ...options, aliases: input.apiAliases }), record };
|
|
31
|
+
};
|
|
32
|
+
const mcp = async (input) => {
|
|
33
|
+
const registry = createToolRegistry();
|
|
34
|
+
const ctx = input.ctx();
|
|
35
|
+
await callCreate(input, 'mcp', { ...ctx, tool: registry.tool });
|
|
36
|
+
const token = !input.openListing && ctx.settings['mcp.listing'] === 'token';
|
|
37
|
+
const listing = token ? 'token' : 'open';
|
|
38
|
+
const options = { ...input.base, tools: registry.tools, listing, paths: input.mcpPaths };
|
|
39
|
+
const tools = toolRecords(input.pkg.agentage.id, registry.tools);
|
|
40
|
+
return { mount: (app) => mountMcp(app, options), record: tools.length ? { tools } : {} };
|
|
41
|
+
};
|
|
42
|
+
const worker = async (input) => {
|
|
43
|
+
const ctx = input.ctx();
|
|
44
|
+
const log = runLog(ctx.log);
|
|
45
|
+
const runs = hasStore(input.pkg, 'postgres') ? storeRuns(ctx.store) : memoryRuns();
|
|
46
|
+
const scheduler = createScheduler({ runs, log });
|
|
47
|
+
const span = createSpan(log);
|
|
48
|
+
await callCreate(input, 'worker', { ...ctx, log, span, schedule: scheduler.schedule });
|
|
49
|
+
// The same object ctx.health adds to, so a later registration still shows.
|
|
50
|
+
const checks = Object.assign(input.base.checks ?? {}, scheduler.checks());
|
|
51
|
+
return { mount: () => undefined, checks, start: scheduler.start, stop: scheduler.stop };
|
|
52
|
+
};
|
|
53
|
+
export const MOUNTS = {
|
|
54
|
+
api,
|
|
55
|
+
mcp,
|
|
56
|
+
worker,
|
|
57
|
+
};
|
|
@@ -1,20 +1,7 @@
|
|
|
1
1
|
import { type Express } from 'express';
|
|
2
|
-
import { type PartContext } from '../context/context.js';
|
|
3
|
-
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
4
|
-
import { type ImportModule } from '../manifest/entry.js';
|
|
5
2
|
import { type ImagePartKind } from '../manifest/primitives.schema.js';
|
|
6
|
-
import { type
|
|
7
|
-
|
|
8
|
-
export interface RunnerInput {
|
|
9
|
-
dir: string;
|
|
10
|
-
pkg: ExtensionPackage;
|
|
11
|
-
env: Env;
|
|
12
|
-
ctx: () => PartContext;
|
|
13
|
-
base: PartAppBase;
|
|
14
|
-
openapi: Record<string, unknown> | null;
|
|
15
|
-
importModule?: ImportModule;
|
|
16
|
-
apiAliases?: string[];
|
|
17
|
-
}
|
|
3
|
+
import { type RunnerInput } from './part-mounts.js';
|
|
4
|
+
export { type RunnerInput } from './part-mounts.js';
|
|
18
5
|
export interface Running {
|
|
19
6
|
app: Express;
|
|
20
7
|
stop?: () => void;
|