@getpaseo/server 0.17.0 → 0.17.1
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/server/builtin-plugins/antigravity-provider/server/usage.ts +22 -12
- package/dist/server/builtin-plugins/package.json +1 -1
- package/dist/server/server/agent/agent-manager.d.ts +10 -1
- package/dist/server/server/agent/agent-manager.js +22 -5
- package/dist/server/server/agent/agent-messages/delivery.d.ts +43 -5
- package/dist/server/server/agent/agent-messages/delivery.js +386 -213
- package/dist/server/server/agent/agent-messages/envelope.d.ts +8 -0
- package/dist/server/server/agent/agent-messages/envelope.js +8 -1
- package/dist/server/server/agent/agent-prompt.d.ts +9 -2
- package/dist/server/server/agent/agent-prompt.js +21 -10
- package/dist/server/server/agent/host-operations/index.js +1 -1
- package/dist/server/server/agent/providers/codex-app-server-agent.js +5 -1
- package/dist/server/server/agent/tools/paseo-tools.js +6 -5
- package/dist/server/server/bootstrap.js +43 -26
- package/dist/server/server/session/checkout/checkout-session.d.ts +3 -1
- package/dist/server/server/session/checkout/checkout-session.js +2 -2
- package/dist/server/server/session.d.ts +3 -2
- package/dist/server/server/session.js +29 -22
- package/dist/server/server/websocket-server.d.ts +1 -0
- package/dist/server/server/websocket-server.js +14 -0
- package/dist/server/server/workspace-archive-operations.d.ts +4 -0
- package/dist/server/server/workspace-archive-operations.js +24 -0
- package/dist/server/server/workspace-directory.d.ts +1 -3
- package/dist/server/server/workspace-directory.js +1 -12
- package/dist/server/skills/paseo/SKILL.md +1 -1
- package/dist/server/web-ui/_expo/static/js/web/{index-9e9b3c5827a186678679c43439d3998a.js → index-12cbb5f1badfbe448300b289565efbeb.js} +42 -42
- package/dist/server/web-ui/_expo/static/js/web/index-12cbb5f1badfbe448300b289565efbeb.js.br +0 -0
- package/dist/server/web-ui/_expo/static/js/web/index-12cbb5f1badfbe448300b289565efbeb.js.gz +0 -0
- package/dist/server/web-ui/index.html +1 -1
- package/dist/server/web-ui/index.html.br +0 -0
- package/dist/server/web-ui/index.html.gz +0 -0
- package/package.json +6 -6
- package/dist/server/web-ui/_expo/static/js/web/index-9e9b3c5827a186678679c43439d3998a.js.br +0 -0
- package/dist/server/web-ui/_expo/static/js/web/index-9e9b3c5827a186678679c43439d3998a.js.gz +0 -0
|
@@ -1,71 +1,162 @@
|
|
|
1
|
-
import { AgentTurnBusyError } from "../agent-prompt.js";
|
|
1
|
+
import { AgentTurnBusyError, } from "../agent-prompt.js";
|
|
2
2
|
import { randomUUID } from "node:crypto";
|
|
3
|
-
import { prepareAgentMessage } from "./envelope.js";
|
|
4
|
-
/**
|
|
3
|
+
import { envelopeMessage, prepareAgentMessage } from "./envelope.js";
|
|
4
|
+
/** A message's quiet window closes this long after the latest arrival. */
|
|
5
|
+
const QUIET_MS = 2000;
|
|
6
|
+
/** Steady arrivals cannot keep a quiet window open longer than this. */
|
|
7
|
+
const MAX_COLLECTING_MS = 10000;
|
|
8
|
+
const isQueued = (message) => message.state === "queued";
|
|
9
|
+
const isWithdrawn = (message) => message.state === "withdrawn";
|
|
10
|
+
const isUrgent = (message) => isQueued(message) && message.urgent;
|
|
11
|
+
const isOfferable = (message) => isQueued(message) && !message.outOfBandDeclined && message.sessionMode === undefined;
|
|
12
|
+
/**
|
|
13
|
+
* Providers read a command from the raw start of the prompt: a slash and a name with no
|
|
14
|
+
* second slash in it. A path such as `/tmp/build.log` is plain text.
|
|
15
|
+
*/
|
|
16
|
+
function isCommand(prompt) {
|
|
17
|
+
const first = typeof prompt === "string" ? prompt : prompt[0];
|
|
18
|
+
if (typeof first !== "string" && first?.type !== "text")
|
|
19
|
+
return false;
|
|
20
|
+
const text = typeof first === "string" ? first : first.text;
|
|
21
|
+
return /^\/[^\s/]+(\s|$)/.test(text.trim());
|
|
22
|
+
}
|
|
23
|
+
function combine(prompts) {
|
|
24
|
+
return prompts.every((entry) => typeof entry === "string")
|
|
25
|
+
? prompts.join("\n\n")
|
|
26
|
+
: prompts.flatMap((entry) => typeof entry === "string" ? [{ type: "text", text: entry }] : entry);
|
|
27
|
+
}
|
|
28
|
+
function envelope(message) {
|
|
29
|
+
return prepareAgentMessage(message.prompt, message.source, message.receipt.messageId).prompt;
|
|
30
|
+
}
|
|
31
|
+
/** Explicitly conflicting session modes require separate, ordered dispatches. */
|
|
32
|
+
function modeGroups(messages) {
|
|
33
|
+
const groups = [];
|
|
34
|
+
let group = [];
|
|
35
|
+
let mode;
|
|
36
|
+
for (const message of messages) {
|
|
37
|
+
if (mode && message.sessionMode && mode !== message.sessionMode) {
|
|
38
|
+
groups.push(group);
|
|
39
|
+
group = [];
|
|
40
|
+
mode = undefined;
|
|
41
|
+
}
|
|
42
|
+
group.push(message);
|
|
43
|
+
mode ?? (mode = message.sessionMode);
|
|
44
|
+
}
|
|
45
|
+
if (group.length)
|
|
46
|
+
groups.push(group);
|
|
47
|
+
return groups;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Owns the order in which prompts reach a recipient. Delivery order equals admission order:
|
|
51
|
+
* only a prefix of the queue is ever dispatched, except a command a provider runs out of band.
|
|
52
|
+
*
|
|
53
|
+
* Every prompt for a recipient takes its place when it is admitted, and one operation at a
|
|
54
|
+
* time acts on the queue. A message stays in the queue until the provider admits the prompt
|
|
55
|
+
* that carries it, so agent messages, a person's prompts, and Stop all decide over the same list.
|
|
56
|
+
*/
|
|
5
57
|
export function createAgentMessageDelivery(dependencies) {
|
|
6
58
|
const schedule = dependencies.schedule ??
|
|
7
59
|
((delayMs, callback) => {
|
|
8
60
|
const timer = setTimeout(callback, delayMs);
|
|
9
61
|
return () => clearTimeout(timer);
|
|
10
62
|
});
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const admitted = new Map();
|
|
14
|
-
let sequence = 0;
|
|
63
|
+
const inboxes = new Map();
|
|
64
|
+
const releasing = new Map();
|
|
15
65
|
let closed = false;
|
|
16
66
|
let closing;
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
67
|
+
const isRunning = (recipientId) => Boolean(dependencies.isRunning?.(recipientId));
|
|
68
|
+
const isHeld = (recipientId) => inboxes.get(recipientId)?.phase === "held";
|
|
69
|
+
/** Tells whether a Stop has reached the recipient since this call. */
|
|
70
|
+
function watchForStop(inbox) {
|
|
71
|
+
const seen = inbox.stops;
|
|
72
|
+
return () => inbox.stops !== seen;
|
|
73
|
+
}
|
|
74
|
+
function requestFor(recipientId, messages, idleOnly, stopped) {
|
|
23
75
|
return {
|
|
24
76
|
recipientId,
|
|
25
|
-
idleOnly
|
|
77
|
+
idleOnly,
|
|
26
78
|
admit() {
|
|
27
|
-
if (messages.some(isWithdrawn))
|
|
79
|
+
if (messages.some(isWithdrawn) || stopped())
|
|
28
80
|
return false;
|
|
29
81
|
for (const message of messages)
|
|
30
82
|
message.state = "claimed";
|
|
31
83
|
return true;
|
|
32
84
|
},
|
|
33
85
|
messageIds: messages.map((message) => message.receipt.messageId),
|
|
34
|
-
prompt,
|
|
35
|
-
activeTurnBehavior: messages
|
|
36
|
-
|
|
86
|
+
prompt: combine(messages.map(envelope)),
|
|
87
|
+
activeTurnBehavior: messages.some((message) => message.urgent) ? "interrupt" : "steer",
|
|
88
|
+
// An interrupt may follow a message that asked for another mode; the latest choice wins.
|
|
89
|
+
sessionMode: messages.findLast((message) => message.sessionMode)?.sessionMode,
|
|
37
90
|
clearPendingPermissions: messages.some((message) => message.clearPendingPermissions),
|
|
38
91
|
unarchive: messages.some((message) => message.unarchive !== false),
|
|
39
92
|
};
|
|
40
93
|
}
|
|
41
|
-
function
|
|
42
|
-
|
|
43
|
-
|
|
94
|
+
function retire(recipientId, inbox) {
|
|
95
|
+
inbox.cancelTimer();
|
|
96
|
+
inbox.cancelCap();
|
|
97
|
+
if (inboxes.get(recipientId) === inbox)
|
|
98
|
+
inboxes.delete(recipientId);
|
|
44
99
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
100
|
+
/** The message reached its end: delivered, failed, or withdrawn. */
|
|
101
|
+
function settle(message, state) {
|
|
102
|
+
message.state = state;
|
|
103
|
+
const inbox = inboxes.get(message.recipientId);
|
|
104
|
+
const index = inbox?.messages.indexOf(message) ?? -1;
|
|
105
|
+
if (!inbox || index < 0)
|
|
106
|
+
return;
|
|
107
|
+
inbox.messages.splice(index, 1);
|
|
108
|
+
if (!inbox.messages.length)
|
|
109
|
+
retire(message.recipientId, inbox);
|
|
110
|
+
}
|
|
111
|
+
function claim(message, disposition, request) {
|
|
112
|
+
settle(message, "claimed");
|
|
113
|
+
try {
|
|
114
|
+
message.onDispatch?.(disposition);
|
|
52
115
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
entry.messages = entry.messages.filter((message) => !isWithdrawn(message));
|
|
56
|
-
if (!entry.messages.length) {
|
|
57
|
-
entry.cancel();
|
|
58
|
-
pending.delete(recipientId);
|
|
59
|
-
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
dependencies.onError(error, request);
|
|
60
118
|
}
|
|
61
|
-
|
|
119
|
+
message.resolve(disposition);
|
|
120
|
+
}
|
|
121
|
+
/** A dispatch that did not happen leaves its messages queued, each still in its place. */
|
|
122
|
+
function handBack(messages) {
|
|
123
|
+
for (const message of messages)
|
|
124
|
+
if (message.state === "claimed")
|
|
125
|
+
message.state = "queued";
|
|
62
126
|
}
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
|
|
127
|
+
function fail(message, error) {
|
|
128
|
+
settle(message, "rejected");
|
|
129
|
+
message.reject(error);
|
|
130
|
+
}
|
|
131
|
+
/** One operation per recipient at a time, whether the queue or a person started it. */
|
|
132
|
+
function run(recipientId, operation) {
|
|
133
|
+
const previous = releasing.get(recipientId) ?? Promise.resolve();
|
|
134
|
+
const tracked = previous
|
|
135
|
+
.then(operation)
|
|
136
|
+
.catch(() => undefined)
|
|
137
|
+
.then(() => rest(recipientId));
|
|
138
|
+
releasing.set(recipientId, tracked);
|
|
139
|
+
void tracked.then(() => {
|
|
140
|
+
if (releasing.get(recipientId) === tracked)
|
|
141
|
+
releasing.delete(recipientId);
|
|
142
|
+
return undefined;
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
/** After an operation, a waiting queue keeps polling for the turn to end. */
|
|
146
|
+
function rest(recipientId) {
|
|
147
|
+
const inbox = inboxes.get(recipientId);
|
|
148
|
+
if (!inbox || inbox.advancing || inbox.phase !== "waiting")
|
|
149
|
+
return;
|
|
150
|
+
inbox.cancelTimer();
|
|
151
|
+
inbox.cancelTimer = schedule(QUIET_MS, () => attempt(recipientId));
|
|
152
|
+
}
|
|
153
|
+
/** Dispatch one batch. A batch that finds the recipient stopped, or busy when idle-only, stays queued. */
|
|
154
|
+
async function deliver(recipientId, batch, idleOnly, stopped) {
|
|
155
|
+
for (;;) {
|
|
156
|
+
const messages = batch.filter(isQueued);
|
|
66
157
|
if (!messages.length)
|
|
67
158
|
return;
|
|
68
|
-
const request = requestFor(recipientId, messages);
|
|
159
|
+
const request = requestFor(recipientId, messages, idleOnly && !closed, stopped);
|
|
69
160
|
try {
|
|
70
161
|
for (const message of messages)
|
|
71
162
|
message.beforeDispatch?.();
|
|
@@ -79,173 +170,235 @@ export function createAgentMessageDelivery(dependencies) {
|
|
|
79
170
|
// Shutdown drains admitted messages before closing the recipients.
|
|
80
171
|
disposition = await dependencies.dispatch({ ...request, idleOnly: false });
|
|
81
172
|
}
|
|
82
|
-
for (const message of messages)
|
|
83
|
-
message
|
|
84
|
-
|
|
85
|
-
message.onDispatch?.(disposition);
|
|
86
|
-
}
|
|
87
|
-
catch (error) {
|
|
88
|
-
dependencies.onError(error, request);
|
|
89
|
-
}
|
|
90
|
-
message.resolve(disposition);
|
|
91
|
-
}
|
|
92
|
-
return undefined;
|
|
173
|
+
for (const message of messages)
|
|
174
|
+
claim(message, disposition, request);
|
|
175
|
+
return;
|
|
93
176
|
}
|
|
94
177
|
catch (error) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
flush(recipientId);
|
|
178
|
+
// A Stop reached the recipient before the provider admitted the prompt.
|
|
179
|
+
if (stopped()) {
|
|
180
|
+
handBack(messages);
|
|
99
181
|
return;
|
|
100
182
|
}
|
|
101
|
-
|
|
102
|
-
|
|
183
|
+
// A withdrawal invalidated this exact prompt; the rest goes again without it.
|
|
184
|
+
if (messages.some(isWithdrawn)) {
|
|
185
|
+
handBack(messages);
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (error instanceof AgentTurnBusyError && request.idleOnly) {
|
|
189
|
+
handBack(messages);
|
|
103
190
|
return;
|
|
104
191
|
}
|
|
105
192
|
for (const message of messages)
|
|
106
|
-
message
|
|
193
|
+
fail(message, error);
|
|
107
194
|
dependencies.onError(error, request);
|
|
108
195
|
throw error;
|
|
109
196
|
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
/** A provider may run a command beside the turn. Each message is offered once, alone. */
|
|
200
|
+
async function offerOutOfBand(recipientId, candidates, stopped, stillDue = () => true) {
|
|
201
|
+
const tryRunOutOfBand = dependencies.tryRunOutOfBand;
|
|
202
|
+
if (!tryRunOutOfBand)
|
|
203
|
+
return;
|
|
204
|
+
for (const message of candidates) {
|
|
205
|
+
if (closed || stopped() || !stillDue())
|
|
206
|
+
return;
|
|
207
|
+
if (!isOfferable(message))
|
|
208
|
+
continue;
|
|
209
|
+
const request = requestFor(recipientId, [message], true, stopped);
|
|
210
|
+
try {
|
|
211
|
+
const accepted = await tryRunOutOfBand(request, () => {
|
|
212
|
+
if (!request.admit())
|
|
213
|
+
throw new Error("Queued agent message was withdrawn");
|
|
214
|
+
message.beforeDispatch?.();
|
|
215
|
+
});
|
|
216
|
+
if (accepted)
|
|
217
|
+
claim(message, "out_of_band", request);
|
|
218
|
+
else
|
|
219
|
+
message.outOfBandDeclined = true;
|
|
220
|
+
}
|
|
221
|
+
catch (error) {
|
|
222
|
+
if (!isQueued(message) || stopped())
|
|
223
|
+
continue;
|
|
224
|
+
fail(message, error);
|
|
225
|
+
dependencies.onError(error, request);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Move the queue forward: an interrupt takes it now, a closed window once the turn has ended.
|
|
231
|
+
* `ahead` is what was admitted before this step got in line; it leaves later messages alone.
|
|
232
|
+
*/
|
|
233
|
+
async function advance(recipientId, inbox, ahead, stopped) {
|
|
234
|
+
const interrupts = ahead.filter(isUrgent);
|
|
235
|
+
if (interrupts.length) {
|
|
236
|
+
// A command the provider runs beside the turn replaces nothing, so it takes nothing along.
|
|
237
|
+
await offerOutOfBand(recipientId, interrupts, stopped);
|
|
238
|
+
const last = ahead.findLastIndex(isUrgent);
|
|
239
|
+
if (last >= 0) {
|
|
240
|
+
// An interrupt does not jump the queue: everything admitted before it goes first.
|
|
241
|
+
await deliver(recipientId, ahead.slice(0, last + 1), false, stopped);
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (inbox.phase !== "waiting")
|
|
246
|
+
return;
|
|
247
|
+
const running = () => inbox.phase === "waiting" && isRunning(recipientId);
|
|
248
|
+
if (running())
|
|
249
|
+
await offerOutOfBand(recipientId, ahead, stopped, running);
|
|
250
|
+
// Later mode groups stay queued for the turn ends that follow.
|
|
251
|
+
if (inbox.phase === "waiting" && !isRunning(recipientId))
|
|
252
|
+
await deliver(recipientId, modeGroups(ahead.filter(isQueued))[0] ?? [], true, stopped);
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Line up a queue step behind the recipient's current operation, for what is queued now.
|
|
256
|
+
* A Stop voids the steps reserved before it: what they covered waits for the next prompt.
|
|
257
|
+
*/
|
|
258
|
+
function lineUp(recipientId, inbox, onStart) {
|
|
259
|
+
const ahead = [...inbox.messages];
|
|
260
|
+
const stopped = watchForStop(inbox);
|
|
261
|
+
run(recipientId, async () => {
|
|
262
|
+
onStart?.();
|
|
263
|
+
if (!stopped())
|
|
264
|
+
await advance(recipientId, inbox, ahead, stopped);
|
|
110
265
|
});
|
|
111
266
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
})
|
|
121
|
-
.catch(() => undefined);
|
|
267
|
+
/** Polling lines up one step at a time; an interrupt always reserves its own place. */
|
|
268
|
+
function nudge(recipientId, inbox) {
|
|
269
|
+
if (inbox.advancing)
|
|
270
|
+
return;
|
|
271
|
+
inbox.advancing = true;
|
|
272
|
+
lineUp(recipientId, inbox, () => {
|
|
273
|
+
inbox.advancing = false;
|
|
274
|
+
});
|
|
122
275
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
276
|
+
/** The quiet window closed, a retry came due, or the recipient settled. */
|
|
277
|
+
function attempt(recipientId) {
|
|
278
|
+
const inbox = inboxes.get(recipientId);
|
|
279
|
+
if (!inbox || inbox.phase === "held")
|
|
126
280
|
return;
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
281
|
+
inbox.cancelTimer();
|
|
282
|
+
inbox.cancelCap();
|
|
283
|
+
inbox.phase = "waiting";
|
|
284
|
+
const running = isRunning(recipientId);
|
|
285
|
+
if (running)
|
|
286
|
+
inbox.cancelTimer = schedule(QUIET_MS, () => attempt(recipientId));
|
|
287
|
+
const nothingToOffer = !dependencies.tryRunOutOfBand || !inbox.messages.some(isOfferable);
|
|
288
|
+
if (running && nothingToOffer)
|
|
289
|
+
return;
|
|
290
|
+
nudge(recipientId, inbox);
|
|
291
|
+
}
|
|
292
|
+
function admit(item) {
|
|
293
|
+
const existing = inboxes.get(item.recipientId);
|
|
294
|
+
const inbox = existing ?? {
|
|
130
295
|
messages: [],
|
|
131
|
-
|
|
132
|
-
|
|
296
|
+
phase: "waiting",
|
|
297
|
+
cancelTimer: () => { },
|
|
298
|
+
cancelCap: () => { },
|
|
299
|
+
advancing: false,
|
|
300
|
+
stops: 0,
|
|
133
301
|
};
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
302
|
+
inboxes.set(item.recipientId, inbox);
|
|
303
|
+
inbox.messages.push(item);
|
|
304
|
+
inbox.cancelTimer();
|
|
305
|
+
if (item.urgent) {
|
|
306
|
+
// An interrupt does not wait: it closes the window and takes the queue along. Its place
|
|
307
|
+
// is reserved now, so a prompt admitted after it cannot take it away.
|
|
308
|
+
inbox.cancelCap();
|
|
309
|
+
inbox.phase = "waiting";
|
|
310
|
+
lineUp(item.recipientId, inbox);
|
|
143
311
|
return;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
if (
|
|
147
|
-
const interrupts = entry.messages.filter((message) => message.activeTurnBehavior === "interrupt");
|
|
148
|
-
entry.messages = entry.messages.filter((message) => message.activeTurnBehavior !== "interrupt");
|
|
149
|
-
if (interrupts.length)
|
|
150
|
-
dispatchGroups(recipientId, interrupts);
|
|
151
|
-
if (entry.messages.length && dependencies.tryRunOutOfBand) {
|
|
152
|
-
pending.delete(recipientId);
|
|
153
|
-
offerOutOfBand(recipientId, entry.messages);
|
|
154
|
-
}
|
|
155
|
-
else if (entry.messages.length) {
|
|
156
|
-
entry.cancel = schedule(2000, () => flush(recipientId));
|
|
157
|
-
}
|
|
158
|
-
else {
|
|
159
|
-
pending.delete(recipientId);
|
|
160
|
-
}
|
|
312
|
+
}
|
|
313
|
+
inbox.cancelTimer = schedule(QUIET_MS, () => attempt(item.recipientId));
|
|
314
|
+
if (inbox.phase === "collecting")
|
|
161
315
|
return;
|
|
316
|
+
// A new window. It closes after a quiet spell, and no later than the cap.
|
|
317
|
+
inbox.phase = "collecting";
|
|
318
|
+
inbox.cancelCap = schedule(MAX_COLLECTING_MS, () => attempt(item.recipientId));
|
|
319
|
+
}
|
|
320
|
+
function join(queued, direct) {
|
|
321
|
+
if (!queued.length) {
|
|
322
|
+
const own = prepareAgentMessage(direct.prompt, direct.source, direct.messageId);
|
|
323
|
+
return {
|
|
324
|
+
prompt: own.prompt,
|
|
325
|
+
messageId: own.messageId,
|
|
326
|
+
sessionMode: direct.sessionMode,
|
|
327
|
+
unarchive: direct.unarchive !== false,
|
|
328
|
+
};
|
|
162
329
|
}
|
|
163
|
-
|
|
164
|
-
|
|
330
|
+
// Sharing a prompt with agent messages, a person's text needs its own envelope to stay
|
|
331
|
+
// a separate row with its own identity.
|
|
332
|
+
const own = envelopeMessage(direct.prompt, direct.source ?? null, direct.messageId);
|
|
333
|
+
return {
|
|
334
|
+
prompt: combine([...queued.map(envelope), own.prompt]),
|
|
335
|
+
messageId: own.messageId,
|
|
336
|
+
sessionMode: direct.sessionMode ?? queued.findLast((message) => message.sessionMode)?.sessionMode,
|
|
337
|
+
unarchive: direct.unarchive !== false || queued.some((message) => message.unarchive !== false),
|
|
338
|
+
};
|
|
165
339
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
continue;
|
|
178
|
-
}
|
|
179
|
-
const request = requestFor(recipientId, [message]);
|
|
180
|
-
try {
|
|
181
|
-
const accepted = await dependencies.tryRunOutOfBand(request, () => {
|
|
182
|
-
if (!request.admit())
|
|
183
|
-
throw new Error("Queued agent message was withdrawn");
|
|
184
|
-
message.beforeDispatch?.();
|
|
185
|
-
});
|
|
186
|
-
if (!accepted) {
|
|
187
|
-
message.outOfBandDeclined = true;
|
|
188
|
-
waiting.push(message);
|
|
189
|
-
continue;
|
|
190
|
-
}
|
|
191
|
-
message.state = "claimed";
|
|
192
|
-
try {
|
|
193
|
-
message.onDispatch?.("out_of_band");
|
|
194
|
-
}
|
|
195
|
-
catch (error) {
|
|
196
|
-
dependencies.onError(error, request);
|
|
197
|
-
}
|
|
198
|
-
message.resolve("out_of_band");
|
|
199
|
-
}
|
|
200
|
-
catch (error) {
|
|
201
|
-
if (isWithdrawn(message))
|
|
202
|
-
continue;
|
|
203
|
-
message.reject(error);
|
|
204
|
-
dependencies.onError(error, request);
|
|
205
|
-
}
|
|
340
|
+
/**
|
|
341
|
+
* Messages that rode a direct prompt are delivered when the provider admits that prompt.
|
|
342
|
+
* Until it answers, nothing later may pass them.
|
|
343
|
+
*/
|
|
344
|
+
async function ride(riders, result) {
|
|
345
|
+
if (!riders.length)
|
|
346
|
+
return;
|
|
347
|
+
const { recipientId } = riders[0];
|
|
348
|
+
if (result.disposition === "turn_started") {
|
|
349
|
+
try {
|
|
350
|
+
await (dependencies.awaitStart?.(recipientId, result.started) ?? result.started);
|
|
206
351
|
}
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
});
|
|
212
|
-
}
|
|
213
|
-
function dispatchGroups(recipientId, messages) {
|
|
214
|
-
const groups = [];
|
|
215
|
-
let group = [];
|
|
216
|
-
let mode;
|
|
217
|
-
for (const message of messages) {
|
|
218
|
-
// Explicitly conflicting send options require separate, ordered dispatches.
|
|
219
|
-
if (group.length &&
|
|
220
|
-
((message.activeTurnBehavior ?? "steer") !== (group[0].activeTurnBehavior ?? "steer") ||
|
|
221
|
-
(mode && message.sessionMode && mode !== message.sessionMode))) {
|
|
222
|
-
groups.push(group);
|
|
223
|
-
group = [];
|
|
224
|
-
mode = undefined;
|
|
352
|
+
catch {
|
|
353
|
+
// The provider rejected the prompt that carried them. They go again, in their place.
|
|
354
|
+
handBack(riders);
|
|
355
|
+
return;
|
|
225
356
|
}
|
|
226
|
-
group.push(message);
|
|
227
|
-
mode ?? (mode = message.sessionMode);
|
|
228
357
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
358
|
+
const request = requestFor(recipientId, riders, false, () => false);
|
|
359
|
+
for (const message of riders)
|
|
360
|
+
claim(message, result.disposition, request);
|
|
361
|
+
}
|
|
362
|
+
/**
|
|
363
|
+
* The stopped recipient stays stopped. Its queue, interrupts included, goes with whatever
|
|
364
|
+
* prompts it next.
|
|
365
|
+
*/
|
|
366
|
+
function hold(inbox) {
|
|
367
|
+
inbox.cancelTimer();
|
|
368
|
+
inbox.cancelCap();
|
|
369
|
+
inbox.phase = "held";
|
|
370
|
+
inbox.stops++;
|
|
371
|
+
for (const message of inbox.messages) {
|
|
372
|
+
message.urgent = false;
|
|
373
|
+
message.held = true;
|
|
240
374
|
}
|
|
241
375
|
}
|
|
242
376
|
const unsubscribe = dependencies.subscribe?.((recipientId) => {
|
|
243
|
-
if (
|
|
244
|
-
|
|
377
|
+
if (inboxes.get(recipientId)?.phase === "waiting" && !isRunning(recipientId))
|
|
378
|
+
attempt(recipientId);
|
|
379
|
+
});
|
|
380
|
+
const unsubscribeStop = dependencies.onStop?.((recipientId) => {
|
|
381
|
+
const inbox = inboxes.get(recipientId);
|
|
382
|
+
if (inbox)
|
|
383
|
+
hold(inbox);
|
|
245
384
|
});
|
|
246
385
|
return {
|
|
247
|
-
withdrawPending
|
|
248
|
-
|
|
386
|
+
withdrawPending(recipientId, matches) {
|
|
387
|
+
const withdrawn = (inboxes.get(recipientId)?.messages ?? []).filter((message) => isQueued(message) && matches(message.source));
|
|
388
|
+
for (const message of withdrawn) {
|
|
389
|
+
settle(message, "withdrawn");
|
|
390
|
+
message.reject(new DOMException("Agent message withdrawn", "AbortError"));
|
|
391
|
+
}
|
|
392
|
+
// Without the message that reopened it after a Stop, the queue is held again.
|
|
393
|
+
const inbox = inboxes.get(recipientId);
|
|
394
|
+
if (withdrawn.length && inbox?.messages.every((message) => message.held))
|
|
395
|
+
hold(inbox);
|
|
396
|
+
return withdrawn.length;
|
|
397
|
+
},
|
|
398
|
+
hasPending(recipientId) {
|
|
399
|
+
const inbox = inboxes.get(recipientId);
|
|
400
|
+
return inbox !== undefined && inbox.phase !== "held";
|
|
401
|
+
},
|
|
249
402
|
enqueue(message) {
|
|
250
403
|
if (closed)
|
|
251
404
|
throw new Error("Agent message delivery is closed");
|
|
@@ -261,51 +414,58 @@ export function createAgentMessageDelivery(dependencies) {
|
|
|
261
414
|
messageId: message.messageId ?? randomUUID(),
|
|
262
415
|
dispatched,
|
|
263
416
|
};
|
|
264
|
-
|
|
417
|
+
admit({
|
|
265
418
|
...message,
|
|
266
419
|
receipt,
|
|
267
420
|
resolve,
|
|
268
421
|
reject,
|
|
269
|
-
sequence: sequence++,
|
|
270
422
|
state: "queued",
|
|
423
|
+
urgent: message.activeTurnBehavior === "interrupt",
|
|
424
|
+
held: false,
|
|
271
425
|
outOfBandDeclined: false,
|
|
272
|
-
};
|
|
273
|
-
const owned = admitted.get(message.recipientId) ?? new Set();
|
|
274
|
-
owned.add(item);
|
|
275
|
-
admitted.set(message.recipientId, owned);
|
|
276
|
-
void dispatched
|
|
277
|
-
.finally(() => {
|
|
278
|
-
owned.delete(item);
|
|
279
|
-
if (!owned.size)
|
|
280
|
-
admitted.delete(message.recipientId);
|
|
281
|
-
})
|
|
282
|
-
.catch(() => undefined);
|
|
283
|
-
const entry = pending.get(message.recipientId) ?? {
|
|
284
|
-
messages: [],
|
|
285
|
-
ready: false,
|
|
286
|
-
cancel: () => { },
|
|
287
|
-
};
|
|
288
|
-
entry.cancel();
|
|
289
|
-
entry.ready = false;
|
|
290
|
-
entry.messages.push(item);
|
|
291
|
-
entry.cancel = schedule(2000, () => flush(message.recipientId));
|
|
292
|
-
pending.set(message.recipientId, entry);
|
|
426
|
+
});
|
|
293
427
|
return receipt;
|
|
294
428
|
},
|
|
429
|
+
sendDirect(direct, send) {
|
|
430
|
+
// Its place in line is decided now: what is queued goes with it, what comes later follows.
|
|
431
|
+
const ahead = closed || isCommand(direct.prompt)
|
|
432
|
+
? []
|
|
433
|
+
: [...(inboxes.get(direct.recipientId)?.messages ?? [])];
|
|
434
|
+
return new Promise((resolve, reject) => {
|
|
435
|
+
run(direct.recipientId, async () => {
|
|
436
|
+
const riders = ahead.filter(isQueued);
|
|
437
|
+
for (const message of riders)
|
|
438
|
+
message.state = "claimed";
|
|
439
|
+
let result;
|
|
440
|
+
try {
|
|
441
|
+
for (const message of riders)
|
|
442
|
+
message.beforeDispatch?.();
|
|
443
|
+
result = await send(join(riders, direct));
|
|
444
|
+
}
|
|
445
|
+
catch (error) {
|
|
446
|
+
handBack(riders);
|
|
447
|
+
reject(error);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
resolve(result);
|
|
451
|
+
await ride(riders, result);
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
},
|
|
295
455
|
async waitForDispatch(recipientId, signal) {
|
|
296
456
|
signal.throwIfAborted();
|
|
297
|
-
const waiting = Array.from(
|
|
457
|
+
const waiting = Array.from(inboxes.get(recipientId)?.messages ?? [], (message) => message.receipt.dispatched.catch((error) => {
|
|
298
458
|
if (message.state !== "withdrawn")
|
|
299
459
|
throw error;
|
|
300
460
|
return undefined;
|
|
301
461
|
}));
|
|
302
|
-
let
|
|
303
|
-
const
|
|
462
|
+
let unsubscribeCheckpoint;
|
|
463
|
+
const checkpoint = new Promise((resolve) => {
|
|
304
464
|
const check = () => {
|
|
305
|
-
if (dependencies.hasPendingPermission?.(recipientId))
|
|
465
|
+
if (dependencies.hasPendingPermission?.(recipientId) || isHeld(recipientId))
|
|
306
466
|
resolve();
|
|
307
467
|
};
|
|
308
|
-
|
|
468
|
+
unsubscribeCheckpoint = dependencies.subscribe?.((agentId) => {
|
|
309
469
|
if (agentId === recipientId)
|
|
310
470
|
check();
|
|
311
471
|
});
|
|
@@ -318,14 +478,14 @@ export function createAgentMessageDelivery(dependencies) {
|
|
|
318
478
|
});
|
|
319
479
|
try {
|
|
320
480
|
await Promise.race([
|
|
321
|
-
Promise.all([
|
|
481
|
+
Promise.all([releasing.get(recipientId), ...waiting]),
|
|
322
482
|
cancellation,
|
|
323
|
-
|
|
483
|
+
checkpoint,
|
|
324
484
|
]);
|
|
325
485
|
}
|
|
326
486
|
finally {
|
|
327
487
|
signal.removeEventListener("abort", aborted);
|
|
328
|
-
|
|
488
|
+
unsubscribeCheckpoint?.();
|
|
329
489
|
}
|
|
330
490
|
},
|
|
331
491
|
close() {
|
|
@@ -333,10 +493,23 @@ export function createAgentMessageDelivery(dependencies) {
|
|
|
333
493
|
return closing;
|
|
334
494
|
closed = true;
|
|
335
495
|
unsubscribe?.();
|
|
336
|
-
|
|
337
|
-
|
|
496
|
+
unsubscribeStop?.();
|
|
497
|
+
const receipts = [];
|
|
498
|
+
for (const [recipientId, inbox] of inboxes) {
|
|
499
|
+
inbox.cancelTimer();
|
|
500
|
+
inbox.cancelCap();
|
|
501
|
+
// Shutdown delivers held messages too: nothing in memory survives it.
|
|
502
|
+
inbox.phase = "waiting";
|
|
503
|
+
receipts.push(...inbox.messages.map((message) => message.receipt.dispatched));
|
|
504
|
+
// The drain reads the queue when its turn comes, so it sees what the operation in
|
|
505
|
+
// flight returns to it.
|
|
506
|
+
run(recipientId, async () => {
|
|
507
|
+
for (const group of modeGroups(inbox.messages.filter(isQueued)))
|
|
508
|
+
await deliver(recipientId, group, false, () => false).catch(() => undefined);
|
|
509
|
+
});
|
|
510
|
+
}
|
|
338
511
|
// Each failure is reported and rejects its receipts; shutdown still drains other recipients.
|
|
339
|
-
closing = Promise.allSettled(
|
|
512
|
+
closing = Promise.allSettled(receipts).then(() => undefined);
|
|
340
513
|
return closing;
|
|
341
514
|
},
|
|
342
515
|
};
|