Browse Source

Merge pull request #2290 from visuddhinanda/development

Development
visuddhinanda 11 months ago
parent
commit
920156cef8

+ 93 - 0
api-v8/app/Console/Commands/InitCommentary.php

@@ -0,0 +1,93 @@
+<?php
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Models\Tag;
+use App\Models\TagMap;
+use App\Models\PaliText;
+use App\Models\PaliSentence;
+use App\Models\Commentary;
+use App\Models\RelatedParagraph;
+
+class InitCommentary extends Command
+{
+    /**
+     * The name and signature of the console command.
+     * php artisan init:commentary
+     * @var string
+     */
+    protected $signature = 'init:commentary {--book=}';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'init commentary sentences';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        //查询注释书标签
+        $tags = Tag::whereIn('name', ['aṭṭhakathā', 'ṭīkā'])->select('id')->get();
+        //查询段落编号
+        $paliId = TagMap::whereIn('tag_id', $tags)
+            ->where('table_name', 'pali_texts')
+            ->cursor();
+        foreach ($paliId as $key => $paraId) {
+            $book = PaliText::where('uid', $paraId->anchor_id)
+                ->where('level', 1)->first();
+            if (!$book) {
+                continue;
+            }
+            $paragraphs = PaliText::where('book', $book->book)
+                ->whereBetween('paragraph', [$book->paragraph, $book->paragraph + $book->chapter_len - 1])
+                ->get();
+            foreach ($paragraphs as $key => $para) {
+                $this->info($para->book . '-' . $para->paragraph);
+                $sentences = PaliSentence::where('book', $para->book)
+                    ->where('paragraph', $para->paragraph)
+                    ->get();
+                $del = Commentary::where('book1', $para->book)
+                    ->where('paragraph1', $para->paragraph)
+                    ->where('owner_id', config("mint.admin.root_uuid"))
+                    ->delete();
+                $csPara = RelatedParagraph::where('book', $para->book)
+                    ->where('para', $para->paragraph)
+                    ->first();
+                if ($csPara) {
+                    foreach ($sentences as $key => $sentence) {
+                        $new = new Commentary();
+                        $new->book1 = $sentence->book;
+                        $new->paragraph1 = $sentence->paragraph;
+                        $new->start1 = $sentence->word_begin;
+                        $new->end1 = $sentence->word_end;
+                        $new->editor_id = config("mint.admin.root_uuid");
+                        $new->owner_id = config("mint.admin.root_uuid");
+                        $new->p_number = $csPara->book_name . '-' . $csPara->para;
+                        $new->save();
+                    }
+                } else {
+                    $this->error('no relation paragraph');
+                }
+            }
+        }
+        $this->info('all done');
+        return 0;
+    }
+}

+ 26 - 23
api-v8/app/Console/Commands/MqAiTranslate.php

@@ -65,6 +65,9 @@ class MqAiTranslate extends Command
             Log::debug($queue . ' ai assistant token', ['user' => $first->model->uid]);
             $modelToken = AuthController::getUserToken($first->model->uid);
             Log::debug($queue . ' ai assistant token', ['token' => $modelToken]);
+
+            $this->setTaskStatus($first->task->info->id, 'running', $modelToken);
+
             $discussionUrl = config('app.url') . '/api/v2/discussion';
             $taskDiscussionData = [
                 'res_id' => $first->task->info->id,
@@ -97,7 +100,6 @@ class MqAiTranslate extends Command
                         ["role" => "system", "content" => $message->model->system_prompt ?? ''],
                         ["role" => "user", "content" => $message->prompt],
                     ],
-                    'prompt' => $message->prompt,
                     "temperature" => 0.7,
                     "stream" => false
                 ];
@@ -158,10 +160,7 @@ class MqAiTranslate extends Command
                     Log::debug($queue . ' reasoning=' . $reasoningContent);
                 }
 
