From e7fa51b3b20d3182507918f236dc4f52458e0cba Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 11:04:52 -0800 Subject: [PATCH 1/5] refactor(execution): parse user code with Babel, not the TypeScript 6 compiler TypeScript 7 ships no compiler API, so the server kept @typescript/typescript6 only to parse and transpile code users write. Three of its four runtime uses move to libraries built for the job: - MCP code mode strips types with sucrase after Babel checks the syntax, so a program that does not parse still reports every error with its own line. - The Function block's import scan and the document file-reference scanner parse with Babel through one shared parser for user JavaScript. A Function body that does not parse now runs where its syntax error is reported, instead of first starting a remote sandbox for the imports found in it. --- apps/sim/lib/api/mcp/code-mode.ts | 30 ++-- apps/sim/lib/execution/javascript-syntax.ts | 30 ++++ apps/sim/lib/execution/typescript.test.ts | 44 ++++++ apps/sim/lib/execution/typescript.ts | 57 +++++-- .../execute-request.test.ts | 32 ++++ .../lib/function-execution/execute-request.ts | 146 +++++++++--------- apps/sim/lib/uploads/documents/references.ts | 97 ++++++------ apps/sim/package.json | 3 + bun.lock | 21 +++ 9 files changed, 304 insertions(+), 156 deletions(-) create mode 100644 apps/sim/lib/execution/javascript-syntax.ts create mode 100644 apps/sim/lib/execution/typescript.test.ts diff --git a/apps/sim/lib/api/mcp/code-mode.ts b/apps/sim/lib/api/mcp/code-mode.ts index c11e17340f7..bec5b504307 100644 --- a/apps/sim/lib/api/mcp/code-mode.ts +++ b/apps/sim/lib/api/mcp/code-mode.ts @@ -19,7 +19,7 @@ import { } from '@/lib/api/mcp/dispatch' import { createConcurrencyLimit } from '@/lib/core/utils/concurrency' import { runSandboxTask, SandboxUserCodeError } from '@/lib/execution/sandbox/run-task' -import { loadTypeScript } from '@/lib/execution/typescript' +import { stripTypes } from '@/lib/execution/typescript' import { structuredToolResult, toolError } from '@/lib/mcp/tool-result' import { SIM_API_CODE_MAX_TIMEOUT_MS } from '@/sandboxes/sim-api-code' @@ -128,24 +128,16 @@ function envelopeFailure(status: number, text: string): SimApiAnswer { * value or error, is recorded for `finalize` rather than lost to the worker. * Syntax errors come back with lines numbered as the caller wrote them. */ -async function compileProgram(code: string): Promise<{ script: string } | { error: string }> { - 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/execution/javascript-syntax.ts b/apps/sim/lib/execution/javascript-syntax.ts new file mode 100644 index 00000000000..33135098129 --- /dev/null +++ b/apps/sim/lib/execution/javascript-syntax.ts @@ -0,0 +1,30 @@ +import { type ParseResult, parse } from '@babel/parser' +import type { File } from '@babel/types' + +interface UserJavaScriptParseOptions { + /** Keep parsing past errors that leave the tree intact, listing them on `errors`. */ + errorRecovery?: boolean + /** 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` + * when the source does not parse. + */ +export function parseUserJavaScript( + code: string, + { errorRecovery = false, tokens = false }: UserJavaScriptParseOptions = {} +): ParseResult { + return parse(code, { + sourceType: 'script', + allowImportExportEverywhere: true, + allowReturnOutsideFunction: true, + allowAwaitOutsideFunction: true, + errorRecovery, + 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..7a28d01bb66 100644 --- a/apps/sim/lib/function-execution/execute-request.test.ts +++ b/apps/sim/lib/function-execution/execute-request.test.ts @@ -823,6 +823,38 @@ 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], + ['code that does not parse', 'import path from "node:path"\nreturn (', 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..a718ae53f37 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,87 @@ 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) + if (Array.isArray(child)) pending.push(...child.filter(isSyntaxNode)) + 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, { errorRecovery: true }).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.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 +2603,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/uploads/documents/references.ts b/apps/sim/lib/uploads/documents/references.ts index 0ff76631d10..4096dd2d3da 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,58 @@ 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 (const [index, token] of lexed.entries()) { + 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 is a string only when nothing is substituted into it: `...` with one chunk. */ + if ( + token.label === 'template' && + lexed[index - 1]?.label === '`' && + lexed[index + 1]?.label === '`' + ) { + yield { kind: 'string', value: source.slice(token.start, token.end), start: token.start } + 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..8d6d21585a9 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", @@ -298,6 +300,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/bun.lock b/bun.lock index bab6aae6bb7..0ad5db8bf20 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", @@ -443,6 +445,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", @@ -2644,6 +2647,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 +3655,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=="], @@ -3890,6 +3897,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 +4079,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=="], @@ -4506,6 +4517,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 +4555,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 +4607,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 +5499,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=="], From 64a39536cdb5cc24574ed1a5d5a728fd0648b758 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 11:37:44 -0800 Subject: [PATCH 2/5] fix(execution): read backtick file references and never hoist imports from broken code - A template literal with nothing substituted into it is one string token again, so getFileBase64(`photo-1`) keeps its file reference. - The import scan parses strictly: a recoverable error is still an error, so imports are never hoisted out of code that will not run. --- apps/sim/lib/execution/javascript-syntax.ts | 9 +++------ .../function-execution/execute-request.test.ts | 5 +++++ .../lib/function-execution/execute-request.ts | 2 +- apps/sim/lib/uploads/documents/compile.test.ts | 6 ++++++ apps/sim/lib/uploads/documents/references.ts | 18 ++++++++++-------- 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/apps/sim/lib/execution/javascript-syntax.ts b/apps/sim/lib/execution/javascript-syntax.ts index 33135098129..68be435a1ab 100644 --- a/apps/sim/lib/execution/javascript-syntax.ts +++ b/apps/sim/lib/execution/javascript-syntax.ts @@ -2,8 +2,6 @@ import { type ParseResult, parse } from '@babel/parser' import type { File } from '@babel/types' interface UserJavaScriptParseOptions { - /** Keep parsing past errors that leave the tree intact, listing them on `errors`. */ - errorRecovery?: boolean /** Include every token, comments among them, on `tokens`. */ tokens?: boolean } @@ -11,19 +9,18 @@ interface UserJavaScriptParseOptions { /** * 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` - * when the source does not parse. + * 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, - { errorRecovery = false, tokens = false }: UserJavaScriptParseOptions = {} + { tokens = false }: UserJavaScriptParseOptions = {} ): ParseResult { return parse(code, { sourceType: 'script', allowImportExportEverywhere: true, allowReturnOutsideFunction: true, allowAwaitOutsideFunction: true, - errorRecovery, tokens, plugins: ['jsx'], }) diff --git a/apps/sim/lib/function-execution/execute-request.test.ts b/apps/sim/lib/function-execution/execute-request.test.ts index 7a28d01bb66..da47c7fc90a 100644 --- a/apps/sim/lib/function-execution/execute-request.test.ts +++ b/apps/sim/lib/function-execution/execute-request.test.ts @@ -847,6 +847,11 @@ describe('Function execution request', () => { it.each([ ['a 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 diff --git a/apps/sim/lib/function-execution/execute-request.ts b/apps/sim/lib/function-execution/execute-request.ts index a718ae53f37..72991dd6bd7 100644 --- a/apps/sim/lib/function-execution/execute-request.ts +++ b/apps/sim/lib/function-execution/execute-request.ts @@ -418,7 +418,7 @@ function extractJavaScriptImports(code: string): { } { let program: Program try { - program = parseUserJavaScript(code, { errorRecovery: true }).program + program = parseUserJavaScript(code).program } catch { return { imports: '', remainingCode: code, hasRequireCalls: false, identifierNames: new Set() } } 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 4096dd2d3da..1c8e4063184 100644 --- a/apps/sim/lib/uploads/documents/references.ts +++ b/apps/sim/lib/uploads/documents/references.ts @@ -52,7 +52,8 @@ function* javascriptTokens(source: string): Generator { if (error instanceof SyntaxError) throw new DocCompileUserError(error.message) throw error } - for (const [index, token] of lexed.entries()) { + for (let index = 0; index < lexed.length; index++) { + const token = lexed[index] if (token.label === 'eof') continue if (token.label === 'string') { yield { @@ -62,13 +63,14 @@ function* javascriptTokens(source: string): Generator { } continue } - /* A template is a string only when nothing is substituted into it: `...` with one chunk. */ - if ( - token.label === 'template' && - lexed[index - 1]?.label === '`' && - lexed[index + 1]?.label === '`' - ) { - yield { kind: 'string', value: source.slice(token.start, token.end), start: token.start } + /* + * 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 { From 3bae054a6116309afa4ebea7afcb79fe529981aa Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 12:08:36 -0800 Subject: [PATCH 3/5] feat(editor): replace Monaco with Pierre's editor in the file viewer and the Code tab One CodeEditor on @pierre/diffs (already shipped for diffs) replaces Monaco in both places: the editable file viewer and the read-only workflow Code tab. - Lighter and faster: opening a .ts file loads ~460 KB instead of ~1.25 MB plus up to 9 MB of language workers; first paint is 4x faster, the heap a third to a half of Monaco's up to 30k lines, and highlighting never blocks the main thread. - Highlighting runs on Pierre's WebAssembly highlighter, so the CSP allows 'wasm-unsafe-eval' (WebAssembly compilation only); Shiki is the fallback. - Read-only is a guard in front of a live editor, so search, selection and copy keep working; Tab leaves the editor, every edit is refused. - External updates (streaming, refresh, reload) keep the scroll position and never join the undo history; a growing document follows its end. - Chrome uses the app's tokens: a --surface-hover caret line, ::selection and find colors, a quiet bracket match, and one syntax palette for Shiki and Zed themes. - Removes the Monaco wrapper, its themes and workers, the Code tab's language registration and minimal-edit helper, and the Monaco-only PostHog filter. --- apps/sim/app/_shell/paste-admission-guard.tsx | 4 +- .../file-viewer/editor-context-menu.tsx | 9 +- .../components/file-viewer/monaco-editor.tsx | 260 ----------- .../components/file-viewer/text-editor.tsx | 369 ++++------------ .../file-viewer/use-selection-copy-bridge.ts | 20 +- .../panel/components/code/code.test.tsx | 8 +- .../components/panel/components/code/code.tsx | 56 +-- .../panel/components/code/source-language.ts | 28 -- .../panel/components/code/text-edit.test.ts | 59 --- .../panel/components/code/text-edit.ts | 35 -- .../components/code-editor/code-editor.tsx | 416 ++++++++++++++++++ .../components/code-editor/pierre-theme.ts | 64 +++ apps/sim/components/diff/sim-file-diff.tsx | 40 +- apps/sim/lib/core/security/csp.ts | 2 + apps/sim/lib/posthog/exception-filter.test.ts | 4 +- apps/sim/lib/posthog/exception-filter.ts | 16 +- apps/sim/package.json | 2 - apps/sim/scripts/test-workflow-code-ui-e2e.ts | 36 +- .../types/monaco-typescript-definition.d.ts | 11 - bun.lock | 10 - .../emcn/src/components/code/syntax-theme.ts | 64 ++- 21 files changed, 680 insertions(+), 833 deletions(-) delete mode 100644 apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor.tsx delete mode 100644 apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/source-language.ts delete mode 100644 apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.test.ts delete mode 100644 apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/code/text-edit.ts create mode 100644 apps/sim/components/code-editor/code-editor.tsx create mode 100644 apps/sim/components/code-editor/pierre-theme.ts delete mode 100644 apps/sim/types/monaco-typescript-definition.d.ts diff --git a/apps/sim/app/_shell/paste-admission-guard.tsx b/apps/sim/app/_shell/paste-admission-guard.tsx index 2555c1d698c..50ef25ac35f 100644 --- a/apps/sim/app/_shell/paste-admission-guard.tsx +++ b/apps/sim/app/_shell/paste-admission-guard.tsx @@ -6,7 +6,7 @@ import { assessTextPaste, formatPasteLimit, PASTE_LIMITS } from '@sim/utils/past import { readSelectionContextFromClipboard } from '@/lib/mothership/chat/selection-clipboard' const EDITABLE_TARGET_SELECTOR = - 'input:not([type="file"]):not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="hidden"]), textarea, [contenteditable]:not([contenteditable="false"]), .monaco-editor, .xterm' + 'input:not([type="file"]):not([type="checkbox"]):not([type="radio"]):not([type="button"]):not([type="submit"]):not([type="hidden"]), textarea, [contenteditable]:not([contenteditable="false"]), [data-code-editor], .xterm' function finitePositiveAttribute(element: Element | null, name: string): number | undefined { const raw = element?.getAttribute(name) @@ -29,7 +29,7 @@ function clipboardHasImageFile(data: DataTransfer | null): boolean { * crash-only fallback. This layer bounds only the clipboard payload, so a small paste into an already * large field keeps native behavior. Editors with a real result-size contract enforce it themselves. * Targets that explicitly handle clipboard images may claim those file events before the text guards. - * The capture listener runs before React, ProseMirror, Monaco, and xterm parse the clipboard value. + * The capture listener runs before React, ProseMirror, the code editor, and xterm parse the clipboard value. */ export function PasteAdmissionGuard() { const { toast: notify } = useToast() diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/editor-context-menu.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/editor-context-menu.tsx index 08205839da4..6ff4c182c76 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/editor-context-menu.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/editor-context-menu.tsx @@ -8,7 +8,7 @@ import { DropdownMenuShortcut, DropdownMenuTrigger, } from '@sim/emcn' -import { Blimp, Clipboard, Duplicate, Scissors, Search, SelectAll } from '@sim/emcn/icons' +import { Blimp, Clipboard, Duplicate, Scissors, SelectAll } from '@sim/emcn/icons' interface EditorContextMenuProps { isOpen: boolean @@ -21,7 +21,6 @@ interface EditorContextMenuProps { onCopyAll: () => void onPaste: () => void onSelectAll: () => void - onFind: () => void /** Adds the current selection to Chat as a reference. Omit to hide the item. */ onAddToChat?: () => void } @@ -37,7 +36,6 @@ export function EditorContextMenu({ onCopyAll, onPaste, onSelectAll, - onFind, onAddToChat, }: EditorContextMenuProps) { return ( @@ -103,11 +101,6 @@ export function EditorContextMenu({ Select all ⌘A - - - Find - ⌘F - ) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor.tsx deleted file mode 100644 index 369024d6304..00000000000 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor.tsx +++ /dev/null @@ -1,260 +0,0 @@ -'use client' - -import { useSyncExternalStore } from 'react' -import type { editor as MonacoEditorTypes } from 'monaco-editor' -import dynamic from 'next/dynamic' - -/** - * The Monaco editor as the workspace configures it: lazily loaded with its language workers and - * the Sim light and dark themes, plus the shared text-editor options. Used by the file viewer's - * text editor and the workflow Code tab. - */ - -export const TEXT_EDITOR_OPTIONS = { - largeFileOptimizations: true, - maxTokenizationLineLength: 20_000, - minimap: { enabled: false }, - scrollBeyondLastLine: false, - wordWrap: 'on', - fontSize: 13, - lineNumbers: 'on', - padding: { top: 24, bottom: 24 }, - fontFamily: - 'ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace', - tabSize: 2, - automaticLayout: true, - renderLineHighlight: 'line', - occurrencesHighlight: 'singleFile', - overviewRulerLanes: 0, - hideCursorInOverviewRuler: true, - scrollbar: { - verticalScrollbarSize: 6, - horizontalScrollbarSize: 6, - }, - quickSuggestions: false, - suggestOnTriggerCharacters: false, - wordBasedSuggestions: 'currentDocument', - parameterHints: { enabled: false }, - codeLens: false, - lightbulb: { - enabled: 'off' as MonacoEditorTypes.ShowLightbulbIconMode, - }, - inlayHints: { enabled: 'off' }, - contextmenu: false, - fixedOverflowWidgets: true, - glyphMargin: false, - stickyScroll: { enabled: false }, - bracketPairColorization: { enabled: false }, - unicodeHighlight: { ambiguousCharacters: false }, -} satisfies MonacoEditorTypes.IStandaloneEditorConstructionOptions - -const SIM_DARK_RULES: MonacoEditorTypes.ITokenThemeRule[] = [ - { token: 'comment', foreground: '606060', fontStyle: 'italic' }, - { token: 'string', foreground: '3ab872' }, - { token: 'string.escape', foreground: '3ab872' }, - { token: 'string.link', foreground: '33b4ff' }, - { token: 'number', foreground: 'e8a87c' }, - { token: 'number.float', foreground: 'e8a87c' }, - { token: 'number.hex', foreground: 'e8a87c' }, - { token: 'keyword', foreground: '33b4ff' }, - { token: 'keyword.control', foreground: '33b4ff' }, - { token: 'storage', foreground: '33b4ff' }, - { token: 'type', foreground: '8fc7f5' }, - { token: 'type.identifier', foreground: '8fc7f5' }, - { token: 'regexp', foreground: 'ff8a65' }, - { token: 'annotation', foreground: 'ffca28' }, - { token: 'delimiter', foreground: '555555' }, - { token: 'tag', foreground: '33b4ff' }, - { token: 'attribute.name', foreground: '8fc7f5' }, - { token: 'attribute.value', foreground: '3ab872' }, - { token: 'strong', foreground: 'e6e6e6', fontStyle: 'bold' }, - { token: 'emphasis', foreground: 'c8c8c8', fontStyle: 'italic' }, - { token: 'variable', foreground: '3ab872' }, - { token: 'variable.source', foreground: 'b0b0b0' }, - { token: 'meta.separator', foreground: '404040' }, -] - -const SIM_LIGHT_RULES: MonacoEditorTypes.ITokenThemeRule[] = [ - { token: 'comment', foreground: '888888', fontStyle: 'italic' }, - { token: 'string', foreground: '16825d' }, - { token: 'string.escape', foreground: '16825d' }, - { token: 'string.link', foreground: '0078d4' }, - { token: 'number', foreground: 'a85500' }, - { token: 'number.float', foreground: 'a85500' }, - { token: 'number.hex', foreground: 'a85500' }, - { token: 'keyword', foreground: '0078d4' }, - { token: 'keyword.control', foreground: '0078d4' }, - { token: 'storage', foreground: '0078d4' }, - { token: 'type', foreground: '7c4dcc' }, - { token: 'type.identifier', foreground: '7c4dcc' }, - { token: 'regexp', foreground: 'd7390c' }, - { token: 'annotation', foreground: 'e67700' }, - { token: 'delimiter', foreground: 'aaaaaa' }, - { token: 'tag', foreground: '0078d4' }, - { token: 'attribute.name', foreground: '7c4dcc' }, - { token: 'attribute.value', foreground: '16825d' }, - { token: 'strong', foreground: '1a1a1a', fontStyle: 'bold' }, - { token: 'emphasis', foreground: '444444', fontStyle: 'italic' }, - { token: 'variable', foreground: '16825d' }, - { token: 'variable.source', foreground: '555555' }, - { token: 'meta.separator', foreground: 'cccccc' }, -] - -export const MonacoEditor = dynamic( - async () => { - const [{ default: Editor, loader }, monaco] = await Promise.all([ - import('@monaco-editor/react'), - import('monaco-editor'), - ]) - - if (typeof window !== 'undefined' && !window.MonacoEnvironment) { - window.MonacoEnvironment = { - getWorker(_: string, label: string) { - if (label === 'json') { - return new Worker( - new URL('monaco-editor/languages/features/json/json.worker', import.meta.url) - ) - } - if (label === 'css' || label === 'scss' || label === 'less') { - return new Worker( - new URL('monaco-editor/languages/features/css/css.worker', import.meta.url) - ) - } - if (label === 'html' || label === 'handlebars' || label === 'razor') { - return new Worker( - new URL('monaco-editor/languages/features/html/html.worker', import.meta.url) - ) - } - if (label === 'typescript' || label === 'javascript') { - return new Worker( - new URL('monaco-editor/languages/features/typescript/ts.worker', import.meta.url) - ) - } - return new Worker(new URL('monaco-editor/editor/editor.worker', import.meta.url)) - }, - } - } - - loader.config({ monaco }) - - monaco.editor.defineTheme('sim-dark', { - base: 'vs-dark', - inherit: true, - rules: SIM_DARK_RULES, - colors: { - 'editor.background': '#1b1b1b', - 'editor.foreground': '#e6e6e6', - 'editorLineNumber.foreground': '#404040', - 'editorLineNumber.activeForeground': '#787878', - 'editor.selectionBackground': '#33b4ff28', - 'editor.inactiveSelectionBackground': '#33b4ff14', - 'editor.lineHighlightBackground': '#23232380', - 'editor.lineHighlightBorder': '#00000000', - 'editorGutter.background': '#1b1b1b', - 'editorWidget.background': '#242424', - 'editorWidget.border': '#333333', - 'editorWidget.foreground': '#e6e6e6', - 'editor.findMatchBackground': '#33b4ff40', - 'editor.findMatchHighlightBackground': '#33b4ff1a', - 'editor.findMatchBorder': '#33b4ff', - 'scrollbar.shadow': '#00000000', - 'scrollbarSlider.background': '#33333380', - 'scrollbarSlider.hoverBackground': '#45454580', - 'scrollbarSlider.activeBackground': '#505050', - 'editorBracketMatch.background': '#33b4ff1a', - 'editorBracketMatch.border': '#33b4ff80', - 'editorIndentGuide.background1': '#2a2a2a', - 'editorIndentGuide.activeBackground1': '#454545', - 'editorCursor.foreground': '#e6e6e6', - 'editor.wordHighlightBackground': '#33b4ff14', - 'editor.wordHighlightBorder': '#33b4ff40', - 'editorSuggestWidget.background': '#242424', - 'editorSuggestWidget.border': '#333333', - 'editorSuggestWidget.foreground': '#e6e6e6', - 'editorSuggestWidget.selectedBackground': '#292929', - 'editorSuggestWidget.selectedForeground': '#e6e6e6', - 'editorHoverWidget.background': '#242424', - 'editorHoverWidget.border': '#333333', - 'editorHoverWidget.foreground': '#e6e6e6', - 'minimap.background': '#1b1b1b', - 'minimapSlider.background': '#33333380', - focusBorder: '#33b4ff80', - 'input.background': '#242424', - 'input.border': '#333333', - 'input.foreground': '#e6e6e6', - 'inputOption.activeBorder': '#33b4ff', - }, - }) - - monaco.editor.defineTheme('sim-light', { - base: 'vs', - inherit: true, - rules: SIM_LIGHT_RULES, - colors: { - 'editor.background': '#fefefe', - 'editor.foreground': '#1a1a1a', - 'editorLineNumber.foreground': '#bbbbbb', - 'editorLineNumber.activeForeground': '#707070', - 'editor.selectionBackground': '#0078d430', - 'editor.inactiveSelectionBackground': '#0078d418', - 'editor.lineHighlightBackground': '#f7f7f7', - 'editor.lineHighlightBorder': '#00000000', - 'editorGutter.background': '#fefefe', - 'editorWidget.background': '#ffffff', - 'editorWidget.border': '#dedede', - 'editorWidget.foreground': '#1a1a1a', - 'editor.findMatchBackground': '#0078d428', - 'editor.findMatchHighlightBackground': '#0078d414', - 'editor.findMatchBorder': '#0078d4', - 'scrollbar.shadow': '#00000000', - 'scrollbarSlider.background': '#dedede80', - 'scrollbarSlider.hoverBackground': '#cccccc', - 'scrollbarSlider.activeBackground': '#b0b0b0', - 'editorBracketMatch.background': '#0078d418', - 'editorBracketMatch.border': '#0078d480', - 'editorIndentGuide.background1': '#f0f0f0', - 'editorIndentGuide.activeBackground1': '#d8d8d8', - 'editorCursor.foreground': '#1a1a1a', - 'editor.wordHighlightBackground': '#0078d414', - 'editor.wordHighlightBorder': '#0078d450', - 'editorSuggestWidget.background': '#ffffff', - 'editorSuggestWidget.border': '#dedede', - 'editorSuggestWidget.foreground': '#1a1a1a', - 'editorSuggestWidget.selectedBackground': '#f5f5f5', - 'editorSuggestWidget.selectedForeground': '#1a1a1a', - 'editorHoverWidget.background': '#ffffff', - 'editorHoverWidget.border': '#dedede', - 'editorHoverWidget.foreground': '#1a1a1a', - 'minimap.background': '#fefefe', - 'minimapSlider.background': '#dedede80', - focusBorder: '#0078d480', - 'input.background': '#ffffff', - 'input.border': '#dedede', - 'input.foreground': '#1a1a1a', - 'inputOption.activeBorder': '#0078d4', - }, - }) - - return Editor - }, - { ssr: false } -) - -function subscribeToThemeClass(onStoreChange: () => void) { - const observer = new MutationObserver(onStoreChange) - observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] }) - return () => observer.disconnect() -} - -const getIsDarkSnapshot = () => document.documentElement.classList.contains('dark') -const getServerIsDarkSnapshot = () => false - -/** `sim-dark` or `sim-light`, following the document theme class. */ -export function useMonacoTheme(): string { - const isDark = useSyncExternalStore( - subscribeToThemeClass, - getIsDarkSnapshot, - getServerIsDarkSnapshot - ) - return isDark ? 'sim-dark' : 'sim-light' -} diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx index 938effc04f7..cc4e119ab0c 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx @@ -4,19 +4,20 @@ import { memo, type ClipboardEvent as ReactClipboardEvent, type KeyboardEvent as ReactKeyboardEvent, + type MouseEvent as ReactMouseEvent, type ReactNode, useCallback, useEffect, useId, useImperativeHandle, useLayoutEffect, - useMemo, useRef, useState, } from 'react' -import type { OnMount } from '@monaco-editor/react' +import { getFiletypeFromFileName, type SupportedLanguages } from '@pierre/diffs' import { cn, toast } from '@sim/emcn' import { formatPasteLimit, PASTE_LIMITS } from '@sim/utils/paste' +import { CodeEditor, type CodeEditorHandle } from '@/components/code-editor/code-editor' import { readSeparatorKey } from '@/lib/core/utils/separator-keys' import { buildFileSelectionLabel, @@ -24,17 +25,11 @@ import { } from '@/lib/mothership/chat/selection-context' import type { FileDownloadSource } from '@/lib/uploads/client/download' import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace' -import { getFileExtension } from '@/lib/uploads/utils/file' import { isSimPageSource } from '@/lib/workspace-files/page-compile' import { SIM_PAGE_CONTENT_TYPE } from '@/lib/workspace-files/page-content-type' import { EditorContextMenu } from '@/app/workspace/[workspaceId]/files/components/file-viewer/editor-context-menu' import { FileSaveConflict } from '@/app/workspace/[workspaceId]/files/components/file-viewer/file-save-conflict' import type { PreviewMode } from '@/app/workspace/[workspaceId]/files/components/file-viewer/file-viewer' -import { - MonacoEditor, - TEXT_EDITOR_OPTIONS, - useMonacoTheme, -} from '@/app/workspace/[workspaceId]/files/components/file-viewer/monaco-editor' import { PreviewPanel, resolvePreviewType, @@ -53,90 +48,18 @@ const SPLIT_DEFAULT_PCT = 50 const SPLIT_KEYBOARD_STEP_PCT = 5 /** - * Monaco's find-widget button hover tooltips render above the button by default. Because the find - * widget sits at the very top of the editor, the tooltip overlaps the button itself — - * `document.elementFromPoint` at the button's center returns `.hover-contents` instead of the - * button, which fires `mouseleave` on the button. Monaco then disposes the tooltip, the cursor - * lands back on the button, and the cycle repeats every ~210ms, making the buttons unclickable. - * - * Translate the tooltip's `.context-view` below the button so the cursor stays on the button - * while the tooltip is shown, and flip the carat to point up at the button. + * Syntax for a file whose name does not give it away, by MIME type. A name with a known + * extension wins; the editor infers that itself. */ -const FIND_TOOLTIP_FIX_CSS = ` -[data-find-tooltip-fix] .context-view.top.left { - transform: translateY(56px); -} -[data-find-tooltip-fix] .context-view.top.left .workbench-hover-pointer.bottom { - top: -3px; - bottom: auto; - transform: rotate(225deg); -} -` - -/** Maps file extensions to Monaco editor language IDs. */ -const MONACO_LANGUAGE_BY_EXTENSION: Partial> = { - js: 'javascript', - jsx: 'javascript', - ts: 'typescript', - tsx: 'typescript', - py: 'python', - rb: 'ruby', - go: 'go', - rs: 'rust', - java: 'java', - kt: 'kotlin', - swift: 'swift', - c: 'c', - cpp: 'cpp', - h: 'cpp', - hpp: 'cpp', - cs: 'csharp', - php: 'php', - sh: 'shell', - bash: 'shell', - zsh: 'shell', - fish: 'shell', - sql: 'sql', - graphql: 'graphql', - gql: 'graphql', - json: 'json', - /** - * Not `json`: JSON Lines holds one value per line, which Monaco's - * single-document parser flags as invalid. Validation is global - * (`jsonDefaults`), so opting JSONL out is the only per-file lever. - */ - jsonl: 'plaintext', - yaml: 'yaml', - yml: 'yaml', - toml: 'toml', - html: 'html', - htm: 'html', - xml: 'xml', - svg: 'xml', - css: 'css', - scss: 'scss', - less: 'less', - md: 'markdown', - mdx: 'markdown', - mmd: 'markdown', - dockerfile: 'dockerfile', - ini: 'ini', - conf: 'ini', - cfg: 'ini', - env: 'shell', - diff: 'diff', - patch: 'diff', -} - -const MONACO_LANGUAGE_BY_MIME: Partial> = { +const LANGUAGE_BY_MIME: Partial> = { 'text/javascript': 'javascript', 'application/javascript': 'javascript', 'text/typescript': 'typescript', 'application/typescript': 'typescript', 'text/x-python': 'python', 'application/json': 'json', - 'text/x-shellscript': 'shell', - 'text/x-sh': 'shell', + 'text/x-shellscript': 'bash', + 'text/x-sh': 'bash', 'text/css': 'css', 'text/html': 'html', 'text/xml': 'xml', @@ -146,12 +69,14 @@ const MONACO_LANGUAGE_BY_MIME: Partial> = { 'application/x-yaml': 'yaml', 'text/markdown': 'markdown', 'text/x-mermaid': 'markdown', - 'text/plain': 'plaintext', } -function resolveMonacoLanguage(file: { type: string; name: string }): string { - const ext = getFileExtension(file.name) - return MONACO_LANGUAGE_BY_EXTENSION[ext] ?? MONACO_LANGUAGE_BY_MIME[file.type] ?? 'plaintext' +function languageOverride(file: { type: string; name: string }): SupportedLanguages | undefined { + return getFiletypeFromFileName(file.name) === 'text' ? LANGUAGE_BY_MIME[file.type] : undefined +} + +function isSaveShortcut(event: ReactKeyboardEvent): boolean { + return (event.metaKey || event.ctrlKey) && !event.altKey && event.key.toLowerCase() === 's' } interface TextEditorProps { @@ -197,14 +122,9 @@ export const TextEditor = memo(function TextEditor({ previewContextKey, }: TextEditorProps) { const containerRef = useRef(null) - const monacoEditorRef = useRef[0] | null>(null) - const lastEditorValueRef = useRef('') - const hasAutoFocusedRef = useRef(false) + const editorRef = useRef(null) const contentRef = useRef('') - const textareaStuckRef = useRef(false) - const suppressScrollListenerRef = useRef(false) - const [mountedEditor, setMountedEditor] = useState[0] | null>(null) const sourcePaneId = useId() const [splitPct, setSplitPct] = useState(SPLIT_DEFAULT_PCT) const [isResizing, setIsResizing] = useState(false) @@ -214,25 +134,12 @@ export const TextEditor = memo(function TextEditor({ hasSelection: boolean } | null>(null) - const monacoLanguage = resolveMonacoLanguage(file) - const monacoTheme = useMonacoTheme() const addToChat = useAddToChat() const buildSelectionContext = useCallback((): ChatContext | null => { - const editor = monacoEditorRef.current - const sel = editor?.getSelection() - const model = editor?.getModel() - if (!editor || !sel || sel.isEmpty() || !model) return null - const text = model.getValueInRange(sel) - if (!text.trim()) return null - const startLine = sel.startLineNumber - // A full-line highlight ends at column 1 of the FOLLOWING line, so that line - // contributed no text — reporting it would claim a range one line longer - // than what was selected, in both the chip label and the agent's prompt. - const endLine = - sel.endColumn === 1 && sel.endLineNumber > startLine - ? sel.endLineNumber - 1 - : sel.endLineNumber + const selection = editorRef.current?.getSelection() + if (!selection?.text.trim()) return null + const { text, startLine, endLine } = selection return { kind: 'file_selection', fileId: file.id, @@ -290,83 +197,10 @@ export const TextEditor = memo(function TextEditor({ [file.id, workspaceId, isContentLoading, hasContentError] ) - // Enable once content has loaded — the container (and Monaco) only mount after + // Enable once content has loaded — the container (and the editor) only mount after // the `isContentLoading` early return below, so the bridge must (re-)attach then. useSelectionCopyBridge(containerRef, buildSelectionContext, workspaceId, !isContentLoading) - useEffect(() => { - if (lastEditorValueRef.current === content) return - - const editor = monacoEditorRef.current - if (!editor) return - const model = editor.getModel() - if (!model) return - const monacoValue = model.getValue() - lastEditorValueRef.current = monacoValue - if (monacoValue === content) return - if (isStreamInteractionLocked) { - const scrollTop = editor.getScrollTop() - const scrollHeight = editor.getScrollHeight() - const { height } = editor.getLayoutInfo() - if (scrollHeight - scrollTop - height < 80) { - textareaStuckRef.current = true - } - } - suppressScrollListenerRef.current = true - if (content.startsWith(monacoValue) && monacoValue.length < content.length) { - const lastLine = model.getLineCount() - const lastCol = model.getLineMaxColumn(lastLine) - model.applyEdits([ - { - range: { - startLineNumber: lastLine, - startColumn: lastCol, - endLineNumber: lastLine, - endColumn: lastCol, - }, - text: content.slice(monacoValue.length), - }, - ]) - } else { - model.applyEdits([{ range: model.getFullModelRange(), text: content }]) - } - suppressScrollListenerRef.current = false - lastEditorValueRef.current = content - }, [content, isStreamInteractionLocked]) - - useEffect(() => { - const editor = mountedEditor - if (!editor || !isStreamInteractionLocked || disableStreamingAutoScroll) { - textareaStuckRef.current = false - return - } - - const disposable = editor.onDidScrollChange(() => { - if (suppressScrollListenerRef.current) return - const scrollTop = editor.getScrollTop() - const scrollHeight = editor.getScrollHeight() - const { height } = editor.getLayoutInfo() - if (scrollHeight - scrollTop - height >= 80) { - textareaStuckRef.current = false - } - }) - - return () => { - disposable.dispose() - } - }, [mountedEditor, isStreamInteractionLocked, disableStreamingAutoScroll]) - - useEffect(() => { - if (!isStreamInteractionLocked || !textareaStuckRef.current || disableStreamingAutoScroll) - return - const editor = monacoEditorRef.current - if (!editor) return - const lineCount = editor.getModel()?.getLineCount() ?? 0 - if (lineCount > 0) { - editor.revealLine(lineCount) - } - }, [content, isStreamInteractionLocked, disableStreamingAutoScroll]) - useEffect(() => { if (!isResizing) return @@ -412,93 +246,54 @@ export const TextEditor = memo(function TextEditor({ setSplitPct((current) => Math.min(SPLIT_MAX_PCT, Math.max(SPLIT_MIN_PCT, current + delta))) } - const handleEditorMount: OnMount = (editor, monaco) => { - monacoEditorRef.current = editor - setMountedEditor(editor) - - editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => { - saveImmediately() - }) - - const model = editor.getModel() - const currentContent = contentRef.current - if (model) { - if (model.getValue() !== currentContent) { - model.setValue(currentContent) - } - lastEditorValueRef.current = currentContent - } - - if (autoFocus && !hasAutoFocusedRef.current) { - hasAutoFocusedRef.current = true - editor.focus() - } - - const contextMenuDisposable = editor.onContextMenu((e) => { - e.event.preventDefault() - const sel = editor.getSelection() - setContextMenu({ - x: e.event.posx, - y: e.event.posy, - hasSelection: sel !== null && !sel.isEmpty(), - }) - }) - editor.onDidDispose(() => contextMenuDisposable.dispose()) - } - const handleEditorChange = useCallback( - (value: string | undefined) => { - const nextValue = value ?? '' - lastEditorValueRef.current = nextValue - contentRef.current = nextValue - setDraftContent(nextValue) + (value: string) => { + contentRef.current = value + setDraftContent(value) }, [setDraftContent] ) - const handleEditorPasteCapture = (event: ReactClipboardEvent) => { - const pastedText = event.clipboardData.getData('text/plain') - if (!pastedText) return + const handleSourceKeyDownCapture = (event: ReactKeyboardEvent) => { + if (!isSaveShortcut(event)) return + event.preventDefault() + saveImmediately() + } - const editor = monacoEditorRef.current - const model = editor?.getModel() - const selections = editor?.getSelections() - const currentText = contentRef.current - const selectionOffsets = - model && selections?.length - ? selections.map((selection) => ({ - start: model.getOffsetAt({ - lineNumber: selection.startLineNumber, - column: selection.startColumn, - }), - end: model.getOffsetAt({ - lineNumber: selection.endLineNumber, - column: selection.endColumn, - }), - })) - : [{ start: currentText.length, end: currentText.length }] + const handleSourceContextMenu = (event: ReactMouseEvent) => { + event.preventDefault() + setContextMenu({ + x: event.clientX, + y: event.clientY, + hasSelection: editorRef.current?.getSelection() != null, + }) + } + /** Whether pasting `pastedText` over the selections keeps the file within the editable size; warns when not. */ + const admitsPaste = (pastedText: string): boolean => { + /* The editor pastes the whole text into every selection, never spreading it across them. */ const admission = assessTextEditorPaste({ pastedText, - currentText, - selections: selectionOffsets, - multiCursorPaste: editor?.getRawOptions().multiCursorPaste ?? 'spread', + currentText: contentRef.current, + selections: editorRef.current?.getSelectionRanges() ?? [], + multiCursorPaste: 'full', }) - if (admission.accepted) return - - event.preventDefault() - event.stopPropagation() + if (admission.accepted) return true toast.warning('Paste would make this file too large to edit', { description: `Inline editing supports files up to ${formatPasteLimit(PASTE_LIMITS.TEXT_EDITOR_BYTES)}. Import or replace the file to keep larger content available without loading it into the editor.`, }) + return false + } + + const handleEditorPasteCapture = (event: ReactClipboardEvent) => { + const pastedText = event.clipboardData.getData('text/plain') + if (!pastedText || admitsPaste(pastedText)) return + event.preventDefault() + event.stopPropagation() } const isStreaming = isStreamInteractionLocked const isEditorReadOnly = isStreamInteractionLocked || !canEdit - const editorOptions = useMemo( - () => ({ ...TEXT_EDITOR_OPTIONS, readOnly: isEditorReadOnly }), - [isEditorReadOnly] - ) const previewType = resolvePreviewType(file.type, file.name) const isIframeRendered = previewType === 'html' || previewType === 'svg' @@ -557,19 +352,19 @@ export const TextEditor = memo(function TextEditor({ )}
- {showEditor && (
-
)} @@ -646,45 +442,36 @@ export const TextEditor = memo(function TextEditor({ closeContextMenu() }} onCut={() => { - monacoEditorRef.current?.focus() - monacoEditorRef.current?.trigger( - 'contextmenu', - 'editor.action.clipboardCutAction', - null - ) + const selection = editorRef.current?.getSelection() + if (selection) { + navigator.clipboard.writeText(selection.text).catch(() => {}) + editorRef.current?.replaceSelection('') + } closeContextMenu() }} onCopy={() => { - monacoEditorRef.current?.focus() - monacoEditorRef.current?.trigger( - 'contextmenu', - 'editor.action.clipboardCopyAction', - null - ) + const selection = editorRef.current?.getSelection() + if (selection) navigator.clipboard.writeText(selection.text).catch(() => {}) closeContextMenu() }} onCopyAll={() => { - navigator.clipboard - .writeText(monacoEditorRef.current?.getValue() ?? '') - .catch(() => {}) + navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {}) closeContextMenu() }} 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 ( { 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/package.json b/apps/sim/package.json index 8d6d21585a9..221bd7beab4 100644 --- a/apps/sim/package.json +++ b/apps/sim/package.json @@ -128,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", @@ -259,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", diff --git a/apps/sim/scripts/test-workflow-code-ui-e2e.ts b/apps/sim/scripts/test-workflow-code-ui-e2e.ts index 3c4950bb0ed..c3354c86d73 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 @@ -349,16 +356,17 @@ 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.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 +444,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 +456,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 0ad5db8bf20..0b9ed5900a3 100644 --- a/bun.lock +++ b/bun.lock @@ -272,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", @@ -404,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", @@ -1641,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=="], @@ -3881,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=="], @@ -4459,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=="], 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}`]) + ), + }, + } +} From 4d8b69c5e7e6f048002ff2ebadf8aff7b5c3dfdd Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 12:24:41 -0800 Subject: [PATCH 4/5] fix(editor): cut and copy every selection, and keep the import scan within engine limits - The context menu's Cut and Copy take every selected passage, and Cut deletes only after the clipboard write succeeds, so nothing is removed that never reached it. - The import scan sees require?.() calls and walks array children one push at a time, so a 100,000-element literal stays under Bun's argument limit. - The Code tab e2e seeds the clipboard before pasting into the read-only view. --- .../components/file-viewer/text-editor.tsx | 28 ++++++++++++++----- .../execute-request.test.ts | 1 + .../lib/function-execution/execute-request.ts | 8 ++++-- apps/sim/scripts/test-workflow-code-ui-e2e.ts | 6 +++- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx index cc4e119ab0c..c1f7463f348 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx @@ -285,6 +285,18 @@ export const TextEditor = memo(function TextEditor({ return false } + /** Every selected passage, joined by line breaks as a multi-cursor copy is. */ + const selectedText = (): string => { + const editor = editorRef.current + if (!editor) return '' + const text = editor.getText() + return editor + .getSelectionRanges() + .filter(({ start, end }) => start < end) + .map(({ start, end }) => text.slice(start, end)) + .join('\n') + } + const handleEditorPasteCapture = (event: ReactClipboardEvent) => { const pastedText = event.clipboardData.getData('text/plain') if (!pastedText || admitsPaste(pastedText)) return @@ -442,17 +454,19 @@ export const TextEditor = memo(function TextEditor({ closeContextMenu() }} onCut={() => { - const selection = editorRef.current?.getSelection() - if (selection) { - navigator.clipboard.writeText(selection.text).catch(() => {}) - editorRef.current?.replaceSelection('') - } closeContextMenu() + const text = selectedText() + if (!text) return + /* Delete only once the text is safely on the clipboard. */ + navigator.clipboard + .writeText(text) + .then(() => editorRef.current?.replaceSelection('')) + .catch(() => {}) }} onCopy={() => { - const selection = editorRef.current?.getSelection() - if (selection) navigator.clipboard.writeText(selection.text).catch(() => {}) closeContextMenu() + const text = selectedText() + if (text) navigator.clipboard.writeText(text).catch(() => {}) }} onCopyAll={() => { navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {}) diff --git a/apps/sim/lib/function-execution/execute-request.test.ts b/apps/sim/lib/function-execution/execute-request.test.ts index da47c7fc90a..3ce4f43f4c7 100644 --- a/apps/sim/lib/function-execution/execute-request.test.ts +++ b/apps/sim/lib/function-execution/execute-request.test.ts @@ -846,6 +846,7 @@ describe('Function execution request', () => { 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', diff --git a/apps/sim/lib/function-execution/execute-request.ts b/apps/sim/lib/function-execution/execute-request.ts index 72991dd6bd7..5f44a897b9c 100644 --- a/apps/sim/lib/function-execution/execute-request.ts +++ b/apps/sim/lib/function-execution/execute-request.ts @@ -399,8 +399,10 @@ function forEachSyntaxNode(root: Node, visit: (node: Node) => void): void { visit(node) for (const key of VISITOR_KEYS[node.type] ?? []) { const child: unknown = Reflect.get(node, key) - if (Array.isArray(child)) pending.push(...child.filter(isSyntaxNode)) - else if (isSyntaxNode(child)) pending.push(child) + /* 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) } } } @@ -428,7 +430,7 @@ function extractJavaScriptImports(code: string): { forEachSyntaxNode(program, (node) => { if (node.type === 'Identifier') identifierNames.add(node.name) if ( - node.type === 'CallExpression' && + (node.type === 'CallExpression' || node.type === 'OptionalCallExpression') && node.callee.type === 'Identifier' && node.callee.name === 'require' ) { diff --git a/apps/sim/scripts/test-workflow-code-ui-e2e.ts b/apps/sim/scripts/test-workflow-code-ui-e2e.ts index c3354c86d73..43e76fe791b 100644 --- a/apps/sim/scripts/test-workflow-code-ui-e2e.ts +++ b/apps/sim/scripts/test-workflow-code-ui-e2e.ts @@ -303,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 }, ]) @@ -365,6 +368,7 @@ try { await page.keyboard.type('typed into a read-only view') await page.keyboard.press('Enter') await page.keyboard.press('Backspace') + 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( From 4c9e19acf5991e5b5cadd4be3f0e080aecc62217 Mon Sep 17 00:00:00 2001 From: Waleed Latif Date: Fri, 20 Nov 2026 12:44:46 -0800 Subject: [PATCH 5/5] fix(editor): run the context menu's cut and copy as the editor's own commands MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cut and Copy run as native commands on the refocused editor, like ⌘X and ⌘C: one synchronous step that copies every selection, cuts exactly what it copied, keeps the chat selection reference, and leaves focus in the editor. Copy all returns focus too. --- .../components/file-viewer/text-editor.tsx | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx index c1f7463f348..f735ba9e2b1 100644 --- a/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx +++ b/apps/sim/app/workspace/[workspaceId]/files/components/file-viewer/text-editor.tsx @@ -285,16 +285,14 @@ export const TextEditor = memo(function TextEditor({ return false } - /** Every selected passage, joined by line breaks as a multi-cursor copy is. */ - const selectedText = (): string => { - const editor = editorRef.current - if (!editor) return '' - const text = editor.getText() - return editor - .getSelectionRanges() - .filter(({ start, end }) => start < end) - .map(({ start, end }) => text.slice(start, end)) - .join('\n') + /** + * Runs the menu's Cut or Copy as the native command on the focused editor, as ⌘X and ⌘C do: + * the editor's own handler copies every selection and cuts in one step, and the copy carries + * the chat selection reference. + */ + const runClipboardCommand = (command: 'cut' | 'copy') => { + editorRef.current?.focus() + document.execCommand(command) } const handleEditorPasteCapture = (event: ReactClipboardEvent) => { @@ -455,22 +453,16 @@ export const TextEditor = memo(function TextEditor({ }} onCut={() => { closeContextMenu() - const text = selectedText() - if (!text) return - /* Delete only once the text is safely on the clipboard. */ - navigator.clipboard - .writeText(text) - .then(() => editorRef.current?.replaceSelection('')) - .catch(() => {}) + runClipboardCommand('cut') }} onCopy={() => { closeContextMenu() - const text = selectedText() - if (text) navigator.clipboard.writeText(text).catch(() => {}) + runClipboardCommand('copy') }} onCopyAll={() => { - navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {}) closeContextMenu() + navigator.clipboard.writeText(editorRef.current?.getText() ?? '').catch(() => {}) + editorRef.current?.focus() }} onPaste={() => { closeContextMenu()