[{"data":1,"prerenderedAt":1738},["ShallowReactive",2],{"navigation":3,"/agent-api/streaming":261,"/agent-api/streaming-surround":1733},[4,22,50,83,125,161,194,205,231],{"title":5,"path":6,"stem":7,"children":8,"icon":16},"Getting Started","/getting-started","1.getting-started/1.index",[9,12,17],{"title":10,"path":6,"stem":7,"icon":11},"Introduction","i-ph-house",{"title":13,"path":14,"stem":15,"icon":16},"Quickstart","/getting-started/quickstart","1.getting-started/2.quickstart","i-ph-rocket",{"title":18,"path":19,"stem":20,"icon":21},"Plans","/getting-started/plans","1.getting-started/3.plans","i-ph-credit-card",{"title":23,"path":24,"stem":25,"children":26,"icon":29},"Agents","/agents","2.agents/1.index",[27,30,35,40,45],{"title":28,"path":24,"stem":25,"icon":29},"Overview","i-ph-robot",{"title":31,"path":32,"stem":33,"icon":34},"Creating an agent","/agents/creating-an-agent","2.agents/2.creating-an-agent","i-ph-plus",{"title":36,"path":37,"stem":38,"icon":39},"Agent settings","/agents/settings","2.agents/3.settings","i-ph-gear",{"title":41,"path":42,"stem":43,"icon":44},"Memory","/agents/memory","2.agents/4.memory","i-ph-database",{"title":46,"path":47,"stem":48,"icon":49},"Residential proxy","/agents/proxy","2.agents/5.proxy","i-ph-globe-hemisphere-west",{"title":51,"path":52,"stem":53,"children":54,"icon":57},"Chats & Tasks","/chats-and-tasks","3.chats-and-tasks/1.index",[55,58,63,68,73,78],{"title":56,"path":52,"stem":53,"icon":57},"Chats","i-ph-chats-circle",{"title":59,"path":60,"stem":61,"icon":62},"Sharing chats","/chats-and-tasks/sharing-chats","3.chats-and-tasks/2.sharing-chats","i-ph-share-network",{"title":64,"path":65,"stem":66,"icon":67},"Tasks","/chats-and-tasks/tasks","3.chats-and-tasks/3.tasks","i-ph-list-checks",{"title":69,"path":70,"stem":71,"icon":72},"Recurring tasks","/chats-and-tasks/recurring-tasks","3.chats-and-tasks/4.recurring-tasks","i-ph-repeat",{"title":74,"path":75,"stem":76,"icon":77},"Results & deliverables","/chats-and-tasks/results-and-deliverables","3.chats-and-tasks/5.results-and-deliverables","i-ph-package",{"title":79,"path":80,"stem":81,"icon":82},"Save a chat as a skill","/chats-and-tasks/save-chat-as-skill","3.chats-and-tasks/6.save-chat-as-skill","i-ph-sparkle",{"title":84,"path":85,"stem":86,"children":87,"icon":90},"Models & Skills","/models-and-skills","4.models-and-skills/1.index",[88,91,96,101,105,110,115,120],{"title":89,"path":85,"stem":86,"icon":90},"LLM configuration","i-ph-cpu",{"title":92,"path":93,"stem":94,"icon":95},"Providers & models","/models-and-skills/providers","4.models-and-skills/2.providers","i-ph-stack",{"title":97,"path":98,"stem":99,"icon":100},"Bring your own key","/models-and-skills/byok","4.models-and-skills/3.byok","i-ph-key",{"title":102,"path":103,"stem":104,"icon":29},"ChatGPT subscription","/models-and-skills/chatgpt-subscription","4.models-and-skills/4.chatgpt-subscription",{"title":106,"path":107,"stem":108,"icon":109},"Skills","/models-and-skills/skills","4.models-and-skills/5.skills","i-ph-puzzle-piece",{"title":111,"path":112,"stem":113,"icon":114},"Installing skills","/models-and-skills/installing-skills","4.models-and-skills/6.installing-skills","i-ph-download-simple",{"title":116,"path":117,"stem":118,"icon":119},"Custom skills","/models-and-skills/custom-skills","4.models-and-skills/7.custom-skills","i-ph-code",{"title":121,"path":122,"stem":123,"icon":124},"Local models","/models-and-skills/local-models","4.models-and-skills/8.local-models","i-ph-desktop-tower",{"title":126,"path":127,"stem":128,"children":129,"icon":131},"Connect Your Tools","/connect-your-tools","5.connect-your-tools/1.index",[130,132,137,142,147,152,156],{"title":28,"path":127,"stem":128,"icon":131},"i-ph-plug",{"title":133,"path":134,"stem":135,"icon":136},"Telegram","/connect-your-tools/telegram","5.connect-your-tools/2.telegram","i-ph-telegram-logo",{"title":138,"path":139,"stem":140,"icon":141},"Discord","/connect-your-tools/discord","5.connect-your-tools/3.discord","i-ph-discord-logo",{"title":143,"path":144,"stem":145,"icon":146},"Slack","/connect-your-tools/slack","5.connect-your-tools/4.slack","i-ph-slack-logo",{"title":148,"path":149,"stem":150,"icon":151},"Connectors","/connect-your-tools/connectors","5.connect-your-tools/5.connectors","i-ph-link",{"title":153,"path":154,"stem":155,"icon":100},"Secrets","/connect-your-tools/secrets","5.connect-your-tools/6.secrets",{"title":157,"path":158,"stem":159,"icon":160},"Audit log","/connect-your-tools/secret-audit-log","5.connect-your-tools/7.secret-audit-log","i-ph-scroll",{"title":162,"path":163,"stem":164,"children":165,"icon":193},"Account & Team","/account-and-team","6.account-and-team/1.index",[166,169,174,179,184,188],{"title":167,"path":163,"stem":164,"icon":168},"Signing in","i-ph-sign-in",{"title":170,"path":171,"stem":172,"icon":173},"Team members","/account-and-team/teams","6.account-and-team/3.teams","i-ph-users",{"title":175,"path":176,"stem":177,"icon":178},"Roles & permissions","/account-and-team/roles-and-permissions","6.account-and-team/4.roles-and-permissions","i-ph-shield",{"title":180,"path":181,"stem":182,"icon":183},"Invitations","/account-and-team/invitations","6.account-and-team/5.invitations","i-ph-envelope-simple",{"title":185,"path":186,"stem":187,"icon":21},"Billing & upgrades","/account-and-team/billing","6.account-and-team/6.billing",{"title":189,"path":190,"stem":191,"icon":192},"Support","/account-and-team/support","6.account-and-team/7.support","i-ph-lifebuoy","i-ph-user",{"title":195,"path":196,"stem":197,"children":198,"icon":200},"Apps","/apps","7.apps/1.index",[199,201],{"title":195,"path":196,"stem":197,"icon":200},"i-ph-squares-four",{"title":202,"path":203,"stem":204,"icon":119},"Building an app","/apps/building-an-app","7.apps/2.building-an-app",{"title":206,"path":207,"stem":208,"children":209,"icon":211},"Workflows","/workflows","8.workflows/1.index",[210,212,216,221,226],{"title":28,"path":207,"stem":208,"icon":211},"i-ph-flow-arrow",{"title":213,"path":214,"stem":215,"icon":82},"Create a workflow","/workflows/creating-a-workflow","8.workflows/2.creating-a-workflow",{"title":217,"path":218,"stem":219,"icon":220},"Edit sections","/workflows/editing-sections","8.workflows/3.editing-sections","i-ph-pencil-simple",{"title":222,"path":223,"stem":224,"icon":225},"Run and schedule","/workflows/running-and-scheduling","8.workflows/4.running-and-scheduling","i-ph-clock",{"title":227,"path":228,"stem":229,"icon":230},"Workflow directory","/workflows/directory","8.workflows/5.directory","i-ph-compass",{"title":232,"path":233,"stem":234,"children":235,"icon":119},"Agent API","/agent-api","9.agent-api/1.index",[236,237,241,246,251,256],{"title":28,"path":233,"stem":234,"icon":119},{"title":13,"path":238,"stem":239,"icon":240},"/agent-api/quickstart","9.agent-api/2.quickstart","i-ph-rocket-launch",{"title":242,"path":243,"stem":244,"icon":245},"Authentication","/agent-api/authentication","9.agent-api/3.authentication","i-ph-lock-key",{"title":247,"path":248,"stem":249,"icon":250},"Endpoints","/agent-api/endpoints","9.agent-api/4.endpoints","i-ph-list-bullets",{"title":252,"path":253,"stem":254,"icon":255},"Streaming","/agent-api/streaming","9.agent-api/5.streaming","i-ph-broadcast",{"title":257,"path":258,"stem":259,"icon":260},"Errors","/agent-api/errors","9.agent-api/6.errors","i-ph-warning-circle",{"id":262,"title":263,"body":264,"description":1726,"extension":1727,"links":1728,"meta":1729,"navigation":1730,"path":253,"seo":1731,"stem":254,"__hash__":1732},"docs/9.agent-api/5.streaming.md","Streaming replies",{"type":265,"value":266,"toc":1716},"minimark",[267,271,280,290,295,301,312,455,470,512,519,535,539,545,648,653,910,1001,1011,1015,1023,1081,1097,1101,1121,1124,1128,1217,1220,1224,1712],[268,269,263],"h1",{"id":270},"streaming-replies",[272,273,274,275,279],"p",{},"Instead of fetching a chat until its reply is done, open one WebSocket and BetterClaw pushes every\nchange as it happens: new chats, new messages, and replies as they're written. It needs the\n",[276,277,278],"code",{},"chats:read"," permission.",[281,282,287],"pre",{"className":283,"code":285,"language":286},[284],"language-text","wss://api.betterclaw.io/ws/chats?workspaceId={workspaceId}\n","text",[276,288,285],{"__ignoreMap":289},"",[291,292,294],"h2",{"id":293},"connecting","Connecting",[272,296,297,300],{},[276,298,299],{},"workspaceId"," is required and must be the key's workspace.",[272,302,303,307,308,311],{},[304,305,306],"strong",{},"From a server",", send your API key (or a session token) in the ",[276,309,310],{},"Authorization"," header of the\nconnection request:",[281,313,317],{"className":314,"code":315,"language":316,"meta":289,"style":289},"language-js shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","import WebSocket from 'ws';\n\nconst ws = new WebSocket(`wss://api.betterclaw.io/ws/chats?workspaceId=${workspaceId}`, {\n  headers: { Authorization: `Bearer ${process.env.BETTERCLAW_API_KEY}` },\n});\n","js",[276,318,319,349,356,399,444],{"__ignoreMap":289},[320,321,324,328,332,335,339,343,346],"span",{"class":322,"line":323},"line",1,[320,325,327],{"class":326},"s7zQu","import",[320,329,331],{"class":330},"sTEyZ"," WebSocket ",[320,333,334],{"class":326},"from",[320,336,338],{"class":337},"sMK4o"," '",[320,340,342],{"class":341},"sfazB","ws",[320,344,345],{"class":337},"'",[320,347,348],{"class":337},";\n",[320,350,352],{"class":322,"line":351},2,[320,353,355],{"emptyLinePlaceholder":354},true,"\n",[320,357,359,363,366,369,372,376,379,382,385,388,390,393,396],{"class":322,"line":358},3,[320,360,362],{"class":361},"spNyl","const",[320,364,365],{"class":330}," ws ",[320,367,368],{"class":337},"=",[320,370,371],{"class":337}," new",[320,373,375],{"class":374},"s2Zo4"," WebSocket",[320,377,378],{"class":330},"(",[320,380,381],{"class":337},"`",[320,383,384],{"class":341},"wss://api.betterclaw.io/ws/chats?workspaceId=",[320,386,387],{"class":337},"${",[320,389,299],{"class":330},[320,391,392],{"class":337},"}`",[320,394,395],{"class":337},",",[320,397,398],{"class":337}," {\n",[320,400,402,406,409,412,415,417,420,423,425,428,431,434,436,439,441],{"class":322,"line":401},4,[320,403,405],{"class":404},"swJcz","  headers",[320,407,408],{"class":337},":",[320,410,411],{"class":337}," {",[320,413,414],{"class":404}," Authorization",[320,416,408],{"class":337},[320,418,419],{"class":337}," `",[320,421,422],{"class":341},"Bearer ",[320,424,387],{"class":337},[320,426,427],{"class":330},"process",[320,429,430],{"class":337},".",[320,432,433],{"class":330},"env",[320,435,430],{"class":337},[320,437,438],{"class":330},"BETTERCLAW_API_KEY",[320,440,392],{"class":337},[320,442,443],{"class":337}," },\n",[320,445,447,450,453],{"class":322,"line":446},5,[320,448,449],{"class":337},"}",[320,451,452],{"class":330},")",[320,454,348],{"class":337},[272,456,457,460,461,466,467,408],{},[304,458,459],{},"From a browser",", which can't set headers on a WebSocket, pass a\n",[462,463,465],"a",{"href":464},"/agent-api/authentication#session-tokens","session token"," as ",[276,468,469],{},"token",[281,471,473],{"className":314,"code":472,"language":316,"meta":289,"style":289},"const ws = new WebSocket(`wss://api.betterclaw.io/ws/chats?workspaceId=${workspaceId}&token=${token}`);\n",[276,474,475],{"__ignoreMap":289},[320,476,477,479,481,483,485,487,489,491,493,495,497,499,502,504,506,508,510],{"class":322,"line":323},[320,478,362],{"class":361},[320,480,365],{"class":330},[320,482,368],{"class":337},[320,484,371],{"class":337},[320,486,375],{"class":374},[320,488,378],{"class":330},[320,490,381],{"class":337},[320,492,384],{"class":341},[320,494,387],{"class":337},[320,496,299],{"class":330},[320,498,449],{"class":337},[320,500,501],{"class":341},"&token=",[320,503,387],{"class":337},[320,505,469],{"class":330},[320,507,392],{"class":337},[320,509,452],{"class":330},[320,511,348],{"class":337},[272,513,514,515,518],{},"Never put an API key in the URL: the connection is closed straight away (code ",[276,516,517],{},"4003",").",[272,520,521,522,525,526,530,531,534],{},"The stream only sends; there's nothing to subscribe to. You receive events for ",[304,523,524],{},"every chat in the\nworkspace",", or only the pinned agent's chats if the key is\n",[462,527,529],{"href":528},"/agent-api/authentication#pinning-a-key-to-an-agent","pinned",", so filter by ",[276,532,533],{},"chatId"," for the ones you\ncare about. The server pings the connection\nevery 25 seconds; WebSocket libraries and browsers answer those pings for you.",[291,536,538],{"id":537},"events","Events",[272,540,541,542,430],{},"Every event is a JSON text frame with a ",[276,543,544],{},"type",[546,547,548,565],"table",{},[549,550,551],"thead",{},[552,553,554,559,562],"tr",{},[555,556,557],"th",{},[276,558,544],{},[555,560,561],{},"Sent when",[555,563,564],{},"Payload",[566,567,568,582,602,616,635],"tbody",{},[552,569,570,576,579],{},[571,572,573],"td",{},[276,574,575],{},"connected",[571,577,578],{},"The connection is ready.",[571,580,581],{},"—",[552,583,584,589,592],{},[571,585,586],{},[276,587,588],{},"chat_upserted",[571,590,591],{},"A chat is created or changes (title, archive…).",[571,593,594,597,598],{},[276,595,596],{},"chat",": a ",[462,599,601],{"href":600},"/agent-api/endpoints#the-chat-object","chat object",[552,603,604,609,612],{},[571,605,606],{},[276,607,608],{},"chat_deleted",[571,610,611],{},"A chat is deleted.",[571,613,614],{},[276,615,533],{},[552,617,618,623,626],{},[571,619,620],{},[276,621,622],{},"message_upserted",[571,624,625],{},"A message is created or saved.",[571,627,628,630,631,634],{},[276,629,533],{},", ",[276,632,633],{},"message",": a message object",[552,636,637,642,645],{},[571,638,639],{},[276,640,641],{},"message_streaming",[571,643,644],{},"A reply is being written.",[571,646,647],{},"See below",[649,650,651],"h3",{"id":641},[276,652,641],{},[281,654,658],{"className":655,"code":656,"language":657,"meta":289,"style":289},"language-json shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","{\n  \"type\": \"message_streaming\",\n  \"chatId\": \"9a8b7c6d-5e4f-4a3b-2c1d-0e9f8a7b6c5d\",\n  \"messageId\": \"c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7f\",\n  \"content\": \"Here's the draft. Revenue is up 8% week\",\n  \"status\": \"streaming\",\n  \"todos\": [\n    { \"id\": \"1\", \"content\": \"Read numbers.csv\", \"status\": \"completed\" },\n    { \"id\": \"2\", \"content\": \"Write the report\", \"status\": \"in_progress\" }\n  ]\n}\n","json",[276,659,660,665,687,706,726,746,767,782,841,898,904],{"__ignoreMap":289},[320,661,662],{"class":322,"line":323},[320,663,664],{"class":337},"{\n",[320,666,667,670,672,675,677,680,682,684],{"class":322,"line":351},[320,668,669],{"class":337},"  \"",[320,671,544],{"class":361},[320,673,674],{"class":337},"\"",[320,676,408],{"class":337},[320,678,679],{"class":337}," \"",[320,681,641],{"class":341},[320,683,674],{"class":337},[320,685,686],{"class":337},",\n",[320,688,689,691,693,695,697,699,702,704],{"class":322,"line":358},[320,690,669],{"class":337},[320,692,533],{"class":361},[320,694,674],{"class":337},[320,696,408],{"class":337},[320,698,679],{"class":337},[320,700,701],{"class":341},"9a8b7c6d-5e4f-4a3b-2c1d-0e9f8a7b6c5d",[320,703,674],{"class":337},[320,705,686],{"class":337},[320,707,708,710,713,715,717,719,722,724],{"class":322,"line":401},[320,709,669],{"class":337},[320,711,712],{"class":361},"messageId",[320,714,674],{"class":337},[320,716,408],{"class":337},[320,718,679],{"class":337},[320,720,721],{"class":341},"c3d4e5f6-a7b8-4c9d-0e1f-2a3b4c5d6e7f",[320,723,674],{"class":337},[320,725,686],{"class":337},[320,727,728,730,733,735,737,739,742,744],{"class":322,"line":446},[320,729,669],{"class":337},[320,731,732],{"class":361},"content",[320,734,674],{"class":337},[320,736,408],{"class":337},[320,738,679],{"class":337},[320,740,741],{"class":341},"Here's the draft. Revenue is up 8% week",[320,743,674],{"class":337},[320,745,686],{"class":337},[320,747,749,751,754,756,758,760,763,765],{"class":322,"line":748},6,[320,750,669],{"class":337},[320,752,753],{"class":361},"status",[320,755,674],{"class":337},[320,757,408],{"class":337},[320,759,679],{"class":337},[320,761,762],{"class":341},"streaming",[320,764,674],{"class":337},[320,766,686],{"class":337},[320,768,770,772,775,777,779],{"class":322,"line":769},7,[320,771,669],{"class":337},[320,773,774],{"class":361},"todos",[320,776,674],{"class":337},[320,778,408],{"class":337},[320,780,781],{"class":337}," [\n",[320,783,785,788,790,794,796,798,800,803,805,807,809,811,813,815,817,820,822,824,826,828,830,832,834,837,839],{"class":322,"line":784},8,[320,786,787],{"class":337},"    {",[320,789,679],{"class":337},[320,791,793],{"class":792},"sBMFI","id",[320,795,674],{"class":337},[320,797,408],{"class":337},[320,799,679],{"class":337},[320,801,802],{"class":341},"1",[320,804,674],{"class":337},[320,806,395],{"class":337},[320,808,679],{"class":337},[320,810,732],{"class":792},[320,812,674],{"class":337},[320,814,408],{"class":337},[320,816,679],{"class":337},[320,818,819],{"class":341},"Read numbers.csv",[320,821,674],{"class":337},[320,823,395],{"class":337},[320,825,679],{"class":337},[320,827,753],{"class":792},[320,829,674],{"class":337},[320,831,408],{"class":337},[320,833,679],{"class":337},[320,835,836],{"class":341},"completed",[320,838,674],{"class":337},[320,840,443],{"class":337},[320,842,844,846,848,850,852,854,856,859,861,863,865,867,869,871,873,876,878,880,882,884,886,888,890,893,895],{"class":322,"line":843},9,[320,845,787],{"class":337},[320,847,679],{"class":337},[320,849,793],{"class":792},[320,851,674],{"class":337},[320,853,408],{"class":337},[320,855,679],{"class":337},[320,857,858],{"class":341},"2",[320,860,674],{"class":337},[320,862,395],{"class":337},[320,864,679],{"class":337},[320,866,732],{"class":792},[320,868,674],{"class":337},[320,870,408],{"class":337},[320,872,679],{"class":337},[320,874,875],{"class":341},"Write the report",[320,877,674],{"class":337},[320,879,395],{"class":337},[320,881,679],{"class":337},[320,883,753],{"class":792},[320,885,674],{"class":337},[320,887,408],{"class":337},[320,889,679],{"class":337},[320,891,892],{"class":341},"in_progress",[320,894,674],{"class":337},[320,896,897],{"class":337}," }\n",[320,899,901],{"class":322,"line":900},10,[320,902,903],{"class":337},"  ]\n",[320,905,907],{"class":322,"line":906},11,[320,908,909],{"class":337},"}\n",[546,911,912,922],{},[549,913,914],{},[552,915,916,919],{},[555,917,918],{},"Field",[555,920,921],{},"Description",[566,923,924,937,956,966,991],{},[552,925,926,930],{},[571,927,928],{},[276,929,732],{},[571,931,932,933,936],{},"The ",[304,934,935],{},"whole"," reply so far, not just the new text. Replace what you show; to print only new text, slice off what you've already printed.",[552,938,939,943],{},[571,940,941],{},[276,942,753],{},[571,944,945,947,948,951,952,955],{},[276,946,762],{},", then ",[276,949,950],{},"complete"," or ",[276,953,954],{},"error"," on the last frame.",[552,957,958,963],{},[571,959,960],{},[276,961,962],{},"thinking",[571,964,965],{},"The agent's live reasoning, when it shares it. Optional.",[552,967,968,972],{},[571,969,970],{},[276,971,774],{},[571,973,974,975,630,977,979,980,982,983,630,986,630,988,990],{},"The agent's live checklist: ",[276,976,793],{},[276,978,732],{},", and ",[276,981,753],{}," (",[276,984,985],{},"pending",[276,987,892],{},[276,989,836],{},"). Optional.",[552,992,993,998],{},[571,994,995],{},[276,996,997],{},"subagents",[571,999,1000],{},"Progress of helper agents it started for this reply. Optional.",[272,1002,1003,1004,630,1006,979,1008,1010],{},"Frames arrive up to about four times a second. ",[276,1005,962],{},[276,1007,774],{},[276,1009,997],{}," are only\npresent while the reply is still being written.",[291,1012,1014],{"id":1013},"what-one-turn-looks-like","What one turn looks like",[272,1016,1017,1018,1022],{},"After you ",[462,1019,1021],{"href":1020},"/agent-api/endpoints#send-a-message","send a message",", the stream carries:",[1024,1025,1026,1032,1040,1051,1056,1069],"ol",{},[1027,1028,1029,1031],"li",{},[276,1030,588],{}," for the chat, now with new activity.",[1027,1033,1034,1036,1037,518],{},[276,1035,622],{}," for your message (",[276,1038,1039],{},"role: \"user\"",[1027,1041,1042,1044,1045,630,1048,518],{},[276,1043,622],{}," for the empty reply (",[276,1046,1047],{},"role: \"assistant\"",[276,1049,1050],{},"status: \"streaming\"",[1027,1052,1053,1055],{},[276,1054,641],{}," frames as the reply is written.",[1027,1057,1058,1059,1061,1062,1064,1065,951,1067,430],{},"A last ",[276,1060,641],{}," with ",[276,1063,753],{}," ",[276,1066,950],{},[276,1068,954],{},[1027,1070,1071,1073,1074,1077,1078,430],{},[276,1072,622],{}," with the saved reply, including any ",[276,1075,1076],{},"deliverable"," files and ",[276,1079,1080],{},"errorMessage",[272,1082,1083,1084,1087,1088,1090,1091,1093,1094,1096],{},"If the reply fails before the agent starts writing, for example because the agent couldn't start,\nyou get step 6 with ",[276,1085,1086],{},"status: \"error\""," and no ",[276,1089,641],{}," frames. Treat ",[276,1092,622],{},"\nwith a finished ",[276,1095,753],{}," as the reply's final state.",[291,1098,1100],{"id":1099},"reconnecting","Reconnecting",[272,1102,1103,1104,1107,1108,1110,1111,1113,1114,1120],{},"Connections drop. When you reconnect, the stream re-sends replies that are ",[304,1105,1106],{},"still being written","\n(a ",[276,1109,622],{}," followed by the latest ",[276,1112,641],{},"). Anything that finished while\nyou were away is not re-sent, so fetch the chats you're following with\n",[462,1115,1117],{"href":1116},"/agent-api/endpoints#get-a-chat-with-its-messages",[276,1118,1119],{},"GET /chats/{chatId}"," after reconnecting.",[272,1122,1123],{},"An open connection stays open after the session token it used expires. You only need a fresh token\nto reconnect.",[291,1125,1127],{"id":1126},"close-codes","Close codes",[546,1129,1130,1143],{},[549,1131,1132],{},[552,1133,1134,1137,1140],{},[555,1135,1136],{},"Code",[555,1138,1139],{},"Reason",[555,1141,1142],{},"What to do",[566,1144,1145,1160,1174,1194],{},[552,1146,1147,1152,1157],{},[571,1148,1149],{},[276,1150,1151],{},"4001",[571,1153,1154],{},[276,1155,1156],{},"missing-token",[571,1158,1159],{},"Send a key or token. Don't retry without one.",[552,1161,1162,1166,1171],{},[571,1163,1164],{},[276,1165,517],{},[571,1167,1168],{},[276,1169,1170],{},"auth-failed",[571,1172,1173],{},"An API key was sent in the URL, or the server failed while connecting. Fix the request, then retry.",[552,1175,1176,1181,1186],{},[571,1177,1178],{},[276,1179,1180],{},"4004",[571,1182,1183],{},[276,1184,1185],{},"forbidden-workspace",[571,1187,1188,1190,1191,1193],{},[276,1189,299],{}," is missing or not the key's, the key lacks ",[276,1192,278],{},", or its creator left the workspace. Don't retry.",[552,1195,1196,1201,1206],{},[571,1197,1198],{},[276,1199,1200],{},"4005",[571,1202,1203],{},[276,1204,1205],{},"key-revoked",[571,1207,1208,1209,1212,1213,1216],{},"The key was revoked, or the session token is invalid or ",[304,1210,1211],{},"expired",". Get a new session token and reconnect; if that fails with ",[276,1214,1215],{},"401",", the key is gone.",[272,1218,1219],{},"Any other close is a dropped connection: reconnect with a back-off.",[291,1221,1223],{"id":1222},"example-a-browser-chat-stream","Example: a browser chat stream",[281,1225,1227],{"className":314,"code":1226,"language":316,"meta":289,"style":289},"async function openStream(onReply) {\n  // Your own endpoint that mints a session token (see Authentication).\n  const { token, workspaceId } = await (await fetch('/betterclaw-token', { method: 'POST' })).json();\n  const ws = new WebSocket(`wss://api.betterclaw.io/ws/chats?workspaceId=${workspaceId}&token=${token}`);\n\n  ws.onmessage = (e) => {\n    const frame = JSON.parse(e.data);\n    if (frame.type === 'message_streaming') onReply(frame.chatId, frame.messageId, frame.content, frame.status);\n    if (frame.type === 'message_upserted' && frame.message.role === 'assistant') {\n      onReply(frame.chatId, frame.message.id, frame.message.content, frame.message.status);\n    }\n  };\n\n  ws.onclose = (e) => {\n    if (e.code === 4001 || e.code === 4004) return; // won't succeed on retry\n    setTimeout(() => openStream(onReply), 2000); // a fresh token covers 4005 too\n  };\n}\n",[276,1228,1229,1250,1256,1327,1364,1368,1392,1423,1487,1534,1587,1592,1598,1603,1625,1670,1702,1707],{"__ignoreMap":289},[320,1230,1231,1234,1237,1240,1242,1246,1248],{"class":322,"line":323},[320,1232,1233],{"class":361},"async",[320,1235,1236],{"class":361}," function",[320,1238,1239],{"class":374}," openStream",[320,1241,378],{"class":337},[320,1243,1245],{"class":1244},"sHdIc","onReply",[320,1247,452],{"class":337},[320,1249,398],{"class":337},[320,1251,1252],{"class":322,"line":351},[320,1253,1255],{"class":1254},"sHwdD","  // Your own endpoint that mints a session token (see Authentication).\n",[320,1257,1258,1261,1263,1266,1268,1271,1274,1277,1280,1282,1285,1288,1290,1292,1295,1297,1299,1301,1304,1306,1308,1311,1313,1315,1318,1320,1322,1325],{"class":322,"line":358},[320,1259,1260],{"class":361},"  const",[320,1262,411],{"class":337},[320,1264,1265],{"class":330}," token",[320,1267,395],{"class":337},[320,1269,1270],{"class":330}," workspaceId",[320,1272,1273],{"class":337}," }",[320,1275,1276],{"class":337}," =",[320,1278,1279],{"class":326}," await",[320,1281,982],{"class":404},[320,1283,1284],{"class":326},"await",[320,1286,1287],{"class":374}," fetch",[320,1289,378],{"class":404},[320,1291,345],{"class":337},[320,1293,1294],{"class":341},"/betterclaw-token",[320,1296,345],{"class":337},[320,1298,395],{"class":337},[320,1300,411],{"class":337},[320,1302,1303],{"class":404}," method",[320,1305,408],{"class":337},[320,1307,338],{"class":337},[320,1309,1310],{"class":341},"POST",[320,1312,345],{"class":337},[320,1314,1273],{"class":337},[320,1316,1317],{"class":404},"))",[320,1319,430],{"class":337},[320,1321,657],{"class":374},[320,1323,1324],{"class":404},"()",[320,1326,348],{"class":337},[320,1328,1329,1331,1334,1336,1338,1340,1342,1344,1346,1348,1350,1352,1354,1356,1358,1360,1362],{"class":322,"line":401},[320,1330,1260],{"class":361},[320,1332,1333],{"class":330}," ws",[320,1335,1276],{"class":337},[320,1337,371],{"class":337},[320,1339,375],{"class":374},[320,1341,378],{"class":404},[320,1343,381],{"class":337},[320,1345,384],{"class":341},[320,1347,387],{"class":337},[320,1349,299],{"class":330},[320,1351,449],{"class":337},[320,1353,501],{"class":341},[320,1355,387],{"class":337},[320,1357,469],{"class":330},[320,1359,392],{"class":337},[320,1361,452],{"class":404},[320,1363,348],{"class":337},[320,1365,1366],{"class":322,"line":446},[320,1367,355],{"emptyLinePlaceholder":354},[320,1369,1370,1373,1375,1378,1380,1382,1385,1387,1390],{"class":322,"line":748},[320,1371,1372],{"class":330},"  ws",[320,1374,430],{"class":337},[320,1376,1377],{"class":374},"onmessage",[320,1379,1276],{"class":337},[320,1381,982],{"class":337},[320,1383,1384],{"class":1244},"e",[320,1386,452],{"class":337},[320,1388,1389],{"class":361}," =>",[320,1391,398],{"class":337},[320,1393,1394,1397,1400,1402,1405,1407,1410,1412,1414,1416,1419,1421],{"class":322,"line":769},[320,1395,1396],{"class":361},"    const",[320,1398,1399],{"class":330}," frame",[320,1401,1276],{"class":337},[320,1403,1404],{"class":330}," JSON",[320,1406,430],{"class":337},[320,1408,1409],{"class":374},"parse",[320,1411,378],{"class":404},[320,1413,1384],{"class":330},[320,1415,430],{"class":337},[320,1417,1418],{"class":330},"data",[320,1420,452],{"class":404},[320,1422,348],{"class":337},[320,1424,1425,1428,1430,1433,1435,1437,1440,1442,1444,1446,1449,1451,1453,1455,1457,1459,1461,1463,1465,1467,1469,1471,1473,1475,1477,1479,1481,1483,1485],{"class":322,"line":784},[320,1426,1427],{"class":326},"    if",[320,1429,982],{"class":404},[320,1431,1432],{"class":330},"frame",[320,1434,430],{"class":337},[320,1436,544],{"class":330},[320,1438,1439],{"class":337}," ===",[320,1441,338],{"class":337},[320,1443,641],{"class":341},[320,1445,345],{"class":337},[320,1447,1448],{"class":404},") ",[320,1450,1245],{"class":374},[320,1452,378],{"class":404},[320,1454,1432],{"class":330},[320,1456,430],{"class":337},[320,1458,533],{"class":330},[320,1460,395],{"class":337},[320,1462,1399],{"class":330},[320,1464,430],{"class":337},[320,1466,712],{"class":330},[320,1468,395],{"class":337},[320,1470,1399],{"class":330},[320,1472,430],{"class":337},[320,1474,732],{"class":330},[320,1476,395],{"class":337},[320,1478,1399],{"class":330},[320,1480,430],{"class":337},[320,1482,753],{"class":330},[320,1484,452],{"class":404},[320,1486,348],{"class":337},[320,1488,1489,1491,1493,1495,1497,1499,1501,1503,1505,1507,1510,1512,1514,1516,1518,1521,1523,1525,1528,1530,1532],{"class":322,"line":843},[320,1490,1427],{"class":326},[320,1492,982],{"class":404},[320,1494,1432],{"class":330},[320,1496,430],{"class":337},[320,1498,544],{"class":330},[320,1500,1439],{"class":337},[320,1502,338],{"class":337},[320,1504,622],{"class":341},[320,1506,345],{"class":337},[320,1508,1509],{"class":337}," &&",[320,1511,1399],{"class":330},[320,1513,430],{"class":337},[320,1515,633],{"class":330},[320,1517,430],{"class":337},[320,1519,1520],{"class":330},"role",[320,1522,1439],{"class":337},[320,1524,338],{"class":337},[320,1526,1527],{"class":341},"assistant",[320,1529,345],{"class":337},[320,1531,1448],{"class":404},[320,1533,664],{"class":337},[320,1535,1536,1539,1541,1543,1545,1547,1549,1551,1553,1555,1557,1559,1561,1563,1565,1567,1569,1571,1573,1575,1577,1579,1581,1583,1585],{"class":322,"line":900},[320,1537,1538],{"class":374},"      onReply",[320,1540,378],{"class":404},[320,1542,1432],{"class":330},[320,1544,430],{"class":337},[320,1546,533],{"class":330},[320,1548,395],{"class":337},[320,1550,1399],{"class":330},[320,1552,430],{"class":337},[320,1554,633],{"class":330},[320,1556,430],{"class":337},[320,1558,793],{"class":330},[320,1560,395],{"class":337},[320,1562,1399],{"class":330},[320,1564,430],{"class":337},[320,1566,633],{"class":330},[320,1568,430],{"class":337},[320,1570,732],{"class":330},[320,1572,395],{"class":337},[320,1574,1399],{"class":330},[320,1576,430],{"class":337},[320,1578,633],{"class":330},[320,1580,430],{"class":337},[320,1582,753],{"class":330},[320,1584,452],{"class":404},[320,1586,348],{"class":337},[320,1588,1589],{"class":322,"line":906},[320,1590,1591],{"class":337},"    }\n",[320,1593,1595],{"class":322,"line":1594},12,[320,1596,1597],{"class":337},"  };\n",[320,1599,1601],{"class":322,"line":1600},13,[320,1602,355],{"emptyLinePlaceholder":354},[320,1604,1606,1608,1610,1613,1615,1617,1619,1621,1623],{"class":322,"line":1605},14,[320,1607,1372],{"class":330},[320,1609,430],{"class":337},[320,1611,1612],{"class":374},"onclose",[320,1614,1276],{"class":337},[320,1616,982],{"class":337},[320,1618,1384],{"class":1244},[320,1620,452],{"class":337},[320,1622,1389],{"class":361},[320,1624,398],{"class":337},[320,1626,1628,1630,1632,1634,1636,1638,1640,1644,1647,1650,1652,1654,1656,1659,1661,1664,1667],{"class":322,"line":1627},15,[320,1629,1427],{"class":326},[320,1631,982],{"class":404},[320,1633,1384],{"class":330},[320,1635,430],{"class":337},[320,1637,276],{"class":330},[320,1639,1439],{"class":337},[320,1641,1643],{"class":1642},"sbssI"," 4001",[320,1645,1646],{"class":337}," ||",[320,1648,1649],{"class":330}," e",[320,1651,430],{"class":337},[320,1653,276],{"class":330},[320,1655,1439],{"class":337},[320,1657,1658],{"class":1642}," 4004",[320,1660,1448],{"class":404},[320,1662,1663],{"class":326},"return",[320,1665,1666],{"class":337},";",[320,1668,1669],{"class":1254}," // won't succeed on retry\n",[320,1671,1673,1676,1678,1680,1682,1684,1686,1688,1690,1692,1695,1697,1699],{"class":322,"line":1672},16,[320,1674,1675],{"class":374},"    setTimeout",[320,1677,378],{"class":404},[320,1679,1324],{"class":337},[320,1681,1389],{"class":361},[320,1683,1239],{"class":374},[320,1685,378],{"class":404},[320,1687,1245],{"class":330},[320,1689,452],{"class":404},[320,1691,395],{"class":337},[320,1693,1694],{"class":1642}," 2000",[320,1696,452],{"class":404},[320,1698,1666],{"class":337},[320,1700,1701],{"class":1254}," // a fresh token covers 4005 too\n",[320,1703,1705],{"class":322,"line":1704},17,[320,1706,1597],{"class":337},[320,1708,1710],{"class":322,"line":1709},18,[320,1711,909],{"class":337},[1713,1714,1715],"style",{},"html pre.shiki code .s7zQu, html code.shiki .s7zQu{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#89DDFF;--shiki-default-font-style:italic;--shiki-dark:#89DDFF;--shiki-dark-font-style:italic}html pre.shiki code .sTEyZ, html code.shiki .sTEyZ{--shiki-light:#90A4AE;--shiki-default:#EEFFFF;--shiki-dark:#BABED8}html pre.shiki code .sMK4o, html code.shiki .sMK4o{--shiki-light:#39ADB5;--shiki-default:#89DDFF;--shiki-dark:#89DDFF}html pre.shiki code .sfazB, html code.shiki .sfazB{--shiki-light:#91B859;--shiki-default:#C3E88D;--shiki-dark:#C3E88D}html pre.shiki code .spNyl, html code.shiki .spNyl{--shiki-light:#9C3EDA;--shiki-default:#C792EA;--shiki-dark:#C792EA}html pre.shiki code .s2Zo4, html code.shiki .s2Zo4{--shiki-light:#6182B8;--shiki-default:#82AAFF;--shiki-dark:#82AAFF}html pre.shiki code .swJcz, html code.shiki .swJcz{--shiki-light:#E53935;--shiki-default:#F07178;--shiki-dark:#F07178}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sBMFI, html code.shiki .sBMFI{--shiki-light:#E2931D;--shiki-default:#FFCB6B;--shiki-dark:#FFCB6B}html pre.shiki code .sHdIc, html code.shiki .sHdIc{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#EEFFFF;--shiki-default-font-style:italic;--shiki-dark:#BABED8;--shiki-dark-font-style:italic}html pre.shiki code .sHwdD, html code.shiki .sHwdD{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#546E7A;--shiki-default-font-style:italic;--shiki-dark:#676E95;--shiki-dark-font-style:italic}html pre.shiki code .sbssI, html code.shiki .sbssI{--shiki-light:#F76D47;--shiki-default:#F78C6C;--shiki-dark:#F78C6C}",{"title":289,"searchDepth":323,"depth":351,"links":1717},[1718,1719,1722,1723,1724,1725],{"id":293,"depth":351,"text":294},{"id":537,"depth":351,"text":538,"children":1720},[1721],{"id":641,"depth":358,"text":641},{"id":1013,"depth":351,"text":1014},{"id":1099,"depth":351,"text":1100},{"id":1126,"depth":351,"text":1127},{"id":1222,"depth":351,"text":1223},"Watch chats and replies update live over the /ws/chats WebSocket.","md",null,{},{"title":252,"icon":255},{"title":263,"description":1726},"73l6Fu9tRiqS-NtvL-aCEyu5Obe3l3noBHMJQKcCNQ0",[1734,1736],{"title":247,"path":248,"stem":249,"description":1735,"icon":250,"children":-1},"Every Agent API request, its permission, parameters, and response.",{"title":257,"path":258,"stem":259,"description":1737,"icon":260,"children":-1},"Agent API status codes, error bodies, and what to do about each.",1791467821390]