diff --git a/app/lib/features/chat/domain/chat_shown_diff.dart b/app/lib/features/chat/domain/chat_shown_diff.dart new file mode 100644 index 00000000..c61f5957 --- /dev/null +++ b/app/lib/features/chat/domain/chat_shown_diff.dart @@ -0,0 +1,88 @@ +import '../../../core/api_gateway.dart'; +import '../../changes/domain/change_capture.dart'; +import '../../changes/domain/unified_patch.dart'; + +/// A `show_diff` item's comparison, as the server records it +/// (`internal/protocol/chat_shown.go` ChatShownDiffBody): a repository's work +/// tree, or HEAD when committed only, against HEAD or the merge base with a +/// base branch, with the same per-file changes a run records. +class ChatShownDiff { + const ChatShownDiff({ + required this.repo, + required this.base, + required this.baseCommit, + required this.head, + required this.committedOnly, + required this.files, + required this.additions, + required this.deletions, + required this.capture, + }); + + /// The diff of a `diff` item, or null for any other item or a malformed one. + static ChatShownDiff? of(JsonMap item) { + final body = item['diff']; + if (item['kind'] != 'diff' || body is! Map) return null; + final changes = body['changes']; + return ChatShownDiff( + repo: _text(body['repo']), + base: _text(body['base']), + baseCommit: _text(body['base_commit']), + head: _text(body['head']), + committedOnly: body['committed_only'] == true, + files: _count(body['files']), + additions: _count(body['additions']), + deletions: _count(body['deletions']), + capture: item['expired'] == true || changes is! Map + ? null + : ChangeCapture.fromJson(Map.from(changes)), + ); + } + + final String repo; + final String base; + final String baseCommit; + final String head; + final bool committedOnly; + final int files; + final int additions; + final int deletions; + + /// The per-file changes, or null once the server no longer retains them. + final ChangeCapture? capture; + + /// The repository's folder name. + String get repoName { + final parts = repo.split('/').where((part) => part.isNotEmpty); + return parts.isEmpty ? repo : parts.last; + } + + /// What was compared, in words: the branch against its base and whether + /// uncommitted work is included. + String get comparison { + final head = this.head.isEmpty ? 'HEAD' : this.head; + final against = base.isEmpty + ? (baseCommit.isEmpty ? 'no commits yet' : 'uncommitted changes') + : '$head vs $base${committedOnly ? ' · committed only' : ' · with uncommitted work'}'; + final where = repoName.isEmpty ? '' : '$repoName · '; + return base.isEmpty ? '$where$head · $against' : '$where$against'; + } + + /// "3 files changed +12 -4", or "No changes". + String get heading { + if (files == 0) return 'No changes'; + final stat = changeStat(additions, deletions); + return '${files == 1 ? '1 file' : '$files files'} changed' + '${stat.isEmpty ? '' : ' $stat'}'; + } + + /// True when files or patches were left out by the size limits. + bool get partial { + final capture = this.capture; + return capture != null && (!capture.complete || capture.omitted > 0); + } +} + +String _text(Object? value) => '${value ?? ''}'.trim(); + +int _count(Object? value) => value is num && value > 0 ? value.toInt() : 0; diff --git a/app/lib/features/chat/domain/chat_shown_items.dart b/app/lib/features/chat/domain/chat_shown_items.dart index 3539e1ab..1757ad7d 100644 --- a/app/lib/features/chat/domain/chat_shown_items.dart +++ b/app/lib/features/chat/domain/chat_shown_items.dart @@ -2,15 +2,15 @@ import '../../../core/api_gateway.dart'; import 'chat_transcript.dart'; /// Entries a chat's model showed the operator through the Pipelines tools -/// (`show_file`, `show_image`, `show_html`, `notify`). +/// (`show_file`, `show_image`, `show_html`, `show_diff`, `notify`). /// /// The server keeps them per chat, outside the harness transcript: the /// `chat_subscribe` reply carries the retained list (oldest first) and a /// pushed `chat_shown` frame carries each new or updated item. Items are keyed /// by `id`; a later copy replaces the held one in place. -/// The four recorded kinds. -const chatShownKinds = {'file', 'image', 'html', 'notify'}; +/// The recorded kinds. +const chatShownKinds = {'file', 'image', 'html', 'diff', 'notify'}; /// The transcript row kind an item is drawn as. const String chatShownRowKind = 'shown'; @@ -52,7 +52,7 @@ List mergeChatShownItems( /// `pipelines/show_file`. final RegExp _pipelinesTool = RegExp( r'^(?:mcp__pipelines__|pipelines_|pipelines/)' - r'(?:show_file|show_image|show_html|notify)$', + r'(?:show_file|show_image|show_html|show_diff|notify)$', ); /// Whether [part] is a call of a Pipelines tool whose result the transcript diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart index ade376f6..afa44f41 100644 --- a/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_card.dart @@ -3,6 +3,7 @@ import 'package:flutter/widgets.dart'; import '../../../../../core/api_gateway.dart'; import '../../../../artifacts/ui/artifact_html_view.dart'; import '../../../files/files_viewer.dart'; +import 'chat_shown_diff_card.dart'; import 'chat_shown_file_card.dart'; import 'chat_shown_html_card.dart'; import 'chat_shown_link_image.dart'; @@ -35,6 +36,7 @@ class ChatShownCard extends StatelessWidget { return switch ('${item['kind']}') { 'notify' => ChatShownNotifyRow(item: item), 'html' => ChatShownHtmlCard(item: item, htmlBuilder: htmlBuilder), + 'diff' => ChatShownDiffCard(item: item), 'image' when path.isEmpty => ChatShownLinkImage( item: item, openExternal: viewer.openExternal, diff --git a/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart b/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart new file mode 100644 index 00000000..50185b02 --- /dev/null +++ b/app/lib/features/chat/ui/transcript/shown/chat_shown_diff_card.dart @@ -0,0 +1,115 @@ +import 'package:flutter/material.dart'; + +import '../../../../../core/api_gateway.dart'; +import '../../../../changes/changes.dart'; +import '../../../domain/chat_shown_diff.dart'; +import 'chat_shown_surface.dart'; + +/// How many changed files the card lists before "Show all". +const chatShownDiffPreviewFiles = 8; + +/// A Git comparison the model showed through `show_diff`: what was compared, +/// the totals, an honest note when the size limits left something out, and +/// one row per changed file that opens the app's file diff viewer, stepping +/// through every file of the diff. A diff the server no longer retains says so. +class ChatShownDiffCard extends StatefulWidget { + const ChatShownDiffCard({super.key, required this.item}); + + final JsonMap item; + + @override + State createState() => _ChatShownDiffCardState(); +} + +class _ChatShownDiffCardState extends State { + bool _all = false; + + @override + Widget build(BuildContext context) { + final diff = ChatShownDiff.of(widget.item); + if (diff == null) return const SizedBox.shrink(); + final title = chatShownTitle(widget.item, 'Diff'); + final secondary = chatShownSecondary(context); + final noteStyle = Theme.of( + context, + ).textTheme.bodySmall?.copyWith(color: secondary); + final capture = diff.capture; + final files = capture?.files ?? const []; + final shown = _all || files.length <= chatShownDiffPreviewFiles + ? files + : files.take(chatShownDiffPreviewFiles).toList(); + return ChatShownSurface( + label: 'Shown diff $title', + maxWidth: 720, + child: Column( + mainAxisSize: MainAxisSize.min, + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 8, 12, 4), + child: ChatShownHeading( + title: title, + subtitle: diff.comparison, + leading: Icon(Icons.difference_outlined, color: secondary), + ), + ), + Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 8), + child: ChangeStatText( + diff.heading, + key: const Key('chat-shown-diff-heading'), + style: const TextStyle(fontWeight: FontWeight.w600), + ), + ), + if (capture == null) + Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12), + child: Text('This diff is no longer retained', style: noteStyle), + ) + else ...[ + if (diff.partial) + Padding( + key: const Key('chat-shown-diff-partial'), + padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 8), + child: Text( + capture.diagnostic.isEmpty + ? 'Part of this diff was left out by the size limit.' + : 'Partial diff: ${capture.diagnostic}', + style: noteStyle, + ), + ), + for (final file in shown) + ChangeFileRow( + key: ValueKey('chat-shown-diff-file-${file.path}'), + file: file, + onOpen: (file) => + showFileDiffViewer(context, file, files: files), + ), + if (shown.length < files.length) + Align( + alignment: AlignmentDirectional.centerStart, + child: Padding( + padding: const EdgeInsetsDirectional.fromSTEB(4, 0, 4, 4), + child: TextButton( + key: const Key('chat-shown-diff-all'), + onPressed: () => setState(() => _all = true), + child: Text('Show all ${files.length} files'), + ), + ), + ), + if (capture.omitted > 0) + Padding( + padding: const EdgeInsetsDirectional.fromSTEB(12, 0, 12, 12), + child: Text( + '${capture.omitted} more ' + '${capture.omitted == 1 ? 'file is' : 'files are'} ' + 'not listed.', + style: noteStyle, + ), + ), + ], + ], + ), + ); + } +} diff --git a/app/notes/chat-pipelines-tools.md b/app/notes/chat-pipelines-tools.md index e72202c8..057609e0 100644 --- a/app/notes/chat-pipelines-tools.md +++ b/app/notes/chat-pipelines-tools.md @@ -1,6 +1,6 @@ -# Chat: Pipelines tools (show file / image / HTML, notify phone) +# Chat: Pipelines tools (show file / image / HTML / diff, notify phone) -A chat's model can call four tools that act on Pipelines, not on the harness +A chat's model can call five tools that act on Pipelines, not on the harness or the workspace. The tools come from an MCP server that runs inside `pipelines-server`. Each call adds an entry to the chat transcript, and the entry is persisted. @@ -10,6 +10,7 @@ entry is persisted. | `show_file` | `path`, `title?` | Shows an existing file inside the operator's home. The path can be absolute, `~/…`, or relative to the chat's directory. Images render inline; other files render as a preview card. | | `show_image` | `url_or_path`, `title?`, `alt?` | Shows a home image file (it must be `image/*`) or an `https://` URL. | | `show_html` | `html`, `title?`, `height_hint?` | Renders model-authored HTML or SVG inline in a sandbox, with an **Open** button for full screen. | +| `show_diff` | `path?`, `base?`, `committed_only?`, `title?` | Runs Git in a repository inside the operator's home (default: the chat's directory) and shows a diff card: every changed file with its +/- counts, each opening the app's file diff viewer. Without `base`: uncommitted work (staged, unstaged, untracked) against HEAD. With `base` (e.g. `main`): everything since the merge base with it, which is what a pull request would contain, plus uncommitted work unless `committed_only`. | | `notify` | `title`, `message`, `priority?` | Sends a phone notification through the existing ntfy "system" destination. Tapping it opens `pipelines://chat/`. | Each tool returns one short text line to the model. A refusal comes back as @@ -58,6 +59,16 @@ Each tool returns one short text line to the model. A refusal comes back as ## Limits +- `show_diff` lists at most **300 files** (the rest are counted as omitted). + A patch over **256 KiB**, or past **1 MiB** in total, keeps its row with a + no-patch reason, and the card says the diff is partial. Binary files are + listed without a patch. `base` must be a plain revision name (no leading + `-`, no `..`, no whitespace); Git runs without pager, external diff or text + conversion, with a 30 s timeout. Renames show as a deletion plus an addition. +- Diff patches count against the same **2 MiB per chat** budget as HTML + bodies; an older diff past it keeps its summary and says it is no longer + retained. + - `show_html` documents are refused, not truncated, above **512 KiB**. The model is told the size and the limit. - At most **100 items are retained per chat**. HTML bodies are kept only for the diff --git a/app/test/features/chat/ui/transcript/shown/chat_shown_diff_card_test.dart b/app/test/features/chat/ui/transcript/shown/chat_shown_diff_card_test.dart new file mode 100644 index 00000000..4dd3aecc --- /dev/null +++ b/app/test/features/chat/ui/transcript/shown/chat_shown_diff_card_test.dart @@ -0,0 +1,147 @@ +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/changes/changes.dart'; +import 'package:pipelines_app/features/chat/domain/chat_shown_items.dart'; +import 'package:pipelines_app/features/chat/ui/transcript/shown/chat_shown_card.dart'; + +JsonMap _file(String path, String kind, String patch) => { + 'path': path, + 'kind': kind, + 'patch': patch, +}; + +JsonMap _item({ + List? files, + String status = 'complete', + int omitted = 0, + bool expired = false, + String base = 'main', +}) { + final list = + files ?? + [ + _file( + 'app/lib/a.dart', + 'modified', + '--- a/app/lib/a.dart\n+++ b/app/lib/a.dart\n@@ -1,2 +1,2 @@\n one\n-two\n+TWO\n', + ), + _file('docs/new.md', 'added', '@@ -0,0 +1,2 @@\n+# New\n+text\n'), + ]; + return { + 'id': 'd1', + 'kind': 'diff', + 'created_at': 1, + 'title': 'PR preview', + 'expired': expired, + 'diff': { + 'repo': '/home/op/repos/pipelines', + 'base': base, + 'base_commit': 'abc123def456', + 'head': 'feature', + 'files': list.length + omitted, + 'additions': 3, + 'deletions': 1, + if (!expired) + 'changes': { + 'status': status, + 'files': list, + 'omitted': omitted, + 'skipped': 0, + if (status != 'complete') + 'diagnostic': 'some patches were left out by the size limit.', + }, + }, + }; +} + +Future _pump(WidgetTester tester, JsonMap item) async { + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: SingleChildScrollView( + child: ChatShownCard( + row: {'kind': chatShownRowKind, 'item': item}, + endpoint: 'ws://127.0.0.1:1/connect', + ), + ), + ), + ), + ); + await tester.pumpAndSettle(); +} + +void main() { + test('diff items are a recorded kind and show_diff calls are hidden', () { + expect(chatShownKinds, contains('diff')); + expect( + chatPipelinesToolRepresented({ + 'type': 'tool', + 'name': 'mcp__pipelines__show_diff', + 'state': {'status': 'completed'}, + }), + isTrue, + ); + }); + + testWidgets('a diff card lists its files and opens the diff viewer', ( + tester, + ) async { + await _pump(tester, _item()); + expect(find.text('PR preview'), findsOneWidget); + expect( + find.text('pipelines · feature vs main · with uncommitted work'), + findsOneWidget, + ); + expect(find.text('2 files changed +3 -1'), findsOneWidget); + expect(find.text('app/lib/a.dart'), findsOneWidget); + expect(find.text('docs/new.md'), findsOneWidget); + expect(find.byKey(const Key('chat-shown-diff-partial')), findsNothing); + + await tester.tap(find.text('app/lib/a.dart')); + await tester.pumpAndSettle(); + expect(find.byType(FileDiffPage), findsOneWidget); + expect(find.textContaining('TWO', findRichText: true), findsWidgets); + }); + + testWidgets('a long diff lists the first files until Show all', ( + tester, + ) async { + final files = [ + for (var i = 0; i < 12; i++) + _file('f$i.txt', 'added', '@@ -0,0 +1 @@\n+x\n'), + ]; + await _pump(tester, _item(files: files)); + expect(find.text('f7.txt'), findsOneWidget); + expect(find.text('f8.txt'), findsNothing); + await tester.ensureVisible(find.byKey(const Key('chat-shown-diff-all'))); + await tester.tap(find.byKey(const Key('chat-shown-diff-all'))); + await tester.pumpAndSettle(); + expect(find.text('f11.txt'), findsOneWidget); + expect(find.byKey(const Key('chat-shown-diff-all')), findsNothing); + }); + + testWidgets('a partial diff says what was left out', (tester) async { + await _pump(tester, _item(status: 'partial', omitted: 3)); + expect(find.byKey(const Key('chat-shown-diff-partial')), findsOneWidget); + expect(find.text('3 more files are not listed.'), findsOneWidget); + expect(find.text('5 files changed +3 -1'), findsOneWidget); + }); + + testWidgets('an expired diff keeps its summary and says so', (tester) async { + await _pump(tester, _item(expired: true)); + expect(find.text('2 files changed +3 -1'), findsOneWidget); + expect(find.text('This diff is no longer retained'), findsOneWidget); + expect(find.byType(ChangeFileRow), findsNothing); + }); + + testWidgets('without a base it describes the uncommitted changes', ( + tester, + ) async { + await _pump(tester, _item(base: '')); + expect( + find.text('pipelines · feature · uncommitted changes'), + findsOneWidget, + ); + }); +} diff --git a/app/tool/perf/chat_shown_fixture.dart b/app/tool/perf/chat_shown_fixture.dart index 78b14d48..fd923ad2 100644 --- a/app/tool/perf/chat_shown_fixture.dart +++ b/app/tool/perf/chat_shown_fixture.dart @@ -68,5 +68,65 @@ JsonMap chatShownFixture(Object? sessionId) => { 'priority': 'default', 'delivered': true, }, + { + 'id': 'shown-diff', + 'kind': 'diff', + 'created_at': _at(19, 50), + 'title': 'Pull request preview', + 'diff_bytes': 612, + 'diff': { + 'repo': '/srv/probe/pipelines', + 'base': 'main', + 'base_commit': '79e51834a1b2', + 'head': 'show-diff-tool', + 'files': 3, + 'additions': 7, + 'deletions': 2, + 'changes': { + 'status': 'complete', + 'skipped': 0, + 'files': [ + { + 'path': 'internal/chattools/tools.go', + 'kind': 'modified', + 'patch': + 'diff --git a/internal/chattools/tools.go b/internal/chattools/tools.go\n' + '--- a/internal/chattools/tools.go\n' + '+++ b/internal/chattools/tools.go\n' + '@@ -6,7 +6,8 @@ const (\n' + ' \tToolShowFile = "show_file"\n' + ' \tToolShowImage = "show_image"\n' + '-\tToolShowHTML = "show_html"\n' + '+\tToolShowHTML = "show_html"\n' + '+\tToolShowDiff = "show_diff"\n' + ' \tToolNotify = "notify"\n' + ' )\n', + }, + { + 'path': 'internal/server/chat_tools_diff.go', + 'kind': 'added', + 'patch': + '--- /dev/null\n' + '+++ b/internal/server/chat_tools_diff.go\n' + '@@ -0,0 +1,5 @@\n' + '+package server\n' + '+\n' + '+// runShowDiff compares a repository inside the operator\'s home\n' + '+// and records the per-file patches as one diff item.\n' + '+const showDiffMaxFiles = 300\n', + }, + { + 'path': 'docs/old-notes.md', + 'kind': 'deleted', + 'patch': + '--- a/docs/old-notes.md\n' + '+++ /dev/null\n' + '@@ -1,1 +0,0 @@\n' + '-Superseded notes.\n', + }, + ], + }, + }, + }, ], }; diff --git a/internal/chattools/mcp_test.go b/internal/chattools/mcp_test.go index 6cc9b7da..a0a94525 100644 --- a/internal/chattools/mcp_test.go +++ b/internal/chattools/mcp_test.go @@ -75,7 +75,7 @@ func TestHandshakeListAndCall(t *testing.T) { t.Fatalf("tool without description or schema: %v", entry) } } - if strings.Join(names, ",") != "show_file,show_image,show_html,notify" { + if strings.Join(names, ",") != "show_file,show_image,show_html,show_diff,notify" { t.Fatalf("tools = %v", names) } if list["id"] != "l" { diff --git a/internal/chattools/tools.go b/internal/chattools/tools.go index db6e842b..e8f3b33d 100644 --- a/internal/chattools/tools.go +++ b/internal/chattools/tools.go @@ -5,6 +5,7 @@ const ( ToolShowFile = "show_file" ToolShowImage = "show_image" ToolShowHTML = "show_html" + ToolShowDiff = "show_diff" ToolNotify = "notify" ) @@ -15,7 +16,7 @@ const MaxHTMLBytes = 512 << 10 // IsTool reports whether name is one of the Pipelines tools. func IsTool(name string) bool { switch name { - case ToolShowFile, ToolShowImage, ToolShowHTML, ToolNotify: + case ToolShowFile, ToolShowImage, ToolShowHTML, ToolShowDiff, ToolNotify: return true } return false @@ -24,6 +25,7 @@ func IsTool(name string) bool { // Instructions is the MCP initialize instructions text. const Instructions = "These tools act on Pipelines, the app the operator reads this chat in, not on your workspace. " + "Use show_file / show_image instead of telling the operator a path to open, show_html for diagrams, charts or small visual views, " + + "show_diff when the operator asks to see changes or a diff (for example before submitting a pull request), " + "and notify only when the operator should look at their phone now." type toolDefinition struct { @@ -84,6 +86,24 @@ func definitions() []toolDefinition { }), Annotations: map[string]any{"readOnlyHint": true, "openWorldHint": false}, }, + { + Name: ToolShowDiff, + Title: "Show a Git diff in the chat", + Description: "Show the operator the changes in a Git repository on this machine as a diff card in the Pipelines chat: " + + "every changed file with its added and removed lines, each opening the app's diff viewer. " + + "Use it when the operator asks to see the diff or the changes, for example before a pull request is submitted. " + + "Pipelines runs Git itself, so do not paste the diff. Without base it shows uncommitted work (staged, unstaged and untracked files) against HEAD. " + + "With base (the pull request's target branch, such as main or origin/main) it shows everything since the branch left base, " + + "which is what the pull request would contain, plus uncommitted work unless committed_only is true. " + + "The repository must be inside the operator's home directory. Very large diffs are shown partially and say so.", + InputSchema: objectSchema([]string{}, map[string]any{ + "path": stringProperty("Optional repository directory: absolute, ~/path, or relative to the chat's working directory. Defaults to the chat's working directory."), + "base": stringProperty("Optional branch, tag or commit to compare against, such as main or origin/main. Omit to show uncommitted changes."), + "committed_only": map[string]any{"type": "boolean", "description": "Only with base: show the committed changes (base...HEAD) and leave out uncommitted work."}, + "title": stringProperty("Optional short caption shown above the diff."), + }), + Annotations: map[string]any{"readOnlyHint": true, "openWorldHint": false}, + }, { Name: ToolNotify, Title: "Notify the operator's phone", diff --git a/internal/pipeline/chat_tools.go b/internal/pipeline/chat_tools.go index df56b2ce..349b76fe 100644 --- a/internal/pipeline/chat_tools.go +++ b/internal/pipeline/chat_tools.go @@ -83,8 +83,8 @@ func (s *Store) BindChatToolToken(ctx context.Context, token, scope string) erro } // AppendChatShownItem records one shown item and applies the retention bounds: -// at most protocol.ChatShownLimit items per chat, and HTML bodies only for the -// newest documents within protocol.ChatShownHTMLBudget. +// at most protocol.ChatShownLimit items per chat, and HTML and diff bodies only +// for the newest documents within protocol.ChatShownHTMLBudget. func (s *Store) AppendChatShownItem(ctx context.Context, sessionID string, item protocol.ChatShownItem) error { payload, err := json.Marshal(item) if err != nil { @@ -109,15 +109,27 @@ SELECT id FROM chat_shown_items WHERE session_id=? ORDER BY created_at DESC, row budget := protocol.ChatShownHTMLBudget for i := len(items) - 1; i >= 0; i-- { current := items[i] - if current.Kind != protocol.ChatShownHTML || current.Expired { + if current.Expired { continue } - if len(current.HTML) <= budget { - budget -= len(current.HTML) + var size int + switch { + case current.Kind == protocol.ChatShownHTML: + size = len(current.HTML) + case current.Kind == protocol.ChatShownDiff && current.Diff != nil && current.Diff.Changes != nil: + size = current.DiffBytes + default: + continue + } + if size <= budget { + budget -= size continue } budget = 0 current.HTML, current.Expired = "", true + if current.Diff != nil { + current.Diff.Changes = nil + } data, err := json.Marshal(current) if err != nil { return err diff --git a/internal/protocol/chat_shown.go b/internal/protocol/chat_shown.go index 932df197..e5f26e92 100644 --- a/internal/protocol/chat_shown.go +++ b/internal/protocol/chat_shown.go @@ -2,7 +2,7 @@ package protocol // ChatShown is the pushed frame type for an item a chat's model showed the // operator through the Pipelines tools (show_file, show_image, show_html, -// notify). The chat_subscribe reply carries the retained list in the same +// show_diff, notify). The chat_subscribe reply carries the retained list in the same // ChatShown field. const ChatShown = "chat_shown" @@ -11,12 +11,13 @@ const ( ChatShownFile = "file" ChatShownImage = "image" ChatShownHTML = "html" + ChatShownDiff = "diff" ChatShownNotify = "notify" ) // ChatShownLimit bounds the items retained per chat, and ChatShownHTMLBudget -// the HTML bytes kept across them: older documents beyond it keep their entry -// but drop the body (Expired). +// the HTML and diff bytes kept across them: older documents and diffs beyond +// it keep their entry but drop the body (Expired). const ( ChatShownLimit = 100 ChatShownHTMLBudget = 2 << 20 @@ -44,6 +45,10 @@ type ChatShownItem struct { Alt string `json:"alt,omitempty"` HTML string `json:"html,omitempty"` HTMLBytes int `json:"html_bytes,omitempty"` + // Diff is a show_diff item's repository comparison; DiffBytes is the size + // of its patches, counted against ChatShownHTMLBudget. + Diff *ChatShownDiffBody `json:"diff,omitempty"` + DiffBytes int `json:"diff_bytes,omitempty"` // HeightHint is the model's preferred inline height in logical pixels. HeightHint int `json:"height_hint,omitempty"` Expired bool `json:"expired,omitempty"` @@ -53,3 +58,19 @@ type ChatShownItem struct { // Detail says why a notification was not delivered. Detail string `json:"detail,omitempty"` } + +// ChatShownDiffBody is one Git comparison a chat's model showed: the work tree +// (or HEAD when CommittedOnly) of Repo against BaseCommit, which is HEAD when +// Base is empty and the merge base with Base otherwise. Changes uses the same +// per-file shape as a run's recorded changes; it is nil once Expired. +type ChatShownDiffBody struct { + Repo string `json:"repo"` + Base string `json:"base,omitempty"` + BaseCommit string `json:"base_commit,omitempty"` + Head string `json:"head,omitempty"` + CommittedOnly bool `json:"committed_only,omitempty"` + Files int `json:"files"` + Additions int `json:"additions"` + Deletions int `json:"deletions"` + Changes *StageFileChanges `json:"changes,omitempty"` +} diff --git a/internal/server/chat_tools_diff.go b/internal/server/chat_tools_diff.go new file mode 100644 index 00000000..d8a4ede3 --- /dev/null +++ b/internal/server/chat_tools_diff.go @@ -0,0 +1,236 @@ +package server + +import ( + "bytes" + "context" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "time" + "unicode" + + "git.a-wan.net/alan/pipelines/internal/chattools" + "git.a-wan.net/alan/pipelines/internal/protocol" + "git.a-wan.net/alan/pipelines/internal/workenv" +) + +// The show_diff bounds. A larger diff is shown partially, never cut silently: +// files past the count are omitted (and counted), and a patch over the per-file +// or total size keeps its entry with a no-patch reason. +const ( + showDiffMaxFiles = 300 + showDiffMaxFileBytes = 256 << 10 + showDiffMaxBytes = 1 << 20 + showDiffTimeout = 30 * time.Second + // The empty tree is the base of a repository without commits. + gitEmptyTree = "4b825dc642cb6eb9a060e54bf8d69288fbee4904" +) + +type showDiffRequest struct { + Path string + Base string + CommittedOnly bool + Title string +} + +// runShowDiff compares a repository inside the operator's home and records +// the per-file patches as one diff item. +func (s *chatToolService) runShowDiff(ctx context.Context, sessionID string, in showDiffRequest) (*protocol.ChatShownItem, chattools.Result) { + fail := func(err error) (*protocol.ChatShownItem, chattools.Result) { + return nil, chattools.Result{Text: "Nothing was shown: " + err.Error(), IsError: true} + } + base := strings.TrimSpace(in.Base) + if in.CommittedOnly && base == "" { + return fail(errors.New("committed_only needs a base to compare against")) + } + if base != "" && !validGitRevision(base) { + return fail(fmt.Errorf("base %q is not a branch, tag or commit name", base)) + } + directory, err := s.resolveDiffDirectory(ctx, sessionID, in.Path) + if err != nil { + return fail(err) + } + ctx, cancel := context.WithTimeout(ctx, showDiffTimeout) + defer cancel() + git := gitRunner{ctx: ctx, directory: directory} + top, err := git.output("rev-parse", "--show-toplevel") + if err != nil { + return fail(fmt.Errorf("%s is not inside a Git repository", directory)) + } + git.directory = strings.TrimSpace(top) + + body := protocol.ChatShownDiffBody{Repo: git.directory, Base: base, CommittedOnly: in.CommittedOnly} + head, headErr := git.output("rev-parse", "--verify", "--quiet", "HEAD^{commit}") + head = strings.TrimSpace(head) + if branch, err := git.output("symbolic-ref", "--quiet", "--short", "HEAD"); err == nil { + body.Head = strings.TrimSpace(branch) + } else if head != "" { + body.Head = shortCommit(head) + } + var compare string + switch { + case base != "": + if headErr != nil || head == "" { + return fail(errors.New("this repository has no commits yet, so there is nothing to compare with " + base)) + } + target, err := git.output("rev-parse", "--verify", "--quiet", "--end-of-options", base+"^{commit}") + if err != nil { + return fail(fmt.Errorf("base %q is not a commit in %s", base, git.directory)) + } + mergeBase, err := git.output("merge-base", strings.TrimSpace(target), head) + if err != nil { + return fail(fmt.Errorf("%s and HEAD share no history", base)) + } + compare = strings.TrimSpace(mergeBase) + case headErr == nil && head != "": + compare = head + default: + compare = gitEmptyTree + } + if compare != gitEmptyTree { + body.BaseCommit = shortCommit(compare) + } + revisions := []string{compare} + if in.CommittedOnly { + revisions = append(revisions, head) + } + + changes, err := collectGitDiff(git, revisions, !in.CommittedOnly) + if err != nil { + return fail(err) + } + body.Changes = changes + diffBytes := 0 + for _, file := range changes.Files { + added, removed := patchLineCounts(file.Patch) + body.Additions += added + body.Deletions += removed + diffBytes += len(file.Patch) + } + body.Files = len(changes.Files) + changes.Omitted + item := protocol.ChatShownItem{Kind: protocol.ChatShownDiff, Title: in.Title, Diff: &body, DiffBytes: diffBytes} + return &item, chattools.Result{Text: showDiffSummary(body)} +} + +func showDiffSummary(body protocol.ChatShownDiffBody) string { + against := "HEAD" + switch { + case body.Base != "" && body.CommittedOnly: + against = fmt.Sprintf("%s (merge base %s), committed changes only", body.Base, body.BaseCommit) + case body.Base != "": + against = fmt.Sprintf("%s (merge base %s), including uncommitted work", body.Base, body.BaseCommit) + case body.BaseCommit == "": + against = "an empty repository" + } + if body.Files == 0 { + return fmt.Sprintf("Shown in the operator's Pipelines chat: no changes in %s against %s.", body.Repo, against) + } + text := fmt.Sprintf("Shown in the operator's Pipelines chat: %d changed %s (+%d -%d) in %s against %s.", + body.Files, plural(body.Files, "file", "files"), body.Additions, body.Deletions, body.Repo, against) + if body.Changes != nil && body.Changes.Status != "complete" { + text += " The diff is partial: " + body.Changes.Diagnostic + } + return text +} + +func plural(n int, one, many string) string { + if n == 1 { + return one + } + return many +} + +// resolveDiffDirectory applies the home containment of show_file to a +// directory, defaulting to the chat's working directory. +func (s *chatToolService) resolveDiffDirectory(ctx context.Context, sessionID, requested string) (string, error) { + requested = strings.TrimSpace(requested) + home, err := s.home() + if err != nil { + return "", err + } + realHome, err := filepath.EvalSymlinks(filepath.Clean(home)) + if err != nil { + return "", errors.New("the operator's home directory is not available") + } + if requested == "" || (!filepath.IsAbs(requested) && !strings.HasPrefix(requested, "~")) { + directory := s.chatSessionDirectory(ctx, sessionID) + if directory == "" { + return "", errors.New("this chat has no recorded working directory; pass the repository's absolute path") + } + requested = filepath.Join(directory, requested) + } + target, err := expandHomePath(filepath.Clean(home), requested) + if err != nil { + return "", err + } + _, realTarget, err := resolveInsideHome(realHome, target) + if err != nil { + if strings.Contains(err.Error(), "outside the home directory") { + return "", errors.New(requested + " is outside the operator's home directory; only repositories inside home can be shown") + } + return "", err + } + info, err := os.Stat(realTarget) + if err != nil { + return "", describePathError(requested, err) + } + if !info.IsDir() { + return "", errors.New(requested + " is not a directory; pass the repository directory") + } + return realTarget, nil +} + +// validGitRevision accepts a plain revision name: no option prefix, range, +// whitespace or control character, so it can only name one commit. +func validGitRevision(name string) bool { + if name == "" || len(name) > 200 || strings.HasPrefix(name, "-") || strings.Contains(name, "..") { + return false + } + for _, r := range name { + if unicode.IsSpace(r) || unicode.IsControl(r) { + return false + } + } + return true +} + +// gitRunner runs git in one directory with the operator's environment and no +// pager, external diff, text conversion or path quoting. +type gitRunner struct { + ctx context.Context + directory string +} + +func (g gitRunner) command(args ...string) *exec.Cmd { + full := append([]string{"-c", "core.quotepath=false", "-c", "core.fsmonitor=false", "-C", g.directory}, args...) + cmd := exec.CommandContext(g.ctx, "git", full...) + cmd.Env = append(workenv.CommandEnv(g.ctx, g.directory), "GIT_PAGER=cat", "GIT_OPTIONAL_LOCKS=0", "LC_ALL=C") + return cmd +} + +func (g gitRunner) output(args ...string) (string, error) { + var stdout, stderr bytes.Buffer + cmd := g.command(args...) + cmd.Stdout, cmd.Stderr = &stdout, &stderr + if err := cmd.Run(); err != nil { + return "", fmt.Errorf("git %s: %v: %s", args[0], err, strings.TrimSpace(stderr.String())) + } + return stdout.String(), nil +} + +// patch returns one file's patch. A `diff --no-index` exits 1 when the files +// differ, which is its success. +func (g gitRunner) patch(noIndex bool, args ...string) (string, error) { + var stdout, stderr bytes.Buffer + cmd := g.command(args...) + cmd.Stdout, cmd.Stderr = &stdout, &stderr + err := cmd.Run() + var exit *exec.ExitError + if err != nil && !(noIndex && errors.As(err, &exit) && exit.ExitCode() == 1) { + return "", fmt.Errorf("git diff: %v: %s", err, strings.TrimSpace(stderr.String())) + } + return stdout.String(), nil +} diff --git a/internal/server/chat_tools_diff_collect.go b/internal/server/chat_tools_diff_collect.go new file mode 100644 index 00000000..4a3f8b85 --- /dev/null +++ b/internal/server/chat_tools_diff_collect.go @@ -0,0 +1,146 @@ +package server + +import ( + "fmt" + "os" + "path/filepath" + "sort" + "strings" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +type gitDiffEntry struct { + path string + kind string + untracked bool +} + +// collectGitDiff lists the files changed between revisions (one revision: the +// work tree against it; two: those commits) plus, when includeUntracked, the +// untracked files Git does not ignore, and reads each file's patch within the +// show_diff bounds. +func collectGitDiff(git gitRunner, revisions []string, includeUntracked bool) (*protocol.StageFileChanges, error) { + nameArgs := append([]string{"diff", "--name-status", "-z", "--no-renames"}, revisions...) + names, err := git.output(append(nameArgs, "--")...) + if err != nil { + return nil, err + } + entries := parseNameStatus(names) + if includeUntracked { + others, err := git.output("ls-files", "--others", "--exclude-standard", "-z") + if err != nil { + return nil, err + } + for _, path := range strings.Split(others, "\x00") { + if path != "" { + entries = append(entries, gitDiffEntry{path: path, kind: "added", untracked: true}) + } + } + } + sort.SliceStable(entries, func(i, j int) bool { return entries[i].path < entries[j].path }) + + changes := &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{}} + used := 0 + var partial []string + for i, entry := range entries { + if i >= showDiffMaxFiles { + changes.Omitted = len(entries) - showDiffMaxFiles + partial = append(partial, fmt.Sprintf("%d more files are not listed (the limit is %d)", changes.Omitted, showDiffMaxFiles)) + break + } + file := protocol.FileChange{Path: entry.path, Kind: entry.kind} + patch, reason, err := readEntryPatch(git, revisions, entry) + if err != nil { + return nil, err + } + switch { + case reason != "": + case isBinaryPatch(patch): + file.Binary, reason = true, "binary" + case len(patch) > showDiffMaxFileBytes: + reason = "file_size" + case used+len(patch) > showDiffMaxBytes: + reason = "budget" + } + if reason == "" { + file.Patch = patch + used += len(patch) + } else { + file.NoPatch, file.NoPatchReason = true, reason + file.Truncated = reason != "binary" + } + if reason == "file_size" || reason == "budget" { + partial = appendOnce(partial, "some patches were left out by the size limit") + } + changes.Files = append(changes.Files, file) + } + if len(partial) > 0 { + changes.Status = "partial" + changes.Diagnostic = strings.Join(partial, "; ") + "." + } + return changes, nil +} + +// readEntryPatch returns one file's patch, or the no-patch reason when its +// content is not read. +func readEntryPatch(git gitRunner, revisions []string, entry gitDiffEntry) (string, string, error) { + flags := []string{"diff", "--no-color", "--no-ext-diff", "--no-textconv", "--no-renames"} + if entry.untracked { + info, err := os.Lstat(filepath.Join(git.directory, entry.path)) + if err != nil { + return "", "git_status", nil + } + if info.Mode().IsRegular() && info.Size() > showDiffMaxFileBytes { + return "", "file_size", nil + } + patch, err := git.patch(true, append(flags, "--no-index", "--", os.DevNull, entry.path)...) + return patch, "", err + } + args := append(append(flags, revisions...), "--", ":(literal)"+entry.path) + patch, err := git.patch(false, args...) + return patch, "", err +} + +// parseNameStatus reads `git diff --name-status -z --no-renames`: a status +// letter, then the path, each NUL-terminated. +func parseNameStatus(out string) []gitDiffEntry { + fields := strings.Split(out, "\x00") + var entries []gitDiffEntry + for i := 0; i+1 < len(fields); i += 2 { + status, path := fields[i], fields[i+1] + if status == "" || path == "" { + continue + } + kind := "modified" + switch status[0] { + case 'A': + kind = "added" + case 'D': + kind = "deleted" + } + entries = append(entries, gitDiffEntry{path: path, kind: kind}) + } + return entries +} + +func isBinaryPatch(patch string) bool { + for _, line := range strings.Split(patch, "\n") { + if strings.HasPrefix(line, "@@") { + return false + } + if strings.HasPrefix(line, "Binary files ") || line == "GIT binary patch" { + return true + } + } + return false +} + +func appendOnce(list []string, value string) []string { + for _, existing := range list { + if existing == value { + return list + } + } + return append(list, value) +} diff --git a/internal/server/chat_tools_diff_test.go b/internal/server/chat_tools_diff_test.go new file mode 100644 index 00000000..e1526c14 --- /dev/null +++ b/internal/server/chat_tools_diff_test.go @@ -0,0 +1,199 @@ +package server + +import ( + "context" + "os" + "os/exec" + "path/filepath" + "strings" + "testing" + + "git.a-wan.net/alan/pipelines/internal/protocol" +) + +// showDiffRepo makes a repository inside the fixture home: main has a.txt, the +// checked-out feature branch modifies it and adds b.txt in commits, and the +// work tree has an unstaged change to b.txt plus an untracked u.txt. +func showDiffRepo(t *testing.T, home string) string { + t.Helper() + repo := filepath.Join(home, "repo") + if err := os.MkdirAll(repo, 0o755); err != nil { + t.Fatal(err) + } + git := func(args ...string) { + t.Helper() + cmd := exec.Command("git", append([]string{"-c", "user.name=t", "-c", "user.email=t@example.com", "-c", "commit.gpgsign=false", "-C", repo}, args...)...) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("git %v: %v\n%s", args, err, out) + } + } + write := func(name, content string) { + t.Helper() + if err := os.WriteFile(filepath.Join(repo, name), []byte(content), 0o644); err != nil { + t.Fatal(err) + } + } + git("init", "-q", "-b", "main") + write("a.txt", "one\ntwo\n") + git("add", ".") + git("commit", "-q", "-m", "base") + git("switch", "-q", "-c", "feature") + write("a.txt", "one\nTWO\n") + write("b.txt", "new\n") + git("add", ".") + git("commit", "-q", "-m", "feature") + write("b.txt", "new\nmore\n") + write("u.txt", "untracked\n") + return repo +} + +func diffItem(t *testing.T, f *chatToolFixture, session string) protocol.ChatShownItem { + t.Helper() + items := f.items(t, session) + if len(items) == 0 { + t.Fatal("no item recorded") + } + item := items[len(items)-1] + if item.Kind != protocol.ChatShownDiff || item.Diff == nil || item.Diff.Changes == nil { + t.Fatalf("want a diff item, got %+v", item) + } + return item +} + +func diffPaths(item protocol.ChatShownItem) map[string]string { + paths := map[string]string{} + for _, file := range item.Diff.Changes.Files { + paths[file.Path] = file.Kind + } + return paths +} + +func TestShowDiffComparesAgainstHeadOrABase(t *testing.T) { + f := newChatToolFixture(t) + repo := showDiffRepo(t, f.home) + token := f.token(t, chatToolSessionScope+"ses_d") + + r := f.call(token, "show_diff", `{"path":"`+repo+`"}`) + if r.IsError { + t.Fatal(r.Text) + } + item := diffItem(t, f, "ses_d") + if got := diffPaths(item); len(got) != 2 || got["b.txt"] != "modified" || got["u.txt"] != "added" { + t.Fatalf("uncommitted diff lists the work tree change and the untracked file: %v", got) + } + if item.Diff.Head != "feature" || item.Diff.Base != "" || item.Diff.Additions != 2 || item.Diff.Deletions != 0 { + t.Fatalf("summary: %+v", item.Diff) + } + if !strings.Contains(r.Text, "2 changed files (+2 -0)") { + t.Fatalf("model text: %s", r.Text) + } + + if r := f.call(token, "show_diff", `{"path":"~/repo","base":"main","title":"PR preview"}`); r.IsError { + t.Fatal(r.Text) + } + item = diffItem(t, f, "ses_d") + if got := diffPaths(item); len(got) != 3 || got["a.txt"] != "modified" || got["b.txt"] != "added" || got["u.txt"] != "added" { + t.Fatalf("a base diff covers the branch and uncommitted work: %v", got) + } + if item.Title != "PR preview" || item.Diff.Base != "main" || item.Diff.BaseCommit == "" || item.DiffBytes == 0 { + t.Fatalf("summary: %+v", item) + } + for _, file := range item.Diff.Changes.Files { + if file.Path == "a.txt" && !strings.Contains(file.Patch, "-two\n+TWO") { + t.Fatalf("a.txt patch: %q", file.Patch) + } + } + + if r := f.call(token, "show_diff", `{"path":"`+repo+`","base":"main","committed_only":true}`); r.IsError { + t.Fatal(r.Text) + } + item = diffItem(t, f, "ses_d") + got := diffPaths(item) + if len(got) != 2 || got["a.txt"] != "modified" || got["b.txt"] != "added" { + t.Fatalf("committed_only leaves out the work tree: %v", got) + } + for _, file := range item.Diff.Changes.Files { + if file.Path == "b.txt" && strings.Contains(file.Patch, "more") { + t.Fatal("committed_only must not include the unstaged line") + } + } +} + +func TestShowDiffRefusals(t *testing.T) { + f := newChatToolFixture(t) + repo := showDiffRepo(t, f.home) + plain := filepath.Join(f.home, "plain") + if err := os.MkdirAll(plain, 0o755); err != nil { + t.Fatal(err) + } + outside := t.TempDir() + token := f.token(t, chatToolSessionScope+"ses_r") + for _, tc := range []struct{ args, want string }{ + {`{"path":"` + outside + `"}`, "outside the operator's home"}, + {`{"path":"` + plain + `"}`, "not inside a Git repository"}, + {`{"path":"` + repo + `","base":"--output=/tmp/x"}`, "is not a branch"}, + {`{"path":"` + repo + `","base":"main..feature"}`, "is not a branch"}, + {`{"path":"` + repo + `","base":"nope"}`, "is not a commit"}, + {`{"path":"` + repo + `","committed_only":true}`, "needs a base"}, + {`{}`, "no recorded working directory"}, + } { + r := f.call(token, "show_diff", tc.args) + if !r.IsError || !strings.Contains(r.Text, tc.want) { + t.Errorf("%s: want refusal %q, got %+v", tc.args, tc.want, r) + } + } + if items := f.items(t, "ses_r"); len(items) != 0 { + t.Fatalf("refusals record nothing: %+v", items) + } +} + +func TestShowDiffLeavesOutOversizedPatchesAndSaysSo(t *testing.T) { + f := newChatToolFixture(t) + repo := showDiffRepo(t, f.home) + big := strings.Repeat("line of text\n", (showDiffMaxFileBytes/13)+10) + if err := os.WriteFile(filepath.Join(repo, "big.txt"), []byte(big), 0o644); err != nil { + t.Fatal(err) + } + if err := os.WriteFile(filepath.Join(repo, "blob.bin"), []byte{0, 1, 2, 0, 3}, 0o644); err != nil { + t.Fatal(err) + } + token := f.token(t, chatToolSessionScope+"ses_big") + r := f.call(token, "show_diff", `{"path":"`+repo+`"}`) + if r.IsError { + t.Fatal(r.Text) + } + item := diffItem(t, f, "ses_big") + if item.Diff.Changes.Status != "partial" || !strings.Contains(r.Text, "partial") { + t.Fatalf("an oversized patch makes the diff partial: %+v / %s", item.Diff.Changes, r.Text) + } + for _, file := range item.Diff.Changes.Files { + switch file.Path { + case "big.txt": + if file.Patch != "" || file.NoPatchReason != "file_size" || !file.Truncated { + t.Fatalf("big.txt: %+v", file) + } + case "blob.bin": + if !file.Binary || file.Patch != "" { + t.Fatalf("blob.bin: %+v", file) + } + } + } +} + +func TestShowDiffBodiesShareTheRetentionBudget(t *testing.T) { + f := newChatToolFixture(t) + ctx := context.Background() + body := func() *protocol.ChatShownDiffBody { + return &protocol.ChatShownDiffBody{Repo: "/r", Files: 1, Changes: &protocol.StageFileChanges{Status: "complete", Files: []protocol.FileChange{{Path: "a", Kind: "modified", Patch: "@@"}}}} + } + for i, id := range []string{"old", "new"} { + item := protocol.ChatShownItem{ID: id, Kind: protocol.ChatShownDiff, CreatedAt: int64(i + 1), Diff: body(), DiffBytes: protocol.ChatShownHTMLBudget/2 + 1} + if err := f.store.AppendChatShownItem(ctx, "ses_ret", item); err != nil { + t.Fatal(err) + } + } + items := f.items(t, "ses_ret") + if len(items) != 2 || !items[0].Expired || items[0].Diff == nil || items[0].Diff.Changes != nil || items[1].Expired || items[1].Diff.Changes == nil { + t.Fatalf("the older diff drops its patches past the budget: %+v", items) + } +} diff --git a/internal/server/chat_tools_run.go b/internal/server/chat_tools_run.go index 98119dab..af9ddde5 100644 --- a/internal/server/chat_tools_run.go +++ b/internal/server/chat_tools_run.go @@ -26,6 +26,8 @@ func (s *chatToolService) run(ctx context.Context, sessionID, tool string, argum HeightHint int `json:"height_hint"` Message string `json:"message"` Priority string `json:"priority"` + Base string `json:"base"` + Committed bool `json:"committed_only"` } if err := json.Unmarshal(arguments, &in); err != nil { return fail("The arguments must be a JSON object matching the %s schema: %v", tool, err) @@ -72,6 +74,8 @@ func (s *chatToolService) run(ctx context.Context, sessionID, tool string, argum } item := protocol.ChatShownItem{Kind: protocol.ChatShownHTML, Title: title, HTML: in.HTML, HTMLBytes: len(in.HTML), HeightHint: clampHeightHint(in.HeightHint)} return &item, chattools.Result{Text: fmt.Sprintf("Shown in the operator's Pipelines chat (%s, sandboxed: scripts and network are disabled).", humanBytes(int64(len(in.HTML))))} + case chattools.ToolShowDiff: + return s.runShowDiff(ctx, sessionID, showDiffRequest{Path: in.Path, Base: in.Base, CommittedOnly: in.Committed, Title: title}) case chattools.ToolNotify: return s.runNotify(ctx, sessionID, title, in.Message, in.Priority) }