Skip to content

Commit

Permalink
Merge pull request #1479 from elizaOS/enhance/client-direct
Browse files Browse the repository at this point in the history
feat: Enhance client direct
  • Loading branch information
shakkernerd authored Dec 26, 2024
2 parents e60449d + 998f706 commit b3dcc51
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions packages/client-direct/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import bodyParser from "body-parser";
import cors from "cors";
import express, { Request as ExpressRequest } from "express";
import multer, { File } from "multer";
import { elizaLogger, generateCaption, generateImage } from "@elizaos/core";
import {
elizaLogger,
generateCaption,
generateImage,
getEmbeddingZeroVector,
} from "@elizaos/core";
import { composeContext } from "@elizaos/core";
import { generateMessageResponse } from "@elizaos/core";
import { messageCompletionFooter } from "@elizaos/core";
Expand Down Expand Up @@ -177,17 +182,19 @@ export class DirectClient {
};

const memory: Memory = {
id: messageId,
id: stringToUuid(messageId + "-" + userId),
...userMessage,
agentId: runtime.agentId,
userId,
roomId,
content,
createdAt: Date.now(),
};

await runtime.messageManager.addEmbeddingToMemory(memory);
await runtime.messageManager.createMemory(memory);

const state = await runtime.composeState(userMessage, {
let state = await runtime.composeState(userMessage, {
agentName: runtime.character.name,
});

Expand All @@ -202,34 +209,30 @@ export class DirectClient {
modelClass: ModelClass.LARGE,
});

if (!response) {
res.status(500).send(
"No response from generateMessageResponse"
);
return;
}

// save response to memory
const responseMessage = {
const responseMessage: Memory = {
id: stringToUuid(messageId + "-" + runtime.agentId),
...userMessage,
userId: runtime.agentId,
content: response,
embedding: getEmbeddingZeroVector(),
createdAt: Date.now(),
};

await runtime.messageManager.createMemory(responseMessage);

if (!response) {
res.status(500).send(
"No response from generateMessageResponse"
);
return;
}
state = await runtime.updateRecentMessageState(state);

let message = null as Content | null;

await runtime.evaluate(memory, state);

// Check if we should suppress the initial message
const action = runtime.actions.find(
(a) => a.name === response.action
);
const shouldSuppressInitialMessage =
action?.suppressInitialMessage;

const _result = await runtime.processActions(
await runtime.processActions(
memory,
[responseMessage],
state,
Expand All @@ -239,6 +242,15 @@ export class DirectClient {
}
);

await runtime.evaluate(memory, state);

// Check if we should suppress the initial message
const action = runtime.actions.find(
(a) => a.name === response.action
);
const shouldSuppressInitialMessage =
action?.suppressInitialMessage;

if (!shouldSuppressInitialMessage) {
if (message) {
res.json([response, message]);
Expand Down

0 comments on commit b3dcc51

Please sign in to comment.