import { z } from '@genkit-ai/core'; import { e as GenerateOptions, E as ExecutablePrompt, f as GenerateStreamOptions, h as GenerateStreamResponse } from './generate-vJnagx8l.js'; import { MessageData, GenerationCommonConfigSchema, Part } from './model-types.js'; import { Registry } from '@genkit-ai/core/registry'; import { GenerateResponse } from './generate/response.js'; import './document-Batw8a-E.js'; import './generate/chunk.js'; import './model-B2GL_8eB.js'; import './formats/types.js'; import './message.js'; import './resource.js'; /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ type BaseGenerateOptions = Omit, 'prompt'>; interface SessionOptions { /** Session store implementation for persisting the session state. */ store?: SessionStore; /** Initial state of the session. */ initialState?: S; /** Custom session Id. */ sessionId?: string; } /** * 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. * * ```ts * 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'); * ``` */ declare class Session { readonly registry: Registry; readonly id: string; private sessionData?; private store; constructor(registry: Registry, options?: { id?: string; stateSchema?: S; sessionData?: SessionData; store?: SessionStore; }); get state(): S | undefined; /** * Update session state data. */ updateState(data: S): Promise; /** * Update messages for a given thread. */ updateMessages(thread: string, messages: MessageData[]): Promise; /** * Create a chat session with the provided options. * * ```ts * 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'); * ``` */ chat(options?: ChatOptions): Chat; /** * Create a chat session with the provided preamble. * * ```ts * 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'); * ``` */ chat(preamble: ExecutablePrompt, options?: ChatOptions): Chat; /** * Craete a separate chat conversation ("thread") within the given preamble. * * ```ts * 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'); * ``` */ chat(threadName: string, preamble: ExecutablePrompt, options?: ChatOptions): Chat; /** * Craete a separate chat conversation ("thread"). * * ```ts * 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'); * ``` */ chat(threadName: string, options?: ChatOptions): Chat; /** * Executes provided function within this session context allowing calling * `ai.currentSession().state` */ run(fn: () => O): O; toJSON(): SessionData | undefined; } interface SessionData { id: string; state?: S; threads?: Record; } /** * Executes provided function within the provided session state. */ declare function runWithSession(registry: Registry, session: Session, fn: () => O): O; /** Returns the current session. */ declare function getCurrentSession(registry: Registry): Session | undefined; /** Throw when session state errors occur, ex. missing state, etc. */ declare class SessionError extends Error { constructor(msg: string); } /** Session store persists session data such as state and chat messages. */ interface SessionStore { get(sessionId: string): Promise | undefined>; save(sessionId: string, data: Omit, 'id'>): Promise; } declare function inMemorySessionStore(): InMemorySessionStore; declare class InMemorySessionStore implements SessionStore { private data; get(sessionId: string): Promise | undefined>; save(sessionId: string, sessionData: SessionData): Promise; } /** * Copyright 2024 Google LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ declare const MAIN_THREAD = "main"; declare const SESSION_ID_ATTR: string; declare const THREAD_NAME_ATTR: string; type ChatGenerateOptions = GenerateOptions; interface PromptRenderOptions { input?: I; } type ChatOptions = (PromptRenderOptions | BaseGenerateOptions) & { store?: SessionStore; sessionId?: string; }; /** * Chat encapsulates a statful execution environment for chat. * Chat session executed within a session in this environment will have acesss to * session convesation history. * * ```ts * const ai = genkit({...}); * const chat = ai.chat(); // create a Chat * let response = await chat.send('hi, my name is Genkit'); * response = await chat.send('what is my name?'); // chat history aware conversation * ``` */ declare class Chat { readonly session: Session; private requestBase?; readonly sessionId: string; private _messages?; private threadName; constructor(session: Session, requestBase: Promise, options: { id: string; thread: string; messages?: MessageData[]; }); send(options: string | Part[] | ChatGenerateOptions): Promise>>; sendStream(options: string | Part[] | GenerateStreamOptions): GenerateStreamResponse>; get messages(): MessageData[]; private updateMessages; } export { type BaseGenerateOptions, type ChatGenerateOptions as C, MAIN_THREAD as M, type PromptRenderOptions as P, SESSION_ID_ATTR as S, Session, type SessionData, SessionError, type SessionOptions, type SessionStore, THREAD_NAME_ATTR as T, type ChatOptions as a, Chat as b, getCurrentSession, inMemorySessionStore, runWithSession };