Skip to content

Commit 9110a7d

Browse files
release: fixes
- Updated dependencies. - Migrated to OpenAI Response API format.
2 parents d86bc2b + 87f7408 commit 9110a7d

File tree

9 files changed

+725
-805
lines changed

9 files changed

+725
-805
lines changed

bin/set-settings.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
$settings = [
55
'api_key' => 'sk_XXXXXXXXX', // Dummy license key.
6-
'assistant_id' => 'asst_TtalCGxTygMEqb7g3vNy6q8h', // Dummy assistant key.
76
'qdrant_api_key' => '',
87
'qdrant_endpoint' => '',
98
'chat_enabled' => false,

composer.lock

Lines changed: 198 additions & 129 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inc/API.php

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -404,13 +404,11 @@ function ( $carry, $item ) {
404404

405405
if ( 'api_key' === $key && ! empty( $value ) ) {
406406
$openai = new OpenAI( $value );
407-
$valid_api = $openai->setup_assistant();
407+
$valid_api = $openai->moderate( 'This is a test message.' );
408408

409409
if ( is_wp_error( $valid_api ) ) {
410410
return rest_ensure_response( [ 'error' => $this->get_error_message( $valid_api ) ] );
411411
}
412-
413-
$settings['assistant_id'] = $valid_api;
414412
}
415413

416414
if ( 'telemetry_enabled' === $key ) {
@@ -755,40 +753,48 @@ public function get_chat( $request ) {
755753

756754
$openai = OpenAI::instance();
757755

758-
$status = $openai->get_status( $run_id, $thread_id );
756+
$response = $openai->get_response( $run_id );
759757

760-
if ( is_wp_error( $status ) ) {
761-
return rest_ensure_response( [ 'error' => $this->get_error_message( $status ) ] );
758+
if ( is_wp_error( $response ) ) {
759+
return rest_ensure_response( [ 'error' => $this->get_error_message( $response ) ] );
762760
}
763761

764-
if ( 'completed' !== $status ) {
765-
return rest_ensure_response( [ 'status' => $status ] );
762+
if ( 'completed' !== $response->status ) {
763+
return rest_ensure_response( [ 'status' => $response->status ] );
766764
}
767765

768-
$messages = $openai->get_messages( $thread_id );
769-
770-
if ( is_wp_error( $messages ) ) {
771-
return rest_ensure_response( [ 'error' => $this->get_error_message( $messages ) ] );
772-
}
766+
$status = $response->status;
773767

774-
$messages = array_filter(
775-
$messages,
776-
function ( $message ) use ( $run_id ) {
777-
return $message->run_id === $run_id;
768+
$message = array_filter(
769+
$response->output,
770+
function ( $message ) {
771+
return (
772+
isset( $message->type, $message->status, $message->role ) &&
773+
'message' === $message->type &&
774+
'completed' === $message->status &&
775+
'assistant' === $message->role
776+
);
778777
}
779778
);
780779

781-
$message = reset( $messages )->content[0]->text->value;
780+
if ( empty( $message ) ) {
781+
return rest_ensure_response( [ 'error' => __( 'No messages found.', 'hyve-lite' ) ] );
782+
}
782783

784+
$message = reset( $message )->content[0]->text;
783785
$message = json_decode( $message, true );
784786

785787
if ( json_last_error() !== JSON_ERROR_NONE ) {
786788
return rest_ensure_response( [ 'error' => __( 'No messages found.', 'hyve-lite' ) ] );
787789
}
788-
790+
789791
Main::add_labels_to_default_settings();
790792
$settings = Main::get_settings();
791793

794+
if ( isset( $message['properties'] ) ) {
795+
$message = $message['properties'];
796+
}
797+
792798
$response = ( isset( $message['success'] ) && true === $message['success'] && isset( $message['response'] ) ) ? $message['response'] : esc_html( $settings['default_message'] );
793799

794800
do_action( 'hyve_chat_response', $run_id, $thread_id, $query, $record_id, $message, $response );
@@ -984,7 +990,7 @@ public function send_chat( $request ) {
984990
if ( $request->get_param( 'thread_id' ) ) {
985991
$thread_id = $request->get_param( 'thread_id' );
986992
} else {
987-
$thread_id = $openai->create_thread();
993+
$thread_id = $openai->create_conversation();
988994
}
989995

990996
if ( is_wp_error( $thread_id ) ) {
@@ -1006,37 +1012,42 @@ public function send_chat( $request ) {
10061012
$similarity_score_threshold = apply_filters( 'hyve_similarity_score_threshold', 0.4 );
10071013

10081014
$article_context = $this->search_knowledge_base( $message_vector, $similarity_score_threshold );
1009-
$query_run = $openai->create_run(
1015+
1016+
$query_run = $openai->create_response(
10101017
[
10111018
[
1019+
'type' => 'message',
10121020
'role' => 'user',
1013-
'content' => 'START QUESTION: ' . $message . ' :END QUESTION',
1021+
'content' => 'START CONTEXT: ' . $article_context . ' :END CONTEXT',
10141022
],
10151023
[
1024+
'type' => 'message',
10161025
'role' => 'user',
1017-
'content' => 'START CONTEXT: ' . $article_context . ' :END CONTEXT',
1026+
'content' => 'START QUESTION: ' . $message . ' :END QUESTION',
10181027
],
10191028
],
10201029
$thread_id
10211030
);
10221031

10231032
if ( is_wp_error( $query_run ) ) {
1024-
if ( strpos( $this->get_error_message( $query_run ), 'No thread found with id' ) !== false ) {
1025-
$thread_id = $openai->create_thread();
1033+
if ( strpos( $this->get_error_message( $query_run ), 'Conversation with id' ) !== false ) {
1034+
$thread_id = $openai->create_conversation();
10261035

10271036
if ( is_wp_error( $thread_id ) ) {
10281037
return rest_ensure_response( [ 'error' => $this->get_error_message( $thread_id ) ] );
10291038
}
10301039

1031-
$query_run = $openai->create_run(
1040+
$query_run = $openai->create_response(
10321041
[
10331042
[
1043+
'type' => 'message',
10341044
'role' => 'user',
1035-
'content' => 'Question: ' . $message,
1045+
'content' => 'START CONTEXT: ' . $article_context . ' :END CONTEXT',
10361046
],
10371047
[
1048+
'type' => 'message',
10381049
'role' => 'user',
1039-
'content' => 'Context: ' . $article_context,
1050+
'content' => 'START QUESTION: ' . $message . ' :END QUESTION',
10401051
],
10411052
],
10421053
$thread_id

inc/Main.php

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ public function __construct() {
7777
}
7878

7979
if (
80-
isset( $settings['api_key'] ) && isset( $settings['assistant_id'] ) &&
81-
! empty( $settings['api_key'] ) && ! empty( $settings['assistant_id'] )
80+
isset( $settings['api_key'] ) && ! empty( $settings['api_key'] )
8281
) {
8382
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] );
8483
}
@@ -579,33 +578,28 @@ public function delete_post( $post_id ) {
579578
* @return array<string, mixed>
580579
*/
581580
public function add_black_friday_data( $configs ) {
581+
$plan = apply_filters( 'product_hyve_license_plan', 0 );
582+
$is_pro = 0 < $plan;
583+
584+
// NOTE: Currently, only lifetime plan is available for Hyve Pro.
585+
if ( $is_pro ) {
586+
return $configs;
587+
}
588+
582589
$config = $configs['default'];
583590

584591
// translators: %1$s - HTML tag, %2$s - discount, %3$s - HTML tag, %4$s - product name.
585592
$message_template = __( 'Our biggest sale of the year: %1$sup to %2$s OFF%3$s on %4$s. Don\'t miss this limited-time offer.', 'hyve-lite' );
586593
$product_label = 'Hyve';
587594
$discount = '70%';
588-
589-
$plan = apply_filters( 'product_hyve_license_plan', 0 );
590-
$license = apply_filters( 'product_hyve_license_key', false );
591-
$is_pro = 0 < $plan;
592595

593-
if ( $is_pro ) {
594-
// translators: %1$s - HTML tag, %2$s - discount, %3$s - HTML tag, %4$s - product name.
595-
$message_template = __( 'Get %1$sup to %2$s off%3$s when you upgrade your %4$s plan or renew early.', 'hyve-lite' );
596-
$product_label = 'Hyve Pro';
597-
$discount = '30%';
598-
}
599-
600596
$product_label = sprintf( '<strong>%s</strong>', $product_label );
601-
$url_params = [
602-
'utm_term' => $is_pro ? 'plan-' . $plan : 'free',
603-
'lkey' => ! empty( $license ) ? $license : false,
604-
];
605597

606598
$config['message'] = sprintf( $message_template, '<strong>', $discount, '</strong>', $product_label );
607599
$config['sale_url'] = add_query_arg(
608-
$url_params,
600+
[
601+
'utm_term' => 'free',
602+
],
609603
tsdk_translate_link( tsdk_utmify( 'https://themeisle.link/hyve-bf', 'bfcm', 'hyve' ) )
610604
);
611605

@@ -750,6 +744,8 @@ public function plugin_usage( $data ) {
750744
unset( $settings['qdrant_endpoint'] );
751745
}
752746

747+
// We no longer use assistant_id but in case the setting exists,
748+
// it is private and we omit it from the usage data.
753749
if ( isset( $settings['assistant_id'] ) ) {
754750
unset( $settings['assistant_id'] );
755751
}

0 commit comments

Comments
 (0)