mirror of
https://github.com/simstudioai/sim.git
synced 2026-09-24 15:45:35 +08:00
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:
@@ -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
|
||||
|
||||
+5
-6
@@ -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),
|
||||
|
||||
@@ -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 }
|
||||
|
||||
Reference in New Issue
Block a user