|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2023 Datalayer, Inc. |
| 3 | + * |
| 4 | + * MIT License |
| 5 | + */ |
| 6 | + |
| 7 | +/** |
| 8 | + * Platform-agnostic code execution operation for Lexical documents. |
| 9 | + * Executes code directly in the kernel without creating or modifying blocks. |
| 10 | + * |
| 11 | + * @module tools/operations/executeCode |
| 12 | + */ |
| 13 | + |
| 14 | +import type { |
| 15 | + ToolOperation, |
| 16 | + LexicalExecutionContext, |
| 17 | +} from '../core/interfaces'; |
| 18 | +import { formatResponse } from '../core/formatter'; |
| 19 | + |
| 20 | +/** |
| 21 | + * Parameters for executeCode operation. |
| 22 | + */ |
| 23 | +export interface ExecuteCodeParams { |
| 24 | + /** Code to execute */ |
| 25 | + code: string; |
| 26 | + |
| 27 | + /** Whether to store in kernel history (default: false) */ |
| 28 | + storeHistory?: boolean; |
| 29 | + |
| 30 | + /** Silent execution - no output displayed (default: false) */ |
| 31 | + silent?: boolean; |
| 32 | + |
| 33 | + /** Stop execution on error (default: true) */ |
| 34 | + stopOnError?: boolean; |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Validates ExecuteCodeParams at runtime. |
| 39 | + */ |
| 40 | +function isExecuteCodeParams(params: unknown): params is ExecuteCodeParams { |
| 41 | + if (typeof params !== 'object' || params === null) { |
| 42 | + return false; |
| 43 | + } |
| 44 | + |
| 45 | + const p = params as Record<string, unknown>; |
| 46 | + |
| 47 | + // code is required and must be a string |
| 48 | + if (typeof p.code !== 'string') { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // Optional boolean fields |
| 53 | + if (p.storeHistory !== undefined && typeof p.storeHistory !== 'boolean') { |
| 54 | + return false; |
| 55 | + } |
| 56 | + if (p.silent !== undefined && typeof p.silent !== 'boolean') { |
| 57 | + return false; |
| 58 | + } |
| 59 | + if (p.stopOnError !== undefined && typeof p.stopOnError !== 'boolean') { |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +/** |
| 67 | + * Result from executeCode operation. |
| 68 | + */ |
| 69 | +export interface ExecuteCodeResult { |
| 70 | + success: boolean; |
| 71 | + /** Execution outputs (streams, results, errors) */ |
| 72 | + outputs?: Array<{ |
| 73 | + type: 'stream' | 'execute_result' | 'display_data' | 'error'; |
| 74 | + content: unknown; |
| 75 | + }>; |
| 76 | + /** Error message if execution failed */ |
| 77 | + error?: string; |
| 78 | + /** Execution count (if stored in history) */ |
| 79 | + executionCount?: number; |
| 80 | +} |
| 81 | + |
| 82 | +/** |
| 83 | + * Executes code directly in the kernel without creating a block. |
| 84 | + * |
| 85 | + * This is useful for: |
| 86 | + * - Variable inspection |
| 87 | + * - Environment setup |
| 88 | + * - Background tasks |
| 89 | + * - Tool introspection |
| 90 | + */ |
| 91 | +export const executeCodeOperation: ToolOperation< |
| 92 | + ExecuteCodeParams, |
| 93 | + ExecuteCodeResult |
| 94 | +> = { |
| 95 | + name: 'executeCode', |
| 96 | + |
| 97 | + async execute( |
| 98 | + params: unknown, |
| 99 | + context: LexicalExecutionContext, |
| 100 | + ): Promise<ExecuteCodeResult> { |
| 101 | + // Validate params using type guard |
| 102 | + if (!isExecuteCodeParams(params)) { |
| 103 | + throw new Error( |
| 104 | + `Invalid parameters for executeCode. Expected { code: string, storeHistory?: boolean, silent?: boolean, stopOnError?: boolean }. ` + |
| 105 | + `Received: ${JSON.stringify(params)}`, |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + const { lexicalId } = context; |
| 110 | + |
| 111 | + if (!lexicalId) { |
| 112 | + return formatResponse( |
| 113 | + { |
| 114 | + success: false, |
| 115 | + error: 'Lexical ID is required for this operation.', |
| 116 | + }, |
| 117 | + context.format, |
| 118 | + ) as ExecuteCodeResult; |
| 119 | + } |
| 120 | + |
| 121 | + // Ensure executeCommand is available |
| 122 | + if (!context.executeCommand) { |
| 123 | + throw new Error( |
| 124 | + 'executeCommand callback is required for executeCode operation. ' + |
| 125 | + 'This should be provided by the platform adapter.', |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + // Call internal command to execute code directly in kernel |
| 131 | + const result = await context.executeCommand<ExecuteCodeResult>( |
| 132 | + 'lexical.executeCode', |
| 133 | + { |
| 134 | + lexicalId, |
| 135 | + ...params, |
| 136 | + }, |
| 137 | + ); |
| 138 | + |
| 139 | + return formatResponse(result, context.format) as ExecuteCodeResult; |
| 140 | + } catch (error) { |
| 141 | + const errorMessage = |
| 142 | + error instanceof Error ? error.message : String(error); |
| 143 | + throw new Error(`Failed to execute code: ${errorMessage}`); |
| 144 | + } |
| 145 | + }, |
| 146 | +}; |
0 commit comments