@agentage/extension 0.6.1 → 0.8.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/agcid/current.d.ts +2 -0
- package/dist/agcid/current.js +1 -0
- package/dist/agcid/fetch.d.ts +1 -0
- package/dist/agcid/fetch.js +11 -0
- package/dist/agcid/scope.js +4 -2
- package/dist/api/openapi-doc.js +55 -19
- package/dist/api/page.d.ts +14 -0
- package/dist/api/page.js +33 -0
- package/dist/api/routes.d.ts +18 -8
- package/dist/api/routes.js +4 -2
- package/dist/api/well-known.d.ts +13 -0
- package/dist/api/well-known.js +13 -0
- package/dist/auth/revoked.js +2 -1
- package/dist/auth/token-check.d.ts +1 -1
- package/dist/auth/token-check.js +8 -4
- package/dist/auth/verify-token.d.ts +2 -1
- package/dist/auth/verify-token.js +3 -2
- package/dist/command/check-cli.js +3 -2
- package/dist/command/cli-context.d.ts +15 -0
- package/dist/command/cli-context.js +34 -0
- package/dist/command/command.types.d.ts +11 -0
- package/dist/command/command.types.js +3 -0
- package/dist/command/run-command.d.ts +4 -1
- package/dist/command/run-command.js +12 -8
- package/dist/context/context.js +1 -1
- package/dist/context/recording-log.d.ts +7 -0
- package/dist/context/recording-log.js +10 -0
- package/dist/context/test-command-context.d.ts +34 -0
- package/dist/context/test-command-context.js +51 -0
- package/dist/context/test-context.d.ts +6 -23
- package/dist/context/test-context.js +7 -35
- package/dist/envelope/envelope.d.ts +12 -0
- package/dist/envelope/envelope.js +8 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +3 -1
- package/dist/log/log.d.ts +3 -0
- package/dist/log/log.js +24 -7
- package/dist/mcp/snapshot.js +2 -1
- package/dist/mcp/tools.d.ts +1 -1
- package/dist/runtime/part-runners.js +2 -2
- package/dist/runtime/run-part.js +1 -1
- package/dist/server/api-routes.d.ts +2 -0
- package/dist/server/api-routes.js +14 -7
- package/dist/server/app.d.ts +2 -0
- package/dist/server/app.js +15 -0
- package/dist/server/request-line.d.ts +4 -0
- package/dist/server/request-line.js +25 -0
- package/dist/store/kit-migrations.d.ts +7 -0
- package/dist/store/kit-migrations.js +18 -0
- package/dist/store/migrate.js +1 -1
- package/dist/store/migrations.js +14 -6
- package/dist/testing/test-caller.d.ts +2 -0
- package/dist/testing/test-caller.js +13 -0
- package/dist/testing/test-server.d.ts +2 -0
- package/dist/testing/test-server.js +15 -4
- package/dist/tool/record-extras.js +2 -1
- package/dist/worker/runs.d.ts +1 -1
- package/dist/worker/runs.js +7 -12
- package/dist/worker/scheduler.js +8 -1
- package/package.json +1 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { mkdtempSync } from 'node:fs';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import {} from '../command/args.js';
|
|
5
|
+
import {} from '../auth/token-check.js';
|
|
6
|
+
import {} from '../command/command.types.js';
|
|
7
|
+
import { createOut } from '../command/out.js';
|
|
8
|
+
import { packageExtension } from './package-extension.js';
|
|
9
|
+
import { recordingLog } from './recording-log.js';
|
|
10
|
+
// One recorded line per line written, the trailing newline dropped.
|
|
11
|
+
const lines = (into) => (text) => void into.push(...text.replace(/\n$/, '').split('\n'));
|
|
12
|
+
const recordingClient = (calls, answers = {}) => ({
|
|
13
|
+
api: async (method, path, input) => {
|
|
14
|
+
calls.push({ kind: 'api', method, path, ...(input === undefined ? {} : { input }) });
|
|
15
|
+
return answers.api?.(method, path, input);
|
|
16
|
+
},
|
|
17
|
+
tool: async (name, args) => {
|
|
18
|
+
calls.push({ kind: 'tool', name, ...(args === undefined ? {} : { args }) });
|
|
19
|
+
return answers.tool?.(name, args);
|
|
20
|
+
},
|
|
21
|
+
});
|
|
22
|
+
// A throwaway owner-only folder, made on first read like the real one.
|
|
23
|
+
const lazyDir = () => {
|
|
24
|
+
let dir;
|
|
25
|
+
return () => (dir ??= mkdtempSync(join(tmpdir(), 'agentage-cli-')));
|
|
26
|
+
};
|
|
27
|
+
const recorded = (options) => {
|
|
28
|
+
const output = { stdout: [], stderr: [] };
|
|
29
|
+
const json = options.json ?? false;
|
|
30
|
+
const out = createOut({ json, stdout: lines(output.stdout), stderr: lines(output.stderr) });
|
|
31
|
+
return { out, output, logs: [], calls: [] };
|
|
32
|
+
};
|
|
33
|
+
export const commandContext = (options) => {
|
|
34
|
+
const { out, output, logs, calls } = recorded(options);
|
|
35
|
+
const { id = 'test', version = '0.0.0' } = packageExtension();
|
|
36
|
+
const extension = { id, version, ...options.extension, part: 'cli' };
|
|
37
|
+
const fixedUser = { sub: 'usr_test', scopes: [`${extension.id}:read`] };
|
|
38
|
+
const dir = lazyDir();
|
|
39
|
+
return {
|
|
40
|
+
...{ extension, args: { ...options.args }, out, output, logs, calls },
|
|
41
|
+
log: recordingLog(logs),
|
|
42
|
+
user: options.user === undefined ? fixedUser : options.user,
|
|
43
|
+
client: recordingClient(calls, options.client),
|
|
44
|
+
get dir() {
|
|
45
|
+
return dir();
|
|
46
|
+
},
|
|
47
|
+
get exitCode() {
|
|
48
|
+
return out.exitCode;
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
};
|
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
import { type RouteEntry, type RouteRegistrar } from '../api/routes.js';
|
|
2
|
-
import { type
|
|
3
|
-
import { type LogFields, type LogLevel } from '../log/log.js';
|
|
2
|
+
import { type WellKnownRegistrar } from '../api/well-known.js';
|
|
4
3
|
import { type ToolEntry, type ToolRegistrar } from '../mcp/tools.js';
|
|
5
4
|
import { type JobOptions } from '../worker/job-options.js';
|
|
6
5
|
import { type JobRun } from '../worker/runs.js';
|
|
7
6
|
import { type Schedule } from '../worker/scheduler.js';
|
|
8
7
|
import { type ExtensionInfo, type PartContext, type SettingValue } from './context.js';
|
|
9
|
-
import { type
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
fields: LogFields;
|
|
14
|
-
}
|
|
8
|
+
import { type LogEntry } from './recording-log.js';
|
|
9
|
+
import { type TestCommandContext, type TestCommandOptions } from './test-command-context.js';
|
|
10
|
+
export { type LogEntry } from './recording-log.js';
|
|
11
|
+
export { type ClientCall, type TestCommandContext, type TestCommandOptions, } from './test-command-context.js';
|
|
15
12
|
export interface TestContext extends PartContext {
|
|
16
13
|
readonly logs: LogEntry[];
|
|
17
14
|
close(): Promise<void>;
|
|
@@ -20,6 +17,7 @@ export interface TestContext extends PartContext {
|
|
|
20
17
|
readonly tools: ToolEntry[];
|
|
21
18
|
readonly route: RouteRegistrar;
|
|
22
19
|
readonly routes: RouteEntry[];
|
|
20
|
+
readonly wellKnown: WellKnownRegistrar;
|
|
23
21
|
readonly jobs: ScheduledJob[];
|
|
24
22
|
runJob(name: string): Promise<JobRun | undefined>;
|
|
25
23
|
}
|
|
@@ -28,13 +26,6 @@ export interface ScheduledJob {
|
|
|
28
26
|
cron: string;
|
|
29
27
|
options?: JobOptions;
|
|
30
28
|
}
|
|
31
|
-
export interface TestCommandContext extends CliContext {
|
|
32
|
-
readonly output: {
|
|
33
|
-
stdout: string[];
|
|
34
|
-
stderr: string[];
|
|
35
|
-
};
|
|
36
|
-
readonly exitCode: number | undefined;
|
|
37
|
-
}
|
|
38
29
|
export interface TestContextOptions {
|
|
39
30
|
extension?: Partial<ExtensionInfo>;
|
|
40
31
|
settings?: Record<string, SettingValue>;
|
|
@@ -43,13 +34,5 @@ export interface TestContextOptions {
|
|
|
43
34
|
dir: string;
|
|
44
35
|
};
|
|
45
36
|
}
|
|
46
|
-
export interface TestCommandOptions {
|
|
47
|
-
args: Record<string, ArgValue>;
|
|
48
|
-
extension?: {
|
|
49
|
-
id?: string;
|
|
50
|
-
version?: string;
|
|
51
|
-
};
|
|
52
|
-
json?: boolean;
|
|
53
|
-
}
|
|
54
37
|
export declare function createTestContext(options: TestCommandOptions): TestCommandContext;
|
|
55
38
|
export declare function createTestContext(options?: TestContextOptions): TestContext;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { createRouteRegistry } from '../api/routes.js';
|
|
2
|
-
import {} from '../
|
|
3
|
-
import {
|
|
4
|
-
import { messageOf } from '../log/log.js';
|
|
2
|
+
import {} from '../api/well-known.js';
|
|
3
|
+
import {} from '../log/log.js';
|
|
5
4
|
import { createToolRegistry } from '../mcp/tools.js';
|
|
6
5
|
import { migrate } from '../store/migrations.js';
|
|
7
6
|
import { createTestStore } from '../store/test-store.js';
|
|
@@ -10,18 +9,12 @@ import { runLog } from '../worker/run-log.js';
|
|
|
10
9
|
import { memoryRuns } from '../worker/runs.js';
|
|
11
10
|
import { createScheduler } from '../worker/scheduler.js';
|
|
12
11
|
import {} from '../manifest/primitives.schema.js';
|
|
13
|
-
import { packageExtension } from './package-extension.js';
|
|
14
12
|
import { NO_STORE } from './context.js';
|
|
15
|
-
import {} from '
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
level: 'error',
|
|
21
|
-
message: messageOf(error),
|
|
22
|
-
fields: { ...fields, err: error },
|
|
23
|
-
}),
|
|
24
|
-
});
|
|
13
|
+
import { packageExtension } from './package-extension.js';
|
|
14
|
+
import { recordingLog } from './recording-log.js';
|
|
15
|
+
import { commandContext, } from './test-command-context.js';
|
|
16
|
+
export {} from './recording-log.js';
|
|
17
|
+
export {} from './test-command-context.js';
|
|
25
18
|
const testStore = (option, log) => {
|
|
26
19
|
if (!option)
|
|
27
20
|
return undefined;
|
|
@@ -79,27 +72,6 @@ const serverContext = (options) => {
|
|
|
79
72
|
...recorders(log),
|
|
80
73
|
};
|
|
81
74
|
};
|
|
82
|
-
// One recorded line per line written, the trailing newline dropped.
|
|
83
|
-
const lines = (into) => (text) => void into.push(...text.replace(/\n$/, '').split('\n'));
|
|
84
|
-
const commandContext = (options) => {
|
|
85
|
-
const output = { stdout: [], stderr: [] };
|
|
86
|
-
const out = createOut({
|
|
87
|
-
json: options.json ?? false,
|
|
88
|
-
stdout: lines(output.stdout),
|
|
89
|
-
stderr: lines(output.stderr),
|
|
90
|
-
});
|
|
91
|
-
const { id = 'test', version = '0.0.0' } = packageExtension();
|
|
92
|
-
const extension = { id, version, ...options.extension, part: 'cli' };
|
|
93
|
-
return {
|
|
94
|
-
extension,
|
|
95
|
-
args: { ...options.args },
|
|
96
|
-
out,
|
|
97
|
-
output,
|
|
98
|
-
get exitCode() {
|
|
99
|
-
return out.exitCode;
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
};
|
|
103
75
|
export function createTestContext(options = {}) {
|
|
104
76
|
return 'args' in options ? commandContext(options) : serverContext(options);
|
|
105
77
|
}
|
|
@@ -30,7 +30,19 @@ export interface FailOptions {
|
|
|
30
30
|
retryAfter?: number;
|
|
31
31
|
headers?: Record<string, string>;
|
|
32
32
|
}
|
|
33
|
+
declare const REPLY: unique symbol;
|
|
34
|
+
export interface ReplyOptions {
|
|
35
|
+
meta?: Record<string, unknown>;
|
|
36
|
+
etag?: string;
|
|
37
|
+
}
|
|
38
|
+
export interface Reply<T> extends ReplyOptions {
|
|
39
|
+
readonly [REPLY]: true;
|
|
40
|
+
data: T;
|
|
41
|
+
}
|
|
42
|
+
export declare const reply: <T>(data: T, options?: ReplyOptions) => Reply<T>;
|
|
43
|
+
export declare const isReply: (value: unknown) => value is Reply<unknown>;
|
|
33
44
|
export declare const ok: <T>(data: T, meta?: Record<string, unknown>) => OkEnvelope<T>;
|
|
34
45
|
export declare const fail: (code: ErrorCode, message: string, options?: FailOptions) => Failure;
|
|
35
46
|
export declare const isFailure: (err: unknown) => err is Failure;
|
|
36
47
|
export declare const errorEnvelope: (failure: Failure, agcid: string) => ErrorEnvelope;
|
|
48
|
+
export {};
|
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { DEFAULT_RETRY_AFTER_SECONDS, ERROR_STATUS, RETRY_AFTER_CODES, } from './codes.js';
|
|
2
2
|
// A registry symbol, so a failure thrown through a second copy of the kit is still recognised.
|
|
3
3
|
const FAILURE = Symbol.for('agentage.extension.failure');
|
|
4
|
+
const REPLY = Symbol.for('agentage.extension.reply');
|
|
5
|
+
// What a route returns when it answers with meta or an ETag; bare data stays the short form.
|
|
6
|
+
export const reply = (data, options = {}) => ({
|
|
7
|
+
[REPLY]: true,
|
|
8
|
+
data,
|
|
9
|
+
...options,
|
|
10
|
+
});
|
|
11
|
+
export const isReply = (value) => typeof value === 'object' && value !== null && value[REPLY] === true;
|
|
4
12
|
export const ok = (data, meta) => meta === undefined ? { success: true, data } : { success: true, data, meta };
|
|
5
13
|
const retryAfterOf = (code, given) => {
|
|
6
14
|
if (given !== undefined)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
export { type ApiContext, type ParamsOf, type RouteHandler, type RouteMethod, type RouteRegistrar, type RouteRequest, type RouteSpec, } from './api/routes.js';
|
|
2
|
+
export { pageOf, PageQuerySchema, type Pagination } from './api/page.js';
|
|
3
|
+
export { type Access, type Caller } from './auth/token-check.js';
|
|
2
4
|
export { type ArgValue } from './command/args.js';
|
|
3
|
-
export { type CliContext, type CliCreate, type CommandExtension, type CommandHandler, } from './command/command.types.js';
|
|
5
|
+
export { type CliClient, type CliContext, type CliCreate, type CommandExtension, type CommandHandler, } from './command/command.types.js';
|
|
4
6
|
export { type Out } from './command/out.js';
|
|
5
7
|
export { type ExtensionInfo, type PartContext, type ServerPartKind, type SettingValue, } from './context/context.js';
|
|
6
8
|
export { type Secrets } from './context/secrets.js';
|
|
7
|
-
export { createTestContext, type LogEntry, type ScheduledJob, type TestCommandContext, type TestCommandOptions, type TestContext, type TestContextOptions, } from './context/test-context.js';
|
|
9
|
+
export { createTestContext, type ClientCall, type LogEntry, type ScheduledJob, type TestCommandContext, type TestCommandOptions, type TestContext, type TestContextOptions, } from './context/test-context.js';
|
|
8
10
|
export { DiscoveryDocumentSchema, ExtensionRecordSchema, type DiscoveryDocument, type ExtensionRecord, } from './discovery/discovery.schema.js';
|
|
9
11
|
export { AGCID_HEADER, isAgcid } from './agcid/agcid.js';
|
|
10
12
|
export { ERROR_CODES, ERROR_STATUS, type ErrorCode } from './envelope/codes.js';
|
|
11
|
-
export { fail, isFailure, ok, type ErrorDetail, type ErrorEnvelope, type Failure, type FailOptions, type OkEnvelope, } from './envelope/envelope.js';
|
|
13
|
+
export { fail, isFailure, ok, reply, type ErrorDetail, type ErrorEnvelope, type Failure, type FailOptions, type OkEnvelope, type Reply, type ReplyOptions, } from './envelope/envelope.js';
|
|
12
14
|
export { ErrorEnvelopeSchema, OkEnvelopeSchema } from './envelope/envelope.schema.js';
|
|
13
15
|
export { parse, type ParseSchema } from './envelope/parse.js';
|
|
14
16
|
export { HealthSchema, type HealthBody, type HealthStatus } from './health/health.schema.js';
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export {} from './api/routes.js';
|
|
2
|
+
export { pageOf, PageQuerySchema } from './api/page.js';
|
|
3
|
+
export {} from './auth/token-check.js';
|
|
2
4
|
export {} from './command/args.js';
|
|
3
5
|
export {} from './command/command.types.js';
|
|
4
6
|
export {} from './command/out.js';
|
|
@@ -8,7 +10,7 @@ export { createTestContext, } from './context/test-context.js';
|
|
|
8
10
|
export { DiscoveryDocumentSchema, ExtensionRecordSchema, } from './discovery/discovery.schema.js';
|
|
9
11
|
export { AGCID_HEADER, isAgcid } from './agcid/agcid.js';
|
|
10
12
|
export { ERROR_CODES, ERROR_STATUS } from './envelope/codes.js';
|
|
11
|
-
export { fail, isFailure, ok, } from './envelope/envelope.js';
|
|
13
|
+
export { fail, isFailure, ok, reply, } from './envelope/envelope.js';
|
|
12
14
|
export { ErrorEnvelopeSchema, OkEnvelopeSchema } from './envelope/envelope.schema.js';
|
|
13
15
|
export { parse } from './envelope/parse.js';
|
|
14
16
|
export { HealthSchema } from './health/health.schema.js';
|
package/dist/log/log.d.ts
CHANGED
|
@@ -8,7 +8,10 @@ export interface Log {
|
|
|
8
8
|
export interface LogTags {
|
|
9
9
|
extension: string;
|
|
10
10
|
part: string;
|
|
11
|
+
version?: string;
|
|
12
|
+
base?: LogFields;
|
|
11
13
|
}
|
|
12
14
|
export type LogWrite = (line: string) => void;
|
|
13
15
|
export declare const messageOf: (error: unknown) => string;
|
|
14
16
|
export declare const createLog: (tags: LogTags, write?: LogWrite) => Log;
|
|
17
|
+
export declare const logAt: (log: Log, level: LogLevel, message: string, fields: LogFields) => void;
|
package/dist/log/log.js
CHANGED
|
@@ -9,13 +9,30 @@ const errorFields = (error) => {
|
|
|
9
9
|
const line = (level, message, tags, fields) => {
|
|
10
10
|
const agcid = currentAgcid();
|
|
11
11
|
const ids = { 'extension.id': tags.extension, 'extension.part': tags.part };
|
|
12
|
-
const
|
|
12
|
+
const version = tags.version === undefined ? {} : { version: tags.version };
|
|
13
|
+
const own = { time: new Date().toISOString(), level, msg: message, ...ids, ...version };
|
|
13
14
|
const fixed = agcid ? { ...own, agcid } : own;
|
|
14
|
-
return `${JSON.stringify({ ...fixed, ...fields, ...fixed })}\n`;
|
|
15
|
+
return `${JSON.stringify({ ...fixed, ...tags.base, ...fields, ...fixed })}\n`;
|
|
15
16
|
};
|
|
16
17
|
const stderrWrite = (text) => void process.stderr.write(text);
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
})
|
|
18
|
+
const raw = new WeakMap();
|
|
19
|
+
export const createLog = (tags, write = stderrWrite) => {
|
|
20
|
+
const at = (level, message, fields) => write(line(level, message, tags, fields));
|
|
21
|
+
const log = {
|
|
22
|
+
info: (message, fields = {}) => at('info', message, fields),
|
|
23
|
+
warn: (message, fields = {}) => at('warn', message, fields),
|
|
24
|
+
error: (error, fields = {}) => at('error', messageOf(error), { ...fields, ...errorFields(error) }),
|
|
25
|
+
};
|
|
26
|
+
raw.set(log, at);
|
|
27
|
+
return log;
|
|
28
|
+
};
|
|
29
|
+
// A line at any level with no err field (an error-level line that is not an error event).
|
|
30
|
+
export const logAt = (log, level, message, fields) => {
|
|
31
|
+
const at = raw.get(log);
|
|
32
|
+
if (at)
|
|
33
|
+
at(level, message, fields);
|
|
34
|
+
else if (level === 'error')
|
|
35
|
+
log.error(message, fields);
|
|
36
|
+
else
|
|
37
|
+
log[level](message, fields);
|
|
38
|
+
};
|
package/dist/mcp/snapshot.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { kitFetch } from '../agcid/fetch.js';
|
|
1
2
|
export const PROTOCOL_VERSIONS = ['2025-11-25', '2026-07-28'];
|
|
2
3
|
const CLIENT = { name: 'agentage-snapshot', version: '1.0.0' };
|
|
3
4
|
// Key order never counts: the two eras serialize the same schema in different orders.
|
|
@@ -44,7 +45,7 @@ const post = async (url, version, method, params) => {
|
|
|
44
45
|
method,
|
|
45
46
|
params: modern ? { ...params, _meta: META } : params,
|
|
46
47
|
};
|
|
47
|
-
return payload(await
|
|
48
|
+
return payload(await kitFetch(url, { method: 'POST', headers, body: JSON.stringify(body) }));
|
|
48
49
|
};
|
|
49
50
|
const serverInfoOf = async (url, version) => {
|
|
50
51
|
if (version === '2025-11-25') {
|
package/dist/mcp/tools.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface ToolSpec<I extends ObjectSchema, O extends ObjectSchema> {
|
|
|
6
6
|
description: string;
|
|
7
7
|
input: I;
|
|
8
8
|
output: O;
|
|
9
|
-
scope?: Access
|
|
9
|
+
scope?: Exclude<Access, 'optional'>;
|
|
10
10
|
}
|
|
11
11
|
export type ToolHandler<I extends ObjectSchema, O extends ObjectSchema> = (args: z.output<I>) => z.input<O> | Promise<z.input<O>>;
|
|
12
12
|
export type ToolRegistrar = <I extends ObjectSchema, O extends ObjectSchema>(name: string, spec: ToolSpec<I, O>, handler: ToolHandler<I, O>) => void;
|
|
@@ -20,10 +20,10 @@ import { createScheduler } from '../worker/scheduler.js';
|
|
|
20
20
|
const callCreate = async (input, part, ctx) => (await loadCreate(input.dir, input.pkg, part, input.importModule))(ctx);
|
|
21
21
|
const api = async (input) => {
|
|
22
22
|
const ctx = input.ctx();
|
|
23
|
-
const { router, routes } = await mountApi(ctx, (api) => callCreate(input, 'api', api));
|
|
23
|
+
const { router, routes, documents } = await mountApi(ctx, (api) => callCreate(input, 'api', api));
|
|
24
24
|
// Declared routes guard at their own scope, whatever the committed file says.
|
|
25
25
|
const openapi = routes.length > 0 ? generatedOpenapi(input.pkg, routes) : input.openapi;
|
|
26
|
-
return { app: createApiApp({ ...input.base, router, openapi }) };
|
|
26
|
+
return { app: createApiApp({ ...input.base, router, openapi, wellKnown: documents }) };
|
|
27
27
|
};
|
|
28
28
|
const mcp = async (input) => {
|
|
29
29
|
const registry = createToolRegistry();
|
package/dist/runtime/run-part.js
CHANGED
|
@@ -89,7 +89,7 @@ const inputFor = async (options, start) => {
|
|
|
89
89
|
export const runPart = async (options) => {
|
|
90
90
|
const pkg = await readManifest(options.dir);
|
|
91
91
|
const part = partOf(pkg, options.part);
|
|
92
|
-
const log = createLog({ extension: pkg.agentage.id, part });
|
|
92
|
+
const log = createLog({ extension: pkg.agentage.id, part, version: pkg.version });
|
|
93
93
|
const names = part === 'ui' ? [] : (pkg.agentage.secrets ?? []);
|
|
94
94
|
const secrets = await readSecrets(names, options.secretsDir ?? SECRETS_DIR, part);
|
|
95
95
|
const start = { pkg, part, env: options.env, log, secrets };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { type ApiContext, type RouteEntry } from '../api/routes.js';
|
|
2
|
+
import { type WellKnownEntry } from '../api/well-known.js';
|
|
2
3
|
import { type PartContext } from '../context/context.js';
|
|
3
4
|
import { type ApiHandler } from './part.types.js';
|
|
4
5
|
export declare const mountApi: (ctx: PartContext, create: (ctx: ApiContext) => unknown) => Promise<{
|
|
5
6
|
router: ApiHandler;
|
|
6
7
|
routes: RouteEntry[];
|
|
8
|
+
documents: WellKnownEntry[];
|
|
7
9
|
}>;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { Router } from 'express';
|
|
2
2
|
import { createRouteRegistry } from '../api/routes.js';
|
|
3
|
+
import {} from '../api/well-known.js';
|
|
3
4
|
import {} from '../context/context.js';
|
|
4
|
-
import { ok } from '../envelope/envelope.js';
|
|
5
|
+
import { isReply, ok, reply } from '../envelope/envelope.js';
|
|
5
6
|
import { parse } from '../envelope/parse.js';
|
|
6
7
|
import {} from './part.types.js';
|
|
7
8
|
const inQuery = (method) => method === 'get' || method === 'delete';
|
|
@@ -12,12 +13,18 @@ const inputOf = ({ method, spec }, query, body) => {
|
|
|
12
13
|
return parse(spec.input, query, 'Invalid query');
|
|
13
14
|
return parse(spec.input, body ?? {}, 'Invalid body');
|
|
14
15
|
};
|
|
15
|
-
// Validated in, the handler's
|
|
16
|
+
// Validated in, the handler's data (bare or a reply) parsed by its output schema and sent in the envelope.
|
|
16
17
|
const handlerOf = (ctx, entry) => async (req, res) => {
|
|
17
18
|
const input = inputOf(entry, req.query, req.body);
|
|
18
19
|
const caller = ctx.auth.caller(req);
|
|
19
|
-
const
|
|
20
|
-
|
|
20
|
+
const { params: schema } = entry.spec;
|
|
21
|
+
const params = schema ? parse(schema, req.params, 'Invalid path') : req.params;
|
|
22
|
+
const ifMatch = req.get('if-match');
|
|
23
|
+
const answer = await entry.handler({ input, params, caller, ifMatch });
|
|
24
|
+
const { data, meta, etag } = isReply(answer) ? answer : reply(answer);
|
|
25
|
+
if (etag)
|
|
26
|
+
res.setHeader('ETag', etag);
|
|
27
|
+
res.json(ok(entry.spec.output ? entry.spec.output.parse(data) : data, meta));
|
|
21
28
|
};
|
|
22
29
|
// /items/{id} as Express writes it.
|
|
23
30
|
const expressPath = (path) => path.replace(/\{(\w+)\}/g, ':$1');
|
|
@@ -39,7 +46,7 @@ const handlerFor = (ctx, routes, returned) => {
|
|
|
39
46
|
};
|
|
40
47
|
// Runs the api part's create(ctx) with ctx.route; the runtime and the test server mount alike.
|
|
41
48
|
export const mountApi = async (ctx, create) => {
|
|
42
|
-
const { route, routes } = createRouteRegistry();
|
|
43
|
-
const returned = await create({ ...ctx, route });
|
|
44
|
-
return { router: handlerFor(ctx, routes, returned), routes };
|
|
49
|
+
const { route, routes, wellKnown, documents } = createRouteRegistry();
|
|
50
|
+
const returned = await create({ ...ctx, route, wellKnown });
|
|
51
|
+
return { router: handlerFor(ctx, routes, returned), routes, documents };
|
|
45
52
|
};
|
package/dist/server/app.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type CorsOptions } from 'cors';
|
|
2
2
|
import { type Express } from 'express';
|
|
3
3
|
import { type HelmetOptions } from 'helmet';
|
|
4
|
+
import { type WellKnownEntry } from '../api/well-known.js';
|
|
4
5
|
import { type TokenCheck } from '../auth/token-check.js';
|
|
5
6
|
import { type BuildInfo } from '../discovery/discovery.js';
|
|
6
7
|
import { type ExtensionRecord } from '../discovery/discovery.schema.js';
|
|
@@ -28,6 +29,7 @@ export interface PartAppOptions extends PartAppBase {
|
|
|
28
29
|
export interface ApiAppOptions extends PartAppBase {
|
|
29
30
|
router: ApiHandler;
|
|
30
31
|
openapi?: Record<string, unknown> | null;
|
|
32
|
+
wellKnown?: WellKnownEntry[];
|
|
31
33
|
}
|
|
32
34
|
export declare const PUBLIC_CORS: CorsOptions;
|
|
33
35
|
export declare const BODY_LIMIT = "1mb";
|
package/dist/server/app.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import cors, {} from 'cors';
|
|
2
2
|
import express, {} from 'express';
|
|
3
3
|
import helmet, {} from 'helmet';
|
|
4
|
+
import {} from '../api/well-known.js';
|
|
4
5
|
import { agcidMiddleware } from '../agcid/scope.js';
|
|
5
6
|
import { routeAccess } from '../auth/route-access.js';
|
|
6
7
|
import { setCaller } from '../auth/token-check.js';
|
|
@@ -11,6 +12,7 @@ import {} from '../log/log.js';
|
|
|
11
12
|
import {} from '../manifest/manifest.schema.js';
|
|
12
13
|
import {} from '../manifest/primitives.schema.js';
|
|
13
14
|
import { errorHandler, notFound } from './errors.js';
|
|
15
|
+
import { requestLine } from './request-line.js';
|
|
14
16
|
import {} from './part.types.js';
|
|
15
17
|
// api-standards section 6: CORS * without credentials on /.well-known/, /api/, /ui/ and /mcp.
|
|
16
18
|
export const PUBLIC_CORS = {
|
|
@@ -47,6 +49,8 @@ export const createPartApp = (options) => {
|
|
|
47
49
|
app.set('trust proxy', 'loopback, linklocal, uniquelocal');
|
|
48
50
|
app.use(dropCookie, agcidMiddleware, helmet(options.helmet));
|
|
49
51
|
app.get('/health', healthHandler(options));
|
|
52
|
+
// After /health: it is probed every few seconds and never gets a request line.
|
|
53
|
+
app.use(requestLine(options.log));
|
|
50
54
|
if (options.part !== 'worker') {
|
|
51
55
|
app.use('/.well-known', cors(PUBLIC_CORS));
|
|
52
56
|
app.get('/.well-known/agentage-extension.json', discoveryHandler(options));
|
|
@@ -61,6 +65,15 @@ export const createPartApp = (options) => {
|
|
|
61
65
|
};
|
|
62
66
|
// The committed OpenAPI document, bare JSON and anonymous like discovery (api-standards section 1).
|
|
63
67
|
const openapiHandler = (doc) => (_req, res) => void res.json(doc);
|
|
68
|
+
// Public like discovery, cached briefly: crawlers and registries read these from the root.
|
|
69
|
+
const wellKnownHandler = (render) => async (_req, res) => {
|
|
70
|
+
const doc = await render();
|
|
71
|
+
res.setHeader('Cache-Control', 'public, max-age=300');
|
|
72
|
+
if (typeof doc === 'string')
|
|
73
|
+
res.type('text/plain').send(doc);
|
|
74
|
+
else
|
|
75
|
+
res.json(doc);
|
|
76
|
+
};
|
|
64
77
|
// Before the body is read: each request checked for the access its OpenAPI operation declares.
|
|
65
78
|
const routeGuard = (options) => async (req, res, next) => {
|
|
66
79
|
if (!options.tokenCheck)
|
|
@@ -77,6 +90,8 @@ export const createApiApp = (options) => createPartApp({
|
|
|
77
90
|
part: 'api',
|
|
78
91
|
mount: (app) => {
|
|
79
92
|
app.use('/api/v1', cors(PUBLIC_CORS));
|
|
93
|
+
for (const { name, render } of options.wellKnown ?? [])
|
|
94
|
+
app.get(`/.well-known/${name}`, wellKnownHandler(render));
|
|
80
95
|
if (options.openapi)
|
|
81
96
|
app.get('/api/v1/openapi.json', openapiHandler(options.openapi));
|
|
82
97
|
const json = express.json({ limit: BODY_LIMIT });
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {} from 'express';
|
|
2
|
+
import { agcidOfResponse } from '../agcid/scope.js';
|
|
3
|
+
import { logAt } from '../log/log.js';
|
|
4
|
+
const MOUNTS = ['/api/v1', '/mcp', '/.well-known', '/ui'];
|
|
5
|
+
const levelOf = (status) => status >= 500 ? 'error' : status >= 400 ? 'warn' : 'info';
|
|
6
|
+
// The matched route template, never the raw URL, so no id or query string reaches the log.
|
|
7
|
+
export const routeOf = (req) => {
|
|
8
|
+
const url = req.originalUrl.split('?')[0] ?? '';
|
|
9
|
+
const prefix = MOUNTS.find((m) => url === m || url.startsWith(`${m}/`)) ?? '/';
|
|
10
|
+
const path = req.route?.path;
|
|
11
|
+
if (typeof path !== 'string')
|
|
12
|
+
return prefix;
|
|
13
|
+
return path.startsWith(prefix) ? path : `${prefix}${path}`;
|
|
14
|
+
};
|
|
15
|
+
// One line per request on finish; the 5xx error itself is errors.ts's line, never repeated here.
|
|
16
|
+
export const requestLine = (log) => (req, res, next) => {
|
|
17
|
+
const start = performance.now();
|
|
18
|
+
res.on('finish', () => {
|
|
19
|
+
const { statusCode: status } = res;
|
|
20
|
+
const ms = Math.round(performance.now() - start);
|
|
21
|
+
const fields = { agcid: agcidOfResponse(res), method: req.method, route: routeOf(req) };
|
|
22
|
+
logAt(log, levelOf(status), 'request', { ...fields, status, ms });
|
|
23
|
+
});
|
|
24
|
+
next();
|
|
25
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const KIT_PREFIX = "kit:";
|
|
2
|
+
export declare const JOB_RUNS_SQL = "create table if not exists agentage_job_runs (\n id bigint generated always as identity primary key,\n job text not null,\n started_at timestamptz not null,\n ended_at timestamptz not null,\n status text not null,\n log text not null,\n timeout_ms integer\n);\nalter table agentage_job_runs add column if not exists agcid text";
|
|
3
|
+
export declare const KIT_MIGRATIONS: readonly {
|
|
4
|
+
name: string;
|
|
5
|
+
sql: string;
|
|
6
|
+
}[];
|
|
7
|
+
export declare const KIT_TABLES: string[];
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Applied before the extension's own files; `kit:` never matches NNN-name.sql, so no name collides.
|
|
2
|
+
export const KIT_PREFIX = 'kit:';
|
|
3
|
+
// Defensive: a store from before 0.7 already holds the table its worker created lazily.
|
|
4
|
+
export const JOB_RUNS_SQL = `create table if not exists agentage_job_runs (
|
|
5
|
+
id bigint generated always as identity primary key,
|
|
6
|
+
job text not null,
|
|
7
|
+
started_at timestamptz not null,
|
|
8
|
+
ended_at timestamptz not null,
|
|
9
|
+
status text not null,
|
|
10
|
+
log text not null,
|
|
11
|
+
timeout_ms integer
|
|
12
|
+
);
|
|
13
|
+
alter table agentage_job_runs add column if not exists agcid text`;
|
|
14
|
+
export const KIT_MIGRATIONS = [
|
|
15
|
+
{ name: `${KIT_PREFIX}001-job-runs`, sql: JOB_RUNS_SQL },
|
|
16
|
+
];
|
|
17
|
+
// The kit's own tables: a store holding only these still counts as empty and gets the seeds.
|
|
18
|
+
export const KIT_TABLES = ['_migrations', 'agentage_job_runs'];
|
package/dist/store/migrate.js
CHANGED
|
@@ -9,7 +9,7 @@ const MIGRATE_TIMEOUTS = { connectMs: 10_000, statementMs: 300_000 };
|
|
|
9
9
|
let log = createLog({ extension: 'unknown', part: 'migrate' });
|
|
10
10
|
try {
|
|
11
11
|
const pkg = await readManifest(process.cwd());
|
|
12
|
-
log = createLog({ extension: pkg.agentage.id, part: 'migrate' });
|
|
12
|
+
log = createLog({ extension: pkg.agentage.id, part: 'migrate', version: pkg.version });
|
|
13
13
|
const store = createStore(process.env, log, MIGRATE_TIMEOUTS);
|
|
14
14
|
const result = await migrate(store, process.cwd(), log);
|
|
15
15
|
process.stdout.write(`${JSON.stringify(result)}\n`);
|
package/dist/store/migrations.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readdir, readFile } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { messageOf } from '../log/log.js';
|
|
4
|
+
import { KIT_MIGRATIONS, KIT_PREFIX, KIT_TABLES } from './kit-migrations.js';
|
|
4
5
|
import {} from './store.types.js';
|
|
5
6
|
export const SQL_FILE = /^[0-9]{3}-[a-z0-9-]+\.sql$/;
|
|
6
7
|
// Filename order; anything else in the folder is refused, so a typo never silently skips a file.
|
|
@@ -29,16 +30,17 @@ export const SEEDS_OWED = 'seeds:owed';
|
|
|
29
30
|
// Tables other than the kit's own: a store holding any (a restored dump) never gets the seeds.
|
|
30
31
|
const USER_TABLES = `select count(*)::int as n from information_schema.tables
|
|
31
32
|
where table_schema not in ('pg_catalog', 'information_schema')
|
|
32
|
-
and table_name not in ('
|
|
33
|
-
|
|
33
|
+
and table_name not in (${KIT_TABLES.map((name) => `'${name}'`).join(', ')})`;
|
|
34
|
+
// Kit rows alone leave a store empty: an extension that adds its first migration later still seeds.
|
|
35
|
+
const isEmpty = async (store, done) => [...done].every((name) => name.startsWith(KIT_PREFIX)) &&
|
|
36
|
+
((await store.query(USER_TABLES)).rows[0]?.n ?? 0) === 0;
|
|
34
37
|
const appliedNames = async (store) => {
|
|
35
38
|
await store.query(LEDGER);
|
|
36
39
|
const { rows } = await store.query('select name from _migrations');
|
|
37
40
|
return new Set(rows.map((row) => row.name));
|
|
38
41
|
};
|
|
39
42
|
// One string, one round trip: the file and its ledger row commit together or not at all.
|
|
40
|
-
const
|
|
41
|
-
const sql = await readFile(file, 'utf8');
|
|
43
|
+
const applySql = async (store, sql, name) => {
|
|
42
44
|
try {
|
|
43
45
|
await store.query(`begin;\n${sql}\n;insert into _migrations (name) values ('${name}');\ncommit;`);
|
|
44
46
|
}
|
|
@@ -54,23 +56,29 @@ const applyAll = async (store, dir, prefix, done) => {
|
|
|
54
56
|
const name = `${prefix}${file}`;
|
|
55
57
|
if (done.has(name))
|
|
56
58
|
continue;
|
|
57
|
-
await
|
|
59
|
+
await applySql(store, await readFile(join(dir, file), 'utf8'), name);
|
|
58
60
|
count += 1;
|
|
59
61
|
}
|
|
60
62
|
return count;
|
|
61
63
|
};
|
|
64
|
+
const applyKit = async (store, done) => {
|
|
65
|
+
for (const { name, sql } of KIT_MIGRATIONS)
|
|
66
|
+
if (!done.has(name))
|
|
67
|
+
await applySql(store, sql, name);
|
|
68
|
+
};
|
|
62
69
|
const seedOnce = async (store, dir, done) => {
|
|
63
70
|
const seeds = await applyAll(store, join(dir, 'seeds'), SEED, done);
|
|
64
71
|
await store.query(`delete from _migrations where name = '${SEEDS_OWED}'`);
|
|
65
72
|
return seeds;
|
|
66
73
|
};
|
|
67
|
-
//
|
|
74
|
+
// The kit's migrations, then the extension's in order, each once; seeds once, only on a store with no ledger row and no user table.
|
|
68
75
|
export const migrate = async (store, dir, log) => {
|
|
69
76
|
const done = await appliedNames(store);
|
|
70
77
|
const empty = await isEmpty(store, done);
|
|
71
78
|
if (empty)
|
|
72
79
|
await store.query(`insert into _migrations (name) values ('${SEEDS_OWED}')`);
|
|
73
80
|
const owed = empty || done.has(SEEDS_OWED);
|
|
81
|
+
await applyKit(store, done);
|
|
74
82
|
const migrations = (await sqlFiles(join(dir, 'migrations'))).length;
|
|
75
83
|
const applied = await applyAll(store, join(dir, 'migrations'), '', done);
|
|
76
84
|
const seeds = owed ? await seedOnce(store, dir, done) : 0;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { scopeOf } from '../auth/token-check.js';
|
|
2
|
+
import { grants } from '../auth/verify-token.js';
|
|
3
|
+
import { fail } from '../envelope/envelope.js';
|
|
4
|
+
// As if every request carried caller's token (none when null): scopes enforced, no issuer.
|
|
5
|
+
export const callerCheck = (id, caller) => async (_authorization, access) => {
|
|
6
|
+
if (access === 'public' || (access === 'optional' && !caller))
|
|
7
|
+
return null;
|
|
8
|
+
if (!caller)
|
|
9
|
+
throw fail('UNAUTHORIZED', 'Missing token');
|
|
10
|
+
if (!grants(caller.scopes.join(' '), id, access))
|
|
11
|
+
throw fail('FORBIDDEN', `Needs ${scopeOf(id, access)}`);
|
|
12
|
+
return caller;
|
|
13
|
+
};
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { type ApiContext } from '../api/routes.js';
|
|
2
|
+
import { type Caller } from '../auth/token-check.js';
|
|
2
3
|
import { type PartContext } from '../context/context.js';
|
|
3
4
|
import { type McpContext } from '../mcp/tools.js';
|
|
4
5
|
import { type ApiCreate } from '../server/part.types.js';
|
|
5
6
|
export interface TestServerOptions<C extends PartContext> {
|
|
6
7
|
create: ApiCreate<C & ApiContext>;
|
|
7
8
|
ctx?: C;
|
|
9
|
+
caller?: Caller | null;
|
|
8
10
|
}
|
|
9
11
|
export interface TestMcpOptions {
|
|
10
12
|
create: (ctx: McpContext) => unknown;
|