| 118 | return data.content.runId as string; |
| 119 | } |
| 120 | async function retoolGetMessage( |
| 121 | acc: RetoolAccount, |
| 122 | agentId: string, |
| 123 | runId: string, |
| 124 | timeoutMs = 300_000, |
| 125 | ) { |
| 126 | const url = `https://${acc.domain_name}/api/agents/${agentId}/logs/${runId}`; |
| 127 | const deadline = Date.now() + timeoutMs; |
| 128 | while (Date.now() < deadline) { |
| 129 | const r = await fetch(url, { |
| 130 | headers: { |
| 131 | "x-xsrf-token": acc.x_xsrf_token, |
| 132 | "Cookie": `accessToken=${acc.accessToken}`, |
| 133 | Accept: "application/json", |
| 134 | }, |
| 135 | }); |
| 136 | if (!r.ok) throw new Error(`log ${r.status}`); |
| 137 | const data = await r.json(); |
| 138 | if (data.status === "COMPLETED") { |
| 139 | const trace = data.trace; |
| 140 | const last = trace[trace.length - 1]; |
| 141 | return last.data.data.content as string; |
| 142 | } |
| 143 | await new Promise((res) => setTimeout(res, 1_000)); |
| 144 | } |
| 145 | throw new Error("timeout"); |
| 146 | } |
| 147 | |
| 148 | /* ------------------------------------------------------------------------------------------------ |
| 149 | * Misc helpers |