This commit is contained in:
unknown 2025-08-19 16:42:50 +07:00
parent 5e4525e979
commit e63f0bc7a7
242 changed files with 22660 additions and 5953 deletions

8
src/utils/classnames.ts Normal file
View file

@ -0,0 +1,8 @@
import { twMerge } from 'tailwind-merge';
import cn from 'classnames';
const classNames = (...cls: cn.ArgumentArray) => {
return twMerge(cn(cls));
};
export default classNames;

55
src/utils/format.ts Normal file
View file

@ -0,0 +1,55 @@
/**
* Formats a number with comma separators.
* @example formatNumber(1234567) will return '1,234,567'
* @example formatNumber(1234567.89) will return '1,234,567.89'
*/
export const formatNumber = (num: number | string) => {
if (!num) return num;
const parts = num.toString().split('.');
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return parts.join('.');
};
/**
* Format file size into standard string format.
* @param fileSize file size (Byte)
* @example formatFileSize(1024) will return '1.00KB'
* @example formatFileSize(1024 * 1024) will return '1.00MB'
*/
export const formatFileSize = (fileSize: number) => {
if (!fileSize) return fileSize;
const units = ['', 'K', 'M', 'G', 'T', 'P'];
let index = 0;
while (fileSize >= 1024 && index < units.length) {
fileSize = fileSize / 1024;
index++;
}
return `${fileSize.toFixed(2)}${units[index]}B`;
};
/**
* Format time into standard string format.
* @example formatTime(60) will return '1.00 min'
* @example formatTime(60 * 60) will return '1.00 h'
*/
export const formatTime = (seconds: number) => {
if (!seconds) return seconds;
const units = ['sec', 'min', 'h'];
let index = 0;
while (seconds >= 60 && index < units.length) {
seconds = seconds / 60;
index++;
}
return `${seconds.toFixed(2)} ${units[index]}`;
};
export const downloadFile = ({ data, fileName }: { data: Blob; fileName: string }) => {
const url = window.URL.createObjectURL(data);
const a = document.createElement('a');
a.href = url;
a.download = fileName;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
};

60
src/utils/prompt.ts Normal file
View file

@ -0,0 +1,60 @@
import type { PromptVariable, UserInputFormItem } from '@/types/app';
export function replaceVarWithValues(str: string, promptVariables: PromptVariable[], inputs: Record<string, any>) {
return str.replace(/\{\{([^}]+)\}\}/g, (match, key) => {
const name = inputs[key];
if (name) return name;
const valueObj: PromptVariable | undefined = promptVariables.find((v) => v.key === key);
return valueObj ? `{{${valueObj.key}}}` : match;
});
}
export const userInputsFormToPromptVariables = (useInputs: UserInputFormItem[] | null) => {
if (!useInputs) return [];
const promptVariables: PromptVariable[] = [];
useInputs.forEach((item: any) => {
const [type, content] = (() => {
const type = Object.keys(item)[0];
return [type === 'text-input' ? 'string' : type, item[type]];
})();
if (type === 'string' || type === 'paragraph') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
max_length: content.max_length,
options: []
});
} else if (type === 'number') {
promptVariables.push({
key: content.variable,
name: content.label,
required: content.required,
type,
options: []
});
} else if (type === 'file' || type === 'file-list') {
promptVariables.push({
...content,
key: content.variable,
name: content.label,
required: content.required,
type,
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;
};

6
src/utils/string.ts Normal file
View file

@ -0,0 +1,6 @@
const chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_';
export function randomString(length: number) {
let result = '';
for (let i = length; i > 0; --i) result += chars[Math.floor(Math.random() * chars.length)];
return result;
}

View file

@ -0,0 +1,5 @@
import { createBrowserClient } from '@supabase/ssr';
export function createClient() {
return createBrowserClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!);
}

View file

@ -0,0 +1,67 @@
import { createServerClient } from '@supabase/ssr';
import { NextResponse, type NextRequest } from 'next/server';
export async function updateSession(request: NextRequest) {
let supabaseResponse = NextResponse.next({
request
});
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll();
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value, options }) => request.cookies.set(name, value));
supabaseResponse = NextResponse.next({
request
});
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
);
}
}
}
);
// Do not run code between createServerClient and
// supabase.auth.getUser(). A simple mistake could make it very hard to debug
// issues with users being randomly logged out.
// IMPORTANT: DO NOT REMOVE auth.getUser()
const {
data: { user }
} = await supabase.auth.getUser();
if (
!user &&
!request.nextUrl.pathname.startsWith('/login') &&
!request.nextUrl.pathname.startsWith('/auth') &&
!request.nextUrl.pathname.startsWith('/error') &&
!request.nextUrl.pathname.startsWith('/')
) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone();
url.pathname = '/login';
return NextResponse.redirect(url);
}
// IMPORTANT: You *must* return the supabaseResponse object as it is.
// If you're creating a new response object with NextResponse.next() make sure to:
// 1. Pass the request in it, like so:
// const myNewResponse = NextResponse.next({ request })
// 2. Copy over the cookies, like so:
// myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll())
// 3. Change the myNewResponse object to fit your needs, but avoid changing
// the cookies!
// 4. Finally:
// return myNewResponse
// If this is not done, you may be causing the browser and server to go out
// of sync and terminate the user's session prematurely!
return supabaseResponse;
}

View file

@ -0,0 +1,23 @@
import { createServerClient } from '@supabase/ssr';
import { cookies } from 'next/headers';
export async function createClient() {
const cookieStore = await cookies();
return createServerClient(process.env.NEXT_PUBLIC_SUPABASE_URL!, process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!, {
cookies: {
getAll() {
return cookieStore.getAll();
},
setAll(cookiesToSet) {
try {
cookiesToSet.forEach(({ name, value, options }) => cookieStore.set(name, value, options));
} catch {
// The `setAll` method was called from a Server Component.
// This can be ignored if you have middleware refreshing
// user sessions.
}
}
}
});
}

25
src/utils/tools.ts Normal file
View file

@ -0,0 +1,25 @@
import type { ThoughtItem } from '@/components/layouts/chat/type';
import type { VisionFile } from '@/types/app';
export const sortAgentSorts = (list: ThoughtItem[]) => {
if (!list) return list;
if (list.some((item) => item.position === undefined)) return list;
const temp = [...list];
temp.sort((a, b) => a.position - b.position);
return temp;
};
export const addFileInfos = (list: ThoughtItem[], messageFiles: VisionFile[]) => {
if (!list || !messageFiles) return list;
return list.map((item) => {
if (item.files && item.files?.length > 0) {
return {
...item,
message_files: item.files.map((fileId) =>
messageFiles.find((file) => file.id === fileId)
) as VisionFile[]
};
}
return item;
});
};