QuestionGeneration.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace App\Filament\Pages;
  3. use App\Services\QuestionBankService;
  4. use App\Services\KnowledgeGraphService;
  5. use BackedEnum;
  6. use Filament\Notifications\Notification;
  7. use Filament\Pages\Page;
  8. use UnitEnum;
  9. use Livewire\Attributes\Computed;
  10. use Illuminate\Support\Facades\Session;
  11. use Illuminate\Support\Facades\Http;
  12. class QuestionGeneration extends Page
  13. {
  14. protected static ?string $title = '题目生成';
  15. protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-sparkles';
  16. protected static ?string $navigationLabel = '题目生成';
  17. protected static string|UnitEnum|null $navigationGroup = '资源';
  18. protected static ?int $navigationSort = 21;
  19. protected string $view = 'filament.pages.question-generation';
  20. public ?string $generateKpCode = null;
  21. public array $selectedSkills = [];
  22. public int $questionCount = 100;
  23. public ?string $generateDifficulty = null;
  24. public ?string $generateType = null;
  25. public ?string $promptTemplate = null;
  26. public bool $isGenerating = false;
  27. public ?string $currentTaskId = null;
  28. public int $currentTaskProgress = 0;
  29. public ?string $currentTaskMessage = null;
  30. #[Computed(cache: false)]
  31. public function knowledgePointOptions(): array
  32. {
  33. return app(\App\Services\QuestionServiceApi::class)->getKnowledgePointOptions();
  34. }
  35. #[Computed(cache: false)]
  36. public function skillsOptions(): array
  37. {
  38. if (!$this->generateKpCode) {
  39. return [];
  40. }
  41. $service = app(KnowledgeGraphService::class);
  42. return $service->getSkillsByKnowledgePoint($this->generateKpCode);
  43. }
  44. #[Computed(cache: false)]
  45. public function questionTypeOptions(): array
  46. {
  47. return [
  48. 'CHOICE' => '单选题',
  49. 'MULTIPLE_CHOICE' => '多选题',
  50. 'FILL_IN_THE_BLANK' => '填空题',
  51. 'CALCULATION' => '计算题',
  52. 'WORD_PROBLEM' => '应用题',
  53. 'PROOF' => '证明题',
  54. ];
  55. }
  56. #[Computed(cache: false)]
  57. public function promptOptions(): array
  58. {
  59. $service = app(\App\Services\QuestionServiceApi::class);
  60. $prompts = $service->listPrompts(type: 'question_generation', active: 'yes');
  61. // 只展示激活的题目生成模板
  62. $options = [];
  63. foreach ($prompts as $prompt) {
  64. $label = $prompt['template_name'];
  65. if (!empty($prompt['description'])) {
  66. $label .= ' - ' . (is_string($prompt['description']) ? $prompt['description'] : json_encode($prompt['description']));
  67. }
  68. $options[$prompt['template_name']] = $label;
  69. }
  70. // 自动选择第一个激活模板
  71. if (!$this->promptTemplate && !empty($options)) {
  72. $this->promptTemplate = array_key_first($options);
  73. }
  74. return $options;
  75. }
  76. public function updatedGenerateKpCode(): void
  77. {
  78. // 选择新知识点时清空技能选择
  79. $this->selectedSkills = [];
  80. }
  81. public function toggleAllSkills(): void
  82. {
  83. $skillsOptions = $this->skillsOptions;
  84. $skillsCount = count($skillsOptions);
  85. \Log::info('[ToggleAllSkills] Called', [
  86. 'skillsCount' => $skillsCount,
  87. 'selectedSkillsCount' => count($this->selectedSkills),
  88. 'skillsOptions' => array_column($skillsOptions, 'code'),
  89. 'selectedSkills' => $this->selectedSkills
  90. ]);
  91. if ($skillsCount === 0) {
  92. \Log::info('[ToggleAllSkills] No skills available, returning');
  93. return;
  94. }
  95. // 获取所有技能的编码列表
  96. $allSkillCodes = array_values(array_unique(array_column($skillsOptions, 'code')));
  97. // 检查当前选中的技能是否等于全部技能
  98. if (count($this->selectedSkills) === $skillsCount &&
  99. count(array_intersect($this->selectedSkills, $allSkillCodes)) === $skillsCount) {
  100. // 如果已全选,则清空
  101. $this->selectedSkills = [];
  102. \Log::info('[ToggleAllSkills] Deselecting all skills');
  103. } else {
  104. // 否则全选
  105. $this->selectedSkills = $allSkillCodes;
  106. \Log::info('[ToggleAllSkills] Selecting all skills', ['newSelection' => $this->selectedSkills]);
  107. }
  108. }
  109. public function executeGenerate()
  110. {
  111. if ($this->isGenerating) {
  112. return;
  113. }
  114. if (!$this->generateKpCode) {
  115. Notification::make()->title('请选择知识点')->danger()->send();
  116. return;
  117. }
  118. $skillsOptions = $this->skillsOptions;
  119. $skillsCount = count($skillsOptions);
  120. if ($skillsCount > 0 && empty($this->selectedSkills)) {
  121. Notification::make()->title('请选择至少一个技能')->danger()->send();
  122. return;
  123. }
  124. $this->isGenerating = true;
  125. $this->currentTaskId = null;
  126. try {
  127. $service = app(QuestionBankService::class);
  128. $callbackUrl = route('api.questions.callback');
  129. \Log::info("[QuestionGen] 开始异步生成,callback URL: " . $callbackUrl);
  130. // 生成参数(交由服务发送,带超时与重试)
  131. $params = [
  132. 'kp_code' => $this->generateKpCode,
  133. 'skills' => $this->selectedSkills,
  134. 'count' => $this->questionCount,
  135. 'difficulty' => $this->generateDifficulty,
  136. 'type' => $this->generateType,
  137. 'prompt_template' => $this->promptTemplate ?? null
  138. ];
  139. // 添加回调 URL
  140. $params['callback_url'] = $callbackUrl;
  141. // 通过服务请求,期望快速返回 task_id(超时与重试由服务配置)
  142. $response = $service->generateIntelligentQuestions($params, $callbackUrl);
  143. \Log::info("[QuestionGen] 生成请求已发送", [
  144. 'response' => $response,
  145. 'kp_code' => $this->generateKpCode,
  146. 'skills' => $this->selectedSkills,
  147. 'count' => $this->questionCount,
  148. ]);
  149. // 快速跳转到题目管理页,等待回调提示
  150. $redirectUrl = "/admin/question-management";
  151. $this->js("window.location.href = '{$redirectUrl}';");
  152. return;
  153. } catch (\Illuminate\Http\Client\ConnectionException $e) {
  154. $this->isGenerating = false;
  155. \Log::error("[QuestionGen] 连接异常: " . $e->getMessage());
  156. Notification::make()
  157. ->title('连接AI服务失败')
  158. ->body('请检查AI服务是否正常运行')
  159. ->danger()
  160. ->send();
  161. } catch (\Exception $e) {
  162. $this->isGenerating = false;
  163. \Log::error("[QuestionGen] 生成异常: " . $e->getMessage());
  164. Notification::make()
  165. ->title('请求异常')
  166. ->body($e->getMessage())
  167. ->danger()
  168. ->send();
  169. }
  170. }
  171. public function forceCloseStatusBar(string $taskId): void
  172. {
  173. \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
  174. $this->isGenerating = false;
  175. $this->currentTaskId = null;
  176. Notification::make()
  177. ->title('⚠️ 任务超时')
  178. ->body('生成任务可能已完成,请刷新页面查看')
  179. ->warning()
  180. ->persistent()
  181. ->send();
  182. }
  183. }