@agentage/extension 0.11.1 → 0.12.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/README.md +2 -0
- package/dist/agcid/agcid.d.ts +1 -1
- package/dist/agcid/agcid.js +18 -4
- package/dist/agcid/current.d.ts +4 -1
- package/dist/agcid/current.js +2 -0
- package/dist/agcid/header.js +2 -1
- package/dist/agcid/scope.js +5 -3
- package/dist/auth/token-check.d.ts +1 -0
- package/dist/auth/token-check.js +17 -1
- package/dist/auth/verify-token.js +2 -2
- package/dist/context/context.d.ts +5 -0
- package/dist/context/context.js +17 -0
- package/dist/context/recording-log.js +12 -10
- package/dist/context/test-context.d.ts +3 -0
- package/dist/context/test-context.js +7 -1
- package/dist/discovery/discovery.d.ts +1 -1
- package/dist/discovery/discovery.schema.d.ts +2 -0
- package/dist/health/health.d.ts +2 -0
- package/dist/health/health.js +17 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/log/log.d.ts +4 -11
- package/dist/log/log.js +3 -41
- package/dist/log/span.js +6 -10
- package/dist/log/who.d.ts +12 -0
- package/dist/log/who.js +46 -0
- package/dist/logs/index.d.ts +1 -0
- package/dist/logs/index.js +2 -0
- package/dist/manifest/manifest.schema.d.ts +4 -0
- package/dist/manifest/primitives.schema.d.ts +4 -0
- package/dist/manifest/primitives.schema.js +2 -1
- package/dist/mcp/mcp-app.js +23 -2
- package/dist/mcp/server.js +11 -5
- package/dist/pages/pages-app.js +6 -3
- package/dist/runtime/main.js +7 -2
- package/dist/runtime/part-runners.js +2 -1
- package/dist/runtime/run-part.d.ts +2 -0
- package/dist/runtime/run-part.js +20 -6
- package/dist/server/app.js +5 -4
- package/dist/server/errors.js +24 -3
- package/dist/server/request-line.d.ts +2 -2
- package/dist/server/request-line.js +31 -12
- package/dist/tool/up/local-record.d.ts +1 -1
- package/dist/vendor/logs/error-row.d.ts +3 -0
- package/dist/vendor/logs/error-row.js +16 -0
- package/dist/vendor/logs/event.schema.d.ts +46 -0
- package/dist/vendor/logs/event.schema.js +24 -0
- package/dist/vendor/logs/events-client.d.ts +22 -0
- package/dist/vendor/logs/events-client.js +74 -0
- package/dist/vendor/logs/index.d.ts +5 -0
- package/dist/vendor/logs/index.js +5 -0
- package/dist/vendor/logs/logger.d.ts +31 -0
- package/dist/vendor/logs/logger.js +56 -0
- package/dist/vendor/logs/parse-line.d.ts +6 -0
- package/dist/vendor/logs/parse-line.js +29 -0
- package/dist/vendor/logs/redact.d.ts +3 -0
- package/dist/vendor/logs/redact.js +27 -0
- package/dist/vendor/logs/rows.d.ts +34 -0
- package/dist/vendor/logs/rows.js +57 -0
- package/dist/vendor/logs/uuidv7.d.ts +1 -0
- package/dist/vendor/logs/uuidv7.js +10 -0
- package/dist/worker/run-log.js +15 -18
- package/dist/worker/scheduler.js +11 -4
- package/package.json +10 -3
- package/schemas/error-envelope.schema.json +1 -1
- package/schemas/extension-discovery.schema.json +2 -1
- package/schemas/extension-package.schema.json +4 -2
- package/schemas/extension-record.schema.json +2 -1
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import {} from './event.schema.js';
|
|
2
|
+
import { errorRow } from './error-row.js';
|
|
3
|
+
import { MAX_STRING, redact } from './redact.js';
|
|
4
|
+
import { eventRow, httpRow, jobRow, mcpRow, spanRow, } from './rows.js';
|
|
5
|
+
const timed = async (fn, end) => {
|
|
6
|
+
const start = performance.now();
|
|
7
|
+
const ms = () => Math.round(performance.now() - start);
|
|
8
|
+
try {
|
|
9
|
+
const value = await fn();
|
|
10
|
+
end(ms());
|
|
11
|
+
return value;
|
|
12
|
+
}
|
|
13
|
+
catch (err) {
|
|
14
|
+
end(ms(), err);
|
|
15
|
+
throw err;
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
// Every method is a row; row is the one sink, so a wrapper replaces only it.
|
|
19
|
+
const loggerOf = (row) => ({
|
|
20
|
+
info: (msg, fields = {}) => row({ kind: 'log', level: 'info', msg, fields }),
|
|
21
|
+
warn: (msg, fields = {}) => row({ kind: 'log', level: 'warn', msg, fields }),
|
|
22
|
+
error: (err, where) => row(errorRow(err, where)),
|
|
23
|
+
http: (o) => {
|
|
24
|
+
const r = httpRow(o);
|
|
25
|
+
if (r)
|
|
26
|
+
row(r);
|
|
27
|
+
},
|
|
28
|
+
mcp: (o) => row(mcpRow(o)),
|
|
29
|
+
job: (o) => row(jobRow(o)),
|
|
30
|
+
span: (name, fn) => timed(fn, (ms, err) => row(spanRow(name, ms, err))),
|
|
31
|
+
event: (name, fields = {}, msg) => row(eventRow(name, fields, msg)),
|
|
32
|
+
row,
|
|
33
|
+
});
|
|
34
|
+
const lineOf = (o, r) => {
|
|
35
|
+
const c = o.context?.() ?? {};
|
|
36
|
+
const event = {
|
|
37
|
+
ts: new Date().toISOString(),
|
|
38
|
+
level: r.level,
|
|
39
|
+
kind: r.kind,
|
|
40
|
+
msg: r.msg.slice(0, MAX_STRING),
|
|
41
|
+
extension: o.extension,
|
|
42
|
+
part: o.part,
|
|
43
|
+
version: o.version ?? '',
|
|
44
|
+
...(o.host ? { host: o.host } : {}),
|
|
45
|
+
agcid: c.agcid ?? null,
|
|
46
|
+
sub: c.sub ?? null,
|
|
47
|
+
session: c.session ?? null,
|
|
48
|
+
user_type: c.user_type ?? 'service',
|
|
49
|
+
client: c.client ?? null,
|
|
50
|
+
fields: redact({ ...o.base, ...r.fields }),
|
|
51
|
+
};
|
|
52
|
+
return `${JSON.stringify(event)}\n`;
|
|
53
|
+
};
|
|
54
|
+
const stderr = (line) => void process.stderr.write(line);
|
|
55
|
+
// One JSON line per unit of work on stderr; the host ships it.
|
|
56
|
+
export const createLogger = (o) => loggerOf(o.sink ?? ((r) => (o.write ?? stderr)(lineOf(o, r))));
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { EventSchema } from './event.schema.js';
|
|
2
|
+
import { MAX_STRING, redact } from './redact.js';
|
|
3
|
+
const RAW_ERROR = /\b(ERROR|FATAL)\b/;
|
|
4
|
+
const jsonOf = (text) => {
|
|
5
|
+
try {
|
|
6
|
+
return JSON.parse(text);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return undefined;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const isObject = (value) => typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
13
|
+
// A line that is not a valid event is a raw log row; a JSON object keeps its keys, redacted.
|
|
14
|
+
export const parseLine = (text, source = {}) => {
|
|
15
|
+
const line = text.trim();
|
|
16
|
+
const json = jsonOf(line);
|
|
17
|
+
const parsed = EventSchema.safeParse(json);
|
|
18
|
+
if (parsed.success)
|
|
19
|
+
return parsed.data;
|
|
20
|
+
return EventSchema.parse({
|
|
21
|
+
ts: new Date().toISOString(),
|
|
22
|
+
level: RAW_ERROR.test(line) ? 'error' : 'info',
|
|
23
|
+
kind: 'log',
|
|
24
|
+
msg: line.slice(0, MAX_STRING),
|
|
25
|
+
extension: source.extension ?? 'unknown',
|
|
26
|
+
part: source.part ?? 'unknown',
|
|
27
|
+
fields: { ...(isObject(json) ? redact(json) : {}), 'agentage.raw': true },
|
|
28
|
+
});
|
|
29
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import {} from './event.schema.js';
|
|
2
|
+
const SECRET = /token|secret|password|key|authorization|cookie/i;
|
|
3
|
+
export const MAX_STRING = 200;
|
|
4
|
+
const MAX_STACK = 2000;
|
|
5
|
+
const MAX_DEPTH = 3;
|
|
6
|
+
const cut = (text, max) => (text.length > max ? text.slice(0, max) : text);
|
|
7
|
+
const clean = (value, depth) => {
|
|
8
|
+
if (typeof value === 'string')
|
|
9
|
+
return cut(value, MAX_STRING);
|
|
10
|
+
if (value === null || typeof value !== 'object')
|
|
11
|
+
return value;
|
|
12
|
+
if (depth >= MAX_DEPTH)
|
|
13
|
+
return '[depth]';
|
|
14
|
+
if (Array.isArray(value))
|
|
15
|
+
return value.map((item) => clean(item, depth + 1));
|
|
16
|
+
const entries = Object.entries(value).map(([k, v]) => [k, keyed(k, v, depth + 1)]);
|
|
17
|
+
return Object.fromEntries(entries);
|
|
18
|
+
};
|
|
19
|
+
// A stack keeps more than 200 characters: the first in-app frame is rarely in the first line.
|
|
20
|
+
const keyed = (key, value, depth) => {
|
|
21
|
+
if (SECRET.test(key))
|
|
22
|
+
return '[redacted]';
|
|
23
|
+
if (key === 'exception.stacktrace' && typeof value === 'string')
|
|
24
|
+
return cut(value, MAX_STACK);
|
|
25
|
+
return clean(value, depth);
|
|
26
|
+
};
|
|
27
|
+
export const redact = (fields) => clean(fields, 0);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { type Fields, type Kind, type Level } from './event.schema.js';
|
|
2
|
+
export interface Row {
|
|
3
|
+
kind: Kind;
|
|
4
|
+
level: Level;
|
|
5
|
+
msg: string;
|
|
6
|
+
fields: Fields;
|
|
7
|
+
}
|
|
8
|
+
export interface HttpRow {
|
|
9
|
+
method: string;
|
|
10
|
+
route: string;
|
|
11
|
+
status: number;
|
|
12
|
+
ms: number;
|
|
13
|
+
bytes?: number;
|
|
14
|
+
reason?: string;
|
|
15
|
+
errorType?: string;
|
|
16
|
+
clientInfo?: string;
|
|
17
|
+
}
|
|
18
|
+
export interface McpRow {
|
|
19
|
+
tool: string;
|
|
20
|
+
ms: number;
|
|
21
|
+
error?: unknown;
|
|
22
|
+
}
|
|
23
|
+
export interface JobRow {
|
|
24
|
+
name: string;
|
|
25
|
+
runId: string;
|
|
26
|
+
ms: number;
|
|
27
|
+
error?: unknown;
|
|
28
|
+
}
|
|
29
|
+
export declare const typeOf: (err: unknown) => string;
|
|
30
|
+
export declare const httpRow: (o: HttpRow) => Row | null;
|
|
31
|
+
export declare const mcpRow: (o: McpRow) => Row;
|
|
32
|
+
export declare const jobRow: (o: JobRow) => Row;
|
|
33
|
+
export declare const spanRow: (name: string, ms: number, error?: unknown) => Row;
|
|
34
|
+
export declare const eventRow: (name: string, fields: Fields, msg?: string) => Row;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import {} from './event.schema.js';
|
|
2
|
+
const HEALTH = /^\/(health|healthz|livez|readyz)$/;
|
|
3
|
+
export const typeOf = (err) => (err instanceof Error ? err.name : 'Error');
|
|
4
|
+
const failed = (err) => (err === undefined ? {} : { 'error.type': typeOf(err) });
|
|
5
|
+
const httpLevel = (status, ms) => status >= 500 ? 'error' : status >= 400 || ms > 1000 ? 'warn' : 'info';
|
|
6
|
+
// Health probes leave no row (logs-events Q2).
|
|
7
|
+
export const httpRow = (o) => {
|
|
8
|
+
if (HEALTH.test(o.route))
|
|
9
|
+
return null;
|
|
10
|
+
const route = o.route.slice(0, 200);
|
|
11
|
+
const fields = {
|
|
12
|
+
'http.request.method': o.method,
|
|
13
|
+
'http.route': route,
|
|
14
|
+
'http.response.status_code': o.status,
|
|
15
|
+
'agentage.duration_ms': o.ms,
|
|
16
|
+
'agentage.bytes': o.bytes ?? 0,
|
|
17
|
+
...(o.reason ? { 'agentage.auth.reason': o.reason } : {}),
|
|
18
|
+
...(o.status >= 500 ? { 'error.type': o.errorType ?? String(o.status) } : {}),
|
|
19
|
+
...(o.clientInfo ? { 'agentage.client_info': o.clientInfo } : {}),
|
|
20
|
+
};
|
|
21
|
+
const msg = `${o.method} ${route} ${o.status}`;
|
|
22
|
+
return { kind: 'http', level: httpLevel(o.status, o.ms), msg, fields };
|
|
23
|
+
};
|
|
24
|
+
export const mcpRow = (o) => ({
|
|
25
|
+
kind: 'mcp',
|
|
26
|
+
level: o.error !== undefined ? 'error' : o.ms > 5000 ? 'warn' : 'info',
|
|
27
|
+
msg: `${o.tool} ${o.error !== undefined ? 'failed' : 'ok'} ${o.ms}ms`,
|
|
28
|
+
fields: {
|
|
29
|
+
'mcp.method.name': 'tools/call',
|
|
30
|
+
'gen_ai.tool.name': o.tool,
|
|
31
|
+
'agentage.duration_ms': o.ms,
|
|
32
|
+
...failed(o.error),
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
export const jobRow = (o) => ({
|
|
36
|
+
kind: 'job',
|
|
37
|
+
level: o.error === undefined ? 'info' : 'error',
|
|
38
|
+
msg: `${o.name} ${o.error === undefined ? 'ok' : 'failed'} ${o.ms}ms`,
|
|
39
|
+
fields: {
|
|
40
|
+
'agentage.job.name': o.name,
|
|
41
|
+
'agentage.job.run_id': o.runId,
|
|
42
|
+
'agentage.duration_ms': o.ms,
|
|
43
|
+
...failed(o.error),
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
export const spanRow = (name, ms, error) => ({
|
|
47
|
+
kind: 'span',
|
|
48
|
+
level: error !== undefined ? 'error' : ms > 1000 ? 'warn' : 'info',
|
|
49
|
+
msg: `${name} ${ms}ms`,
|
|
50
|
+
fields: { 'agentage.span.name': name, 'agentage.duration_ms': ms, ...failed(error) },
|
|
51
|
+
});
|
|
52
|
+
export const eventRow = (name, fields, msg = name) => ({
|
|
53
|
+
kind: 'event',
|
|
54
|
+
level: 'info',
|
|
55
|
+
msg,
|
|
56
|
+
fields: { 'event.name': name, ...fields },
|
|
57
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const uuidv7: (now?: number, random?: Buffer) => string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
// RFC 9562 UUIDv7 as 32 lowercase hex, a valid W3C trace id: 48-bit ms, version 7, variant 10.
|
|
3
|
+
export const uuidv7 = (now = Date.now(), random = randomBytes(10)) => {
|
|
4
|
+
const bytes = Buffer.alloc(16);
|
|
5
|
+
bytes.writeUIntBE(now, 0, 6);
|
|
6
|
+
random.copy(bytes, 6, 0, 10);
|
|
7
|
+
bytes.writeUInt8((bytes.readUInt8(6) & 0x0f) | 0x70, 6);
|
|
8
|
+
bytes.writeUInt8((bytes.readUInt8(8) & 0x3f) | 0x80, 8);
|
|
9
|
+
return bytes.toString('hex');
|
|
10
|
+
};
|
package/dist/worker/run-log.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
2
|
-
import {
|
|
2
|
+
import { createLogger } from '../vendor/logs/index.js';
|
|
3
|
+
import { messageOf } from '../log/log.js';
|
|
3
4
|
export const LOG_CAP_BYTES = 64 * 1024;
|
|
4
5
|
const TRUNCATED = '\n[log truncated at 64 KB]\n';
|
|
5
6
|
const current = new AsyncLocalStorage();
|
|
@@ -22,21 +23,17 @@ const record = (level, message) => {
|
|
|
22
23
|
append(buffer, `${level} ${message}`);
|
|
23
24
|
};
|
|
24
25
|
// ctx.log for a worker: the usual JSON line, and inside a run also the run's own log.
|
|
25
|
-
const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
record('
|
|
36
|
-
base.
|
|
37
|
-
},
|
|
38
|
-
error: (error, fields) => {
|
|
39
|
-
record('error', messageOf(error));
|
|
40
|
-
base.error(error, fields);
|
|
26
|
+
export const runLog = (base) => ({
|
|
27
|
+
...createLogger({
|
|
28
|
+
extension: 'worker',
|
|
29
|
+
part: 'worker',
|
|
30
|
+
sink: (row) => {
|
|
31
|
+
record(row.level, row.msg);
|
|
32
|
+
base.row(row);
|
|
33
|
+
},
|
|
34
|
+
}),
|
|
35
|
+
error: (err, where) => {
|
|
36
|
+
record('error', messageOf(err));
|
|
37
|
+
base.error(err, where);
|
|
41
38
|
},
|
|
42
|
-
}
|
|
39
|
+
});
|
package/dist/worker/scheduler.js
CHANGED
|
@@ -39,11 +39,11 @@ const newEntry = (name, cron, job, options, at) => {
|
|
|
39
39
|
const runJob = async (entry, log, buffer) => {
|
|
40
40
|
try {
|
|
41
41
|
await withRunBuffer(buffer, async () => entry.job());
|
|
42
|
-
return 'ok';
|
|
42
|
+
return { status: 'ok' };
|
|
43
43
|
}
|
|
44
44
|
catch (err) {
|
|
45
|
-
await withRunBuffer(buffer, async () => log.error(err, {
|
|
46
|
-
return 'failed';
|
|
45
|
+
await withRunBuffer(buffer, async () => log.error(err, { 'agentage.where': entry.name }));
|
|
46
|
+
return { status: 'failed', error: err };
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
// Never twice at once; every run saved with its log, a failed save is logged and the job goes on.
|
|
@@ -53,10 +53,17 @@ const executeScoped = async (s, entry) => {
|
|
|
53
53
|
const startedAt = s.now();
|
|
54
54
|
Object.assign(entry, { running: true, runningSince: startedAt, overran: false });
|
|
55
55
|
const buffer = { text: '', truncated: false };
|
|
56
|
-
const status = await runJob(entry, s.deps.log, buffer);
|
|
56
|
+
const { status, error } = await runJob(entry, s.deps.log, buffer);
|
|
57
57
|
entry.running = false;
|
|
58
58
|
entry.last = status;
|
|
59
59
|
const agcid = requestScope.getStore()?.agcid;
|
|
60
|
+
const ms = s.now().getTime() - startedAt.getTime();
|
|
61
|
+
s.deps.log.job({
|
|
62
|
+
name: entry.name,
|
|
63
|
+
runId: agcid ?? '',
|
|
64
|
+
ms,
|
|
65
|
+
...(error === undefined ? {} : { error }),
|
|
66
|
+
});
|
|
60
67
|
const run = { job: entry.name, startedAt, endedAt: s.now(), status, log: buffer.text };
|
|
61
68
|
if (agcid)
|
|
62
69
|
run.agcid = agcid;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentage/extension",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
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",
|
|
@@ -32,6 +32,10 @@
|
|
|
32
32
|
"types": "./dist/contracts/index.d.ts",
|
|
33
33
|
"default": "./dist/contracts/index.js"
|
|
34
34
|
},
|
|
35
|
+
"./logs": {
|
|
36
|
+
"types": "./dist/logs/index.d.ts",
|
|
37
|
+
"default": "./dist/logs/index.js"
|
|
38
|
+
},
|
|
35
39
|
"./migrate": {
|
|
36
40
|
"default": "./dist/store/migrate.js"
|
|
37
41
|
},
|
|
@@ -45,7 +49,7 @@
|
|
|
45
49
|
"!dist/test-support"
|
|
46
50
|
],
|
|
47
51
|
"scripts": {
|
|
48
|
-
"build": "tsc -b",
|
|
52
|
+
"build": "tsc -b && node ../../scripts/inline-logs.mjs",
|
|
49
53
|
"schemas": "node dist/schemas/write-schemas.js schemas",
|
|
50
54
|
"verify": "npm run verify --prefix ../..",
|
|
51
55
|
"prepublishOnly": "npm run verify",
|
|
@@ -75,5 +79,8 @@
|
|
|
75
79
|
"keywords": [
|
|
76
80
|
"agentage",
|
|
77
81
|
"extension"
|
|
78
|
-
]
|
|
82
|
+
],
|
|
83
|
+
"devDependencies": {
|
|
84
|
+
"@agentage/logs": "^0.1.0"
|
|
85
|
+
}
|
|
79
86
|
}
|
|
@@ -153,7 +153,8 @@
|
|
|
153
153
|
"enum": [
|
|
154
154
|
"postgres",
|
|
155
155
|
"signoz",
|
|
156
|
-
"git"
|
|
156
|
+
"git",
|
|
157
|
+
"clickhouse"
|
|
157
158
|
]
|
|
158
159
|
},
|
|
159
160
|
{
|
|
@@ -164,7 +165,8 @@
|
|
|
164
165
|
"enum": [
|
|
165
166
|
"postgres",
|
|
166
167
|
"signoz",
|
|
167
|
-
"git"
|
|
168
|
+
"git",
|
|
169
|
+
"clickhouse"
|
|
168
170
|
]
|
|
169
171
|
}
|
|
170
172
|
},
|