171 lines
5.3 KiB
TypeScript
171 lines
5.3 KiB
TypeScript
import type {
|
|
OrchestrationV2ThreadProjection,
|
|
OrchestrationV2ThreadShell,
|
|
ThreadId,
|
|
} from "@t3tools/contracts";
|
|
|
|
export type ThreadRelationshipKind = "parent" | "fork" | "subagent" | "transfer";
|
|
|
|
export interface ThreadRelationshipNode {
|
|
readonly threadId: ThreadId;
|
|
readonly thread: OrchestrationV2ThreadShell | null;
|
|
readonly missing: boolean;
|
|
}
|
|
|
|
export interface ThreadRelationshipEdge {
|
|
readonly sourceThreadId: ThreadId;
|
|
readonly targetThreadId: ThreadId;
|
|
readonly kind: ThreadRelationshipKind;
|
|
readonly status: string | null;
|
|
}
|
|
|
|
export interface ThreadRelationshipGraph {
|
|
readonly nodes: ReadonlyMap<ThreadId, ThreadRelationshipNode>;
|
|
readonly edges: ReadonlyArray<ThreadRelationshipEdge>;
|
|
}
|
|
|
|
export interface ThreadRelationshipWalkRow {
|
|
readonly threadId: ThreadId;
|
|
readonly fromThreadId: ThreadId;
|
|
readonly depth: number;
|
|
readonly edge: ThreadRelationshipEdge;
|
|
}
|
|
|
|
export function resolveMergeBackTargetThreadId(
|
|
projection: Pick<OrchestrationV2ThreadProjection, "thread"> | null,
|
|
): ThreadId | null {
|
|
if (projection?.thread.lineage.relationshipToParent !== "fork") return null;
|
|
return projection.thread.forkedFrom?.type === "run"
|
|
? projection.thread.forkedFrom.threadId
|
|
: projection.thread.lineage.parentThreadId;
|
|
}
|
|
|
|
function edgeKey(edge: ThreadRelationshipEdge): string {
|
|
return `${edge.sourceThreadId}\u001f${edge.targetThreadId}\u001f${edge.kind}`;
|
|
}
|
|
|
|
export function deriveThreadRelationshipGraph(input: {
|
|
readonly threads: ReadonlyArray<OrchestrationV2ThreadShell>;
|
|
readonly projection: OrchestrationV2ThreadProjection | null;
|
|
}): ThreadRelationshipGraph {
|
|
const nodes = new Map<ThreadId, ThreadRelationshipNode>(
|
|
input.threads.map(
|
|
(thread) => [thread.id, { threadId: thread.id, thread, missing: false }] as const,
|
|
),
|
|
);
|
|
const edgesByKey = new Map<string, ThreadRelationshipEdge>();
|
|
const ensureNode = (threadId: ThreadId) => {
|
|
if (!nodes.has(threadId)) {
|
|
nodes.set(threadId, { threadId, thread: null, missing: true });
|
|
}
|
|
};
|
|
const addEdge = (edge: ThreadRelationshipEdge) => {
|
|
ensureNode(edge.sourceThreadId);
|
|
ensureNode(edge.targetThreadId);
|
|
edgesByKey.set(edgeKey(edge), edge);
|
|
};
|
|
|
|
for (const thread of input.threads) {
|
|
const parentThreadId =
|
|
thread.forkedFrom?.type === "run"
|
|
? thread.forkedFrom.threadId
|
|
: thread.lineage.parentThreadId;
|
|
if (parentThreadId === null) continue;
|
|
addEdge({
|
|
sourceThreadId: parentThreadId,
|
|
targetThreadId: thread.id,
|
|
kind: thread.lineage.relationshipToParent === "subagent" ? "subagent" : "fork",
|
|
status: thread.status,
|
|
});
|
|
}
|
|
|
|
if (input.projection !== null) {
|
|
const ownerThreadId = input.projection.thread.id;
|
|
for (const subagent of input.projection.subagents) {
|
|
if (subagent.childThreadId === null) continue;
|
|
addEdge({
|
|
sourceThreadId: ownerThreadId,
|
|
targetThreadId: subagent.childThreadId,
|
|
kind: "subagent",
|
|
status: subagent.status,
|
|
});
|
|
}
|
|
for (const transfer of input.projection.contextTransfers) {
|
|
if (transfer.sourceThreadId === transfer.targetThreadId) continue;
|
|
addEdge({
|
|
sourceThreadId: transfer.sourceThreadId,
|
|
targetThreadId: transfer.targetThreadId,
|
|
kind: "transfer",
|
|
status: transfer.status,
|
|
});
|
|
}
|
|
}
|
|
|
|
return { nodes, edges: [...edgesByKey.values()] };
|
|
}
|
|
|
|
export function relatedThreadIds(
|
|
graph: ThreadRelationshipGraph,
|
|
threadId: ThreadId,
|
|
): ReadonlyArray<ThreadId> {
|
|
const ids = new Set<ThreadId>();
|
|
for (const edge of graph.edges) {
|
|
if (edge.sourceThreadId === threadId) ids.add(edge.targetThreadId);
|
|
if (edge.targetThreadId === threadId) ids.add(edge.sourceThreadId);
|
|
}
|
|
return [...ids];
|
|
}
|
|
|
|
export function walkThreadRelationships(
|
|
graph: ThreadRelationshipGraph,
|
|
threadId: ThreadId,
|
|
): ReadonlyArray<ThreadRelationshipWalkRow> {
|
|
const visited = new Set<ThreadId>([threadId]);
|
|
const pending: Array<{ readonly threadId: ThreadId; readonly depth: number }> = [
|
|
{ threadId, depth: 0 },
|
|
];
|
|
const rows: ThreadRelationshipWalkRow[] = [];
|
|
|
|
for (let index = 0; index < pending.length; index += 1) {
|
|
const current = pending[index];
|
|
if (current === undefined) continue;
|
|
for (const edge of graph.edges) {
|
|
const relatedId =
|
|
edge.sourceThreadId === current.threadId
|
|
? edge.targetThreadId
|
|
: edge.targetThreadId === current.threadId
|
|
? edge.sourceThreadId
|
|
: null;
|
|
if (relatedId === null || visited.has(relatedId)) continue;
|
|
visited.add(relatedId);
|
|
const depth = current.depth + 1;
|
|
rows.push({ threadId: relatedId, fromThreadId: current.threadId, depth, edge });
|
|
pending.push({ threadId: relatedId, depth });
|
|
}
|
|
}
|
|
|
|
return rows;
|
|
}
|
|
|
|
export function immediateThreadRelationships(
|
|
graph: ThreadRelationshipGraph,
|
|
threadId: ThreadId,
|
|
): ReadonlyArray<ThreadRelationshipWalkRow> {
|
|
const visited = new Set<ThreadId>();
|
|
const rows: ThreadRelationshipWalkRow[] = [];
|
|
|
|
for (const edge of graph.edges) {
|
|
const relatedId =
|
|
edge.sourceThreadId === threadId
|
|
? edge.targetThreadId
|
|
: edge.targetThreadId === threadId
|
|
? edge.sourceThreadId
|
|
: null;
|
|
if (relatedId === null || visited.has(relatedId)) continue;
|
|
visited.add(relatedId);
|
|
rows.push({ threadId: relatedId, fromThreadId: threadId, depth: 1, edge });
|
|
}
|
|
|
|
return rows;
|
|
}
|