refactor: 修复知识库和操作指引

This commit is contained in:
2026-03-13 14:32:37 +08:00
parent bbe8e60646
commit 58f42de9df
88 changed files with 3387 additions and 2472 deletions

View File

@@ -41,6 +41,10 @@ class DocumentSearchService
$searchBuilder->where('uploaded_by', $filters['uploaded_by']);
}
if (!empty($filters['knowledge_base_id'])) {
$searchBuilder->where('knowledge_base_id', $filters['knowledge_base_id']);
}
// 执行搜索并获取结果
$results = $searchBuilder->get();
@@ -103,6 +107,7 @@ class DocumentSearchService
'markdown_content' => $document->getMarkdownContent(),
'type' => $document->type,
'group_id' => $document->group_id,
'knowledge_base_id' => $document->knowledge_base_id,
'uploaded_by' => $document->uploaded_by,
'created_at' => $document->created_at?->timestamp,
'updated_at' => $document->updated_at?->timestamp,

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Services;
use App\Models\Terminal;
use App\Models\Document;
use Illuminate\Support\Facades\Log;
class KnowledgeContextService
{
private const MAX_CONTEXT_LENGTH = 2000;
private const TOP_K = 5;
/**
* 搜索终端关联知识库中的文档
*
* @param Terminal $terminal
* @param string $query
* @return array{context: string, sources: array}
*/
public function search(Terminal $terminal, string $query): array
{
$knowledgeBaseIds = $terminal->knowledgeBases->pluck('id')->toArray();
if (empty($knowledgeBaseIds)) {
return [
'context' => '',
'sources' => [],
];
}
try {
// 使用 Scout/Meilisearch 原生过滤(与 DocumentSearchService 一致)
$documents = Document::search($query)
->whereIn('knowledge_base_id', $knowledgeBaseIds)
->take(self::TOP_K)
->get();
} catch (\Exception $e) {
Log::warning('Knowledge search failed', [
'query' => $query,
'error' => $e->getMessage(),
]);
return [
'context' => '',
'sources' => [],
];
}
if ($documents->isEmpty()) {
return [
'context' => '',
'sources' => [],
];
}
$context = '';
$sources = [];
foreach ($documents as $document) {
$snippet = $this->extractSnippet($document);
if (mb_strlen($context) + mb_strlen($snippet) > self::MAX_CONTEXT_LENGTH) {
break;
}
$context .= $snippet . "\n\n";
$sources[] = [
'id' => $document->id,
'title' => $document->title,
'knowledge_base' => $document->knowledgeBase?->name,
];
}
return [
'context' => trim($context),
'sources' => $sources,
];
}
/**
* 从文档中提取摘要片段
*/
private function extractSnippet($document): string
{
$content = $document->markdown_preview ?? $document->description ?? '';
if (mb_strlen($content) <= 500) {
return "{$document->title}\n{$content}";
}
return "{$document->title}\n" . mb_substr($content, 0, 500) . '...';
}
}

View File

