@wzyjs/utils 0.3.9 → 0.3.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/web.esm.js CHANGED
@@ -10074,16 +10074,26 @@ var require_isoWeek = __commonJS((exports, module) => {
10074
10074
 
10075
10075
  // ../../node_modules/dayjs/plugin/updateLocale.js
10076
10076
  var require_updateLocale = __commonJS((exports, module) => {
10077
- (function(e, n) {
10078
- typeof exports == "object" && typeof module != "undefined" ? module.exports = n() : typeof define == "function" && define.amd ? define(n) : (e = typeof globalThis != "undefined" ? globalThis : e || self).dayjs_plugin_updateLocale = n();
10077
+ (function(e, t) {
10078
+ typeof exports == "object" && typeof module != "undefined" ? module.exports = t() : typeof define == "function" && define.amd ? define(t) : (e = typeof globalThis != "undefined" ? globalThis : e || self).dayjs_plugin_updateLocale = t();
10079
10079
  })(exports, function() {
10080
- return function(e, n, t) {
10081
- t.updateLocale = function(e2, n2) {
10082
- var o = t.Ls[e2];
10083
- if (o)
10084
- return (n2 ? Object.keys(n2) : []).forEach(function(e3) {
10085
- o[e3] = n2[e3];
10086
- }), o;
10080
+ function e() {
10081
+ return e = Object.assign || function(e2) {
10082
+ for (var t = 1;t < arguments.length; t++) {
10083
+ var n = arguments[t];
10084
+ for (var o in n)
10085
+ Object.prototype.hasOwnProperty.call(n, o) && (e2[o] = n[o]);
10086
+ }
10087
+ return e2;
10088
+ }, e.apply(this, arguments);
10089
+ }
10090
+ return function(t, n, o) {
10091
+ o.updateLocale = function(t2, n2) {
10092
+ var r = o.Ls[t2];
10093
+ if (r)
10094
+ return (n2 ? Object.keys(n2) : []).forEach(function(t3) {
10095
+ r[t3] && n2[t3] && typeof r[t3] == "object" && typeof n2[t3] == "object" && !Array.isArray(r[t3]) ? r[t3] = e({}, r[t3], n2[t3]) : r[t3] = n2[t3];
10096
+ }), r;
10087
10097
  };
10088
10098
  };
10089
10099
  });
@@ -18085,8 +18095,9 @@ var import_copy_to_clipboard = __toESM(require_copy_to_clipboard(), 1);
18085
18095
  // src/common/ai/index.ts
18086
18096
  var exports_ai = {};
18087
18097
  __export(exports_ai, {
18088
- video: () => exports_video,
18089
18098
  text: () => exports_text,
18099
+ keling: () => exports_keling,
18100
+ doubao: () => exports_doubao,
18090
18101
  ai302: () => exports_302
18091
18102
  });
18092
18103
 
@@ -18121,27 +18132,237 @@ var chat = async (message, model = "gpt-4.1", isParse = true) => {
18121
18132
  console.log(666, typeof data, data);
18122
18133
  const output = typeof data === "string" ? import_json5.default.parse(data)?.output : data?.output;
18123
18134
  if (isParse) {
18124
- const match = /```(json)?(.*)```/s.exec(output);
18125
- if (!match) {
18126
- return import_json5.default.parse(output);
18127
- } else {
18128
- return import_json5.default.parse(match[2]);
18129
- }
18135
+ const cleanOutput = output.replace(/<think>[\s\S]*?<\/think>/g, "");
18136
+ let jsonString = cleanOutput;
18137
+ const startBlockJson = jsonString.indexOf("```json");
18138
+ const startBlock = startBlockJson !== -1 ? startBlockJson : jsonString.indexOf("```");
18139
+ if (startBlock !== -1) {
18140
+ const endBlock = jsonString.lastIndexOf("```");
18141
+ if (endBlock > startBlock) {
18142
+ jsonString = jsonString.substring(startBlock + (startBlockJson !== -1 ? 7 : 3), endBlock);
18143
+ }
18144
+ }
18145
+ const firstBrace = jsonString.indexOf("{");
18146
+ const lastBrace = jsonString.lastIndexOf("}");
18147
+ const firstBracket = jsonString.indexOf("[");
18148
+ const lastBracket = jsonString.lastIndexOf("]");
18149
+ const isObject2 = firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket);
18150
+ const isArray2 = firstBracket !== -1 && (firstBrace === -1 || firstBracket < firstBrace);
18151
+ if (isObject2 && lastBrace >= firstBrace) {
18152
+ jsonString = jsonString.substring(firstBrace, lastBrace + 1);
18153
+ } else if (isArray2 && lastBracket >= firstBracket) {
18154
+ jsonString = jsonString.substring(firstBracket, lastBracket + 1);
18155
+ }
18156
+ return import_json5.default.parse(jsonString);
18130
18157
  }
18131
18158
  return output;
18132
18159
  } catch (error) {
18133
18160
  throw new Error(error?.response?.statusText || error?.message || "未知原因");
18134
18161
  }
18135
18162
  };
18136
- // src/common/ai/video/index.ts
18137
- var exports_video = {};
18138
- __export(exports_video, {
18139
- keling: () => exports_keling
18163
+ // src/common/ai/doubao/index.ts
18164
+ var exports_doubao = {};
18165
+ __export(exports_doubao, {
18166
+ vc: () => vc,
18167
+ tts: () => tts
18140
18168
  });
18141
18169
 
18142
- // src/common/ai/video/keling/index.ts
18170
+ // src/common/ai/doubao/tts.ts
18171
+ var createReqid = () => {
18172
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
18173
+ return crypto.randomUUID();
18174
+ }
18175
+ return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
18176
+ };
18177
+ var decodeBase64ToArrayBuffer = (value) => {
18178
+ if (typeof window !== "undefined" && typeof window.atob === "function") {
18179
+ const binary = window.atob(value);
18180
+ const bytes = new Uint8Array(binary.length);
18181
+ for (let i = 0;i < binary.length; i++) {
18182
+ bytes[i] = binary.charCodeAt(i);
18183
+ }
18184
+ return bytes.buffer;
18185
+ }
18186
+ const buf = Buffer.from(value, "base64");
18187
+ return buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength);
18188
+ };
18189
+ var tts = async (params) => {
18190
+ const { app = {}, user, audio, request } = params;
18191
+ const url = "https://openspeech.bytedance.com/api/v3/tts/unidirectional";
18192
+ const reqid = request.reqid || createReqid();
18193
+ const speechRate = audio.speed_ratio ? Math.round((audio.speed_ratio - 1) * 100) : 0;
18194
+ const loudnessRate = audio.volume_ratio ? Math.round((audio.volume_ratio - 1) * 100) : 0;
18195
+ const headers = {
18196
+ "X-Api-App-Id": app.appid || process.env.DOUBAO_TTS_APPID || "",
18197
+ "X-Api-Access-Key": app.accessToken || process.env.DOUBAO_TTS_ACCESS_TOKEN || "",
18198
+ "X-Api-Resource-Id": app.resourceId || (audio.voice_type.startsWith("S_") ? "seed-icl-2.0" : "seed-tts-2.0"),
18199
+ "Content-Type": "application/json",
18200
+ "X-Api-Request-Id": reqid
18201
+ };
18202
+ const payload = {
18203
+ user: {
18204
+ uid: user?.uid || "uid"
18205
+ },
18206
+ req_params: {
18207
+ text: request.text,
18208
+ speaker: audio.voice_type,
18209
+ audio_params: {
18210
+ format: audio.encoding || "pcm",
18211
+ sample_rate: 24000,
18212
+ speech_rate: speechRate,
18213
+ loudness_rate: loudnessRate,
18214
+ emotion: audio.emotion,
18215
+ emotion_scale: audio.emotion_scale
18216
+ }
18217
+ }
18218
+ };
18219
+ if (audio.context_texts) {
18220
+ payload.req_params.additions = JSON.stringify({
18221
+ context_texts: audio.context_texts
18222
+ });
18223
+ }
18224
+ try {
18225
+ const response = await axios_default.post(url, payload, {
18226
+ headers,
18227
+ responseType: "arraybuffer"
18228
+ });
18229
+ const responseText = new TextDecoder().decode(response.data);
18230
+ const jsonObjects = [];
18231
+ let depth = 0;
18232
+ let start = 0;
18233
+ for (let i = 0;i < responseText.length; i++) {
18234
+ if (responseText[i] === "{") {
18235
+ if (depth === 0)
18236
+ start = i;
18237
+ depth++;
18238
+ } else if (responseText[i] === "}") {
18239
+ depth--;
18240
+ if (depth === 0) {
18241
+ try {
18242
+ const jsonStr = responseText.substring(start, i + 1);
18243
+ const jsonObj = JSON.parse(jsonStr);
18244
+ jsonObjects.push(jsonObj);
18245
+ } catch (e) {
18246
+ console.warn("Failed to parse JSON chunk", e);
18247
+ }
18248
+ }
18249
+ }
18250
+ }
18251
+ if (jsonObjects.length === 0) {
18252
+ try {
18253
+ jsonObjects.push(JSON.parse(responseText));
18254
+ } catch (e) {
18255
+ console.error("Doubao TTS Response Parse Error:", responseText);
18256
+ throw new Error("Invalid response format");
18257
+ }
18258
+ }
18259
+ const audioChunks = [];
18260
+ let totalLength = 0;
18261
+ for (const data of jsonObjects) {
18262
+ const code = data.code;
18263
+ const message = data.message;
18264
+ if (typeof code === "number" && code !== 0 && code !== 20000000) {
18265
+ console.error("Doubao TTS Error Response:", JSON.stringify(data, null, 2));
18266
+ const err = new Error(`豆包语音合成失败: ${message} (code: ${code})`);
18267
+ err.code = code;
18268
+ err.reqid = data.reqid;
18269
+ throw err;
18270
+ }
18271
+ if (data.data) {
18272
+ const chunkBuffer = decodeBase64ToArrayBuffer(data.data);
18273
+ audioChunks.push(chunkBuffer);
18274
+ totalLength += chunkBuffer.byteLength;
18275
+ }
18276
+ }
18277
+ if (totalLength === 0) {
18278
+ throw new Error("豆包语音合成失败:未返回音频数据");
18279
+ }
18280
+ const resultBuffer = new Uint8Array(totalLength);
18281
+ let offset = 0;
18282
+ for (const chunk of audioChunks) {
18283
+ resultBuffer.set(new Uint8Array(chunk), offset);
18284
+ offset += chunk.byteLength;
18285
+ }
18286
+ return resultBuffer.buffer;
18287
+ } catch (error) {
18288
+ if (error.response) {
18289
+ try {
18290
+ let errorData = error.response.data;
18291
+ if (errorData instanceof ArrayBuffer || typeof Buffer !== "undefined" && Buffer.isBuffer(errorData)) {
18292
+ errorData = new TextDecoder().decode(errorData);
18293
+ } else if (typeof errorData === "object") {
18294
+ errorData = JSON.stringify(errorData);
18295
+ }
18296
+ throw new Error(`Doubao TTS Error: ${error.response.status} - ${errorData}`);
18297
+ } catch (e) {
18298
+ if (e.message && e.message.startsWith("Doubao TTS Error")) {
18299
+ throw e;
18300
+ }
18301
+ throw new Error(`Doubao TTS Error: ${error.response.status} ${error.message}`);
18302
+ }
18303
+ }
18304
+ throw error;
18305
+ }
18306
+ };
18307
+ // src/common/ai/doubao/vc.ts
18308
+ var getHeaders = (accessToken, contentType) => {
18309
+ const headers = {
18310
+ Authorization: `Bearer; ${accessToken}`
18311
+ };
18312
+ if (contentType) {
18313
+ headers["content-type"] = contentType;
18314
+ }
18315
+ return headers;
18316
+ };
18317
+ var handleResponse = (data) => {
18318
+ if (data instanceof ArrayBuffer) {
18319
+ return data;
18320
+ }
18321
+ if (typeof Buffer !== "undefined" && Buffer.isBuffer(data)) {
18322
+ return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength);
18323
+ }
18324
+ return data;
18325
+ };
18326
+ var vc = {
18327
+ submit: async (params) => {
18328
+ const { app, audio, options = {} } = params;
18329
+ const url = new URL("https://openspeech.bytedance.com/api/v1/vc/submit");
18330
+ url.searchParams.set("appid", app.appid);
18331
+ Object.entries(options).forEach(([key2, value]) => {
18332
+ if (value !== undefined && value !== null) {
18333
+ url.searchParams.set(key2, String(value));
18334
+ }
18335
+ });
18336
+ const response = await axios_default.post(url.toString(), audio.data, {
18337
+ headers: getHeaders(app.accessToken, audio.contentType || "audio/wav"),
18338
+ responseType: "arraybuffer"
18339
+ });
18340
+ return handleResponse(response.data);
18341
+ },
18342
+ query: async (params) => {
18343
+ const { app, id, blocking } = params;
18344
+ const url = new URL("https://openspeech.bytedance.com/api/v1/vc/query");
18345
+ url.searchParams.set("appid", app.appid);
18346
+ url.searchParams.set("id", id);
18347
+ if (blocking !== undefined) {
18348
+ url.searchParams.set("blocking", String(blocking));
18349
+ }
18350
+ const response = await axios_default.get(url.toString(), {
18351
+ headers: getHeaders(app.accessToken),
18352
+ responseType: "arraybuffer"
18353
+ });
18354
+ return handleResponse(response.data);
18355
+ }
18356
+ };
18357
+ // src/common/ai/keling/index.ts
18143
18358
  var exports_keling = {};
18144
18359
  __export(exports_keling, {
18360
+ video: () => exports_video
18361
+ });
18362
+
18363
+ // src/common/ai/keling/video/index.ts
18364
+ var exports_video = {};
18365
+ __export(exports_video, {
18145
18366
  setCookie: () => setCookie,
18146
18367
  getVideoStatus: () => getVideoStatus,
18147
18368
  generateVideo: () => generateVideo
@@ -18559,7 +18780,7 @@ g.win32 = y.win32 = y;
18559
18780
  g.posix = g;
18560
18781
  var { resolve: B2, normalize: G2, isAbsolute: H2, join: K2, relative: Q2, toNamespacedPath: U, dirname: V, basename: X2, extname: Y2, format: Z2, parse: $, sep: I2, delimiter: O } = g;
18561
18782
 
18562
- // src/common/ai/video/keling/axios.ts
18783
+ // src/common/ai/keling/video/axios.ts
18563
18784
  var axios3 = axios_default.create({
18564
18785
  headers: {
18565
18786
  "Content-Type": "application/json",
@@ -18567,7 +18788,7 @@ var axios3 = axios_default.create({
18567
18788
  }
18568
18789
  });
18569
18790
 
18570
- // src/common/ai/video/keling/utils.ts
18791
+ // src/common/ai/keling/video/utils.ts
18571
18792
  var getToken = async (filename) => {
18572
18793
  try {
18573
18794
  const response = await axios3.get("https://klingai.kuaishou.com/api/upload/issue/token", {
@@ -18740,7 +18961,7 @@ var setCookie = (newCookieValue) => {
18740
18961
  axios3.defaults.headers.Cookie = newCookieValue;
18741
18962
  };
18742
18963
 
18743
- // src/common/ai/video/keling/index.ts
18964
+ // src/common/ai/keling/video/index.ts
18744
18965
  var getVideoStatus = async (taskId) => {
18745
18966
  if (!taskId) {
18746
18967
  return;
@@ -18797,11 +19018,28 @@ var complete = async (apiUrl, model, token, messages, json = true) => {
18797
19018
  throw new Error("No content in response");
18798
19019
  }
18799
19020
  if (json) {
18800
- const match = /```(json)?(.*)```/s.exec(content);
18801
- if (match && match[2]) {
18802
- return import_json52.default.parse(match[2]);
18803
- }
18804
- return import_json52.default.parse(content);
19021
+ const cleanContent = content.replace(/<think>[\s\S]*?<\/think>/g, "");
19022
+ let jsonString = cleanContent;
19023
+ const startBlockJson = jsonString.indexOf("```json");
19024
+ const startBlock = startBlockJson !== -1 ? startBlockJson : jsonString.indexOf("```");
19025
+ if (startBlock !== -1) {
19026
+ const endBlock = jsonString.lastIndexOf("```");
19027
+ if (endBlock > startBlock) {
19028
+ jsonString = jsonString.substring(startBlock + (startBlockJson !== -1 ? 7 : 3), endBlock);
19029
+ }
19030
+ }
19031
+ const firstBrace = jsonString.indexOf("{");
19032
+ const lastBrace = jsonString.lastIndexOf("}");
19033
+ const firstBracket = jsonString.indexOf("[");
19034
+ const lastBracket = jsonString.lastIndexOf("]");
19035
+ const isObject2 = firstBrace !== -1 && (firstBracket === -1 || firstBrace < firstBracket);
19036
+ const isArray2 = firstBracket !== -1 && (firstBrace === -1 || firstBracket < firstBrace);
19037
+ if (isObject2 && lastBrace >= firstBrace) {
19038
+ jsonString = jsonString.substring(firstBrace, lastBrace + 1);
19039
+ } else if (isArray2 && lastBracket >= firstBracket) {
19040
+ jsonString = jsonString.substring(firstBracket, lastBracket + 1);
19041
+ }
19042
+ return import_json52.default.parse(jsonString);
18805
19043
  }
18806
19044
  return content;
18807
19045
  } catch (error) {
@@ -21560,7 +21798,6 @@ var import_isSameOrAfter = __toESM(require_isSameOrAfter(), 1);
21560
21798
  var import_isSameOrBefore = __toESM(require_isSameOrBefore(), 1);
21561
21799
  var import_utc = __toESM(require_utc(), 1);
21562
21800
  var import_zh_cn = __toESM(require_zh_cn(), 1);
21563
- var import_dayjs2 = __toESM(require_dayjs_min(), 1);
21564
21801
  import_dayjs.default.extend(import_utc.default);
21565
21802
  import_dayjs.default.extend(import_isBetween.default);
21566
21803
  import_dayjs.default.extend(import_weekday.default);
@@ -21714,7 +21951,6 @@ var export_copy = import_copy_to_clipboard.default;
21714
21951
  var export_consola = import_consola.default;
21715
21952
  var export_classnames = import_classnames.default;
21716
21953
  var export__ = import_lodash2.default;
21717
- var export_Dayjs = import_dayjs2.Dayjs;
21718
21954
 
21719
21955
  export {
21720
21956
  exports_external as z,
@@ -21888,7 +22124,6 @@ export {
21888
22124
  INVALID,
21889
22125
  Enum,
21890
22126
  EMPTY_PATH,
21891
- export_Dayjs as Dayjs,
21892
22127
  DIRTY,
21893
22128
  BRAND
21894
22129
  };
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@wzyjs/utils",
3
- "version": "0.3.9",
3
+ "version": "0.3.16",
4
4
  "description": "description",
5
5
  "author": "wzy",
6
6
  "sideEffects": false,
7
7
  "scripts": {
8
8
  "build-web-esm": "bun build ./src/web.ts --outfile dist/web.esm.js --format esm",
9
9
  "build-web-cjs": "bun build ./src/web.ts --outfile dist/web.cjs.js --format cjs",
10
- "build-node-esm": "bun build ./src/node.ts --outfile dist/node.esm.js --format esm --target node --external nodemailer --external fs-extra --external axios --external node:* --external ali-oss --external nedb --external cheerio --external json5 --external consola --external dayjs --external node-cron",
11
- "build-node-cjs": "bun build ./src/node.ts --outfile dist/node.cjs.js --format cjs --target node --external nodemailer --external fs-extra --external axios --external node:* --external ali-oss --external nedb --external cheerio --external json5 --external consola --external dayjs --external node-cron",
10
+ "build-node-esm": "bun build ./src/node.ts --outfile dist/node.esm.js --format esm --target node --external nodemailer --external fs-extra --external axios --external node:* --external ali-oss --external nedb --external cheerio --external json5 --external consola --external dayjs --external node-cron --external @aws-sdk/client-s3",
11
+ "build-node-cjs": "bun build ./src/node.ts --outfile dist/node.cjs.js --format cjs --target node --external nodemailer --external fs-extra --external axios --external node:* --external ali-oss --external nedb --external cheerio --external json5 --external consola --external dayjs --external node-cron --external @aws-sdk/client-s3",
12
12
  "build": "rm -rf dist && npm run build-web-esm && npm run build-web-cjs && npm run build-node-esm && npm run build-node-cjs && tsc"
13
13
  },
14
14
  "files": [
@@ -37,6 +37,7 @@
37
37
  }
38
38
  },
39
39
  "dependencies": {
40
+ "@aws-sdk/client-s3": "^3.1004.0",
40
41
  "ali-oss": "^6.21.0",
41
42
  "animejs": "^3.2.1",
42
43
  "axios": "^1.6.2",
@@ -46,7 +47,7 @@
46
47
  "copy-to-clipboard": "^3.3.3",
47
48
  "dayjs": "^1.11.12",
48
49
  "decimal.js": "^10.5.0",
49
- "fs-extra": "^10.1.0",
50
+ "fs-extra": "^11.3.4",
50
51
  "js-base64": "^3.7.7",
51
52
  "json5": "^2.2.3",
52
53
  "localforage": "^1.10.0",
@@ -62,12 +63,15 @@
62
63
  "devDependencies": {
63
64
  "@types/ali-oss": "^6.16.11",
64
65
  "@types/animejs": "^3.1.6",
65
- "@types/fs-extra": "^9.0.13",
66
+ "@types/fs-extra": "^11.0.4",
66
67
  "@types/lodash": "^4.14.189",
67
68
  "@types/md5": "^2.3.2",
68
69
  "@types/nedb": "^1.8.16",
69
70
  "@types/nodemailer": "^6.4.7",
70
71
  "@types/papaparse": "^5.3.15"
71
72
  },
72
- "gitHead": "e65ba2fd0a4471a28a92f69e1786606433f7b3c7"
73
+ "gitHead": "58a36c9fd59e43928b80701cdd7896754ea0db97",
74
+ "publishConfig": {
75
+ "access": "public"
76
+ }
73
77
  }
@@ -1 +0,0 @@
1
- export * as keling from './keling';
@@ -1,2 +0,0 @@
1
- export declare const replaceContentInFile: (filePath: string, targetContent: string, replacement: string) => Promise<void>;
2
- export declare const downloadFile: (httpUrl: string, outputPath?: string) => Promise<string>;
@@ -1,9 +0,0 @@
1
- type FileInput = string | Buffer;
2
- /**
3
- * 上传文件到 OSS
4
- * @param file - 文件,可以是 HTTP URL、本地文件路径或 Buffer
5
- * @param filename - 文件名,当 file 是 Buffer 时必须提供
6
- * @returns 上传后的文件 URL
7
- */
8
- export declare function uploadFile(file: FileInput, filename?: string): Promise<string>;
9
- export {};
File without changes