File size: 13,056 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import { normalizeChatType } from "../channels/chat-type.js";
import {
  readAgentRunTerminalOutcome,
  recordAgentRunTerminalOutcome,
} from "../channels/turn/agent-run-terminal-outcome.js";
import { buildAgentSessionKey } from "../routing/resolve-route.js";
import {
  DEFAULT_ACCOUNT_ID,
  isAcpSessionKey,
  normalizeAccountId,
  resolveThreadSessionKeys,
  toAgentRequestSessionKey,
  toAgentStoreSessionKey,
} from "../routing/session-key.js";
import { INTERNAL_MESSAGE_CHANNEL } from "../utils/message-channel-constants.js";
import { withReplyDispatcher } from "./dispatch-dispatcher.js";
import { resolveGroupThreadConfig } from "./group-thread-config.js";
import {
  adoptGroupThreadRoot,
  bindGroupThreadDispatchContext,
  getGroupThreadTurn,
  recordGroupThreadReply,
} from "./group-thread-context.js";
import { runGroupThread } from "./group-thread.js";
import { isReplyPayloadTerminalContent } from "./reply-payload.js";
import { resolveBoundAcpDispatchSessionKey } from "./reply/dispatch-from-config.context.js";
import type {
  DispatchFromConfigParams,
  DispatchFromConfigResult,
  DispatchReplyFromConfig,
} from "./reply/dispatch-from-config.types.js";
import { finalizeInboundContext } from "./reply/inbound-context.js";
import { REPLY_ADMISSION_TICKET } from "./reply/reply-admission-ticket.js";
import {
  createReplyDispatchSettledCounts,
  REPLY_DISPATCH_OUTCOME_COUNTS,
} from "./reply/reply-dispatch-outcome.js";
import { captureReplyDispatchDeliveryOutcome } from "./reply/reply-dispatcher.js";
import {
  mapReplyDispatchCounts,
  type ReplyDispatchKind,
  type ReplyDispatcher,
} from "./reply/reply-dispatcher.types.js";
import { REPLY_OPERATION_RUN_STATE } from "./reply/reply-operation-run-state.js";
import { buildChannelSourceTurnId, setChannelSourceTurnId } from "./reply/source-turn-id.js";
import { withReplySystemEventContext } from "./reply/system-event-session-key.js";
import type { FinalizedMsgContext } from "./templating.js";
import type { ReplyPayload } from "./types.js";

function participantDispatcher(parent: ReplyDispatcher): ReplyDispatcher {
  const queued = { tool: 0, block: 0, final: 0 };
  const counts = {
    tool: createReplyDispatchSettledCounts(),
    block: createReplyDispatchSettledCounts(),
    final: createReplyDispatchSettledCounts(),
  };
  const pending: Promise<void>[] = [];
  const send = (kind: ReplyDispatchKind, payload: ReplyPayload) => {
    const outcome = captureReplyDispatchDeliveryOutcome(payload);
    const accepted =
      kind === "tool"
        ? parent.sendToolResult(payload)
        : kind === "block"
          ? parent.sendBlockReply(payload)
          : parent.sendFinalReply(payload);
    if (accepted) {
      queued[kind]++;
    }
    if (outcome.isTracked()) {
      pending.push(
        outcome.promise.then((result) => {
          counts[kind][REPLY_DISPATCH_OUTCOME_COUNTS[result]]++;
          const delivered = outcome.getDeliveredPayload() ?? payload;
          if (
            kind !== "tool" &&
            result === "delivered" &&
            isReplyPayloadTerminalContent(delivered)
          ) {
            recordGroupThreadReply(delivered);
          }
        }),
      );
    } else if (accepted) {
      counts[kind].delivered++;
      if (kind !== "tool" && isReplyPayloadTerminalContent(payload)) {
        recordGroupThreadReply(payload);
      }
    }
    return accepted;
  };
  return {
    prepareReplyPayload: parent.prepareReplyPayload,
    sendToolResult: (payload) => send("tool", payload),
    sendBlockReply: (payload) => send("block", payload),
    sendFinalReply: (payload) => send("final", payload),
    supportsSettledReceipt: true,
    waitForIdle: async () => {
      await Promise.all(pending);
      return {
        counts,
        anyVisibleDelivered: Object.values(counts).some((value) => value.delivered > 0),
      };
    },
    getQueuedCounts: () => ({ ...queued }),
    getCancelledCounts: () => mapReplyDispatchCounts(counts, (value) => value.cancelled),
    getFailedCounts: () =>
      mapReplyDispatchCounts(counts, (value) => value.failedBeforeSend + value.failedAfterSend),
    // The physical channel dispatcher belongs to the root, not an individual participant.
    markComplete: () => {},
  };
}

