初始化仓库及v1.0.0提交

This commit is contained in:
2026-05-05 03:21:58 +08:00
commit 813bb02672
67 changed files with 5263 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Services;
use App\Services\Providers\OpenAIProvider;
use App\Services\Providers\ClaudeProvider;
use App\Services\Providers\NewAPIProvider;
class AIService
{
public static function streamChat(string $providerName, string $model, array $messages, array $options, callable $onChunk): void
{
$provider = self::getProvider($providerName);
$provider::stream($model, $messages, $options, $onChunk);
}
public static function getProvider(string $name): string
{
return match ($name) {
'openai' => OpenAIProvider::class,
'claude' => ClaudeProvider::class,
'newapi' => NewAPIProvider::class,
default => throw new \RuntimeException('不支持的供应商: ' . $name)
};
}
}

132
app/Services/Installer.php Normal file
View File

@@ -0,0 +1,132 @@
<?php
namespace App\Services;
use App\Config\Database;
use App\Models\Personality;
class Installer
{
public static function isInstalled(): bool
{
$configPath = dirname(__DIR__, 2) . '/config/db-config.json';
if (!file_exists($configPath)) {
return false;
}
try {
$db = Database::getInstance();
$stmt = $db->query("SHOW TABLES LIKE 'users'");
return $stmt->fetch() !== false;
} catch (\Throwable $e) {
return false;
}
}
public static function runMigration(): void
{
$db = Database::getInstance();
$db->exec("CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
role ENUM('admin','user') DEFAULT 'user',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$db->exec("CREATE TABLE IF NOT EXISTS sessions (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(100),
provider VARCHAR(50),
model VARCHAR(50),
system_prompt TEXT,
personality_id INT NULL,
thinking_mode TINYINT(1) DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_user_id (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$db->exec("CREATE TABLE IF NOT EXISTS messages (
id INT AUTO_INCREMENT PRIMARY KEY,
session_id INT NOT NULL,
role ENUM('user','assistant','system') NOT NULL,
content LONGTEXT,
file_info JSON,
thinking_content LONGTEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
INDEX idx_session_id (session_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$db->exec("CREATE TABLE IF NOT EXISTS config (
id INT AUTO_INCREMENT PRIMARY KEY,
config_key VARCHAR(50) UNIQUE NOT NULL,
config_value LONGTEXT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
$db->exec("CREATE TABLE IF NOT EXISTS personalities (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
prompt TEXT NOT NULL,
description TEXT,
icon VARCHAR(50),
is_preset TINYINT(1) DEFAULT 0,
created_by INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
}
public static function seedDefaults(): void
{
$db = Database::getInstance();
$presets = [
[
'name' => '智能助手',
'prompt' => '你是一个全能的智能助手,善于回答各种问题,提供准确、有帮助的回答。',
'icon' => '🤖',
],
[
'name' => '代码专家',
'prompt' => '你是一个专业的编程专家,精通多种编程语言和技术框架,擅长代码编写、调试和优化。',
'icon' => '💻',
],
[
'name' => '翻译官',
'prompt' => '你是一个专业的翻译官,精通中文、英文、日文等多种语言,提供准确、流畅的翻译服务。',
'icon' => '🌐',
],
[
'name' => '写作助手',
'prompt' => '你是一个专业的写作助手,擅长各类文体的写作,包括文章、报告、邮件、创意写作等。',
'icon' => '✍️',
],
[
'name' => '数学家',
'prompt' => '你是一个数学专家,精通各领域的数学知识,善于解答数学问题并提供详细的解题步骤。',
'icon' => '🔢',
],
];
$stmt = $db->prepare('INSERT INTO personalities (name, prompt, description, icon, is_preset) VALUES (:name, :prompt, :description, :icon, :is_preset)');
foreach ($presets as $preset) {
$stmt->execute([
'name' => $preset['name'],
'prompt' => $preset['prompt'],
'description' => null,
'icon' => $preset['icon'],
'is_preset' => 1,
]);
}
$configStmt = $db->prepare('INSERT INTO config (config_key, config_value) VALUES (:config_key, :config_value) ON DUPLICATE KEY UPDATE config_value = VALUES(config_value)');
$configStmt->execute([
'config_key' => 'providers',
'config_value' => '[]',
]);
}
}

View File

View File

@@ -0,0 +1,118 @@
<?php
namespace App\Services\Providers;
class ClaudeProvider
{
public static function stream(string $model, array $messages, array $options, callable $onChunk): void
{
$provider = $options['provider'];
$apiUrl = rtrim($provider['apiUrl'] ?? 'https://api.anthropic.com', '/');
$apiKey = $provider['apiKey'] ?? '';
$systemPrompt = $options['systemPrompt'] ?? '';
$thinkingMode = $options['thinkingMode'] ?? false;
$url = $apiUrl . '/v1/messages';
// Claude 的 system 消息通过单独参数传递,从 messages 中排除
$claudeMessages = array_values(array_filter($messages, function ($msg) {
return $msg['role'] !== 'system';
}));
$bodyData = [
'model' => $model,
'max_tokens' => $thinkingMode ? 16000 : 4096,
'messages' => $claudeMessages,
'stream' => true
];
if (!empty($systemPrompt)) {
$bodyData['system'] = $systemPrompt;
}
if ($thinkingMode) {
$bodyData['thinking'] = [
'type' => 'enabled',
'budget_tokens' => 10000
];
}
$body = json_encode($bodyData);
$headers = [
'x-api-key: ' . $apiKey,
'anthropic-version: 2023-06-01',
'Content-Type: application/json',
'Accept: text/event-stream'
];
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_TIMEOUT => 120,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_WRITEFUNCTION => function ($ch, $data) use ($onChunk) {
$lines = explode("\n", $data);
$currentEvent = '';
foreach ($lines as $line) {
$line = trim($line);
if ($line === '') {
$currentEvent = '';
continue;
}
if (str_starts_with($line, 'event: ')) {
$currentEvent = substr($line, 7);
continue;
}
if (str_starts_with($line, 'data: ')) {
$payload = substr($line, 6);
$json = json_decode($payload, true);
if (!$json) {
continue;
}
switch ($currentEvent) {
case 'content_block_delta':
if (isset($json['delta'])) {
$delta = $json['delta'];
if (isset($delta['type']) && $delta['type'] === 'thinking_delta' && isset($delta['thinking'])) {
$onChunk($delta['thinking'], 'thinking');
} elseif (isset($delta['text'])) {
$onChunk($delta['text'], 'content');
}
}
break;
case 'message_stop':
return strlen($data);
case 'error':
if (isset($json['error']['message'])) {
throw new \RuntimeException('Claude API错误: ' . $json['error']['message']);
}
break;
}
}
}
return strlen($data);
}
]);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
curl_close($ch);
throw new \RuntimeException('API请求失败: ' . $error);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new \RuntimeException('API返回错误状态码: ' . $httpCode);
}
}
}

View File

@@ -0,0 +1,11 @@
<?php
namespace App\Services\Providers;
class NewAPIProvider
{
public static function stream(string $model, array $messages, array $options, callable $onChunk): void
{
OpenAIProvider::stream($model, $messages, $options, $onChunk);
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Services\Providers;
class OpenAIProvider
{
public static function stream(string $model, array $messages, array $options, callable $onChunk): void
{
$provider = $options['provider'];
$apiUrl = rtrim($provider['apiUrl'] ?? 'https://api.openai.com', '/');
$apiKey = $provider['apiKey'] ?? '';
$systemPrompt = $options['systemPrompt'] ?? '';
$url = $apiUrl . '/v1/chat/completions';
if (!empty($systemPrompt)) {
array_unshift($messages, ['role' => 'system', 'content' => $systemPrompt]);
}
$body = json_encode([
'model' => $model,
'messages' => $messages,
'stream' => true
]);
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $apiKey,
'Content-Type: application/json',
'Accept: text/event-stream'
],
CURLOPT_TIMEOUT => 120,
CURLOPT_RETURNTRANSFER => false,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_WRITEFUNCTION => function ($ch, $data) use ($onChunk) {
$lines = explode("\n", $data);
foreach ($lines as $line) {
$line = trim($line);
if ($line === '') {
continue;
}
if (str_starts_with($line, 'data: ')) {
$payload = substr($line, 6);
if ($payload === '[DONE]') {
return strlen($data);
}
$json = json_decode($payload, true);
if ($json && isset($json['choices'][0]['delta']['content'])) {
$content = $json['choices'][0]['delta']['content'];
if ($content !== '') {
$onChunk($content);
}
}
}
}
return strlen($data);
}
]);
$result = curl_exec($ch);
if ($result === false) {
$error = curl_error($ch);
curl_close($ch);
throw new \RuntimeException('API请求失败: ' . $error);
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new \RuntimeException('API返回错误状态码: ' . $httpCode);
}
}
}