Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions ui-packages/code-lab/src/runtime/clamp-output.test.ts
Original file line number Diff line number Diff line change
@@ -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)}`,
)
})
})
38 changes: 38 additions & 0 deletions ui-packages/code-lab/src/runtime/clamp-output.ts
Original file line number Diff line number Diff line change
@@ -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),
})
3 changes: 2 additions & 1 deletion ui-packages/code-lab/src/runtime/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference lib="webworker" />

import type { CodeLabLanguage } from '../types'
import { clampResult } from './clamp-output'
import type { LanguageRuntime, RuntimeRequest, RuntimeResponse } from './protocol'

const runtimeLoaders: Readonly<Record<CodeLabLanguage, () => Promise<LanguageRuntime>>> = {
Expand Down Expand Up @@ -30,7 +31,7 @@ const run = async (request: RuntimeRequest): Promise<void> => {
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({
Expand Down