First commit to the code repository
This commit is contained in:
1 parent
692cdee407
commit
0c0dd1cb56
323 files changed
+45712
No files matched your search
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* @description common copy to clipboard
|
||||
* support https and non-https environment
|
||||
*/
|
||||
|
||||
export interface CopyResult {
|
||||
success: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description copy text to clipboard
|
||||
* @param text text to copy
|
||||
* @returns Promise<CopyResult> copy result
|
||||
*/
|
||||
export const copyToClipboard = async (text: string): Promise<CopyResult> => {
|
||||
try {
|
||||
// first try to use modern clipboard api (need https)
|
||||
if (navigator.clipboard && window.isSecureContext) {
|
||||
await navigator.clipboard.writeText(text);
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
// fallback: use document.execCommand (for non-https environment)
|
||||
const textArea = document.createElement("textarea");
|
||||
textArea.value = text;
|
||||
textArea.style.position = "fixed";
|
||||
textArea.style.left = "-999999px";
|
||||
textArea.style.top = "-999999px";
|
||||
document.body.appendChild(textArea);
|
||||
textArea.focus();
|
||||
textArea.select();
|
||||
|
||||
const successful = document.execCommand("copy");
|
||||
document.body.removeChild(textArea);
|
||||
|
||||
if (successful) {
|
||||
return { success: true };
|
||||
} else {
|
||||
throw new Error("execCommand failed");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Copy failed:", error);
|
||||
return {
|
||||
success: false,
|
||||
error: error instanceof Error ? error.message : "Unknown error",
|
||||
};
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
import CryptoJS from "crypto-js";
|
||||
|
||||
/**
|
||||
* MD5 encrypt function
|
||||
* @param text text to encrypt
|
||||
* @returns MD5 encrypted string
|
||||
*/
|
||||
export const md5Encrypt = (text: string): string => {
|
||||
return CryptoJS.MD5(text).toString();
|
||||
};
|
||||
@@ -0,0 +1,172 @@
|
||||
/**
|
||||
* 货币格式化工具函数
|
||||
*/
|
||||
|
||||
export interface CurrencyFormatOptions {
|
||||
minimumFractionDigits?: number;
|
||||
maximumFractionDigits?: number;
|
||||
showSymbol?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 格式化货币金额
|
||||
* @param amount 金额(数字)
|
||||
* @param currency 货币代码,如 'USD', 'CNY', 'EUR'
|
||||
* @param options 格式化选项
|
||||
* @returns 格式化后的货币字符串
|
||||
*/
|
||||
export const formatCurrency = (
|
||||
amount: number | undefined | null,
|
||||
currency: string = 'USD',
|
||||
options: CurrencyFormatOptions = {}
|
||||
): string => {
|
||||
// 处理无效值
|
||||
if (amount === undefined || amount === null || isNaN(amount)) {
|
||||
return '0.00';
|
||||
}
|
||||
|
||||
const {
|
||||
minimumFractionDigits = 2,
|
||||
maximumFractionDigits = 2,
|
||||
showSymbol = true
|
||||
} = options;
|
||||
|
||||
try {
|
||||
if (showSymbol) {
|
||||
return new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: currency,
|
||||
minimumFractionDigits,
|
||||
maximumFractionDigits
|
||||
}).format(amount);
|
||||
} else {
|
||||
return amount.toLocaleString('en-US', {
|
||||
minimumFractionDigits,
|
||||
maximumFractionDigits
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
// 如果货币代码无效,回退到基本格式化
|
||||
console.warn(`Invalid currency code: ${currency}, falling back to basic formatting`);
|
||||
return amount.toLocaleString('en-US', {
|
||||
minimumFractionDigits,
|
||||
maximumFractionDigits
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化价格(以分为单位转换为元)
|
||||
* @param priceInCents 以分为单位的价格
|
||||
* @param currency 货币代码
|
||||
* @param options 格式化选项
|
||||
* @returns 格式化后的价格字符串
|
||||
*/
|
||||
export const formatPrice = (
|
||||
priceInCents: number | undefined | null,
|
||||
currency: string = 'USD',
|
||||
options: CurrencyFormatOptions = {}
|
||||
): string => {
|
||||
if (priceInCents === undefined || priceInCents === null || isNaN(priceInCents)) {
|
||||
return formatCurrency(0, currency, options);
|
||||
}
|
||||
|
||||
const priceInDollars = priceInCents / 100;
|
||||
return formatCurrency(priceInDollars, currency, options);
|
||||
};
|
||||
|
||||
/**
|
||||
* 获取货币符号
|
||||
* @param currency 货币代码
|
||||
* @returns 货币符号
|
||||
*/
|
||||
export const getCurrencySymbol = (currency: string = 'USD'): string => {
|
||||
try {
|
||||
const formatter = new Intl.NumberFormat('en-US', {
|
||||
style: 'currency',
|
||||
currency: currency,
|
||||
minimumFractionDigits: 0,
|
||||
maximumFractionDigits: 0
|
||||
});
|
||||
|
||||
// 格式化0并提取符号
|
||||
const formatted = formatter.format(0);
|
||||
return formatted.replace(/[\d\s,]/g, '');
|
||||
} catch (error) {
|
||||
// 回退到常见货币符号
|
||||
const symbols: Record<string, string> = {
|
||||
'USD': '$',
|
||||
'EUR': '€',
|
||||
'GBP': '£',
|
||||
'JPY': '¥',
|
||||
'CNY': '¥',
|
||||
'KRW': '₩'
|
||||
};
|
||||
return symbols[currency] || '$';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 格式化余额显示
|
||||
* @param balance 余额
|
||||
* @param currency 货币代码
|
||||
* @returns 格式化后的余额字符串
|
||||
*/
|
||||
export const formatBalance = (
|
||||
balance: number | undefined | null,
|
||||
currency: string = 'USD'
|
||||
): string => {
|
||||
return formatCurrency(balance, currency, {
|
||||
minimumFractionDigits: 2,
|
||||
maximumFractionDigits: 2
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* 货币选项接口
|
||||
*/
|
||||
export interface CurrencyOption {
|
||||
value: string;
|
||||
label: string;
|
||||
symbol: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取支持的货币选项列表
|
||||
* @returns 货币选项数组
|
||||
*/
|
||||
export const getCurrencyOptions = (): CurrencyOption[] => {
|
||||
return [
|
||||
{ value: 'USD', label: 'US Dollar (USD)', symbol: '$' },
|
||||
{ value: 'EUR', label: 'Euro (EUR)', symbol: '€' },
|
||||
{ value: 'GBP', label: 'British Pound (GBP)', symbol: '£' },
|
||||
{ value: 'JPY', label: 'Japanese Yen (JPY)', symbol: '¥' },
|
||||
{ value: 'CNY', label: 'Chinese Yuan (CNY)', symbol: '¥' },
|
||||
{ value: 'KRW', label: 'Korean Won (KRW)', symbol: '₩' },
|
||||
{ value: 'CAD', label: 'Canadian Dollar (CAD)', symbol: 'C$' },
|
||||
{ value: 'AUD', label: 'Australian Dollar (AUD)', symbol: 'A$' },
|
||||
{ value: 'CHF', label: 'Swiss Franc (CHF)', symbol: 'CHF' },
|
||||
{ value: 'SEK', label: 'Swedish Krona (SEK)', symbol: 'kr' },
|
||||
{ value: 'NOK', label: 'Norwegian Krone (NOK)', symbol: 'kr' },
|
||||
{ value: 'DKK', label: 'Danish Krone (DKK)', symbol: 'kr' },
|
||||
{ value: 'PLN', label: 'Polish Zloty (PLN)', symbol: 'zł' },
|
||||
{ value: 'CZK', label: 'Czech Koruna (CZK)', symbol: 'Kč' },
|
||||
{ value: 'HUF', label: 'Hungarian Forint (HUF)', symbol: 'Ft' },
|
||||
{ value: 'RUB', label: 'Russian Ruble (RUB)', symbol: '₽' },
|
||||
{ value: 'BRL', label: 'Brazilian Real (BRL)', symbol: 'R$' },
|
||||
{ value: 'MXN', label: 'Mexican Peso (MXN)', symbol: '$' },
|
||||
{ value: 'INR', label: 'Indian Rupee (INR)', symbol: '₹' },
|
||||
{ value: 'SGD', label: 'Singapore Dollar (SGD)', symbol: 'S$' },
|
||||
{ value: 'HKD', label: 'Hong Kong Dollar (HKD)', symbol: 'HK$' },
|
||||
{ value: 'TWD', label: 'Taiwan Dollar (TWD)', symbol: 'NT$' },
|
||||
{ value: 'THB', label: 'Thai Baht (THB)', symbol: '฿' },
|
||||
{ value: 'MYR', label: 'Malaysian Ringgit (MYR)', symbol: 'RM' },
|
||||
{ value: 'IDR', label: 'Indonesian Rupiah (IDR)', symbol: 'Rp' },
|
||||
{ value: 'PHP', label: 'Philippine Peso (PHP)', symbol: '₱' },
|
||||
{ value: 'VND', label: 'Vietnamese Dong (VND)', symbol: '₫' },
|
||||
{ value: 'ZAR', label: 'South African Rand (ZAR)', symbol: 'R' },
|
||||
{ value: 'TRY', label: 'Turkish Lira (TRY)', symbol: '₺' },
|
||||
{ value: 'AED', label: 'UAE Dirham (AED)', symbol: 'د.إ' },
|
||||
{ value: 'SAR', label: 'Saudi Riyal (SAR)', symbol: '﷼' }
|
||||
];
|
||||
};
|
||||
@@ -0,0 +1,70 @@
|
||||
const STORAGE_KEY = "XPack_Shared";
|
||||
|
||||
const getUrlLanguage = (): string | null => {
|
||||
if (typeof window === "undefined") return null;
|
||||
|
||||
const urlParams = new URLSearchParams(window.location.search);
|
||||
const langParam = urlParams.get("lang") || urlParams.get("locale");
|
||||
|
||||
if (langParam && ["en", "zh-CN"].includes(langParam)) {
|
||||
return langParam;
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const getBrowserDefaultLanguage = (): string => {
|
||||
if (typeof window === "undefined") {
|
||||
// SSR时根据Accept-Language header判断,这里先返回英语
|
||||
return "en";
|
||||
}
|
||||
|
||||
const browserLang = navigator.language;
|
||||
const supportedLanguages: string[] = ["en", "zh-CN"];
|
||||
|
||||
// 尝试完全匹配
|
||||
if (supportedLanguages.includes(browserLang)) {
|
||||
return browserLang;
|
||||
}
|
||||
|
||||
// 尝试匹配语言代码前缀(如 zh-CN 匹配 zh)
|
||||
const langPrefix = browserLang.split("-")[0];
|
||||
const match = supportedLanguages.find((lang) => lang.startsWith(langPrefix));
|
||||
|
||||
return match || "en"; // 最后才fallback到英语
|
||||
};
|
||||
|
||||
export const getDefaultLanguage = (): string => {
|
||||
// 1. 优先检查URL参数
|
||||
const urlLang = getUrlLanguage();
|
||||
if (urlLang) {
|
||||
return urlLang;
|
||||
}
|
||||
|
||||
if (typeof window === "undefined") {
|
||||
return getBrowserDefaultLanguage();
|
||||
}
|
||||
|
||||
try {
|
||||
// 2. 检查i18next的localStorage缓存
|
||||
const i18nextLng = localStorage.getItem("i18nextLng");
|
||||
if (i18nextLng && ["en", "zh-CN"].includes(i18nextLng)) {
|
||||
return i18nextLng;
|
||||
}
|
||||
|
||||
// 3. 检查Zustand store中的语言设置
|
||||
const raw = localStorage.getItem(STORAGE_KEY);
|
||||
if (raw) {
|
||||
const parsed = JSON.parse(raw).state?.preference;
|
||||
if (parsed?.language && typeof parsed.language === "string") {
|
||||
return parsed.language;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
// eslint-disable-next-line no-console
|
||||
console.warn("Failed to parse language from localStorage", error);
|
||||
}
|
||||
|
||||
// 4. 最后使用浏览器语言检测
|
||||
return getBrowserDefaultLanguage();
|
||||
};
|
||||
@@ -0,0 +1,57 @@
|
||||
// Utility functions related to Model Context Protocol (MCP)
|
||||
|
||||
/**
|
||||
* Sanitises a user-provided Agent name so that it can safely be used as an
|
||||
* MCP server identifier.
|
||||
*
|
||||
* Rules (derived from MCP spec and common conventions):
|
||||
* 1. Lower-case alphanumeric characters, hyphen (-) and underscore (_)
|
||||
* 2. Cannot start with a digit or hyphen – if it does, we prefix with "mcp-".
|
||||
* 3. Consecutive illegal characters are collapsed into a single hyphen.
|
||||
* 4. Empty or fully sanitised away strings fallback to "xpack-mcp-service".
|
||||
*/
|
||||
export function sanitizeMCPServerName(rawName: string | undefined): string {
|
||||
if (!rawName) return "xpack-mcp-service";
|
||||
|
||||
// Lower-case & replace invalid chars with hyphen
|
||||
let name = rawName
|
||||
.trim()
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9-_]+/g, "-");
|
||||
|
||||
// Collapse multiple hyphens
|
||||
name = name.replace(/-+/g, "-");
|
||||
|
||||
// Trim leading/trailing hyphens
|
||||
name = name.replace(/^-+/, "").replace(/-+$/, "");
|
||||
|
||||
// Ensure it doesn't start with a digit or empty
|
||||
if (!name || !/^(xpack-)/.test(name)) {
|
||||
name = `xpack-${name}`;
|
||||
}
|
||||
|
||||
return name || "xpack-mcp-service";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates a complete MCP configuration JSON string for a given API key & agent.
|
||||
*
|
||||
* @param key The raw API key returned from backend.
|
||||
* @param name The human-friendly Agent name.
|
||||
* @returns A pretty-printed JSON string that can be used directly in config files.
|
||||
*/
|
||||
export function generateMCPConfig(key: string, name?: string): string {
|
||||
const sanitizedName = sanitizeMCPServerName(name);
|
||||
const baseUrl = `${process.env.NEXT_PUBLIC_MCP_URL}?apikey=${key}`;
|
||||
|
||||
const mcpConfig = {
|
||||
mcpServers: {
|
||||
[sanitizedName]: {
|
||||
type: "sse",
|
||||
url: baseUrl,
|
||||
},
|
||||
},
|
||||
} as const;
|
||||
|
||||
return JSON.stringify(mcpConfig, null, 2);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { platformConfigService } from "@/services/platformConfigService";
|
||||
import type { Metadata } from "next";
|
||||
import { unstable_cache } from "next/cache";
|
||||
|
||||
// use Next.js unstable_cache to cache with tags
|
||||
export const getCachedPlatformConfig = unstable_cache(
|
||||
async () => {
|
||||
return await platformConfigService.getPlatformConfig();
|
||||
},
|
||||
["platform-config"],
|
||||
{
|
||||
tags: ["platform-config"],
|
||||
revalidate: 300, // 5 minutes cache
|
||||
}
|
||||
);
|
||||
|
||||
// get dynamic title
|
||||
export const getDynamicTitle = async (pageTitle?: string) => {
|
||||
const platformConfig = await getCachedPlatformConfig();
|
||||
const websiteTitle =
|
||||
platformConfig.platform?.website_title ||
|
||||
"Connect Your AI Agent to the Real World - XPack";
|
||||
|
||||
if (pageTitle) {
|
||||
return `${pageTitle} - ${websiteTitle}`;
|
||||
}
|
||||
|
||||
return websiteTitle;
|
||||
};
|
||||
|
||||
// create base metadata object
|
||||
export const createBaseMetadata = (title?: string): Metadata => ({
|
||||
title,
|
||||
description:
|
||||
"unified platform simplifies API access, accelerates development, and unlocks a universe of capabilities for your AI agents, chatbots, and automated workflows. ",
|
||||
keywords: [
|
||||
"API integration platform",
|
||||
"Agent API connectivity",
|
||||
"AI agent integration",
|
||||
"Chatbot API access",
|
||||
"Automated workflow API",
|
||||
"Third-party API connector",
|
||||
"MCP connector",
|
||||
"MCP integration",
|
||||
"API management for agents",
|
||||
"Agent development platform",
|
||||
"API orchestration",
|
||||
"API gateway for agents",
|
||||
"No-code API integration",
|
||||
"Low-code API development",
|
||||
"Enterprise API connectivity",
|
||||
"Developer tools API",
|
||||
"XPack.AI",
|
||||
],
|
||||
authors: [{ name: "XPack.AI Team" }],
|
||||
openGraph: {
|
||||
title: "XPack.AI",
|
||||
description: "Connect Your Agent to Any API/MCP",
|
||||
type: "website",
|
||||
},
|
||||
});
|
||||
Reference in new issue
Block a user