diff --git a/ui-packages/code-lab/src/runtime/clamp-output.test.ts b/ui-packages/code-lab/src/runtime/clamp-output.test.ts new file mode 100644 index 0000000..14bbea5 --- /dev/null +++ b/ui-packages/code-lab/src/runtime/clamp-output.test.ts @@ -0,0 +1,33 @@ +import { describe, expect, it } from 'vitest' +import { clampText } from './clamp-output' + +const numberedLines = (count: number): string => + Array.from({ length: count }, (_, index) => `line ${index}`).join('\n') + +describe('clampText', () => { + it('leaves teaching-sized output untouched', () => { + const text = numberedLines(2_000) + expect(clampText(text)).toBe(text) + }) + + it('keeps whole lines from the head and tail and counts what it omits', () => { + const lines = numberedLines(100_000).split('\n') + const clamped = clampText(lines.join('\n')).split('\n') + const note = clamped.findIndex(line => line.startsWith('…')) + const head = clamped.slice(0, note) + const tail = clamped.slice(note + 1) + + expect(head).toEqual(lines.slice(0, head.length)) + expect(tail).toEqual(lines.slice(-tail.length)) + expect(clamped[note]).toBe( + `… ${(lines.length - head.length - tail.length).toLocaleString('en-US')} lines omitted …`, + ) + }) + + it('cuts a single long line by characters', () => { + const clamped = clampText('x'.repeat(100_000)) + expect(clamped).toBe( + `${'x'.repeat(32_000)}\n… 36,000 characters omitted …\n${'x'.repeat(32_000)}`, + ) + }) +}) diff --git a/ui-packages/code-lab/src/runtime/clamp-output.ts b/ui-packages/code-lab/src/runtime/clamp-output.ts new file mode 100644 index 0000000..ea3917a --- /dev/null +++ b/ui-packages/code-lab/src/runtime/clamp-output.ts @@ -0,0 +1,38 @@ +import type { CodeLabExecutionResult, CodeLabOutput } from '../types' + +// Each side keeps up to this many characters; teaching-sized output never reaches it. +const keptChars = 32_000 +// A cut moves to the nearest line break when one is this close, so kept lines stay whole. +const lineSnapChars = 1_000 + +const countLineBreaks = (text: string): number => text.split('\n').length - 1 + +export const clampText = (text: string): string => { + if (text.length <= keptChars * 2) return text + + const rawHead = text.slice(0, keptChars) + const headBreak = rawHead.lastIndexOf('\n') + const head = headBreak >= keptChars - lineSnapChars ? rawHead.slice(0, headBreak + 1) : rawHead + + const rawTail = text.slice(-keptChars) + const tailBreak = rawTail.indexOf('\n') + const tail = tailBreak >= 0 && tailBreak < lineSnapChars ? rawTail.slice(tailBreak + 1) : rawTail + + const omitted = text.slice(head.length, text.length - tail.length) + const lines = countLineBreaks(omitted) + const amount = + lines > 0 + ? `${lines.toLocaleString('en-US')} lines` + : `${omitted.length.toLocaleString('en-US')} characters` + return `${head.endsWith('\n') ? head : `${head}\n`}… ${amount} omitted …\n${tail}` +} + +const clampOutput = (output: CodeLabOutput): CodeLabOutput => + output.kind === 'stdout' || output.kind === 'stderr' || output.kind === 'text' + ? { kind: output.kind, text: clampText(output.text) } + : output + +export const clampResult = (result: CodeLabExecutionResult): CodeLabExecutionResult => ({ + ...result, + outputs: result.outputs.map(clampOutput), +}) diff --git a/ui-packages/code-lab/src/runtime/worker.ts b/ui-packages/code-lab/src/runtime/worker.ts index 92f037f..7277336 100644 --- a/ui-packages/code-lab/src/runtime/worker.ts +++ b/ui-packages/code-lab/src/runtime/worker.ts @@ -1,6 +1,7 @@ /// import type { CodeLabLanguage } from '../types' +import { clampResult } from './clamp-output' import type { LanguageRuntime, RuntimeRequest, RuntimeResponse } from './protocol' const runtimeLoaders: Readonly Promise>> = { @@ -30,7 +31,7 @@ const run = async (request: RuntimeRequest): Promise => { const result = await languageRuntime.run(request.source, (message, phase = 'loading') => { respond({ type: 'progress', requestId: request.requestId, message, phase }) }) - respond({ type: 'result', requestId: request.requestId, result }) + respond({ type: 'result', requestId: request.requestId, result: clampResult(result) }) } catch (error) { console.error(`The ${request.language} runtime failed`, error) respond({