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

Optimize promise.all in compose state, pass initial state to providers #67

Merged
merged 1 commit into from
Mar 21, 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
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
Loading