From b4857dbf7bb01972df934f21824ee2511803440c Mon Sep 17 00:00:00 2001
From: wangsijie <wangsijie@silverhand.io>
Date: Thu, 8 Jun 2023 11:23:31 +0900
Subject: [PATCH] fix(cli): polish translate prompts (#3955)

---
 packages/cli/src/commands/translate/openai.ts | 17 ++++-------
 .../cli/src/commands/translate/prompts.ts     | 30 ++++++++++++-------
 2 files changed, 25 insertions(+), 22 deletions(-)

diff --git a/packages/cli/src/commands/translate/openai.ts b/packages/cli/src/commands/translate/openai.ts
index b424892e6..0864943a4 100644
--- a/packages/cli/src/commands/translate/openai.ts
+++ b/packages/cli/src/commands/translate/openai.ts
@@ -8,7 +8,7 @@ import { z } from 'zod';
 
 import { consoleLog, getProxy } from '../../utils.js';
 
-import { getTranslationPrompt } from './prompts.js';
+import { getTranslationPromptMessages } from './prompts.js';
 
 export const createOpenaiApi = () => {
   const proxy = getProxy();
@@ -51,16 +51,11 @@ export const translate = async ({
       .post('chat/completions', {
         json: {
           model: 'gpt-3.5-turbo',
-          messages: [
-            {
-              role: 'user',
-              content: getTranslationPrompt({
-                sourceFileContent,
-                targetLanguage,
-                extraPrompt,
-              }),
-            },
-          ],
+          messages: getTranslationPromptMessages({
+            sourceFileContent,
+            targetLanguage,
+            extraPrompt,
+          }),
         },
       })
       .json(),
diff --git a/packages/cli/src/commands/translate/prompts.ts b/packages/cli/src/commands/translate/prompts.ts
index dc95c9047..df575d6db 100644
--- a/packages/cli/src/commands/translate/prompts.ts
+++ b/packages/cli/src/commands/translate/prompts.ts
@@ -13,19 +13,20 @@ type GetTranslationPromptProperties = {
  * Remember to check the token limit before adding more prompt.
  * Tokens can be counted in https://platform.openai.com/tokenizer
  */
-export const getTranslationPrompt = ({
+export const getTranslationPromptMessages = ({
   sourceFileContent,
   targetLanguage,
   extraPrompt,
-}: GetTranslationPromptProperties) => `Given the following code snippet:
-\`\`\`ts
-${sourceFileContent}
-\`\`\`
-only translate object values to ${
-  languages[targetLanguage]
-}, keep all object keys original, output ts code only, the code format should be strictly consistent, and should not contains the given code snippet.
+}: GetTranslationPromptProperties) => [
+  {
+    role: 'assistant',
+    content: `You are a translate assistant of a Typescript engenieer, when you receive a code snippet that contains an object, translate those values that are marked with comment "// UNTRANSLATED" into the language ${
+      languages[targetLanguage]
+    },  keep all object keys original, output ts code only, the code format should be strictly consistent, and should not contain the given code snippet. ${conditionalString(
+      extraPrompt
+    )}
 
-Take zh-cn as an example, if the input is:
+Take Chinese as an example, if the input is:
 \`\`\`ts
 import others from './others.js';
 
@@ -48,5 +49,12 @@ const translation = {
 };
 
 export default translation;
-\`\`\`
-${conditionalString(extraPrompt)}`;
+\`\`\``,
+  },
+  {
+    role: 'user',
+    content: `\`\`\`ts
+${sourceFileContent}
+\`\`\``,
+  },
+];