From 4159eed9fac8427e0248d1ba092ad76bf234d3fa Mon Sep 17 00:00:00 2001 From: Joel Date: Sat, 10 Jun 2023 15:29:08 +0800 Subject: [PATCH] feat: lint --- .eslintrc.json | 2 +- .vscode/launch.json | 2 +- .vscode/settings.json | 2 +- README.md | 10 +- .../messages/[messageId]/feedbacks/route.ts | 9 +- app/api/messages/route.ts | 12 +- app/api/parameters/route.ts | 10 +- app/api/utils/common.ts | 12 +- app/components/app-unavailable.tsx | 3 +- app/components/base/markdown.tsx | 2 +- app/components/index.tsx | 76 ++++---- app/components/result/index.tsx | 169 +++++++++--------- config/index.ts | 14 +- hooks/use-breakpoints.ts | 24 +-- i18n/client.ts | 3 +- i18n/i18next-config.ts | 2 +- i18n/i18next-serverside-config.ts | 6 +- i18n/lang/app.en.ts | 36 ++-- i18n/lang/app.zh.ts | 36 ++-- i18n/lang/common.en.ts | 2 +- i18n/lang/common.zh.ts | 2 +- next.config.js | 2 +- package.json | 16 +- tailwind.config.js | 10 +- tsconfig.json | 2 +- types/app.ts | 24 +-- typography.js | 70 ++++---- utils/prompt.ts | 12 +- 28 files changed, 292 insertions(+), 278 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 9d8ea2c..631cb2b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -25,4 +25,4 @@ ], "react-hooks/exhaustive-deps": "warn" } -} \ No newline at end of file +} diff --git a/.vscode/launch.json b/.vscode/launch.json index 9e36291..2a80d06 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -25,4 +25,4 @@ } } ] -} \ No newline at end of file +} diff --git a/.vscode/settings.json b/.vscode/settings.json index e35fdc6..45d5fcf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -29,4 +29,4 @@ "i18n/lang", "app/api/messages" ] -} \ No newline at end of file +} diff --git a/README.md b/README.md index a74ce78..9f59d2b 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ Config app in `config/index.ts`.Please config: More config: ```js export const APP_INFO: AppInfo = { - "title": 'Chat APP', - "description": '', - "copyright": '', - "privacy_policy": '', - "default_language": 'zh-Hans' + title: 'Chat APP', + description: '', + copyright: '', + privacy_policy: '', + default_language: 'zh-Hans' } export const isShowPrompt = true diff --git a/app/api/messages/[messageId]/feedbacks/route.ts b/app/api/messages/[messageId]/feedbacks/route.ts index e97aca0..253061a 100644 --- a/app/api/messages/[messageId]/feedbacks/route.ts +++ b/app/api/messages/[messageId]/feedbacks/route.ts @@ -1,20 +1,21 @@ import { type NextRequest } from 'next/server' import { NextResponse } from 'next/server' -import { getInfo, client } from '@/app/api/utils/common' +import { client, getInfo } from '@/app/api/utils/common' export async function POST(request: NextRequest, { params }: { params: { messageId: string } }) { const body = await request.json() const { - rating + rating, } = body const { messageId } = params - const { user } = getInfo(request); + const { user } = getInfo(request) try { const { data } = await client.messageFeedback(messageId, rating, user) return NextResponse.json(data) - } catch (e) { + } + catch (e) { return NextResponse.json(e) } } diff --git a/app/api/messages/route.ts b/app/api/messages/route.ts index 57a027c..0edafe7 100644 --- a/app/api/messages/route.ts +++ b/app/api/messages/route.ts @@ -1,13 +1,13 @@ import { type NextRequest } from 'next/server' import { NextResponse } from 'next/server' -import { getInfo, setSession, client } from '@/app/api/utils/common' +import { client, getInfo, setSession } from '@/app/api/utils/common' export async function GET(request: NextRequest) { - const { sessionId, user } = getInfo(request); - const { searchParams } = new URL(request.url); + const { sessionId, user } = getInfo(request) + const { searchParams } = new URL(request.url) const conversationId = searchParams.get('conversation_id') - const { data }: any = await client.getConversationMessages(user, conversationId as string); + const { data }: any = await client.getConversationMessages(user, conversationId as string) return NextResponse.json(data, { - headers: setSession(sessionId) + headers: setSession(sessionId), }) -} \ No newline at end of file +} diff --git a/app/api/parameters/route.ts b/app/api/parameters/route.ts index 1c1d917..97bbdb2 100644 --- a/app/api/parameters/route.ts +++ b/app/api/parameters/route.ts @@ -1,11 +1,11 @@ import { type NextRequest } from 'next/server' import { NextResponse } from 'next/server' -import { getInfo, setSession, client } from '@/app/api/utils/common' +import { client, getInfo, setSession } from '@/app/api/utils/common' export async function GET(request: NextRequest) { - const { sessionId, user } = getInfo(request); - const { data } = await client.getApplicationParameters(user); + const { sessionId, user } = getInfo(request) + const { data } = await client.getApplicationParameters(user) return NextResponse.json(data as object, { - headers: setSession(sessionId) + headers: setSession(sessionId), }) -} \ No newline at end of file +} diff --git a/app/api/utils/common.ts b/app/api/utils/common.ts index d2234cf..3e8f392 100644 --- a/app/api/utils/common.ts +++ b/app/api/utils/common.ts @@ -1,16 +1,16 @@ import { type NextRequest } from 'next/server' -import { APP_ID, API_KEY, API_URL } from '@/config' import { CompletionClient } from 'dify-client' import { v4 } from 'uuid' +import { API_KEY, API_URL, APP_ID } from '@/config' -const userPrefix = `user_${APP_ID}:`; +const userPrefix = `user_${APP_ID}:` export const getInfo = (request: NextRequest) => { - const sessionId = request.cookies.get('session_id')?.value || v4(); - const user = userPrefix + sessionId; + const sessionId = request.cookies.get('session_id')?.value || v4() + const user = userPrefix + sessionId return { sessionId, - user + user, } } @@ -18,4 +18,4 @@ export const setSession = (sessionId: string) => { return { 'Set-Cookie': `session_id=${sessionId}` } } -export const client = new CompletionClient(API_KEY, API_URL ? API_URL : undefined) +export const client = new CompletionClient(API_KEY, API_URL || undefined) diff --git a/app/components/app-unavailable.tsx b/app/components/app-unavailable.tsx index ce4d7c7..b018875 100644 --- a/app/components/app-unavailable.tsx +++ b/app/components/app-unavailable.tsx @@ -14,9 +14,8 @@ const AppUnavailable: FC = ({ }) => { const { t } = useTranslation() let message = errMessage - if (!errMessage) { + if (!errMessage) message = (isUnknwonReason ? t('app.common.appUnkonwError') : t('app.common.appUnavailable')) as string - } return (
diff --git a/app/components/base/markdown.tsx b/app/components/base/markdown.tsx index aac5f7b..29e1b80 100644 --- a/app/components/base/markdown.tsx +++ b/app/components/base/markdown.tsx @@ -18,7 +18,7 @@ export function Markdown(props: { content: string }) { components={{ code({ node, inline, className, children, ...props }) { const match = /language-(\w+)/.exec(className || '') - return !inline && match + return (!inline && match) ? ( { const { t } = useTranslation() @@ -44,7 +43,7 @@ const TextGeneration = () => { const isNoData = !completionRes const [messageId, setMessageId] = useState(null) const [feedback, setFeedback] = useState({ - rating: null + rating: null, }) const handleFeedback = async (feedback: Feedbacktype) => { @@ -95,7 +94,6 @@ const TextGeneration = () => { return false } - const data = { inputs, query, @@ -103,7 +101,7 @@ const TextGeneration = () => { setMessageId(null) setFeedback({ - rating: null + rating: null, }) setCompletionRes('') @@ -123,7 +121,7 @@ const TextGeneration = () => { }, onError() { setResponsingFalse() - } + }, }) } @@ -164,14 +162,14 @@ const TextGeneration = () => { const [isShowResSidebar, { setTrue: showResSidebar, setFalse: hideResSidebar }] = useBoolean(false) const resRef = useRef(null) useClickAway(() => { - hideResSidebar(); + hideResSidebar() }, resRef) const renderRes = (
@@ -192,27 +190,29 @@ const TextGeneration = () => {
- {(isResponsing && !completionRes) ? ( -
- -
) : ( - <> - {isNoData - ? - : ( - - ) - } - - )} + {(isResponsing && !completionRes) + ? ( +
+ +
) + : ( + <> + {isNoData + ? + : ( + + ) + } + + )}
@@ -224,12 +224,11 @@ const TextGeneration = () => { if (!APP_INFO || !promptConfig) return - return ( <>
{/* Left */} -
+
@@ -264,7 +263,6 @@ const TextGeneration = () => { />
- {/* copyright */}
© {APP_INFO.copyright || APP_INFO.title} {(new Date()).getFullYear()}
@@ -294,7 +292,7 @@ const TextGeneration = () => {
{renderRes} diff --git a/app/components/result/index.tsx b/app/components/result/index.tsx index 08635a2..341539b 100644 --- a/app/components/result/index.tsx +++ b/app/components/result/index.tsx @@ -1,15 +1,16 @@ 'use client' -import React, { FC, useState } from 'react' +import type { FC } from 'react' +import React from 'react' import { useTranslation } from 'react-i18next' import cn from 'classnames' +import copy from 'copy-to-clipboard' +import { HandThumbDownIcon, HandThumbUpIcon } from '@heroicons/react/24/outline' import { Markdown } from '@/app/components/base/markdown' import Loading from '@/app/components/base/loading' -import copy from 'copy-to-clipboard' import Toast from '@/app/components/base/toast' -import { Feedbacktype } from '@/types/app' -import { HandThumbDownIcon, HandThumbUpIcon } from '@heroicons/react/24/outline' +import type { Feedbacktype } from '@/types/app' -export interface IGenerationItemProps { +export type IGenerationItemProps = { className?: string content: string messageId?: string | null @@ -22,7 +23,7 @@ export interface IGenerationItemProps { export const SimpleBtn = ({ className, onClick, children }: { className?: string - onClick?: () => void, + onClick?: () => void children: React.ReactNode }) => (
= ({ isInWebApp = false, feedback, onFeedback, - isMobile + isMobile, }) => { const { t } = useTranslation() return (
- {isLoading ? ( -
- ) : ( -
- - {messageId && ( -
-
- { - copy(content) - Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') }) - }}> - {copyIcon} - {!isMobile &&
{t('common.operation.copy')}
} -
- {isInWebApp && ( - <> -
- {!feedback?.rating && ( - - <> -
{ - onFeedback?.({ - rating: 'like' - }) - }} - className='flex w-6 h-6 items-center justify-center rounded-md cursor-pointer hover:bg-gray-100'> - -
-
{ - onFeedback?.({ - rating: 'dislike' - }) - }} - className='flex w-6 h-6 items-center justify-center rounded-md cursor-pointer hover:bg-gray-100'> - -
- -
- )} - {feedback?.rating === 'like' && ( -
{ - onFeedback?.({ - rating: null - }) - }} - className='flex w-7 h-7 items-center justify-center rounded-md cursor-pointer !text-primary-600 border border-primary-200 bg-primary-100 hover:border-primary-300 hover:bg-primary-200'> - -
- )} - {feedback?.rating === 'dislike' && ( -
{ - onFeedback?.({ - rating: null - }) - }} - className='flex w-7 h-7 items-center justify-center rounded-md cursor-pointer !text-red-600 border border-red-200 bg-red-100 hover:border-red-300 hover:bg-red-200'> - -
- )} - - )} + {isLoading + ? ( +
+ ) + : ( +
+ + {messageId && ( +
+
+ { + copy(content) + Toast.notify({ type: 'success', message: t('common.actionMsg.copySuccessfully') }) + }}> + {copyIcon} + {!isMobile &&
{t('common.operation.copy')}
} +
+ {isInWebApp && ( + <> +
+ {!feedback?.rating && ( + + <> +
{ + onFeedback?.({ + rating: 'like', + }) + }} + className='flex w-6 h-6 items-center justify-center rounded-md cursor-pointer hover:bg-gray-100'> + +
+
{ + onFeedback?.({ + rating: 'dislike', + }) + }} + className='flex w-6 h-6 items-center justify-center rounded-md cursor-pointer hover:bg-gray-100'> + +
+ +
+ )} + {feedback?.rating === 'like' && ( +
{ + onFeedback?.({ + rating: null, + }) + }} + className='flex w-7 h-7 items-center justify-center rounded-md cursor-pointer !text-primary-600 border border-primary-200 bg-primary-100 hover:border-primary-300 hover:bg-primary-200'> + +
+ )} + {feedback?.rating === 'dislike' && ( +
{ + onFeedback?.({ + rating: null, + }) + }} + className='flex w-7 h-7 items-center justify-center rounded-md cursor-pointer !text-red-600 border border-red-200 bg-red-100 hover:border-red-300 hover:bg-red-200'> + +
+ )} + + )} +
+
{content?.length} {t('common.unit.char')}
-
{content?.length} {t('common.unit.char')}
-
- )} + )} -
- )} +
+ )}
) } diff --git a/config/index.ts b/config/index.ts index dd42b81..eff3431 100644 --- a/config/index.ts +++ b/config/index.ts @@ -1,16 +1,16 @@ -import { AppInfo } from "@/types/app" +import type { AppInfo } from '@/types/app' export const APP_ID = '' export const API_KEY = '' export const API_URL = '' export const APP_INFO: AppInfo = { - "title": 'Text Generator APP', - "description": 'App description', - "copyright": '', - "privacy_policy": '', - "default_language": 'zh-Hans' + title: 'Text Generator APP', + description: 'App description', + copyright: '', + privacy_policy: '', + default_language: 'zh-Hans', } -export const API_PREFIX = '/api'; +export const API_PREFIX = '/api' export const LOCALE_COOKIE_NAME = 'locale' diff --git a/hooks/use-breakpoints.ts b/hooks/use-breakpoints.ts index 1aab56a..99c2b75 100644 --- a/hooks/use-breakpoints.ts +++ b/hooks/use-breakpoints.ts @@ -8,20 +8,22 @@ export enum MediaType { } const useBreakpoints = () => { - const [width, setWidth] = React.useState(globalThis.innerWidth); + const [width, setWidth] = React.useState(globalThis.innerWidth) const media = (() => { - if (width <= 640) return MediaType.mobile; - if (width <= 768) return MediaType.tablet; - return MediaType.pc; - })(); + if (width <= 640) + return MediaType.mobile + if (width <= 768) + return MediaType.tablet + return MediaType.pc + })() React.useEffect(() => { - const handleWindowResize = () => setWidth(window.innerWidth); - window.addEventListener("resize", handleWindowResize); - return () => window.removeEventListener("resize", handleWindowResize); - }, []); + const handleWindowResize = () => setWidth(window.innerWidth) + window.addEventListener('resize', handleWindowResize) + return () => window.removeEventListener('resize', handleWindowResize) + }, []) - return media; + return media } -export default useBreakpoints \ No newline at end of file +export default useBreakpoints diff --git a/i18n/client.ts b/i18n/client.ts index 98424d2..f8dafb4 100644 --- a/i18n/client.ts +++ b/i18n/client.ts @@ -12,7 +12,6 @@ export const getLocaleOnClient = (): Locale => { export const setLocaleOnClient = (locale: Locale, notReload?: boolean) => { Cookies.set(LOCALE_COOKIE_NAME, locale) changeLanguage(locale) - if (!notReload) { + if (!notReload) location.reload() - } } diff --git a/i18n/i18next-config.ts b/i18n/i18next-config.ts index 9f949ae..13580b1 100644 --- a/i18n/i18next-config.ts +++ b/i18n/i18next-config.ts @@ -5,7 +5,7 @@ import commonEn from './lang/common.en' import commonZh from './lang/common.zh' import appEn from './lang/app.en' import appZh from './lang/app.zh' -import { Locale } from '.' +import type { Locale } from '.' const resources = { 'en': { diff --git a/i18n/i18next-serverside-config.ts b/i18n/i18next-serverside-config.ts index fe89475..6e0b692 100644 --- a/i18n/i18next-serverside-config.ts +++ b/i18n/i18next-serverside-config.ts @@ -1,7 +1,7 @@ import { createInstance } from 'i18next' import resourcesToBackend from 'i18next-resources-to-backend' import { initReactI18next } from 'react-i18next/initReactI18next' -import { Locale } from '.' +import type { Locale } from '.' // https://locize.com/blog/next-13-app-dir-i18n/ const initI18next = async (lng: Locale, ns: string) => { @@ -21,6 +21,6 @@ export async function useTranslation(lng: Locale, ns = '', options: Record @media (min-width: 100px) { ... } - 'tablet': '640px', // 391 + tablet: '640px', // 391 // => @media (min-width: 600px) { ... } - 'pc': '769px', + pc: '769px', // => @media (min-width: 769px) { ... } }, }, diff --git a/tsconfig.json b/tsconfig.json index 668a47e..1510574 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -40,4 +40,4 @@ "exclude": [ "node_modules" ] -} \ No newline at end of file +} diff --git a/types/app.ts b/types/app.ts index 43a772f..3ccaa59 100644 --- a/types/app.ts +++ b/types/app.ts @@ -1,4 +1,4 @@ -import { Locale } from '@/i18n' +import type { Locale } from '@/i18n' export type AppInfo = { title: string @@ -9,31 +9,31 @@ export type AppInfo = { } export type PromptVariable = { - key: string, - name: string, - type: "string" | "number" | "select", - default?: string | number, + key: string + name: string + type: 'string' | 'number' | 'select' + default?: string | number required?: boolean options?: string[] max_length?: number } export type PromptConfig = { - prompt_template: string, - prompt_variables: PromptVariable[], + prompt_template: string + prompt_variables: PromptVariable[] } export type TextTypeFormItem = { - label: string, - variable: string, + label: string + variable: string required: boolean max_length: number } export type SelectTypeFormItem = { - label: string, - variable: string, - required: boolean, + label: string + variable: string + required: boolean options: string[] } /** diff --git a/typography.js b/typography.js index 706e456..92bea1e 100644 --- a/typography.js +++ b/typography.js @@ -38,15 +38,15 @@ module.exports = ({ theme }) => ({ '--tw-prose-invert-td-borders': theme('colors.zinc.700'), // Base - color: 'var(--tw-prose-body)', - fontSize: theme('fontSize.sm')[0], - lineHeight: theme('lineHeight.7'), + 'color': 'var(--tw-prose-body)', + 'fontSize': theme('fontSize.sm')[0], + 'lineHeight': theme('lineHeight.7'), // Layout '> *': { - maxWidth: theme('maxWidth.2xl'), - marginLeft: 'auto', - marginRight: 'auto', + 'maxWidth': theme('maxWidth.2xl'), + 'marginLeft': 'auto', + 'marginRight': 'auto', '@screen lg': { maxWidth: theme('maxWidth.3xl'), marginLeft: `calc(50% - min(50%, ${theme('maxWidth.lg')}))`, @@ -55,7 +55,7 @@ module.exports = ({ theme }) => ({ }, // Text - p: { + 'p': { marginTop: theme('spacing.6'), marginBottom: theme('spacing.6'), }, @@ -65,7 +65,7 @@ module.exports = ({ theme }) => ({ }, // Lists - ol: { + 'ol': { listStyleType: 'decimal', marginTop: theme('spacing.5'), marginBottom: theme('spacing.5'), @@ -98,13 +98,13 @@ module.exports = ({ theme }) => ({ 'ol[type="1"]': { listStyleType: 'decimal', }, - ul: { + 'ul': { listStyleType: 'disc', marginTop: theme('spacing.5'), marginBottom: theme('spacing.5'), paddingLeft: '1.625rem', }, - li: { + 'li': { marginTop: theme('spacing.2'), marginBottom: theme('spacing.2'), }, @@ -140,14 +140,14 @@ module.exports = ({ theme }) => ({ }, // Horizontal rules - hr: { - borderColor: 'var(--tw-prose-hr)', - borderTopWidth: 1, - marginTop: theme('spacing.16'), - marginBottom: theme('spacing.16'), - maxWidth: 'none', - marginLeft: `calc(-1 * ${theme('spacing.4')})`, - marginRight: `calc(-1 * ${theme('spacing.4')})`, + 'hr': { + 'borderColor': 'var(--tw-prose-hr)', + 'borderTopWidth': 1, + 'marginTop': theme('spacing.16'), + 'marginBottom': theme('spacing.16'), + 'maxWidth': 'none', + 'marginLeft': `calc(-1 * ${theme('spacing.4')})`, + 'marginRight': `calc(-1 * ${theme('spacing.4')})`, '@screen sm': { marginLeft: `calc(-1 * ${theme('spacing.6')})`, marginRight: `calc(-1 * ${theme('spacing.6')})`, @@ -159,7 +159,7 @@ module.exports = ({ theme }) => ({ }, // Quotes - blockquote: { + 'blockquote': { fontWeight: '500', fontStyle: 'italic', color: 'var(--tw-prose-quotes)', @@ -178,14 +178,14 @@ module.exports = ({ theme }) => ({ }, // Headings - h1: { + 'h1': { color: 'var(--tw-prose-headings)', fontWeight: '700', fontSize: theme('fontSize.2xl')[0], ...theme('fontSize.2xl')[1], marginBottom: theme('spacing.2'), }, - h2: { + 'h2': { color: 'var(--tw-prose-headings)', fontWeight: '600', fontSize: theme('fontSize.lg')[0], @@ -193,7 +193,7 @@ module.exports = ({ theme }) => ({ marginTop: theme('spacing.16'), marginBottom: theme('spacing.2'), }, - h3: { + 'h3': { color: 'var(--tw-prose-headings)', fontSize: theme('fontSize.base')[0], ...theme('fontSize.base')[1], @@ -211,7 +211,7 @@ module.exports = ({ theme }) => ({ marginTop: '0', marginBottom: '0', }, - figcaption: { + 'figcaption': { color: 'var(--tw-prose-captions)', fontSize: theme('fontSize.xs')[0], ...theme('fontSize.xs')[1], @@ -219,7 +219,7 @@ module.exports = ({ theme }) => ({ }, // Tables - table: { + 'table': { width: '100%', tableLayout: 'auto', textAlign: 'left', @@ -227,7 +227,7 @@ module.exports = ({ theme }) => ({ marginBottom: theme('spacing.8'), lineHeight: theme('lineHeight.6'), }, - thead: { + 'thead': { borderBottomWidth: '1px', borderBottomColor: 'var(--tw-prose-th-borders)', }, @@ -255,7 +255,7 @@ module.exports = ({ theme }) => ({ 'tbody td': { verticalAlign: 'baseline', }, - tfoot: { + 'tfoot': { borderTopWidth: '1px', borderTopColor: 'var(--tw-prose-th-borders)', }, @@ -276,13 +276,13 @@ module.exports = ({ theme }) => ({ }, // Inline elements - a: { - color: 'var(--tw-prose-links)', - textDecoration: 'underline transparent', - fontWeight: '500', - transitionProperty: 'color, text-decoration-color', - transitionDuration: theme('transitionDuration.DEFAULT'), - transitionTimingFunction: theme('transitionTimingFunction.DEFAULT'), + 'a': { + 'color': 'var(--tw-prose-links)', + 'textDecoration': 'underline transparent', + 'fontWeight': '500', + 'transitionProperty': 'color, text-decoration-color', + 'transitionDuration': theme('transitionDuration.DEFAULT'), + 'transitionTimingFunction': theme('transitionTimingFunction.DEFAULT'), '&:hover': { color: 'var(--tw-prose-links-hover)', textDecorationColor: 'var(--tw-prose-links-underline)', @@ -291,14 +291,14 @@ module.exports = ({ theme }) => ({ ':is(h1, h2, h3) a': { fontWeight: 'inherit', }, - strong: { + 'strong': { color: 'var(--tw-prose-bold)', fontWeight: '600', }, ':is(a, blockquote, thead th) strong': { color: 'inherit', }, - code: { + 'code': { color: 'var(--tw-prose-code)', borderRadius: theme('borderRadius.lg'), paddingTop: theme('padding.1'), diff --git a/utils/prompt.ts b/utils/prompt.ts index 2b11efe..c23fccd 100644 --- a/utils/prompt.ts +++ b/utils/prompt.ts @@ -1,4 +1,4 @@ -import { PromptVariable, UserInputFormItem } from '@/types/app' +import type { PromptVariable, UserInputFormItem } from '@/types/app' export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record) { return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => { @@ -12,11 +12,12 @@ export function replaceVarWithValues(str: string, promptVariables: PromptVariabl } export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => { - if (!useInputs) return [] + if (!useInputs) + return [] const promptVariables: PromptVariable[] = [] useInputs.forEach((item: any) => { const type = item['text-input'] ? 'string' : 'select' - const content = type === 'string' ? item['text-input'] : item['select'] + const content = type === 'string' ? item['text-input'] : item.select if (type === 'string') { promptVariables.push({ key: content.variable, @@ -26,7 +27,8 @@ export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | max_length: content.max_length, options: [], }) - } else { + } + else { promptVariables.push({ key: content.variable, name: content.label, @@ -37,4 +39,4 @@ export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | } }) return promptVariables -} \ No newline at end of file +}