@agentage/extension 0.13.0 → 0.14.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/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/host/daemon/add.d.ts +9 -0
- package/dist/host/daemon/add.js +63 -0
- package/dist/host/daemon/child.d.ts +5 -3
- package/dist/host/daemon/child.js +21 -13
- package/dist/host/daemon/daemon-files.d.ts +3 -5
- package/dist/host/daemon/daemon-files.js +15 -11
- 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 +2 -3
- package/dist/host/daemon/daemon.js +53 -31
- package/dist/host/daemon/entries.d.ts +21 -0
- package/dist/host/daemon/entries.js +24 -0
- package/dist/host/daemon/guard.d.ts +1 -1
- package/dist/host/daemon/guard.js +4 -3
- 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/limits.d.ts +11 -0
- package/dist/host/daemon/limits.js +22 -0
- package/dist/host/daemon/ops.d.ts +2 -26
- package/dist/host/daemon/ops.js +43 -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 +46 -24
- package/dist/host/daemon/queue.d.ts +1 -0
- package/dist/host/daemon/queue.js +19 -0
- package/dist/host/daemon/routes.d.ts +5 -3
- package/dist/host/daemon/routes.js +67 -17
- package/dist/host/daemon/stopping.d.ts +10 -0
- package/dist/host/daemon/stopping.js +28 -0
- package/dist/host/daemon/supervisor.d.ts +13 -11
- package/dist/host/daemon/supervisor.js +17 -18
- 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 +1 -0
- package/dist/host/data-lock.js +63 -23
- package/dist/host/host-env.d.ts +2 -1
- package/dist/host/host-env.js +8 -2
- package/dist/host/host-extension.d.ts +4 -0
- package/dist/host/host-extension.js +11 -6
- package/dist/host/host-store.d.ts +8 -1
- package/dist/host/host-store.js +24 -10
- package/dist/host/index.d.ts +4 -0
- package/dist/host/index.js +4 -0
- package/dist/host/serve-host.d.ts +2 -1
- package/dist/host/serve-host.js +12 -9
- package/dist/server/app.d.ts +2 -1
- package/dist/server/app.js +1 -1
- 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/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 +12 -5
- package/dist/tool/index.d.ts +2 -0
- package/dist/tool/index.js +2 -0
- package/dist/tool/kit-version.d.ts +1 -0
- package/dist/tool/kit-version.js +16 -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/package.json +1 -1
- package/schemas/extension-discovery.schema.json +8 -1
- package/schemas/extension-record.schema.json +8 -1
- 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
package/dist/host/host-env.js
CHANGED
|
@@ -5,13 +5,17 @@ import { readOpenapi } from '../discovery/openapi.js';
|
|
|
5
5
|
import {} from '../log/log.js';
|
|
6
6
|
import {} from '../manifest/manifest.schema.js';
|
|
7
7
|
import { portFromEnv } from '../runtime/build-info.js';
|
|
8
|
+
import { GRACE_MS } from '../server/serve.js';
|
|
9
|
+
import { CONTAINER_PORT } from '../tool/container-recipe.js';
|
|
8
10
|
import {} from './host-extension.js';
|
|
9
|
-
|
|
11
|
+
import { STORE_OPEN_MS } from './host-store.js';
|
|
10
12
|
export const BIND_REFUSED = 'AGENTAGE_HOST_BIND=all needs a token check (AGENTAGE_ISSUER, AGENTAGE_AUDIENCE, AGENTAGE_ORIGIN) or AGENTAGE_HOST_AUTH=off';
|
|
11
13
|
const TOKEN_ENV = ['AGENTAGE_ISSUER', 'AGENTAGE_AUDIENCE', 'AGENTAGE_ORIGIN'];
|
|
14
|
+
// A limit: a positive integer of ms, else the default.
|
|
15
|
+
const msOf = (value = '', fallback) => /^[1-9][0-9]*$/.test(value) ? Number(value) : fallback;
|
|
12
16
|
// The entry's env with its defaults: port 8080, the current folder, loopback.
|
|
13
17
|
export const hostEnvOf = (env) => {
|
|
14
|
-
const port = portFromEnv(env,
|
|
18
|
+
const port = portFromEnv(env, CONTAINER_PORT);
|
|
15
19
|
return {
|
|
16
20
|
dir: env['AGENTAGE_EXTENSION_DIR'] || process.cwd(),
|
|
17
21
|
port,
|
|
@@ -20,6 +24,8 @@ export const hostEnvOf = (env) => {
|
|
|
20
24
|
settings: env['AGENTAGE_SETTINGS'] ? parseObject(env['AGENTAGE_SETTINGS']) : {},
|
|
21
25
|
secretsDir: env['AGENTAGE_SECRETS_DIR'] || SECRETS_DIR,
|
|
22
26
|
origin: env['AGENTAGE_ORIGIN'] || `http://127.0.0.1${port ? `:${port}` : ''}`,
|
|
27
|
+
drainMs: msOf(env['AGENTAGE_DRAIN_MS'], GRACE_MS),
|
|
28
|
+
storeOpenMs: msOf(env['AGENTAGE_STORE_OPEN_MS'], STORE_OPEN_MS),
|
|
23
29
|
};
|
|
24
30
|
};
|
|
25
31
|
// Loopback: no token check. Every address: the cloud part's check, or an explicit AGENTAGE_HOST_AUTH=off.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Express } from 'express';
|
|
2
|
+
import { type BuildInfo } from '../discovery/discovery.js';
|
|
2
3
|
import { type HealthBody } from '../health/health.schema.js';
|
|
3
4
|
import { type Log } from '../log/log.js';
|
|
4
5
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
@@ -9,6 +10,8 @@ export interface HostOptions {
|
|
|
9
10
|
secrets?: Record<string, string>;
|
|
10
11
|
onExit?: () => void;
|
|
11
12
|
origin?: string;
|
|
13
|
+
localOnly?: boolean;
|
|
14
|
+
storeOpenMs?: number;
|
|
12
15
|
}
|
|
13
16
|
export interface HostedExtension {
|
|
14
17
|
id: string;
|
|
@@ -20,6 +23,7 @@ export interface HostedExtension {
|
|
|
20
23
|
export interface HostExtras {
|
|
21
24
|
secretsDir?: string;
|
|
22
25
|
guard?: (pkg: ExtensionPackage, log: Log) => Promise<Partial<PartAppBase>>;
|
|
26
|
+
build?: BuildInfo;
|
|
23
27
|
}
|
|
24
28
|
export declare const hostWith: (dir: string, opts: HostOptions, extras: HostExtras) => Promise<HostedExtension>;
|
|
25
29
|
export declare const hostExtension: (dir: string, opts: HostOptions) => Promise<HostedExtension>;
|
|
@@ -3,6 +3,7 @@ import {} from 'express';
|
|
|
3
3
|
import { createContext } from '../context/context.js';
|
|
4
4
|
import { readSecrets, secretsFrom } from '../context/secrets.js';
|
|
5
5
|
import { settingsOf } from '../context/settings.js';
|
|
6
|
+
import {} from '../discovery/discovery.js';
|
|
6
7
|
import { readOpenapi } from '../discovery/openapi.js';
|
|
7
8
|
import { healthBody } from '../health/health.js';
|
|
8
9
|
import {} from '../health/health.schema.js';
|
|
@@ -15,7 +16,8 @@ import { RUNNERS } from '../runtime/part-runners.js';
|
|
|
15
16
|
import { createPartApp } from '../server/app.js';
|
|
16
17
|
import { storeCheck } from '../store/store.js';
|
|
17
18
|
import {} from '../store/pglite-store.js';
|
|
18
|
-
import {
|
|
19
|
+
import { localOnly } from './daemon/guard.js';
|
|
20
|
+
import { openHostStore, STORE_OPEN_MS } from './host-store.js';
|
|
19
21
|
const once = (fn) => {
|
|
20
22
|
let done = false;
|
|
21
23
|
return () => {
|
|
@@ -33,7 +35,7 @@ const contextOf = async (h, checks) => createContext({
|
|
|
33
35
|
pkg: h.pkg,
|
|
34
36
|
part: 'api',
|
|
35
37
|
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
36
|
-
commit:
|
|
38
|
+
commit: h.build.commit,
|
|
37
39
|
log: h.log,
|
|
38
40
|
settings: settingsOf(h.pkg, h.opts.settings ?? {}, h.log),
|
|
39
41
|
store: h.store,
|
|
@@ -54,8 +56,9 @@ const baseOf = async (h) => {
|
|
|
54
56
|
const guard = (await h.extras.guard?.(h.pkg, h.log)) ?? {};
|
|
55
57
|
const checks = { ...(h.store && { store: storeCheck(h.store) }), ...guard.checks };
|
|
56
58
|
const resolved = { instance: randomBytes(4).toString('hex'), startedAt: Date.now() };
|
|
57
|
-
const
|
|
58
|
-
|
|
59
|
+
const guards = h.opts.localOnly ? [localOnly()] : undefined;
|
|
60
|
+
const { pkg, log, build, onExit } = h;
|
|
61
|
+
return { pkg, log, build, onExit, guards, ...guard, checks, ...resolved };
|
|
59
62
|
};
|
|
60
63
|
const hostedOf = (h, app, base) => {
|
|
61
64
|
const { id } = h.pkg.agentage;
|
|
@@ -70,9 +73,11 @@ const hostedOf = (h, app, base) => {
|
|
|
70
73
|
export const hostWith = async (dir, opts, extras) => {
|
|
71
74
|
const pkg = await readManifest(dir);
|
|
72
75
|
const log = createLog({ extension: pkg.agentage.id, part: 'api', version: pkg.version });
|
|
73
|
-
const
|
|
76
|
+
const openMs = opts.storeOpenMs ?? STORE_OPEN_MS;
|
|
77
|
+
const store = await openHostStore(pkg, { dir, dataDir: opts.dataDir, openMs }, log);
|
|
74
78
|
const onExit = once(opts.onExit ?? (() => undefined));
|
|
75
|
-
const
|
|
79
|
+
const build = extras.build ?? buildInfoFromEnv({});
|
|
80
|
+
const h = { dir, pkg, log, opts, extras, store, onExit, build };
|
|
76
81
|
try {
|
|
77
82
|
const base = await baseOf(h);
|
|
78
83
|
return hostedOf(h, await appOf(h, base), base);
|
|
@@ -1,4 +1,11 @@
|
|
|
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
|
+
interface StoreAt {
|
|
6
|
+
dir: string;
|
|
7
|
+
dataDir: string;
|
|
8
|
+
openMs: number;
|
|
9
|
+
}
|
|
10
|
+
export declare const openHostStore: (pkg: ExtensionPackage, at: StoreAt, log: Log) => Promise<PgliteStore | undefined>;
|
|
11
|
+
export {};
|
package/dist/host/host-store.js
CHANGED
|
@@ -6,8 +6,21 @@ import { migrate } from '../store/migrations.js';
|
|
|
6
6
|
import { createPgliteStore } from '../store/pglite-store.js';
|
|
7
7
|
import { lockDataDir } from './data-lock.js';
|
|
8
8
|
const unlocked = async () => undefined;
|
|
9
|
-
//
|
|
10
|
-
const
|
|
9
|
+
// How long the host waits for the store to open (and to close); the daemon sets its healthMs.
|
|
10
|
+
export const STORE_OPEN_MS = 120_000;
|
|
11
|
+
const within = (work, ms) => Promise.race([
|
|
12
|
+
work,
|
|
13
|
+
new Promise((resolve) => setTimeout(() => resolve('late'), ms).unref()),
|
|
14
|
+
]);
|
|
15
|
+
// A close past openMs keeps the data lock: PGlite may still write there until the process exits.
|
|
16
|
+
const closer = (store, release, openMs) => async () => {
|
|
17
|
+
const closed = await within(store.close().catch(() => undefined), openMs);
|
|
18
|
+
if (closed !== 'late')
|
|
19
|
+
await release();
|
|
20
|
+
};
|
|
21
|
+
// Locks dataDir, then opens PGlite in it and migrates; past openMs the part starts, /health says not ready.
|
|
22
|
+
const open = async (at, log) => {
|
|
23
|
+
const { dir, dataDir, openMs } = at;
|
|
11
24
|
if (dataDir)
|
|
12
25
|
await mkdir(dataDir, { recursive: true });
|
|
13
26
|
const release = dataDir ? await lockDataDir(dataDir) : unlocked;
|
|
@@ -15,21 +28,22 @@ const open = async (dir, dataDir, log) => {
|
|
|
15
28
|
dataDir: dataDir || undefined,
|
|
16
29
|
from: dir,
|
|
17
30
|
});
|
|
18
|
-
const close =
|
|
19
|
-
|
|
20
|
-
await release();
|
|
21
|
-
};
|
|
22
|
-
await store.query('select 1').catch(async (err) => {
|
|
31
|
+
const close = closer(store, release, openMs);
|
|
32
|
+
const opened = store.query('select 1').catch(async (err) => {
|
|
23
33
|
await close();
|
|
24
34
|
throw err;
|
|
25
35
|
});
|
|
36
|
+
if ((await within(opened, openMs)) === 'late') {
|
|
37
|
+
opened.catch(() => undefined);
|
|
38
|
+
log.warn('the store did not open in time: /health says unavailable until it does', { openMs });
|
|
39
|
+
}
|
|
26
40
|
return { query: store.query, close };
|
|
27
41
|
};
|
|
28
42
|
// A postgres store as PGlite with its files in dataDir; none set keeps it in memory.
|
|
29
|
-
export const openHostStore = async (
|
|
43
|
+
export const openHostStore = async (pkg, at, log) => {
|
|
30
44
|
if (!hasStore(pkg, 'postgres'))
|
|
31
45
|
return undefined;
|
|
32
|
-
if (!dataDir)
|
|
46
|
+
if (!at.dataDir)
|
|
33
47
|
log.warn('no data folder: the store is in memory and lost on exit');
|
|
34
|
-
return open(
|
|
48
|
+
return open(at, log);
|
|
35
49
|
};
|
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';
|
|
@@ -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,13 @@
|
|
|
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';
|
|
6
7
|
import { guardOf, hostEnvOf } from './host-env.js';
|
|
7
8
|
import { hostWith } from './host-extension.js';
|
|
8
9
|
export const ORPHAN_MS = 10_000;
|
|
9
|
-
// The port to the daemon;
|
|
10
|
-
// within ORPHAN_MS even with a request that never ends.
|
|
10
|
+
// The port to the daemon; its IPC channel closed: drain, and exit within ORPHAN_MS in any case.
|
|
11
11
|
export const announce = (port, stop, exit = process.exit) => {
|
|
12
12
|
if (!process.send)
|
|
13
13
|
return;
|
|
@@ -30,16 +30,19 @@ const exitHook = () => {
|
|
|
30
30
|
return { onExit, arm };
|
|
31
31
|
};
|
|
32
32
|
// SIGTERM, ctx.exit() and a gone parent drain the server, then stop the host and exit.
|
|
33
|
-
const drainOn = (server, host, deps) => {
|
|
33
|
+
const drainOn = (server, host, deps, graceMs) => {
|
|
34
34
|
const log = createLog({ extension: host.id, part: 'api', version: host.version });
|
|
35
35
|
const exit = deps.exit ?? ((code) => process.exit(code));
|
|
36
|
-
|
|
36
|
+
// A store close that hangs gets graceMs too, so the process always exits.
|
|
37
|
+
const closed = () => Promise.race([host.stop(), sleep(graceMs, undefined, { ref: false })]);
|
|
38
|
+
const done = (code) => void closed().finally(() => exit(code));
|
|
37
39
|
const stop = shutdownOn(server, {
|
|
38
40
|
app: host.app,
|
|
39
41
|
port: 0,
|
|
40
42
|
log,
|
|
41
43
|
exit: done,
|
|
42
44
|
signals: deps.signals,
|
|
45
|
+
graceMs,
|
|
43
46
|
});
|
|
44
47
|
return { log, stop };
|
|
45
48
|
};
|
|
@@ -48,11 +51,11 @@ export const serveHost = async (env, deps = {}) => {
|
|
|
48
51
|
const at = hostEnvOf(env);
|
|
49
52
|
const guard = guardOf(env, at);
|
|
50
53
|
const hook = exitHook();
|
|
51
|
-
const { dataDir, settings, origin, secretsDir } = at;
|
|
52
|
-
const opts = { dataDir, settings, origin, onExit: hook.onExit };
|
|
53
|
-
const host = await hostWith(at.dir, opts, { secretsDir, guard });
|
|
54
|
+
const { dataDir, settings, origin, secretsDir, storeOpenMs } = at;
|
|
55
|
+
const opts = { dataDir, settings, origin, storeOpenMs, onExit: hook.onExit };
|
|
56
|
+
const host = await hostWith(at.dir, opts, { secretsDir, guard, build: buildInfoFromEnv(env) });
|
|
54
57
|
const server = await listen(host.app, at.port, at.bind);
|
|
55
|
-
const { log, stop } = drainOn(server, host, deps);
|
|
58
|
+
const { log, stop } = drainOn(server, host, deps, at.drainMs);
|
|
56
59
|
const { port } = server.address();
|
|
57
60
|
log.info('listening', { port, bind: at.bind });
|
|
58
61
|
(deps.ready ?? announce)(port, stop);
|
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,7 @@ export interface PartAppBase {
|
|
|
23
23
|
onExit?: () => void;
|
|
24
24
|
instance?: string;
|
|
25
25
|
startedAt?: number;
|
|
26
|
+
guards?: RequestHandler[];
|
|
26
27
|
}
|
|
27
28
|
export interface PartAppOptions extends PartAppBase {
|
|
28
29
|
part: ImagePartKind;
|
package/dist/server/app.js
CHANGED
|
@@ -58,7 +58,7 @@ export const createPartApp = (options) => {
|
|
|
58
58
|
const app = express();
|
|
59
59
|
// The gateway calls parts over a private network; its X-Forwarded-Proto then sets req.protocol.
|
|
60
60
|
app.set('trust proxy', 'loopback, linklocal, uniquelocal');
|
|
61
|
-
app.use(dropCookie, agcidMiddleware, helmet(options.helmet));
|
|
61
|
+
app.use(dropCookie, agcidMiddleware, helmet(options.helmet), ...(options.guards ?? []));
|
|
62
62
|
app.get('/health', healthHandler(options));
|
|
63
63
|
// After /health: it is probed every few seconds and never gets a request line.
|
|
64
64
|
app.use(requestLine(options.log, options.onExit));
|
package/dist/server/serve.d.ts
CHANGED
|
@@ -7,7 +7,10 @@ export interface ServeOptions {
|
|
|
7
7
|
log: Log;
|
|
8
8
|
exit?: (code: number) => void;
|
|
9
9
|
signals?: NodeJS.Signals[];
|
|
10
|
+
graceMs?: number;
|
|
10
11
|
}
|
|
12
|
+
export declare const GRACE_MS = 10000;
|
|
11
13
|
export declare const listen: (app: Express, port: number, host?: string) => Promise<Server>;
|
|
14
|
+
export declare const closeNow: (server: Server) => Promise<void>;
|
|
12
15
|
export declare const shutdownOn: (server: Server, options: ServeOptions) => (() => void);
|
|
13
16
|
export declare const serve: (options: ServeOptions) => Promise<Server>;
|
package/dist/server/serve.js
CHANGED
|
@@ -2,20 +2,25 @@ import {} from 'node:http';
|
|
|
2
2
|
import {} from 'node:net';
|
|
3
3
|
import {} from 'express';
|
|
4
4
|
import {} from '../log/log.js';
|
|
5
|
-
const GRACE_MS = 10_000;
|
|
5
|
+
export const GRACE_MS = 10_000;
|
|
6
6
|
export const listen = (app, port, host) => new Promise((resolve, reject) => {
|
|
7
7
|
const done = (err) => (err ? reject(err) : resolve(server));
|
|
8
8
|
const server = host === undefined ? app.listen(port, done) : app.listen(port, host, done);
|
|
9
9
|
});
|
|
10
|
+
// At once: open and idle connections end now; a drain is shutdownOn.
|
|
11
|
+
export const closeNow = (server) => new Promise((resolve) => {
|
|
12
|
+
server.close(() => resolve());
|
|
13
|
+
server.closeAllConnections();
|
|
14
|
+
});
|
|
10
15
|
// Open requests are answered; a keep-alive connection is closed once idle, not at its timeout.
|
|
11
|
-
const drain = (server, exit) => {
|
|
16
|
+
const drain = (server, exit, graceMs) => {
|
|
12
17
|
const idle = setInterval(() => server.closeIdleConnections(), 100).unref();
|
|
13
18
|
server.close((err) => {
|
|
14
19
|
clearInterval(idle);
|
|
15
20
|
exit(err ? 1 : 0);
|
|
16
21
|
});
|
|
17
22
|
server.closeIdleConnections();
|
|
18
|
-
setTimeout(() => exit(1),
|
|
23
|
+
setTimeout(() => exit(1), graceMs).unref();
|
|
19
24
|
};
|
|
20
25
|
// SIGTERM drains; a close error or a drain past the grace period exits 1; a second Ctrl-C at once.
|
|
21
26
|
export const shutdownOn = (server, options) => {
|
|
@@ -34,7 +39,7 @@ export const shutdownOn = (server, options) => {
|
|
|
34
39
|
return;
|
|
35
40
|
stopping = true;
|
|
36
41
|
options.log.info('shutting down');
|
|
37
|
-
drain(server, exit);
|
|
42
|
+
drain(server, exit, options.graceMs ?? GRACE_MS);
|
|
38
43
|
};
|
|
39
44
|
for (const signal of options.signals ?? ['SIGTERM', 'SIGINT'])
|
|
40
45
|
process.on(signal, () => stop(signal));
|
package/dist/tool/build.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { type ExtensionRecord } from '../discovery/discovery.schema.js';
|
|
|
3
3
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
4
4
|
import { type ImagePartKind } from '../manifest/primitives.schema.js';
|
|
5
5
|
import { type Env } from '../runtime/build-info.js';
|
|
6
|
+
import { type BuiltContainer } from './container.js';
|
|
6
7
|
import { type Docker } from './docker.js';
|
|
7
8
|
import { type Run } from './exec.js';
|
|
8
9
|
export declare const DEFAULT_REGISTRY = "ghcr.io/agentage";
|
|
@@ -10,6 +11,7 @@ export interface BuildOptions {
|
|
|
10
11
|
dir?: string;
|
|
11
12
|
push?: boolean;
|
|
12
13
|
registry?: string;
|
|
14
|
+
container?: boolean;
|
|
13
15
|
env?: Env;
|
|
14
16
|
}
|
|
15
17
|
export interface BuildDeps {
|
|
@@ -29,6 +31,7 @@ export interface BuildResult {
|
|
|
29
31
|
commit: string;
|
|
30
32
|
buildTime: string;
|
|
31
33
|
images: BuiltImage[];
|
|
34
|
+
container?: BuiltContainer;
|
|
32
35
|
recordFile?: string;
|
|
33
36
|
}
|
|
34
37
|
export declare const imageTag: (registry: string, pkg: ExtensionPackage, part: ImagePartKind) => string;
|
package/dist/tool/build.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { writeFile } from 'node:fs/promises';
|
|
2
2
|
import { join, resolve } from 'node:path';
|
|
3
3
|
import { recordOf, validateRecord } from '../discovery/discovery.js';
|
|
4
4
|
import {} from '../discovery/discovery.schema.js';
|
|
@@ -9,7 +9,9 @@ import { readManifest } from '../manifest/read-manifest.js';
|
|
|
9
9
|
import {} from '../runtime/build-info.js';
|
|
10
10
|
import { resolveBuildInfo } from './build-info.js';
|
|
11
11
|
import { assertConforms } from './check.js';
|
|
12
|
-
import {
|
|
12
|
+
import { containerParts } from './container-parts.js';
|
|
13
|
+
import { buildContainer, CONTAINER_RECORD_FILE } from './container.js';
|
|
14
|
+
import { buildStaged, createDocker, pushByDigest } from './docker.js';
|
|
13
15
|
import { mustRun, runCommand } from './exec.js';
|
|
14
16
|
import { labelsOf } from './labels.js';
|
|
15
17
|
import { recordExtras } from './record-extras.js';
|
|
@@ -24,24 +26,16 @@ const compile = async (dir, pkg, run) => {
|
|
|
24
26
|
};
|
|
25
27
|
const buildImage = async (job, docker, tmpRoot) => {
|
|
26
28
|
const context = await stageContext(job.dir, job.pkg, job.part, tmpRoot);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
IMAGE: job.tag,
|
|
32
|
-
};
|
|
33
|
-
const labels = labelsOf(job.pkg, job.part, job.info);
|
|
34
|
-
await docker.build({ context, tag: job.tag, buildArgs, labels });
|
|
35
|
-
}
|
|
36
|
-
finally {
|
|
37
|
-
await rm(context, { recursive: true, force: true });
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
const pushImage = async (job, docker) => {
|
|
41
|
-
const digest = await docker.push(job.tag);
|
|
42
|
-
const repository = job.tag.slice(0, job.tag.lastIndexOf(':'));
|
|
43
|
-
return { part: job.part, tag: job.tag, digest, image: `${repository}@${digest}` };
|
|
29
|
+
const { commit, buildTime } = job.info;
|
|
30
|
+
const buildArgs = { COMMIT_SHA: commit, BUILD_TIME: buildTime, IMAGE: job.tag };
|
|
31
|
+
const labels = labelsOf(job.pkg, job.part, job.info);
|
|
32
|
+
await buildStaged(docker, { context, tag: job.tag, buildArgs, labels });
|
|
44
33
|
};
|
|
34
|
+
const pushImage = async (job, docker) => ({
|
|
35
|
+
part: job.part,
|
|
36
|
+
tag: job.tag,
|
|
37
|
+
...(await pushByDigest(docker, job.tag)),
|
|
38
|
+
});
|
|
45
39
|
// The record from the code and the images by digest; `up` writes the same shape from local digests.
|
|
46
40
|
export const recordFor = async (dir, pkg, info, images) => {
|
|
47
41
|
const byPart = Object.fromEntries(images.map((built) => [built.part, built.image]));
|
|
@@ -51,9 +45,12 @@ const writeRecord = async (dir, record, file = join(dir, 'dist', RECORD_FILE)) =
|
|
|
51
45
|
await writeFile(file, `${JSON.stringify(record, null, 2)}\n`);
|
|
52
46
|
return file;
|
|
53
47
|
};
|
|
48
|
+
const targetOf = (options, deps) => ({
|
|
49
|
+
docker: deps.docker ?? createDocker(deps.run),
|
|
50
|
+
registry: options.registry ?? DEFAULT_REGISTRY,
|
|
51
|
+
});
|
|
54
52
|
const buildParts = async (base, options, deps) => {
|
|
55
|
-
const docker
|
|
56
|
-
const registry = options.registry ?? DEFAULT_REGISTRY;
|
|
53
|
+
const { docker, registry } = targetOf(options, deps);
|
|
57
54
|
const images = [];
|
|
58
55
|
for (const part of imageParts(base.pkg)) {
|
|
59
56
|
const job = { ...base, part, tag: imageTag(registry, base.pkg, part) };
|
|
@@ -62,16 +59,34 @@ const buildParts = async (base, options, deps) => {
|
|
|
62
59
|
}
|
|
63
60
|
return images;
|
|
64
61
|
};
|
|
65
|
-
//
|
|
62
|
+
// --container: one image; with --push also dist/agentage-container.json.
|
|
63
|
+
const containerOf = async (base, parts, opts, deps) => {
|
|
64
|
+
const { docker, registry } = targetOf(opts, deps);
|
|
65
|
+
const job = { ...base, parts, registry, push: opts.push, tmpRoot: deps.tmpRoot };
|
|
66
|
+
const { container, record } = await buildContainer(job, docker);
|
|
67
|
+
if (!record)
|
|
68
|
+
return { images: [], container };
|
|
69
|
+
const file = join(base.dir, 'dist', CONTAINER_RECORD_FILE);
|
|
70
|
+
return { images: [], container, recordFile: await writeRecord(base.dir, record, file) };
|
|
71
|
+
};
|
|
72
|
+
const partImages = async (base, options, deps) => {
|
|
73
|
+
const images = await buildParts(base, options, deps);
|
|
74
|
+
if (!options.push)
|
|
75
|
+
return { images };
|
|
76
|
+
const record = await recordFor(base.dir, base.pkg, base.info, images);
|
|
77
|
+
return { images, recordFile: await writeRecord(base.dir, record) };
|
|
78
|
+
};
|
|
79
|
+
// Validate, compile, check, one image per part (or the one image); --push pushes, writes the record.
|
|
66
80
|
export const build = async (options = {}, deps = { run: runCommand }) => {
|
|
67
81
|
const dir = resolve(options.dir ?? '.');
|
|
68
82
|
const pkg = await readManifest(dir);
|
|
83
|
+
const parts = options.container ? containerParts(pkg) : undefined;
|
|
69
84
|
const info = await resolveBuildInfo(dir, options.env ?? process.env, deps.run);
|
|
70
85
|
await compile(dir, pkg, deps.run);
|
|
71
86
|
await assertConforms(dir, { run: deps.run });
|
|
72
|
-
const
|
|
73
|
-
const
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
return {
|
|
87
|
+
const base = { dir, pkg, info };
|
|
88
|
+
const built = parts
|
|
89
|
+
? await containerOf(base, parts, options, deps)
|
|
90
|
+
: await partImages(base, options, deps);
|
|
91
|
+
return { id: pkg.agentage.id, version: pkg.version, ...info, ...built };
|
|
77
92
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const capabilities: readonly ["container", "localOnly", "installDisabled", "updateInstallation", "withFileLock"];
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
2
|
+
export interface ContainerParts {
|
|
3
|
+
runs: string[];
|
|
4
|
+
skipped: string[];
|
|
5
|
+
}
|
|
6
|
+
export declare const NO_API = "build --container needs an api part: the one image runs only the api part today";
|
|
7
|
+
export declare const containerParts: (pkg: ExtensionPackage) => ContainerParts;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import {} from '../manifest/manifest.schema.js';
|
|
2
|
+
import { imageParts } from '../manifest/parts.js';
|
|
3
|
+
export const NO_API = 'build --container needs an api part: the one image runs only the api part today';
|
|
4
|
+
// The kit host (host/host-extension.ts) serves the api part; other image parts come later.
|
|
5
|
+
export const containerParts = (pkg) => {
|
|
6
|
+
const parts = imageParts(pkg);
|
|
7
|
+
if (!parts.includes('api'))
|
|
8
|
+
throw new Error(NO_API);
|
|
9
|
+
return { runs: ['api'], skipped: parts.filter((kind) => kind !== 'api') };
|
|
10
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export const CONTAINER_PORT = 8080;
|
|
2
|
+
// The volume holds the store folder, so its <dir>.lock sibling persists with it.
|
|
3
|
+
const DATA_VOLUME = '/data';
|
|
4
|
+
const STORE_DIR = `${DATA_VOLUME}/store`;
|
|
5
|
+
const HEALTH_PROBE = `fetch('http://127.0.0.1:'+(process.env.PORT||${CONTAINER_PORT})+'/health')` +
|
|
6
|
+
'.then((r) => process.exit(r.status === 200 || r.status === 503 ? 0 : 1), () => process.exit(1))';
|
|
7
|
+
const DEPS = [
|
|
8
|
+
'# syntax=docker/dockerfile:1',
|
|
9
|
+
'# Generated by @agentage/extension build --container. Extension authors never write a Dockerfile.',
|
|
10
|
+
'FROM node:26-alpine AS deps',
|
|
11
|
+
'WORKDIR /app',
|
|
12
|
+
'COPY deps/ ./',
|
|
13
|
+
'RUN npm ci --omit=dev --no-audit --no-fund',
|
|
14
|
+
'',
|
|
15
|
+
];
|
|
16
|
+
// PGlite is an optional peer and a dev dependency of an extension, so npm ci --omit=dev drops it.
|
|
17
|
+
const pgliteStage = (version) => [
|
|
18
|
+
'FROM node:26-alpine AS pglite',
|
|
19
|
+
'WORKDIR /pglite',
|
|
20
|
+
`RUN npm install --no-audit --no-fund --no-package-lock @electric-sql/pglite@${version}`,
|
|
21
|
+
'',
|
|
22
|
+
];
|
|
23
|
+
const runnerEnv = (store) => [
|
|
24
|
+
'FROM node:26-alpine AS runner',
|
|
25
|
+
'ARG COMMIT_SHA',
|
|
26
|
+
'ARG BUILD_TIME',
|
|
27
|
+
'ENV NODE_ENV=production \\',
|
|
28
|
+
` PORT=${CONTAINER_PORT} \\`,
|
|
29
|
+
' AGENTAGE_HOST_BIND=all \\',
|
|
30
|
+
' AGENTAGE_DRAIN_MS=8000 \\',
|
|
31
|
+
...(store ? [` AGENTAGE_DATA_DIR=${STORE_DIR} \\`] : []),
|
|
32
|
+
' COMMIT_SHA=$COMMIT_SHA \\',
|
|
33
|
+
' BUILD_TIME=$BUILD_TIME',
|
|
34
|
+
];
|
|
35
|
+
const runner = (store) => [
|
|
36
|
+
...runnerEnv(store),
|
|
37
|
+
'WORKDIR /app',
|
|
38
|
+
'COPY --from=deps /app/node_modules ./node_modules',
|
|
39
|
+
...(store
|
|
40
|
+
? [
|
|
41
|
+
'COPY --from=pglite /pglite/node_modules/@electric-sql/pglite ./node_modules/@electric-sql/pglite',
|
|
42
|
+
]
|
|
43
|
+
: []),
|
|
44
|
+
'COPY app/ ./',
|
|
45
|
+
...(store
|
|
46
|
+
? [`RUN mkdir -p ${DATA_VOLUME} && chown node:node ${DATA_VOLUME}`, `VOLUME ${DATA_VOLUME}`]
|
|
47
|
+
: []),
|
|
48
|
+
'USER node',
|
|
49
|
+
`EXPOSE ${CONTAINER_PORT}`,
|
|
50
|
+
`HEALTHCHECK --interval=10s --timeout=3s --start-period=15s --retries=3 CMD ["node", "-e", "${HEALTH_PROBE}"]`,
|
|
51
|
+
'CMD ["node", "node_modules/@agentage/extension/dist/host/main.js"]',
|
|
52
|
+
'',
|
|
53
|
+
];
|
|
54
|
+
// The one image of an extension: the kit host in one process; pglite is set for a postgres store.
|
|
55
|
+
export const containerRecipe = ({ pglite }) => [...DEPS, ...(pglite ? pgliteStage(pglite) : []), ...runner(pglite !== undefined)].join('\n');
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
2
|
+
export declare const pgliteVersion: (dir: string, kitCopy?: (from?: string) => string | undefined) => Promise<string>;
|
|
3
|
+
export declare const stageContainer: (dir: string, pkg: ExtensionPackage, tmpRoot?: string) => Promise<string>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { cp, mkdir, mkdtemp, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { basename, dirname, join } from 'node:path';
|
|
4
|
+
import { OPENAPI_FILE } from '../discovery/openapi.js';
|
|
5
|
+
import {} from '../manifest/manifest.schema.js';
|
|
6
|
+
import { hasStore, imageParts, migratesStore } from '../manifest/parts.js';
|
|
7
|
+
import { containerRecipe } from './container-recipe.js';
|
|
8
|
+
import { kitPgliteVersion } from './kit-version.js';
|
|
9
|
+
import { assertBuilt, copyDeps, exists } from './stage.js';
|
|
10
|
+
const PGLITE_KEY = 'node_modules/@electric-sql/pglite';
|
|
11
|
+
// An exact semver, nothing else: the value goes into a RUN line.
|
|
12
|
+
const EXACT_VERSION = /^\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?$/;
|
|
13
|
+
// The PGlite the package tests with (its lock), else the exact copy the kit resolves; never a range.
|
|
14
|
+
export const pgliteVersion = async (dir, kitCopy = kitPgliteVersion) => {
|
|
15
|
+
const lock = JSON.parse(await readFile(join(dir, 'package-lock.json'), 'utf8'));
|
|
16
|
+
const version = lock.packages?.[PGLITE_KEY]?.version ?? kitCopy();
|
|
17
|
+
if (version === undefined)
|
|
18
|
+
throw new Error('no PGlite version: npm install -D @electric-sql/pglite in the package');
|
|
19
|
+
if (typeof version !== 'string' || !EXACT_VERSION.test(version))
|
|
20
|
+
throw new Error(`PGlite version is not an exact semver: ${JSON.stringify(version)}`);
|
|
21
|
+
return version;
|
|
22
|
+
};
|
|
23
|
+
// Compiled tests, source maps and earlier build records (dist/agentage-*.json) never ship.
|
|
24
|
+
const shippedFrom = (dir) => (path) => {
|
|
25
|
+
const name = basename(path);
|
|
26
|
+
const record = dirname(path) === join(dir, 'dist') && /^agentage-.*\.json$/.test(name);
|
|
27
|
+
return !record && !/\.test\.|\.map$/.test(name);
|
|
28
|
+
};
|
|
29
|
+
// The one image carries all of dist/, the OpenAPI file and the migrations; seeds never ship.
|
|
30
|
+
const containerPaths = async (dir, pkg) => {
|
|
31
|
+
const paths = ['package.json', 'dist'];
|
|
32
|
+
if (await exists(join(dir, OPENAPI_FILE)))
|
|
33
|
+
paths.push(OPENAPI_FILE);
|
|
34
|
+
if (migratesStore(pkg) && (await exists(join(dir, 'migrations'))))
|
|
35
|
+
paths.push('migrations');
|
|
36
|
+
return paths;
|
|
37
|
+
};
|
|
38
|
+
// A clean build context for the one image: deps/ (npm ci), app/ (the package) and the Dockerfile.
|
|
39
|
+
export const stageContainer = async (dir, pkg, tmpRoot = tmpdir()) => {
|
|
40
|
+
for (const part of imageParts(pkg))
|
|
41
|
+
await assertBuilt(dir, pkg, part);
|
|
42
|
+
const root = await mkdtemp(join(tmpRoot, `agentage-${pkg.agentage.id}-container-`));
|
|
43
|
+
await copyDeps(dir, root, pkg);
|
|
44
|
+
await mkdir(join(root, 'app'));
|
|
45
|
+
const filter = shippedFrom(dir);
|
|
46
|
+
for (const path of await containerPaths(dir, pkg))
|
|
47
|
+
await cp(join(dir, path), join(root, 'app', path), { recursive: true, filter });
|
|
48
|
+
const pglite = hasStore(pkg, 'postgres') ? await pgliteVersion(dir) : undefined;
|
|
49
|
+
await writeFile(join(root, 'Dockerfile'), containerRecipe({ pglite }));
|
|
50
|
+
return root;
|
|
51
|
+
};
|