@otium-team/negotium 0.24.0 → 0.24.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/registry.js CHANGED
@@ -133,7 +133,7 @@ var codexRegistryOperations = {
133
133
  return { sessionId: threadId, rolloutPath };
134
134
  },
135
135
  async forkSession({ parentSessionId }) {
136
- const { forkCodexSession } = await import("./chunk-3r4xfhkx.js");
136
+ const { forkCodexSession } = await import("./chunk-kfckfff2.js");
137
137
  return await forkCodexSession(parentSessionId);
138
138
  },
139
139
  async cleanupRollouts({ sessionIds }) {
@@ -44,6 +44,20 @@ const MAX_CUSTOM_ANSWER_CHARS = 2000;
44
44
  export type AskUserAnswerKind = "custom" | "skip";
45
45
  export interface AnswerAskUserQuestionOptions {
46
46
  kind?: AskUserAnswerKind;
47
+ /**
48
+ * Who and what the recorded answer message stands in for. A chat message
49
+ * that answers the card keeps its own author and source ids, so the host
50
+ * that sent it recognises the answer as its message.
51
+ */
52
+ answerMessage?: Partial<
53
+ Pick<MessageDto, "authorId" | "authorName" | "sourceAdapter" | "sourceMessageId">
54
+ >;
55
+ }
56
+
57
+ export interface OpenQuestionAnswerOptions {
58
+ /** The flow the message was typed in; a card is answered from the flow it sits in. */
59
+ threadRootId?: string;
60
+ answerMessage?: AnswerAskUserQuestionOptions["answerMessage"];
47
61
  }
48
62
 
49
63
  export interface AskUserToolContext {
@@ -139,6 +153,12 @@ export interface AskUserRuntime {
139
153
  userId: string,
140
154
  options?: AnswerAskUserQuestionOptions,
141
155
  ): AnswerAskUserQuestionResult;
156
+ answerOpenQuestionWithText(
157
+ topicId: string,
158
+ text: string,
159
+ userId: string,
160
+ options?: OpenQuestionAnswerOptions,
161
+ ): AnswerAskUserQuestionResult | null;
142
162
  cancelPendingQuestions(topicId: string, queryId: string): void;
143
163
  createToolDefinition(ctx: AskUserToolContext): SharedMcpTool;
144
164
  }
@@ -206,7 +226,7 @@ function answerToolResult(answer: AskUserAnswer) {
206
226
  }
207
227
  if (answer.kind === "custom") {
208
228
  return textResult(
209
- `User answered in their own words: ${answer.label}\nContinue from this answer and finish the current turn.`,
229
+ `The user did not pick a listed option. They typed this reply in the chat instead: "${answer.label}"\nTreat it as their free-text answer to your question and read it for what it says. If it hands the decision back to you (for example "decide yourself" or "you handle it"), choose the best option yourself and go on. Continue from this answer and finish the current turn.`,
210
230
  );
211
231
  }
212
232
  const description = answer.description ? `\nDescription: ${answer.description}` : "";
@@ -328,6 +348,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
328
348
  id: createId(),
329
349
  topicId,
330
350
  authorId: userId,
351
+ ...options.answerMessage,
331
352
  text: label,
332
353
  parentId: messageId,
333
354
  // Follow the card rather than the caller: whoever clicks the button is
@@ -352,6 +373,29 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
352
373
  return { ok: true, queryId: pending.queryId, answerMessage };
353
374
  }
354
375
 
