Skip to content

Commit 7c6e6d1

Browse files
authored
improvement(code): add wand config and system prompt for python code generation, strip \n from stdout in JS/Python (#1862)
1 parent e186ea6 commit 7c6e6d1

File tree

3 files changed

+26
-11
lines changed
  • apps/sim
    • app
      • api/function/execute
      • workspace/[workspaceId]/w/[workflowId]/components/panel-new/components/editor/components/sub-block/components/code
    • lib/execution

3 files changed

+26
-11
lines changed

apps/sim/app/api/function/execute/route.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -640,6 +640,18 @@ function escapeRegExp(string: string): string {
640640
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
641641
}
642642

643+
/**
644+
* Remove one trailing newline from stdout
645+
* This handles the common case where print() or console.log() adds a trailing \n
646+
* that users don't expect to see in the output
647+
*/
648+
function cleanStdout(stdout: string): string {
649+
if (stdout.endsWith('\n')) {
650+
return stdout.slice(0, -1)
651+
}
652+
return stdout
653+
}
654+
643655
export async function POST(req: NextRequest) {
644656
const requestId = generateRequestId()
645657
const startTime = Date.now()
@@ -820,7 +832,7 @@ export async function POST(req: NextRequest) {
820832

821833
return NextResponse.json({
822834
success: true,
823-
output: { result: e2bResult ?? null, stdout, executionTime },
835+
output: { result: e2bResult ?? null, stdout: cleanStdout(stdout), executionTime },
824836
})
825837
}
826838
// Track prologue lines for error adjustment
@@ -884,7 +896,7 @@ export async function POST(req: NextRequest) {
884896

885897
return NextResponse.json({
886898
success: true,
887-
output: { result: e2bResult ?? null, stdout, executionTime },
899+
output: { result: e2bResult ?? null, stdout: cleanStdout(stdout), executionTime },
888900
})
889901
}
890902

@@ -948,7 +960,7 @@ export async function POST(req: NextRequest) {
948960

949961
return NextResponse.json({
950962
success: true,
951-
output: { result, stdout, executionTime },
963+
output: { result, stdout: cleanStdout(stdout), executionTime },
952964
})
953965
} catch (error: any) {
954966
const executionTime = Date.now() - startTime
@@ -981,7 +993,7 @@ export async function POST(req: NextRequest) {
981993
error: userFriendlyErrorMessage,
982994
output: {
983995
result: null,
984-
stdout,
996+
stdout: cleanStdout(stdout),
985997
executionTime,
986998
},
987999
// Include debug information in development or for debugging

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel-new/components/editor/components/sub-block/components/code/code.tsx

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ export function Code({
210210
const accessiblePrefixes = useAccessibleReferencePrefixes(blockId)
211211
const emitTagSelection = useTagSelection(blockId, subBlockId)
212212
const [languageValue] = useSubBlockValue<string>(blockId, 'language')
213-
const [remoteExecution] = useSubBlockValue<boolean>(blockId, 'remoteExecution')
214213

215214
// Derived state
216215
const effectiveLanguage = (languageValue as 'javascript' | 'python' | 'json') || language
@@ -244,26 +243,26 @@ export function Code({
244243
}, [generationType])
245244

246245
const dynamicPlaceholder = useMemo(() => {
247-
if (remoteExecution && languageValue === CodeLanguage.Python) {
246+
if (languageValue === CodeLanguage.Python) {
248247
return 'Write Python...'
249248
}
250249
return placeholder
251-
}, [remoteExecution, languageValue, placeholder])
250+
}, [languageValue, placeholder])
252251

253252
const dynamicWandConfig = useMemo(() => {
254-
if (remoteExecution && languageValue === CodeLanguage.Python) {
253+
if (languageValue === CodeLanguage.Python) {
255254
return {
256255
...wandConfig,
257256
prompt: PYTHON_AI_PROMPT,
258257
placeholder: 'Describe the Python function you want to create...',
259258
}
260259
}
261260
return wandConfig
262-
}, [wandConfig, remoteExecution, languageValue])
261+
}, [wandConfig, languageValue])
263262

264263
// AI code generation integration
265264
const wandHook = useWand({
266-
wandConfig: wandConfig || { enabled: false, prompt: '' },
265+
wandConfig: dynamicWandConfig || { enabled: false, prompt: '' },
267266
currentValue: code,
268267
onStreamStart: () => handleStreamStartRef.current?.(),
269268
onStreamChunk: (chunk: string) => handleStreamChunkRef.current?.(chunk),

apps/sim/lib/execution/e2b.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ export async function executeInE2B(req: E2BExecutionRequest): Promise<E2BExecuti
8686
} catch {
8787
result = jsonPart
8888
}
89-
cleanedStdout = lines.filter((l) => !l.startsWith(prefix)).join('\n')
89+
const filteredLines = lines.filter((l) => !l.startsWith(prefix))
90+
if (filteredLines.length > 0 && filteredLines[filteredLines.length - 1] === '') {
91+
filteredLines.pop()
92+
}
93+
cleanedStdout = filteredLines.join('\n')
9094
}
9195

9296
return { result, stdout: cleanedStdout, sandboxId }

0 commit comments

Comments
 (0)