@fitchmultz/pi-subagents 0.47.0 → 0.48.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/CHANGELOG.md +15 -3
- package/dist/extension/build-info.js +1 -1
- package/dist/pi-intercom/bridge-broadcast.js +60 -0
- package/dist/pi-intercom/bridge-catalog.js +372 -0
- package/dist/pi-intercom/bridge-config.js +14 -2
- package/dist/pi-intercom/bridge-control.js +104 -0
- package/dist/pi-intercom/bridge-groups.js +135 -0
- package/dist/pi-intercom/bridge-http.js +8 -1
- package/dist/pi-intercom/bridge-launch-journal.js +84 -0
- package/dist/pi-intercom/bridge-launch-policy.js +71 -5
- package/dist/pi-intercom/bridge-launch.js +186 -76
- package/dist/pi-intercom/bridge-operations.js +127 -0
- package/dist/pi-intercom/bridge-protocol.js +7 -0
- package/dist/pi-intercom/bridge-routes.js +171 -22
- package/dist/pi-intercom/bridge-rpc-process.js +43 -15
- package/dist/pi-intercom/bridge-session.js +24 -22
- package/dist/pi-intercom/bridge-target.js +22 -0
- package/dist/pi-intercom/bridge.js +29 -37
- package/dist/pi-intercom/broker/client.js +1 -0
- package/dist/pi-intercom/connection.js +2 -3
- package/dist/pi-intercom/inbound.js +2 -4
- package/dist/pi-intercom/index.js +7 -1
- package/dist/pi-intercom/native-control-auth.js +80 -0
- package/dist/pi-intercom/native-control-protocol.js +111 -0
- package/dist/pi-intercom/native-control.js +130 -0
- package/dist/pi-intercom/saved-transcript.js +135 -0
- package/dist/pi-intercom/session-targets.js +3 -0
- package/dist/pi-intercom/transcript.js +175 -0
- package/docs/intercom-bridge.md +285 -175
- package/docs/intercom.md +1 -1
- package/docs/reference.md +1 -1
- package/package.json +1 -1
- package/scripts/intercom-remote.mjs +176 -35
- package/src/pi-intercom/bridge-broadcast.ts +70 -0
- package/src/pi-intercom/bridge-catalog.ts +489 -0
- package/src/pi-intercom/bridge-config.ts +19 -2
- package/src/pi-intercom/bridge-control.ts +179 -0
- package/src/pi-intercom/bridge-groups.ts +169 -0
- package/src/pi-intercom/bridge-http.ts +15 -1
- package/src/pi-intercom/bridge-launch-journal.ts +122 -0
- package/src/pi-intercom/bridge-launch-policy.ts +88 -7
- package/src/pi-intercom/bridge-launch.ts +296 -107
- package/src/pi-intercom/bridge-operations.ts +169 -0
- package/src/pi-intercom/bridge-protocol.ts +8 -0
- package/src/pi-intercom/bridge-routes.ts +254 -42
- package/src/pi-intercom/bridge-rpc-process.ts +53 -16
- package/src/pi-intercom/bridge-session.ts +35 -36
- package/src/pi-intercom/bridge-target.ts +35 -0
- package/src/pi-intercom/bridge.ts +30 -47
- package/src/pi-intercom/broker/client.ts +3 -0
- package/src/pi-intercom/connection.ts +2 -3
- package/src/pi-intercom/inbound.ts +2 -4
- package/src/pi-intercom/index.ts +7 -1
- package/src/pi-intercom/native-control-auth.ts +89 -0
- package/src/pi-intercom/native-control-protocol.ts +180 -0
- package/src/pi-intercom/native-control.ts +160 -0
- package/src/pi-intercom/saved-transcript.ts +175 -0
- package/src/pi-intercom/session-targets.ts +4 -0
- package/src/pi-intercom/transcript.ts +226 -0
- package/src/pi-intercom/types.ts +2 -0
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { isRecord, type UnknownRecord } from "../shared/unknown.ts";
|
|
3
|
+
import type { Identity } from "./bridge-config.ts";
|
|
4
|
+
import type { RequestContext } from "./bridge-http.ts";
|
|
5
|
+
import type { Operation } from "./bridge-routes.ts";
|
|
6
|
+
import type { Session } from "./bridge-session.ts";
|
|
7
|
+
import { Launcher } from "./bridge-launch.ts";
|
|
8
|
+
import { SessionCatalog, savedView, trustedJournal } from "./bridge-catalog.ts";
|
|
9
|
+
import { Groups } from "./bridge-groups.ts";
|
|
10
|
+
import { broadcastTargets } from "./bridge-broadcast.ts";
|
|
11
|
+
import { getPiAgentDir } from "./agent-dir.ts";
|
|
12
|
+
import { Fault } from "./bridge-protocol.ts";
|
|
13
|
+
import { resolveBridgeTarget } from "./bridge-target.ts";
|
|
14
|
+
import { readSavedTranscript } from "./saved-transcript.ts";
|
|
15
|
+
|
|
16
|
+
type OperationContext = RequestContext & {
|
|
17
|
+
readonly expires: number;
|
|
18
|
+
readonly certificate: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
/** Request operations own launch/catalog/group state; transport owns admission and cancellation. */
|
|
22
|
+
export class BridgeOperations {
|
|
23
|
+
private readonly catalog = new SessionCatalog();
|
|
24
|
+
private readonly launcher = new Launcher(this.catalog);
|
|
25
|
+
private readonly stateDir: string;
|
|
26
|
+
private groups?: Groups;
|
|
27
|
+
|
|
28
|
+
constructor(stateDir?: string) {
|
|
29
|
+
this.stateDir = stateDir ?? join(getPiAgentDir(), "intercom", "bridge-state");
|
|
30
|
+
}
|
|
31
|
+
revoke(clients: ReadonlyMap<string, Identity>): Promise<void> {
|
|
32
|
+
return this.launcher.revoke(clients);
|
|
33
|
+
}
|
|
34
|
+
shutdown(): Promise<void> {
|
|
35
|
+
return this.launcher.shutdown();
|
|
36
|
+
}
|
|
37
|
+
private groupStore(): Groups {
|
|
38
|
+
this.groups ??= new Groups(this.stateDir);
|
|
39
|
+
return this.groups;
|
|
40
|
+
}
|
|
41
|
+
async execute(
|
|
42
|
+
operation: Operation,
|
|
43
|
+
session: Readonly<Session>,
|
|
44
|
+
context: OperationContext,
|
|
45
|
+
): Promise<UnknownRecord> {
|
|
46
|
+
const launchContext = {
|
|
47
|
+
identity: session.identity,
|
|
48
|
+
expires: context.expires,
|
|
49
|
+
signal: context.signal,
|
|
50
|
+
peers: (signal: AbortSignal) => session.connectedPeers(signal),
|
|
51
|
+
submit: (peer: string, task: string) => session.send(peer, task, context),
|
|
52
|
+
};
|
|
53
|
+
switch (operation.route) {
|
|
54
|
+
case "POST /v1/start":
|
|
55
|
+
return this.launcher.start(operation.name, launchContext);
|
|
56
|
+
case "POST /v1/spawn":
|
|
57
|
+
return this.launcher.spawn(operation, launchContext);
|
|
58
|
+
case "POST /v1/resume":
|
|
59
|
+
return this.launcher.resume(operation, launchContext);
|
|
60
|
+
case "GET /v1/sessions":
|
|
61
|
+
return operation.query === undefined
|
|
62
|
+
? this.launcher.list(session.identity)
|
|
63
|
+
: this.launcher.browse(operation.query, launchContext);
|
|
64
|
+
case "POST /v1/stop":
|
|
65
|
+
case "POST /v1/cancel":
|
|
66
|
+
return this.stop(operation, session, context);
|
|
67
|
+
case "GET /v1/groups":
|
|
68
|
+
return { groups: this.groupStore().list(session.identity.fingerprint256) };
|
|
69
|
+
case "POST /v1/groups":
|
|
70
|
+
return {
|
|
71
|
+
groups: this.groupStore().upsert(session.identity.fingerprint256, operation.group),
|
|
72
|
+
};
|
|
73
|
+
case "POST /v1/broadcast":
|
|
74
|
+
return broadcastTargets(
|
|
75
|
+
session,
|
|
76
|
+
this.groupStore().targets(
|
|
77
|
+
session.identity.fingerprint256,
|
|
78
|
+
operation.group,
|
|
79
|
+
await session.connectedPeers(context.signal),
|
|
80
|
+
),
|
|
81
|
+
operation.message,
|
|
82
|
+
context,
|
|
83
|
+
);
|
|
84
|
+
case "POST /v1/control":
|
|
85
|
+
return session.nativeControl(operation, context.certificate, context);
|
|
86
|
+
case "GET /v1/transcript":
|
|
87
|
+
return this.transcript(operation, session, context);
|
|
88
|
+
case "GET /v1/inbox":
|
|
89
|
+
case "GET /v1/list":
|
|
90
|
+
case "POST /v1/ack":
|
|
91
|
+
case "POST /v1/ask":
|
|
92
|
+
case "POST /v1/register":
|
|
93
|
+
case "POST /v1/reply":
|
|
94
|
+
case "POST /v1/send":
|
|
95
|
+
return session.execute(operation, context);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
private async stop(
|
|
99
|
+
operation: Extract<Operation, { readonly route: "POST /v1/stop" | "POST /v1/cancel" }>,
|
|
100
|
+
session: Readonly<Session>,
|
|
101
|
+
context: OperationContext,
|
|
102
|
+
): Promise<UnknownRecord> {
|
|
103
|
+
const cancel = operation.route === "POST /v1/cancel";
|
|
104
|
+
const control = {
|
|
105
|
+
signal: context.signal,
|
|
106
|
+
peers: (signal: AbortSignal) => session.connectedPeers(signal),
|
|
107
|
+
};
|
|
108
|
+
try {
|
|
109
|
+
return cancel
|
|
110
|
+
? await this.launcher.cancel(session.identity, operation.sessionId, control)
|
|
111
|
+
: await this.launcher.stop(session.identity, operation.sessionId, control);
|
|
112
|
+
} catch (error) {
|
|
113
|
+
// Only a pre-effect ownership miss may dispatch elsewhere; never retry an unknown outcome.
|
|
114
|
+
if (!(error instanceof Fault) || error.code !== "unknown_session") {
|
|
115
|
+
throw error;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return session.nativeControl(
|
|
119
|
+
{ to: operation.sessionId, verb: cancel ? "cancel" : "stop", timeoutMs: operation.timeoutMs },
|
|
120
|
+
context.certificate,
|
|
121
|
+
context,
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
private async transcript(
|
|
125
|
+
operation: Extract<Operation, { readonly route: "GET /v1/transcript" }>,
|
|
126
|
+
session: Readonly<Session>,
|
|
127
|
+
context: OperationContext,
|
|
128
|
+
): Promise<UnknownRecord> {
|
|
129
|
+
const peers = await session.connectedPeers(context.signal);
|
|
130
|
+
try {
|
|
131
|
+
const peer = resolveBridgeTarget(peers, operation.target, null);
|
|
132
|
+
const result = await session.nativeControl(
|
|
133
|
+
{
|
|
134
|
+
to: peer.id,
|
|
135
|
+
verb: "transcript",
|
|
136
|
+
options: operation.options,
|
|
137
|
+
timeoutMs: operation.timeoutMs,
|
|
138
|
+
},
|
|
139
|
+
context.certificate,
|
|
140
|
+
context,
|
|
141
|
+
);
|
|
142
|
+
return {
|
|
143
|
+
...result,
|
|
144
|
+
source: "live",
|
|
145
|
+
transcript: isRecord(result.native) ? result.native.transcript : undefined,
|
|
146
|
+
};
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (!(error instanceof Fault) || error.code !== "peer_offline") {
|
|
149
|
+
throw error;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const policy = session.identity.launch;
|
|
153
|
+
if (!policy) {
|
|
154
|
+
throw new Fault(403, "launch_disabled", "Saved transcripts require local resume policy.");
|
|
155
|
+
}
|
|
156
|
+
const saved = await this.catalog.resolve(policy, { target: operation.target }, context.signal);
|
|
157
|
+
trustedJournal(policy, saved.sessionFile);
|
|
158
|
+
const page = await readSavedTranscript(
|
|
159
|
+
policy.packageRoot,
|
|
160
|
+
saved.sessionFile,
|
|
161
|
+
operation.options,
|
|
162
|
+
);
|
|
163
|
+
session.guard(context.signal);
|
|
164
|
+
if (page.sessionId !== saved.sessionId) {
|
|
165
|
+
throw new Fault(409, "session_changed", "Saved journal identity changed during reading.");
|
|
166
|
+
}
|
|
167
|
+
return { status: "ok", source: "saved", session: savedView(saved), transcript: page };
|
|
168
|
+
}
|
|
169
|
+
}
|
|
@@ -17,6 +17,14 @@ export class Fault extends Error {
|
|
|
17
17
|
}
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
+
export class BroadcastFault extends Fault {
|
|
21
|
+
readonly outcome: UnknownRecord;
|
|
22
|
+
constructor(fault: Fault, outcome: UnknownRecord) {
|
|
23
|
+
super(fault.status, fault.code, fault.message, fault.details);
|
|
24
|
+
this.outcome = { ...outcome, status: "partial" };
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
20
28
|
export function object(value: unknown, fields: readonly string[]): UnknownRecord {
|
|
21
29
|
if (!isRecord(value)) {
|
|
22
30
|
throw new Fault(400, "invalid_input", "Expected a JSON object.");
|
|
@@ -2,13 +2,14 @@ import type { IncomingMessage } from "node:http";
|
|
|
2
2
|
import { isUnknownArray } from "../shared/unknown.ts";
|
|
3
3
|
import { ASK_TIMEOUT, Fault, INBOX_COUNT, label, object, text } from "./bridge-protocol.ts";
|
|
4
4
|
import { body } from "./bridge-http.ts";
|
|
5
|
+
import type { CatalogQuery } from "./bridge-catalog.ts";
|
|
6
|
+
import { groupDefinition, type GroupDefinition } from "./bridge-groups.ts";
|
|
7
|
+
import { isControlVerb, type NativeControlVerb } from "./native-control-protocol.ts";
|
|
8
|
+
import { transcriptOptions, type TranscriptOptions } from "./transcript.ts";
|
|
5
9
|
|
|
6
|
-
export type
|
|
10
|
+
export type MessagingOperation =
|
|
7
11
|
| { readonly route: "GET /v1/list" | "POST /v1/register" }
|
|
8
12
|
| { readonly route: "GET /v1/inbox"; readonly after: number; readonly limit: number }
|
|
9
|
-
| { readonly route: "GET /v1/sessions" }
|
|
10
|
-
| { readonly route: "POST /v1/start"; readonly name: string }
|
|
11
|
-
| { readonly route: "POST /v1/stop"; readonly sessionId: string }
|
|
12
13
|
| { readonly route: "POST /v1/ack"; readonly ids: readonly string[] }
|
|
13
14
|
| { readonly route: "POST /v1/send"; readonly to: string; readonly message: string }
|
|
14
15
|
| {
|
|
@@ -18,12 +19,55 @@ export type Operation =
|
|
|
18
19
|
readonly timeoutMs: number;
|
|
19
20
|
}
|
|
20
21
|
| { readonly route: "POST /v1/reply"; readonly replyTo: string; readonly message: string };
|
|
21
|
-
export type
|
|
22
|
-
|
|
23
|
-
{ readonly route: "
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
22
|
+
export type Operation =
|
|
23
|
+
| MessagingOperation
|
|
24
|
+
| { readonly route: "GET /v1/sessions"; readonly query?: CatalogQuery }
|
|
25
|
+
| { readonly route: "POST /v1/start"; readonly name: string }
|
|
26
|
+
| {
|
|
27
|
+
readonly route: "POST /v1/spawn";
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly cwd?: string;
|
|
30
|
+
readonly provider?: string;
|
|
31
|
+
readonly model?: string;
|
|
32
|
+
readonly task?: string;
|
|
33
|
+
}
|
|
34
|
+
| {
|
|
35
|
+
readonly route: "POST /v1/resume";
|
|
36
|
+
readonly sessionId?: string;
|
|
37
|
+
readonly target?: string;
|
|
38
|
+
readonly name?: string;
|
|
39
|
+
readonly task?: string;
|
|
40
|
+
}
|
|
41
|
+
| {
|
|
42
|
+
readonly route: "POST /v1/stop" | "POST /v1/cancel";
|
|
43
|
+
readonly sessionId: string;
|
|
44
|
+
readonly timeoutMs: number;
|
|
45
|
+
}
|
|
46
|
+
| { readonly route: "GET /v1/groups" }
|
|
47
|
+
| { readonly route: "POST /v1/groups"; readonly group: GroupDefinition }
|
|
48
|
+
| { readonly route: "POST /v1/broadcast"; readonly message: string; readonly group?: string }
|
|
49
|
+
| {
|
|
50
|
+
readonly route: "POST /v1/control";
|
|
51
|
+
readonly to: string;
|
|
52
|
+
readonly verb: NativeControlVerb;
|
|
53
|
+
readonly options?: TranscriptOptions;
|
|
54
|
+
readonly timeoutMs: number;
|
|
55
|
+
}
|
|
56
|
+
| {
|
|
57
|
+
readonly route: "GET /v1/transcript";
|
|
58
|
+
readonly target: string;
|
|
59
|
+
readonly options: TranscriptOptions;
|
|
60
|
+
readonly timeoutMs: number;
|
|
61
|
+
};
|
|
62
|
+
type Route = Operation["route"] | "POST /v1/transcript" | "POST /v1/capabilities";
|
|
63
|
+
type MessagingRoute =
|
|
64
|
+
| MessagingOperation["route"]
|
|
65
|
+
| "GET /v1/groups"
|
|
66
|
+
| "GET /v1/sessions"
|
|
67
|
+
| "GET /v1/transcript"
|
|
68
|
+
| "POST /v1/control"
|
|
69
|
+
| "POST /v1/transcript"
|
|
70
|
+
| "POST /v1/capabilities";
|
|
27
71
|
const fields = {
|
|
28
72
|
"GET /v1/list": [],
|
|
29
73
|
"GET /v1/inbox": [],
|
|
@@ -33,10 +77,19 @@ const fields = {
|
|
|
33
77
|
"POST /v1/ask": ["to", "message", "timeoutMs"],
|
|
34
78
|
"POST /v1/reply": ["replyTo", "message"],
|
|
35
79
|
"POST /v1/start": ["name"],
|
|
80
|
+
"POST /v1/spawn": ["name", "cwd", "task", "provider", "model"],
|
|
81
|
+
"POST /v1/resume": ["sessionId", "name", "target", "task"],
|
|
36
82
|
"GET /v1/sessions": [],
|
|
37
|
-
"POST /v1/stop": ["sessionId"],
|
|
83
|
+
"POST /v1/stop": ["sessionId", "target", "to", "mode", "timeoutMs"],
|
|
84
|
+
"POST /v1/cancel": ["sessionId", "target", "to", "timeoutMs"],
|
|
85
|
+
"GET /v1/groups": [],
|
|
86
|
+
"POST /v1/groups": ["name", "members", "namePattern", "cwdPattern", "match"],
|
|
87
|
+
"POST /v1/broadcast": ["message", "group"],
|
|
88
|
+
"POST /v1/control": ["to", "verb", "options", "timeoutMs"],
|
|
89
|
+
"POST /v1/capabilities": ["to", "timeoutMs"],
|
|
90
|
+
"POST /v1/transcript": ["to", "limit", "maxBytes", "cursor", "timeoutMs"],
|
|
91
|
+
"GET /v1/transcript": ["target", "limit", "maxBytes", "cursor", "timeoutMs"],
|
|
38
92
|
};
|
|
39
|
-
|
|
40
93
|
function isRoute(route: string): route is Route {
|
|
41
94
|
return Object.hasOwn(fields, route);
|
|
42
95
|
}
|
|
@@ -51,13 +104,19 @@ export function routeFor(req: IncomingMessage): Route {
|
|
|
51
104
|
if (!isRoute(route)) {
|
|
52
105
|
throw new Fault(404, "not_found", "Unknown method or path.");
|
|
53
106
|
}
|
|
54
|
-
if (
|
|
55
|
-
|
|
107
|
+
if (
|
|
108
|
+
req.url?.includes("?") === true &&
|
|
109
|
+
!["GET /v1/inbox", "GET /v1/sessions", "GET /v1/transcript"].includes(route)
|
|
110
|
+
) {
|
|
111
|
+
throw new Fault(
|
|
112
|
+
400,
|
|
113
|
+
"invalid_input",
|
|
114
|
+
"Query parameters are only supported for inbox, sessions and transcript.",
|
|
115
|
+
);
|
|
56
116
|
}
|
|
57
117
|
checkGetBody(req);
|
|
58
118
|
return route;
|
|
59
119
|
}
|
|
60
|
-
|
|
61
120
|
function checkGetBody(req: IncomingMessage): void {
|
|
62
121
|
if (
|
|
63
122
|
req.method === "GET" &&
|
|
@@ -67,18 +126,15 @@ function checkGetBody(req: IncomingMessage): void {
|
|
|
67
126
|
throw new Fault(400, "invalid_input", "GET requests must not have a body.");
|
|
68
127
|
}
|
|
69
128
|
}
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
const query = new URLSearchParams(separator === -1 ? "" : url.slice(separator + 1));
|
|
75
|
-
if (
|
|
76
|
-
[...query.keys()].some(
|
|
77
|
-
(key) => !["after", "limit"].includes(key) || query.getAll(key).length !== 1,
|
|
78
|
-
)
|
|
79
|
-
) {
|
|
80
|
-
throw new Fault(400, "invalid_input", "Unknown or repeated inbox query parameter.");
|
|
129
|
+
function queryFor(req: IncomingMessage, allowed: readonly string[]): URLSearchParams {
|
|
130
|
+
const query = new URL(req.url ?? "", "https://127.0.0.1").searchParams;
|
|
131
|
+
if ([...query.keys()].some((key) => !allowed.includes(key) || query.getAll(key).length !== 1)) {
|
|
132
|
+
throw new Fault(400, "invalid_input", "Unknown or repeated query parameter.");
|
|
81
133
|
}
|
|
134
|
+
return query;
|
|
135
|
+
}
|
|
136
|
+
function inboxQuery(req: IncomingMessage): Extract<Operation, { readonly route: "GET /v1/inbox" }> {
|
|
137
|
+
const query = queryFor(req, ["after", "limit"]);
|
|
82
138
|
const after = query.get("after") ?? "0";
|
|
83
139
|
const limit = query.get("limit") ?? String(INBOX_COUNT);
|
|
84
140
|
if (
|
|
@@ -95,38 +151,142 @@ function inboxQuery(req: IncomingMessage): Extract<Operation, { readonly route:
|
|
|
95
151
|
}
|
|
96
152
|
return { route: "GET /v1/inbox", after: Number(after), limit: Number(limit) };
|
|
97
153
|
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
154
|
+
function pageNumber(value: string | null, fallback: number, min: number, max: number): number {
|
|
155
|
+
const number = value === null ? fallback : Number(value);
|
|
156
|
+
if (value === "" || !Number.isSafeInteger(number) || number < min || number > max) {
|
|
157
|
+
throw new Fault(400, "invalid_input", "Invalid catalog pagination.");
|
|
158
|
+
}
|
|
159
|
+
return number;
|
|
160
|
+
}
|
|
161
|
+
function queryBoolean(value: string | null): boolean {
|
|
162
|
+
if (value !== null && value !== "true" && value !== "false") {
|
|
163
|
+
throw new Fault(400, "invalid_input", "Catalog switches must be true or false.");
|
|
164
|
+
}
|
|
165
|
+
return value === "true";
|
|
166
|
+
}
|
|
167
|
+
function catalogQuery(req: IncomingMessage): CatalogQuery | undefined {
|
|
168
|
+
if (req.url?.includes("?") !== true) {
|
|
169
|
+
return undefined;
|
|
170
|
+
}
|
|
171
|
+
const params = queryFor(req, ["live", "saved", "q", "offset", "limit"]);
|
|
172
|
+
return {
|
|
173
|
+
live: queryBoolean(params.get("live")),
|
|
174
|
+
saved: queryBoolean(params.get("saved")),
|
|
175
|
+
q: params.has("q") ? label(params.get("q"), "catalog filter", 128) : undefined,
|
|
176
|
+
offset: pageNumber(params.get("offset"), 0, 0, Number.MAX_SAFE_INTEGER),
|
|
177
|
+
limit: pageNumber(params.get("limit"), 50, 1, 100),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
function timeout(value: unknown, control = false): number {
|
|
181
|
+
const defaultMs = control ? 3000 : ASK_TIMEOUT;
|
|
182
|
+
const ms = value === undefined ? defaultMs : value;
|
|
183
|
+
const max = control ? 10000 : ASK_TIMEOUT;
|
|
184
|
+
if (typeof ms !== "number" || !Number.isInteger(ms) || ms < 100 || ms > max) {
|
|
185
|
+
throw new Fault(400, "invalid_input", `timeoutMs must be an integer from 100 to ${max}.`);
|
|
103
186
|
}
|
|
104
187
|
return ms;
|
|
105
188
|
}
|
|
106
|
-
|
|
107
|
-
|
|
189
|
+
function resume(
|
|
190
|
+
input: Readonly<Record<string, unknown>>,
|
|
191
|
+
): Extract<Operation, { readonly route: "POST /v1/resume" }> {
|
|
192
|
+
if (["sessionId", "name", "target"].filter((key) => input[key] !== undefined).length !== 1) {
|
|
193
|
+
throw new Fault(
|
|
194
|
+
400,
|
|
195
|
+
"invalid_input",
|
|
196
|
+
"Resume requires exactly one of sessionId, name or target.",
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
const sessionId =
|
|
200
|
+
input.sessionId === undefined ? undefined : label(input.sessionId, "native session ID", 128);
|
|
201
|
+
if (sessionId !== undefined && !/^[a-zA-Z0-9][a-zA-Z0-9._-]*$/.test(sessionId)) {
|
|
202
|
+
throw new Fault(400, "invalid_input", "Resume takes a native ID, never a journal path.");
|
|
203
|
+
}
|
|
204
|
+
return {
|
|
205
|
+
route: "POST /v1/resume",
|
|
206
|
+
sessionId,
|
|
207
|
+
target:
|
|
208
|
+
input.target === undefined ? undefined : label(input.target, "resume target", 256).trim(),
|
|
209
|
+
name:
|
|
210
|
+
input.name === undefined ? undefined : label(input.name, "saved session name", 256).trim(),
|
|
211
|
+
task: input.task === undefined ? undefined : text(input.task),
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function stop(
|
|
215
|
+
route: "POST /v1/stop" | "POST /v1/cancel",
|
|
216
|
+
input: Readonly<Record<string, unknown>>,
|
|
217
|
+
): Extract<Operation, { readonly route: "POST /v1/stop" | "POST /v1/cancel" }> {
|
|
218
|
+
if (["sessionId", "target", "to"].filter((key) => input[key] !== undefined).length !== 1) {
|
|
219
|
+
throw new Fault(400, "invalid_input", "Control requires exactly one sessionId, target or to.");
|
|
220
|
+
}
|
|
221
|
+
const mode = input.mode ?? "kill";
|
|
222
|
+
if (mode !== "kill" && mode !== "cancel") {
|
|
223
|
+
throw new Fault(400, "invalid_input", "Stop mode must be cancel or kill.");
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
route: mode === "cancel" ? "POST /v1/cancel" : route,
|
|
227
|
+
sessionId: label(input.sessionId ?? input.target ?? input.to, "session target", 256).trim(),
|
|
228
|
+
timeoutMs: timeout(input.timeoutMs, true),
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function nativeControl(
|
|
232
|
+
route: Route,
|
|
233
|
+
input: Readonly<Record<string, unknown>>,
|
|
234
|
+
): Extract<Operation, { readonly route: "POST /v1/control" | "GET /v1/transcript" }> {
|
|
235
|
+
const verb = route === "POST /v1/control" ? input.verb : route.split("/").at(-1);
|
|
236
|
+
if (!isControlVerb(verb)) {
|
|
237
|
+
throw new Fault(400, "invalid_input", "Unknown native control verb.");
|
|
238
|
+
}
|
|
239
|
+
const timeoutMs = timeout(input.timeoutMs, true);
|
|
240
|
+
const to = label(input.to ?? input.target, "session target").trim();
|
|
241
|
+
if (verb === "transcript") {
|
|
242
|
+
const options = transcriptOptions(
|
|
243
|
+
route === "POST /v1/control"
|
|
244
|
+
? input.options
|
|
245
|
+
: { limit: input.limit, maxBytes: input.maxBytes, cursor: input.cursor },
|
|
246
|
+
);
|
|
247
|
+
return { route: "GET /v1/transcript", target: to, options, timeoutMs };
|
|
248
|
+
}
|
|
249
|
+
if (input.options !== undefined) {
|
|
250
|
+
throw new Fault(400, "invalid_input", "Only transcript accepts options.");
|
|
251
|
+
}
|
|
252
|
+
return { route: "POST /v1/control", verb, to, timeoutMs };
|
|
253
|
+
}
|
|
254
|
+
async function operationInput(
|
|
108
255
|
req: IncomingMessage,
|
|
109
256
|
route: Route,
|
|
110
257
|
signal: AbortSignal,
|
|
111
|
-
): Promise<
|
|
112
|
-
|
|
258
|
+
): Promise<Record<string, unknown>> {
|
|
259
|
+
if (req.method === "POST") {
|
|
260
|
+
return object(await body(req, signal), fields[route]);
|
|
261
|
+
}
|
|
262
|
+
if (route !== "GET /v1/transcript") {
|
|
263
|
+
return {};
|
|
264
|
+
}
|
|
265
|
+
const input: Record<string, unknown> = {};
|
|
266
|
+
for (const [key, value] of queryFor(req, fields[route])) {
|
|
267
|
+
input[key] = ["limit", "maxBytes", "timeoutMs"].includes(key) ? Number(value) : value;
|
|
268
|
+
}
|
|
269
|
+
return input;
|
|
270
|
+
}
|
|
271
|
+
function messagingOperation(
|
|
272
|
+
route: MessagingRoute,
|
|
273
|
+
input: Readonly<Record<string, unknown>>,
|
|
274
|
+
req: IncomingMessage,
|
|
275
|
+
): Operation {
|
|
113
276
|
switch (route) {
|
|
114
277
|
case "GET /v1/list":
|
|
115
278
|
case "POST /v1/register":
|
|
116
|
-
case "GET /v1/
|
|
279
|
+
case "GET /v1/groups":
|
|
117
280
|
return { route };
|
|
118
281
|
case "GET /v1/inbox":
|
|
119
282
|
return inboxQuery(req);
|
|
120
|
-
case "
|
|
121
|
-
return { route,
|
|
122
|
-
case "POST /v1/
|
|
123
|
-
return { route, sessionId: label(input.sessionId, "session ID", 128) };
|
|
124
|
-
case "POST /v1/ack": {
|
|
283
|
+
case "GET /v1/sessions":
|
|
284
|
+
return { route, query: catalogQuery(req) };
|
|
285
|
+
case "POST /v1/ack":
|
|
125
286
|
if (!isUnknownArray(input.ids) || input.ids.length > INBOX_COUNT) {
|
|
126
287
|
throw new Fault(400, "invalid_input", "ids must be an array of at most 256 message IDs.");
|
|
127
288
|
}
|
|
128
289
|
return { route, ids: [...new Set(input.ids.map((id) => label(id, "message ID")))] };
|
|
129
|
-
}
|
|
130
290
|
case "POST /v1/send":
|
|
131
291
|
return { route, message: text(input.message), to: label(input.to, "to").trim() };
|
|
132
292
|
case "POST /v1/ask":
|
|
@@ -138,5 +298,57 @@ export async function operationFor(
|
|
|
138
298
|
};
|
|
139
299
|
case "POST /v1/reply":
|
|
140
300
|
return { route, message: text(input.message), replyTo: label(input.replyTo, "replyTo") };
|
|
301
|
+
case "GET /v1/transcript":
|
|
302
|
+
case "POST /v1/control":
|
|
303
|
+
case "POST /v1/transcript":
|
|
304
|
+
case "POST /v1/capabilities":
|
|
305
|
+
return nativeControl(route, input);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
export async function operationFor(
|
|
309
|
+
req: IncomingMessage,
|
|
310
|
+
route: Route,
|
|
311
|
+
signal: AbortSignal,
|
|
312
|
+
): Promise<Operation> {
|
|
313
|
+
const input = await operationInput(req, route, signal);
|
|
314
|
+
switch (route) {
|
|
315
|
+
case "POST /v1/start":
|
|
316
|
+
return { route, name: label(input.name, "session name", 64).trim() };
|
|
317
|
+
case "POST /v1/spawn":
|
|
318
|
+
return {
|
|
319
|
+
route,
|
|
320
|
+
name: label(input.name, "session name", 64).trim(),
|
|
321
|
+
cwd: input.cwd === undefined ? undefined : label(input.cwd, "cwd", 4096),
|
|
322
|
+
provider: input.provider === undefined ? undefined : label(input.provider, "provider", 128),
|
|
323
|
+
model: input.model === undefined ? undefined : label(input.model, "model reference", 512),
|
|
324
|
+
task: input.task === undefined ? undefined : text(input.task),
|
|
325
|
+
};
|
|
326
|
+
case "POST /v1/resume":
|
|
327
|
+
return resume(input);
|
|
328
|
+
case "POST /v1/stop":
|
|
329
|
+
case "POST /v1/cancel":
|
|
330
|
+
return stop(route, input);
|
|
331
|
+
case "POST /v1/groups":
|
|
332
|
+
return { route, group: groupDefinition(input) };
|
|
333
|
+
case "POST /v1/broadcast":
|
|
334
|
+
return {
|
|
335
|
+
route,
|
|
336
|
+
message: text(input.message),
|
|
337
|
+
group: input.group === undefined ? undefined : label(input.group, "group", 64).trim(),
|
|
338
|
+
};
|
|
339
|
+
case "GET /v1/groups":
|
|
340
|
+
case "GET /v1/inbox":
|
|
341
|
+
case "GET /v1/list":
|
|
342
|
+
case "GET /v1/sessions":
|
|
343
|
+
case "GET /v1/transcript":
|
|
344
|
+
case "POST /v1/ack":
|
|
345
|
+
case "POST /v1/ask":
|
|
346
|
+
case "POST /v1/capabilities":
|
|
347
|
+
case "POST /v1/control":
|
|
348
|
+
case "POST /v1/register":
|
|
349
|
+
case "POST /v1/reply":
|
|
350
|
+
case "POST /v1/send":
|
|
351
|
+
case "POST /v1/transcript":
|
|
352
|
+
return messagingOperation(route, input, req);
|
|
141
353
|
}
|
|
142
354
|
}
|