@agimon-ai/po-session 0.0.0-alpha.13 → 0.0.0-alpha.131
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/LICENSE +149 -21
- package/README.md +32 -0
- package/dist/extensions/server.cjs +474 -114
- package/dist/extensions/server.cjs.map +1 -1
- package/dist/extensions/server.d.mts +2 -2
- package/dist/extensions/server.d.mts.map +1 -1
- package/dist/extensions/server.mjs +469 -109
- package/dist/extensions/server.mjs.map +1 -1
- package/dist/index.cjs +404 -25
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.mts +373 -6
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +354 -1
- package/dist/index.mjs.map +1 -0
- package/dist/{sessionDelivery-4svjzWUW.mjs → type-0ObaPMby.mjs} +853 -17
- package/dist/type-0ObaPMby.mjs.map +1 -0
- package/dist/{sessionDelivery-CGWuQyLV.cjs → type-Bzv_vxKC.cjs} +986 -12
- package/dist/type-Bzv_vxKC.cjs.map +1 -0
- package/llms.txt +3 -1
- package/package.json +17 -10
- package/src/prompts/codemode-how-to-session/SKILL.md +21 -0
- package/dist/sessionDelivery-4svjzWUW.mjs.map +0 -1
- package/dist/sessionDelivery-CGWuQyLV.cjs.map +0 -1
|
@@ -1,9 +1,73 @@
|
|
|
1
|
-
import { D as
|
|
1
|
+
import { A as parseSessionPeerEnvelope, B as sessionPeerConfigPath, C as createSessionGroupStore, D as createPeerReplayGuardAsync, E as createPeerCommunication, G as createStartSessionTool, L as authenticatedPeerPost, M as peerSessionReference, N as readSessionPeerConfigAsync, O as ensurePrivateDirectory, P as verifyPeerRequest, R as createSealedPeerChannels, T as createSessionDirectory, U as readSessionOperations, V as createSessionsNamespace, W as START_SESSION_TOOL_NAME, X as provideBackgroundWorkService, _ as readPoSessionDelivery, a as createSessionGroupBridge, b as PO_SESSION_INTERCOM_SERVICE, c as createPeerLink, d as createSessionPeers, h as provideSessionDeliveryService, i as PO_SESSION_GROUP_BRIDGE_SERVICE, j as peerRequestHeaders, k as parseRemoteSessionReference, l as PO_SESSION_PEERS_CONTRACT, n as PO_ROSTER_PEER_SERVICE, o as namedSessionGroupId, p as requireSessionPeers, r as PO_SESSION_GROUP_BRIDGE_CONTRACT, s as readSessionGroupBridge, u as PO_SESSION_PEERS_SERVICE, v as createSessionGroupDeliveryAuthorizer, y as createSessionIntercom, z as PEER_REPLAY_DATABASE_FILE } from "../type-0ObaPMby.mjs";
|
|
2
|
+
import { COORDINATOR_SESSION_PROVENANCE, PO_SESSION_ATTENTION_EVENT } from "@agimon-ai/po-core/hubChannel";
|
|
3
|
+
import { PO_CODEMODE_SERVICE, readPoCodemode } from "@agimon-ai/po-core/codemode";
|
|
2
4
|
import path from "node:path";
|
|
3
|
-
import { BACKGROUND_CONTEXT, requirePoFs, requirePoSqlite } from "@agimon-ai/po-core/platform";
|
|
5
|
+
import { BACKGROUND_CONTEXT, PO_FS_SERVICE, PO_SQLITE_SERVICE, requirePoFs, requirePoSqlite } from "@agimon-ai/po-core/platform";
|
|
4
6
|
import { poHomeDirectory } from "@agimon-ai/po-core/piSettings";
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
+
import { composeRootHooks, composeRootInjects, defineResource, defineRoot, defineRoute } from "@agimon-ai/po-core/extensionFile";
|
|
8
|
+
import { PO_SERVER_SCOPE_SERVICE, composeServerProviderOverrides, defineServerPlugin, packageResourcePath, readPackageResource, requirePoServerScope } from "@agimon-ai/po-core/serverPlugin";
|
|
9
|
+
import { PO_REMOTE_ORIGIN_SERVICE } from "@agimon-ai/po-core/remoteOrigin";
|
|
10
|
+
import { poApiCallerFrom } from "@agimon-ai/po-core/packageApi";
|
|
11
|
+
//#region src/extensions/(backend)/root.server.ts
|
|
12
|
+
var root_server_default$1 = defineRoot((plugin) => ({
|
|
13
|
+
value: void 0,
|
|
14
|
+
services: [async (context) => {
|
|
15
|
+
await context.inject([
|
|
16
|
+
PO_FS_SERVICE,
|
|
17
|
+
PO_SQLITE_SERVICE,
|
|
18
|
+
PO_SERVER_SCOPE_SERVICE
|
|
19
|
+
], async (scoped) => {
|
|
20
|
+
const homeDirectory = plugin.host.context.homeDirectory;
|
|
21
|
+
if (!homeDirectory) throw new Error("Host links require an explicit host home directory.");
|
|
22
|
+
const peers = await createSessionPeers({
|
|
23
|
+
fs: requirePoFs(scoped),
|
|
24
|
+
homeDirectory
|
|
25
|
+
});
|
|
26
|
+
await peers.identity();
|
|
27
|
+
const directory = path.join(poHomeDirectory(homeDirectory), "session");
|
|
28
|
+
await ensurePrivateDirectory(requirePoFs(scoped), directory, BACKGROUND_CONTEXT);
|
|
29
|
+
const database = await requirePoSqlite(scoped).open(path.join(directory, "groups.sqlite"), BACKGROUND_CONTEXT);
|
|
30
|
+
const groups = await createSessionGroupStore({ database });
|
|
31
|
+
await database.run("DELETE FROM session_group_members WHERE group_id IN (SELECT group_id FROM session_groups WHERE owner_authority = 'session-group-bridge')");
|
|
32
|
+
await database.run("DELETE FROM session_groups WHERE owner_authority = 'session-group-bridge'");
|
|
33
|
+
const bridge = createSessionGroupBridge({
|
|
34
|
+
groups,
|
|
35
|
+
peers,
|
|
36
|
+
onError: (error) => plugin.host.context.onNotice(`Group bridge failed: ${String(error)}`),
|
|
37
|
+
send: async (hostId, advert) => {
|
|
38
|
+
const config = peers.current();
|
|
39
|
+
const peer = config?.peers.find((entry) => entry.hostId === hostId);
|
|
40
|
+
if (!config || !peer) return;
|
|
41
|
+
await authenticatedPeerPost({
|
|
42
|
+
config,
|
|
43
|
+
peer,
|
|
44
|
+
path: "/api/plugins/session-peer/control",
|
|
45
|
+
value: {
|
|
46
|
+
type: "groups",
|
|
47
|
+
groups: advert
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
const scope = requirePoServerScope(scoped);
|
|
53
|
+
const releasePeers = plugin.host.scope === "global" ? await scope.provide(PO_SESSION_PEERS_CONTRACT, peers) : scoped.provide(PO_SESSION_PEERS_SERVICE, peers);
|
|
54
|
+
let releaseBridge;
|
|
55
|
+
try {
|
|
56
|
+
releaseBridge = plugin.host.scope === "global" ? await scope.provide(PO_SESSION_GROUP_BRIDGE_CONTRACT, bridge) : scoped.provide(PO_SESSION_GROUP_BRIDGE_SERVICE, bridge);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
await Promise.resolve(releasePeers());
|
|
59
|
+
await bridge.close();
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
return async () => {
|
|
63
|
+
await releaseBridge();
|
|
64
|
+
await Promise.resolve(releasePeers());
|
|
65
|
+
await bridge.close();
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
}]
|
|
69
|
+
}));
|
|
70
|
+
//#endregion
|
|
7
71
|
//#region src/services/peerInbox/index.ts
|
|
8
72
|
const inboxes = /* @__PURE__ */ new Map();
|
|
9
73
|
/**
|
|
@@ -22,93 +86,216 @@ async function deliverSessionPeerEnvelope(targetSessionId, sourceKey, type, payl
|
|
|
22
86
|
return inboxes.get(targetSessionId)?.(sourceKey, type, payload);
|
|
23
87
|
}
|
|
24
88
|
//#endregion
|
|
89
|
+
//#region src/services/sessionsNamespace/nodeOperations.ts
|
|
90
|
+
/** Reuses the same authorized peers and durable delivery as Team's peer messaging. */
|
|
91
|
+
function createNodeSessionOperations(host, context, sessionId) {
|
|
92
|
+
const peers = () => [...host.context.sessionCommunication?.listPeers?.() ?? [], ...readSessionGroupBridge(context)?.peersFor(sessionId) ?? []];
|
|
93
|
+
return {
|
|
94
|
+
async list(signal) {
|
|
95
|
+
signal.throwIfAborted();
|
|
96
|
+
return peers();
|
|
97
|
+
},
|
|
98
|
+
async send(to, text, signal) {
|
|
99
|
+
signal.throwIfAborted();
|
|
100
|
+
const current = peers();
|
|
101
|
+
const exact = current.find((peer) => peer.sessionId === to);
|
|
102
|
+
const matching = current.filter((peer) => peer.name === to);
|
|
103
|
+
if (!exact && matching.length > 1) throw new Error("Ambiguous session name. Use its session ID.");
|
|
104
|
+
const recipient = exact?.sessionId ?? matching[0]?.sessionId ?? to;
|
|
105
|
+
const remote = recipient.startsWith("peer/");
|
|
106
|
+
const metadata = remote ? readSessionGroupBridge(context)?.metadataFor(sessionId, recipient) : void 0;
|
|
107
|
+
if (remote ? !metadata : host.context.sessionService?.canCommunicate?.(sessionId, recipient) !== true) throw new Error("Session is outside your communication scope.");
|
|
108
|
+
const delivery = readPoSessionDelivery(context);
|
|
109
|
+
if (!delivery) throw new Error("Session delivery is unavailable.");
|
|
110
|
+
const sender = host.context.sessionCommunication?.sessionName?.trim() || sessionId;
|
|
111
|
+
return {
|
|
112
|
+
...await delivery.deliver({
|
|
113
|
+
deliveryId: `sessions:${sessionId}:${crypto.randomUUID()}`,
|
|
114
|
+
recipientKey: recipient,
|
|
115
|
+
kind: "session-message",
|
|
116
|
+
prompt: `Message from session ${sender}.\n\n${text}`,
|
|
117
|
+
delivery: "steer",
|
|
118
|
+
metadata
|
|
119
|
+
}),
|
|
120
|
+
to: recipient,
|
|
121
|
+
sender
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
//#endregion
|
|
25
127
|
//#region src/extensions/workspaces/sessions/(backend)/root.server.ts
|
|
26
128
|
var root_server_default = defineRoot((pluginContext) => ({
|
|
27
129
|
value: void 0,
|
|
28
|
-
services: [
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
};
|
|
50
|
-
const fallbackAuthorization = (peerKey) => sessionService.canCommunicate?.(agent.context.sessionId, peerKey) === true || remotePeerIsGranted(peerKey);
|
|
51
|
-
const groups = peerConfig ? await createSessionGroupStore({ database: await sqlite.open(path.join(sessionDirectory, "groups.sqlite"), io) }) : void 0;
|
|
52
|
-
const localHostId = peerConfig?.hostId;
|
|
53
|
-
const selfReference = localHostId ? peerSessionReference(localHostId, agent.context.sessionId) : void 0;
|
|
54
|
-
const groupAuthorization = groups && selfReference && localHostId ? createSessionGroupDeliveryAuthorizer({
|
|
55
|
-
selfReference,
|
|
56
|
-
groups,
|
|
57
|
-
peerReference: (peerKey) => parseRemoteSessionReference(peerKey) === void 0 ? peerSessionReference(localHostId, peerKey) : peerKey,
|
|
58
|
-
fallback: fallbackAuthorization
|
|
59
|
-
}) : void 0;
|
|
60
|
-
let delivery;
|
|
61
|
-
try {
|
|
62
|
-
delivery = await provideSessionDeliveryService(context, {
|
|
63
|
-
database: await sqlite.open(path.join(deliveryDirectory, `${agent.context.sessionId}.sqlite`), io),
|
|
64
|
-
recipientKey: agent.context.sessionId,
|
|
65
|
-
communication: peerCommunication,
|
|
66
|
-
authorizePeer: (peerKey, metadata) => groupAuthorization ? groupAuthorization(peerKey, metadata) : fallbackAuthorization(peerKey),
|
|
67
|
-
admitPrompt: (prompt, admission, requestId) => session.admitPrompt(prompt, admission, void 0, requestId)
|
|
130
|
+
services: [
|
|
131
|
+
provideBackgroundWorkService,
|
|
132
|
+
(owner) => {
|
|
133
|
+
const agent = pluginContext.agent;
|
|
134
|
+
if (!agent) return;
|
|
135
|
+
const options = {
|
|
136
|
+
sessionId: agent.context.sessionId,
|
|
137
|
+
provenance: () => pluginContext.host.context.sessionContext?.provenance,
|
|
138
|
+
coordination: () => pluginContext.host.context.coordination,
|
|
139
|
+
operations: readSessionOperations(owner) ?? createNodeSessionOperations(pluginContext.host, owner, agent.context.sessionId),
|
|
140
|
+
assertAvailable: () => pluginContext.signal.throwIfAborted()
|
|
141
|
+
};
|
|
142
|
+
const tool = agent.registerTool(createStartSessionTool(options));
|
|
143
|
+
const eligibility = options.provenance() === COORDINATOR_SESSION_PROVENANCE ? void 0 : agent.registerToolRestriction({ excludedTools: [START_SESSION_TOOL_NAME] });
|
|
144
|
+
const injection = owner.inject([PO_CODEMODE_SERVICE], (context) => {
|
|
145
|
+
const registration = readPoCodemode(context)?.register("@agimon-ai/po-session", createSessionsNamespace(options));
|
|
146
|
+
const restriction = registration ? agent.registerToolRestriction({ excludedTools: [START_SESSION_TOOL_NAME, ...options.operations.fallbackToolName ? [options.operations.fallbackToolName] : []] }) : void 0;
|
|
147
|
+
return () => {
|
|
148
|
+
restriction?.dispose();
|
|
149
|
+
registration?.dispose();
|
|
150
|
+
};
|
|
68
151
|
});
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
152
|
+
return async () => {
|
|
153
|
+
await injection.dispose();
|
|
154
|
+
eligibility?.dispose();
|
|
155
|
+
tool.dispose();
|
|
156
|
+
};
|
|
157
|
+
},
|
|
158
|
+
(owner) => {
|
|
159
|
+
owner.inject([
|
|
160
|
+
PO_SESSION_PEERS_SERVICE,
|
|
161
|
+
PO_SESSION_GROUP_BRIDGE_SERVICE,
|
|
162
|
+
PO_FS_SERVICE,
|
|
163
|
+
PO_SQLITE_SERVICE
|
|
164
|
+
], async (context) => {
|
|
165
|
+
const agent = pluginContext.agent;
|
|
166
|
+
const session = agent?.context.session;
|
|
167
|
+
const communication = pluginContext.host.context.sessionCommunication;
|
|
168
|
+
const homeDirectory = pluginContext.host.context.homeDirectory;
|
|
169
|
+
const sessionService = pluginContext.host.context.sessionService;
|
|
170
|
+
if (!agent || !session?.admitPrompt || !communication || !homeDirectory || !sessionService?.canCommunicate) throw new Error("Session delivery requires the headless agent, authenticated session communication, host home directory, and communication policy.");
|
|
171
|
+
const fs = requirePoFs(context);
|
|
172
|
+
const sqlite = requirePoSqlite(context);
|
|
173
|
+
const io = BACKGROUND_CONTEXT;
|
|
174
|
+
const sessionDirectory = path.join(poHomeDirectory(homeDirectory), "session");
|
|
175
|
+
const deliveryDirectory = path.join(sessionDirectory, "delivery");
|
|
176
|
+
await ensurePrivateDirectory(fs, deliveryDirectory, io);
|
|
177
|
+
const peers = requireSessionPeers(context);
|
|
178
|
+
const peerConfig = peers.current();
|
|
179
|
+
const peerCommunication = createPeerCommunication({
|
|
180
|
+
local: communication,
|
|
181
|
+
config: () => peers.current(),
|
|
182
|
+
canPublish: (target, type, payload) => {
|
|
183
|
+
if (type === "po/session-delivery/ack") return true;
|
|
184
|
+
const metadata = payload?.metadata;
|
|
185
|
+
return bridge?.peersFor(agent.context.sessionId).some((peer) => peer.sessionId === target && peer.sharedGroups.some((name) => namedSessionGroupId(name) === metadata?.groupId)) === true;
|
|
186
|
+
},
|
|
187
|
+
onError: (error) => pluginContext.host.context.onNotice(`Peer delivery failed: ${String(error)}`)
|
|
188
|
+
});
|
|
189
|
+
const remotePeerIsGranted = (peerKey) => {
|
|
190
|
+
const peer = parseRemoteSessionReference(peerKey);
|
|
191
|
+
return peer !== void 0 && peers.current()?.peers.some((candidate) => candidate.hostId === peer.hostId && candidate.allowedSessionIds.includes(agent.context.sessionId)) === true;
|
|
192
|
+
};
|
|
193
|
+
const fallbackAuthorization = (peerKey) => sessionService.canCommunicate?.(agent.context.sessionId, peerKey) === true || remotePeerIsGranted(peerKey);
|
|
194
|
+
const bridge = readSessionGroupBridge(context);
|
|
195
|
+
const groups = bridge?.groups;
|
|
196
|
+
await bridge?.updateLocal(agent.context.sessionId, communication.sessionName ?? agent.context.sessionId, communication.groups ?? []);
|
|
197
|
+
const localHostId = peerConfig?.hostId;
|
|
198
|
+
const selfReference = localHostId ? peerSessionReference(localHostId, agent.context.sessionId) : void 0;
|
|
199
|
+
const groupAuthorization = groups && selfReference && localHostId ? createSessionGroupDeliveryAuthorizer({
|
|
200
|
+
selfReference,
|
|
201
|
+
groups,
|
|
202
|
+
peerReference: (peerKey) => parseRemoteSessionReference(peerKey) === void 0 ? peerSessionReference(localHostId, peerKey) : peerKey,
|
|
203
|
+
fallback: fallbackAuthorization
|
|
204
|
+
}) : void 0;
|
|
205
|
+
let delivery;
|
|
206
|
+
try {
|
|
207
|
+
delivery = await provideSessionDeliveryService(context, {
|
|
208
|
+
database: await sqlite.open(path.join(deliveryDirectory, `${agent.context.sessionId}.sqlite`), io),
|
|
209
|
+
recipientKey: agent.context.sessionId,
|
|
210
|
+
communication: peerCommunication,
|
|
211
|
+
authorizePeer: (peerKey, metadata) => {
|
|
212
|
+
const remote = parseRemoteSessionReference(peerKey);
|
|
213
|
+
if (remote && !peers.current()?.peers.some((peer) => peer.hostId === remote.hostId)) return false;
|
|
214
|
+
if (remote) {
|
|
215
|
+
if (!bridge?.metadataFor(agent.context.sessionId, peerKey) || !metadata?.groupId || !bridge?.peersFor(agent.context.sessionId).find((peer) => peer.sessionId === peerKey)?.sharedGroups.some((name) => namedSessionGroupId(name) === metadata.groupId)) return false;
|
|
216
|
+
return groupAuthorization ? groupAuthorization(peerKey, metadata) : false;
|
|
217
|
+
}
|
|
218
|
+
return groupAuthorization ? groupAuthorization(peerKey, metadata) : fallbackAuthorization(peerKey);
|
|
219
|
+
},
|
|
220
|
+
admitPrompt: async (prompt, admission, requestId, metadata) => {
|
|
221
|
+
if (!metadata.customType) {
|
|
222
|
+
await session.admitPrompt(prompt, admission, void 0, requestId);
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (!session.admitMessage) throw new Error("The session cannot admit a custom delivery.");
|
|
226
|
+
await session.admitMessage({
|
|
227
|
+
type: metadata.customType,
|
|
228
|
+
text: prompt,
|
|
229
|
+
display: true,
|
|
230
|
+
details: metadata.customDetails === void 0 ? void 0 : JSON.parse(metadata.customDetails)
|
|
231
|
+
}, admission === "steer" ? "steer" : "followUp", requestId);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
} catch (error) {
|
|
235
|
+
await bridge?.removeLocal(agent.context.sessionId);
|
|
236
|
+
throw error;
|
|
95
237
|
}
|
|
238
|
+
const { service, close: closeDelivery } = delivery;
|
|
239
|
+
const unprovide = [];
|
|
240
|
+
if (groups && selfReference && peerConfig) {
|
|
241
|
+
const directory = createSessionDirectory({ authorizeDiscovery: (_caller, subject) => {
|
|
242
|
+
const reference = peerSessionReference(subject.hostId, subject.sessionId);
|
|
243
|
+
return bridge?.peersFor(agent.context.sessionId).some((peer) => peer.sessionId === reference) ? { capabilities: ["message"] } : void 0;
|
|
244
|
+
} });
|
|
245
|
+
const observedHosts = /* @__PURE__ */ new Set();
|
|
246
|
+
let sequence = 0;
|
|
247
|
+
const observeRemote = () => {
|
|
248
|
+
const remotePeers = bridge?.peersFor(agent.context.sessionId) ?? [];
|
|
249
|
+
for (const peer of remotePeers) observedHosts.add(parseRemoteSessionReference(peer.sessionId).hostId);
|
|
250
|
+
sequence += 1;
|
|
251
|
+
for (const hostId of observedHosts) directory.replaceHostSnapshot(hostId, "linked", remotePeers.flatMap((peer) => {
|
|
252
|
+
const remote = parseRemoteSessionReference(peer.sessionId);
|
|
253
|
+
return remote.hostId !== hostId ? [] : [{
|
|
254
|
+
hostId,
|
|
255
|
+
sessionId: remote.sessionId,
|
|
256
|
+
label: peer.name,
|
|
257
|
+
hostIncarnation: "linked",
|
|
258
|
+
sessionIncarnation: "live",
|
|
259
|
+
sequence,
|
|
260
|
+
observedAt: Date.now(),
|
|
261
|
+
staleAfterMs: 12e4,
|
|
262
|
+
deliveryTarget: peer.sessionId,
|
|
263
|
+
reachability: "unknown",
|
|
264
|
+
runtime: "unknown",
|
|
265
|
+
activity: "unknown",
|
|
266
|
+
voice: {
|
|
267
|
+
eligible: "unknown",
|
|
268
|
+
readiness: "unknown"
|
|
269
|
+
}
|
|
270
|
+
}];
|
|
271
|
+
}));
|
|
272
|
+
};
|
|
273
|
+
observeRemote();
|
|
274
|
+
unprovide.push(bridge.subscribe(() => {
|
|
275
|
+
observeRemote();
|
|
276
|
+
service.reauthorize?.().catch((error) => pluginContext.host.context.onNotice(`Delivery reauthorization failed: ${String(error)}`));
|
|
277
|
+
}));
|
|
278
|
+
unprovide.push(context.provide(PO_SESSION_INTERCOM_SERVICE, createSessionIntercom({
|
|
279
|
+
selfReference,
|
|
280
|
+
directory,
|
|
281
|
+
groups,
|
|
282
|
+
delivery: service
|
|
283
|
+
})));
|
|
284
|
+
}
|
|
285
|
+
const releaseGroups = pluginContext.host.context.directEvents?.subscribe(PO_SESSION_ATTENTION_EVENT, agent.context.sessionId, () => {
|
|
286
|
+
bridge?.updateLocal(agent.context.sessionId, communication.sessionName ?? agent.context.sessionId, communication.groups ?? []).catch((error) => pluginContext.host.context.onNotice(`Group update failed: ${String(error)}`));
|
|
287
|
+
});
|
|
288
|
+
const unregister = registerSessionPeerInbox(agent.context.sessionId, (sourceKey, kind, payload) => service.receive?.(sourceKey, kind, payload));
|
|
289
|
+
return async () => {
|
|
290
|
+
unregister();
|
|
291
|
+
releaseGroups?.();
|
|
292
|
+
await bridge?.removeLocal(agent.context.sessionId);
|
|
293
|
+
for (const release of unprovide.reverse()) release();
|
|
294
|
+
await closeDelivery();
|
|
295
|
+
};
|
|
96
296
|
});
|
|
97
|
-
unprovide.push(context.provide(PO_SESSION_INTERCOM_SERVICE, createSessionIntercom({
|
|
98
|
-
selfReference,
|
|
99
|
-
directory,
|
|
100
|
-
groups,
|
|
101
|
-
delivery: service
|
|
102
|
-
})));
|
|
103
297
|
}
|
|
104
|
-
|
|
105
|
-
return async () => {
|
|
106
|
-
unregister();
|
|
107
|
-
for (const release of unprovide.reverse()) release();
|
|
108
|
-
await closeDelivery();
|
|
109
|
-
await groups?.close();
|
|
110
|
-
};
|
|
111
|
-
}]
|
|
298
|
+
]
|
|
112
299
|
}));
|
|
113
300
|
//#endregion
|
|
114
301
|
//#region src/services/peerInboxApi/index.ts
|
|
@@ -124,31 +311,108 @@ function createPeerInboxApi(platform) {
|
|
|
124
311
|
start(context) {
|
|
125
312
|
const homeDirectory = context.homeDirectory;
|
|
126
313
|
let opened;
|
|
314
|
+
const channels = createSealedPeerChannels();
|
|
315
|
+
let link;
|
|
316
|
+
let announcing = false;
|
|
317
|
+
const announce = () => {
|
|
318
|
+
if (!link || announcing) return;
|
|
319
|
+
announcing = true;
|
|
320
|
+
link.announce().catch((error) => context.onNotice(`Peer URL announcement failed: ${String(error)}`)).finally(() => {
|
|
321
|
+
announcing = false;
|
|
322
|
+
});
|
|
323
|
+
};
|
|
324
|
+
const timer = platform.peers && platform.origin ? setInterval(() => {
|
|
325
|
+
open().then(announce).catch((error) => context.onNotice(`Host links could not start: ${String(error)}`));
|
|
326
|
+
}, 1e3) : void 0;
|
|
327
|
+
timer?.unref?.();
|
|
127
328
|
const open = () => opened ??= (async () => {
|
|
128
329
|
if (homeDirectory === void 0) return {
|
|
129
330
|
config: void 0,
|
|
130
331
|
replay: void 0
|
|
131
332
|
};
|
|
132
|
-
const config = await readSessionPeerConfigAsync(platform.fs, homeDirectory, BACKGROUND_CONTEXT);
|
|
333
|
+
const config = platform.peers ? platform.peers.current() : await readSessionPeerConfigAsync(platform.fs, homeDirectory, BACKGROUND_CONTEXT);
|
|
133
334
|
const file = path.join(path.dirname(sessionPeerConfigPath(homeDirectory)), PEER_REPLAY_DATABASE_FILE);
|
|
134
335
|
await ensurePrivateDirectory(platform.fs, path.dirname(file), BACKGROUND_CONTEXT);
|
|
336
|
+
const replay = await createPeerReplayGuardAsync(await platform.sqlite.open(file, BACKGROUND_CONTEXT));
|
|
337
|
+
if (platform.peers) link = createPeerLink({
|
|
338
|
+
peers: platform.peers,
|
|
339
|
+
network: platform.network,
|
|
340
|
+
groups: platform.groups,
|
|
341
|
+
replay,
|
|
342
|
+
channels,
|
|
343
|
+
origin: platform.origin ?? (() => void 0),
|
|
344
|
+
fetch: platform.fetch
|
|
345
|
+
});
|
|
346
|
+
announce();
|
|
135
347
|
return {
|
|
136
348
|
config,
|
|
137
|
-
replay
|
|
349
|
+
replay
|
|
138
350
|
};
|
|
139
351
|
})().catch((error) => {
|
|
140
352
|
opened = void 0;
|
|
141
353
|
throw error;
|
|
142
354
|
});
|
|
355
|
+
if (platform.peers) open().catch((error) => context.onNotice(`Host links could not start: ${String(error)}`));
|
|
143
356
|
return {
|
|
144
357
|
async fetch(request) {
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
358
|
+
const pathname = new URL(request.url).pathname;
|
|
359
|
+
const openedState = await open();
|
|
360
|
+
const config = platform.peers ? platform.peers.current() : openedState.config;
|
|
361
|
+
const replay = openedState.replay;
|
|
362
|
+
if (request.method === "GET" && pathname === "/links" && platform.peers) {
|
|
363
|
+
if (!poApiCallerFrom(request.headers)) return response(403, "An authenticated caller is required.");
|
|
364
|
+
return Response.json({
|
|
365
|
+
hostId: config?.hostId,
|
|
366
|
+
peers: config?.peers.map(({ hostId, url, urlGeneration }) => ({
|
|
367
|
+
hostId,
|
|
368
|
+
url,
|
|
369
|
+
urlGeneration
|
|
370
|
+
})) ?? []
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
if (request.method !== "POST" || ![
|
|
374
|
+
"/inbox",
|
|
375
|
+
"/control",
|
|
376
|
+
"/link/prepare",
|
|
377
|
+
"/link/accept",
|
|
378
|
+
"/link/unlink",
|
|
379
|
+
"/link/hint"
|
|
380
|
+
].includes(pathname)) return response(404, "Not found.");
|
|
148
381
|
const length = request.headers.get("content-length");
|
|
149
382
|
if (length !== null && (!/^\d+$/u.test(length) || Number(length) > MAX_BODY_BYTES)) return response(413, "Request body is too large.");
|
|
150
383
|
const body = await request.text();
|
|
151
384
|
if (Buffer.byteLength(body) > MAX_BODY_BYTES) return response(413, "Request body is too large.");
|
|
385
|
+
if (pathname.startsWith("/link/")) {
|
|
386
|
+
const caller = poApiCallerFrom(request.headers);
|
|
387
|
+
if (!caller || caller.locality === "remote" && pathname !== "/link/hint" && caller.stepUp !== "verified") return response(403, "Linking hosts needs a fresh peer.link step-up.");
|
|
388
|
+
if (!link) return response(503, "Host links are unavailable.");
|
|
389
|
+
try {
|
|
390
|
+
const input = body === "" ? {} : JSON.parse(body);
|
|
391
|
+
if (pathname === "/link/prepare") return Response.json(await link.prepare());
|
|
392
|
+
if (pathname === "/link/accept") {
|
|
393
|
+
await link.accept(input.invite);
|
|
394
|
+
return Response.json({ linked: true });
|
|
395
|
+
}
|
|
396
|
+
if (typeof input.hostId !== "string") return response(400, "A host identity is required.");
|
|
397
|
+
if (pathname === "/link/unlink") {
|
|
398
|
+
await link.unlink(input.hostId);
|
|
399
|
+
return Response.json({ unlinked: true });
|
|
400
|
+
}
|
|
401
|
+
if (typeof input.url !== "string") return response(400, "A URL hint is required.");
|
|
402
|
+
return Response.json({ rebound: await link.hint(input.hostId, input.url) });
|
|
403
|
+
} catch (error) {
|
|
404
|
+
return response(400, error instanceof Error ? error.message : "Host link failed.");
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
if (pathname === "/control") {
|
|
408
|
+
if (!link) return response(503, "Host links are unavailable.");
|
|
409
|
+
try {
|
|
410
|
+
return await link.control(request, body);
|
|
411
|
+
} catch (error) {
|
|
412
|
+
return response(400, error instanceof Error ? error.message : "Invalid peer control.");
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
if (!config) return response(503, "Paired Session delivery is not configured.");
|
|
152
416
|
const peer = verifyPeerRequest(config, request, body);
|
|
153
417
|
if (!peer) return response(401, "Paired Session request is unauthorized.");
|
|
154
418
|
if (!await replay?.admit(request)) return response(409, "Paired Session request was already received.");
|
|
@@ -158,17 +422,41 @@ function createPeerInboxApi(platform) {
|
|
|
158
422
|
} catch {
|
|
159
423
|
return response(400, "Paired Session request is malformed.");
|
|
160
424
|
}
|
|
425
|
+
let peerChannel;
|
|
426
|
+
if (peer.linkGeneration) {
|
|
427
|
+
const sealed = parsed;
|
|
428
|
+
peerChannel = typeof sealed?.epochId === "string" ? channels.take(peer, sealed.epochId) : void 0;
|
|
429
|
+
const opened = peerChannel?.open(sealed?.envelope);
|
|
430
|
+
if (!opened?.ok) return response(401, "A fresh sealed peer channel is required.");
|
|
431
|
+
try {
|
|
432
|
+
parsed = JSON.parse(Buffer.from(opened.plaintext).toString("utf8"));
|
|
433
|
+
} catch {
|
|
434
|
+
return response(400, "Invalid sealed peer envelope.");
|
|
435
|
+
}
|
|
436
|
+
}
|
|
161
437
|
const envelope = parseSessionPeerEnvelope(parsed);
|
|
162
438
|
if (!envelope || !DELIVERY_TYPES.has(envelope.type)) return response(400, "Paired Session envelope is invalid.");
|
|
163
|
-
if (!peer.allowedSessionIds.includes(envelope.targetSessionId)) return response(403, "Peer is not granted this session.");
|
|
439
|
+
if (!peer.linkGeneration && !peer.allowedSessionIds.includes(envelope.targetSessionId)) return response(403, "Peer is not granted this session.");
|
|
164
440
|
const state = await deliverSessionPeerEnvelope(envelope.targetSessionId, peerSessionReference(peer.hostId, envelope.sourceSessionId), envelope.type, envelope.payload);
|
|
441
|
+
if (state !== void 0 && peerChannel) {
|
|
442
|
+
const sealed = peerChannel.seal(Buffer.from(JSON.stringify({
|
|
443
|
+
accepted: true,
|
|
444
|
+
state
|
|
445
|
+
})));
|
|
446
|
+
if (!sealed.ok) return response(503, "Peer acknowledgement could not be sealed.");
|
|
447
|
+
const text = JSON.stringify(sealed.envelope);
|
|
448
|
+
return new Response(text, { headers: peerRequestHeaders(config.hostId, peer, "RESPONSE", "/inbox", text) });
|
|
449
|
+
}
|
|
165
450
|
return state === void 0 ? response(404, "Target Session is unavailable.") : Response.json({
|
|
166
451
|
accepted: true,
|
|
167
452
|
state
|
|
168
453
|
}, { status: 202 });
|
|
169
454
|
},
|
|
170
455
|
async close() {
|
|
171
|
-
|
|
456
|
+
if (timer) clearInterval(timer);
|
|
457
|
+
const state = await opened?.catch(() => void 0);
|
|
458
|
+
link?.close();
|
|
459
|
+
await state?.replay?.close();
|
|
172
460
|
}
|
|
173
461
|
};
|
|
174
462
|
}
|
|
@@ -176,37 +464,109 @@ function createPeerInboxApi(platform) {
|
|
|
176
464
|
}
|
|
177
465
|
//#endregion
|
|
178
466
|
//#region src/extensions/(backend)/api/session-peer/route.server.ts
|
|
179
|
-
var route_server_default = defineRoute(({ context }) =>
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
467
|
+
var route_server_default = defineRoute(({ context }) => {
|
|
468
|
+
let origin;
|
|
469
|
+
let network;
|
|
470
|
+
context.inject([PO_ROSTER_PEER_SERVICE], (scoped) => {
|
|
471
|
+
network = scoped.get(PO_ROSTER_PEER_SERVICE);
|
|
472
|
+
return () => {
|
|
473
|
+
network = void 0;
|
|
474
|
+
};
|
|
475
|
+
});
|
|
476
|
+
context.inject([PO_REMOTE_ORIGIN_SERVICE], (scoped) => {
|
|
477
|
+
origin = scoped.get(PO_REMOTE_ORIGIN_SERVICE);
|
|
478
|
+
return () => {
|
|
479
|
+
origin = void 0;
|
|
480
|
+
};
|
|
481
|
+
});
|
|
482
|
+
const hasOrigin = origin !== void 0;
|
|
483
|
+
return createPeerInboxApi({
|
|
484
|
+
fs: requirePoFs(context),
|
|
485
|
+
sqlite: requirePoSqlite(context),
|
|
486
|
+
peers: requireSessionPeers(context),
|
|
487
|
+
network,
|
|
488
|
+
groups: () => readSessionGroupBridge(context),
|
|
489
|
+
...hasOrigin ? { origin: () => origin?.current() } : {}
|
|
490
|
+
});
|
|
491
|
+
});
|
|
492
|
+
//#endregion
|
|
493
|
+
//#region src/extensions/workspaces/sessions/(backend)/resource/codemode-how-to-session.server.ts
|
|
494
|
+
var codemode_how_to_session_server_default = defineResource({
|
|
495
|
+
name: "codemode-how-to-session",
|
|
496
|
+
description: "List related sessions, send guidance, and start briefed independent work as a coordinator through the sessions namespace. No stop or delete capability.",
|
|
497
|
+
kind: "skill",
|
|
498
|
+
pinned: true,
|
|
499
|
+
path: packageResourcePath(import.meta.url, "src/prompts/codemode-how-to-session/SKILL.md"),
|
|
500
|
+
read: () => readPackageResource(import.meta.url, "src/prompts/codemode-how-to-session/SKILL.md")
|
|
501
|
+
});
|
|
183
502
|
//#endregion
|
|
184
503
|
//#region generated/server.ts
|
|
185
504
|
const at = (value, context) => typeof value === "function" ? value(context) : value;
|
|
186
|
-
|
|
505
|
+
/* v8 ignore next -- generated normalization branch */
|
|
506
|
+
const defined = (values) => values.filter((value) => value !== void 0);
|
|
507
|
+
const serverPlugin = defineServerPlugin({
|
|
187
508
|
name: "@agimon-ai/po-session",
|
|
188
|
-
providerOverrides: composeServerProviderOverrides(root_server_default),
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
509
|
+
providerOverrides: composeServerProviderOverrides(root_server_default$1, root_server_default),
|
|
510
|
+
injects: composeRootInjects({
|
|
511
|
+
global: [root_server_default$1],
|
|
512
|
+
workspace: [root_server_default$1],
|
|
513
|
+
session: [root_server_default$1, root_server_default]
|
|
514
|
+
}),
|
|
515
|
+
global: async (context) => {
|
|
516
|
+
const scopeGlobal = await context.scopeRoot("global", root_server_default$1, context);
|
|
517
|
+
const contextGlobal = {
|
|
518
|
+
...context,
|
|
519
|
+
root: scopeGlobal.value
|
|
520
|
+
};
|
|
521
|
+
return {
|
|
522
|
+
...composeRootHooks(scopeGlobal),
|
|
523
|
+
get api() {
|
|
524
|
+
return [at(route_server_default, contextGlobal)];
|
|
525
|
+
},
|
|
526
|
+
services: [...scopeGlobal.services ?? []]
|
|
527
|
+
};
|
|
528
|
+
},
|
|
529
|
+
workspace: async (context) => {
|
|
530
|
+
const scopeGlobal = await context.scopeRoot("global", root_server_default$1, context);
|
|
531
|
+
const contextGlobal = {
|
|
532
|
+
...context,
|
|
533
|
+
root: scopeGlobal.value
|
|
534
|
+
};
|
|
535
|
+
return {
|
|
536
|
+
...composeRootHooks(scopeGlobal),
|
|
537
|
+
get api() {
|
|
538
|
+
return defined([context.hosted("global") ? void 0 : at(route_server_default, contextGlobal)]);
|
|
539
|
+
},
|
|
540
|
+
services: [...scopeGlobal.services ?? []]
|
|
541
|
+
};
|
|
542
|
+
},
|
|
195
543
|
session: async (context) => {
|
|
196
|
-
const
|
|
544
|
+
const scopeGlobal = await context.scopeRoot("global", root_server_default$1, context);
|
|
545
|
+
const contextGlobal = {
|
|
546
|
+
...context,
|
|
547
|
+
root: scopeGlobal.value
|
|
548
|
+
};
|
|
549
|
+
const scopeSession = await context.scopeRoot("session", root_server_default, contextGlobal);
|
|
550
|
+
const contextSession = {
|
|
551
|
+
...contextGlobal,
|
|
552
|
+
root: scopeSession.value
|
|
553
|
+
};
|
|
197
554
|
return {
|
|
198
|
-
...composeRootHooks(scopeSession),
|
|
555
|
+
...composeRootHooks(scopeGlobal, scopeSession),
|
|
199
556
|
get api() {
|
|
200
|
-
return [at(route_server_default,
|
|
557
|
+
return defined([context.hosted("global") ? void 0 : at(route_server_default, contextGlobal)]);
|
|
558
|
+
},
|
|
559
|
+
get resources() {
|
|
560
|
+
return [at(codemode_how_to_session_server_default, contextSession)];
|
|
201
561
|
},
|
|
202
|
-
services: [...scopeSession.services ?? []],
|
|
562
|
+
services: [...scopeGlobal.services ?? [], ...scopeSession.services ?? []],
|
|
203
563
|
get activities() {
|
|
204
|
-
return [...scopeSession.activities ?? []];
|
|
564
|
+
return [...scopeGlobal.activities ?? [], ...scopeSession.activities ?? []];
|
|
205
565
|
}
|
|
206
566
|
};
|
|
207
567
|
}
|
|
208
568
|
});
|
|
209
569
|
//#endregion
|
|
210
|
-
export {
|
|
570
|
+
export { serverPlugin as default, serverPlugin };
|
|
211
571
|
|
|
212
572
|
//# sourceMappingURL=server.mjs.map
|