Browse Source

feat: poem handle progress change & resolve true/false

未来全栈 4 months ago
parent
commit
bbbeaed093

+ 4 - 0
src/agent/agent.start.ts

@@ -9,6 +9,10 @@ for (let index = 0; index < taskStepList.length; index++) {
     let step = taskStepList[index];
     let result = await step.handle()
     if(result == false) {break;} 
+
+    if(step.error){
+        break;
+    }
     if(!step.error){ // 任务执行正常,无Error
 
     }

+ 1 - 1
src/agent/agent.task.ts

@@ -64,7 +64,7 @@ export class AgentTaskStep{
 
     /** 
      * 任务执行函数
-     * @desc 函数内必须有明确的完成、结束情形,函数内必须有进度的变化指示 从0.00 - 1.00
+     * @desc 函数内必须有明确的完成、结束情形,函数内必须有进度的变化指示 progress从0.00 - 1.00
      */
     progress:number = 0;
     handle():Promise<any>|any{

+ 40 - 24
src/agent/tasks/poem/poem-desc.ts

@@ -8,32 +8,48 @@ export function TaskPoemPictureDesc(options:{
     shareData:any}
     ):AgentTaskStep{
         let task1 = new AgentTaskStep({title:"意境分析",shareData:options.shareData})
-        task1.handle = async ()=>{
-        // 获取用户输入的诗词
-        let userInput = await getUserInput(options.modalCtrl,{fieldsArray:[
-            {name:"诗文内容",type:"text",desc:"诗文句子或段落"}
-        ]});
-        console.log("已获取用户输入:",userInput)
-        console.log("已获取诗文内容:",userInput['诗文内容'])
+        task1.handle = ()=>{
+            return new Promise(async (resolve,reject)=>{
+                // 获取用户输入的诗词
+                let userInput = await getUserInput(options.modalCtrl,{fieldsArray:[
+                    {name:"诗文内容",type:"text",desc:"诗文句子或段落"}
+                ]});
+                console.log("已获取用户输入:",userInput)
+                if(userInput){
+                    console.log("已获取诗文内容:",userInput['诗文内容'])
+                }
+                if(!userInput?.['诗文内容']){
+                    task1.error = "缺少诗文内容,请重新开始"
+                    resolve(false);
+                }
 
-            // 文本生成
-        let PromptTemplate = `您是一名专业的美术画家,请您根据古诗文的内容,将其描述的画面、场景、人物、物品等用最简短的语言表达,直接写出画面,并且以中国的古风意境为主
-        诗文如下:
-        ${userInput['诗文内容']}
-        `
-        let completion = new FmodeChatCompletion([
-            {role:"system",content:""},
-            {role:"user",content:PromptTemplate}
-            ])
-            completion.sendCompletion().subscribe((message:any)=>{
-            // 打印消息体
-            console.log(message.content)
-            // 赋值消息内容给组件内属性
-            options.shareData.PictureDescResult = message.content
-            if(message.complete){ // 判断message为完成状态,则设置isComplete为完成
-                task1.progress = 1
-            }
+                    // 文本生成
+                let PromptTemplate = `您是一名专业的美术画家,请您根据古诗文的内容,将其描述的画面、场景、人物、物品等用最简短的语言表达,直接写出画面,并且以中国的古风意境为主
+                诗文如下:
+                ${userInput['诗文内容']}
+                `
+                let completion = new FmodeChatCompletion([
+                    {role:"system",content:""},
+                    {role:"user",content:PromptTemplate}
+                    ])
+                    completion.sendCompletion().subscribe((message:any)=>{
+                        if(task1.progress < 0.5){
+                            task1.progress += 0.1
+                        }
+                        if(task1.progress >= 0.5){
+                            task1.progress += 0.01
+                        }
+                        // 打印消息体
+                        // console.log(message.content)
+                        // 赋值消息内容给组件内属性
+                        options.shareData.PictureDescResult = message.content
+                        if(message.complete){ // 判断message为完成状态,则设置isComplete为完成
+                            task1.progress = 1
+                            resolve(true)
+                        }
+                })
             })
+
         }
         return task1
 }

+ 29 - 18
src/agent/tasks/poem/poem-picture.ts

@@ -7,26 +7,37 @@ export function TaskPoemPictureCreate(options:{
     modalCtrl:ModalController
     shareData:any}):AgentTaskStep{
     let task2 = new AgentTaskStep({title:"意境绘制",shareData:options.shareData})
-    task2.handle = async ()=>{
-      // let userInput = await getUserInput(options.modalCtrl,{fieldsArray:[
-      //   {name:"绘图要求",type:"text",desc:"画风、构图等等"}
-      // ]});
-      // console.log(userInput)
-      console.log("意境绘制:执行过程")
+    task2.handle = ()=>{
+      return new Promise(resolve=>{
+        // let userInput = await getUserInput(options.modalCtrl,{fieldsArray:[
+        //   {name:"绘图要求",type:"text",desc:"画风、构图等等"}
+        // ]});
+        // console.log(userInput)
+        console.log("意境绘制:执行过程")
 
-      options.shareData.PictureDescResult
-      let imagineWork = new ImagineWork();
-      // 图片生成
-      let PicturePrompt = `${options.shareData.PictureDescResult}\n风格:中国古风。画面不带任何文字。`
-      let imgOptions:DalleOptions = {prompt:PicturePrompt}
-      imagineWork?.draw(imgOptions).subscribe((work:any)=>{
-          console.log("imagineWork",work?.toJSON())
-          console.log("images",work?.get("images"))
-          if(work?.get("images")?.length){
-            options.shareData.images = work?.get("images");
-            task2.progress = 1
-          }
+        if(!options.shareData.PictureDescResult){
+          task2.error = "缺少古风描述结果,请重新执行。"
+          resolve(false)
+        }
+        let imagineWork = new ImagineWork();
+        // 图片生成
+        let PicturePrompt = `${options.shareData.PictureDescResult}\n风格:中国古风。画面不带任何文字。`
+        let imgOptions:DalleOptions = {prompt:PicturePrompt}
+        let countDownInt = setInterval(()=>{
+          task2.progress += 0.01
+        },1000)
+        imagineWork?.draw(imgOptions).subscribe((work:any)=>{
+            console.log("imagineWork",work?.toJSON())
+            console.log("images",work?.get("images"))
+            if(work?.get("images")?.length){
+              options.shareData.images = work?.get("images");
+              clearInterval(countDownInt);
+              task2.progress = 1
+              resolve(true)
+            }
+        })
       })
+
     }
     return task2
 }

+ 6 - 2
src/app/tab1/tab1.page.html

@@ -14,7 +14,7 @@
   @for(step of taskList;track step.title;){
     <div>
       <!-- 待开始 -->
-      @if(step.progress==0){
+      @if(step.progress==0 && !step.error){
         <ion-icon name="radio-button-off-outline"></ion-icon>
       }
       <!-- 进行中 -->
@@ -29,7 +29,11 @@
       @if(step.error){
         <ion-icon name="close-circle-outline"></ion-icon>
       }
-      {{step.title}} {{step.progress}}
+      {{step.title}}  @if(step.progress){<span>{{step.progress * 100 | number:"2.0-0"}}%</span>}
+
+      @if(step.error){
+        <span style="color:red;">{{step.error}}</span>
+      }
     </div>   
   }
   </ul>

+ 2 - 1
src/app/tab1/tab1.page.ts

@@ -9,6 +9,7 @@ import { getUserInput } from 'src/agent/agent.input';
 import { FmodeChatCompletion, ImagineWork, DalleOptions } from "fmode-ng";
 import { TaskPoemPictureDesc } from 'src/agent/tasks/poem/poem-desc';
 import { TaskPoemPictureCreate } from 'src/agent/tasks/poem/poem-picture';
+import { DecimalPipe } from '@angular/common';
 addIcons({radioButtonOffOutline,reloadOutline,checkmarkCircleOutline,closeCircleOutline})
 @Component({
   selector: 'app-tab1',
@@ -16,7 +17,7 @@ addIcons({radioButtonOffOutline,reloadOutline,checkmarkCircleOutline,closeCircle
   styleUrls: ['tab1.page.scss'],
   standalone: true,
   imports: [IonHeader, IonToolbar, IonTitle, IonContent, IonButton,
-    IonIcon
+    IonIcon,DecimalPipe
   ],
 })
 export class Tab1Page  {