You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Document enhanced stream method with transformer support (#255)
- Add comprehensive stream method documentation to JavaScript SDK API reference
- Include Vercel AI SDK streamObject integration example as requested
- Document function and generator transformer patterns for filtering/transforming stream data
- Add examples showing auto-conversion to JSON for object streams
- Update agent streaming guide with structured object streaming section
- Maintain backward compatibility documentation
- Include error handling patterns and best practices
Addresses PR #155 changes in agentuity/sdk-js for enhanced streaming capabilities.
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: [email protected] <[email protected]>
### Structured Object Streaming with Vercel AI SDK
82
+
83
+
The stream method now supports transformer functions that can filter and transform stream items. This is particularly useful when working with structured data from AI SDKs like Vercel AI SDK's `streamObject`.
84
+
85
+
<CodeExamplejs={`import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
86
+
import { openai } from '@ai-sdk/openai';
87
+
import { streamObject } from 'ai';
88
+
import { z } from 'zod';
89
+
90
+
export default async function Agent(
91
+
req: AgentRequest,
92
+
resp: AgentResponse,
93
+
ctx: AgentContext,
94
+
) {
95
+
const { elementStream } = streamObject({
96
+
model: openai('gpt-4o'),
97
+
output: 'array',
98
+
schema: z.object({
99
+
name: z.string(),
100
+
class: z
101
+
.string()
102
+
.describe('Character class, e.g. warrior, mage, or thief.'),
103
+
description: z.string(),
104
+
}),
105
+
prompt: 'Generate 3 hero descriptions for a fantasy role playing game.',
106
+
});
107
+
108
+
return resp.stream(elementStream);
109
+
}`} />
110
+
111
+
The SDK automatically detects object streams and converts them to JSON newline format with the appropriate `application/json` content type.
112
+
113
+
### Stream Transformers
114
+
115
+
You can provide transformer functions to filter and transform stream data:
116
+
117
+
<CodeExamplejs={`import type { AgentRequest, AgentResponse, AgentContext } from "@agentuity/sdk";
118
+
119
+
export default async function Agent(
120
+
req: AgentRequest,
121
+
resp: AgentResponse,
122
+
ctx: AgentContext,
123
+
) {
124
+
// Get stream from another source
125
+
const dataStream = getDataStream();
126
+
127
+
// Transform and filter items
128
+
const transformer = (item: any) => {
129
+
// Filter out items (return null/undefined to skip)
Copy file name to clipboardExpand all lines: content/SDKs/javascript/api-reference.mdx
+177Lines changed: 177 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,7 @@ This section provides detailed documentation for the Agentuity JavaScript SDK AP
14
14
-[Object Storage](#object-storage)
15
15
-[Agent Communication](#agent-communication)
16
16
-[Response Types](#response-types)
17
+
-[Stream Method](#stream-method)
17
18
-[Request Handling](#request-handling)
18
19
-[Logging](#logging)
19
20
-[Telemetry](#telemetry)
@@ -556,6 +557,182 @@ return response.handoff(
556
557
557
558
The Agentuity SDK provides various methods for creating different types of responses through the `response` object.
558
559
560
+
### Stream Method
561
+
562
+
The `stream` method allows you to stream responses back to the client, supporting both `ReadableStream` and `AsyncIterable` inputs with optional transformer functions for filtering and transforming stream data.
563
+
564
+
#### Method Signature
565
+
566
+
```typescript
567
+
stream<T=unknown, U=T, M=unknown>(
568
+
stream: ReadableStream<T>|AsyncIterable<T>,
569
+
contentType?:string,
570
+
metadata?:M,
571
+
transformer?:
572
+
| ((item:T) =>U|null|undefined)
573
+
| ((item:T) =>Generator<U, void, unknown>)
574
+
): Promise<AgentResponseData>
575
+
```
576
+
577
+
#### Parameters
578
+
579
+
-`stream`: The stream to return - can be a `ReadableStream` or `AsyncIterable`
580
+
-`contentType` (optional): The content type of the stream. If not provided, the SDK will auto-detect based on the stream content
581
+
-`metadata` (optional): Metadata to return as headers
582
+
-`transformer` (optional): Function or generator to transform/filter each stream item
583
+
-**Function transformer**: `(item: T) => U | null | undefined` - returns single value or skips with null/undefined
The stream method automatically detects object streams and converts them to JSON newline format with the appropriate `application/json` content type. This makes it seamless to work with structured data from AI SDKs.
0 commit comments