import { Router } from 'express';
import { createExpressMiddleware } from '@trpc/server/adapters/express';
import { RuntimeWorkspaceService } from '../../../core/runtime/workspaces/index.js';
import { appRouter } from '../../router.js';
export function createTrpcExpressRouter(options) {
    const trpcRouter = Router();
    trpcRouter.use('/trpc', createExpressMiddleware({
        router: appRouter,
        createContext: () => {
            const workspaceContext = RuntimeWorkspaceService.resolveContext({
                workspaceRoot: options.workspaceRoot,
                stateRoot: options.stateRoot,
            });
            return {
                workspaceRoot: options.workspaceRoot,
                stateRoot: options.stateRoot,
                preferApiKey: Boolean(options.preferApiKey),
                activeWorkspaceId: workspaceContext.activeWorkspaceId,
                activeWorkspace: workspaceContext.activeWorkspace,
                workspaces: workspaceContext.workspaces,
                runtimeHost: options.runtimeHost ?? null,
                logger: options.logger,
            };
        },
        onError: ({ error, path, type }) => {
            options.logger.error({
                error: {
                    message: error.message,
                    code: error.code,
                    stack: error.stack,
                },
                path,
                type,
            }, 'tRPC request failed');
        },
    }));
    return trpcRouter;
}
//# sourceMappingURL=trpc.js.map