Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS
JavaScript ai-agentsdashboarddeveloper-toolsdigital-twinquantified-selfsecond-brainself-hostedtailscale
fix([llm-playground-preserve-partial-output]): surface partial stream on timeout/abort
[._.]/ Adam Eivya34ffb081726
diff --git a/client/src/pages/LocalLlmPlayground.jsx b/client/src/pages/LocalLlmPlayground.jsxindex 8e8da9c42d..d36dcde8b2 100644--- a/client/src/pages/LocalLlmPlayground.jsx+++ b/client/src/pages/LocalLlmPlayground.jsx@@ -66,6 +66,9 @@ function Metric({ icon: Icon, label, value }) {function ResultPanel({ result }) {const ok = !result?.error;+ // A failed run that still streamed tokens before it stopped (e.g. a timeout) —+ // show the partial output alongside the error rather than hiding it.+ const hasPartial = Boolean(result?.error && result?.text);return (<section className={`border rounded-lg bg-port-bg p-3 space-y-3 ${ok ? 'border-port-border' : 'border-port-error/40'}`}><div className="flex items-start justify-between gap-3">@@ -73,8 +76,8 @@ function ResultPanel({ result }) {<div className="text-sm text-white truncate">{result.modelId}</div><div className="text-xs text-gray-500">{BACKEND_LABEL[result.backend] || result.backend}</div></div>- <span className={`text-xs px-2 py-1 rounded ${ok ? 'bg-port-success/15 text-port-success' : 'bg-port-error/15 text-port-error'}`}>- {ok ? 'Done' : 'Failed'}+ <span className={`text-xs px-2 py-1 rounded ${ok ? 'bg-port-success/15 text-port-success' : hasPartial ? 'bg-port-warning/15 text-port-warning' : 'bg-port-error/15 text-port-error'}`}>+ {ok ? 'Done' : hasPartial ? 'Partial' : 'Failed'}</span></div>@@ -84,10 +87,14 @@ function ResultPanel({ result }) {<Metric icon={MessageSquare} label="Speed" value={formatRate(result.timings?.charsPerSecond)} /></div>- {result.error ? (+ {result.error && (<p className="text-sm text-port-error whitespace-pre-wrap">{result.error}</p>- ) : (- <pre className="text-sm text-gray-200 whitespace-pre-wrap break-words font-sans leading-relaxed max-h-72 overflow-auto">{result.text}</pre>+ )}+ {result.text && (+ <div className="space-y-1">+ {hasPartial && <div className="text-xs text-port-warning">Partial output before the run stopped</div>}+ <pre className="text-sm text-gray-200 whitespace-pre-wrap break-words font-sans leading-relaxed max-h-72 overflow-auto">{result.text}</pre>+ </div>)}<div className="flex items-center gap-2 flex-wrap">diff --git a/server/services/localLlmPlayground.js b/server/services/localLlmPlayground.jsindex 83f47e046c..467eea62b6 100644--- a/server/services/localLlmPlayground.js+++ b/server/services/localLlmPlayground.js@@ -63,6 +63,17 @@ export function buildMessages({ systemPrompt, prompt }) {];}+// Resolve the text to surface from a (possibly interrupted) stream: prefer the+// visible content, fall back to reasoning when no content arrived (some models+// emit only a reasoning channel), and '' when neither did. Used on both the+// normal-finish path and the partial-output-on-throw path so a timed-out run+// still shows what streamed before the abort.+export function resolvePartialOutput({ output = '', reasoning = '' }) {+ if (output.trim()) return output;+ if (reasoning.trim()) return reasoning;+ return '';+}+async function resolveLocalProvider(backend) {const providerId = PROVIDER_BY_BACKEND[backend];if (!providerId) {@@ -143,6 +154,9 @@ async function streamChatCompletion({ provider, backend, modelId, prompt, system// Always release the reader (and tear down the socket) on every exit path —// a normal finish, an abort via the timeout signal, or a throw mid-stream.+ // On a throw (e.g. an AbortError from the timeout) surface the tokens already+ // streamed by attaching them to the error, so runLocalLlmTest can render the+ // partial output alongside the timeout message instead of discarding it.try {while (true) {const { done, value } = await reader.read();@@ -153,15 +167,16 @@ async function streamChatCompletion({ provider, backend, modelId, prompt, systemfor (const line of lines) consumeLine(line);}if (buffer.trim()) consumeLine(buffer);+ } catch (err) {+ err.partialOutput = resolvePartialOutput({ output, reasoning });+ throw err;} finally {await reader.cancel().catch(() => {});}- if (!output.trim() && reasoning.trim()) {- output = reasoning;- onChunk(reasoning);- }- return output;+ const resolved = resolvePartialOutput({ output, reasoning });+ if (resolved && resolved !== output) onChunk(resolved);+ return resolved;}export async function runLocalLlmTest({@@ -222,10 +237,14 @@ export async function runLocalLlmTest({} catch (err) {const endedAt = Date.now();const error = err?.name === 'AbortError' ? `Timed out after ${timeoutMs}ms` : err?.message || 'Local LLM test failed';+ // A timeout/abort mid-stream still has tokens worth keeping — surface what the+ // model already streamed (attached to the error by streamChatCompletion) instead+ // of discarding it. Persist it on the failed run record too so /runs replay shows it.+ const partialText = typeof err?.partialOutput === 'string' ? err.partialOutput : '';if (runId) {await finalizeRunRecord({runId,- output: '',+ output: partialText,exitCode: 1,success: false,error,@@ -238,7 +257,8 @@ export async function runLocalLlmTest({providerId: provider.id,runId,error,- timings: summarizeTimings({ startedAt, firstChunkAt, endedAt, text: '' }),+ text: partialText,+ timings: summarizeTimings({ startedAt, firstChunkAt, endedAt, text: partialText }),options: { temperature, maxTokens, timeoutMs },};}diff --git a/server/services/localLlmPlayground.test.js b/server/services/localLlmPlayground.test.jsindex 288bfc34e4..5931ddff6c 100644--- a/server/services/localLlmPlayground.test.js+++ b/server/services/localLlmPlayground.test.js@@ -1,5 +1,5 @@import { describe, expect, it } from 'vitest';-import { buildPrompt, buildMessages, summarizeTimings, extractStreamDelta } from './localLlmPlayground.js';+import { buildPrompt, buildMessages, summarizeTimings, extractStreamDelta, resolvePartialOutput } from './localLlmPlayground.js';describe('buildPrompt', () => {it('returns the bare prompt when no system instructions', () => {@@ -75,3 +75,18 @@ describe('extractStreamDelta', () => {expect(extractStreamDelta('data: {"choices":[{}]}')).toEqual({ content: '', reasoning: '' });});});++describe('resolvePartialOutput', () => {+ it('prefers visible content over reasoning', () => {+ expect(resolvePartialOutput({ output: 'hello', reasoning: 'thinking' })).toBe('hello');+ });++ it('falls back to reasoning when no content streamed', () => {+ expect(resolvePartialOutput({ output: ' ', reasoning: 'partial thought' })).toBe('partial thought');+ });++ it('returns empty string when neither content nor reasoning streamed', () => {+ expect(resolvePartialOutput({ output: '', reasoning: '' })).toBe('');+ expect(resolvePartialOutput({})).toBe('');+ });+});