Skip to content

Commit

Permalink
Merge pull request #938 from bmgalego/evaluator-callback
Browse files Browse the repository at this point in the history
feat: add callback handler to runtime evaluate method
  • Loading branch information
shakkernerd authored Dec 10, 2024
2 parents 33289ce + f0f1f00 commit 4c2fcf3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
38 changes: 18 additions & 20 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,10 +572,16 @@ export class AgentRuntime implements IAgentRuntime {
* Evaluate the message and state using the registered evaluators.
* @param message The message to evaluate.
* @param state The state of the agent.
* @param didRespond Whether the agent responded to the message.
* @param didRespond Whether the agent responded to the message.~
* @param callback The handler callback
* @returns The results of the evaluation.
*/
async evaluate(message: Memory, state?: State, didRespond?: boolean) {
async evaluate(
message: Memory,
state?: State,
didRespond?: boolean,
callback?: HandlerCallback
) {
const evaluatorPromises = this.evaluators.map(
async (evaluator: Evaluator) => {
elizaLogger.log("Evaluating", evaluator.name);
Expand All @@ -601,17 +607,12 @@ export class AgentRuntime implements IAgentRuntime {
return [];
}

const evaluators = formatEvaluators(evaluatorsData as Evaluator[]);
const evaluatorNames = formatEvaluatorNames(
evaluatorsData as Evaluator[]
);

const context = composeContext({
state: {
...state,
evaluators,
evaluatorNames,
} as State,
evaluators: formatEvaluators(evaluatorsData),
evaluatorNames: formatEvaluatorNames(evaluatorsData),
},
template:
this.character.templates?.evaluationTemplate ||
evaluationTemplate,
Expand All @@ -623,21 +624,18 @@ export class AgentRuntime implements IAgentRuntime {
modelClass: ModelClass.SMALL,
});

const parsedResult = parseJsonArrayFromText(
const evaluators = parseJsonArrayFromText(
result
) as unknown as string[];

this.evaluators
.filter((evaluator: Evaluator) =>
parsedResult?.includes(evaluator.name)
)
.forEach((evaluator: Evaluator) => {
if (!evaluator?.handler) return;
for (const evaluator of this.evaluators) {
if (!evaluators.includes(evaluator.name)) continue;

evaluator.handler(this, message);
});
if (evaluator.handler)
await evaluator.handler(this, message, state, {}, callback);
}

return parsedResult;
return evaluators;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,8 @@ export interface IAgentRuntime {
evaluate(
message: Memory,
state?: State,
didRespond?: boolean
didRespond?: boolean,
callback?: HandlerCallback
): Promise<string[]>;

ensureParticipantExists(userId: UUID, roomId: UUID): Promise<void>;
Expand Down

0 comments on commit 4c2fcf3

Please sign in to comment.