@mastra/client-js 1.15.2-alpha.0 → 1.15.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1557,6 +1557,51 @@ var Agent = class extends BaseResource {
1557
1557
  };
1558
1558
  return streamResponse;
1559
1559
  }
1560
+ /**
1561
+ * Observe (reconnect to) an existing agent stream.
1562
+ * Use this to resume receiving events after a disconnection.
1563
+ *
1564
+ * @param params.runId - The run ID to observe
1565
+ * @param params.offset - Optional position to resume from (0-based). If omitted, replays all events.
1566
+ * @returns Promise containing a streaming Response
1567
+ *
1568
+ * @example
1569
+ * ```typescript
1570
+ * // Reconnect to a stream from a specific position
1571
+ * const response = await client.agents('my-agent').observe({
1572
+ * runId: 'run-123',
1573
+ * offset: 42, // Resume from event 42
1574
+ * });
1575
+ *
1576
+ * await response.processDataStream({
1577
+ * onChunk: (chunk) => console.log('Received:', chunk),
1578
+ * });
1579
+ * ```
1580
+ */
1581
+ async observe(params) {
1582
+ const response = await this.request(`/agents/${this.agentId}/observe`, {
1583
+ method: "POST",
1584
+ body: params,
1585
+ stream: true
1586
+ });
1587
+ if (!response.body) {
1588
+ throw new Error("No response body");
1589
+ }
1590
+ const streamResponse = new Response(response.body, {
1591
+ status: response.status,
1592
+ statusText: response.statusText,
1593
+ headers: response.headers
1594
+ });
1595
+ streamResponse.processDataStream = async ({
1596
+ onChunk
1597
+ }) => {
1598
+ await processMastraStream({
1599
+ stream: streamResponse.body,
1600
+ onChunk
1601
+ });
1602
+ };
1603
+ return streamResponse;
1604
+ }
1560
1605
  /**
1561
1606
  * Resumes a suspended agent stream with custom resume data.
1562
1607
  * Used to continue execution after a suspension point (e.g., workflow suspend within an agent).
@@ -2310,19 +2355,32 @@ var Run = class extends BaseResource {
2310
2355
  return response.body.pipeThrough(this.createChunkTransformStream());
2311
2356
  }
2312
2357
  /**
2313
- * Observes workflow stream for a workflow run
2314
- * @returns Promise containing the workflow execution results
2358
+ * Observe (reconnect to) an existing workflow stream.
2359
+ * Use this to resume receiving events after a disconnection.
2360
+ *
2361
+ * @param params.offset - Optional position to resume from (0-based). If omitted, replays all events.
2362
+ * @returns Promise containing a ReadableStream of workflow events
2363
+ *
2364
+ * @example
2365
+ * ```typescript
2366
+ * // Reconnect to a workflow stream from a specific position
2367
+ * const stream = await run.observe({ offset: 42 });
2368
+ *
2369
+ * for await (const event of stream) {
2370
+ * console.log('Received:', event);
2371
+ * }
2372
+ * ```
2315
2373
  */
2316
- async observeStream() {
2374
+ async observe(params) {
2317
2375
  const searchParams = new URLSearchParams();
2318
2376
  searchParams.set("runId", this.runId);
2319
- const response = await this.request(
2320
- `/workflows/${this.workflowId}/observe-stream?${searchParams.toString()}`,
2321
- {
2322
- method: "POST",
2323
- stream: true
2324
- }
2325
- );
2377
+ if (params?.offset !== void 0) {
2378
+ searchParams.set("offset", String(params.offset));
2379
+ }
2380
+ const response = await this.request(`/workflows/${this.workflowId}/observe?${searchParams.toString()}`, {
2381
+ method: "POST",
2382
+ stream: true
2383
+ });
2326
2384
  if (!response.ok) {
2327
2385
  throw new Error(`Failed to observe workflow stream: ${response.statusText}`);
2328
2386
  }
@@ -2331,6 +2389,14 @@ var Run = class extends BaseResource {
2331
2389
  }
2332
2390
  return response.body.pipeThrough(this.createChunkTransformStream());
2333
2391
  }
2392
+ /**
2393
+ * Observes workflow stream for a workflow run
2394
+ * @deprecated Use `observe()` instead for better control over replay position
2395
+ * @returns Promise containing the workflow execution results
2396
+ */
2397
+ async observeStream() {
2398
+ return this.observe();
2399
+ }
2334
2400
  /**
2335
2401
  * Resumes a suspended workflow step asynchronously and returns a promise that resolves when the workflow is complete
2336
2402
  * @param params - Object containing the step, resumeData and requestContext