import 'package:cutdex_agent/cutdex_agent.dart'; import 'package:cutdex_agent/io.dart'; import 'package:riverpod_annotation/riverpod_annotation.dart'; import 'agent_credentials.dart'; import 'agent_models.dart'; import 'agent_runtime.dart'; part 'agent_runtime_factory.g.dart'; @Riverpod(keepAlive: true) AgentRuntimeFactory agentRuntimeFactory(Ref ref) { return AgentRuntimeFactory(ref.watch(agentCredentialStoreProvider)); } /// Converts persisted provider metadata into runtime-only Agent dependencies. class AgentRuntimeFactory { const AgentRuntimeFactory(this._credentials); final AgentCredentialStore _credentials; Future create(AgentSettingsState settings) async { if (!settings.enabled) return null; return createFor( settings, providerId: settings.selectedProviderId, modelId: settings.selectedModelId, ); } Future createFor( AgentSettingsState settings, { required String? providerId, required String? modelId, }) async { if (!settings.enabled) return null; final provider = settings.providers .where((candidate) => candidate.id == providerId) .firstOrNull; final model = provider?.models .where((candidate) => candidate.id == modelId) .firstOrNull; if (provider == null || !provider.enabled || model == null) return null; final apiType = AgentApiType.values .where((candidate) => candidate.name == provider.apiType) .firstOrNull; if (apiType == null) return null; final credentialId = provider.credentialId; final apiKey = credentialId == null ? '' : await _credentials.read(credentialId) ?? ''; final adapter = switch (apiType) { AgentApiType.openAiChatCompletions => OpenAiChatAdapter( baseUrl: Uri.parse(provider.baseUrl), model: model.id, apiKey: apiKey, ), AgentApiType.openAiResponses => OpenAiResponsesAdapter( baseUrl: Uri.parse(provider.baseUrl), model: model.id, apiKey: apiKey, ), AgentApiType.anthropicMessages => AnthropicMessagesAdapter( baseUrl: Uri.parse(provider.baseUrl), model: model.id, apiKey: apiKey, ), }; return AgentRuntime( model: adapter, contextBuilder: HarnessContextBuilder( directTools: const {'cutdex_workspaces'}, summarizer: ModelContextSummarizer( model: adapter, budget: ContextBuilder( maxInputTokens: model.effectiveMaxInputTokens, maxOutputTokens: model.effectiveMaxOutputTokens, ), instructions: 'For video editing preserve project, timeline, sequence ' 'and clip IDs, lane relationships, source versus timeline time, ' 'keyframe units, mask identifiers, committed edits and unsaved ' 'or uncertain results. Preserve user constraints and pending edits. ' 'Preserve the latest confirmed workspace and version IDs and ' 'whether any later edit or error invalidated them. Compaction ' 'alone does not invalidate a successful query or commit. Reuse ' 'its version for the next sequential write; do not add a pending ' 'query merely because a summary was created. Requery only when ' 'the target changes or a stale/uncertain ' 'result requires reconciliation. Preserve durable resultId references and cursors needed for pending reads. Do not copy full curves or unrelated tool call IDs. Keep the summary concise: group completed edits by target, retain only latest valid version, exact pending actions and user constraints. Do not repeat the same IDs under multiple headings.', ), maxInputTokens: model.effectiveMaxInputTokens, maxOutputTokens: model.effectiveMaxOutputTokens, ), revision: settings.revision, sessionIdentity: '${provider.id}\u0000${model.id}', providerId: provider.id, modelId: model.id, ); } }