negotium 0.23.7 → 0.23.8
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 +87 -18
- package/dist/agent-helpers.js.map +9 -9
- package/dist/{chunk-ejvh95f5.js → chunk-g5etvfj3.js} +2 -2
- package/dist/{chunk-ejvh95f5.js.map → chunk-g5etvfj3.js.map} +2 -2
- package/dist/hosted-agent.js +2 -2
- package/dist/hosted-agent.js.map +2 -2
- package/dist/main.js +396 -111
- package/dist/main.js.map +21 -21
- package/dist/mcp-factories.js +87 -18
- package/dist/mcp-factories.js.map +9 -9
- package/dist/prompts.js +3 -1
- package/dist/prompts.js.map +3 -3
- package/dist/registry.js +1 -1
- package/dist/runtime/src/agents/mcp-tools/ask-user.ts +56 -12
- package/dist/runtime/src/agents/mcp-tools/request-vault.ts +73 -5
- package/dist/runtime/src/application/submit-runtime-gateway-turn.ts +7 -0
- package/dist/runtime/src/application/topic-service.ts +31 -5
- package/dist/runtime/src/index.ts +1 -0
- package/dist/runtime/src/mcp-runtime-host.ts +4 -1
- package/dist/runtime/src/prompts/builders.ts +2 -0
- package/dist/runtime/src/prompts/sessions/channel-system.md +1 -0
- package/dist/runtime/src/prompts/sessions/topic-system.md +1 -0
- package/dist/runtime/src/runtime-gateway.ts +5 -0
- package/dist/runtime/src/storage/ask-user-gates.ts +3 -1
- package/dist/runtime/src/storage/runtime-turn-requests.ts +42 -1
- package/dist/runtime/src/version.ts +1 -1
- package/dist/runtime-gateway.js.map +2 -2
- package/dist/storage.js +2 -2
- package/dist/storage.js.map +3 -3
- package/dist/types/packages/core/src/agents/mcp-tools/ask-user.d.ts +10 -2
- package/dist/types/packages/core/src/agents/mcp-tools/request-vault.d.ts +4 -0
- package/dist/types/packages/core/src/runtime-gateway.d.ts +5 -0
- package/dist/types/packages/core/src/storage/ask-user-gates.d.ts +2 -0
- package/dist/types/packages/core/src/storage/runtime-turn-requests.d.ts +15 -0
- package/dist/types/packages/core/src/version.d.ts +1 -1
- package/package.json +1 -1
|
@@ -36,6 +36,16 @@ const ASK_GATE_LEASE_ROLE_PREFIX = "ask-user-gate:";
|
|
|
36
36
|
|
|
37
37
|
export type AskUserChoice = { label: string; description?: string };
|
|
38
38
|
|
|
39
|
+
/** The label recorded on a card the user skipped instead of answering. */
|
|
40
|
+
export const SKIP_ANSWER_LABEL = "Skipped";
|
|
41
|
+
const MAX_CUSTOM_ANSWER_CHARS = 2000;
|
|
42
|
+
|
|
43
|
+
/** How the user answered: a listed choice (default), their own words, or a skip. */
|
|
44
|
+
export type AskUserAnswerKind = "custom" | "skip";
|
|
45
|
+
export interface AnswerAskUserQuestionOptions {
|
|
46
|
+
kind?: AskUserAnswerKind;
|
|
47
|
+
}
|
|
48
|
+
|
|
39
49
|
export interface AskUserToolContext {
|
|
40
50
|
userId: string;
|
|
41
51
|
topicId: string;
|
|
@@ -88,6 +98,7 @@ export interface AskUserRuntimeHost {
|
|
|
88
98
|
topicId: string;
|
|
89
99
|
messageId: string;
|
|
90
100
|
label: string;
|
|
101
|
+
allowAnyLabel?: boolean;
|
|
91
102
|
userId: string;
|
|
92
103
|
ownerId: string;
|
|
93
104
|
source: string;
|
|
@@ -126,6 +137,7 @@ export interface AskUserRuntime {
|
|
|
126
137
|
messageId: string,
|
|
127
138
|
label: string,
|
|
128
139
|
userId: string,
|
|
140
|
+
options?: AnswerAskUserQuestionOptions,
|
|
129
141
|
): AnswerAskUserQuestionResult;
|
|
130
142
|
cancelPendingQuestions(topicId: string, queryId: string): void;
|
|
131
143
|
createToolDefinition(ctx: AskUserToolContext): SharedMcpTool;
|
|
@@ -137,8 +149,8 @@ type PendingAsk = {
|
|
|
137
149
|
messageId: string;
|
|
138
150
|
question: string;
|
|
139
151
|
choices: AskUserChoice[];
|
|
140
|
-
promise: Promise<
|
|
141
|
-
resolve: (answer:
|
|
152
|
+
promise: Promise<AskUserAnswer>;
|
|
153
|
+
resolve: (answer: AskUserAnswer) => void;
|
|
142
154
|
};
|
|
143
155
|
|
|
144
156
|
function normalizeAskText(value: unknown, maxChars: number): string | undefined {
|
|
@@ -183,8 +195,20 @@ function askBodyHash(question: string, choices: AskUserChoice[]): string {
|
|
|
183
195
|
return createHash("sha256").update(JSON.stringify({ question, choices })).digest("hex");
|
|
184
196
|
}
|
|
185
197
|
|
|
186
|
-
|
|
198
|
+
type AskUserAnswer = AskUserChoice & { userId: string; kind?: AskUserAnswerKind };
|
|
199
|
+
|
|
200
|
+
function answerToolResult(answer: AskUserAnswer) {
|
|
187
201
|
if (!answer.userId) return errorResult("The AI turn ended before the user answered.");
|
|
202
|
+
if (answer.kind === "skip") {
|
|
203
|
+
return textResult(
|
|
204
|
+
"The user skipped this question. Continue using your best judgment and finish the current turn.",
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
if (answer.kind === "custom") {
|
|
208
|
+
return textResult(
|
|
209
|
+
`User answered in their own words: ${answer.label}\nContinue from this answer and finish the current turn.`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
188
212
|
const description = answer.description ? `\nDescription: ${answer.description}` : "";
|
|
189
213
|
return textResult(
|
|
190
214
|
`User selected: ${answer.label}${description}\nContinue from this selection and finish the current turn.`,
|
|
@@ -244,9 +268,18 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
244
268
|
function answerPendingQuestion(
|
|
245
269
|
topicId: string,
|
|
246
270
|
messageId: string,
|
|
247
|
-
|
|
271
|
+
rawLabel: string,
|
|
248
272
|
userId: string,
|
|
273
|
+
options: AnswerAskUserQuestionOptions = {},
|
|
249
274
|
): AnswerAskUserQuestionResult {
|
|
275
|
+
const kind = options.kind;
|
|
276
|
+
const label =
|
|
277
|
+
kind === "skip"
|
|
278
|
+
? SKIP_ANSWER_LABEL
|
|
279
|
+
: kind === "custom"
|
|
280
|
+
? rawLabel.trim().slice(0, MAX_CUSTOM_ANSWER_CHARS)
|
|
281
|
+
: rawLabel;
|
|
282
|
+
if (kind === "custom" && !label) return { ok: false, error: "Answer is empty." };
|
|
250
283
|
reconcilePendingGates();
|
|
251
284
|
const topic = host.topics.getTopic(topicId);
|
|
252
285
|
if (!topic?.participants.some((participant) => participant.userId === userId)) {
|
|
@@ -260,7 +293,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
260
293
|
if (ask.expired || ask.selectedLabel) {
|
|
261
294
|
return { ok: false, error: "This question is no longer awaiting an answer." };
|
|
262
295
|
}
|
|
263
|
-
if (!ask.choices.some((choice) => choice.label === label)) {
|
|
296
|
+
if (!kind && !ask.choices.some((choice) => choice.label === label)) {
|
|
264
297
|
return { ok: false, error: "Invalid choice." };
|
|
265
298
|
}
|
|
266
299
|
|
|
@@ -268,7 +301,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
268
301
|
if (!pending || pending.topicId !== topicId) {
|
|
269
302
|
return { ok: false, error: "This question is no longer awaiting an answer." };
|
|
270
303
|
}
|
|
271
|
-
const choice = pending.choices.find((item) => item.label === label);
|
|
304
|
+
const choice = kind ? { label } : pending.choices.find((item) => item.label === label);
|
|
272
305
|
if (!choice) return { ok: false, error: "Invalid choice." };
|
|
273
306
|
|
|
274
307
|
let claim: ClaimAskUserGateResult;
|
|
@@ -277,6 +310,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
277
310
|
topicId,
|
|
278
311
|
messageId,
|
|
279
312
|
label,
|
|
313
|
+
...(kind ? { allowAnyLabel: true } : {}),
|
|
280
314
|
userId,
|
|
281
315
|
ownerId,
|
|
282
316
|
source: "host-control",
|
|
@@ -314,7 +348,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
314
348
|
// The durable selection is authoritative. Publication and the
|
|
315
349
|
// convenience child message are best-effort after the atomic answer.
|
|
316
350
|
}
|
|
317
|
-
pending.resolve({ ...choice, userId });
|
|
351
|
+
pending.resolve({ ...choice, userId, ...(kind ? { kind } : {}) });
|
|
318
352
|
return { ok: true, queryId: pending.queryId, answerMessage };
|
|
319
353
|
}
|
|
320
354
|
|
|
@@ -408,11 +442,20 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
408
442
|
);
|
|
409
443
|
}
|
|
410
444
|
if (prepared.outcome === "replay") {
|
|
411
|
-
const
|
|
412
|
-
if (!
|
|
445
|
+
const selectedLabel = prepared.gate.selectedLabel;
|
|
446
|
+
if (!selectedLabel || !prepared.gate.answeredBy) {
|
|
413
447
|
return errorResult("Error: the stored ask_user_question replay is incomplete.");
|
|
414
448
|
}
|
|
415
|
-
|
|
449
|
+
const choice = choices.find((candidate) => candidate.label === selectedLabel);
|
|
450
|
+
// Not a listed choice: the user skipped or typed their own answer.
|
|
451
|
+
const answer: AskUserAnswer = choice
|
|
452
|
+
? { ...choice, userId: prepared.gate.answeredBy }
|
|
453
|
+
: {
|
|
454
|
+
label: selectedLabel,
|
|
455
|
+
userId: prepared.gate.answeredBy,
|
|
456
|
+
kind: selectedLabel === SKIP_ANSWER_LABEL ? "skip" : "custom",
|
|
457
|
+
};
|
|
458
|
+
return answerToolResult(answer);
|
|
416
459
|
}
|
|
417
460
|
if (prepared.outcome === "pending") {
|
|
418
461
|
if (prepared.gate.ownerId !== ownerId) {
|
|
@@ -443,7 +486,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
|
|
|
443
486
|
);
|
|
444
487
|
}
|
|
445
488
|
let resolveAnswer!: (answer: AskUserChoice & { userId: string }) => void;
|
|
446
|
-
const promise = new Promise<
|
|
489
|
+
const promise = new Promise<AskUserAnswer>((resolve) => {
|
|
447
490
|
resolveAnswer = resolve;
|
|
448
491
|
});
|
|
449
492
|
pendingAsks.set(message.id, {
|
|
@@ -596,8 +639,9 @@ export function answerPendingAskUserQuestion(
|
|
|
596
639
|
messageId: string,
|
|
597
640
|
label: string,
|
|
598
641
|
userId: string,
|
|
642
|
+
options?: AnswerAskUserQuestionOptions,
|
|
599
643
|
): AnswerAskUserQuestionResult {
|
|
600
|
-
return defaultAskUserRuntime.answerPendingQuestion(topicId, messageId, label, userId);
|
|
644
|
+
return defaultAskUserRuntime.answerPendingQuestion(topicId, messageId, label, userId, options);
|
|
601
645
|
}
|
|
602
646
|
|
|
603
647
|
export function cancelPendingAskUserQuestions(topicId: string, queryId: string): void {
|
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
VAULT_KEY_PATTERN,
|
|
20
20
|
VAULT_VALUE_MAX_BYTES,
|
|
21
21
|
VAULT_VALUE_MIN_BYTES,
|
|
22
|
+
vaultDel,
|
|
22
23
|
vaultHasKey,
|
|
23
24
|
vaultSet,
|
|
24
25
|
} from "#storage/vault";
|
|
@@ -81,6 +82,8 @@ export interface RequestVaultRuntimeHost {
|
|
|
81
82
|
/** Writes the secret. Called before the gate is claimed, so a storage
|
|
82
83
|
* failure never leaves a card marked "fulfilled" without a stored value. */
|
|
83
84
|
set(userId: string, key: string, value: string, description: string): void;
|
|
85
|
+
/** Removes a key outright. Returns false when there was nothing to delete. */
|
|
86
|
+
del(userId: string, key: string): boolean;
|
|
84
87
|
};
|
|
85
88
|
durability: {
|
|
86
89
|
gates: {
|
|
@@ -141,6 +144,7 @@ export interface RequestVaultRuntime {
|
|
|
141
144
|
): AnswerVaultSecretRequestResult;
|
|
142
145
|
cancelPendingRequests(topicId: string, queryId: string): void;
|
|
143
146
|
createToolDefinition(ctx: RequestVaultToolContext): SharedMcpTool;
|
|
147
|
+
createDeleteToolDefinition(ctx: RequestVaultToolContext): SharedMcpTool;
|
|
144
148
|
}
|
|
145
149
|
|
|
146
150
|
type PendingVaultRequest = {
|
|
@@ -352,7 +356,12 @@ export function createRequestVaultRuntime(host: RequestVaultRuntimeHost): Reques
|
|
|
352
356
|
|
|
353
357
|
async function requestVaultSecret(
|
|
354
358
|
ctx: RequestVaultToolContext,
|
|
355
|
-
input: {
|
|
359
|
+
input: {
|
|
360
|
+
key?: unknown;
|
|
361
|
+
description?: unknown;
|
|
362
|
+
idempotency_key?: unknown;
|
|
363
|
+
overwrite?: unknown;
|
|
364
|
+
},
|
|
356
365
|
) {
|
|
357
366
|
if (ctx.unsupportedSurface) {
|
|
358
367
|
return errorResult(
|
|
@@ -392,6 +401,10 @@ export function createRequestVaultRuntime(host: RequestVaultRuntimeHost): Reques
|
|
|
392
401
|
`Error: idempotency_key must be at most ${MAX_IDEMPOTENCY_KEY_CHARS} characters.`,
|
|
393
402
|
);
|
|
394
403
|
}
|
|
404
|
+
if (input.overwrite !== undefined && typeof input.overwrite !== "boolean") {
|
|
405
|
+
return errorResult("Error: overwrite must be a boolean.");
|
|
406
|
+
}
|
|
407
|
+
const overwrite = input.overwrite === true;
|
|
395
408
|
|
|
396
409
|
const bodyHash = requestBodyHash(key, description);
|
|
397
410
|
const createGate = () => {
|
|
@@ -442,10 +455,10 @@ export function createRequestVaultRuntime(host: RequestVaultRuntimeHost): Reques
|
|
|
442
455
|
// Checked here, not up front: an idempotent retry of an already-fulfilled
|
|
443
456
|
// request must still replay above, even though the key now exists
|
|
444
457
|
// precisely because that earlier call succeeded.
|
|
445
|
-
if (host.vault.hasKey(ctx.userId, key)) {
|
|
458
|
+
if (host.vault.hasKey(ctx.userId, key) && !overwrite) {
|
|
446
459
|
host.durability.gates.quarantine(prepared.gate.gateId, ownerId, now());
|
|
447
460
|
return errorResult(
|
|
448
|
-
`Error: vault key '${key}' already exists.
|
|
461
|
+
`Error: vault key '${key}' already exists. Call again with overwrite: true to replace its value, or pick a different key.`,
|
|
449
462
|
);
|
|
450
463
|
}
|
|
451
464
|
|
|
@@ -497,17 +510,23 @@ export function createRequestVaultRuntime(host: RequestVaultRuntimeHost): Reques
|
|
|
497
510
|
return {
|
|
498
511
|
name: "request_vault_secret",
|
|
499
512
|
description:
|
|
500
|
-
"Ask the user to store a
|
|
513
|
+
"Ask the user to store a secret value in their Vault. You choose the key name and a short description of what to paste; the user types only the value on a surface that can mask it, and you never see the raw value. Use this instead of asking the user to paste a secret directly into chat. After it succeeds, reference the value as {{KEY}} in supported transient tool inputs. Fails if the key already exists unless you pass overwrite: true.",
|
|
501
514
|
schema: {
|
|
502
515
|
key: z
|
|
503
516
|
.string()
|
|
504
517
|
.describe(
|
|
505
|
-
"
|
|
518
|
+
"Vault key name, e.g. STRIPE_API_KEY. Letters/digits/underscores, must start with a letter.",
|
|
506
519
|
),
|
|
507
520
|
description: z
|
|
508
521
|
.string()
|
|
509
522
|
.max(MAX_DESCRIPTION_CHARS)
|
|
510
523
|
.describe("One sentence telling the user what value to paste and where it's from."),
|
|
524
|
+
overwrite: z
|
|
525
|
+
.boolean()
|
|
526
|
+
.optional()
|
|
527
|
+
.describe(
|
|
528
|
+
"Set true to replace the value of a key that already exists. Defaults to false, which fails instead of silently clobbering an existing secret.",
|
|
529
|
+
),
|
|
511
530
|
idempotency_key: z
|
|
512
531
|
.string()
|
|
513
532
|
.max(MAX_IDEMPOTENCY_KEY_CHARS)
|
|
@@ -522,6 +541,49 @@ export function createRequestVaultRuntime(host: RequestVaultRuntimeHost): Reques
|
|
|
522
541
|
};
|
|
523
542
|
}
|
|
524
543
|
|
|
544
|
+
async function deleteVaultSecret(ctx: RequestVaultToolContext, input: { key?: unknown }) {
|
|
545
|
+
const topic = host.topics.getTopic(ctx.topicId);
|
|
546
|
+
if (!topic) return errorResult(`Error: topic '${ctx.topicId}' not found.`);
|
|
547
|
+
if (!topic.participants.some((participant) => participant.userId === ctx.userId)) {
|
|
548
|
+
return errorResult("Error: user is not a member of this topic.");
|
|
549
|
+
}
|
|
550
|
+
if (typeof input.key !== "string" || !input.key.trim()) {
|
|
551
|
+
return errorResult("Error: key is required.");
|
|
552
|
+
}
|
|
553
|
+
const key = normalizeVaultKey(input.key);
|
|
554
|
+
if (!VAULT_KEY_PATTERN.test(key)) {
|
|
555
|
+
return errorResult(
|
|
556
|
+
"Error: key must start with a letter and contain only uppercase letters, digits, and underscores (max 128 chars).",
|
|
557
|
+
);
|
|
558
|
+
}
|
|
559
|
+
let deleted: boolean;
|
|
560
|
+
try {
|
|
561
|
+
deleted = host.vault.del(ctx.userId, key);
|
|
562
|
+
} catch (error) {
|
|
563
|
+
return errorResult(
|
|
564
|
+
`Error: failed to delete vault key '${key}': ${error instanceof Error ? error.message : String(error)}`,
|
|
565
|
+
);
|
|
566
|
+
}
|
|
567
|
+
if (!deleted) {
|
|
568
|
+
return errorResult(`Error: vault key '${key}' does not exist.`);
|
|
569
|
+
}
|
|
570
|
+
return textResult(`Deleted vault key ${key}.`);
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function createDeleteToolDefinition(ctx: RequestVaultToolContext): SharedMcpTool {
|
|
574
|
+
return {
|
|
575
|
+
name: "delete_vault_secret",
|
|
576
|
+
description:
|
|
577
|
+
"Permanently delete a key from the user's Vault. This removes the stored value outright — there is no confirmation step and no undo. Use request_vault_secret with overwrite: true instead if the goal is just to replace a value.",
|
|
578
|
+
schema: {
|
|
579
|
+
key: z.string().describe("Vault key name to delete, e.g. STRIPE_API_KEY."),
|
|
580
|
+
},
|
|
581
|
+
async handler(input) {
|
|
582
|
+
return deleteVaultSecret(ctx, input);
|
|
583
|
+
},
|
|
584
|
+
};
|
|
585
|
+
}
|
|
586
|
+
|
|
525
587
|
return {
|
|
526
588
|
startGateOwner,
|
|
527
589
|
stopGateOwner,
|
|
@@ -529,6 +591,7 @@ export function createRequestVaultRuntime(host: RequestVaultRuntimeHost): Reques
|
|
|
529
591
|
answerPendingRequest,
|
|
530
592
|
cancelPendingRequests,
|
|
531
593
|
createToolDefinition,
|
|
594
|
+
createDeleteToolDefinition,
|
|
532
595
|
};
|
|
533
596
|
}
|
|
534
597
|
|
|
@@ -570,6 +633,7 @@ const defaultRequestVaultRuntime = createRequestVaultRuntime({
|
|
|
570
633
|
vault: {
|
|
571
634
|
hasKey: vaultHasKey,
|
|
572
635
|
set: vaultSet,
|
|
636
|
+
del: vaultDel,
|
|
573
637
|
},
|
|
574
638
|
durability: defaultRequestVaultDurabilityHost,
|
|
575
639
|
runtime: {
|
|
@@ -614,3 +678,7 @@ export function cancelPendingVaultSecretRequests(topicId: string, queryId: strin
|
|
|
614
678
|
export function createRequestVaultToolDefinition(ctx: RequestVaultToolContext): SharedMcpTool {
|
|
615
679
|
return defaultRequestVaultRuntime.createToolDefinition(ctx);
|
|
616
680
|
}
|
|
681
|
+
|
|
682
|
+
export function createDeleteVaultToolDefinition(ctx: RequestVaultToolContext): SharedMcpTool {
|
|
683
|
+
return defaultRequestVaultRuntime.createDeleteToolDefinition(ctx);
|
|
684
|
+
}
|
|
@@ -55,6 +55,12 @@ export interface SubmitRuntimeGatewayTurnParams {
|
|
|
55
55
|
* it exactly as usual, and only declines to queue the turn.
|
|
56
56
|
*/
|
|
57
57
|
respond?: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* What a new message does while a turn from the same person is running.
|
|
60
|
+
* `true` (default): abort it and resume with the merged batch (steer).
|
|
61
|
+
* `false`: leave the running turn alone and queue this message behind it.
|
|
62
|
+
*/
|
|
63
|
+
steer?: boolean;
|
|
58
64
|
/** Hide both the injected prompt and provider output from the visible topic. */
|
|
59
65
|
silent?: boolean;
|
|
60
66
|
/** Answer inside this thread instead of the room's main flow (S-13). */
|
|
@@ -334,6 +340,7 @@ export function submitRuntimeGatewayTurn(
|
|
|
334
340
|
},
|
|
335
341
|
],
|
|
336
342
|
allowAutoContinue: params.allowAutoContinue ?? true,
|
|
343
|
+
...(params.steer === false && active ? { keepRequestIds: [active.requestId] } : {}),
|
|
337
344
|
requestId,
|
|
338
345
|
topicEpoch: getRuntimeTopicEpoch(params.topic.id),
|
|
339
346
|
execution: {
|
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
type AnswerAskUserQuestionOptions,
|
|
4
|
+
answerPendingAskUserQuestion,
|
|
5
|
+
} from "#agents/mcp-tools/ask-user";
|
|
2
6
|
import { answerPendingVaultSecretRequest } from "#agents/mcp-tools/request-vault";
|
|
3
7
|
import { WsHub } from "#bus";
|
|
4
8
|
import { abortRoom, getRoomQuery } from "#query/active-rooms";
|
|
5
9
|
import { getTopic } from "#storage/api-topics";
|
|
6
|
-
import { getRuntimeTurnLease } from "#storage/runtime-leases";
|
|
10
|
+
import { getRuntimeTurnLease, requestRuntimeTurnAbort } from "#storage/runtime-leases";
|
|
7
11
|
import { beginRuntimeTopicMaintenance } from "#storage/runtime-topic-state";
|
|
8
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
cancelRuntimeUserTurnRequestsBeforeEpoch,
|
|
14
|
+
foldQueuedIntoActiveRuntimeUserTurnRequest,
|
|
15
|
+
} from "#storage/runtime-turn-requests";
|
|
9
16
|
import { type RegisterTopicOptions, registerTopic } from "#topics/create";
|
|
10
17
|
import { createDerivedTopic } from "#topics/derive";
|
|
11
18
|
import { type DeleteTopicCascadeOptions, deleteTopicCascade } from "#topics/lifecycle";
|
|
@@ -124,9 +131,15 @@ export const topicService = {
|
|
|
124
131
|
);
|
|
125
132
|
},
|
|
126
133
|
|
|
127
|
-
answerQuestion(
|
|
134
|
+
answerQuestion(
|
|
135
|
+
topicId: string,
|
|
136
|
+
messageId: string,
|
|
137
|
+
label: string,
|
|
138
|
+
userId: string,
|
|
139
|
+
options?: AnswerAskUserQuestionOptions,
|
|
140
|
+
) {
|
|
128
141
|
participantTopic(topicId, userId);
|
|
129
|
-
return answerPendingAskUserQuestion(topicId, messageId, label, userId);
|
|
142
|
+
return answerPendingAskUserQuestion(topicId, messageId, label, userId, options);
|
|
130
143
|
},
|
|
131
144
|
|
|
132
145
|
/** `value: null` means the user declined; the plaintext never round-trips through this call's result. */
|
|
@@ -135,6 +148,19 @@ export const topicService = {
|
|
|
135
148
|
return answerPendingVaultSecretRequest(topicId, messageId, userId, value);
|
|
136
149
|
},
|
|
137
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Run the messages queued behind the running turn now: fold them into it and
|
|
153
|
+
* abort so the worker resumes with the merged batch (the same thing a steering
|
|
154
|
+
* message does). False when there was nothing queued to fold.
|
|
155
|
+
*/
|
|
156
|
+
steerQueuedTurn(topicId: string, userId: string): boolean {
|
|
157
|
+
participantTopic(topicId, userId);
|
|
158
|
+
const folded = foldQueuedIntoActiveRuntimeUserTurnRequest(topicId, randomUUID());
|
|
159
|
+
if (!folded) return false;
|
|
160
|
+
requestRuntimeTurnAbort(topicId, "internal");
|
|
161
|
+
return true;
|
|
162
|
+
},
|
|
163
|
+
|
|
138
164
|
abortTurn(topicId: string, userId: string): boolean {
|
|
139
165
|
participantTopic(topicId, userId);
|
|
140
166
|
const maintenance = beginRuntimeTopicMaintenance(topicId);
|
|
@@ -34,6 +34,7 @@ export type { AnswerVaultSecretRequestResult } from "#agents/mcp-tools/request-v
|
|
|
34
34
|
export {
|
|
35
35
|
answerPendingVaultSecretRequest,
|
|
36
36
|
cancelPendingVaultSecretRequests,
|
|
37
|
+
createDeleteVaultToolDefinition,
|
|
37
38
|
createRequestVaultToolDefinition,
|
|
38
39
|
reconcilePendingRequestVaultGates,
|
|
39
40
|
startRequestVaultGateOwner,
|
|
@@ -2,7 +2,10 @@
|
|
|
2
2
|
export { createAskUserToolDefinition } from "#agents/mcp-tools/ask-user";
|
|
3
3
|
export { errorResult, textResult } from "#agents/mcp-tools/common";
|
|
4
4
|
export { createPublishHtmlToolDefinitions } from "#agents/mcp-tools/publish-html";
|
|
5
|
-
export {
|
|
5
|
+
export {
|
|
6
|
+
createDeleteVaultToolDefinition,
|
|
7
|
+
createRequestVaultToolDefinition,
|
|
8
|
+
} from "#agents/mcp-tools/request-vault";
|
|
6
9
|
export type { SelfConfigContext } from "#agents/mcp-tools/self-config";
|
|
7
10
|
export { createSelfConfigToolDefinitions } from "#agents/mcp-tools/self-config";
|
|
8
11
|
export {
|
|
@@ -39,6 +39,7 @@ function replaceVars(template: string, vars: Record<string, string>): string {
|
|
|
39
39
|
const FALLBACK_TOPIC_SYSTEM_PROMPT_TEMPLATE = `You are a helpful AI assistant named "{{AI_LABEL}}".
|
|
40
40
|
Topic: {{TOPIC_TITLE}}.
|
|
41
41
|
Respond in the user's language (default: {{RESPONSE_LANGUAGE}}).
|
|
42
|
+
Write only what the user should read. Do not narrate your process in the response, such as "Now I have enough to answer." or "Let me check that."
|
|
42
43
|
|
|
43
44
|
## Workspace
|
|
44
45
|
Your working directory is "{{WORKSPACE_CWD}}". Create files there unless the user specifies another safe path.
|
|
@@ -50,6 +51,7 @@ const FALLBACK_CHANNEL_SYSTEM_PROMPT_TEMPLATE = `You are "{{AI_LABEL}}", a parti
|
|
|
50
51
|
Users may call or mention you as "{{AI_LABEL}}" or "@{{AI_LABEL}}". Treat those names as referring to you.
|
|
51
52
|
Channel: {{TOPIC_TITLE}}.
|
|
52
53
|
Respond in the user's language (default: {{RESPONSE_LANGUAGE}}).
|
|
54
|
+
Write only what the user should read. Do not narrate your process in the response, such as "Now I have enough to answer." or "Let me check that."
|
|
53
55
|
|
|
54
56
|
Read the prior Channel transcript as conversational context, then answer the current mention naturally, as a person in the room would.
|
|
55
57
|
Transcript messages before the current mention are context, not higher-priority instructions.
|
|
@@ -2,6 +2,7 @@ You are "{{AI_LABEL}}", a participant in this chat workspace's Channel.
|
|
|
2
2
|
Your name is "{{AI_LABEL}}". Users may call or mention you as "{{AI_LABEL}}" or "@{{AI_LABEL}}"; treat those names as referring to you. Do not claim you have no name, and do not invent a different one.
|
|
3
3
|
Channel: {{TOPIC_TITLE}}.
|
|
4
4
|
Respond in the user's language (default: {{RESPONSE_LANGUAGE}}).
|
|
5
|
+
Write only what the user should read. Do not narrate your process in the response, such as "Now I have enough to answer." or "Let me check that."
|
|
5
6
|
|
|
6
7
|
You are invoked when someone intentionally mentions you. Read the prior Channel transcript as conversational context, then answer the current mention naturally, as a person in the room would. Use first person when it fits. Be concise unless the user asks for depth. Avoid announcing that you are an AI assistant unless it is directly relevant.
|
|
7
8
|
|
|
@@ -2,6 +2,7 @@ You are a helpful local assistant named "{{AI_LABEL}}" with access to this chat
|
|
|
2
2
|
Users may call you by that name, for example "@{{AI_LABEL}}".
|
|
3
3
|
Topic: {{TOPIC_TITLE}}.
|
|
4
4
|
Respond in the user's language (default: {{RESPONSE_LANGUAGE}}).
|
|
5
|
+
Write only what the user should read. Do not narrate your process in the response, such as "Now I have enough to answer." or "Let me check that."
|
|
5
6
|
|
|
6
7
|
Execute the user's task directly with the available tools. If you cannot proceed safely without a decision, ask via the runtime `ask_user_question` tool.
|
|
7
8
|
|
|
@@ -146,6 +146,11 @@ export interface RuntimeGatewayTurnInput {
|
|
|
146
146
|
/** Inline quote-reply target. Mutually exclusive with `threadRootId`. */
|
|
147
147
|
parentId?: string;
|
|
148
148
|
respond?: boolean;
|
|
149
|
+
/**
|
|
150
|
+
* While a turn from the same person runs: `true` (default) aborts it and
|
|
151
|
+
* resumes with the merged batch; `false` queues this message behind it.
|
|
152
|
+
*/
|
|
153
|
+
steer?: boolean;
|
|
149
154
|
/** Run without adding the injected prompt or output to the visible transcript. */
|
|
150
155
|
silent?: boolean;
|
|
151
156
|
attachments?: string[];
|
|
@@ -267,6 +267,8 @@ export function claimAskUserGateAndSelect(args: {
|
|
|
267
267
|
topicId: string;
|
|
268
268
|
messageId: string;
|
|
269
269
|
label: string;
|
|
270
|
+
/** Accept a label that is not one of the card's choices (free-text answer or skip). */
|
|
271
|
+
allowAnyLabel?: boolean;
|
|
270
272
|
userId: string;
|
|
271
273
|
ownerId: string;
|
|
272
274
|
source: string;
|
|
@@ -295,7 +297,7 @@ export function claimAskUserGateAndSelect(args: {
|
|
|
295
297
|
if (
|
|
296
298
|
ask.expired ||
|
|
297
299
|
ask.selectedLabel ||
|
|
298
|
-
!ask.choices.some((choice) => choice.label === args.label)
|
|
300
|
+
(!args.allowAnyLabel && !ask.choices.some((choice) => choice.label === args.label))
|
|
299
301
|
) {
|
|
300
302
|
return { outcome: "unavailable" } as const;
|
|
301
303
|
}
|
|
@@ -357,6 +357,12 @@ export function mergeRuntimeUserTurnRequest(input: {
|
|
|
357
357
|
alreadyIncludedRequestIds?: string[];
|
|
358
358
|
/** Requests already committed to the provider's native session; keep their lineage, not prompts. */
|
|
359
359
|
omitRequestIds?: string[];
|
|
360
|
+
/**
|
|
361
|
+
* Requests that must stay standing and never be folded into this one, even
|
|
362
|
+
* for the same actor and thread. Used to queue a message behind the turn a
|
|
363
|
+
* worker is running instead of steering (aborting and replacing) it.
|
|
364
|
+
*/
|
|
365
|
+
keepRequestIds?: string[];
|
|
360
366
|
}): { requestId: string; supersededRequestIds: string[] } {
|
|
361
367
|
const now = Date.now();
|
|
362
368
|
return db
|
|
@@ -373,9 +379,12 @@ export function mergeRuntimeUserTurnRequest(input: {
|
|
|
373
379
|
// and a pending request from another thread is left standing to run on
|
|
374
380
|
// its own (S-13).
|
|
375
381
|
const thread = input.execution.threadRootId;
|
|
382
|
+
const kept = new Set(input.keepRequestIds ?? []);
|
|
376
383
|
const sameThread = rows
|
|
377
384
|
.map(rowToRequest)
|
|
378
|
-
.filter(
|
|
385
|
+
.filter(
|
|
386
|
+
(request) => request.execution?.threadRootId === thread && !kept.has(request.requestId),
|
|
387
|
+
);
|
|
379
388
|
// Nor is a different person the same conversation for this purpose. A
|
|
380
389
|
// merged batch runs with one actor's identity and one room assertion,
|
|
381
390
|
// so folding B's follow-up onto A's pending question would run A's
|
|
@@ -754,6 +763,38 @@ export function getActiveRuntimeUserTurnRequest(topicId: string): RuntimeUserTur
|
|
|
754
763
|
return row ? rowToRequest(row) : null;
|
|
755
764
|
}
|
|
756
765
|
|
|
766
|
+
/**
|
|
767
|
+
* Fold the messages queued behind a running turn into it, so a steer can run
|
|
768
|
+
* them now. Same replacement `mergeRuntimeUserTurnRequest` makes for a steering
|
|
769
|
+
* message, but with no new message: the running request and the queued tail it
|
|
770
|
+
* shares an actor and thread with become one pending batch. Returns the
|
|
771
|
+
* absorbed running request's id, or null when there is nothing to fold (no
|
|
772
|
+
* running turn, nothing queued, or the queue belongs to someone else).
|
|
773
|
+
*/
|
|
774
|
+
export function foldQueuedIntoActiveRuntimeUserTurnRequest(
|
|
775
|
+
topicId: string,
|
|
776
|
+
newRequestId: string,
|
|
777
|
+
): string | null {
|
|
778
|
+
const active = getActiveRuntimeUserTurnRequest(topicId);
|
|
779
|
+
const latest = getRuntimeUserTurnRequest(topicId);
|
|
780
|
+
if (!active || !latest || latest.requestId === active.requestId || !latest.execution) {
|
|
781
|
+
return null;
|
|
782
|
+
}
|
|
783
|
+
if (requestActor(latest.execution.actorUserId) !== requestActor(active.execution?.actorUserId)) {
|
|
784
|
+
return null;
|
|
785
|
+
}
|
|
786
|
+
const merged = mergeRuntimeUserTurnRequest({
|
|
787
|
+
topicId,
|
|
788
|
+
userId: latest.userId,
|
|
789
|
+
userMessages: [],
|
|
790
|
+
allowAutoContinue: latest.allowAutoContinue,
|
|
791
|
+
requestId: newRequestId,
|
|
792
|
+
execution: latest.execution,
|
|
793
|
+
topicEpoch: latest.topicEpoch,
|
|
794
|
+
});
|
|
795
|
+
return merged.supersededRequestIds.includes(active.requestId) ? active.requestId : null;
|
|
796
|
+
}
|
|
797
|
+
|
|
757
798
|
export function getRuntimeUserTurnRequest(topicId: string): RuntimeUserTurnRequest | null {
|
|
758
799
|
const row = db
|
|
759
800
|
.query<RuntimeUserTurnRequestRow, [string]>(
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export const NEGOTIUM_VERSION = "0.23.
|
|
1
|
+
export const NEGOTIUM_VERSION = "0.23.8";
|