Files
pipelines/app/lib/application/open_in_chat.dart
T
alan 6cd7774811 Open in chat opens beside the active chat and uses a chosen harness
A chat opened from a run or pipeline now goes in a new pane beside the
active chat instead of replacing it, becomes active and takes the
keyboard once built. Settings > Open in chat chooses its harness and
model (models listed from that harness's most recent chat).
2026-11-13 13:34:26 +00:00

101 lines
3.8 KiB
Dart

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<void> 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<void> openReferenceInChat({
required BuildContext context,
required ChatRuntime runtime,
required ValueChanged<String> 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<String> settingsWorkspace(String serverId) async {
final values = await SharedPreferencesSettingsStore(
serverId: serverId,
).load();
return values.workspace.trim();
}