import { createDefaultConversationHistoryConfig } from "../data/conversations.js";
import { ArtifactReferenceSchema } from "../artifacts/artifact-component-schema.js";
import { ContextResolver } from "../context/ContextResolver.js";
import "../context/index.js";
import { hasToolCallWithPrefix, resolveGenerationResponse } from "./agent-types.js";
import { PromptConfig } from "./versions/v1/PromptConfig.js";
import { getRelationshipIdForTool } from "./tools/tool-utils.js";
import { getFunctionTools } from "./tools/function-tools.js";
import { getRelationTools } from "./tools/relation-tools.js";
import { runGenerate } from "./generation/generate.js";
import { SystemPromptBuilder } from "./SystemPromptBuilder.js";
import { AgentMcpManager } from "./services/AgentMcpManager.js";
import { CredentialStuffer } from "@inkeep/agents-core";

//#region src/domains/run/agents/Agent.ts
var Agent = class {
	ctx;
	constructor(config, executionContext, credentialStoreRegistry) {
		const artifactComponents = config.artifactComponents || [];
		let processedDataComponents = config.dataComponents || [];
		if (processedDataComponents.length > 0) processedDataComponents.push({
			id: "text-content",
			name: "Text",
			description: "Natural conversational text for the user - write naturally without mentioning technical details. Avoid redundancy and repetition with data components.",
			props: {
				type: "object",
				properties: { text: {
					type: "string",
					description: "Natural conversational text - respond as if having a normal conversation, never mention JSON, components, schemas, or technical implementation. Avoid redundancy and repetition with data components."
				} },
				required: ["text"]
			}
		});
		if (artifactComponents.length > 0 && config.dataComponents && config.dataComponents.length > 0) processedDataComponents = [ArtifactReferenceSchema.getDataComponent(config.tenantId, config.projectId), ...processedDataComponents];
		const processedConfig = {
			...config,
			dataComponents: processedDataComponents,
			conversationHistoryConfig: config.conversationHistoryConfig || createDefaultConversationHistoryConfig()
		};
		let contextResolver;
		let credentialStuffer;
		if (credentialStoreRegistry) {
			contextResolver = new ContextResolver(executionContext, credentialStoreRegistry);
			credentialStuffer = new CredentialStuffer(credentialStoreRegistry, contextResolver);
		}
		const systemPromptBuilder = new SystemPromptBuilder("v1", new PromptConfig());
		const ctx = {
			config: processedConfig,
			executionContext,
			mcpManager: void 0,
			contextResolver,
			credentialStoreRegistry,
			credentialStuffer,
			systemPromptBuilder,
			streamHelper: void 0,
			streamRequestId: void 0,
			conversationId: void 0,
			delegationId: void 0,
			isDelegatedAgent: false,
			artifactComponents,
			currentCompressor: null,
			functionToolRelationshipIdByName: /* @__PURE__ */ new Map(),
			taskDenialRedirects: []
		};
		ctx.mcpManager = new AgentMcpManager(processedConfig, executionContext, credentialStuffer, () => ctx.conversationId, () => ctx.streamRequestId, (toolName, toolType) => getRelationshipIdForTool(ctx, toolName, toolType));
		this.ctx = ctx;
	}
	get streamRequestId() {
		return this.ctx.streamRequestId;
	}
	set streamRequestId(value) {
		this.ctx.streamRequestId = value;
	}
	get mcpManager() {
		return this.ctx.mcpManager;
	}
	setConversationId(conversationId) {
		this.ctx.conversationId = conversationId;
	}
	setDelegationStatus(isDelegated) {
		this.ctx.isDelegatedAgent = isDelegated;
	}
	setDelegationId(delegationId) {
		this.ctx.delegationId = delegationId;
	}
	setDurableWorkflowRunId(runId) {
		this.ctx.durableWorkflowRunId = runId;
	}
	setApprovedToolCalls(approvedToolCalls) {
		this.ctx.approvedToolCalls = approvedToolCalls;
	}
	getPendingDurableApproval() {
		return this.ctx.pendingDurableApproval;
	}
	getTaskDenialRedirects() {
		return this.ctx.taskDenialRedirects;
	}
	getStreamingHelper() {
		return this.ctx.isDelegatedAgent ? void 0 : this.ctx.streamHelper;
	}
	async getFunctionTools(sessionId, streamRequestId) {
		return getFunctionTools(this.ctx, sessionId, streamRequestId);
	}
	get runContext() {
		return this.ctx;
	}
	async generate(userParts, runtimeContext, options) {
		return runGenerate(this.ctx, userParts, runtimeContext, options);
	}
	getRelationTools(runtimeContext, sessionId) {
		return getRelationTools(this.ctx, runtimeContext, sessionId);
	}
	cleanupCompression() {
		if (this.ctx.currentCompressor) {
			this.ctx.currentCompressor.fullCleanup();
			this.ctx.currentCompressor = null;
		}
	}
	async cleanup() {
		await this.ctx.mcpManager?.cleanup();
		this.cleanupCompression();
	}
};

//#endregion
export { Agent, hasToolCallWithPrefix, resolveGenerationResponse };