Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 2x 6x 5x 5x 5x 5x 1x 4x 4x 4x 4x 4x 1x 3x 3x 5x 5x 5x | import type { Logger } from '@metamask/logger';
import { CapabilityResultMessage } from './messages.ts';
import type { AssistantMessage, Transcript } from './messages.ts';
import type { CapabilityRecord } from '../../types.ts';
export const makeEvaluator =
({
capabilities = {},
logger,
}: {
capabilities?: CapabilityRecord;
logger?: Logger;
}) =>
async (
history: Transcript,
message: AssistantMessage,
): Promise<CapabilityResultMessage | null> => {
logger?.info('history:', history);
logger?.info('message:', message.toJSON());
// Validate the message.
const invocations = message.messageBody.invoke;
if (!invocations) {
throw new Error('No invoke in message');
}
Iif (invocations.length === 0) {
throw new Error('Empty invocation list in message');
}
const results = await Promise.all(
invocations.map(async ({ name, args }) => ({
name,
args,
result: await (async () => {
const toInvoke = capabilities[name];
if (!toInvoke) {
throw new Error(`Invoked capability ${name} not found`);
}
return await toInvoke.func(args as never);
})(),
})),
);
logger?.info('results:', results);
const resultMessage = new CapabilityResultMessage(results);
history.push(message, resultMessage);
return resultMessage;
};
|