From fd5141d48050762b9abb580878abe3df3ac6880d Mon Sep 17 00:00:00 2001 From: canisminor1990 Date: Sun, 24 Mar 2024 15:50:49 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20concurrency=20pMap=20?= =?UTF-8?q?to=20lobe=20seo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/lobe-seo/README.md | 19 +++---- packages/lobe-seo/README.zh-CN.md | 1 + packages/lobe-seo/src/commands/Seo/index.tsx | 48 +++++++++++------- packages/lobe-seo/src/components/Progress.tsx | 50 ------------------- 4 files changed, 40 insertions(+), 78 deletions(-) delete mode 100644 packages/lobe-seo/src/components/Progress.tsx diff --git a/packages/lobe-seo/README.md b/packages/lobe-seo/README.md index 20a4794..a782de4 100644 --- a/packages/lobe-seo/README.md +++ b/packages/lobe-seo/README.md @@ -128,15 +128,16 @@ Some additional configurations are provided in this project, set using environme ## 🔍 Configuration -| Property Name | Required | Type | Default Value | Description | -| -------------- | -------- | -------------- | --------------- | -------------------------------- | -| entry | `*` | `string` | - | Entry file or folder | -| entryExtension | | `string` | `.mdx` | Entry file extension | -| groupKey | | `string` | - | Set group key for SEO matters | -| tagStringify | | `boolean` | `false` | Stringify the tags array | -| modelName | | `string` | `gpt-3.5-turbo` | Model used | -| temperature | | `number` | `0` | Sampling temperature used | -| experimental | | `experimental` | `{}` | Experimental features, see below | +| Property Name | Required | Type | Default Value | Description | +| -------------- | -------- | -------------- | --------------- | ------------------------------------------------ | +| entry | `*` | `string` | - | Entry file or folder | +| entryExtension | | `string` | `.mdx` | Entry file extension | +| groupKey | | `string` | - | Set group key for SEO matters | +| tagStringify | | `boolean` | `false` | Stringify the tags array | +| modelName | | `string` | `gpt-3.5-turbo` | Model used | +| temperature | | `number` | `0` | Sampling temperature used | +| concurrency | | `number` | `5` | Number of concurrently pending promises returned | +| experimental | | `experimental` | `{}` | Experimental features, see below | #### `experimental` diff --git a/packages/lobe-seo/README.zh-CN.md b/packages/lobe-seo/README.zh-CN.md index 31972ec..987795d 100644 --- a/packages/lobe-seo/README.zh-CN.md +++ b/packages/lobe-seo/README.zh-CN.md @@ -136,6 +136,7 @@ $ lobe-seo -c './custom-config.js' # or use the full flag --config | tagStringify | | `boolean` | `false` | 将 tags 字符串化 | | modelName | | `string` | `gpt-3.5-turbo` | 使用的模型 | | temperature | | `number` | `0` | 使用的采样温度 | +| concurrency | | `number` | `5` | 同时并发的队列请求数量 | | experimental | | `experimental` | `{}` | 实验性功能,见下文 | #### `experimental` diff --git a/packages/lobe-seo/src/commands/Seo/index.tsx b/packages/lobe-seo/src/commands/Seo/index.tsx index e7efca5..b027b3d 100644 --- a/packages/lobe-seo/src/commands/Seo/index.tsx +++ b/packages/lobe-seo/src/commands/Seo/index.tsx @@ -3,6 +3,7 @@ import chalk from 'chalk'; import { consola } from 'consola'; import { globSync } from 'glob'; import matter from 'gray-matter'; +import pMap from 'p-map'; import { SeoCore, SeoQueryItem } from '@/core/SeoCore'; import { selectors } from '@/store'; @@ -54,25 +55,34 @@ class Seo { )}) ${this.config.experimental?.jsonMode ? chalk.red(' [JSON Mode]') : ''}}`, ); let totalTokenUsage = 0; - for (const item of this.query) { - const data = await this.seo.run({ - ...item, - onProgress: ({ isLoading }) => { - if (isLoading) { - consola.start(item.entry); - } - }, - }); - - if (data?.result && Object.keys(data.result).length > 0) { - const result = matter.stringify(item.content, data.result); - writeMarkdown(item.entry, result); - totalTokenUsage += data.tokenUsage; - consola.success(chalk.yellow(item.entry), chalk.gray(`[Token usage: ${data.tokenUsage}]`)); - } else { - consola.warn('No translation result was found:', chalk.yellow(item.entry)); - } - } + + await pMap( + this.query, + async (item) => { + const data = await this.seo.run({ + ...item, + onProgress: ({ isLoading }) => { + if (isLoading) { + consola.start(item.entry); + } + }, + }); + + if (data?.result && Object.keys(data.result).length > 0) { + const result = matter.stringify(item.content, data.result); + writeMarkdown(item.entry, result); + totalTokenUsage += data.tokenUsage; + consola.success( + chalk.yellow(item.entry), + chalk.gray(`[Token usage: ${data.tokenUsage}]`), + ); + } else { + consola.warn('No translation result was found:', chalk.yellow(item.entry)); + } + }, + { concurrency: this.config.concurrency || 5 }, + ); + if (totalTokenUsage > 0) consola.info('Total token usage:', chalk.cyan(totalTokenUsage)); } diff --git a/packages/lobe-seo/src/components/Progress.tsx b/packages/lobe-seo/src/components/Progress.tsx deleted file mode 100644 index 305deeb..0000000 --- a/packages/lobe-seo/src/components/Progress.tsx +++ /dev/null @@ -1,50 +0,0 @@ -// import { ProgressBar, Spinner, StatusMessage } from '@inkjs/ui'; -// import { SplitView, useTheme } from '@lobehub/cli-ui'; -// import { Box, Text } from 'ink'; -// import { memo } from 'react'; -// -// import { onProgressProps } from '@/core/Seo'; -// -// interface ProgressProps extends onProgressProps { -// filename: string; -// from: string; -// hide?: boolean; -// to: string; -// } -// -// const Progress = memo( -// ({ hide, filename, to, from, progress, maxStep, step, isLoading, needToken }) => { -// const theme = useTheme(); -// -// if (hide) return null; -// -// return ( -// -// -// {` 📝 ${filename} `} -// -// -// {`- from `} -// -// {from} -// -// {` to `} -// -// {to} -// -// {` [Tokens: ${needToken}]`} -// -// {isLoading ? ( -// -// -// -// -// ) : ( -// Success -// )} -// -// ); -// }, -// ); -// -// export default Progress;