Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cuddly-lobsters-build.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@workflow/ai": patch
---

DurableAgent#stream now sends `start` and `finish` chunks properly at the start and end
16 changes: 9 additions & 7 deletions packages/ai/src/agent/do-stream-step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ export async function doStreamStep(
conversationPrompt: LanguageModelV2Prompt,
modelInit: string | (() => Promise<LanguageModelV2>),
writable: WritableStream<UIMessageChunk>,
tools?: LanguageModelV2CallOptions['tools']
tools?: LanguageModelV2CallOptions['tools'],
options?: {
sendStart?: boolean;
}
) {
'use step';

Expand Down Expand Up @@ -65,9 +68,11 @@ export async function doStreamStep(
.pipeThrough(
new TransformStream<LanguageModelV2StreamPart, UIMessageChunk>({
start: (controller) => {
controller.enqueue({
type: 'start',
});
if (options?.sendStart) {
controller.enqueue({
type: 'start',
});
}
controller.enqueue({
type: 'start-step',
});
Expand All @@ -76,9 +81,6 @@ export async function doStreamStep(
controller.enqueue({
type: 'finish-step',
});
controller.enqueue({
type: 'finish',
});
},
transform: async (part, controller) => {
const partType = part.type;
Expand Down
42 changes: 38 additions & 4 deletions packages/ai/src/agent/durable-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ export interface DurableAgentStreamOptions {
*/
preventClose?: boolean;

/**
* If true, sends a 'start' chunk at the beginning of the stream.
* Defaults to true.
*/
sendStart?: boolean;

/**
* If true, sends a 'finish' chunk at the end of the stream.
* Defaults to true.
*/
sendFinish?: boolean;

/**
* Condition for stopping the generation when there are tool results in the last step.
* When the condition is an array, any of the conditions can be met to stop the generation.
Expand Down Expand Up @@ -133,6 +145,7 @@ export class DurableAgent {
writable: options.writable,
prompt: modelPrompt,
stopConditions: options.stopWhen,
sendStart: options.sendStart ?? true,
});

let result = await iterator.next();
Expand All @@ -147,16 +160,37 @@ export class DurableAgent {
result = await iterator.next(toolResults);
}

if (!options.preventClose) {
await closeStream(options.writable);
const sendFinish = options.sendFinish ?? true;
const preventClose = options.preventClose ?? false;

// Only call closeStream if there's something to do
if (sendFinish || !preventClose) {
await closeStream(options.writable, preventClose, sendFinish);
}
}
}

async function closeStream(writable: WritableStream<UIMessageChunk>) {
async function closeStream(
writable: WritableStream<UIMessageChunk>,
preventClose?: boolean,
sendFinish?: boolean
) {
'use step';

await writable.close();
// Conditionally write the finish chunk
if (sendFinish) {
const writer = writable.getWriter();
try {
await writer.write({ type: 'finish' });
} finally {
writer.releaseLock();
}
}

// Conditionally close the stream
if (!preventClose) {
await writable.close();
}
}

async function executeTool(
Expand Down
10 changes: 9 additions & 1 deletion packages/ai/src/agent/stream-text-iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ export async function* streamTextIterator({
writable,
model,
stopConditions,
sendStart = true,
}: {
prompt: LanguageModelV2Prompt;
tools: ToolSet;
writable: WritableStream<UIMessageChunk>;
model: string | (() => Promise<LanguageModelV2>);
stopConditions?: ModelStopCondition[] | ModelStopCondition;
sendStart?: boolean;
}): AsyncGenerator<
LanguageModelV2ToolCall[],
void,
Expand All @@ -30,13 +32,19 @@ export async function* streamTextIterator({

const steps: StepResult<any>[] = [];
let done = false;
let isFirstIteration = true;

while (!done) {
const { toolCalls, finish, step } = await doStreamStep(
conversationPrompt,
model,
writable,
toolsToModelTools(tools)
toolsToModelTools(tools),
{
sendStart: sendStart && isFirstIteration,
}
);
isFirstIteration = false;
steps.push(step);

if (finish?.finishReason === 'tool-calls') {
Expand Down
Loading