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

feat: add callback handler to runtime evaluate method #938

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
38 changes: 18 additions & 20 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -566,10 +566,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.
shakkernerd marked this conversation as resolved.
Show resolved Hide resolved
*/
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 @@ -595,17 +601,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 @@ -617,21 +618,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);
shakkernerd marked this conversation as resolved.
Show resolved Hide resolved
}

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 @@ -1023,7 +1023,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
Loading