Merge pull request #2459 from pikasTech/fix/workbench-kafka-session-filter
Pipelines as Code CI / hwlab-nc01-v03-ci-poll- Success

修复 Workbench Kafka 实时事件会话归属
This commit is contained in:
Lyon
2026-07-10 04:16:58 +08:00
committed by GitHub
+17 -10
View File
@@ -1203,39 +1203,46 @@ export const useWorkbenchStore = defineStore("workbench", () => {
return firstNonEmptyString(currentRequest.value?.traceId, activeTraceIdFromMessages(messages.value, turnStatusAuthority.value));
}
function normalizeTraceAuthoritySessionId(value: unknown): string | null {
const sessionId = normalizeWorkbenchSessionId(value);
const match = sessionId?.match(/^ses_agentrun_([0-9a-f]{8})_([0-9a-f]{4})_([0-9a-f]{4})_([0-9a-f]{4})_([0-9a-f]{12})$/iu);
if (!match) return sessionId;
return `ses_${match.slice(1).join("-").toLowerCase()}`;
}
function realtimeEventSessionId(event: WorkbenchRealtimeEvent): string | null {
const turn = recordValue(event.turn);
const snapshot = recordValue(event.snapshot);
const traceEvent = recordValue(event.event);
return normalizeWorkbenchSessionId(firstNonEmptyString(event.sessionId, turn?.sessionId, snapshot?.sessionId, traceEvent?.sessionId));
return normalizeTraceAuthoritySessionId(firstNonEmptyString(event.sessionId, turn?.sessionId, snapshot?.sessionId, traceEvent?.sessionId));
}
function traceResultSessionId(result: AgentChatResultResponse | TraceSnapshot | Record<string, unknown> | null | undefined): string | null {
const value = recordValue(result);
const runnerTrace = recordValue(value?.runnerTrace);
const session = recordValue(value?.session);
return normalizeWorkbenchSessionId(firstNonEmptyString(value?.sessionId, runnerTrace?.sessionId, session?.sessionId));
return normalizeTraceAuthoritySessionId(firstNonEmptyString(value?.sessionId, runnerTrace?.sessionId, session?.sessionId));
}
function messageSessionAuthority(message: ChatMessage | null | undefined): string | null {
return normalizeWorkbenchSessionId(firstNonEmptyString(message?.sessionId, message?.runnerTrace?.sessionId));
return normalizeTraceAuthoritySessionId(firstNonEmptyString(message?.sessionId, message?.runnerTrace?.sessionId));
}
function traceOwnerSessionId(traceId: string | null | undefined, authoritySessionId: string | null | undefined): string | null {
const sessionId = normalizeWorkbenchSessionId(authoritySessionId);
const sessionId = normalizeTraceAuthoritySessionId(authoritySessionId);
if (sessionId) return sessionId;
const id = firstNonEmptyString(traceId);
if (!id) return activeSessionId.value;
for (const [candidateSessionId, candidateMessages] of Object.entries(serverState.value.messagesBySessionId)) {
if (candidateMessages.some((message) => firstNonEmptyString(message.traceId, message.runnerTrace?.traceId) === id)) return normalizeWorkbenchSessionId(candidateSessionId);
if (candidateMessages.some((message) => firstNonEmptyString(message.traceId, message.runnerTrace?.traceId) === id)) return normalizeTraceAuthoritySessionId(candidateSessionId);
}
return activeSessionId.value;
}
function messageMatchesTraceAuthority(message: ChatMessage, traceId: string, authoritySessionId: string | null | undefined, ownerSessionId: string | null | undefined): boolean {
if (message.role !== "agent" || firstNonEmptyString(message.traceId, message.runnerTrace?.traceId) !== traceId) return false;
const sessionId = normalizeWorkbenchSessionId(authoritySessionId);
const ownerId = normalizeWorkbenchSessionId(ownerSessionId);
const sessionId = normalizeTraceAuthoritySessionId(authoritySessionId);
const ownerId = normalizeTraceAuthoritySessionId(ownerSessionId);
const messageSessionId = messageSessionAuthority(message);
if (sessionId && ownerId && sessionId !== ownerId) return false;
if (messageSessionId && ownerId && messageSessionId !== ownerId) return false;
@@ -1246,7 +1253,7 @@ export const useWorkbenchStore = defineStore("workbench", () => {
function shouldApplyActiveTraceAuthority(traceId: string | null | undefined, authoritySessionId: string | null | undefined): boolean {
const id = firstNonEmptyString(traceId);
const activeId = activeSessionId.value;
const sessionId = normalizeWorkbenchSessionId(authoritySessionId);
const sessionId = normalizeTraceAuthoritySessionId(authoritySessionId);
if (!activeId) return false;
if (sessionId && sessionId !== activeId) return false;
if (!id) return Boolean(sessionId && sessionId === activeId);
@@ -1254,7 +1261,7 @@ export const useWorkbenchStore = defineStore("workbench", () => {
const messageSessionId = messageSessionAuthority(message);
if (messageSessionId && messageSessionId !== activeId) return false;
if (sessionId && messageSessionId && sessionId !== messageSessionId) return false;
const knownSessionId = normalizeWorkbenchSessionId(firstNonEmptyString(turnStatusAuthority.value[id]?.sessionId, traceAuthorityById.value[id]?.sessionId));
const knownSessionId = normalizeTraceAuthoritySessionId(firstNonEmptyString(turnStatusAuthority.value[id]?.sessionId, traceAuthorityById.value[id]?.sessionId));
if (knownSessionId && knownSessionId !== activeId) return false;
if (sessionId && knownSessionId && sessionId !== knownSessionId) return false;
return Boolean(sessionId || messageSessionId || knownSessionId || message);
@@ -1263,7 +1270,7 @@ export const useWorkbenchStore = defineStore("workbench", () => {
function shouldApplyTraceToMessage(message: ChatMessage, traceId: string, authoritySessionId: string | null | undefined): boolean {
if (message.role !== "agent" || firstNonEmptyString(message.traceId, message.runnerTrace?.traceId) !== traceId) return false;
const activeId = activeSessionId.value;
const sessionId = normalizeWorkbenchSessionId(authoritySessionId);
const sessionId = normalizeTraceAuthoritySessionId(authoritySessionId);
const messageSessionId = messageSessionAuthority(message);
if (activeId && messageSessionId && messageSessionId !== activeId) return false;
if (sessionId && activeId && sessionId !== activeId) return false;