import { resolve } from 'node:path';
import { AutonomyPermissionModeService } from '../../../core/approvals/index.js';
import { ProjectConfigService } from '../../../core/project-config/index.js';
import { FileDaemonRegistryRepository, RuntimeDaemonRegistryService } from '../../../core/runtime/daemon/index.js';
import { getWorkspaceOperationLogger } from '../../../server/logging/workspace-operation-logger.js';
import { procedure } from '../../../server/trpc.js';
import { workspaceScopedInputSchema } from './schema.js';
export const controlPlaneWorkspaceProcedure = procedure
    .input(workspaceScopedInputSchema)
    .use(async ({ ctx, input, next, path, type }) => {
    const requestWorkspace = resolveControlPlaneRequestWorkspace(ctx, input);
    const startedAt = Date.now();
    const result = await next({
        ctx: {
            requestWorkspace,
        },
    });
    requestWorkspace.logger.info({
        durationMs: Date.now() - startedAt,
        ok: result.ok,
        path,
        type,
        workspaceId: requestWorkspace.workspace.id,
        workspaceRoot: requestWorkspace.workspace.workspaceRoot,
        stateRoot: requestWorkspace.workspace.stateRoot,
        error: result.ok ? undefined : {
            code: result.error.code,
            message: result.error.message,
        },
    }, 'Control-plane workspace request');
    return result;
});
/**
 * Resolves the workspace for one control-plane request.
 *
 * Web v2 routes carry the workspace being viewed. Every workspace-owned
 * tRPC procedure should resolve through this helper so reads, mutations,
 * subscriptions, traces, logs, memory, and task state use the same state root.
 */
export function resolveControlPlaneRequestWorkspace(ctx, input) {
    const workspace = resolveWorkspaceDescriptor(ctx, input?.workspaceId);
    const projectConfig = ProjectConfigService.read(workspace.workspaceRoot);
    const autopilot = AutonomyPermissionModeService.resolveEffectiveProfile({
        config: projectConfig,
        workspaceRoot: workspace.workspaceRoot,
    });
    const logger = getWorkspaceOperationLogger(workspace.stateRoot);
    return {
        workspace,
        logger,
        sessionEngineArgs: {
            workspaceRoot: workspace.workspaceRoot,
            stateRoot: workspace.stateRoot,
            sessionStoragePath: resolve(workspace.stateRoot, 'chat-sessions.catalog.json'),
            preferApiKey: ctx.preferApiKey,
            workspaceId: workspace.id,
            autopilot,
        },
    };
}
function resolveWorkspaceDescriptor(ctx, workspaceId) {
    if (!workspaceId) {
        return ctx.activeWorkspace;
    }
    const workspace = ctx.workspaces.find((candidate) => candidate.id === workspaceId)
        ?? RuntimeDaemonRegistryService.readWorkspaceRegistration(ctx.runtimeHost?.registryPath ?? FileDaemonRegistryRepository.resolvePath(), workspaceId)?.workspace;
    if (!workspace) {
        throw new Error(`Workspace not found: ${workspaceId}`);
    }
    return workspace;
}
//# sourceMappingURL=control-plane-workspace.js.map