function prepareParticipant(
  params: DispatchFromConfigParams,
  sessionKey: string,
): DispatchFromConfigParams {
  const turn = getGroupThreadTurn();
  if (!turn) {
    return params;
  }
  const original = params.replyOptions;
  const signals = [original?.abortSignal, original?.turnAdoptionLifecycle?.abortSignal].filter(
    (signal): signal is AbortSignal => signal !== undefined,
  );
  const ctx = finalizeInboundContext({
    ...params.ctx,
    AgentId: turn.agentId,
    SessionKey: sessionKey,
    RuntimePolicySessionKey: sessionKey,
    ParentSessionKey: undefined,
    ModelParentSessionKey: undefined,
    CommandTargetSessionKey: undefined,
    ...(turn.digest
      ? {
          Body: turn.digest,
          BodyForAgent: turn.digest,
          RawBody: turn.digest,
          CommandBody: turn.digest,
          BodyForCommands: "",
          agentText: turn.digest,
          rawText: turn.digest,
          commandText: "",
          CommandTurn: {
            kind: "normal" as const,
            source: "message" as const,
            authorized: false as const,
          },
          CommandInterpretationSuppressed: true,
          MessageSid: turn.messageId,
          MessageSidFull: params.ctx.MessageSidFull ?? params.ctx.MessageSid,
          // Keep transport reply references anchored to the physical inbound message.
          ReplyToId: params.ctx.ReplyToId ?? params.ctx.MessageSid,
          ReplyToIdFull: params.ctx.ReplyToIdFull ?? params.ctx.MessageSidFull,
        }
      : {}),
  });
  if (turn.digest) {
    setChannelSourceTurnId(
      ctx,
      buildChannelSourceTurnId({
        provider: ctx.OriginatingChannel ?? ctx.Provider ?? ctx.Surface,
        accountId: ctx.AccountId,
        conversationId: ctx.OriginatingTo ?? ctx.To ?? ctx.From,
        messageId: turn.messageId,
      }),
    );
  }
  const runState = bindGroupThreadDispatchContext(ctx);
  return {
    ...params,
    ctx,
    dispatcher: participantDispatcher(params.dispatcher),
    replyOptions: withReplySystemEventContext(
      {
        ...original,
        abortSignal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
        turnAdoptionLifecycle: undefined,
        replyConversation: undefined,
        expectedExistingSessionId: undefined,
        newlyCreatedSessionId: undefined,
        pinExpectedExistingSession: undefined,
        requestedSessionId: undefined,
        resumeRequestedSession: undefined,
        admittedSessionSettings: undefined,
        userTurnTranscriptRecorder: undefined,
        prepareAssistantTranscriptMessage: undefined,
        suppressNextUserMessagePersistence: undefined,
        messageInjectionDisposition: undefined,
        runId: undefined,
        onAgentRunStart: (...args) => {
          runState.runId = args[0];
          runState.executionIdentityToken = args[1];
          return original?.onAgentRunStart?.(...args);
        },
        promptCacheKey: undefined,
        onSessionPrepared: undefined,
        replyOperation: undefined,
        [REPLY_OPERATION_RUN_STATE]: undefined,
        [REPLY_ADMISSION_TICKET]: undefined,
        onQueuedFollowupReplyBatch: undefined,
        onQueuedFollowupAdmitted: undefined,
        onQueuedFollowupSettled: undefined,
        queuedDeliveryCorrelations: undefined,
      },
      { sessionKey },
    ),
  };
}

function resolvePeer(ctx: FinalizedMsgContext, channel: string): string | undefined {
  const canonical = ctx.ConversationRoutePeerId ?? ctx.NativeChannelId;
  if (canonical) {
    return canonical;
  }
  const target = ctx.OriginatingTo ?? ctx.From;
  if (!target) {
    return undefined;
  }
  const unprefixed = target.startsWith(`${channel}:`) ? target.slice(channel.length + 1) : target;
  return unprefixed.replace(/^(?:group|channel):/, "");
}

