Class Episodes
java.lang.Object
org.machanism.machai.gw.processor.Episodes
Maintains an ordered collection of act episode prompts and provides execution
helpers that support several playback strategies.
Supported functionality:
- Holds an ordered list of episode prompts, each optionally starting with a
markdown heading (
"# Name") that names the episode. - Executes episodes in their natural order via
regularOrder(Integer, BiFunction), honoring repeat requests (RepeatEpisodeException) and jump/redirect requests (MoveToEpisodeException). - Executes an explicitly selected subset of episodes in the requested order
via
requestedOrder(BiFunction). - Resolves episode indices either by 1-based ID or by heading name when a
MoveToEpisodeExceptionrequests a jump to a named episode. - Exposes act/episode metadata for reporting via
getActInformation(int).
Example: regular order execution
Episodes episodes = new Episodes(actProcessor);
episodes.setName("demo-act");
episodes.setEpisodes(List.of(
"# Introduction\nWelcome to the show!",
"# Recap\nLast time on our show..."));
episodes.regularOrder(1, (id, prompt) -> executor.run(id, prompt));
Example: executing only a selected subset
episodes.setSelectedEpisodes(List.of(2));
if (!episodes.isRegularOrder()) {
episodes.requestedOrder((id, prompt) -> executor.run(id, prompt));
}
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ActProcessorOrdered list of act episode prompts to execute.private static final Stringprivate static final org.slf4j.LoggerLogger for documentation input processing events.private StringLogical act name associated with the episodes.Explicitly selected 1-based episode identifiers. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiongetActInformation(int episodeId) Returns episode information as a map suitable for serialization.getEpisodeId(Integer requestedEpisodeId, MoveToEpisodeException e) Resolves the next episode index from a move request exception.private intgetEpisodeIdByName(String episodeName) Returns the 1-based index of the episode whose prompt text contains a heading that matches the specified episode name.private StringgetEpisodeName(int episodeId) Extracts and returns the episode name (heading) from the prompt text of the episode at the specified index.Returns the configured episode prompts.getName()Returns the act name associated with these episodes.booleanReturns whether no explicit episode subset has been selected.private voidlogEpisodeHeader(int episodeId, int iteration, String msg) Logs a formatted episode banner for the current execution step.voidregularOrder(Integer startEpisodeId, BiFunction<Integer, String, String> func) Executes episodes in regular order starting from the supplied 1-based index while honoring repeat and move requests.intrequestedOrder(BiFunction<Integer, String, String> func) Executes only the explicitly selected episodes in their requested order.voidsetEpisodes(List<String> episodes) Replaces the current ordered episode list.voidSets the act name associated with these episodes.voidsetSelectedEpisodes(List<Integer> selectedEpisodeIds) Sets the list of explicitly requested episode identifiers.intsize()Returns the number of configured episodes.
-
Field Details
-
HEADER_MARKER
- See Also:
-
logger
private static final org.slf4j.Logger loggerLogger for documentation input processing events. -
episodes
Ordered list of act episode prompts to execute. -
selectedEpisodes
Explicitly selected 1-based episode identifiers. -
name
Logical act name associated with the episodes. -
actProcessor
-
-
Constructor Details
-
Episodes
-
-
Method Details
-
setSelectedEpisodes
Sets the list of explicitly requested episode identifiers.- Parameters:
selectedEpisodeIds- 1-based episode identifiers to execute- Throws:
IllegalArgumentException- if any identifier is outside the available episode range
-
getEpisodeIdByName
Returns the 1-based index of the episode whose prompt text contains a heading that matches the specified episode name.Scans each episode's prompt text, extracts the first line that appears between the heading marker "# " and the next newline character, trims any leading or trailing whitespace, and compares it to the provided
episodeName. If a match is found, the corresponding episode index is returned.Example:
episodes.get(0): "# Introduction\nWelcome to the show!" episodes.get(1): "# Recap\nLast time on our show..." lookup for "Recap" returns 2 lookup for "Introduction" returns 1
- Parameters:
episodeName- the heading text to match (e.g., "Recap")- Returns:
- the 1-based index of the matching episode
- Throws:
EpisodeNotFoundException- if no episode with the specified heading exists
-
getEpisodeName
Extracts and returns the episode name (heading) from the prompt text of the episode at the specified index.Retrieves the episode prompt at index
episodeId, extracts the substring between the first occurrence of the heading marker "#" and the next newline character, and trims any leading or trailing whitespace. If the extracted heading is empty after trimming,nullis returned.Example:
episodes.get(0): "# Introduction\nWelcome to the show!" lookup for episode id 1 returns "Introduction"
- Parameters:
episodeId- the 1-based index of the episode- Returns:
- the trimmed episode name, or
nullif no heading is found or the heading is empty
-
regularOrder
Executes episodes in regular order starting from the supplied 1-based index while honoring repeat and move requests.- Parameters:
startEpisodeId- starting 1-based episode indexfunc- callback used to execute an episode
-
requestedOrder
Executes only the explicitly selected episodes in their requested order.- Parameters:
func- callback used to execute an episode- Returns:
- the last processed episode identifier
-
getEpisodeId
Resolves the next episode index from a move request exception.- Parameters:
requestedEpisodeId- current fallback episode indexe- exception describing the requested move- Returns:
- resolved 1-based episode index
-
logEpisodeHeader
Logs a formatted episode banner for the current execution step.- Parameters:
episodeId- 1-based episode indexiteration- current iteration number for the same episodemsg- banner prefix (for example"Start"or"End")
-
setEpisodes
Replaces the current ordered episode list.- Parameters:
episodes- episode prompts to execute
-
getEpisodes
Returns the configured episode prompts.- Returns:
- configured episode prompt list
-
isRegularOrder
public boolean isRegularOrder()Returns whether no explicit episode subset has been selected.- Returns:
truewhen regular order should be used
-
size
public int size()Returns the number of configured episodes.- Returns:
- episode count
-
getActInformation
Returns episode information as a map suitable for serialization.- ACT_NAME: The name of the act.
- EPISODES: An array of episode objects, each with ID and EPISODE_NAME.
- CURRENT_EPISODE_ID: The currently selected episode ID.
- Parameters:
episodeId- the ID of the current episode- Returns:
- a
Mapcontaining act and episode information; it may be converted to aJsonNodeby callers when required
-
getName
Returns the act name associated with these episodes.- Returns:
- act name
-
setName
Sets the act name associated with these episodes.- Parameters:
name- act name
-