@@ -1,222 +0,0 @@
<?php
namespace App\Services;
use App\Models\SopTemplate;
use App\Models\SopTemplateVersion;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\ValidationException;
class SopTemplateService
{
/**
* 导出模板为 JSON 格式
*
* @param SopTemplate $template
* @return string
*/
public function exportToJson(SopTemplate $template): string
{
$data = [
'template' => [
'name' => $template->name,
'description' => $template->description,
'category' => $template->category,
'tags' => $template->tags,
'version' => $template->version,
'applicable_departments' => $template->applicable_departments,
'applicable_positions' => $template->applicable_positions,
],
'steps' => $template->steps->map(function ($step) {
return [
'step_number' => $step->step_number,
'title' => $step->title,
'content' => $step->content,
'sort_order' => $step->sort_order,
'is_required' => $step->is_required,
'interactive_tasks' => $step->interactiveTasks->map(function ($task) {
return [
'task_type' => $task->task_type,
'task_config' => $task->task_config,
'validation_rules' => $task->validation_rules,
'timeout_seconds' => $task->timeout_seconds,
'is_required' => $task->is_required,
];
})->toArray(),
];
})->toArray(),
'exported_at' => now()->toIso8601String(),
'exported_by' => auth()->user()?->name,
];
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}
/**
* JSON 导入模板
*
* @param string $json
* @return SopTemplate
* @throws ValidationException
*/
public function importFromJson(string $json): SopTemplate
{
$data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw ValidationException::withMessages([
'file' => ['无效的 JSON 格式'],
]);
}
// 验证数据结构
$validator = Validator::make($data, [
'template' => 'required|array',
'template.name' => 'required|string|max:255',
'template.version' => 'required|string|max:50',
'steps' => 'required|array|min:1',
'steps.*.step_number' => 'required|integer',
'steps.*.title' => 'required|string|max:255',
]);
if ($validator->fails()) {
throw new ValidationException($validator);
}
return DB::transaction(function () use ($data) {
// 创建模板
$template = SopTemplate::create([
'name' => $data['template']['name'],
'description' => $data['template']['description'] ?? null,
'category' => $data['template']['category'] ?? null,
'tags' => $data['template']['tags'] ?? [],
'version' => $data['template']['version'],
'status' => 'draft',
'applicable_departments' => $data['template']['applicable_departments'] ?? [],
'applicable_positions' => $data['template']['applicable_positions'] ?? [],
'created_by' => auth()->id(),
]);
// 创建步骤
foreach ($data['steps'] as $stepData) {
$step = $template->steps()->create([
'step_number' => $stepData['step_number'],
'title' => $stepData['title'],
'content' => $stepData['content'] ?? null,
'sort_order' => $stepData['sort_order'] ?? $stepData['step_number'],
'is_required' => $stepData['is_required'] ?? true,
]);
// 创建交互任务
if (!empty($stepData['interactive_tasks'])) {
foreach ($stepData['interactive_tasks'] as $taskData) {
$step->interactiveTasks()->create([
'task_type' => $taskData['task_type'],
'task_config' => $taskData['task_config'] ?? [],
'validation_rules' => $taskData['validation_rules'] ?? [],
'timeout_seconds' => $taskData['timeout_seconds'] ?? null,
'is_required' => $taskData['is_required'] ?? true,
]);
}
}
}
return $template;
});
}
/**
* 发布模板
*
* @param SopTemplate $template
* @param string|null $changeLog
* @return void
*/
public function publish(SopTemplate $template, ?string $changeLog = null): void
{
// 创建版本快照
$this->createVersion($template, $changeLog);
// 更新状态
$template->update([
'status' => 'published',
'published_at' => now(),
]);
}
/**
* 创建版本快照
*
* @param SopTemplate $template
* @param string|null $changeLog
* @return SopTemplateVersion
*/
public function createVersion(SopTemplate $template, ?string $changeLog = null): SopTemplateVersion
{
return SopTemplateVersion::create([
'sop_template_id' => $template->id,
'version' => $template->version,
'change_log' => $changeLog ?? '版本快照',
'content_snapshot' => [
'template' => $template->toArray(),
'steps' => $template->steps->map(function ($step) {
return array_merge($step->toArray(), [
'interactive_tasks' => $step->interactiveTasks->toArray(),
]);
})->toArray(),
],
'created_by' => auth()->id(),
'created_at' => now(),
]);
}
/**
* 归档模板
*
* @param SopTemplate $template
* @return void
*/
public function archive(SopTemplate $template): void
{
$template->update([
'status' => 'archived',
]);
}
/**
* 复制模板
*
* @param SopTemplate $template
* @param string $newName
* @return SopTemplate
*/
public function duplicate(SopTemplate $template, string $newName): SopTemplate
{
return DB::transaction(function () use ($template, $newName) {
// 复制模板
$newTemplate = $template->replicate();
$newTemplate->name = $newName;
$newTemplate->status = 'draft';
$newTemplate->published_at = null;
$newTemplate->created_by = auth()->id();
$newTemplate->save();
// 复制步骤
foreach ($template->steps as $step) {
$newStep = $step->replicate();
$newStep->sop_template_id = $newTemplate->id;
$newStep->save();
// 复制交互任务
foreach ($step->interactiveTasks as $task) {
$newTask = $task->replicate();
$newTask->sop_step_id = $newStep->id;
$newTask->save();
}
}
return $newTemplate;
});
}
}