@@ -15,15 +15,8 @@ import type {
1515 StreamTextResult ,
1616 ToolChoice ,
1717 ToolSet ,
18- UIMessage as AIUIMessage ,
19- } from "ai" ;
20- import {
21- generateObject ,
22- generateText ,
23- stepCountIs ,
24- streamObject ,
25- streamText ,
2618} from "ai" ;
19+ import { generateObject , generateText , stepCountIs , streamObject } from "ai" ;
2720import { assert , omit , pick } from "convex-helpers" ;
2821import {
2922 internalActionGeneric ,
@@ -70,13 +63,7 @@ import {
7063 generateAndSaveEmbeddings ,
7164} from "./search.js" ;
7265import { startGeneration } from "./start.js" ;
73- import {
74- compressUIMessageChunks ,
75- DeltaStreamer ,
76- mergeTransforms ,
77- syncStreams ,
78- type StreamingOptions ,
79- } from "./streaming.js" ;
66+ import { syncStreams , type StreamingOptions } from "./streaming.js" ;
8067import { createThread , getThreadMetadata } from "./threads.js" ;
8168import type {
8269 ActionCtx ,
@@ -100,6 +87,8 @@ import type {
10087 QueryCtx ,
10188 AgentPrompt ,
10289} from "./types.js" ;
90+ import { streamText } from "./streamText.js" ;
91+ import { errorToString , willContinue } from "./utils.js" ;
10392
10493export { stepCountIs } from "ai" ;
10594export {
@@ -550,97 +539,27 @@ export class Agent<
550539 > &
551540 GenerationOutputMetadata
552541 > {
553- const { threadId } = threadOpts ;
554- const { args, userId, order, stepOrder, promptMessageId, ...call } =
555- await this . start ( ctx , streamTextArgs , { ...threadOpts , ...options } ) ;
556-
557542 type Tools = TOOLS extends undefined ? AgentTools : TOOLS ;
558- const steps : StepResult < Tools > [ ] = [ ] ;
559-
560- const opts = { ...this . options , ...options } ;
561- const streamer =
562- threadId && opts . saveStreamDeltas
563- ? new DeltaStreamer (
564- this . component ,
565- ctx ,
566- {
567- throttleMs :
568- typeof opts . saveStreamDeltas === "object"
569- ? opts . saveStreamDeltas . throttleMs
570- : undefined ,
571- onAsyncAbort : call . fail ,
572- compress : compressUIMessageChunks ,
573- abortSignal : args . abortSignal ,
574- } ,
575- {
576- threadId,
577- userId,
578- agentName : this . options . name ,
579- model : getModelName ( args . model ) ,
580- provider : getProviderName ( args . model ) ,
581- providerOptions : args . providerOptions ,
582- format : "UIMessageChunk" ,
583- order,
584- stepOrder,
585- } ,
586- )
587- : undefined ;
588-
589- const result = streamText ( {
590- ...args ,
591- abortSignal : streamer ?. abortController . signal ?? args . abortSignal ,
592- experimental_transform : mergeTransforms (
593- options ?. saveStreamDeltas ,
594- streamTextArgs . experimental_transform ,
595- ) ,
596- onError : async ( error ) => {
597- console . error ( "onError" , error ) ;
598- await call . fail ( errorToString ( error . error ) ) ;
599- await streamer ?. fail ( errorToString ( error . error ) ) ;
600- return streamTextArgs . onError ?.( error ) ;
601- } ,
602- prepareStep : async ( options ) => {
603- const result = await streamTextArgs . prepareStep ?.( options ) ;
604- if ( result ) {
605- const model = result . model ?? options . model ;
606- call . updateModel ( model ) ;
607- // streamer?.updateMetadata({
608- // model: getModelName(model),
609- // provider: getProviderName(model),
610- // providerOptions: options.messages.at(-1)?.providerOptions,
611- // });
612- return result ;
613- }
614- return undefined ;
543+ return streamText < Tools , OUTPUT , PARTIAL_OUTPUT > (
544+ ctx ,
545+ this . component ,
546+ {
547+ ...streamTextArgs ,
548+ model : streamTextArgs . model ?? this . options . languageModel ,
549+ tools : ( streamTextArgs . tools ?? this . options . tools ) as Tools ,
550+ system : streamTextArgs . system ?? this . options . instructions ,
551+ stopWhen : ( streamTextArgs . stopWhen ?? this . options . stopWhen ) as
552+ | StopCondition < Tools >
553+ | Array < StopCondition < Tools > > ,
615554 } ,
616- onStepFinish : async ( step ) => {
617- steps . push ( step ) ;
618- const createPendingMessage = await willContinue ( steps , args . stopWhen ) ;
619- await call . save ( { step } , createPendingMessage ) ;
620- return args . onStepFinish ?.( step ) ;
555+ {
556+ ...threadOpts ,
557+ ...this . options ,
558+ agentName : this . options . name ,
559+ agentForToolCtx : this ,
560+ ...options ,
621561 } ,
622- } ) as StreamTextResult <
623- TOOLS extends undefined ? AgentTools : TOOLS ,
624- PARTIAL_OUTPUT
625- > ;
626- const stream = streamer ?. consumeStream (
627- result . toUIMessageStream < AIUIMessage < Tools > > ( ) ,
628562 ) ;
629- if (
630- ( typeof options ?. saveStreamDeltas === "object" &&
631- ! options . saveStreamDeltas . returnImmediately ) ||
632- options ?. saveStreamDeltas === true
633- ) {
634- await stream ;
635- await result . consumeStream ( ) ;
636- }
637- const metadata : GenerationOutputMetadata = {
638- promptMessageId,
639- order,
640- savedMessages : call . getSavedMessages ( ) ,
641- messageId : promptMessageId ,
642- } ;
643- return Object . assign ( result , metadata ) ;
644563 }
645564
646565 /**
@@ -1569,29 +1488,3 @@ export class Agent<
15691488 } ) ;
15701489 }
15711490}
1572-
1573- async function willContinue (
1574- steps : StepResult < any > [ ] ,
1575-
1576- stopWhen : StopCondition < any > | Array < StopCondition < any > > | undefined ,
1577- ) : Promise < boolean > {
1578- const step = steps . at ( - 1 ) ! ;
1579- // we aren't doing another round after a tool result
1580- // TODO: whether to handle continuing after too much context used..
1581- if ( step . finishReason !== "tool-calls" ) return false ;
1582- // we don't have a tool result, so we'll wait for more
1583- if ( step . toolCalls . length > step . toolResults . length ) return false ;
1584- if ( Array . isArray ( stopWhen ) ) {
1585- return ( await Promise . all ( stopWhen . map ( async ( s ) => s ( { steps } ) ) ) ) . every (
1586- ( stop ) => ! stop ,
1587- ) ;
1588- }
1589- return ! ! stopWhen && ! ( await stopWhen ( { steps } ) ) ;
1590- }
1591-
1592- function errorToString ( error : unknown ) : string {
1593- if ( error instanceof Error ) {
1594- return error . message ;
1595- }
1596- return String ( error ) ;
1597- }
0 commit comments