@agentage/extension 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +11 -2
- package/dist/agcid/agcid.d.ts +4 -0
- package/dist/agcid/agcid.js +15 -0
- package/dist/agcid/scope.d.ts +4 -0
- package/dist/agcid/scope.js +16 -0
- package/dist/context/context.d.ts +36 -0
- package/dist/context/context.js +25 -0
- package/dist/discovery/discovery.d.ts +47 -0
- package/dist/discovery/discovery.js +52 -0
- package/dist/discovery/discovery.schema.d.ts +106 -0
- package/dist/discovery/discovery.schema.js +56 -0
- package/dist/envelope/codes.d.ts +16 -0
- package/dist/envelope/codes.js +15 -0
- package/dist/envelope/envelope.d.ts +34 -0
- package/dist/envelope/envelope.js +23 -0
- package/dist/envelope/envelope.schema.d.ts +24 -0
- package/dist/envelope/envelope.schema.js +18 -0
- package/dist/envelope/parse.d.ts +17 -0
- package/dist/envelope/parse.js +12 -0
- package/dist/health/health.d.ts +18 -0
- package/dist/health/health.js +43 -0
- package/dist/health/health.schema.d.ts +46 -0
- package/dist/health/health.schema.js +20 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +19 -0
- package/dist/log/log.d.ts +13 -0
- package/dist/log/log.js +21 -0
- package/dist/manifest/manifest.schema.d.ts +195 -0
- package/dist/manifest/manifest.schema.js +88 -0
- package/dist/manifest/parts.d.ts +11 -0
- package/dist/manifest/parts.js +24 -0
- package/dist/manifest/primitives.schema.d.ts +20 -0
- package/dist/manifest/primitives.schema.js +52 -0
- package/dist/manifest/read-manifest.d.ts +5 -0
- package/dist/manifest/read-manifest.js +19 -0
- package/dist/runtime/build-info.d.ts +4 -0
- package/dist/runtime/build-info.js +16 -0
- package/dist/runtime/main.d.ts +2 -0
- package/dist/runtime/main.js +10 -0
- package/dist/runtime/run-part.d.ts +13 -0
- package/dist/runtime/run-part.js +57 -0
- package/dist/schemas/json-schemas.d.ts +2 -0
- package/dist/schemas/json-schemas.js +29 -0
- package/dist/schemas/write-schemas.d.ts +1 -0
- package/dist/schemas/write-schemas.js +8 -0
- package/dist/server/app.d.ts +18 -0
- package/dist/server/app.js +47 -0
- package/dist/server/errors.d.ts +6 -0
- package/dist/server/errors.js +29 -0
- package/dist/server/part.types.d.ts +7 -0
- package/dist/server/part.types.js +2 -0
- package/dist/server/serve.d.ts +14 -0
- package/dist/server/serve.js +27 -0
- package/dist/testing/test-server.d.ts +12 -0
- package/dist/testing/test-server.js +26 -0
- package/dist/tool/build-info.d.ts +4 -0
- package/dist/tool/build-info.js +18 -0
- package/dist/tool/build.d.ts +35 -0
- package/dist/tool/build.js +83 -0
- package/dist/tool/cli.d.ts +2 -0
- package/dist/tool/cli.js +3 -0
- package/dist/tool/docker.d.ts +15 -0
- package/dist/tool/docker.js +26 -0
- package/dist/tool/exec.d.ts +12 -0
- package/dist/tool/exec.js +24 -0
- package/dist/tool/format.d.ts +2 -0
- package/dist/tool/format.js +14 -0
- package/dist/tool/index.d.ts +11 -0
- package/dist/tool/index.js +11 -0
- package/dist/tool/labels.d.ts +4 -0
- package/dist/tool/labels.js +25 -0
- package/dist/tool/main.d.ts +8 -0
- package/dist/tool/main.js +54 -0
- package/dist/tool/recipe.d.ts +3 -0
- package/dist/tool/recipe.js +32 -0
- package/dist/tool/stage.d.ts +5 -0
- package/dist/tool/stage.js +52 -0
- package/package.json +61 -4
- package/schemas/error-envelope.schema.json +70 -0
- package/schemas/extension-discovery.schema.json +182 -0
- package/schemas/extension-package.schema.json +309 -0
- package/schemas/extension-record.schema.json +176 -0
- package/schemas/health.schema.json +100 -0
- package/schemas/ok-envelope.schema.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Agentage Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @agentage/extension
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
The agentage extension kit. A part module exports only `create(ctx)`; the kit owns the server,
|
|
4
|
+
port, `/health`, the discovery document, the `agcid` correlation id and the response envelope.
|
|
5
|
+
`@agentage/extension/tool` is the build tool behind `agentage extension build`.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @agentage/extension express zod
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
See the [platform README](https://github.com/agentage/platform#readme) for the api part, the
|
|
12
|
+
manifest, the build tool and how a part image runs.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
export const AGCID_HEADER = 'agcid';
|
|
3
|
+
const AGCID = /^[A-Za-z0-9._:-]{8,128}$/;
|
|
4
|
+
export const isAgcid = (value) => typeof value === 'string' && AGCID.test(value);
|
|
5
|
+
const hyphenate = (hex) => [hex.slice(0, 8), hex.slice(8, 12), hex.slice(12, 16), hex.slice(16, 20), hex.slice(20)].join('-');
|
|
6
|
+
// RFC 9562 UUIDv7: 48-bit unix ms, version 7, variant 10, the rest random.
|
|
7
|
+
export const uuidv7 = (now = Date.now(), random = randomBytes(10)) => {
|
|
8
|
+
const bytes = Buffer.alloc(16);
|
|
9
|
+
bytes.writeUIntBE(now, 0, 6);
|
|
10
|
+
random.copy(bytes, 6, 0, 10);
|
|
11
|
+
bytes.writeUInt8((bytes.readUInt8(6) & 0x0f) | 0x70, 6);
|
|
12
|
+
bytes.writeUInt8((bytes.readUInt8(8) & 0x3f) | 0x80, 8);
|
|
13
|
+
return hyphenate(bytes.toString('hex'));
|
|
14
|
+
};
|
|
15
|
+
export const agcidOf = (incoming) => (isAgcid(incoming) ? incoming : uuidv7());
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
+
import {} from 'express';
|
|
3
|
+
import { AGCID_HEADER, agcidOf } from './agcid.js';
|
|
4
|
+
const scope = new AsyncLocalStorage();
|
|
5
|
+
export const currentAgcid = () => scope.getStore()?.agcid;
|
|
6
|
+
// Keeps a valid incoming agcid, else mints one; echoed on every response and tags every log line.
|
|
7
|
+
export const agcidMiddleware = (req, res, next) => {
|
|
8
|
+
const agcid = agcidOf(req.get(AGCID_HEADER));
|
|
9
|
+
res.locals['agcid'] = agcid;
|
|
10
|
+
res.setHeader(AGCID_HEADER, agcid);
|
|
11
|
+
scope.run({ agcid }, next);
|
|
12
|
+
};
|
|
13
|
+
export const agcidOfResponse = (res) => {
|
|
14
|
+
const agcid = res.locals['agcid'];
|
|
15
|
+
return typeof agcid === 'string' ? agcid : agcidOf(undefined);
|
|
16
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
2
|
+
import { type Log, type LogFields, type LogLevel } from '../log/log.js';
|
|
3
|
+
export type ServerPartKind = 'pages' | 'api' | 'mcp' | 'worker';
|
|
4
|
+
export type SettingValue = string | number | boolean;
|
|
5
|
+
export interface ExtensionInfo {
|
|
6
|
+
id: string;
|
|
7
|
+
version: string;
|
|
8
|
+
part: ServerPartKind;
|
|
9
|
+
origin: string;
|
|
10
|
+
}
|
|
11
|
+
export interface PartContext {
|
|
12
|
+
readonly extension: Readonly<ExtensionInfo>;
|
|
13
|
+
readonly log: Log;
|
|
14
|
+
readonly settings: Readonly<Record<string, SettingValue>>;
|
|
15
|
+
}
|
|
16
|
+
export declare const settingDefaults: (pkg: ExtensionPackage) => Record<string, SettingValue>;
|
|
17
|
+
export interface ContextInput {
|
|
18
|
+
pkg: ExtensionPackage;
|
|
19
|
+
part: ServerPartKind;
|
|
20
|
+
origin: string;
|
|
21
|
+
log?: Log;
|
|
22
|
+
}
|
|
23
|
+
export declare const createContext: ({ pkg, part, origin, log }: ContextInput) => PartContext;
|
|
24
|
+
export interface LogEntry {
|
|
25
|
+
level: LogLevel;
|
|
26
|
+
message: string;
|
|
27
|
+
fields: LogFields;
|
|
28
|
+
}
|
|
29
|
+
export interface TestContext extends PartContext {
|
|
30
|
+
readonly logs: LogEntry[];
|
|
31
|
+
}
|
|
32
|
+
export interface TestContextOptions {
|
|
33
|
+
extension?: Partial<ExtensionInfo>;
|
|
34
|
+
settings?: Record<string, SettingValue>;
|
|
35
|
+
}
|
|
36
|
+
export declare const createTestContext: (options?: TestContextOptions) => TestContext;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {} from '../manifest/manifest.schema.js';
|
|
2
|
+
import { createLog } from '../log/log.js';
|
|
3
|
+
export const settingDefaults = (pkg) => Object.fromEntries(Object.entries(pkg.agentage.settings ?? {}).map(([key, setting]) => [key, setting.default]));
|
|
4
|
+
export const createContext = ({ pkg, part, origin, log }) => Object.freeze({
|
|
5
|
+
extension: Object.freeze({ id: pkg.agentage.id, version: pkg.version, part, origin }),
|
|
6
|
+
log: log ?? createLog({ extension: pkg.agentage.id, part }),
|
|
7
|
+
settings: Object.freeze(settingDefaults(pkg)),
|
|
8
|
+
});
|
|
9
|
+
const recordingLog = (logs) => ({
|
|
10
|
+
info: (message, fields = {}) => void logs.push({ level: 'info', message, fields }),
|
|
11
|
+
warn: (message, fields = {}) => void logs.push({ level: 'warn', message, fields }),
|
|
12
|
+
error: (error, fields = {}) => void logs.push({ level: 'error', message: String(error), fields: { ...fields, err: error } }),
|
|
13
|
+
});
|
|
14
|
+
// A ctx for unit tests: fixed identity, settings as given, a log that records instead of printing.
|
|
15
|
+
export const createTestContext = (options = {}) => {
|
|
16
|
+
const logs = [];
|
|
17
|
+
const extension = {
|
|
18
|
+
id: 'test',
|
|
19
|
+
version: '0.0.0',
|
|
20
|
+
part: 'api',
|
|
21
|
+
origin: 'http://127.0.0.1',
|
|
22
|
+
...options.extension,
|
|
23
|
+
};
|
|
24
|
+
return { extension, log: recordingLog(logs), settings: { ...options.settings }, logs };
|
|
25
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ExtensionPackage } from '../manifest/manifest.schema.js';
|
|
2
|
+
import { type ImagePartKind } from '../manifest/primitives.schema.js';
|
|
3
|
+
import { type ExtensionRecord } from './discovery.schema.js';
|
|
4
|
+
export interface BuildInfo {
|
|
5
|
+
commit: string;
|
|
6
|
+
buildTime: string;
|
|
7
|
+
}
|
|
8
|
+
export type PartImages = Partial<Record<ImagePartKind, string>>;
|
|
9
|
+
export declare const DEFAULT_RECORD_FILE = "/run/agentage/agentage-extension.json";
|
|
10
|
+
export declare const recordOf: (pkg: ExtensionPackage, build: BuildInfo, images: PartImages) => ExtensionRecord;
|
|
11
|
+
export declare const validateRecord: (value: unknown) => ExtensionRecord;
|
|
12
|
+
export declare const loadRecord: (file: string) => Promise<ExtensionRecord | null>;
|
|
13
|
+
export declare const discoveryDocument: (record: ExtensionRecord, host: string) => {
|
|
14
|
+
parts: {
|
|
15
|
+
pages?: {
|
|
16
|
+
image: string;
|
|
17
|
+
} | undefined;
|
|
18
|
+
ui?: {
|
|
19
|
+
image: string;
|
|
20
|
+
} | undefined;
|
|
21
|
+
api?: {
|
|
22
|
+
image: string;
|
|
23
|
+
} | undefined;
|
|
24
|
+
mcp?: {
|
|
25
|
+
image: string;
|
|
26
|
+
} | undefined;
|
|
27
|
+
worker?: {
|
|
28
|
+
image: string;
|
|
29
|
+
} | undefined;
|
|
30
|
+
store?: {
|
|
31
|
+
kind: "postgres" | "git" | "signoz";
|
|
32
|
+
} | undefined;
|
|
33
|
+
};
|
|
34
|
+
openapi?: string | undefined;
|
|
35
|
+
tools?: string[] | undefined;
|
|
36
|
+
widgets?: {
|
|
37
|
+
name: string;
|
|
38
|
+
element: string;
|
|
39
|
+
module: string;
|
|
40
|
+
}[] | undefined;
|
|
41
|
+
commands?: string[] | undefined;
|
|
42
|
+
id: string;
|
|
43
|
+
version: string;
|
|
44
|
+
commit: string;
|
|
45
|
+
buildTime: string;
|
|
46
|
+
host: string;
|
|
47
|
+
};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { formatIssues } from '../manifest/read-manifest.js';
|
|
3
|
+
import {} from '../manifest/manifest.schema.js';
|
|
4
|
+
import { normalizeParts } from '../manifest/parts.js';
|
|
5
|
+
import {} from '../manifest/primitives.schema.js';
|
|
6
|
+
import { ExtensionRecordSchema } from './discovery.schema.js';
|
|
7
|
+
export const DEFAULT_RECORD_FILE = '/run/agentage/agentage-extension.json';
|
|
8
|
+
const commandNames = (pkg) => {
|
|
9
|
+
const names = (pkg.agentage.commands ?? []).map((command) => command.name);
|
|
10
|
+
return names.length > 0 ? names : undefined;
|
|
11
|
+
};
|
|
12
|
+
const partsOf = (pkg, images) => {
|
|
13
|
+
const parts = {};
|
|
14
|
+
for (const [kind, image] of Object.entries(images))
|
|
15
|
+
parts[kind] = { image };
|
|
16
|
+
const store = normalizeParts(pkg.agentage.parts).store;
|
|
17
|
+
if (store)
|
|
18
|
+
parts['store'] = { kind: store };
|
|
19
|
+
return parts;
|
|
20
|
+
};
|
|
21
|
+
// From the manifest, the build and the pushed images by digest; OpenAPI, tools, widgets come later.
|
|
22
|
+
export const recordOf = (pkg, build, images) => {
|
|
23
|
+
const record = {
|
|
24
|
+
id: pkg.agentage.id,
|
|
25
|
+
version: pkg.version,
|
|
26
|
+
commit: build.commit,
|
|
27
|
+
buildTime: build.buildTime,
|
|
28
|
+
parts: partsOf(pkg, images),
|
|
29
|
+
commands: commandNames(pkg),
|
|
30
|
+
};
|
|
31
|
+
return JSON.parse(JSON.stringify(record));
|
|
32
|
+
};
|
|
33
|
+
export const validateRecord = (value) => {
|
|
34
|
+
const result = ExtensionRecordSchema.safeParse(value);
|
|
35
|
+
if (result.success)
|
|
36
|
+
return result.data;
|
|
37
|
+
throw new Error(`invalid extension record: ${formatIssues(result.error.issues)}`);
|
|
38
|
+
};
|
|
39
|
+
export const loadRecord = async (file) => {
|
|
40
|
+
let text;
|
|
41
|
+
try {
|
|
42
|
+
text = await readFile(file, 'utf8');
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return null; // no record mounted: discovery lists the part's own image by tag
|
|
46
|
+
}
|
|
47
|
+
return validateRecord(JSON.parse(text));
|
|
48
|
+
};
|
|
49
|
+
export const discoveryDocument = (record, host) => {
|
|
50
|
+
const { id, version, commit, buildTime, ...rest } = record;
|
|
51
|
+
return { id, version, commit, buildTime, host, ...rest };
|
|
52
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const ImageDigestRefSchema: z.ZodString;
|
|
3
|
+
export declare const PartsRecordSchema: z.ZodObject<{
|
|
4
|
+
pages: z.ZodOptional<z.ZodObject<{
|
|
5
|
+
image: z.ZodString;
|
|
6
|
+
}, z.core.$strict>>;
|
|
7
|
+
ui: z.ZodOptional<z.ZodObject<{
|
|
8
|
+
image: z.ZodString;
|
|
9
|
+
}, z.core.$strict>>;
|
|
10
|
+
api: z.ZodOptional<z.ZodObject<{
|
|
11
|
+
image: z.ZodString;
|
|
12
|
+
}, z.core.$strict>>;
|
|
13
|
+
mcp: z.ZodOptional<z.ZodObject<{
|
|
14
|
+
image: z.ZodString;
|
|
15
|
+
}, z.core.$strict>>;
|
|
16
|
+
worker: z.ZodOptional<z.ZodObject<{
|
|
17
|
+
image: z.ZodString;
|
|
18
|
+
}, z.core.$strict>>;
|
|
19
|
+
store: z.ZodOptional<z.ZodObject<{
|
|
20
|
+
kind: z.ZodEnum<{
|
|
21
|
+
postgres: "postgres";
|
|
22
|
+
git: "git";
|
|
23
|
+
signoz: "signoz";
|
|
24
|
+
}>;
|
|
25
|
+
}, z.core.$strict>>;
|
|
26
|
+
}, z.core.$strict>;
|
|
27
|
+
export declare const OriginSchema: z.ZodString;
|
|
28
|
+
export declare const ExtensionRecordSchema: z.ZodObject<{
|
|
29
|
+
id: z.ZodString;
|
|
30
|
+
version: z.ZodString;
|
|
31
|
+
commit: z.ZodString;
|
|
32
|
+
buildTime: z.ZodISODateTime;
|
|
33
|
+
parts: z.ZodObject<{
|
|
34
|
+
pages: z.ZodOptional<z.ZodObject<{
|
|
35
|
+
image: z.ZodString;
|
|
36
|
+
}, z.core.$strict>>;
|
|
37
|
+
ui: z.ZodOptional<z.ZodObject<{
|
|
38
|
+
image: z.ZodString;
|
|
39
|
+
}, z.core.$strict>>;
|
|
40
|
+
api: z.ZodOptional<z.ZodObject<{
|
|
41
|
+
image: z.ZodString;
|
|
42
|
+
}, z.core.$strict>>;
|
|
43
|
+
mcp: z.ZodOptional<z.ZodObject<{
|
|
44
|
+
image: z.ZodString;
|
|
45
|
+
}, z.core.$strict>>;
|
|
46
|
+
worker: z.ZodOptional<z.ZodObject<{
|
|
47
|
+
image: z.ZodString;
|
|
48
|
+
}, z.core.$strict>>;
|
|
49
|
+
store: z.ZodOptional<z.ZodObject<{
|
|
50
|
+
kind: z.ZodEnum<{
|
|
51
|
+
postgres: "postgres";
|
|
52
|
+
git: "git";
|
|
53
|
+
signoz: "signoz";
|
|
54
|
+
}>;
|
|
55
|
+
}, z.core.$strict>>;
|
|
56
|
+
}, z.core.$strict>;
|
|
57
|
+
openapi: z.ZodOptional<z.ZodString>;
|
|
58
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
59
|
+
widgets: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
60
|
+
name: z.ZodString;
|
|
61
|
+
element: z.ZodString;
|
|
62
|
+
module: z.ZodString;
|
|
63
|
+
}, z.core.$strict>>>;
|
|
64
|
+
commands: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
65
|
+
}, z.core.$strict>;
|
|
66
|
+
export declare const DiscoveryDocumentSchema: z.ZodObject<{
|
|
67
|
+
host: z.ZodString;
|
|
68
|
+
id: z.ZodString;
|
|
69
|
+
version: z.ZodString;
|
|
70
|
+
commit: z.ZodString;
|
|
71
|
+
buildTime: z.ZodISODateTime;
|
|
72
|
+
parts: z.ZodObject<{
|
|
73
|
+
pages: z.ZodOptional<z.ZodObject<{
|
|
74
|
+
image: z.ZodString;
|
|
75
|
+
}, z.core.$strict>>;
|
|
76
|
+
ui: z.ZodOptional<z.ZodObject<{
|
|
77
|
+
image: z.ZodString;
|
|
78
|
+
}, z.core.$strict>>;
|
|
79
|
+
api: z.ZodOptional<z.ZodObject<{
|
|
80
|
+
image: z.ZodString;
|
|
81
|
+
}, z.core.$strict>>;
|
|
82
|
+
mcp: z.ZodOptional<z.ZodObject<{
|
|
83
|
+
image: z.ZodString;
|
|
84
|
+
}, z.core.$strict>>;
|
|
85
|
+
worker: z.ZodOptional<z.ZodObject<{
|
|
86
|
+
image: z.ZodString;
|
|
87
|
+
}, z.core.$strict>>;
|
|
88
|
+
store: z.ZodOptional<z.ZodObject<{
|
|
89
|
+
kind: z.ZodEnum<{
|
|
90
|
+
postgres: "postgres";
|
|
91
|
+
git: "git";
|
|
92
|
+
signoz: "signoz";
|
|
93
|
+
}>;
|
|
94
|
+
}, z.core.$strict>>;
|
|
95
|
+
}, z.core.$strict>;
|
|
96
|
+
openapi: z.ZodOptional<z.ZodString>;
|
|
97
|
+
tools: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
98
|
+
widgets: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
99
|
+
name: z.ZodString;
|
|
100
|
+
element: z.ZodString;
|
|
101
|
+
module: z.ZodString;
|
|
102
|
+
}, z.core.$strict>>>;
|
|
103
|
+
commands: z.ZodOptional<z.ZodArray<z.ZodString>>;
|
|
104
|
+
}, z.core.$strict>;
|
|
105
|
+
export type ExtensionRecord = z.infer<typeof ExtensionRecordSchema>;
|
|
106
|
+
export type DiscoveryDocument = z.infer<typeof DiscoveryDocumentSchema>;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { CommandNameSchema, CommitSchema, ExtensionIdSchema, IMAGE_PART_KINDS, PrefixedToolNameSchema, StoreKindSchema, VersionSchema, } from '../manifest/primitives.schema.js';
|
|
3
|
+
// registry host[:port]/owner/{id}-{part}@sha256: deployed by digest, a version's images never change.
|
|
4
|
+
export const ImageDigestRefSchema = z
|
|
5
|
+
.string()
|
|
6
|
+
.max(256)
|
|
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
|
+
const PartImageSchema = z.strictObject({ image: ImageDigestRefSchema });
|
|
9
|
+
export const PartsRecordSchema = z
|
|
10
|
+
.strictObject({
|
|
11
|
+
pages: PartImageSchema.optional(),
|
|
12
|
+
ui: PartImageSchema.optional(),
|
|
13
|
+
api: PartImageSchema.optional(),
|
|
14
|
+
mcp: PartImageSchema.optional(),
|
|
15
|
+
worker: PartImageSchema.optional(),
|
|
16
|
+
store: z.strictObject({ kind: StoreKindSchema }).optional(),
|
|
17
|
+
})
|
|
18
|
+
.refine((parts) => IMAGE_PART_KINDS.some((kind) => parts[kind] !== undefined), {
|
|
19
|
+
message: 'at least one part with an image',
|
|
20
|
+
});
|
|
21
|
+
export const OriginSchema = z
|
|
22
|
+
.string()
|
|
23
|
+
.max(270)
|
|
24
|
+
.regex(/^(https:\/\/[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)+|http:\/\/(127\.0\.0\.1|localhost))(:[0-9]{1,5})?$/);
|
|
25
|
+
const WidgetSchema = z.strictObject({
|
|
26
|
+
name: z.string().regex(/^[a-z][a-z0-9-]*$/),
|
|
27
|
+
element: z.string().regex(/^[a-z][a-z0-9]*-[a-z0-9-]+$/),
|
|
28
|
+
module: z.string().regex(/^\/ui\/[^/]+\/[^/]+\.m?js$/),
|
|
29
|
+
});
|
|
30
|
+
const recordShape = {
|
|
31
|
+
id: ExtensionIdSchema,
|
|
32
|
+
version: VersionSchema,
|
|
33
|
+
commit: CommitSchema,
|
|
34
|
+
buildTime: z.iso.datetime(),
|
|
35
|
+
parts: PartsRecordSchema,
|
|
36
|
+
openapi: z
|
|
37
|
+
.string()
|
|
38
|
+
.regex(/^\/api\/v[1-9][0-9]*\/openapi\.json$/)
|
|
39
|
+
.optional(),
|
|
40
|
+
tools: z.array(PrefixedToolNameSchema).min(1).optional(),
|
|
41
|
+
widgets: z.array(WidgetSchema).min(1).optional(),
|
|
42
|
+
commands: z.array(CommandNameSchema).min(1).optional(),
|
|
43
|
+
};
|
|
44
|
+
const imagesNameTheirExtension = (doc) => IMAGE_PART_KINDS.every((kind) => {
|
|
45
|
+
const image = doc.parts[kind]?.image;
|
|
46
|
+
return image === undefined || image.includes(`/${doc.id}-${kind}@sha256:`);
|
|
47
|
+
});
|
|
48
|
+
const IMAGE_RULE = { message: 'every image is {id}-{part}', path: ['parts'] };
|
|
49
|
+
// The build record: written by `build --push` into the package and the release asset; no host.
|
|
50
|
+
export const ExtensionRecordSchema = z
|
|
51
|
+
.strictObject(recordShape)
|
|
52
|
+
.refine(imagesNameTheirExtension, IMAGE_RULE);
|
|
53
|
+
// What a deployment serves at /.well-known/agentage-extension.json: the record plus its host.
|
|
54
|
+
export const DiscoveryDocumentSchema = z
|
|
55
|
+
.strictObject({ ...recordShape, host: OriginSchema })
|
|
56
|
+
.refine(imagesNameTheirExtension, IMAGE_RULE);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const ERROR_STATUS: {
|
|
2
|
+
readonly VALIDATION_ERROR: 400;
|
|
3
|
+
readonly UNAUTHORIZED: 401;
|
|
4
|
+
readonly FORBIDDEN: 403;
|
|
5
|
+
readonly NOT_FOUND: 404;
|
|
6
|
+
readonly CONFLICT: 409;
|
|
7
|
+
readonly PRECONDITION_FAILED: 412;
|
|
8
|
+
readonly PAYLOAD_TOO_LARGE: 413;
|
|
9
|
+
readonly RATE_LIMITED: 429;
|
|
10
|
+
readonly INTERNAL: 500;
|
|
11
|
+
readonly UNAVAILABLE: 503;
|
|
12
|
+
};
|
|
13
|
+
export type ErrorCode = keyof typeof ERROR_STATUS;
|
|
14
|
+
export declare const ERROR_CODES: ErrorCode[];
|
|
15
|
+
export declare const RETRY_AFTER_CODES: readonly ErrorCode[];
|
|
16
|
+
export declare const DEFAULT_RETRY_AFTER_SECONDS = 30;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const ERROR_STATUS = {
|
|
2
|
+
VALIDATION_ERROR: 400,
|
|
3
|
+
UNAUTHORIZED: 401,
|
|
4
|
+
FORBIDDEN: 403,
|
|
5
|
+
NOT_FOUND: 404,
|
|
6
|
+
CONFLICT: 409,
|
|
7
|
+
PRECONDITION_FAILED: 412,
|
|
8
|
+
PAYLOAD_TOO_LARGE: 413,
|
|
9
|
+
RATE_LIMITED: 429,
|
|
10
|
+
INTERNAL: 500,
|
|
11
|
+
UNAVAILABLE: 503,
|
|
12
|
+
};
|
|
13
|
+
export const ERROR_CODES = Object.keys(ERROR_STATUS);
|
|
14
|
+
export const RETRY_AFTER_CODES = ['RATE_LIMITED', 'UNAVAILABLE'];
|
|
15
|
+
export const DEFAULT_RETRY_AFTER_SECONDS = 30;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type ErrorCode } from './codes.js';
|
|
2
|
+
export interface ErrorDetail {
|
|
3
|
+
field: string;
|
|
4
|
+
message: string;
|
|
5
|
+
}
|
|
6
|
+
export interface OkEnvelope<T> {
|
|
7
|
+
success: true;
|
|
8
|
+
data: T;
|
|
9
|
+
meta?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
export interface ErrorBody {
|
|
12
|
+
code: ErrorCode;
|
|
13
|
+
message: string;
|
|
14
|
+
agcid: string;
|
|
15
|
+
details?: ErrorDetail[];
|
|
16
|
+
}
|
|
17
|
+
export interface ErrorEnvelope {
|
|
18
|
+
success: false;
|
|
19
|
+
error: ErrorBody;
|
|
20
|
+
}
|
|
21
|
+
export interface Failure extends Error {
|
|
22
|
+
readonly code: ErrorCode;
|
|
23
|
+
readonly status: number;
|
|
24
|
+
readonly details?: ErrorDetail[];
|
|
25
|
+
readonly retryAfter?: number;
|
|
26
|
+
}
|
|
27
|
+
export interface FailOptions {
|
|
28
|
+
details?: ErrorDetail[];
|
|
29
|
+
retryAfter?: number;
|
|
30
|
+
}
|
|
31
|
+
export declare const ok: <T>(data: T, meta?: Record<string, unknown>) => OkEnvelope<T>;
|
|
32
|
+
export declare const fail: (code: ErrorCode, message: string, options?: FailOptions) => Failure;
|
|
33
|
+
export declare const isFailure: (err: unknown) => err is Failure;
|
|
34
|
+
export declare const errorEnvelope: (failure: Failure, agcid: string) => ErrorEnvelope;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DEFAULT_RETRY_AFTER_SECONDS, ERROR_STATUS, RETRY_AFTER_CODES, } from './codes.js';
|
|
2
|
+
const FAILURE = Symbol.for('agentage.extension.failure');
|
|
3
|
+
export const ok = (data, meta) => meta === undefined ? { success: true, data } : { success: true, data, meta };
|
|
4
|
+
const retryAfterOf = (code, given) => {
|
|
5
|
+
if (given !== undefined)
|
|
6
|
+
return given;
|
|
7
|
+
return RETRY_AFTER_CODES.includes(code) ? DEFAULT_RETRY_AFTER_SECONDS : undefined;
|
|
8
|
+
};
|
|
9
|
+
// Returned, not thrown, so a route reads `throw fail(...)`; the kit turns it into the envelope.
|
|
10
|
+
export const fail = (code, message, options = {}) => Object.assign(new Error(message), {
|
|
11
|
+
[FAILURE]: true,
|
|
12
|
+
code,
|
|
13
|
+
status: ERROR_STATUS[code],
|
|
14
|
+
details: options.details,
|
|
15
|
+
retryAfter: retryAfterOf(code, options.retryAfter),
|
|
16
|
+
});
|
|
17
|
+
export const isFailure = (err) => err instanceof Error && err[FAILURE] === true;
|
|
18
|
+
export const errorEnvelope = (failure, agcid) => {
|
|
19
|
+
const error = { code: failure.code, message: failure.message, agcid };
|
|
20
|
+
if (failure.details && failure.details.length > 0)
|
|
21
|
+
error.details = failure.details;
|
|
22
|
+
return { success: false, error };
|
|
23
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
export declare const AgcidSchema: z.ZodString;
|
|
3
|
+
export declare const ErrorCodeSchema: z.ZodEnum<{
|
|
4
|
+
[x: string]: string;
|
|
5
|
+
}>;
|
|
6
|
+
export declare const ErrorEnvelopeSchema: z.ZodObject<{
|
|
7
|
+
success: z.ZodLiteral<false>;
|
|
8
|
+
error: z.ZodObject<{
|
|
9
|
+
code: z.ZodEnum<{
|
|
10
|
+
[x: string]: string;
|
|
11
|
+
}>;
|
|
12
|
+
message: z.ZodString;
|
|
13
|
+
agcid: z.ZodString;
|
|
14
|
+
details: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
15
|
+
field: z.ZodString;
|
|
16
|
+
message: z.ZodString;
|
|
17
|
+
}, z.core.$strict>>>;
|
|
18
|
+
}, z.core.$strict>;
|
|
19
|
+
}, z.core.$strict>;
|
|
20
|
+
export declare const OkEnvelopeSchema: z.ZodObject<{
|
|
21
|
+
success: z.ZodLiteral<true>;
|
|
22
|
+
data: z.ZodUnknown;
|
|
23
|
+
meta: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodUnknown>>;
|
|
24
|
+
}, z.core.$strict>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { ERROR_CODES } from './codes.js';
|
|
3
|
+
export const AgcidSchema = z.string().regex(/^[A-Za-z0-9._:-]{8,128}$/);
|
|
4
|
+
export const ErrorCodeSchema = z.enum(ERROR_CODES);
|
|
5
|
+
export const ErrorEnvelopeSchema = z.strictObject({
|
|
6
|
+
success: z.literal(false),
|
|
7
|
+
error: z.strictObject({
|
|
8
|
+
code: ErrorCodeSchema,
|
|
9
|
+
message: z.string().min(1),
|
|
10
|
+
agcid: AgcidSchema,
|
|
11
|
+
details: z.array(z.strictObject({ field: z.string(), message: z.string() })).optional(),
|
|
12
|
+
}),
|
|
13
|
+
});
|
|
14
|
+
export const OkEnvelopeSchema = z.strictObject({
|
|
15
|
+
success: z.literal(true),
|
|
16
|
+
data: z.unknown(),
|
|
17
|
+
meta: z.record(z.string(), z.unknown()).optional(),
|
|
18
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
interface Issue {
|
|
2
|
+
readonly path: readonly PropertyKey[];
|
|
3
|
+
readonly message: string;
|
|
4
|
+
}
|
|
5
|
+
export interface ParseSchema<T> {
|
|
6
|
+
safeParse(value: unknown): {
|
|
7
|
+
success: true;
|
|
8
|
+
data: T;
|
|
9
|
+
} | {
|
|
10
|
+
success: false;
|
|
11
|
+
error: {
|
|
12
|
+
issues: readonly Issue[];
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export declare const parse: <T>(schema: ParseSchema<T>, value: unknown, message?: string) => T;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { fail } from './envelope.js';
|
|
2
|
+
const detailsOf = (issues) => issues.map((issue) => ({
|
|
3
|
+
field: issue.path.map((segment) => String(segment)).join('.') || '(root)',
|
|
4
|
+
message: issue.message,
|
|
5
|
+
}));
|
|
6
|
+
// Structural over Zod's safeParse, so an author's own zod copy works with the kit's.
|
|
7
|
+
export const parse = (schema, value, message = 'Invalid request') => {
|
|
8
|
+
const result = schema.safeParse(value);
|
|
9
|
+
if (result.success)
|
|
10
|
+
return result.data;
|
|
11
|
+
throw fail('VALIDATION_ERROR', message, { details: detailsOf(result.error.issues) });
|
|
12
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type RequestHandler } from 'express';
|
|
2
|
+
import { type BuildInfo } from '../discovery/discovery.js';
|
|
3
|
+
import { type HealthBody, type HealthStatus } from './health.schema.js';
|
|
4
|
+
export type HealthCheck = () => Promise<HealthStatus>;
|
|
5
|
+
export interface HealthOptions {
|
|
6
|
+
extension: string;
|
|
7
|
+
part: 'api' | 'mcp' | 'worker' | 'pages' | 'ui';
|
|
8
|
+
version: string;
|
|
9
|
+
build: BuildInfo;
|
|
10
|
+
checks?: Record<string, HealthCheck>;
|
|
11
|
+
instance?: string;
|
|
12
|
+
startedAt?: number;
|
|
13
|
+
now?: () => Date;
|
|
14
|
+
}
|
|
15
|
+
export declare const worstOf: (statuses: HealthStatus[]) => HealthStatus;
|
|
16
|
+
export declare const isLoopback: (address: string | undefined) => boolean;
|
|
17
|
+
export declare const healthBody: (options: HealthOptions) => Promise<HealthBody>;
|
|
18
|
+
export declare const createHealthHandler: (options: HealthOptions) => RequestHandler;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import {} from 'express';
|
|
3
|
+
import {} from '../discovery/discovery.js';
|
|
4
|
+
import { fail } from '../envelope/envelope.js';
|
|
5
|
+
import {} from './health.schema.js';
|
|
6
|
+
const RANK = { ok: 0, degraded: 1, unavailable: 2 };
|
|
7
|
+
export const worstOf = (statuses) => statuses.reduce((worst, s) => (RANK[s] > RANK[worst] ? s : worst), 'ok');
|
|
8
|
+
const runChecks = async (checks) => {
|
|
9
|
+
const entries = await Promise.all(Object.entries(checks).map(async ([name, check]) => {
|
|
10
|
+
const status = await check().catch(() => 'unavailable');
|
|
11
|
+
return [name, status];
|
|
12
|
+
}));
|
|
13
|
+
return Object.fromEntries(entries);
|
|
14
|
+
};
|
|
15
|
+
const isoSeconds = (date) => `${date.toISOString().slice(0, 19)}Z`;
|
|
16
|
+
export const isLoopback = (address) => address !== undefined && /^(127\.|::1$|::ffff:127\.)/.test(address);
|
|
17
|
+
export const healthBody = async (options) => {
|
|
18
|
+
const now = (options.now ?? (() => new Date()))();
|
|
19
|
+
const checks = await runChecks(options.checks ?? {});
|
|
20
|
+
const status = worstOf(Object.values(checks));
|
|
21
|
+
const { extension, part, version, build } = options;
|
|
22
|
+
const uptimeSeconds = Math.max(0, Math.floor((now.getTime() - (options.startedAt ?? 0)) / 1000));
|
|
23
|
+
const data = { status, extension, part, version, ...build, instance: options.instance ?? '' };
|
|
24
|
+
return {
|
|
25
|
+
success: status !== 'unavailable',
|
|
26
|
+
data: { ...data, uptimeSeconds, checkedAt: isoSeconds(now), checks },
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
// Answers only on loopback: the container HEALTHCHECK and an operator on the node, never the gateway.
|
|
30
|
+
export const createHealthHandler = (options) => {
|
|
31
|
+
const resolved = {
|
|
32
|
+
...options,
|
|
33
|
+
instance: options.instance ?? randomBytes(4).toString('hex'),
|
|
34
|
+
startedAt: options.startedAt ?? Date.now(),
|
|
35
|
+
};
|
|
36
|
+
return async (req, res) => {
|
|
37
|
+
if (!isLoopback(req.socket.localAddress))
|
|
38
|
+
throw fail('NOT_FOUND', 'Not found');
|
|
39
|
+
const body = await healthBody(resolved);
|
|
40
|
+
res.setHeader('Cache-Control', 'no-store');
|
|
41
|
+
res.status(body.data.status === 'unavailable' ? 503 : 200).json(body);
|
|
42
|
+
};
|
|
43
|
+
};
|