Skip to content

Commit

Permalink
Fix relationships, fix messages storing
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Feb 7, 2024
1 parent 5792142 commit b73a9a3
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 36 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"scripts": {
"dev": "workspaces-run --parallel -- npm run dev",
"dev:terminal": "cd packages/terminal && yarn run dev",
"build": "npm run build --workspaces --if-present",
"test": "npm test --workspaces --if-present",
"coverage": "npm run coverage --workspaces --if-present",
Expand Down
51 changes: 43 additions & 8 deletions packages/agent/src/agent/actions/profile.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk'
import {parseJSONObjectFromText} from '../utils'
import {composeContext} from '../../lib/context'
import {createRelationship} from '../../lib/relationships'
import { Memory } from '@/lib'

const template = `You are writing a profile for {{senderName}} based on their existing profile and ongoing conversations.
Expand All @@ -24,9 +24,12 @@ Your response must include the JSON block.`

const handler = async (message: any, state: any, runtime: any) => {
console.log('profile update message', message)
// TODO:
// first, evaluate if the profile should be updated

console.log('state is', state)

//

// TODO:
// get the target from the message

// then, search for the user in the actors
Expand Down Expand Up @@ -54,6 +57,7 @@ const handler = async (message: any, state: any, runtime: any) => {

if (parsedResponse) {
responseData = parsedResponse
console.log('got response', responseData)
break
}

Expand All @@ -62,12 +66,43 @@ const handler = async (message: any, state: any, runtime: any) => {
}
}

if (responseData && responseData.userA && responseData.userB) {
await createRelationship({
supabase: runtime.supabase,
userA: responseData.userA,
userB: responseData.userB,
if (responseData) {
const { user, description } = responseData;

// find the user
const response = await runtime.supabase.from('accounts').select('*').eq('name', user).single()
const { data: userRecord, error } = response;
if(error) {
console.error('error getting user', error)
return
}

const userA = state.agentId;
const userB = userRecord.id;

// find the room_id in 'relationships' where user_a is the agent and user_b is the user, OR vice versa
const response2 = await runtime.supabase.from("relationships").select("*")
.or(`user_a.eq.${userA},user_b.eq.${userB},user_a.eq.${userB},user_b.eq.${userA}`)
.single();
const { data: relationshipRecord, error: error2 } = response2;
if(error2) {
console.error('error getting relationship', error2)
return
}

console.log('relationshipRecord is', relationshipRecord)

console.log('userRecord is', userRecord)

const descriptionMemory = new Memory({
user_ids: [state.agentId, userRecord.id],
user_id: state.agentId,
content: description,
room_id: relationshipRecord.room_id,
})
console.log('descriptionMemory', descriptionMemory.toJSON())
await runtime.descriptionManager.upsertRawMemory(descriptionMemory);
console.log('stored descriptionMemory', descriptionMemory)
} else if (runtime.debugMode) {
console.log(chalk.red('Could not parse response'))
}
Expand Down
15 changes: 12 additions & 3 deletions packages/agent/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,24 @@ import {
} from '../lib'
import {defaultActions} from '../lib/actions'
import {formatGoalsAsString, getGoals} from '../lib/goals'
import introduce from './actions/introduce'
// import introduce from './actions/introduce'
import profile from './actions/profile'
// import objective from './actions/objective'
// import goal
// from './actions/goal'
import {
reflection_template,
response_generation_template,
update_generation_template,
} from './templates'
import {parseJSONObjectFromText, parseJsonArrayFromText} from './utils'

const customActions = [introduce]
const customActions = [
// introduce,
profile,
// goal,
// objective
]

export const constants = {
avatarPlaceholder: (seed: string | number) => {
Expand Down Expand Up @@ -395,7 +404,7 @@ export const onMessage = async (message: any, runtime: any) => {
await runtime.messageManager.upsertRawMemory(senderMemory.toJSON())
}

responseData.content = responseData.content.trim()
responseData.content = responseData.content?.trim()
if (responseData.content) {
const responseMemory = new Memory({
user_ids: userIds,
Expand Down
42 changes: 21 additions & 21 deletions packages/agent/src/lib/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ export const defaultActions = [
JSON.stringify({ user: "CJ", content: "I know right lol", action: "NONE" }),
],
},
{
name: "CONTINUE",
description: "Continue the conversation with the user",
examples: [
JSON.stringify({ user: "CJ", content: "The comet passing over tonight is going to be a sight to behold. Are you excited about it?", action: "CONTINUE" }),
],
},
{
name: "WAIT",
description: "Do nothing and wait for another person to reply, or continue their message",
examples: [
JSON.stringify({ user: "CJ", content: "", action: "WAIT" }),
],
},
{
name: "IGNORE",
description: "Ignore the user and do not respond, use this if your role involves being sassy, or mad at user",
examples: [
JSON.stringify({ user: "CJ", content: "", action: "IGNORE" }),
],
},
// {
// name: "CONTINUE",
// description: "Continue the conversation with the user",
// examples: [
// JSON.stringify({ user: "CJ", content: "The comet passing over tonight is going to be a sight to behold. Are you excited about it?", action: "CONTINUE" }),
// ],
// },
// {
// name: "WAIT",
// description: "Do nothing and wait for another person to reply, or continue their message",
// examples: [
// JSON.stringify({ user: "CJ", content: "", action: "WAIT" }),
// ],
// },
// {
// name: "IGNORE",
// description: "Ignore the user and do not respond, use this if your role involves being sassy, or mad at user",
// examples: [
// JSON.stringify({ user: "CJ", content: "", action: "IGNORE" }),
// ],
// },
// {
// name: 'UPDATE_GOAL',
// description: 'Update a the current state of a goal - set goal status to CANCELED, FAILED or COMPLETED',
Expand Down
2 changes: 1 addition & 1 deletion packages/agent/src/lib/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export async function getMessageActors({ supabase, userIds }: any) {
export function formatMessageActors({ actors }: any) {
// format actors as a string
const actorStrings = actors.map((actor: { name: any; description: any; }) => {
const header = `${actor.name}: ${actor.description}`;
const header = `${actor.name}: ${actor.description ?? "No description"}`;
return header;
});
const finalActorStrings = actorStrings.join("\n");
Expand Down
3 changes: 1 addition & 2 deletions packages/agent/src/lib/relationships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ export async function createRelationship({ supabase, userA, userB}: { supabase:

export async function getRelationship({ supabase, userA, userB }: { supabase: any, userA: string, userB: string }) {
const { data, error } = await supabase.from("relationships").select("*")
.or(`user_a.eq.${userA},user_b.eq.${userB}`)
.or(`user_a.eq.${userB},user_b.eq.${userA}`);
.or(`user_a.eq.${userA},user_b.eq.${userB},user_a.eq.${userB},user_b.eq.${userA}`);

if (error) {
throw new Error(error.message);
Expand Down
2 changes: 1 addition & 1 deletion packages/terminal/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"license": "MIT",
"sideEffects": false,
"scripts": {
"dev": "concurrently \"rollup -c -w\" \"node --no-warnings terminal.mjs\"",
"dev": "rollup -c -w",
"dev:terminal": "node --no-warnings terminal.mjs",
"build": "rollup -c && tsc --declaration --emitDeclarationOnly --declarationDir dist",
"build:types": "tsc --declaration --emitDeclarationOnly --declarationDir dist",
Expand Down

0 comments on commit b73a9a3

Please sign in to comment.