Skip to content

armgit / crm

Public Imported from GitHub
0

Comp AI CRM is an open source, CRM designed for AI agents. Agentic-first CRM.

TypeScript
989 B
Raw Blame
import type { EveToolOutput } from "@crm/validation/eve-tool";
import type { TranscriptItem } from "./agent-transcript";

export type AnchoredResult<T> = { itemId: string; value: T };

export function anchorResults<T>({
	items,
	tool,
	validate,
	group,
}: {
	items: readonly TranscriptItem[];
	tool: string;
	validate: (output: EveToolOutput) => T | null;
	group?: (
		results: readonly AnchoredResult<T>[],
	) => readonly AnchoredResult<T>[];
}): Map<string, T[]> {
	const valid: AnchoredResult<T>[] = [];

	for (const item of items) {
		if (item.kind !== "did" || item.tool !== tool || item.pending) continue;
		const value = validate(item.output);
		if (value !== null) valid.push({ itemId: item.id, value });
	}

	const anchored = group ? group(valid) : valid;
	const byAnchor = new Map<string, T[]>();

	for (const { itemId, value } of anchored) {
		const bucket = byAnchor.get(itemId);
		if (bucket) bucket.push(value);
		else byAnchor.set(itemId, [value]);
	}

	return byAnchor;
}