{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36834357",
   "metadata": {},
   "outputs": [],
   "source": [
    "from terminaluse.lib import TerminalUse\n",
    "\n",
    "client = TerminalUse(base_url=\"http://localhost:5003\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d1c309d6",
   "metadata": {},
   "outputs": [],
   "source": [
    "AGENT_NAME = \"{{ agent_name }}\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f6e6ef0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# (REQUIRED) Create a new task. For Async agents, you must create a task for messages to be associated with.\n",
    "import uuid\n",
    "\n",
    "rpc_response = client.tasks.create(\n",
    "    agent_name=AGENT_NAME,\n",
    "    params={\n",
    "        \"name\": f\"{str(uuid.uuid4())[:8]}-task\",\n",
    "        \"params\": {}\n",
    "    }\n",
    ")\n",
    "\n",
    "task = rpc_response.result\n",
    "print(task)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b03b0d37",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Send an event to the agent\n",
    "\n",
    "# The response is expected to be a list of TaskMessage objects, which is a union of the following types:\n",
    "# - TextContent: A message with just text content   \n",
    "# - DataContent: A message with JSON-serializable data content\n",
    "# - ToolRequestContent: A message with a tool request, which contains a JSON-serializable request to call a tool\n",
    "# - ToolResponseContent: A message with a tool response, which contains response object from a tool call in its content\n",
    "\n",
    "# When processing the event/send response, if you are expecting more than TextContent, such as DataContent, ToolRequestContent, or ToolResponseContent, you can process them as well\n",
    "\n",
    "rpc_response = client.tasks.send_event(\n",
    "    agent_name=AGENT_NAME,\n",
    "    params={\n",
    "        \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n",
    "        \"task_id\": task.id,\n",
    "    }\n",
    ")\n",
    "\n",
    "event = rpc_response.result\n",
    "print(event)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6927cc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Subscribe to the async task messages produced by the agent\n",
    "from terminaluse.lib.utils.dev_tools import subscribe_to_async_task_messages\n",
    "\n",
    "task_messages = subscribe_to_async_task_messages(\n",
    "    client=client,\n",
    "    task=task, \n",
    "    only_after_timestamp=event.created_at, \n",
    "    print_messages=True,\n",
    "    rich_print=True,\n",
    "    timeout=5,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4864e354",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
