assist-feedback.php
10 months ago
assistant.php
2 months ago
base.php
1 week ago
dropped-file.php
3 months ago
edit-image.php
3 months ago
embed.php
3 months ago
feedback.php
10 months ago
function.php
1 month ago
image.php
1 month ago
parameter.php
11 months ago
text.php
1 week ago
transcribe.php
8 months ago
assistant.php
145 lines
| 1 | <?php |
| 2 | |
| 3 | class Meow_MWAI_Query_Assistant extends Meow_MWAI_Query_Base implements JsonSerializable { |
| 4 | // Core Content |
| 5 | public ?Meow_MWAI_Query_DroppedFile $attachedFile = null; |
| 6 | public ?array $attachedFiles = null; |
| 7 | |
| 8 | // Parameters |
| 9 | public ?string $chatId = null; |
| 10 | public ?string $runId = null; |
| 11 | public ?string $assistantId = null; |
| 12 | public ?string $threadId = null; |
| 13 | public ?string $storeId = null; // Vector Store ID (https://platform.openai.com/docs/api-reference/vector-stores) |
| 14 | |
| 15 | #region Constructors, Serialization |
| 16 | |
| 17 | public function __construct( ?string $message = '' ) { |
| 18 | parent::__construct( $message ); |
| 19 | $this->feature = 'assistant'; |
| 20 | } |
| 21 | |
| 22 | #[\ReturnTypeWillChange] |
| 23 | public function jsonSerialize(): array { |
| 24 | $json = [ |
| 25 | 'message' => $this->message, |
| 26 | |
| 27 | 'ai' => [ |
| 28 | 'model' => $this->model, |
| 29 | 'feature' => $this->feature, |
| 30 | 'assistantId' => $this->assistantId, |
| 31 | 'threadId' => $this->threadId, |
| 32 | 'storeId' => $this->storeId, |
| 33 | 'runId' => $this->runId, |
| 34 | ], |
| 35 | |
| 36 | 'system' => [ |
| 37 | 'class' => get_class( $this ), |
| 38 | 'envId' => $this->envId, |
| 39 | 'scope' => $this->scope, |
| 40 | 'session' => $this->session, |
| 41 | 'customId' => $this->customId, |
| 42 | 'chatId' => $this->chatId, |
| 43 | ] |
| 44 | ]; |
| 45 | |
| 46 | if ( !empty( $this->context ) ) { |
| 47 | $json['context']['context'] = $this->context; |
| 48 | } |
| 49 | |
| 50 | if ( !empty( $this->attachedFile ) ) { |
| 51 | $json['context']['hasFile'] = true; |
| 52 | if ( $this->attachedFile->get_type() === 'url' ) { |
| 53 | $json['context']['fileUrl'] = $this->attachedFile->get_url(); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | if ( !empty( $this->attachedFiles ) ) { |
| 58 | $json['context']['hasFiles'] = true; |
| 59 | $json['context']['fileCount'] = count( $this->attachedFiles ); |
| 60 | } |
| 61 | |
| 62 | return $json; |
| 63 | } |
| 64 | |
| 65 | #endregion |
| 66 | |
| 67 | #region File Handling |
| 68 | |
| 69 | /** |
| 70 | * Get all attached files as a normalized array. |
| 71 | * @return Meow_MWAI_Query_DroppedFile[] Array of attached files |
| 72 | */ |
| 73 | public function getAttachments(): array { |
| 74 | $files = $this->attachedFiles ?? []; |
| 75 | if ( $this->attachedFile && !in_array( $this->attachedFile, $files, true ) ) { |
| 76 | array_unshift( $files, $this->attachedFile ); |
| 77 | } |
| 78 | return $files; |
| 79 | } |
| 80 | |
| 81 | public function add_file( Meow_MWAI_Query_DroppedFile $file ): void { |
| 82 | if ( $this->attachedFiles === null ) { |
| 83 | $this->attachedFiles = []; |
| 84 | } |
| 85 | $this->attachedFiles[] = $file; |
| 86 | } |
| 87 | |
| 88 | public function set_files( array $files ): void { |
| 89 | $this->attachedFiles = $files; |
| 90 | } |
| 91 | |
| 92 | public function get_files(): ?array { |
| 93 | return $this->attachedFiles; |
| 94 | } |
| 95 | |
| 96 | #endregion |
| 97 | |
| 98 | #region Parameters |
| 99 | |
| 100 | public function setAssistantId( string $assistantId ): void { |
| 101 | $this->assistantId = $assistantId; |
| 102 | } |
| 103 | |
| 104 | public function setChatId( string $chatId ): void { |
| 105 | $this->chatId = $chatId; |
| 106 | } |
| 107 | |
| 108 | public function setThreadId( string $threadId ): void { |
| 109 | $this->threadId = $threadId; |
| 110 | } |
| 111 | |
| 112 | public function setStoreId( string $storeId ): void { |
| 113 | $this->storeId = $storeId; |
| 114 | } |
| 115 | |
| 116 | public function setRunId( string $runId ): void { |
| 117 | $this->runId = $runId; |
| 118 | } |
| 119 | |
| 120 | #endregion |
| 121 | |
| 122 | #region Inject Params |
| 123 | |
| 124 | // Based on the params of the query, update the attributes |
| 125 | public function inject_params( array $params ): void { |
| 126 | parent::inject_params( $params ); |
| 127 | |
| 128 | // Those are for the keys passed directly by the shortcode. |
| 129 | $params = $this->convert_keys( $params ); |
| 130 | |
| 131 | // Additional for Assistant. |
| 132 | if ( !empty( $params['chatId'] ) ) { |
| 133 | $this->setChatId( $params['chatId'] ); |
| 134 | } |
| 135 | if ( !empty( $params['assistantId'] ) ) { |
| 136 | $this->setAssistantId( $params['assistantId'] ); |
| 137 | } |
| 138 | if ( !empty( $params['threadId'] ) ) { |
| 139 | $this->setThreadId( $params['threadId'] ); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #endregion |
| 144 | } |
| 145 |