import type { Context, InferToolContext, InferToolSetContext, ToolSet, } from '@ai-sdk/provider-utils'; import type { IncludedContext, IncludedToolsContext, } from './telemetry-options'; /** * Returns a shallow copy of the runtime context with only top-level * properties marked for telemetry inclusion. */ export function filterIncludedContext({ context, includeContext, }: { context: CONTEXT; includeContext: IncludedContext; }): Context { if (context == null) { return {}; } return Object.fromEntries( Object.entries(context).filter( ([key]) => includeContext?.[key as keyof CONTEXT] === true, ), ); } export function filterToolsContext({ toolsContext, includeToolsContext, }: { toolsContext: InferToolSetContext; includeToolsContext: IncludedToolsContext; }): InferToolSetContext { if (includeToolsContext == null) { return {} as InferToolSetContext; } return Object.fromEntries( Object.entries(toolsContext).map(([toolName, toolContext]) => [ toolName, filterToolContext({ toolName, toolContext, includeToolsContext, }), ]), ) as InferToolSetContext; } export function filterToolContext({ toolName, toolContext, includeToolsContext, }: { toolName: string; toolContext: unknown; includeToolsContext: IncludedToolsContext; }) { const includeToolContext = ( includeToolsContext as | Record< string, IncludedContext> > | undefined )?.[toolName]; return filterIncludedContext({ context: toolContext as InferToolContext, includeContext: includeToolContext, }); }