@agentage/extension 0.13.0 → 0.15.0
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/discovery/discovery.d.ts +1 -0
- package/dist/discovery/discovery.schema.d.ts +2 -0
- package/dist/discovery/discovery.schema.js +22 -10
- 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.d.ts +9 -0
- package/dist/host/daemon/add.js +84 -0
- 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 +12 -4
- package/dist/host/daemon/child.js +78 -44
- package/dist/host/daemon/daemon-files.d.ts +6 -5
- package/dist/host/daemon/daemon-files.js +20 -12
- package/dist/host/daemon/daemon-owner.d.ts +1 -0
- package/dist/host/daemon/daemon-owner.js +32 -0
- package/dist/host/daemon/daemon.d.ts +3 -3
- package/dist/host/daemon/daemon.js +72 -35
- package/dist/host/daemon/entries.d.ts +21 -0
- package/dist/host/daemon/entries.js +24 -0
- 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 +17 -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/installation.d.ts +17 -4
- package/dist/host/daemon/installation.js +52 -8
- package/dist/host/daemon/installer-call.d.ts +16 -2
- package/dist/host/daemon/installer-call.js +33 -7
- package/dist/host/daemon/json-file.d.ts +3 -2
- package/dist/host/daemon/json-file.js +21 -10
- package/dist/host/daemon/lane.d.ts +1 -3
- package/dist/host/daemon/lane.js +4 -9
- 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 +15 -0
- package/dist/host/daemon/limits.js +29 -0
- package/dist/host/daemon/ops.d.ts +2 -26
- package/dist/host/daemon/ops.js +46 -103
- package/dist/host/daemon/patch-values.d.ts +6 -0
- package/dist/host/daemon/patch-values.js +39 -0
- package/dist/host/daemon/pending.d.ts +15 -0
- package/dist/host/daemon/pending.js +18 -0
- package/dist/host/daemon/proxy.d.ts +3 -2
- package/dist/host/daemon/proxy.js +54 -24
- package/dist/host/daemon/queue.d.ts +1 -0
- package/dist/host/daemon/queue.js +19 -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 +3 -9
- package/dist/host/daemon/routes.js +54 -22
- 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 +6 -0
- package/dist/host/daemon/start-checks.js +28 -0
- package/dist/host/daemon/stopping.d.ts +10 -0
- package/dist/host/daemon/stopping.js +28 -0
- package/dist/host/daemon/supervisor.d.ts +19 -11
- package/dist/host/daemon/supervisor.js +39 -47
- package/dist/host/daemon/write-all.d.ts +8 -0
- package/dist/host/daemon/write-all.js +45 -0
- package/dist/host/data-lock.d.ts +3 -1
- package/dist/host/data-lock.js +88 -29
- package/dist/host/host-env.d.ts +3 -1
- package/dist/host/host-env.js +10 -2
- package/dist/host/host-extension.d.ts +8 -0
- package/dist/host/host-extension.js +30 -45
- package/dist/host/host-parts.d.ts +25 -0
- package/dist/host/host-parts.js +75 -0
- package/dist/host/host-store.d.ts +11 -1
- package/dist/host/host-store.js +45 -12
- package/dist/host/index.d.ts +4 -0
- package/dist/host/index.js +4 -0
- 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.d.ts +2 -1
- package/dist/host/serve-host.js +16 -10
- 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 +29 -0
- package/dist/runtime/part-mounts.js +52 -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 -1
- package/dist/server/app.js +17 -17
- package/dist/server/serve.d.ts +3 -0
- package/dist/server/serve.js +9 -4
- package/dist/tool/build.d.ts +3 -0
- package/dist/tool/build.js +42 -27
- package/dist/tool/capabilities.d.ts +1 -0
- package/dist/tool/capabilities.js +8 -0
- package/dist/tool/check.d.ts +2 -0
- package/dist/tool/check.js +23 -8
- package/dist/tool/container-parts.d.ts +7 -0
- package/dist/tool/container-parts.js +10 -0
- package/dist/tool/container-recipe.d.ts +4 -0
- package/dist/tool/container-recipe.js +55 -0
- package/dist/tool/container-stage.d.ts +3 -0
- package/dist/tool/container-stage.js +51 -0
- package/dist/tool/container.d.ts +73 -0
- package/dist/tool/container.js +28 -0
- package/dist/tool/docker.d.ts +5 -0
- package/dist/tool/docker.js +15 -0
- package/dist/tool/format.js +23 -9
- package/dist/tool/index.d.ts +2 -0
- package/dist/tool/index.js +2 -0
- package/dist/tool/kit-version.d.ts +3 -0
- package/dist/tool/kit-version.js +28 -2
- package/dist/tool/labels.d.ts +1 -1
- package/dist/tool/labels.js +4 -2
- package/dist/tool/main.js +10 -1
- package/dist/tool/stage.d.ts +1 -0
- package/dist/tool/stage.js +1 -1
- package/dist/tool/up/local-record.d.ts +1 -0
- 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 +8 -1
- package/schemas/extension-record.schema.json +8 -1
- package/schemas/health.schema.json +17 -0
- package/dist/host/daemon/secret-names.d.ts +0 -1
- package/dist/host/daemon/secret-names.js +0 -14
- package/dist/host/daemon/setting-values.d.ts +0 -1
- package/dist/host/daemon/setting-values.js +0 -26
|
@@ -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,75 @@
|
|
|
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 {} from '../discovery/discovery.js';
|
|
6
|
+
import { readOpenapi } from '../discovery/openapi.js';
|
|
7
|
+
import { createLog } from '../log/log.js';
|
|
8
|
+
import {} from '../manifest/manifest.schema.js';
|
|
9
|
+
import { hostParts } from '../manifest/parts.js';
|
|
10
|
+
import { mcpPathOf } from '../mcp/mcp-app.js';
|
|
11
|
+
import { MOUNTS } from '../runtime/part-mounts.js';
|
|
12
|
+
import { createPartApp } from '../server/app.js';
|
|
13
|
+
import {} from '../store/pglite-store.js';
|
|
14
|
+
import {} from '../health/health.js';
|
|
15
|
+
import {} from './host-extension.js';
|
|
16
|
+
import { partChecks } from './part-checks.js';
|
|
17
|
+
const secretsOf = ({ pkg, opts, extras }, part) => extras.secretsDir
|
|
18
|
+
? readSecrets(pkg, part, extras.secretsDir)
|
|
19
|
+
: secretsFrom(pkg, part, async (name) => opts.secrets?.[name] ?? '');
|
|
20
|
+
// A ctx for each part: its own log and mounted secrets, the one store and the one map of checks.
|
|
21
|
+
const contextOf = async (h, part, checks) => createContext({
|
|
22
|
+
pkg: h.pkg,
|
|
23
|
+
part,
|
|
24
|
+
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
25
|
+
commit: h.build.commit,
|
|
26
|
+
log: part === 'api'
|
|
27
|
+
? h.log
|
|
28
|
+
: createLog({ extension: h.pkg.agentage.id, part, version: h.pkg.version }),
|
|
29
|
+
settings: settingsOf(h.pkg, h.opts.settings ?? {}, h.log),
|
|
30
|
+
store: h.store,
|
|
31
|
+
secrets: await secretsOf(h, part),
|
|
32
|
+
checks,
|
|
33
|
+
onExit: h.onExit,
|
|
34
|
+
});
|
|
35
|
+
const inputOf = async (h, part, base, reserved) => {
|
|
36
|
+
const ctx = await contextOf(h, part, partChecks(base.checks ?? {}, reserved));
|
|
37
|
+
const openapi = await readOpenapi(h.dir);
|
|
38
|
+
const mcpPaths = [mcpPathOf(h.pkg.agentage.id)];
|
|
39
|
+
return {
|
|
40
|
+
dir: h.dir,
|
|
41
|
+
pkg: h.pkg,
|
|
42
|
+
env: {},
|
|
43
|
+
ctx: () => ctx,
|
|
44
|
+
base,
|
|
45
|
+
openapi,
|
|
46
|
+
mcpPaths,
|
|
47
|
+
openListing: !base.tokenCheck,
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
// In order api, mcp, worker; a create that throws stops what the earlier ones started.
|
|
51
|
+
const mountsOf = async (h, parts, base) => {
|
|
52
|
+
const mounts = [];
|
|
53
|
+
// The host's own checks (store, token) stay refused names, as in a part image.
|
|
54
|
+
const reserved = new Set(Object.keys(base.checks ?? {}));
|
|
55
|
+
try {
|
|
56
|
+
for (const part of parts)
|
|
57
|
+
mounts.push(await MOUNTS[part](await inputOf(h, part, base, reserved)));
|
|
58
|
+
}
|
|
59
|
+
catch (err) {
|
|
60
|
+
for (const m of mounts)
|
|
61
|
+
m.stop?.();
|
|
62
|
+
throw err;
|
|
63
|
+
}
|
|
64
|
+
return mounts;
|
|
65
|
+
};
|
|
66
|
+
// The api at /api/{id}/*, the mcp at /mcp/{id}, the worker jobs; one /health with every check.
|
|
67
|
+
export const hostPartsOf = async (h, base) => {
|
|
68
|
+
const parts = hostParts(h.pkg);
|
|
69
|
+
const mounts = await mountsOf(h, parts, base);
|
|
70
|
+
const mount = (app) => mounts.forEach((m) => m.mount(app));
|
|
71
|
+
const app = createPartApp({ ...base, parts, part: 'api', mount });
|
|
72
|
+
const start = () => mounts.forEach((m) => m.start?.());
|
|
73
|
+
const stop = () => mounts.forEach((m) => m.stop?.());
|
|
74
|
+
return { app, parts, start, stop };
|
|
75
|
+
};
|
|
@@ -1,4 +1,14 @@
|
|
|
1
1
|
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
|
-
export declare const
|
|
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;
|
|
7
|
+
interface StoreAt {
|
|
8
|
+
dir: string;
|
|
9
|
+
dataDir: string;
|
|
10
|
+
runDir?: string;
|
|
11
|
+
openMs: number;
|
|
12
|
+
}
|
|
13
|
+
export declare const openHostStore: (pkg: ExtensionPackage, at: StoreAt, log: Log) => Promise<PgliteStore | undefined>;
|
|
14
|
+
export {};
|
package/dist/host/host-store.js
CHANGED
|
@@ -1,35 +1,68 @@
|
|
|
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
|
-
//
|
|
10
|
-
const
|
|
11
|
+
// How long the host waits for the store to open (and to close); the daemon sets its healthMs.
|
|
12
|
+
export const STORE_OPEN_MS = 120_000;
|
|
13
|
+
const within = (work, ms) => Promise.race([
|
|
14
|
+
work,
|
|
15
|
+
new Promise((resolve) => setTimeout(() => resolve('late'), ms).unref()),
|
|
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
|
+
};
|
|
32
|
+
// A close past openMs keeps the data lock: PGlite may still write there until the process exits.
|
|
33
|
+
const closer = (store, release, openMs) => async () => {
|
|
34
|
+
const closed = await within(store.close().catch(() => undefined), openMs);
|
|
35
|
+
if (closed !== 'late')
|
|
36
|
+
await release();
|
|
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`;
|
|
40
|
+
// Locks dataDir, then opens PGlite in it and migrates; past openMs the part starts, /health says not ready.
|
|
41
|
+
const open = async (at, log) => {
|
|
42
|
+
const { dir, dataDir, openMs } = at;
|
|
11
43
|
if (dataDir)
|
|
12
44
|
await mkdir(dataDir, { recursive: true });
|
|
13
|
-
const release = dataDir ? await
|
|
45
|
+
const release = dataDir ? await lockWithin(dataDir, openMs, lockOf(at)) : unlocked;
|
|
14
46
|
const store = createPgliteStore((s) => migrate(s, dir, log), {
|
|
15
47
|
dataDir: dataDir || undefined,
|
|
16
48
|
from: dir,
|
|
17
49
|
});
|
|
18
|
-
const close =
|
|
19
|
-
|
|
20
|
-
await release();
|
|
21
|
-
};
|
|
22
|
-
await store.query('select 1').catch(async (err) => {
|
|
50
|
+
const close = closer(store, release, openMs);
|
|
51
|
+
const opened = store.query('select 1').catch(async (err) => {
|
|
23
52
|
await close();
|
|
24
53
|
throw err;
|
|
25
54
|
});
|
|
55
|
+
if ((await within(opened, openMs)) === 'late') {
|
|
56
|
+
opened.catch(() => undefined);
|
|
57
|
+
log.warn('the store did not open in time: /health says unavailable until it does', { openMs });
|
|
58
|
+
}
|
|
26
59
|
return { query: store.query, close };
|
|
27
60
|
};
|
|
28
61
|
// A postgres store as PGlite with its files in dataDir; none set keeps it in memory.
|
|
29
|
-
export const openHostStore = async (
|
|
62
|
+
export const openHostStore = async (pkg, at, log) => {
|
|
30
63
|
if (!hasStore(pkg, 'postgres'))
|
|
31
64
|
return undefined;
|
|
32
|
-
if (!dataDir)
|
|
65
|
+
if (!at.dataDir)
|
|
33
66
|
log.warn('no data folder: the store is in memory and lost on exit');
|
|
34
|
-
return open(
|
|
67
|
+
return open(at, log);
|
|
35
68
|
};
|
package/dist/host/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export { capabilities } from '../tool/capabilities.js';
|
|
2
|
+
export { localOnly } from './daemon/guard.js';
|
|
1
3
|
export { createDaemon, DAEMON_PORT, type Daemon, type DaemonOptions } from './daemon/daemon.js';
|
|
2
4
|
export { type Installer, type State } from './daemon/supervisor.js';
|
|
5
|
+
export { withFileLock } from './daemon/json-file.js';
|
|
6
|
+
export { updateInstallation, type InstallEntry, type Installation } from './daemon/installation.js';
|
|
3
7
|
export { hostExtension, type HostedExtension, type HostOptions } from './host-extension.js';
|
package/dist/host/index.js
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
export { capabilities } from '../tool/capabilities.js';
|
|
2
|
+
export { localOnly } from './daemon/guard.js';
|
|
1
3
|
export { createDaemon, DAEMON_PORT } from './daemon/daemon.js';
|
|
2
4
|
export {} from './daemon/supervisor.js';
|
|
5
|
+
export { withFileLock } from './daemon/json-file.js';
|
|
6
|
+
export { updateInstallation } from './daemon/installation.js';
|
|
3
7
|
export { hostExtension } from './host-extension.js';
|
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
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Server } from 'node:http';
|
|
2
2
|
import { type Env } from '../runtime/build-info.js';
|
|
3
3
|
import { type HostedExtension } from './host-extension.js';
|
|
4
|
-
|
|
4
|
+
interface ServeHostDeps {
|
|
5
5
|
exit?: (code: number) => void;
|
|
6
6
|
signals?: NodeJS.Signals[];
|
|
7
7
|
ready?: (port: number, stop: () => void) => void;
|
|
@@ -14,3 +14,4 @@ export interface ServedHost {
|
|
|
14
14
|
export declare const ORPHAN_MS = 10000;
|
|
15
15
|
export declare const announce: (port: number, stop: () => void, exit?: (code?: number | string | null) => never) => void;
|
|
16
16
|
export declare const serveHost: (env: Env, deps?: ServeHostDeps) => Promise<ServedHost>;
|
|
17
|
+
export {};
|
package/dist/host/serve-host.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
import {} from 'node:http';
|
|
2
2
|
import {} from 'node:net';
|
|
3
|
+
import { setTimeout as sleep } from 'node:timers/promises';
|
|
3
4
|
import { createLog } from '../log/log.js';
|
|
4
|
-
import {} from '../runtime/build-info.js';
|
|
5
|
+
import { buildInfoFromEnv } from '../runtime/build-info.js';
|
|
5
6
|
import { listen, shutdownOn } from '../server/serve.js';
|
|
7
|
+
import { loopbackPorts } from './daemon/guard.js';
|
|
6
8
|
import { guardOf, hostEnvOf } from './host-env.js';
|
|
7
9
|
import { hostWith } from './host-extension.js';
|
|
8
10
|
export const ORPHAN_MS = 10_000;
|
|
9
|
-
// The port to the daemon;
|
|
10
|
-
// within ORPHAN_MS even with a request that never ends.
|
|
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) => {
|
|
12
13
|
if (!process.send)
|
|
13
14
|
return;
|
|
@@ -30,16 +31,19 @@ const exitHook = () => {
|
|
|
30
31
|
return { onExit, arm };
|
|
31
32
|
};
|
|
32
33
|
// SIGTERM, ctx.exit() and a gone parent drain the server, then stop the host and exit.
|
|
33
|
-
const drainOn = (server, host, deps) => {
|
|
34
|
+
const drainOn = (server, host, deps, graceMs) => {
|
|
34
35
|
const log = createLog({ extension: host.id, part: 'api', version: host.version });
|
|
35
36
|
const exit = deps.exit ?? ((code) => process.exit(code));
|
|
36
|
-
|
|
37
|
+
// A store close that hangs gets graceMs too, so the process always exits.
|
|
38
|
+
const closed = () => Promise.race([host.stop(), sleep(graceMs, undefined, { ref: false })]);
|
|
39
|
+
const done = (code) => void closed().finally(() => exit(code));
|
|
37
40
|
const stop = shutdownOn(server, {
|
|
38
41
|
app: host.app,
|
|
39
42
|
port: 0,
|
|
40
43
|
log,
|
|
41
44
|
exit: done,
|
|
42
45
|
signals: deps.signals,
|
|
46
|
+
graceMs,
|
|
43
47
|
});
|
|
44
48
|
return { log, stop };
|
|
45
49
|
};
|
|
@@ -48,13 +52,15 @@ export const serveHost = async (env, deps = {}) => {
|
|
|
48
52
|
const at = hostEnvOf(env);
|
|
49
53
|
const guard = guardOf(env, at);
|
|
50
54
|
const hook = exitHook();
|
|
51
|
-
const { dataDir, settings, origin, secretsDir } = at;
|
|
52
|
-
|
|
53
|
-
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 host = await hostWith(at.dir, opts, { secretsDir, guard, build: buildInfoFromEnv(env) });
|
|
54
60
|
const server = await listen(host.app, at.port, at.bind);
|
|
55
|
-
const { log, stop } = drainOn(server, host, deps);
|
|
61
|
+
const { log, stop } = drainOn(server, host, deps, at.drainMs);
|
|
56
62
|
const { port } = server.address();
|
|
57
|
-
log.info('listening', { port, bind: at.bind });
|
|
63
|
+
log.info('listening', { port, bind: at.bind, parts: host.parts.join(',') });
|
|
58
64
|
(deps.ready ?? announce)(port, stop);
|
|
59
65
|
hook.arm(stop);
|
|
60
66
|
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,29 @@
|
|
|
1
|
+
import { type Express } from 'express';
|
|
2
|
+
import { type PartContext } from '../context/context.js';
|
|
3
|
+
import { type HealthCheck } from '../health/health.js';
|
|
4
|
+
import { type ImportModule } from '../manifest/entry.js';
|
|
5
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
6
|
+
import { type HostPartKind } from '../manifest/parts.js';
|
|
7
|
+
import { type ImagePartKind } from '../manifest/primitives.schema.js';
|
|
8
|
+
import { type PartAppBase } from '../server/app.js';
|
|
9
|
+
import { type Env } from './build-info.js';
|
|
10
|
+
export interface RunnerInput {
|
|
11
|
+
dir: string;
|
|
12
|
+
pkg: ExtensionPackage;
|
|
13
|
+
env: Env;
|
|
14
|
+
ctx: () => PartContext;
|
|
15
|
+
base: PartAppBase;
|
|
16
|
+
openapi: Record<string, unknown> | null;
|
|
17
|
+
importModule?: ImportModule;
|
|
18
|
+
apiAliases?: string[];
|
|
19
|
+
mcpPaths?: string[];
|
|
20
|
+
openListing?: boolean;
|
|
21
|
+
}
|
|
22
|
+
export interface PartMount {
|
|
23
|
+
mount: (app: Express) => void;
|
|
24
|
+
checks?: Record<string, HealthCheck>;
|
|
25
|
+
start?: () => void;
|
|
26
|
+
stop?: () => void;
|
|
27
|
+
}
|
|
28
|
+
export declare const callCreate: <C>(input: RunnerInput, part: ImagePartKind, ctx: C) => Promise<unknown>;
|
|
29
|
+
export declare const MOUNTS: Record<HostPartKind, (input: RunnerInput) => Promise<PartMount>>;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import {} from 'express';
|
|
2
|
+
import { generatedOpenapi } from '../api/openapi-doc.js';
|
|
3
|
+
import {} from '../context/context.js';
|
|
4
|
+
import {} from '../health/health.js';
|
|
5
|
+
import { loadCreate } from '../manifest/entry.js';
|
|
6
|
+
import {} from '../manifest/manifest.schema.js';
|
|
7
|
+
import { hasStore } from '../manifest/parts.js';
|
|
8
|
+
import {} from '../manifest/primitives.schema.js';
|
|
9
|
+
import { createSpan } from '../log/span.js';
|
|
10
|
+
import { mountMcp } from '../mcp/mcp-app.js';
|
|
11
|
+
import { createToolRegistry } from '../mcp/tools.js';
|
|
12
|
+
import { mountApiRoutes } from '../server/app.js';
|
|
13
|
+
import { mountApi } from '../server/api-routes.js';
|
|
14
|
+
import { runLog } from '../worker/run-log.js';
|
|
15
|
+
import { memoryRuns, storeRuns } from '../worker/runs.js';
|
|
16
|
+
import { createScheduler } from '../worker/scheduler.js';
|
|
17
|
+
import {} from './build-info.js';
|
|
18
|
+
// ui has no code to run; every other part is its create(ctx).
|
|
19
|
+
export const callCreate = async (input, part, ctx) => (await loadCreate(input.dir, input.pkg, part, input.importModule))(ctx);
|
|
20
|
+
const api = async (input) => {
|
|
21
|
+
const ctx = input.ctx();
|
|
22
|
+
const { router, routes, documents } = await mountApi(ctx, (api) => callCreate(input, 'api', api));
|
|
23
|
+
// Declared routes guard at their own scope, whatever the committed file says.
|
|
24
|
+
const openapi = routes.length > 0 ? generatedOpenapi(input.pkg, routes) : input.openapi;
|
|
25
|
+
const options = { ...input.base, router, routes, openapi, wellKnown: documents };
|
|
26
|
+
return { mount: (app) => mountApiRoutes(app, { ...options, aliases: input.apiAliases }) };
|
|
27
|
+
};
|
|
28
|
+
const mcp = async (input) => {
|
|
29
|
+
const registry = createToolRegistry();
|
|
30
|
+
const ctx = input.ctx();
|
|
31
|
+
await callCreate(input, 'mcp', { ...ctx, tool: registry.tool });
|
|
32
|
+
const token = !input.openListing && ctx.settings['mcp.listing'] === 'token';
|
|
33
|
+
const listing = token ? 'token' : 'open';
|
|
34
|
+
const options = { ...input.base, tools: registry.tools, listing, paths: input.mcpPaths };
|
|
35
|
+
return { mount: (app) => mountMcp(app, options) };
|
|
36
|
+
};
|
|
37
|
+
const worker = async (input) => {
|
|
38
|
+
const ctx = input.ctx();
|
|
39
|
+
const log = runLog(ctx.log);
|
|
40
|
+
const runs = hasStore(input.pkg, 'postgres') ? storeRuns(ctx.store) : memoryRuns();
|
|
41
|
+
const scheduler = createScheduler({ runs, log });
|
|
42
|
+
const span = createSpan(log);
|
|
43
|
+
await callCreate(input, 'worker', { ...ctx, log, span, schedule: scheduler.schedule });
|
|
44
|
+
// The same object ctx.health adds to, so a later registration still shows.
|
|
45
|
+
const checks = Object.assign(input.base.checks ?? {}, scheduler.checks());
|
|
46
|
+
return { mount: () => undefined, checks, start: scheduler.start, stop: scheduler.stop };
|
|
47
|
+
};
|
|
48
|
+
export const MOUNTS = {
|
|
49
|
+
api,
|
|
50
|
+
mcp,
|
|
51
|
+
worker,
|
|
52
|
+
};
|
|
@@ -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;
|
|
@@ -1,47 +1,16 @@
|
|
|
1
1
|
import { join } from 'node:path';
|
|
2
|
-
import { generatedOpenapi } from '../api/openapi-doc.js';
|
|
3
2
|
import {} from 'express';
|
|
4
|
-
import {} from '../context/context.js';
|
|
5
|
-
import {} from '../manifest/manifest.schema.js';
|
|
6
|
-
import { loadCreate } from '../manifest/entry.js';
|
|
7
|
-
import { hasStore } from '../manifest/parts.js';
|
|
8
|
-
import {} from '../manifest/primitives.schema.js';
|
|
9
|
-
import { createSpan } from '../log/span.js';
|
|
10
|
-
import { createMcpApp } from '../mcp/mcp-app.js';
|
|
11
|
-
import { createToolRegistry } from '../mcp/tools.js';
|
|
12
3
|
import { createPagesApp, platformSources } from '../pages/pages-app.js';
|
|
13
4
|
import {} from '../pages/pages.js';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
import {} from './build-info.js';
|
|
5
|
+
import {} from '../manifest/primitives.schema.js';
|
|
6
|
+
import { createPartApp } from '../server/app.js';
|
|
17
7
|
import { createUiApp } from '../ui/ui-app.js';
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const
|
|
23
|
-
|
|
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
|
-
return {
|
|
29
|
-
app: createApiApp({
|
|
30
|
-
...input.base,
|
|
31
|
-
router,
|
|
32
|
-
routes,
|
|
33
|
-
openapi,
|
|
34
|
-
wellKnown: documents,
|
|
35
|
-
aliases: input.apiAliases,
|
|
36
|
-
}),
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
const mcp = async (input) => {
|
|
40
|
-
const registry = createToolRegistry();
|
|
41
|
-
const ctx = input.ctx();
|
|
42
|
-
await callCreate(input, 'mcp', { ...ctx, tool: registry.tool });
|
|
43
|
-
const listing = ctx.settings['mcp.listing'] === 'token' ? 'token' : 'open';
|
|
44
|
-
return { app: createMcpApp({ ...input.base, tools: registry.tools, listing }) };
|
|
8
|
+
import { callCreate, MOUNTS } from './part-mounts.js';
|
|
9
|
+
export {} from './part-mounts.js';
|
|
10
|
+
// One part in its own image: the part's mount on the kit app.
|
|
11
|
+
const own = (part) => async (input) => {
|
|
12
|
+
const { mount } = await MOUNTS[part](input);
|
|
13
|
+
return { app: createPartApp({ ...input.base, part, mount }) };
|
|
45
14
|
};
|
|
46
15
|
const pages = async (input) => {
|
|
47
16
|
const site = (await callCreate(input, 'pages', input.ctx()));
|
|
@@ -55,27 +24,13 @@ const ui = async (input) => ({
|
|
|
55
24
|
}),
|
|
56
25
|
});
|
|
57
26
|
const worker = async (input) => {
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
const scheduler = createScheduler({ runs, log });
|
|
62
|
-
await callCreate(input, 'worker', {
|
|
63
|
-
...ctx,
|
|
64
|
-
log,
|
|
65
|
-
span: createSpan(log),
|
|
66
|
-
schedule: scheduler.schedule,
|
|
67
|
-
});
|
|
68
|
-
// The same object ctx.health adds to, so a later registration still shows.
|
|
69
|
-
const checks = Object.assign(input.base.checks ?? {}, scheduler.checks());
|
|
70
|
-
scheduler.start();
|
|
71
|
-
return {
|
|
72
|
-
app: createPartApp({ ...input.base, checks, part: 'worker', mount: () => undefined }),
|
|
73
|
-
stop: scheduler.stop,
|
|
74
|
-
};
|
|
27
|
+
const { mount, checks, start, stop } = await MOUNTS.worker(input);
|
|
28
|
+
start?.();
|
|
29
|
+
return { app: createPartApp({ ...input.base, checks, part: 'worker', mount }), stop };
|
|
75
30
|
};
|
|
76
31
|
export const RUNNERS = {
|
|
77
|
-
api,
|
|
78
|
-
mcp,
|
|
32
|
+
api: own('api'),
|
|
33
|
+
mcp: own('mcp'),
|
|
79
34
|
pages,
|
|
80
35
|
ui,
|
|
81
36
|
worker,
|
package/dist/server/app.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type CorsOptions } from 'cors';
|
|
2
|
-
import { type Express } from 'express';
|
|
2
|
+
import { type Express, type RequestHandler } from 'express';
|
|
3
3
|
import { type HelmetOptions } from 'helmet';
|
|
4
4
|
import { type WellKnownEntry } from '../api/well-known.js';
|
|
5
5
|
import { type TokenCheck } from '../auth/token-check.js';
|
|
@@ -23,6 +23,9 @@ export interface PartAppBase {
|
|
|
23
23
|
onExit?: () => void;
|
|
24
24
|
instance?: string;
|
|
25
25
|
startedAt?: number;
|
|
26
|
+
guards?: RequestHandler[];
|
|
27
|
+
parts?: ImagePartKind[];
|
|
28
|
+
clockRate?: number;
|
|
26
29
|
}
|
|
27
30
|
export interface PartAppOptions extends PartAppBase {
|
|
28
31
|
part: ImagePartKind;
|
|
@@ -42,4 +45,5 @@ export interface ApiAppOptions extends PartAppBase {
|
|
|
42
45
|
export declare const PUBLIC_CORS: CorsOptions;
|
|
43
46
|
export declare const BODY_LIMIT = "1mb";
|
|
44
47
|
export declare const createPartApp: (options: PartAppOptions) => Express;
|
|
48
|
+
export declare const mountApiRoutes: (app: Express, options: ApiAppOptions) => void;
|
|
45
49
|
export declare const createApiApp: (options: ApiAppOptions) => Express;
|