assist-feedback.php
11 months ago
assistant.php
11 months ago
base.php
11 months ago
dropped-file.php
11 months ago
edit-image.php
11 months ago
embed.php
11 months ago
feedback.php
11 months ago
function.php
11 months ago
image.php
11 months ago
parameter.php
11 months ago
text.php
11 months ago
transcribe.php
11 months ago
feedback.php
110 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->envId ) ) { |
| 43 | $this->set_env_id( $query->envId ); |
| 44 | } |
| 45 | if ( !empty( $query->functions ) ) { |
| 46 | $this->set_functions( $query->functions ); |
| 47 | } |
| 48 | if ( !empty( $query->instructions ) ) { |
| 49 | $this->set_instructions( $query->instructions ); |
| 50 | } |
| 51 | |
| 52 | // Build the complete conversation history including the assistant's function call |
| 53 | if ( !empty( $query->messages ) ) { |
| 54 | $messages = $query->messages; |
| 55 | |
| 56 | // Add the assistant's response with tool_calls to maintain conversation flow |
| 57 | if ( !empty( $reply->choices ) ) { |
| 58 | $assistantMessage = $reply->choices[0]['message'] ?? null; |
| 59 | if ( $assistantMessage ) { |
| 60 | $messages[] = $assistantMessage; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | $this->set_messages( $messages ); |
| 65 | } |
| 66 | |
| 67 | // For Responses API: Use the response ID from the reply to maintain stateful conversation |
| 68 | // This is critical for the Responses API to link function results with their calls |
| 69 | if ( !empty( $reply->id ) ) { |
| 70 | $this->previousResponseId = $reply->id; |
| 71 | } |
| 72 | elseif ( !empty( $query->previousResponseId ) ) { |
| 73 | // Fallback to query's previousResponseId if reply doesn't have one |
| 74 | $this->previousResponseId = $query->previousResponseId; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public function clear_feedback_blocks() { |
| 79 | $this->blocks = []; |
| 80 | } |
| 81 | |
| 82 | public function add_feedback_block( $block ) { |
| 83 | $this->blocks[] = $block; |
| 84 | } |
| 85 | |
| 86 | #[\ReturnTypeWillChange] |
| 87 | public function jsonSerialize(): array { |
| 88 | $json = [ |
| 89 | 'message' => $this->message, |
| 90 | 'blocks' => $this->blocks, |
| 91 | |
| 92 | 'ai' => [ |
| 93 | 'model' => $this->model, |
| 94 | 'feature' => $this->feature, |
| 95 | ], |
| 96 | |
| 97 | 'system' => [ |
| 98 | 'class' => get_class( $this ), |
| 99 | 'envId' => $this->envId, |
| 100 | 'scope' => $this->scope, |
| 101 | 'session' => $this->session, |
| 102 | ] |
| 103 | ]; |
| 104 | |
| 105 | return $json; |
| 106 | } |
| 107 | |
| 108 | #endregion |
| 109 | } |
| 110 |