Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to new bgent #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
521 changes: 305 additions & 216 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"dependencies": {
"@supabase/supabase-js": "^2.39.7",
"bgent": "^0.0.46",
"bgent": "^0.1.8",
"chalk": "^5.3.0",
"clsx": "^2.1.0",
"dotenv": "^16.4.1",
Expand Down
28 changes: 9 additions & 19 deletions packages/agent/src/actions/__tests__/introduce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
test
} from '@jest/globals'
import { type Session, type User } from '@supabase/supabase-js'
import { composeContext, createRuntime, getRelationship, type BgentRuntime, type State } from 'bgent'
import { composeContext, createRuntime, getRelationship, type BgentRuntime, type State, SupabaseDatabaseAdapter } from 'bgent'
import { type UUID } from 'crypto'
import { config } from 'dotenv'
import { zeroUuid } from '../../../test/constants'
Expand Down Expand Up @@ -117,16 +117,13 @@ describe('INTRODUCE Action Tests', () => {
await runtime.descriptionManager.createMemory(
await runtime.descriptionManager.addEmbeddingToMemory({
user_id: user?.id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: { content: 'Likes indie music' },
room_id
})
)

const message = {
senderId: user?.id as UUID,
agentId: zeroUuid,
userIds: [user?.id as UUID, zeroUuid],
userId: user?.id as UUID,
room_id,
content: {
content:
Expand All @@ -146,9 +143,7 @@ describe('INTRODUCE Action Tests', () => {
)

const message = {
senderId: userWithoutDescription.id as UUID,
agentId: zeroUuid as UUID,
userIds: [userWithoutDescription.id as UUID, zeroUuid],
userId: userWithoutDescription.id as UUID,
room_id,
content: { content: "I'm new here!", action: 'WAIT' }
}
Expand Down Expand Up @@ -177,7 +172,6 @@ describe('INTRODUCE Action Tests', () => {
await runtime.descriptionManager.createMemory(
await runtime.descriptionManager.addEmbeddingToMemory({
user_id: mainUser.id as UUID,
user_ids: [mainUser.id as UUID, zeroUuid],
content: { content: 'Enjoys playing Guitar Hero' },
room_id: zeroUuid
})
Expand All @@ -188,7 +182,6 @@ describe('INTRODUCE Action Tests', () => {
await runtime.descriptionManager.createMemory(
await runtime.descriptionManager.addEmbeddingToMemory({
user_id: similarUser.id as UUID,
user_ids: [similarUser.id as UUID, zeroUuid],
content: { content: 'Loves music and Guitar Hero' },
room_id: zeroUuid
})
Expand All @@ -199,7 +192,6 @@ describe('INTRODUCE Action Tests', () => {
await runtime.descriptionManager.createMemory(
await runtime.descriptionManager.addEmbeddingToMemory({
user_id: differentUser.id as UUID,
user_ids: [differentUser.id as UUID, zeroUuid],
content: { content: 'Enjoys outdoor activities' },
room_id: zeroUuid
})
Expand All @@ -208,8 +200,7 @@ describe('INTRODUCE Action Tests', () => {

test('Rolodex returns potential connections based on user descriptions', async () => {
const message = {
senderId: mainUser.id as UUID,
agentId: zeroUuid,
userId: mainUser.id as UUID,
content: {
content: 'Looking to connect with someone with similar interests.',
action: 'WAIT'
Expand All @@ -219,9 +210,10 @@ describe('INTRODUCE Action Tests', () => {
}

// select all from descriptions
const response = await runtime.supabase.from('descriptions').select('*')
// @ts-expect-error - supabase is private atm
const response = await (runtime.databaseAdapter as SupabaseDatabaseAdapter).supabase.from('descriptions').select('*')
if (response.error) {
throw new Error(response.error.message)
throw new Error(response.error.message as string)
}

const relevantRelationships = await getRelevantRelationships(
Expand All @@ -236,14 +228,12 @@ describe('INTRODUCE Action Tests', () => {

test('More similar user is higher in the rolodex list than the less similar user', async () => {
const message = {
senderId: mainUser.id as UUID,
agentId: zeroUuid as UUID,
userId: mainUser.id as UUID,
content: {
content: 'Who should I meet that enjoys music as much as I do?',
action: 'WAIT'
},
room_id,
userIds: [mainUser.id as UUID, zeroUuid]
room_id
}

const state = (await runtime.composeState(message)) as State
Expand Down
45 changes: 14 additions & 31 deletions packages/agent/src/actions/introduce.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { type UUID } from 'crypto'
import {
type BgentRuntime, composeContext, formatActors, getActorDetails, createRelationship, type Action,
addHeader,
composeContext,
createRelationship,
formatActors, getActorDetails,
parseJSONObjectFromText,
type Action,
type ActionExample,
type Memory,
type BgentRuntime,
type Message,
type State,
parseJSONObjectFromText,
addHeader
type State
} from 'bgent'

export const template = `TASK: Introduce {{senderName}} to someone from {{agentName}}'s rolodex.
Expand Down Expand Up @@ -54,38 +56,19 @@ export const getRelevantRelationships = async (
count: number = 5
) => {
// Check if the user has a profile
const descriptions = await runtime.descriptionManager.getMemoriesByIds({
userIds: [message.senderId, message.agentId]
const descriptions = await runtime.descriptionManager.getMemories({
room_id: message.room_id
})
// if they dont, return empty string
if (descriptions.length === 0) {
throw new Error('User does not have a profile')
}
// if they do, run the rolodex match and return a list of good matches for them
const description = descriptions[0] as Memory
const searchEmbedding = description.embedding as number[]

const otherPeopleDescriptions = (
await runtime.descriptionManager.searchMemoriesByEmbedding(
searchEmbedding,
{
match_threshold: 0,
count: count + 1,
unique: true
}
)
)
.filter((d: Memory) => d.user_id !== message.senderId && d.user_id !== message.agentId)
// .slice(0, count)

// get all the userIds from the user_ids of the otherPeopleDescriptions, make sure unique
const userIds = Array.from(
new Set(otherPeopleDescriptions.map((d: Memory) => d.user_id as UUID))
)
// const description = descriptions[0] as Memory

const actorsData = await getActorDetails({
runtime,
userIds
room_id: message.room_id
})

const formattedActorData = formatActors({ actors: actorsData })
Expand Down Expand Up @@ -144,8 +127,8 @@ export const action = {
message: Message
): Promise<boolean> => {
// Retrieve descriptions for the message sender
const descriptions = await runtime.descriptionManager.getMemoriesByIds({
userIds: [message.senderId]
const descriptions = await runtime.descriptionManager.getMemories({
room_id: message.room_id
})

// If the sender has at least one description, return true to indicate the action is valid
Expand Down
6 changes: 1 addition & 5 deletions packages/agent/src/evaluators/__tests__/details.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ describe('User Details', () => {
const room_id = data?.room_id

const message: Message = {
senderId: user?.id as UUID,
agentId: zeroUuid,
userIds: [user?.id as UUID, zeroUuid],
userId: user?.id as UUID,
content: { content: '' },
room_id
}
Expand All @@ -54,7 +52,6 @@ describe('User Details', () => {
const embedding = getCachedEmbedding(c.content.content as string)
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: c.content,
room_id,
embedding
Expand Down Expand Up @@ -88,7 +85,6 @@ describe('User Details', () => {
const embedding = getCachedEmbedding(c.content.content)
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: {
content: c.content.content
},
Expand Down
9 changes: 2 additions & 7 deletions packages/agent/src/evaluators/__tests__/profile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ describe('User Profile', () => {
const room_id = data?.room_id

const message: Message = {
senderId: user?.id as UUID,
agentId: zeroUuid,
userIds: [user?.id as UUID, zeroUuid],
userId: user?.id as UUID,
content: { content: '' },
room_id
}
Expand All @@ -82,7 +80,6 @@ describe('User Profile', () => {
const existingEmbedding = getCachedEmbedding(c.content.content as string)
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: c.content,
room_id,
embedding: existingEmbedding
Expand Down Expand Up @@ -114,7 +111,6 @@ describe('User Profile', () => {
const existingEmbedding = getCachedEmbedding(c.content.content)
const bakedMemory = await runtime.messageManager.addEmbeddingToMemory({
user_id: c.user_id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: c.content,
room_id,
embedding: existingEmbedding
Expand All @@ -138,7 +134,6 @@ describe('User Profile', () => {
const bakedMemory =
await runtime.descriptionManager.addEmbeddingToMemory({
user_id: user?.id as UUID,
user_ids: [user?.id as UUID, zeroUuid],
content: { content: c },
room_id,
embedding: existingEmbedding
Expand All @@ -157,7 +152,7 @@ describe('User Profile', () => {
})

const descriptions = await runtime.descriptionManager.getMemoriesByIds({
userIds: [message.senderId, message.agentId] as UUID[],
userIds: [message.userId, message.agentId] as UUID[],
count: 5
})
expect(descriptions.length === 3).toBeTruthy()
Expand Down
10 changes: 6 additions & 4 deletions packages/agent/src/evaluators/details.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type BgentRuntime, composeContext, type Evaluator, type Message, type State, parseJSONObjectFromText } from 'bgent'
import { type BgentRuntime, composeContext, type Evaluator, type Message, type State, parseJSONObjectFromText, SupabaseDatabaseAdapter } from 'bgent'

const template = `You are collecting details about {{senderName}} based on their ongoing conversation with {{agentName}}.

Expand Down Expand Up @@ -53,10 +53,11 @@ const handler = async (runtime: BgentRuntime, message: Message) => {

const { name, age, location, gender } = responseData

const response = await runtime.supabase
// @ts-expect-error - supabase is private atm
const response = await (runtime.databaseAdapter as SupabaseDatabaseAdapter).supabase
.from('accounts')
.select('*')
.eq('id', message.senderId)
.eq('id', message.userId)
.single()
const { data: userRecord, error } = response
if (error) {
Expand All @@ -81,7 +82,8 @@ const handler = async (runtime: BgentRuntime, message: Message) => {
currentDetails.gender = gender
}

const { error: updateError } = await runtime.supabase
// @ts-expect-error - supabase is private atm
const { error: updateError } = await (runtime.databaseAdapter as SupabaseDatabaseAdapter).supabase
.from('accounts')
.update({ details: currentDetails })
.eq('id', userRecord.id)
Expand Down
17 changes: 10 additions & 7 deletions packages/agent/src/evaluators/profile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import {
type Evaluator,
type Memory,
type Message,
type State
type State,
type SupabaseDatabaseAdapter
} from 'bgent'
import { type UUID } from 'crypto'

Expand Down Expand Up @@ -74,9 +75,8 @@ const handler = async (runtime: BgentRuntime, message: Message, state: State) =>
state = state ?? (await runtime.composeState(message)) as State

// read the description for the current user
const { senderId, agentId } = state
const descriptions = await runtime.descriptionManager.getMemoriesByIds({
userIds: [senderId, agentId] as UUID[],
const descriptions = await runtime.descriptionManager.getMemories({
room_id: message.room_id,
count: 5
})
const profiles = descriptions
Expand Down Expand Up @@ -119,7 +119,9 @@ const handler = async (runtime: BgentRuntime, message: Message, state: State) =>
const content = Object.values(responseData).join('\n')

// find the user
const response = await runtime.supabase
// TODO: update supabase for access to this
// @ts-expect-error - supabase is private atm
const response = await (runtime.databaseAdapter as SupabaseDatabaseAdapter).supabase
.from('accounts')
.select('*')
.eq('name', state.senderName)
Expand All @@ -141,7 +143,6 @@ const handler = async (runtime: BgentRuntime, message: Message, state: State) =>

const descriptionMemory =
await runtime.descriptionManager.addEmbeddingToMemory({
user_ids: [state.agentId, userRecord.id],
user_id: state.agentId!,
content: { content },
room_id: relationshipRecord.room_id
Expand Down Expand Up @@ -186,7 +187,9 @@ const handler = async (runtime: BgentRuntime, message: Message, state: State) =>
}

// save the new description to the user's account
const response2 = await runtime.supabase
// TODO: update supabase for access to this
// @ts-expect-error - supabase is private atm
const response2 = await (runtime.databaseAdapter as SupabaseDatabaseAdapter).supabase
.from('accounts')
.update({ details: { ...details, ...responseData2 } })
.eq('id', userRecord.id)
Expand Down
Loading
Loading