Skip to content

Commit

Permalink
UserRapport now updates properly inside the DB
Browse files Browse the repository at this point in the history
  • Loading branch information
nusk0 committed Feb 5, 2025
1 parent 21ec459 commit a84572d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 26 deletions.
20 changes: 17 additions & 3 deletions packages/adapter-sqlite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,10 +805,24 @@ export class SqliteDatabaseAdapter
}
}

async setUserRapport(userId: UUID, agentId: UUID, score: number): Promise<void> {
async setUserRapport(userIdOrUsername: UUID | string, agentId: UUID, score: number): Promise<void> {
try {
const sql = "UPDATE accounts SET userRapport = ? WHERE id = ?";
this.db.prepare(sql).run(score, userId);
let sql;
let params;

// Check if we're dealing with a username or userId
if (userIdOrUsername.includes('-')) {
// It's a UUID
sql = "UPDATE accounts SET userRapport = userRapport + ? WHERE id = ?";
params = [score, userIdOrUsername];
} else {
// It's a username
sql = "UPDATE accounts SET userRapport = userRapport + ? WHERE username = ?";
params = [score, userIdOrUsername.replace('@', '')];
}

console.log("Setting rapport for", userIdOrUsername, "score:", score);
this.db.prepare(sql).run(...params);
} catch (error) {
console.error("Error setting user rapport:", error);
}
Expand Down
12 changes: 3 additions & 9 deletions packages/adapter-sqlite/src/sqliteTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const sqliteTables = `
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
-- Uncomment the following line to reset all user rapport values to 0
--UPDATE accounts SET userRapport = 0;
-- Table: accounts
CREATE TABLE IF NOT EXISTS "accounts" (
Expand All @@ -11,17 +13,9 @@ CREATE TABLE IF NOT EXISTS "accounts" (
"username" TEXT,
"email" TEXT NOT NULL,
"avatarUrl" TEXT,
"details" TEXT DEFAULT '{}' CHECK(json_valid("details")), -- Ensuring details is a valid JSON field
"userRapport" REAL NOT NULL DEFAULT 0
"details" TEXT DEFAULT '{}' CHECK(json_valid("details")) -- Ensuring details is a valid JSON field
);
-- Add userRapport column if it doesn't exist (using correct SQLite syntax)
SELECT CASE
WHEN NOT EXISTS(SELECT 1 FROM pragma_table_info('accounts') WHERE name='userRapport')
THEN 'ALTER TABLE accounts ADD COLUMN "userRapport" REAL NOT NULL DEFAULT 0;'
END
WHERE NOT EXISTS(SELECT 1 FROM pragma_table_info('accounts') WHERE name='userRapport');
-- Table: memories
CREATE TABLE IF NOT EXISTS "memories" (
"id" TEXT PRIMARY KEY,
Expand Down
2 changes: 1 addition & 1 deletion packages/client-twitter/src/interactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class TwitterInteractionClient {

elizaLogger.log("Checking active conversations");
// Get all active conversations
const activeConversations = await this.runtime.databaseAdapter.getConversationsByStatus('CLOSED');
const activeConversations = await this.runtime.databaseAdapter.getConversationsByStatus('ACTIVE');

for (const conversation of activeConversations) {
const messageIds = JSON.parse(conversation.messageIds);
Expand Down
20 changes: 7 additions & 13 deletions packages/client-twitter/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -484,13 +484,14 @@ export async function analyzeConversation(
template: analysisTemplate
});
console.log("context", context)

const analysis = await generateText({
runtime,
context,
modelClass: ModelClass.LARGE,
});


elizaLogger.log("User sentiment scores:", analysis);

try {
Expand All @@ -504,18 +505,11 @@ export async function analyzeConversation(

// Update user rapport based on sentiment scores
for (const [username, score] of Object.entries(sentimentScores)) {
const userId = messages.find(m =>
(m.content.username || m.userId) === username.replace('@', '')
)?.userId;

if (userId) {
await runtime.databaseAdapter.setUserRapport(
userId,
runtime.agentId,
score as number // Use the sentiment score directly
);
elizaLogger.log(`Updated rapport for user ${username}:`, score);
}
await runtime.databaseAdapter.setUserRapport(
username,
runtime.agentId,
score as number
);
}
} catch (error) {
elizaLogger.error("Error parsing sentiment analysis:", error);
Expand Down

0 comments on commit a84572d

Please sign in to comment.