UNPKG

@genkit-ai/ai

Version:

Genkit AI framework generative AI APIs.

175 lines 5.18 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var session_exports = {}; __export(session_exports, { AgentFinishReasonSchema: () => import_agent_types2.AgentFinishReasonSchema, ArtifactSchema: () => import_agent_types2.ArtifactSchema, Session: () => Session, SessionError: () => SessionError, SessionSnapshotSchema: () => import_agent_types2.SessionSnapshotSchema, SessionStateSchema: () => import_agent_types2.SessionStateSchema, assertValidSessionId: () => assertValidSessionId, getCurrentSession: () => getCurrentSession, reserveSnapshotId: () => reserveSnapshotId, runWithSession: () => runWithSession }); module.exports = __toCommonJS(session_exports); var import_core = require("@genkit-ai/core"); var import_async = require("@genkit-ai/core/async"); var import_agent_types2 = require("./agent-types.js"); class Session extends import_async.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 (0, import_core.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 (0, import_core.getAsyncContext)().run("ai.session", session, fn); } function getCurrentSession(registry) { return (0, import_core.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(); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AgentFinishReasonSchema, ArtifactSchema, Session, SessionError, SessionSnapshotSchema, SessionStateSchema, assertValidSessionId, getCurrentSession, reserveSnapshotId, runWithSession }); //# sourceMappingURL=session.js.map