From eb1d46d18e374e2dba815111efa21b805ed8e575 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 20 Sep 2024 11:52:59 +0800 Subject: [PATCH 1/4] CozeAI --- components/Coze.js | 39 +++++++++++++++++++++++++++++++++++ components/ExternalPlugins.js | 3 +++ 2 files changed, 42 insertions(+) create mode 100644 components/Coze.js diff --git a/components/Coze.js b/components/Coze.js new file mode 100644 index 00000000000..d4472d296dc --- /dev/null +++ b/components/Coze.js @@ -0,0 +1,39 @@ +import { siteConfig } from '@/lib/config' +import { loadExternalResource } from '@/lib/utils' +import { useEffect } from 'react' + +/** + * Coze-AI机器人 + * @returns + */ +export default function Coze() { + const cozeSrc = siteConfig( + 'COZE_SRC_URL', + 'https://lf-cdn.coze.cn/obj/unpkg/flow-platform/chat-app-sdk/0.1.0-beta.6/libs/cn/index.js' + ) + const title = siteConfig('COZE_TITLE', 'NotionNext助手') + const botId = siteConfig('COZE_BOT_ID') + + const loadCoze = async () => { + await loadExternalResource(cozeSrc) + CozeWebSDK = window.CozeWebSDK + if (CozeWebSDK) { + new CozeWebSDK.WebChatClient({ + config: { + bot_id: botId + }, + componentProps: { + title: title + } + }) + } + } + + useEffect(() => { + if (!botId) { + return + } + loadCoze() + }, []) + return <> +} diff --git a/components/ExternalPlugins.js b/components/ExternalPlugins.js index d798a940cdd..772f8b005fc 100644 --- a/components/ExternalPlugins.js +++ b/components/ExternalPlugins.js @@ -9,6 +9,7 @@ import { convertInnerUrl } from '@/lib/notion/convertInnerUrl' import { isBrowser, loadExternalResource } from '@/lib/utils' import { useRouter } from 'next/router' import { useEffect } from 'react' +import Coze from './Coze' import { initGoogleAdsense } from './GoogleAdsense' /** @@ -65,6 +66,7 @@ const ExternalPlugin = props => { const CUSTOM_EXTERNAL_JS = siteConfig('CUSTOM_EXTERNAL_JS') // 默认关闭NProgress const ENABLE_NPROGRSS = siteConfig('ENABLE_NPROGRSS', false) + const COZE_BOT_ID = siteConfig('COZE_BOT_ID') // 自定义样式css和js引入 if (isBrowser) { @@ -150,6 +152,7 @@ const ExternalPlugin = props => { {ENABLE_NPROGRSS && } {ANALYTICS_51LA_ID && ANALYTICS_51LA_CK && } + {COZE_BOT_ID && } {ANALYTICS_51LA_ID && ANALYTICS_51LA_CK && ( <> From d309aefca86281ac005cc6f67d7c8d039b6c200d Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 20 Sep 2024 11:54:31 +0800 Subject: [PATCH 2/4] fix --- components/Coze.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/Coze.js b/components/Coze.js index d4472d296dc..cfed4a9ffea 100644 --- a/components/Coze.js +++ b/components/Coze.js @@ -16,7 +16,7 @@ export default function Coze() { const loadCoze = async () => { await loadExternalResource(cozeSrc) - CozeWebSDK = window.CozeWebSDK + const CozeWebSDK = window?.CozeWebSDK if (CozeWebSDK) { new CozeWebSDK.WebChatClient({ config: { From 6c0882c151587bcb6c0f826e22e7fb43ff231ca6 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 20 Sep 2024 11:55:39 +0800 Subject: [PATCH 3/4] fix --- components/Coze.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/components/Coze.js b/components/Coze.js index cfed4a9ffea..7b806ae706d 100644 --- a/components/Coze.js +++ b/components/Coze.js @@ -18,7 +18,7 @@ export default function Coze() { await loadExternalResource(cozeSrc) const CozeWebSDK = window?.CozeWebSDK if (CozeWebSDK) { - new CozeWebSDK.WebChatClient({ + const cozeClient = new CozeWebSDK.WebChatClient({ config: { bot_id: botId }, @@ -26,6 +26,7 @@ export default function Coze() { title: title } }) + console.log('coze', cozeClient) } } From ca58e15f291b6fd99fc3e20a139bfe0c84a90957 Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Fri, 20 Sep 2024 12:14:01 +0800 Subject: [PATCH 4/4] siteConfig MAX_SAFE_INTEGER --- lib/config.js | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) diff --git a/lib/config.js b/lib/config.js index b4f70e82528..13d805bac34 100644 --- a/lib/config.js +++ b/lib/config.js @@ -102,50 +102,47 @@ export const siteConfig = (key, defaultVal = null, extendConfig = {}) => { * @returns */ export const convertVal = val => { - // 如果传入参数本身就是obj、数组、boolean 就无需处理 + // 如果传入参数本身就是 obj、数组、boolean,就无需处理 if (typeof val !== 'string' || !val) { return val } - // 解析数字,parseInt将字符串转换为数字 + // 检测是否数字并避免数值溢出 if (/^\d+$/.test(val)) { - return parseInt(val) + const parsedNum = Number(val) + // 如果数值大于 JavaScript 最大安全整数,则作为字符串返回 + if (parsedNum > Number.MAX_SAFE_INTEGER) { + return val + '' + } + return parsedNum } - // 检测是否url - if (isUrl(val)) { - return val - } - // 检测是否url + // 检测是否为布尔值 if (val === 'true' || val === 'false') { return JSON.parse(val) } + // 检测是否为 URL + if (isUrl(val)) { + return val + } + // 配置值前可能有污染的空格 if (val.indexOf('[') < 0 && val.indexOf('{') < 0) { return val } - // 转换 [] , {} , true/false 这类字符串为对象 + // 转换 [] , {} 这类字符串为对象 try { - // 尝试解析json const parsedJson = JSON.parse(val) if (parsedJson !== null) { return parsedJson } } catch (error) { - // try { - // // 尝试解析对象,对象解析能力不如上一步的json - // const evalObj = eval('(' + val + ')') - // if (evalObj !== null) { - // return evalObj - // } - // } catch (error) { - // // Ojbject 解析失败,返回原始字符串值 - // return val - // } + // 解析失败,返回原始字符串 return val } + return val }