import { RuntimeToolService } from '../../../../../core/runtime/tools/index.js';
import { FileChatSessionRepository } from '../../../../../core/chat/engine/sessions/repository/index.js';
import { ConversationTurnRuntimeResolver } from '../runtime/index.js';
/**
 * Builds the concrete runtime context needed before a persisted turn can run.
 */
export class ConversationTurnContextBuilder {
    static build(args) {
        const sessions = new FileChatSessionRepository({ sessionStoragePath: args.sessionStoragePath }).list();
        const session = sessions.find((candidate) => candidate.id === args.sessionId);
        if (!session) {
            throw new Error(`Chat session not found: ${args.sessionId}`);
        }
        const runtimeConfig = args;
        const runtime = ConversationTurnRuntimeResolver.resolve({ config: runtimeConfig, session });
        const toolContext = args;
        const toolRuntime = runtime;
        const tools = RuntimeToolService.createDefaultAgentTools({
            ...toolContext,
            ...toolRuntime,
            stateRoot: args.stateRoot,
        });
        return {
            sessions,
            session,
            runtime,
            tools,
            toolNames: tools.map((tool) => tool.name),
            leaseOwner: args.leaseOwner ?? {
                ownerKind: 'ask',
                ownerId: `submit-${process.pid}`,
                clientLabel: 'another Heddle client',
            },
        };
    }
}
//# sourceMappingURL=turn-context-builder.js.map