@agentage/extension 0.15.2 → 0.17.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/files-registry.d.ts +17 -0
- package/dist/api/files-registry.js +10 -0
- package/dist/api/files.d.ts +8 -0
- package/dist/api/files.js +82 -0
- package/dist/api/routes.d.ts +6 -0
- package/dist/api/routes.js +3 -1
- package/dist/auth/token-check.d.ts +1 -1
- package/dist/auth/verify-token.js +9 -9
- package/dist/context/test-context.d.ts +5 -1
- package/dist/context/test-context.js +2 -1
- package/dist/host/daemon/child.js +6 -4
- package/dist/host/daemon/health-routes.js +13 -8
- package/dist/host/daemon/routes.js +3 -1
- package/dist/host/daemon/seal.d.ts +6 -2
- package/dist/host/daemon/seal.js +9 -2
- package/dist/host/host-env.d.ts +1 -0
- package/dist/host/host-env.js +2 -0
- package/dist/host/host-extension.d.ts +1 -0
- package/dist/host/host-extension.js +7 -3
- package/dist/host/host-git-store.d.ts +10 -0
- package/dist/host/host-git-store.js +36 -0
- package/dist/host/host-migrations.d.ts +8 -0
- package/dist/host/host-migrations.js +62 -0
- package/dist/host/host-parts.d.ts +2 -2
- package/dist/host/host-parts.js +1 -1
- package/dist/host/host-url-store.d.ts +22 -0
- package/dist/host/host-url-store.js +62 -0
- package/dist/host/main.js +9 -3
- package/dist/host/serve-host.js +3 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/manifest/primitives.schema.js +1 -1
- package/dist/mcp/connector.d.ts +4 -0
- package/dist/mcp/connector.js +11 -0
- package/dist/mcp/mcp-app.d.ts +3 -1
- package/dist/mcp/mcp-app.js +27 -17
- package/dist/mcp/server.d.ts +6 -3
- package/dist/mcp/server.js +52 -14
- package/dist/mcp/tools.d.ts +22 -4
- package/dist/mcp/tools.js +22 -12
- package/dist/runtime/part-mounts.js +12 -6
- package/dist/server/api-routes.d.ts +5 -2
- package/dist/server/api-routes.js +8 -6
- package/dist/server/app.d.ts +3 -0
- package/dist/server/app.js +11 -1
- package/dist/store/clickhouse-http.d.ts +20 -0
- package/dist/store/clickhouse-http.js +90 -0
- package/dist/store/clickhouse-store.d.ts +22 -0
- package/dist/store/clickhouse-store.js +87 -0
- package/dist/store/git-env.d.ts +11 -0
- package/dist/store/git-env.js +44 -0
- package/dist/store/migrate.js +1 -3
- package/dist/store/store-url.d.ts +4 -0
- package/dist/store/store-url.js +30 -0
- package/dist/store/store.d.ts +1 -0
- package/dist/store/store.js +2 -0
- package/dist/store/store.types.d.ts +3 -0
- package/dist/store/store.types.js +1 -1
- package/dist/testing/test-server.js +11 -12
- package/dist/tool/container-recipe.d.ts +5 -2
- package/dist/tool/container-recipe.js +6 -5
- package/dist/tool/container-stage.js +1 -1
- package/dist/tool/record-extras.js +4 -3
- package/dist/tool/rules/store.js +0 -3
- package/dist/tool/rules/surface.js +4 -1
- package/package.json +1 -1
package/dist/server/app.js
CHANGED
|
@@ -3,6 +3,8 @@ import express, {} from 'express';
|
|
|
3
3
|
import helmet, {} from 'helmet';
|
|
4
4
|
import {} from '../api/well-known.js';
|
|
5
5
|
import { agcidMiddleware } from '../agcid/scope.js';
|
|
6
|
+
import { mountFiles } from '../api/files.js';
|
|
7
|
+
import {} from '../api/files-registry.js';
|
|
6
8
|
import { routeAccess, sameShape } from '../auth/route-access.js';
|
|
7
9
|
import { setCaller } from '../auth/token-check.js';
|
|
8
10
|
import { discoveryDocument, recordOf, } from '../discovery/discovery.js';
|
|
@@ -54,6 +56,11 @@ const healthHandler = (options) => createHealthHandler({
|
|
|
54
56
|
parts: options.parts,
|
|
55
57
|
clockRate: options.clockRate,
|
|
56
58
|
});
|
|
59
|
+
// RFC 9728 path form: the document of the resource at {path}, the one a 401 there names.
|
|
60
|
+
export const resourceDocAt = (app, path, resource) => {
|
|
61
|
+
const doc = { ...resource, resource: `${String(resource['resource'])}${path}` };
|
|
62
|
+
app.get(`/.well-known/oauth-protected-resource${path}`, (_req, res) => void res.json(doc));
|
|
63
|
+
};
|
|
57
64
|
// The kit app every part image runs: agcid, headers, /health, discovery, the part, the envelope.
|
|
58
65
|
export const createPartApp = (options) => {
|
|
59
66
|
const app = express();
|
|
@@ -107,7 +114,10 @@ const routeGuard = (options) => {
|
|
|
107
114
|
};
|
|
108
115
|
// The api routes on an app: the part's own (createApiApp) or the host's one app of all parts.
|
|
109
116
|
export const mountApiRoutes = (app, options) => {
|
|
110
|
-
const
|
|
117
|
+
const { id } = options.pkg.agentage;
|
|
118
|
+
if (options.files)
|
|
119
|
+
mountFiles(app, id, options.files, options.tokenCheck);
|
|
120
|
+
const prefixes = [apiPrefix(id), ...(options.aliases ?? [])];
|
|
111
121
|
app.use(prefixes, cors(PUBLIC_CORS));
|
|
112
122
|
for (const { name, render } of options.wellKnown ?? [])
|
|
113
123
|
app.get(`/.well-known/${name}`, wellKnownHandler(render));
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export interface ClickhouseConnection {
|
|
2
|
+
origin: string;
|
|
3
|
+
username: string;
|
|
4
|
+
password: string;
|
|
5
|
+
database: string;
|
|
6
|
+
}
|
|
7
|
+
export type ClickhouseParams = Record<string, unknown>;
|
|
8
|
+
export type ClickhouseSettings = Record<string, string | number>;
|
|
9
|
+
export interface ClickhouseCall {
|
|
10
|
+
sql: string;
|
|
11
|
+
params?: ClickhouseParams;
|
|
12
|
+
settings?: ClickhouseSettings;
|
|
13
|
+
body?: string;
|
|
14
|
+
signal?: AbortSignal;
|
|
15
|
+
timeoutMs: number;
|
|
16
|
+
}
|
|
17
|
+
export declare const connectionOf: (text: string) => ClickhouseConnection;
|
|
18
|
+
export declare const value: (v: unknown) => string;
|
|
19
|
+
export declare const request: (c: ClickhouseConnection, call: ClickhouseCall) => Promise<string>;
|
|
20
|
+
export declare const rowsOf: <R>(text: string) => R[];
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// The estate's form, http://logs:{pw}@logs_store:8123/logs; the URL was checked by readStoreUrl.
|
|
2
|
+
export const connectionOf = (text) => {
|
|
3
|
+
const url = new URL(text);
|
|
4
|
+
return {
|
|
5
|
+
origin: url.origin,
|
|
6
|
+
username: decodeURIComponent(url.username) || 'default',
|
|
7
|
+
password: decodeURIComponent(url.password),
|
|
8
|
+
database: decodeURIComponent(url.pathname.slice(1)) || 'default',
|
|
9
|
+
};
|
|
10
|
+
};
|
|
11
|
+
const ESCAPES = { '\\': '\\\\', '\t': '\\t', '\n': '\\n', "'": "\\'" };
|
|
12
|
+
const escaped = (s) => s.replace(/[\\\t\n']/g, (c) => ESCAPES[c] ?? c);
|
|
13
|
+
// Whole seconds fit DateTime and DateTime64; a Date with milliseconds needs DateTime64(3).
|
|
14
|
+
const epoch = (d) => {
|
|
15
|
+
const ms = d.getTime();
|
|
16
|
+
return ms % 1000 === 0 ? String(ms / 1000) : (ms / 1000).toFixed(3);
|
|
17
|
+
};
|
|
18
|
+
const inner = (v) => {
|
|
19
|
+
if (v === null || v === undefined)
|
|
20
|
+
return 'NULL';
|
|
21
|
+
if (typeof v === 'string')
|
|
22
|
+
return `'${escaped(v)}'`;
|
|
23
|
+
if (v instanceof Date)
|
|
24
|
+
return epoch(v);
|
|
25
|
+
return Array.isArray(v) ? `[${v.map(inner).join(',')}]` : value(v);
|
|
26
|
+
};
|
|
27
|
+
// A {name:Type} parameter in the escaped text form ClickHouse reads from param_<name>.
|
|
28
|
+
export const value = (v) => {
|
|
29
|
+
if (v === null || v === undefined)
|
|
30
|
+
return '\\N';
|
|
31
|
+
if (typeof v === 'string')
|
|
32
|
+
return escaped(v);
|
|
33
|
+
if (Array.isArray(v))
|
|
34
|
+
return `[${v.map(inner).join(',')}]`;
|
|
35
|
+
if (v instanceof Date)
|
|
36
|
+
return epoch(v);
|
|
37
|
+
if (typeof v === 'object')
|
|
38
|
+
return JSON.stringify(v);
|
|
39
|
+
return String(v);
|
|
40
|
+
};
|
|
41
|
+
const urlOf = (c, call) => {
|
|
42
|
+
const url = new URL('/', c.origin);
|
|
43
|
+
url.searchParams.set('database', c.database);
|
|
44
|
+
for (const [k, v] of Object.entries(call.settings ?? {}))
|
|
45
|
+
url.searchParams.set(k, String(v));
|
|
46
|
+
for (const [k, v] of Object.entries(call.params ?? {}))
|
|
47
|
+
url.searchParams.set(`param_${k}`, value(v));
|
|
48
|
+
if (call.body !== undefined)
|
|
49
|
+
url.searchParams.set('query', call.sql);
|
|
50
|
+
return url;
|
|
51
|
+
};
|
|
52
|
+
// ClickHouse's own code (159, 243, ...) on err.code, as @clickhouse/client gives it.
|
|
53
|
+
const failure = async (res) => {
|
|
54
|
+
const text = (await res.text()).trim();
|
|
55
|
+
const code = res.headers.get('x-clickhouse-exception-code') ?? /Code: (\d+)/.exec(text)?.[1];
|
|
56
|
+
// No ClickHouse code: a proxy or the edge answered, never the server itself.
|
|
57
|
+
return Object.assign(new Error(text || `clickhouse answered ${res.status}`), {
|
|
58
|
+
code: code ?? `HTTP_${res.status}`,
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const signalOf = (call) => {
|
|
62
|
+
const timeout = AbortSignal.timeout(call.timeoutMs);
|
|
63
|
+
return call.signal ? AbortSignal.any([call.signal, timeout]) : timeout;
|
|
64
|
+
};
|
|
65
|
+
// One POST; the answer's text, or an Error carrying ClickHouse's code.
|
|
66
|
+
export const request = async (c, call) => {
|
|
67
|
+
const res = await fetch(urlOf(c, call), {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
headers: { 'X-ClickHouse-User': c.username, 'X-ClickHouse-Key': c.password },
|
|
70
|
+
body: call.body ?? call.sql,
|
|
71
|
+
signal: signalOf(call),
|
|
72
|
+
});
|
|
73
|
+
if (!res.ok)
|
|
74
|
+
throw await failure(res);
|
|
75
|
+
return res.text();
|
|
76
|
+
};
|
|
77
|
+
// An error after the first rows comes in the body of a 200: never read it as a row.
|
|
78
|
+
const rowOf = (line) => {
|
|
79
|
+
try {
|
|
80
|
+
return JSON.parse(line);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
const code = /Code: (\d+)/.exec(line)?.[1] ?? 'BAD_ROW';
|
|
84
|
+
throw Object.assign(new Error(line.slice(0, 500)), { code });
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
export const rowsOf = (text) => text
|
|
88
|
+
.split('\n')
|
|
89
|
+
.filter((line) => line.trim())
|
|
90
|
+
.map((line) => rowOf(line));
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { type Log } from '../log/log.js';
|
|
2
|
+
import { type ClickhouseParams, type ClickhouseSettings } from './clickhouse-http.js';
|
|
3
|
+
import { type QueryResult, type Row } from './store.types.js';
|
|
4
|
+
export interface ClickhouseOptions {
|
|
5
|
+
settings?: ClickhouseSettings;
|
|
6
|
+
signal?: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export interface ClickhouseStore {
|
|
9
|
+
readonly kind: 'clickhouse';
|
|
10
|
+
query<R extends Row = Row>(sql: string, params?: ClickhouseParams | unknown[], options?: ClickhouseOptions): Promise<QueryResult<R>>;
|
|
11
|
+
command(sql: string, options?: ClickhouseOptions): Promise<void>;
|
|
12
|
+
insert(table: string, rows: object[], options?: ClickhouseOptions): Promise<void>;
|
|
13
|
+
ping(): Promise<boolean>;
|
|
14
|
+
close(): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
export declare const isClickhouseOutage: (err: unknown) => boolean;
|
|
17
|
+
export declare const createClickhouseStore: (url: string, log: Log, timeoutMs?: number) => ClickhouseStore;
|
|
18
|
+
export declare const isClickhouseStore: (store: unknown) => store is ClickhouseStore;
|
|
19
|
+
export declare const NO_CLICKHOUSE_STORE: ClickhouseStore;
|
|
20
|
+
export declare const clickhouseOf: (ctx: {
|
|
21
|
+
readonly store: unknown;
|
|
22
|
+
}) => ClickhouseStore;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { fail } from '../envelope/envelope.js';
|
|
2
|
+
import { messageOf } from '../log/log.js';
|
|
3
|
+
import { connectionOf, request, rowsOf, } from './clickhouse-http.js';
|
|
4
|
+
import {} from './store.types.js';
|
|
5
|
+
// Counts as JSON numbers; the whole answer before the status, so a late error is a 500, not a 200.
|
|
6
|
+
const READS = {
|
|
7
|
+
output_format_json_quote_64bit_integers: 0,
|
|
8
|
+
wait_end_of_query: 1,
|
|
9
|
+
};
|
|
10
|
+
const TABLE = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)?$/;
|
|
11
|
+
const PING_MS = 900;
|
|
12
|
+
const OUTAGE = /^(ECONNREFUSED|ECONNRESET|ETIMEDOUT|ENOTFOUND|EAI_AGAIN|EHOSTUNREACH|EPIPE)$/;
|
|
13
|
+
// 159 timeout, 202 too many queries; a proxy's 502-504 with no ClickHouse code.
|
|
14
|
+
const BUSY = /^(159|202|HTTP_50[234])$/;
|
|
15
|
+
const codeOf = (err) => {
|
|
16
|
+
const own = err?.code;
|
|
17
|
+
const cause = err?.cause?.code;
|
|
18
|
+
return String(own ?? cause ?? '');
|
|
19
|
+
};
|
|
20
|
+
// undici: "fetch failed" before the answer, "terminated" when the socket closes mid-body.
|
|
21
|
+
const NETWORK = (err) => err instanceof TypeError && /^(fetch failed|terminated)$/.test(err.message);
|
|
22
|
+
// A store that cannot answer now is UNAVAILABLE, as in store.ts; a caller's abort and a query error pass through.
|
|
23
|
+
export const isClickhouseOutage = (err) => OUTAGE.test(codeOf(err)) ||
|
|
24
|
+
BUSY.test(codeOf(err)) ||
|
|
25
|
+
NETWORK(err) ||
|
|
26
|
+
err?.name === 'TimeoutError';
|
|
27
|
+
const guarded = (log) => async (work) => {
|
|
28
|
+
try {
|
|
29
|
+
return await work();
|
|
30
|
+
}
|
|
31
|
+
catch (err) {
|
|
32
|
+
if (!isClickhouseOutage(err))
|
|
33
|
+
throw err;
|
|
34
|
+
log.warn('store unavailable', { reason: messageOf(err) });
|
|
35
|
+
throw fail('UNAVAILABLE', 'The store is unavailable');
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
const named = (params) => {
|
|
39
|
+
if (!Array.isArray(params))
|
|
40
|
+
return params;
|
|
41
|
+
if (params.length === 0)
|
|
42
|
+
return undefined;
|
|
43
|
+
throw new Error('a clickhouse query takes named parameters: {name:Type} and an object');
|
|
44
|
+
};
|
|
45
|
+
const ndjson = (rows) => rows.map((row) => JSON.stringify(row)).join('\n');
|
|
46
|
+
const queryOf = (call) => async (sql, params, o = {}) => {
|
|
47
|
+
const { settings, signal } = o;
|
|
48
|
+
const all = { default_format: 'JSONEachRow', ...READS, ...settings };
|
|
49
|
+
return { rows: rowsOf(await call({ sql, params: named(params), settings: all, signal })) };
|
|
50
|
+
};
|
|
51
|
+
const insertOf = (call) => async (table, rows, o = {}) => {
|
|
52
|
+
if (!TABLE.test(table))
|
|
53
|
+
throw new Error(`not a table name: ${table}`);
|
|
54
|
+
await call({ sql: `INSERT INTO ${table} FORMAT JSONEachRow`, body: ndjson(rows), ...o });
|
|
55
|
+
};
|
|
56
|
+
// A store client over the HTTP interface; nothing connects until the first call.
|
|
57
|
+
export const createClickhouseStore = (url, log, timeoutMs = 10_000) => {
|
|
58
|
+
const at = connectionOf(url);
|
|
59
|
+
const run = guarded(log);
|
|
60
|
+
const call = (c) => run(() => request(at, { ...c, timeoutMs }));
|
|
61
|
+
const ping = () => request(at, { sql: 'SELECT 1', timeoutMs: PING_MS });
|
|
62
|
+
const store = {
|
|
63
|
+
kind: 'clickhouse',
|
|
64
|
+
query: queryOf(call),
|
|
65
|
+
command: async (sql, o = {}) => void (await call({ sql, ...o })),
|
|
66
|
+
insert: insertOf(call),
|
|
67
|
+
ping: () => ping().then(() => true, () => false),
|
|
68
|
+
close: async () => undefined,
|
|
69
|
+
};
|
|
70
|
+
return store;
|
|
71
|
+
};
|
|
72
|
+
export const isClickhouseStore = (store) => store?.kind === 'clickhouse';
|
|
73
|
+
const NO_CLICKHOUSE = 'no clickhouse store: declare parts.store "clickhouse" and set AGENTAGE_STORE_URL_FILE';
|
|
74
|
+
const missing = async () => {
|
|
75
|
+
throw new Error(NO_CLICKHOUSE);
|
|
76
|
+
};
|
|
77
|
+
// Says so on first use, as ctx.store does: the check and test contexts run create() with no store.
|
|
78
|
+
export const NO_CLICKHOUSE_STORE = Object.freeze({
|
|
79
|
+
kind: 'clickhouse',
|
|
80
|
+
query: missing,
|
|
81
|
+
command: missing,
|
|
82
|
+
insert: missing,
|
|
83
|
+
ping: async () => false,
|
|
84
|
+
close: async () => undefined,
|
|
85
|
+
});
|
|
86
|
+
// ctx.store as the clickhouse face; the PartContext type stays as it is.
|
|
87
|
+
export const clickhouseOf = (ctx) => isClickhouseStore(ctx.store) ? ctx.store : NO_CLICKHOUSE_STORE;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface GitAccess {
|
|
2
|
+
readonly remote?: string;
|
|
3
|
+
readonly cwd: string;
|
|
4
|
+
readonly env: Readonly<Record<string, string>>;
|
|
5
|
+
}
|
|
6
|
+
export declare const splitCredential: (url: string) => {
|
|
7
|
+
remote: string;
|
|
8
|
+
header?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const gitEnvOf: (root: string, remote?: string, header?: string) => Record<string, string>;
|
|
11
|
+
export declare const gitAccessOf: (root: string, url?: string) => GitAccess;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { inspect } from 'node:util';
|
|
2
|
+
// The URL of the estate file holds user:password; git gets it as a header, never in a URL or argv.
|
|
3
|
+
export const splitCredential = (url) => {
|
|
4
|
+
const parsed = new URL(url);
|
|
5
|
+
const user = decodeURIComponent(parsed.username);
|
|
6
|
+
const password = decodeURIComponent(parsed.password);
|
|
7
|
+
parsed.username = '';
|
|
8
|
+
parsed.password = '';
|
|
9
|
+
if (!user && !password)
|
|
10
|
+
return { remote: parsed.href };
|
|
11
|
+
const basic = Buffer.from(`${user}:${password}`).toString('base64');
|
|
12
|
+
return { remote: parsed.href, header: `Authorization: Basic ${basic}` };
|
|
13
|
+
};
|
|
14
|
+
// No host or user config, no hooks, no ext:: transport, no prompt; only PATH from the parent.
|
|
15
|
+
const configOf = (remote, header) => [
|
|
16
|
+
['core.hooksPath', '/dev/null'],
|
|
17
|
+
...(remote && header ? [[`http.${remote}.extraHeader`, header]] : []),
|
|
18
|
+
];
|
|
19
|
+
export const gitEnvOf = (root, remote, header) => {
|
|
20
|
+
const config = configOf(remote, header);
|
|
21
|
+
const env = {
|
|
22
|
+
PATH: process.env['PATH'] ?? '/usr/bin:/bin',
|
|
23
|
+
HOME: root,
|
|
24
|
+
GIT_CONFIG_NOSYSTEM: '1',
|
|
25
|
+
GIT_CONFIG_GLOBAL: '/dev/null',
|
|
26
|
+
GIT_TERMINAL_PROMPT: '0',
|
|
27
|
+
GIT_ALLOW_PROTOCOL: 'file:http:https',
|
|
28
|
+
GIT_CONFIG_COUNT: String(config.length),
|
|
29
|
+
};
|
|
30
|
+
config.forEach(([key, value], i) => {
|
|
31
|
+
env[`GIT_CONFIG_KEY_${i}`] = key;
|
|
32
|
+
env[`GIT_CONFIG_VALUE_${i}`] = value;
|
|
33
|
+
});
|
|
34
|
+
return env;
|
|
35
|
+
};
|
|
36
|
+
// A log line or a JSON dump of ctx.store shows no env, so it shows no credential.
|
|
37
|
+
export const gitAccessOf = (root, url) => {
|
|
38
|
+
const { remote, header } = url ? splitCredential(url) : { remote: undefined, header: undefined };
|
|
39
|
+
const shown = { ...(remote && { remote }), cwd: root, env: '[redacted]' };
|
|
40
|
+
return Object.defineProperties({ ...(remote && { remote }), cwd: root, env: gitEnvOf(root, remote, header) }, {
|
|
41
|
+
toJSON: { value: () => shown },
|
|
42
|
+
[inspect.custom]: { value: () => shown },
|
|
43
|
+
});
|
|
44
|
+
};
|
package/dist/store/migrate.js
CHANGED
|
@@ -2,9 +2,7 @@
|
|
|
2
2
|
import { createLog } from '../log/log.js';
|
|
3
3
|
import { readManifest } from '../manifest/read-manifest.js';
|
|
4
4
|
import { migrate } from './migrations.js';
|
|
5
|
-
import { createStore } from './store.js';
|
|
6
|
-
// A migration may rewrite a table: minutes, not a request's seconds; the job itself has no retry.
|
|
7
|
-
const MIGRATE_TIMEOUTS = { connectMs: 10_000, statementMs: 300_000 };
|
|
5
|
+
import { createStore, MIGRATE_TIMEOUTS } from './store.js';
|
|
8
6
|
// The api image's migrate job (`node dist/store/migrate.js`); lines before the manifest read 'unknown'.
|
|
9
7
|
let log = createLog({ extension: 'unknown', part: 'migrate' });
|
|
10
8
|
try {
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const STORE_URL_FILE_ENV = "AGENTAGE_STORE_URL_FILE";
|
|
2
|
+
export type UrlStoreKind = 'postgres' | 'clickhouse';
|
|
3
|
+
export declare const isUrlStoreKind: (kind: string | undefined) => kind is UrlStoreKind;
|
|
4
|
+
export declare const readStoreUrl: (file: string, kind: UrlStoreKind | "git", read?: (at: string) => Promise<string>) => Promise<string>;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { messageOf } from '../log/log.js';
|
|
3
|
+
export const STORE_URL_FILE_ENV = 'AGENTAGE_STORE_URL_FILE';
|
|
4
|
+
// git reads its URL too, as the remote of its repos (the host opens the repos in dataDir).
|
|
5
|
+
const SCHEMES = {
|
|
6
|
+
postgres: { protocol: /^postgres(ql)?:$/, form: 'postgres://' },
|
|
7
|
+
clickhouse: { protocol: /^https?:$/, form: 'http:// or https://' },
|
|
8
|
+
git: { protocol: /^https?:$/, form: 'http:// or https://' },
|
|
9
|
+
};
|
|
10
|
+
export const isUrlStoreKind = (kind) => kind === 'postgres' || kind === 'clickhouse';
|
|
11
|
+
const refused = (file, why) => new Error(`${STORE_URL_FILE_ENV} ${file}: ${why}`);
|
|
12
|
+
const readText = async (file, read) => {
|
|
13
|
+
try {
|
|
14
|
+
return (await read(file)).trim();
|
|
15
|
+
}
|
|
16
|
+
catch (err) {
|
|
17
|
+
throw refused(file, `cannot read it (${err.code ?? messageOf(err)})`);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
// Every error names the variable and the file, never the URL: it holds a credential.
|
|
21
|
+
export const readStoreUrl = async (file, kind, read = (at) => readFile(at, 'utf8')) => {
|
|
22
|
+
const text = await readText(file, read);
|
|
23
|
+
if (!text)
|
|
24
|
+
throw refused(file, 'the file is empty');
|
|
25
|
+
const { protocol, form } = SCHEMES[kind];
|
|
26
|
+
const url = URL.parse(text);
|
|
27
|
+
if (!url || !protocol.test(url.protocol) || !url.hostname)
|
|
28
|
+
throw refused(file, `holds no ${form} URL for the ${kind} store`);
|
|
29
|
+
return text;
|
|
30
|
+
};
|
package/dist/store/store.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface StoreTimeouts {
|
|
|
10
10
|
statementMs: number;
|
|
11
11
|
}
|
|
12
12
|
export declare const PART_TIMEOUTS: StoreTimeouts;
|
|
13
|
+
export declare const MIGRATE_TIMEOUTS: StoreTimeouts;
|
|
13
14
|
export declare const storeUrl: (env: Env, read?: (file: string) => string) => string | undefined;
|
|
14
15
|
export declare const isOutage: (err: unknown) => boolean;
|
|
15
16
|
export declare const createStore: (env: Env, log: Log, timeouts?: StoreTimeouts) => PoolStore;
|
package/dist/store/store.js
CHANGED
|
@@ -7,6 +7,8 @@ import {} from '../runtime/build-info.js';
|
|
|
7
7
|
import {} from './store.types.js';
|
|
8
8
|
// A part's query is bounded by the server and, if the network itself hangs, by the client too.
|
|
9
9
|
export const PART_TIMEOUTS = { connectMs: 5_000, statementMs: 10_000 };
|
|
10
|
+
// A migration may rewrite a table: minutes, not a request's seconds.
|
|
11
|
+
export const MIGRATE_TIMEOUTS = { connectMs: 10_000, statementMs: 300_000 };
|
|
10
12
|
const CLIENT_SLACK_MS = 2_000;
|
|
11
13
|
// The host sets the connection: AGENTAGE_STORE_URL, a secret file named by _FILE, else libpq PG* env.
|
|
12
14
|
export const storeUrl = (env, read = (file) => readFileSync(file, 'utf8')) => {
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { type GitAccess } from './git-env.js';
|
|
1
2
|
export type Row = Record<string, unknown>;
|
|
2
3
|
export interface QueryResult<R extends Row = Row> {
|
|
3
4
|
rows: R[];
|
|
4
5
|
}
|
|
5
6
|
export interface Store {
|
|
6
7
|
query<R extends Row = Row>(text: string, params?: unknown[]): Promise<QueryResult<R>>;
|
|
8
|
+
readonly gitRoot?: string;
|
|
9
|
+
readonly git?: GitAccess;
|
|
7
10
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
import {} from './git-env.js';
|
|
@@ -6,11 +6,11 @@ import { callerOf } from '../auth/token-check.js';
|
|
|
6
6
|
import {} from '../context/context.js';
|
|
7
7
|
import { createTestContext } from '../context/test-context.js';
|
|
8
8
|
import {} from '../manifest/manifest.schema.js';
|
|
9
|
-
import {
|
|
9
|
+
import { mountWithConnector } from '../mcp/connector.js';
|
|
10
10
|
import { callTool } from '../mcp/snapshot.js';
|
|
11
11
|
import { createToolRegistry, prefixed } from '../mcp/tools.js';
|
|
12
12
|
import { mountApi } from '../server/api-routes.js';
|
|
13
|
-
import { createApiApp } from '../server/app.js';
|
|
13
|
+
import { createApiApp, createPartApp } from '../server/app.js';
|
|
14
14
|
import {} from '../server/part.types.js';
|
|
15
15
|
import { listen } from '../server/serve.js';
|
|
16
16
|
import { KIT_VERSION } from '../tool/kit-version.js';
|
|
@@ -42,22 +42,21 @@ export const startTestServer = async (options) => {
|
|
|
42
42
|
tokenCheck: callerCheck(ctx.extension.id, options.caller ?? null),
|
|
43
43
|
openapi: generatedOpenapi(pkg, mounted.routes),
|
|
44
44
|
};
|
|
45
|
-
const
|
|
45
|
+
const { documents: wellKnown, files } = mounted;
|
|
46
|
+
const base = { pkg, build: TEST_BUILD, log: ctx.log, wellKnown, files };
|
|
46
47
|
return startOn(createApiApp({ ...base, router: mounted.router, routes: mounted.routes, ...guard }));
|
|
47
48
|
};
|
|
48
|
-
// Runs an mcp part's create(ctx) inside the kit's real /mcp app: tools named {id}__{name}.
|
|
49
|
+
// Runs an mcp part's create(ctx) inside the kit's real /mcp app: tools named {id}__{name}; connector tools at /connector.
|
|
49
50
|
export const startTestMcp = async (options) => {
|
|
50
51
|
const ctx = options.ctx ?? createTestContext();
|
|
51
52
|
const registry = createToolRegistry();
|
|
52
|
-
|
|
53
|
+
const { tool, instructions, connector } = registry;
|
|
54
|
+
await options.create({ ...ctx, tool, instructions, connector });
|
|
53
55
|
const pkg = testPackage(ctx, { mcp: true });
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
tools: registry.tools,
|
|
59
|
-
listing: 'open',
|
|
60
|
-
});
|
|
56
|
+
const base = { pkg, build: TEST_BUILD, log: ctx.log, tools: registry.tools };
|
|
57
|
+
const mcp = { ...base, listing: 'open', ...registry.served };
|
|
58
|
+
const mount = (app) => mountWithConnector(app, mcp, registry.connectors);
|
|
59
|
+
const app = createPartApp({ ...mcp, part: 'mcp', mount });
|
|
61
60
|
const server = await startOn(app);
|
|
62
61
|
const call = (name, args = {}) => callTool(`${server.url}/mcp`, prefixed(ctx.extension.id, name), args);
|
|
63
62
|
return { ...server, call };
|
|
@@ -32,17 +32,18 @@ const runnerEnv = (store) => [
|
|
|
32
32
|
' COMMIT_SHA=$COMMIT_SHA \\',
|
|
33
33
|
' BUILD_TIME=$BUILD_TIME',
|
|
34
34
|
];
|
|
35
|
-
const runner = (
|
|
36
|
-
...runnerEnv(
|
|
35
|
+
const runner = ({ pglite, git }) => [
|
|
36
|
+
...runnerEnv(pglite !== undefined || Boolean(git)),
|
|
37
|
+
...(git ? ['RUN apk add --no-cache git'] : []),
|
|
37
38
|
'WORKDIR /app',
|
|
38
39
|
'COPY --from=deps /app/node_modules ./node_modules',
|
|
39
|
-
...(
|
|
40
|
+
...(pglite !== undefined
|
|
40
41
|
? [
|
|
41
42
|
'COPY --from=pglite /pglite/node_modules/@electric-sql/pglite ./node_modules/@electric-sql/pglite',
|
|
42
43
|
]
|
|
43
44
|
: []),
|
|
44
45
|
'COPY app/ ./',
|
|
45
|
-
...(
|
|
46
|
+
...(pglite !== undefined || git
|
|
46
47
|
? [`RUN mkdir -p ${DATA_VOLUME} && chown node:node ${DATA_VOLUME}`, `VOLUME ${DATA_VOLUME}`]
|
|
47
48
|
: []),
|
|
48
49
|
'USER node',
|
|
@@ -52,4 +53,4 @@ const runner = (store) => [
|
|
|
52
53
|
'',
|
|
53
54
|
];
|
|
54
55
|
// The one image of an extension: the kit host in one process; pglite is set for a postgres store.
|
|
55
|
-
export const containerRecipe = (
|
|
56
|
+
export const containerRecipe = (stores) => [...DEPS, ...(stores.pglite ? pgliteStage(stores.pglite) : []), ...runner(stores)].join('\n');
|
|
@@ -46,6 +46,6 @@ export const stageContainer = async (dir, pkg, tmpRoot = tmpdir()) => {
|
|
|
46
46
|
for (const path of await containerPaths(dir, pkg))
|
|
47
47
|
await cp(join(dir, path), join(root, 'app', path), { recursive: true, filter });
|
|
48
48
|
const pglite = hasStore(pkg, 'postgres') ? await pgliteVersion(dir) : undefined;
|
|
49
|
-
await writeFile(join(root, 'Dockerfile'), containerRecipe({ pglite }));
|
|
49
|
+
await writeFile(join(root, 'Dockerfile'), containerRecipe({ pglite, git: hasStore(pkg, 'git') }));
|
|
50
50
|
return root;
|
|
51
51
|
};
|
|
@@ -31,14 +31,15 @@ export const recordingContext = (pkg, part) => createContext({
|
|
|
31
31
|
});
|
|
32
32
|
export const registeredTools = async (create, pkg) => {
|
|
33
33
|
const registry = createToolRegistry();
|
|
34
|
-
|
|
34
|
+
const { tool, instructions, connector } = registry;
|
|
35
|
+
await create({ ...recordingContext(pkg, 'mcp'), tool, instructions, connector });
|
|
35
36
|
return registry.tools;
|
|
36
37
|
};
|
|
37
38
|
// What an api part's create(ctx) registers with ctx.route, and what it returns.
|
|
38
39
|
export const registeredRoutes = async (create, pkg) => {
|
|
39
40
|
const registry = createRouteRegistry();
|
|
40
|
-
const { route, wellKnown } = registry;
|
|
41
|
-
const returned = await create({ ...recordingContext(pkg, 'api'), route, wellKnown });
|
|
41
|
+
const { route, wellKnown, files } = registry;
|
|
42
|
+
const returned = await create({ ...recordingContext(pkg, 'api'), route, wellKnown, files });
|
|
42
43
|
return { routes: registry.routes, returned };
|
|
43
44
|
};
|
|
44
45
|
export const toolsOf = async (dir, pkg) => {
|
package/dist/tool/rules/store.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { readFile } from 'node:fs/promises';
|
|
2
2
|
import { join, resolve } from 'node:path';
|
|
3
3
|
import { migratesStore, storeOf } from '../../manifest/parts.js';
|
|
4
|
-
import { SERVED_KINDS } from '../../manifest/primitives.schema.js';
|
|
5
4
|
import { sqlFiles } from '../../store/migrations.js';
|
|
6
5
|
import {} from '../check.js';
|
|
7
6
|
import {} from '../exec.js';
|
|
@@ -45,8 +44,6 @@ export const store = {
|
|
|
45
44
|
const kind = storeOf(pkg);
|
|
46
45
|
if (!kind)
|
|
47
46
|
return { skipped: 'no store' };
|
|
48
|
-
if (!SERVED_KINDS.includes(kind))
|
|
49
|
-
return [{ problem: `kind ${kind} is not served yet; served: ${SERVED_KINDS.join(', ')}` }];
|
|
50
47
|
if (!migratesStore(pkg))
|
|
51
48
|
return { skipped: `kind ${kind} has no migrations` };
|
|
52
49
|
const files = await sqlFiles(join(dir, 'migrations'));
|
|
@@ -71,11 +71,14 @@ const apiSurface = async ({ dir, pkg }) => {
|
|
|
71
71
|
: routerProblems(pkg, doc, returned);
|
|
72
72
|
return found.map((problem) => ({ file: OPENAPI_FILE, problem }));
|
|
73
73
|
};
|
|
74
|
+
// As the server lists it: title, annotations and outputSchema only when set.
|
|
74
75
|
const listed = (id, { name, spec }) => ({
|
|
75
76
|
name: prefixed(id, name),
|
|
77
|
+
...(spec.title !== undefined && { title: spec.title }),
|
|
76
78
|
description: spec.description,
|
|
77
79
|
inputSchema: schemaOf(spec.input, 'input'),
|
|
78
|
-
outputSchema: schemaOf(spec.output, 'output'),
|
|
80
|
+
...(spec.output && { outputSchema: schemaOf(spec.output, 'output') }),
|
|
81
|
+
...(spec.annotations && { annotations: spec.annotations }),
|
|
79
82
|
});
|
|
80
83
|
const byName = ({ serverInfo, tools }) => ({
|
|
81
84
|
serverInfo,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentage/extension",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.0",
|
|
4
4
|
"description": "The agentage extension kit: create(ctx) per part (pages, ui, api, mcp, worker, cli), the store, /health, discovery, the envelope, and the build tool with up and token",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|