• Utility function to get both streaming text and the final event.

    This helper yields text deltas during streaming and returns the final complete Event object at the end. Useful when you want to stream text but also need access to metadata like usage stats and tool calls.

    Parameters

    • events: AsyncIterable<Event, any, any>

      AsyncIterable of Event objects from runner.runAsync()

    Returns {
        textStream: AsyncGenerator<string, void, unknown>;
        finalEvent: Promise<Event | undefined>;
    }

    Object with textStream AsyncGenerator and finalEvent Promise

    • textStream: AsyncGenerator<string, void, unknown>
    • finalEvent: Promise<Event | undefined>
    import { streamTextWithFinalEvent } from '@iqai/adk/utils';

    const events = runner.runAsync({
    userId: 'user-123',
    sessionId: 'session-456',
    newMessage: { role: 'user', parts: [{ text: 'Calculate 2+2' }] }
    });

    const { textStream, finalEvent } = streamTextWithFinalEvent(events);

    // Stream the text as it arrives
    for await (const text of textStream) {
    process.stdout.write(text);
    }

    // Get the final event with metadata
    const final = await finalEvent;
    console.log('Tokens used:', final.usageMetadata?.totalTokens);
    console.log('Tool calls:', final.getFunctionCalls());