MCPcopy Create free account
hub / github.com/astercloud/aster / wrapCode

Method wrapCode

pkg/tools/bridge/runtime.go:188–330  ·  view source on GitHub ↗
(code string, input map[string]any)

Source from the content-addressed store, hash-verified

186}
187
188func (r *PythonRuntime) wrapCode(code string, input map[string]any) string {
189 inputJSON, _ := json.Marshal(input)
190
191 // 如果没有配置工具,使用简单包装
192 if len(r.availableTools) == 0 {
193 return fmt.Sprintf(`import json
194import sys
195
196# Input data
197_input = json.loads('%s')
198
199# User code
200%s
201`, string(inputJSON), code)
202 }
203
204 // PTC 模式: 注入工具桥接代码
205 bridgeURL := r.bridgeURL
206 if bridgeURL == "" {
207 bridgeURL = "http://localhost:8080"
208 }
209
210 // 生成工具列表 JSON
211 toolsJSON, _ := json.Marshal(r.availableTools)
212
213 return fmt.Sprintf(`import json
214import asyncio
215import sys
216import os
217
218# ========== Aster Bridge SDK (内联) ==========
219try:
220 import aiohttp
221except ImportError:
222 print("Error: aiohttp is required. Install it with: pip install aiohttp", file=sys.stderr)
223 sys.exit(1)
224
225class _ToolExecutionError(Exception):
226 """工具执行错误"""
227 pass
228
229class _NetworkError(Exception):
230 """网络错误"""
231 pass
232
233class _AsterBridge:
234 def __init__(self, base_url, max_retries=3, retry_delay=0.5):
235 self.base_url = base_url
236 self.max_retries = max_retries
237 self.retry_delay = retry_delay
238 self._session = None
239
240 async def _get_session(self):
241 if self._session is None or self._session.closed:
242 self._session = aiohttp.ClientSession()
243 return self._session
244
245 async def call_tool(self, name, **kwargs):

Callers 2

ExecuteMethod · 0.95

Calls 1

indentCodeFunction · 0.85

Tested by 1