Skip to content

Commit

Permalink
updating save-this index.ts fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
metakai1 committed Dec 31, 2024
1 parent afdf27e commit 12c89b4
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions packages/plugin-save-this/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Plugin, AgentRuntime, knowledge, stringToUuid, generateText, settings, Action, elizaLogger, MemoryManager, EvaluationExample } from "@ai16z/eliza";
import { Plugin, AgentRuntime, knowledge, stringToUuid, generateText, settings, Action, elizaLogger, MemoryManager, EvaluationExample, Content } from "@ai16z/eliza";
import type { KnowledgeItem } from "@ai16z/eliza";

import {
Expand Down Expand Up @@ -27,7 +27,10 @@ const saveThisAction: Action = {
) => {
try {

elizaLogger.info(state.recentMessages);
if (!state) {
elizaLogger.error("[SaveThisAction] handler.start - no state");
return state;
}

// Only proceed if explicitly requested via state
if (!state?.shouldSave) {
Expand All @@ -47,31 +50,21 @@ const saveThisAction: Action = {
.map(msg => msg.content.text)
.join("\n\n");

if (callback) {
await callback({
text: "Summary in progress...",
content: {
text: "Summary in progress..."
}
}, []);
} else {
elizaLogger.error('[SaveThisAction] No callback');
}

elizaLogger.info("Recent messages:", recentMessagesText);

const saveKnowledge = await generateText({
runtime,
context: `\
The following messages are from a conversation between an ai agent and a user.
The most recent 7 messages are sent to this query, ordered from oldest to newest. Some messages from
the user may be included. The last message is the "save this" request from the user, which is then triggers this query. Realize that conversation history may include agent responses \
from previous user queries. You should determine by the flow of conversation what
information the user is wanting to save. Prioritize saving the most recent agent response.
the user may be included. The last message is the "save this" request from the user, which is then triggers this query.
Realize that conversation history may include agent responses from previous user queries.
You should determine by the flow of conversation what information the user is wanting to save.
Prioritize saving the most recent agent response.
The user my also append additional words to the "save this" request, which may be relevant in
The user may also append additional words to the "save this" request, which may be relevant in
deciding what information for you to save. For example, he/she could say "save this information about cars".
By those words, you can determine what information the wants to focus on.
By those words, you can determine what information the user wants to focus on.
Save instructions: Do not store information about the user. Focus on saving knowledge, not conversation
history. Don't save what the conversation is about, but rather the facts and details contained in the
Expand All @@ -96,12 +89,12 @@ ${recentMessagesText}`,

await knowledge.set(runtime as AgentRuntime, memoryToSave);

// TODO: callback is undefined. Need to receive callback from Provider.
if (callback) {
callback({
// text: `I've stored this information: "${saveKnowledge}"`,
await callback({
text: `I've stored the information for you`,
});
return;
type: "text"
},
}

} catch (error) {
Expand All @@ -122,20 +115,29 @@ ${recentMessagesText}`,
};

export const saveThisProvider: Provider = {
get: async (runtime: IAgentRuntime, message: Memory, state?: State) => {
get: async (runtime: IAgentRuntime, message: Memory,
state?: State, callback?: HandlerCallback ) => {
const text = message.content?.text?.toLowerCase() || '';

// Trigger if message starts with "save this"
if (text.trim().startsWith('save this')) {
// Modify state in place first
/* if (state) {
if (state) {
state.shouldSave = true;
}

if(!state.shouldSave) {
elizaLogger.error('saveThisProvider: state.shouldSave is faised');
}
*/

//elizaLogger.info(state.recentMessages);
elizaLogger.info("saveThisProvider: state.shouldSave", state.shouldSave);

// need to figure out how to pass a callback into runtime.processActions
//const mycallback = async (response: Content, files?: Memory[]): Promise<Memory[]> => {
// to be implemented
//};

// Then trigger the SAVE_THIS action
await runtime.processActions(message, [{
id: stringToUuid(`save_this_response_${Date.now()}`),
Expand All @@ -145,8 +147,11 @@ export const saveThisProvider: Provider = {
content: {
action: 'SAVE_THIS',
text: 'Saving previous message...'
}
}]);
},

}], state=state,
//callback=mycallback figure out what this should be
);
}

return;
Expand Down

0 comments on commit 12c89b4

Please sign in to comment.