import { wrapToolWithStreaming } from "./tool-wrapper.js";
import { createDelegateToAgentTool, createTransferToAgentTool } from "../relationTools.js";

//#region src/domains/run/agents/tools/relation-tools.ts
function createRelationToolName(prefix, targetId) {
	return `${prefix}_to_${targetId.toLowerCase().replace(/\s+/g, "_")}`;
}
function getRelationTools(ctx, runtimeContext, sessionId) {
	const { transferRelations = [], delegateRelations = [] } = ctx.config;
	return Object.fromEntries([...transferRelations.map((agentConfig) => {
		const toolName = createRelationToolName("transfer", agentConfig.id);
		return [toolName, wrapToolWithStreaming(ctx, toolName, createTransferToAgentTool({
			transferConfig: agentConfig,
			callingAgentId: ctx.config.id,
			streamRequestId: runtimeContext?.metadata?.streamRequestId
		}), runtimeContext?.metadata?.streamRequestId, "transfer")];
	}), ...delegateRelations.map((relation) => {
		const toolName = createRelationToolName("delegate", relation.config.id);
		return [toolName, wrapToolWithStreaming(ctx, toolName, createDelegateToAgentTool({
			delegateConfig: relation,
			callingAgentId: ctx.config.id,
			executionContext: ctx.executionContext,
			contextId: runtimeContext?.contextId || "default",
			metadata: runtimeContext?.metadata || {
				conversationId: runtimeContext?.contextId || "default",
				threadId: runtimeContext?.contextId || "default",
				streamRequestId: runtimeContext?.metadata?.streamRequestId,
				apiKey: runtimeContext?.metadata?.apiKey
			},
			sessionId,
			credentialStoreRegistry: ctx.credentialStoreRegistry,
			agentRunContext: ctx
		}), runtimeContext?.metadata?.streamRequestId, "delegation")];
	})]);
}

//#endregion
export { getRelationTools };