@cotal-ai/web 0.171.1 → 0.171.3
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/web-post.d.ts +87 -0
- package/dist/web-post.d.ts.map +1 -0
- package/dist/web-post.js +171 -0
- package/dist/web-post.js.map +1 -0
- package/dist/web-read.d.ts +306 -0
- package/dist/web-read.d.ts.map +1 -0
- package/dist/web-read.js +471 -0
- package/dist/web-read.js.map +1 -0
- package/dist/web.d.ts +3 -374
- package/dist/web.d.ts.map +1 -1
- package/dist/web.js +8 -632
- package/dist/web.js.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { CotalEndpoint, type SpaceAuth } from "@cotal-ai/core";
|
|
2
|
+
/** What the dashboard can post as, decided once at startup and reported in `/api/meta`. A
|
|
3
|
+
* `participant` posts as one operator for the life of the process and joins the roster on its
|
|
4
|
+
* first post, on open and static-auth meshes alike. */
|
|
5
|
+
export type PostCapability = {
|
|
6
|
+
mode: "participant";
|
|
7
|
+
} | {
|
|
8
|
+
mode: "off";
|
|
9
|
+
reason: string;
|
|
10
|
+
};
|
|
11
|
+
/** Why posting is off on a dashboard started with `--creds`: the file is the only credential it
|
|
12
|
+
* holds, and it has no seed to mint an operator from. */
|
|
13
|
+
export declare const POST_OFF_CREDS = "This dashboard was started with --creds, which can only read.";
|
|
14
|
+
/** Why posting is off on a per-user sign-in mesh. */
|
|
15
|
+
export declare const POST_OFF_USER = "Posting from the dashboard isn't available on a sign-in mesh yet. Use `cotal send`.";
|
|
16
|
+
/** Why posting is off on a dashboard run as a service: the service holds a read-only credential. */
|
|
17
|
+
export declare const POST_OFF_SERVICE = "This dashboard runs as a read-only service. Use `cotal send`.";
|
|
18
|
+
/** The operator credential's lifetime: 24 days. Core arms a reconnect fence with one `setTimeout`
|
|
19
|
+
* at the credential's expiry, and Node caps a timer at 2^31-1 ms (about 24.8 days), so a longer
|
|
20
|
+
* credential turns library reconnect off 1 ms after every connect. */
|
|
21
|
+
export declare const OPERATOR_TTL_SECONDS: number;
|
|
22
|
+
/** The posting decision. `--creds` wins: a raw credential file is all such a dashboard holds. */
|
|
23
|
+
export declare function postCapability(creds: string | undefined, userMode: boolean): PostCapability;
|
|
24
|
+
/** The static-mesh operator: its credential, the lifecycle uid its presence registers under, and
|
|
25
|
+
* the credential's expiry in epoch milliseconds, read from the credential itself. */
|
|
26
|
+
export interface OperatorCred {
|
|
27
|
+
creds: string;
|
|
28
|
+
lifecycleUid: string;
|
|
29
|
+
expiresAt: number;
|
|
30
|
+
}
|
|
31
|
+
/** Mints the operator for a static-auth mesh with one new identity. Called once, from
|
|
32
|
+
* {@link connectWithoutSeed}, so every post the process makes carries that one `from.id` and an
|
|
33
|
+
* agent's replies land in one DM thread. With `consume: false` the uid names no broker resources,
|
|
34
|
+
* so it needs no provisioning. */
|
|
35
|
+
export declare function mintOperatorCred(auth: SpaceAuth): Promise<OperatorCred>;
|
|
36
|
+
/** The dashboard's one posting endpoint, built at startup and started on the first post. */
|
|
37
|
+
export interface OperatorPoster {
|
|
38
|
+
/** The id the operator posts as, once it is known: from startup on a static mesh, whose
|
|
39
|
+
* credential names it, and from the first post on an open mesh. */
|
|
40
|
+
operatorId(): string | undefined;
|
|
41
|
+
/** Starts the endpoint once. Concurrent first posts share one start. A failed start is stopped
|
|
42
|
+
* and dropped, so the next post tries again under the same id. Rejects once the poster has
|
|
43
|
+
* stopped. */
|
|
44
|
+
ensure(): Promise<CotalEndpoint>;
|
|
45
|
+
/** Whether the operator credential has expired, so a refused post can say why. Never on an open
|
|
46
|
+
* mesh, which has no credential. */
|
|
47
|
+
expired(): boolean;
|
|
48
|
+
/** Stops the endpoint if it started, so the operator leaves an offline record, and closes the
|
|
49
|
+
* poster: a start still in flight stops what it started, and a later post is refused. */
|
|
50
|
+
stop(): Promise<void>;
|
|
51
|
+
}
|
|
52
|
+
/** What `ensure()` rejects with once the poster has stopped. */
|
|
53
|
+
export declare const POSTER_STOPPED = "the dashboard is shutting down, so it no longer posts";
|
|
54
|
+
/** Why a post is refused once the operator credential has expired. */
|
|
55
|
+
export declare const POST_OFF_EXPIRED = "The operator credential expired. Restart cotal web to post again.";
|
|
56
|
+
/** What `POST /api/send` answers for a post that went out. `seq` is a DM's stored sequence, the
|
|
57
|
+
* broker's proof that it kept the message; a channel post has none. */
|
|
58
|
+
export interface SendAnswer {
|
|
59
|
+
ok: true;
|
|
60
|
+
id: string;
|
|
61
|
+
operatorId: string;
|
|
62
|
+
seq?: number;
|
|
63
|
+
}
|
|
64
|
+
/** `POST /api/send`: posts the body `{kind: "msg", channel, text}` to one channel, or
|
|
65
|
+
* `{kind: "dm", to, text}` to one roster member, as the operator. Separate from the handler so the
|
|
66
|
+
* refusals that need a stopped or expired poster can be driven with one.
|
|
67
|
+
*
|
|
68
|
+
* The body is read before any refusal, so the size cap bounds what a refused post costs: a reply
|
|
69
|
+
* written over an unread body leaves Node draining the rest of it to keep the connection. Every
|
|
70
|
+
* check runs before the poster starts, so a refused post never puts the operator on the roster.
|
|
71
|
+
* Throws `PayloadTooLarge`, `BadRequest` and `BrokerRefused` for the request frame to answer. */
|
|
72
|
+
export declare function sendAsOperator(post: PostCapability, poster: OperatorPoster | undefined, onRoster: (id: string) => boolean, readRequest: () => Promise<unknown>): Promise<{
|
|
73
|
+
status: number;
|
|
74
|
+
body: SendAnswer | {
|
|
75
|
+
error: string;
|
|
76
|
+
};
|
|
77
|
+
}>;
|
|
78
|
+
/** Builds the operator poster: a presence-registering endpoint with the card
|
|
79
|
+
* `{ name: "operator", role: "operator", kind: "endpoint" }`, on the static operator credential
|
|
80
|
+
* when there is one. The id is fixed here. The credential names it on a static mesh, and an open
|
|
81
|
+
* mesh pins one, so a start retried after a failure keeps it. */
|
|
82
|
+
export declare function operatorPoster(target: {
|
|
83
|
+
space: string;
|
|
84
|
+
server: string;
|
|
85
|
+
tls: boolean;
|
|
86
|
+
}, operator: OperatorCred | undefined): OperatorPoster;
|
|
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,cAAc,0DAA0D,CAAC;AAEtF,sEAAsE;AACtE,eAAO,MAAM,gBAAgB,sEAAsE,CAAC;AAEpG;wEACwE;AACxE,MAAM,WAAW,UAAU;IAAG,EAAE,EAAE,IAAI,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAA;CAAE;AAEtF;;;;;;;kGAOkG;AAClG,wBAAsB,cAAc,CAClC,IAAI,EAAE,cAAc,EACpB,MAAM,EAAE,cAAc,GAAG,SAAS,EAClC,QAAQ,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,EACjC,WAAW,EAAE,MAAM,OAAO,CAAC,OAAO,CAAC,GAClC,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,UAAU,GAAG;QAAE,KAAK,EAAE,MAAM,CAAA;KAAE,CAAA;CAAE,CAAC,CA6CnE;AAED;;;kEAGkE;AAClE,wBAAgB,cAAc,CAAC,MAAM,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,OAAO,CAAA;CAAE,EAAE,QAAQ,EAAE,YAAY,GAAG,SAAS,GAAG,cAAc,CA0D1I"}
|
package/dist/web-post.js
ADDED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// The dashboard's operator posting, moved verbatim from web.ts: the posting decision, the operator
|
|
2
|
+
// credential, the poster and `POST /api/send`. web.ts re-exports the public names.
|
|
3
|
+
import { randomUUID } from "node:crypto";
|
|
4
|
+
import { CotalEndpoint, credsClaims, mintCreds, mintLifecycleUid, newIdentity, mentionsIn, } from "@cotal-ai/core";
|
|
5
|
+
import { c } from "@cotal-ai/workspace";
|
|
6
|
+
import { BadRequest, PayloadTooLarge, BrokerRefused, quoteForOperator, escapeForOperator, canonicalChannel, } from "./web-request.js";
|
|
7
|
+
/** Why posting is off on a dashboard started with `--creds`: the file is the only credential it
|
|
8
|
+
* holds, and it has no seed to mint an operator from. */
|
|
9
|
+
export const POST_OFF_CREDS = "This dashboard was started with --creds, which can only read.";
|
|
10
|
+
/** Why posting is off on a per-user sign-in mesh. */
|
|
11
|
+
export const POST_OFF_USER = "Posting from the dashboard isn't available on a sign-in mesh yet. Use `cotal send`.";
|
|
12
|
+
/** Why posting is off on a dashboard run as a service: the service holds a read-only credential. */
|
|
13
|
+
export const POST_OFF_SERVICE = "This dashboard runs as a read-only service. Use `cotal send`.";
|
|
14
|
+
/** The operator credential's lifetime: 24 days. Core arms a reconnect fence with one `setTimeout`
|
|
15
|
+
* at the credential's expiry, and Node caps a timer at 2^31-1 ms (about 24.8 days), so a longer
|
|
16
|
+
* credential turns library reconnect off 1 ms after every connect. */
|
|
17
|
+
export const OPERATOR_TTL_SECONDS = 24 * 24 * 3600;
|
|
18
|
+
/** The posting decision. `--creds` wins: a raw credential file is all such a dashboard holds. */
|
|
19
|
+
export function postCapability(creds, userMode) {
|
|
20
|
+
if (creds !== undefined)
|
|
21
|
+
return { mode: "off", reason: POST_OFF_CREDS };
|
|
22
|
+
if (userMode)
|
|
23
|
+
return { mode: "off", reason: POST_OFF_USER };
|
|
24
|
+
return { mode: "participant" };
|
|
25
|
+
}
|
|
26
|
+
/** Mints the operator for a static-auth mesh with one new identity. Called once, from
|
|
27
|
+
* {@link connectWithoutSeed}, so every post the process makes carries that one `from.id` and an
|
|
28
|
+
* agent's replies land in one DM thread. With `consume: false` the uid names no broker resources,
|
|
29
|
+
* so it needs no provisioning. */
|
|
30
|
+
export async function mintOperatorCred(auth) {
|
|
31
|
+
const creds = await mintCreds(auth, newIdentity(), "operator", { expiresInSeconds: OPERATOR_TTL_SECONDS });
|
|
32
|
+
const exp = credsClaims(creds).exp;
|
|
33
|
+
if (exp === undefined)
|
|
34
|
+
throw new Error("the operator credential was minted without an expiry");
|
|
35
|
+
return { creds, lifecycleUid: mintLifecycleUid(), expiresAt: exp * 1000 };
|
|
36
|
+
}
|
|
37
|
+
/** What `ensure()` rejects with once the poster has stopped. */
|
|
38
|
+
export const POSTER_STOPPED = "the dashboard is shutting down, so it no longer posts";
|
|
39
|
+
/** Why a post is refused once the operator credential has expired. */
|
|
40
|
+
export const POST_OFF_EXPIRED = "The operator credential expired. Restart cotal web to post again.";
|
|
41
|
+
/** `POST /api/send`: posts the body `{kind: "msg", channel, text}` to one channel, or
|
|
42
|
+
* `{kind: "dm", to, text}` to one roster member, as the operator. Separate from the handler so the
|
|
43
|
+
* refusals that need a stopped or expired poster can be driven with one.
|
|
44
|
+
*
|
|
45
|
+
* The body is read before any refusal, so the size cap bounds what a refused post costs: a reply
|
|
46
|
+
* written over an unread body leaves Node draining the rest of it to keep the connection. Every
|
|
47
|
+
* check runs before the poster starts, so a refused post never puts the operator on the roster.
|
|
48
|
+
* Throws `PayloadTooLarge`, `BadRequest` and `BrokerRefused` for the request frame to answer. */
|
|
49
|
+
export async function sendAsOperator(post, poster, onRoster, readRequest) {
|
|
50
|
+
const read = await readRequest().then((value) => ({ value }), (e) => {
|
|
51
|
+
if (e instanceof PayloadTooLarge)
|
|
52
|
+
throw e;
|
|
53
|
+
return undefined;
|
|
54
|
+
});
|
|
55
|
+
if (post.mode === "off")
|
|
56
|
+
return { status: 403, body: { error: post.reason } };
|
|
57
|
+
if (!poster)
|
|
58
|
+
throw new Error("posting is on but no operator poster was built");
|
|
59
|
+
if (poster.expired())
|
|
60
|
+
return { status: 403, body: { error: POST_OFF_EXPIRED } };
|
|
61
|
+
const body = read?.value;
|
|
62
|
+
if (typeof body !== "object" || body === null || Array.isArray(body))
|
|
63
|
+
throw new BadRequest("the request body must be a JSON object");
|
|
64
|
+
const { kind, channel, to, text: raw } = body;
|
|
65
|
+
const text = typeof raw === "string" ? raw.trim() : "";
|
|
66
|
+
if (!text)
|
|
67
|
+
throw new BadRequest("text required");
|
|
68
|
+
let target;
|
|
69
|
+
if (kind === "msg") {
|
|
70
|
+
if (typeof channel !== "string")
|
|
71
|
+
throw new BadRequest("channel required");
|
|
72
|
+
const name = canonicalChannel(channel);
|
|
73
|
+
if (name.includes("*") || name.includes(">"))
|
|
74
|
+
throw new BadRequest(`${quoteForOperator(name)} is not one channel`);
|
|
75
|
+
target = { channel: name };
|
|
76
|
+
}
|
|
77
|
+
else if (kind === "dm") {
|
|
78
|
+
if (typeof to !== "string")
|
|
79
|
+
throw new BadRequest("to required");
|
|
80
|
+
if (!onRoster(to))
|
|
81
|
+
throw new BadRequest(`no agent with id ${quoteForOperator(to)} is on the roster`);
|
|
82
|
+
target = { to };
|
|
83
|
+
}
|
|
84
|
+
else
|
|
85
|
+
throw new BadRequest('kind must be "msg" or "dm"');
|
|
86
|
+
let sender;
|
|
87
|
+
try {
|
|
88
|
+
sender = await poster.ensure();
|
|
89
|
+
}
|
|
90
|
+
catch (e) {
|
|
91
|
+
const why = e instanceof Error ? e.message : String(e);
|
|
92
|
+
if (why === POSTER_STOPPED)
|
|
93
|
+
return { status: 503, body: { error: why } };
|
|
94
|
+
throw new BrokerRefused(escapeForOperator(why));
|
|
95
|
+
}
|
|
96
|
+
const refused = (e) => {
|
|
97
|
+
throw new BrokerRefused(escapeForOperator(e instanceof Error ? e.message : String(e)));
|
|
98
|
+
};
|
|
99
|
+
if ("channel" in target) {
|
|
100
|
+
const msg = await sender.multicast(text, { channel: target.channel, mentions: mentionsIn(text) }).catch(refused);
|
|
101
|
+
return { status: 200, body: { ok: true, id: msg.id, operatorId: sender.card.id } };
|
|
102
|
+
}
|
|
103
|
+
const { msg, ack } = await sender.unicastAttributed(target.to, text).catch(refused);
|
|
104
|
+
return { status: 200, body: { ok: true, id: msg.id, operatorId: sender.card.id, seq: ack.seq } };
|
|
105
|
+
}
|
|
106
|
+
/** Builds the operator poster: a presence-registering endpoint with the card
|
|
107
|
+
* `{ name: "operator", role: "operator", kind: "endpoint" }`, on the static operator credential
|
|
108
|
+
* when there is one. The id is fixed here. The credential names it on a static mesh, and an open
|
|
109
|
+
* mesh pins one, so a start retried after a failure keeps it. */
|
|
110
|
+
export function operatorPoster(target, operator) {
|
|
111
|
+
const card = { ...(operator ? {} : { id: randomUUID().replace(/-/g, "") }), name: "operator", role: "operator", kind: "endpoint" };
|
|
112
|
+
const build = () => {
|
|
113
|
+
const e = new CotalEndpoint({
|
|
114
|
+
space: target.space,
|
|
115
|
+
servers: target.server,
|
|
116
|
+
tls: target.tls,
|
|
117
|
+
...(operator ? { creds: operator.creds, lifecycleUid: operator.lifecycleUid } : {}),
|
|
118
|
+
card,
|
|
119
|
+
channels: [],
|
|
120
|
+
consume: false,
|
|
121
|
+
registerPresence: true,
|
|
122
|
+
watchPresence: false,
|
|
123
|
+
});
|
|
124
|
+
e.on("error", (err) => console.error(c.red("! operator: " + err.message)));
|
|
125
|
+
e.on("warning", (err) => console.error(c.yellow("! operator: " + err.message)));
|
|
126
|
+
return e;
|
|
127
|
+
};
|
|
128
|
+
let endpoint = build();
|
|
129
|
+
let live;
|
|
130
|
+
let starting;
|
|
131
|
+
let closed = false;
|
|
132
|
+
const start = async () => {
|
|
133
|
+
const e = endpoint;
|
|
134
|
+
try {
|
|
135
|
+
await e.start();
|
|
136
|
+
}
|
|
137
|
+
catch (err) {
|
|
138
|
+
// Dropped quietly, the way the console drops a peer that never started, and stopped before
|
|
139
|
+
// the next post may start its successor, so this one's offline record cannot land after the
|
|
140
|
+
// successor's online one.
|
|
141
|
+
e.removeAllListeners("error");
|
|
142
|
+
e.removeAllListeners("warning");
|
|
143
|
+
e.on("error", () => { });
|
|
144
|
+
await e.stop().catch(() => undefined);
|
|
145
|
+
endpoint = build();
|
|
146
|
+
starting = undefined;
|
|
147
|
+
throw err;
|
|
148
|
+
}
|
|
149
|
+
if (closed) {
|
|
150
|
+
await e.stop();
|
|
151
|
+
throw new Error(POSTER_STOPPED);
|
|
152
|
+
}
|
|
153
|
+
return (live = e);
|
|
154
|
+
};
|
|
155
|
+
return {
|
|
156
|
+
operatorId: () => (operator || live ? endpoint.card.id : undefined),
|
|
157
|
+
ensure() {
|
|
158
|
+
if (closed)
|
|
159
|
+
return Promise.reject(new Error(POSTER_STOPPED));
|
|
160
|
+
starting ??= start();
|
|
161
|
+
return starting;
|
|
162
|
+
},
|
|
163
|
+
expired: () => operator !== undefined && Date.now() >= operator.expiresAt,
|
|
164
|
+
async stop() {
|
|
165
|
+
closed = true;
|
|
166
|
+
await starting?.catch(() => undefined);
|
|
167
|
+
await live?.stop();
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
//# sourceMappingURL=web-post.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-post.js","sourceRoot":"","sources":["../src/web-post.ts"],"names":[],"mappings":"AAAA,mGAAmG;AACnG,mFAAmF;AACnF,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACL,aAAa,EACb,WAAW,EACX,SAAS,EACT,gBAAgB,EAChB,WAAW,EACX,UAAU,GAEX,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,CAAC,EAAE,MAAM,qBAAqB,CAAC;AACxC,OAAO,EACL,UAAU,EACV,eAAe,EACf,aAAa,EACb,gBAAgB,EAChB,iBAAiB,EACjB,gBAAgB,GACjB,MAAM,kBAAkB,CAAC;AAQ1B;0DAC0D;AAC1D,MAAM,CAAC,MAAM,cAAc,GAAG,+DAA+D,CAAC;AAC9F,qDAAqD;AACrD,MAAM,CAAC,MAAM,aAAa,GAAG,qFAAqF,CAAC;AACnH,oGAAoG;AACpG,MAAM,CAAC,MAAM,gBAAgB,GAAG,+DAA+D,CAAC;AAEhG;;uEAEuE;AACvE,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,GAAG,EAAE,GAAG,IAAI,CAAC;AAEnD,iGAAiG;AACjG,MAAM,UAAU,cAAc,CAAC,KAAyB,EAAE,QAAiB;IACzE,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc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YACnB,QAAQ,GAAG,SAAS,CAAC;YACrB,MAAM,GAAG,CAAC;QACZ,CAAC;QACD,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC;IACpB,CAAC,CAAC;IACF,OAAO;QACL,UAAU,EAAE,GAAG,EAAE,CAAC,CAAC,QAAQ,IAAI,IAAI,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC;QACnE,MAAM;YACJ,IAAI,MAAM;gBAAE,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,cAAc,CAAC,CAAC,CAAC;YAC7D,QAAQ,KAAK,KAAK,EAAE,CAAC;YACrB,OAAO,QAAQ,CAAC;QAClB,CAAC;QACD,OAAO,EAAE,GAAG,EAAE,CAAC,QAAQ,KAAK,SAAS,IAAI,IAAI,CAAC,GAAG,EAAE,IAAI,QAAQ,CAAC,SAAS;QACzE,KAAK,CAAC,IAAI;YACR,MAAM,GAAG,IAAI,CAAC;YACd,MAAM,QAAQ,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;YACvC,MAAM,IAAI,EAAE,IAAI,EAAE,CAAC;QACrB,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { type CotalMessage } from "@cotal-ai/core";
|
|
2
|
+
/** The one condition name for "the membership read did not answer", shared by the HTTP body and the
|
|
3
|
+
* SSE event so a browser matches ONE token and a test asserts the same token the server emits.
|
|
4
|
+
* Exported so a test asserts the token the server emits rather than a restated copy. */
|
|
5
|
+
export declare const MEMBERSHIP_READ_FAILED = "membership-read-failed";
|
|
6
|
+
/** One servable file of the client build. `immutable` marks a content-hashed asset, which a
|
|
7
|
+
* browser may cache forever because a new build gives it a new name. */
|
|
8
|
+
interface ClientFile {
|
|
9
|
+
path: string;
|
|
10
|
+
type: string;
|
|
11
|
+
immutable: boolean;
|
|
12
|
+
}
|
|
13
|
+
/** The answer when the client build is missing: the server keeps serving the API and says what to
|
|
14
|
+
* do instead of serving nothing. It speaks to both readers who can meet it: in a source checkout
|
|
15
|
+
* the page has not been built yet, while an installed package always ships its page, so there the
|
|
16
|
+
* install itself is broken. */
|
|
17
|
+
export declare const CLIENT_NOT_BUILT: string;
|
|
18
|
+
/** The classic script the page loads in its head, before the stylesheet, to put the saved theme on
|
|
19
|
+
* the root element before anything is drawn (`client/src/lib/themeBoot.ts`, written by the build). */
|
|
20
|
+
export declare const THEME_BOOT = "theme-boot.js";
|
|
21
|
+
/** The client build as an allow-list, read once at startup: the page at `/` (and `/graph`, so an
|
|
22
|
+
* old link opens the app), the theme boot script at `/theme-boot.js`, and the files directly inside
|
|
23
|
+
* `assets/`. Nothing else under `dir` is reachable, and no request path is ever joined onto the
|
|
24
|
+
* filesystem. The boot script's name carries no hash, so unlike the assets it is not cached for good. */
|
|
25
|
+
export declare function clientAssets(dir: string): Map<string, ClientFile>;
|
|
26
|
+
/** The answer to a static request: the file when the build has it, a 503 naming the build command
|
|
27
|
+
* when the build is missing and the page is asked for, otherwise a 404. A pure function of the
|
|
28
|
+
* route map and the path, so the 503 is testable without a server. */
|
|
29
|
+
export declare function clientResponse(routes: Map<string, ClientFile>, path: string): {
|
|
30
|
+
status: 200;
|
|
31
|
+
file: ClientFile;
|
|
32
|
+
} | {
|
|
33
|
+
status: 503;
|
|
34
|
+
body: {
|
|
35
|
+
error: string;
|
|
36
|
+
};
|
|
37
|
+
} | {
|
|
38
|
+
status: 404;
|
|
39
|
+
};
|
|
40
|
+
/** What the two backfill routes need from an endpoint. Narrow on purpose: it is the seam the filter
|
|
41
|
+
* is measured through, and a mock satisfying six methods it never calls would prove less. */
|
|
42
|
+
export interface ActivitySource {
|
|
43
|
+
listChannels(): Promise<{
|
|
44
|
+
channel: string;
|
|
45
|
+
messages: number;
|
|
46
|
+
config?: unknown;
|
|
47
|
+
}[]>;
|
|
48
|
+
multiChannelHistory(channels: readonly string[], opts: {
|
|
49
|
+
limit: number;
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
}): Promise<{
|
|
52
|
+
channel: string;
|
|
53
|
+
msg: CotalMessage;
|
|
54
|
+
}[]>;
|
|
55
|
+
dmHistory(opts: {
|
|
56
|
+
limit: number;
|
|
57
|
+
signal?: AbortSignal;
|
|
58
|
+
}): Promise<CotalMessage[]>;
|
|
59
|
+
}
|
|
60
|
+
/** The channels this dashboard LISTS and BACKFILLS: chat only.
|
|
61
|
+
*
|
|
62
|
+
* WHY AN AGENT'S EVENT CHANNEL IS NOT ONE OF THEM. `listChannels()` derives a row from every
|
|
63
|
+
* retained concrete subject and the chat stream caps per subject rather than by age, so the list
|
|
64
|
+
* grows by one row per agent that has ever run and those rows never age out. Unfiltered, the channel
|
|
65
|
+
* sidebar is buried under machine streams, the client's graph grows a hub node for each, and
|
|
66
|
+
* `/api/activity` merges every event channel into a global top-N that a human reading chat did not
|
|
67
|
+
* ask for, and pays to move those messages across the link first. That is a cost that scales with
|
|
68
|
+
* the number of agents ever run, which is the wrong axis entirely.
|
|
69
|
+
*
|
|
70
|
+
* FILTERED BEFORE THE FETCH, NOT AFTER, AND THE ORDER IS THE CLAIM. Filtering the merged output
|
|
71
|
+
* would still move every byte and then discard it. The activity backfill reads all of chat in
|
|
72
|
+
* ONE stream read, and this classifier decides that read's FILTER SET, so an event channel is
|
|
73
|
+
* dropped by the broker and never crosses the link at all. The console applies the identical rule at
|
|
74
|
+
* the identical point (`mesh-view.ts` filters `listChannels()` before `channelHistory`), and the two
|
|
75
|
+
* surfaces share this classifier rather than each spelling the convention, so they cannot disagree
|
|
76
|
+
* about what a channel is.
|
|
77
|
+
*
|
|
78
|
+
* WHAT IS NOT FILTERED, DELIBERATELY, IN TWO PLACES. The live SSE tap still receives frames, and
|
|
79
|
+
* sends each one as an `agent-frame` only to the feeds watching that agent (see {@link classifyTap}
|
|
80
|
+
* and {@link SessionWatchers}), never as a `message` to every browser and never dropped for a
|
|
81
|
+
* watcher. And `/api/channels/<name>/history` still serves an event channel
|
|
82
|
+
* when a caller names one, because that route answers a question about a channel the caller already
|
|
83
|
+
* identified; a filter there would mean the dashboard could render a frame it could never fetch. */
|
|
84
|
+
export declare function chatOnly<T extends {
|
|
85
|
+
channel: string;
|
|
86
|
+
}>(rows: readonly T[]): T[];
|
|
87
|
+
/** How long one aggregating request may take before it answers with what it has.
|
|
88
|
+
*
|
|
89
|
+
* WHY A DEADLINE AT ALL, with the measurement that set it. The cost of a history read is the link,
|
|
90
|
+
* not the broker. Against a local broker behind a 160ms-RTT, 128 KiB/s link with 40 channels and
|
|
91
|
+
* 12000 messages, an unbounded aggregation reading one source per channel finished in 125ms for a
|
|
92
|
+
* reader ON the broker host and returned 500 `timeout` after 15.94s for the reader across the link;
|
|
93
|
+
* at a less constrained 256 KiB/s it SUCCEEDED after 34491ms, which is the same defect with a
|
|
94
|
+
* different ending. An unbounded aggregation has no answer for either case.
|
|
95
|
+
*
|
|
96
|
+
* WHY ONE READ FOR ALL OF CHAT STILL NEEDS IT. A bound is not a repair for a read that is too
|
|
97
|
+
* expensive; it is what makes the surface answer at all when a link cannot serve a request in a
|
|
98
|
+
* useful time. Any read across any link can still be too slow, and a page that says what it is
|
|
99
|
+
* missing beats one that never arrives.
|
|
100
|
+
*
|
|
101
|
+
* WHY THIS NUMBER. It is longer than a healthy remote read of this shape and far shorter than a
|
|
102
|
+
* reader will sit in front of a blank panel. It is not tuned to any one link: what makes the surface
|
|
103
|
+
* honest is that it always answers and always says what it left out, not that the bound is
|
|
104
|
+
* optimal. */
|
|
105
|
+
export declare const AGGREGATION_DEADLINE_MS = 8000;
|
|
106
|
+
/** How many per-source reads are in flight at once.
|
|
107
|
+
*
|
|
108
|
+
* WHY NOT ALL OF THEM. Every source shares ONE connection to ONE broker, so past the point where
|
|
109
|
+
* the link is saturated extra concurrency buys no throughput: it spreads the same bytes over more
|
|
110
|
+
* unfinished reads, and a read that is 90% done when the deadline fires contributes nothing.
|
|
111
|
+
*
|
|
112
|
+
* THE NUMBER IS MEASURED, NOT PREFERRED, and the measurement includes what it costs. Corpus of 40
|
|
113
|
+
* channels, 12000 chat messages and 2000 DMs at 160ms RTT, sources answered inside the 8000ms
|
|
114
|
+
* deadline, three strategies, each arm on an idle link, one source per channel plus the DMs:
|
|
115
|
+
*
|
|
116
|
+
* link fan out all 41 pool of 8 pool of 1 widening on each completion
|
|
117
|
+
* 1024 KiB/s 1 16 3
|
|
118
|
+
* 512 KiB/s 1 8 3
|
|
119
|
+
* 256 KiB/s 1 0 3
|
|
120
|
+
* 128 KiB/s 1 0 1
|
|
121
|
+
*
|
|
122
|
+
* The fan-out is the worst column at every speed: reading the whole set at once leaves the panel
|
|
123
|
+
* empty rather than short. A pool that starts at one and widens on each completed read was built and
|
|
124
|
+
* measured too, on the reasoning that it would adapt to a link it cannot know; it does not pay,
|
|
125
|
+
* because at a healthy link a single source is round-trip bound rather than throughput bound, so the
|
|
126
|
+
* first completion arrives too late to be useful evidence and the ramp costs more than the
|
|
127
|
+
* adaptation returns.
|
|
128
|
+
*
|
|
129
|
+
* WITH TWO SOURCES IT DOES NOT BIND. The aggregation reads chat and DMs, so the pool never fills
|
|
130
|
+
* and this number chooses nothing for that source list. The bound is on the SHAPE (a cursor the
|
|
131
|
+
* workers pull from) rather than on a source count, so a third or a tenth source is pooled at this
|
|
132
|
+
* width instead of fanned out, the worst column above. */
|
|
133
|
+
export declare const AGGREGATION_CONCURRENCY = 8;
|
|
134
|
+
/** The sentinel a source resolves to when the deadline beat it. */
|
|
135
|
+
export declare const LATE: unique symbol;
|
|
136
|
+
/** A promise that resolves at `ms`, plus the handle to cancel its timer. `unref` alone is not
|
|
137
|
+
* enough: an 8-second timer in a long-lived server would hold a poll's worth of state per request. */
|
|
138
|
+
export declare function deadline(ms: number): {
|
|
139
|
+
until: Promise<typeof LATE>;
|
|
140
|
+
signal: AbortSignal;
|
|
141
|
+
done(): void;
|
|
142
|
+
};
|
|
143
|
+
/** Race one source against the request's deadline. History sources receive the deadline signal, so
|
|
144
|
+
* a late JetStream read stops its pull iterator and deletes its ephemeral consumer instead of keeping
|
|
145
|
+
* the shared link busy after this response has moved on. */
|
|
146
|
+
export declare function within<T>(p: Promise<T>, until: Promise<typeof LATE>): Promise<T | typeof LATE>;
|
|
147
|
+
/** What a feed write is. A `live` event is traffic the feed may fall behind on, so it is held to the
|
|
148
|
+
* ceiling. A `snapshot` is the state a feed is opened with (its hello, the roster, the presence view
|
|
149
|
+
* and the membership seed), written once on connect: a mesh whose roster alone is larger than the
|
|
150
|
+
* ceiling must still open, or every new feed would end at once and reconnect into the same end. */
|
|
151
|
+
export type FeedWrite = "live" | "snapshot";
|
|
152
|
+
/** The parts of a feed's response that `writeFeed` uses. `once` is the stream's own, used to learn
|
|
153
|
+
* when the opening state has drained; a response without it is warmed by the zero-backlog check. */
|
|
154
|
+
interface FeedResponse {
|
|
155
|
+
readonly writableEnded: boolean;
|
|
156
|
+
readonly writableLength: number;
|
|
157
|
+
write(chunk: string): unknown;
|
|
158
|
+
end(): unknown;
|
|
159
|
+
once?(event: "drain", listener: () => void): unknown;
|
|
160
|
+
}
|
|
161
|
+
/** Writes one SSE event to a feed. A feed that has ended takes nothing more.
|
|
162
|
+
*
|
|
163
|
+
* A live event is checked against the backlog the feed already holds, measured BEFORE the write:
|
|
164
|
+
* when more than `ceiling` bytes are still unsent, the feed is told so with one `overflow` event
|
|
165
|
+
* `{ reason }` and ended, and the event is not written. Measuring after the write would end a feed
|
|
166
|
+
* for one event larger than the ceiling on its own, such as a single long chat message, which would
|
|
167
|
+
* disconnect every dashboard at once. So one large event over an empty backlog is written, and the
|
|
168
|
+
* feed ends only when it is in fact behind.
|
|
169
|
+
*
|
|
170
|
+
* A snapshot is never checked. Its unsent bytes are not live traffic the feed fell behind on, so
|
|
171
|
+
* while they drain (the feed is cold) a live event is allowed them on top of the ceiling: on a slow
|
|
172
|
+
* link a roster over the ceiling would otherwise end the feed at its first live event, and every
|
|
173
|
+
* reconnect would end the same way. The allowance goes when the opening state has drained, so a
|
|
174
|
+
* cold feed still has a bound, its opening bytes plus the ceiling plus one event. */
|
|
175
|
+
export declare function writeFeed(res: FeedResponse, event: string, data: unknown, kind?: FeedWrite, ceiling?: number): void;
|
|
176
|
+
/** The state a feed opens with, in the order a browser needs it. */
|
|
177
|
+
interface FeedOpening {
|
|
178
|
+
/** The feed's id, which every watch request names. */
|
|
179
|
+
feed: string;
|
|
180
|
+
roster: unknown;
|
|
181
|
+
presenceView: unknown;
|
|
182
|
+
/** Reads the current membership snapshot. The live tap carries only post-connect traffic, and
|
|
183
|
+
* membership is state, so a fresh feed needs it read for it. */
|
|
184
|
+
membership(): Promise<unknown>;
|
|
185
|
+
}
|
|
186
|
+
/** Writes a new feed's opening state through `send`, every write a snapshot: `hello` first, so a
|
|
187
|
+
* browser can watch an agent before the roster arrives, then the roster and the presence view, then
|
|
188
|
+
* the membership seed once it is read, or its refusal when the read fails. Resolves when the seed
|
|
189
|
+
* is written. */
|
|
190
|
+
export declare function writeOpening(send: (event: string, data: unknown, kind: FeedWrite) => void, state: FeedOpening): Promise<void>;
|
|
191
|
+
/** Which feeds watch which agent. A feed is counted once per agent however many times it asks.
|
|
192
|
+
*
|
|
193
|
+
* This is the refcount behind "subscribe on demand": the tap already receives every frame, so an
|
|
194
|
+
* agent with no watcher costs no browser anything, and an agent leaves the map with its last
|
|
195
|
+
* watcher. Both directions are kept so `drop` touches only the agents its feed held, and so the
|
|
196
|
+
* per-feed ceiling is a lookup rather than a scan. */
|
|
197
|
+
export declare class SessionWatchers<F> {
|
|
198
|
+
private readonly perFeed;
|
|
199
|
+
private readonly byAgent;
|
|
200
|
+
private readonly byFeed;
|
|
201
|
+
constructor(perFeed?: number);
|
|
202
|
+
/** Throws a BadRequest when the feed already watches `perFeed` other agents. */
|
|
203
|
+
watch(agent: string, feed: F): void;
|
|
204
|
+
unwatch(agent: string, feed: F): void;
|
|
205
|
+
/** A feed closed: forget every watch it held. */
|
|
206
|
+
drop(feed: F): void;
|
|
207
|
+
targets(agent: string): readonly F[];
|
|
208
|
+
/** Agents with at least one watcher. An agent with none is absent, not zero. */
|
|
209
|
+
agents(): string[];
|
|
210
|
+
}
|
|
211
|
+
/** The events read ran past its deadline. Its own type, so the route can tell it from a refusal
|
|
212
|
+
* (an answer) and from any other failure (a 503 with the reason). */
|
|
213
|
+
export declare class AgentEventsLate extends Error {
|
|
214
|
+
}
|
|
215
|
+
/** A payload-only read failure leaves the independently read metadata available. */
|
|
216
|
+
export interface PayloadReadFailure {
|
|
217
|
+
status: "timeout" | "refused" | "failed";
|
|
218
|
+
reason: string;
|
|
219
|
+
}
|
|
220
|
+
/** One agent's newest frames within the deadline. Reads `limit + 1` from `events.<owner>.<actor>`
|
|
221
|
+
* with `channelHistory` and answers the newest `limit`, oldest first, with `more` when one more
|
|
222
|
+
* existed. A permission denial (`isPermissionDenied` from core) is an answer, `{ refused }` with the
|
|
223
|
+
* broker's own text and the grant command for this mesh. The deadline throws `AgentEventsLate`; any
|
|
224
|
+
* other failure rethrows.
|
|
225
|
+
*
|
|
226
|
+
* SENDER-CHECKED LIKE THE LIVE FEED. The read names the agent as `publisher`, so it filters on the
|
|
227
|
+
* agent's own subject, `chat.<owner>.<actor>.events.<owner>.<actor>`, and a frame whose subject
|
|
228
|
+
* names another sender is never read. That is the check `classifyTap` makes on a live frame, so a
|
|
229
|
+
* session's history and its live frames hold the same frames. Like that check, it is as strong as
|
|
230
|
+
* the mesh's publish permissions: on an open mesh any raw NATS client can publish on this subject.
|
|
231
|
+
*
|
|
232
|
+
* BOUNDED TWICE. The caller's `limit` is already capped at {@link MAX_HISTORY_LIMIT}, so one read
|
|
233
|
+
* holds at most one more message than that, and the read races the same deadline its siblings do,
|
|
234
|
+
* with the signal handed down so a late read stops its pull and drops its consumer.
|
|
235
|
+
*
|
|
236
|
+
* `channelHistory`, not the mediated `readHistory`: the mediated read caps a page at 200 and has
|
|
237
|
+
* no cursor, and this is the same credential the channel history route already reads with. */
|
|
238
|
+
export declare function agentEventsPage(source: {
|
|
239
|
+
eventPayloadHistory?(owner: string, actor: string, opts: {
|
|
240
|
+
limit: number;
|
|
241
|
+
signal: AbortSignal;
|
|
242
|
+
}): Promise<CotalMessage[]>;
|
|
243
|
+
channelHistory(channel: string, opts: {
|
|
244
|
+
limit: number;
|
|
245
|
+
signal: AbortSignal;
|
|
246
|
+
publisher: {
|
|
247
|
+
owner: string;
|
|
248
|
+
actor: string;
|
|
249
|
+
};
|
|
250
|
+
}): Promise<CotalMessage[]>;
|
|
251
|
+
}, agent: {
|
|
252
|
+
owner: string;
|
|
253
|
+
actor: string;
|
|
254
|
+
}, limit: number, grantFor: (channel: string, owner: string) => string | undefined, deadlineMs?: number): Promise<{
|
|
255
|
+
frames: CotalMessage[];
|
|
256
|
+
more: boolean;
|
|
257
|
+
payloadRead?: PayloadReadFailure;
|
|
258
|
+
} | {
|
|
259
|
+
refused: string;
|
|
260
|
+
grant?: string;
|
|
261
|
+
}>;
|
|
262
|
+
/** The command that lets a reader see one agent's event channel, as `docs/connect-claude.md` gives
|
|
263
|
+
* it for each kind of mesh, or nothing on an open mesh, where there is no grant to give. */
|
|
264
|
+
export declare function eventsGrantFor(mode: "user" | "static" | "open"): (channel: string, owner: string) => string | undefined;
|
|
265
|
+
/** One aggregated page, and what it is missing. `partial` and the counts are ALWAYS present, so a
|
|
266
|
+
* page that ran out of time cannot be mistaken for a complete one by omission. */
|
|
267
|
+
export interface ActivityPage {
|
|
268
|
+
entries: ({
|
|
269
|
+
mode: "chat";
|
|
270
|
+
channel: string;
|
|
271
|
+
msg: CotalMessage;
|
|
272
|
+
} | {
|
|
273
|
+
mode: "unicast";
|
|
274
|
+
msg: CotalMessage;
|
|
275
|
+
})[];
|
|
276
|
+
/** True iff at least one source did not answer within the deadline. */
|
|
277
|
+
partial: boolean;
|
|
278
|
+
/** Sources that answered, out of sources asked. There are two of them: all of chat in one stream
|
|
279
|
+
* read, and the DM backlog. */
|
|
280
|
+
read: number;
|
|
281
|
+
of: number;
|
|
282
|
+
/** Every source that did not answer, NAMED. A count alone tells a reader something is missing and
|
|
283
|
+
* not what, which on a dashboard is the difference between "the chat half of the feed is late"
|
|
284
|
+
* and "the space is empty". The names are those two sources rather than individual channels: one
|
|
285
|
+
* read either arrived or it did not, and {@link activityBackfill} says why at its source list. */
|
|
286
|
+
missing: string[];
|
|
287
|
+
/** Why each source in `missing` did not answer, keyed by its name. A read that ran out of time and
|
|
288
|
+
* a read that was refused (the client refusing a consumer create over `max_payload`, say) are
|
|
289
|
+
* different things for an operator to act on, and a name alone would make them the same bytes. */
|
|
290
|
+
reasons: Record<string, string>;
|
|
291
|
+
deadlineMs: number;
|
|
292
|
+
}
|
|
293
|
+
/** The all-activity backfill: recent chat history merged with DM history, oldest-first by `ts`,
|
|
294
|
+
* capped, and BOUNDED.
|
|
295
|
+
*
|
|
296
|
+
* Both sources, all of chat in one stream read and the DM backlog, race one shared deadline, so the
|
|
297
|
+
* caller never waits on the slower read past it and one source's rejection never discards the
|
|
298
|
+
* other's rows. Sources that answered are merged; sources that refused or ran late are NAMED in the
|
|
299
|
+
* page. The page is never a 500 and never silently short.
|
|
300
|
+
*
|
|
301
|
+
* Separate from the route so the filter above is reachable by a test that can see WHICH channels
|
|
302
|
+
* were asked for, which is the only evidence that separates filtering before the fetch from
|
|
303
|
+
* filtering after it. The route is a thin caller. */
|
|
304
|
+
export declare function activityBackfill(ep: ActivitySource, limit: number, deadlineMs?: number, concurrency?: number): Promise<ActivityPage>;
|
|
305
|
+
export {};
|
|
306
|
+
//# sourceMappingURL=web-read.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"web-read.d.ts","sourceRoot":"","sources":["../src/web-read.ts"],"names":[],"mappings":"AAIA,OAAO,EAIL,KAAK,YA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