@agentage/extension 0.12.3 → 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/api/openapi-doc.js +2 -1
- package/dist/api/routes.js +1 -1
- package/dist/context/context.d.ts +1 -0
- package/dist/context/context.js +3 -3
- package/dist/context/secrets.d.ts +1 -0
- package/dist/context/secrets.js +7 -5
- package/dist/context/settings.d.ts +4 -0
- package/dist/context/settings.js +6 -5
- package/dist/discovery/discovery.d.ts +1 -0
- package/dist/discovery/discovery.schema.d.ts +2 -0
- package/dist/discovery/discovery.schema.js +30 -12
- package/dist/discovery/openapi.d.ts +2 -1
- package/dist/discovery/openapi.js +3 -1
- package/dist/host/daemon/add.d.ts +9 -0
- package/dist/host/daemon/add.js +63 -0
- package/dist/host/daemon/child.d.ts +23 -0
- package/dist/host/daemon/child.js +100 -0
- package/dist/host/daemon/daemon-files.d.ts +17 -0
- package/dist/host/daemon/daemon-files.js +27 -0
- 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 +17 -0
- package/dist/host/daemon/daemon.js +106 -0
- package/dist/host/daemon/entries.d.ts +21 -0
- package/dist/host/daemon/entries.js +24 -0
- package/dist/host/daemon/guard.d.ts +2 -0
- package/dist/host/daemon/guard.js +16 -0
- package/dist/host/daemon/installation.d.ts +36 -0
- package/dist/host/daemon/installation.js +92 -0
- package/dist/host/daemon/installer-call.d.ts +21 -0
- package/dist/host/daemon/installer-call.js +48 -0
- package/dist/host/daemon/json-file.d.ts +4 -0
- package/dist/host/daemon/json-file.js +62 -0
- package/dist/host/daemon/lane.d.ts +9 -0
- package/dist/host/daemon/lane.js +24 -0
- package/dist/host/daemon/limits.d.ts +11 -0
- package/dist/host/daemon/limits.js +22 -0
- package/dist/host/daemon/ops.d.ts +12 -0
- package/dist/host/daemon/ops.js +89 -0
- 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 +6 -0
- package/dist/host/daemon/proxy.js +89 -0
- package/dist/host/daemon/queue.d.ts +3 -0
- package/dist/host/daemon/queue.js +27 -0
- package/dist/host/daemon/routes.d.ts +13 -0
- package/dist/host/daemon/routes.js +98 -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 +54 -0
- package/dist/host/daemon/supervisor.js +115 -0
- 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 -0
- package/dist/host/data-lock.js +121 -0
- package/dist/host/host-env.d.ts +18 -0
- package/dist/host/host-env.js +42 -0
- package/dist/host/host-extension.d.ts +29 -0
- package/dist/host/host-extension.js +91 -0
- package/dist/host/host-store.d.ts +11 -0
- package/dist/host/host-store.js +49 -0
- package/dist/host/index.d.ts +7 -0
- package/dist/host/index.js +7 -0
- package/dist/host/main.d.ts +2 -0
- package/dist/host/main.js +10 -0
- package/dist/host/serve-host.d.ts +17 -0
- package/dist/host/serve-host.js +64 -0
- package/dist/runtime/part-runners.d.ts +1 -0
- package/dist/runtime/part-runners.js +10 -1
- package/dist/runtime/run-part.js +2 -0
- package/dist/server/app.d.ts +6 -1
- package/dist/server/app.js +29 -19
- package/dist/server/request-line.d.ts +1 -1
- package/dist/server/request-line.js +4 -4
- package/dist/server/serve.d.ts +3 -0
- package/dist/server/serve.js +17 -4
- package/dist/store/pglite-store.d.ts +25 -0
- package/dist/store/pglite-store.js +59 -0
- package/dist/store/test-store.d.ts +2 -3
- package/dist/store/test-store.js +3 -38
- package/dist/testing/test-server.d.ts +2 -0
- package/dist/testing/test-server.js +1 -1
- 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/record-extras.js +2 -2
- package/dist/tool/rules/surface.js +6 -6
- package/dist/tool/stage.d.ts +1 -0
- package/dist/tool/stage.js +1 -1
- package/dist/tool/up/gateway.d.ts +6 -2
- package/dist/tool/up/gateway.js +10 -7
- package/dist/tool/up/local-record.d.ts +1 -0
- package/dist/tool/up/token.js +1 -1
- package/dist/tool/up/up.js +2 -2
- package/dist/vendor/logs/events-client.js +1 -1
- package/package.json +5 -1
- package/schemas/extension-discovery.schema.json +8 -1
- package/schemas/extension-record.schema.json +8 -1
package/dist/api/openapi-doc.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { scopeOf } from '../auth/token-check.js';
|
|
2
|
+
import { apiPrefix } from '../discovery/openapi.js';
|
|
2
3
|
import {} from '../envelope/codes.js';
|
|
3
4
|
import { ErrorEnvelopeSchema } from '../envelope/envelope.schema.js';
|
|
4
5
|
import {} from '../manifest/manifest.schema.js';
|
|
@@ -94,7 +95,7 @@ export const generatedOpenapi = (pkg, routes) => {
|
|
|
94
95
|
const { id } = pkg.agentage;
|
|
95
96
|
const paths = {};
|
|
96
97
|
for (const entry of routes) {
|
|
97
|
-
const at =
|
|
98
|
+
const at = `${apiPrefix(id)}${entry.path}`;
|
|
98
99
|
paths[at] = { ...paths[at], [entry.method]: operation(id, entry) };
|
|
99
100
|
}
|
|
100
101
|
return {
|
package/dist/api/routes.js
CHANGED
|
@@ -9,7 +9,7 @@ const problemOf = (routes, { method, path, spec }) => {
|
|
|
9
9
|
if (!ROUTE_METHODS.includes(method))
|
|
10
10
|
return `method ${String(method)}: one of get, post, put, patch, delete`;
|
|
11
11
|
if (!PATH.test(path))
|
|
12
|
-
return 'path: /segments and {param} segments under /api/
|
|
12
|
+
return 'path: /segments and {param} segments under /api/{id}';
|
|
13
13
|
if (routes.some((r) => r.method === method && r.path === path))
|
|
14
14
|
return 'is registered twice';
|
|
15
15
|
if (!spec.summary)
|
package/dist/context/context.js
CHANGED
|
@@ -15,12 +15,12 @@ export const healthRegistrar = (checks) => (name, check) => {
|
|
|
15
15
|
checks[name] = check;
|
|
16
16
|
};
|
|
17
17
|
// Inside a request: after its answer (server/request-line.ts); outside one: now.
|
|
18
|
-
const exitAfterAnswer = () => {
|
|
18
|
+
const exitAfterAnswer = (onExit = sigtermOnce) => () => {
|
|
19
19
|
const scope = requestScope.getStore();
|
|
20
20
|
if (scope)
|
|
21
21
|
scope.exit = true;
|
|
22
22
|
else
|
|
23
|
-
|
|
23
|
+
onExit();
|
|
24
24
|
};
|
|
25
25
|
// ctx.store is always there; without a store part in the manifest it says so on first use.
|
|
26
26
|
export const NO_STORE = {
|
|
@@ -55,7 +55,7 @@ export const createContext = (input) => {
|
|
|
55
55
|
store: store ?? NO_STORE,
|
|
56
56
|
secrets,
|
|
57
57
|
platform,
|
|
58
|
-
exit: exitAfterAnswer,
|
|
58
|
+
exit: exitAfterAnswer(input.onExit),
|
|
59
59
|
health: healthRegistrar(input.checks ?? {}),
|
|
60
60
|
});
|
|
61
61
|
};
|
|
@@ -3,5 +3,6 @@ import { type ImagePartKind as Part } from '../manifest/primitives.schema.js';
|
|
|
3
3
|
export interface Secrets {
|
|
4
4
|
}
|
|
5
5
|
export declare const SECRETS_DIR = "/run/secrets";
|
|
6
|
+
export declare const secretsFrom: (pkg: ExtensionPackage, part: Part, value: (name: string) => Promise<string>) => Promise<Readonly<Record<string, string>>>;
|
|
6
7
|
export declare const readSecrets: (pkg: ExtensionPackage, part: Part, dir: string) => Promise<Readonly<Record<string, string>>>;
|
|
7
8
|
export declare const withAbsent: (values: Record<string, string>, part: Part, pkg?: ExtensionPackage) => Record<string, string>;
|
package/dist/context/secrets.js
CHANGED
|
@@ -10,11 +10,11 @@ const valueOf = (file, refused) => readFile(file, 'utf8').then((text) => text.re
|
|
|
10
10
|
return '';
|
|
11
11
|
throw new Error(`${refused} cannot be read: ${err.code ?? 'unknown'}`);
|
|
12
12
|
});
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const refused = (
|
|
17
|
-
const read =
|
|
13
|
+
const refusal = (part) => (name) => `${part} refused to start: secret ${name}`;
|
|
14
|
+
// Each name the part mounts, from value(name); an empty value stops the part, named, never valued.
|
|
15
|
+
export const secretsFrom = async (pkg, part, value) => {
|
|
16
|
+
const refused = refusal(part);
|
|
17
|
+
const read = mountedIn(pkg, part).map(async (name) => [name, await value(name)]);
|
|
18
18
|
const pairs = await Promise.all(read);
|
|
19
19
|
const missing = pairs.find(([, value]) => value === '');
|
|
20
20
|
if (missing)
|
|
@@ -22,6 +22,8 @@ export const readSecrets = async (pkg, part, dir) => {
|
|
|
22
22
|
const secrets = Object.fromEntries(pairs.map(([name, value]) => [camelName(name), value]));
|
|
23
23
|
return Object.freeze(withAbsent(secrets, part, pkg));
|
|
24
24
|
};
|
|
25
|
+
// Each declared name from its mounted file; a missing or empty file stops the part, named, never valued.
|
|
26
|
+
export const readSecrets = (pkg, part, dir) => secretsFrom(pkg, part, (name) => valueOf(join(dir, name), refusal(part)(name)));
|
|
25
27
|
// A secret of other parts throws on read, never reads empty; spread and keys skip it.
|
|
26
28
|
export const withAbsent = (values, part, pkg) => {
|
|
27
29
|
for (const name of unmountedIn(pkg, part))
|
|
@@ -2,4 +2,8 @@ import { type Log } from '../log/log.js';
|
|
|
2
2
|
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
3
3
|
import { type Env } from '../runtime/build-info.js';
|
|
4
4
|
import { type SettingValue } from './context.js';
|
|
5
|
+
export type Setting = NonNullable<ExtensionPackage['agentage']['settings']>[string];
|
|
6
|
+
export declare const fits: (setting: Setting, value: unknown) => value is SettingValue;
|
|
7
|
+
export declare const parseObject: (text: string) => Record<string, unknown>;
|
|
8
|
+
export declare const settingsOf: (pkg: ExtensionPackage, given: Record<string, unknown>, log: Log) => Record<string, SettingValue>;
|
|
5
9
|
export declare const settingsFromEnv: (pkg: ExtensionPackage, env: Env, log: Log) => Record<string, SettingValue>;
|
package/dist/context/settings.js
CHANGED
|
@@ -2,22 +2,21 @@ import {} from '../log/log.js';
|
|
|
2
2
|
import {} from '../manifest/manifest.schema.js';
|
|
3
3
|
import {} from '../runtime/build-info.js';
|
|
4
4
|
import { settingDefaults } from './context.js';
|
|
5
|
-
const fits = (setting, value) => {
|
|
5
|
+
export const fits = (setting, value) => {
|
|
6
6
|
if (setting.type === 'integer' ? !Number.isInteger(value) : typeof value !== setting.type)
|
|
7
7
|
return false;
|
|
8
8
|
return !setting.enum || setting.enum.includes(String(value));
|
|
9
9
|
};
|
|
10
|
-
const parseObject = (text) => {
|
|
10
|
+
export const parseObject = (text) => {
|
|
11
11
|
const value = JSON.parse(text);
|
|
12
12
|
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
|
|
13
13
|
throw new Error('AGENTAGE_SETTINGS is not a JSON object');
|
|
14
14
|
}
|
|
15
15
|
return value;
|
|
16
16
|
};
|
|
17
|
-
// The host's values
|
|
18
|
-
export const
|
|
17
|
+
// The host's values over the defaults; a wrong type stops the start.
|
|
18
|
+
export const settingsOf = (pkg, given, log) => {
|
|
19
19
|
const declared = pkg.agentage.settings ?? {};
|
|
20
|
-
const given = env['AGENTAGE_SETTINGS'] ? parseObject(env['AGENTAGE_SETTINGS']) : {};
|
|
21
20
|
const values = settingDefaults(pkg);
|
|
22
21
|
for (const [key, value] of Object.entries(given)) {
|
|
23
22
|
const setting = declared[key];
|
|
@@ -30,3 +29,5 @@ export const settingsFromEnv = (pkg, env, log) => {
|
|
|
30
29
|
}
|
|
31
30
|
return values;
|
|
32
31
|
};
|
|
32
|
+
// The values as AGENTAGE_SETTINGS (JSON) of the image gives them.
|
|
33
|
+
export const settingsFromEnv = (pkg, env, log) => settingsOf(pkg, env['AGENTAGE_SETTINGS'] ? parseObject(env['AGENTAGE_SETTINGS']) : {}, log);
|
|
@@ -47,6 +47,7 @@ export declare const discoveryDocument: (record: ExtensionRecord, host: string)
|
|
|
47
47
|
};
|
|
48
48
|
secrets?: string[] | undefined;
|
|
49
49
|
secretParts?: Record<string, ("pages" | "api" | "mcp" | "worker")[]> | undefined;
|
|
50
|
+
image?: string | undefined;
|
|
50
51
|
openapi?: string | undefined;
|
|
51
52
|
tools?: {
|
|
52
53
|
name: string;
|
|
@@ -43,6 +43,7 @@ export declare const ExtensionRecordSchema: z.ZodObject<{
|
|
|
43
43
|
migrations: z.ZodOptional<z.ZodInt>;
|
|
44
44
|
}, z.core.$strict>>;
|
|
45
45
|
}, z.core.$strict>;
|
|
46
|
+
image: z.ZodOptional<z.ZodString>;
|
|
46
47
|
openapi: z.ZodOptional<z.ZodString>;
|
|
47
48
|
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
48
49
|
name: z.ZodString;
|
|
@@ -87,6 +88,7 @@ export declare const DiscoveryDocumentSchema: z.ZodObject<{
|
|
|
87
88
|
migrations: z.ZodOptional<z.ZodInt>;
|
|
88
89
|
}, z.core.$strict>>;
|
|
89
90
|
}, z.core.$strict>;
|
|
91
|
+
image: z.ZodOptional<z.ZodString>;
|
|
90
92
|
openapi: z.ZodOptional<z.ZodString>;
|
|
91
93
|
tools: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
92
94
|
name: z.ZodString;
|
|
@@ -6,8 +6,12 @@ const ImageDigestRefSchema = z
|
|
|
6
6
|
.max(256)
|
|
7
7
|
.regex(/^[a-z0-9][a-z0-9.-]*(:[0-9]{1,5})?\/[a-z0-9][a-z0-9-]*\/[a-z][a-z0-9-]*-(pages|ui|api|mcp|worker)@sha256:[0-9a-f]{64}$/);
|
|
8
8
|
const PartImageSchema = z.strictObject({ image: ImageDigestRefSchema });
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
// The one image: registry host[:port]/owner[/team...]/{id}@sha256.
|
|
10
|
+
const OneImageRefSchema = z
|
|
11
|
+
.string()
|
|
12
|
+
.max(256)
|
|
13
|
+
.regex(/^[a-z0-9][a-z0-9.-]*(:[0-9]{1,5})?(\/[a-z0-9][a-z0-9_.-]*)+\/[a-z][a-z0-9-]*@sha256:[0-9a-f]{64}$/);
|
|
14
|
+
const PartsRecordSchema = z.strictObject({
|
|
11
15
|
pages: PartImageSchema.optional(),
|
|
12
16
|
ui: PartImageSchema.optional(),
|
|
13
17
|
api: PartImageSchema.optional(),
|
|
@@ -16,9 +20,6 @@ const PartsRecordSchema = z
|
|
|
16
20
|
store: z
|
|
17
21
|
.strictObject({ kind: StoreKindSchema, migrations: z.int().min(0).optional() })
|
|
18
22
|
.optional(),
|
|
19
|
-
})
|
|
20
|
-
.refine((parts) => IMAGE_PART_KINDS.some((kind) => parts[kind] !== undefined), {
|
|
21
|
-
message: 'at least one part with an image',
|
|
22
23
|
});
|
|
23
24
|
export const OriginSchema = z
|
|
24
25
|
.string()
|
|
@@ -39,19 +40,32 @@ const recordShape = {
|
|
|
39
40
|
commit: CommitSchema,
|
|
40
41
|
buildTime: z.iso.datetime(),
|
|
41
42
|
parts: PartsRecordSchema,
|
|
43
|
+
image: OneImageRefSchema.optional().describe('The one image (build --container). A record has part images or this image: never both, never none.'),
|
|
44
|
+
// /api/v{N}: an extension still on a kit before 0.13, read by a newer one; it goes in phase 4.
|
|
42
45
|
openapi: z
|
|
43
46
|
.string()
|
|
44
|
-
.regex(/^\/api\/v[
|
|
47
|
+
.regex(/^\/api\/(?:v[0-9]+|[a-z][a-z0-9-]*)\/openapi\.json$/)
|
|
48
|
+
.describe('/api/{id}/openapi.json, or the old /api/v{N}; only the kit checks that {id} is the id')
|
|
45
49
|
.optional(),
|
|
46
50
|
tools: z.array(ToolRecordSchema).min(1).optional(),
|
|
47
51
|
widgets: z.array(WidgetSchema).min(1).optional(),
|
|
48
52
|
commands: z.array(CommandNameSchema).min(1).optional(),
|
|
49
53
|
};
|
|
50
|
-
const imagesNameTheirExtension = (doc) =>
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
});
|
|
54
|
-
|
|
54
|
+
const imagesNameTheirExtension = (doc) => (!doc.image || doc.image.includes(`/${doc.id}@sha256:`)) &&
|
|
55
|
+
IMAGE_PART_KINDS.every((kind) => {
|
|
56
|
+
const image = doc.parts[kind]?.image;
|
|
57
|
+
return image === undefined || image.includes(`/${doc.id}-${kind}@sha256:`);
|
|
58
|
+
});
|
|
59
|
+
const IMAGE_RULE = { message: 'every image is {id}-{part}, the one image {id}', path: ['parts'] };
|
|
60
|
+
// One image per part, or the one image of every part (build --container): never both, never none.
|
|
61
|
+
const oneImageForm = (doc) => IMAGE_PART_KINDS.some((kind) => doc.parts[kind] !== undefined) !== Boolean(doc.image);
|
|
62
|
+
const FORM_RULE = {
|
|
63
|
+
message: 'at least one part with an image, or the one image and no part image',
|
|
64
|
+
path: ['parts'],
|
|
65
|
+
};
|
|
66
|
+
// The api prefix is /api/{id}, or the old /api/v{N}: never another extension's id.
|
|
67
|
+
const openapiNamesItsExtension = ({ id, openapi }) => !openapi || /^\/api\/v[0-9]+\//.test(openapi) || openapi === `/api/${id}/openapi.json`;
|
|
68
|
+
const OPENAPI_RULE = { message: 'openapi is /api/{id}/openapi.json', path: ['openapi'] };
|
|
55
69
|
// A scoped name lives only in secretParts, so an estate that does not read it mounts nothing.
|
|
56
70
|
const scoped = (name) => `secret ${name}: one or more parts, none repeated, and never also in secrets`;
|
|
57
71
|
// The build record: written by `build --push` into the package and the release asset; no host.
|
|
@@ -62,7 +76,9 @@ export const ExtensionRecordSchema = z
|
|
|
62
76
|
secrets: z.array(SecretNameSchema).min(1).optional(),
|
|
63
77
|
secretParts: z.record(SecretNameSchema, z.array(SecretPartSchema)).optional(),
|
|
64
78
|
})
|
|
79
|
+
.refine(oneImageForm, FORM_RULE)
|
|
65
80
|
.refine(imagesNameTheirExtension, IMAGE_RULE)
|
|
81
|
+
.refine(openapiNamesItsExtension, OPENAPI_RULE)
|
|
66
82
|
.superRefine((doc, ctx) => {
|
|
67
83
|
for (const [name, parts] of Object.entries(doc.secretParts ?? {}))
|
|
68
84
|
if (!parts.length || new Set(parts).size !== parts.length || doc.secrets?.includes(name))
|
|
@@ -71,4 +87,6 @@ export const ExtensionRecordSchema = z
|
|
|
71
87
|
// What a deployment serves at /.well-known/agentage-extension.json: the record plus its host.
|
|
72
88
|
export const DiscoveryDocumentSchema = z
|
|
73
89
|
.strictObject({ ...recordShape, host: OriginSchema })
|
|
74
|
-
.refine(
|
|
90
|
+
.refine(oneImageForm, FORM_RULE)
|
|
91
|
+
.refine(imagesNameTheirExtension, IMAGE_RULE)
|
|
92
|
+
.refine(openapiNamesItsExtension, OPENAPI_RULE);
|
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export declare const OPENAPI_FILE = "openapi.json";
|
|
2
|
-
export declare const
|
|
2
|
+
export declare const apiPrefix: (id: string) => string;
|
|
3
|
+
export declare const openapiPath: (id: string) => string;
|
|
3
4
|
export declare const readOpenapi: (dir: string) => Promise<Record<string, unknown> | null>;
|
|
@@ -2,7 +2,9 @@ import { join } from 'node:path';
|
|
|
2
2
|
import { z } from 'zod';
|
|
3
3
|
import { readJsonIfExists } from '../manifest/read-manifest.js';
|
|
4
4
|
export const OPENAPI_FILE = 'openapi.json';
|
|
5
|
-
|
|
5
|
+
// The api part's URL prefix: the extension id, no version (D3 of S1.1.1).
|
|
6
|
+
export const apiPrefix = (id) => `/api/${id}`;
|
|
7
|
+
export const openapiPath = (id) => `${apiPrefix(id)}/openapi.json`;
|
|
6
8
|
const OpenapiSchema = z.looseObject({
|
|
7
9
|
openapi: z.string().regex(/^3\.1\.[0-9]+$/, { message: 'OpenAPI 3.1' }),
|
|
8
10
|
paths: z.record(z.string(), z.unknown()),
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type Entries } from './entries.js';
|
|
2
|
+
import { type SupervisorDeps } from './supervisor.js';
|
|
3
|
+
export declare const addExtension: (deps: SupervisorDeps, entries: Entries, body: unknown) => Promise<{
|
|
4
|
+
error?: string | undefined;
|
|
5
|
+
pid?: number | undefined;
|
|
6
|
+
id: string;
|
|
7
|
+
version: string;
|
|
8
|
+
state: import("./supervisor.js").State;
|
|
9
|
+
}>;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { isAbsolute } from 'node:path';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { fail } from '../../envelope/envelope.js';
|
|
4
|
+
import { parse } from '../../envelope/parse.js';
|
|
5
|
+
import { ExtensionIdSchema } from '../../manifest/primitives.schema.js';
|
|
6
|
+
import { readManifest } from '../../manifest/read-manifest.js';
|
|
7
|
+
import { persist, summary } from './entries.js';
|
|
8
|
+
import { runInstaller } from './installer-call.js';
|
|
9
|
+
import {} from './installation.js';
|
|
10
|
+
import { startEntry } from './supervisor.js';
|
|
11
|
+
const RESERVED_ID = 'host';
|
|
12
|
+
// enabled: false records the extension and never starts it (a profile "off" entry).
|
|
13
|
+
const AddSchema = z.union([
|
|
14
|
+
z.strictObject({
|
|
15
|
+
id: ExtensionIdSchema,
|
|
16
|
+
version: z.string().min(1).optional(),
|
|
17
|
+
enabled: z.boolean().optional(),
|
|
18
|
+
}),
|
|
19
|
+
z.strictObject({
|
|
20
|
+
source: z.string().refine(isAbsolute, 'an absolute path'),
|
|
21
|
+
enabled: z.boolean().optional(),
|
|
22
|
+
}),
|
|
23
|
+
]);
|
|
24
|
+
// The entry a 409 names, so a client can tell a repeat from another package of that id.
|
|
25
|
+
const existing = ({ id, version, install }) => ({
|
|
26
|
+
id,
|
|
27
|
+
...(version ? { version } : {}),
|
|
28
|
+
...(install.source ? { source: install.source } : {}),
|
|
29
|
+
});
|
|
30
|
+
const reserve = (entries, id) => {
|
|
31
|
+
if (id === RESERVED_ID)
|
|
32
|
+
throw fail('VALIDATION_ERROR', 'the id host is reserved');
|
|
33
|
+
const has = entries.get(id);
|
|
34
|
+
if (has)
|
|
35
|
+
throw fail('CONFLICT', `${id} is installed`, { details: existing(has) });
|
|
36
|
+
};
|
|
37
|
+
const sourceEntry = async (source) => {
|
|
38
|
+
const pkg = await readManifest(source).catch((err) => {
|
|
39
|
+
throw fail('VALIDATION_ERROR', `${source}: ${err.message}`);
|
|
40
|
+
});
|
|
41
|
+
return { install: { id: pkg.agentage.id, source }, dir: source, version: pkg.version };
|
|
42
|
+
};
|
|
43
|
+
const marketEntry = async (deps, entries, id, version) => {
|
|
44
|
+
reserve(entries, id);
|
|
45
|
+
const got = await runInstaller(deps, { id, version });
|
|
46
|
+
return { install: { id: got.id, version: got.version }, dir: got.dir, version: got.version };
|
|
47
|
+
};
|
|
48
|
+
// POST: installs (or links a folder), records it, then starts it; a failed start stays listed as failed.
|
|
49
|
+
export const addExtension = async (deps, entries, body) => {
|
|
50
|
+
const spec = parse(AddSchema, body, 'Invalid body');
|
|
51
|
+
const got = 'source' in spec
|
|
52
|
+
? await sourceEntry(spec.source)
|
|
53
|
+
: await marketEntry(deps, entries, spec.id, spec.version);
|
|
54
|
+
reserve(entries, got.install.id);
|
|
55
|
+
const off = spec.enabled === false;
|
|
56
|
+
const install = off ? { ...got.install, enabled: false } : got.install;
|
|
57
|
+
await persist(deps, install.id, install);
|
|
58
|
+
const entry = { ...got, id: install.id, install, state: 'disabled' };
|
|
59
|
+
entries.set(entry.id, entry);
|
|
60
|
+
if (!off)
|
|
61
|
+
await startEntry(deps, entry);
|
|
62
|
+
return summary(entry);
|
|
63
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
interface LaunchSpec {
|
|
2
|
+
id: string;
|
|
3
|
+
dir: string;
|
|
4
|
+
dataDir: string;
|
|
5
|
+
settings: Record<string, unknown>;
|
|
6
|
+
secrets: Record<string, string>;
|
|
7
|
+
origin: string;
|
|
8
|
+
storeOpenMs?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface Exit {
|
|
11
|
+
code: number | null;
|
|
12
|
+
signal: string | null;
|
|
13
|
+
}
|
|
14
|
+
export interface Child {
|
|
15
|
+
port: number;
|
|
16
|
+
pid: number;
|
|
17
|
+
exited: Promise<Exit>;
|
|
18
|
+
stop(): Promise<void>;
|
|
19
|
+
}
|
|
20
|
+
export type Launch = (spec: LaunchSpec) => Promise<Child>;
|
|
21
|
+
export declare const childEnv: (spec: LaunchSpec, secretsDir: string) => Record<string, string>;
|
|
22
|
+
export declare const forkLaunch: (main?: string, startMs?: number) => Launch;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { fork } from 'node:child_process';
|
|
2
|
+
import { mkdtemp, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { tmpdir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { SecretNameSchema } from '../../manifest/primitives.schema.js';
|
|
7
|
+
import { LIMITS } from './limits.js';
|
|
8
|
+
const HOST_MAIN = fileURLToPath(new URL('../main.js', import.meta.url));
|
|
9
|
+
const STOP_MS = 5_000;
|
|
10
|
+
const PASSED = ['PATH', 'HOME', 'TMPDIR', 'LANG', 'NODE_EXTRA_CA_CERTS'];
|
|
11
|
+
// Handed over as 0600 files in a 0700 folder, removed once the child has read them.
|
|
12
|
+
const secretFiles = async (spec) => {
|
|
13
|
+
const dir = await mkdtemp(join(tmpdir(), `agentage-${spec.id}-`));
|
|
14
|
+
for (const [name, value] of Object.entries(spec.secrets))
|
|
15
|
+
await writeFile(join(dir, SecretNameSchema.parse(name)), value, { mode: 0o600 });
|
|
16
|
+
return dir;
|
|
17
|
+
};
|
|
18
|
+
// What dist/host/main.js reads (docs/part-image.md), on a port the OS picks.
|
|
19
|
+
export const childEnv = (spec, secretsDir) => ({
|
|
20
|
+
PORT: '0',
|
|
21
|
+
AGENTAGE_EXTENSION_DIR: spec.dir,
|
|
22
|
+
AGENTAGE_DATA_DIR: spec.dataDir,
|
|
23
|
+
AGENTAGE_SETTINGS: JSON.stringify(spec.settings),
|
|
24
|
+
AGENTAGE_SECRETS_DIR: secretsDir,
|
|
25
|
+
AGENTAGE_ORIGIN: spec.origin,
|
|
26
|
+
...(spec.storeOpenMs && { AGENTAGE_STORE_OPEN_MS: String(spec.storeOpenMs) }),
|
|
27
|
+
});
|
|
28
|
+
const passedEnv = () => Object.fromEntries(PASSED.flatMap((k) => (process.env[k] ? [[k, process.env[k]]] : [])));
|
|
29
|
+
// The child's last stderr line, its msg when it is a log row: why it did not start.
|
|
30
|
+
const lastWords = (tail, how) => {
|
|
31
|
+
const lines = tail
|
|
32
|
+
.trim()
|
|
33
|
+
.split('\n')
|
|
34
|
+
.filter((l) => !/^Node\.js v\d/.test(l));
|
|
35
|
+
const line = lines.at(-1) ?? '';
|
|
36
|
+
try {
|
|
37
|
+
const msg = String(JSON.parse(line).msg ?? line);
|
|
38
|
+
return msg.replace(/^[A-Za-z]*Error: /, '');
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
return line || `exited with ${how.signal ? `signal ${how.signal}` : `code ${how.code}`}`;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const captured = (child) => {
|
|
45
|
+
let tail = '';
|
|
46
|
+
child.stdout?.pipe(process.stdout);
|
|
47
|
+
child.stderr?.on('data', (chunk) => {
|
|
48
|
+
process.stderr.write(chunk);
|
|
49
|
+
tail = (tail + chunk.toString()).slice(-4000);
|
|
50
|
+
});
|
|
51
|
+
return () => tail;
|
|
52
|
+
};
|
|
53
|
+
const portOf = (msg) => {
|
|
54
|
+
const { type, port } = (msg ?? {});
|
|
55
|
+
return type === 'ready' && typeof port === 'number' ? port : undefined;
|
|
56
|
+
};
|
|
57
|
+
// The port the child bound, or why it exited first; a child silent for startMs is killed.
|
|
58
|
+
const readyOf = (child, tail, startMs) => new Promise((resolve, reject) => {
|
|
59
|
+
let killed = false;
|
|
60
|
+
const timer = setTimeout(() => ((killed = true), child.kill('SIGKILL')), startMs);
|
|
61
|
+
const settle = (fn) => {
|
|
62
|
+
clearTimeout(timer);
|
|
63
|
+
fn();
|
|
64
|
+
};
|
|
65
|
+
child.on('message', (msg) => {
|
|
66
|
+
const port = portOf(msg);
|
|
67
|
+
if (port)
|
|
68
|
+
settle(() => resolve(port));
|
|
69
|
+
});
|
|
70
|
+
child.once('exit', (code, signal) => {
|
|
71
|
+
const why = killed
|
|
72
|
+
? `reported no port in ${startMs} ms: killed`
|
|
73
|
+
: lastWords(tail(), { code, signal });
|
|
74
|
+
settle(() => reject(new Error(why)));
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
// SIGTERM drains; a child still there after STOP_MS (a request that never ends) gets SIGKILL.
|
|
78
|
+
const stopOf = (child, exited) => async () => {
|
|
79
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
80
|
+
child.kill('SIGTERM');
|
|
81
|
+
const kill = setTimeout(() => child.kill('SIGKILL'), STOP_MS);
|
|
82
|
+
await exited;
|
|
83
|
+
clearTimeout(kill);
|
|
84
|
+
};
|
|
85
|
+
// One child process per extension: node dist/host/main.js on a port the OS picks.
|
|
86
|
+
export const forkLaunch = (main = HOST_MAIN, startMs = LIMITS.startMs) => async (spec) => {
|
|
87
|
+
const secretsDir = await secretFiles(spec);
|
|
88
|
+
const stdio = ['ignore', 'pipe', 'pipe', 'ipc'];
|
|
89
|
+
// execArgv []: the daemon's own node flags (a loader, --input-type) are not the child's.
|
|
90
|
+
const env = { ...passedEnv(), ...childEnv(spec, secretsDir) };
|
|
91
|
+
const child = fork(main, [], { env, execArgv: [], stdio: [...stdio] });
|
|
92
|
+
const exited = new Promise((resolve) => child.once('exit', (code, signal) => resolve({ code, signal })));
|
|
93
|
+
try {
|
|
94
|
+
const port = await readyOf(child, captured(child), startMs);
|
|
95
|
+
return { port, pid: child.pid ?? 0, exited, stop: stopOf(child, exited) };
|
|
96
|
+
}
|
|
97
|
+
finally {
|
|
98
|
+
await rm(secretsDir, { recursive: true, force: true });
|
|
99
|
+
}
|
|
100
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export declare const DAEMON_PROTOCOL = 1;
|
|
2
|
+
interface DaemonFile {
|
|
3
|
+
pid: number;
|
|
4
|
+
port: number;
|
|
5
|
+
version: string;
|
|
6
|
+
protocol: number;
|
|
7
|
+
startedBy: string;
|
|
8
|
+
}
|
|
9
|
+
export declare const writeDaemonFile: (configDir: string, file: DaemonFile) => Promise<void>;
|
|
10
|
+
export declare const removeDaemonFile: (configDir: string) => Promise<void>;
|
|
11
|
+
export declare const dataDirOf: (configDir: string, id: string) => string;
|
|
12
|
+
export declare const purgeData: (configDir: string, id: string) => Promise<void>;
|
|
13
|
+
type ValueKind = 'settings' | 'secrets';
|
|
14
|
+
type Values = Record<string, unknown>;
|
|
15
|
+
export declare const readValues: (configDir: string, kind: ValueKind, id: string) => Promise<Values>;
|
|
16
|
+
export declare const writeValues: (configDir: string, kind: ValueKind, id: string, values?: Values, waitMs?: number) => Promise<void>;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { rm, unlink } from 'node:fs/promises';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { readJsonFile, withFileLock, writeJsonFile } from './json-file.js';
|
|
4
|
+
const DAEMON_FILE = 'daemon.json';
|
|
5
|
+
export const DAEMON_PROTOCOL = 1;
|
|
6
|
+
export const writeDaemonFile = (configDir, file) => writeJsonFile(join(configDir, DAEMON_FILE), file);
|
|
7
|
+
export const removeDaemonFile = (configDir) => unlink(join(configDir, DAEMON_FILE)).catch(() => undefined);
|
|
8
|
+
export const dataDirOf = (configDir, id) => join(configDir, 'data', id);
|
|
9
|
+
export const purgeData = (configDir, id) => rm(dataDirOf(configDir, id), { recursive: true, force: true });
|
|
10
|
+
const fileOf = (configDir, kind) => join(configDir, `${kind}.json`);
|
|
11
|
+
export const readValues = async (configDir, kind, id) => (await readJsonFile(fileOf(configDir, kind), {}))[id] ?? {};
|
|
12
|
+
// The rows of one file changed under its lock; the same object back writes nothing.
|
|
13
|
+
const changeValues = (configDir, kind, change, waitMs) => {
|
|
14
|
+
const file = fileOf(configDir, kind);
|
|
15
|
+
const write = async () => {
|
|
16
|
+
const all = await readJsonFile(file, {});
|
|
17
|
+
const next = change(all);
|
|
18
|
+
if (next !== all)
|
|
19
|
+
await writeJsonFile(file, next, kind === 'secrets' ? 0o600 : 0o644);
|
|
20
|
+
};
|
|
21
|
+
return withFileLock(file, write, waitMs);
|
|
22
|
+
};
|
|
23
|
+
export const writeValues = (configDir, kind, id, values, waitMs) => changeValues(configDir, kind, (all) => {
|
|
24
|
+
const rest = { ...all };
|
|
25
|
+
delete rest[id];
|
|
26
|
+
return values ? { ...rest, [id]: values } : rest;
|
|
27
|
+
}, waitMs);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const assertNoOtherDaemon: (configDir: string) => Promise<void>;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { join } from 'node:path';
|
|
2
|
+
import { fail } from '../../envelope/envelope.js';
|
|
3
|
+
import { readJsonFile } from './json-file.js';
|
|
4
|
+
const alive = (pid) => {
|
|
5
|
+
try {
|
|
6
|
+
return process.kill(pid, 0);
|
|
7
|
+
}
|
|
8
|
+
catch (err) {
|
|
9
|
+
return err.code === 'EPERM';
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
// A daemon.json of a dead or other process is stale: only one that answers as that daemon counts.
|
|
13
|
+
const answersAsDaemon = async (port, pid) => {
|
|
14
|
+
const res = await fetch(`http://127.0.0.1:${port}/health`, {
|
|
15
|
+
signal: AbortSignal.timeout(1_000),
|
|
16
|
+
});
|
|
17
|
+
const body = (await res.json());
|
|
18
|
+
return body.service === 'agentage-daemon' && body.pid === pid;
|
|
19
|
+
};
|
|
20
|
+
// installation.json belongs to the daemon that runs on configDir; its own process may write it.
|
|
21
|
+
export const assertNoOtherDaemon = async (configDir) => {
|
|
22
|
+
const file = await readJsonFile(join(configDir, 'daemon.json'), null).catch(() => null);
|
|
23
|
+
const { pid, port } = file ?? {};
|
|
24
|
+
if (!pid || !port || pid === process.pid || !alive(pid))
|
|
25
|
+
return;
|
|
26
|
+
if (!(await answersAsDaemon(port, pid).catch(() => false)))
|
|
27
|
+
return;
|
|
28
|
+
throw fail('CONFLICT', `a daemon (process ${pid}) runs on ${configDir}: change it through it`, {
|
|
29
|
+
code: 'DAEMON_RUNNING',
|
|
30
|
+
details: { pid, port },
|
|
31
|
+
});
|
|
32
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type Launch } from './child.js';
|
|
2
|
+
import { type Limits } from './limits.js';
|
|
3
|
+
import { type Installer } from './supervisor.js';
|
|
4
|
+
export interface DaemonOptions extends Partial<Limits> {
|
|
5
|
+
configDir: string;
|
|
6
|
+
port?: number;
|
|
7
|
+
install?: Installer;
|
|
8
|
+
version: string;
|
|
9
|
+
startedBy: string;
|
|
10
|
+
}
|
|
11
|
+
export interface Daemon {
|
|
12
|
+
port: number;
|
|
13
|
+
stop(): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
export declare const DAEMON_PORT = 4243;
|
|
16
|
+
export declare const startDaemon: (opts: DaemonOptions, launch: Launch) => Promise<Daemon>;
|
|
17
|
+
export declare const createDaemon: (opts: DaemonOptions) => Promise<Daemon>;
|