From 1db5c3bf397b6ed2e2599dc636381300cba5ac0c Mon Sep 17 00:00:00 2001 From: mlogclub Date: Fri, 24 Apr 2026 22:37:11 +0800 Subject: [PATCH] feat: minify SDK JavaScript and improve build process - Implemented minification of the cs-ai-agent-sdk.min.js file using Terser during the build process. - Updated build-sdk.mjs to read the source code, minify it, and write the output to the target directory. - Added logging to display the size reduction after minification. - Removed redundant code and streamlined the configuration handling in the SDK. --- web/package.json | 1 + web/pnpm-lock.yaml | 78 +++++ web/public/sdk/cs-ai-agent-sdk.min.js | 436 +------------------------- web/scripts/build-sdk.mjs | 26 +- 4 files changed, 104 insertions(+), 437 deletions(-) diff --git a/web/package.json b/web/package.json index 4827d22..58c8040 100644 --- a/web/package.json +++ b/web/package.json @@ -61,6 +61,7 @@ "eslint-config-next": "16.1.6", "sass": "^1.98.0", "tailwindcss": "^4", + "terser": "^5.46.2", "typescript": "^5" } } diff --git a/web/pnpm-lock.yaml b/web/pnpm-lock.yaml index 16bff45..c4fc394 100644 --- a/web/pnpm-lock.yaml +++ b/web/pnpm-lock.yaml @@ -153,6 +153,9 @@ importers: tailwindcss: specifier: ^4 version: 4.2.1 + terser: + specifier: ^5.46.2 + version: 5.46.2 typescript: specifier: ^5 version: 5.9.3 @@ -571,89 +574,105 @@ packages: resolution: {integrity: sha512-excjX8DfsIcJ10x1Kzr4RcWe1edC9PquDRRPx3YVCvQv+U5p7Yin2s32ftzikXojb1PIFc/9Mt28/y+iRklkrw==} cpu: [arm64] os: [linux] + libc: [glibc] '@img/sharp-libvips-linux-arm@1.2.4': resolution: {integrity: sha512-bFI7xcKFELdiNCVov8e44Ia4u2byA+l3XtsAj+Q8tfCwO6BQ8iDojYdvoPMqsKDkuoOo+X6HZA0s0q11ANMQ8A==} cpu: [arm] os: [linux] + libc: [glibc] '@img/sharp-libvips-linux-ppc64@1.2.4': resolution: {integrity: sha512-FMuvGijLDYG6lW+b/UvyilUWu5Ayu+3r2d1S8notiGCIyYU/76eig1UfMmkZ7vwgOrzKzlQbFSuQfgm7GYUPpA==} cpu: [ppc64] os: [linux] + libc: [glibc] '@img/sharp-libvips-linux-riscv64@1.2.4': resolution: {integrity: sha512-oVDbcR4zUC0ce82teubSm+x6ETixtKZBh/qbREIOcI3cULzDyb18Sr/Wcyx7NRQeQzOiHTNbZFF1UwPS2scyGA==} cpu: [riscv64] os: [linux] + libc: [glibc] '@img/sharp-libvips-linux-s390x@1.2.4': resolution: {integrity: sha512-qmp9VrzgPgMoGZyPvrQHqk02uyjA0/QrTO26Tqk6l4ZV0MPWIW6LTkqOIov+J1yEu7MbFQaDpwdwJKhbJvuRxQ==} cpu: [s390x] os: [linux] + libc: [glibc] '@img/sharp-libvips-linux-x64@1.2.4': resolution: {integrity: sha512-tJxiiLsmHc9Ax1bz3oaOYBURTXGIRDODBqhveVHonrHJ9/+k89qbLl0bcJns+e4t4rvaNBxaEZsFtSfAdquPrw==} cpu: [x64] os: [linux] + libc: [glibc] '@img/sharp-libvips-linuxmusl-arm64@1.2.4': resolution: {integrity: sha512-FVQHuwx1IIuNow9QAbYUzJ+En8KcVm9Lk5+uGUQJHaZmMECZmOlix9HnH7n1TRkXMS0pGxIJokIVB9SuqZGGXw==} cpu: [arm64] os: [linux] + libc: [musl] '@img/sharp-libvips-linuxmusl-x64@1.2.4': resolution: {integrity: sha512-+LpyBk7L44ZIXwz/VYfglaX/okxezESc6UxDSoyo2Ks6Jxc4Y7sGjpgU9s4PMgqgjj1gZCylTieNamqA1MF7Dg==} cpu: [x64] os: [linux] + libc: [musl] '@img/sharp-linux-arm64@0.34.5': resolution: {integrity: sha512-bKQzaJRY/bkPOXyKx5EVup7qkaojECG6NLYswgktOZjaXecSAeCWiZwwiFf3/Y+O1HrauiE3FVsGxFg8c24rZg==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [arm64] os: [linux] + libc: [glibc] '@img/sharp-linux-arm@0.34.5': resolution: {integrity: sha512-9dLqsvwtg1uuXBGZKsxem9595+ujv0sJ6Vi8wcTANSFpwV/GONat5eCkzQo/1O6zRIkh0m/8+5BjrRr7jDUSZw==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [arm] os: [linux] + libc: [glibc] '@img/sharp-linux-ppc64@0.34.5': resolution: {integrity: sha512-7zznwNaqW6YtsfrGGDA6BRkISKAAE1Jo0QdpNYXNMHu2+0dTrPflTLNkpc8l7MUP5M16ZJcUvysVWWrMefZquA==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [ppc64] os: [linux] + libc: [glibc] '@img/sharp-linux-riscv64@0.34.5': resolution: {integrity: sha512-51gJuLPTKa7piYPaVs8GmByo7/U7/7TZOq+cnXJIHZKavIRHAP77e3N2HEl3dgiqdD/w0yUfiJnII77PuDDFdw==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [riscv64] os: [linux] + libc: [glibc] '@img/sharp-linux-s390x@0.34.5': resolution: {integrity: sha512-nQtCk0PdKfho3eC5MrbQoigJ2gd1CgddUMkabUj+rBevs8tZ2cULOx46E7oyX+04WGfABgIwmMC0VqieTiR4jg==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [s390x] os: [linux] + libc: [glibc] '@img/sharp-linux-x64@0.34.5': resolution: {integrity: sha512-MEzd8HPKxVxVenwAa+JRPwEC7QFjoPWuS5NZnBt6B3pu7EG2Ge0id1oLHZpPJdn3OQK+BQDiw9zStiHBTJQQQQ==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [x64] os: [linux] + libc: [glibc] '@img/sharp-linuxmusl-arm64@0.34.5': resolution: {integrity: sha512-fprJR6GtRsMt6Kyfq44IsChVZeGN97gTD331weR1ex1c1rypDEABN6Tm2xa1wE6lYb5DdEnk03NZPqA7Id21yg==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [arm64] os: [linux] + libc: [musl] '@img/sharp-linuxmusl-x64@0.34.5': resolution: {integrity: sha512-Jg8wNT1MUzIvhBFxViqrEhWDGzqymo3sV7z7ZsaWbZNDLXRJZoRGrjulp60YYtV4wfY8VIKcWidjojlLcWrd8Q==} engines: {node: ^18.17.0 || ^20.3.0 || >=21.0.0} cpu: [x64] os: [linux] + libc: [musl] '@img/sharp-wasm32@0.34.5': resolution: {integrity: sha512-OdWTEiVkY2PHwqkbBI8frFxQQFekHaSSkUIJkwzclWZe64O1X4UlUjqqqLaPbUpMOQk6FBu/HtlGXNblIs0huw==} @@ -723,6 +742,9 @@ packages: resolution: {integrity: sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==} engines: {node: '>=6.0.0'} + '@jridgewell/source-map@0.3.11': + resolution: {integrity: sha512-ZMp1V8ZFcPG5dIWnQLr3NSI1MiCU7UETdS/A0G8V/XWHvJv3ZsFqutJn1Y5RPmAPX6F3BiE397OqveU/9NCuIA==} + '@jridgewell/sourcemap-codec@1.5.5': resolution: {integrity: sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==} @@ -826,24 +848,28 @@ packages: engines: {node: '>= 10'} cpu: [arm64] os: [linux] + libc: [glibc] '@next/swc-linux-arm64-musl@16.1.6': resolution: {integrity: sha512-S4J2v+8tT3NIO9u2q+S0G5KdvNDjXfAv06OhfOzNDaBn5rw84DGXWndOEB7d5/x852A20sW1M56vhC/tRVbccQ==} engines: {node: '>= 10'} cpu: [arm64] os: [linux] + libc: [musl] '@next/swc-linux-x64-gnu@16.1.6': resolution: {integrity: sha512-2eEBDkFlMMNQnkTyPBhQOAyn2qMxyG2eE7GPH2WIDGEpEILcBPI/jdSv4t6xupSP+ot/jkfrCShLAa7+ZUPcJQ==} engines: {node: '>= 10'} cpu: [x64] os: [linux] + libc: [glibc] '@next/swc-linux-x64-musl@16.1.6': resolution: {integrity: sha512-oicJwRlyOoZXVlxmIMaTq7f8pN9QNbdes0q2FXfRsPhfCi8n8JmOZJm5oo1pwDaFbnnD421rVU409M3evFbIqg==} engines: {node: '>= 10'} cpu: [x64] os: [linux] + libc: [musl] '@next/swc-win32-arm64-msvc@16.1.6': resolution: {integrity: sha512-gQmm8izDTPgs+DCWH22kcDmuUp7NyiJgEl18bcr8irXA5N2m2O+JQIr6f3ct42GOs9c0h8QF3L5SzIxcYAAXXw==} @@ -923,36 +949,42 @@ packages: engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] + libc: [glibc] '@parcel/watcher-linux-arm-musl@2.5.6': resolution: {integrity: sha512-Ve3gUCG57nuUUSyjBq/MAM0CzArtuIOxsBdQ+ftz6ho8n7s1i9E1Nmk/xmP323r2YL0SONs1EuwqBp2u1k5fxg==} engines: {node: '>= 10.0.0'} cpu: [arm] os: [linux] + libc: [musl] '@parcel/watcher-linux-arm64-glibc@2.5.6': resolution: {integrity: sha512-f2g/DT3NhGPdBmMWYoxixqYr3v/UXcmLOYy16Bx0TM20Tchduwr4EaCbmxh1321TABqPGDpS8D/ggOTaljijOA==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] + libc: [glibc] '@parcel/watcher-linux-arm64-musl@2.5.6': resolution: {integrity: sha512-qb6naMDGlbCwdhLj6hgoVKJl2odL34z2sqkC7Z6kzir8b5W65WYDpLB6R06KabvZdgoHI/zxke4b3zR0wAbDTA==} engines: {node: '>= 10.0.0'} cpu: [arm64] os: [linux] + libc: [musl] '@parcel/watcher-linux-x64-glibc@2.5.6': resolution: {integrity: sha512-kbT5wvNQlx7NaGjzPFu8nVIW1rWqV780O7ZtkjuWaPUgpv2NMFpjYERVi0UYj1msZNyCzGlaCWEtzc+exjMGbQ==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] + libc: [glibc] '@parcel/watcher-linux-x64-musl@2.5.6': resolution: {integrity: sha512-1JRFeC+h7RdXwldHzTsmdtYR/Ku8SylLgTU/reMuqdVD7CtLwf0VR1FqeprZ0eHQkO0vqsbvFLXUmYm/uNKJBg==} engines: {node: '>= 10.0.0'} cpu: [x64] os: [linux] + libc: [musl] '@parcel/watcher-win32-arm64@2.5.6': resolution: {integrity: sha512-3ukyebjc6eGlw9yRt678DxVF7rjXatWiHvTXqphZLvo7aC5NdEgFufVwjFfY51ijYEWpXbqF5jtrK275z52D4Q==} @@ -1208,24 +1240,28 @@ packages: engines: {node: '>= 20'} cpu: [arm64] os: [linux] + libc: [glibc] '@tailwindcss/oxide-linux-arm64-musl@4.2.1': resolution: {integrity: sha512-WZA0CHRL/SP1TRbA5mp9htsppSEkWuQ4KsSUumYQnyl8ZdT39ntwqmz4IUHGN6p4XdSlYfJwM4rRzZLShHsGAQ==} engines: {node: '>= 20'} cpu: [arm64] os: [linux] + libc: [musl] '@tailwindcss/oxide-linux-x64-gnu@4.2.1': resolution: {integrity: sha512-qMFzxI2YlBOLW5PhblzuSWlWfwLHaneBE0xHzLrBgNtqN6mWfs+qYbhryGSXQjFYB1Dzf5w+LN5qbUTPhW7Y5g==} engines: {node: '>= 20'} cpu: [x64] os: [linux] + libc: [glibc] '@tailwindcss/oxide-linux-x64-musl@4.2.1': resolution: {integrity: sha512-5r1X2FKnCMUPlXTWRYpHdPYUY6a1Ar/t7P24OuiEdEOmms5lyqjDRvVY1yy9Rmioh+AunQ0rWiOTPE8F9A3v5g==} engines: {node: '>= 20'} cpu: [x64] os: [linux] + libc: [musl] '@tailwindcss/oxide-wasm32-wasi@4.2.1': resolution: {integrity: sha512-MGFB5cVPvshR85MTJkEvqDUnuNoysrsRxd6vnk1Lf2tbiqNlXpHYZqkqOQalydienEWOHHFyyuTSYRsLfxFJ2Q==} @@ -1613,41 +1649,49 @@ packages: resolution: {integrity: sha512-34gw7PjDGB9JgePJEmhEqBhWvCiiWCuXsL9hYphDF7crW7UgI05gyBAi6MF58uGcMOiOqSJ2ybEeCvHcq0BCmQ==} cpu: [arm64] os: [linux] + libc: [glibc] '@unrs/resolver-binding-linux-arm64-musl@1.11.1': resolution: {integrity: sha512-RyMIx6Uf53hhOtJDIamSbTskA99sPHS96wxVE/bJtePJJtpdKGXO1wY90oRdXuYOGOTuqjT8ACccMc4K6QmT3w==} cpu: [arm64] os: [linux] + libc: [musl] '@unrs/resolver-binding-linux-ppc64-gnu@1.11.1': resolution: {integrity: sha512-D8Vae74A4/a+mZH0FbOkFJL9DSK2R6TFPC9M+jCWYia/q2einCubX10pecpDiTmkJVUH+y8K3BZClycD8nCShA==} cpu: [ppc64] os: [linux] + libc: [glibc] '@unrs/resolver-binding-linux-riscv64-gnu@1.11.1': resolution: {integrity: sha512-frxL4OrzOWVVsOc96+V3aqTIQl1O2TjgExV4EKgRY09AJ9leZpEg8Ak9phadbuX0BA4k8U5qtvMSQQGGmaJqcQ==} cpu: [riscv64] os: [linux] + libc: [glibc] '@unrs/resolver-binding-linux-riscv64-musl@1.11.1': resolution: {integrity: sha512-mJ5vuDaIZ+l/acv01sHoXfpnyrNKOk/3aDoEdLO/Xtn9HuZlDD6jKxHlkN8ZhWyLJsRBxfv9GYM2utQ1SChKew==} cpu: [riscv64] os: [linux] + libc: [musl] '@unrs/resolver-binding-linux-s390x-gnu@1.11.1': resolution: {integrity: sha512-kELo8ebBVtb9sA7rMe1Cph4QHreByhaZ2QEADd9NzIQsYNQpt9UkM9iqr2lhGr5afh885d/cB5QeTXSbZHTYPg==} cpu: [s390x] os: [linux] + libc: [glibc] '@unrs/resolver-binding-linux-x64-gnu@1.11.1': resolution: {integrity: sha512-C3ZAHugKgovV5YvAMsxhq0gtXuwESUKc5MhEtjBpLoHPLYM+iuwSj3lflFwK3DPm68660rZ7G8BMcwSro7hD5w==} cpu: [x64] os: [linux] + libc: [glibc] '@unrs/resolver-binding-linux-x64-musl@1.11.1': resolution: {integrity: sha512-rV0YSoyhK2nZ4vEswT/QwqzqQXw5I6CjoaYMOX0TqBlWhojUf8P94mvI7nuJTeaCkkds3QE4+zS8Ko+GdXuZtA==} cpu: [x64] os: [linux] + libc: [musl] '@unrs/resolver-binding-wasm32-wasi@1.11.1': resolution: {integrity: sha512-5u4RkfxJm+Ng7IWgkzi3qrFOvLvQYnPBmjmZQ8+szTK/b31fQCnleNl1GgEt7nIsZRIf5PLhPwT0WM+q45x/UQ==} @@ -1821,6 +1865,9 @@ packages: engines: {node: ^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7} hasBin: true + buffer-from@1.1.2: + resolution: {integrity: sha512-E+XQCRwSbaaiChtv6k6Dwgc+bx+Bs6vuKJHHl5kox/BaKbhiXzqQOwK4cO22yElGp2OCmjwVhT3HmxgyPGnJfQ==} + bundle-name@4.1.0: resolution: {integrity: sha512-tjwM5exMg6BGRI+kNmTntNsvdZS1X8BFYS6tnJ2hdH0kVxM6/eVZ2xy+FqStSWvYmtfFMDLIxurorHwDKfDz5Q==} engines: {node: '>=18'} @@ -2935,24 +2982,28 @@ packages: engines: {node: '>= 12.0.0'} cpu: [arm64] os: [linux] + libc: [glibc] lightningcss-linux-arm64-musl@1.31.1: resolution: {integrity: sha512-mVZ7Pg2zIbe3XlNbZJdjs86YViQFoJSpc41CbVmKBPiGmC4YrfeOyz65ms2qpAobVd7WQsbW4PdsSJEMymyIMg==} engines: {node: '>= 12.0.0'} cpu: [arm64] os: [linux] + libc: [musl] lightningcss-linux-x64-gnu@1.31.1: resolution: {integrity: sha512-xGlFWRMl+0KvUhgySdIaReQdB4FNudfUTARn7q0hh/V67PVGCs3ADFjw+6++kG1RNd0zdGRlEKa+T13/tQjPMA==} engines: {node: '>= 12.0.0'} cpu: [x64] os: [linux] + libc: [glibc] lightningcss-linux-x64-musl@1.31.1: resolution: {integrity: sha512-eowF8PrKHw9LpoZii5tdZwnBcYDxRw2rRCyvAXLi34iyeYfqCQNA9rmUM0ce62NlPhCvof1+9ivRaTY6pSKDaA==} engines: {node: '>= 12.0.0'} cpu: [x64] os: [linux] + libc: [musl] lightningcss-win32-arm64-msvc@1.31.1: resolution: {integrity: sha512-aJReEbSEQzx1uBlQizAOBSjcmr9dCdL3XuC/6HLXAxmtErsj2ICo5yYggg1qOODQMtnjNQv2UHb9NpOuFtYe4w==} @@ -3709,6 +3760,9 @@ packages: resolution: {integrity: sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==} engines: {node: '>=0.10.0'} + source-map-support@0.5.21: + resolution: {integrity: sha512-uBHU3L3czsIyYXKX88fdrGovxdSCoTGDRZ6SYXtSRxLZUzHg5P/66Ht6uoUlHu9EZod+inXhKo3qQgwXUT/y1w==} + source-map@0.6.1: resolution: {integrity: sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==} engines: {node: '>=0.10.0'} @@ -3831,6 +3885,11 @@ packages: resolution: {integrity: sha512-g9ljZiwki/LfxmQADO3dEY1CbpmXT5Hm2fJ+QaGKwSXUylMybePR7/67YW7jOrrvjEgL1Fmz5kzyAjWVWLlucg==} engines: {node: '>=6'} + terser@5.46.2: + resolution: {integrity: sha512-uxfo9fPcSgLDYob/w1FuL0c99MWiJDnv+5qXSQc5+Ki5NjVNsYi66INnMFBjf6uFz6OnX12piJQPF4IpjJTNTw==} + engines: {node: '>=10'} + hasBin: true + tiny-invariant@1.3.3: resolution: {integrity: sha512-+FbBPE1o9QAYvviau/qC5SE3caw21q3xkvWKBtja5vgqOWIHHJ3ioaq1VPfn/Szqctz2bU/oYeKd9/z5BL+PVg==} @@ -4884,6 +4943,11 @@ snapshots: '@jridgewell/resolve-uri@3.1.2': {} + '@jridgewell/source-map@0.3.11': + dependencies: + '@jridgewell/gen-mapping': 0.3.13 + '@jridgewell/trace-mapping': 0.3.31 + '@jridgewell/sourcemap-codec@1.5.5': {} '@jridgewell/trace-mapping@0.3.31': @@ -5978,6 +6042,8 @@ snapshots: node-releases: 2.0.36 update-browserslist-db: 1.2.3(browserslist@4.28.1) + buffer-from@1.1.2: {} + bundle-name@4.1.0: dependencies: run-applescript: 7.1.0 @@ -8115,6 +8181,11 @@ snapshots: source-map-js@1.2.1: {} + source-map-support@0.5.21: + dependencies: + buffer-from: 1.1.2 + source-map: 0.6.1 + source-map@0.6.1: {} stable-hash@0.0.5: {} @@ -8239,6 +8310,13 @@ snapshots: tapable@2.3.0: {} + terser@5.46.2: + dependencies: + '@jridgewell/source-map': 0.3.11 + acorn: 8.16.0 + commander: 2.20.3 + source-map-support: 0.5.21 + tiny-invariant@1.3.3: {} tinyexec@1.0.4: {} diff --git a/web/public/sdk/cs-ai-agent-sdk.min.js b/web/public/sdk/cs-ai-agent-sdk.min.js index c5abd4f..4376810 100644 --- a/web/public/sdk/cs-ai-agent-sdk.min.js +++ b/web/public/sdk/cs-ai-agent-sdk.min.js @@ -1,435 +1 @@ -(function () { - var DEFAULT_CONFIG = { - position: "right", - themeColor: "#0f6cbd", - width: "380px", - externalSource: "web_chat", - }; - - var state = window.__CS_AGENT_WIDGET_STATE__; - if (!state) { - state = { - button: null, - frame: null, - frameLoaded: false, - frameReady: false, - initSent: false, - isOpen: false, - isMaximized: false, - configLoading: false, - frameHideTimer: null, - frameDestroyTimer: null, - config: null, - frameUrl: null, - animationDuration: 260, - }; - window.__CS_AGENT_WIDGET_STATE__ = state; - } - - function normalizeConfig(config) { - var merged = {}; - var key; - for (key in DEFAULT_CONFIG) { - if (Object.prototype.hasOwnProperty.call(DEFAULT_CONFIG, key)) { - merged[key] = DEFAULT_CONFIG[key]; - } - } - config = config || {}; - for (key in config) { - if (Object.prototype.hasOwnProperty.call(config, key)) { - merged[key] = config[key]; - } - } - merged.baseUrl = String(merged.baseUrl || window.location.origin).replace(/\/$/, ""); - if (merged.apiBaseUrl) { - merged.apiBaseUrl = String(merged.apiBaseUrl).replace(/\/$/, ""); - } else { - delete merged.apiBaseUrl; - } - merged.channelId = String(merged.channelId || ""); - merged.externalSource = String(merged.externalSource || "web_chat"); - return merged; - } - - function resolveWidgetBaseUrl(config) { - var currentScript = document.currentScript; - if (currentScript && currentScript.src) { - return currentScript.src.replace(/\/sdk\/cs-ai-agent-sdk\.min\.js(?:\?.*)?$/, ""); - } - return String(config.widgetBaseUrl || config.baseUrl || window.location.origin).replace(/\/$/, ""); - } - - function createFrameUrl(config) { - var widgetBaseUrl = resolveWidgetBaseUrl(config); - var frameUrl = new URL(widgetBaseUrl + "/kefu/chat/"); - frameUrl.searchParams.set("channelId", config.channelId); - frameUrl.searchParams.set("baseUrl", config.baseUrl); - if (config.apiBaseUrl) frameUrl.searchParams.set("apiBaseUrl", config.apiBaseUrl); - if (config.externalSource) frameUrl.searchParams.set("externalSource", config.externalSource); - if (config.subject) frameUrl.searchParams.set("subject", config.subject); - return frameUrl; - } - - function mergeWidgetConfig(config, remoteConfig) { - if (!remoteConfig) { - return config; - } - var merged = {}; - var key; - for (key in config) { - if (Object.prototype.hasOwnProperty.call(config, key)) { - merged[key] = config[key]; - } - } - var remoteKeys = ["title", "subtitle", "themeColor", "position", "width"]; - for (var i = 0; i < remoteKeys.length; i += 1) { - key = remoteKeys[i]; - if ( - Object.prototype.hasOwnProperty.call(remoteConfig, key) && - remoteConfig[key] !== undefined && - remoteConfig[key] !== null - ) { - merged[key] = remoteConfig[key]; - } - } - return merged; - } - - function fetchWidgetConfig(config) { - var baseUrl = String(config.apiBaseUrl || config.baseUrl || "").replace(/\/$/, ""); - if (!baseUrl || !config.channelId || typeof fetch !== "function") { - return Promise.resolve(config); - } - var url = baseUrl + "/api/open/im/widget/config?channelId=" + encodeURIComponent(config.channelId); - return fetch(url, { - method: "GET", - cache: "no-store", - headers: { - "X-Channel-Id": config.channelId, - }, - }) - .then(function (response) { - return response.json(); - }) - .then(function (payload) { - if (!payload || payload.success === false) { - return config; - } - return mergeWidgetConfig(config, payload.data || {}); - }) - .catch(function () { - return config; - }); - } - - function clearFrameTimers() { - if (state.frameHideTimer) { - window.clearTimeout(state.frameHideTimer); - state.frameHideTimer = null; - } - if (state.frameDestroyTimer) { - window.clearTimeout(state.frameDestroyTimer); - state.frameDestroyTimer = null; - } - } - - function applyFrameLayout() { - var frame = state.frame; - var config = state.config; - if (!frame || !config) { - return; - } - - frame.style.position = "fixed"; - frame.style.border = "0"; - frame.style.overflow = "hidden"; - frame.style.background = "#fff"; - frame.style.zIndex = "2147483000"; - frame.style.boxShadow = "0 28px 80px rgba(15, 35, 65, 0.28)"; - frame.style.willChange = "top,right,bottom,left,width,height,opacity,transform,border-radius"; - frame.style.transition = - "top 260ms cubic-bezier(0.22, 1, 0.36, 1), right 260ms cubic-bezier(0.22, 1, 0.36, 1), bottom 260ms cubic-bezier(0.22, 1, 0.36, 1), left 260ms cubic-bezier(0.22, 1, 0.36, 1), width 260ms cubic-bezier(0.22, 1, 0.36, 1), height 260ms cubic-bezier(0.22, 1, 0.36, 1), opacity 220ms ease, transform 260ms cubic-bezier(0.22, 1, 0.36, 1), border-radius 260ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 260ms ease"; - frame.style.transformOrigin = - config.position === "left" ? "left bottom" : "right bottom"; - - if (state.isMaximized) { - frame.style.top = "20px"; - frame.style.right = "20px"; - frame.style.bottom = "20px"; - frame.style.left = "20px"; - frame.style.width = "calc(100vw - 40px)"; - frame.style.maxWidth = "none"; - frame.style.height = "calc(100vh - 40px)"; - frame.style.borderRadius = "24px"; - return; - } - - frame.style.top = ""; - frame.style.bottom = "88px"; - frame.style.right = config.position === "left" ? "" : "24px"; - frame.style.left = config.position === "left" ? "24px" : ""; - frame.style.width = config.width || "380px"; - frame.style.maxWidth = "calc(100vw - 24px)"; - frame.style.height = "min(760px, calc(100vh - 112px))"; - frame.style.borderRadius = "28px"; - } - - function postToFrame(message) { - if (!state.frame || !state.frame.contentWindow || !state.frameUrl) { - return; - } - try { - state.frame.contentWindow.postMessage(message, state.frameUrl.origin); - } catch (error) { - console.error("[cs-agent-widget] postMessage failed", error); - } - } - - function flushFrameState() { - if (!state.frame || !state.frameLoaded || !state.frameReady) { - return; - } - - if (!state.initSent) { - state.initSent = true; - postToFrame({ - type: "cs-agent:init", - payload: state.config, - }); - } - - postToFrame({ type: state.isOpen ? "cs-agent:open" : "cs-agent:minimize" }); - postToFrame({ - type: "cs-agent:maximized", - payload: { isMaximized: state.isMaximized }, - }); - } - - function syncFrameVisibility() { - var frame = state.frame; - if (!frame) { - return; - } - clearFrameTimers(); - applyFrameLayout(); - frame.style.display = "block"; - - if (state.isOpen) { - frame.style.visibility = "visible"; - frame.style.pointerEvents = "auto"; - state.frameHideTimer = window.setTimeout(function () { - if (!state.frame) { - return; - } - state.frame.style.opacity = "1"; - state.frame.style.transform = "translate3d(0, 0, 0) scale(1)"; - }, 16); - flushFrameState(); - return; - } - - frame.style.pointerEvents = "none"; - frame.style.opacity = "0"; - frame.style.transform = state.isMaximized - ? "translate3d(0, 10px, 0) scale(0.985)" - : "translate3d(0, 16px, 0) scale(0.96)"; - state.frameHideTimer = window.setTimeout(function () { - if (!state.frame || state.isOpen) { - return; - } - state.frame.style.visibility = "hidden"; - }, state.animationDuration); - flushFrameState(); - } - - function destroyFrame() { - if (!state.frame) { - return; - } - clearFrameTimers(); - state.frame.style.pointerEvents = "none"; - state.frame.style.opacity = "0"; - state.frame.style.transform = "translate3d(0, 18px, 0) scale(0.94)"; - state.frame.style.visibility = "hidden"; - state.frameDestroyTimer = window.setTimeout(function () { - if (!state.frame) { - return; - } - if (state.frame.parentNode) { - state.frame.parentNode.removeChild(state.frame); - } - state.frame = null; - state.frameLoaded = false; - state.frameReady = false; - state.initSent = false; - state.isOpen = false; - state.isMaximized = false; - clearFrameTimers(); - }, state.animationDuration); - } - - function createFrame() { - if (state.frame) { - return state.frame; - } - if (!state.frameUrl || !state.config) { - return null; - } - - state.frame = document.createElement("iframe"); - state.frame.dataset.csAgentWidget = "frame"; - state.frame.title = state.config.title || "在线客服"; - state.frame.src = state.frameUrl.toString(); - applyFrameLayout(); - state.frame.style.display = "block"; - state.frame.style.visibility = "hidden"; - state.frame.style.pointerEvents = "none"; - state.frame.style.opacity = "0"; - state.frame.style.transform = "translate3d(0, 18px, 0) scale(0.96)"; - state.frame.addEventListener("load", function () { - state.frameLoaded = true; - syncFrameVisibility(); - }); - - document.body.appendChild(state.frame); - return state.frame; - } - - function handleWindowMessage(event) { - if (!state.frame || event.source !== state.frame.contentWindow) { - return; - } - - var data = event.data || {}; - if (data.type === "cs-agent:ready") { - state.frameReady = true; - flushFrameState(); - return; - } - - if (data.type === "cs-agent:request-minimize") { - state.isOpen = false; - syncFrameVisibility(); - return; - } - - if (data.type === "cs-agent:request-close") { - destroyFrame(); - return; - } - - if (data.type === "cs-agent:request-toggle-maximize") { - state.isMaximized = !state.isMaximized; - syncFrameVisibility(); - } - } - - function createLauncher() { - if (state.button) { - return state.button; - } - - var config = state.config; - var button = document.createElement("button"); - button.type = "button"; - button.dataset.csAgentWidget = "launcher"; - button.setAttribute("aria-label", config.title || "在线客服"); - button.textContent = config.title || "在线客服"; - button.style.position = "fixed"; - button.style.bottom = "24px"; - button.style.right = config.position === "left" ? "" : "24px"; - button.style.left = config.position === "left" ? "24px" : ""; - button.style.zIndex = "2147483000"; - button.style.border = "0"; - button.style.borderRadius = "999px"; - button.style.padding = "14px 18px"; - button.style.background = config.themeColor || "#0f6cbd"; - button.style.color = "#fff"; - button.style.font = "600 14px/1 sans-serif"; - button.style.boxShadow = "0 18px 40px rgba(15, 35, 65, 0.24)"; - button.style.cursor = "pointer"; - - button.addEventListener("click", function () { - if (!state.frame) { - createFrame(); - } - state.isOpen = !state.isOpen; - syncFrameVisibility(); - }); - - document.body.appendChild(button); - state.button = button; - return button; - } - - function mount(config) { - var rawConfig = config || window.CSAgentConfig || {}; - state.config = normalizeConfig(rawConfig); - var widgetBaseUrl = resolveWidgetBaseUrl(state.config); - if (!rawConfig.baseUrl) { - state.config.baseUrl = widgetBaseUrl; - } - if (!state.config.channelId) { - console.error("[cs-agent-widget] channelId is required"); - return; - } - - state.configLoading = true; - fetchWidgetConfig(state.config).then(function (nextConfig) { - state.configLoading = false; - state.config = normalizeConfig(nextConfig); - state.frameUrl = createFrameUrl(state.config); - if (state.button && state.button.parentNode) { - state.button.parentNode.removeChild(state.button); - state.button = null; - } - createLauncher(); - }); - } - - function destroy() { - clearFrameTimers(); - if (state.frame && state.frame.parentNode) { - state.frame.parentNode.removeChild(state.frame); - } - if (state.button && state.button.parentNode) { - state.button.parentNode.removeChild(state.button); - } - state.button = null; - state.frame = null; - state.frameLoaded = false; - state.frameReady = false; - state.initSent = false; - state.isOpen = false; - state.isMaximized = false; - state.configLoading = false; - } - - window.CSAgentWidget = { - mount: mount, - destroy: destroy, - open: function () { - if (!state.frame) { - createFrame(); - } - if (!state.frame) { - return; - } - state.isOpen = true; - syncFrameVisibility(); - }, - close: function () { - state.isOpen = false; - syncFrameVisibility(); - }, - }; - - if (!state.listenerBound) { - window.addEventListener("message", handleWindowMessage); - state.listenerBound = true; - } - - if (window.CSAgentConfig) { - mount(window.CSAgentConfig); - } -})(); +!function(){var e={position:"right",themeColor:"#0f6cbd",width:"380px",externalSource:"web_chat"},t=window.__CS_AGENT_WIDGET_STATE__;function n(t){var n,i={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(i[n]=e[n]);for(n in t=t||{})Object.prototype.hasOwnProperty.call(t,n)&&(i[n]=t[n]);return i.baseUrl=String(i.baseUrl||window.location.origin).replace(/\/$/,""),i.apiBaseUrl?i.apiBaseUrl=String(i.apiBaseUrl).replace(/\/$/,""):delete i.apiBaseUrl,i.channelId=String(i.channelId||""),i.externalSource=String(i.externalSource||"web_chat"),i}function i(e){var t=document.currentScript;return t&&t.src?t.src.replace(/\/sdk\/cs-ai-agent-sdk\.min\.js(?:\?.*)?$/,""):String(e.widgetBaseUrl||e.baseUrl||window.location.origin).replace(/\/$/,"")}function r(){t.frameHideTimer&&(window.clearTimeout(t.frameHideTimer),t.frameHideTimer=null),t.frameDestroyTimer&&(window.clearTimeout(t.frameDestroyTimer),t.frameDestroyTimer=null)}function a(){var e=t.frame,n=t.config;if(e&&n){if(e.style.position="fixed",e.style.border="0",e.style.overflow="hidden",e.style.background="#fff",e.style.zIndex="2147483000",e.style.boxShadow="0 28px 80px rgba(15, 35, 65, 0.28)",e.style.willChange="top,right,bottom,left,width,height,opacity,transform,border-radius",e.style.transition="top 260ms cubic-bezier(0.22, 1, 0.36, 1), right 260ms cubic-bezier(0.22, 1, 0.36, 1), bottom 260ms cubic-bezier(0.22, 1, 0.36, 1), left 260ms cubic-bezier(0.22, 1, 0.36, 1), width 260ms cubic-bezier(0.22, 1, 0.36, 1), height 260ms cubic-bezier(0.22, 1, 0.36, 1), opacity 220ms ease, transform 260ms cubic-bezier(0.22, 1, 0.36, 1), border-radius 260ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 260ms ease",e.style.transformOrigin="left"===n.position?"left bottom":"right bottom",t.isMaximized)return e.style.top="20px",e.style.right="20px",e.style.bottom="20px",e.style.left="20px",e.style.width="calc(100vw - 40px)",e.style.maxWidth="none",e.style.height="calc(100vh - 40px)",void(e.style.borderRadius="24px");e.style.top="",e.style.bottom="88px",e.style.right="left"===n.position?"":"24px",e.style.left="left"===n.position?"24px":"",e.style.width=n.width||"380px",e.style.maxWidth="calc(100vw - 24px)",e.style.height="min(760px, calc(100vh - 112px))",e.style.borderRadius="28px"}}function o(e){if(t.frame&&t.frame.contentWindow&&t.frameUrl)try{t.frame.contentWindow.postMessage(e,t.frameUrl.origin)}catch(e){console.error("[cs-agent-widget] postMessage failed",e)}}function s(){t.frame&&t.frameLoaded&&t.frameReady&&(t.initSent||(t.initSent=!0,o({type:"cs-agent:init",payload:t.config})),o({type:t.isOpen?"cs-agent:open":"cs-agent:minimize"}),o({type:"cs-agent:maximized",payload:{isMaximized:t.isMaximized}}))}function l(){var e=t.frame;if(e){if(r(),a(),e.style.display="block",t.isOpen)return e.style.visibility="visible",e.style.pointerEvents="auto",t.frameHideTimer=window.setTimeout(function(){t.frame&&(t.frame.style.opacity="1",t.frame.style.transform="translate3d(0, 0, 0) scale(1)")},16),void s();e.style.pointerEvents="none",e.style.opacity="0",e.style.transform=t.isMaximized?"translate3d(0, 10px, 0) scale(0.985)":"translate3d(0, 16px, 0) scale(0.96)",t.frameHideTimer=window.setTimeout(function(){t.frame&&!t.isOpen&&(t.frame.style.visibility="hidden")},t.animationDuration),s()}}function c(){return t.frame?t.frame:t.frameUrl&&t.config?(t.frame=document.createElement("iframe"),t.frame.dataset.csAgentWidget="frame",t.frame.title=t.config.title||"\u5728\u7ebf\u5ba2\u670d",t.frame.src=t.frameUrl.toString(),a(),t.frame.style.display="block",t.frame.style.visibility="hidden",t.frame.style.pointerEvents="none",t.frame.style.opacity="0",t.frame.style.transform="translate3d(0, 18px, 0) scale(0.96)",t.frame.addEventListener("load",function(){t.frameLoaded=!0,l()}),document.body.appendChild(t.frame),t.frame):null}function d(e){var r=e||window.CSAgentConfig||{};t.config=n(r);var a=i(t.config);r.baseUrl||(t.config.baseUrl=a),t.config.channelId?(t.configLoading=!0,function(e){var t=String(e.apiBaseUrl||e.baseUrl||"").replace(/\/$/,"");if(!t||!e.channelId||"function"!=typeof fetch)return Promise.resolve(e);var n=t+"/api/open/im/widget/config?channelId="+encodeURIComponent(e.channelId);return fetch(n,{method:"GET",cache:"no-store",headers:{"X-Channel-Id":e.channelId}}).then(function(e){return e.json()}).then(function(t){return t&&!1!==t.success?function(e,t){if(!t)return e;var n,i={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(i[n]=e[n]);for(var r=["title","subtitle","themeColor","position","width"],a=0;a ${targetSize} bytes (${reduction}% smaller)`);