376
+ /**
377
+ * A chat message typed while a card of the running turn is still open
378
+ * answers that card in the user's own words. Null when no card is open in
379
+ * this flow, so the message goes the usual way.
380
+ */
381
+ function answerOpenQuestionWithText(
382
+ topicId: string,
383
+ text: string,
384
+ userId: string,
385
+ options: OpenQuestionAnswerOptions = {},
386
+ ): AnswerAskUserQuestionResult | null {
387
+ for (const [messageId, pending] of pendingAsks) {
388
+ if (pending.topicId !== topicId) continue;
389
+ const card = host.messaging.persistence.getMessage(topicId, messageId);
390
+ if ((card?.threadRootId ?? undefined) !== (options.threadRootId ?? undefined)) continue;
391
+ return answerPendingQuestion(topicId, messageId, text, userId, {
392
+ kind: "custom",
393
+ ...(options.answerMessage ? { answerMessage: options.answerMessage } : {}),
394
+ });
395
+ }
396
+ return null;
397
+ }
398
+
355
399
  function cancelPendingQuestions(topicId: string, queryId: string): void {
356
400
  for (const [messageId, pending] of pendingAsks) {
357
401
  if (pending.topicId === topicId && pending.queryId === queryId) {
@@ -551,6 +595,7 @@ export function createAskUserRuntime(host: AskUserRuntimeHost): AskUserRuntime {
551
595
  reconcilePendingGates,
552
596
  hasPendingQuestion,
553
597
  answerPendingQuestion,
598
+ answerOpenQuestionWithText,
554
599
  cancelPendingQuestions,
555
600
  createToolDefinition,
556
601
  };
@@ -644,6 +689,16 @@ export function answerPendingAskUserQuestion(
644
689
  return defaultAskUserRuntime.answerPendingQuestion(topicId, messageId, label, userId, options);
645
690
  }
646
691
 
692
+ /** Settle the open ask card of this room's running turn with a typed chat message. */
693
+ export function answerOpenAskUserQuestionWithText(
694
+ topicId: string,
695
+ text: string,
696
+ userId: string,
697
+ options?: OpenQuestionAnswerOptions,
698
+ ): AnswerAskUserQuestionResult | null {
699
+ return defaultAskUserRuntime.answerOpenQuestionWithText(topicId, text, userId, options);
700
+ }
701
+
647
702
  export function cancelPendingAskUserQuestions(topicId: string, queryId: string): void {
648
703
  defaultAskUserRuntime.cancelPendingQuestions(topicId, queryId);
649
704
  }
@@ -1,4 +1,5 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
+ import { answerOpenAskUserQuestionWithText } from "#agents/mcp-tools/ask-user";
2
3
  import { hostMcpGrantRefusal, isReservedRuntimeMcpServerName } from "#platform/mcp-config";
3
4
  import { abortRoom, getRoomQuery } from "#query/active-rooms";
4
5
  import { AbortReason } from "#query/types";
@@ -246,6 +247,73 @@ export class RuntimeGatewayTopicUnavailableError extends Error {
246
247
  }
247
248
  }
248
249
 
250
+ /**
251
+ * A message typed while the running turn waits on an `ask_user_question` card
252
+ * in the same flow is that card's answer, in the person's own words. Queuing it
253
+ * as a turn would leave it behind the card until the card times out, with the
254
+ * room looking frozen. Plain text only: a message with attachments, a quote, or
255
+ * no AI response keeps the normal path, as does a sender who may not answer the
256
+ * card (the same participant rule as pressing a button). Null means "not an
257
+ * answer"; the caller carries on as before.
258
+ */
259
+ export function answerOpenAskWithMessage(
260
+ params: SubmitRuntimeGatewayTurnParams,
261
+ ): SubmitRuntimeGatewayTurnResult | null {
262
+ const text = params.text.trim();
263
+ if (
264
+ !text ||
265
+ params.respond === false ||
266
+ params.silent === true ||
267
+ params.parentId ||
268
+ params.attachments?.length ||
269
+ isTopicClaimAbortFenced(params.topic.id)
270
+ ) {
271
+ return null;
272
+ }
273
+ const requestId = params.requestId ?? params.clientMessageId;
274
+ const actorUserId = params.actorUserId ?? params.userId;
275
+ const sourceAdapter = params.sourceAdapter?.trim() || "runtime-gateway";
276
+ const answered = answerOpenAskUserQuestionWithText(params.topic.id, text, params.userId, {
277
+ ...(params.threadRootId ? { threadRootId: params.threadRootId } : {}),
278
+ answerMessage: {
279
+ authorId: actorUserId,
280
+ ...(params.actorLabel ? { authorName: params.actorLabel } : {}),
281
+ sourceAdapter,
282
+ sourceMessageId: params.clientMessageId,
283
+ },
284
+ });
285
+ if (!answered?.ok) return null;
286
+
287
+ const message = answered.answerMessage;
288
+ const submission: RuntimeGatewaySubmission = {
289
+ clientMessageId: params.clientMessageId,
290
+ requestId,
291
+ topicId: params.topic.id,
292
+ messageId: message.id,
293
+ userId: params.userId,
294
+ createdAt: message.createdAt,
295
+ ackCursor: 0,
296
+ messageCursor: 0,
297
+ payloadHash: gatewayPayloadHash(params, requestId, actorUserId),
298
+ };
299
+ db.transaction(() => {
300
+ const acceptedEvent = appendRuntimeEvent("runtime-gateway-ingress", {
301
+ type: "ai-status",
302
+ topicId: params.topic.id,
303
+ payload: {
304
+ kind: "turn_accepted",
305
+ requestId,
306
+ clientMessageId: params.clientMessageId,
307
+ messageId: message.id,
308
+ delivery: "answered",
309
+ },
310
+ });
311
+ submission.ackCursor = acceptedEvent.seq;
312
+ recordRuntimeGatewaySubmission(submission);
313
+ })();
314
+ return { ...submission, message, deduplicated: false };
315
+ }
316
+
249
317
  export function submitRuntimeGatewayTurn(
250
318
  params: SubmitRuntimeGatewayTurnParams,
251
319
  ): SubmitRuntimeGatewayTurnResult {
@@ -276,6 +344,9 @@ export function submitRuntimeGatewayTurn(
276
344
  // atomically, by the `api_messages` trigger inside the transaction below.
277
345
  if (isTopicClaimAbortFenced(params.topic.id)) throw new RuntimeGatewayTopicUnavailableError();
278
346
 
347
+ const answeredAsk = answerOpenAskWithMessage(params);
348
+ if (answeredAsk) return answeredAsk;
349
+
279
350
  // Remember what this adapter grants for the room, so the turns that never
280
351
  // see an adapter — tell/ask, cron, auto-continue, subagent reports — inherit
281
352
  // it instead of silently running without the tools.
@@ -36,6 +36,7 @@ import { isRuntimeTopicMaintenance } from "#storage/runtime-topic-state";
36
36
  import { getRuntimeUserTurnRequest } from "#storage/runtime-turn-requests";
37
37
  import type { AttachmentDto } from "#types/api";
38
38
  import {
39
+ answerOpenAskWithMessage,
39
40
  type SubmitRuntimeGatewayTurnParams,
40
41
  type SubmitRuntimeGatewayTurnResult,
41
42
  submitRuntimeGatewayTurn,
@@ -116,6 +117,12 @@ export function holdOrSubmitRuntimeGatewayTurn(
116
117
  const alreadySent = findRuntimeGatewaySubmission(params.clientMessageId, requestId) !== null;
117
118
  const waiting = listRuntimeFollowUps(topicId).length > 0;
118
119
  const paused = areRuntimeFollowUpsPaused(topicId);
120
+ // An open question is waiting for exactly this: answer it instead of holding
121
+ // the message behind the question until the question times out.
122
+ if (!alreadySent) {
123
+ const answered = answerOpenAskWithMessage(params);
124
+ if (answered) return { held: false, submission: answered };
125
+ }
119
126
  const holdable = !alreadySent && params.respond !== false && params.silent !== true;
120
127
  if (holdable && (isRoomWorking(topicId) || (waiting && !paused))) {
121
128
  const { topic: _topic, text, ...rest } = params;
@@ -1 +1 @@
1
- export const NEGOTIUM_VERSION = "0.24.0";
1
+ export const NEGOTIUM_VERSION = "0.24.1";
@@ -13,6 +13,17 @@ export declare const SKIP_ANSWER_LABEL = "Skipped";
13
13
  export type AskUserAnswerKind = "custom" | "skip";
14
14
  export interface AnswerAskUserQuestionOptions {
15
15
  kind?: AskUserAnswerKind;
16
+ /**
17
+ * Who and what the recorded answer message stands in for. A chat message
18
+ * that answers the card keeps its own author and source ids, so the host
19
+ * that sent it recognises the answer as its message.
20
+ */
21
+ answerMessage?: Partial<Pick<MessageDto, "authorId" | "authorName" | "sourceAdapter" | "sourceMessageId">>;
22
+ }
23
+ export interface OpenQuestionAnswerOptions {
24
+ /** The flow the message was typed in; a card is answered from the flow it sits in. */
25
+ threadRootId?: string;
26
+ answerMessage?: AnswerAskUserQuestionOptions["answerMessage"];
16
27
  }
17
28
  export interface AskUserToolContext {
18
29
  userId: string;
@@ -100,6 +111,7 @@ export interface AskUserRuntime {
100
111
  reconcilePendingGates(): number;
101
112
  hasPendingQuestion(topicId: string, messageId: string): boolean;
102
113
  answerPendingQuestion(topicId: string, messageId: string, label: string, userId: string, options?: AnswerAskUserQuestionOptions): AnswerAskUserQuestionResult;
114
+ answerOpenQuestionWithText(topicId: string, text: string, userId: string, options?: OpenQuestionAnswerOptions): AnswerAskUserQuestionResult | null;
103
115
  cancelPendingQuestions(topicId: string, queryId: string): void;
104
116
  createToolDefinition(ctx: AskUserToolContext): SharedMcpTool;
105
117
  }
@@ -143,5 +155,7 @@ export declare function hasPendingAskUserQuestion(topicId: string, messageId: st
143
155
  * same provider turn.
144
156
  */
145
157
  export declare function answerPendingAskUserQuestion(topicId: string, messageId: string, label: string, userId: string, options?: AnswerAskUserQuestionOptions): AnswerAskUserQuestionResult;
158
+ /** Settle the open ask card of this room's running turn with a typed chat message. */
159
+ export declare function answerOpenAskUserQuestionWithText(topicId: string, text: string, userId: string, options?: OpenQuestionAnswerOptions): AnswerAskUserQuestionResult | null;
146
160
  export declare function cancelPendingAskUserQuestions(topicId: string, queryId: string): void;
147
161
  export declare function createAskUserToolDefinition(ctx: AskUserToolContext): SharedMcpTool;
@@ -1 +1 @@
1
- export declare const NEGOTIUM_VERSION = "0.24.0";
1
+ export declare const NEGOTIUM_VERSION = "0.24.1";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@otium-team/negotium",
3
- "version": "0.24.0",
3
+ "version": "0.24.1",
4
4
  "type": "module",
5
5
  "description": "Install the Negotium multi-agent runtime and CLI with one package",
6
6
  "license": "Apache-2.0",