UNPKG

magnitude-core

Version:
78 lines (77 loc) 2.97 kB
import { Observation, renderObservations } from './observation'; import { observableDataToJson } from './serde'; export class AgentMemory { //public readonly events: EventEmitter<AgentMemoryEvents> = new EventEmitter(); //private options: Required<MemoryOptions>; //private history: StoredHistoryEntry[] = []; // Custom instructions relating to this memory instance (e.g. agent-level and/or task-level instructions) instructions; observations = []; //private tasks: { task: string, observations: Observation[] }[] = []; constructor(instructions) { this.instructions = instructions ?? null; } // get observations(): Observation[] { // if (this.tasks.length === 0) { // } // return this.tasks.at(-1).observations // } // public newTask(task: string): void { // // Mark start of task for a new isolated memory window // } isEmpty() { return this.observations.length === 0; } recordThought(content) { this.observations.push(Observation.fromThought(content)); //this.events.emit('thought', content); } recordObservation(obs) { this.observations.push(obs); } getLastThoughtMessage() { for (let i = this.observations.length - 1; i >= 0; i--) { const obs = this.observations[i]; // toString() is a little funky here, or the idea that thought might not just be text if (obs.source.startsWith('thought')) return obs.toString(); } return null; } async buildContext(activeConnectors) { const content = await renderObservations(this.observations); // TODO: doesn't really make sense for memory to be responsible for current state and instruction render logic const connectorInstructions = []; for (const connector of activeConnectors) { if (connector.getInstructions) { const instructions = await connector.getInstructions(); if (instructions) { connectorInstructions.push({ connectorId: connector.id, instructions: instructions }); } } } return { instructions: this.instructions, observationContent: content, connectorInstructions: connectorInstructions }; } async toJSON() { const observations = []; for (const observation of this.observations) { observations.push({ source: observation.source, timestamp: observation.timestamp, data: await observableDataToJson(observation.data), options: observation.options, }); } return { ...(this.instructions ? { instructions: this.instructions } : {}), observations: observations }; } }