QuestionGeneration.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // 获取 base URL(通过公共方法或配置)
  142. $baseUrl = config('services.question_bank.base_url', env('QUESTION_BANK_API_BASE', 'http://localhost:5015'));
  143. $baseUrl = rtrim($baseUrl, '/');
  144. if (!str_ends_with($baseUrl, '/api')) {
  145. $baseUrl .= '/api';
  146. }
  147. // 直接发送异步请求,不使用队列
  148. try {
  149. // 先立即跳转,避免阻塞
  150. $redirectUrl = "/admin/question-management";
  151. $this->js("window.location.href = '{$redirectUrl}';");
  152. // 使用 stream 方式快速发送请求
  153. $ch = curl_init();
  154. curl_setopt($ch, CURLOPT_URL, $baseUrl . '/ai/generate-intelligent-questions');
  155. curl_setopt($ch, CURLOPT_POST, true);
  156. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params));
  157. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  158. 'Content-Type: application/json',
  159. 'Accept: application/json'
  160. ]);
  161. curl_setopt($ch, CURLOPT_TIMEOUT, 1); // 1秒超时,只确保请求发出
  162. curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  163. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  164. curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
  165. curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  166. // 异步执行,不等待响应
  167. curl_exec($ch);
  168. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  169. curl_close($ch);
  170. \Log::info("[QuestionGen] 异步请求已发送", [
  171. 'http_code' => $httpCode
  172. ]);
  173. return; // 立即返回
  174. } catch (\Exception $e) {
  175. $this->isGenerating = false;
  176. \Log::error("[QuestionGen] 发送异步请求失败", [
  177. 'error' => $e->getMessage()
  178. ]);
  179. Notification::make()
  180. ->title('❌ 请求发送失败')
  181. ->body('请检查网络连接并重试')
  182. ->danger()
  183. ->send();
  184. }
  185. } catch (\Illuminate\Http\Client\ConnectionException $e) {
  186. $this->isGenerating = false;
  187. \Log::error("[QuestionGen] 连接异常: " . $e->getMessage());
  188. Notification::make()
  189. ->title('连接AI服务失败')
  190. ->body('请检查AI服务是否正常运行')
  191. ->danger()
  192. ->send();
  193. } catch (\Exception $e) {
  194. $this->isGenerating = false;
  195. \Log::error("[QuestionGen] 生成异常: " . $e->getMessage());
  196. Notification::make()
  197. ->title('请求异常')
  198. ->body($e->getMessage())
  199. ->danger()
  200. ->send();
  201. }
  202. }
  203. public function forceCloseStatusBar(string $taskId): void
  204. {
  205. \Log::info("[QuestionGen] 强制关闭状态栏: {$taskId}");
  206. $this->isGenerating = false;
  207. $this->currentTaskId = null;
  208. Notification::make()
  209. ->title('⚠️ 任务超时')
  210. ->body('生成任务可能已完成,请刷新页面查看')
  211. ->warning()
  212. ->persistent()
  213. ->send();
  214. }
  215. }