Sfoglia il codice sorgente

feat(interaction): support slash command routing

Ma 7 mesi fa
parent
commit
d79c871a5e

+ 21 - 0
packages/core/src/__tests__/interaction-nl-router.test.ts

@@ -72,4 +72,25 @@ describe("interaction natural-language router", () => {
       mode: "manual",
       mode: "manual",
     });
     });
   });
   });
+
+  it("maps slash commands for direct control", () => {
+    expect(routeNaturalLanguageIntent("/write", { activeBookId: "harbor" })).toEqual({
+      intent: "write_next",
+      bookId: "harbor",
+    });
+    expect(routeNaturalLanguageIntent("/mode auto", { activeBookId: "harbor" })).toEqual({
+      intent: "switch_mode",
+      mode: "auto",
+    });
+    expect(routeNaturalLanguageIntent("/rewrite 3", { activeBookId: "harbor" })).toEqual({
+      intent: "rewrite_chapter",
+      bookId: "harbor",
+      chapterNumber: 3,
+    });
+    expect(routeNaturalLanguageIntent("/focus bring it back to the old case", { activeBookId: "harbor" })).toEqual({
+      intent: "update_focus",
+      bookId: "harbor",
+      instruction: "bring it back to the old case",
+    });
+  });
 });
 });

+ 33 - 0
packages/core/src/interaction/nl-router.ts

@@ -19,6 +19,13 @@ export function routeNaturalLanguageIntent(
     };
     };
   }
   }
 
 
+  if (/^\/write$/i.test(trimmed)) {
+    return {
+      intent: "write_next",
+      ...(bookId ? { bookId } : {}),
+    };
+  }
+
   if (/^(pause|pause this book|暂停|暂停这本书)$/i.test(trimmed)) {
   if (/^(pause|pause this book|暂停|暂停这本书)$/i.test(trimmed)) {
     return {
     return {
       intent: "pause_book",
       intent: "pause_book",
@@ -26,6 +33,14 @@ export function routeNaturalLanguageIntent(
     };
     };
   }
   }
 
 
+  const modeCommand = trimmed.match(/^\/mode\s+(auto|semi|manual)$/i);
+  if (modeCommand) {
+    return {
+      intent: "switch_mode",
+      mode: modeCommand[1]!.toLowerCase() as "auto" | "semi" | "manual",
+    };
+  }
+
   if (/(全自动|auto mode|switch to auto|切换到全自动)/i.test(trimmed)) {
   if (/(全自动|auto mode|switch to auto|切换到全自动)/i.test(trimmed)) {
     return {
     return {
       intent: "switch_mode",
       intent: "switch_mode",
@@ -47,6 +62,24 @@ export function routeNaturalLanguageIntent(
     };
     };
   }
   }
 
 
+  const slashRewrite = trimmed.match(/^\/rewrite\s+(\d+)$/i);
+  if (slashRewrite) {
+    return {
+      intent: "rewrite_chapter",
+      ...(bookId ? { bookId } : {}),
+      chapterNumber: parseInt(slashRewrite[1]!, 10),
+    };
+  }
+
+  const slashFocus = trimmed.match(/^\/focus\s+(.+)$/i);
+  if (slashFocus) {
+    return {
+      intent: "update_focus",
+      ...(bookId ? { bookId } : {}),
+      instruction: slashFocus[1]!.trim(),
+    };
+  }
+
   const rewriteMatch = trimmed.match(/(?:rewrite chapter|重写第)\s*(\d+)\s*(?:章)?/i);
   const rewriteMatch = trimmed.match(/(?:rewrite chapter|重写第)\s*(\d+)\s*(?:章)?/i);
   if (rewriteMatch) {
   if (rewriteMatch) {
     return {
     return {