From 8af69ab9e9fd93d349c308d03c313f43a6b2d1d9 Mon Sep 17 00:00:00 2001 From: Sambit Biswas Date: Sat, 3 Oct 2026 06:44:34 -0400 Subject: [PATCH 01/13] perf(renderer): keep the dictation pill off the ipc.ts parser graph (UI-4) Move the bridge primitives and the settings/voice/dictation wrappers into parser-free modules that ipc.ts re-exports, and point the pill window and its voice helpers at them directly. Co-Authored-By: Claude Opus 5.5 --- renderer/lib/ipc-bridge.ts | 14 +++++ renderer/lib/ipc-voice.ts | 78 +++++++++++++++++++++++++++ renderer/lib/ipc.ts | 82 ++--------------------------- renderer/lib/live-pcm-capture.ts | 2 +- renderer/lib/pill-appearance.ts | 3 +- renderer/lib/voice-recorder-core.ts | 2 +- renderer/pill/pill-app.tsx | 3 +- 7 files changed, 101 insertions(+), 83 deletions(-) create mode 100644 renderer/lib/ipc-bridge.ts create mode 100644 renderer/lib/ipc-voice.ts diff --git a/renderer/lib/ipc-bridge.ts b/renderer/lib/ipc-bridge.ts new file mode 100644 index 0000000000..50a00db32d --- /dev/null +++ b/renderer/lib/ipc-bridge.ts @@ -0,0 +1,14 @@ +// Minimal IPC bridge primitives. Kept free of runtime parsers so small windows +// (the dictation pill) can talk to main without loading the full `ipc.ts` graph. + +function bridge() { + return window.aidenAPI.ipc; +} + +export function invoke(channel: string, ...args: unknown[]): Promise { + return bridge().invoke(channel, ...args) as Promise; +} + +export function onNotification(method: string, handler: (payload: T) => void): () => void { + return bridge().onNotification(method, handler as (params: unknown) => void); +} diff --git a/renderer/lib/ipc-voice.ts b/renderer/lib/ipc-voice.ts new file mode 100644 index 0000000000..3a6ab6d009 --- /dev/null +++ b/renderer/lib/ipc-voice.ts @@ -0,0 +1,78 @@ +// Settings, voice and dictation IPC wrappers shared by the main window and the +// dictation pill. Type-only imports keep this module parser-free so the pill +// chunk does not pull in the rest of `ipc.ts`. + +import type { AppSettings } from "./types"; +import type { AnthropicThinkingLevel } from "../shared/anthropic-thinking"; +import type { GoogleThinkingLevel } from "../shared/google-thinking"; +import type { CodexThinkingLevel } from "../shared/codex-thinking"; +import type { AppearanceConfig, AppearancePreviewSnapshot } from "../shared/appearance"; +import { invoke, onNotification } from "./ipc-bridge"; + +export const settingsApi = { + get: () => invoke("settings:get"), + getAppearance: () => invoke("settings:getAppearance"), + getAppearanceState: () => invoke("settings:getAppearanceState"), + previewAppearance: (appearance: AppearanceConfig) => + invoke("settings:previewAppearance", appearance), + set: (patch: Partial) => invoke("settings:set", patch), + setGeminiVoiceSetup: (scope: NonNullable, model: string) => + invoke("settings:setGeminiVoiceSetup", scope, model), + setGeminiUsageScope: (scope: NonNullable) => + invoke("settings:setGeminiUsageScope", scope), + setGoogleThinking: (modelId: string, level: GoogleThinkingLevel) => + invoke("settings:setGoogleThinking", modelId, level), + setCodexThinking: (modelId: string, level: CodexThinkingLevel) => + invoke("settings:setCodexThinking", modelId, level), + setAnthropicThinking: (modelId: string, level: AnthropicThinkingLevel) => + invoke("settings:setAnthropicThinking", modelId, level), + setProviderThinking: ( + providerId: string, + modelId: string, + level: import("../shared/generation-thinking").GenerationThinkingLevel, + ) => invoke("settings:setProviderThinking", providerId, modelId, level), + setModelVisibility: (providerId: string, modelId: string, hidden: boolean) => + invoke("settings:setModelVisibility", providerId, modelId, hidden), + showAllProviderModels: (providerId: string) => + invoke("settings:showAllProviderModels", providerId), + hideAllProviderModels: (providerId: string) => + invoke("settings:hideAllProviderModels", providerId), +}; + +export const voiceApi = { + transcribe: (audioBase64: string, mimeType: string, model?: string, operationId?: string) => + invoke("voice:transcribe", audioBase64, mimeType, model, operationId), + cancelTranscription: (operationId: string) => invoke("voice:transcribeCancel", operationId), + /** On-device transcription: base64 raw 16 kHz mono Float32 PCM + downloaded model id. */ + transcribeLocal: (pcmBase64: string, modelId: string, operationId: string) => + invoke("voice:transcribeLocal", pcmBase64, modelId, operationId), + cancelLocalTranscription: (operationId: string) => + invoke("voice:transcribeLocalCancel", operationId), + streamStart: () => invoke<{ sessionId: string }>("voice:streamStart"), + streamPush: (sessionId: string, pcmBase64: string) => + invoke("voice:streamPush", sessionId, pcmBase64), + streamFinish: (sessionId: string) => invoke("voice:streamFinish", sessionId), + streamCancel: (sessionId: string) => invoke("voice:streamCancel", sessionId), + onStreamText: ( + handler: (payload: { sessionId: string; committed: string; tentative: string }) => void, + ) => onNotification("voice:stream-text", handler), +}; + +// ── Global dictation (pill + auto-paste) ────────────────────────────── +export const dictationApi = { + /** Pill reports the finished transcript to the main-process coordinator. */ + reportResult: (operationId: string, text: string) => + invoke("dictation:result", operationId, text), + /** Pill reports a capture/transcription failure. */ + reportError: (operationId: string, message: string) => + invoke("dictation:error", operationId, message), + /** Pill reports finalization/consent/fallback progress for accurate UI and diagnostics. */ + reportProgress: (operationId: string, progress: "finalizing" | "fallback-consent" | "fallback") => + invoke("dictation:progress", operationId, progress), + /** Pill cancel button: discard the in-flight recording/transcription. */ + cancel: () => invoke("dictation:cancel"), + /** Pill renderer is mounted and subscribed to dictation state broadcasts. */ + ready: () => invoke("dictation:ready"), + /** Silence detector or UI asked to end capture without cancelling. */ + stopRecording: () => invoke("dictation:stop"), +}; diff --git a/renderer/lib/ipc.ts b/renderer/lib/ipc.ts index 70a5885cf8..984fe8cb74 100644 --- a/renderer/lib/ipc.ts +++ b/renderer/lib/ipc.ts @@ -122,9 +122,6 @@ export interface BotAccessState { modelSelection?: { providerId: string; modelId: string }; visionModelSelection?: { providerId: string; modelId: string }; } -import type { AnthropicThinkingLevel } from "../shared/anthropic-thinking"; -import type { GoogleThinkingLevel } from "../shared/google-thinking"; -import type { CodexThinkingLevel } from "../shared/codex-thinking"; import type { ChatTimelineNotification, GenerationTimeline } from "../shared/generation-timeline"; import type { ChatRunInputAdmissionResult, @@ -162,7 +159,6 @@ import { rememberDetachedLifecycleStream, } from "./chat-terminal-sync"; import type { AppCapabilities } from "./app-capabilities"; -import type { AppearanceConfig, AppearancePreviewSnapshot } from "../shared/appearance"; import { parseSkillCatalog, type SkillCatalogEntry } from "../shared/slash-commands"; import { rememberAppendReconciliationFailure } from "./append-reconciliation"; import { @@ -213,10 +209,10 @@ import { } from "../shared/devices"; import type { PeerHostView } from "../shared/peer-host"; import type { PeerOperation } from "../shared/peer-operation"; +import { invoke, onNotification } from "./ipc-bridge"; -function bridge() { - return window.aidenAPI.ipc; -} +export { invoke, onNotification } from "./ipc-bridge"; +export { dictationApi, settingsApi, voiceApi } from "./ipc-voice"; export interface AppInfo { name: string; @@ -225,13 +221,6 @@ export interface AppInfo { capabilities: AppCapabilities; } -export function invoke(channel: string, ...args: unknown[]): Promise { - return bridge().invoke(channel, ...args) as Promise; -} - -export function onNotification(method: string, handler: (payload: T) => void): () => void { - return bridge().onNotification(method, handler as (params: unknown) => void); -} export const peerHostsApi = { list: () => invoke("remote:peersList"), @@ -325,35 +314,6 @@ export const providersApi = { onNotification("providers:auth:status-changed", handler), }; -export const settingsApi = { - get: () => invoke("settings:get"), - getAppearance: () => invoke("settings:getAppearance"), - getAppearanceState: () => invoke("settings:getAppearanceState"), - previewAppearance: (appearance: AppearanceConfig) => - invoke("settings:previewAppearance", appearance), - set: (patch: Partial) => invoke("settings:set", patch), - setGeminiVoiceSetup: (scope: NonNullable, model: string) => - invoke("settings:setGeminiVoiceSetup", scope, model), - setGeminiUsageScope: (scope: NonNullable) => - invoke("settings:setGeminiUsageScope", scope), - setGoogleThinking: (modelId: string, level: GoogleThinkingLevel) => - invoke("settings:setGoogleThinking", modelId, level), - setCodexThinking: (modelId: string, level: CodexThinkingLevel) => - invoke("settings:setCodexThinking", modelId, level), - setAnthropicThinking: (modelId: string, level: AnthropicThinkingLevel) => - invoke("settings:setAnthropicThinking", modelId, level), - setProviderThinking: ( - providerId: string, - modelId: string, - level: import("../shared/generation-thinking").GenerationThinkingLevel, - ) => invoke("settings:setProviderThinking", providerId, modelId, level), - setModelVisibility: (providerId: string, modelId: string, hidden: boolean) => - invoke("settings:setModelVisibility", providerId, modelId, hidden), - showAllProviderModels: (providerId: string) => - invoke("settings:showAllProviderModels", providerId), - hideAllProviderModels: (providerId: string) => - invoke("settings:hideAllProviderModels", providerId), -}; export const assistantApi = { config: () => invoke("assistant:get-config"), @@ -638,24 +598,6 @@ export const aidenRemoteApi = { }; // ── Voice + shortcut ────────────────────────────────────────────────── -export const voiceApi = { - transcribe: (audioBase64: string, mimeType: string, model?: string, operationId?: string) => - invoke("voice:transcribe", audioBase64, mimeType, model, operationId), - cancelTranscription: (operationId: string) => invoke("voice:transcribeCancel", operationId), - /** On-device transcription: base64 raw 16 kHz mono Float32 PCM + downloaded model id. */ - transcribeLocal: (pcmBase64: string, modelId: string, operationId: string) => - invoke("voice:transcribeLocal", pcmBase64, modelId, operationId), - cancelLocalTranscription: (operationId: string) => - invoke("voice:transcribeLocalCancel", operationId), - streamStart: () => invoke<{ sessionId: string }>("voice:streamStart"), - streamPush: (sessionId: string, pcmBase64: string) => - invoke("voice:streamPush", sessionId, pcmBase64), - streamFinish: (sessionId: string) => invoke("voice:streamFinish", sessionId), - streamCancel: (sessionId: string) => invoke("voice:streamCancel", sessionId), - onStreamText: ( - handler: (payload: { sessionId: string; committed: string; tentative: string }) => void, - ) => onNotification("voice:stream-text", handler), -}; /** On-device (sherpa-onnx / Parakeet) engine + model management. */ export const localVoiceApi = { @@ -685,24 +627,6 @@ export const shortcutApi = { onNotification("shortcut:changed", handler), }; -// ── Global dictation (pill + auto-paste) ────────────────────────────── -export const dictationApi = { - /** Pill reports the finished transcript to the main-process coordinator. */ - reportResult: (operationId: string, text: string) => - invoke("dictation:result", operationId, text), - /** Pill reports a capture/transcription failure. */ - reportError: (operationId: string, message: string) => - invoke("dictation:error", operationId, message), - /** Pill reports finalization/consent/fallback progress for accurate UI and diagnostics. */ - reportProgress: (operationId: string, progress: "finalizing" | "fallback-consent" | "fallback") => - invoke("dictation:progress", operationId, progress), - /** Pill cancel button: discard the in-flight recording/transcription. */ - cancel: () => invoke("dictation:cancel"), - /** Pill renderer is mounted and subscribed to dictation state broadcasts. */ - ready: () => invoke("dictation:ready"), - /** Silence detector or UI asked to end capture without cancelling. */ - stopRecording: () => invoke("dictation:stop"), -}; /** Native folder picker (uses the default-exposed dialog bridge). Returns null if cancelled. */ export async function pickFolder(): Promise { diff --git a/renderer/lib/live-pcm-capture.ts b/renderer/lib/live-pcm-capture.ts index 056b505262..9838ff2aaf 100644 --- a/renderer/lib/live-pcm-capture.ts +++ b/renderer/lib/live-pcm-capture.ts @@ -1,4 +1,4 @@ -import { voiceApi } from "./ipc"; +import { voiceApi } from "./ipc-voice"; export interface LiveTranscriptSnapshot { committed: string; diff --git a/renderer/lib/pill-appearance.ts b/renderer/lib/pill-appearance.ts index 3cd7576c73..c49fc15d1c 100644 --- a/renderer/lib/pill-appearance.ts +++ b/renderer/lib/pill-appearance.ts @@ -1,4 +1,5 @@ -import { onNotification, settingsApi } from "./ipc"; +import { onNotification } from "./ipc-bridge"; +import { settingsApi } from "./ipc-voice"; import { APPEARANCE_STORAGE_KEY, applyAppearanceConfig, diff --git a/renderer/lib/voice-recorder-core.ts b/renderer/lib/voice-recorder-core.ts index ba512f0527..46ceb47ea7 100644 --- a/renderer/lib/voice-recorder-core.ts +++ b/renderer/lib/voice-recorder-core.ts @@ -4,7 +4,7 @@ // cloud provider, or (for the on-device provider) decoded and resampled to // 16 kHz mono PCM in the renderer and transcribed locally via sherpa-onnx. -import { voiceApi } from "./ipc"; +import { voiceApi } from "./ipc-voice"; import type { VoiceProvider } from "./types"; import { bytesToBase64 } from "./live-pcm-capture"; import { encodeMonoPcm16Wav } from "./wav-audio"; diff --git a/renderer/pill/pill-app.tsx b/renderer/pill/pill-app.tsx index bca2750cc6..4ddfef2430 100644 --- a/renderer/pill/pill-app.tsx +++ b/renderer/pill/pill-app.tsx @@ -7,7 +7,8 @@ import * as React from "react"; import { Check, Loader2, X } from "lucide-react"; import { PillCopiedNotice, type PillCopiedNoticeProps } from "./pill-copied-notice"; -import { dictationApi, onNotification, settingsApi } from "../lib/ipc"; +import { onNotification } from "../lib/ipc-bridge"; +import { dictationApi, settingsApi } from "../lib/ipc-voice"; import type { DictationStatePayload } from "../shared/dictation"; import { ensureMicrophoneAccess, From 64881571e292256414f28f4273fded291211e55b Mon Sep 17 00:00:00 2001 From: Sambit Biswas Date: Sat, 3 Oct 2026 06:48:35 -0400 Subject: [PATCH 02/13] perf(renderer): lazy-load highlight.js grammars and drop auto-detection (UI-1) Code blocks now load highlight.js/lib/core plus an allowlisted grammar on demand, keyed by the fence label. Unlabeled or unknown fences render as plain text instead of running every grammar through highlightAuto. Main-window initial JS: 3328 KB -> 2438 KB (gzip 1019 KB -> 723 KB). Co-Authored-By: Claude Opus 5.5 --- package.json | 2 +- renderer/components/code-block.tsx | 33 ++-- renderer/lib/syntax-highlight.test.ts | 67 ++++++++ renderer/lib/syntax-highlight.ts | 210 ++++++++++++++++++++++++++ scripts/ci-test-registry.json | 1 + 5 files changed, 301 insertions(+), 12 deletions(-) create mode 100644 renderer/lib/syntax-highlight.test.ts create mode 100644 renderer/lib/syntax-highlight.ts diff --git a/package.json b/package.json index ee986d23ab..87f2e7e680 100644 --- a/package.json +++ b/package.json @@ -130,7 +130,7 @@ "test:tts": "tsx --test main/services/tts/authority-contract.test.ts renderer/components/settings/tts-settings.test.tsx main/services/tts/audio-store.test.ts renderer/lib/tts-client.test.ts renderer/lib/tts-player.test.ts main/services/tts/speech-text.test.ts main/services/tts/source.test.ts main/services/tts/gemini-wire.test.ts main/services/tts/service.test.ts main/services/tts/gemini-provider.loopback.test.ts renderer/components/message-bubble.test.tsx renderer/shared/tts.test.ts", "test:diagnostics": "tsx --test main/services/diagnostics-contract.test.ts main/services/diagnostic-health.test.ts main/services/diagnostic-journal.test.ts main/services/diagnostic-support.test.ts main/services/dev-log.test.ts main/services/process-diagnostics.test.ts main/services/renderer-crash-recovery.test.ts main/services/renderer-diagnostic-rate.test.ts main/services/subagents/subagent-runtime-diagnostics.test.ts renderer/components/settings/diagnostics-settings.test.tsx && node --test scripts/diagnostic-policy.test.mjs", "diagnostics:failure-receipt": "node scripts/write-diagnostic-failure-receipt.mjs", - "test": "npm run test:agents-instructions && npm run test:custom-model-options && tsx --test main/services/tool-output-store.test.ts main/services/mcp-oauth-client-metadata.test.ts main/handlers/assistant-parse.test.ts main/services/assistant/system-prompt.test.ts main/services/chat-activity-core.test.ts main/services/chat-generation-start.test.ts main/services/chat-title-policy.test.ts main/services/chat-title-routing.test.ts main/services/chat-store-core.test.ts main/services/empty-chat-migration.test.ts main/services/chat-first-message-commit.test.ts main/services/codex-provider.test.ts main/services/coding-tools.test.ts main/services/config-store-core.test.ts main/services/computer-use/computer-use-foundation.test.ts main/services/computer-use/computer-use-tool.test.ts main/services/computer-use/generation-gate.test.ts main/services/computer-use/safety.test.ts main/services/computer-use/settings-core.test.ts main/services/computer-use/status-core.test.ts main/services/data-store.test.ts main/services/data-store.resilience.test.ts main/services/terminal.test.ts main/services/terminal-history.test.ts main/services/aiden-config-dir.test.ts main/services/portable-config-core.test.ts main/services/portable-config-core.roundtrip.test.ts main/services/portable-config-watch-core.test.ts main/services/secret-map-core.test.ts main/services/dev-log.test.ts main/services/process-diagnostics.test.ts main/services/dictation-coordinator.test.ts main/services/dictation-paste.test.ts main/services/dictation-cleanup-core.test.ts main/services/dictation-hotkey.test.ts main/services/dictation-key-state.test.ts main/services/dictation-keycode.test.ts main/services/parakeet-protocol.test.ts main/services/parakeet-process-core.test.ts main/services/foundation-models-connection.test.ts main/services/foundation-models-connection-core.test.ts main/services/gemini-context-cache.test.ts main/services/generation-bound-connection-cache.test.ts main/services/generation-context.test.ts main/services/generation-messages.test.ts main/services/generation-runtime.test.ts main/services/external-editors.test.ts main/services/git.test.ts main/services/git-executable.test.ts main/services/generation-git-context.test.ts main/services/github-pull-request.test.ts main/services/model-runtime-core.test.ts main/services/opencode-session-attribution.test.ts main/services/models.test.ts main/services/mcp-oauth-operation.test.ts main/services/mcp-oauth-session.test.ts main/services/mcp-presets.test.ts renderer/shared/plugin-catalog.test.ts main/services/pi-credential-store-core.test.ts main/services/pi-provider-contract.test.ts main/services/profile-share-core.test.ts main/services/profile-share.test.ts main/services/profile-share-files.test.ts main/services/profile.test.ts main/services/provider-auth-flow-core.test.ts main/services/provider-auth-owner.test.ts main/services/provider-key-policy.test.ts main/services/provider-list-core.test.ts main/services/provider-artwork-core.test.ts main/services/provider-artwork.test.ts main/services/quit-barrier.test.ts main/services/scratch-workspace.test.ts main/services/skills-discovery.test.ts main/services/tool-approval.test.ts main/services/local-runtime-status.test.ts main/services/usage-store-core.test.ts main/services/workspace-files.test.ts main/windows/pill-window-security.test.ts renderer/components/assistant/use-assistant-chat.test.ts renderer/components/assistant/assistant-ui.test.tsx renderer/components/activity-feed.test.tsx renderer/components/environment-subagents-contract.test.ts renderer/components/subagents-panel.test.tsx renderer/lib/subagent-context-usage-store.test.ts renderer/components/chat-sidebar.test.tsx renderer/components/composer.test.tsx renderer/components/context-meter.test.tsx renderer/lib/chat-message-queue.test.ts renderer/lib/chat-draft.test.ts renderer/main/chat-transition.test.tsx renderer/components/usage/profile-share-card.test.tsx renderer/lib/accessibility-refresh.test.ts renderer/lib/agent-activity.test.ts renderer/lib/chat-activity.test.ts renderer/lib/assistant-dock.test.ts renderer/lib/assistant-motion-contract.test.ts renderer/lib/dialog-motion-contract.test.ts renderer/lib/scrollbar-gutter-contract.test.ts renderer/lib/scroll-follow.test.ts renderer/lib/text-entry-focus-contract.test.ts renderer/lib/chat-deletion-cache.test.ts renderer/lib/chat-terminal-sync.test.ts renderer/lib/ipc-stream.test.ts renderer/lib/chat-title-reveal.test.ts renderer/lib/codex-auth-session.test.ts renderer/lib/codex-provider-cache.test.ts renderer/lib/composer-placeholder.test.ts renderer/lib/composer-attachment-operation.test.ts renderer/components/composer-attachments-mounted.test.tsx renderer/lib/computer-use-notice.test.ts renderer/lib/context-pressure-feed.test.ts renderer/lib/dictation-operation-gate.test.ts renderer/lib/media-recorder-stop.test.ts renderer/lib/dictation-vad.test.ts renderer/lib/dictation-sounds.test.ts renderer/lib/editor-preference.test.ts renderer/lib/environment-panel-layout.test.ts renderer/lib/subagent-view-state.test.ts renderer/lib/truncate-path.test.ts renderer/lib/mcp-preset-state.test.ts renderer/components/settings/mcp-settings.test.tsx renderer/components/settings/mcp-preset-icons.test.ts renderer/lib/model-display.test.ts renderer/lib/model-picker-data.test.ts renderer/lib/profile-share-data.test.ts renderer/lib/sidebar-chat-shortcuts.test.ts renderer/lib/usage-profile-data.test.ts renderer/shared/appearance.test.ts renderer/shared/context-pressure.test.ts renderer/components/interface-polish.test.tsx renderer/shared/provider-deployment.test.ts main/handlers/ipc-contract.test.ts main/handlers/chat.parse.test.ts main/handlers/voice-codec.test.ts main/handlers/phase2-parse.test.ts scripts/apple-developer-tools.test.mjs scripts/check-macos-release.test.mjs scripts/computer-use-packaged-acceptance.test.mjs scripts/configure-electron-fuses.test.mjs scripts/model-snapshot-core.test.mjs scripts/prepare-macos-dev-runtime.test.mjs scripts/prepare-macos-package-output.test.mjs scripts/run-macos-distribution.test.mjs scripts/sign-macos.test.mjs scripts/hide-dmg-support-files.test.mjs scripts/update-model-capabilities.test.mjs scripts/vendor-cua-driver.test.mjs scripts/verify-macos-package.test.mjs main/services/chat-pull-request-store.test.ts main/services/chat-pull-request-service.test.ts main/services/pull-request-create-reconciliation.test.ts main/services/pull-request-current-resolver.test.ts renderer/shared/chat-pull-requests.test.ts main/services/form-fill/artifacts-core.test.ts main/services/form-fill/extract-core.test.ts main/services/form-fill/helper-client.test.ts main/services/form-fill/planner-core.test.ts main/services/form-fill/batch-core.test.ts main/services/form-fill/service.test.ts main/services/form-fill/eval-fixtures.test.ts main/services/computer-use/controller-form-fill.test.ts renderer/components/form-fill-approval.test.tsx && npm run test:telegram && npm run test:worktree-remover:native && npm run test:computer-use:native && npm run test:settings-design && npm run test:ci-policy && npm run test:devices && npm run test:durable-jobs && npm run test:cli && tsx --test main/services/tool-approval-rules.test.ts renderer/components/settings/tool-approval-settings.test.tsx && npm run test:chat-row-states", + "test": "npm run test:agents-instructions && npm run test:custom-model-options && tsx --test main/services/tool-output-store.test.ts main/services/mcp-oauth-client-metadata.test.ts main/handlers/assistant-parse.test.ts main/services/assistant/system-prompt.test.ts main/services/chat-activity-core.test.ts main/services/chat-generation-start.test.ts main/services/chat-title-policy.test.ts main/services/chat-title-routing.test.ts main/services/chat-store-core.test.ts main/services/empty-chat-migration.test.ts main/services/chat-first-message-commit.test.ts main/services/codex-provider.test.ts main/services/coding-tools.test.ts main/services/config-store-core.test.ts main/services/computer-use/computer-use-foundation.test.ts main/services/computer-use/computer-use-tool.test.ts main/services/computer-use/generation-gate.test.ts main/services/computer-use/safety.test.ts main/services/computer-use/settings-core.test.ts main/services/computer-use/status-core.test.ts main/services/data-store.test.ts main/services/data-store.resilience.test.ts main/services/terminal.test.ts main/services/terminal-history.test.ts main/services/aiden-config-dir.test.ts main/services/portable-config-core.test.ts main/services/portable-config-core.roundtrip.test.ts main/services/portable-config-watch-core.test.ts main/services/secret-map-core.test.ts main/services/dev-log.test.ts main/services/process-diagnostics.test.ts main/services/dictation-coordinator.test.ts main/services/dictation-paste.test.ts main/services/dictation-cleanup-core.test.ts main/services/dictation-hotkey.test.ts main/services/dictation-key-state.test.ts main/services/dictation-keycode.test.ts main/services/parakeet-protocol.test.ts main/services/parakeet-process-core.test.ts main/services/foundation-models-connection.test.ts main/services/foundation-models-connection-core.test.ts main/services/gemini-context-cache.test.ts main/services/generation-bound-connection-cache.test.ts main/services/generation-context.test.ts main/services/generation-messages.test.ts main/services/generation-runtime.test.ts main/services/external-editors.test.ts main/services/git.test.ts main/services/git-executable.test.ts main/services/generation-git-context.test.ts main/services/github-pull-request.test.ts main/services/model-runtime-core.test.ts main/services/opencode-session-attribution.test.ts main/services/models.test.ts main/services/mcp-oauth-operation.test.ts main/services/mcp-oauth-session.test.ts main/services/mcp-presets.test.ts renderer/shared/plugin-catalog.test.ts main/services/pi-credential-store-core.test.ts main/services/pi-provider-contract.test.ts main/services/profile-share-core.test.ts main/services/profile-share.test.ts main/services/profile-share-files.test.ts main/services/profile.test.ts main/services/provider-auth-flow-core.test.ts main/services/provider-auth-owner.test.ts main/services/provider-key-policy.test.ts main/services/provider-list-core.test.ts main/services/provider-artwork-core.test.ts main/services/provider-artwork.test.ts main/services/quit-barrier.test.ts main/services/scratch-workspace.test.ts main/services/skills-discovery.test.ts main/services/tool-approval.test.ts main/services/local-runtime-status.test.ts main/services/usage-store-core.test.ts main/services/workspace-files.test.ts main/windows/pill-window-security.test.ts renderer/components/assistant/use-assistant-chat.test.ts renderer/components/assistant/assistant-ui.test.tsx renderer/components/activity-feed.test.tsx renderer/components/environment-subagents-contract.test.ts renderer/components/subagents-panel.test.tsx renderer/lib/subagent-context-usage-store.test.ts renderer/components/chat-sidebar.test.tsx renderer/components/composer.test.tsx renderer/components/context-meter.test.tsx renderer/lib/chat-message-queue.test.ts renderer/lib/chat-draft.test.ts renderer/main/chat-transition.test.tsx renderer/components/usage/profile-share-card.test.tsx renderer/lib/accessibility-refresh.test.ts renderer/lib/agent-activity.test.ts renderer/lib/syntax-highlight.test.ts renderer/lib/chat-activity.test.ts renderer/lib/assistant-dock.test.ts renderer/lib/assistant-motion-contract.test.ts renderer/lib/dialog-motion-contract.test.ts renderer/lib/scrollbar-gutter-contract.test.ts renderer/lib/scroll-follow.test.ts renderer/lib/text-entry-focus-contract.test.ts renderer/lib/chat-deletion-cache.test.ts renderer/lib/chat-terminal-sync.test.ts renderer/lib/ipc-stream.test.ts renderer/lib/chat-title-reveal.test.ts renderer/lib/codex-auth-session.test.ts renderer/lib/codex-provider-cache.test.ts renderer/lib/composer-placeholder.test.ts renderer/lib/composer-attachment-operation.test.ts renderer/components/composer-attachments-mounted.test.tsx renderer/lib/computer-use-notice.test.ts renderer/lib/context-pressure-feed.test.ts renderer/lib/dictation-operation-gate.test.ts renderer/lib/media-recorder-stop.test.ts renderer/lib/dictation-vad.test.ts renderer/lib/dictation-sounds.test.ts renderer/lib/editor-preference.test.ts renderer/lib/environment-panel-layout.test.ts renderer/lib/subagent-view-state.test.ts renderer/lib/truncate-path.test.ts renderer/lib/mcp-preset-state.test.ts renderer/components/settings/mcp-settings.test.tsx renderer/components/settings/mcp-preset-icons.test.ts renderer/lib/model-display.test.ts renderer/lib/model-picker-data.test.ts renderer/lib/profile-share-data.test.ts renderer/lib/sidebar-chat-shortcuts.test.ts renderer/lib/usage-profile-data.test.ts renderer/shared/appearance.test.ts renderer/shared/context-pressure.test.ts renderer/components/interface-polish.test.tsx renderer/shared/provider-deployment.test.ts main/handlers/ipc-contract.test.ts main/handlers/chat.parse.test.ts main/handlers/voice-codec.test.ts main/handlers/phase2-parse.test.ts scripts/apple-developer-tools.test.mjs scripts/check-macos-release.test.mjs scripts/computer-use-packaged-acceptance.test.mjs scripts/configure-electron-fuses.test.mjs scripts/model-snapshot-core.test.mjs scripts/prepare-macos-dev-runtime.test.mjs scripts/prepare-macos-package-output.test.mjs scripts/run-macos-distribution.test.mjs scripts/sign-macos.test.mjs scripts/hide-dmg-support-files.test.mjs scripts/update-model-capabilities.test.mjs scripts/vendor-cua-driver.test.mjs scripts/verify-macos-package.test.mjs main/services/chat-pull-request-store.test.ts main/services/chat-pull-request-service.test.ts main/services/pull-request-create-reconciliation.test.ts main/services/pull-request-current-resolver.test.ts renderer/shared/chat-pull-requests.test.ts main/services/form-fill/artifacts-core.test.ts main/services/form-fill/extract-core.test.ts main/services/form-fill/helper-client.test.ts main/services/form-fill/planner-core.test.ts main/services/form-fill/batch-core.test.ts main/services/form-fill/service.test.ts main/services/form-fill/eval-fixtures.test.ts main/services/computer-use/controller-form-fill.test.ts renderer/components/form-fill-approval.test.tsx && npm run test:telegram && npm run test:worktree-remover:native && npm run test:computer-use:native && npm run test:settings-design && npm run test:ci-policy && npm run test:devices && npm run test:durable-jobs && npm run test:cli && tsx --test main/services/tool-approval-rules.test.ts renderer/components/settings/tool-approval-settings.test.tsx && npm run test:chat-row-states", "test:coverage": "tsx --test --experimental-test-coverage main/services/mcp-oauth-client-metadata.test.ts main/handlers/assistant-parse.test.ts main/services/assistant/system-prompt.test.ts main/services/chat-activity-core.test.ts main/services/chat-generation-start.test.ts main/services/chat-title-policy.test.ts main/services/chat-title-routing.test.ts main/services/chat-store-core.test.ts main/services/empty-chat-migration.test.ts main/services/chat-first-message-commit.test.ts main/services/codex-provider.test.ts main/services/coding-tools.test.ts main/services/config-store-core.test.ts main/services/computer-use/computer-use-foundation.test.ts main/services/computer-use/computer-use-tool.test.ts main/services/computer-use/generation-gate.test.ts main/services/computer-use/safety.test.ts main/services/computer-use/settings-core.test.ts main/services/computer-use/status-core.test.ts main/services/data-store.test.ts main/services/data-store.resilience.test.ts main/services/terminal.test.ts main/services/terminal-history.test.ts main/services/aiden-config-dir.test.ts main/services/portable-config-core.test.ts main/services/portable-config-core.roundtrip.test.ts main/services/portable-config-watch-core.test.ts main/services/dev-log.test.ts main/services/process-diagnostics.test.ts main/services/dictation-coordinator.test.ts main/services/dictation-paste.test.ts main/services/dictation-cleanup-core.test.ts main/services/dictation-hotkey.test.ts main/services/dictation-key-state.test.ts main/services/dictation-keycode.test.ts main/services/parakeet-protocol.test.ts main/services/parakeet-process-core.test.ts main/services/foundation-models-connection.test.ts main/services/foundation-models-connection-core.test.ts main/services/generation-bound-connection-cache.test.ts main/services/generation-context.test.ts main/services/generation-messages.test.ts main/services/generation-runtime.test.ts main/services/external-editors.test.ts main/services/git.test.ts main/services/git-executable.test.ts main/services/generation-git-context.test.ts main/services/github-pull-request.test.ts main/services/model-runtime-core.test.ts main/services/opencode-session-attribution.test.ts main/services/pi-compaction-core.test.ts main/services/models.test.ts main/services/mcp-oauth-operation.test.ts main/services/mcp-oauth-session.test.ts main/services/mcp-presets.test.ts renderer/shared/plugin-catalog.test.ts main/services/pi-credential-store-core.test.ts main/services/pi-provider-contract.test.ts main/services/profile-share-core.test.ts main/services/profile-share.test.ts main/services/profile-share-files.test.ts main/services/profile.test.ts main/services/provider-auth-flow-core.test.ts main/services/provider-auth-owner.test.ts main/services/provider-key-policy.test.ts main/services/provider-list-core.test.ts main/services/provider-artwork-core.test.ts main/services/provider-artwork.test.ts main/services/quit-barrier.test.ts main/services/scratch-workspace.test.ts main/services/skills-discovery.test.ts main/services/tool-approval.test.ts main/services/local-runtime-status.test.ts main/services/usage-store-core.test.ts main/services/workspace-files.test.ts main/windows/pill-window-security.test.ts renderer/components/assistant/use-assistant-chat.test.ts renderer/components/assistant/assistant-ui.test.tsx renderer/components/environment-subagents-contract.test.ts renderer/components/subagents-panel.test.tsx renderer/lib/subagent-context-usage-store.test.ts renderer/components/chat-sidebar.test.tsx renderer/components/composer.test.tsx renderer/main/chat-transition.test.tsx renderer/components/usage/profile-share-card.test.tsx renderer/lib/accessibility-refresh.test.ts renderer/lib/agent-activity.test.ts renderer/lib/chat-activity.test.ts renderer/lib/assistant-dock.test.ts renderer/lib/assistant-motion-contract.test.ts renderer/lib/dialog-motion-contract.test.ts renderer/lib/chat-deletion-cache.test.ts renderer/lib/chat-terminal-sync.test.ts renderer/lib/ipc-stream.test.ts renderer/lib/chat-title-reveal.test.ts renderer/lib/codex-auth-session.test.ts renderer/lib/codex-auth-view-state.test.ts renderer/lib/codex-provider-cache.test.ts renderer/lib/composer-placeholder.test.ts renderer/lib/composer-attachment-operation.test.ts renderer/components/composer-attachments-mounted.test.tsx renderer/lib/computer-use-notice.test.ts renderer/lib/dictation-operation-gate.test.ts renderer/lib/media-recorder-stop.test.ts renderer/lib/dictation-vad.test.ts renderer/lib/dictation-sounds.test.ts renderer/lib/editor-preference.test.ts renderer/lib/environment-panel-layout.test.ts renderer/lib/subagent-view-state.test.ts renderer/lib/truncate-path.test.ts renderer/lib/mcp-preset-state.test.ts renderer/components/settings/mcp-settings.test.tsx renderer/components/settings/mcp-preset-icons.test.ts renderer/lib/model-display.test.ts renderer/lib/profile-share-data.test.ts renderer/lib/sidebar-chat-shortcuts.test.ts renderer/lib/usage-profile-data.test.ts renderer/shared/appearance.test.ts renderer/shared/provider-deployment.test.ts main/handlers/ipc-contract.test.ts main/handlers/chat.parse.test.ts main/handlers/voice-codec.test.ts main/handlers/phase2-parse.test.ts scripts/apple-developer-tools.test.mjs scripts/check-macos-release.test.mjs scripts/computer-use-packaged-acceptance.test.mjs scripts/configure-electron-fuses.test.mjs scripts/model-snapshot-core.test.mjs scripts/prepare-macos-dev-runtime.test.mjs scripts/prepare-macos-package-output.test.mjs scripts/run-macos-distribution.test.mjs scripts/sign-macos.test.mjs scripts/update-model-capabilities.test.mjs scripts/vendor-cua-driver.test.mjs scripts/verify-macos-package.test.mjs main/services/tool-output-store.test.ts main/services/chat-pull-request-store.test.ts main/services/chat-pull-request-service.test.ts main/services/pull-request-create-reconciliation.test.ts main/services/pull-request-current-resolver.test.ts renderer/shared/chat-pull-requests.test.ts main/services/form-fill/artifacts-core.test.ts main/services/form-fill/extract-core.test.ts main/services/form-fill/helper-client.test.ts main/services/form-fill/planner-core.test.ts main/services/form-fill/batch-core.test.ts main/services/form-fill/service.test.ts main/services/form-fill/eval-fixtures.test.ts main/services/computer-use/controller-form-fill.test.ts renderer/components/form-fill-approval.test.tsx", "test:computer-use": "tsx --test main/services/computer-use/computer-use-foundation.test.ts main/services/computer-use/computer-use-tool.test.ts main/services/computer-use/generation-gate.test.ts main/services/computer-use/safety.test.ts main/services/computer-use/settings-core.test.ts main/services/computer-use/status-core.test.ts main/services/data-store.test.ts main/services/generation-messages.test.ts main/services/generation-runtime.test.ts main/services/quit-barrier.test.ts main/services/tool-approval.test.ts scripts/check-macos-release.test.mjs scripts/computer-use-packaged-acceptance.test.mjs scripts/configure-electron-fuses.test.mjs scripts/prepare-macos-package-output.test.mjs scripts/run-macos-distribution.test.mjs scripts/sign-macos.test.mjs scripts/vendor-cua-driver.test.mjs scripts/verify-macos-package.test.mjs main/services/form-fill/artifacts-core.test.ts main/services/form-fill/extract-core.test.ts main/services/form-fill/helper-client.test.ts main/services/form-fill/planner-core.test.ts main/services/form-fill/batch-core.test.ts main/services/form-fill/service.test.ts main/services/form-fill/eval-fixtures.test.ts main/services/computer-use/controller-form-fill.test.ts renderer/components/form-fill-approval.test.tsx && npm run test:computer-use:native", "test:computer-use:packaged": "node scripts/computer-use-packaged-acceptance.mjs", diff --git a/renderer/components/code-block.tsx b/renderer/components/code-block.tsx index 8883a1d4d2..522efd21b6 100644 --- a/renderer/components/code-block.tsx +++ b/renderer/components/code-block.tsx @@ -1,11 +1,18 @@ // Fenced code block for the chat transcript: syntax highlighting via highlight.js, // a language label, and a per-block copy button. Source formatting is preserved // so a valid-looking JSON prefix never reformats and jumps while streaming. +// Grammars load lazily per language; the block renders as plain text until its +// grammar is ready, and unlabeled fences stay plain text (no auto-detection). // Highlighting is memoized so streaming re-renders stay cheap. import * as React from "react"; -import hljs from "highlight.js"; import { CopyButton } from "./copy-button"; +import { + highlightCode, + highlightLanguagesVersion, + loadHighlightLanguage, + subscribeHighlightLanguages, +} from "../lib/syntax-highlight"; interface CodeBlockProps { /** Raw code text (without the enclosing fence). */ @@ -17,17 +24,21 @@ interface CodeBlockProps { export const CodeBlock = React.memo(function CodeBlock({ code, lang }: CodeBlockProps) { const display = code.replace(/\n$/, ""); const language = lang; + // Changes when any lazily loaded grammar lands, re-running the memo below. + const grammarsVersion = React.useSyncExternalStore( + subscribeHighlightLanguages, + highlightLanguagesVersion, + highlightLanguagesVersion, + ); + + React.useEffect(() => { + void loadHighlightLanguage(language); + }, [language]); - const html = React.useMemo(() => { - try { - if (language && hljs.getLanguage(language)) { - return hljs.highlight(display, { language, ignoreIllegals: true }).value; - } - return hljs.highlightAuto(display).value; - } catch { - return null; - } - }, [display, language]); + const html = React.useMemo( + () => highlightCode(display, language), + [display, language, grammarsVersion], + ); return (
diff --git a/renderer/lib/syntax-highlight.test.ts b/renderer/lib/syntax-highlight.test.ts new file mode 100644 index 0000000000..8e32b05f34 --- /dev/null +++ b/renderer/lib/syntax-highlight.test.ts @@ -0,0 +1,67 @@ +import assert from "node:assert/strict"; +import test from "node:test"; +import { + highlightCode, + highlightLanguagesVersion, + loadHighlightLanguage, + resolveHighlightLanguage, + subscribeHighlightLanguages, +} from "./syntax-highlight.js"; + +test("common fence labels resolve to a supported grammar", () => { + assert.equal(resolveHighlightLanguage("TSX"), "typescript"); + assert.equal(resolveHighlightLanguage(" sh "), "bash"); + assert.equal(resolveHighlightLanguage("html"), "xml"); + assert.equal(resolveHighlightLanguage("c++"), "cpp"); + assert.equal(resolveHighlightLanguage("python"), "python"); +}); + +test("unlabeled and unknown fences stay plain text", async () => { + assert.equal(resolveHighlightLanguage(undefined), undefined); + assert.equal(resolveHighlightLanguage(""), undefined); + assert.equal(resolveHighlightLanguage("brainfuck"), undefined); + assert.equal(resolveHighlightLanguage("__proto__"), undefined); + assert.equal(await loadHighlightLanguage("brainfuck"), false); + assert.equal(highlightCode("+++[>+<-]", "brainfuck"), null); +}); + +test("a grammar highlights only after it loads and notifies subscribers once", async () => { + const code = "const answer: number = 42;"; + assert.equal(highlightCode(code, "ts"), null, "not highlighted before the grammar loads"); + + let notifications = 0; + const unsubscribe = subscribeHighlightLanguages(() => { + notifications += 1; + }); + const before = highlightLanguagesVersion(); + const [first, second] = await Promise.all([ + loadHighlightLanguage("ts"), + loadHighlightLanguage("typescript"), + ]); + unsubscribe(); + + assert.equal(first, true); + assert.equal(second, true); + assert.equal(notifications, 1, "concurrent loads of one grammar share a single registration"); + assert.ok(highlightLanguagesVersion() > before); + + const html = highlightCode(code, "tsx"); + assert.ok(html, "highlighted after load"); + assert.match(html, /const<\/span>/u); + assert.match(html, /42<\/span>/u); +}); + +test("highlighted output escapes markup in the source", async () => { + assert.equal(await loadHighlightLanguage("html"), true); + const html = highlightCode("", "html"); + assert.ok(html); + assert.doesNotMatch(html, /", "xml"); + assert.ok(html); + assert.match(html, /const<\/span>/u); +}); diff --git a/renderer/lib/syntax-highlight.ts b/renderer/lib/syntax-highlight.ts new file mode 100644 index 0000000000..5a4efe1737 --- /dev/null +++ b/renderer/lib/syntax-highlight.ts @@ -0,0 +1,210 @@ +// Lazy syntax highlighting for transcript code blocks. +// +// The full highlight.js build registers ~190 grammars (~900 KB minified) and +// its auto-detection runs every grammar over the text. Chat code blocks only +// need a handful of common languages, so the core and each grammar load on +// demand the first time a fence names them. Unlabeled or unknown fences stay +// plain text: there is no auto-detection. + +import type { HLJSApi, LanguageFn } from "highlight.js"; + +type GrammarModule = { default: LanguageFn }; + +const GRAMMARS: Record Promise> = { + bash: () => import("highlight.js/lib/languages/bash"), + c: () => import("highlight.js/lib/languages/c"), + cpp: () => import("highlight.js/lib/languages/cpp"), + csharp: () => import("highlight.js/lib/languages/csharp"), + css: () => import("highlight.js/lib/languages/css"), + dart: () => import("highlight.js/lib/languages/dart"), + diff: () => import("highlight.js/lib/languages/diff"), + dockerfile: () => import("highlight.js/lib/languages/dockerfile"), + elixir: () => import("highlight.js/lib/languages/elixir"), + go: () => import("highlight.js/lib/languages/go"), + graphql: () => import("highlight.js/lib/languages/graphql"), + haskell: () => import("highlight.js/lib/languages/haskell"), + ini: () => import("highlight.js/lib/languages/ini"), + java: () => import("highlight.js/lib/languages/java"), + javascript: () => import("highlight.js/lib/languages/javascript"), + json: () => import("highlight.js/lib/languages/json"), + kotlin: () => import("highlight.js/lib/languages/kotlin"), + less: () => import("highlight.js/lib/languages/less"), + lua: () => import("highlight.js/lib/languages/lua"), + makefile: () => import("highlight.js/lib/languages/makefile"), + markdown: () => import("highlight.js/lib/languages/markdown"), + objectivec: () => import("highlight.js/lib/languages/objectivec"), + perl: () => import("highlight.js/lib/languages/perl"), + php: () => import("highlight.js/lib/languages/php"), + plaintext: () => import("highlight.js/lib/languages/plaintext"), + powershell: () => import("highlight.js/lib/languages/powershell"), + protobuf: () => import("highlight.js/lib/languages/protobuf"), + python: () => import("highlight.js/lib/languages/python"), + r: () => import("highlight.js/lib/languages/r"), + ruby: () => import("highlight.js/lib/languages/ruby"), + rust: () => import("highlight.js/lib/languages/rust"), + scala: () => import("highlight.js/lib/languages/scala"), + scss: () => import("highlight.js/lib/languages/scss"), + shell: () => import("highlight.js/lib/languages/shell"), + sql: () => import("highlight.js/lib/languages/sql"), + swift: () => import("highlight.js/lib/languages/swift"), + typescript: () => import("highlight.js/lib/languages/typescript"), + xml: () => import("highlight.js/lib/languages/xml"), + yaml: () => import("highlight.js/lib/languages/yaml"), +}; + +// Grammars that embed another grammar (shell sessions highlight their commands +// as bash; HTML highlights inline