MCPcopy Create free account
hub / github.com/angular/examples / handleProgress

Function handleProgress

genkit-angular-story-generator/src/flows.ts:200–222  ·  view source on GitHub ↗

* Handles logic to move the story forward, updates manages the current * session state's current milestone based on whether or not the next milestone * was achieved, and calculates the progress. If the final milestone was acheived, * meaning the story is over, gets the story ending and appends th

(
  storyParts: string[],
  achievedMilestone: boolean,
  sessionId: string)

Source from the content-addressed store, hash-verified

198 * @returns updated storyParts and progress
199 */
200async function handleProgress(
201 storyParts: string[],
202 achievedMilestone: boolean,
203 sessionId: string): Promise<{ storyParts: string[], progress: number }> {
204 const session = await ai.loadSession(sessionId, {
205 store: new JsonSessionStore(),
206 });
207 let currentMilestone = session.state.currentMilestone;
208 const milestones = session.state.milestones;
209 const finalMilestone = milestones[milestones.length - 1];
210 let progress = milestones.indexOf(currentMilestone) / milestones.length;
211 if (achievedMilestone && currentMilestone === finalMilestone) {
212 progress = 1;
213 const storyEnding = await endStoryFlow(sessionId);
214 storyParts = [...storyParts, ...storyEnding];
215 } else if (achievedMilestone) {
216 const nextMilestoneIndex = milestones.indexOf(currentMilestone) + 1;
217 currentMilestone = milestones[nextMilestoneIndex];
218 progress = nextMilestoneIndex / milestones.length;
219 session.updateState({ ...session.state, currentMilestone });
220 }
221 return { storyParts, progress };
222}
223
224const endStoryFlow = ai.defineFlow(
225 {

Callers 1

flows.tsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected