assist-feedback.php
10 months ago
assistant.php
6 months ago
base.php
6 months ago
dropped-file.php
5 months ago
edit-image.php
8 months ago
embed.php
7 months ago
feedback.php
10 months ago
function.php
8 months ago
image.php
6 months ago
parameter.php
1 year ago
text.php
6 months ago
transcribe.php
8 months ago
feedback.php
114 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Feedback extends Meow_MWAI_Query_Text implements JsonSerializable { |
| 4 | public $lastReply = null; |
| 5 | public $originalQuery = null; |
| 6 | public array $blocks; |
| 7 | |
| 8 | #region Constructors, Serialization |
| 9 | |
| 10 | /** |
| 11 | * Creates a feedback query that carries function execution results back to the AI model. |
| 12 | * |
| 13 | * @param Meow_MWAI_Reply $reply The AI's response containing function call requests |
| 14 | * @param Meow_MWAI_Query_Text $query The original query that triggered the function calls |
| 15 | */ |
| 16 | public function __construct( Meow_MWAI_Reply $reply, Meow_MWAI_Query_Text $query ) { |
| 17 | parent::__construct( $query->message ); |
| 18 | |
| 19 | // Store references to the reply and original query for context |
| 20 | $this->lastReply = $reply; |
| 21 | $this->originalQuery = $query; |
| 22 | |
| 23 | // Inherit all settings from the original query to maintain consistency |
| 24 | if ( !empty( $query->model ) ) { |
| 25 | $this->set_model( $query->model ); |
| 26 | } |
| 27 | if ( !empty( $query->maxTokens ) ) { |
| 28 | $this->set_max_tokens( $query->maxTokens ); |
| 29 | } |
| 30 | if ( !empty( $query->temperature ) ) { |
| 31 | $this->set_temperature( $query->temperature ); |
| 32 | } |
| 33 | if ( !empty( $query->scope ) ) { |
| 34 | $this->set_scope( $query->scope ); |
| 35 | } |
| 36 | if ( !empty( $query->session ) ) { |
| 37 | $this->set_session( $query->session ); |
| 38 | } |
| 39 | if ( !empty( $query->botId ) ) { |
| 40 | $this->set_bot_id( $query->botId ); |
| 41 | } |
| 42 | if ( !empty( $query->customId ) ) { |
| 43 | $this->set_custom_id( $query->customId ); |
| 44 | } |
| 45 | if ( !empty( $query->envId ) ) { |
| 46 | $this->set_env_id( $query->envId ); |
| 47 | } |
| 48 | if ( !empty( $query->functions ) ) { |
| 49 | $this->set_functions( $query->functions ); |
| 50 | } |
| 51 | if ( !empty( $query->instructions ) ) { |
| 52 | $this->set_instructions( $query->instructions ); |
| 53 | } |
| 54 | |
| 55 | // Build the complete conversation history including the assistant's function call |
| 56 | if ( !empty( $query->messages ) ) { |
| 57 | $messages = $query->messages; |
| 58 | |
| 59 | // Add the assistant's response with tool_calls to maintain conversation flow |
| 60 | if ( !empty( $reply->choices ) ) { |
| 61 | $assistantMessage = $reply->choices[0]['message'] ?? null; |
| 62 | if ( $assistantMessage ) { |
| 63 | $messages[] = $assistantMessage; |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | $this->set_messages( $messages ); |
| 68 | } |
| 69 | |
| 70 | // For Responses API: Use the response ID from the reply to maintain stateful conversation |
| 71 | // This is critical for the Responses API to link function results with their calls |
| 72 | if ( !empty( $reply->id ) ) { |
| 73 | $this->previousResponseId = $reply->id; |
| 74 | } |
| 75 | elseif ( !empty( $query->previousResponseId ) ) { |
| 76 | // Fallback to query's previousResponseId if reply doesn't have one |
| 77 | $this->previousResponseId = $query->previousResponseId; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | public function clear_feedback_blocks() { |
| 82 | $this->blocks = []; |
| 83 | } |
| 84 | |
| 85 | public function add_feedback_block( $block ) { |
| 86 | $this->blocks[] = $block; |
| 87 | } |
| 88 | |
| 89 | #[\ReturnTypeWillChange] |
| 90 | public function jsonSerialize(): array { |
| 91 | $json = [ |
| 92 | 'message' => $this->message, |
| 93 | 'blocks' => $this->blocks, |
| 94 | |
| 95 | 'ai' => [ |
| 96 | 'model' => $this->model, |
| 97 | 'feature' => $this->feature, |
| 98 | ], |
| 99 | |
| 100 | 'system' => [ |
| 101 | 'class' => get_class( $this ), |
| 102 | 'envId' => $this->envId, |
| 103 | 'scope' => $this->scope, |
| 104 | 'session' => $this->session, |
| 105 | 'customId' => $this->customId, |
| 106 | ] |
| 107 | ]; |
| 108 | |
| 109 | return $json; |
| 110 | } |
| 111 | |
| 112 | #endregion |
| 113 | } |
| 114 |