fix: 上下文,聊天计费,额度

This commit is contained in:
npc0-hue
2026-01-21 18:10:18 +08:00
parent 0cf63b0f08
commit 9ed0d7c891
111 changed files with 1954 additions and 729 deletions
+20
View File
@@ -83,6 +83,16 @@ export const copyApp = ({
return post<AppDetailResponse>(`apps/${appID}/copy`, { body: { name, icon_type, icon, icon_background, mode, description } })
}
// extend: start sync app
export const syncApp = ({ appID }: { appID: string }): Promise<AppDetailResponse> => {
return put<AppDetailResponse>(`apps/${appID}/sync`)
}
export const syncCancelApp = ({ appID }: { appID: string }): Promise<CommonResponse> => {
return del<CommonResponse>(`apps/${appID}/sync`)
}
// extend: stop sync app
export const exportAppConfig = ({ appID, include = false, workflowID }: { appID: string, include?: boolean, workflowID?: string }): Promise<{ data: string }> => {
const params = new URLSearchParams({
include_secret: include.toString(),
@@ -236,3 +246,13 @@ export const updateTracingConfig = ({ appId, body }: { appId: string, body: Trac
export const removeTracingConfig = ({ appId, provider }: { appId: string, provider: TracingProvider }): Promise<CommonResponse> => {
return del<CommonResponse>(`/apps/${appId}/trace-config?tracing_provider=${provider}`)
}
// Extend: start messages context handling
export const messageContextList = ({ conversation_id }: { conversation_id: string }): Promise<string[]> => {
return get<string[]>(`/message/context?conversation_id=${conversation_id}`)
}
export const deleteMessageContext = ({ conversation_id, message_id }: { conversation_id: string; message_id: string }): Promise<string[]> => {
return del<string[]>(`/message/context?conversation_id=${conversation_id}&message_id=${message_id}`)
}
// Extend: stop messages context handling
+35 -16
View File
@@ -3,8 +3,7 @@ import type { IOtherOptions } from './base'
import Cookies from 'js-cookie'
import ky from 'ky'
import Toast from '@/app/components/base/toast'
// extend: 后台地址
import { API_ADMIN, API_PREFIX, APP_VERSION, CSRF_COOKIE_NAME, CSRF_HEADER_NAME, IS_MARKETPLACE, MARKETPLACE_API_PREFIX, PASSPORT_HEADER_NAME, PUBLIC_API_PREFIX, WEB_APP_SHARE_CODE_HEADER_NAME } from '@/config'
import { API_PREFIX, APP_VERSION, CSRF_COOKIE_NAME, CSRF_HEADER_NAME, IS_MARKETPLACE, MARKETPLACE_API_PREFIX, PASSPORT_HEADER_NAME, PUBLIC_API_PREFIX, WEB_APP_SHARE_CODE_HEADER_NAME } from '@/config'
import { getWebAppAccessToken, getWebAppPassport } from './webapp-auth'
const TIME_OUT = 100000
@@ -25,8 +24,12 @@ export type FetchOptionType = Omit<RequestInit, 'body'> & {
}
const afterResponse204: AfterResponseHook = async (_request, _options, response) => {
if (response.status === 204)
return Response.json({ result: 'success' })
if (response.status === 204) {
return new Response(JSON.stringify({ result: 'success' }), {
status: 200,
headers: { 'Content-Type': ContentType.json },
})
}
}
export type ResponseError = {
@@ -128,23 +131,39 @@ export const getBaseOptions = (): RequestInit => ({
})
async function base<T>(url: string, options: FetchOptionType = {}, otherOptions: IOtherOptions = {}): Promise<T> {
const baseOptions = getBaseOptions()
const { params, body, headers, ...init } = Object.assign({}, baseOptions, options)
// In fetchCompat mode, skip baseOptions to avoid overriding Request object's method, headers,
const baseOptions = otherOptions.fetchCompat
? {
mode: 'cors',
credentials: 'include', // always send cookies、HTTP Basic authentication.
redirect: 'follow',
}
: {
mode: 'cors',
credentials: 'include', // always send cookies、HTTP Basic authentication.
headers: new Headers({
'Content-Type': ContentType.json,
}),
method: 'GET',
redirect: 'follow',
}
const { params, body, headers: headersFromProps, ...init } = Object.assign({}, baseOptions, options)
const headers = new Headers(headersFromProps || {})
const {
isAdminAPI = false, // extend: admin
isPublicAPI = false,
isMarketplaceAPI = false,
bodyStringify = true,
needAllResponseContent,
deleteContentType,
getAbortController,
fetchCompat = false,
request,
} = otherOptions
let base: string
if (isMarketplaceAPI)
base = MARKETPLACE_API_PREFIX
else if (isAdminAPI)
base = API_ADMIN // extend: admin
else if (isPublicAPI)
base = PUBLIC_API_PREFIX
else
@@ -158,14 +177,14 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
const fetchPathname = base + (url.startsWith('/') ? url : `/${url}`)
if (!isMarketplaceAPI)
(headers as any).set(CSRF_HEADER_NAME, Cookies.get(CSRF_COOKIE_NAME()) || '')
headers.set(CSRF_HEADER_NAME, Cookies.get(CSRF_COOKIE_NAME()) || '')
if (deleteContentType)
(headers as any).delete('Content-Type')
headers.delete('Content-Type')
// ! For Marketplace API, help to filter tags added in new version
if (isMarketplaceAPI)
(headers as any).set('X-Dify-Version', !IS_MARKETPLACE ? APP_VERSION : '999.0.0')
headers.set('X-Dify-Version', !IS_MARKETPLACE ? APP_VERSION : '999.0.0')
const client = baseClient.extend({
hooks: {
@@ -185,7 +204,7 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
},
})
const res = await client(fetchPathname, {
const res = await client(request || fetchPathname, {
...init,
headers,
credentials: isMarketplaceAPI
@@ -194,8 +213,8 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
retry: {
methods: [],
},
...(bodyStringify ? { json: body } : { body: body as BodyInit }),
searchParams: params,
...(bodyStringify && !fetchCompat ? { json: body } : { body: body as BodyInit }),
searchParams: !fetchCompat ? params : undefined,
fetch(resource: RequestInfo | URL, options?: RequestInit) {
if (resource instanceof Request && options) {
const mergedHeaders = new Headers(options.headers || {})
@@ -208,7 +227,7 @@ async function base<T>(url: string, options: FetchOptionType = {}, otherOptions:
},
})
if (needAllResponseContent)
if (needAllResponseContent || fetchCompat)
return res as T
const contentType = res.headers.get('content-type')
if (