UNPKG

@microsoft/teams-ai

Version:

SDK focused on building AI based applications for Microsoft Teams.

109 lines 3.98 kB
"use strict"; /** * @module teams-ai */ /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.MemoryFork = void 0; /** * @private */ const TEMP_SCOPE = 'temp'; /** * Forks an existing memory. * @remarks * A memory fork is a memory that is a copy of another memory, but can be modified without * affecting the original memory. */ class MemoryFork { _fork = {}; _memory; /** * Creates a new `MemoryFork` instance. * @param {Memory} memory Memory to fork. */ constructor(memory) { this._memory = memory; } /** * Deletes a value from the memory. * @remarks * Only forked values will be deleted. * @param {string} path Path to the value to delete in the form of `[scope].property`. If scope is omitted, the value is deleted from the temporary scope. */ deleteValue(path) { const { scope, name } = this.getScopeAndName(path); if (Object.prototype.hasOwnProperty.call(this._fork, scope) && Object.prototype.hasOwnProperty.call(this._fork[scope], name)) { delete this._fork[scope][name]; } } /** * Checks if a value exists in the memory. * @remarks * The forked memory is checked first, then the original memory. * @param {string} path Path to the value to check in the form of `[scope].property`. If scope is omitted, the value is checked in the temporary scope. * @returns {boolean} True if the value exists, false otherwise. */ hasValue(path) { const { scope, name } = this.getScopeAndName(path); if (Object.prototype.hasOwnProperty.call(this._fork, scope)) { return Object.prototype.hasOwnProperty.call(this._fork[scope], name); } else { return this._memory.hasValue(path); } } /** * Retrieves a value from the memory. * @template TValue Type of the value to retrieve. * @remarks * The forked memory is checked first, then the original memory. * @param {string} path Path to the value to retrieve in the form of `[scope].property`. If scope is omitted, the value is retrieved from the temporary scope. * @returns {TValue | undefined} The value or undefined if not found. */ getValue(path) { const { scope, name } = this.getScopeAndName(path); if (Object.prototype.hasOwnProperty.call(this._fork, scope)) { if (Object.prototype.hasOwnProperty.call(this._fork[scope], name)) { return this._fork[scope][name]; } } return this._memory.getValue(path); } /** * Assigns a value to the memory. * @remarks * The value is assigned to the forked memory. * @param {string} path Path to the value to assign in the form of `[scope].property`. If scope is omitted, the value is assigned to the temporary scope. * @param {unknown} value Value to assign. */ setValue(path, value) { const { scope, name } = this.getScopeAndName(path); if (!Object.prototype.hasOwnProperty.call(this._fork, scope)) { this._fork[scope] = {}; } this._fork[scope][name] = value; } /** * @private * @param {string} path Path to the value to check in the form of `[scope].property`. If scope is omitted, the value is checked in the temporary scope. * @returns {Record<string, string>} Scope and name. */ getScopeAndName(path) { // Get variable scope and name const parts = path.split('.'); if (parts.length > 2) { throw new Error(`Invalid state path: ${path}`); } else if (parts.length === 1) { parts.unshift(TEMP_SCOPE); } return { scope: parts[0], name: parts[1] }; } } exports.MemoryFork = MemoryFork; //# sourceMappingURL=MemoryFork.js.map