diff --git a/apps/server/build.gradle b/apps/server/build.gradle index a52b1c47..f4901c0b 100644 --- a/apps/server/build.gradle +++ b/apps/server/build.gradle @@ -27,6 +27,9 @@ dependencies { implementation project(':infrastructure:inference') implementation project(':infrastructure:inference:llama_cpp') implementation project(':infrastructure:inference:commons') + implementation project(':core:tools') + implementation project(':infrastructure:tools') + implementation project(':infrastructure:tools:filesystem') implementation "com.github.ajalt.clikt:clikt:5.0.1" diff --git a/apps/server/src/main/kotlin/com/correx/apps/server/Main.kt b/apps/server/src/main/kotlin/com/correx/apps/server/Main.kt index 2b8dc14e..7304b597 100644 --- a/apps/server/src/main/kotlin/com/correx/apps/server/Main.kt +++ b/apps/server/src/main/kotlin/com/correx/apps/server/Main.kt @@ -26,79 +26,92 @@ import com.correx.core.validation.artifact.ArtifactPayloadValidator import com.correx.core.validation.pipeline.ValidationPipeline import com.correx.apps.server.logging.LoggingEventStore import com.correx.infrastructure.InfrastructureModule +import com.correx.infrastructure.artifactscas.DefaultMaterializingArtifactWriter import com.correx.infrastructure.inference.DefaultProviderRegistry import com.correx.infrastructure.inference.FirstAvailableRoutingStrategy +import com.correx.infrastructure.tools.FileWriteConfig +import com.correx.infrastructure.tools.ShellConfig +import com.correx.infrastructure.tools.ToolConfig +import com.correx.core.events.EventDispatcher import io.ktor.server.engine.embeddedServer import io.ktor.server.netty.Netty +import java.nio.file.Paths fun main() { val artifactStore = InfrastructureModule.createArtifactStore() val eventStore = LoggingEventStore(InfrastructureModule.createEventStore(artifactStore)) - - val llamaProvider = InfrastructureModule.createLlamaCppProvider( - modelId = System.getenv("CORREX_MODEL_ID") ?: "default", - modelPath = System.getenv("CORREX_MODEL_PATH") ?: "", - baseUrl = System.getenv("CORREX_LLAMA_URL") ?: "http://127.0.0.1:10000", - ) - val infraRegistry = InfrastructureModule.createProviderRegistry(listOf(llamaProvider)) - - val artifactRepository = InfrastructureModule.createArtifactRepository(eventStore) - - val sessionRepository = DefaultSessionRepository( - DefaultEventReplayer(eventStore, SessionProjector(DefaultSessionReducer())) - ) - val inferenceRepository = InferenceRepository( - DefaultEventReplayer(eventStore, InferenceProjector()) - ) - val orchestrationRepository = OrchestrationRepository( - DefaultEventReplayer(eventStore, OrchestrationProjector(DefaultOrchestrationReducer())) - ) - - val inferenceRouter = DefaultInferenceRouter(infraRegistry, FirstAvailableRoutingStrategy()) - - val promptLoader = InfrastructureModule.createPromptLoader() - val promptResolver = PromptResolver { path -> promptLoader.load(path) } - - val repositories = OrchestratorRepositories( + val infraRegistry = InfrastructureModule.createProviderRegistry(listOf(buildLlamaProvider())) + val repositories = buildRepositories(eventStore) + val approvalEngine = DefaultApprovalEngine() + val sandboxRoot = Paths.get(System.getProperty("user.home"), ".config", "correx", "sandbox") + val toolRegistry = InfrastructureModule.createToolRegistry(buildToolConfig(artifactStore, sandboxRoot)) + val toolExecutor = InfrastructureModule.createToolExecutor( + registry = toolRegistry, + approvalEngine = approvalEngine, eventStore = eventStore, - inferenceRepository = inferenceRepository, - orchestrationRepository = orchestrationRepository, - sessionRepository = sessionRepository, - artifactRepository = artifactRepository, + eventDispatcher = EventDispatcher(eventStore), + workDir = sandboxRoot, ) - val engines = OrchestratorEngines( transitionResolver = DefaultTransitionResolver { condition, ctx -> condition.evaluate(ctx) }, contextPackBuilder = DefaultContextPackBuilder(DefaultContextCompressor()), - inferenceRouter = inferenceRouter, + inferenceRouter = DefaultInferenceRouter(infraRegistry, FirstAvailableRoutingStrategy()), validationPipeline = ValidationPipeline(validators = listOf(ArtifactPayloadValidator(artifactStore))), - approvalEngine = DefaultApprovalEngine(), + approvalEngine = approvalEngine, riskAssessor = DefaultRiskAssessor(), - promptResolver = promptResolver, + promptResolver = { path -> InfrastructureModule.createPromptLoader().load(path) }, + toolRegistry = toolRegistry, + toolExecutor = toolExecutor, ) - val orchestrator = DefaultSessionOrchestrator( repositories = repositories, engines = engines, retryCoordinator = DefaultRetryCoordinator(eventStore), artifactStore = artifactStore, ) - - val workflowRegistry = FileSystemWorkflowRegistry(InfrastructureModule.createWorkflowLoader()) - val module = ServerModule( orchestrator = orchestrator, eventStore = eventStore, - sessionRepository = sessionRepository, - workflowRegistry = workflowRegistry, + sessionRepository = repositories.sessionRepository, + workflowRegistry = FileSystemWorkflowRegistry(InfrastructureModule.createWorkflowLoader()), providerRegistry = infraRegistry.asServerRegistry(), ) - - embeddedServer(Netty, port = 8080) { - configureServer(module) - }.start(wait = true) + embeddedServer(Netty, port = 8080) { configureServer(module) }.start(wait = true) } +private fun buildLlamaProvider() = InfrastructureModule.createLlamaCppProvider( + modelId = System.getenv("CORREX_MODEL_ID") ?: "default", + modelPath = System.getenv("CORREX_MODEL_PATH") ?: "", + baseUrl = System.getenv("CORREX_LLAMA_URL") ?: "http://127.0.0.1:10000", +) + +private fun buildRepositories( + eventStore: com.correx.core.events.stores.EventStore, +) = OrchestratorRepositories( + eventStore = eventStore, + inferenceRepository = InferenceRepository(DefaultEventReplayer(eventStore, InferenceProjector())), + orchestrationRepository = OrchestrationRepository( + DefaultEventReplayer(eventStore, OrchestrationProjector(DefaultOrchestrationReducer())) + ), + sessionRepository = DefaultSessionRepository( + DefaultEventReplayer(eventStore, SessionProjector(DefaultSessionReducer())) + ), + artifactRepository = InfrastructureModule.createArtifactRepository(eventStore), +) + +private fun buildToolConfig( + artifactStore: com.correx.core.artifactstore.ArtifactStore, + sandboxRoot: java.nio.file.Path, +) = ToolConfig( + shell = ShellConfig(enabled = true), + fileWrite = FileWriteConfig( + enabled = true, + artifactStore = artifactStore, + materializingWriter = DefaultMaterializingArtifactWriter(), + sandboxRoot = sandboxRoot, + ), +) + private fun DefaultProviderRegistry.asServerRegistry(): ProviderRegistry = object : ProviderRegistry { override fun listAll() = this@asServerRegistry.listAll() override suspend fun healthCheckAll() = this@asServerRegistry.healthCheckAll()