-
{showEditor && (
-
)}
@@ -646,45 +452,32 @@ export const TextEditor = memo(function TextEditor({
closeContextMenu()
}}
onCut={() => {
- monacoEditorRef.current?.focus()
- monacoEditorRef.current?.trigger(
- 'contextmenu',
- 'editor.action.clipboardCutAction',
- null
- )
closeContextMenu()
+ runClipboardCommand('cut')
}}
onCopy={() => {
- monacoEditorRef.current?.focus()
- monacoEditorRef.current?.trigger(
- 'contextmenu',
- 'editor.action.clipboardCopyAction',
- null
- )
closeContextMenu()
+ runClipboardCommand('copy')
}}
onCopyAll={() => {
- navigator.clipboard
- .writeText(monacoEditorRef.current?.getValue() ?? '')
- .catch(() => {})
closeContextMenu()
+ navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {})
+ editorRef.current?.focus()
}}
onPaste={() => {
- monacoEditorRef.current?.focus()
- monacoEditorRef.current?.trigger(
- 'contextmenu',
- 'editor.action.clipboardPasteAction',
- null
- )
closeContextMenu()
+ navigator.clipboard
+ .readText()
+ .then((text) => {
+ if (!text || !admitsPaste(text)) return
+ editorRef.current?.replaceSelection(text)
+ editorRef.current?.focus()
+ })
+ .catch(() => {})
}}
onSelectAll={() => {
- monacoEditorRef.current?.focus()
- monacoEditorRef.current?.trigger('contextmenu', 'editor.action.selectAll', null)
- closeContextMenu()
- }}
- onFind={() => {
- monacoEditorRef.current?.getAction('actions.find')?.run()
+ editorRef.current?.selectAll()
+ editorRef.current?.focus()
closeContextMenu()
}}
/>
diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge.ts b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge.ts
index 908951e0745..295a2cab403 100644
--- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge.ts
+++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/use-selection-copy-bridge.ts
@@ -9,8 +9,8 @@ import type { ChatContext } from '@/stores/panel'
* highlighted passage copied with Cmd+C pastes into Chat as a reference chip.
*
* Attached in the BUBBLE phase so it runs after the inner editor's own copy
- * handler — Monaco and ProseMirror both `clearData()` before writing
- * `text/plain`, so the custom type must be added last to survive.
+ * handler — the code editor and ProseMirror both write `text/plain`
+ * themselves, so the custom type must be added last to survive.
*
* @param buildContext - Returns null when there is no non-empty selection.
* @param workspaceId - Workspace that owns the selected resource.
@@ -28,15 +28,17 @@ export function useSelectionCopyBridge(
const dom = containerRef.current
if (!dom || !enabled) return
const onCopy = (e: ClipboardEvent) => {
- // A copy from a field nested in the editor — Monaco's find box being the
+ // A copy from a field nested in the editor — its search box being the
// common one — bubbles here while the document still holds a highlight,
- // so the selection would be attached to text the user never copied.
+ // so the selection would be attached to text the user never copied. The
+ // field sits inside the code editor's shadow root, where `target` is
+ // retargeted to the host, so the origin is read from the composed path.
//
- // Only INPUT is skipped, deliberately: Monaco's own editing surface is a
- // hidden TEXTAREA, so excluding textareas (as the table grid does, where
- // the cell editors really are form fields) would suppress the chip on the
- // main copy path this hook exists for.
- if ((e.target as HTMLElement | null)?.tagName === 'INPUT') return
+ // Only INPUT is skipped, deliberately: an editing surface can be a
+ // TEXTAREA, so excluding textareas (as the table grid does, where the cell
+ // editors really are form fields) would suppress the chip on the main
+ // copy path this hook exists for.
+ if (e.composedPath()[0] instanceof HTMLInputElement) return
const context = buildContext()
if (context) attachSelectionContextToClipboard(e.clipboardData, context, workspaceId)
}
diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.test.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.test.tsx
index a585d09f1b5..da05646819e 100644
--- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.test.tsx
+++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.test.tsx
@@ -6,10 +6,8 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createRoot, type Root } from 'react-dom/client'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
-vi.mock('@/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor', () => ({
- MonacoEditor: () => null,
- TEXT_EDITOR_OPTIONS: {},
- useMonacoTheme: () => 'vs',
+vi.mock('@/components/code-editor/code-editor', () => ({
+ CodeEditor: () => null,
}))
import { WorkflowScopeProvider } from '@/app/workspace/[workspaceId]/providers/workflow-scope-provider'
@@ -26,7 +24,7 @@ const WORKFLOW_ID = 'wf-1'
const SETTLE_MS = 1_200
/**
- * The Code tab against the real stores, with only the network (the source read) and Monaco
+ * The Code tab against the real stores, with only the network (the source read) and the editor
* replaced: a collaborator's field edit arrives as the collaborative hook applies it (a subblock
* store write plus `markRemoteApplied`), with no workflow-store change and no queued operation.
*/
diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.tsx b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.tsx
index 1404990a408..665564a8125 100644
--- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.tsx
+++ b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/code.tsx
@@ -1,24 +1,14 @@
'use client'
-import { useEffect, useState } from 'react'
+import { useEffect } from 'react'
import { Badge, Button, Tooltip, useCopyToClipboard } from '@sim/emcn'
import { BookOpen, Check, TerminalWindow } from '@sim/emcn/icons'
import { BLOCK_OPERATIONS, BLOCKS_OPERATIONS } from '@sim/realtime-protocol/constants'
import { slugify } from '@sim/utils/string'
import { useQueryClient } from '@tanstack/react-query'
-import type { editor } from 'monaco-editor'
+import { CodeEditor } from '@/components/code-editor/code-editor'
import { DOCS_URL } from '@/lib/help-links'
-import {
- MonacoEditor,
- TEXT_EDITOR_OPTIONS,
- useMonacoTheme,
-} from '@/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor'
import { useWorkflowScope } from '@/app/workspace/[workspaceId]/providers/workflow-scope-provider'
-import {
- registerSourceLanguage,
- SOURCE_LANGUAGE_ID,
-} from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/source-language'
-import { minimalTextEdit } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit'
import { subscribeToDraftChanges } from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/deploy/hooks/use-draft-workflow-state'
import { useWorkflowSourceText, workflowSourceKeys } from '@/hooks/queries/workflow-source'
import { useOperationQueueStore } from '@/stores/operation-queue/store'
@@ -27,8 +17,6 @@ import type { QueuedOperation } from '@/stores/operation-queue/types'
/** Quiet period after the last edit before the source is re-read. */
const REFRESH_DEBOUNCE_MS = 750
-const CODE_VIEW_OPTIONS = { ...TEXT_EDITOR_OPTIONS, readOnly: true, domReadOnly: true }
-
const WORKFLOW_CODE_DOCS_URL = `${DOCS_URL}/workflows/code`
/** Canvas geometry operations (moves and handle orientation), which the printed source never shows. */
@@ -93,33 +81,6 @@ function useRefreshWhenDraftSettles(workflowId: string | null) {
}, [workflowId, queryClient])
}
-/**
- * Brings the editor's model to `text` with the one edit that differs, so a refresh keeps the
- * scroll position, cursor and tokens of every unchanged line. Replacing the value instead (what
- * the `value` prop does for a read-only editor) would jump to line 1 and re-tokenize the file.
- */
-function useSyncedModelText(view: editor.IStandaloneCodeEditor | null, text: string) {
- useEffect(() => {
- const model = view?.getModel()
- if (!model) return
- const edit = minimalTextEdit(model.getValue(), text)
- if (!edit) return
- const start = model.getPositionAt(edit.start)
- const end = model.getPositionAt(edit.end)
- model.applyEdits([
- {
- range: {
- startLineNumber: start.lineNumber,
- startColumn: start.column,
- endLineNumber: end.lineNumber,
- endColumn: end.column,
- },
- text: edit.text,
- },
- ])
- }, [view, text])
-}
-
interface CodeHeaderProps {
workflowId: string | null
workflowName?: string
@@ -194,8 +155,6 @@ interface CodeProps {
/** The workflow draft as read-only TypeScript source. */
export function Code({ isActive, workflowName }: CodeProps) {
const { workflowId } = useWorkflowScope()
- const monacoTheme = useMonacoTheme()
- const [view, setView] = useState
(null)
const {
data: text,
error,
@@ -204,7 +163,6 @@ export function Code({ isActive, workflowName }: CodeProps) {
enabled: isActive,
})
useRefreshWhenDraftSettles(workflowId)
- useSyncedModelText(view, text ?? '')
return (
@@ -221,15 +179,7 @@ export function Code({ isActive, workflowName }: CodeProps) {
Loading code…
) : (
-
+
)}
diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/source-language.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/source-language.ts
deleted file mode 100644
index 314ee6e43b5..00000000000
--- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/source-language.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import type * as MonacoApi from 'monaco-editor'
-
-/**
- * Display-only TypeScript: the printed source colored with Monaco's TypeScript grammar under a
- * language id of its own. Under `typescript` the model would start the TypeScript worker and run
- * diagnostics over up to 1.2 MB of source nobody can edit.
- */
-export const SOURCE_LANGUAGE_ID = 'sim-workflow-source'
-
-const loadTypeScriptGrammar = () =>
- import('monaco-editor/languages/definitions/typescript/typescript')
-
-/** Registers {@link SOURCE_LANGUAGE_ID} once per Monaco instance; pass as `beforeMount`. */
-export function registerSourceLanguage(monaco: typeof MonacoApi): void {
- if (monaco.languages.getLanguages().some((language) => language.id === SOURCE_LANGUAGE_ID)) {
- return
- }
- monaco.languages.register({ id: SOURCE_LANGUAGE_ID })
- monaco.languages.registerTokensProviderFactory(SOURCE_LANGUAGE_ID, {
- create: async () => (await loadTypeScriptGrammar()).language,
- })
- monaco.languages.onLanguageEncountered(SOURCE_LANGUAGE_ID, async () => {
- monaco.languages.setLanguageConfiguration(
- SOURCE_LANGUAGE_ID,
- (await loadTypeScriptGrammar()).conf
- )
- })
-}
diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.test.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.test.ts
deleted file mode 100644
index 4f8e4ebb1b3..00000000000
--- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.test.ts
+++ /dev/null
@@ -1,59 +0,0 @@
-import { describe, expect, it } from 'vitest'
-import {
- minimalTextEdit,
- type TextEdit,
-} from '@/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit'
-
-function apply(before: string, edit: TextEdit | null): string {
- if (!edit) return before
- return before.slice(0, edit.start) + edit.text + before.slice(edit.end)
-}
-
-describe('minimalTextEdit', () => {
- it('returns null for identical text', () => {
- expect(minimalTextEdit('const a = 1\n', 'const a = 1\n')).toBeNull()
- })
-
- it('replaces only the changed range of a long file', () => {
- const head = 'line\n'.repeat(10_000)
- const tail = 'other\n'.repeat(10_000)
- const edit = minimalTextEdit(`${head}return 40 + 2\n${tail}`, `${head}return 6 * 7\n${tail}`)
- expect(edit).toEqual({ start: head.length + 7, end: head.length + 13, text: '6 * 7' })
- })
-
- it('never counts a character into both the prefix and the suffix', () => {
- expect(minimalTextEdit('aa', 'aaa')).toEqual({ start: 2, end: 2, text: 'a' })
- expect(minimalTextEdit('aaa', 'aa')).toEqual({ start: 2, end: 3, text: '' })
- expect(minimalTextEdit('abab', 'ab')).toEqual({ start: 2, end: 4, text: '' })
- })
-
- it('keeps a changed surrogate pair whole', () => {
- const edit = minimalTextEdit('x😀y', 'x😁y')
- expect(edit).toEqual({ start: 1, end: 3, text: '😁' })
- // U+1F600 and U+1FA00 share their low surrogate, so only the high one differs.
- expect(minimalTextEdit('x\u{1F600}', 'x\u{1FA00}')).toEqual({
- start: 1,
- end: 3,
- text: '\u{1FA00}',
- })
- const removed = minimalTextEdit('😀😀', '😀')
- expect(removed?.start).toBe(2)
- expect(removed?.end).toBe(4)
- })
-
- it('turns the old text into the new one for every pair of a small alphabet', () => {
- const words = ['', 'a', 'b', 'ab', 'ba', 'aab', 'abb', 'aba', '😀', 'a😀', '😀a', '😁😀']
- for (const before of words) {
- for (const after of words) {
- const edit = minimalTextEdit(before, after)
- expect(apply(before, edit)).toBe(after)
- if (edit) {
- expect(edit.start).toBeLessThanOrEqual(edit.end)
- expect(edit.end - edit.start + edit.text.length).toBeLessThanOrEqual(
- before.length + after.length
- )
- }
- }
- }
- })
-})
diff --git a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.ts b/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.ts
deleted file mode 100644
index 5a349df3ef4..00000000000
--- a/apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.ts
+++ /dev/null
@@ -1,35 +0,0 @@
-/** Replace `before.slice(start, end)` with `text` to turn `before` into the new text. */
-export interface TextEdit {
- start: number
- end: number
- text: string
-}
-
-const isHighSurrogate = (code: number) => code >= 0xd800 && code <= 0xdbff
-const isLowSurrogate = (code: number) => code >= 0xdc00 && code <= 0xdfff
-
-/**
- * The single smallest edit from `before` to `after`: everything between their common prefix and
- * common suffix. Null when they are equal. The edit never splits a surrogate pair, so each end
- * lands on a character boundary the editor can map to a position.
- */
-export function minimalTextEdit(before: string, after: string): TextEdit | null {
- if (before === after) return null
- const shorter = Math.min(before.length, after.length)
- let prefix = 0
- while (prefix < shorter && before.charCodeAt(prefix) === after.charCodeAt(prefix)) prefix++
- if (prefix > 0 && isHighSurrogate(before.charCodeAt(prefix - 1))) prefix--
- let suffix = 0
- while (
- suffix < shorter - prefix &&
- before.charCodeAt(before.length - 1 - suffix) === after.charCodeAt(after.length - 1 - suffix)
- ) {
- suffix++
- }
- if (suffix > 0 && isLowSurrogate(before.charCodeAt(before.length - suffix))) suffix--
- return {
- start: prefix,
- end: before.length - suffix,
- text: after.slice(prefix, after.length - suffix),
- }
-}
diff --git a/apps/sim/components/code-editor/code-editor.tsx b/apps/sim/components/code-editor/code-editor.tsx
new file mode 100644
index 00000000000..0439fbd63b6
--- /dev/null
+++ b/apps/sim/components/code-editor/code-editor.tsx
@@ -0,0 +1,416 @@
+'use client'
+
+import {
+ type CSSProperties,
+ type Ref,
+ useCallback,
+ useEffect,
+ useImperativeHandle,
+ useMemo,
+ useRef,
+ useState,
+} from 'react'
+import type { CodeViewFileItem, SupportedLanguages } from '@pierre/diffs'
+import {
+ type EditCompletionDecision,
+ type Editor,
+ type EditorChangeEvent,
+ type EditorCommand,
+ type EditorFactory,
+ type EditorOptions,
+ type EditorType,
+ Editor as PierreEditor,
+ type Position,
+ resolveEditorCommandFromKeyboardEvent,
+} from '@pierre/diffs/edit'
+import { CodeView, type CodeViewHandle, EditProvider } from '@pierre/diffs/react'
+import { cn } from '@sim/emcn'
+import { EDITOR_SURFACE, usePierreThemeType } from '@/components/code-editor/pierre-theme'
+
+const ITEM_ID = 'document'
+
+const createEditor: EditorFactory = (type, options, editStateKey) =>
+ new PierreEditor(type, options, editStateKey)
+
+const rejectCompletion = (): EditCompletionDecision => 'reject'
+
+/** Distance from the bottom, in pixels, inside which a growing document keeps its end in view. */
+const FOLLOW_THRESHOLD_PX = 80
+
+/** Commands that change the document, which a read-only editor refuses. */
+const EDITING_COMMANDS: ReadonlySet = new Set([
+ 'indent',
+ 'outdent',
+ 'indentLess',
+ 'indentMore',
+ 'undo',
+ 'redo',
+ 'moveLineUp',
+ 'moveLineDown',
+ 'copyLineUp',
+ 'copyLineDown',
+ 'insertBlankLine',
+ 'deleteHardLineForward',
+ 'toggleComment',
+ 'toggleBlockComment',
+ 'openSearchReplacePanel',
+])
+
+/** Keys the editor turns into edits without naming a command. */
+const EDITING_KEYS = new Set(['Enter', 'Backspace', 'Delete'])
+
+/** Events that put text into, or take text out of, the document. */
+const MUTATING_EVENTS = [
+ 'beforeinput',
+ 'paste',
+ 'cut',
+ 'drop',
+ 'compositionstart',
+ 'compositionend',
+]
+
+/**
+ * The editor's chrome in the app's own roles rather than Pierre's VS Code-like defaults:
+ * selections and the current search match take the app-wide `::selection` tint, other matches
+ * the app's find highlight (as chat's find does), a matched bracket a quiet fill with no
+ * underline, and the caret's line a full-width `--surface-hover` band with its number in body
+ * text instead of an outlined row.
+ *
+ * Pierre declares these unlayered inside its shadow root and layers `unsafeCSS` beneath them,
+ * so every declaration here is important; nothing else outranks them from outside.
+ */
+const EDITOR_CHROME_CSS = `
+ :host {
+ --diffs-editor-selection-bg: var(--selection-muted) !important;
+ --diffs-editor-match-bg: var(--selection-muted) !important;
+ --diffs-editor-match-highlight-bg: var(--highlight-match-bg) !important;
+ --diffs-editor-bracket-match-border: transparent !important;
+ --diffs-editor-bracket-match-bg: var(--surface-active) !important;
+ --diffs-editor-line-highlight-border: transparent !important;
+ }
+`
+
+const EDITOR_CSS = `${EDITOR_CHROME_CSS}
+ [data-line][data-editor-active-line],
+ [data-column-number][data-editor-active-line] {
+ --diffs-computed-editor-active-line-bg: var(--surface-hover) !important;
+ }
+ [data-column-number][data-editor-active-line] { color: var(--text-primary) !important; }
+`
+
+/** Read-only marks no caret line, and search keeps its find half without the replace half. */
+const READ_ONLY_CSS = `${EDITOR_CHROME_CSS}
+ [data-replace-toggle] { display: none !important; }
+`
+
+/** The caret in the body text color; a read-only document has no caret worth drawing. */
+const EDITOR_TOKENS = {
+ ...EDITOR_SURFACE.tokens,
+ '--diffs-bg-caret-override': 'var(--text-primary)',
+} as CSSProperties
+
+const READ_ONLY_TOKENS = {
+ ...EDITOR_SURFACE.tokens,
+ '--diffs-bg-caret-override': 'transparent',
+} as CSSProperties
+
+/** The selected text and the 1-based lines it spans. */
+interface CodeEditorSelection {
+ text: string
+ startLine: number
+ endLine: number
+}
+
+export interface CodeEditorHandle {
+ getText(): string
+ /** The first selection, or null when nothing is selected. */
+ getSelection(): CodeEditorSelection | null
+ /** Every selection and caret as `[start, end)` offsets into the text. */
+ getSelectionRanges(): Array<{ start: number; end: number }>
+ /** Replaces every selection with `text`, or inserts it at the caret. Ignored read-only. */
+ replaceSelection(text: string): void
+ selectAll(): void
+ focus(): void
+}
+
+interface CodeEditorProps {
+ /**
+ * The document. A value that differs from what the editor holds is applied as an external
+ * update, which keeps the scroll position and does not join the undo history.
+ */
+ value: string
+ /** Names the document; its extension picks the syntax unless `language` is given. */
+ fileName: string
+ language?: SupportedLanguages
+ /** Selection, copy and search still work; every edit is refused. */
+ readOnly?: boolean
+ onChange?: (value: string) => void
+ autoFocus?: boolean
+ /** While the document grows, keep its end in view unless the reader has scrolled away. */
+ followOutput?: boolean
+ className?: string
+ ref?: Ref
+}
+
+function offsetAt(text: string, { line, character }: Position): number {
+ let offset = 0
+ for (let index = 0; index < line; index++) {
+ const newline = text.indexOf('\n', offset)
+ if (newline === -1) return text.length
+ offset = newline + 1
+ }
+ return Math.min(offset + character, text.length)
+}
+
+function endOf(text: string): Position {
+ const lines = text.split('\n')
+ return { line: lines.length - 1, character: lines.at(-1)?.length ?? 0 }
+}
+
+/** Whether an event belongs to a form field inside the editor, such as the search box. */
+function isFromField(event: Event): boolean {
+ const target = event.composedPath()[0]
+ return target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement
+}
+
+/**
+ * A code editor on Pierre: syntax highlighted in Sim's palette, virtualized, with search,
+ * undo, multiple selections and bracket matching. Light enough for the panel and the file
+ * viewer alike; there is no completion or type checking.
+ */
+export function CodeEditor({
+ value,
+ fileName,
+ language,
+ readOnly = false,
+ onChange,
+ autoFocus = false,
+ followOutput = false,
+ className,
+ ref,
+}: CodeEditorProps) {
+ const themeType = usePierreThemeType()
+ const containerRef = useRef(null)
+ const scrollerRef = useRef(null)
+ const viewRef = useRef>(null)
+ const editorRef = useRef | null>(null)
+ const readOnlyRef = useRef(readOnly)
+ readOnlyRef.current = readOnly
+ const onChangeRef = useRef(onChange)
+ onChangeRef.current = onChange
+ const valueRef = useRef(value)
+ valueRef.current = value
+ const followingRef = useRef(true)
+
+ /* What the editor holds, and the external document last handed to it. */
+ const [editorText, setEditorText] = useState(value)
+ const [published, setPublished] = useState({ contents: value, version: 0 })
+ if (value !== editorText) {
+ setEditorText(value)
+ setPublished({ contents: value, version: published.version + 1 })
+ }
+
+ const items = useMemo(
+ (): CodeViewFileItem[] => [
+ {
+ id: ITEM_ID,
+ type: 'file',
+ file: { name: fileName, contents: published.contents, lang: language },
+ edit: true,
+ version: published.version,
+ },
+ ],
+ [fileName, language, published]
+ )
+
+ /* Creation-time options: Pierre reads them once, when it builds the editor. */
+ const [editorOptions] = useState, 'onChange'>>(
+ () => ({
+ onAttach: (editor) => {
+ editorRef.current = editor
+ if (autoFocus) editor.focus({ preventScroll: true })
+ },
+ })
+ )
+
+ useImperativeHandle(
+ ref,
+ () => ({
+ getText: () => editorRef.current?.getText() ?? editorText,
+ getSelection: () => {
+ const editor = editorRef.current
+ const selection = editor?.getViewState().selections?.[0]
+ if (!editor || !selection) return null
+ const text = editor.getText()
+ const start = offsetAt(text, selection.start)
+ const end = offsetAt(text, selection.end)
+ if (start === end) return null
+ /* A selection ending at the start of a line takes nothing from that line. */
+ const endLine =
+ selection.end.character === 0 && selection.end.line > selection.start.line
+ ? selection.end.line
+ : selection.end.line + 1
+ return {
+ text: text.slice(Math.min(start, end), Math.max(start, end)),
+ startLine: selection.start.line + 1,
+ endLine,
+ }
+ },
+ getSelectionRanges: () => {
+ const editor = editorRef.current
+ if (!editor) return []
+ const text = editor.getText()
+ return (editor.getViewState().selections ?? []).map((selection) => {
+ const anchor = offsetAt(text, selection.start)
+ const focus = offsetAt(text, selection.end)
+ return { start: Math.min(anchor, focus), end: Math.max(anchor, focus) }
+ })
+ },
+ replaceSelection: (text) => {
+ const editor = editorRef.current
+ if (!editor || readOnlyRef.current) return
+ const selections = editor.getViewState().selections ?? []
+ editor.applyEdits(
+ selections.map((selection) => ({
+ range: { start: selection.start, end: selection.end },
+ newText: text,
+ }))
+ )
+ },
+ selectAll: () => {
+ const editor = editorRef.current
+ if (!editor) return
+ editor.setSelections([
+ {
+ start: { line: 0, character: 0 },
+ end: endOf(editor.getText()),
+ direction: 'forward',
+ },
+ ])
+ },
+ focus: () => editorRef.current?.focus({ preventScroll: true }),
+ }),
+ [editorText]
+ )
+
+ /** Read-only is a guard in front of a live editor, so search and selection keep working. */
+ useEffect(() => {
+ const container = containerRef.current
+ if (!container) return
+ const refuse = (event: Event) => {
+ if (!readOnlyRef.current || isFromField(event)) return
+ event.preventDefault()
+ event.stopPropagation()
+ }
+ const refuseEditingKeys = (event: KeyboardEvent) => {
+ if (!readOnlyRef.current || isFromField(event)) return
+ /* Tab leaves a read-only document, as it does any other read-only field, instead of indenting. */
+ if (event.key === 'Tab') {
+ event.stopPropagation()
+ return
+ }
+ const command = resolveEditorCommandFromKeyboardEvent(event)
+ if ((command && EDITING_COMMANDS.has(command)) || EDITING_KEYS.has(event.key)) {
+ event.preventDefault()
+ event.stopPropagation()
+ }
+ }
+ /*
+ * Closing search removes its field, which drops focus to the page; hand it back to the
+ * document, whose caret search left on the last match, so typing and shortcuts carry on.
+ */
+ const returnFocusFromClosedField = (event: FocusEvent) => {
+ const field = event.composedPath()[0]
+ if (!(field instanceof HTMLInputElement) || event.relatedTarget !== null) return
+ queueMicrotask(() => {
+ if (field.isConnected || document.activeElement !== document.body) return
+ editorRef.current?.focus({ preventScroll: true })
+ })
+ }
+ /* The editor moves its caret on any press; a right-click keeps the selection for a context menu. */
+ const keepSelectionOnSecondaryPress = (event: PointerEvent) => {
+ if (event.button === 2) event.stopPropagation()
+ }
+ for (const type of MUTATING_EVENTS) container.addEventListener(type, refuse, true)
+ container.addEventListener('keydown', refuseEditingKeys, true)
+ container.addEventListener('pointerdown', keepSelectionOnSecondaryPress, true)
+ container.addEventListener('focusout', returnFocusFromClosedField)
+ return () => {
+ for (const type of MUTATING_EVENTS) container.removeEventListener(type, refuse, true)
+ container.removeEventListener('keydown', refuseEditingKeys, true)
+ container.removeEventListener('pointerdown', keepSelectionOnSecondaryPress, true)
+ container.removeEventListener('focusout', returnFocusFromClosedField)
+ }
+ }, [])
+
+ useEffect(() => {
+ if (!followOutput || !followingRef.current) return
+ viewRef.current?.scrollTo({ type: 'item', id: ITEM_ID, align: 'end' })
+ }, [followOutput, published])
+
+ /* CodeView rebuilds on any new option or callback identity, so each one is stable. */
+ const options = useMemo(
+ () => ({
+ ...EDITOR_SURFACE.options,
+ themeType,
+ disableFileHeader: true,
+ overflow: 'wrap' as const,
+ unsafeCSS: readOnly ? READ_ONLY_CSS : EDITOR_CSS,
+ }),
+ [themeType, readOnly]
+ )
+
+ const handleScroll = useCallback((scrollTop: number) => {
+ const scroller = scrollerRef.current
+ if (!scroller) return
+ followingRef.current =
+ scroller.scrollHeight - scrollTop - scroller.clientHeight < FOLLOW_THRESHOLD_PX
+ }, [])
+
+ const handleEditChange = useCallback(
+ (event: EditorChangeEvent) => {
+ const text = event.file.contents
+ /*
+ * Applying an external update reports it too; only the user's edits are changes. The update
+ * replaced the document, so undo must not step back into what it replaced.
+ */
+ if (text === valueRef.current) {
+ event.editor.getEditState()?.document.clearHistory()
+ setEditorText(text)
+ return
+ }
+ /* An edit the guard could not intercept (an IME commit) is undone by restoring the value. */
+ if (readOnlyRef.current) {
+ setPublished((current) => ({ contents: valueRef.current, version: current.version + 1 }))
+ return
+ }
+ setEditorText(text)
+ onChangeRef.current?.(text)
+ },
+ []
+ )
+
+ return (
+
+
+
+
+
+ )
+}
diff --git a/apps/sim/components/code-editor/pierre-theme.ts b/apps/sim/components/code-editor/pierre-theme.ts
new file mode 100644
index 00000000000..c0406a7660b
--- /dev/null
+++ b/apps/sim/components/code-editor/pierre-theme.ts
@@ -0,0 +1,64 @@
+'use client'
+
+import '@sim/emcn/components/code/code.css'
+import type { CSSProperties } from 'react'
+import {
+ type BaseCodeOptions,
+ canUseWASM,
+ registerCustomTheme,
+ type ThemeTypes,
+} from '@pierre/diffs'
+import { buildCodeTokenZedTheme, buildSyntaxTheme, CODE_TOKEN_COLORS } from '@sim/emcn/syntax-theme'
+import { useTheme } from 'next-themes'
+
+/**
+ * Highlights tokenizes in slices around what is on screen, so a 30,000-line file never blocks
+ * the main thread the way a whole-file Shiki pass does. Only one backend runs per page, so every
+ * Pierre surface uses this one; Shiki remains for browsers without WebAssembly SIMD.
+ */
+const PREFERRED_HIGHLIGHTER =
+ typeof window !== 'undefined' && !canUseWASM() ? 'shiki-js' : 'highlights'
+
+interface CodeSurface {
+ /** Pierre's `theme` and highlighter options. */
+ options: Pick
+ /** Sim's fonts and surfaces as Pierre's custom properties, inherited through its shadow root. */
+ tokens: CSSProperties
+}
+
+/**
+ * Sim's syntax colors on one background, for both highlighter backends. Built on `code.css`'s
+ * variables, so Pierre colors code exactly like `Code` does, in both themes.
+ */
+function codeSurface(name: string, background: string): CodeSurface {
+ registerCustomTheme(name, async () =>
+ buildSyntaxTheme(name, 'light', { ...CODE_TOKEN_COLORS, background })
+ )
+ registerCustomTheme(name, async () => buildCodeTokenZedTheme(name, background), 'zed')
+ return {
+ options: { theme: { light: name, dark: name }, preferredHighlighter: PREFERRED_HIGHLIGHTER },
+ tokens: {
+ '--diffs-font-family': 'var(--font-mono)',
+ '--diffs-header-font-family': 'var(--font-season)',
+ '--diffs-font-size': '13px',
+ '--diffs-line-height': '22px',
+ '--diffs-bg-context-override': background,
+ '--diffs-bg-context-gutter-override': background,
+ '--diffs-bg-separator-override': 'var(--surface-3)',
+ '--diffs-bg-buffer-override': 'var(--surface-3)',
+ '--diffs-fg-number-override': 'var(--text-muted)',
+ } as CSSProperties,
+ }
+}
+
+/** Diffs sit on cards. */
+export const DIFF_SURFACE = codeSurface('sim-code', 'var(--surface-2)')
+
+/** The editor fills its pane, on the page background. */
+export const EDITOR_SURFACE = codeSurface('sim-code-editor', 'var(--bg)')
+
+/** Light or dark, following the app theme; a forced page theme wins over the user's choice. */
+export function usePierreThemeType(): ThemeTypes {
+ const { forcedTheme, resolvedTheme } = useTheme()
+ return (forcedTheme ?? resolvedTheme) === 'dark' ? 'dark' : 'light'
+}
diff --git a/apps/sim/components/diff/sim-file-diff.tsx b/apps/sim/components/diff/sim-file-diff.tsx
index e6365bf9cc5..2e669b360f4 100644
--- a/apps/sim/components/diff/sim-file-diff.tsx
+++ b/apps/sim/components/diff/sim-file-diff.tsx
@@ -1,39 +1,13 @@
'use client'
-import '@sim/emcn/components/code/code.css'
import type { CSSProperties } from 'react'
-import { type FileDiffMetadata, type FileDiffOptions, registerCustomTheme } from '@pierre/diffs'
+import type { FileDiffMetadata, FileDiffOptions } from '@pierre/diffs'
import { FileDiff } from '@pierre/diffs/react'
-import { buildSyntaxTheme, CODE_TOKEN_COLORS } from '@sim/emcn/syntax-theme'
-import { useTheme } from 'next-themes'
+import { DIFF_SURFACE, usePierreThemeType } from '@/components/code-editor/pierre-theme'
-const SYNTAX_THEME = 'sim-code'
-
-/**
- * Pierre highlights with Shiki, so it takes Sim's syntax colors as a TextMate theme built on
- * `code.css`'s variables: diffs color code exactly like `Code` does, in both themes.
- */
-registerCustomTheme(SYNTAX_THEME, async () =>
- buildSyntaxTheme(SYNTAX_THEME, 'light', {
- ...CODE_TOKEN_COLORS,
- background: 'var(--surface-2)',
- })
-)
-
-/**
- * Maps Sim's surfaces, change colors and fonts onto Pierre's custom properties, which inherit
- * through its shadow root. Row and word tints derive from the two change colors.
- */
+/** Sim's surface tokens plus its change colors; row and word tints derive from the two colors. */
const DIFF_TOKENS = {
- '--diffs-font-family': 'var(--font-mono)',
- '--diffs-header-font-family': 'var(--font-season)',
- '--diffs-font-size': '13px',
- '--diffs-line-height': '22px',
- '--diffs-bg-context-override': 'var(--surface-2)',
- '--diffs-bg-context-gutter-override': 'var(--surface-2)',
- '--diffs-bg-separator-override': 'var(--surface-3)',
- '--diffs-bg-buffer-override': 'var(--surface-3)',
- '--diffs-fg-number-override': 'var(--text-muted)',
+ ...DIFF_SURFACE.tokens,
'--diffs-addition-color-override': 'var(--badge-success-text)',
'--diffs-deletion-color-override': 'var(--badge-error-text)',
} as CSSProperties
@@ -55,15 +29,15 @@ interface SimFileDiffProps {
* and Sim's syntax colors. Every diff surface renders through this so they stay identical.
*/
export function SimFileDiff({ fileDiff, wrapLines = true, options }: SimFileDiffProps) {
- const { forcedTheme, resolvedTheme } = useTheme()
+ const themeType = usePierreThemeType()
return (
{
- const ts = await loadTypeScript()
- const { outputText, diagnostics } = ts.transpileModule(
- `const __simProgram = async () => {\n${code}\n};`,
- {
- reportDiagnostics: true,
- compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext },
- }
- )
- const problems = (diagnostics ?? []).flatMap((diagnostic) => {
- if (!diagnostic.file || diagnostic.start === undefined) return []
- const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
- const { line } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start)
- return [`Line ${Math.max(1, line)}: ${message}`]
- })
- if (problems.length > 0) return { error: `The program does not parse.\n${problems.join('\n')}` }
+function compileProgram(code: string): { script: string } | { error: string } {
+ const stripped = stripTypes(`const __simProgram = async () => {\n${code}\n};`)
+ if ('errors' in stripped) {
+ const problems = stripped.errors.map(
+ ({ line, message }) => `Line ${Math.max(1, line - 1)}: ${message}`
+ )
+ return { error: `The program does not parse.\n${problems.join('\n')}` }
+ }
return {
- script: `${outputText}\ntry { __simCodeSettle(true, await __simProgram()); } catch (error) { __simCodeSettle(false, error); }`,
+ script: `${stripped.code}\ntry { __simCodeSettle(true, await __simProgram()); } catch (error) { __simCodeSettle(false, error); }`,
}
}
@@ -192,7 +184,7 @@ export async function runCodeMode(
codeSha256: sha256Hex(input.code),
})
- const compiled = await compileProgram(input.code)
+ const compiled = compileProgram(input.code)
if ('error' in compiled) {
summary('syntax_error')
return toolError(compiled.error)
diff --git a/apps/sim/lib/core/security/csp.ts b/apps/sim/lib/core/security/csp.ts
index 1ef0cfb87d9..4287e508faf 100644
--- a/apps/sim/lib/core/security/csp.ts
+++ b/apps/sim/lib/core/security/csp.ts
@@ -91,6 +91,8 @@ interface CSPDirectives {
const STATIC_SCRIPT_SRC = [
"'self'",
"'unsafe-inline'",
+ // Compiles WebAssembly and nothing else (no JS eval): the code editor's highlighter is Wasm.
+ "'wasm-unsafe-eval'",
...(isDev ? ["'unsafe-eval'"] : []),
'https://*.google.com',
'https://apis.google.com',
diff --git a/apps/sim/lib/execution/javascript-syntax.ts b/apps/sim/lib/execution/javascript-syntax.ts
new file mode 100644
index 00000000000..68be435a1ab
--- /dev/null
+++ b/apps/sim/lib/execution/javascript-syntax.ts
@@ -0,0 +1,27 @@
+import { type ParseResult, parse } from '@babel/parser'
+import type { File } from '@babel/types'
+
+interface UserJavaScriptParseOptions {
+ /** Include every token, comments among them, on `tokens`. */
+ tokens?: boolean
+}
+
+/**
+ * Parses JavaScript a user wrote, which runs as the body of an async function: top-level
+ * `return` and `await` are allowed, static imports may sit anywhere, and the source is
+ * sloppy-mode script, as the sandboxes evaluate it. Throws a `SyntaxError` carrying `loc` at
+ * the first error, recoverable or not: code that does not parse is never half-read.
+ */
+export function parseUserJavaScript(
+ code: string,
+ { tokens = false }: UserJavaScriptParseOptions = {}
+): ParseResult {
+ return parse(code, {
+ sourceType: 'script',
+ allowImportExportEverywhere: true,
+ allowReturnOutsideFunction: true,
+ allowAwaitOutsideFunction: true,
+ tokens,
+ plugins: ['jsx'],
+ })
+}
diff --git a/apps/sim/lib/execution/typescript.test.ts b/apps/sim/lib/execution/typescript.test.ts
new file mode 100644
index 00000000000..c2e4d837664
--- /dev/null
+++ b/apps/sim/lib/execution/typescript.test.ts
@@ -0,0 +1,44 @@
+/**
+ * @vitest-environment node
+ */
+import { runInNewContext } from 'node:vm'
+import { describe, expect, it } from 'vitest'
+import { stripTypes } from '@/lib/execution/typescript'
+
+function run(source: string): unknown {
+ const stripped = stripTypes(`(() => {\n${source}\n})()`)
+ if ('errors' in stripped) throw new Error(JSON.stringify(stripped.errors))
+ return runInNewContext(stripped.code)
+}
+
+describe('stripTypes', () => {
+ it('removes annotations, interfaces and assertions but keeps the values they wrap', () => {
+ expect(
+ run(`interface Row { id: string }
+const rows = [{ id: 'a' }] as const satisfies readonly Row[]
+const first: Row | undefined = rows[0]!
+return rows.map((row): string => row.id).concat(first ? [first.id] : [])`)
+ ).toEqual(['a', 'a'])
+ })
+
+ it('compiles the TypeScript constructs that have a runtime value', () => {
+ expect(run('enum Color { Red, Blue }\nreturn [Color.Blue, Color[0]]')).toEqual([1, 'Red'])
+ })
+
+ it('keeps every line where it was written, so runtime positions match the source', () => {
+ const stripped = stripTypes('type A = {\n a: number\n}\nconst value: A = { a: 1 }\nvalue.b()')
+ expect('code' in stripped && stripped.code.split('\n')[4]).toBe('value.b()')
+ })
+
+ it('reports a program that does not parse with the line of each error', () => {
+ expect(stripTypes('const a = 1\nconst b: = 2')).toEqual({
+ errors: [{ line: 2, message: expect.stringContaining('Unexpected token') }],
+ })
+ expect(stripTypes('let a = 1\nlet a = 2\nlet b = 1\nlet b = 2')).toEqual({
+ errors: [
+ { line: 2, message: "Identifier 'a' has already been declared." },
+ { line: 4, message: "Identifier 'b' has already been declared." },
+ ],
+ })
+ })
+})
diff --git a/apps/sim/lib/execution/typescript.ts b/apps/sim/lib/execution/typescript.ts
index c42aeca2924..03e5011703a 100644
--- a/apps/sim/lib/execution/typescript.ts
+++ b/apps/sim/lib/execution/typescript.ts
@@ -1,19 +1,48 @@
-type TypeScriptModule = typeof import('@typescript/typescript6')
+import { parse } from '@babel/parser'
+import { transform } from 'sucrase'
-let typescriptModule: Promise | null = null
+interface TypeScriptSyntaxError {
+ /** 1-based, in the source as given. */
+ line: number
+ message: string
+}
+
+type StripTypesResult = { code: string } | { errors: TypeScriptSyntaxError[] }
+
+interface LocatedError {
+ message: string
+ loc: { line: number }
+}
+
+function isLocatedError(value: unknown): value is LocatedError {
+ if (!(value instanceof Error)) return false
+ const loc: unknown = Reflect.get(value, 'loc')
+ return typeof loc === 'object' && loc !== null && typeof Reflect.get(loc, 'line') === 'number'
+}
+
+function toSyntaxError(error: LocatedError): TypeScriptSyntaxError {
+ return { line: error.loc.line, message: error.message.replace(/ \(\d+:\d+\)$/, '') }
+}
/**
- * The TypeScript compiler, imported on first use: it is large, and most
- * requests never parse or transpile user code. A failed import is dropped so
- * the next call retries it.
+ * TypeScript as JavaScript a sandbox can run: types are removed and nothing else is
+ * transformed, so every line keeps its number. The source is checked first, so a program
+ * that does not parse comes back as its errors and their lines instead of failing later at
+ * run time with positions that no longer match what was written.
*/
-export function loadTypeScript(): Promise {
- typescriptModule ??= import('@typescript/typescript6').then(
- (mod) => (mod?.default ?? mod) as TypeScriptModule,
- (error) => {
- typescriptModule = null
- throw error
- }
- )
- return typescriptModule
+export function stripTypes(source: string): StripTypesResult {
+ let errors: TypeScriptSyntaxError[]
+ try {
+ const ast = parse(source, {
+ sourceType: 'module',
+ errorRecovery: true,
+ plugins: ['typescript'],
+ })
+ errors = (ast.errors ?? []).filter(isLocatedError).map(toSyntaxError)
+ } catch (error) {
+ if (!isLocatedError(error)) throw error
+ errors = [toSyntaxError(error)]
+ }
+ if (errors.length > 0) return { errors }
+ return { code: transform(source, { transforms: ['typescript'], disableESTransforms: true }).code }
}
diff --git a/apps/sim/lib/function-execution/execute-request.test.ts b/apps/sim/lib/function-execution/execute-request.test.ts
index 62433c6e3e6..3ce4f43f4c7 100644
--- a/apps/sim/lib/function-execution/execute-request.test.ts
+++ b/apps/sim/lib/function-execution/execute-request.test.ts
@@ -823,6 +823,44 @@ describe('Function execution request', () => {
})
describe('Basic Function Execution', () => {
+ it('hoists every static import, with the comment above it, and keeps the body on its lines', async () => {
+ envFlagsMock.isRemoteSandboxEnabled = true
+
+ const response = await POST(
+ createMockRequest('POST', {
+ code: [
+ 'const label = "sep"',
+ '// joins paths',
+ 'import path from "node:path"',
+ 'return label + path.sep',
+ ].join('\n'),
+ language: 'javascript',
+ })
+ )
+
+ expect(response.status).toBe(200)
+ const code: string = mockExecuteInSandbox.mock.calls.at(-1)?.[0].code
+ expect(code).toContain('// joins paths\nimport path from "node:path"\n')
+ expect(code).toContain('const label = "sep"\n \n \n return label + path.sep')
+ })
+
+ it.each([
+ ['a require call', 'const path = require("node:path")\nreturn path.sep', 1],
+ ['an optional require call', 'const path = require?.("node:path")\nreturn path.sep', 1],
+ ['code that does not parse', 'import path from "node:path"\nreturn (', 0],
+ [
+ 'code whose only error a parser can recover from',
+ 'import path from "node:path"\nlet a = 1\nlet a = 2\nreturn path.sep',
+ 0,
+ ],
+ ])('sends %s to the remote sandbox only when it loads modules', async (_, code, calls) => {
+ envFlagsMock.isRemoteSandboxEnabled = true
+
+ await POST(createMockRequest('POST', { code, language: 'javascript' }))
+
+ expect(mockExecuteInSandbox).toHaveBeenCalledTimes(calls)
+ })
+
it('compacts large array result fields to manifests when execution context is durable', async () => {
// Compaction cost scales with element count, so a few wide rows cross the threshold cheaply.
const rowCount = 64
diff --git a/apps/sim/lib/function-execution/execute-request.ts b/apps/sim/lib/function-execution/execute-request.ts
index 9def9ddfc01..5f44a897b9c 100644
--- a/apps/sim/lib/function-execution/execute-request.ts
+++ b/apps/sim/lib/function-execution/execute-request.ts
@@ -1,3 +1,4 @@
+import { type Node, type Program, VISITOR_KEYS } from '@babel/types'
import type {
DelegatedPrincipal,
OrganizationDelegatedPrincipal,
@@ -35,6 +36,7 @@ import {
mergeDurableSecretProvenance,
} from '@/lib/execution/durable-secret-provenance'
import { executeInIsolatedVM, type IsolatedVMBrokerHandler } from '@/lib/execution/isolated-vm'
+import { parseUserJavaScript } from '@/lib/execution/javascript-syntax'
import { CodeLanguage, DEFAULT_CODE_LANGUAGE, isValidCodeLanguage } from '@/lib/execution/languages'
import {
inspectPrivateSecretProvenanceRequest,
@@ -100,7 +102,6 @@ import type {
SandboxSessionRequest,
} from '@/lib/execution/remote-sandbox/types'
import { isExecutionResourceLimitError } from '@/lib/execution/resource-errors'
-import { loadTypeScript } from '@/lib/execution/typescript'
import { MAX_FUNCTION_REFERENCES } from '@/lib/function-execution/limits'
import type { SandboxExportedFile } from '@/lib/function-execution/output'
import { planUserFileMounts, resolveUserFileMounts } from '@/lib/function-execution/sandbox-mounts'
@@ -386,90 +387,89 @@ const JS_RESERVED_WORDS = new Set([
'public',
])
-async function extractJavaScriptImports(code: string): Promise<{
+function isSyntaxNode(value: unknown): value is Node {
+ return (
+ typeof value === 'object' && value !== null && typeof Reflect.get(value, 'type') === 'string'
+ )
+}
+
+function forEachSyntaxNode(root: Node, visit: (node: Node) => void): void {
+ const pending: Node[] = [root]
+ for (let node = pending.pop(); node; node = pending.pop()) {
+ visit(node)
+ for (const key of VISITOR_KEYS[node.type] ?? []) {
+ const child: unknown = Reflect.get(node, key)
+ /* One push per child: spreading a 100,000-element array literal exceeds Bun's argument limit. */
+ if (Array.isArray(child)) {
+ for (const item of child) if (isSyntaxNode(item)) pending.push(item)
+ } else if (isSyntaxNode(child)) pending.push(child)
+ }
+ }
+}
+
+/**
+ * Splits a function body's static imports from the rest of its code. Each import is
+ * blanked to its newlines in the remaining code, so the lines after it keep their numbers.
+ * Code that does not parse comes back whole, with no imports, and fails where it runs.
+ */
+function extractJavaScriptImports(code: string): {
imports: string
remainingCode: string
hasRequireCalls: boolean
identifierNames: ReadonlySet
-}> {
+} {
+ let program: Program
try {
- const tsModule = await loadTypeScript()
-
- const sourceFile = tsModule.createSourceFile(
- 'user-code.js',
- code,
- tsModule.ScriptTarget.Latest,
- true,
- tsModule.ScriptKind.JS
- )
-
- const importSegments: Array<{ text: string; start: number; end: number }> = []
- const identifierNames = new Set()
- let hasRequireCalls = false
-
- const visit = (node: import('@typescript/typescript6').Node): void => {
- if (tsModule.isIdentifier(node)) identifierNames.add(node.text)
- if (
- tsModule.isCallExpression(node) &&
- tsModule.isIdentifier(node.expression) &&
- node.expression.text === 'require'
- ) {
- hasRequireCalls = true
- }
- tsModule.forEachChild(node, visit)
- }
- visit(sourceFile)
-
- sourceFile.statements.forEach((statement) => {
- if (
- tsModule.isImportDeclaration(statement) ||
- tsModule.isImportEqualsDeclaration(statement)
- ) {
- importSegments.push({
- text: statement.getFullText(sourceFile).trim(),
- start: statement.getFullStart(),
- end: statement.getEnd(),
- })
- }
- })
+ program = parseUserJavaScript(code).program
+ } catch {
+ return { imports: '', remainingCode: code, hasRequireCalls: false, identifierNames: new Set() }
+ }
- if (importSegments.length === 0) {
- return { imports: '', remainingCode: code, hasRequireCalls, identifierNames }
+ const identifierNames = new Set()
+ let hasRequireCalls = false
+ forEachSyntaxNode(program, (node) => {
+ if (node.type === 'Identifier') identifierNames.add(node.name)
+ if (
+ (node.type === 'CallExpression' || node.type === 'OptionalCallExpression') &&
+ node.callee.type === 'Identifier' &&
+ node.callee.name === 'require'
+ ) {
+ hasRequireCalls = true
}
+ })
- importSegments.sort((a, b) => a.start - b.start)
-
- const imports = importSegments.map((segment) => segment.text).join('\n')
-
- let cursor = 0
- const parts: string[] = []
- for (const segment of importSegments) {
- if (segment.start > cursor) {
- parts.push(code.slice(cursor, segment.start))
- }
-
- const removedSegment = code.slice(segment.start, segment.end)
- const newlinePlaceholder = removedSegment.replace(/[^\n]/g, '')
- parts.push(newlinePlaceholder)
-
- cursor = segment.end
+ /* An import's text runs from the end of whatever precedes it, so the comments above it travel with it. */
+ const importSegments: Array<{ text: string; start: number; end: number }> = []
+ let previousEnd = program.directives.at(-1)?.end ?? program.interpreter?.end ?? 0
+ for (const statement of program.body) {
+ if (statement.type === 'ImportDeclaration' && statement.end != null) {
+ importSegments.push({
+ text: code.slice(previousEnd, statement.end).trim(),
+ start: previousEnd,
+ end: statement.end,
+ })
}
+ previousEnd = statement.end ?? previousEnd
+ }
- if (cursor < code.length) {
- parts.push(code.slice(cursor))
- }
+ if (importSegments.length === 0) {
+ return { imports: '', remainingCode: code, hasRequireCalls, identifierNames }
+ }
- const remainingCode = parts.join('')
+ let cursor = 0
+ let remainingCode = ''
+ for (const segment of importSegments) {
+ remainingCode += code.slice(cursor, segment.start)
+ remainingCode += code.slice(segment.start, segment.end).replace(/[^\n]/g, '')
+ cursor = segment.end
+ }
+ remainingCode += code.slice(cursor)
- return { imports, remainingCode, hasRequireCalls, identifierNames }
- } catch {
- logger.error('Failed to extract JavaScript imports')
- return {
- imports: '',
- remainingCode: code,
- hasRequireCalls: false,
- identifierNames: new Set(),
- }
+ return {
+ imports: importSegments.map((segment) => segment.text).join('\n'),
+ remainingCode,
+ hasRequireCalls,
+ identifierNames,
}
}
@@ -2605,7 +2605,7 @@ export async function executeFunctionRequest(
let hasImports = false
if (lang === CodeLanguage.JavaScript) {
- const extractionResult = await extractJavaScriptImports(resolvedCode)
+ const extractionResult = extractJavaScriptImports(resolvedCode)
jsImports = extractionResult.imports
jsRemainingCode = extractionResult.remainingCode
jsIdentifierNames = extractionResult.identifierNames
diff --git a/apps/sim/lib/posthog/exception-filter.test.ts b/apps/sim/lib/posthog/exception-filter.test.ts
index 209b2c3c26f..151a9c8805f 100644
--- a/apps/sim/lib/posthog/exception-filter.test.ts
+++ b/apps/sim/lib/posthog/exception-filter.test.ts
@@ -45,7 +45,9 @@ describe('dropUnactionableExceptions', () => {
it('drops a cancellation whose name is the coerced type', () => {
expect(
- dropUnactionableExceptions(browserRaised({ type: 'Canceled', value: 'Canceled' }))
+ dropUnactionableExceptions(
+ browserRaised({ type: 'AbortError', value: 'The operation was aborted.' })
+ )
).toBeNull()
})
diff --git a/apps/sim/lib/posthog/exception-filter.ts b/apps/sim/lib/posthog/exception-filter.ts
index 4aaaf6a8a1d..06012c473b1 100644
--- a/apps/sim/lib/posthog/exception-filter.ts
+++ b/apps/sim/lib/posthog/exception-filter.ts
@@ -12,15 +12,11 @@ const URL_PROPERTIES = [
*
* `AbortError` is what a fetch rejects with once its `AbortSignal` fires —
* React Query aborts in-flight queries on unmount and on refetch, so this is
- * routine teardown. `Canceled` is Monaco's `CancellationError`
- * (`monaco-editor/esm/vs/base/common/errors.js` sets both `name` and `message`
- * to the bare string), raised whenever a language-service request is superseded
- * by a newer keystroke.
- *
- * Neither can be acted on: there is no defect to fix and no user impact, but
- * both fire often enough per session to bury real crashes in the issue list.
+ * routine teardown. It cannot be acted on: there is no defect to fix and no
+ * user impact, but it fires often enough per session to bury real crashes in
+ * the issue list.
*/
-const CANCELLATION_ERROR_NAMES = new Set(['AbortError', 'Canceled'])
+const CANCELLATION_ERROR_NAMES = new Set(['AbortError'])
/**
* Exception messages that carry no diagnosable content.
@@ -73,8 +69,8 @@ function isBrowserRaised(exceptions: CapturedException[]): boolean {
* Whether this is a cancellation, testing both shapes PostHog can produce.
*
* Which field carries the error's `name` depends on which coercer ran. A plain
- * `Error` keeps its `name` as `type`, so Monaco's `CancellationError` arrives as
- * type `Canceled`. A `DOMException` — what `fetch` rejects with when its signal
+ * `Error` keeps its `name` as `type`, so one named `AbortError` arrives as type
+ * `AbortError`. A `DOMException` — what `fetch` rejects with when its signal
* fires — always coerces to type `DOMException`, with the name folded into the
* front of the value as `"AbortError: signal is aborted without reason"`.
* Matching on `type` alone therefore misses every real aborted request.
diff --git a/apps/sim/lib/uploads/documents/compile.test.ts b/apps/sim/lib/uploads/documents/compile.test.ts
index 08bf263e77a..3ee08ad068f 100644
--- a/apps/sim/lib/uploads/documents/compile.test.ts
+++ b/apps/sim/lib/uploads/documents/compile.test.ts
@@ -92,6 +92,12 @@ describe('collectReferencedFileIds', () => {
expect(collectReferencedFileIds(src, 'javascript')).toEqual(new Set(['pdf-img-2']))
})
+ it('captures the id from a template literal with nothing substituted into it', () => {
+ expect(collectReferencedFileIds('await getFileBase64(`photo-1`)', 'javascript')).toEqual(
+ new Set(['photo-1'])
+ )
+ })
+
it('still supports the legacy /home/user/inputs/ path form', () => {
expect(
collectReferencedFileIds(`fs.readFileSync('/home/user/inputs/legacy-1')`, 'javascript')
diff --git a/apps/sim/lib/uploads/documents/references.ts b/apps/sim/lib/uploads/documents/references.ts
index 0ff76631d10..1c8e4063184 100644
--- a/apps/sim/lib/uploads/documents/references.ts
+++ b/apps/sim/lib/uploads/documents/references.ts
@@ -1,4 +1,4 @@
-import type ts from '@typescript/typescript6'
+import { parseUserJavaScript } from '@/lib/execution/javascript-syntax'
import { DocCompileUserError } from '@/lib/uploads/documents/compile-error'
export type DocumentSourceLanguage = 'javascript' | 'python'
@@ -23,59 +23,60 @@ const INPUT_PATH_PREFIX = '/home/user/inputs/'
*/
const REFERENCE_MARKERS = [...FILE_HELPERS, INPUT_PATH_PREFIX]
+interface LexedToken {
+ label: string
+ start: number
+ end: number
+}
+
+/** A token Babel lexed, as opposed to a comment, which shares its token list. */
+function lexedToken(value: unknown): LexedToken | undefined {
+ if (typeof value !== 'object' || value === null) return undefined
+ const type: unknown = Reflect.get(value, 'type')
+ const label: unknown =
+ typeof type === 'object' && type !== null ? Reflect.get(type, 'label') : undefined
+ const start: unknown = Reflect.get(value, 'start')
+ const end: unknown = Reflect.get(value, 'end')
+ if (typeof label !== 'string' || typeof start !== 'number' || typeof end !== 'number')
+ return undefined
+ return { label, start, end }
+}
+
function* javascriptTokens(source: string): Generator {
- // The compiler is large and most sources carry no reference, so it loads on first parse.
- const typescript = require('@typescript/typescript6') as typeof ts
- const fileName = 'document.js'
- const sourceFile = typescript.createSourceFile(
- fileName,
- source,
- typescript.ScriptTarget.Latest,
- true,
- typescript.ScriptKind.JS
- )
- const program = typescript.createProgram(
- [fileName],
- { allowJs: true, noLib: true, noResolve: true },
- {
- getSourceFile: (name) => (name === fileName ? sourceFile : undefined),
- getDefaultLibFileName: () => '',
- writeFile: () => {},
- getCurrentDirectory: () => '',
- getDirectories: () => [],
- fileExists: (name) => name === fileName,
- readFile: (name) => (name === fileName ? source : undefined),
- getCanonicalFileName: (name) => name,
- useCaseSensitiveFileNames: () => true,
- getNewLine: () => '\n',
- }
- )
- const diagnostic = program.getSyntacticDiagnostics(sourceFile)[0]
- if (diagnostic) {
- throw new DocCompileUserError(
- typescript.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
+ let lexed: LexedToken[]
+ try {
+ lexed = (parseUserJavaScript(source, { tokens: true }).tokens ?? []).flatMap(
+ (token: unknown) => lexedToken(token) ?? []
)
+ } catch (error) {
+ if (error instanceof SyntaxError) throw new DocCompileUserError(error.message)
+ throw error
}
-
- const pending: ts.Node[] = [sourceFile]
- while (pending.length > 0) {
- const node = pending.pop()
- if (!node) break
- if (typescript.isStringLiteralLike(node)) {
- const start = node.getStart(sourceFile) + 1
- yield { kind: 'string', value: source.slice(start, node.end - 1), start }
- continue
- }
- const children = node.getChildren(sourceFile)
- if (children.length > 0) {
- for (let index = children.length - 1; index >= 0; index--) pending.push(children[index])
- } else {
- const start = node.getStart(sourceFile)
+ for (let index = 0; index < lexed.length; index++) {
+ const token = lexed[index]
+ if (token.label === 'eof') continue
+ if (token.label === 'string') {
yield {
- kind: typescript.isIdentifier(node) ? 'word' : 'punctuation',
- value: source.slice(start, node.end),
- start,
+ kind: 'string',
+ value: source.slice(token.start + 1, token.end - 1),
+ start: token.start + 1,
}
+ continue
+ }
+ /*
+ * A template with nothing substituted into it is one string, as a quoted literal is: its
+ * backticks are not tokens of their own, so a call's `(` stays right before the string.
+ */
+ const chunk = lexed[index + 1]
+ if (token.label === '`' && chunk?.label === 'template' && lexed[index + 2]?.label === '`') {
+ yield { kind: 'string', value: source.slice(chunk.start, chunk.end), start: chunk.start }
+ index += 2
+ continue
+ }
+ yield {
+ kind: token.label === 'name' ? 'word' : 'punctuation',
+ value: source.slice(token.start, token.end),
+ start: token.start,
}
}
}
diff --git a/apps/sim/package.json b/apps/sim/package.json
index 1f25af6afa7..221bd7beab4 100644
--- a/apps/sim/package.json
+++ b/apps/sim/package.json
@@ -107,6 +107,8 @@
"@aws-sdk/s3-request-presigner": "3.1162.0",
"@azure/communication-email": "1.1.0",
"@azure/storage-blob": "12.34.0",
+ "@babel/parser": "7.29.9",
+ "@babel/types": "7.29.8",
"@better-auth/oauth-provider": "1.6.27",
"@better-auth/sso": "1.6.27",
"@better-auth/stripe": "1.6.27",
@@ -126,7 +128,6 @@
"@linear/sdk": "97.1.0",
"@marsidev/react-turnstile": "1.6.3",
"@modelcontextprotocol/sdk": "1.32.2",
- "@monaco-editor/react": "4.7.0",
"@napi-rs/canvas": "0.1.100",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/core": "2.12.0",
@@ -257,7 +258,6 @@
"marked": "18.0.11",
"mermaid": "11.16.1",
"micromatch": "4.0.8",
- "monaco-editor": "0.57.0",
"mongodb": "6.21.0",
"mssql": "12.7.13",
"mysql2": "3.24.6",
@@ -298,6 +298,7 @@
"ssh2": "^1.17.0",
"streamdown": "2.7.1",
"stripe": "18.5.0",
+ "sucrase": "3.35.1",
"tldts": "7.4.19",
"twilio": "6.2.0",
"typebox": "1.3.27",
diff --git a/apps/sim/scripts/test-workflow-code-ui-e2e.ts b/apps/sim/scripts/test-workflow-code-ui-e2e.ts
index 3c4950bb0ed..43e76fe791b 100644
--- a/apps/sim/scripts/test-workflow-code-ui-e2e.ts
+++ b/apps/sim/scripts/test-workflow-code-ui-e2e.ts
@@ -28,7 +28,7 @@ import { getWorkflowSourceContract, type WorkflowSource } from '@/lib/api/contra
* session and personal API key; the workflow is built through the v2 routes.
*
* - Flag off: the Code tab is absent, and a persisted `activeTab: 'code'` falls back to the toolbar.
- * - Flag on: Monaco shows the draft's source and ignores typing; disabling a block on the canvas
+ * - Flag on: the editor shows the draft's source and ignores typing; disabling a block on the canvas
* and applying source through `POST /api/v2/workflows/{id}/source` each refresh it; dragging
* a block does not.
* - A failed source read shows the error state.
@@ -223,7 +223,7 @@ function entryFile(source: WorkflowSource): string {
return text
}
-/** Monaco renders spaces as no-break spaces and wraps long lines; compare without whitespace. */
+/** The editor wraps long lines; compare without whitespace. */
const compact = (text: string) => text.replace(/\s+/g, '')
const isSourceRead = (url: string | URL) =>
@@ -249,19 +249,26 @@ async function openCodeTab(page: Page) {
}).toPass({ timeout: PAGE_LOAD_TIMEOUT_MS })
}
-/** The editor's lines in screen order: Monaco recycles line nodes, so DOM order can drift. */
+/** The editor's rendered lines in document order; Playwright's CSS locators pierce its shadow root. */
async function editorText(page: Page): Promise {
return codeTab(page)
- .locator('.monaco-editor .view-lines')
- .evaluate((lines) =>
- [...lines.querySelectorAll('.view-line')]
- .sort((a, b) => Number.parseFloat(a.style.top) - Number.parseFloat(b.style.top))
+ .locator('[data-code-editor] [data-content] [data-line]')
+ .evaluateAll((lines) =>
+ lines
+ .sort((a, b) => Number(a.getAttribute('data-line')) - Number(b.getAttribute('data-line')))
.map((line) => line.textContent ?? '')
.join('\n')
)
}
-/** Waits until the stored source differs from `previous`, then until Monaco shows exactly it. */
+/** Whether the editor's editable surface holds focus, inside the host's shadow root. */
+async function editorHasFocus(page: Page): Promise {
+ return page.evaluate(
+ () => document.activeElement?.shadowRoot?.activeElement?.hasAttribute('data-content') === true
+ )
+}
+
+/** Waits until the stored source differs from `previous`, then until the editor shows exactly it. */
async function expectEditorToShowNewSource(page: Page, previous: WorkflowSource) {
let current = previous
await expect
@@ -296,7 +303,10 @@ async function visitWorkflow(page: Page, origin: URL) {
/** A page in a fresh browser context signed in to `origin`. */
async function signedInPage(origin: URL): Promise {
assert(browser, 'The browser must be launched first')
- const context = await browser.newContext({ viewport: { width: 1440, height: 1000 } })
+ const context = await browser.newContext({
+ viewport: { width: 1440, height: 1000 },
+ permissions: ['clipboard-read', 'clipboard-write'],
+ })
await context.addCookies([
{ name: 'better-auth.session_token', value: cookie, url: origin.origin },
])
@@ -349,16 +359,18 @@ try {
async () => {
await visitWorkflow(page, baseUrl)
await openCodeTab(page)
- const editor = codeTab(page).locator('.monaco-editor')
+ const editor = codeTab(page).locator('[data-code-editor][data-read-only]')
await expect(editor).toBeVisible()
await expect.poll(async () => compact(await editorText(page))).toBe(compact(entryFile(shown)))
assert(entryFile(shown).includes(SCORE_CODE), 'The inline source carries the function code')
- await codeTab(page).locator('.monaco-editor .view-lines').click()
- await expect(editor).toHaveClass(/\bfocused\b/)
+ await codeTab(page).locator('[data-code-editor] [data-content] [data-line]').first().click()
+ await expect.poll(() => editorHasFocus(page)).toBe(true)
await page.keyboard.type('typed into a read-only view')
await page.keyboard.press('Enter')
await page.keyboard.press('Backspace')
- // Monaco paints edits on the next animation frame; two frames cover any queued render.
+ await page.evaluate(() => navigator.clipboard.writeText('pasted into a read-only view'))
+ await page.keyboard.press('ControlOrMeta+v')
+ // The editor paints edits on the next animation frame; two frames cover any queued render.
await page.evaluate(
() =>
new Promise((done) =>
@@ -436,7 +448,7 @@ try {
await toggleScoreBlock(page, 'Enable')
assert.equal((await refused).status(), 403, 'The flag-off app refuses the source read')
await expect(codeTab(page).getByTestId('workflow-code-error')).toBeVisible()
- await expect(codeTab(page).locator('.monaco-editor')).toHaveCount(0)
+ await expect(codeTab(page).locator('[data-code-editor]')).toHaveCount(0)
await page.unroute(isSourceRead)
}
)
@@ -448,7 +460,7 @@ try {
await visitWorkflow(failingPage, baseUrl)
await openCodeTab(failingPage)
await expect(codeTab(failingPage).getByTestId('workflow-code-error')).toBeVisible()
- await expect(codeTab(failingPage).locator('.monaco-editor')).toHaveCount(0)
+ await expect(codeTab(failingPage).locator('[data-code-editor]')).toHaveCount(0)
})
await failingPage.context().close()
} catch (error) {
diff --git a/apps/sim/types/monaco-typescript-definition.d.ts b/apps/sim/types/monaco-typescript-definition.d.ts
deleted file mode 100644
index ebdf4dea979..00000000000
--- a/apps/sim/types/monaco-typescript-definition.d.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-/**
- * Monaco's TypeScript Monarch grammar ships without a declaration file. Only the two exports the
- * Code tab reads are declared: it colors the printed source with this grammar under its own
- * language id, so no TypeScript worker or diagnostics start for a display-only model.
- */
-declare module 'monaco-editor/languages/definitions/typescript/typescript' {
- import type { languages } from 'monaco-editor'
-
- export const conf: languages.LanguageConfiguration
- export const language: languages.IMonarchLanguage
-}
diff --git a/bun.lock b/bun.lock
index bab6aae6bb7..0b9ed5900a3 100644
--- a/bun.lock
+++ b/bun.lock
@@ -251,6 +251,8 @@
"@aws-sdk/s3-request-presigner": "3.1162.0",
"@azure/communication-email": "1.1.0",
"@azure/storage-blob": "12.34.0",
+ "@babel/parser": "7.29.9",
+ "@babel/types": "7.29.8",
"@better-auth/oauth-provider": "1.6.27",
"@better-auth/sso": "1.6.27",
"@better-auth/stripe": "1.6.27",
@@ -270,7 +272,6 @@
"@linear/sdk": "97.1.0",
"@marsidev/react-turnstile": "1.6.3",
"@modelcontextprotocol/sdk": "1.32.2",
- "@monaco-editor/react": "4.7.0",
"@napi-rs/canvas": "0.1.100",
"@opentelemetry/api": "^1.9.0",
"@opentelemetry/core": "2.12.0",
@@ -402,7 +403,6 @@
"marked": "18.0.11",
"mermaid": "11.16.1",
"micromatch": "4.0.8",
- "monaco-editor": "0.57.0",
"mongodb": "6.21.0",
"mssql": "12.7.13",
"mysql2": "3.24.6",
@@ -443,6 +443,7 @@
"ssh2": "^1.17.0",
"streamdown": "2.7.1",
"stripe": "18.5.0",
+ "sucrase": "3.35.1",
"tldts": "7.4.19",
"twilio": "6.2.0",
"typebox": "1.3.27",
@@ -1638,10 +1639,6 @@
"@modelcontextprotocol/server": ["@modelcontextprotocol/server@2.4.0", "", { "dependencies": { "@modelcontextprotocol/core": "2.4.0", "zod": "^4.2.0" } }, "sha512-rgR9yxSaAJ+bcsi/XroT/S2AfPiIWloXAnrdfYSCms9ivVju0ishu/wW0J21Ho6+ZOR1r7RyeRbjiRuF87ix5Q=="],
- "@monaco-editor/loader": ["@monaco-editor/loader@1.7.0", "", { "dependencies": { "state-local": "^1.0.6" } }, "sha512-gIwR1HrJrrx+vfyOhYmCZ0/JcWqG5kbfG7+d3f/C1LXk2EvzAbHSg3MQ5lO2sMlo9izoAZ04shohfKLVT6crVA=="],
-
- "@monaco-editor/react": ["@monaco-editor/react@4.7.0", "", { "dependencies": { "@monaco-editor/loader": "^1.5.0" }, "peerDependencies": { "monaco-editor": ">= 0.25.0 < 1", "react": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0", "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0 || ^19.0.0" } }, "sha512-cyzXQCtO47ydzxpQtCGSQGOC8Gk3ZUeBXFAxD+CWXYFo5OqZyZUonFl0DwUlTyAfRHntBfw2p3w4s9R6oe1eCA=="],
-
"@mongodb-js/saslprep": ["@mongodb-js/saslprep@1.4.11", "", { "dependencies": { "sparse-bitfield": "^3.0.3" } }, "sha512-o9rAHc0IpIjuPSxRutWpE1F62x7n+4mVS4rCNHkzhIUMQcc18bb6xEq5wd2NdN0WjepIyXIppRshYI2kQDOZVA=="],
"@napi-rs/canvas": ["@napi-rs/canvas@0.1.100", "", { "optionalDependencies": { "@napi-rs/canvas-android-arm64": "0.1.100", "@napi-rs/canvas-darwin-arm64": "0.1.100", "@napi-rs/canvas-darwin-x64": "0.1.100", "@napi-rs/canvas-linux-arm-gnueabihf": "0.1.100", "@napi-rs/canvas-linux-arm64-gnu": "0.1.100", "@napi-rs/canvas-linux-arm64-musl": "0.1.100", "@napi-rs/canvas-linux-riscv64-gnu": "0.1.100", "@napi-rs/canvas-linux-x64-gnu": "0.1.100", "@napi-rs/canvas-linux-x64-musl": "0.1.100", "@napi-rs/canvas-win32-arm64-msvc": "0.1.100", "@napi-rs/canvas-win32-x64-msvc": "0.1.100" } }, "sha512-xglYA6q3XO5P3BNJYxVZ1IV7DLVjp1Py6nwag88YntrS+3vKHyYcMqXVS4ZztJmwz2uGvz1FWhI/4LgbR5uQDA=="],
@@ -2644,6 +2641,8 @@
"ansi-styles": ["ansi-styles@4.3.0", "", { "dependencies": { "color-convert": "^2.0.1" } }, "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg=="],
+ "any-promise": ["any-promise@1.3.0", "", {}, "sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A=="],
+
"anynum": ["anynum@1.0.1", "", {}, "sha512-N6//FLET/tXYNM/F6ABca1oH6fWB+KlTt909Le28WMDBk8oaT4vY17DCrwg2MvmuqUKt3Ni4N5dGJ/EoBgcO6A=="],
"apache-arrow": ["apache-arrow@21.2.0", "", { "dependencies": { "@types/node": "^25.2.0", "flatbuffers": "^25.1.24", "json-with-bigint": "^3.5.3", "tslib": "^2.6.2" }, "bin": { "arrow2csv": "bin/arrow2csv.js" } }, "sha512-Hxe6Agq26gQOM954qpzYSllJBPJl+e16U5CkfuMUhLrNba+5nKkttIVlflaovN6oaTratqMGAO8H5u/aNhmHWQ=="],
@@ -3650,6 +3649,8 @@
"linebreak": ["linebreak@1.1.0", "", { "dependencies": { "base64-js": "0.0.8", "unicode-trie": "^2.0.0" } }, "sha512-MHp03UImeVhB7XZtjd0E4n6+3xr5Dq/9xI/5FptGk5FrbDR3zagPa2DS6U8ks/3HjbKWG9Q1M2ufOzxV2qLYSQ=="],
+ "lines-and-columns": ["lines-and-columns@1.2.4", "", {}, "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg=="],
+
"linkifyjs": ["linkifyjs@4.3.3", "", {}, "sha512-P8aEP5U/D1/IlTY2OeYsErdwh9bGuLE30NcXtKEjgdHcahveQoQwM2yZNsioQHsWFz0P7KKudisbrzCgR0sDHg=="],
"lint-staged": ["lint-staged@16.4.0", "", { "dependencies": { "commander": "^14.0.3", "listr2": "^9.0.5", "picomatch": "^4.0.3", "string-argv": "^0.3.2", "tinyexec": "^1.0.4", "yaml": "^2.8.2" }, "bin": { "lint-staged": "bin/lint-staged.js" } }, "sha512-lBWt8hujh/Cjysw5GYVmZpFHXDCgZzhrOm8vbcUdobADZNOK/bRshr2kM3DfgrrtR1DQhfupW9gnIXOfiFi+bw=="],
@@ -3874,8 +3875,6 @@
"module-details-from-path": ["module-details-from-path@1.0.4", "", {}, "sha512-EGWKgxALGMgzvxYF1UyGTy0HXX/2vHLkw6+NvDKW2jypWbHpjQuj4UMcqQWXHERJhVGKikolT06G3bcKe4fi7w=="],
- "monaco-editor": ["monaco-editor@0.57.0", "", { "dependencies": { "dompurify": "3.4.15", "marked": "14.0.0" } }, "sha512-5BkI9KGoqrNvBGUe15/QlZq3OooZ8WLg1AxTpaqHRCP3HNpzPPZKE2EDz8M7c+VRmCeUw1Brp4cx/PWm3kI/5A=="],
-
"mongodb": ["mongodb@6.21.0", "", { "dependencies": { "@mongodb-js/saslprep": "^1.3.0", "bson": "^6.10.4", "mongodb-connection-string-url": "^3.0.2" }, "peerDependencies": { "@aws-sdk/credential-providers": "^3.188.0", "@mongodb-js/zstd": "^1.1.0 || ^2.0.0", "gcp-metadata": "^5.2.0", "kerberos": "^2.0.1", "mongodb-client-encryption": ">=6.0.0 <7", "snappy": "^7.3.2", "socks": "^2.7.1" }, "optionalPeers": ["@aws-sdk/credential-providers", "@mongodb-js/zstd", "gcp-metadata", "kerberos", "mongodb-client-encryption", "snappy", "socks"] }, "sha512-URyb/VXMjJ4da46OeSXg+puO39XH9DeQpWCslifrRn9JWugy0D+DvvBvkm2WxmHe61O/H19JM66p1z7RHVkZ6A=="],
"mongodb-connection-string-url": ["mongodb-connection-string-url@3.0.2", "", { "dependencies": { "@types/whatwg-url": "^11.0.2", "whatwg-url": "^14.1.0 || ^13.0.0" } }, "sha512-rMO7CGo/9BFwyZABcKAWL8UJwH/Kc2x0g72uhDWzG48URRax5TCIcJ7Rc3RZqffZzO/Gwff/jyKwCU9TN8gehA=="],
@@ -3890,6 +3889,8 @@
"mysql2": ["mysql2@3.24.6", "", { "dependencies": { "aws-ssl-profiles": "^1.1.2", "generate-function": "^2.3.1", "iconv-lite": "^0.7.3", "long": "^5.3.2", "lru.min": "^1.1.4", "named-placeholders": "^1.1.6", "sql-escaper": "^1.5.1" }, "peerDependencies": { "@types/node": ">= 8" } }, "sha512-enu73KhbAua2L3dIxjc6qg6ZJ7H3pt1BImZASWNWY5xSsmJ1ZuUPULTDGXgmJbI61vb+nJAG19IZclIq19UidQ=="],
+ "mz": ["mz@2.7.0", "", { "dependencies": { "any-promise": "^1.0.0", "object-assign": "^4.0.1", "thenify-all": "^1.0.0" } }, "sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q=="],
+
"named-placeholders": ["named-placeholders@1.1.6", "", { "dependencies": { "lru.min": "^1.1.0" } }, "sha512-Tz09sEL2EEuv5fFowm419c1+a/jSMiBjI9gHxVLrVdbUkkNUUfjsVYs9pVZu5oCon/kmRh9TfLEObFtkVxmY0w=="],
"nan": ["nan@2.27.0", "", {}, "sha512-hC+0LidcL3XE4rp1C4H54KujgXKzbfyTngZTwBByQxsOxCEKZT0MPQ4hOKUH2jU1OYstqdDH4onyHPDzcV0XdQ=="],
@@ -4070,6 +4071,8 @@
"pino-std-serializers": ["pino-std-serializers@7.1.0", "", {}, "sha512-BndPH67/JxGExRgiX1dX0w1FvZck5Wa4aal9198SrRhZjH3GxKQUKIBnYJTdj2HDN3UQAS06HlfcSbQj2OHmaw=="],
+ "pirates": ["pirates@4.0.7", "", {}, "sha512-TfySrs/5nm8fQJDcBDuUng3VOUKsd7S+zqvbOTiGXHfxX4wK31ard+hoNuvkicM/2YFzlpDgABOevKSsB4G/FA=="],
+
"pkce-challenge": ["pkce-challenge@5.0.1", "", {}, "sha512-wQ0b/W4Fr01qtpHlqSqspcj3EhBvimsdh0KlHhH8HRZnMsEa0ea2fTULOXOS9ccQr3om+GcGRk4e+isrZWV8qQ=="],
"pkg-types": ["pkg-types@1.3.1", "", { "dependencies": { "confbox": "^0.1.8", "mlly": "^1.7.4", "pathe": "^2.0.1" } }, "sha512-/Jm5M4RvtBFVkKWRu2BLUTNP8/M2a+UwuAX+ae4770q1qVGtfjG+WTCupoZixokjmHiry8uI+dlY8KXYV5HVVQ=="],
@@ -4448,8 +4451,6 @@
"stat-mode": ["stat-mode@1.0.0", "", {}, "sha512-jH9EhtKIjuXZ2cWxmXS8ZP80XyC3iasQxMDV8jzhNJpfDb7VbQLVW4Wvsxz9QZvzV+G4YoSfBUVKDOyxLzi/sg=="],
- "state-local": ["state-local@1.0.7", "", {}, "sha512-HTEHMNieakEnoe33shBYcZ7NX83ACUjCu8c40iOGEZsngj9zRnkqS9j1pqQPXwobB0ZcVTk27REb7COQ0UR59w=="],
-
"statuses": ["statuses@2.0.2", "", {}, "sha512-DvEy55V3DB7uknRo+4iOGT5fP1slR8wQohVdknigZPMpMstaKJQWhwiYBACJE3Ul2pTnATihhBYnRhZQHGBiRw=="],
"std-env": ["std-env@4.3.0", "", {}, "sha512-OtU/EgQ1kIm5KwqQpBC6ZEMXrZRui11w8zgfTWp8cdO9B8OaPsbA8bTHO2P+HNo1VlUTGMVBwPhydu6poeXiag=="],
@@ -4506,6 +4507,8 @@
"stylis": ["stylis@4.4.0", "", {}, "sha512-5Z9ZpRzfuH6l/UAvCPAPUo3665Nk2wLaZU3x+TLHKVzIz33+sbJqbtrYoC3KD4/uVOr2Zp+L0LySezP9OHV9yA=="],
+ "sucrase": ["sucrase@3.35.1", "", { "dependencies": { "@jridgewell/gen-mapping": "^0.3.2", "commander": "^4.0.0", "lines-and-columns": "^1.1.6", "mz": "^2.7.0", "pirates": "^4.0.1", "tinyglobby": "^0.2.11", "ts-interface-checker": "^0.1.9" }, "bin": { "sucrase": "bin/sucrase", "sucrase-node": "bin/sucrase-node" } }, "sha512-DhuTmvZWux4H1UOnWMB3sk0sbaCVOoQZjv8u1rDoTV0HTdGem9hkAZtl4JZy8P2z4Bg0nT+YMeOFyVr4zcG5Tw=="],
+
"sumchecker": ["sumchecker@3.0.1", "", { "dependencies": { "debug": "^4.1.0" } }, "sha512-MvjXzkz/BOfyVDkG0oFOtBxHX2u3gKbMHIF/dXblZsgD3BWOFLmHovIpZY7BykJdAjcqRCBi1WYBNdEC9yI7vg=="],
"supports-color": ["supports-color@10.2.2", "", {}, "sha512-SS+jx45GF1QjgEXQx4NJZV9ImqmO2NPz5FNsIHrsDjh2YsHnawpan7SNQ1o8NuhrbHZy9AZhIoCUiCeaW/C80g=="],
@@ -4542,6 +4545,10 @@
"text-decoder": ["text-decoder@1.2.7", "", { "dependencies": { "b4a": "^1.6.4" } }, "sha512-vlLytXkeP4xvEq2otHeJfSQIRyWxo/oZGEbXrtEEF9Hnmrdly59sUbzZ/QgyWuLYHctCHxFF4tRQZNQ9k60ExQ=="],
+ "thenify": ["thenify@3.3.1", "", { "dependencies": { "any-promise": "^1.0.0" } }, "sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw=="],
+
+ "thenify-all": ["thenify-all@1.6.0", "", { "dependencies": { "thenify": ">= 3.1.0 < 4" } }, "sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA=="],
+
"thread-stream": ["thread-stream@3.2.0", "", { "dependencies": { "real-require": "^0.2.0" } }, "sha512-zLBvqpwr4Esa0kRjcrzGU6zL25lePWaCLMx0RQFrmteozIfeNdaMLpG5U7PeHzvlFkAWaRKA9/KVW4F60iB+qw=="],
"tiny-async-pool": ["tiny-async-pool@1.3.0", "", { "dependencies": { "semver": "^5.5.0" } }, "sha512-01EAw5EDrcVrdgyCLgoSPvqznC0sVxDSVeiOz09FUpjh71G79VCqneOr+xvt7T1r76CF6ZZfPjHorN2+d+3mqA=="],
@@ -4590,6 +4597,8 @@
"ts-dedent": ["ts-dedent@2.3.0", "", {}, "sha512-JfJeIHke7y2egdGGgRAvpCwYFUsHlM2gPcrVOxFkznt/4uzQ7HFmvE63iFHVLBJNDuyDOQgijDK/tXH/f6Msjg=="],
+ "ts-interface-checker": ["ts-interface-checker@0.1.13", "", {}, "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA=="],
+
"tsconfck": ["tsconfck@3.1.3", "", { "peerDependencies": { "typescript": "^5.0.0" }, "optionalPeers": ["typescript"], "bin": { "tsconfck": "bin/tsconfck.js" } }, "sha512-ulNZP1SVpRDesxeMLON/LtWM8HIgAJEIVpVVhBM6gsmvQ8+Rh+ZG7FWGvHh7Ah3pRABwVJWklWCr/BTZSv0xnQ=="],
"tsconfig-paths": ["tsconfig-paths@4.2.0", "", { "dependencies": { "json5": "^2.2.2", "minimist": "^1.2.6", "strip-bom": "^3.0.0" } }, "sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg=="],
@@ -5480,6 +5489,8 @@
"style-to-js/style-to-object": ["style-to-object@1.0.14", "", { "dependencies": { "inline-style-parser": "0.2.7" } }, "sha512-LIN7rULI0jBscWQYaSswptyderlarFkjQ+t79nzty8tcIAceVomEVlLzH5VP4Cmsv6MtKhs7qaAiwlcp+Mgaxw=="],
+ "sucrase/commander": ["commander@4.1.1", "", {}, "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA=="],
+
"tedious/@types/node": ["@types/node@25.9.3", "", { "dependencies": { "undici-types": ">=7.24.0 <7.24.7" } }, "sha512-603BddQMv3pUcr4U2dhujk83N2tTDVr/34wII2B6bJy6g+8WD6yUb11jszNs0gdi4PesVWl7ABt8nYMVpnLUcg=="],
"tedious/iconv-lite": ["iconv-lite@0.7.2", "", { "dependencies": { "safer-buffer": ">= 2.1.2 < 3.0.0" } }, "sha512-im9DjEDQ55s9fL4EYzOAv0yMqmMBSZp6G0VvFyTMPKWxiSBHUj9NW/qqLmXUwXrrM7AvqSlTCfvqRb0cM8yYqw=="],
diff --git a/packages/emcn/src/components/code/syntax-theme.ts b/packages/emcn/src/components/code/syntax-theme.ts
index a5fad1cafda..15f3827e1f4 100644
--- a/packages/emcn/src/components/code/syntax-theme.ts
+++ b/packages/emcn/src/components/code/syntax-theme.ts
@@ -1,6 +1,7 @@
/**
- * Sim's syntax colors as a TextMate (Shiki) theme, for surfaces that highlight with Shiki
- * rather than with Prism through `Code`: docs code blocks and the app's diffs.
+ * Sim's syntax colors as a TextMate (Shiki) theme, and as a Zed-format theme, for surfaces
+ * that highlight with Shiki or Pierre rather than with Prism through `Code`: docs code
+ * blocks and the app's diffs and code editor.
*
* Prism tokenizes into its own classes, colored by `code.css`. Shiki assigns TextMate
* scopes, so parity is a scope-to-color mapping, derived by highlighting the same snippets
@@ -196,3 +197,62 @@ export function buildSyntaxTheme(
],
}
}
+
+/** Every role but the base foreground, which an unmapped token falls back to on its own. */
+type TokenRole = Exclude
+
+/**
+ * The same roles for tree-sitter token types, which Zed-format themes color. A type with no
+ * entry takes its parent's role (`keyword.control` → `keyword`) and an unlisted family takes
+ * the foreground, so this mirrors the TextMate mapping above rather than every token type.
+ */
+const TREE_SITTER_ROLES = new Map([
+ ['comment', 'comment'],
+ ['keyword', 'keyword'],
+ ['keyword.operator', 'punctuation'],
+ ['function', 'function'],
+ ['constructor', 'function'],
+ ['type', 'function'],
+ ['enum', 'function'],
+ ['property', 'variable'],
+ ['attribute', 'variable'],
+ ['number', 'constant'],
+ ['boolean', 'constant'],
+ ['constant', 'constant'],
+ ['tag', 'constant'],
+ ['variable.special', 'constant'],
+ ['string', 'string'],
+ ['string.regex', 'regex'],
+ ['punctuation', 'punctuation'],
+ ['operator', 'punctuation'],
+ ['diff.plus', 'string'],
+ ['diff.minus', 'deleted'],
+])
+
+/** A Zed-format theme; structurally what Pierre's Highlights backend accepts. */
+interface ZedSyntaxTheme {
+ name: string
+ appearance: 'light' | 'dark'
+ cssVariables: { prefix: string; defaults: Record }
+ style: { background: string; foreground: string; syntax: Record }
+}
+
+/**
+ * {@link CODE_TOKEN_COLORS} as a Zed-format theme for highlighters that color tree-sitter
+ * tokens instead of TextMate scopes. Its colors name `code.css` variables rather than hold
+ * values, so it follows light and dark exactly as the TextMate theme does.
+ */
+export function buildCodeTokenZedTheme(name: string, background: string): ZedSyntaxTheme {
+ return {
+ name,
+ appearance: 'light',
+ cssVariables: { prefix: '--code-', defaults: { background } },
+ style: {
+ background: 'background',
+ foreground: 'foreground',
+ syntax: Object.fromEntries(
+ [...TREE_SITTER_ROLES].map(([type, role]) => [type, `token-${role}`])
+ ),
+ },
+ }
+}