@nylorun/core 0.17.0-beta → 0.18.0-beta
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/CHANGELOG.md +20 -0
- package/README.md +10 -0
- package/dist/contracts.d.ts +12 -2
- package/dist/contracts.js +46 -31
- package/dist/definition/from.js +22 -188
- package/dist/definition/removed.d.ts +4 -2
- package/dist/definition/removed.js +8 -2
- package/dist/harness-api/messages.d.ts +30 -352
- package/dist/harness-api/messages.js +2 -6
- package/dist/harness-api/schema.d.ts +701 -13
- package/dist/harness-api/schema.js +129 -53
- package/dist/harness-api/transcript.d.ts +3 -5
- package/dist/harness-api/transcript.js +0 -5
- package/dist/project.d.ts +67 -0
- package/dist/project.js +210 -0
- package/dist/transport.d.ts +52 -0
- package/dist/transport.js +73 -0
- package/package.json +9 -1
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every Runtime client does on the wire (`@nylorun/core/transport`): the headers of an
|
|
3
|
+
* authenticated request, reading a response body, and the `/health` compatibility probe. It
|
|
4
|
+
* imports nothing from Node, so browser clients use it too. `@nylorun/agents` (the Runtime
|
|
5
|
+
* API), `@nylorun/admin` (the Management API) and Studio's server build on it.
|
|
6
|
+
*/
|
|
7
|
+
import { type Compatibility, type ProtocolRange } from "./compatibility.js";
|
|
8
|
+
export type Incompatibility = Extract<Compatibility, {
|
|
9
|
+
ok: false;
|
|
10
|
+
}>;
|
|
11
|
+
/** A protocol range (`{ min, max, features }`); undefined when `value` is not one. */
|
|
12
|
+
export declare function parseProtocolRange(value: unknown): ProtocolRange | undefined;
|
|
13
|
+
/** The `protocol` a `/health` or `426` body advertises; undefined when it has none. */
|
|
14
|
+
export declare function advertisedProtocol(body: unknown): ProtocolRange | undefined;
|
|
15
|
+
/** Whether a Host advertising `host` serves this client's protocol and required features. */
|
|
16
|
+
export declare function clientCompatibility(host: ProtocolRange): Compatibility;
|
|
17
|
+
/** Why a Host does not serve this client, starting lowercase. */
|
|
18
|
+
export declare function describeIncompatibility(result: Incompatibility): string;
|
|
19
|
+
/** The headers of an authenticated request: this client's protocol and the bearer, when given. */
|
|
20
|
+
export declare function requestHeaders(bearer?: string): Record<string, string>;
|
|
21
|
+
/** A response body's text as JSON, or the text itself when it is not JSON. */
|
|
22
|
+
export declare function parseBody(text: string): unknown;
|
|
23
|
+
/** Reads `response`'s body: JSON, else its text. */
|
|
24
|
+
export declare function readBody(response: Response): Promise<unknown>;
|
|
25
|
+
/** What the Host's `/health` says about this client. */
|
|
26
|
+
export type HealthCheck =
|
|
27
|
+
/** `/health` answered an error status; `body` is what it sent. */
|
|
28
|
+
{
|
|
29
|
+
result: "failed";
|
|
30
|
+
status: number;
|
|
31
|
+
body: unknown;
|
|
32
|
+
}
|
|
33
|
+
/** `/health` advertised no protocol range: a Host older than protocol ranges. */
|
|
34
|
+
| {
|
|
35
|
+
result: "unadvertised";
|
|
36
|
+
body: unknown;
|
|
37
|
+
} | {
|
|
38
|
+
result: "incompatible";
|
|
39
|
+
protocol: ProtocolRange;
|
|
40
|
+
compatibility: Incompatibility;
|
|
41
|
+
} | {
|
|
42
|
+
result: "compatible";
|
|
43
|
+
protocol: ProtocolRange;
|
|
44
|
+
};
|
|
45
|
+
/**
|
|
46
|
+
* `GET <url>/health` (no credential, no redirects) and whether the Host serves this client. A
|
|
47
|
+
* network failure rejects.
|
|
48
|
+
*/
|
|
49
|
+
export declare function checkHealth(url: string, options?: {
|
|
50
|
+
fetch?: typeof fetch;
|
|
51
|
+
signal?: AbortSignal;
|
|
52
|
+
}): Promise<HealthCheck>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What every Runtime client does on the wire (`@nylorun/core/transport`): the headers of an
|
|
3
|
+
* authenticated request, reading a response body, and the `/health` compatibility probe. It
|
|
4
|
+
* imports nothing from Node, so browser clients use it too. `@nylorun/agents` (the Runtime
|
|
5
|
+
* API), `@nylorun/admin` (the Management API) and Studio's server build on it.
|
|
6
|
+
*/
|
|
7
|
+
import { PROTOCOL_FEATURES, PROTOCOL_HEADER, PROTOCOL_VERSION, checkCompatibility, } from "./compatibility.js";
|
|
8
|
+
import { ProtocolRangeSchema } from "./contracts.js";
|
|
9
|
+
// A newer Host may add fields to its range: they are ignored, not refused.
|
|
10
|
+
const ProtocolRangeReader = ProtocolRangeSchema.strip();
|
|
11
|
+
/** A protocol range (`{ min, max, features }`); undefined when `value` is not one. */
|
|
12
|
+
export function parseProtocolRange(value) {
|
|
13
|
+
const parsed = ProtocolRangeReader.safeParse(value);
|
|
14
|
+
return parsed.success ? parsed.data : undefined;
|
|
15
|
+
}
|
|
16
|
+
/** The `protocol` a `/health` or `426` body advertises; undefined when it has none. */
|
|
17
|
+
export function advertisedProtocol(body) {
|
|
18
|
+
return body && typeof body === "object" && !Array.isArray(body)
|
|
19
|
+
? parseProtocolRange(body.protocol)
|
|
20
|
+
: undefined;
|
|
21
|
+
}
|
|
22
|
+
/** Whether a Host advertising `host` serves this client's protocol and required features. */
|
|
23
|
+
export function clientCompatibility(host) {
|
|
24
|
+
return checkCompatibility({ version: PROTOCOL_VERSION, required: [...PROTOCOL_FEATURES] }, host);
|
|
25
|
+
}
|
|
26
|
+
/** Why a Host does not serve this client, starting lowercase. */
|
|
27
|
+
export function describeIncompatibility(result) {
|
|
28
|
+
return result.reason === "version"
|
|
29
|
+
? `client protocol ${result.client} is outside Host range ${result.host.min}–${result.host.max}`
|
|
30
|
+
: `Host is missing required features: ${result.missing.join(", ")}`;
|
|
31
|
+
}
|
|
32
|
+
/** The headers of an authenticated request: this client's protocol and the bearer, when given. */
|
|
33
|
+
export function requestHeaders(bearer) {
|
|
34
|
+
return {
|
|
35
|
+
[PROTOCOL_HEADER]: String(PROTOCOL_VERSION),
|
|
36
|
+
...(bearer ? { authorization: `Bearer ${bearer}` } : {}),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/** A response body's text as JSON, or the text itself when it is not JSON. */
|
|
40
|
+
export function parseBody(text) {
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(text);
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return text;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/** Reads `response`'s body: JSON, else its text. */
|
|
49
|
+
export async function readBody(response) {
|
|
50
|
+
return parseBody(await response.text());
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* `GET <url>/health` (no credential, no redirects) and whether the Host serves this client. A
|
|
54
|
+
* network failure rejects.
|
|
55
|
+
*/
|
|
56
|
+
export async function checkHealth(url, options = {}) {
|
|
57
|
+
const fetcher = options.fetch ?? globalThis.fetch;
|
|
58
|
+
const response = await fetcher(`${url}/health`, {
|
|
59
|
+
method: "GET",
|
|
60
|
+
redirect: "error",
|
|
61
|
+
...(options.signal ? { signal: options.signal } : {}),
|
|
62
|
+
});
|
|
63
|
+
const body = await readBody(response);
|
|
64
|
+
if (!response.ok)
|
|
65
|
+
return { result: "failed", status: response.status, body };
|
|
66
|
+
const protocol = advertisedProtocol(body);
|
|
67
|
+
if (!protocol)
|
|
68
|
+
return { result: "unadvertised", body };
|
|
69
|
+
const compatibility = clientCompatibility(protocol);
|
|
70
|
+
return compatibility.ok
|
|
71
|
+
? { result: "compatible", protocol }
|
|
72
|
+
: { result: "incompatible", protocol, compatibility };
|
|
73
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nylorun/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0-beta",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"description": "Shared agent definitions and wire contracts",
|
|
@@ -54,6 +54,14 @@
|
|
|
54
54
|
"types": "./dist/compatibility.d.ts",
|
|
55
55
|
"import": "./dist/compatibility.js"
|
|
56
56
|
},
|
|
57
|
+
"./transport": {
|
|
58
|
+
"types": "./dist/transport.d.ts",
|
|
59
|
+
"import": "./dist/transport.js"
|
|
60
|
+
},
|
|
61
|
+
"./project": {
|
|
62
|
+
"types": "./dist/project.d.ts",
|
|
63
|
+
"import": "./dist/project.js"
|
|
64
|
+
},
|
|
57
65
|
"./harness-api": {
|
|
58
66
|
"types": "./dist/harness-api/index.d.ts",
|
|
59
67
|
"import": "./dist/harness-api/index.js"
|