Skip to content

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

Merge branch 'worktree-agent-ab85e0f4aa2c96e2b' into integrate-next-up

Adam Eivyc5f1b18b8aec
diff --cc server/services/voice/pipeline.js
index fd6e0ca85f,4cad4c238d..0274739948
--- a/server/services/voice/pipeline.js
+++ b/server/services/voice/pipeline.js
@@@ -94,42 -94,39 +94,74 @@@ const waitForUiRefresh = (state, timeou
state.uiWaiters.push(finish);
});
+// Ask the client to screenshot the active tab and await the data URL. Mirrors
+// waitForUiRefresh's waiter pattern: emit voice:screenshot:request, then resolve
+// when the client posts voice:screenshot:result (handled in sockets/voice.js).
+// Resolves with the data URL or null on timeout/abort/denied-capture.
+const requestScreenshot = (emit, state, timeoutMs, signal) => new Promise((resolve) => {
+ if (!state) { resolve(null); return; }
+ if (!Array.isArray(state.screenshotWaiters)) state.screenshotWaiters = [];
+ let done = false;
+ const finish = (value) => {
+ if (done) return;
+ done = true;
+ clearTimeout(timer);
+ signal?.removeEventListener?.('abort', onAbort);
+ const i = state.screenshotWaiters.indexOf(finish);
+ if (i !== -1) state.screenshotWaiters.splice(i, 1);
+ resolve(value);
+ };
+ const onAbort = () => finish(null);
+ const timer = setTimeout(() => finish(null), timeoutMs);
+ signal?.addEventListener?.('abort', onAbort, { once: true });
+ state.screenshotWaiters.push(finish);
+ emit('voice:screenshot:request', {});
+});
+
+// Send a captured image (base64 data URL) to the voice LLM provider's vision
+// endpoint and return the description text. Reuses visionTest's provider-aware
+// OpenAI-compatible /chat/completions call so we don't reimplement the request
+// shape. Lazy import keeps tools.js / the pipeline free of a hard vision dep.
+const describeScreenshot = async (dataUrl, prompt, cfg) => {
+ const { describeImageDataUrl } = await import('../visionTest.js');
+ const providerId = cfg?.llm?.provider || 'lmstudio';
+ const model = cfg?.llm?.model && cfg.llm.model !== 'auto' ? cfg.llm.model : undefined;
+ return describeImageDataUrl({ dataUrl, prompt, providerId, model });
+};
+
- export { summarizeUi, shouldIncludeUi };
+ // Lazily fetch the page's visible text from the client. The client ships the
+ // UI index WITHOUT the heavy visible-text blob by default (textOnDemand:true);
+ // when ui_read actually needs it we emit voice:ui:read-request and await a
+ // voice:ui:read-response (the socket handler resolves the matching waiter).
+ // Resolves with the text string, or null on timeout / abort / no-waiter-infra
+ // (e.g. tests). Falls back gracefully: if the client never replies (legacy
+ // client that doesn't understand read-request) the timeout fires and the
+ // caller treats it as "no text available".
+ const UI_TEXT_READ_TIMEOUT_MS = 1500;
+ let uiTextRequestSeq = 0;
+ const requestUiText = (state, emit, signal, timeoutMs = UI_TEXT_READ_TIMEOUT_MS) => new Promise((resolve) => {
+ if (!state || typeof emit !== 'function') { resolve(null); return; }
+ if (!(state.uiTextWaiters instanceof Map)) state.uiTextWaiters = new Map();
+ const requestId = `uitext_${++uiTextRequestSeq}`;
+ let done = false;
+ const finish = (value) => {
+ if (done) return;
+ done = true;
+ clearTimeout(timer);
+ signal?.removeEventListener?.('abort', onAbort);
+ // Drop our own waiter so a late response (after timeout/abort) is a no-op
+ // rather than resolving a stale promise.
+ state.uiTextWaiters.delete(requestId);
+ resolve(value);
+ };
+ const onAbort = () => finish(null);
+ const timer = setTimeout(() => finish(null), timeoutMs);
+ signal?.addEventListener?.('abort', onAbort, { once: true });
+ state.uiTextWaiters.set(requestId, finish);
+ emit('voice:ui:read-request', { requestId });
+ });
+
+ export { summarizeUi, shouldIncludeUi, requestUiText };
const buildSystemPrompt = (cfg) => {
if (!cfg.llm.usePersonality) return cfg.llm.systemPrompt;
@@@ -553,11 -553,7 +588,12 @@@ export const runTurn = async ({ audio,
sideEffects: [],
state,
signal,
+ // ui_describe_visually: capture the active tab (client round-trip) and
+ // describe it via the voice provider's vision endpoint. Provided here
+ // so tools.js stays free of socket/vision coupling.
+ captureScreenshot: () => requestScreenshot(emit, state, 15000, signal),
+ describeImage: (dataUrl, prompt) => describeScreenshot(dataUrl, prompt, cfg),
+ requestUiText: () => requestUiText(state, emit, signal),
};
try {
args = tc.function.arguments ? JSON.parse(tc.function.arguments) : {};
diff --cc server/sockets/voice.js
index a4b646dc7e,eea0393d0f..83a6d83fd8
--- a/server/sockets/voice.js
+++ b/server/sockets/voice.js
@@@ -1,11 -1,11 +1,13 @@@
// Per-socket voice handlers.
// Inbound: voice:turn | voice:text | voice:interrupt | voice:reset
-// | voice:dictation:set | voice:ui:index | voice:ui:read-response
+// | voice:dictation:set | voice:ui:index | voice:screenshot:result
++// | voice:ui:read-response
// Outbound: voice:transcript | voice:llm:delta | voice:llm:done | voice:tts:audio
// | voice:tool | voice:dictation | voice:navigate
// | voice:ui:click | voice:ui:fill | voice:ui:select | voice:ui:check
+ // | voice:ui:read-request
// | voice:dailyLog:appended | voice:error | voice:idle
+// | voice:screenshot:request
import { runTurn } from '../services/voice/pipeline.js';
import { getVoiceConfig } from '../services/voice/config.js';
@@@ -64,10 -64,11 +66,15 @@@ export const registerVoiceHandlers = (s
// ui:click, wait for the client's fresh index before the next tool
// runs so the LLM can see the modal/new content it just opened.
uiWaiters: [],
+ // Promises awaiting a voice:screenshot:result for an in-flight
+ // ui_describe_visually capture. The server emits voice:screenshot:request,
+ // the client captures the active tab and replies with a data URL.
+ screenshotWaiters: [],
+ // Resolvers keyed by requestId, awaiting a voice:ui:read-response. The
+ // ui_read tool emits voice:ui:read-request and parks a resolver here so
+ // the heavy visible-text blob is only computed by the client (and shipped
+ // over the wire) when actually needed — not eagerly on every index push.
+ uiTextWaiters: new Map(),
// Ring of recently-spoken TTS sentences (with cached trigrams). The
// pipeline uses this to detect the bot's own voice being echoed back
// through the user's mic when laptop speakers are in play. The buffer is
@@@ -257,32 -263,36 +269,56 @@@
}
});
+ // Client replies to a voice:screenshot:request with a base64 data URL of the
+ // captured tab (or null if capture failed / the user denied permission).
+ // Cap the payload to bound memory from a runaway/malicious client — a
+ // full-screen PNG data URL is typically a few MB, so 16 MB is generous.
+ const MAX_SCREENSHOT_BYTES = 16 * 1024 * 1024;
+ socket.on('voice:screenshot:result', (payload) => {
+ if (!state.screenshotWaiters.length) return;
+ const raw = payload && typeof payload === 'object' ? payload.dataUrl : null;
+ const dataUrl = (typeof raw === 'string' && raw.startsWith('data:image/') && raw.length <= MAX_SCREENSHOT_BYTES)
+ ? raw
+ : null;
+ const waiters = state.screenshotWaiters;
+ state.screenshotWaiters = [];
+ waiters.forEach((resolve) => resolve(dataUrl));
+ });
+
+ // Lazy visible-text reply. The ui_read tool emitted voice:ui:read-request;
+ // the client recomputed extractVisibleText on the live DOM and sent it back
+ // here. Re-apply the same ~8 KB word-boundary cap server-side so a runaway
+ // client can't blow past it, then resolve the matching waiter. Echo of the
+ // requestId correlates the response with the awaiting ui_read call.
+ socket.on('voice:ui:read-response', (payload) => {
+ if (!payload || typeof payload !== 'object') return;
+ const { requestId, text } = payload;
+ const capped = typeof text === 'string' ? truncateOnWordBoundary(text, MAX_UI_TEXT_CHARS) : null;
+ // Cache on the current ui snapshot too, so a follow-up read in the same
+ // turn (same page) doesn't need another round-trip.
+ if (state.ui && capped !== null) state.ui.text = capped;
+ const resolve = state.uiTextWaiters.get(requestId);
+ if (resolve) {
+ state.uiTextWaiters.delete(requestId);
+ resolve(capped);
+ }
+ });
+
socket.on('disconnect', () => {
state.ctrl?.abort();
// Abort any pending UI refresh waiters so their turns don't hang.
const waiters = state.uiWaiters;
state.uiWaiters = [];
waiters.forEach((resolve) => resolve(null));
+ // Same for any pending screenshot capture.
+ const shotWaiters = state.screenshotWaiters;
+ state.screenshotWaiters = [];
+ shotWaiters.forEach((resolve) => resolve(null));
+ // Same for any pending lazy-text read waiters — resolve null so a
+ // ui_read awaiting a response that will never arrive doesn't hang.
+ const textWaiters = Array.from(state.uiTextWaiters.values());
+ state.uiTextWaiters.clear();
+ textWaiters.forEach((resolve) => resolve(null));
unregisterEchoBuffer(state.recentTts);
});
};