UNPKG

@genkit-ai/ai

Version:

Genkit AI framework generative AI APIs.

147 lines 3.68 kB
import { getAsyncContext } from "@genkit-ai/core"; import { EventEmitter } from "@genkit-ai/core/async"; import { AgentFinishReasonSchema, ArtifactSchema, SessionSnapshotSchema, SessionStateSchema } from "./agent-types.mjs"; class Session extends EventEmitter { state; version = 0; /** Stable identifier that correlates traces across agent turns. */ sessionId; constructor(initialState) { super(); const state = structuredClone(initialState); this.sessionId = state.sessionId || globalThis.crypto.randomUUID(); state.sessionId = this.sessionId; this.state = state; } /** * Returns a deep copy of the current session state. */ getState() { return structuredClone(this.state); } /** * Retrieves all messages associated with the session. * * Returns a copy so callers cannot mutate the session's internal message * array in place. */ getMessages() { return structuredClone(this.state.messages || []); } /** * Appends a list of messages to the session. */ addMessages(messages) { this.state.messages = [...this.state.messages || [], ...messages]; this.version++; } /** * Overwrites the session messages. */ setMessages(messages) { this.state.messages = messages; this.version++; } /** * Retrieves the custom state of the session. */ getCustom() { return this.state.custom; } /** * Updates the custom state of the session using a mutator function. */ updateCustom(fn) { this.state.custom = fn(this.state.custom); this.version++; this.emit("customChanged"); } /** * Retrieves the list of artifacts generated during the session. */ getArtifacts() { return this.state.artifacts || []; } /** * Adds artifacts to the session, deduplicating items by name. * Emits 'artifactAdded' for new artifacts and 'artifactUpdated' for replacements. */ addArtifacts(artifacts) { const existing = this.state.artifacts || []; const added = []; const updated = []; for (const a of artifacts) { if (a.name) { const idx = existing.findIndex((e) => e.name === a.name); if (idx >= 0) { existing[idx] = a; updated.push(a); continue; } } existing.push(a); added.push(a); } this.state.artifacts = existing; if (added.length + updated.length > 0) { this.version++; } for (const a of added) { this.emit("artifactAdded", a); } for (const a of updated) { this.emit("artifactUpdated", a); } } /** * Runs the provided function inside the session's context. */ run(fn) { return getAsyncContext().run("ai.session", this, fn); } /** * Gets the current mutation version of the session state. */ getVersion() { return this.version; } } function runWithSession(registry, session, fn) { return getAsyncContext().run("ai.session", session, fn); } function getCurrentSession(registry) { return getAsyncContext().getStore("ai.session"); } class SessionError extends Error { constructor(msg) { super(msg); } } function assertValidSessionId(sessionId) { if (typeof sessionId !== "string" || sessionId.trim() === "") { throw new Error( `Invalid sessionId: expected a non-empty string, got "${sessionId}".` ); } } function reserveSnapshotId() { return globalThis.crypto.randomUUID(); } export { AgentFinishReasonSchema, ArtifactSchema, Session, SessionError, SessionSnapshotSchema, SessionStateSchema, assertValidSessionId, getCurrentSession, reserveSnapshotId, runWithSession }; //# sourceMappingURL=session.mjs.map