-
Notifications
You must be signed in to change notification settings - Fork 31
Open
Description
// executeCode executes code through the CodeInterpreter API using Python SDK
func (e *testEnv) executeCode(namespace, name string, req *CodeExecuteRequest) (*CodeExecuteResponse, string, error) {
// Create a temporary Python file
tmpFile, err := os.CreateTemp("", "e2e-code-exec-*.py")
if err != nil {
return nil, "", fmt.Errorf("failed to create temp file: %w", err)
}
defer os.Remove(tmpFile.Name())
// Create a Python script that uses the agentcube SDK
// Note: Not using 'with' statement to keep session alive for test verification
pythonScript := fmt.Sprintf(`
import os
import sys
import json
# Set environment variables
os.environ['ROUTER_URL'] = %q
os.environ['WORKLOAD_MANAGER_URL'] = %q
if %q:
os.environ['API_TOKEN'] = %q
# Add SDK to path
sys.path.insert(0, '/root/agentcube/sdk-python')
from agentcube import CodeInterpreterClient
try:
client = CodeInterpreterClient(name=%q, namespace=%q)
result = client.run_code(%q, %q)
# Output as JSON for easy parsing
output = {
'stdout': result,
'stderr': '',
'exit_code': 0,
'session_id': client.session_id
}
print(json.dumps(output))
except Exception as e:
# Return error in expected format
output = {
'stdout': '',
'stderr': str(e),
'exit_code': 1,
'session_id': ''
}
print(json.dumps(output))
sys.exit(1)
`, e.routerURL, e.workloadMgrURL, e.authToken, e.authToken, name, namespace, req.Language, req.Code)
// Write the Python script to the temp file
if _, err := tmpFile.WriteString(pythonScript); err != nil {
return nil, "", fmt.Errorf("failed to write temp file: %w", err)
}
tmpFile.Close()
// Execute the Python file with timeout
ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
defer cancel()
//nolint:gosec // G204: tmpFile.Name() is controlled by this test, not user input
cmd := exec.CommandContext(ctx, "python3", tmpFile.Name())
var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
output := stdout.String()
// If stderr has content but stdout is empty, use stderr as output for error info
if output == "" && stderr.Len() > 0 {
output = stderr.String()
}
// Parse the JSON output
var jsonOutput struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode int `json:"exit_code"`
SessionID string `json:"session_id"`
}
if err := json.Unmarshal([]byte(output), &jsonOutput); err != nil {
return nil, "", fmt.Errorf("failed to parse python output: %w, output: %s, stderr: %s", err, output, stderr.String())
}
response := &CodeExecuteResponse{
Output: jsonOutput.Stdout,
Error: jsonOutput.Stderr,
ExitCode: jsonOutput.ExitCode,
}
/assign @YaoZengzeng
I guess this is a mistake from ai coding
Metadata
Metadata
Assignees
Labels
No labels