Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

✨ feat(tool-render): open the readFile card by default when it read an image (#20239)

Arvin Xu94812177c0f6
diff --git a/packages/builtin-tool-local-system/package.json b/packages/builtin-tool-local-system/package.json
index 285f808a864..6b39a1e1364 100644
--- a/packages/builtin-tool-local-system/package.json
+++ b/packages/builtin-tool-local-system/package.json
@@ -5,6 +5,7 @@
"exports": {
".": "./src/index.ts",
"./client": "./src/client/index.ts",
+ "./client/displayControls": "./src/client/displayControls.ts",
"./client/executor": "./src/client/executor/index.ts"
},
"main": "./src/index.ts",
diff --git a/packages/builtin-tool-local-system/src/client/displayControls.test.ts b/packages/builtin-tool-local-system/src/client/displayControls.test.ts
new file mode 100644
index 00000000000..cc479853fcc
--- /dev/null
+++ b/packages/builtin-tool-local-system/src/client/displayControls.test.ts
@@ -0,0 +1,55 @@
+import { describe, expect, it } from 'vitest';
+
+import { resolveLocalSystemRenderDisplayControl } from './displayControls';
+
+const imageRead = {
+ images: [{ mediaType: 'image/png', url: 'https://cdn.test/g1.png' }],
+ path: 'shots/g1.png',
+};
+
+describe('resolveLocalSystemRenderDisplayControl', () => {
+ it('opens the card for a read that uploaded an image', () => {
+ expect(resolveLocalSystemRenderDisplayControl('readFile', imageRead)).toBe('expand');
+ });
+
+ it('keeps a text read collapsed so it does not dump a file into the transcript', () => {
+ expect(
+ resolveLocalSystemRenderDisplayControl('readFile', { content: 'const a = 1;', path: 'a.ts' }),
+ ).toBeUndefined();
+ });
+
+ it('keeps the card collapsed while the read is still in flight', () => {
+ expect(resolveLocalSystemRenderDisplayControl('readFile')).toBeUndefined();
+ expect(resolveLocalSystemRenderDisplayControl('readFile', undefined)).toBeUndefined();
+ });
+
+ it('does not open an empty card when the image never got a url', () => {
+ expect(
+ resolveLocalSystemRenderDisplayControl('readFile', {
+ images: [{ mediaType: 'image/png' }],
+ path: 'shots/g1.png',
+ }),
+ ).toBeUndefined();
+ });
+
+ it('opens the card for the legacy readLocalFile alias too', () => {
+ // Older gateways emit `readLocalFile`, and persisted results still carry that
+ // name; `LocalSystemRenders` keeps the alias registered for those historical
+ // messages, so the display control has to answer for both names.
+ expect(resolveLocalSystemRenderDisplayControl('readLocalFile', imageRead)).toBe('expand');
+ });
+
+ it('keeps a legacy-named text read collapsed', () => {
+ expect(
+ resolveLocalSystemRenderDisplayControl('readLocalFile', {
+ content: 'const a = 1;',
+ path: 'a.ts',
+ }),
+ ).toBeUndefined();
+ });
+
+ it('only refines readFile, not the other local-system APIs', () => {
+ expect(resolveLocalSystemRenderDisplayControl('runCommand', imageRead)).toBeUndefined();
+ expect(resolveLocalSystemRenderDisplayControl('grepContent', imageRead)).toBeUndefined();
+ });
+});
diff --git a/packages/builtin-tool-local-system/src/client/displayControls.ts b/packages/builtin-tool-local-system/src/client/displayControls.ts
new file mode 100644
index 00000000000..23de6df971c
--- /dev/null
+++ b/packages/builtin-tool-local-system/src/client/displayControls.ts
@@ -0,0 +1,42 @@
+import type { ReadFileState } from '@lobechat/tool-runtime';
+import type { RenderDisplayControl } from '@lobechat/types';
+
+import { LocalSystemApiName } from '../types';
+
+/**
+ * The read API answers to two names: `readFile` going forward, and the legacy
+ * `readLocalFile` that older gateways emit and that persisted results still
+ * carry — `LocalSystemRenders` keeps the alias registered for exactly those
+ * historical messages, and the local-system execution runtime normalizes the
+ * same pair before dispatch (`LEGACY_API_ALIASES`). A display control that only
+ * answers for the modern name leaves an image read from the legacy path
+ * collapsed while its modern twin opens, so both names are the same API here.
+ */
+const READ_FILE_API_NAMES: readonly string[] = [LocalSystemApiName.readFile, 'readLocalFile'];
+
+/**
+ * Display control for the shared `readFile` card, refined by what was read.
+ *
+ * `readFile` serves two incomparable payloads, so one static default can only be
+ * wrong for one of them:
+ *
+ * - source / document text stays collapsed, because auto-opening it would dump
+ * a whole file into the transcript on every read;
+ * - an image file is the one case where the payload IS the point of the call —
+ * collapsing it hides the thing the agent just looked at, and the user has to
+ * click to unfold a single picture.
+ *
+ * Gated on an actually-uploaded image: `pluginState` is undefined while the call
+ * is in flight, a text read never carries `images`, and a failed upload leaves
+ * no `url`. In any of those the card would open empty, so it stays collapsed.
+ */
+export const resolveLocalSystemRenderDisplayControl = (
+ apiName: string,
+ pluginState?: unknown,
+): RenderDisplayControl | undefined => {
+ if (!READ_FILE_API_NAMES.includes(apiName)) return undefined;
+
+ const images = (pluginState as Partial<ReadFileState> | undefined)?.images;
+
+ return images?.some((image) => !!image.url) ? 'expand' : undefined;
+};
diff --git a/packages/builtin-tools/src/displayControls.test.ts b/packages/builtin-tools/src/displayControls.test.ts
index c3d09e5d40a..a9673d3b2cd 100644
--- a/packages/builtin-tools/src/displayControls.test.ts
+++ b/packages/builtin-tools/src/displayControls.test.ts
@@ -41,6 +41,44 @@ describe('getBuiltinRenderDisplayControl', () => {
expect(getBuiltinRenderDisplayControl('claude-code', 'Read', {})).toBeUndefined();
});
+ it('opens the shared readFile card when either file-system host read an image', () => {
+ const imageRead = {
+ images: [{ mediaType: 'image/png', url: 'https://cdn/a.png' }],
+ path: 'shots/g1.png',
+ };
+
+ expect(getBuiltinRenderDisplayControl('lobe-local-system', 'readFile', imageRead)).toBe(
+ 'expand',
+ );
+ expect(getBuiltinRenderDisplayControl('lobe-cloud-sandbox', 'readFile', imageRead)).toBe(
+ 'expand',
+ );
+ });
+
+ it('opens the shared card for a legacy readLocalFile image read too', () => {
+ const imageRead = {
+ images: [{ mediaType: 'image/png', url: 'https://cdn/a.png' }],
+ path: 'shots/g1.png',
+ };
+
+ expect(getBuiltinRenderDisplayControl('lobe-local-system', 'readLocalFile', imageRead)).toBe(
+ 'expand',
+ );
+ expect(getBuiltinRenderDisplayControl('lobe-cloud-sandbox', 'readLocalFile', imageRead)).toBe(
+ 'expand',
+ );
+ });
+
+ it('leaves a text read on either file-system host undecided', () => {
+ // Text is not the payload of the call, so the card must not auto-open on it.
+ expect(
+ getBuiltinRenderDisplayControl('lobe-local-system', 'readFile', { content: 'openai' }),
+ ).toBeUndefined();
+ expect(
+ getBuiltinRenderDisplayControl('lobe-cloud-sandbox', 'readFile', { content: 'openai' }),
+ ).toBeUndefined();
+ });
+
it('returns undefined without an identifier or apiName', () => {
expect(getBuiltinRenderDisplayControl(undefined, 'Read')).toBeUndefined();
expect(getBuiltinRenderDisplayControl('claude-code', undefined)).toBeUndefined();
diff --git a/packages/builtin-tools/src/displayControls.ts b/packages/builtin-tools/src/displayControls.ts
index 8ee1b5bfb1d..24b53d46e75 100644
--- a/packages/builtin-tools/src/displayControls.ts
+++ b/packages/builtin-tools/src/displayControls.ts
@@ -1,5 +1,8 @@
import { ClaudeCodeIdentifier } from '@lobechat/builtin-tool-claude-code';
import { resolveClaudeCodeRenderDisplayControl } from '@lobechat/builtin-tool-claude-code/client/displayControls';
+import { CloudSandboxIdentifier } from '@lobechat/builtin-tool-cloud-sandbox';
+import { LocalSystemIdentifier } from '@lobechat/builtin-tool-local-system';
+import { resolveLocalSystemRenderDisplayControl } from '@lobechat/builtin-tool-local-system/client/displayControls';
import { type RenderDisplayControl } from '@lobechat/types';
import { CodexRenderDisplayControls } from './codex/displayControls';
@@ -21,6 +24,11 @@ const getBuiltinRenderDisplayControls = (): Record<
/**
* Packages whose display control can't be decided from `apiName` alone — the
* same API renders differently depending on what its result carries.
+ *
+ * Both file-system hosts are listed because they render the SAME `readFile`
+ * card (`LocalSystemRenders.readFile`), so whether an image read opens itself is
+ * a property of that card rather than of the host. The heterogeneous CLIs'
+ * `read` reuses the card too, but only ever returns text, so it needs no entry.
*/
const getDynamicRenderDisplayControlResolvers = (): Record<
string,
@@ -28,6 +36,8 @@ const getDynamicRenderDisplayControlResolvers = (): Record<
> => {
return {
[ClaudeCodeIdentifier]: resolveClaudeCodeRenderDisplayControl,
+ [CloudSandboxIdentifier]: resolveLocalSystemRenderDisplayControl,
+ [LocalSystemIdentifier]: resolveLocalSystemRenderDisplayControl,
[QODER_IDENTIFIER]: resolveClaudeCodeRenderDisplayControl,
};
};