-                //获取model token
-                Log::debug($queue . ' ai assistant token', ['user' => $message->model->uid]);
-                $token = AuthController::getUserToken($message->model->uid);
-                Log::debug($queue . ' ai assistant token', ['token' => $token]);
+
 
                 if ($message->task->info->category === 'translate') {
                     //写入句子库
@@ -170,7 +169,7 @@ class MqAiTranslate extends Command
                     $message->sentence->content = $responseContent;
                     $sentData[] = $message->sentence;
                     Log::info($queue . " sentence update {$url}");
-                    $response = Http::timeout(10)->withToken($token)->post($url, [
+                    $response = Http::timeout(10)->withToken($modelToken)->post($url, [
                         'sentences' => $sentData,
                     ]);
                     if ($response->failed()) {
@@ -188,7 +187,7 @@ class MqAiTranslate extends Command
                     //写入pr
                     $url = config('app.url') . '/api/v2/sentpr';
                     Log::info($queue . " sentence update {$url}");
-                    $response = Http::timeout(10)->withToken($token)->post($url, [
+                    $response = Http::timeout(10)->withToken($modelToken)->post($url, [
                         'book' => $message->sentence->book_id,
                         'para' => $message->sentence->paragraph,
                         'begin' => $message->sentence->word_start,
@@ -234,7 +233,7 @@ class MqAiTranslate extends Command
                     'type' => 'discussion',
                     'notification' => false,
                 ];
-                $response = Http::timeout(10)->withToken($token)->post($url, $data);
+                $response = Http::timeout(10)->withToken($modelToken)->post($url, $data);
                 if ($response->failed()) {
                     Log::error($queue . ' discussion create topic error', ['data' => $response->json()]);
                 } else {
@@ -254,7 +253,7 @@ class MqAiTranslate extends Command
                         foreach ($topicChildren as  $content) {
                             $data['content'] = $content;
                             Log::debug($queue . ' discussion child request', ['url' => $url, 'data' => $data]);
-                            $response = Http::timeout(10)->withToken($token)->post($url, $data);
+                            $response = Http::timeout(10)->withToken($modelToken)->post($url, $data);
                             if ($response->failed()) {
                                 Log::error($queue . ' discussion error', ['data' => $response->json()]);
                             } else {
@@ -280,7 +279,7 @@ class MqAiTranslate extends Command
                     'progress' => $progress,
                 ];
                 Log::debug($queue . ' task progress request', ['url' => $url, 'data' => $data]);
-                $response = Http::timeout(10)->withToken($token)->patch($url, $data);
+                $response = Http::timeout(10)->withToken($modelToken)->patch($url, $data);
                 if ($response->failed()) {
                     Log::error($queue . ' task progress error', ['data' => $response->json()]);
                 } else {
@@ -292,7 +291,7 @@ class MqAiTranslate extends Command
                     unset($taskDiscussionData['title']);
                     $taskDiscussionData['content'] = implode('\n', $taskDiscussionContent);
                     Log::debug($queue . ' task discussion child request', ['url' => $discussionUrl, 'data' => $data]);
-                    $response = Http::timeout(10)->withToken($token)->post($discussionUrl, $taskDiscussionData);
+                    $response = Http::timeout(10)->withToken($modelToken)->post($discussionUrl, $taskDiscussionData);
                     if ($response->failed()) {
                         Log::error($queue . ' task discussion error', ['data' => $response->json()]);
                     } else {
@@ -304,18 +303,7 @@ class MqAiTranslate extends Command
 
                 //任务完成 修改任务状态为 done
                 if ($progress === 100) {
-                    $url = config('app.url') . '/api/v2/task-status/' . $message->task->info->id;
-                    $data = [
-                        'status' => 'done',
-                    ];
-                    Log::debug($queue . ' task status request', ['url' => $url, 'data' => $data]);
-                    $response = Http::timeout(10)->withToken($token)->patch($url, $data);
-                    //判断状态码
-                    if ($response->failed()) {
-                        Log::error($queue . ' task status error', ['data' => $response->json()]);
-                    } else {
-                        Log::info($queue . ' task status done');
-                    }
+                    $this->setTaskStatus($message->task->info->id, 'done', $modelToken);
                 }
             }
             $this->info('ai translate task complete');
@@ -323,4 +311,19 @@ class MqAiTranslate extends Command
         });
         return 0;
     }
+    private function setTaskStatus($taskId, $status, $token)
+    {
+        $url = config('app.url') . '/api/v2/task-status/' . $taskId;
+        $data = [
+            'status' => 'done',
+        ];
+        Log::debug('ai_translate task status request', ['url' => $url, 'data' => $data]);
+        $response = Http::timeout(10)->withToken($token)->patch($url, $data);
+        //判断状态码
+        if ($response->failed()) {
+            Log::error('ai_translate task status error', ['data' => $response->json()]);
+        } else {
+            Log::info('ai_translate task status done');
+        }
+    }
 }

+ 0 - 6
api-v8/app/Http/Api/MdRender.php

@@ -3,13 +3,7 @@
 namespace App\Http\Api;
 
 use Illuminate\Support\Str;
-use mustache\mustache;
-use App\Models\DhammaTerm;
-use App\Models\PaliText;
 use App\Models\Channel;
-use App\Http\Controllers\CorpusController;
-use Illuminate\Support\Facades\Cache;
-use App\Tools\RedisClusters;
 use Illuminate\Support\Facades\Log;
 use App\Tools\Markdown;
 

+ 85 - 0
api-v8/app/Http/Controllers/CommentaryController.php

@@ -0,0 +1,85 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use App\Models\Commentary;
+use Illuminate\Http\Request;
+
+class CommentaryController extends Controller
+{
+    /**
+     * Display a listing of the resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function index()
+    {
+        //
+    }
+
+    /**
+     * Show the form for creating a new resource.
+     *
+     * @return \Illuminate\Http\Response
+     */
+    public function create()
+    {
+        //
+    }
+
+    /**
+     * Store a newly created resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @return \Illuminate\Http\Response
+     */
+    public function store(Request $request)
+    {
+        //
+    }
+
+    /**
+     * Display the specified resource.
+     *
+     * @param  \App\Models\Commentary  $commentary
+     * @return \Illuminate\Http\Response
+     */
+    public function show(Commentary $commentary)
+    {
+        //
+    }
+
+    /**
+     * Show the form for editing the specified resource.
+     *
+     * @param  \App\Models\Commentary  $commentary
+     * @return \Illuminate\Http\Response
+     */
+    public function edit(Commentary $commentary)
+    {
+        //
+    }
+
+    /**
+     * Update the specified resource in storage.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  \App\Models\Commentary  $commentary
+     * @return \Illuminate\Http\Response
+     */
+    public function update(Request $request, Commentary $commentary)
+    {
+        //
+    }
+
+    /**
+     * Remove the specified resource from storage.
+     *
+     * @param  \App\Models\Commentary  $commentary
+     * @return \Illuminate\Http\Response
+     */
+    public function destroy(Commentary $commentary)
+    {
+        //
+    }
+}

+ 1 - 1
api-v8/app/Http/Controllers/CorpusController.php

@@ -771,7 +771,7 @@ class CorpusController extends Controller
                             ];
                             $mdRender = new MdRender($options);
                             $newSent['html'] = $mdRender->convert($row->content, [$row->channel_uid]);
-                            Log::debug('md render', ['content' => $row->content, 'options' => $options, 'render' => $newSent['html']]);
+                            //Log::debug('md render', ['content' => $row->content, 'options' => $options, 'render' => $newSent['html']]);
                             break;
                     }
                 }

+ 3 - 3
api-v8/app/Http/Controllers/TaskStatusController.php

@@ -189,11 +189,11 @@ class TaskStatusController extends Controller
             if ($aiAssistant) {
                 $aiTask = Task::find($taskId);
                 $aiTask->executor_id = $aiAssistant->uid;
-                $aiTask->status = 'running';
+                $aiTask->status = 'queue';
                 $aiTask->save();
-                $this->pushChange('running', $taskId);
+                $this->pushChange('queue', $taskId);
                 $params = AiTaskPrepare::translate($taskId);
-                Log::debug('ai running', ['params' => $params]);
+                Log::debug('ai task', ['message' => count($params)]);
             }
         }
 

+ 11 - 0
api-v8/app/Models/Commentary.php

@@ -0,0 +1,11 @@
+<?php
+
+namespace App\Models;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\Model;
+
+class Commentary extends Model
+{
+    use HasFactory;
+}

+ 42 - 0
api-v8/database/migrations/2025_03_20_141911_create_commentaries_table.php

@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+class CreateCommentariesTable extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        Schema::create('commentaries', function (Blueprint $table) {
+            $table->id();
+            $table->integer('book1')->index()->comment('注释书号');
+            $table->integer('paragraph1')->index()->comment('注释书段落');
+            $table->integer('start1')->index()->comment('注释书句子单词起始');
+            $table->integer('end1')->index()->comment('注释书句子单词结束');
+            $table->integer('book2')->nullable()->index();
+            $table->integer('paragraph2')->nullable()->index();
+            $table->integer('start2')->nullable()->index();
+            $table->integer('end2')->nullable()->index();
+            $table->string('p_number')->index()->comment('段落号');
+            $table->uuid('owner_id')->index();
+            $table->uuid('editor_id')->index();
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('commentaries');
+    }
+}

+ 11 - 1
dashboard-v4/dashboard/src/components/api/task.ts

@@ -25,7 +25,17 @@ export type TTaskStatus =
   | "requested_restart"
   | "closed"
   | "canceled"
-  | "expired";
+  | "expired"
+  | "queue"
+  | "stop";
+export const StatusButtons: TTaskStatus[] = [
+  "pending",
+  "published",
+  "running",
+  "done",
+  "restarted",
+  "requested_restart",
+];
 export type TTaskType = "instance" | "workflow" | "group";
 
 export interface IProject {

+ 1 - 2
dashboard-v4/dashboard/src/components/task/ProjectTable.tsx

@@ -1,7 +1,7 @@
 import { ActionType, ProTable } from "@ant-design/pro-components";
 import { useIntl } from "react-intl";
 import { Link } from "react-router-dom";
-import { Alert, Badge, Button, message, Modal, Popover } from "antd";
+import { Badge, Button, message, Modal, Popover } from "antd";
 import { Dropdown } from "antd";
 import {
   ExclamationCircleOutlined,
@@ -10,7 +10,6 @@ import {
 } from "@ant-design/icons";
 
 import { delete_, get } from "../../request";
-import { TChannelType } from "../api/Channel";
 import { PublicityValueEnum } from "../studio/table";
 import { IDeleteResponse } from "../api/Article";
 import { useEffect, useRef, useState } from "react";

+ 15 - 1
dashboard-v4/dashboard/src/components/task/ProjectTask.tsx

@@ -24,7 +24,21 @@ export function update(input: ITaskData[], target: ITaskData[]): void {
     }
   }
 }
-
+// 更新函数
+export function updateNode(tree: ITaskData[], changed: ITaskData): boolean {
+  for (let i = 0; i < tree.length; i++) {
+    if (tree[i].id === changed.id) {
+      tree[i] = { ...tree[i], ...changed };
+      return true;
+    }
+    if (tree[i].children) {
+      const updated = updateNode(tree[i].children!, changed);
+      updated && console.debug("TaskList children", tree[i].children);
+      if (updated) return true;
+    }
+  }
+  return false;
+}
 interface IWidget {
   studioName?: string;
   projectId?: string;

+ 11 - 5
dashboard-v4/dashboard/src/components/task/TaskList.tsx

@@ -25,6 +25,7 @@ import TaskStatusButton from "./TaskStatusButton";
 import Executors from "./Executors";
 import Category from "./Category";
 import TaskListAdd from "./TaskListAdd";
+import { updateNode } from "./ProjectTask";
 
 const { Text } = Typography;
 
@@ -164,7 +165,7 @@ const TaskList = ({
   });
 
   const changeData = (data: ITaskData[]) => {
-    console.debug("task change", data);
+    /*    console.debug("task change", data);
     const update = (item: ITaskData): ITaskData => {
       item.children = item.children?.map(update);
       const found = data.find((t) => t.id === item.id);
@@ -173,10 +174,15 @@ const TaskList = ({
       }
       return item;
     };
-    const newData = dataSource.map(update);
-    setRawData(treeToList(newData));
-    setDataSource(newData);
-    onChange && onChange(JSON.parse(JSON.stringify(newData)));
+    const newData = dataSource.map(update);*/
+    let origin = JSON.parse(JSON.stringify(dataSource));
+    data.forEach((value) => {
+      updateNode(origin, value);
+    });
+    console.debug("TaskList change", dataSource, origin);
+    setRawData(treeToList(origin));
+    setDataSource(origin);
+    onChange && onChange(origin);
   };
 
   const columns: ProColumns<ITaskData>[] = [

+ 6 - 12
dashboard-v4/dashboard/src/components/task/TaskStatus.tsx

@@ -65,18 +65,12 @@ const TaskStatus = ({ task }: IWidget) => {
           defaultMessage: "unknown",
         })}
       </Tag>
-      {task?.status === "running" ? (
-        progress && progress > 0 ? (
-          <div style={{ display: "inline-block", width: 80 }}>
-            <Tooltip title={`${progress}%`}>
-              <Progress percent={progress} size="small" showInfo={false} />
-            </Tooltip>
-          </div>
-        ) : task?.executor?.roles?.includes("ai") ? (
-          <>任务排队中</>
-        ) : (
-          <></>
-        )
+      {task?.status === "running" && task?.executor?.roles?.includes("ai") ? (
+        <div style={{ display: "inline-block", width: 80 }}>
+          <Tooltip title={`${progress}%`}>
+            <Progress percent={progress} size="small" showInfo={false} />
+          </Tooltip>
+        </div>
       ) : (
         <></>
       )}

+ 14 - 42
dashboard-v4/dashboard/src/components/task/TaskStatusButton.tsx

@@ -18,6 +18,7 @@ import {
   ITaskData,
   ITaskListResponse,
   ITaskUpdateRequest,
+  StatusButtons,
   TTaskStatus,
 } from "../api/task";
 import { patch } from "../../request";
@@ -88,6 +89,7 @@ const TaskStatusButton = ({
     case "running":
       menuEnable = [
         "done",
+        "stop",
         requested_restart_enable ? "requested_restart" : "done",
       ];
       break;
@@ -100,52 +102,22 @@ const TaskStatusButton = ({
     case "requested_restart":
       menuEnable = ["done"];
       break;
+    case "queue":
+      menuEnable = ["stop"];
+      break;
   }
-  const items: IStatusMenu[] = [
-    {
-      key: "pending",
-      label: intl.formatMessage({
-        id: "buttons.task.status.change.to.pending",
-      }),
-      disabled: task?.type === "instance" && !menuEnable.includes("pending"),
-    },
-    {
-      key: "published",
-      label: intl.formatMessage({
-        id: "buttons.task.status.change.to.published",
-      }),
-      disabled: task?.type === "instance" && !menuEnable.includes("published"),
-    },
-    {
-      key: "running",
-      label: intl.formatMessage({
-        id: `buttons.task.status.change.to.running`,
-      }),
-      disabled: task?.type === "instance" && !menuEnable.includes("running"),
-    },
-    {
-      key: "done",
-      label: intl.formatMessage({
-        id: `buttons.task.status.change.to.done`,
-      }),
-      disabled: task?.type === "instance" && !menuEnable.includes("done"),
-    },
-    {
-      key: "restarted",
-      label: intl.formatMessage({
-        id: `buttons.task.status.change.to.restarted`,
-      }),
-      disabled: task?.type === "instance" && !menuEnable.includes("restarted"),
-    },
-    {
-      key: "requested_restart",
+
+  const items: IStatusMenu[] = StatusButtons.map((item) => {
+    return {
+      key: item,
       label: intl.formatMessage({
-        id: `buttons.task.status.change.to.requested_restart`,
+        id: `buttons.task.status.change.to.${item}`,
       }),
       disabled:
-        task?.type === "instance" && !menuEnable.includes("requested_restart"),
-    },
-  ];
+        task?.type === "instance" && !menuEnable.includes(item as TTaskStatus),
+    };
+  });
+
   const menuProps = {
     items: items,
     onClick: handleMenuClick,

+ 1 - 0
dashboard-v4/dashboard/src/locales/en-US/label.ts

@@ -67,6 +67,7 @@ const items = {
   "labels.task.status.closed": "closed",
   "labels.task.status.canceled": "canceled",
   "labels.task.status.expired": "expired",
+  "labels.task.status.queue": "queue",
   "labels.filter": "filter",
   "labels.participants": "participants",
   "labels.task.category": "task category",

+ 1 - 0
dashboard-v4/dashboard/src/locales/zh-Hans/label.ts

@@ -75,6 +75,7 @@ const items = {
   "labels.task.status.closed": "已关闭",
   "labels.task.status.canceled": "已取消",
   "labels.task.status.expired": "已过期",
+  "labels.task.status.queue": "排队中",
   "labels.filter": "过滤器",
   "labels.participants": "参与者",
   "labels.task.category": "任务类型",