





















@@ -2596,16 +2596,19 @@ export const chatHandlers: GatewayRequestHandlers = {
25962596const preparedUserTurnMediaPromise =
25972597normalizedAttachments.length > 0 ? getPersistedMediaForTranscript() : Promise.resolve([]);
25982598const userTurnMediaPromise = preparedUserTurnMediaPromise.then(buildChatSendUserTurnMedia);
2599-const userTurnInputPromise: Promise<UserTurnInput> = userTurnMediaPromise.then((media) => ({
2599+const baseUserTurnInput: UserTurnInput = {
26002600text: rawMessage,
2601+timestamp: now,
2602+idempotencyKey: `${clientRunId}:user`,
2603+};
2604+const userTurnInputPromise: Promise<UserTurnInput> = userTurnMediaPromise.then((media) => ({
2605+ ...baseUserTurnInput,
26012606 ...(media.length > 0
26022607 ? {
26032608 media,
26042609mediaOnlyText: "[User sent media without caption]",
26052610}
26062611 : {}),
2607-timestamp: now,
2608-idempotencyKey: `${clientRunId}:user`,
26092612}));
26102613const pluginBoundMediaFieldsPromise =
26112614explicitOriginTargetsPlugin && parsedImages.length > 0
@@ -2701,37 +2704,33 @@ export const chatHandlers: GatewayRequestHandlers = {
27012704const deliveredReplies: Array<{ payload: ReplyPayload; kind: "block" | "final" }> = [];
27022705let appendedWebchatAgentMedia = false;
27032706let agentRunStarted = false;
2704-const userTurnRecorderPromise: Promise<UserTurnTranscriptRecorder> =
2705-userTurnInputPromise.then((input) =>
2706-createUserTurnTranscriptRecorder({
2707- input,
2708-target: () => {
2709-const { storePath: latestStorePath, entry: latestEntry } =
2710-loadSessionEntry(sessionKey);
2711-const resolvedSessionId = latestEntry?.sessionId ?? backingSessionId;
2712-if (!resolvedSessionId) {
2713-return undefined;
2714-}
2715-return {
2716-sessionId: resolvedSessionId,
2717- sessionKey,
2718-sessionEntry: latestEntry ?? entry,
2719-storePath: latestStorePath,
2720- agentId,
2721-config: cfg,
2722-};
2723-},
2724-errorContext: "gateway chat user turn transcript",
2725-beforeMessageWrite: runAgentHarnessBeforeMessageWriteHook,
2726-onPersistenceError: (error) => {
2727-context.logGateway.warn(
2728-`gateway user transcript persistence failed: ${formatForLog(error)}`,
2729-);
2730-},
2731-}),
2732-);
2707+const userTurnRecorder: UserTurnTranscriptRecorder = createUserTurnTranscriptRecorder({
2708+input: baseUserTurnInput,
2709+resolveInput: () => userTurnInputPromise,
2710+target: () => {
2711+const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(sessionKey);
2712+const resolvedSessionId = latestEntry?.sessionId ?? backingSessionId;
2713+if (!resolvedSessionId) {
2714+return undefined;
2715+}
2716+return {
2717+sessionId: resolvedSessionId,
2718+ sessionKey,
2719+sessionEntry: latestEntry ?? entry,
2720+storePath: latestStorePath,
2721+ agentId,
2722+config: cfg,
2723+};
2724+},
2725+errorContext: "gateway chat user turn transcript",
2726+beforeMessageWrite: runAgentHarnessBeforeMessageWriteHook,
2727+onPersistenceError: (error) => {
2728+context.logGateway.warn(
2729+`gateway user transcript persistence failed: ${formatForLog(error)}`,
2730+);
2731+},
2732+});
27332733const persistGatewayUserTurnTranscript = async () => {
2734-const userTurnRecorder = await userTurnRecorderPromise;
27352734await measureDiagnosticsTimelineSpan(
27362735"gateway.chat_send.persist_user_transcript",
27372736async () => {
@@ -2842,7 +2841,6 @@ export const chatHandlers: GatewayRequestHandlers = {
28422841},
28432842deliver: async (payload, info) => {
28442843if (getReplyPayloadMetadata(payload)?.beforeAgentRunBlocked === true) {
2845-const userTurnRecorder = await userTurnRecorderPromise;
28462844userTurnRecorder.markBlocked();
28472845}
28482846switch (info.kind) {
@@ -2870,7 +2868,6 @@ export const chatHandlers: GatewayRequestHandlers = {
28702868"gateway.chat_send.dispatch_inbound",
28712869async () => {
28722870applyChatSendManagedMediaFields(ctx, await pluginBoundMediaFieldsPromise);
2873-const userTurnRecorder = await userTurnRecorderPromise;
28742871const dispatchResult = await dispatchInboundMessage({
28752872 ctx,
28762873 cfg,
@@ -2915,7 +2912,6 @@ export const chatHandlers: GatewayRequestHandlers = {
29152912},
29162913});
29172914if (dispatchResult.beforeAgentRunBlocked === true) {
2918-const userTurnRecorder = await userTurnRecorderPromise;
29192915userTurnRecorder.markBlocked();
29202916}
29212917return dispatchResult;
@@ -2940,7 +2936,6 @@ export const chatHandlers: GatewayRequestHandlers = {
29402936.map((payload) => payload.text?.trim())
29412937.filter((text): text is string => Boolean(text))
29422938.join(" | ") || undefined;
2943-const userTurnRecorder = await userTurnRecorderPromise;
29442939if (
29452940agentRunStarted &&
29462941returnedAgentErrorPayloads.length > 0 &&
@@ -3501,7 +3496,6 @@ export const chatHandlers: GatewayRequestHandlers = {
35013496);
35023497})
35033498.catch(async (err) => {
3504-const userTurnRecorder = await userTurnRecorderPromise;
35053499const emitAfterError =
35063500userTurnRecorder.hasPersisted() || userTurnRecorder.isBlocked()
35073501 ? Promise.resolve()
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。