/** Returns undefined for ordinary single-agent turns, including exclusively bound ACP rooms. */
export async function dispatchGroupThread(
  params: DispatchFromConfigParams,
  dispatch: DispatchReplyFromConfig,
): Promise<DispatchFromConfigResult | undefined> {
  const activeTurn = getGroupThreadTurn();
  if (activeTurn) {
    await adoptGroupThreadRoot(params.replyOptions?.turnAdoptionLifecycle);
    const child = prepareParticipant(params, params.ctx.SessionKey ?? "");
    return withReplyDispatcher({ dispatcher: child.dispatcher, run: () => dispatch(child) });
  }
  const { ctx, cfg } = params;
  if (
    ctx.InternalTurnSource ||
    ctx.Surface === INTERNAL_MESSAGE_CHANNEL ||
    ctx.Provider === INTERNAL_MESSAGE_CHANNEL
  ) {
    return undefined;
  }
  const channel = ctx.OriginatingChannel ?? ctx.Surface ?? ctx.Provider;
  if (!channel || isAcpSessionKey(ctx.SessionKey) || isAcpSessionKey(ctx.CommandTargetSessionKey)) {
    return undefined;
  }
  const peerId = ctx.GroupThread?.peerId ?? resolvePeer(ctx, channel);
  const group =
    ctx.GroupThread?.group ??
    (peerId ? resolveGroupThreadConfig({ cfg, channel, peerId }) : undefined);
  if (!peerId || !group || resolveBoundAcpDispatchSessionKey({ ctx, cfg })) {
    return undefined;
  }
  const accountId = normalizeAccountId(ctx.AccountId);
  const kind = normalizeChatType(ctx.ChatType) ?? "group";
  const signals = [
    params.replyOptions?.abortSignal,
    params.replyOptions?.turnAdoptionLifecycle?.abortSignal,
  ].filter((signal): signal is AbortSignal => signal !== undefined);
  const run = await runGroupThread<DispatchFromConfigResult>({
    cfg,
    group,
    channel,
    accountId,
    peerId,
    threadId: ctx.MessageThreadId,
    messageId: ctx.MessageSid,
    text: ctx.rawText ?? ctx.RawBody ?? ctx.Body ?? "",
    mentionedAgentIds: ctx.GroupThread?.mentionedAgentIds,
    abortSignal: signals.length > 1 ? AbortSignal.any(signals) : signals[0],
    formatReply: params.replyOptions?.groupThreadReplyFormatter,
    runTurn: async (turn) => {
      await adoptGroupThreadRoot(params.replyOptions?.turnAdoptionLifecycle);
      // Direct-message routing may already isolate accounts and chat-qualified topics.
      const resolvedDirectKey = kind === "direct" ? ctx.SessionKey : undefined;
      let sessionKey = resolvedDirectKey
        ? toAgentStoreSessionKey({
            agentId: turn.agentId,
            requestKey: toAgentRequestSessionKey(resolvedDirectKey),
          })
        : buildAgentSessionKey({
            agentId: turn.agentId,
            channel,
            accountId,
            peer: { kind, id: peerId },
            dmScope: ctx.DmScope ?? cfg.session?.dmScope,
            identityLinks: cfg.session?.identityLinks,
          });
      if (kind !== "direct" && accountId !== DEFAULT_ACCOUNT_ID) {
        sessionKey = resolveThreadSessionKeys({
          baseSessionKey: sessionKey,
          threadId: `${channel}-account-${accountId}`,
        }).sessionKey;
      }
      if (!resolvedDirectKey) {
        sessionKey = resolveThreadSessionKeys({
          baseSessionKey: sessionKey,
          threadId: ctx.MessageThreadId?.toString(),
        }).sessionKey;
      }
      const child = prepareParticipant(params, sessionKey);
      return withReplyDispatcher({ dispatcher: child.dispatcher, run: () => dispatch(child) });
    },
  });
  const all = (test: (result: DispatchFromConfigResult) => boolean) =>
    run.results.length > 0 && run.results.every(test);
  const metadata = run.results.flatMap((result) => result.sessionMetadataChanges ?? []);
  const result: DispatchFromConfigResult = {
    queuedFinal: run.results.some((value) => value.queuedFinal),
    counts: run.results.reduce(
      (counts, value) => ({
        tool: counts.tool + value.counts.tool,
        block: counts.block + value.counts.block,
        final: counts.final + value.counts.final,
      }),
      { tool: 0, block: 0, final: 0 },
    ),
    ...(run.results.some((value) => value.observedReplyDelivery)
      ? { observedReplyDelivery: true }
      : {}),
    ...(all((value) => value.sourceReplyDeliveryMode === "message_tool_only")
      ? { sourceReplyDeliveryMode: "message_tool_only" }
      : {}),
    ...(all((value) => value.deliberateSilentTerminalReply === true)
      ? { deliberateSilentTerminalReply: true }
      : {}),
    ...(all((value) => value.sendPolicyDenied === true) ? { sendPolicyDenied: true } : {}),
    ...(all((value) => value.beforeAgentRunBlocked === true)
      ? { beforeAgentRunBlocked: true }
      : {}),
    ...(metadata.length > 0 ? { sessionMetadataChanges: metadata } : {}),
  };
  if (
    run.failedTurns > 0 ||
    run.results.some((value) => readAgentRunTerminalOutcome(value) === "failed")
  ) {
    recordAgentRunTerminalOutcome(result, "failed");
  } else if (run.results.some((value) => readAgentRunTerminalOutcome(value) === "completed")) {
    recordAgentRunTerminalOutcome(result, "completed");
  }
  return result;
}