Skip to content

Commit

Permalink
Optimize promise.all in compose state, pass initial state to providers
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Mar 21, 2024
1 parent b9b48c3 commit 0278aed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/lib/goals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getGoals = async ({
});
};

export const formatGoalsAsString = async ({ goals }: { goals: Goal[] }) => {
export const formatGoalsAsString = ({ goals }: { goals: Goal[] }) => {
const goalStrings = goals.map((goal: Goal) => {
const header = `Goal: ${goal.name}\nid: ${goal.id}`;
const objectives =
Expand Down
65 changes: 32 additions & 33 deletions src/lib/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,35 +460,32 @@ export class BgentRuntime {
recentFactsData,
goalsData,
loreData,
providers,
]: [Actor[], Memory[], Memory[], Goal[], Memory[], string] =
await Promise.all([
getActorDetails({ runtime: this, room_id }),
this.messageManager.getMemories({
room_id,
count: recentMessageCount,
unique: false,
}),
this.factManager.getMemories({
room_id,
count: recentFactsCount,
}),
getGoals({
runtime: this,
count: 10,
onlyInProgress: false,
room_id,
}),
getLore({
runtime: this,
message: (message.content as Content).content,
count: 5,
match_threshold: 0.5,
}),
getProviders(this, message),
]);

const goals = await formatGoalsAsString({ goals: goalsData });
]: [Actor[], Memory[], Memory[], Goal[], Memory[]] = await Promise.all([
getActorDetails({ runtime: this, room_id }),
this.messageManager.getMemories({
room_id,
count: recentMessageCount,
unique: false,
}),
this.factManager.getMemories({
room_id,
count: recentFactsCount,
}),
getGoals({
runtime: this,
count: 10,
onlyInProgress: false,
room_id,
}),
getLore({
runtime: this,
message: (message.content as Content).content,
count: 5,
match_threshold: 0.5,
}),
]);

const goals = formatGoalsAsString({ goals: goalsData });

let relevantFactsData: Memory[] = [];

Expand Down Expand Up @@ -544,7 +541,6 @@ export class BgentRuntime {
),
lore: addHeader("### Important Information", lore),
loreData,
providers,
goalsData,
recentMessages: addHeader("### Conversation Messages", recentMessages),
recentMessagesData,
Expand All @@ -571,16 +567,18 @@ export class BgentRuntime {
return null;
});

const resolvedEvaluators = await Promise.all(evaluatorPromises);
const [resolvedEvaluators, resolvedActions, providers] = await Promise.all([
Promise.all(evaluatorPromises),
Promise.all(actionPromises),
getProviders(this, message, initialState),
]);

const evaluatorsData = resolvedEvaluators.filter(Boolean) as Evaluator[];
const evaluators = formatEvaluators(evaluatorsData);
const evaluatorNames = formatEvaluatorNames(evaluatorsData);
const evaluatorConditions = formatEvaluatorConditions(evaluatorsData);
const evaluatorExamples = formatEvaluatorExamples(evaluatorsData);

const resolvedActions = await Promise.all(actionPromises);

const actionsData = resolvedActions.filter(Boolean) as Action[];

const formattedActionExamples =
Expand All @@ -599,6 +597,7 @@ export class BgentRuntime {
evaluatorNames,
evaluatorConditions,
evaluatorExamples,
providers,
};

return { ...initialState, ...actionState };
Expand Down

0 comments on commit 0278aed

Please sign in to comment.