@agentage/extension 0.11.1 → 0.12.2
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 +3 -2
- package/dist/agcid/agcid.js +17 -11
- package/dist/agcid/current.d.ts +4 -1
- package/dist/agcid/current.js +9 -0
- package/dist/agcid/fetch.d.ts +1 -0
- package/dist/agcid/fetch.js +4 -2
- 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.d.ts +3 -2
- package/dist/context/recording-log.js +12 -10
- package/dist/context/test-context.d.ts +3 -0
- package/dist/context/test-context.js +8 -2
- 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 +20 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/log/log.d.ts +4 -19
- package/dist/log/log.js +12 -42
- package/dist/log/span.js +6 -10
- package/dist/log/who.d.ts +13 -0
- package/dist/log/who.js +48 -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/server/serve.js +19 -7
- package/dist/tool/up/local-record.d.ts +1 -1
- package/dist/vendor/logs/event.schema.d.ts +45 -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 +88 -0
- package/dist/vendor/logs/index.d.ts +6 -0
- package/dist/vendor/logs/index.js +6 -0
- package/dist/vendor/logs/logger.d.ts +33 -0
- package/dist/vendor/logs/logger.js +83 -0
- package/dist/vendor/logs/parse-line.d.ts +6 -0
- package/dist/vendor/logs/parse-line.js +30 -0
- package/dist/vendor/logs/redact.d.ts +7 -0
- package/dist/vendor/logs/redact.js +63 -0
- package/dist/vendor/logs/rows.d.ts +34 -0
- package/dist/vendor/logs/rows.js +68 -0
- package/dist/vendor/logs/uuidv7.d.ts +1 -0
- package/dist/vendor/logs/uuidv7.js +10 -0
- package/dist/worker/run-log.d.ts +1 -1
- package/dist/worker/run-log.js +16 -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,88 @@
|
|
|
1
|
+
import { hostname } from 'node:os';
|
|
2
|
+
import { EventSchema } from './event.schema.js';
|
|
3
|
+
import { redact, scrub } from './redact.js';
|
|
4
|
+
import { uuidv7 } from './uuidv7.js';
|
|
5
|
+
const MAX_BATCH_BYTES = 900_000;
|
|
6
|
+
export const MAX_BATCH_ITEMS = 1000;
|
|
7
|
+
// A row the schema refuses is dropped with a warning and counted as rejected; the rest still go.
|
|
8
|
+
const complete = (event, host) => {
|
|
9
|
+
const filled = { ts: new Date().toISOString(), level: 'info', agcid: uuidv7(), host, ...event };
|
|
10
|
+
const msg = typeof event.msg === 'string' ? scrub(event.msg) : event.msg;
|
|
11
|
+
const parsed = EventSchema.safeParse({ ...filled, msg, fields: redact(event.fields ?? {}) });
|
|
12
|
+
if (parsed.success)
|
|
13
|
+
return parsed.data;
|
|
14
|
+
process.emitWarning(`@agentage/logs: dropped an invalid row (${parsed.error.issues[0]?.path.join('.')})`);
|
|
15
|
+
return null;
|
|
16
|
+
};
|
|
17
|
+
const sizeOf = (event) => Buffer.byteLength(JSON.stringify(event)) + 1;
|
|
18
|
+
// A row alone over the batch limit would be a 413: it is dropped with a warning instead.
|
|
19
|
+
const fitting = (event) => {
|
|
20
|
+
if (event === null)
|
|
21
|
+
return false;
|
|
22
|
+
if (sizeOf(event) + 2 <= MAX_BATCH_BYTES)
|
|
23
|
+
return true;
|
|
24
|
+
process.emitWarning(`@agentage/logs: dropped a ${event.kind} row over 900 KB`);
|
|
25
|
+
return false;
|
|
26
|
+
};
|
|
27
|
+
// Each batch under 1 MB and at most 1000 rows (the ingest wire, C3).
|
|
28
|
+
export const batchesOf = (events) => {
|
|
29
|
+
const batches = [];
|
|
30
|
+
let batch = [];
|
|
31
|
+
let bytes = 2;
|
|
32
|
+
for (const event of events) {
|
|
33
|
+
const size = sizeOf(event);
|
|
34
|
+
if (batch.length > 0 && (bytes + size > MAX_BATCH_BYTES || batch.length >= MAX_BATCH_ITEMS)) {
|
|
35
|
+
batches.push(batch);
|
|
36
|
+
[batch, bytes] = [[], 2];
|
|
37
|
+
}
|
|
38
|
+
batch.push(event);
|
|
39
|
+
bytes += size;
|
|
40
|
+
}
|
|
41
|
+
return batch.length > 0 ? [...batches, batch] : batches;
|
|
42
|
+
};
|
|
43
|
+
class IngestError extends Error {
|
|
44
|
+
status;
|
|
45
|
+
constructor(status) {
|
|
46
|
+
super(`POST /api/v1/events answered ${status}`);
|
|
47
|
+
this.status = status;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// A network error, 429 or 5xx is tried once more; a 4xx would fail the same way again.
|
|
51
|
+
const retries = (err) => !(err instanceof IngestError) || err.status === 429 || err.status >= 500;
|
|
52
|
+
const sendOnce = async (o, body) => {
|
|
53
|
+
const res = await (o.fetch ?? fetch)(`${o.url.replace(/\/+$/, '')}/api/v1/events`, {
|
|
54
|
+
method: 'POST',
|
|
55
|
+
headers: { authorization: `Bearer ${o.token}`, 'content-type': 'application/json' },
|
|
56
|
+
body,
|
|
57
|
+
signal: AbortSignal.timeout(o.timeoutMs ?? 5000),
|
|
58
|
+
});
|
|
59
|
+
if (!res.ok)
|
|
60
|
+
throw new IngestError(res.status);
|
|
61
|
+
// A 2xx was taken: an unreadable answer is never retried, it would post the rows twice.
|
|
62
|
+
const reply = (await res.json().catch(() => ({})));
|
|
63
|
+
return reply.data ?? { accepted: 0, rejected: 0 };
|
|
64
|
+
};
|
|
65
|
+
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
66
|
+
const send = (o, body) => sendOnce(o, body).catch(async (err) => {
|
|
67
|
+
if (!retries(err))
|
|
68
|
+
throw err;
|
|
69
|
+
await pause(o.retryDelayMs ?? 1000);
|
|
70
|
+
return sendOnce(o, body);
|
|
71
|
+
});
|
|
72
|
+
const post = async (o, events) => {
|
|
73
|
+
if (!o.url || !o.token)
|
|
74
|
+
throw new Error('@agentage/logs: the events client needs a url and a token');
|
|
75
|
+
const host = o.host ?? hostname();
|
|
76
|
+
const rows = events.map((event) => complete(event, host)).filter(fitting);
|
|
77
|
+
const total = { accepted: 0, rejected: events.length - rows.length };
|
|
78
|
+
for (const batch of batchesOf(rows)) {
|
|
79
|
+
const result = await send(o, JSON.stringify(batch));
|
|
80
|
+
total.accepted += result.accepted;
|
|
81
|
+
total.rejected += result.rejected;
|
|
82
|
+
}
|
|
83
|
+
return total;
|
|
84
|
+
};
|
|
85
|
+
// Posts rows to the ingest; a refused or unreachable ingest rejects: the caller catches.
|
|
86
|
+
export const createEventsClient = (o) => ({
|
|
87
|
+
post: (events) => post(o, events),
|
|
88
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { EventSchema, KINDS, LEVELS, USER_TYPES, type Event, type EventInput, type Fields, type Kind, type Level, type UserType, } from './event.schema.js';
|
|
2
|
+
export { parseLine, type LineSource } from './parse-line.js';
|
|
3
|
+
export { uuidv7 } from './uuidv7.js';
|
|
4
|
+
export { createLogger, type Logger, type LoggerOptions, type RowContext } from './logger.js';
|
|
5
|
+
export { type HttpRow, type JobRow, type McpRow, type Row } from './rows.js';
|
|
6
|
+
export { createEventsClient, type EventsClientOptions, type PostEvent, type PostResult, } from './events-client.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { EventSchema, KINDS, LEVELS, USER_TYPES, } from './event.schema.js';
|
|
2
|
+
export { parseLine } from './parse-line.js';
|
|
3
|
+
export { uuidv7 } from './uuidv7.js';
|
|
4
|
+
export { createLogger } from './logger.js';
|
|
5
|
+
export {} from './rows.js';
|
|
6
|
+
export { createEventsClient, } from './events-client.js';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { type Fields, type UserType } from './event.schema.js';
|
|
2
|
+
import { type HttpRow, type JobRow, type McpRow, type Row } from './rows.js';
|
|
3
|
+
export interface RowContext {
|
|
4
|
+
agcid?: string | null;
|
|
5
|
+
sub?: string | null;
|
|
6
|
+
session?: string | null;
|
|
7
|
+
user_type?: UserType;
|
|
8
|
+
client?: string | null;
|
|
9
|
+
}
|
|
10
|
+
export interface Logger {
|
|
11
|
+
info(msg: string, fields?: Fields): void;
|
|
12
|
+
warn(msg: string, fields?: Fields): void;
|
|
13
|
+
error(err: unknown, where?: string | Fields): void;
|
|
14
|
+
http(o: HttpRow): void;
|
|
15
|
+
mcp(o: McpRow): void;
|
|
16
|
+
job(o: JobRow): void;
|
|
17
|
+
span<T>(name: string, fn: () => T | Promise<T>): Promise<T>;
|
|
18
|
+
event(name: string, fields?: Fields, msg?: string): void;
|
|
19
|
+
row(row: Row): void;
|
|
20
|
+
}
|
|
21
|
+
export interface LoggerOptions {
|
|
22
|
+
extension: string;
|
|
23
|
+
part: string;
|
|
24
|
+
version?: string;
|
|
25
|
+
host?: string;
|
|
26
|
+
context?: () => RowContext | undefined;
|
|
27
|
+
write?: (line: string) => void;
|
|
28
|
+
sink?: (row: Row) => void;
|
|
29
|
+
base?: Fields;
|
|
30
|
+
strict?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare const MAX_LINE_BYTES: number;
|
|
33
|
+
export declare const createLogger: (o: LoggerOptions) => Logger;
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {} from './event.schema.js';
|
|
2
|
+
import { redact, scrub, stringOf } from './redact.js';
|
|
3
|
+
import { errorRow, eventRow, httpRow, jobRow, mcpRow, spanRow, } from './rows.js';
|
|
4
|
+
const timed = async (fn, end) => {
|
|
5
|
+
const start = performance.now();
|
|
6
|
+
const ms = () => Math.round(performance.now() - start);
|
|
7
|
+
try {
|
|
8
|
+
const value = await fn();
|
|
9
|
+
end(ms());
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
12
|
+
catch (err) {
|
|
13
|
+
end(ms(), err);
|
|
14
|
+
throw err;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
// Builds and writes a row; outside a strict log, a failed row is dropped, never thrown at the caller.
|
|
18
|
+
const guarded = (row, strict) => (build) => {
|
|
19
|
+
try {
|
|
20
|
+
const r = build();
|
|
21
|
+
if (r)
|
|
22
|
+
row(r);
|
|
23
|
+
}
|
|
24
|
+
catch (err) {
|
|
25
|
+
if (strict)
|
|
26
|
+
throw err;
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
// Every method is a row; row is the one sink, so a wrapper replaces only it.
|
|
30
|
+
const loggerOf = (row, strict = false) => {
|
|
31
|
+
const emit = guarded(row, strict);
|
|
32
|
+
return {
|
|
33
|
+
info: (msg, fields = {}) => emit(() => ({ kind: 'log', level: 'info', msg, fields })),
|
|
34
|
+
warn: (msg, fields = {}) => emit(() => ({ kind: 'log', level: 'warn', msg, fields })),
|
|
35
|
+
error: (err, where) => emit(() => errorRow(err, where)),
|
|
36
|
+
http: (o) => emit(() => httpRow(o)),
|
|
37
|
+
mcp: (o) => emit(() => mcpRow(o)),
|
|
38
|
+
job: (o) => emit(() => jobRow(o)),
|
|
39
|
+
span: (name, fn) => timed(fn, (ms, err) => emit(() => spanRow(name, ms, err))),
|
|
40
|
+
event: (name, fields = {}, msg) => emit(() => eventRow(name, fields, msg)),
|
|
41
|
+
row: (r) => emit(() => r),
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
const CONTROL = /[\u0000-\u001f\u007f]/g;
|
|
45
|
+
// A line over this is written without its fields: a shipper splits longer lines.
|
|
46
|
+
export const MAX_LINE_BYTES = 64 * 1024;
|
|
47
|
+
const text = (value, max) => value == null ? null : scrub(stringOf(value).replace(CONTROL, ''), max) || null;
|
|
48
|
+
// The columns the kit sets from request headers and tokens, bounded as the schema bounds them.
|
|
49
|
+
const columnsOf = (o, r) => {
|
|
50
|
+
const c = o.context?.() ?? {};
|
|
51
|
+
const { extension, part, version = '' } = o;
|
|
52
|
+
const [level, kind, msg] = [r.level, r.kind, scrub(r.msg)];
|
|
53
|
+
return {
|
|
54
|
+
...{ ts: new Date().toISOString(), level, kind, msg, extension, part, version },
|
|
55
|
+
...(o.host ? { host: o.host } : {}),
|
|
56
|
+
agcid: c.agcid ?? null,
|
|
57
|
+
sub: text(c.sub, 128),
|
|
58
|
+
session: text(c.session, 128),
|
|
59
|
+
user_type: c.user_type ?? 'service',
|
|
60
|
+
client: text(c.client, 64),
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
const lineOf = (o, r) => {
|
|
64
|
+
const columns = columnsOf(o, r);
|
|
65
|
+
const line = JSON.stringify({
|
|
66
|
+
...columns,
|
|
67
|
+
fields: { ...redact(o.base ?? {}), ...redact(r.fields) },
|
|
68
|
+
});
|
|
69
|
+
if (Buffer.byteLength(line) <= MAX_LINE_BYTES)
|
|
70
|
+
return `${line}\n`;
|
|
71
|
+
return `${JSON.stringify({ ...columns, fields: { 'agentage.dropped': 'fields over 64 KB' } })}\n`;
|
|
72
|
+
};
|
|
73
|
+
let quiet = false;
|
|
74
|
+
// A closed stderr (EPIPE) emits an error event; with no listener that would crash the part.
|
|
75
|
+
const stderr = (line) => {
|
|
76
|
+
if (!quiet) {
|
|
77
|
+
process.stderr.on('error', () => undefined);
|
|
78
|
+
quiet = true;
|
|
79
|
+
}
|
|
80
|
+
process.stderr.write(line);
|
|
81
|
+
};
|
|
82
|
+
// One JSON line per unit of work on stderr; the host ships it.
|
|
83
|
+
export const createLogger = (o) => loggerOf(o.sink ?? ((r) => (o.write ?? stderr)(lineOf(o, r))), o.strict);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { EventSchema } from './event.schema.js';
|
|
2
|
+
import { cut, redact, scrub } 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
|
+
const nameOf = (value) => cut(value ?? '', 64) || 'unknown';
|
|
14
|
+
// Not a valid event: a raw log row, a JSON object keeping its keys; both redacted as the logger does.
|
|
15
|
+
export const parseLine = (text, source = {}) => {
|
|
16
|
+
const line = text.trim();
|
|
17
|
+
const json = jsonOf(line);
|
|
18
|
+
const parsed = EventSchema.safeParse(json);
|
|
19
|
+
if (parsed.success)
|
|
20
|
+
return { ...parsed.data, msg: scrub(parsed.data.msg), fields: redact(parsed.data.fields) };
|
|
21
|
+
return EventSchema.parse({
|
|
22
|
+
ts: new Date().toISOString(),
|
|
23
|
+
level: RAW_ERROR.test(line) ? 'error' : 'info',
|
|
24
|
+
kind: 'log',
|
|
25
|
+
msg: scrub(line),
|
|
26
|
+
extension: nameOf(source.extension),
|
|
27
|
+
part: nameOf(source.part),
|
|
28
|
+
fields: { ...(isObject(json) ? redact(json) : {}), 'agentage.raw': true },
|
|
29
|
+
});
|
|
30
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { type Fields } from './event.schema.js';
|
|
2
|
+
export declare const MAX_STRING = 200;
|
|
3
|
+
export declare const cut: (text: string, max: number) => string;
|
|
4
|
+
export declare const stringOf: (value: unknown) => string;
|
|
5
|
+
export declare const scrub: (text: unknown, max?: number) => string;
|
|
6
|
+
export declare const read: (value: object, key: string) => unknown;
|
|
7
|
+
export declare const redact: (fields: Fields) => Fields;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import {} from './event.schema.js';
|
|
2
|
+
const SECRET = /token|secret|password|key|authorization|cookie/i;
|
|
3
|
+
// A JWT (three parts, or a token-length first part a cut left), an aga_ PAT, a Bearer credential.
|
|
4
|
+
const JWT = String.raw `(?<![\w-])eyJ(?:[\w-]{8,}\.[\w-]+\.[\w-]*|[\w-]{17,}(?:\.[\w-]*)?)`;
|
|
5
|
+
const PAT = String.raw `(?<![\w-])aga_[\w.-]{16,}`;
|
|
6
|
+
// An opaque Bearer value is mixed case with a digit, or 32 characters and more.
|
|
7
|
+
const OPAQUE = String.raw `(?=[\w.~+/=-]*\d)(?=[\w.~+/=-]*[A-Z])[\w.~+/=-]{16,}|[\w.~+/=-]{32,}`;
|
|
8
|
+
const BEARER = String.raw `\b[Bb]earer\s+(?:${OPAQUE})`;
|
|
9
|
+
const CREDENTIAL = new RegExp(`${JWT}|${PAT}|${BEARER}`, 'g');
|
|
10
|
+
export const MAX_STRING = 200;
|
|
11
|
+
const MAX_STACK = 2000;
|
|
12
|
+
const MAX_DEPTH = 3;
|
|
13
|
+
// On a code point boundary: a cut never leaves half a surrogate pair.
|
|
14
|
+
export const cut = (text, max) => text.length > max ? text.slice(0, max).replace(/[\uD800-\uDBFF]$/, '') : text;
|
|
15
|
+
export const stringOf = (value) => {
|
|
16
|
+
try {
|
|
17
|
+
return typeof value === 'string' ? value : String(value);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return '[unprintable]';
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
// Masked before the cut, so the cut never leaves a credential shape half-matched.
|
|
24
|
+
export const scrub = (text, max = MAX_STRING) => cut(stringOf(text).replace(CREDENTIAL, '[redacted]'), max);
|
|
25
|
+
export const read = (value, key) => {
|
|
26
|
+
try {
|
|
27
|
+
return Reflect.get(value, key);
|
|
28
|
+
}
|
|
29
|
+
catch {
|
|
30
|
+
return '[unreadable]';
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const clean = (value, depth) => {
|
|
34
|
+
if (typeof value === 'string')
|
|
35
|
+
return scrub(value);
|
|
36
|
+
if (typeof value === 'bigint')
|
|
37
|
+
return value.toString();
|
|
38
|
+
if (value === null || typeof value !== 'object')
|
|
39
|
+
return value;
|
|
40
|
+
if (depth >= MAX_DEPTH)
|
|
41
|
+
return '[depth]';
|
|
42
|
+
if (Array.isArray(value))
|
|
43
|
+
return value.map((item) => clean(item, depth + 1));
|
|
44
|
+
const entries = Object.keys(value).map((k) => [k, keyed(k, read(value, k), depth + 1)]);
|
|
45
|
+
return Object.fromEntries(entries);
|
|
46
|
+
};
|
|
47
|
+
// A stack keeps more than 200 characters: the first in-app frame is rarely in the first line.
|
|
48
|
+
const keyed = (key, value, depth) => {
|
|
49
|
+
if (SECRET.test(key))
|
|
50
|
+
return '[redacted]';
|
|
51
|
+
if (key === 'exception.stacktrace' && typeof value === 'string')
|
|
52
|
+
return scrub(value, MAX_STACK);
|
|
53
|
+
return clean(value, depth);
|
|
54
|
+
};
|
|
55
|
+
// Never throws: a getter that throws reads [unreadable], a bigint its digits, a proxy that throws empty.
|
|
56
|
+
export const redact = (fields) => {
|
|
57
|
+
try {
|
|
58
|
+
return clean(fields, 0);
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return { 'agentage.unreadable': true };
|
|
62
|
+
}
|
|
63
|
+
};
|
|
@@ -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 httpRow: (o: HttpRow) => Row | null;
|
|
30
|
+
export declare const mcpRow: (o: McpRow) => Row;
|
|
31
|
+
export declare const jobRow: (o: JobRow) => Row;
|
|
32
|
+
export declare const spanRow: (name: string, ms: number, error?: unknown) => Row;
|
|
33
|
+
export declare const eventRow: (name: string, fields: Fields, msg?: string) => Row;
|
|
34
|
+
export declare const errorRow: (err: unknown, where?: string | Fields) => Row;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {} from './event.schema.js';
|
|
2
|
+
import { cut, MAX_STRING, read, stringOf } from './redact.js';
|
|
3
|
+
const HEALTH = /^\/(health|healthz|livez|readyz)$/;
|
|
4
|
+
const typeOf = (err) => (err instanceof Error ? err.name : 'Error');
|
|
5
|
+
const failed = (err) => (err === undefined ? {} : { 'error.type': typeOf(err) });
|
|
6
|
+
const httpLevel = (status, ms) => status >= 500 ? 'error' : status >= 400 || ms > 1000 ? 'warn' : 'info';
|
|
7
|
+
// Health probes leave no row (logs-events Q2).
|
|
8
|
+
export const httpRow = (o) => {
|
|
9
|
+
if (HEALTH.test(o.route))
|
|
10
|
+
return null;
|
|
11
|
+
const route = cut(o.route, MAX_STRING);
|
|
12
|
+
const fields = {
|
|
13
|
+
'http.request.method': o.method,
|
|
14
|
+
'http.route': route,
|
|
15
|
+
'http.response.status_code': o.status,
|
|
16
|
+
'agentage.duration_ms': o.ms,
|
|
17
|
+
'agentage.bytes': o.bytes ?? 0,
|
|
18
|
+
...(o.reason ? { 'agentage.auth.reason': o.reason } : {}),
|
|
19
|
+
...(o.status >= 500 ? { 'error.type': o.errorType ?? String(o.status) } : {}),
|
|
20
|
+
...(o.clientInfo ? { 'agentage.client_info': o.clientInfo } : {}),
|
|
21
|
+
};
|
|
22
|
+
const msg = `${o.method} ${route} ${o.status}`;
|
|
23
|
+
return { kind: 'http', level: httpLevel(o.status, o.ms), msg, fields };
|
|
24
|
+
};
|
|
25
|
+
export const mcpRow = (o) => ({
|
|
26
|
+
kind: 'mcp',
|
|
27
|
+
level: o.error !== undefined ? 'error' : o.ms > 5000 ? 'warn' : 'info',
|
|
28
|
+
msg: `${o.tool} ${o.error !== undefined ? 'failed' : 'ok'} ${o.ms}ms`,
|
|
29
|
+
fields: {
|
|
30
|
+
'mcp.method.name': 'tools/call',
|
|
31
|
+
'gen_ai.tool.name': o.tool,
|
|
32
|
+
'agentage.duration_ms': o.ms,
|
|
33
|
+
...failed(o.error),
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
export const jobRow = (o) => ({
|
|
37
|
+
kind: 'job',
|
|
38
|
+
level: o.error === undefined ? 'info' : 'error',
|
|
39
|
+
msg: `${o.name} ${o.error === undefined ? 'ok' : 'failed'} ${o.ms}ms`,
|
|
40
|
+
fields: {
|
|
41
|
+
'agentage.job.name': o.name,
|
|
42
|
+
'agentage.job.run_id': o.runId,
|
|
43
|
+
'agentage.duration_ms': o.ms,
|
|
44
|
+
...failed(o.error),
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
export const spanRow = (name, ms, error) => ({
|
|
48
|
+
kind: 'span',
|
|
49
|
+
level: error !== undefined ? 'error' : ms > 1000 ? 'warn' : 'info',
|
|
50
|
+
msg: `${name} ${ms}ms`,
|
|
51
|
+
fields: { 'agentage.span.name': name, 'agentage.duration_ms': ms, ...failed(error) },
|
|
52
|
+
});
|
|
53
|
+
export const eventRow = (name, fields, msg = name) => ({
|
|
54
|
+
kind: 'event',
|
|
55
|
+
level: 'info',
|
|
56
|
+
msg,
|
|
57
|
+
fields: { 'event.name': name, ...fields },
|
|
58
|
+
});
|
|
59
|
+
// where: the route, tool or job; an author's agentage.fingerprint passes through (Q11 A).
|
|
60
|
+
export const errorRow = (err, where) => {
|
|
61
|
+
const own = typeof where === 'string' ? { 'agentage.where': where } : (where ?? {});
|
|
62
|
+
const error = err instanceof Error;
|
|
63
|
+
const text = (key) => stringOf(read(err, key) ?? '');
|
|
64
|
+
const [type, message] = error ? [text('name'), text('message')] : ['Error', stringOf(err)];
|
|
65
|
+
const stack = error ? { 'exception.stacktrace': text('stack') } : {};
|
|
66
|
+
const fields = { 'exception.type': type, 'exception.message': message, ...stack, ...own };
|
|
67
|
+
return { kind: 'error', level: 'error', msg: `${type}: ${message}`, fields };
|
|
68
|
+
};
|
|
@@ -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.d.ts
CHANGED
|
@@ -6,4 +6,4 @@ export interface RunBuffer {
|
|
|
6
6
|
}
|
|
7
7
|
export declare const withRunBuffer: <T>(buffer: RunBuffer, fn: () => Promise<T>) => Promise<T>;
|
|
8
8
|
export declare const append: (buffer: RunBuffer, line: string) => void;
|
|
9
|
-
export declare const runLog: (base: Log) => Log;
|
|
9
|
+
export declare const runLog: (base: Log, strict?: boolean) => Log;
|
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,18 @@ 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
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
error: (error, fields) => {
|
|
39
|
-
record('error', messageOf(error));
|
|
40
|
-
base.error(error, fields);
|
|
26
|
+
export const runLog = (base, strict = false) => ({
|
|
27
|
+
...createLogger({
|
|
28
|
+
extension: 'worker',
|
|
29
|
+
part: 'worker',
|
|
30
|
+
strict,
|
|
31
|
+
sink: (row) => {
|
|
32
|
+
record(row.level, row.msg);
|
|
33
|
+
base.row(row);
|
|
34
|
+
},
|
|
35
|
+
}),
|
|
36
|
+
error: (err, where) => {
|
|
37
|
+
record('error', messageOf(err));
|
|
38
|
+
base.error(err, where);
|
|
41
39
|
},
|
|
42
|
-
}
|
|
40
|
+
});
|
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.2",
|
|
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
|
},
|