import 'package:flutter/material.dart'; import '../core/api_gateway.dart'; import '../features/chat/chat_runtime.dart'; import '../features/chat/ui/sessions/chat_create_fields.dart'; import '../features/settings/settings_store.dart'; import 'open_in_chat_preferences.dart'; typedef OpenInChat = Future Function(BuildContext context); String runChatReference(JsonMap run) { final id = run['id']; // A run summary names its pipeline in `name`; `title` is the run's own // title, empty for an untitled run. final title = '${run['title'] ?? ''}'.trim(); final pipeline = '${run['pipeline_name'] ?? run['pipeline'] ?? run['name'] ?? ''}'.trim(); final version = '${run['pipeline_version'] ?? run['version'] ?? ''}'.trim(); final status = '${run['status'] ?? ''}'.trim(); return 'About Pipelines run #$id "${title.isEmpty ? 'Run $id' : title}" ' '(pipeline "${pipeline.isEmpty ? 'unknown' : pipeline}"' '${version.isEmpty ? '' : ' v$version'}, status $status). ' 'Inspect it with `pipelines inspect $id`.'; } String pipelineChatReference(int id, String name, int version) => 'About pipeline "$name" (id $id, version $version). ' 'Show it with `pipelines show "$name"`; to change it, draft with ' '`pipelines edit` and publish with `pipelines submit`.'; /// Creates and opens a chat without showing the general-purpose create dialog. /// The caller supplies the already-resolved directory so run and pipeline /// screens can apply their own workspace provenance rules. Future openReferenceInChat({ required BuildContext context, required ChatRuntime runtime, required ValueChanged selectChat, required String directory, required String title, required String draft, }) async { try { // Match the New chat dialog: prefer OpenCode when it is available, but // use the first server-advertised harness on installations where it is // not. Do not invent a backend when the catalog cannot be read. final repository = runtime.sessions.repository; final backends = await repository.backends(); final chosen = await OpenInChatPreferences.load(repository.gateway); // The harness chosen in Settings → Open in chat, when the server offers it. final backend = backends.contains(chosen.harness) ? chosen.harness : backends.contains('opencode') ? 'opencode' : backends.first; final session = await runtime.sessions.createOrThrow( title: title, directory: directory.trim().isEmpty ? chatHomeDirectory : directory, backend: backend, instructions: '', operationId: 'open-in-chat-${DateTime.now().microsecondsSinceEpoch}', ); final id = '${session['id'] ?? ''}'; if (id.isEmpty) throw StateError('The new chat has no id.'); if (chosen.hasModel && backend == chosen.harness) { try { await repository.selectModel(id, chosen.provider, chosen.model); } catch (error) { // The chat still opens on the harness default; say why. if (context.mounted) { ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( 'Opened with the default model: ${chosen.model} is not ' 'available ($error).', ), ), ); } } } runtime.opened[id] = session; runtime.pendingDrafts[id] = draft; runtime.openedForWork.add(id); selectChat(id); runtime.composerFocus.request(id); } catch (error) { if (context.mounted) { ScaffoldMessenger.of( context, ).showSnackBar(SnackBar(content: Text('Could not open in chat: $error'))); } } } Future settingsWorkspace(String serverId) async { final values = await SharedPreferencesSettingsStore( serverId: serverId, ).load(); return values.workspace.trim(); }