Genkit JS API reference
    Preparing search index...

    Class Session<S>

    Session encapsulates a statful execution environment for chat. Chat session executed within a session in this environment will have acesss to session session convesation history.

    const ai = genkit({...});
    const chat = ai.chat(); // create a Session
    let response = await chat.send('hi'); // session/history aware conversation
    response = await chat.send('tell me a story');

    Type Parameters

    • S = any
    Index

    Constructors

    Properties

    Accessors

    Methods

    Constructors

    • Type Parameters

      • S = any

      Parameters

      Returns Session<S>

    Properties

    id: string
    registry: Registry

    Accessors

    • get state(): S | undefined

      Returns S | undefined

    Methods

    • Create a chat session with the provided options.

      const session = ai.createSession({});
      const chat = session.chat({
      system: 'talk like a pirate',
      })
      let response = await chat.send('tell me a joke');
      response = await chat.send('another one');

      Type Parameters

      • I

      Parameters

      • Optionaloptions: ChatOptions<I, S>

      Returns Chat

    • Create a chat session with the provided preamble.

      const triageAgent = ai.definePrompt({
      system: 'help the user triage a problem',
      })
      const session = ai.createSession({});
      const chat = session.chat(triageAgent);
      const { text } = await chat.send('my phone feels hot');

      Type Parameters

      • I

      Parameters

      Returns Chat

    • Craete a separate chat conversation ("thread") within the given preamble.

      const session = ai.createSession({});
      const lawyerChat = session.chat('lawyerThread', {
      system: 'talk like a lawyer',
      });
      const pirateChat = session.chat('pirateThread', {
      system: 'talk like a pirate',
      });
      await lawyerChat.send('tell me a joke');
      await pirateChat.send('tell me a joke');

      Type Parameters

      • I

      Parameters

      Returns Chat

    • Craete a separate chat conversation ("thread").

      const session = ai.createSession({});
      const lawyerChat = session.chat('lawyerThread', {
      system: 'talk like a lawyer',
      });
      const pirateChat = session.chat('pirateThread', {
      system: 'talk like a pirate',
      });
      await lawyerChat.send('tell me a joke');
      await pirateChat.send('tell me a joke');

      Type Parameters

      • I

      Parameters

      • threadName: string
      • Optionaloptions: ChatOptions<I, S>

      Returns Chat

    • Executes provided function within this session context allowing calling ai.currentSession().state

      Type Parameters

      • O

      Parameters

      • fn: () => O

      Returns O

    • Update messages for a given thread.

      Parameters

      • thread: string
      • messages: {
            content: (
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource?: undefined;
                    text: string;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media: { contentType?: string; url: string };
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource?: undefined;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource?: undefined;
                    text?: undefined;
                    toolRequest: {
                        input?: unknown;
                        name: string;
                        partial?: boolean;
                        ref?: string;
                    };
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource?: undefined;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse: { name: string; output?: unknown; ref?: string } & {
                        content?: (
                            | {
                                custom?: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource?: ...;
                                text: ...;
                                toolRequest?: ...;
                                toolResponse?: ...;
                            }
                            | {
                                custom?: ...;
                                data?: ...;
                                media: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource?: ...;
                                text?: ...;
                                toolRequest?: ...;
                                toolResponse?: ...;
                            }
                            | {
                                custom?: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource?: ...;
                                text?: ...;
                                toolRequest: ...;
                                toolResponse?: ...;
                            }
                            | {
                                custom?: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource?: ...;
                                text?: ...;
                                toolRequest?: ...;
                                toolResponse: ...;
                            }
                            | {
                                custom?: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource?: ...;
                                text?: ...;
                                toolRequest?: ...;
                                toolResponse?: ...;
                            }
                            | {
                                custom: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource?: ...;
                                text?: ...;
                                toolRequest?: ...;
                                toolResponse?: ...;
                            }
                            | {
                                custom?: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning: ...;
                                resource?: ...;
                                text?: ...;
                                toolRequest?: ...;
                                toolResponse?: ...;
                            }
                            | {
                                custom?: ...;
                                data?: ...;
                                media?: ...;
                                metadata?: ...;
                                reasoning?: ...;
                                resource: ...;
                                text?: ...;
                                toolRequest?: ...;
                                toolResponse?: ...;
                            }
                        )[];
                    };
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource?: undefined;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom: Record<string, any>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource?: undefined;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning: string;
                    resource?: undefined;
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
                | {
                    custom?: Record<string, unknown>;
                    data?: unknown;
                    media?: undefined;
                    metadata?: Record<string, unknown>;
                    reasoning?: undefined;
                    resource: { uri: string };
                    text?: undefined;
                    toolRequest?: undefined;
                    toolResponse?: undefined;
                }
            )[];
            metadata?: Record<string, unknown>;
            role: "user" | "model" | "tool" | "system";
        }[]

      Returns Promise<void>

    • Update session state data by patching the existing state.

      Parameters

      • data: Partial<S>

        Partial state update that will be merged with existing state

      Returns Promise<void>