





















@@ -1,6 +1,7 @@
11import { beforeEach, describe, expect, it } from "vitest";
22import type { FinalizedMsgContext } from "../auto-reply/templating.js";
33import type { OpenClawConfig } from "../config/config.js";
4+import type { DiagnosticTraceContext } from "../infra/diagnostic-trace-context.js";
45import { setActivePluginRegistry } from "../plugins/runtime.js";
56import { createChannelTestPluginBase, createTestRegistry } from "../test-utils/channel-plugins.js";
67import {
@@ -31,6 +32,7 @@ function makeInboundCtx(overrides: Partial<FinalizedMsgContext> = {}): Finalized
3132Surface: "demo-chat",
3233OriginatingChannel: "demo-chat",
3334OriginatingTo: "demo-chat:chat:456",
35+SessionKey: "session-1",
3436AccountId: "acc-1",
3537MessageSid: "msg-1",
3638SenderId: "sender-1",
@@ -141,25 +143,60 @@ describe("message hook mappers", () => {
141143});
142144143145it("maps canonical inbound context to plugin/internal received payloads", () => {
144-const canonical = deriveInboundMessageHookContext(makeInboundCtx({ TopicName: "Deployments" }));
146+const trace: DiagnosticTraceContext = {
147+traceId: "11111111111111111111111111111111",
148+spanId: "2222222222222222",
149+parentSpanId: "3333333333333333",
150+};
151+const canonical = {
152+ ...deriveInboundMessageHookContext(makeInboundCtx({ TopicName: "Deployments" })),
153+runId: "run-1",
154+ trace,
155+callDepth: 2,
156+};
145157146-expect(toPluginMessageContext(canonical)).toEqual({
158+const pluginContext = toPluginMessageContext(canonical);
159+const receivedEvent = toPluginMessageReceivedEvent(canonical);
160+expect(pluginContext).toEqual({
147161channelId: "demo-chat",
148162accountId: "acc-1",
149163conversationId: "demo-chat:chat:456",
164+sessionKey: "session-1",
165+runId: "run-1",
166+messageId: "msg-1",
167+senderId: "sender-1",
168+ trace,
169+traceId: "11111111111111111111111111111111",
170+spanId: "2222222222222222",
171+parentSpanId: "3333333333333333",
172+callDepth: 2,
150173});
151-expect(toPluginMessageReceivedEvent(canonical)).toEqual({
174+expect(pluginContext.trace).not.toBe(trace);
175+expect(pluginContext.trace).toEqual(trace);
176+expect(Object.isFrozen(pluginContext.trace)).toBe(true);
177+expect(receivedEvent).toEqual({
152178from: "demo-chat:user:123",
153179content: "commands-body",
154180timestamp: 1710000000,
155181threadId: 42,
182+messageId: "msg-1",
183+senderId: "sender-1",
184+sessionKey: "session-1",
185+runId: "run-1",
186+ trace,
187+traceId: "11111111111111111111111111111111",
188+spanId: "2222222222222222",
189+parentSpanId: "3333333333333333",
156190metadata: expect.objectContaining({
157191messageId: "msg-1",
158192senderName: "User One",
159193threadId: 42,
160194topicName: "Deployments",
161195}),
162196});
197+expect(receivedEvent.trace).not.toBe(trace);
198+expect(receivedEvent.trace).toEqual(trace);
199+expect(Object.isFrozen(receivedEvent.trace)).toBe(true);
163200expect(toInternalMessageReceivedContext(canonical)).toEqual({
164201from: "demo-chat:user:123",
165202content: "commands-body",
@@ -176,6 +213,39 @@ describe("message hook mappers", () => {
176213});
177214});
178215216+it("passes frozen trace copies to inbound claim and sent plugin hooks", () => {
217+const trace: DiagnosticTraceContext = {
218+traceId: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
219+spanId: "bbbbbbbbbbbbbbbb",
220+parentSpanId: "cccccccccccccccc",
221+traceFlags: "01",
222+};
223+const inbound = {
224+ ...deriveInboundMessageHookContext(makeInboundCtx()),
225+ trace,
226+};
227+const inboundContext = toPluginInboundClaimContext(inbound);
228+const inboundEvent = toPluginInboundClaimEvent(inbound);
229+expect(inboundContext.trace).not.toBe(trace);
230+expect(inboundContext.trace).toEqual(trace);
231+expect(Object.isFrozen(inboundContext.trace)).toBe(true);
232+expect(inboundEvent.trace).not.toBe(trace);
233+expect(inboundEvent.trace).toEqual(trace);
234+expect(Object.isFrozen(inboundEvent.trace)).toBe(true);
235+236+const sent = buildCanonicalSentMessageHookContext({
237+to: "demo-chat:chat:456",
238+content: "reply",
239+success: true,
240+channelId: "demo-chat",
241+ trace,
242+});
243+const sentEvent = toPluginMessageSentEvent(sent);
244+expect(sentEvent.trace).not.toBe(trace);
245+expect(sentEvent.trace).toEqual(trace);
246+expect(Object.isFrozen(sentEvent.trace)).toBe(true);
247+});
248+179249it("uses channel plugin claim resolvers for grouped conversations", () => {
180250const canonical = deriveInboundMessageHookContext(
181251makeInboundCtx({
@@ -193,9 +263,16 @@ describe("message hook mappers", () => {
193263channelId: "claim-chat",
194264accountId: "acc-1",
195265conversationId: "channel:123456789012345678",
266+sessionKey: "session-1",
196267parentConversationId: undefined,
197268senderId: "sender-1",
198269messageId: "msg-1",
270+runId: undefined,
271+trace: undefined,
272+traceId: undefined,
273+spanId: undefined,
274+parentSpanId: undefined,
275+callDepth: undefined,
199276});
200277});
201278@@ -217,9 +294,16 @@ describe("message hook mappers", () => {
217294channelId: "claim-chat",
218295accountId: "acc-1",
219296conversationId: "user:1177378744822943744",
297+sessionKey: "session-1",
220298parentConversationId: undefined,
221299senderId: "sender-1",
222300messageId: "msg-1",
301+runId: undefined,
302+trace: undefined,
303+traceId: undefined,
304+spanId: undefined,
305+parentSpanId: undefined,
306+callDepth: undefined,
223307});
224308});
225309@@ -246,7 +330,9 @@ describe("message hook mappers", () => {
246330error: "network error",
247331channelId: "demo-chat",
248332accountId: "acc-1",
333+sessionKey: "session-1",
249334messageId: "out-1",
335+runId: "run-out-1",
250336isGroup: true,
251337groupId: "demo-chat:chat:456",
252338});
@@ -255,11 +341,17 @@ describe("message hook mappers", () => {
255341channelId: "demo-chat",
256342accountId: "acc-1",
257343conversationId: "demo-chat:chat:456",
344+sessionKey: "session-1",
345+runId: "run-out-1",
346+messageId: "out-1",
258347});
259348expect(toPluginMessageSentEvent(canonical)).toEqual({
260349to: "demo-chat:chat:456",
261350content: "reply",
262351success: false,
352+messageId: "out-1",
353+sessionKey: "session-1",
354+runId: "run-out-1",
263355error: "network error",
264356});
265357expect(toInternalMessageSentContext(canonical)).toEqual({
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。