@nickyzj2023/utils 1.0.75 → 1.0.76

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/index.d.mts CHANGED
@@ -1,9 +1,20 @@
1
- //#region src/ai/chatCompletions/types.d.ts
2
- declare namespace ChatCompletions {
1
+ //#region src/ai/types.d.ts
2
+ declare namespace AI {
3
3
  type Model = {
4
- /** 模型名称(如果不传,会尝试从 /models 读取模型) */model?: string; /** API 基础地址 */
5
- baseUrl: string; /** API 密钥(本地模型可不传) */
6
- apiKey?: string;
4
+ baseUrl: string; /** 不传则自动使用{baseUrl}/models接口的第一个模型 */
5
+ model?: string;
6
+ apiKey?: string; /** POST /chat/completions时注入自定义请求体 */
7
+ customBody?: Record<string, any>; /** 模型支持的消息输入类型,如果填了,则会在调用chatCompletions前校验上下文,含有不支持的输入时抑制请求 */
8
+ inputs?: ["text" | "image" | "video" | "audio" | "file"]; /** 模型的最大上下文(输入+输出),如果填了,则会在调用chatCompletions后选择性调用compact(当前上下文>最大上下文*80%时) */
9
+ context?: number;
10
+ };
11
+ type Message = {
12
+ role: "system" | "user" | "assistant" | "tool" | "function"; /** OpenRouter的思考内容字段,其他供应商的会尽可能合并到该字段内 */
13
+ reasoning?: string | null;
14
+ content: string | ContentPart[];
15
+ tool_calls?: ToolCall[];
16
+ tool_call_id?: string;
17
+ [key: string]: unknown;
7
18
  };
8
19
  type TextContent = {
9
20
  type: "text";
@@ -18,7 +29,7 @@ declare namespace ChatCompletions {
18
29
  type AudioContent = {
19
30
  type: "input_audio";
20
31
  input_audio: {
21
- /** 使用公网可访问的音频文件 URL */url?: string; /** 使用 base64 */
32
+ /** 使用公网可访问的音频链接 */url?: string; /** 使用base64 */
22
33
  data?: string;
23
34
  format: string;
24
35
  };
@@ -30,22 +41,22 @@ declare namespace ChatCompletions {
30
41
  };
31
42
  };
32
43
  type ContentPart = TextContent | ImageContent | AudioContent | VideoContent;
33
- type Message = {
34
- role: "system" | "user" | "assistant" | "tool" | "function"; /** 字节的思考字段 */
35
- reasoning_content?: string | null; /** OpenRouter的思考字段 */
36
- reasoning?: string | null;
37
- content: string | ContentPart[];
38
- name?: string;
39
- tool_calls?: ToolCall[];
40
- tool_call_id?: string;
41
- };
42
44
  type ToolDefinition = {
43
45
  type: "function";
44
46
  function: {
45
47
  name: string;
46
- description?: string;
47
- parameters?: Record<string, any>;
48
+ description: string;
49
+ parameters: {
50
+ type: "object";
51
+ properties: Record<string, {
52
+ type: string;
53
+ description?: string; /** 在此处设置的required,发出请求前会自动提到外面去 */
54
+ required?: boolean;
55
+ }>;
56
+ required?: string[];
57
+ };
48
58
  };
59
+ handler: (...args: any) => any;
49
60
  };
50
61
  type ToolCall = {
51
62
  id: string;
@@ -55,44 +66,36 @@ declare namespace ChatCompletions {
55
66
  arguments: string;
56
67
  };
57
68
  };
58
- type ExtraArgs = {
59
- messages?: ChatCompletions.Message[];
60
- [key: string]: any;
61
- };
62
- type ToolHandler = (args: any, extraArgs?: ExtraArgs) => any | Promise<any>;
63
- type ToolHandlers = Record<string, ChatCompletions.ToolHandler>;
64
- type Usage = {
65
- prompt_tokens: number;
66
- completion_tokens: number;
67
- total_tokens: number;
68
- };
69
- /** 请求非流式 /chat/completions 的响应结果 */
70
- type Response = {
69
+ }
70
+ //#endregion
71
+ //#region src/ai/chatCompletions/types.d.ts
72
+ declare namespace ChatCompletions {
73
+ /** 非流式POST /chat/completions的响应结果 */
74
+ type NonStreamResponse = {
71
75
  id: string;
72
76
  object: "chat.completion";
73
77
  created: number;
74
78
  model: string;
75
79
  choices: Array<{
76
80
  index: number;
77
- message: Message;
81
+ message: AI.Message;
78
82
  finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | null;
79
83
  }>;
80
84
  usage: Usage;
81
85
  system_fingerprint?: string;
82
86
  };
83
- type ExtraBody = {
84
- /** 工具列表 */tools?: ToolDefinition[]; /** 工具调用函数表,key 为工具名,value 为函数 */
85
- toolHandlers?: ToolHandlers; /** 是否使用流式传输,启用后函数返回异步迭代器 */
86
- stream?: boolean; /** 其他额外参数 */
87
- [key: string]: any;
88
- };
89
- /** 调用 chatCompletions 返回的结果,流式/非流式通用 */
90
- type Result = {
91
- /** 模型的最终回复内容(多模态时取所有 text 拼接) */content: string; /** Token 消耗情况 */
87
+ /** 调用chatCompletions返回的结果,流式/非流式通用 */
88
+ type NonStreamResult = {
89
+ /** 模型的最终回复内容(多模态时取所有text拼接) */content: string; /** Token 消耗情况 */
92
90
  usage: Usage; /** 原始响应中的其他字段 */
93
91
  [key: string]: any;
94
92
  };
95
- /** 流式响应中的单个 SSE 数据块(OpenAI 原始格式) */
93
+ type Usage = {
94
+ prompt_tokens: number;
95
+ completion_tokens: number;
96
+ total_tokens: number;
97
+ };
98
+ /** 流式响应中的单个SSE数据块(OpenAI原始格式) */
96
99
  type StreamResponse = {
97
100
  id: string;
98
101
  object: "chat.completion.chunk";
@@ -100,28 +103,18 @@ declare namespace ChatCompletions {
100
103
  model: string;
101
104
  choices: Array<{
102
105
  index: number;
103
- delta: {
104
- role?: Message["role"];
105
- content?: string | null; /** 字节的思考字段 */
106
- reasoning_content?: string | null; /** OpenRouter的思考字段 */
107
- reasoning?: string | null;
106
+ delta: Pick<AI.Message, "role" | "reasoning" | "content"> & {
108
107
  tool_calls?: Array<{
109
108
  index: number;
110
- id?: string;
111
- type?: "function";
112
- function?: {
113
- name?: string;
114
- arguments?: string;
115
- };
116
- }>;
109
+ } & Partial<AI.ToolCall>>;
117
110
  };
118
111
  finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | null;
119
112
  }>;
120
113
  usage?: Usage;
121
114
  };
122
- /** 流式调用 chatCompletions 时迭代器产出的数据块 */
115
+ /** 流式调用chatCompletions时迭代器产出的数据块 */
123
116
  type StreamChunk = {
124
- /** 模型流式返回的思考内容增量(仅在生成过程中出现) */reasoningContent?: string; /** 模型流式返回的内容增量(仅在生成过程中出现) */
117
+ /** 模型流式返回的思考内容增量(仅在生成过程中出现) */reasoning?: string; /** 模型流式返回的内容增量(仅在生成过程中出现) */
125
118
  content?: string; /** Token 消耗情况(仅在最后一帧出现) */
126
119
  usage?: Usage;
127
120
  };
@@ -129,22 +122,23 @@ declare namespace ChatCompletions {
129
122
  //#endregion
130
123
  //#region src/ai/chatCompletions/index.d.ts
131
124
  /**
132
- * 兼容 OpenAI API 的聊天补全函数
125
+ * 兼容OpenAI API的聊天补全函数
133
126
  * - 自动处理工具调用
134
- * - 同时支持普通响应和流式响应
127
+ * - 支持普通/流式响应
135
128
  *
136
- * @param model 模型配置,包含 model、baseUrl、apiKey
137
- * @param messages OpenAI API 兼容的消息数组
138
- * @param extraBody 可选的额外参数,如 tools、toolHandlers、temperature、stream
139
- * @returns 普通模式下返回 `{ content, usage, ... }`;`stream: true` 时返回异步迭代器
129
+ * @param model 模型配置,包含model、baseUrl、apiKey
130
+ * @param messages OpenAI API兼容的消息数组
131
+ * @param extraBody 可选的额外参数,如tools、temperature、stream等
132
+ * @returns 普通模式下返回`{ content, usage, ... }`;`stream: true`时返回异步迭代器
140
133
  *
141
134
  * @example
142
135
  * // 最简调用
143
- * // 未填写模型名,会自动使用/v1/models的第一个模型
144
- * const { content, usage } = await chatCompletions(
136
+ * // 未填写模型名,会自动使用/v1/models返回的第一个模型
137
+ * const { reasoning, content, usage } = await chatCompletions(
145
138
  * { baseUrl: "http://127.0.0.1:11434/v1" },
146
139
  * [{ role: "user", content: "你好" }],
147
140
  * );
141
+ * console.log(reasoning); // "The user said..."
148
142
  * console.log(content); // "你好!有什么我可以帮你的吗?"
149
143
  * console.log(usage); // { prompt_tokens: 13, completion_tokens: 9, total_tokens: 22 }
150
144
  *
@@ -160,11 +154,9 @@ declare namespace ChatCompletions {
160
154
  * name: "getWeather",
161
155
  * description: "查询城市天气情况",
162
156
  * parameters: { type: "object", properties: { city: { type: "string" } } },
157
+ * handler: (args) => `${args.city}今日晴转多云,25°C`,
163
158
  * },
164
159
  * }],
165
- * toolHandlers: {
166
- * getWeather: (args) => `${args.city}今日晴转多云,25°C`,
167
- * },
168
160
  * },
169
161
  * );
170
162
  *
@@ -183,14 +175,30 @@ declare namespace ChatCompletions {
183
175
  * }
184
176
  * }
185
177
  */
186
- declare function chatCompletions(model: ChatCompletions.Model, messages: ChatCompletions.Message[], extraBody: ChatCompletions.ExtraBody & {
178
+ declare function chatCompletions(model: AI.Model, messages: AI.Message[], options: {
187
179
  stream: true;
180
+ tools?: AI.ToolDefinition[];
188
181
  }): Promise<AsyncGenerator<ChatCompletions.StreamChunk>>;
189
- declare function chatCompletions(model: ChatCompletions.Model, messages: ChatCompletions.Message[], extraBody?: ChatCompletions.ExtraBody): Promise<ChatCompletions.Result>;
182
+ declare function chatCompletions(model: AI.Model, messages: AI.Message[], options?: {
183
+ stream?: boolean;
184
+ tools?: AI.ToolDefinition[];
185
+ }): Promise<ChatCompletions.NonStreamResult>;
186
+ //#endregion
187
+ //#region src/ai/helper.d.ts
188
+ /**
189
+ * 辅助定义一个POST /chat/completions支持的model参数
190
+ * @remarks 只有baseUrl字段是必须的,其他字段请查看AI.Model类型
191
+ */
192
+ declare const defineModel: (config: AI.Model) => AI.Model;
193
+ /**
194
+ * 辅助定义一个POST /chat/completions支持的tools中的子元素
195
+ * @param handler 在AI请求调用工具时用到
196
+ */
197
+ declare const defineTool: (name: AI.ToolDefinition["function"]["name"], description: AI.ToolDefinition["function"]["description"], properties: AI.ToolDefinition["function"]["parameters"]["properties"], handler: AI.ToolDefinition["handler"]) => AI.ToolDefinition;
190
198
  /**
191
- * 辅助定义一个 chatCompletions 支持的模型配置
199
+ * 从GET /models获取模型名称
192
200
  */
193
- declare const defineModel: (config: ChatCompletions.Model) => ChatCompletions.Model;
201
+ declare const getModelName: (baseUrl: string) => Promise<string>;
194
202
  //#endregion
195
203
  //#region src/dom/logger.d.ts
196
204
  /**
@@ -348,11 +356,15 @@ type Primitive = number | string | boolean | symbol | bigint | undefined | null;
348
356
  declare const isPrimitive: (value: any) => value is Primitive;
349
357
  //#endregion
350
358
  //#region src/network/fetcher.d.ts
351
- type RequestInit = globalThis.RequestInit & {
359
+ type RequestInit = Omit<globalThis.RequestInit, "body"> & {
352
360
  /**
353
361
  * searchParams 查询参数对象
354
362
  */
355
363
  params?: Record<string, any>;
364
+ /**
365
+ * 请求体支持任意类型
366
+ */
367
+ body?: any;
356
368
  /**
357
369
  * 响应解析器,默认的解析方法为 response.json()
358
370
  */
@@ -818,4 +830,4 @@ declare const sleep: (time?: number) => Promise<unknown>;
818
830
  */
819
831
  declare const throttle: <T extends (...args: any[]) => any>(fn: T, delay?: number) => (this: any, ...args: Parameters<T>) => void;
820
832
  //#endregion
821
- export { CamelToSnake, Capitalize, type ChatCompletions, Decapitalize, DeepMapKeys, DeepMapValues, ImageCompressionOptions, LockQueue, LoggerOptions, Primitive, RequestInit, SetTtl, SnakeToCamel, camelToSnake, capitalize, chatCompletions, compactStr, debounce, decapitalize, defineModel, extractErrorMessage, fetcher, getRealURL, imageUrlToBase64, isNil, isObject, isPrimitive, logger, loopUntil, mapKeys, mapValues, mergeObjects, omit, omitBy, parseSSE, pick, pickBy, qs, randomInt, sleep, snakeToCamel, throttle, to, withCache };
833
+ export { AI, CamelToSnake, Capitalize, type ChatCompletions, Decapitalize, DeepMapKeys, DeepMapValues, ImageCompressionOptions, LockQueue, LoggerOptions, Primitive, RequestInit, SetTtl, SnakeToCamel, camelToSnake, capitalize, chatCompletions, compactStr, debounce, decapitalize, defineModel, defineTool, extractErrorMessage, fetcher, getModelName, getRealURL, imageUrlToBase64, isNil, isObject, isPrimitive, logger, loopUntil, mapKeys, mapValues, mergeObjects, omit, omitBy, parseSSE, pick, pickBy, qs, randomInt, sleep, snakeToCamel, throttle, to, withCache };
package/dist/index.mjs CHANGED
@@ -2,6 +2,6 @@ const e=e=>e==null,t=e=>e?.constructor===Object,n=e=>e==null||typeof e!=`object`
2
2
  `)),a=r?a.replace(/\r?\n/g,` `):a.replace(/\r?\n/g,`\\n`),a=a.replace(/\s+/g,` `).trim(),n>0&&a.length>n?`${a.slice(0,n)}...`:a},h=e=>{if(e instanceof Error)return e.message;if(typeof e==`string`)return e;if(t(e)){let t=e.message||e.msg;if(t)return t;for(let t of Object.values(e)){let e=h(t);if(e)return e}}return JSON.stringify(e,null,2)},g={parse:e=>{let t=new URLSearchParams(e),n={};for(let[e,r]of t)Number.isNaN(Number(r))?n[e]=r:n[e]=Number(r);return n},stringify:(e,t)=>{let{addQueryPrefix:n=!1}=t??{},r=new URLSearchParams(e).toString();return r?n?`?${r}`:r:``}},_=(n=``,r={})=>{let i=async(i,o={})=>{let s=new URL(n?`${n}${i}`:i),{params:c,parser:l,...u}=a(r,o);t(c)&&Object.entries(c).forEach(([t,n])=>{e(n)||s.searchParams.append(t,n.toString())}),(t(u.body)||Array.isArray(u.body))&&(u.body=JSON.stringify(u.body),u.headers={...u.headers,"Content-Type":`application/json`});let d=await fetch(s,u);if(!d.ok){let e=d.statusText||await d.text();try{e=h(JSON.parse(e))}catch{}throw Error(e)}return await(l?.(d)??d.json())};return{get:(e,t)=>i(e,{...t,method:`GET`}),post:(e,t,n)=>i(e,{...n,method:`POST`,body:t}),put:(e,t,n)=>i(e,{...n,method:`PUT`,body:t}),delete:(e,t)=>i(e,{...t,method:`DELETE`})}},v=async e=>{try{return[null,await e]}catch(e){return[e,void 0]}},y=async e=>{let[t,n]=await v(fetch(e,{method:`HEAD`,redirect:`manual`}));return t?e:n.headers.get(`location`)||e},b=e=>{let t=new Uint8Array(e),n=``;for(let e=0;e<t.byteLength;e++)n+=String.fromCharCode(t[e]);return btoa(n)},x=async(e,t={})=>{let{quality:n=.92,compressor:r,fetcher:i=fetch}=t;if(!e.startsWith(`http`))throw Error(`图片地址必须以http或https开头`);let a=await i(e);if(!a.ok)throw Error(`获取图片失败: ${a.statusText}`);let o=a.headers.get(`Content-Type`)||`image/jpeg`,s=await a.arrayBuffer();if(o!==`image/jpeg`&&o!==`image/png`)return`data:${o};base64,${b(s)}`;if(r)return await r(s,o,n);if(typeof OffscreenCanvas<`u`){let e=null;try{let t=new Blob([s],{type:o});e=await createImageBitmap(t);let r=new OffscreenCanvas(e.width,e.height),i=r.getContext(`2d`);if(!i)throw Error(`无法获取 OffscreenCanvas context`);return i.drawImage(e,0,0),e.close(),e=null,`data:${o};base64,${b(await(await r.convertToBlob({type:o,quality:n})).arrayBuffer())}`}catch{return e?.close(),`data:${o};base64,${b(s)}`}}return`data:${o};base64,${b(s)}`};async function*S(e){let t=e.body?.getReader();if(!t)return;let n=new TextDecoder,r=``;try{for(;;){let{value:e,done:i}=await t.read();if(i)break;r+=n.decode(e,{stream:!0});let a=r.split(`
3
3
 
4
4
  `);r=a.pop()||``;for(let e of a){let t=e.split(`
5
- `);for(let e of t){if(!e.startsWith(`data:`))continue;let t=e.replace(/^data:\s*/,``).trim();try{yield JSON.parse(t)}catch{yield t}}}}}finally{t.releaseLock()}}const C=async e=>{let t=(await e.get(`/models`)).data[0]?.id;if(!t)throw Error(`无法从 /models 获取模型名称`);return t},w=async(e,t,n)=>{let r=t[e.function.name];if(!r)return`没有找到工具“${e.function.name}”的处理函数`;try{let t=await r(JSON.parse(e.function.arguments),n);return typeof t==`string`?t:JSON.stringify(t)}catch(t){if(t instanceof Error&&t.name===e.function.name)throw t;return`工具“${e.function.name}”处理失败:${h(t)}`}},T=e=>typeof e==`string`?e:e.filter(e=>e.type===`text`).map(e=>e.text).join(`
6
- `),E=async(e,t,n,r)=>{let{baseUrl:i,apiKey:a=``,model:o}=e,s=_(i,{headers:{Authorization:`Bearer ${a}`}}),c={model:o??await C(s),messages:t,...r};for(;;){let{choices:e,usage:r,...i}=await s.post(`/chat/completions`,c),{message:a}=e?.[0]??{};if(!a)throw Error(`模型没有回复任何内容`);t.push(a);let{content:o=``,tool_calls:l=[],...u}=a,d=u?.reasoning_content||u?.reasoning;if(l.length>0&&Object.keys(n).length>0){for(let e of l){let r=await w(e,n,{messages:t});t.push({role:`tool`,content:r,tool_call_id:e.id})}continue}return{content:T(o),reasoningContent:d,usage:r,...i,...u}}},D=async function*(e,t,n,r){let{baseUrl:i,apiKey:a=``,model:o}=e,s=_(i,{headers:{Authorization:`Bearer ${a}`}}),c={model:o??await C(s),messages:t,stream:!0,...r};for(;;){let e=new Map,r=``,i=null,a,o=await s.post(`/chat/completions`,c,{parser:async e=>e});for await(let t of S(o)){t.usage&&(a=t.usage);let n=t.choices?.[0];if(!n)continue;let{delta:o}=n,{content:s,tool_calls:c}=o,l=o.reasoning_content||o.reasoning;if(l&&(yield{reasoningContent:l}),s&&(r+=s,yield{content:s}),c)for(let t of c){let n=e.get(t.index)??{id:``,type:`function`,function:{name:``,arguments:``}};t.id&&(n.id=t.id),t.function?.name&&(n.function.name+=t.function.name),t.function?.arguments&&(n.function.arguments+=t.function.arguments),e.set(t.index,n)}n.finish_reason&&(i=n.finish_reason)}let l=Array.from(e.values());if(i!==`tool_calls`&&l.length>0&&Object.keys(n).length>0){t.push({role:`assistant`,content:r,tool_calls:l});for(let e of l){let r=await w(e,n);t.push({role:`tool`,content:r,tool_call_id:e.id})}continue}t.push({role:`assistant`,content:r}),a&&(yield{usage:a});break}};async function O(e,t,n={}){let{stream:r,toolHandlers:i={},...a}=n;return(r?D:E)(e,t,i,a)}const k=e=>e;function A(e={},t=0){let{withTime:n=!0,withFileName:r=!0}=e;return(...e)=>{let i=[];if(n&&i.push(`[${new Date().toLocaleTimeString()}]`),r){let{stack:e}=Error(),n=(e?.split(`
7
- `)[2+t]?.trim())?.match(/^at\s+(?:.*?\s*\()?(.*?):(\d+)(?::(\d+))?\)?/);if(n?.[1]){let e=n[1].split(/[/\\]/).pop();i.push(`[${e}:${n[2]}]`)}}i.push(...e),console.log(...i)}}function j(e,...n){if(t(e)&&(`withTime`in e||`withFileName`in e))return A(e,0);A({},1)(e,...n)}const M=async(e,t)=>{let{maxRetries:n=5,shouldStop:r}=t??{},i;for(let t=0;t<n;t++)if(i=await e(t),r?.(i)===!0)return i;if(!r)return i;throw Error(`超过了最大循环次数(${n})且未满足停止执行条件`)},N=(e,t=-1)=>{let n=new Map,r=(...r)=>{let i=JSON.stringify(r),a=Date.now(),o=n.get(i);if(o&&a<o.expiresAt)return o.value;let s=t===-1?1/0:a+t*1e3,c=e.apply({setTtl:e=>{s=a+e*1e3}},r);if(c instanceof Promise){let e=c.then(e=>(n.set(i,{value:e,expiresAt:s}),e));return n.set(i,{value:e,expiresAt:s}),e}return n.set(i,{value:c,expiresAt:s}),c};return r.clear=()=>n.clear(),r.updateTtl=e=>{t=e;let r=Date.now(),i=r+e*1e3;for(let[e,t]of n.entries())t.expiresAt>r&&(t.expiresAt=i,n.set(e,t))},r},P=(e,t)=>Math.floor(Math.random()*(t-e+1))+e,F=(e,t=300)=>{let n=null;return(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>{e(...r)},t)}};var I=class{queue;constructor(){this.queue=Promise.resolve()}waitInQueue(){let e,t=new Promise(t=>{e=t}),n=this.queue.then(()=>e);return this.queue=t,n}};const L=async(e=150)=>new Promise(t=>{setTimeout(t,e)}),R=(e,t=300)=>{let n=null;return function(...r){n||=setTimeout(()=>{n=null,e.apply(this,r)},t)}};export{I as LockQueue,d as camelToSnake,f as capitalize,O as chatCompletions,m as compactStr,F as debounce,p as decapitalize,k as defineModel,h as extractErrorMessage,_ as fetcher,y as getRealURL,x as imageUrlToBase64,e as isNil,t as isObject,n as isPrimitive,j as logger,M as loopUntil,r as mapKeys,i as mapValues,a as mergeObjects,o as omit,s as omitBy,S as parseSSE,c as pick,l as pickBy,g as qs,P as randomInt,L as sleep,u as snakeToCamel,R as throttle,v as to,N as withCache};
5
+ `);for(let e of t){if(!e.startsWith(`data:`))continue;let t=e.replace(/^data:\s*/,``).trim();try{yield JSON.parse(t)}catch{yield t}}}}}finally{t.releaseLock()}}const C=e=>e,w=(e,t,n,r)=>{let i=[];return{type:`function`,function:{name:e,description:t,parameters:{type:`object`,properties:l(n,e=>e===`required`?(i.push(e),!1):!0),required:i}},handler:r}},T=async e=>{let t=(await _(e).get(`/v1/models`)).data[0]?.id;if(!t)throw Error(`无法从/models获取模型名称`);return t},E=e=>[c(e,[`type`,`function`]),c(e,[`handler`])],D=async(e,t,n)=>{let r=t.find(t=>t.function.name===e.function.name)?.handler;if(!r)return`没有找到工具“${e.function.name}”的处理函数`;try{let t=await r(JSON.parse(e.function.arguments),n);return typeof t==`string`?t:JSON.stringify(t)}catch(t){if(t instanceof Error&&t.name===e.function.name)throw t;return`工具“${e.function.name}”处理失败:${h(t)}`}},O=e=>e.reasoning||e.reasoning_content,k=e=>typeof e==`string`?e:e.filter(e=>e.type===`text`).map(e=>e.text).join(`
6
+ `),A=async(e,t,n=[])=>{for(;;){let{choices:r,usage:i,...a}=await e.post(`/chat/completions`,{}),{message:o}=r?.[0]??{};if(!o)throw Error(`模型没有回复任何内容`);t.push(o);let{content:s=``,tool_calls:c=[],...l}=o,u=O(o);if(c.length>0&&n.length>0){for(let e of c){let r=await D(e,n,{messages:t});t.push({role:`tool`,content:r,tool_call_id:e.id})}continue}return{content:k(s),reasoning:u,usage:i,...a,...l}}},j=async function*(e,t,n=[]){for(;;){let r=new Map,i=``,a=null,o,s=await e.post(`/chat/completions`,{stream:!0},{parser:async e=>e});for await(let e of S(s)){e.usage&&(o=e.usage);let t=e.choices?.[0];if(!t)continue;let{delta:n}=t,{content:s,tool_calls:c}=n,l=O(n);if(l&&(yield{reasoning:l}),s&&(i+=s,yield{content:s}),c)for(let e of c){let t=r.get(e.index)??{id:``,type:`function`,function:{name:``,arguments:``}};e.id&&(t.id=e.id),e.function?.name&&(t.function.name+=e.function.name),e.function?.arguments&&(t.function.arguments+=e.function.arguments),r.set(e.index,t)}t.finish_reason&&(a=t.finish_reason)}let c=Array.from(r.values());if(a===`tool_calls`&&c.length>0&&n.length>0){t.push({role:`assistant`,content:i,tool_calls:c});for(let e of c){let r=await D(e,n,{messages:t});t.push({role:`tool`,content:r,tool_call_id:e.id})}continue}t.push({role:`assistant`,content:i}),o&&(yield{usage:o});break}};async function M(e,t,n){let{baseUrl:r,apiKey:i=``,model:a,...o}=e,{stream:s,tools:c=[]}=n??{},l=_(r,{headers:{Authorization:`Bearer ${i}`},body:{model:a??await T(r),messages:t,tools:c?.map(e=>E(e)[0]),...o.customBody}});return(s?j:A)(l,t,c)}function N(e={},t=0){let{withTime:n=!0,withFileName:r=!0}=e;return(...e)=>{let i=[];if(n&&i.push(`[${new Date().toLocaleTimeString()}]`),r){let{stack:e}=Error(),n=(e?.split(`
7
+ `)[2+t]?.trim())?.match(/^at\s+(?:.*?\s*\()?(.*?):(\d+)(?::(\d+))?\)?/);if(n?.[1]){let e=n[1].split(/[/\\]/).pop();i.push(`[${e}:${n[2]}]`)}}i.push(...e),console.log(...i)}}function P(e,...n){if(t(e)&&(`withTime`in e||`withFileName`in e))return N(e,0);N({},1)(e,...n)}const F=async(e,t)=>{let{maxRetries:n=5,shouldStop:r}=t??{},i;for(let t=0;t<n;t++)if(i=await e(t),r?.(i)===!0)return i;if(!r)return i;throw Error(`超过了最大循环次数(${n})且未满足停止执行条件`)},I=(e,t=-1)=>{let n=new Map,r=(...r)=>{let i=JSON.stringify(r),a=Date.now(),o=n.get(i);if(o&&a<o.expiresAt)return o.value;let s=t===-1?1/0:a+t*1e3,c=e.apply({setTtl:e=>{s=a+e*1e3}},r);if(c instanceof Promise){let e=c.then(e=>(n.set(i,{value:e,expiresAt:s}),e));return n.set(i,{value:e,expiresAt:s}),e}return n.set(i,{value:c,expiresAt:s}),c};return r.clear=()=>n.clear(),r.updateTtl=e=>{t=e;let r=Date.now(),i=r+e*1e3;for(let[e,t]of n.entries())t.expiresAt>r&&(t.expiresAt=i,n.set(e,t))},r},L=(e,t)=>Math.floor(Math.random()*(t-e+1))+e,R=(e,t=300)=>{let n=null;return(...r)=>{n&&clearTimeout(n),n=setTimeout(()=>{e(...r)},t)}};var z=class{queue;constructor(){this.queue=Promise.resolve()}waitInQueue(){let e,t=new Promise(t=>{e=t}),n=this.queue.then(()=>e);return this.queue=t,n}};const B=async(e=150)=>new Promise(t=>{setTimeout(t,e)}),V=(e,t=300)=>{let n=null;return function(...r){n||=setTimeout(()=>{n=null,e.apply(this,r)},t)}};export{z as LockQueue,d as camelToSnake,f as capitalize,M as chatCompletions,m as compactStr,R as debounce,p as decapitalize,C as defineModel,w as defineTool,h as extractErrorMessage,_ as fetcher,T as getModelName,y as getRealURL,x as imageUrlToBase64,e as isNil,t as isObject,n as isPrimitive,P as logger,F as loopUntil,r as mapKeys,i as mapValues,a as mergeObjects,o as omit,s as omitBy,S as parseSSE,c as pick,l as pickBy,g as qs,L as randomInt,B as sleep,u as snakeToCamel,V as throttle,v as to,I as withCache};
package/package.json CHANGED
@@ -1,33 +1,33 @@
1
- {
2
- "name": "@nickyzj2023/utils",
3
- "version": "1.0.75",
4
- "type": "module",
5
- "main": "dist/index.mjs",
6
- "module": "dist/index.mjs",
7
- "types": "dist/index.d.mts",
8
- "exports": {
9
- ".": {
10
- "types": "./dist/index.d.mts",
11
- "import": "./dist/index.mjs"
12
- }
13
- },
14
- "files": [
15
- "dist"
16
- ],
17
- "scripts": {
18
- "build": "tsdown",
19
- "docs": "typedoc",
20
- "check": "biome check --diagnostic-level=error --write src/"
21
- },
22
- "repository": {
23
- "type": "git",
24
- "url": "https://github.com/Nickyzj628/utils.git"
25
- },
26
- "devDependencies": {
27
- "@types/node": "^25.9.3",
28
- "tsdown": "^0.22.2",
29
- "typedoc": "^0.28.19",
30
- "typedoc-material-theme": "^1.4.1",
31
- "typescript": "^6.0.3"
32
- }
33
- }
1
+ {
2
+ "name": "@nickyzj2023/utils",
3
+ "version": "1.0.76",
4
+ "type": "module",
5
+ "main": "dist/index.mjs",
6
+ "module": "dist/index.mjs",
7
+ "types": "dist/index.d.mts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.mts",
11
+ "import": "./dist/index.mjs"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist"
16
+ ],
17
+ "scripts": {
18
+ "build": "tsdown",
19
+ "docs": "typedoc",
20
+ "check": "biome check --diagnostic-level=error --write src/"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/Nickyzj628/utils.git"
25
+ },
26
+ "devDependencies": {
27
+ "@types/node": "^25.9.3",
28
+ "tsdown": "^0.22.2",
29
+ "typedoc": "^0.28.19",
30
+ "typedoc-material-theme": "^1.4.1",
31
+ "typescript": "^6.0.3"
32
+ }
33
+ }