Repository navigation
fix(components): cap inspect-component and defer-blocks answers #300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
9ba89c9
fix(components): cap inspect-component and defer-blocks answers
erkamyaman c14e892
fix(components): keep capped answers within the limit and shorten dir…
erkamyaman 967dd56
Merge remote-tracking branch 'upstream/main' into components/agent-ou…
erkamyaman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/devtools/src/rpc/__tests__/component-detail-text.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import type { ComponentDetail, ComponentProp } from '../../types.ts'; | ||
| import { DETAIL_VALUE_MAX, inspectComponentText } from '../component-detail-text.ts'; | ||
| import { MAX_TOOL_CHARS } from '../router-tools.ts'; | ||
|
|
||
| const detail = (overrides: Partial<ComponentDetail> = {}): ComponentDetail => ({ | ||
| id: 'c2', | ||
| name: 'Card', | ||
| tag: 'app-card', | ||
| path: 'app-root > app-card', | ||
| inputs: [{ name: 'title', prop: 'title', value: 'Rome' }], | ||
| outputs: [], | ||
| properties: [], | ||
| listeners: [], | ||
| directives: [], | ||
| dependencies: [], | ||
| ...overrides, | ||
| }); | ||
|
|
||
| const HEADING = 'Live detail of `Card`.'; | ||
| const body = (text: string) => JSON.parse(text.slice(text.indexOf('\n{') + 1)) as ComponentDetail; | ||
| const prop = (name: string, value: unknown): ComponentProp => ({ name, prop: name, value }); | ||
|
|
||
| describe('inspectComponentText', () => { | ||
| it('returns the heading and the whole detail when it fits', () => { | ||
| const text = inspectComponentText(detail(), HEADING); | ||
| expect(text.startsWith(`${HEADING}\n\n{`)).toBe(true); | ||
| expect(body(text)).toEqual(detail()); | ||
| expect(text).not.toMatch(/not shown|only the start/); | ||
| }); | ||
|
|
||
| it('cuts long values to their start and names them', () => { | ||
| const big = 'x'.repeat(30_000); | ||
| const text = inspectComponentText( | ||
| detail({ | ||
| inputs: [prop('title', 'Rome'), prop('rows', big)], | ||
| properties: [prop('cache', { blob: big })], | ||
| directives: [{ name: 'Tooltip', inputs: [prop('text', big)], outputs: [] }], | ||
| }), | ||
| HEADING, | ||
| ); | ||
| expect(text.length).toBeLessThanOrEqual(MAX_TOOL_CHARS); | ||
| expect(text).toContain( | ||
| '3 values are longer than 1,000 characters and show only the start of their JSON: `rows`, `cache`, `Tooltip.text`.', | ||
| ); | ||
| expect(text).toContain( | ||
| 'The `detail` of the pangular:component-tree resource has every value in full.', | ||
| ); | ||
| const json = body(text); | ||
| expect(json.inputs[0].value).toBe('Rome'); | ||
| expect(json.inputs[1].value).toBe(`${JSON.stringify(big).slice(0, DETAIL_VALUE_MAX)}…`); | ||
| expect(json.directives[0].inputs[0].value).toMatch(/…$/); | ||
| }); | ||
|
|
||
| it('keeps fewer entries per list and says how many are not shown', () => { | ||
| const properties = Array.from({ length: 60 }, (_, n) => prop(`p${n}`, 'v'.repeat(900))); | ||
| const dependencies = Array.from({ length: 60 }, (_, n) => ({ | ||
| from: 'Card', | ||
| token: `Service${n}`, | ||
| flags: [], | ||
| providedBy: 'r1', | ||
| })); | ||
| const text = inspectComponentText(detail({ properties, dependencies }), HEADING); | ||
| expect(text.length).toBeLessThanOrEqual(MAX_TOOL_CHARS); | ||
| expect(text).toContain( | ||
| 'Each list shows its first 10 entries: 50 more properties, 50 more dependencies not shown.', | ||
| ); | ||
| const json = body(text); | ||
| expect(json.properties).toHaveLength(10); | ||
| expect(json.dependencies).toHaveLength(10); | ||
| expect(json.inputs).toHaveLength(1); | ||
| }); | ||
|
|
||
| it('shortens the inputs and outputs of a directive and counts what it left out', () => { | ||
| const inputs = Array.from({ length: 400 }, (_, n) => prop(`i${n}`, 'v'.repeat(200))); | ||
| const outputs = Array.from({ length: 300 }, (_, n) => ({ | ||
| name: `o${n}`, | ||
| prop: `o${n}`, | ||
| listened: false, | ||
| })); | ||
| const text = inspectComponentText( | ||
| detail({ directives: [{ name: 'Tooltip', inputs, outputs }] }), | ||
| HEADING, | ||
| ); | ||
| expect(text.length).toBeLessThanOrEqual(MAX_TOOL_CHARS); | ||
| expect(text).not.toContain('(truncated;'); | ||
| expect(text).toContain( | ||
| 'Each list shows its first 40 entries: 360 more Tooltip inputs, 260 more Tooltip outputs not shown.', | ||
| ); | ||
| const [directive] = body(text).directives; | ||
| expect(directive.inputs).toHaveLength(40); | ||
| expect(directive.outputs).toHaveLength(40); | ||
| }); | ||
|
|
||
| it('falls back to the shared cut with a pointer to the full detail', () => { | ||
| const text = inspectComponentText(detail({ path: 'a > '.repeat(10_000) }), HEADING); | ||
| expect(text.length).toBeLessThanOrEqual(MAX_TOOL_CHARS); | ||
| expect(text).toMatch( | ||
| /… \(truncated; read the `detail` of the pangular:component-tree resource for every value in full\)$/, | ||
| ); | ||
| }); | ||
|
|
||
| it('keeps redacted values as the page sent them', () => { | ||
| const text = inspectComponentText( | ||
| detail({ | ||
| inputs: [prop('token', '[redacted]'), prop('rows', 'x'.repeat(30_000))], | ||
| }), | ||
| HEADING, | ||
| ); | ||
| expect(body(text).inputs[0].value).toBe('[redacted]'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| import type { ComponentDetail, ComponentProp } from '../types.ts'; | ||
| import { clip } from '../text.ts'; | ||
| import { MAX_TOOL_CHARS, capped } from './router-tools.ts'; | ||
|
|
||
| /** Longest JSON one input or property value keeps once the answer is over {@link MAX_TOOL_CHARS}. */ | ||
| export const DETAIL_VALUE_MAX = 1_000; | ||
|
|
||
| /** Entries kept per list, tried in order until the answer fits. */ | ||
| const LIST_STEPS = [40, 20, 10, 5, 1]; | ||
|
|
||
| const FULL_DETAIL = 'The `detail` of the pangular:component-tree resource has every value in full.'; | ||
| const NARROW = 'read the `detail` of the pangular:component-tree resource for every value in full'; | ||
|
|
||
| type ListKey = 'inputs' | 'outputs' | 'properties' | 'listeners' | 'directives' | 'dependencies'; | ||
|
|
||
| const LISTS: ListKey[] = [ | ||
| 'inputs', | ||
| 'outputs', | ||
| 'properties', | ||
| 'listeners', | ||
| 'directives', | ||
| 'dependencies', | ||
| ]; | ||
|
|
||
| function clipValues(props: ComponentProp[], prefix: string, cut: string[]): ComponentProp[] { | ||
| return props.map((prop) => { | ||
| if (!('value' in prop)) return prop; | ||
| const json = JSON.stringify(prop.value) ?? 'undefined'; | ||
| if (json.length <= DETAIL_VALUE_MAX) return prop; | ||
| cut.push(`${prefix}${prop.name}`); | ||
| return { ...prop, value: clip(json, DETAIL_VALUE_MAX) }; | ||
| }); | ||
| } | ||
|
|
||
| /** The detail with every long value replaced by the start of its JSON, and the names of those values. */ | ||
| function withClippedValues(detail: ComponentDetail) { | ||
| const cut: string[] = []; | ||
| const clipped: ComponentDetail = { | ||
| ...detail, | ||
| inputs: clipValues(detail.inputs, '', cut), | ||
| properties: clipValues(detail.properties, '', cut), | ||
| directives: detail.directives.map((directive) => ({ | ||
| ...directive, | ||
| inputs: clipValues(directive.inputs, `${directive.name}.`, cut), | ||
| })), | ||
| }; | ||
| return { clipped, cut }; | ||
| } | ||
|
|
||
| /** | ||
| * The detail with each list, and the inputs and outputs of each directive it | ||
| * keeps, cut to their first `keep` entries, and how many each list lost. | ||
| */ | ||
| function withShortLists(detail: ComponentDetail, keep: number) { | ||
| const hidden: string[] = []; | ||
| const short = { ...detail }; | ||
| for (const key of LISTS) { | ||
| const list = detail[key] as unknown[]; | ||
| if (list.length <= keep) continue; | ||
| hidden.push(`${list.length - keep} more ${key}`); | ||
| (short as Record<ListKey, unknown[]>)[key] = list.slice(0, keep); | ||
| } | ||
| short.directives = short.directives.map((directive) => { | ||
| const cut = { ...directive }; | ||
| for (const key of ['inputs', 'outputs'] as const) { | ||
| if (directive[key].length <= keep) continue; | ||
| hidden.push(`${directive[key].length - keep} more ${directive.name} ${key}`); | ||
| cut[key] = directive[key].slice(0, keep); | ||
| } | ||
| return cut; | ||
| }); | ||
| return { short, hidden }; | ||
| } | ||
|
|
||
| function quoted(names: string[]): string { | ||
| const shown = names.slice(0, 10).map((name) => `\`${name}\``); | ||
| return `${shown.join(', ')}${names.length > 10 ? ` and ${names.length - 10} more` : ''}`; | ||
| } | ||
|
|
||
| /** | ||
| * The inspect-component answer: a heading line and the detail as JSON. Past | ||
| * {@link MAX_TOOL_CHARS} it cuts long values to {@link DETAIL_VALUE_MAX} | ||
| * characters, then keeps fewer entries per list, and says what it left out. | ||
| * Values arrive serialized and redacted by the page; this only shortens them. | ||
| */ | ||
| export function inspectComponentText(detail: ComponentDetail, heading: string): string { | ||
| const answer = (value: ComponentDetail, notes: string[]) => | ||
| `${heading}${notes.length ? `\n\n${notes.join(' ')}` : ''}\n\n${JSON.stringify(value, null, 2)}`; | ||
| const full = answer(detail, []); | ||
| if (full.length <= MAX_TOOL_CHARS) return full; | ||
|
|
||
| const { clipped, cut } = withClippedValues(detail); | ||
| const notes = cut.length | ||
| ? [ | ||
| `${cut.length} ${cut.length === 1 ? 'value is' : 'values are'} longer than ${DETAIL_VALUE_MAX.toLocaleString('en-US')} characters and ${cut.length === 1 ? 'shows' : 'show'} only the start of ${cut.length === 1 ? 'its' : 'their'} JSON: ${quoted(cut)}.`, | ||
| ] | ||
| : []; | ||
| let text = answer(clipped, [...notes, FULL_DETAIL]); | ||
| if (text.length <= MAX_TOOL_CHARS) return text; | ||
|
|
||
| for (const keep of LIST_STEPS) { | ||
| const { short, hidden } = withShortLists(clipped, keep); | ||
| if (!hidden.length) continue; | ||
| text = answer(short, [ | ||
| ...notes, | ||
| `Each list shows its first ${keep} ${keep === 1 ? 'entry' : 'entries'}: ${hidden.join(', ')} not shown.`, | ||
| FULL_DETAIL, | ||
| ]); | ||
| if (text.length <= MAX_TOOL_CHARS) return text; | ||
| } | ||
| return capped(text, NARROW); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.