@agentage/extension 0.19.0 → 0.20.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/README.md +39 -0
- package/dist/core-client/core-client.d.ts +28 -0
- package/dist/core-client/core-client.js +91 -0
- package/dist/core-client/http.d.ts +12 -0
- package/dist/core-client/http.js +65 -0
- package/dist/core-client/index.d.ts +4 -0
- package/dist/core-client/index.js +5 -0
- package/dist/core-client/logs.d.ts +8 -0
- package/dist/core-client/logs.js +31 -0
- package/dist/core-client/market.d.ts +26 -0
- package/dist/core-client/market.js +102 -0
- package/dist/core-client/reason.d.ts +21 -0
- package/dist/core-client/reason.js +45 -0
- package/dist/host/host-env.d.ts +1 -0
- package/dist/host/host-env.js +2 -1
- package/dist/host/host-extension.d.ts +2 -0
- package/dist/host/host-extension.js +1 -0
- package/dist/host/host-parts.js +1 -0
- package/dist/host/serve-host.js +11 -2
- package/dist/shell/dist/index.html +1 -1
- package/dist/vendor/logs/events-client.js +9 -29
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@ correlation id, the response envelope, the store pool, the scheduler and argumen
|
|
|
8
8
|
schemas.
|
|
9
9
|
`@agentage/extension/logs` is the event line (`EventSchema`, `parseLine`, `createLogger`,
|
|
10
10
|
`createEventsClient`, `KINDS`, `LEVELS`, `USER_TYPES`), built into the kit's own tarball.
|
|
11
|
+
`@agentage/extension/core` is the one client of the core's services (see "The core client").
|
|
11
12
|
`@agentage/extension/app` is the app part: `useAppShell()` (route, regions, notices, state) and
|
|
12
13
|
`useAppHost()` (`user`, `scopes`, `api`, and since 0.18.2 `extension { id, version }` and
|
|
13
14
|
`host { kind, url }`, kind `local`, `desktop` or `cloud`).
|
|
@@ -16,6 +17,44 @@ schemas.
|
|
|
16
17
|
npm install @agentage/extension express zod
|
|
17
18
|
```
|
|
18
19
|
|
|
20
|
+
## The core client
|
|
21
|
+
|
|
22
|
+
`createCoreClient({ url, timeoutMs, token })` calls the core: `market.latest(id)`, `market.list()`,
|
|
23
|
+
`market.down()`, `logs.send(rows)` and `logs.down()`. It never throws. Each method call ends
|
|
24
|
+
before `timeoutMs` (default 2000 ms): the address and all requests of the call share one deadline.
|
|
25
|
+
|
|
26
|
+
- Each call answers `{ data, down }`. When the core is silent, `data` is the empty answer: `null`
|
|
27
|
+
or an empty listing. `down` then has a `code`, a `message` and a `detail`.
|
|
28
|
+
- `data: null, down: null` from `market.latest` means the market itself refused the id: a 404
|
|
29
|
+
with its error envelope, `no listed extension <id>` or `<id> has no release yet`. Any other
|
|
30
|
+
404, such as a route miss of the app host or a proxy page, is `failed`.
|
|
31
|
+
- `market.down()` and `logs.down()` keep the first reason of that service.
|
|
32
|
+
- The address: `url` (an origin, or a function that resolves one), else `AGENTAGE_CORE_URL` of
|
|
33
|
+
`env`. A missing or empty address is no error: each call answers `down.code` `no_core`.
|
|
34
|
+
- `logs.send` drops a failed send with one warning, until a send goes through again. It needs
|
|
35
|
+
`token`, sent over https or to this machine only (`localhost`, `*.localhost`, `127.x`, `::1`).
|
|
36
|
+
- `./core` is for Node: `logs.send` uses the Node events client. The market reads import nothing, so a
|
|
37
|
+
browser entry for them is a small split later.
|
|
38
|
+
|
|
39
|
+
| `down.code` | `message` | When |
|
|
40
|
+
| ------------- | ------------------------- | ----------------------------------------------------------------- |
|
|
41
|
+
| `no_core` | `<service> not reachable` | no address, not a URL, or the `url` function threw (`cause`) |
|
|
42
|
+
| `unreachable` | `<service> not reachable` | no connection |
|
|
43
|
+
| `timeout` | `<service> not reachable` | no answer before the deadline, a stalled body |
|
|
44
|
+
| `failed` | `<service> not reachable` | not 2xx, a 404 that is not the market's, a redirect, over 1 MB |
|
|
45
|
+
| `not_json` | `<service> not reachable` | a market body that is not JSON |
|
|
46
|
+
| `refused` | `market answer refused` | a 2xx with no release or rows, or not an exact version of that id |
|
|
47
|
+
| `no_token` | `logs needs a token` | `logs.send` with no token |
|
|
48
|
+
|
|
49
|
+
The rules for a caller:
|
|
50
|
+
|
|
51
|
+
- A caller that needs the market, such as an install or an update with no version, treats
|
|
52
|
+
`down !== null` as an error and changes nothing. Only a list for a page shows the empty answer.
|
|
53
|
+
- A release loop reads `data: null` as "no release" only when `down` is null. It also skips a
|
|
54
|
+
prerelease version and a `commit` of `null`, and keeps its own origin on https.
|
|
55
|
+
- Pass `timeoutMs` for the work: about 10 s for an install or a list in a terminal; a worker
|
|
56
|
+
sets its own. The default suits a page or a start that must not wait.
|
|
57
|
+
|
|
19
58
|
`agentage extension check [dir] --json` prints the conformance report (exit 1 when `conforms` is
|
|
20
59
|
false); `file` and `line` are there only when a problem has them. `warnings` is there only when a
|
|
21
60
|
rule warned: an `engines.agentage` of an older kit minor from the floor `0.12` up conforms with a
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { PostEvent, PostResult } from '../vendor/logs/index.js';
|
|
2
|
+
import { type Fetch } from './http.js';
|
|
3
|
+
import { type Latest, type Listing } from './market.js';
|
|
4
|
+
import { type Answer, type CoreReason } from './reason.js';
|
|
5
|
+
export declare const CORE_URL_ENV = "AGENTAGE_CORE_URL";
|
|
6
|
+
export declare const DEFAULT_TIMEOUT_MS = 2000;
|
|
7
|
+
export type CoreUrl = string | undefined | (() => Promise<string>);
|
|
8
|
+
export interface CoreClientOptions {
|
|
9
|
+
url?: CoreUrl;
|
|
10
|
+
env?: Readonly<Record<string, string | undefined>>;
|
|
11
|
+
timeoutMs?: number;
|
|
12
|
+
token?: string;
|
|
13
|
+
fetch?: Fetch;
|
|
14
|
+
warn?: (message: string) => void;
|
|
15
|
+
}
|
|
16
|
+
export interface CoreClient {
|
|
17
|
+
market: {
|
|
18
|
+
latest: (id: string) => Promise<Answer<Latest | null>>;
|
|
19
|
+
list: () => Promise<Answer<Listing>>;
|
|
20
|
+
down: () => CoreReason | null;
|
|
21
|
+
};
|
|
22
|
+
logs: {
|
|
23
|
+
send: (events: PostEvent[]) => Promise<Answer<PostResult | null>>;
|
|
24
|
+
down: () => CoreReason | null;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export declare const coreUrlOf: (env: Readonly<Record<string, string | undefined>>) => string | undefined;
|
|
28
|
+
export declare const createCoreClient: (o?: CoreClientOptions) => CoreClient;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { bounded } from './http.js';
|
|
2
|
+
import { sendTo } from './logs.js';
|
|
3
|
+
import { latestOf, listOf } from './market.js';
|
|
4
|
+
import { CoreFailure, fail, reasonOf, reasonOfError, } from './reason.js';
|
|
5
|
+
// The one env name of the core's address: only this module reads it.
|
|
6
|
+
export const CORE_URL_ENV = 'AGENTAGE_CORE_URL';
|
|
7
|
+
export const DEFAULT_TIMEOUT_MS = 2_000;
|
|
8
|
+
export const coreUrlOf = (env) => env[CORE_URL_ENV]?.trim() || undefined;
|
|
9
|
+
const resolved = async (service, url) => {
|
|
10
|
+
if (!url)
|
|
11
|
+
return fail(service, 'no_core', 'no core address');
|
|
12
|
+
if (typeof url === 'string')
|
|
13
|
+
return url;
|
|
14
|
+
return Promise.resolve()
|
|
15
|
+
.then(url)
|
|
16
|
+
.catch((err) => {
|
|
17
|
+
const why = err instanceof Error ? err.message : String(err);
|
|
18
|
+
throw new CoreFailure(reasonOf(service, 'no_core', `no core address: ${why}`, err));
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
const originOf = async (service, url) => {
|
|
22
|
+
const raw = (await resolved(service, url)).trim();
|
|
23
|
+
if (!URL.canParse(raw))
|
|
24
|
+
fail(service, 'no_core', `no core address: ${raw} is not a URL`);
|
|
25
|
+
return new URL(raw).origin;
|
|
26
|
+
};
|
|
27
|
+
// The boundary: whatever happens in the call, the caller gets an answer before the deadline.
|
|
28
|
+
const run = async (s, service, empty, work) => {
|
|
29
|
+
const signal = AbortSignal.timeout(s.timeoutMs);
|
|
30
|
+
try {
|
|
31
|
+
const origin = await bounded(originOf(service, s.url), signal);
|
|
32
|
+
return { data: await bounded(work(origin, signal), signal), down: null };
|
|
33
|
+
}
|
|
34
|
+
catch (err) {
|
|
35
|
+
const down = reasonOfError(service, `the core ${service} (${s.timeoutMs} ms)`, err);
|
|
36
|
+
s.first[service] ??= down;
|
|
37
|
+
return { data: empty, down };
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const marketOf = (s) => {
|
|
41
|
+
const call = (origin, signal) => ({
|
|
42
|
+
service: 'market',
|
|
43
|
+
fetch: s.fetch,
|
|
44
|
+
signal,
|
|
45
|
+
origin,
|
|
46
|
+
});
|
|
47
|
+
const why = (err) => reasonOfError('market', 'the market', err).detail;
|
|
48
|
+
return {
|
|
49
|
+
latest: (id) => run(s, 'market', null, (o, sig) => latestOf(call(o, sig), id)),
|
|
50
|
+
list: () => run(s, 'market', { offered: [], skipped: [] }, (o, sig) => listOf(call(o, sig), why)),
|
|
51
|
+
down: () => s.first.market ?? null,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
// A warn that throws must not turn a dropped send into a rejection.
|
|
55
|
+
const safely = (warn, message) => {
|
|
56
|
+
try {
|
|
57
|
+
warn(message);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
// The warning is lost; the answer still says why.
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
const logsOf = (s, o) => {
|
|
64
|
+
const warn = o.warn ?? ((message) => console.warn(message));
|
|
65
|
+
let warned = false;
|
|
66
|
+
const send = async (events) => {
|
|
67
|
+
const answer = await run(s, 'logs', null, (origin, signal) => sendTo({
|
|
68
|
+
service: 'logs',
|
|
69
|
+
fetch: s.fetch,
|
|
70
|
+
signal,
|
|
71
|
+
origin,
|
|
72
|
+
token: o.token,
|
|
73
|
+
timeoutMs: s.timeoutMs,
|
|
74
|
+
}, events));
|
|
75
|
+
if (answer.down && !warned)
|
|
76
|
+
safely(warn, `${answer.down.message}: ${answer.down.detail}; rows dropped`);
|
|
77
|
+
warned = answer.down !== null;
|
|
78
|
+
return answer;
|
|
79
|
+
};
|
|
80
|
+
return { send, down: () => s.first.logs ?? null };
|
|
81
|
+
};
|
|
82
|
+
// Never throws and never waits past timeoutMs; the caller rules: README "The core client".
|
|
83
|
+
export const createCoreClient = (o = {}) => {
|
|
84
|
+
const s = {
|
|
85
|
+
url: o.url ?? coreUrlOf(o.env ?? {}),
|
|
86
|
+
fetch: o.fetch ?? ((input, init) => fetch(input, init)),
|
|
87
|
+
timeoutMs: o.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
88
|
+
first: {},
|
|
89
|
+
};
|
|
90
|
+
return { market: marketOf(s), logs: logsOf(s, o) };
|
|
91
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type CoreService } from './reason.js';
|
|
2
|
+
export type Fetch = typeof fetch;
|
|
3
|
+
export interface Call {
|
|
4
|
+
service: CoreService;
|
|
5
|
+
fetch: Fetch;
|
|
6
|
+
signal: AbortSignal;
|
|
7
|
+
}
|
|
8
|
+
export declare const bounded: <T>(work: Promise<T>, signal: AbortSignal) => Promise<T>;
|
|
9
|
+
export declare const getJson: (call: Call, url: string) => Promise<{
|
|
10
|
+
status: number;
|
|
11
|
+
body: unknown;
|
|
12
|
+
}>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { CoreFailure, fail, reasonOfError } from './reason.js';
|
|
2
|
+
// A core answer is small; a larger body is not read to the end.
|
|
3
|
+
const CAP = 1_000_000;
|
|
4
|
+
// Settles with the work, or rejects when the deadline fires even if the work ignores the signal.
|
|
5
|
+
export const bounded = (work, signal) => new Promise((resolve, reject) => {
|
|
6
|
+
const stop = () => reject(signal.reason);
|
|
7
|
+
if (signal.aborted)
|
|
8
|
+
return stop();
|
|
9
|
+
signal.addEventListener('abort', stop, { once: true });
|
|
10
|
+
work.then(resolve, reject).finally(() => signal.removeEventListener('abort', stop));
|
|
11
|
+
});
|
|
12
|
+
const textOf = async (call, res, url) => {
|
|
13
|
+
const reader = res.body?.getReader();
|
|
14
|
+
const decoder = new TextDecoder();
|
|
15
|
+
let [text, size] = ['', 0];
|
|
16
|
+
for (let part = await reader?.read(); part && !part.done; part = await reader?.read()) {
|
|
17
|
+
size += part.value.length;
|
|
18
|
+
if (size > CAP) {
|
|
19
|
+
void reader?.cancel().catch(() => undefined);
|
|
20
|
+
fail(call.service, 'failed', `${url} answered over ${CAP} bytes`);
|
|
21
|
+
}
|
|
22
|
+
text += decoder.decode(part.value, { stream: true });
|
|
23
|
+
}
|
|
24
|
+
return text + decoder.decode();
|
|
25
|
+
};
|
|
26
|
+
const parsed = (call, url, status, text) => {
|
|
27
|
+
try {
|
|
28
|
+
return JSON.parse(text);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return fail(call.service, 'not_json', `${url} answered ${status} with a body that is not JSON`);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const sent = (call, url) => bounded(call.fetch(url, {
|
|
35
|
+
redirect: 'manual',
|
|
36
|
+
signal: call.signal,
|
|
37
|
+
headers: { accept: 'application/json' },
|
|
38
|
+
}), call.signal).catch((err) => {
|
|
39
|
+
throw asFailure(call, url, err);
|
|
40
|
+
});
|
|
41
|
+
const asFailure = (call, url, err) => new CoreFailure(reasonOfError(call.service, url, err));
|
|
42
|
+
const bodyOf = (call, res, url) => bounded(textOf(call, res, url), call.signal).catch((err) => {
|
|
43
|
+
throw asFailure(call, url, err);
|
|
44
|
+
});
|
|
45
|
+
const jsonOrNull = (text) => {
|
|
46
|
+
try {
|
|
47
|
+
return JSON.parse(text);
|
|
48
|
+
}
|
|
49
|
+
catch {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
// A redirect is not the core's answer (a sign-in page of a network); a 404 body is null unless JSON.
|
|
54
|
+
export const getJson = async (call, url) => {
|
|
55
|
+
const res = await sent(call, url);
|
|
56
|
+
const { status } = res;
|
|
57
|
+
if (status === 404)
|
|
58
|
+
return { status, body: jsonOrNull(await bodyOf(call, res, url)) };
|
|
59
|
+
if (!res.ok) {
|
|
60
|
+
void res.body?.cancel().catch(() => undefined);
|
|
61
|
+
const to = res.headers.get('location');
|
|
62
|
+
fail(call.service, 'failed', `${url} answered ${status}${to ? ` to ${to}` : ''}`);
|
|
63
|
+
}
|
|
64
|
+
return { status, body: parsed(call, url, status, await bodyOf(call, res, url)) };
|
|
65
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export { CORE_URL_ENV, DEFAULT_TIMEOUT_MS, coreUrlOf, createCoreClient, type CoreClient, type CoreClientOptions, type CoreUrl, } from './core-client.js';
|
|
2
|
+
export { type Fetch } from './http.js';
|
|
3
|
+
export { MAX_PAGES, type Latest, type Listing, type Offered, type Skipped } from './market.js';
|
|
4
|
+
export { type Answer, type CoreCode, type CoreReason, type CoreService } from './reason.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// @agentage/extension/core: the one client of the core's services.
|
|
2
|
+
export { CORE_URL_ENV, DEFAULT_TIMEOUT_MS, coreUrlOf, createCoreClient, } from './core-client.js';
|
|
3
|
+
export {} from './http.js';
|
|
4
|
+
export { MAX_PAGES } from './market.js';
|
|
5
|
+
export {} from './reason.js';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { type PostEvent, type PostResult } from '../vendor/logs/index.js';
|
|
2
|
+
import type { Call } from './http.js';
|
|
3
|
+
export interface LogsCall extends Call {
|
|
4
|
+
origin: string;
|
|
5
|
+
token: string | undefined;
|
|
6
|
+
timeoutMs: number;
|
|
7
|
+
}
|
|
8
|
+
export declare const sendTo: (call: LogsCall, events: PostEvent[]) => Promise<PostResult>;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { createEventsClient } from '../vendor/logs/index.js';
|
|
2
|
+
import { fail } from './reason.js';
|
|
3
|
+
const LOOPBACK = /^(localhost|.+\.localhost|127(\.\d{1,3}){3}|\[::1\]|\[::ffff:7f[0-9a-f]{2}:[0-9a-f]{1,4}\])$/;
|
|
4
|
+
// A bearer goes only over https, or to this machine.
|
|
5
|
+
const safe = (origin) => {
|
|
6
|
+
const url = new URL(origin);
|
|
7
|
+
return url.protocol === 'https:' || LOOPBACK.test(url.hostname);
|
|
8
|
+
};
|
|
9
|
+
const urlOf = (input) => input instanceof Request ? input.url : String(input);
|
|
10
|
+
// Every request of the events client, its retry too, stops at the call's deadline.
|
|
11
|
+
const deadlined = (call) => (input, init) => call.fetch(urlOf(input), {
|
|
12
|
+
...init,
|
|
13
|
+
redirect: 'manual',
|
|
14
|
+
signal: init?.signal ? AbortSignal.any([init.signal, call.signal]) : call.signal,
|
|
15
|
+
});
|
|
16
|
+
// The events client fills, redacts and batches the rows; a failed post rejects and is caught above.
|
|
17
|
+
export const sendTo = async (call, events) => {
|
|
18
|
+
if (!call.token)
|
|
19
|
+
return fail('logs', 'no_token', 'no token for the logs ingest');
|
|
20
|
+
if (!safe(call.origin))
|
|
21
|
+
return fail('logs', 'refused', `${call.origin}: a token goes over https only`);
|
|
22
|
+
const client = createEventsClient({
|
|
23
|
+
url: call.origin,
|
|
24
|
+
token: call.token,
|
|
25
|
+
// Past the call's deadline: the deadline, not the events client, ends a stalled request.
|
|
26
|
+
timeoutMs: call.timeoutMs + 1_000,
|
|
27
|
+
retryDelayMs: 0,
|
|
28
|
+
fetch: deadlined(call),
|
|
29
|
+
});
|
|
30
|
+
return client.post(events);
|
|
31
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Call } from './http.js';
|
|
2
|
+
export interface Latest {
|
|
3
|
+
id: string;
|
|
4
|
+
version: string;
|
|
5
|
+
package: string | null;
|
|
6
|
+
commit: string | null;
|
|
7
|
+
}
|
|
8
|
+
export interface Offered {
|
|
9
|
+
id: string;
|
|
10
|
+
version: string;
|
|
11
|
+
}
|
|
12
|
+
export interface Skipped {
|
|
13
|
+
id: string;
|
|
14
|
+
reason: string;
|
|
15
|
+
}
|
|
16
|
+
export interface Listing {
|
|
17
|
+
offered: Offered[];
|
|
18
|
+
skipped: Skipped[];
|
|
19
|
+
}
|
|
20
|
+
export interface MarketCall extends Call {
|
|
21
|
+
origin: string;
|
|
22
|
+
}
|
|
23
|
+
export declare const isUnknownId: (body: unknown, id: string) => boolean;
|
|
24
|
+
export declare const latestOf: (call: MarketCall, id: string) => Promise<Latest | null>;
|
|
25
|
+
export declare const MAX_PAGES = 20;
|
|
26
|
+
export declare const listOf: (call: MarketCall, why: (e: unknown) => string) => Promise<Listing>;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { getJson } from './http.js';
|
|
2
|
+
import { fail } from './reason.js';
|
|
3
|
+
const BASE = '/api/market/extensions';
|
|
4
|
+
const EXACT = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
|
|
5
|
+
const SHA = /^[0-9a-f]{40}$/;
|
|
6
|
+
const isRecord = (v) => typeof v === 'object' && v !== null && !Array.isArray(v);
|
|
7
|
+
// The market's own refusals for an id it lacks; a router miss or a proxy 404 is none of them.
|
|
8
|
+
const UNKNOWN = [
|
|
9
|
+
(id) => `no listed extension ${id}`,
|
|
10
|
+
(id) => `${id} has no release yet`,
|
|
11
|
+
];
|
|
12
|
+
export const isUnknownId = (body, id) => {
|
|
13
|
+
const error = isRecord(body) && body['success'] === false ? body['error'] : undefined;
|
|
14
|
+
if (!isRecord(error) || error['code'] !== 'NOT_FOUND')
|
|
15
|
+
return false;
|
|
16
|
+
return UNKNOWN.some((text) => error['message'] === text(id));
|
|
17
|
+
};
|
|
18
|
+
// The body of a 2xx; a 404 is the market's answer only when known says so.
|
|
19
|
+
const marketGet = async (call, path, known = (_b) => false) => {
|
|
20
|
+
const url = `${call.origin}${BASE}${path}`;
|
|
21
|
+
const { status, body } = await getJson(call, url);
|
|
22
|
+
if (status !== 404)
|
|
23
|
+
return body;
|
|
24
|
+
return known(body) ? null : fail('market', 'failed', `${url} answered 404, not the market's`);
|
|
25
|
+
};
|
|
26
|
+
const dataOf = (body) => (isRecord(body) ? body['data'] : undefined);
|
|
27
|
+
// An answer is trusted only when it names this id's own package and one exact version.
|
|
28
|
+
const accept = (id, data) => {
|
|
29
|
+
const { version, package: pkg, commit } = data;
|
|
30
|
+
const refuse = (why) => fail('market', 'refused', `market answer refused for ${id}: ${why}`);
|
|
31
|
+
if (typeof version !== 'string' || !EXACT.test(version))
|
|
32
|
+
return refuse(`version ${JSON.stringify(version)} is not an exact version`);
|
|
33
|
+
if (pkg !== null && pkg !== `@agentage/extension-${id}` && pkg !== `agentage-extension-${id}`)
|
|
34
|
+
return refuse(`package ${JSON.stringify(pkg)} is not an extension of that id`);
|
|
35
|
+
return {
|
|
36
|
+
id,
|
|
37
|
+
version,
|
|
38
|
+
package: pkg,
|
|
39
|
+
commit: typeof commit === 'string' && SHA.test(commit) ? commit : null,
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
// null: the market itself said it has no such id, or no release of it.
|
|
43
|
+
export const latestOf = async (call, id) => {
|
|
44
|
+
const known = (body) => isUnknownId(body, id);
|
|
45
|
+
const body = await marketGet(call, `/${encodeURIComponent(id)}/latest`, known);
|
|
46
|
+
if (body === null)
|
|
47
|
+
return null;
|
|
48
|
+
const data = dataOf(body);
|
|
49
|
+
return isRecord(data)
|
|
50
|
+
? accept(id, data)
|
|
51
|
+
: fail('market', 'refused', `${id}: no release in the answer`);
|
|
52
|
+
};
|
|
53
|
+
const PAGE = 200;
|
|
54
|
+
// A market that repeats its cursor would page for ever; past this the listing stops.
|
|
55
|
+
export const MAX_PAGES = 20;
|
|
56
|
+
// /latest calls in flight at once while listing.
|
|
57
|
+
const AT_ONCE = 8;
|
|
58
|
+
const nextOf = (body) => {
|
|
59
|
+
const meta = isRecord(body) ? body['meta'] : undefined;
|
|
60
|
+
const page = isRecord(meta) ? meta['pagination'] : undefined;
|
|
61
|
+
const next = isRecord(page) ? page['nextCursor'] : undefined;
|
|
62
|
+
return typeof next === 'string' ? next : '';
|
|
63
|
+
};
|
|
64
|
+
const rowsOf = async (call, cursor = '', page = 1) => {
|
|
65
|
+
if (page > MAX_PAGES)
|
|
66
|
+
fail('market', 'failed', `the market list has more than ${MAX_PAGES} pages; stopped there`);
|
|
67
|
+
const query = `?limit=${PAGE}${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ''}`;
|
|
68
|
+
const body = await marketGet(call, query);
|
|
69
|
+
const data = dataOf(body);
|
|
70
|
+
const rows = Array.isArray(data) ? data : fail('market', 'refused', 'the list has no rows');
|
|
71
|
+
const next = nextOf(body);
|
|
72
|
+
return next && rows.length > 0 ? [...rows, ...(await rowsOf(call, next, page + 1))] : rows;
|
|
73
|
+
};
|
|
74
|
+
// A row without an exact version asks /latest; its failure skips that row only.
|
|
75
|
+
const rowOf = async (call, row, why) => {
|
|
76
|
+
if (!isRecord(row) || typeof row['id'] !== 'string')
|
|
77
|
+
return null;
|
|
78
|
+
const { id, version } = row;
|
|
79
|
+
if (typeof version === 'string' && EXACT.test(version))
|
|
80
|
+
return { id, version };
|
|
81
|
+
try {
|
|
82
|
+
const latest = await latestOf(call, id);
|
|
83
|
+
return latest
|
|
84
|
+
? { id, version: latest.version }
|
|
85
|
+
: { id, reason: 'the market has no version of it' };
|
|
86
|
+
}
|
|
87
|
+
catch (err) {
|
|
88
|
+
return { id, reason: why(err) };
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
// Each listed id with its newest version; only a failed list call fails the listing.
|
|
92
|
+
export const listOf = async (call, why) => {
|
|
93
|
+
const rows = await rowsOf(call);
|
|
94
|
+
const found = [];
|
|
95
|
+
for (let i = 0; i < rows.length; i += AT_ONCE)
|
|
96
|
+
found.push(...(await Promise.all(rows.slice(i, i + AT_ONCE).map((r) => rowOf(call, r, why)))));
|
|
97
|
+
const listing = { offered: [], skipped: [] };
|
|
98
|
+
for (const row of found)
|
|
99
|
+
if (row)
|
|
100
|
+
'version' in row ? listing.offered.push(row) : listing.skipped.push(row);
|
|
101
|
+
return listing;
|
|
102
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type CoreService = 'market' | 'logs';
|
|
2
|
+
export type CoreCode = 'no_core' | 'unreachable' | 'timeout' | 'failed' | 'not_json' | 'refused' | 'no_token';
|
|
3
|
+
export interface CoreReason {
|
|
4
|
+
service: CoreService;
|
|
5
|
+
code: CoreCode;
|
|
6
|
+
message: string;
|
|
7
|
+
detail: string;
|
|
8
|
+
cause?: unknown;
|
|
9
|
+
}
|
|
10
|
+
export interface Answer<T> {
|
|
11
|
+
data: T;
|
|
12
|
+
down: CoreReason | null;
|
|
13
|
+
}
|
|
14
|
+
export declare const reasonOf: (service: CoreService, code: CoreCode, detail: string, cause?: unknown) => CoreReason;
|
|
15
|
+
export declare class CoreFailure extends Error {
|
|
16
|
+
readonly reason: CoreReason;
|
|
17
|
+
constructor(reason: CoreReason);
|
|
18
|
+
}
|
|
19
|
+
export declare const fail: (service: CoreService, code: CoreCode, detail: string) => never;
|
|
20
|
+
export declare const isTimeout: (err: unknown) => boolean;
|
|
21
|
+
export declare const reasonOfError: (service: CoreService, what: string, err: unknown) => CoreReason;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const MESSAGES = {
|
|
2
|
+
no_core: (s) => `${s} not reachable`,
|
|
3
|
+
unreachable: (s) => `${s} not reachable`,
|
|
4
|
+
timeout: (s) => `${s} not reachable`,
|
|
5
|
+
failed: (s) => `${s} not reachable`,
|
|
6
|
+
not_json: (s) => `${s} not reachable`,
|
|
7
|
+
refused: (s) => `${s} answer refused`,
|
|
8
|
+
no_token: (s) => `${s} needs a token`,
|
|
9
|
+
};
|
|
10
|
+
export const reasonOf = (service, code, detail, cause) => ({
|
|
11
|
+
service,
|
|
12
|
+
code,
|
|
13
|
+
message: MESSAGES[code](service),
|
|
14
|
+
detail,
|
|
15
|
+
...(cause !== undefined && { cause }),
|
|
16
|
+
});
|
|
17
|
+
// Thrown inside the client only; the client boundary turns it into an answer.
|
|
18
|
+
export class CoreFailure extends Error {
|
|
19
|
+
reason;
|
|
20
|
+
constructor(reason) {
|
|
21
|
+
super(reason.detail);
|
|
22
|
+
this.reason = reason;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
export const fail = (service, code, detail) => {
|
|
26
|
+
throw new CoreFailure(reasonOf(service, code, detail));
|
|
27
|
+
};
|
|
28
|
+
const nameOf = (err) => err?.name;
|
|
29
|
+
export const isTimeout = (err) => nameOf(err) === 'TimeoutError' || nameOf(err) === 'AbortError';
|
|
30
|
+
const textOf = (err) => {
|
|
31
|
+
const cause = err?.cause?.message;
|
|
32
|
+
if (typeof cause === 'string')
|
|
33
|
+
return cause;
|
|
34
|
+
return err instanceof Error ? err.message : String(err);
|
|
35
|
+
};
|
|
36
|
+
// Any throw as a reason: the client's own, a timeout, or no connection.
|
|
37
|
+
export const reasonOfError = (service, what, err) => {
|
|
38
|
+
if (err instanceof CoreFailure)
|
|
39
|
+
return err.reason;
|
|
40
|
+
if (typeof err?.status === 'number')
|
|
41
|
+
return reasonOf(service, 'failed', `${what}: ${textOf(err)}`);
|
|
42
|
+
if (isTimeout(err))
|
|
43
|
+
return reasonOf(service, 'timeout', `${what} did not answer in time`);
|
|
44
|
+
return reasonOf(service, 'unreachable', `cannot reach ${what}: ${textOf(err)}`);
|
|
45
|
+
};
|
package/dist/host/host-env.d.ts
CHANGED
package/dist/host/host-env.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { declaredScopes, partAuth } from '../auth/part-auth.js';
|
|
1
|
+
import { declaredScopes, partAuth, platformOf } from '../auth/part-auth.js';
|
|
2
2
|
import { scopeOf } from '../auth/token-check.js';
|
|
3
3
|
import { SECRETS_DIR } from '../context/secrets.js';
|
|
4
4
|
import { parseObject } from '../context/settings.js';
|
|
@@ -28,6 +28,7 @@ export const hostEnvOf = (env) => {
|
|
|
28
28
|
settings: env['AGENTAGE_SETTINGS'] ? parseObject(env['AGENTAGE_SETTINGS']) : {},
|
|
29
29
|
secretsDir: env['AGENTAGE_SECRETS_DIR'] || SECRETS_DIR,
|
|
30
30
|
origin: env['AGENTAGE_ORIGIN'] || `http://127.0.0.1${port ? `:${port}` : ''}`,
|
|
31
|
+
platform: platformOf(env),
|
|
31
32
|
drainMs: msOf(env['AGENTAGE_DRAIN_MS'], GRACE_MS),
|
|
32
33
|
storeOpenMs: msOf(env['AGENTAGE_STORE_OPEN_MS'], STORE_OPEN_MS),
|
|
33
34
|
// G1, G2: the estate's postgres or clickhouse; unset keeps PGlite in dataDir.
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { type Express } from 'express';
|
|
2
|
+
import { type PlatformInfo } from '../context/context.js';
|
|
2
3
|
import { type BuildInfo } from '../discovery/discovery.js';
|
|
3
4
|
import { type HealthBody } from '../health/health.schema.js';
|
|
4
5
|
import { type Log } from '../log/log.js';
|
|
@@ -13,6 +14,7 @@ export interface HostOptions {
|
|
|
13
14
|
secrets?: Record<string, string>;
|
|
14
15
|
onExit?: () => void;
|
|
15
16
|
origin?: string;
|
|
17
|
+
platform?: PlatformInfo;
|
|
16
18
|
localOnly?: boolean;
|
|
17
19
|
localPorts?: number[];
|
|
18
20
|
storeOpenMs?: number;
|
|
@@ -3,6 +3,7 @@ import { stat } from 'node:fs/promises';
|
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { setTimeout as sleep } from 'node:timers/promises';
|
|
5
5
|
import {} from 'express';
|
|
6
|
+
import {} from '../context/context.js';
|
|
6
7
|
import {} from '../discovery/discovery.js';
|
|
7
8
|
import { healthBody } from '../health/health.js';
|
|
8
9
|
import {} from '../health/health.schema.js';
|
package/dist/host/host-parts.js
CHANGED
|
@@ -25,6 +25,7 @@ const contextOf = async (h, part, checks) => createContext({
|
|
|
25
25
|
part,
|
|
26
26
|
origin: h.opts.origin ?? 'http://127.0.0.1',
|
|
27
27
|
commit: h.build.commit,
|
|
28
|
+
platform: h.opts.platform,
|
|
28
29
|
log: part === 'api'
|
|
29
30
|
? h.log
|
|
30
31
|
: createLog({ extension: h.pkg.agentage.id, part, version: h.pkg.version }),
|
package/dist/host/serve-host.js
CHANGED
|
@@ -52,11 +52,20 @@ export const serveHost = async (env, deps = {}) => {
|
|
|
52
52
|
const at = hostEnvOf(env);
|
|
53
53
|
const guard = guardOf(env, at);
|
|
54
54
|
const hook = exitHook();
|
|
55
|
-
const { dataDir, runDir, settings, origin, secretsDir, storeOpenMs, storeUrlFile } = at;
|
|
55
|
+
const { dataDir, runDir, settings, origin, platform, secretsDir, storeOpenMs, storeUrlFile } = at;
|
|
56
56
|
// C2: on loopback the child checks Host and Origin itself, for its own port and the daemon's.
|
|
57
57
|
const local = { localOnly: at.bind === '127.0.0.1', localPorts: loopbackPorts(origin) };
|
|
58
58
|
const store = { storeOpenMs, storeUrlFile };
|
|
59
|
-
const opts = {
|
|
59
|
+
const opts = {
|
|
60
|
+
dataDir,
|
|
61
|
+
runDir,
|
|
62
|
+
settings,
|
|
63
|
+
origin,
|
|
64
|
+
platform,
|
|
65
|
+
...store,
|
|
66
|
+
onExit: hook.onExit,
|
|
67
|
+
...local,
|
|
68
|
+
};
|
|
60
69
|
const build = await hostBuildInfo(env, at.dir);
|
|
61
70
|
const host = await hostWith(at.dir, opts, { secretsDir, guard, build });
|
|
62
71
|
const server = await listen(host.app, at.port, at.bind);
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self' 'sha256-rPnVQ9IYe8CdsZZF35pNR4JDk/fH7Uqv6mzmCnCCky8=' 'sha256-lofENefBf5fETbin4TmrCuvlqGNlwIr9jsrkSYCOq+c=' 'sha256-ZowhplWzFwaUWeLf0FFjuCiJ6T38qHnoSSn0bfCSIG4='; style-src 'self'; img-src 'self' data:; font-src 'self' data:; connect-src 'self'; object-src 'none'; base-uri 'self'; form-action 'self'" />
|
|
5
5
|
<meta charset="utf-8" />
|
|
6
6
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
7
|
-
<meta name="agentage-shell" content="0.
|
|
7
|
+
<meta name="agentage-shell" content="0.20.0" />
|
|
8
8
|
<title>agentage</title>
|
|
9
9
|
<script>
|
|
10
10
|
try {
|
|
@@ -40,57 +40,37 @@ export const batchesOf = (events) => {
|
|
|
40
40
|
}
|
|
41
41
|
return batch.length > 0 ? [...batches, batch] : batches;
|
|
42
42
|
};
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
// The path that answered, per url: a logs host on the one image has no /api/v1/events (404), v13 has no /api/logs/events.
|
|
46
|
-
const paths = new Map();
|
|
43
|
+
// The one ingest path of the logs service.
|
|
44
|
+
const PATH = '/api/logs/events';
|
|
47
45
|
class IngestError extends Error {
|
|
48
46
|
status;
|
|
49
|
-
constructor(status,
|
|
50
|
-
super(`POST ${
|
|
47
|
+
constructor(status, what = `answered ${status}`) {
|
|
48
|
+
super(`POST ${PATH} ${what}`);
|
|
51
49
|
this.status = status;
|
|
52
50
|
}
|
|
53
51
|
}
|
|
54
52
|
// A network error, 429 or 5xx is tried once more; a 4xx would fail the same way again.
|
|
55
53
|
const retries = (err) => !(err instanceof IngestError) || err.status === 429 || err.status >= 500;
|
|
56
|
-
const request = (o, body
|
|
54
|
+
const request = (o, body) => (o.fetch ?? fetch)(`${o.url.replace(/\/+$/, '')}${PATH}`, {
|
|
57
55
|
method: 'POST',
|
|
58
56
|
headers: { authorization: `Bearer ${o.token}`, 'content-type': 'application/json' },
|
|
59
57
|
body,
|
|
60
58
|
signal: AbortSignal.timeout(o.timeoutMs ?? 5000),
|
|
61
59
|
});
|
|
62
|
-
// Statuses the ingest route itself answers: a different route would be a 404 or a proxy's 401, 403, 429 or 5xx.
|
|
63
|
-
const OWN_4XX = new Set([400, 413, 422]);
|
|
64
60
|
const resultOf = (reply) => {
|
|
65
61
|
const data = reply?.data;
|
|
66
62
|
return typeof data?.accepted === 'number' && typeof data.rejected === 'number'
|
|
67
63
|
? { accepted: data.accepted, rejected: data.rejected }
|
|
68
64
|
: undefined;
|
|
69
65
|
};
|
|
70
|
-
// The host moved under the same url: a 404 drops the pin and the other path is tried once.
|
|
71
|
-
const reach = async (o, body, key) => {
|
|
72
|
-
const first = paths.get(key) ?? ONE_IMAGE;
|
|
73
|
-
const res = await request(o, body, first);
|
|
74
|
-
if (res.status !== 404)
|
|
75
|
-
return { res, path: first };
|
|
76
|
-
paths.delete(key);
|
|
77
|
-
const path = first === ONE_IMAGE ? LEGACY : ONE_IMAGE;
|
|
78
|
-
return { res: await request(o, body, path), path };
|
|
79
|
-
};
|
|
80
66
|
const sendOnce = async (o, body) => {
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
if (OWN_4XX.has(res.status))
|
|
85
|
-
paths.set(key, path);
|
|
86
|
-
// Both paths 404: the error names the legacy path, as before the move.
|
|
87
|
-
throw new IngestError(res.status, res.status === 404 ? LEGACY : path);
|
|
88
|
-
}
|
|
67
|
+
const res = await request(o, body);
|
|
68
|
+
if (!res.ok)
|
|
69
|
+
throw new IngestError(res.status);
|
|
89
70
|
// A 2xx was taken: an unreadable or unknown answer is never retried, it would post the rows twice.
|
|
90
71
|
const result = resultOf(await res.json().catch(() => null));
|
|
91
72
|
if (!result)
|
|
92
|
-
throw new IngestError(res.status,
|
|
93
|
-
paths.set(key, path);
|
|
73
|
+
throw new IngestError(res.status, 'answered a body that is not the ingest result');
|
|
94
74
|
return result;
|
|
95
75
|
};
|
|
96
76
|
const pause = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agentage/extension",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.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",
|
|
@@ -36,6 +36,10 @@
|
|
|
36
36
|
"types": "./dist/host/index.d.ts",
|
|
37
37
|
"default": "./dist/host/index.js"
|
|
38
38
|
},
|
|
39
|
+
"./core": {
|
|
40
|
+
"types": "./dist/core-client/index.d.ts",
|
|
41
|
+
"default": "./dist/core-client/index.js"
|
|
42
|
+
},
|
|
39
43
|
"./logs": {
|
|
40
44
|
"types": "./dist/logs/index.d.ts",
|
|
41
45
|
"default": "./dist/logs/index.js"
|