mirror of
https://github.com/langgenius/webapp-conversation.git
synced 2025-12-08 17:32:27 +08:00
feat: to new api
This commit is contained in:
@ -18,7 +18,7 @@ import Loading from '@/app/components/base/loading'
|
||||
import { replaceVarWithValues } from '@/utils/prompt'
|
||||
import AppUnavailable from '@/app/components/app-unavailable'
|
||||
import { APP_ID, API_KEY, APP_INFO, isShowPrompt, promptTemplate } from '@/config'
|
||||
|
||||
import { userInputsFormToPromptVariables } from '@/utils/prompt'
|
||||
|
||||
const Main: FC = () => {
|
||||
const { t } = useTranslation()
|
||||
@ -207,13 +207,13 @@ const Main: FC = () => {
|
||||
const isNotNewConversation = conversations.some(item => item.id === _conversationId)
|
||||
|
||||
// fetch new conversation info
|
||||
const { variables: prompt_variables, introduction }: any = appParams
|
||||
|
||||
const { user_input_form, opening_statement: introduction }: any = appParams
|
||||
setLocaleOnClient(APP_INFO.default_language, true)
|
||||
setNewConversationInfo({
|
||||
name: t('app.chat.newChatDefaultName'),
|
||||
introduction,
|
||||
})
|
||||
const prompt_variables = userInputsFormToPromptVariables(user_input_form)
|
||||
setPromptConfig({
|
||||
prompt_template: promptTemplate,
|
||||
prompt_variables,
|
||||
|
||||
@ -31,7 +31,7 @@
|
||||
"i18next-resources-to-backend": "^1.1.3",
|
||||
"immer": "^9.0.19",
|
||||
"js-cookie": "^3.0.1",
|
||||
"langgenius-client": "^1.0.0",
|
||||
"langgenius-client": "1.1.1",
|
||||
"negotiator": "^0.6.3",
|
||||
"next": "13.2.4",
|
||||
"react": "18.2.0",
|
||||
|
||||
25
types/app.ts
25
types/app.ts
@ -6,7 +6,8 @@ export type PromptVariable = {
|
||||
type: "string" | "number" | "select",
|
||||
default?: string | number,
|
||||
options?: string[]
|
||||
max_length: number
|
||||
max_length?: number
|
||||
required: boolean
|
||||
}
|
||||
|
||||
export type PromptConfig = {
|
||||
@ -14,6 +15,28 @@ export type PromptConfig = {
|
||||
prompt_variables: PromptVariable[],
|
||||
}
|
||||
|
||||
export type TextTypeFormItem = {
|
||||
label: string,
|
||||
variable: string,
|
||||
required: boolean
|
||||
max_length: number
|
||||
}
|
||||
|
||||
export type SelectTypeFormItem = {
|
||||
label: string,
|
||||
variable: string,
|
||||
required: boolean,
|
||||
options: string[]
|
||||
}
|
||||
/**
|
||||
* User Input Form Item
|
||||
*/
|
||||
export type UserInputFormItem = {
|
||||
'text-input': TextTypeFormItem
|
||||
} | {
|
||||
'select': SelectTypeFormItem
|
||||
}
|
||||
|
||||
export const MessageRatings = ['like', 'dislike', null] as const
|
||||
export type MessageRating = typeof MessageRatings[number]
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import { PromptVariable } from '@/types/app'
|
||||
import { PromptVariable, UserInputFormItem } from '@/types/app'
|
||||
|
||||
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
|
||||
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
|
||||
@ -10,3 +10,31 @@ export function replaceVarWithValues(str: string, promptVariables: PromptVariabl
|
||||
return valueObj ? `{{${valueObj.key}}}` : match
|
||||
})
|
||||
}
|
||||
|
||||
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
|
||||
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']
|
||||
if (type === 'string') {
|
||||
promptVariables.push({
|
||||
key: content.variable,
|
||||
name: content.label,
|
||||
required: content.required,
|
||||
type: 'string',
|
||||
max_length: content.max_length,
|
||||
options: [],
|
||||
})
|
||||
} else {
|
||||
promptVariables.push({
|
||||
key: content.variable,
|
||||
name: content.label,
|
||||
required: content.required,
|
||||
type: 'select',
|
||||
options: content.options,
|
||||
})
|
||||
}
|
||||
})
|
||||
return promptVariables
|
||||
}
|
||||
Reference in New Issue
Block a user