@@ -10,6 +10,7 @@ import { DEFAULT_EXECUTION_TIMEOUT_MS } from '@/lib/execution/constants'
1010import { DEFAULT_CODE_LANGUAGE } from '@/lib/execution/languages'
1111import { PRIVATE_SECRET_PROVENANCE_FIELD } from '@/lib/execution/private-tool-metadata'
1212import { SANDBOX_INPUT_DIR , SANDBOX_OUTPUT_DIR } from '@/lib/execution/remote-sandbox/sandbox-paths'
13+ import { MAX_FUNCTION_CODE_LENGTH } from '@/lib/function-execution/limits'
1314import type { UserFile } from '@/executor/types'
1415import type { CodeExecutionInput , CodeExecutionOutput } from '@/tools/function/types'
1516import type { InternalToolConfig } from '@/tools/types'
@@ -43,15 +44,24 @@ function normalizeSandboxInputFiles(value: unknown): FunctionExecuteBody['files'
4344 return userFiles . map ( ( file ) => ( { ...file } ) )
4445}
4546
46- /** Builds the canonical Function protocol body for both HTTP compatibility and in-process calls. */
47+ /**
48+ * Builds the canonical Function protocol body for both HTTP compatibility and in-process calls.
49+ *
50+ * `sourceCode` is the display copy used only to render errors, with referenced values inlined,
51+ * so it can far outgrow the executed code. Past the route's source cap it is omitted, and errors
52+ * fall back to the executed code, rather than failing a request whose executed code is in bounds.
53+ */
4754export function buildFunctionExecuteBody ( params : CodeExecutionInput ) : FunctionExecuteBody {
4855 const codeContent = Array . isArray ( params . code )
4956 ? params . code . map ( ( entry : { content : string } ) => entry . content ) . join ( '\n' )
5057 : params . code
5158
5259 return {
5360 code : codeContent ,
54- sourceCode : params . sourceCode ,
61+ sourceCode :
62+ params . sourceCode !== undefined && params . sourceCode . length <= MAX_FUNCTION_CODE_LENGTH
63+ ? params . sourceCode
64+ : undefined ,
5565 language : params . language || DEFAULT_CODE_LANGUAGE ,
5666 timeout : params . timeout || DEFAULT_EXECUTION_TIMEOUT_MS ,
5767 title : params . title ,
0 commit comments