expected_candidates_callbacks[] = function ( array $contents ) use ( $content ) { $candidates = $content( $contents ); if ( ! $candidates ) { return null; } return $this->parse_candidates( $candidates ); }; return; } $this->expected_candidates = $this->parse_candidates( $content ); } /** * Resolves the expected candidates for the given contents. * * If no expected candidates were provided and no provided expected callbacks match, a fallback default response * will be returned. * * @since 0.7.0 * * @param Content[] $contents Contents (commonly containing only a single item) with the AI request. * @return Candidates The resolved candidates. */ final protected function resolve_expected_candidates( array $contents ): Candidates { foreach ( $this->expected_candidates_callbacks as $callback ) { $candidates = $callback( $contents ); if ( null !== $candidates ) { return $candidates; } } if ( null !== $this->expected_candidates ) { return $this->expected_candidates; } return $this->get_default_candidates(); } /** * Gets the default candidates to return for AI requests where no expected response was provided. * * @since 0.7.0 * * @return Candidates The default candidates. */ abstract protected function get_default_candidates(): Candidates; /** * Parses the given content into a Candidates object. * * @since 0.7.0 * * @param string|Parts|Content|Candidate|Candidates $content The content to parse. * @return Candidates The resulting Candidates object. */ final protected function parse_candidates( $content ): Candidates { if ( $content instanceof Candidates ) { return $content; } if ( ! $content instanceof Candidate ) { $content = Formatter::format_content( $content, Content_Role::MODEL ); $content = new Candidate( $content ); } $candidates = new Candidates(); $candidates->add_candidate( $content ); return $candidates; } }