improvement(code): add wand config and system prompt for python code generation, strip \n from stdout in JS/Python (#1862)

This commit is contained in:
Waleed
2025-11-08 16:44:16 -08:00
committed by GitHub
parent e186ea630a
commit 7c6e6d1603
3 changed files with 26 additions and 11 deletions
+16 -4
View File
@@ -640,6 +640,18 @@ function escapeRegExp(string: string): string {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
/**
* Remove one trailing newline from stdout
* This handles the common case where print() or console.log() adds a trailing \n
* that users don't expect to see in the output
*/
function cleanStdout(stdout: string): string {
if (stdout.endsWith('\n')) {
return stdout.slice(0, -1)
}
return stdout
}
export async function POST(req: NextRequest) {
const requestId = generateRequestId()
const startTime = Date.now()
@@ -820,7 +832,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({
success: true,
output: { result: e2bResult ?? null, stdout, executionTime },
output: { result: e2bResult ?? null, stdout: cleanStdout(stdout), executionTime },
})
}
// Track prologue lines for error adjustment
@@ -884,7 +896,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({
success: true,
output: { result: e2bResult ?? null, stdout, executionTime },
output: { result: e2bResult ?? null, stdout: cleanStdout(stdout), executionTime },
})
}
@@ -948,7 +960,7 @@ export async function POST(req: NextRequest) {
return NextResponse.json({
success: true,
output: { result, stdout, executionTime },
output: { result, stdout: cleanStdout(stdout), executionTime },
})
} catch (error: any) {
const executionTime = Date.now() - startTime
@@ -981,7 +993,7 @@ export async function POST(req: NextRequest) {
error: userFriendlyErrorMessage,
output: {
result: null,
stdout,
stdout: cleanStdout(stdout),
executionTime,
},
// Include debug information in development or for debugging
@@ -210,7 +210,6 @@ export function Code({
const accessiblePrefixes = useAccessibleReferencePrefixes(blockId)
const emitTagSelection = useTagSelection(blockId, subBlockId)
const [languageValue] = useSubBlockValue<string>(blockId, 'language')
const [remoteExecution] = useSubBlockValue<boolean>(blockId, 'remoteExecution')
// Derived state
const effectiveLanguage = (languageValue as 'javascript' | 'python' | 'json') || language
@@ -244,14 +243,14 @@ export function Code({
}, [generationType])
const dynamicPlaceholder = useMemo(() => {
if (remoteExecution && languageValue === CodeLanguage.Python) {
if (languageValue === CodeLanguage.Python) {
return 'Write Python...'
}
return placeholder
}, [remoteExecution, languageValue, placeholder])
}, [languageValue, placeholder])
const dynamicWandConfig = useMemo(() => {
if (remoteExecution && languageValue === CodeLanguage.Python) {
if (languageValue === CodeLanguage.Python) {
return {
...wandConfig,
prompt: PYTHON_AI_PROMPT,
@@ -259,11 +258,11 @@ export function Code({
}
}
return wandConfig
}, [wandConfig, remoteExecution, languageValue])
}, [wandConfig, languageValue])
// AI code generation integration
const wandHook = useWand({
wandConfig: wandConfig || { enabled: false, prompt: '' },
wandConfig: dynamicWandConfig || { enabled: false, prompt: '' },
currentValue: code,
onStreamStart: () => handleStreamStartRef.current?.(),
onStreamChunk: (chunk: string) => handleStreamChunkRef.current?.(chunk),
+5 -1
View File
@@ -86,7 +86,11 @@ export async function executeInE2B(req: E2BExecutionRequest): Promise<E2BExecuti
} catch {
result = jsonPart
}
cleanedStdout = lines.filter((l) => !l.startsWith(prefix)).join('\n')
const filteredLines = lines.filter((l) => !l.startsWith(prefix))
if (filteredLines.length > 0 && filteredLines[filteredLines.length - 1] === '') {
filteredLines.pop()
}
cleanedStdout = filteredLines.join('\n')
}
return { result, stdout: cleanedStdout, sandboxId }