chat-ui-agent-demo / src /lib /components /agent /agentQueueMutations.ts
Mike0021's picture
deploy: production agent journey fixes
6f07329 verified
Raw History Blame Contribute Delete
2.11 kB
import type { MessageAgentQueueUpdate } from "$lib/types/MessageUpdate";
import type {
AgentQueueMutation,
AgentQueueReplacement,
AgentQueueSnapshot,
} from "./activityTypes";
export interface AgentQueueMutationTarget {
queue: AgentQueueMutation["queue"];
index: number;
value: string;
expected: AgentQueueSnapshot;
}
function cloneSnapshot(snapshot: AgentQueueSnapshot): AgentQueueSnapshot {
return {
steering: [...snapshot.steering],
followUp: [...snapshot.followUp],
};
}
export function snapshotAgentQueue(
update: Pick<MessageAgentQueueUpdate, "steering" | "followUp">
): AgentQueueSnapshot {
return {
steering: [...update.steering],
followUp: [...update.followUp],
};
}
export function captureAgentQueueMutationTarget(
update: Pick<MessageAgentQueueUpdate, "steering" | "followUp">,
queue: AgentQueueMutation["queue"],
index: number
): AgentQueueMutationTarget | undefined {
const expected = snapshotAgentQueue(update);
const value = expected[queue][index];
if (typeof value !== "string") return;
return { queue, index, value, expected };
}
export function agentQueueMatchesSnapshot(
update: Pick<MessageAgentQueueUpdate, "steering" | "followUp">,
expected: AgentQueueSnapshot
): boolean {
return (
update.steering.length === expected.steering.length &&
update.followUp.length === expected.followUp.length &&
update.steering.every((value, index) => value === expected.steering[index]) &&
update.followUp.every((value, index) => value === expected.followUp[index])
);
}
export function createAgentQueueReplacement(
target: AgentQueueMutationTarget,
action: AgentQueueMutation["action"],
value?: string
): AgentQueueReplacement | undefined {
if (target.expected[target.queue][target.index] !== target.value) return;
if (action === "edit" && typeof value !== "string") return;
const expected = cloneSnapshot(target.expected);
const replacement = cloneSnapshot(target.expected);
if (action === "remove") replacement[target.queue].splice(target.index, 1);
else replacement[target.queue][target.index] = value as string;
return { expected, replacement };
}