-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode-ai-provider.php
More file actions
94 lines (79 loc) · 2.28 KB
/
Copy pathopencode-ai-provider.php
File metadata and controls
94 lines (79 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/**
* Plugin Name: OpenCode AI Provider
* Plugin URI: https://github.com/murshed/opencode-ai-provider
* Description: OpenCode AI Provider for the WordPress AI Client.
* Requires at least: 6.9
* Requires PHP: 7.4
* Version: 1.0.0
* Author: FahimMurshed
* Author URI: https://fahimm.com
* License: GPL-2.0-or-later
* License URI: https://spdx.org/licenses/GPL-2.0-or-later.html
* Text Domain: opencode-ai-provider
*
* @package WordPress\OpenCodeAiProvider
*/
declare(strict_types=1);
namespace WordPress\OpenCodeAiProvider;
use WordPress\AiClient\AiClient;
use WordPress\OpenCodeAiProvider\Provider\OpenCodeZenProvider;
if (!defined('ABSPATH')) {
return;
}
require_once __DIR__ . '/src/autoload.php';
/**
* Registers the OpenCode AI Provider with the AI Client.
*
* @since 1.0.0
*
* @return void
*/
function register_provider(): void
{
if (!class_exists(AiClient::class)) {
return;
}
$registry = AiClient::defaultRegistry();
if ($registry->hasProvider(OpenCodeZenProvider::class)) {
return;
}
$registry->registerProvider(OpenCodeZenProvider::class);
}
add_action('init', __NAMESPACE__ . '\\register_provider', 5);
/**
* Clears the cached models list when the OpenCode API key option is added or updated.
*
* @since 1.0.0
*
* @return void
*/
function clear_models_cache(): void
{
try {
if (class_exists(OpenCodeZenProvider::class)) {
OpenCodeZenProvider::modelMetadataDirectory()->invalidateCaches();
}
} catch (\Throwable $e) {
// Ignore any failures during bootstrap/init.
}
}
add_action('update_option_connectors_ai_opencode_api_key', __NAMESPACE__ . '\\clear_models_cache');
add_action('add_option_connectors_ai_opencode_api_key', __NAMESPACE__ . '\\clear_models_cache');
/**
* Extends the default WordPress HTTP request timeout for OpenCode Zen API requests.
*
* @since 1.0.0
*
* @param float $timeout The current timeout in seconds.
* @param string $url The request URL.
* @return float The modified timeout.
*/
function extend_http_request_timeout(float $timeout, string $url): float
{
if (strpos($url, 'opencode.ai') !== false) {
return 60.0;
}
return $timeout;
}
add_filter('http_request_timeout', __NAMESPACE__ . '\\extend_http_request_timeout', 10, 2);