Chat info: Session section with native id, resume command and export
The chat information sheet gains a Session section: the harness's native session id (copy), harness, directory, a verified resume command (copy) and Export conversation (Markdown), Export JSON and, for Claude Code, Export native session (the JSONL transcript). Server: chat_sessions rows carry resume_command and native_export; new chat_export command builds Markdown/JSON from the same paged history chat_messages serves (bounded at 8 MiB), refuses non-operator sessions. App: exports go through the artifact file bridge (web download, share sheet on phones).
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../../artifacts/actions/artifact_file.dart';
|
||||
import '../../artifacts/actions/artifact_file_bridge.dart';
|
||||
import '../../artifacts/actions/artifact_transfer.dart';
|
||||
import '../data/chat_repository.dart';
|
||||
import '../domain/chat_session_handoff.dart';
|
||||
|
||||
/// Exports one chat through `chat_export` and hands the file to the host the
|
||||
/// same way the artifact viewer does: a browser download on the web, the share
|
||||
/// sheet (which offers saving) on phones. One export runs at a time.
|
||||
class ChatExportController extends ChangeNotifier {
|
||||
ChatExportController({
|
||||
required this.repository,
|
||||
required this.sessionId,
|
||||
this.bridge = const ArtifactFileBridge(),
|
||||
bool? share,
|
||||
}) : share = share ?? !kIsWeb;
|
||||
|
||||
final ChatRepository repository;
|
||||
final String sessionId;
|
||||
final ArtifactFileBridge bridge;
|
||||
|
||||
/// True hands the file to the share sheet; false downloads/saves it.
|
||||
final bool share;
|
||||
|
||||
ChatExportFormat? _active;
|
||||
String? _status;
|
||||
bool _disposed = false;
|
||||
|
||||
ChatExportFormat? get active => _active;
|
||||
bool get busy => _active != null;
|
||||
String? get status => _status;
|
||||
|
||||
Future<void> export(ChatExportFormat format) async {
|
||||
if (busy) return;
|
||||
final useShare = share && bridge.canShare;
|
||||
if (!useShare && !bridge.canDownload) {
|
||||
_set(null, bridge.downloadUnavailableMessage);
|
||||
return;
|
||||
}
|
||||
_set(format, 'Preparing export…');
|
||||
final JsonMap file;
|
||||
try {
|
||||
file = await repository.export(sessionId, format.wire);
|
||||
} catch (error) {
|
||||
_set(null, 'Could not export the chat: $error');
|
||||
return;
|
||||
}
|
||||
final artifact = ArtifactFile(
|
||||
name: '${file['name']}',
|
||||
mimeType: '${file['mime'] ?? ''}'.isEmpty
|
||||
? 'text/plain'
|
||||
: '${file['mime']}',
|
||||
bytes: Uint8List.fromList(utf8.encode('${file['content'] ?? ''}')),
|
||||
);
|
||||
ArtifactTransferResult result;
|
||||
try {
|
||||
result = useShare
|
||||
? await bridge.shareFile(artifact)
|
||||
: await bridge.saveFile(artifact);
|
||||
} catch (error) {
|
||||
_set(null, 'Could not save ${artifact.name}: $error');
|
||||
return;
|
||||
}
|
||||
final truncated = file['truncated'] == true
|
||||
? ' Older messages were left out to fit the size limit.'
|
||||
: '';
|
||||
final message = switch (result.state) {
|
||||
ArtifactTransferState.saved => 'Exported ${artifact.name}.$truncated',
|
||||
ArtifactTransferState.shared => 'Shared ${artifact.name}.$truncated',
|
||||
ArtifactTransferState.canceled => 'Export canceled.',
|
||||
_ => result.message.isEmpty ? 'Export failed.' : result.message,
|
||||
};
|
||||
_set(null, message);
|
||||
}
|
||||
|
||||
void _set(ChatExportFormat? active, String? status) {
|
||||
_active = active;
|
||||
_status = status;
|
||||
if (!_disposed) notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
import '../../../ui/text/monospace.dart';
|
||||
import '../domain/chat_session_handoff.dart';
|
||||
import '../ui/sessions/chat_create_fields.dart';
|
||||
import 'chat_export_controller.dart';
|
||||
|
||||
/// The info sheet's "Session" section: the harness's native session id with a
|
||||
/// copy control, the harness and the working directory, the verified terminal
|
||||
/// command that resumes the session, and the export actions.
|
||||
class ChatSessionSection extends StatefulWidget {
|
||||
const ChatSessionSection({
|
||||
super.key,
|
||||
required this.handoff,
|
||||
required this.exporter,
|
||||
required this.cupertino,
|
||||
});
|
||||
|
||||
final ChatSessionHandoff handoff;
|
||||
final ChatExportController exporter;
|
||||
final bool cupertino;
|
||||
|
||||
@override
|
||||
State<ChatSessionSection> createState() => _ChatSessionSectionState();
|
||||
}
|
||||
|
||||
class _ChatSessionSectionState extends State<ChatSessionSection> {
|
||||
/// Which copy control last succeeded, shown as "Copied" for a moment.
|
||||
String? _copied;
|
||||
Timer? _reset;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_reset?.cancel();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _copy(String key, String text) async {
|
||||
await Clipboard.setData(ClipboardData(text: text));
|
||||
if (!mounted) return;
|
||||
setState(() => _copied = key);
|
||||
_reset?.cancel();
|
||||
_reset = Timer(const Duration(seconds: 2), () {
|
||||
if (mounted) setState(() => _copied = null);
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino = widget.cupertino;
|
||||
final handoff = widget.handoff;
|
||||
final secondary = cupertino
|
||||
? CupertinoColors.secondaryLabel.resolveFrom(context)
|
||||
: Theme.of(context).colorScheme.onSurfaceVariant;
|
||||
final body = cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodyLarge;
|
||||
final title = cupertino
|
||||
? CupertinoTheme.of(
|
||||
context,
|
||||
).textTheme.textStyle.copyWith(fontWeight: FontWeight.w600)
|
||||
: Theme.of(context).textTheme.titleSmall;
|
||||
final label =
|
||||
(cupertino
|
||||
? CupertinoTheme.of(context).textTheme.textStyle
|
||||
: Theme.of(context).textTheme.bodySmall)
|
||||
?.copyWith(color: secondary);
|
||||
final mono = body?.copyWith(fontFamily: appMonospaceFamily);
|
||||
final muted = body?.copyWith(color: secondary);
|
||||
|
||||
Widget fact(String key, String name, String value, {bool code = false}) {
|
||||
final known = value.isNotEmpty;
|
||||
return Padding(
|
||||
key: Key('chat-session-$key'),
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(name, style: label),
|
||||
Text(
|
||||
known ? value : 'Unavailable',
|
||||
key: Key('chat-session-value-$key'),
|
||||
style: !known ? muted : (code ? mono : body),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return AnimatedBuilder(
|
||||
animation: widget.exporter,
|
||||
builder: (context, _) {
|
||||
final exporter = widget.exporter;
|
||||
final status = exporter.status;
|
||||
return Padding(
|
||||
key: const Key('chat-session-section'),
|
||||
padding: const EdgeInsetsDirectional.fromSTEB(24, 12, 24, 6),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text('Session', style: title),
|
||||
Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Expanded(
|
||||
child: fact('id', 'Session ID', handoff.id, code: true),
|
||||
),
|
||||
_ActionButton(
|
||||
key: const Key('chat-session-copy-id'),
|
||||
cupertino: cupertino,
|
||||
label: _copied == 'id' ? 'Copied' : 'Copy',
|
||||
semanticsLabel: _copied == 'id'
|
||||
? 'Copied'
|
||||
: 'Copy session ID',
|
||||
icon: _copied == 'id'
|
||||
? (cupertino ? CupertinoIcons.checkmark : Icons.check)
|
||||
: (cupertino
|
||||
? CupertinoIcons.doc_on_doc
|
||||
: Icons.copy_outlined),
|
||||
onPressed: handoff.id.isEmpty
|
||||
? null
|
||||
: () => _copy('id', handoff.id),
|
||||
),
|
||||
],
|
||||
),
|
||||
fact('harness', 'Harness', chatBackendLabel(handoff.backend)),
|
||||
fact('directory', 'Directory', handoff.directory, code: true),
|
||||
if (handoff.resumeCommand.isNotEmpty)
|
||||
fact(
|
||||
'resume',
|
||||
'Resume in a terminal',
|
||||
handoff.resumeCommand,
|
||||
code: true,
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 4,
|
||||
children: [
|
||||
if (handoff.resumeCommand.isNotEmpty)
|
||||
_ActionButton(
|
||||
key: const Key('chat-session-copy-resume'),
|
||||
cupertino: cupertino,
|
||||
label: _copied == 'resume'
|
||||
? 'Copied'
|
||||
: 'Copy resume command',
|
||||
icon: _copied == 'resume'
|
||||
? (cupertino ? CupertinoIcons.checkmark : Icons.check)
|
||||
: (cupertino
|
||||
? CupertinoIcons
|
||||
.chevron_left_slash_chevron_right
|
||||
: Icons.terminal),
|
||||
onPressed: () => _copy('resume', handoff.resumeCommand),
|
||||
),
|
||||
_exportButton(
|
||||
ChatExportFormat.markdown,
|
||||
'Export conversation',
|
||||
cupertino
|
||||
? CupertinoIcons.square_arrow_up
|
||||
: Icons.ios_share,
|
||||
),
|
||||
_exportButton(
|
||||
ChatExportFormat.json,
|
||||
'Export JSON',
|
||||
cupertino ? CupertinoIcons.doc_text : Icons.data_object,
|
||||
),
|
||||
if (handoff.nativeExport)
|
||||
_exportButton(
|
||||
ChatExportFormat.native,
|
||||
'Export native session',
|
||||
cupertino
|
||||
? CupertinoIcons.doc
|
||||
: Icons.description_outlined,
|
||||
),
|
||||
],
|
||||
),
|
||||
if (status != null && status.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 6),
|
||||
child: Semantics(
|
||||
liveRegion: true,
|
||||
child: Text(
|
||||
status,
|
||||
key: const Key('chat-session-export-status'),
|
||||
style: label,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _exportButton(ChatExportFormat format, String label, IconData icon) {
|
||||
final exporter = widget.exporter;
|
||||
return _ActionButton(
|
||||
key: Key('chat-session-export-${format.wire}'),
|
||||
cupertino: widget.cupertino,
|
||||
label: label,
|
||||
icon: icon,
|
||||
busy: exporter.active == format,
|
||||
onPressed: exporter.busy ? null : () => exporter.export(format),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A standard text button with a leading icon, in the host's style.
|
||||
class _ActionButton extends StatelessWidget {
|
||||
const _ActionButton({
|
||||
super.key,
|
||||
required this.cupertino,
|
||||
required this.label,
|
||||
required this.icon,
|
||||
required this.onPressed,
|
||||
this.semanticsLabel,
|
||||
this.busy = false,
|
||||
});
|
||||
|
||||
final bool cupertino;
|
||||
final String label;
|
||||
final String? semanticsLabel;
|
||||
final IconData icon;
|
||||
final VoidCallback? onPressed;
|
||||
final bool busy;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final Widget leading = busy
|
||||
? (cupertino
|
||||
? const CupertinoActivityIndicator()
|
||||
: const SizedBox.square(
|
||||
dimension: 18,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
))
|
||||
: Icon(icon, size: 18);
|
||||
final child = Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
leading,
|
||||
const SizedBox(width: 6),
|
||||
Flexible(child: Text(label, semanticsLabel: semanticsLabel)),
|
||||
],
|
||||
);
|
||||
final button = cupertino
|
||||
? CupertinoButton(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 6),
|
||||
onPressed: onPressed,
|
||||
child: child,
|
||||
)
|
||||
: TextButton(onPressed: onPressed, child: child);
|
||||
return button;
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,11 @@ import '../ui/sessions/session_actions.dart';
|
||||
import '../ui/transcript/chat_markdown_view.dart';
|
||||
import '../ui/transcript/chat_message_view.dart';
|
||||
import '../ui/transcript/chat_injected_rows.dart';
|
||||
import '../../artifacts/actions/artifact_file_bridge.dart';
|
||||
import '../domain/chat_session_handoff.dart';
|
||||
import 'chat_account_usage_section.dart';
|
||||
import 'chat_export_controller.dart';
|
||||
import 'chat_session_section.dart';
|
||||
import 'chat_background_tasks.dart';
|
||||
import 'chat_command_actions.dart';
|
||||
import 'chat_composer_bar.dart';
|
||||
@@ -75,6 +79,7 @@ class ChatThreadView extends StatefulWidget {
|
||||
this.onCreatedSession,
|
||||
this.onSessionRenamed,
|
||||
this.onSessionClosed,
|
||||
this.fileBridge = const ArtifactFileBridge(),
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
@@ -96,6 +101,9 @@ class ChatThreadView extends StatefulWidget {
|
||||
/// `/close` was accepted: the conversation is finished here as on the web.
|
||||
final VoidCallback? onSessionClosed;
|
||||
|
||||
/// The host file actions the info sheet's export uses; tests inject one.
|
||||
final ArtifactFileBridge fileBridge;
|
||||
|
||||
/// The list row's title when this visit knows it; a deep link may not.
|
||||
final String title;
|
||||
|
||||
@@ -332,16 +340,31 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
Future<void> _openInfo() async {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
final exporter = ChatExportController(
|
||||
repository: ChatRepository(widget.gateway),
|
||||
sessionId: widget.sessionId,
|
||||
bridge: widget.fileBridge,
|
||||
);
|
||||
try {
|
||||
await _showInfo(cupertino, exporter);
|
||||
} finally {
|
||||
exporter.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _showInfo(bool cupertino, ChatExportController exporter) async {
|
||||
final handoff = ChatSessionHandoff.from(widget.sessionId, widget.session);
|
||||
Widget sheet() => _ChatInfoSheet(
|
||||
controller: _controller,
|
||||
handoff: handoff,
|
||||
exporter: exporter,
|
||||
);
|
||||
if (cupertino) {
|
||||
await showCupertinoModalPopup<void>(
|
||||
context: context,
|
||||
builder: (sheetContext) => Align(
|
||||
alignment: Alignment.bottomCenter,
|
||||
child: SafeArea(
|
||||
child: CupertinoPopupSurface(
|
||||
child: _ChatInfoSheet(controller: _controller),
|
||||
),
|
||||
),
|
||||
child: SafeArea(child: CupertinoPopupSurface(child: sheet())),
|
||||
),
|
||||
);
|
||||
return;
|
||||
@@ -351,7 +374,7 @@ class _ChatThreadViewState extends State<ChatThreadView> {
|
||||
showDragHandle: true,
|
||||
isScrollControlled: true,
|
||||
useSafeArea: true,
|
||||
builder: (sheetContext) => _ChatInfoSheet(controller: _controller),
|
||||
builder: (sheetContext) => sheet(),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1541,9 +1564,15 @@ String _chatInfoDollars(num value) {
|
||||
/// The "Chat information" panel. It rebuilds from the controller, so metrics
|
||||
/// that arrive while it is open replace the displayed snapshot.
|
||||
class _ChatInfoSheet extends StatelessWidget {
|
||||
const _ChatInfoSheet({required this.controller});
|
||||
const _ChatInfoSheet({
|
||||
required this.controller,
|
||||
required this.handoff,
|
||||
required this.exporter,
|
||||
});
|
||||
|
||||
final ChatThreadController controller;
|
||||
final ChatSessionHandoff handoff;
|
||||
final ChatExportController exporter;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -1586,6 +1615,12 @@ class _ChatInfoSheet extends StatelessWidget {
|
||||
),
|
||||
),
|
||||
),
|
||||
ChatSessionSection(
|
||||
handoff: handoff,
|
||||
exporter: exporter,
|
||||
cupertino: cupertino,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
],
|
||||
);
|
||||
if (!cupertino) return SingleChildScrollView(child: content);
|
||||
|
||||
Reference in New Issue
Block a user