|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 6, |
| 6 | + "id": "36834357", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "from agentex import Agentex\n", |
| 11 | + "\n", |
| 12 | + "client = Agentex(base_url=\"http://localhost:5003\")" |
| 13 | + ] |
| 14 | + }, |
| 15 | + { |
| 16 | + "cell_type": "code", |
| 17 | + "execution_count": 7, |
| 18 | + "id": "d1c309d6", |
| 19 | + "metadata": {}, |
| 20 | + "outputs": [], |
| 21 | + "source": [ |
| 22 | + "AGENT_NAME = \"ab020-streaming\"" |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "code", |
| 27 | + "execution_count": 8, |
| 28 | + "id": "9f6e6ef0", |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [ |
| 31 | + { |
| 32 | + "name": "stdout", |
| 33 | + "output_type": "stream", |
| 34 | + "text": [ |
| 35 | + "Task(id='fab911e3-47c5-43f2-9942-382a98d45975', created_at=datetime.datetime(2025, 7, 27, 0, 52, 55, 493055, tzinfo=TzInfo(UTC)), name='69297be2-task', status='RUNNING', status_reason='Task created, forwarding to ACP server', updated_at=datetime.datetime(2025, 7, 27, 0, 52, 55, 493055, tzinfo=TzInfo(UTC)))\n" |
| 36 | + ] |
| 37 | + } |
| 38 | + ], |
| 39 | + "source": [ |
| 40 | + "# (REQUIRED) Create a new task. For Agentic agents, you must create a task for messages to be associated with.\n", |
| 41 | + "\n", |
| 42 | + "from typing import cast\n", |
| 43 | + "import uuid\n", |
| 44 | + "\n", |
| 45 | + "from agentex.types import Task\n", |
| 46 | + "\n", |
| 47 | + "TASK_ID = str(uuid.uuid4())[:8]\n", |
| 48 | + "\n", |
| 49 | + "rpc_response = client.agents.rpc_by_name(\n", |
| 50 | + " agent_name=AGENT_NAME,\n", |
| 51 | + " method=\"task/create\",\n", |
| 52 | + " params={\n", |
| 53 | + " \"name\": f\"{TASK_ID}-task\",\n", |
| 54 | + " \"params\": {}\n", |
| 55 | + " }\n", |
| 56 | + ")\n", |
| 57 | + "\n", |
| 58 | + "task = cast(Task, rpc_response.result)\n", |
| 59 | + "print(task)" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": null, |
| 65 | + "id": "b03b0d37", |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "name": "stdout", |
| 70 | + "output_type": "stream", |
| 71 | + "text": [ |
| 72 | + "Event(id='6f1c066c-f400-49a5-a8bd-afe0f2367b4c', agent_id='ef21f2e7-5bbe-440a-824b-7e89f462a781', sequence_id=203, task_id='fab911e3-47c5-43f2-9942-382a98d45975', content=TextContent(author='user', content='Hello what can you do?', attachments=None, format='plain', style='static', type='text'), created_at=datetime.datetime(2025, 7, 27, 0, 52, 56, 693376, tzinfo=TzInfo(UTC)))\n" |
| 73 | + ] |
| 74 | + } |
| 75 | + ], |
| 76 | + "source": [ |
| 77 | + "# Test non streaming response\n", |
| 78 | + "from typing import cast\n", |
| 79 | + "from agentex.types import Event\n", |
| 80 | + "\n", |
| 81 | + "# The response is expected to be a list of TaskMessage objects, which is a union of the following types:\n", |
| 82 | + "# - TextContent: A message with just text content \n", |
| 83 | + "# - DataContent: A message with JSON-serializable data content\n", |
| 84 | + "# - ToolRequestContent: A message with a tool request, which contains a JSON-serializable request to call a tool\n", |
| 85 | + "# - ToolResponseContent: A message with a tool response, which contains response object from a tool call in its content\n", |
| 86 | + "\n", |
| 87 | + "# When processing the message/send response, if you are expecting more than TextContent, such as DataContent, ToolRequestContent, or ToolResponseContent, you can process them as well\n", |
| 88 | + "\n", |
| 89 | + "rpc_response = client.agents.rpc_by_name(\n", |
| 90 | + " agent_name=AGENT_NAME,\n", |
| 91 | + " method=\"event/send\",\n", |
| 92 | + " params={\n", |
| 93 | + " \"content\": {\"type\": \"text\", \"author\": \"user\", \"content\": \"Hello what can you do?\"},\n", |
| 94 | + " \"task_id\": task.id,\n", |
| 95 | + " }\n", |
| 96 | + ")\n", |
| 97 | + "\n", |
| 98 | + "event = cast(Event, rpc_response.result)\n", |
| 99 | + "print(event)" |
| 100 | + ] |
| 101 | + }, |
| 102 | + { |
| 103 | + "cell_type": "code", |
| 104 | + "execution_count": 10, |
| 105 | + "id": "a6927cc0", |
| 106 | + "metadata": {}, |
| 107 | + "outputs": [ |
| 108 | + { |
| 109 | + "data": { |
| 110 | + "text/html": [ |
| 111 | + "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ffff; text-decoration-color: #00ffff\">╭───────────────────────── </span><span style=\"color: #00ffff; text-decoration-color: #00ffff; font-weight: bold\">USER</span><span style=\"color: #00ffff; text-decoration-color: #00ffff\"> [07/27/2025 00:52:56] ─────────────────────────╮</span>\n", |
| 112 | + "<span style=\"color: #00ffff; text-decoration-color: #00ffff\">│</span> Hello what can you do? <span style=\"color: #00ffff; text-decoration-color: #00ffff\">│</span>\n", |
| 113 | + "<span style=\"color: #00ffff; text-decoration-color: #00ffff\">╰──────────────────────────────────────────────────────────────────────────────╯</span>\n", |
| 114 | + "</pre>\n" |
| 115 | + ], |
| 116 | + "text/plain": [ |
| 117 | + "\u001b[96m╭─\u001b[0m\u001b[96m────────────────────────\u001b[0m\u001b[96m \u001b[0m\u001b[1;96mUSER\u001b[0m\u001b[96m [07/27/2025 00:52:56] \u001b[0m\u001b[96m────────────────────────\u001b[0m\u001b[96m─╮\u001b[0m\n", |
| 118 | + "\u001b[96m│\u001b[0m Hello what can you do? \u001b[96m│\u001b[0m\n", |
| 119 | + "\u001b[96m╰──────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n" |
| 120 | + ] |
| 121 | + }, |
| 122 | + "metadata": {}, |
| 123 | + "output_type": "display_data" |
| 124 | + }, |
| 125 | + { |
| 126 | + "name": "stdout", |
| 127 | + "output_type": "stream", |
| 128 | + "text": [ |
| 129 | + " \r" |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "data": { |
| 134 | + "text/html": [ |
| 135 | + "<pre style=\"white-space:pre;overflow-x:auto;line-height:normal;font-family:Menlo,'DejaVu Sans Mono',consolas,'Courier New',monospace\"><span style=\"color: #00ff00; text-decoration-color: #00ff00\">╭──────────────────────── </span><span style=\"color: #00ff00; text-decoration-color: #00ff00; font-weight: bold\">AGENT</span><span style=\"color: #00ff00; text-decoration-color: #00ff00\"> [07/27/2025 00:52:56] ─────────────────────────╮</span>\n", |
| 136 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> Hello! I can assist you with a variety of tasks, such as: <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 137 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 138 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 1 </span>Answering questions on a wide range of topics, including science, <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 139 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> </span>history, technology, and more. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 140 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 2 </span>Providing explanations and summaries of complex concepts. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 141 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 3 </span>Assisting with language translations and grammar checks. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 142 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 4 </span>Offering writing tips, editing assistance, and brainstorming ideas for <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 143 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> </span>creative projects. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 144 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 5 </span>Giving advice on studying, time management, and organization. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 145 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 6 </span>Sharing information about books, movies, and other media. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 146 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #808000; text-decoration-color: #808000; font-weight: bold\"> 7 </span>Engaging in general conversation or providing entertainment. <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 147 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 148 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> If you have a specific question or need help with something, feel free to <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 149 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span> ask! <span style=\"color: #00ff00; text-decoration-color: #00ff00\">│</span>\n", |
| 150 | + "<span style=\"color: #00ff00; text-decoration-color: #00ff00\">╰──────────────────────────────────────────────────────────────────────────────╯</span>\n", |
| 151 | + "</pre>\n" |
| 152 | + ], |
| 153 | + "text/plain": [ |
| 154 | + "\u001b[92m╭─\u001b[0m\u001b[92m───────────────────────\u001b[0m\u001b[92m \u001b[0m\u001b[1;92mAGENT\u001b[0m\u001b[92m [07/27/2025 00:52:56] \u001b[0m\u001b[92m────────────────────────\u001b[0m\u001b[92m─╮\u001b[0m\n", |
| 155 | + "\u001b[92m│\u001b[0m Hello! I can assist you with a variety of tasks, such as: \u001b[92m│\u001b[0m\n", |
| 156 | + "\u001b[92m│\u001b[0m \u001b[92m│\u001b[0m\n", |
| 157 | + "\u001b[92m│\u001b[0m \u001b[1;33m 1 \u001b[0mAnswering questions on a wide range of topics, including science, \u001b[92m│\u001b[0m\n", |
| 158 | + "\u001b[92m│\u001b[0m \u001b[1;33m \u001b[0mhistory, technology, and more. \u001b[92m│\u001b[0m\n", |
| 159 | + "\u001b[92m│\u001b[0m \u001b[1;33m 2 \u001b[0mProviding explanations and summaries of complex concepts. \u001b[92m│\u001b[0m\n", |
| 160 | + "\u001b[92m│\u001b[0m \u001b[1;33m 3 \u001b[0mAssisting with language translations and grammar checks. \u001b[92m│\u001b[0m\n", |
| 161 | + "\u001b[92m│\u001b[0m \u001b[1;33m 4 \u001b[0mOffering writing tips, editing assistance, and brainstorming ideas for \u001b[92m│\u001b[0m\n", |
| 162 | + "\u001b[92m│\u001b[0m \u001b[1;33m \u001b[0mcreative projects. \u001b[92m│\u001b[0m\n", |
| 163 | + "\u001b[92m│\u001b[0m \u001b[1;33m 5 \u001b[0mGiving advice on studying, time management, and organization. \u001b[92m│\u001b[0m\n", |
| 164 | + "\u001b[92m│\u001b[0m \u001b[1;33m 6 \u001b[0mSharing information about books, movies, and other media. \u001b[92m│\u001b[0m\n", |
| 165 | + "\u001b[92m│\u001b[0m \u001b[1;33m 7 \u001b[0mEngaging in general conversation or providing entertainment. \u001b[92m│\u001b[0m\n", |
| 166 | + "\u001b[92m│\u001b[0m \u001b[92m│\u001b[0m\n", |
| 167 | + "\u001b[92m│\u001b[0m If you have a specific question or need help with something, feel free to \u001b[92m│\u001b[0m\n", |
| 168 | + "\u001b[92m│\u001b[0m ask! \u001b[92m│\u001b[0m\n", |
| 169 | + "\u001b[92m╰──────────────────────────────────────────────────────────────────────────────╯\u001b[0m\n" |
| 170 | + ] |
| 171 | + }, |
| 172 | + "metadata": {}, |
| 173 | + "output_type": "display_data" |
| 174 | + }, |
| 175 | + { |
| 176 | + "name": "stdout", |
| 177 | + "output_type": "stream", |
| 178 | + "text": [ |
| 179 | + "Streaming timed out after 5 seconds - returning collected messages\n" |
| 180 | + ] |
| 181 | + } |
| 182 | + ], |
| 183 | + "source": [ |
| 184 | + "from agentex.lib.utils.dev_tools import subscribe_to_async_task_messages\n", |
| 185 | + "\n", |
| 186 | + "task_messages = subscribe_to_async_task_messages(\n", |
| 187 | + " client=client,\n", |
| 188 | + " task=task, \n", |
| 189 | + " only_after_timestamp=event.created_at, \n", |
| 190 | + " print_messages=True,\n", |
| 191 | + " rich_print=True,\n", |
| 192 | + " timeout=5,\n", |
| 193 | + ")" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "cell_type": "code", |
| 198 | + "execution_count": null, |
| 199 | + "id": "6470ae58", |
| 200 | + "metadata": {}, |
| 201 | + "outputs": [], |
| 202 | + "source": [] |
| 203 | + } |
| 204 | + ], |
| 205 | + "metadata": { |
| 206 | + "kernelspec": { |
| 207 | + "display_name": ".venv", |
| 208 | + "language": "python", |
| 209 | + "name": "python3" |
| 210 | + }, |
| 211 | + "language_info": { |
| 212 | + "codemirror_mode": { |
| 213 | + "name": "ipython", |
| 214 | + "version": 3 |
| 215 | + }, |
| 216 | + "file_extension": ".py", |
| 217 | + "mimetype": "text/x-python", |
| 218 | + "name": "python", |
| 219 | + "nbconvert_exporter": "python", |
| 220 | + "pygments_lexer": "ipython3", |
| 221 | + "version": "3.12.9" |
| 222 | + } |
| 223 | + }, |
| 224 | + "nbformat": 4, |
| 225 | + "nbformat_minor": 5 |
| 226 | +} |
0 commit comments