MCPcopy Create free account
hub / github.com/DSGWJQ/Feagent / generatePythonCode

Function generatePythonCode

web/src/features/workflows/utils/codeGenerator.ts:12–139  ·  view source on GitHub ↗
(nodes: Node[], edges: Edge[])

Source from the content-addressed store, hash-verified

10 * 生成 Python 代码
11 */
12export function generatePythonCode(nodes: Node[], edges: Edge[]): string {
13 const imports = new Set<string>();
14 imports.add('import asyncio');
15 imports.add('from typing import Any, Dict');
16
17 let code = '';
18
19 // 为每个节点生成函数
20 nodes.forEach((node) => {
21 switch (node.type) {
22 case 'start':
23 code += `\nasync def node_${node.id}_start(context: Dict[str, Any]) -> Any:
24 """Start node"""
25 print("Starting workflow...")
26 return context.get("initial_input", {})
27`;
28 break;
29
30 case 'httpRequest':
31 imports.add('import httpx');
32 code += `\nasync def node_${node.id}_http_request(context: Dict[str, Any]) -> Any:
33 """HTTP Request node"""
34 url = "${node.data.url || 'https://api.example.com'}"
35 method = "${node.data.method || 'GET'}"
36
37 async with httpx.AsyncClient() as client:
38 response = await client.request(method, url)
39 return response.json()
40`;
41 break;
42
43 case 'textModel':
44 imports.add('from openai import AsyncOpenAI');
45 code += `\nasync def node_${node.id}_text_model(context: Dict[str, Any]) -> Any:
46 """Text Model node"""
47 client = AsyncOpenAI()
48
49 response = await client.chat.completions.create(
50 model="${node.data.model || 'gpt-4'}",
51 messages=[
52 {"role": "user", "content": str(context.get("prompt", ""))}
53 ],
54 temperature=${node.data.temperature || 0.7},
55 max_tokens=${node.data.maxTokens || 2000}
56 )
57
58 return response.choices[0].message.content
59`;
60 break;
61
62 case 'conditional':
63 code += `\nasync def node_${node.id}_conditional(context: Dict[str, Any]) -> Any:
64 """Conditional node"""
65 condition = ${node.data.condition || 'True'}
66 return {"result": condition, "branch": "true" if condition else "false"}
67`;
68 break;
69

Callers 1

CodeExportModalFunction · 0.90

Calls 1

addMethod · 0.45

Tested by

no test coverage detected