π€― 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.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
β‘οΈ perf(tools): project grepContent, whose match list nothing renders (#19732)
Arvin Xude5afafa017f
diff --git a/packages/tool-view-model/src/project.test.ts b/packages/tool-view-model/src/project.test.tsindex 9fc8f559fa3..f354ccc1f2c 100644--- a/packages/tool-view-model/src/project.test.ts+++ b/packages/tool-view-model/src/project.test.ts@@ -144,6 +144,8 @@ describe('registry', () => {'codex/command_execution','lobe-agent-documents/listDocuments','lobe-agent-documents/readDocument',+ 'lobe-cloud-sandbox/grepContent',+ 'lobe-local-system/grepContent','lobe-local-system/readFile','lobe-local-system/runCommand','lobe-user-memory/searchUserMemory',@@ -216,3 +218,77 @@ describe('searchUserMemoryProjector', () => {expect(projected.pluginState).toEqual({ resultCount: 0 });});});++describe('grepContentProjector', () => {+ const grepMessage = (pluginState: unknown, identifier = 'lobe-local-system') =>+ toolMessage({+ content: 'RAW BODY',+ plugin: { apiName: 'grepContent', arguments: '{}', identifier },+ pluginState,+ } as Partial<UIChatMessage>);++ it.each(['lobe-local-system', 'lobe-cloud-sandbox'])(+ 'drops the match list on %s and leaves the count the chip reads',+ (identifier) => {+ const matches = Array.from({ length: 40 }, (_, index) => `/repo/src/file-${index}.ts`);++ const [projected] = projectToolViewModels(+ [grepMessage({ matches, pattern: 'useEffect', totalMatches: 40 }, identifier)],+ getToolProjector,+ );++ expect(projected.pluginState).toEqual({ pattern: 'useEffect', totalMatches: 40 });+ expect(projected.content).toBe('');+ expect(projected.contentLength).toBe('RAW BODY'.length);+ expect(projected.payloadOmitted).toBe('render');+ },+ );++ it('keeps a zero-match state renderable', () => {+ const [projected] = projectToolViewModels(+ [grepMessage({ matches: [], pattern: 'nothing', totalMatches: 0 })],+ getToolProjector,+ );++ expect(projected.pluginState).toEqual({ pattern: 'nothing', totalMatches: 0 });+ });+});++describe('a row that is still running', () => {+ // `hasToolResultBody` reads the sentinel to mean "no result yet". Projecting+ // it would leave content '' behind contentLength 3, which reads as finished.+ it.each([+ ['with a projector', 'lobe-local-system', 'grepContent'],+ ['with a projector that takes the no-state branch', 'lobe-local-system', 'readFile'],+ ['without one', 'some-mcp-plugin', 'doThing'],+ ])('is untouched %s', (_label, identifier, apiName) => {+ const running = toolMessage({+ content: LOADING_FLAT,+ plugin: { apiName, arguments: '{}', identifier },+ pluginState: undefined,+ } as Partial<UIChatMessage>);++ const [projected] = projectToolViewModels([running], getToolProjector);++ expect(projected).toEqual(running);+ expect(projected.content).toBe(LOADING_FLAT);+ expect(projected.contentLength).toBeUndefined();+ expect(projected.payloadOmitted).toBeUndefined();+ });++ it('still projects once the real body lands', () => {+ const [projected] = projectToolViewModels(+ [+ toolMessage({+ content: 'RAW BODY',+ plugin: { apiName: 'grepContent', arguments: '{}', identifier: 'lobe-local-system' },+ pluginState: { matches: ['/a.ts'], pattern: 'x', totalMatches: 1 },+ } as Partial<UIChatMessage>),+ ],+ getToolProjector,+ );++ expect(projected.pluginState).toEqual({ pattern: 'x', totalMatches: 1 });+ expect(projected.payloadOmitted).toBe('render');+ });+});diff --git a/packages/tool-view-model/src/project.ts b/packages/tool-view-model/src/project.tsindex cd6c93eafd5..bbeede5fa4f 100644--- a/packages/tool-view-model/src/project.ts+++ b/packages/tool-view-model/src/project.ts@@ -31,15 +31,21 @@ export type ProjectorResolver = (const BODY_ONLY_PROJECTION: ToolProjection = { content: null, storedPayloadNeededBy: 'render' };const projectToolMessage = (message: UIChatMessage, resolve: ProjectorResolver): UIChatMessage => {+ // Nothing is projected while the row is still running. The streaming sentinel+ // in `content` is how it is known to be running (`hasToolResultBody`), and+ // projecting it away would leave an empty body behind a non-zero+ // `contentLength` β a row that reads as finished and whose result never+ // arrives. This guards the projectors too: a projector is written against a+ // settled payload and has no reason to see a half-finished one.+ if (message.content === LOADING_FLAT) return message;+const projector = resolve(message.plugin?.identifier, message.plugin?.apiName);// No projector: keep the state whole and drop just the body β and only when- // there is a settled body. An empty result would be flagged for a fetch that- // returns nothing, and the streaming sentinel is the one string whose- // presence in `content` is how the row is known to be still running.+ // there is a body. An empty result would be flagged for a fetch that returns+ // nothing.if (!projector) {- const hasSettledBody = !!message.content && message.content !== LOADING_FLAT;- return hasSettledBody ? applyProjection(message, BODY_ONLY_PROJECTION) : message;+ return message.content ? applyProjection(message, BODY_ONLY_PROJECTION) : message;}let projection;diff --git a/packages/tool-view-model/src/projectToolEvent.test.ts b/packages/tool-view-model/src/projectToolEvent.test.tsindex cab55471627..46be00b52be 100644--- a/packages/tool-view-model/src/projectToolEvent.test.ts+++ b/packages/tool-view-model/src/projectToolEvent.test.ts@@ -78,6 +78,18 @@ describe('projectToolEndResult', () => {it.each([['lobe-agent-documents', 'listDocuments', { documents: [{ id: 'a' }] }, { documentCount: 1 }],['lobe-user-memory', 'searchUserMemory', { identities: [{ id: 'a' }] }, { resultCount: 1 }],+ [+ 'lobe-local-system',+ 'grepContent',+ { matches: ['/a.ts'], pattern: 'x', totalMatches: 1 },+ { pattern: 'x', totalMatches: 1 },+ ],+ [+ 'lobe-cloud-sandbox',+ 'grepContent',+ { matches: ['/a.ts'], pattern: 'x', totalMatches: 1 },+ { pattern: 'x', totalMatches: 1 },+ ],])('drops the body of %s/%s and projects its state', (identifier, apiName, state, expected) => {const projected = projectToolEndResult(toolEndData({ content: 'RAW BODY', state, success: true }, identifier, apiName),diff --git a/packages/tool-view-model/src/projectors/grepContent.ts b/packages/tool-view-model/src/projectors/grepContent.tsnew file mode 100644index 00000000000..8dd58a2d2d0--- /dev/null+++ b/packages/tool-view-model/src/projectors/grepContent.ts@@ -0,0 +1,28 @@+import type { ToolProjector } from '../types';++const isRecord = (value: unknown): value is Record<string, unknown> =>+ !!value && typeof value === 'object' && !Array.isArray(value);++/**+ * `grepContent`, for both the local system and the cloud sandbox β they share+ * one inspector (`shared-tool-ui/Inspector/GrepContent`) and neither registers+ * a render.+ *+ * That inspector reads `totalMatches`, which the state already carries. The+ * `matches` array beside it β every matching path, sometimes with the matching+ * line β has no reader on screen at all, and it is essentially the whole state:+ * on a sampled local-system session, 26 kB of it against the chip's one number.+ *+ * So `matches` goes and `totalMatches` stays where it was; nothing to pin. The+ * body is the model's copy of the same result and goes with it, coming back+ * through the fallback render when the row is expanded.+ */+export const grepContentProjector: ToolProjector = ({ content, pluginState }) => {+ if (!isRecord(pluginState)) {+ return content ? { content: null, storedPayloadNeededBy: 'render' } : undefined;+ }++ const { matches: _matches, ...rest } = pluginState;++ return { content: null, pluginState: rest, storedPayloadNeededBy: 'render' };+};diff --git a/packages/tool-view-model/src/registry.ts b/packages/tool-view-model/src/registry.tsindex c7f7fd7e74b..7c7c6ac02b8 100644--- a/packages/tool-view-model/src/registry.ts+++ b/packages/tool-view-model/src/registry.ts@@ -1,4 +1,5 @@import { listDocumentsProjector, readDocumentProjector } from './projectors/agentDocuments';+import { grepContentProjector } from './projectors/grepContent';import { runCommandProjector } from './projectors/localSystem';import { readFileProjector } from './projectors/readFile';import { searchUserMemoryProjector } from './projectors/userMemory';@@ -33,7 +34,12 @@ const toolProjectors: Record<string, Record<string, ToolProjector>> = {listDocuments: listDocumentsProjector,readDocument: readDocumentProjector,},+ // `grepContent` is the same tool on both hosts, down to the shared inspector.+ 'lobe-cloud-sandbox': {+ grepContent: grepContentProjector,+ },'lobe-local-system': {+ grepContent: grepContentProjector,readFile: readFileProjector,runCommand: runCommandProjector,},@@ -72,7 +78,10 @@ const eventBodyUnused: ReadonlySet<string> = new Set([// `result.success`; a read is neither.'lobe-agent-documents/listDocuments','lobe-agent-documents/readDocument',- // `lobe-local-system`'s hook is scoped to `runCommand`.+ // `lobe-cloud-sandbox` registers no hook, and `lobe-local-system`'s is scoped+ // to `runCommand`.+ 'lobe-cloud-sandbox/grepContent',+ 'lobe-local-system/grepContent','lobe-local-system/readFile',// `lobe-user-memory` and `lobe-web-browsing` register no hook at all.'lobe-user-memory/searchUserMemory',