MCPcopy Create free account
hub / github.com/modelscope/ms-agent / start_agent

Function start_agent

webui/backend/websocket_handler.py:171–317  ·  view source on GitHub ↗

Start an agent for a session

(session_id: str, data: Dict[str, Any],
                      websocket: WebSocket)

Source from the content-addressed store, hash-verified

169
170
171async def start_agent(session_id: str, data: Dict[str, Any],
172 websocket: WebSocket):
173 """Start an agent for a session"""
174 print(f'[Agent] Starting agent for session: {session_id}')
175
176 session = session_manager.get_session(session_id)
177 if not session:
178 print(f'[Agent] ERROR: Session not found: {session_id}')
179 await websocket.send_json({
180 'type': 'error',
181 'message': 'Session not found'
182 })
183 return
184
185 session_type = session.get('session_type', 'project')
186
187 # For chat mode, use default chat agent config
188 if session_type == 'chat':
189 # Create a virtual project for chat mode using the default agent.yaml
190 import ms_agent
191 # Get ms_agent package installation path
192 # Use __path__ which is always available for packages and gives real filesystem paths
193 if hasattr(ms_agent, '__path__') and ms_agent.__path__:
194 ms_agent_package_path = Path(ms_agent.__path__[0])
195 elif ms_agent.__file__ is not None:
196 ms_agent_package_path = Path(ms_agent.__file__).parent
197 else:
198 raise RuntimeError('Cannot determine ms_agent package path. '
199 'Please ensure ms_agent is properly installed.')
200 chat_config_path = ms_agent_package_path / 'agent' / 'agent.yaml'
201
202 project = {
203 'id': '__chat__',
204 'name': 'Chat Assistant',
205 'display_name': 'Chat Assistant',
206 'description': 'Default chat mode',
207 'type': 'agent',
208 'path': str(ms_agent_package_path / 'agent'),
209 'config_file': str(chat_config_path),
210 'has_readme': False,
211 'supports_workflow_switch': False
212 }
213 else:
214 # For project mode, get the project
215 project = project_discovery.get_project(session['project_id'])
216 if not project:
217 print(f"[Agent] ERROR: Project not found: {session['project_id']}")
218 await websocket.send_json({
219 'type': 'error',
220 'message': 'Project not found'
221 })
222 return
223
224 # Clean up output directory for code_genesis before starting
225 if project['id'] == 'code_genesis':
226 output_dir = os.path.join(project['path'], 'output')
227 if os.path.exists(output_dir):
228 try:

Callers 1

handle_session_messageFunction · 0.85

Calls 15

startMethod · 0.95
AgentRunnerClass · 0.90
on_agent_outputFunction · 0.85
on_agent_logFunction · 0.85
on_agent_progressFunction · 0.85
on_agent_completeFunction · 0.85
on_agent_errorFunction · 0.85
get_sessionMethod · 0.80
getMethod · 0.80
get_projectMethod · 0.80
send_to_sessionMethod · 0.80
add_messageMethod · 0.80

Tested by

no test coverage detected