MCPcopy
hub / github.com/langroid/langroid / handle

Method handle

langroid/agent/tools/task_tool.py:185–218  ·  view source on GitHub ↗

Handle the TaskTool by creating a sub-agent with specified tools and running the task non-interactively. Args: agent: The parent ChatAgent that is handling this tool chat_doc: The ChatDocument containing this tool message

(
        self, agent: ChatAgent, chat_doc: Optional[ChatDocument] = None
    )

Source from the content-addressed store, hash-verified

183 return task
184
185 def handle(
186 self, agent: ChatAgent, chat_doc: Optional[ChatDocument] = None
187 ) -> Optional[ChatDocument]:
188 """
189
190 Handle the TaskTool by creating a sub-agent with specified tools
191 and running the task non-interactively.
192
193 Args:
194 agent: The parent ChatAgent that is handling this tool
195 chat_doc: The ChatDocument containing this tool message
196 """
197
198 task = self._set_up_task(agent)
199
200 # Create a ChatDocument for the prompt with parent pointer
201 prompt_doc = None
202 if chat_doc is not None:
203 from langroid.agent.chat_document import ChatDocMetaData
204
205 prompt_doc = ChatDocument(
206 content=self.prompt,
207 metadata=ChatDocMetaData(
208 parent_id=chat_doc.id(),
209 agent_id=agent.id,
210 sender=chat_doc.metadata.sender,
211 ),
212 )
213 # Set bidirectional parent-child relationship
214 chat_doc.metadata.child_id = prompt_doc.id()
215
216 # Run the task with the ChatDocument or string prompt
217 result = task.run(prompt_doc or self.prompt, turns=self.max_iterations or 10)
218 return result
219
220 async def handle_async(
221 self, agent: ChatAgent, chat_doc: Optional[ChatDocument] = None

Callers 1

wrapperMethod · 0.45

Calls 5

_set_up_taskMethod · 0.95
ChatDocumentClass · 0.90
ChatDocMetaDataClass · 0.90
idMethod · 0.80
runMethod · 0.80

Tested by

no test coverage detected