import type { AgentFeature } from "@getpaseo/protocol/agent-types"; export function pruneFeatureValues( featureValues: Record, features: AgentFeature[], ): Record { const allowedFeatureIds = new Set(features.map((feature) => feature.id)); let changed = false; const next: Record = {}; for (const [featureId, value] of Object.entries(featureValues)) { if (!allowedFeatureIds.has(featureId)) { changed = true; continue; } next[featureId] = value; } return changed ? next : featureValues; } export function applyFeatureValues( features: AgentFeature[], featureValues: Record, ): AgentFeature[] { if (Object.keys(featureValues).length === 0) { return features; } return features.map((feature) => { if (!Object.prototype.hasOwnProperty.call(featureValues, feature.id)) { return feature; } return { ...feature, value: featureValues[feature.id], } as AgentFeature; }); } export function resolveFeatureValues(args: { features: AgentFeature[]; persistedFeatureValues: Record; localFeatureValues: Record; }): Record { const next: Record = {}; for (const feature of args.features) { if (Object.prototype.hasOwnProperty.call(args.localFeatureValues, feature.id)) { next[feature.id] = args.localFeatureValues[feature.id]; continue; } if (Object.prototype.hasOwnProperty.call(args.persistedFeatureValues, feature.id)) { next[feature.id] = args.persistedFeatureValues[feature.id]; } } return next; }