feat: the terminal, project knowledge, and downloads that can be cancelled
- lib/api/terminal.dart: the PTY protocol (create/resize/restart/close, and the /api/terminal/ws binary control frames) with a reconnecting socket that replays the server snapshot at the size it was drawn for. - lib/terminal_state.dart: an xterm model plus that socket, kept per workspace on AppState, so closing the panel or switching tabs keeps the shell; the app reattaches when it returns from the background. - lib/screens/terminal_panel.dart: TerminalView, a status bar (keyboard, restart, clear, close) and a Termux-style key bar whose CTRL/ALT/SHIFT modifiers also apply to what the phone keyboard types. - lib/screens/knowledge_panel.dart: notes, todos and plans from /api/project-context, searchable, with a full page for a plan's Markdown and its raw editor. - Downloads stream with a progress dialog (bytes, total, percent) and a cancel that aborts the request; the save dialog only opens on success. The request asks for `accept-encoding: identity` so Content-Length exists. - MarkdownView renders task lists as ✅/⬜ instead of material_ui's Checkbox, which wants a material_ui Material ancestor the app lacks. - Dialogs own their TextEditingControllers, so closing one no longer trips a framework assertion in debug builds. - 0.3.11 (311).
This commit is contained in:
1 parent
60d02083d3
commit
5f95e2da97
14 files changed
+2347
-57
No files matched your search
+238
-11
@@ -4,7 +4,10 @@ library;
|
||||
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:typed_data';
|
||||
|
||||
import 'models.dart';
|
||||
import 'terminal.dart';
|
||||
import 'transport.dart';
|
||||
|
||||
class ApiError implements Exception {
|
||||
@@ -291,19 +294,51 @@ class Client {
|
||||
directory: directory, body: {'agent': agent});
|
||||
}
|
||||
|
||||
Future<List<int>> rawFile(String path, {String? directory}) async {
|
||||
Future<List<int>> rawFile(String path, {String? directory}) =>
|
||||
downloadFile(path, directory: directory);
|
||||
|
||||
/// Downloads a file, reporting bytes as they arrive and letting the caller
|
||||
/// cancel. A cancelled download throws [DownloadCancelled] and drops the
|
||||
/// connection instead of reading it to the end.
|
||||
Future<Uint8List> downloadFile(String path,
|
||||
{String? directory,
|
||||
void Function(int received, int? total)? onProgress,
|
||||
CancelToken? cancel}) async {
|
||||
final res = await _request('GET', '/api/fs/raw',
|
||||
query: {
|
||||
'path': path,
|
||||
'allowOutsideWorkspace': 'true',
|
||||
},
|
||||
directory: directory,
|
||||
rawDirectory: true);
|
||||
final bytes = await res.collect();
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
throw ApiError(res.statusCode, utf8.decode(bytes, allowMalformed: true));
|
||||
rawDirectory: true,
|
||||
// Gzip would leave the response chunked (no total to show) and count
|
||||
// compressed bytes; a download wants the file as it is.
|
||||
noCompression: true);
|
||||
final total = int.tryParse(res.headers['content-length'] ?? '');
|
||||
cancel?.attach(() => res.abort?.call());
|
||||
try {
|
||||
final bytes = BytesBuilder(copy: false);
|
||||
try {
|
||||
await for (final chunk in res.body) {
|
||||
if (cancel?.isCancelled ?? false) throw const DownloadCancelled();
|
||||
bytes.add(chunk);
|
||||
onProgress?.call(bytes.length, total);
|
||||
}
|
||||
} catch (error) {
|
||||
// Aborting the request surfaces as a socket error; the caller only
|
||||
// asked to stop.
|
||||
if (cancel?.isCancelled ?? false) throw const DownloadCancelled();
|
||||
rethrow;
|
||||
}
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
throw ApiError(
|
||||
res.statusCode,
|
||||
utf8.decode(bytes.takeBytes(), allowMalformed: true));
|
||||
}
|
||||
return bytes.takeBytes();
|
||||
} finally {
|
||||
cancel?.detach();
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ files
|
||||
@@ -402,6 +437,192 @@ class Client {
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------ project knowledge
|
||||
|
||||
/// The project id the server derives from a checkout path: `path_` + the
|
||||
/// path base64url-encoded, both ends agreeing on the encoding.
|
||||
static String projectIdFor(String directory) {
|
||||
final normalized = directory
|
||||
.replaceAll('\\', '/')
|
||||
.replaceAll(RegExp(r'/+$'), '')
|
||||
.trim();
|
||||
if (normalized.isEmpty) return '';
|
||||
final encoded = base64Url
|
||||
.encode(utf8.encode(normalized))
|
||||
.replaceAll('=', '');
|
||||
return 'path_$encoded';
|
||||
}
|
||||
|
||||
Future<ProjectContext> projectContext(String projectId,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'GET', '/api/project-context/${Uri.encodeComponent(projectId)}',
|
||||
directory: directory);
|
||||
return ProjectContext.fromJson(r);
|
||||
}
|
||||
|
||||
/// Every mutation answers with the whole context beside the record it
|
||||
/// changed, so the panel refreshes from one reply.
|
||||
static ProjectContext _contextOf(Map<String, dynamic> r) {
|
||||
final context = r['context'];
|
||||
return ProjectContext.fromJson(
|
||||
context is Map<String, dynamic> ? context : r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> createNote(String projectId, String body,
|
||||
{String? planId, String? directory}) async {
|
||||
final r = await _json(
|
||||
'POST', '/api/project-context/${Uri.encodeComponent(projectId)}/notes',
|
||||
body: {
|
||||
'body': body,
|
||||
if (planId != null) 'planId': planId,
|
||||
},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> updateNote(
|
||||
String projectId, String noteId,
|
||||
{String? body, bool? pinned, String? directory}) async {
|
||||
final r = await _json(
|
||||
'PATCH',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/notes/${Uri.encodeComponent(noteId)}',
|
||||
body: {
|
||||
if (body != null) 'body': body,
|
||||
if (pinned != null) 'pinned': pinned,
|
||||
},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> deleteNote(String projectId, String noteId,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'DELETE',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/notes/${Uri.encodeComponent(noteId)}',
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> createTodo(String projectId, String text,
|
||||
{String? planId, String? directory}) async {
|
||||
final r = await _json(
|
||||
'POST', '/api/project-context/${Uri.encodeComponent(projectId)}/todos',
|
||||
body: {
|
||||
'text': text,
|
||||
if (planId != null) 'planId': planId,
|
||||
},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> updateTodo(String projectId, String todoId,
|
||||
{String? text, bool? completed, String? directory}) async {
|
||||
final r = await _json(
|
||||
'PATCH',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/todos/${Uri.encodeComponent(todoId)}',
|
||||
body: {
|
||||
if (text != null) 'text': text,
|
||||
if (completed != null) 'completed': completed,
|
||||
},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> deleteTodo(String projectId, String todoId,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'DELETE',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/todos/${Uri.encodeComponent(todoId)}',
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> createPlan(String projectId,
|
||||
{required String title, required String body, String? directory}) async {
|
||||
final r = await _json(
|
||||
'POST', '/api/project-context/${Uri.encodeComponent(projectId)}/plans',
|
||||
body: {'title': title, 'body': body},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> savePlan(String projectId, String planId, String raw,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'PUT',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/plans/${Uri.encodeComponent(planId)}',
|
||||
body: {'raw': raw},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> setPlanPinned(
|
||||
String projectId, String planId, bool pinned,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'PATCH',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/plans/${Uri.encodeComponent(planId)}',
|
||||
body: {'pinned': pinned},
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectContext> deletePlan(String projectId, String planId,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'DELETE',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/plans/${Uri.encodeComponent(planId)}',
|
||||
directory: directory);
|
||||
return _contextOf(r);
|
||||
}
|
||||
|
||||
Future<ProjectPlanContent> planContent(String projectId, String planId,
|
||||
{String? directory}) async {
|
||||
final r = await _json(
|
||||
'GET',
|
||||
'/api/project-context/${Uri.encodeComponent(projectId)}/plans/${Uri.encodeComponent(planId)}',
|
||||
directory: directory);
|
||||
return ProjectPlanContent.fromJson(r);
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------- terminal
|
||||
|
||||
Future<TerminalSessionInfo> createTerminal(
|
||||
{required String cwd, int cols = 80, int rows = 24}) async {
|
||||
final r = await _json('POST', '/api/terminal/create',
|
||||
body: {'cwd': cwd, 'cols': cols, 'rows': rows}, directory: cwd);
|
||||
return TerminalSessionInfo.fromJson(r);
|
||||
}
|
||||
|
||||
Future<void> resizeTerminal(String sessionId, int cols, int rows,
|
||||
{String? directory}) async {
|
||||
await _json('POST',
|
||||
'/api/terminal/${Uri.encodeComponent(sessionId)}/resize',
|
||||
body: {'cols': cols, 'rows': rows}, directory: directory);
|
||||
}
|
||||
|
||||
Future<TerminalSessionInfo> restartTerminal(String sessionId,
|
||||
{required String cwd, int cols = 80, int rows = 24}) async {
|
||||
final r = await _json('POST',
|
||||
'/api/terminal/${Uri.encodeComponent(sessionId)}/restart',
|
||||
body: {'cwd': cwd, 'cols': cols, 'rows': rows}, directory: cwd);
|
||||
return TerminalSessionInfo.fromJson(r);
|
||||
}
|
||||
|
||||
Future<void> closeTerminal(String sessionId, {String? directory}) async {
|
||||
await _json(
|
||||
'DELETE', '/api/terminal/${Uri.encodeComponent(sessionId)}',
|
||||
directory: directory);
|
||||
}
|
||||
|
||||
/// The headers the terminal socket needs (auth, and the workspace the
|
||||
/// server routes the upgrade by).
|
||||
Map<String, String> get eventHeaders => {
|
||||
'user-agent': 'openchamber-native/0.3',
|
||||
if (token != null && token!.isNotEmpty) 'authorization': 'Bearer $token',
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------- transport
|
||||
|
||||
Future<Map<String, dynamic>> _json(String method, String path,
|
||||
@@ -409,13 +630,15 @@ class Client {
|
||||
Map<String, dynamic>? body,
|
||||
String? directory,
|
||||
bool partShells = false,
|
||||
bool rawDirectory = false}) async {
|
||||
bool rawDirectory = false,
|
||||
bool noCompression = false}) async {
|
||||
final res = await _request(method, path,
|
||||
query: query,
|
||||
body: body,
|
||||
directory: directory,
|
||||
partShells: partShells,
|
||||
rawDirectory: rawDirectory);
|
||||
rawDirectory: rawDirectory,
|
||||
noCompression: noCompression);
|
||||
final text = utf8.decode(await res.collect(), allowMalformed: true);
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
String msg = text;
|
||||
@@ -438,7 +661,8 @@ class Client {
|
||||
Map<String, dynamic>? body,
|
||||
String? directory,
|
||||
bool partShells = false,
|
||||
bool rawDirectory = false}) async {
|
||||
bool rawDirectory = false,
|
||||
bool noCompression = false}) async {
|
||||
final headers = <String, String>{
|
||||
'accept': path == '/api/event' ? 'text/event-stream' : 'application/json',
|
||||
'user-agent': 'openchamber-native/0.3',
|
||||
@@ -452,6 +676,7 @@ class Client {
|
||||
if (!rawDirectory) headers['x-opencode-directory-encoding'] = 'uri';
|
||||
}
|
||||
if (partShells) headers['x-openchamber-part-shells'] = 'v1';
|
||||
if (noCompression) headers['accept-encoding'] = 'identity';
|
||||
if (path == '/api/event') headers['cache-control'] = 'no-cache';
|
||||
final bodyBytes = body == null ? null : utf8.encode(jsonEncode(body));
|
||||
if (bodyBytes != null) headers['content-type'] = 'application/json';
|
||||
@@ -462,7 +687,7 @@ class Client {
|
||||
headers: headers,
|
||||
body: bodyBytes,
|
||||
));
|
||||
return _Response(res.status, res.body);
|
||||
return _Response(res.status, res.headers, res.body, res.abort);
|
||||
}
|
||||
|
||||
void close() => transport.close();
|
||||
@@ -470,8 +695,10 @@ class Client {
|
||||
|
||||
class _Response {
|
||||
final int statusCode;
|
||||
final Map<String, String> headers;
|
||||
final Stream<List<int>> body;
|
||||
_Response(this.statusCode, this.body);
|
||||
final void Function()? abort;
|
||||
_Response(this.statusCode, this.headers, this.body, [this.abort]);
|
||||
|
||||
Future<List<int>> collect() =>
|
||||
body.fold<List<int>>(<int>[], (a, b) => a..addAll(b));
|
||||
|
||||
Reference in new issue
Block a user