@otium-team/negotium 0.23.11 → 0.23.13
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/agent-helpers.js +510 -1323
- package/dist/agent-helpers.js.map +10 -10
- package/dist/background-bash.js +12 -12
- package/dist/background-bash.js.map +3 -3
- package/dist/browser-runtime.js +42 -44
- package/dist/browser-runtime.js.map +3 -3
- package/dist/canonical-mcp-bridge.js +8 -9
- package/dist/canonical-mcp-bridge.js.map +1 -1
- package/dist/chunk-22kg3fsw.js +79 -0
- package/dist/{chunk-ad7kxsaq.js.map → chunk-22kg3fsw.js.map} +1 -1
- package/dist/{chunk-mxxwx5mg.js → chunk-d4j77adx.js} +96 -135
- package/dist/{chunk-mxxwx5mg.js.map → chunk-d4j77adx.js.map} +6 -6
- package/dist/{chunk-0fenxbe6.js → chunk-mk9x18jn.js} +4 -5
- package/dist/{chunk-0fenxbe6.js.map → chunk-mk9x18jn.js.map} +2 -2
- package/dist/{chunk-da9ad838.js → chunk-qd0vtfm2.js} +115 -119
- package/dist/{chunk-da9ad838.js.map → chunk-qd0vtfm2.js.map} +7 -7
- package/dist/{chunk-jtv8yzy1.js → chunk-wf7ny8eq.js} +12 -12
- package/dist/{chunk-jtv8yzy1.js.map → chunk-wf7ny8eq.js.map} +2 -2
- package/dist/{chunk-f7p0h1gs.js → chunk-ww8a4ej4.js} +5 -5
- package/dist/{chunk-f7p0h1gs.js.map → chunk-ww8a4ej4.js.map} +2 -2
- package/dist/hosted-agent.js +198 -135
- package/dist/hosted-agent.js.map +4 -4
- package/dist/main.js +3906 -4213
- package/dist/main.js.map +20 -18
- package/dist/mcp-catalog.js +4 -4
- package/dist/mcp-catalog.js.map +1 -1
- package/dist/mcp-factories.js +753 -1558
- package/dist/mcp-factories.js.map +10 -10
- package/dist/mcp-servers.js +5 -5
- package/dist/mcp-servers.js.map +1 -1
- package/dist/media.js +3 -3
- package/dist/media.js.map +2 -2
- package/dist/outbox.js +15 -15
- package/dist/outbox.js.map +2 -2
- package/dist/platform-runtime.js +7 -7
- package/dist/platform-runtime.js.map +1 -1
- package/dist/prompts.js +6 -6
- package/dist/prompts.js.map +3 -3
- package/dist/query-runtime.js +16 -18
- package/dist/query-runtime.js.map +4 -4
- package/dist/registry.js +19 -20
- package/dist/registry.js.map +2 -2
- package/dist/rollout.js +41 -41
- package/dist/rollout.js.map +2 -2
- package/dist/runtime/src/agents/claude-provider.ts +80 -5
- package/dist/runtime/src/application/submit-runtime-gateway-turn.ts +84 -20
- package/dist/runtime/src/application/topic-service.ts +6 -2
- package/dist/runtime/src/application/turn-follow-ups.ts +291 -0
- package/dist/runtime/src/bus.ts +6 -0
- package/dist/runtime/src/node-host.ts +11 -0
- package/dist/runtime/src/query/active-rooms.ts +7 -1
- package/dist/runtime/src/runtime/turn-runner.ts +34 -7
- package/dist/runtime/src/runtime-gateway.ts +198 -1
- package/dist/runtime/src/storage/runtime-follow-ups.ts +212 -0
- package/dist/runtime/src/storage/runtime-turn-requests.ts +43 -2
- package/dist/runtime/src/types/api.ts +9 -0
- package/dist/runtime/src/types.ts +23 -0
- package/dist/runtime/src/version.ts +1 -1
- package/dist/runtime-gateway.js +73 -12
- package/dist/runtime-gateway.js.map +3 -3
- package/dist/runtime-helpers.js +52 -54
- package/dist/runtime-helpers.js.map +3 -3
- package/dist/sqlite.js +3 -5
- package/dist/sqlite.js.map +2 -2
- package/dist/storage.js +4841 -4812
- package/dist/storage.js.map +3 -3
- package/dist/types/packages/core/src/bus.d.ts +3 -0
- package/dist/types/packages/core/src/query/active-rooms.d.ts +7 -1
- package/dist/types/packages/core/src/runtime-gateway.d.ts +59 -1
- package/dist/types/packages/core/src/storage/runtime-turn-requests.d.ts +25 -0
- package/dist/types/packages/core/src/types/api.d.ts +5 -0
- package/dist/types/packages/core/src/types.d.ts +21 -0
- package/dist/types/packages/core/src/version.d.ts +1 -1
- package/dist/vault.js +20 -22
- package/dist/vault.js.map +3 -3
- package/package.json +1 -1
- package/dist/chunk-2mx7fq49.js +0 -6
- package/dist/chunk-2mx7fq49.js.map +0 -9
- package/dist/chunk-ad7kxsaq.js +0 -202
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Follow-ups: messages a person sends while the room's AI works, held by the
|
|
3
|
+
* node instead of queued as a turn, the way Codex holds its queue. A host opts
|
|
4
|
+
* in per message with `hold`; without it nothing here runs, so hosts that never
|
|
5
|
+
* send it (the terminal, Telegram) keep the queue and steer they have.
|
|
6
|
+
*
|
|
7
|
+
* A held message is not in the transcript yet. Until it goes out it can be
|
|
8
|
+
* edited in place, moved or deleted, or sent now (into the running turn when it
|
|
9
|
+
* can take it). When the room is idle the oldest goes out as a turn of its own,
|
|
10
|
+
* one per turn. A person stopping the AI holds the rest until they send again.
|
|
11
|
+
* Every change is announced as a `follow_ups` ai-status with the whole line.
|
|
12
|
+
*/
|
|
13
|
+
import { logger } from "#platform/logger";
|
|
14
|
+
import { getRoomQuery } from "#query/active-rooms";
|
|
15
|
+
import { stampActorTopicScope } from "#runtime/actor-topic-scope";
|
|
16
|
+
import { resolveAttachmentByFileId } from "#runtime/file-hooks";
|
|
17
|
+
import { getTopic } from "#storage/api-topics";
|
|
18
|
+
import { appendRuntimeEvent } from "#storage/runtime-events";
|
|
19
|
+
import {
|
|
20
|
+
areRuntimeFollowUpsPaused,
|
|
21
|
+
deleteRuntimeFollowUpsForTopic,
|
|
22
|
+
editRuntimeFollowUpText,
|
|
23
|
+
getRuntimeFollowUp,
|
|
24
|
+
holdRuntimeFollowUp,
|
|
25
|
+
listRuntimeFollowUps,
|
|
26
|
+
moveRuntimeFollowUp,
|
|
27
|
+
type RuntimeFollowUp,
|
|
28
|
+
restoreRuntimeFollowUp,
|
|
29
|
+
runtimeFollowUpTopicsReady,
|
|
30
|
+
setRuntimeFollowUpsPaused,
|
|
31
|
+
takeRuntimeFollowUp,
|
|
32
|
+
} from "#storage/runtime-follow-ups";
|
|
33
|
+
import { findRuntimeGatewaySubmission } from "#storage/runtime-gateway-submissions";
|
|
34
|
+
import { getRuntimeTurnLease } from "#storage/runtime-leases";
|
|
35
|
+
import { isRuntimeTopicMaintenance } from "#storage/runtime-topic-state";
|
|
36
|
+
import { getRuntimeUserTurnRequest } from "#storage/runtime-turn-requests";
|
|
37
|
+
import type { AttachmentDto } from "#types/api";
|
|
38
|
+
import {
|
|
39
|
+
type SubmitRuntimeGatewayTurnParams,
|
|
40
|
+
type SubmitRuntimeGatewayTurnResult,
|
|
41
|
+
submitRuntimeGatewayTurn,
|
|
42
|
+
} from "./submit-runtime-gateway-turn";
|
|
43
|
+
|
|
44
|
+
export interface FollowUpView {
|
|
45
|
+
id: string;
|
|
46
|
+
text: string;
|
|
47
|
+
actorUserId: string;
|
|
48
|
+
attachments: AttachmentDto[];
|
|
49
|
+
createdAt: string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface FollowUpLine {
|
|
53
|
+
topicId: string;
|
|
54
|
+
followUps: FollowUpView[];
|
|
55
|
+
/** Held after a stop: nothing goes out by itself until the person sends again. */
|
|
56
|
+
paused: boolean;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export type HoldOrSubmitResult =
|
|
60
|
+
| { held: true; followUp: FollowUpView }
|
|
61
|
+
| { held: false; submission: SubmitRuntimeGatewayTurnResult };
|
|
62
|
+
|
|
63
|
+
function view(followUp: RuntimeFollowUp): FollowUpView {
|
|
64
|
+
return {
|
|
65
|
+
id: followUp.followUpId,
|
|
66
|
+
text: followUp.text,
|
|
67
|
+
actorUserId: followUp.actorUserId,
|
|
68
|
+
attachments: followUp.attachments
|
|
69
|
+
.map(resolveAttachmentByFileId)
|
|
70
|
+
.filter((attachment): attachment is AttachmentDto => attachment !== null),
|
|
71
|
+
createdAt: new Date(followUp.createdAt).toISOString(),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function followUpLine(topicId: string): FollowUpLine {
|
|
76
|
+
return {
|
|
77
|
+
topicId,
|
|
78
|
+
followUps: listRuntimeFollowUps(topicId).map(view),
|
|
79
|
+
paused: areRuntimeFollowUpsPaused(topicId),
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function announce(topicId: string): void {
|
|
84
|
+
const { followUps, paused } = followUpLine(topicId);
|
|
85
|
+
appendRuntimeEvent("runtime-follow-ups", {
|
|
86
|
+
type: "ai-status",
|
|
87
|
+
topicId,
|
|
88
|
+
payload: { kind: "follow_ups", followUps, paused },
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** Whether the room's AI has work now: a turn running or queued, or the room held for upkeep. */
|
|
93
|
+
export function isRoomWorking(topicId: string): boolean {
|
|
94
|
+
return (
|
|
95
|
+
getRuntimeUserTurnRequest(topicId) !== null ||
|
|
96
|
+
getRoomQuery(topicId) !== undefined ||
|
|
97
|
+
getRuntimeTurnLease(topicId) !== null ||
|
|
98
|
+
isRuntimeTopicMaintenance(topicId)
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
type StoredParams = Omit<SubmitRuntimeGatewayTurnParams, "topic" | "text">;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* `hold: true` from a host: while the room works, or while earlier follow-ups
|
|
106
|
+
* wait their turn, the message is held; otherwise it goes out as usual. Sending
|
|
107
|
+
* while the line is held after a stop releases it: this message goes first.
|
|
108
|
+
*/
|
|
109
|
+
export function holdOrSubmitRuntimeGatewayTurn(
|
|
110
|
+
params: SubmitRuntimeGatewayTurnParams,
|
|
111
|
+
): HoldOrSubmitResult {
|
|
112
|
+
const topicId = params.topic.id;
|
|
113
|
+
const requestId = params.requestId ?? params.clientMessageId;
|
|
114
|
+
const replayed = getRuntimeFollowUp(params.clientMessageId);
|
|
115
|
+
if (replayed?.topicId === topicId) return { held: true, followUp: view(replayed) };
|
|
116
|
+
const alreadySent = findRuntimeGatewaySubmission(params.clientMessageId, requestId) !== null;
|
|
117
|
+
const waiting = listRuntimeFollowUps(topicId).length > 0;
|
|
118
|
+
const paused = areRuntimeFollowUpsPaused(topicId);
|
|
119
|
+
const holdable = !alreadySent && params.respond !== false && params.silent !== true;
|
|
120
|
+
if (holdable && (isRoomWorking(topicId) || (waiting && !paused))) {
|
|
121
|
+
const { topic: _topic, text, ...rest } = params;
|
|
122
|
+
// Stamped now: a held message's room assertion ages from when the host
|
|
123
|
+
// made it, not from when the message goes out.
|
|
124
|
+
const stored: StoredParams = {
|
|
125
|
+
...rest,
|
|
126
|
+
...(params.actorTopicScope
|
|
127
|
+
? { actorTopicScope: stampActorTopicScope(params.actorTopicScope) }
|
|
128
|
+
: {}),
|
|
129
|
+
};
|
|
130
|
+
const followUp = holdRuntimeFollowUp({
|
|
131
|
+
followUpId: params.clientMessageId,
|
|
132
|
+
topicId,
|
|
133
|
+
actorUserId: params.actorUserId ?? params.userId,
|
|
134
|
+
text,
|
|
135
|
+
attachments: params.attachments ?? [],
|
|
136
|
+
params: stored as unknown as Record<string, unknown>,
|
|
137
|
+
});
|
|
138
|
+
announce(topicId);
|
|
139
|
+
return { held: true, followUp: view(followUp) };
|
|
140
|
+
}
|
|
141
|
+
const submission = submitRuntimeGatewayTurn(params);
|
|
142
|
+
if (holdable && paused && setRuntimeFollowUpsPaused(topicId, false)) announce(topicId);
|
|
143
|
+
return { held: false, submission };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function sendHeld(
|
|
147
|
+
followUp: RuntimeFollowUp,
|
|
148
|
+
how: { inject: boolean },
|
|
149
|
+
): SubmitRuntimeGatewayTurnResult | null {
|
|
150
|
+
const index = listRuntimeFollowUps(followUp.topicId).findIndex(
|
|
151
|
+
(item) => item.followUpId === followUp.followUpId,
|
|
152
|
+
);
|
|
153
|
+
const taken = takeRuntimeFollowUp(followUp.followUpId);
|
|
154
|
+
if (!taken) return null;
|
|
155
|
+
const topic = getTopic(taken.topicId);
|
|
156
|
+
if (!topic) {
|
|
157
|
+
deleteRuntimeFollowUpsForTopic(taken.topicId);
|
|
158
|
+
return null;
|
|
159
|
+
}
|
|
160
|
+
try {
|
|
161
|
+
const stored = taken.params as unknown as StoredParams;
|
|
162
|
+
return submitRuntimeGatewayTurn({
|
|
163
|
+
...stored,
|
|
164
|
+
topic,
|
|
165
|
+
text: taken.text,
|
|
166
|
+
steer: false,
|
|
167
|
+
inject: how.inject,
|
|
168
|
+
ownTurn: true,
|
|
169
|
+
});
|
|
170
|
+
} catch (error) {
|
|
171
|
+
// Back where it was, and the line holds so a failing message is not retried forever.
|
|
172
|
+
restoreRuntimeFollowUp(taken, index);
|
|
173
|
+
setRuntimeFollowUpsPaused(taken.topicId, true);
|
|
174
|
+
logger.warn(
|
|
175
|
+
{
|
|
176
|
+
topicId: taken.topicId,
|
|
177
|
+
followUpId: taken.followUpId,
|
|
178
|
+
error: error instanceof Error ? error.message : String(error),
|
|
179
|
+
},
|
|
180
|
+
"follow-up could not go out; the line holds",
|
|
181
|
+
);
|
|
182
|
+
return null;
|
|
183
|
+
} finally {
|
|
184
|
+
announce(taken.topicId);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Send the oldest follow-up of every idle room that is not held after a stop. */
|
|
189
|
+
export function dispatchReadyFollowUps(): void {
|
|
190
|
+
for (const topicId of runtimeFollowUpTopicsReady()) {
|
|
191
|
+
if (isRoomWorking(topicId)) continue;
|
|
192
|
+
const first = listRuntimeFollowUps(topicId)[0];
|
|
193
|
+
if (first) sendHeld(first, { inject: false });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
export class FollowUpNotFoundError extends Error {
|
|
198
|
+
constructor() {
|
|
199
|
+
super("follow-up not found");
|
|
200
|
+
this.name = "FollowUpNotFoundError";
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Only the person who sent it may change it; to anyone else it does not exist. */
|
|
205
|
+
function ownFollowUp(topicId: string, followUpId: string, actorUserId: string): RuntimeFollowUp {
|
|
206
|
+
const followUp = getRuntimeFollowUp(followUpId);
|
|
207
|
+
if (!followUp || followUp.topicId !== topicId || followUp.actorUserId !== actorUserId) {
|
|
208
|
+
throw new FollowUpNotFoundError();
|
|
209
|
+
}
|
|
210
|
+
return followUp;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function editFollowUp(
|
|
214
|
+
topicId: string,
|
|
215
|
+
followUpId: string,
|
|
216
|
+
actorUserId: string,
|
|
217
|
+
text: string,
|
|
218
|
+
): FollowUpLine {
|
|
219
|
+
const followUp = ownFollowUp(topicId, followUpId, actorUserId);
|
|
220
|
+
if (!text.trim() && followUp.attachments.length === 0) {
|
|
221
|
+
takeRuntimeFollowUp(followUpId);
|
|
222
|
+
} else {
|
|
223
|
+
editRuntimeFollowUpText(followUpId, text);
|
|
224
|
+
}
|
|
225
|
+
announce(topicId);
|
|
226
|
+
return followUpLine(topicId);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function deleteFollowUp(
|
|
230
|
+
topicId: string,
|
|
231
|
+
followUpId: string,
|
|
232
|
+
actorUserId: string,
|
|
233
|
+
): FollowUpLine {
|
|
234
|
+
ownFollowUp(topicId, followUpId, actorUserId);
|
|
235
|
+
takeRuntimeFollowUp(followUpId);
|
|
236
|
+
announce(topicId);
|
|
237
|
+
return followUpLine(topicId);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
export function moveFollowUp(
|
|
241
|
+
topicId: string,
|
|
242
|
+
followUpId: string,
|
|
243
|
+
actorUserId: string,
|
|
244
|
+
toIndex: number,
|
|
245
|
+
): FollowUpLine {
|
|
246
|
+
ownFollowUp(topicId, followUpId, actorUserId);
|
|
247
|
+
moveRuntimeFollowUp(followUpId, toIndex);
|
|
248
|
+
announce(topicId);
|
|
249
|
+
return followUpLine(topicId);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* 지금 보내기: the message goes out now. Into the running turn when it is the
|
|
254
|
+
* same person's and can take it (read after the step it is on); otherwise as a
|
|
255
|
+
* turn of its own. Either way nothing stops, and the line is no longer held.
|
|
256
|
+
*/
|
|
257
|
+
export function sendFollowUpNow(
|
|
258
|
+
topicId: string,
|
|
259
|
+
followUpId: string,
|
|
260
|
+
actorUserId: string,
|
|
261
|
+
): { line: FollowUpLine; submission: SubmitRuntimeGatewayTurnResult | null } {
|
|
262
|
+
const followUp = ownFollowUp(topicId, followUpId, actorUserId);
|
|
263
|
+
setRuntimeFollowUpsPaused(topicId, false);
|
|
264
|
+
const submission = sendHeld(followUp, { inject: true });
|
|
265
|
+
return { line: followUpLine(topicId), submission };
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* A person stopped the AI: what waits stays until they send again. Called by
|
|
270
|
+
* the host's abort route; a room with nothing held is left as it was.
|
|
271
|
+
*/
|
|
272
|
+
export function holdFollowUpsAfterStop(topicId: string): void {
|
|
273
|
+
if (listRuntimeFollowUps(topicId).length === 0) return;
|
|
274
|
+
if (setRuntimeFollowUpsPaused(topicId, true)) announce(topicId);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** Watch for idle rooms with follow-ups waiting. Every node process may run one; taking a follow-up is atomic. */
|
|
278
|
+
export function startFollowUpDispatcher(intervalMs = 250): () => void {
|
|
279
|
+
const timer = setInterval(() => {
|
|
280
|
+
try {
|
|
281
|
+
dispatchReadyFollowUps();
|
|
282
|
+
} catch (error) {
|
|
283
|
+
logger.warn(
|
|
284
|
+
{ error: error instanceof Error ? error.message : String(error) },
|
|
285
|
+
"follow-up dispatch failed",
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
}, intervalMs);
|
|
289
|
+
timer.unref?.();
|
|
290
|
+
return () => clearInterval(timer);
|
|
291
|
+
}
|
package/dist/runtime/src/bus.ts
CHANGED
|
@@ -96,6 +96,7 @@ export interface RuntimeBus {
|
|
|
96
96
|
severity: "info" | "warning" | "error",
|
|
97
97
|
label: string,
|
|
98
98
|
): void;
|
|
99
|
+
broadcastMidTurnRead(topicId: string, queryId: string, messageIds: readonly string[]): void;
|
|
99
100
|
broadcastAborted(topicId: string, queryId: string, reason?: "superseded" | "stopped"): void;
|
|
100
101
|
broadcastToolCall(
|
|
101
102
|
topicId: string,
|
|
@@ -278,6 +279,11 @@ export class SqliteRuntimeBus implements RuntimeBus {
|
|
|
278
279
|
});
|
|
279
280
|
}
|
|
280
281
|
|
|
282
|
+
/** The running turn read these messages the person sent while it worked. */
|
|
283
|
+
broadcastMidTurnRead(topicId: string, queryId: string, messageIds: readonly string[]): void {
|
|
284
|
+
this.broadcastAiStatus(topicId, { kind: "messages_read", queryId, messageIds });
|
|
285
|
+
}
|
|
286
|
+
|
|
281
287
|
broadcastAborted(topicId: string, queryId: string, reason?: "superseded" | "stopped"): void {
|
|
282
288
|
this.broadcastAiStatus(topicId, { kind: "ai_aborted", queryId, reason });
|
|
283
289
|
}
|
|
@@ -20,6 +20,17 @@ export { submitUserMessage } from "#application/submit-user-message";
|
|
|
20
20
|
export { switchTopicEffort } from "#application/switch-topic-effort";
|
|
21
21
|
export { switchTopicModel } from "#application/switch-topic-model";
|
|
22
22
|
export { TopicServiceError, topicService } from "#application/topic-service";
|
|
23
|
+
export {
|
|
24
|
+
deleteFollowUp,
|
|
25
|
+
editFollowUp,
|
|
26
|
+
FollowUpNotFoundError,
|
|
27
|
+
followUpLine,
|
|
28
|
+
holdFollowUpsAfterStop,
|
|
29
|
+
holdOrSubmitRuntimeGatewayTurn,
|
|
30
|
+
moveFollowUp,
|
|
31
|
+
sendFollowUpNow,
|
|
32
|
+
startFollowUpDispatcher,
|
|
33
|
+
} from "#application/turn-follow-ups";
|
|
23
34
|
export {
|
|
24
35
|
deleteVaultEntry,
|
|
25
36
|
executeVaultCommand,
|
|
@@ -27,7 +27,7 @@ import {
|
|
|
27
27
|
requestRuntimeTurnAbort,
|
|
28
28
|
} from "#storage/runtime-leases";
|
|
29
29
|
import type { AskReplySource } from "#storage/session-asks";
|
|
30
|
-
import type { AgentKind, PeerRuntimeBridgeContext } from "#types";
|
|
30
|
+
import type { AgentKind, MidTurnMessage, PeerRuntimeBridgeContext } from "#types";
|
|
31
31
|
import { createRoomQueryRegistry, type RoomQueryDecision } from "./room-query-registry";
|
|
32
32
|
import { AbortReason } from "./types";
|
|
33
33
|
|
|
@@ -150,6 +150,12 @@ export interface RoomQueryControl {
|
|
|
150
150
|
injectParams?: DeferredInject;
|
|
151
151
|
/** True only after this process successfully queued a local replay. */
|
|
152
152
|
injectRequeued?: boolean;
|
|
153
|
+
/**
|
|
154
|
+
* Set while this turn's provider takes messages mid-turn: hands it a message
|
|
155
|
+
* the same person sent, whose queued row is `requestId`. False when the turn
|
|
156
|
+
* can no longer take it, and the row then runs as the next turn.
|
|
157
|
+
*/
|
|
158
|
+
deliverMidTurn?: (message: MidTurnMessage, requestId: string) => boolean;
|
|
153
159
|
}
|
|
154
160
|
|
|
155
161
|
// ── Room-keyed in-flight registry ──────────────────────────────────────
|
|
@@ -99,6 +99,7 @@ import { getRuntimeTopicEpoch, isRuntimeTopicMaintenance } from "#storage/runtim
|
|
|
99
99
|
import {
|
|
100
100
|
claimNextRuntimeUserTurnRequest,
|
|
101
101
|
completeRuntimeUserTurnRequest,
|
|
102
|
+
dropReadMidTurnRequest,
|
|
102
103
|
getActiveRuntimeUserTurnRequest,
|
|
103
104
|
markRuntimeUserTurnMessagesLogged,
|
|
104
105
|
markRuntimeUserTurnRunning,
|
|
@@ -121,6 +122,7 @@ import type {
|
|
|
121
122
|
ActorTopicScope,
|
|
122
123
|
AgentKind,
|
|
123
124
|
EffortLevel,
|
|
125
|
+
MidTurnInput,
|
|
124
126
|
PeerRuntimeBridgeContext,
|
|
125
127
|
RemoteSessionGrant,
|
|
126
128
|
UnifiedEvent,
|
|
@@ -196,7 +198,7 @@ function notifyPlaywrightUnavailable(topicId: string) {
|
|
|
196
198
|
topicId,
|
|
197
199
|
"Playwright browser tools are unavailable this turn. Browser automation was removed from the tool catalog; retry shortly if browser interaction is required.",
|
|
198
200
|
);
|
|
199
|
-
WsHub.get().broadcastTopicNotice(topicId, "info", "Playwright
|
|
201
|
+
WsHub.get().broadcastTopicNotice(topicId, "info", "Playwright unavailable");
|
|
200
202
|
}
|
|
201
203
|
|
|
202
204
|
function appendAskReplyMessage(
|
|
@@ -1429,6 +1431,30 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
1429
1431
|
from: params.from,
|
|
1430
1432
|
},
|
|
1431
1433
|
};
|
|
1434
|
+
// A person's turn reads what they send while it works (gateway `inject`). The
|
|
1435
|
+
// provider opens this when it can feed its live turn; on close, a read
|
|
1436
|
+
// message's queued row is dropped, and every unread one runs as the next turn.
|
|
1437
|
+
const midTurnRows = new Map<string, string>();
|
|
1438
|
+
const midTurn: MidTurnInput | undefined =
|
|
1439
|
+
control.actorUserId !== undefined
|
|
1440
|
+
? {
|
|
1441
|
+
open: (deliver) => {
|
|
1442
|
+
control.deliverMidTurn = (message, requestId) => {
|
|
1443
|
+
if (!deliver(message)) return false;
|
|
1444
|
+
midTurnRows.set(message.id, requestId);
|
|
1445
|
+
return true;
|
|
1446
|
+
};
|
|
1447
|
+
},
|
|
1448
|
+
close: (read) => {
|
|
1449
|
+
control.deliverMidTurn = undefined;
|
|
1450
|
+
const dropped = read.filter((id) => {
|
|
1451
|
+
const row = midTurnRows.get(id);
|
|
1452
|
+
return row !== undefined && dropReadMidTurnRequest(topicId, row);
|
|
1453
|
+
});
|
|
1454
|
+
if (dropped.length > 0) WsHub.get().broadcastMidTurnRead(topicId, queryId, dropped);
|
|
1455
|
+
},
|
|
1456
|
+
}
|
|
1457
|
+
: undefined;
|
|
1432
1458
|
if (!setRoomQuery(control)) {
|
|
1433
1459
|
// Another process won the lease between the decision and the atomic claim.
|
|
1434
1460
|
// Preserve the same user-priority behavior and retry after that owner
|
|
@@ -1673,7 +1699,7 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
1673
1699
|
|
|
1674
1700
|
if (params.from === FROM_AUTO_CONTINUE) {
|
|
1675
1701
|
turnReminders.push(
|
|
1676
|
-
"<system-reminder
|
|
1702
|
+
"<system-reminder>This turn was auto-resumed after an automatic settings adjustment (effort/model/agent). Start the work immediately, without re-evaluating difficulty or changing effort/model/agent settings.</system-reminder>",
|
|
1677
1703
|
);
|
|
1678
1704
|
}
|
|
1679
1705
|
|
|
@@ -1812,6 +1838,7 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
1812
1838
|
yield* runAgent({
|
|
1813
1839
|
agent: agentKind,
|
|
1814
1840
|
...reminderQuery,
|
|
1841
|
+
...(midTurn ? { midTurn } : {}),
|
|
1815
1842
|
attachments: promptAttachments,
|
|
1816
1843
|
cwd: workspaceCwd,
|
|
1817
1844
|
systemPrompt,
|
|
@@ -2061,7 +2088,7 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
2061
2088
|
if (!silent) {
|
|
2062
2089
|
appendSystemMessage(
|
|
2063
2090
|
topicId,
|
|
2064
|
-
"
|
|
2091
|
+
"Completed up to this point. Work stopped because it reached the cost budget limit.",
|
|
2065
2092
|
threadRootId,
|
|
2066
2093
|
);
|
|
2067
2094
|
WsHub.get().broadcastDone(
|
|
@@ -2078,7 +2105,7 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
2078
2105
|
: undefined,
|
|
2079
2106
|
{ agent: agentKind, model: resolvedModel, budgetReason: outcome.reason },
|
|
2080
2107
|
);
|
|
2081
|
-
WsHub.get().broadcastTopicNotice(topicId, "warning", "
|
|
2108
|
+
WsHub.get().broadcastTopicNotice(topicId, "warning", "Budget limit reached");
|
|
2082
2109
|
}
|
|
2083
2110
|
// Visible ask_session turns run on the target session too, so the
|
|
2084
2111
|
// caller must hear about the cap regardless of `silent`.
|
|
@@ -2099,11 +2126,11 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
2099
2126
|
// providers and explicitly change this topic with /model.
|
|
2100
2127
|
appendSystemMessage(
|
|
2101
2128
|
topicId,
|
|
2102
|
-
`${classifyAgentError(outcome.error, agentKind)}\n\
|
|
2129
|
+
`${classifyAgentError(outcome.error, agentKind)}\n\nTo use another registered model, switch with /model <model> and send again.`,
|
|
2103
2130
|
threadRootId,
|
|
2104
2131
|
);
|
|
2105
2132
|
WsHub.get().broadcastError(topicId, queryId, outcome.error);
|
|
2106
|
-
WsHub.get().broadcastTopicNotice(topicId, "error", "
|
|
2133
|
+
WsHub.get().broadcastTopicNotice(topicId, "error", "Response failed");
|
|
2107
2134
|
}
|
|
2108
2135
|
await deliverAskError(queryId, topic.title, outcome.error);
|
|
2109
2136
|
await settleSubagentFailure(queryId, classifyAgentError(outcome.error, agentKind));
|
|
@@ -2137,7 +2164,7 @@ export function startAiTurn(params: StartAiTurnParams): string | null {
|
|
|
2137
2164
|
);
|
|
2138
2165
|
if (!silent) {
|
|
2139
2166
|
WsHub.get().broadcastError(topicId, queryId, error);
|
|
2140
|
-
WsHub.get().broadcastTopicNotice(topicId, "error", "
|
|
2167
|
+
WsHub.get().broadcastTopicNotice(topicId, "error", "Response failed");
|
|
2141
2168
|
}
|
|
2142
2169
|
await deliverAskError(queryId, topic.title, error);
|
|
2143
2170
|
await settleSubagentFailure(queryId, classifyAgentError(error, agentKind));
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { ActorTopicScope, RemoteSessionGrant } from "#types";
|
|
2
|
-
import type { MessageDto } from "#types/api";
|
|
2
|
+
import type { AttachmentDto, MessageDto } from "#types/api";
|
|
3
3
|
|
|
4
4
|
export const RUNTIME_GATEWAY_VERSION = 1 as const;
|
|
5
5
|
export const RUNTIME_GATEWAY_CONTROL_PATH = "/api/v1/control/runtime/v1";
|
|
@@ -151,6 +151,20 @@ export interface RuntimeGatewayTurnInput {
|
|
|
151
151
|
* resumes with the merged batch; `false` queues this message behind it.
|
|
152
152
|
*/
|
|
153
153
|
steer?: boolean;
|
|
154
|
+
/**
|
|
155
|
+
* While a turn from the same person runs: deliver this message into it, read
|
|
156
|
+
* after the step it is on, and the work goes on (Codex-style steer). Wins over
|
|
157
|
+
* `steer`; when the turn cannot take it, the message is queued behind it. A
|
|
158
|
+
* node too old for this ignores the field, so send `steer: false` with it.
|
|
159
|
+
*/
|
|
160
|
+
inject?: boolean;
|
|
161
|
+
/**
|
|
162
|
+
* While the room works, hold this message as a follow-up instead of queuing
|
|
163
|
+
* a turn (`turn-follow-ups` capability; use `holdOrSubmitTurn`). Held, it
|
|
164
|
+
* is not in the transcript and can be edited, moved, deleted or sent now; the
|
|
165
|
+
* node sends held follow-ups one per turn once the room is idle.
|
|
166
|
+
*/
|
|
167
|
+
hold?: boolean;
|
|
154
168
|
/** Run without adding the injected prompt or output to the visible transcript. */
|
|
155
169
|
silent?: boolean;
|
|
156
170
|
attachments?: string[];
|
|
@@ -174,6 +188,39 @@ export interface RuntimeGatewayTurnAcknowledgement {
|
|
|
174
188
|
cursor: number;
|
|
175
189
|
}
|
|
176
190
|
|
|
191
|
+
/** A message held while the room worked (`turn-follow-ups`). */
|
|
192
|
+
export interface RuntimeGatewayFollowUp {
|
|
193
|
+
id: string;
|
|
194
|
+
text: string;
|
|
195
|
+
actorUserId: string;
|
|
196
|
+
attachments: AttachmentDto[];
|
|
197
|
+
createdAt: string;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** A room's held follow-ups, oldest first, and whether they wait after a stop. */
|
|
201
|
+
export interface RuntimeGatewayFollowUpLine {
|
|
202
|
+
topicId: string;
|
|
203
|
+
followUps: RuntimeGatewayFollowUp[];
|
|
204
|
+
paused: boolean;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
export interface RuntimeGatewayHeldTurn {
|
|
208
|
+
ok: true;
|
|
209
|
+
v: typeof RUNTIME_GATEWAY_VERSION;
|
|
210
|
+
accepted: true;
|
|
211
|
+
held: true;
|
|
212
|
+
clientMessageId: string;
|
|
213
|
+
topicId: string;
|
|
214
|
+
followUp: RuntimeGatewayFollowUp;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export interface RuntimeGatewayFollowUpActor {
|
|
218
|
+
/** Canonical principal on the execution node. */
|
|
219
|
+
userId: string;
|
|
220
|
+
/** The person who sent it; only they may change it. Defaults to `userId`. */
|
|
221
|
+
actorUserId?: string;
|
|
222
|
+
}
|
|
223
|
+
|
|
177
224
|
export interface RuntimeGatewayTopicDeriveInput {
|
|
178
225
|
userId: string;
|
|
179
226
|
copyHistory: boolean;
|
|
@@ -346,6 +393,40 @@ function validTurnAck(value: unknown): value is RuntimeGatewayTurnAcknowledgemen
|
|
|
346
393
|
);
|
|
347
394
|
}
|
|
348
395
|
|
|
396
|
+
function validFollowUp(value: unknown): value is RuntimeGatewayFollowUp {
|
|
397
|
+
const item = record(value);
|
|
398
|
+
return Boolean(
|
|
399
|
+
typeof item?.id === "string" &&
|
|
400
|
+
typeof item.text === "string" &&
|
|
401
|
+
typeof item.actorUserId === "string" &&
|
|
402
|
+
Array.isArray(item.attachments) &&
|
|
403
|
+
typeof item.createdAt === "string",
|
|
404
|
+
);
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function validFollowUpLine(value: unknown): value is RuntimeGatewayFollowUpLine {
|
|
408
|
+
const body = record(value);
|
|
409
|
+
return Boolean(
|
|
410
|
+
typeof body?.topicId === "string" &&
|
|
411
|
+
Array.isArray(body.followUps) &&
|
|
412
|
+
body.followUps.every(validFollowUp) &&
|
|
413
|
+
typeof body.paused === "boolean",
|
|
414
|
+
);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function validHeldTurn(value: unknown): value is RuntimeGatewayHeldTurn {
|
|
418
|
+
const body = record(value);
|
|
419
|
+
return Boolean(
|
|
420
|
+
body?.ok === true &&
|
|
421
|
+
body.v === RUNTIME_GATEWAY_VERSION &&
|
|
422
|
+
body.accepted === true &&
|
|
423
|
+
body.held === true &&
|
|
424
|
+
typeof body.clientMessageId === "string" &&
|
|
425
|
+
typeof body.topicId === "string" &&
|
|
426
|
+
validFollowUp(body.followUp),
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
|
|
349
430
|
function validManagerTopic(value: unknown): value is RuntimeGatewayManagerTopic {
|
|
350
431
|
const topic = record(value);
|
|
351
432
|
return Boolean(
|
|
@@ -529,6 +610,122 @@ export class RuntimeGatewayClient {
|
|
|
529
610
|
return body;
|
|
530
611
|
}
|
|
531
612
|
|
|
613
|
+
/**
|
|
614
|
+
* `submitTurn` with `hold: true`: while the room works the node holds the
|
|
615
|
+
* message as a follow-up and answers with it; otherwise it is a turn as usual.
|
|
616
|
+
*/
|
|
617
|
+
async holdOrSubmitTurn(
|
|
618
|
+
input: RuntimeGatewayTurnInput,
|
|
619
|
+
): Promise<RuntimeGatewayTurnAcknowledgement | RuntimeGatewayHeldTurn> {
|
|
620
|
+
const response = await this.send(
|
|
621
|
+
"/turns",
|
|
622
|
+
{
|
|
623
|
+
method: "POST",
|
|
624
|
+
headers: { "content-type": "application/json" },
|
|
625
|
+
body: JSON.stringify({ v: RUNTIME_GATEWAY_VERSION, ...input, hold: true }),
|
|
626
|
+
},
|
|
627
|
+
this.requestTimeoutMs,
|
|
628
|
+
);
|
|
629
|
+
if (response.status !== 202) {
|
|
630
|
+
throw new RuntimeGatewayError(
|
|
631
|
+
"Runtime Gateway turn was not accepted",
|
|
632
|
+
"http",
|
|
633
|
+
response.status,
|
|
634
|
+
);
|
|
635
|
+
}
|
|
636
|
+
const body = await json(response);
|
|
637
|
+
if (validHeldTurn(body) || validTurnAck(body)) return body;
|
|
638
|
+
throw new RuntimeGatewayError(
|
|
639
|
+
"Runtime Gateway turn acknowledgement is incompatible",
|
|
640
|
+
"protocol",
|
|
641
|
+
);
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
async followUps(topicId: string, userId: string): Promise<RuntimeGatewayFollowUpLine> {
|
|
645
|
+
const response = await this.send(
|
|
646
|
+
`/topics/${encodeURIComponent(topicId)}/follow-ups?userId=${encodeURIComponent(userId)}`,
|
|
647
|
+
{ method: "GET" },
|
|
648
|
+
this.requestTimeoutMs,
|
|
649
|
+
);
|
|
650
|
+
return this.followUpLineFrom(response, "list");
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
async editFollowUp(
|
|
654
|
+
topicId: string,
|
|
655
|
+
followUpId: string,
|
|
656
|
+
actor: RuntimeGatewayFollowUpActor,
|
|
657
|
+
text: string,
|
|
658
|
+
): Promise<RuntimeGatewayFollowUpLine> {
|
|
659
|
+
return this.changeFollowUp(topicId, followUpId, "", "PATCH", { ...actor, text });
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
async deleteFollowUp(
|
|
663
|
+
topicId: string,
|
|
664
|
+
followUpId: string,
|
|
665
|
+
actor: RuntimeGatewayFollowUpActor,
|
|
666
|
+
): Promise<RuntimeGatewayFollowUpLine> {
|
|
667
|
+
return this.changeFollowUp(topicId, followUpId, "", "DELETE", { ...actor });
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
async moveFollowUp(
|
|
671
|
+
topicId: string,
|
|
672
|
+
followUpId: string,
|
|
673
|
+
actor: RuntimeGatewayFollowUpActor,
|
|
674
|
+
toIndex: number,
|
|
675
|
+
): Promise<RuntimeGatewayFollowUpLine> {
|
|
676
|
+
return this.changeFollowUp(topicId, followUpId, "/move", "POST", { ...actor, toIndex });
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** 지금 보내기: into the running turn when it can take it, else as a turn of its own. */
|
|
680
|
+
async sendFollowUpNow(
|
|
681
|
+
topicId: string,
|
|
682
|
+
followUpId: string,
|
|
683
|
+
actor: RuntimeGatewayFollowUpActor,
|
|
684
|
+
): Promise<RuntimeGatewayFollowUpLine & { sent: boolean }> {
|
|
685
|
+
const line = await this.changeFollowUp(topicId, followUpId, "/send", "POST", { ...actor });
|
|
686
|
+
return line as RuntimeGatewayFollowUpLine & { sent: boolean };
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
private async changeFollowUp(
|
|
690
|
+
topicId: string,
|
|
691
|
+
followUpId: string,
|
|
692
|
+
suffix: "" | "/move" | "/send",
|
|
693
|
+
method: "PATCH" | "DELETE" | "POST",
|
|
694
|
+
body: Record<string, unknown>,
|
|
695
|
+
): Promise<RuntimeGatewayFollowUpLine> {
|
|
696
|
+
const response = await this.send(
|
|
697
|
+
`/topics/${encodeURIComponent(topicId)}/follow-ups/${encodeURIComponent(followUpId)}${suffix}`,
|
|
698
|
+
{
|
|
699
|
+
method,
|
|
700
|
+
headers: { "content-type": "application/json" },
|
|
701
|
+
body: JSON.stringify({ v: RUNTIME_GATEWAY_VERSION, ...body }),
|
|
702
|
+
},
|
|
703
|
+
this.requestTimeoutMs,
|
|
704
|
+
);
|
|
705
|
+
return this.followUpLineFrom(response, method === "PATCH" ? "edit" : suffix || "delete");
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
private async followUpLineFrom(
|
|
709
|
+
response: Response,
|
|
710
|
+
what: string,
|
|
711
|
+
): Promise<RuntimeGatewayFollowUpLine> {
|
|
712
|
+
if (!response.ok) {
|
|
713
|
+
throw new RuntimeGatewayError(
|
|
714
|
+
`Runtime Gateway follow-up ${what} failed`,
|
|
715
|
+
"http",
|
|
716
|
+
response.status,
|
|
717
|
+
);
|
|
718
|
+
}
|
|
719
|
+
const body = await json(response);
|
|
720
|
+
if (!validFollowUpLine(body)) {
|
|
721
|
+
throw new RuntimeGatewayError(
|
|
722
|
+
`Runtime Gateway follow-up ${what} response is incompatible`,
|
|
723
|
+
"protocol",
|
|
724
|
+
);
|
|
725
|
+
}
|
|
726
|
+
return body;
|
|
727
|
+
}
|
|
728
|
+
|
|
532
729
|
async deriveTopic(
|
|
533
730
|
topicId: string,
|
|
534
731
|
input: RuntimeGatewayTopicDeriveInput,
|