@shenghuabi/openai 1.1.46 → 1.2.1

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/index.mjs CHANGED
@@ -1,8 +1,8 @@
1
- // packages/openai/chat/service.ts
2
- import { inject as inject2 } from "static-injector";
3
-
4
- // packages/openai/chat/vendor/openai.ts
5
- import { OpenAI } from "openai";
1
+ // packages/openai/chat/token.ts
2
+ import { InjectionToken } from "static-injector";
3
+ var OpenAIConfigToken = new InjectionToken(
4
+ "OpenAIConfig"
5
+ );
6
6
 
7
7
  // packages/openai/chat/message.define.ts
8
8
  import * as v from "valibot";
@@ -47,389 +47,6 @@ var ChatMessageItemDefine = v.union([
47
47
  ]);
48
48
  var ChatMessageListDefine = v.array(ChatMessageItemDefine);
49
49
 
50
- // packages/openai/chat/vendor/openai.ts
51
- import * as v2 from "valibot";
52
- var OpenAIChat = class {
53
- constructor(options) {
54
- this.options = options;
55
- }
56
- options;
57
- instance;
58
- extraBody = {};
59
- extraHeaders = {};
60
- init() {
61
- this.instance = new OpenAI({
62
- apiKey: this.options.apiKey || " ",
63
- baseURL: this.options.baseURL ?? void 0
64
- });
65
- }
66
- async #createStream(input, options) {
67
- return await this.instance.chat.completions.create(input, { headers: this.extraHeaders, ...options }).catch(async (error) => {
68
- if (options?.tryPull?.(error)) {
69
- await options?.pullModel(this.options.model);
70
- return this.#createStream(input, {
71
- ...options,
72
- tryPull: () => false
73
- });
74
- }
75
- throw error;
76
- });
77
- }
78
- async *stream(input, options) {
79
- const input2 = {
80
- ...input,
81
- messages: input.messages,
82
- model: this.options.model,
83
- stream: true,
84
- max_tokens: this.options.max_tokens,
85
- top_p: this.options.top_p,
86
- temperature: this.options.temperature,
87
- frequency_penalty: this.options.frequency_penalty,
88
- presence_penalty: this.options.presence_penalty,
89
- seed: this.options.seed,
90
- stop: this.options.stop,
91
- ...this.extraBody
92
- };
93
- const result = await this.#createStream(input2, options);
94
- let isThinking = 0;
95
- for await (const item of result) {
96
- try {
97
- if (!item.choices[0].finish_reason) {
98
- if (typeof item.choices[0].delta.reasoning_content === "string" || typeof item.choices[0].delta.reasoning === "string") {
99
- if (isThinking === 0) {
100
- isThinking = 1;
101
- yield "<thinking>";
102
- }
103
- const result2 = item.choices[0].delta.reasoning_content ?? item.choices[0].delta.reasoning;
104
- if (result2) {
105
- yield result2;
106
- }
107
- } else if (typeof item.choices[0].delta.content === "string") {
108
- if (isThinking === 1) {
109
- yield "</thinking>";
110
- isThinking = 2;
111
- }
112
- const result2 = item.choices[0].delta.content;
113
- if (result2) {
114
- yield result2;
115
- }
116
- }
117
- }
118
- } catch (error) {
119
- throw error;
120
- }
121
- }
122
- }
123
- // 工具调用,暂时不清楚怎么调用...是返回工具,还是返回对话
124
- async callTool(input, options) {
125
- const result = await this.instance.chat.completions.create(
126
- {
127
- ...input,
128
- messages: v2.parse(ChatMessageListDefine, input.messages),
129
- model: this.options.model,
130
- max_tokens: this.options.max_tokens,
131
- top_p: this.options.top_p,
132
- temperature: this.options.temperature,
133
- frequency_penalty: this.options.frequency_penalty,
134
- presence_penalty: this.options.presence_penalty,
135
- seed: this.options.seed,
136
- stop: this.options.stop,
137
- ...this.extraBody,
138
- stream: false
139
- },
140
- { headers: this.extraHeaders, ...options }
141
- );
142
- const isTool = !!result.choices[0].message.tool_calls;
143
- if (isTool) {
144
- const data = result.choices[0].message.tool_calls[0].function;
145
- return {
146
- type: "function",
147
- function: {
148
- name: data.name,
149
- arguments: JSON.parse(data.arguments)
150
- }
151
- };
152
- }
153
- return { type: "text", text: result.choices[0].message.content };
154
- }
155
- };
156
-
157
- // packages/openai/chat/vendor/openai-compatible.factory.ts
158
- var VendorObject = {
159
- "360": { options: { baseURL: "https://ai.360.cn", modelList: [] } },
160
- azure: { options: { baseURL: "", modelList: [] } },
161
- moonshot: { options: { baseURL: "https://api.moonshot.cn", modelList: [] } },
162
- baichuan: {
163
- options: { baseURL: "https://api.baichuan-ai.com", modelList: [] }
164
- },
165
- minimax: { options: { baseURL: "https://api.minimax.chat", modelList: [] } },
166
- mistralai: { options: { baseURL: "https://api.mistral.ai", modelList: [] } },
167
- groq: {
168
- options: { baseURL: "https://api.groq.com/openai", modelList: [] }
169
- },
170
- lingyiwanwu: {
171
- options: { baseURL: "https://api.lingyiwanwu.com", modelList: [] }
172
- },
173
- stepfun: { options: { baseURL: "https://api.stepfun.com", modelList: [] } },
174
- deepseek: { options: { baseURL: "https://api.deepseek.com", modelList: [] } },
175
- "together.ai": {
176
- options: { baseURL: "https://api.together.xyz", modelList: [] }
177
- },
178
- volcengine: {
179
- options: {
180
- baseURL: "https://ark.cn-beijing.volces.com/api/v3",
181
- modelList: []
182
- }
183
- },
184
- novita: {
185
- options: { baseURL: "https://api.novita.ai/v3/openai", modelList: [] }
186
- },
187
- siliconflow: {
188
- options: { baseURL: "https://api.siliconflow.cn", modelList: [] }
189
- },
190
- // 官网明确兼容
191
- // https://help.aliyun.com/zh/model-studio/developer-reference/use-qwen-by-calling-api?spm=a2c6h.13066369.question.5.3a0e527bbxeJLJ#4ec3e641c294d
192
- tongyi: {
193
- options: {
194
- baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
195
- modelList: []
196
- }
197
- },
198
- // 文档上看是一样的
199
- // https://open.bigmodel.cn/dev/api/normal-model/glm-4
200
- zhipu: {
201
- options: {
202
- baseURL: "https://open.bigmodel.cn/api/paas/v4",
203
- modelList: []
204
- }
205
- },
206
- // 明确兼容
207
- // https://www.xfyun.cn/doc/spark/HTTP%E8%B0%83%E7%94%A8%E6%96%87%E6%A1%A3.html#_7-%E4%BD%BF%E7%94%A8openai-sdk%E8%AF%B7%E6%B1%82%E7%A4%BA%E4%BE%8B
208
- spark: {
209
- options: {
210
- baseURL: "https://spark-api-open.xf-yun.com/v1",
211
- modelList: []
212
- }
213
- },
214
- // 官方兼容
215
- // https://cloud.tencent.com/document/product/1729/111007
216
- hunyuan: {
217
- options: {
218
- baseURL: "https://api.hunyuan.cloud.tencent.com/v1",
219
- modelList: []
220
- }
221
- }
222
- };
223
- function createCompatibleFactory(options) {
224
- const vendorConfig = VendorObject[options.vendor];
225
- return new OpenAIChat({
226
- ...options,
227
- baseURL: vendorConfig.options.baseURL
228
- });
229
- }
230
-
231
- // packages/openai/chat/vendor/genmini/genmini.ts
232
- import { GoogleGenerativeAI } from "@google/generative-ai";
233
-
234
- // packages/openai/chat/vendor/genmini/message.define.ts
235
- import * as v3 from "valibot";
236
- import { isTruthy } from "@cyia/util";
237
- function messageContentTransform(list) {
238
- return list.map((item) => item.type === "text" ? { text: item.text } : void 0).filter(isTruthy);
239
- }
240
- var GenminiSystemMessageDefine = v3.pipe(
241
- SystemChatMessage,
242
- v3.transform((item) => ({
243
- role: "system",
244
- parts: messageContentTransform(item.content)
245
- }))
246
- );
247
- var GenminiChatMessageListDefine = v3.pipe(
248
- v3.array(
249
- v3.union([
250
- v3.pipe(
251
- UserChatMessage,
252
- v3.transform((item) => ({
253
- role: "user",
254
- parts: messageContentTransform(item.content)
255
- }))
256
- ),
257
- v3.pipe(
258
- AssistantChatMessage,
259
- v3.transform((item) => ({
260
- role: "model",
261
- parts: messageContentTransform(item.content)
262
- }))
263
- )
264
- ])
265
- )
266
- );
267
-
268
- // packages/openai/chat/vendor/genmini/genmini.ts
269
- import * as v4 from "valibot";
270
- var GeminiChat = class extends OpenAIChat {
271
- #ggai;
272
- init() {
273
- this.#ggai = new GoogleGenerativeAI(this.options.apiKey);
274
- }
275
- async *stream(input, options) {
276
- const model = this.#ggai.getGenerativeModel({
277
- model: this.options.model,
278
- generationConfig: {
279
- topP: this.options.top_p,
280
- temperature: this.options.temperature,
281
- maxOutputTokens: this.options.max_tokens
282
- }
283
- });
284
- const messages = input.messages;
285
- const data = {};
286
- if (messages[0].role === "system") {
287
- const system = messages.shift();
288
- data.systemInstruction = v4.parse(GenminiSystemMessageDefine, system);
289
- }
290
- data.history = v4.parse(GenminiChatMessageListDefine, messages);
291
- const lastMessage = data.history.pop();
292
- const chat = model.startChat(data);
293
- const result = await chat.sendMessageStream(lastMessage.parts, {
294
- signal: options?.signal
295
- });
296
- for await (const chunk of result.stream) {
297
- const chunkText = chunk.text();
298
- yield chunkText;
299
- }
300
- }
301
- };
302
-
303
- // packages/openai/chat/vendor/anthropic/anthropic.ts
304
- import Anthropic from "@anthropic-ai/sdk";
305
-
306
- // packages/openai/chat/vendor/anthropic/message.define.ts
307
- import { isTruthy as isTruthy2 } from "@cyia/util";
308
- import * as v5 from "valibot";
309
- function baseImageSplit(str) {
310
- const mimeTypeIndex = str.indexOf(";");
311
- const dataStartIndex = str.indexOf(",", mimeTypeIndex);
312
- return {
313
- mimeType: str.slice(5, mimeTypeIndex),
314
- data: str.slice(dataStartIndex + 1)
315
- };
316
- }
317
- var AnthropicSystemMessageDefine = v5.pipe(
318
- SystemChatCompletionContent,
319
- v5.transform(
320
- (list) => list.map((item) => item.type === "text" ? item : void 0).filter(isTruthy2)
321
- )
322
- );
323
- function contentConvert(part) {
324
- if (part.type === "text") {
325
- return part;
326
- }
327
- const result = baseImageSplit(part.image_url.url);
328
- return {
329
- type: "image",
330
- source: {
331
- type: "base64",
332
- media_type: result.mimeType,
333
- data: result.data
334
- }
335
- };
336
- }
337
- var UserMessageDefine = v5.pipe(
338
- UserChatMessage,
339
- v5.transform((item) => ({
340
- ...item,
341
- content: item.content.map(contentConvert)
342
- }))
343
- );
344
- var AssistantMessageDefine = v5.pipe(
345
- AssistantChatMessage,
346
- v5.transform((item) => ({
347
- ...item,
348
- content: item.content.map(contentConvert)
349
- }))
350
- );
351
- var AnthropicChatMessageListDefine = v5.array(
352
- v5.union([UserMessageDefine, AssistantMessageDefine])
353
- );
354
-
355
- // packages/openai/chat/vendor/anthropic/anthropic.ts
356
- import { createAsyncGeneratorAdapter } from "@cyia/util";
357
- import * as v6 from "valibot";
358
- var AnthropicChat = class extends OpenAIChat {
359
- client;
360
- init() {
361
- this.client = new Anthropic({ apiKey: this.options.apiKey });
362
- }
363
- async *stream(input, options) {
364
- const body = {
365
- model: this.options.model,
366
- max_tokens: this.options.max_tokens,
367
- top_p: this.options.top_p,
368
- temperature: this.options.temperature,
369
- stream: true
370
- };
371
- if (input.messages[0].role === "system") {
372
- const system = input.messages.shift().content;
373
- body.system = v6.parse(AnthropicSystemMessageDefine, system);
374
- }
375
- body.messages = v6.parse(AnthropicChatMessageListDefine, input.messages);
376
- const result = this.client.messages.stream(body, {
377
- signal: options?.signal
378
- });
379
- const aga = createAsyncGeneratorAdapter();
380
- result.once("end", () => aga.complete());
381
- result.on("text", (text) => {
382
- aga.next(text);
383
- });
384
- for await (const item of aga.getData()) {
385
- yield item;
386
- }
387
- }
388
- async callTool(input, options) {
389
- const body = {};
390
- if (input.messages[0].role === "system") {
391
- const system = input.messages.shift().content;
392
- body.system = v6.parse(AnthropicSystemMessageDefine, system);
393
- }
394
- body.messages = v6.parse(AnthropicChatMessageListDefine, input.messages);
395
- const result = await this.client.messages.create(
396
- {
397
- ...body,
398
- model: this.options.model,
399
- max_tokens: this.options.max_tokens,
400
- top_p: this.options.top_p,
401
- temperature: this.options.temperature,
402
- stream: false,
403
- tools: input.tools.map((item) => ({
404
- name: item.function.name,
405
- description: item.function.description,
406
- input_schema: item.function.parameters
407
- }))
408
- },
409
- {
410
- signal: options?.signal
411
- }
412
- );
413
- const isToolResult = result.content.find(
414
- (item) => item.type === "tool_use"
415
- );
416
- if (isToolResult) {
417
- return {
418
- type: "function",
419
- function: {
420
- name: isToolResult.name,
421
- arguments: isToolResult.input
422
- }
423
- };
424
- }
425
- const text = result.content.find((item) => item.type === "text");
426
- return {
427
- type: "text",
428
- text: text.text
429
- };
430
- }
431
- };
432
-
433
50
  // packages/openai/chat/util/create.ts
434
51
  function createSystemMessage() {
435
52
  return { role: "system" };
@@ -447,560 +64,606 @@ function createAssistantMessage(text = "") {
447
64
  };
448
65
  }
449
66
 
450
- // packages/openai/chat/const.ts
451
- var ThinkList = [
452
- "think",
453
- "thinking",
454
- "reason",
455
- "reasoning",
456
- "thought",
457
- "Thought"
458
- ];
67
+ // packages/openai/chat/module.ts
68
+ var OPENAI_MODULE = {
69
+ provider: [],
70
+ token: { OpenAIConfigToken }
71
+ };
459
72
 
460
- // packages/openai/chat/options.define.ts
461
- import { asControl, actions, setComponent } from "@piying/view-angular-core";
462
- import * as v7 from "valibot";
463
- import {
464
- asVirtualGroup,
465
- metadataPipe,
466
- omitIntersect
467
- } from "@piying/valibot-visit";
468
- var VendorList = [
469
- {
470
- value: "openai",
471
- label: "openai",
472
- options: {
473
- baseURL: "",
474
- modelList: [],
475
- description: "如果是openai兼容的厂商,需要设置baseURL"
476
- }
477
- },
478
- {
479
- value: "360",
480
- label: "360",
481
- options: { baseURL: "https://ai.360.cn", modelList: [] }
482
- },
483
- {
484
- value: "azure",
485
- label: "azure",
486
- options: { baseURL: "", modelList: [] }
487
- },
488
- {
489
- value: "moonshot",
490
- label: "moonshot",
491
- options: { baseURL: "https://api.moonshot.cn", modelList: [] }
492
- },
493
- {
494
- value: "baichuan",
495
- label: "baichuan",
496
- options: { baseURL: "https://api.baichuan-ai.com", modelList: [] }
497
- },
498
- {
499
- value: "minimax",
500
- label: "minimax",
501
- options: { baseURL: "https://api.minimax.chat", modelList: [] }
502
- },
503
- {
504
- value: "mistralai",
505
- label: "mistralai",
506
- options: { baseURL: "https://api.mistral.ai", modelList: [] }
507
- },
508
- {
509
- value: "groq",
510
- label: "groq",
511
- options: { baseURL: "https://api.groq.com/openai", modelList: [] }
512
- },
513
- {
514
- value: "lingyiwanwu",
515
- label: "lingyiwanwu",
516
- options: { baseURL: "https://api.lingyiwanwu.com", modelList: [] }
517
- },
518
- {
519
- value: "stepfun",
520
- label: "stepfun",
521
- options: { baseURL: "https://api.stepfun.com", modelList: [] }
522
- },
523
- {
524
- value: "deepseek",
525
- label: "deepseek",
526
- options: { baseURL: "https://api.deepseek.com", modelList: [] }
527
- },
528
- {
529
- value: "together.ai",
530
- label: "together.ai",
531
- options: { baseURL: "https://api.together.xyz", modelList: [] }
532
- },
533
- {
534
- value: "volcengine",
535
- label: "volcengine",
536
- options: {
537
- baseURL: "https://ark.cn-beijing.volces.com/api/v3",
538
- modelList: []
539
- }
540
- },
541
- {
542
- value: "novita",
543
- label: "novita",
544
- options: { baseURL: "https://api.novita.ai/v3/openai", modelList: [] }
545
- },
546
- {
547
- value: "siliconflow",
548
- label: "siliconflow",
549
- options: { baseURL: "https://api.siliconflow.cn", modelList: [] }
550
- },
551
- {
552
- value: "tongyi",
553
- label: "tongyi",
554
- options: {
555
- baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
556
- modelList: []
557
- }
558
- },
559
- {
560
- value: "zhipu",
561
- label: "zhipu",
562
- options: {
563
- baseURL: "https://open.bigmodel.cn/api/paas/v4",
564
- modelList: []
565
- }
566
- },
567
- {
568
- value: "spark",
569
- label: "spark",
570
- options: {
571
- baseURL: "https://spark-api-open.xf-yun.com/v1",
572
- modelList: []
573
- }
574
- },
575
- {
576
- value: "hunyuan",
577
- label: "hunyuan",
578
- options: {
579
- baseURL: "https://api.hunyuan.cloud.tencent.com/v1",
580
- modelList: []
581
- }
582
- },
583
- {
584
- value: "gemini",
585
- label: "gemini",
586
- options: {
587
- baseURL: "",
588
- modelList: [],
589
- description: "非openai兼容,手动适配,如果有问题欢迎报告"
590
- }
591
- },
592
- {
593
- value: "claude",
594
- label: "claude",
595
- options: {
596
- baseURL: "",
597
- modelList: [],
598
- description: "非openai兼容,手动适配,如果有问题欢迎报告"
599
- }
73
+ // packages/openai/chat/chat.ts
74
+ import { stream } from "@earendil-works/pi-ai";
75
+
76
+ // packages/openai/chat/util/get-custom-provider.ts
77
+ import * as v3 from "valibot";
78
+ import { getModel } from "@earendil-works/pi-ai";
79
+
80
+ // packages/openai/chat/provider.define.ts
81
+ import * as v2 from "valibot";
82
+ var KnownProviderDefine = v2.pipe(
83
+ v2.picklist([
84
+ "amazon-bedrock",
85
+ "anthropic",
86
+ "google",
87
+ "google-vertex",
88
+ "openai",
89
+ "azure-openai-responses",
90
+ "openai-codex",
91
+ "deepseek",
92
+ "github-copilot",
93
+ "xai",
94
+ "groq",
95
+ "cerebras",
96
+ "openrouter",
97
+ "vercel-ai-gateway",
98
+ "zai",
99
+ "mistral",
100
+ "minimax",
101
+ "minimax-cn",
102
+ "moonshotai",
103
+ "moonshotai-cn",
104
+ "huggingface",
105
+ "fireworks",
106
+ "together",
107
+ "opencode",
108
+ "opencode-go",
109
+ "kimi-coding",
110
+ "cloudflare-workers-ai",
111
+ "cloudflare-ai-gateway",
112
+ "xiaomi",
113
+ "xiaomi-token-plan-cn",
114
+ "xiaomi-token-plan-ams",
115
+ "xiaomi-token-plan-sgp",
116
+ "ant-ling",
117
+ "nvidia",
118
+ "zai-coding-cn",
119
+ /** 自定义 */
120
+ "openai-completions",
121
+ "openai-responses",
122
+ "anthropic-messages"
123
+ ]),
124
+ v2.title("提供商"),
125
+ v2.description(`'openai-completions','openai-responses','anthropic-messages',为自定义提供商`)
126
+ );
127
+
128
+ // packages/openai/chat/util/get-custom-provider.ts
129
+ import { hideWhen } from "@piying/view-angular-core";
130
+ import { map } from "rxjs";
131
+ function getCustomProvider(compat, model, name, input) {
132
+ return { ...input, id: model, api: compat, name };
133
+ }
134
+ function getModelConfig(input) {
135
+ if (input.provider === "faux") {
136
+ return {
137
+ model: input.config,
138
+ config: input
139
+ };
600
140
  }
601
- ];
602
- var vendorOptionsDescribe = `## 通用参数
603
- - \`extraOptions\`字段用于覆盖默认字段
604
- \`\`\`json
605
- {"厂商名(与vendor字段相同)":{ "extraOptions":{"temperature":0.1,"topP":0.8,"maxTokens":8192,"baseURL":"http://127.0.0.1:11434/v1","apiKey":" "} }
606
- \`\`\`
607
- `;
608
- var InputWrapper = metadataPipe(actions.wrappers.set(["tooltip", "label"]));
609
- var ChatItemDefine = v7.pipe(
610
- v7.intersect([
611
- v7.pipe(
612
- v7.object({
613
- name: v7.pipe(v7.string(), v7.title("配置名")),
614
- vendor: v7.pipe(
615
- v7.optional(
616
- v7.picklist(VendorList.map((item) => item.value)),
617
- "openai"
618
- ),
619
- v7.description("大语言模型提供的厂商"),
620
- v7.title("对话厂商"),
621
- v7.metadata({
622
- enumOptions: VendorList.map((item) => ({
623
- label: item.label,
624
- description: [
625
- item.options.description ? item.options.description : "",
626
- item.options.baseURL ? `链接: ${item.options.baseURL}` : ""
627
- ].filter(Boolean).join("\n")
628
- }))
629
- }),
630
- actions.inputs.patch({
631
- options: VendorList
632
- })
141
+ const config = v3.parse(ModelConfigDefine, input);
142
+ if (config.provider === "openai-completions" || config.provider === "openai-responses" || config.provider === "anthropic-messages") {
143
+ return {
144
+ model: getCustomProvider(
145
+ config.provider,
146
+ config.model,
147
+ config.name,
148
+ config.config
149
+ ),
150
+ config
151
+ };
152
+ }
153
+ return {
154
+ model: getModel(config.provider, config.model),
155
+ config
156
+ };
157
+ }
158
+ var ThinkingLevelSchema = v3.pipe(
159
+ v3.picklist(["minimal", "low", "medium", "high", "xhigh"]),
160
+ v3.title("思考等级"),
161
+ v3.description("模型思考的详细程度,从最小到最大")
162
+ );
163
+ var ModelThinkingLevelSchema = v3.pipe(
164
+ v3.picklist(["off", "minimal", "low", "medium", "high", "xhigh"]),
165
+ v3.title("模型思考等级"),
166
+ v3.description("关闭或设置模型思考的详细程度")
167
+ );
168
+ var OpenAICompletionsCompatSchema = v3.pipe(
169
+ v3.optional(
170
+ v3.object({
171
+ supportsStore: v3.optional(
172
+ v3.pipe(
173
+ v3.boolean(),
174
+ v3.title("支持存储"),
175
+ v3.description("是否支持服务端会话存储")
633
176
  )
634
- }),
635
- actions.wrappers.patch(["div"]),
636
- actions.class.top("flex *:flex-1 gap-2 items-center")
637
- ),
638
- v7.pipe(
639
- v7.object({
640
- baseURL: v7.pipe(
641
- v7.optional(v7.string(), "http://127.0.0.1:11434/v1"),
642
- v7.title("地址"),
643
- v7.description("openai兼容接口"),
644
- ...InputWrapper
645
- ),
646
- model: v7.pipe(
647
- v7.optional(v7.string(), `qwen3:8b`),
648
- v7.title("模型"),
649
- v7.description(
650
- "## 模型名\n### ollama模型推荐\n- `qwen3:8b`\n- `qwen3:14b`,`deepseek-r1:7b`,`deepseek-r1:14b`\n### 图片对话模型\n- `minicpm-v:8b`"
651
- ),
652
- ...InputWrapper
177
+ ),
178
+ supportsDeveloperRole: v3.optional(
179
+ v3.pipe(
180
+ v3.boolean(),
181
+ v3.title("支持开发者角色"),
182
+ v3.description("是否支持 developer 系统消息角色")
183
+ )
184
+ ),
185
+ supportsReasoningEffort: v3.optional(
186
+ v3.pipe(
187
+ v3.boolean(),
188
+ v3.title("支持推理努力"),
189
+ v3.description("是否支持 reasoning_effort 参数")
190
+ )
191
+ ),
192
+ supportsUsageInStreaming: v3.optional(
193
+ v3.pipe(
194
+ v3.boolean(),
195
+ v3.title("流式用量"),
196
+ v3.description("流式输出中是否返回 usage 信息")
197
+ )
198
+ ),
199
+ maxTokensField: v3.optional(
200
+ v3.pipe(
201
+ v3.picklist(["max_completion_tokens", "max_tokens"]),
202
+ v3.title("最大 token 字段名"),
203
+ v3.description("指定最大 token 参数使用的是哪个字段名")
204
+ )
205
+ ),
206
+ requiresToolResultName: v3.optional(
207
+ v3.pipe(
208
+ v3.boolean(),
209
+ v3.title("需要工具结果名称"),
210
+ v3.description("是否要求工具结果消息包含 name 字段")
211
+ )
212
+ ),
213
+ requiresAssistantAfterToolResult: v3.optional(
214
+ v3.pipe(
215
+ v3.boolean(),
216
+ v3.title("需要助手回复"),
217
+ v3.description("工具结果后是否需要助手消息跟进")
218
+ )
219
+ ),
220
+ requiresThinkingAsText: v3.optional(
221
+ v3.pipe(
222
+ v3.boolean(),
223
+ v3.title("思考转文本"),
224
+ v3.description("是否将思考过程作为普通文本发送")
225
+ )
226
+ ),
227
+ requiresReasoningContentOnAssistantMessages: v3.optional(
228
+ v3.pipe(
229
+ v3.boolean(),
230
+ v3.title("推理内容在助手消息中"),
231
+ v3.description("是否在助手消息中包含推理内容")
232
+ )
233
+ ),
234
+ thinkingFormat: v3.optional(
235
+ v3.pipe(
236
+ v3.picklist([
237
+ "openai",
238
+ "openrouter",
239
+ "deepseek",
240
+ "together",
241
+ "zai",
242
+ "qwen",
243
+ "qwen-chat-template",
244
+ "string-thinking",
245
+ "ant-ling"
246
+ ]),
247
+ v3.title("思考格式"),
248
+ v3.description("指定 thinking 标签的序列化格式")
249
+ )
250
+ ),
251
+ openRouterRouting: v3.optional(
252
+ v3.pipe(
253
+ v3.unknown(),
254
+ v3.title("OpenRouter 路由"),
255
+ v3.description("自定义 OpenRouter 的路由配置")
256
+ )
257
+ ),
258
+ vercelGatewayRouting: v3.optional(
259
+ v3.pipe(
260
+ v3.unknown(),
261
+ v3.title("Vercel Gateway 路由"),
262
+ v3.description("自定义 Vercel AI Gateway 的路由配置")
263
+ )
264
+ ),
265
+ zaiToolStream: v3.optional(
266
+ v3.pipe(
267
+ v3.boolean(),
268
+ v3.title("ZAI 工具流式"),
269
+ v3.description("是否启用 ZAI 平台工具调用的流式输出")
270
+ )
271
+ ),
272
+ supportsStrictMode: v3.optional(
273
+ v3.pipe(
274
+ v3.boolean(),
275
+ v3.title("严格模式"),
276
+ v3.description("是否支持工具的 JSON Schema 严格校验")
277
+ )
278
+ ),
279
+ cacheControlFormat: v3.optional(
280
+ v3.pipe(
281
+ v3.literal("anthropic"),
282
+ v3.title("缓存控制格式"),
283
+ v3.description("指定缓存控制的语法格式")
284
+ )
285
+ ),
286
+ sendSessionAffinityHeaders: v3.optional(
287
+ v3.pipe(
288
+ v3.boolean(),
289
+ v3.title("发送会话亲和标头"),
290
+ v3.description("是否在请求中携带会话亲和性标头")
291
+ )
292
+ ),
293
+ supportsLongCacheRetention: v3.optional(
294
+ v3.pipe(
295
+ v3.boolean(),
296
+ v3.title("长期缓存保留"),
297
+ v3.description("是否支持长期缓存保留策略")
653
298
  )
654
- }),
655
- actions.wrappers.patch([
656
- {
657
- type: "div",
658
- attributes: { class: "flex *:flex-1 gap-2 items-center" }
659
- }
660
- ])
661
- ),
662
- v7.object({
663
- apiKey: v7.pipe(
664
- v7.optional(v7.string()),
665
- v7.description("本地部署默认可以不填"),
666
- v7.title("apiKey")
667
299
  )
668
- }),
669
- v7.pipe(
670
- v7.object({
671
- max_tokens: v7.pipe(v7.optional(v7.number(), 8192), v7.title("max_tokens")),
672
- top_p: v7.pipe(
673
- v7.optional(v7.number(), 0.8),
674
- v7.minValue(0),
675
- v7.maxValue(10),
676
- v7.description(`核采样(nucleus sampling)是温度采样的另一种替代方法,模型会考虑累积概率质量达到 top_p 的 token。例如,当 top_p 设为 0.1 时,仅考虑累积概率前 10% 的 token。
677
-
678
- 我们通常建议调整 top_p 或温度参数,但不要同时调整两者。`),
679
- ...InputWrapper,
680
- v7.title("top_p")
681
- ),
682
- temperature: v7.pipe(
683
- v7.optional(v7.pipe(v7.number(), v7.minValue(0), v7.maxValue(2)), 0.1),
684
- v7.description(
685
- "采样温度应设置在0到2之间。较高的值(如0.8)会使输出更随机,而较低的值(如0.2)则会使输出更集中且确定。我们通常建议仅调整此参数或top_p,而不同时调整两者。"
686
- ),
687
- ...InputWrapper,
688
- v7.title("temperature")
689
- ),
690
- frequency_penalty: v7.pipe(
691
- v7.optional(v7.pipe(v7.number(), v7.minValue(-2), v7.maxValue(2))),
692
- v7.description(
693
- "取值范围为-2.0至2.0。正值会根据当前文本中已有标记的频率对新标记进行惩罚,从而降低模型逐字重复相同内容的概率。"
694
- ),
695
- ...InputWrapper,
696
- v7.title("frequency_penalty")
697
- ),
698
- presence_penalty: v7.pipe(
699
- v7.optional(v7.pipe(v7.number(), v7.minValue(-2), v7.maxValue(2))),
700
- v7.description(
701
- "数值介于-2.0至2.0之间。正值会基于新标记是否已在当前文本中出现,对新标记施加惩罚,从而提升模型讨论新话题的可能性。"
702
- ),
703
- ...InputWrapper,
704
- v7.title("presence_penalty")
705
- ),
706
- seed: v7.pipe(
707
- v7.optional(v7.pipe(v7.number())),
708
- v7.description(
709
- "若已指定,系统将尽力确保采样具有确定性,即使用相同种子和参数的重复请求将返回相同结果。确定性无法保证,请通过 system_fingerprint 响应参数监控后端变化。"
710
- ),
711
- ...InputWrapper,
712
- v7.title("seed")
713
- ),
714
- stop: v7.pipe(
715
- v7.optional(v7.pipe(v7.array(v7.pipe(v7.string())), v7.maxLength(4))),
716
- v7.description(
717
- "最多可指定4个停止序列,API将在生成到该序列时停止,返回的文本中不包含该停止序列。"
718
- ),
719
- asControl(),
720
- setComponent("chip-input-list"),
721
- actions.inputs.patch({
722
- addOnBlur: true
723
- }),
724
- ...InputWrapper,
725
- v7.title("stop")
300
+ })
301
+ ),
302
+ v3.title("OpenAI Completions 兼容配置"),
303
+ v3.description("针对 OpenAI Completions API 的兼容性覆写选项")
304
+ );
305
+ var OpenAIResponsesCompatSchema = v3.pipe(
306
+ v3.optional(
307
+ v3.object({
308
+ supportsDeveloperRole: v3.optional(
309
+ v3.pipe(
310
+ v3.boolean(),
311
+ v3.title("支持开发者角色"),
312
+ v3.description("是否支持 developer 系统消息角色")
726
313
  )
727
- // top_logprobs: v.pipe(
728
- // v.optional(v.pipe(v.number(), v.minValue(0), v.maxValue(20))),
729
- // v.description(
730
- // '一个介于0和20之间的整数,用于指定在每个标记位置返回的最可能的标记数量,每个标记均关联对应的对数概率。若使用此参数,则必须将logprobs设置为true。',
731
- // ),
732
- // ),
733
- // verbosity: v.pipe(
734
- // v.optional(v.picklist(['low', 'medium', 'high'])),
735
- // v.description(
736
- // `控制模型回复的详细程度。数值越低,回复越简洁;数值越高,回复越详细。当前支持的取值为低、中和高。`,
737
- // ),
738
- // ),
739
- // reasoning_effort: v.pipe(
740
- // v.optional(v.picklist(['minimal', 'low', 'medium', 'high'])),
741
- // v.description(
742
- // `控制模型回复的详细程度。数值越低,回复越简洁;数值越高,回复越详细。当前支持的取值为低、中和高。`,
743
- // ),
744
- // ),
745
- }),
746
- actions.wrappers.patch(["div"]),
747
- actions.class.top("grid gap-2")
748
- )
749
- ]),
750
- actions.wrappers.patch(["div"]),
751
- actions.class.top("grid gap-2"),
752
- asVirtualGroup()
314
+ ),
315
+ sendSessionIdHeader: v3.optional(
316
+ v3.pipe(
317
+ v3.boolean(),
318
+ v3.title("发送会话 ID 标头"),
319
+ v3.description("是否在请求中携带 X-Session-ID 标头")
320
+ )
321
+ ),
322
+ supportsLongCacheRetention: v3.optional(
323
+ v3.pipe(
324
+ v3.boolean(),
325
+ v3.title("长期缓存保留"),
326
+ v3.description("是否支持长期缓存保留策略")
327
+ )
328
+ )
329
+ })
330
+ ),
331
+ v3.title("OpenAI Responses 兼容配置"),
332
+ v3.description("针对 OpenAI Responses API 的兼容性覆写选项")
753
333
  );
754
- var VendorOptionsDefine = v7.pipe(
755
- v7.optional(
756
- v7.record(
757
- v7.string(),
758
- v7.pipe(
759
- v7.intersect([
760
- v7.object({
761
- /** @internal */
762
- extraOptions: v7.pipe(
763
- v7.optional(omitIntersect(ChatItemDefine, ["name", "vendor"])),
764
- v7.description(`附加配置`)
765
- )
766
- })
767
- ]),
768
- actions.wrappers.patch(["div"]),
769
- actions.class.top("grid gap-2"),
770
- asVirtualGroup()
334
+ var AnthropicMessagesCompatSchema = v3.pipe(
335
+ v3.optional(
336
+ v3.object({
337
+ supportsEagerToolInputStreaming: v3.optional(
338
+ v3.pipe(
339
+ v3.boolean(),
340
+ v3.title(" eagerly 工具流式"),
341
+ v3.description("是否支持工具调用的 eager 模式流式输出")
342
+ )
343
+ ),
344
+ supportsLongCacheRetention: v3.optional(
345
+ v3.pipe(
346
+ v3.boolean(),
347
+ v3.title("长期缓存保留"),
348
+ v3.description("是否支持长期缓存保留策略")
349
+ )
350
+ ),
351
+ sendSessionAffinityHeaders: v3.optional(
352
+ v3.pipe(
353
+ v3.boolean(),
354
+ v3.title("发送会话亲和标头"),
355
+ v3.description("是否在请求中携带会话亲和性标头")
356
+ )
357
+ ),
358
+ supportsCacheControlOnTools: v3.optional(
359
+ v3.pipe(
360
+ v3.boolean(),
361
+ v3.title("工具缓存控制"),
362
+ v3.description("是否支持在 tool_use 消息中使用 cache_control")
363
+ )
364
+ ),
365
+ supportsTemperature: v3.optional(
366
+ v3.pipe(
367
+ v3.boolean(),
368
+ v3.title("支持温度参数"),
369
+ v3.description("模型/接口是否支持 temperature 参数")
370
+ )
371
+ ),
372
+ forceAdaptiveThinking: v3.optional(
373
+ v3.pipe(
374
+ v3.boolean(),
375
+ v3.title("强制自适应思考"),
376
+ v3.description("是否强制启用 adaptive thinking 模式")
377
+ )
378
+ ),
379
+ allowEmptySignature: v3.optional(
380
+ v3.pipe(
381
+ v3.boolean(),
382
+ v3.title("允许空签名"),
383
+ v3.description("是否允许工具调用不指定签名(sha256)")
384
+ )
771
385
  )
772
- )
386
+ })
773
387
  ),
774
- v7.description(vendorOptionsDescribe),
775
- setComponent("rest-chip-group"),
776
- actions.inputs.patch({
777
- optionalkeyList: VendorList.map((item) => item.value),
778
- placeholder: "请设置额外配置"
779
- }),
780
- v7.title("厂商额外配置")
388
+ v3.title("Anthropic Messages 兼容配置"),
389
+ v3.description("针对 Anthropic Messages API 的兼容性覆写选项")
781
390
  );
782
- var ChatParamsItemDefine = v7.pipe(
783
- v7.intersect([
784
- ChatItemDefine,
785
- v7.object({ vendorOptions: VendorOptionsDefine })
391
+ var CompatSchema = v3.pipe(
392
+ v3.union([
393
+ OpenAICompletionsCompatSchema,
394
+ OpenAIResponsesCompatSchema,
395
+ AnthropicMessagesCompatSchema
786
396
  ]),
787
- asVirtualGroup(),
788
- actions.wrappers.patch(["div"]),
789
- actions.class.top("grid gap-2")
397
+ v3.title("兼容配置"),
398
+ v3.description(
399
+ "模型 API 兼容性覆写选项,支持 OpenAI Completions、OpenAI Responses、Anthropic Messages 三种格式"
400
+ )
790
401
  );
791
- var ChatParamsListDefine = v7.pipe(
792
- v7.array(ChatParamsItemDefine),
793
- setComponent("label-chip-array"),
794
- v7.description("对话模型列表,目前用于切换使用"),
795
- actions.inputs.patch({
796
- displayKey: "name",
797
- placeholder: "请添加配置"
798
- })
402
+ var InputChoiceSchema = v3.pipe(
403
+ v3.picklist(["text", "image"]),
404
+ v3.title("输入类型"),
405
+ v3.description("支持的输入模态类型")
799
406
  );
800
- var InputChatOptionsDefine = omitIntersect(ChatParamsItemDefine, [
801
- "name"
802
- ]);
803
-
804
- // packages/openai/chat/token.ts
805
- import { InjectionToken } from "static-injector";
806
- var OpenAIConfigToken = new InjectionToken(
807
- "OpenAIConfig"
407
+ var InputSchema = v3.pipe(
408
+ v3.array(InputChoiceSchema),
409
+ v3.title("输入类型列表"),
410
+ v3.description("该模型支持的用户输入类型,如 text 文本或 image 图片")
411
+ );
412
+ var HeadersSchema = v3.pipe(
413
+ v3.record(v3.string(), v3.string()),
414
+ v3.title("自定义请求头"),
415
+ v3.description("额外的 HTTP 请求头键值对配置")
416
+ );
417
+ var CostSchema = v3.pipe(
418
+ v3.optional(
419
+ v3.object({
420
+ input: v3.pipe(
421
+ v3.number(),
422
+ v3.title("输入单价"),
423
+ v3.description("每 token 输入费用")
424
+ ),
425
+ output: v3.pipe(
426
+ v3.number(),
427
+ v3.title("输出单价"),
428
+ v3.description("每 token 输出费用")
429
+ ),
430
+ cacheRead: v3.pipe(
431
+ v3.number(),
432
+ v3.title("缓存读取单价"),
433
+ v3.description("从缓存读取每 token 的费用")
434
+ ),
435
+ cacheWrite: v3.pipe(
436
+ v3.number(),
437
+ v3.title("缓存写入单价"),
438
+ v3.description("写入缓存每 token 的费用")
439
+ )
440
+ }),
441
+ { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
442
+ ),
443
+ v3.title("费用配置"),
444
+ v3.description("模型各操作类型的 token 计价标准")
445
+ );
446
+ var ThinkingLevelMapSchema = v3.pipe(
447
+ v3.record(
448
+ ModelThinkingLevelSchema,
449
+ v3.pipe(
450
+ v3.nullish(v3.string()),
451
+ v3.title("配置值"),
452
+ v3.description("对应思考等级的具体配置字符串,null 表示未设置")
453
+ )
454
+ ),
455
+ v3.title("思考等级配置映射"),
456
+ v3.description("各思考等级对应的具体配置参数映射")
457
+ );
458
+ var ModelSchema = v3.pipe(
459
+ v3.object({
460
+ provider: v3.pipe(
461
+ v3.optional(v3.string(), "default"),
462
+ v3.title("厂商"),
463
+ v3.description("模型提供商,如 openai / anthropic / llama")
464
+ ),
465
+ baseUrl: v3.pipe(
466
+ v3.string(),
467
+ v3.title("基础 URL"),
468
+ v3.description("API 请求的基础地址")
469
+ ),
470
+ reasoning: v3.pipe(
471
+ v3.optional(v3.boolean(), false),
472
+ v3.title("支持推理"),
473
+ v3.description("该模型是否支持思考/推理模式")
474
+ ),
475
+ // thinkingLevelMap: v.optional(
476
+ // v.pipe(
477
+ // v.optional(ThinkingLevelMapSchema),
478
+ // v.title('思考等级配置'),
479
+ // v.description('各思考等级的具体参数配置映射'),
480
+ // ),
481
+ // ),
482
+ input: v3.pipe(
483
+ v3.optional(InputSchema, ["text", "image"]),
484
+ v3.title("输入类型"),
485
+ v3.description("支持的输入模态类型")
486
+ ),
487
+ cost: v3.pipe(
488
+ CostSchema,
489
+ v3.title("费用配置"),
490
+ v3.description("模型 token 计价标准")
491
+ ),
492
+ contextWindow: v3.pipe(
493
+ v3.pipe(v3.optional(v3.number(), 99999999)),
494
+ v3.title("上下文窗口"),
495
+ v3.description("模型支持的上下文最大 token 数")
496
+ ),
497
+ maxTokens: v3.pipe(
498
+ v3.pipe(v3.optional(v3.number(), 99999999)),
499
+ v3.title("最大输出 tokens"),
500
+ v3.description("单次请求允许的最大输出 token 数")
501
+ ),
502
+ headers: v3.optional(
503
+ v3.pipe(
504
+ v3.optional(HeadersSchema),
505
+ v3.title("自定义请求头"),
506
+ v3.description("额外的 HTTP 请求头配置")
507
+ )
508
+ ),
509
+ compat: v3.pipe(
510
+ v3.optional(CompatSchema, { supportsDeveloperRole: false }),
511
+ v3.title("兼容配置"),
512
+ v3.description("API 兼容性覆写选项")
513
+ )
514
+ }),
515
+ v3.title("模型定义"),
516
+ v3.description(
517
+ "pi-ai 模型的核心配置结构,包含模型标识、API 参数、费用及兼容性设置"
518
+ )
519
+ );
520
+ var OutputSchema = InputSchema;
521
+ var ModelConfigDefine = v3.pipe(
522
+ v3.object({
523
+ provider: KnownProviderDefine,
524
+ model: v3.pipe(v3.string(), v3.title("模型名")),
525
+ name: v3.pipe(
526
+ v3.optional(v3.string()),
527
+ v3.title("配置名"),
528
+ v3.description("模型的配置名称(默认为模型名)")
529
+ ),
530
+ /** provider是自定义时使用 */
531
+ apiKey: v3.pipe(v3.optional(v3.string()), v3.title("apiKey")),
532
+ config: v3.pipe(
533
+ v3.optional(ModelSchema),
534
+ hideWhen({
535
+ disabled: true,
536
+ listen(fn, field) {
537
+ return fn({ list: [["..", "provider"]] }).pipe(
538
+ map(
539
+ ({ list: [value] }) => !(value === "openai-completions" || value === "openai-responses" || value === "anthropic-messages")
540
+ )
541
+ );
542
+ }
543
+ })
544
+ )
545
+ }),
546
+ v3.transform((item) => ({ ...item, name: item.name ?? item.model }))
808
547
  );
809
548
 
810
- // packages/openai/chat/chat.history.service.ts
811
- import { computed, inject, signal } from "static-injector";
812
- import { omitBy } from "es-toolkit";
813
- import { bufferWhen, debounceTime, filter, Subject } from "rxjs";
814
- import { path } from "@cyia/vfs2";
815
- import * as fs from "fs";
816
- import { parse as parse4, stringify } from "yaml";
817
- import dayjs from "dayjs";
818
- import { isEmptyInput } from "@cyia/util";
819
- var ChatHistoryService = class {
820
- #update$ = signal(0);
821
- /** 外部监听更新用 */
822
- update$$ = computed(() => this.#update$());
823
- #logMap = /* @__PURE__ */ new Map();
824
- #config = inject(OpenAIConfigToken);
825
- #dir() {
826
- return this.#config().history.dir;
827
- }
828
- #createNewListen(fileName) {
829
- const filePath = path.join(this.#dir(), `${fileName}.yml`);
830
- const instance = new Subject();
831
- instance.pipe(
832
- bufferWhen(() => instance.pipe(debounceTime(2e3))),
833
- filter((list) => !!list.length)
834
- ).subscribe(async (inputList) => {
835
- if (!fs.existsSync(this.#dir())) {
836
- await fs.promises.mkdir(this.#dir(), { recursive: true });
837
- }
838
- try {
839
- let list = [];
840
- if (await fs.existsSync(filePath)) {
841
- const content = await fs.promises.readFile(filePath, {
842
- encoding: "utf-8"
843
- });
844
- list = parse4(content);
845
- } else {
846
- list = [];
549
+ // packages/openai/chat/chat.ts
550
+ import { deepClone } from "@cyia/util";
551
+ function createChatStream(input) {
552
+ const result = getModelConfig(input);
553
+ return (context, options, extra) => {
554
+ context.messages = deepClone(context.messages);
555
+ const sysIndex = context.messages.findIndex(
556
+ (item) => item.role === "system"
557
+ );
558
+ if (sysIndex !== -1) {
559
+ const item = context.messages[sysIndex];
560
+ context.messages.splice(sysIndex, 1);
561
+ context.systemPrompt = item.content[0].text;
562
+ }
563
+ const createStream = () => stream(result.model, context, {
564
+ ...options,
565
+ apiKey: options?.apiKey ?? result.config.apiKey,
566
+ onPayload(payload, model) {
567
+ payload = options?.onPayload?.(payload, model) ?? payload;
568
+ if (model.api === "openai-completions") {
569
+ return {
570
+ ...payload,
571
+ response_format: extra?.response_format
572
+ };
847
573
  }
848
- list.unshift(...inputList.reverse());
849
- await fs.promises.writeFile(filePath, stringify(list));
850
- this.#update$.update((a) => a + 1);
851
- } catch (error) {
852
- this.#config().captureException(error);
574
+ return payload;
853
575
  }
854
576
  });
855
- this.#logMap.set(fileName, instance);
856
- return instance;
857
- }
858
- save(messages, options, config) {
859
- if (!this.#config().history.enable) {
860
- return;
861
- }
862
- try {
863
- const fileName = dayjs().format("YYYY-MM-DD");
864
- const message$ = this.#logMap.get(fileName) ?? this.#createNewListen(fileName);
865
- const item = {
866
- date: dayjs().valueOf(),
867
- messages,
868
- options: omitBy(options, isEmptyInput),
869
- config
870
- };
871
- message$.next(item);
872
- } catch (error) {
873
- try {
874
- this.#config().captureException(error);
875
- } catch (error2) {
876
- }
877
- }
878
- }
879
- };
880
-
881
- // packages/openai/chat/service.ts
882
- import * as v8 from "valibot";
883
- var ChatProviderService = class {
884
- #config = inject2(OpenAIConfigToken);
885
- #chatHistory = inject2(ChatHistoryService);
886
- create(input) {
887
- let options = v8.parse(InputChatOptionsDefine, input);
888
- let instance;
889
- const extraOptions = options.vendorOptions?.[options.vendor]?.["extraOptions"];
890
- options = {
891
- ...extraOptions,
892
- ...options,
893
- apiKey: options.apiKey?.trim() || extraOptions?.apiKey || " "
894
- };
895
- if (!options.vendor || options.vendor === "openai") {
896
- instance = new OpenAIChat(options);
897
- } else if (options.vendor === "gemini") {
898
- instance = new GeminiChat(options);
899
- } else if (options.vendor === "claude") {
900
- instance = new AnthropicChat(options);
901
- } else {
902
- instance = createCompatibleFactory(options);
903
- }
904
- instance.init();
905
- const openAIOptions = this.#config;
906
- const chatHistory = this.#chatHistory;
907
- const fn = async function* (...args) {
908
- const result = instance.stream(args[0], {
909
- ...args[1],
910
- tryPull: openAIOptions().tryPull,
911
- pullModel: openAIOptions().pullModel
912
- });
913
- let content = "";
914
- let isThink = false;
915
- let thinkStart = 0;
916
- let thinkEnd;
917
- let start = true;
918
- let isThinking = false;
919
- let contentEnd;
920
- let lastEmit;
921
- for await (const delta of result) {
922
- content += delta;
923
- if (start) {
924
- start = false;
925
- const result2 = ThinkList.find(
926
- (item) => delta.startsWith(`<${item}>`)
927
- );
928
- if (result2) {
929
- isThink = true;
930
- thinkStart = result2.length + 2;
931
- isThinking = true;
932
- }
933
- } else if (isThink && thinkEnd === void 0) {
934
- const result2 = ThinkList.find((item) => delta === `</${item}>`);
935
- if (result2) {
936
- contentEnd = content.length;
937
- thinkEnd = content.length - result2.length - 3;
938
- isThinking = false;
577
+ let currentStream = createStream();
578
+ const config = extra?.injector?.get(OpenAIConfigToken);
579
+ return (async function* () {
580
+ let hasRetry = false;
581
+ while (true) {
582
+ let retry = false;
583
+ for await (const item of currentStream) {
584
+ if (item.type === "error") {
585
+ if (item.error.errorMessage?.includes("404") && item.error.errorMessage?.includes(
586
+ "no router for requested model"
587
+ ) && !hasRetry) {
588
+ if (config?.().tryPull?.()) {
589
+ await config().pullModel?.(input.model);
590
+ retry = true;
591
+ break;
592
+ }
593
+ }
939
594
  }
595
+ yield item;
940
596
  }
941
- lastEmit = {
942
- content: isThink ? (contentEnd ? content.slice(contentEnd) : "").trim() : content,
943
- isThinking,
944
- delta,
945
- thinkContent: isThink ? content.slice(thinkStart, thinkEnd).trim() : void 0
946
- };
947
- yield lastEmit;
948
- }
949
- const lastMessage = createAssistantMessage(
950
- isThink ? contentEnd ? content.slice(contentEnd) : "" : content
951
- );
952
- if (lastEmit?.thinkContent) {
953
- lastMessage.thinkContent = lastEmit.thinkContent;
954
- }
955
- chatHistory.save(
956
- [...args[0].messages, lastMessage],
957
- { ...args[0], messages: void 0 },
958
- options
959
- );
960
- };
961
- return {
962
- stream: fn,
963
- chat: async (...args) => {
964
- const result = fn(...args);
965
- let obj;
966
- for await (const element of result) {
967
- obj = element;
597
+ if (!retry || hasRetry) {
598
+ break;
968
599
  }
969
- return obj;
970
- },
971
- callTool: (input2, options2) => instance.callTool(input2, options2)
972
- };
973
- }
974
- };
600
+ hasRetry = true;
601
+ currentStream = createStream();
602
+ }
603
+ })();
604
+ };
605
+ }
975
606
 
976
- // packages/openai/chat/module.ts
977
- var OPENAI_MODULE = {
978
- provider: [ChatProviderService, ChatHistoryService],
979
- token: { OpenAIConfigToken }
980
- };
607
+ // packages/openai/index.ts
608
+ import { complete, stream as stream2, fauxAssistantMessage as fauxAssistantMessage2, fauxText as fauxText2, fauxThinking } from "@earendil-works/pi-ai";
609
+
610
+ // packages/openai/chat/util/faux-provider.ts
611
+ import {
612
+ registerFauxProvider,
613
+ fauxAssistantMessage,
614
+ fauxText
615
+ } from "@earendil-works/pi-ai";
616
+ function createDynamicResponse(responseFactory) {
617
+ return async (context, _options, state) => {
618
+ const responses = responseFactory(context, state.callCount);
619
+ const content = Array.isArray(responses) ? responses.map((r) => fauxText(r)) : fauxText(responses);
620
+ return fauxAssistantMessage(content);
621
+ };
622
+ }
623
+ function registerMockProvider(options) {
624
+ const registration = registerFauxProvider(options);
625
+ return {
626
+ model: registration.getModel(),
627
+ setResponses: (responses) => registration.setResponses(responses),
628
+ unregister: () => registration.unregister()
629
+ };
630
+ }
981
631
  export {
632
+ AnthropicMessagesCompatSchema,
982
633
  AssistantChatCompletionContent,
983
634
  AssistantChatMessage,
984
635
  ChatCompletionContentPart,
985
636
  ChatCompletionContentPartImage,
986
637
  ChatCompletionContentPartStr,
987
- ChatHistoryService,
988
- ChatItemDefine,
989
638
  ChatMessageItemDefine,
990
639
  ChatMessageListDefine,
991
- ChatParamsItemDefine,
992
- ChatParamsListDefine,
993
- ChatProviderService,
994
- InputChatOptionsDefine,
640
+ CompatSchema,
641
+ InputChoiceSchema,
642
+ InputSchema,
643
+ ModelConfigDefine,
644
+ ModelSchema,
645
+ ModelThinkingLevelSchema,
995
646
  OPENAI_MODULE,
647
+ OpenAICompletionsCompatSchema,
996
648
  OpenAIConfigToken,
649
+ OpenAIResponsesCompatSchema,
650
+ OutputSchema,
997
651
  SystemChatCompletionContent,
998
652
  SystemChatMessage,
653
+ ThinkingLevelSchema,
999
654
  UserChatCompletionContent,
1000
655
  UserChatMessage,
1001
- VendorOptionsDefine,
656
+ complete,
1002
657
  createAssistantMessage,
658
+ createChatStream,
659
+ createDynamicResponse,
1003
660
  createSystemMessage,
1004
- createUserMessage
661
+ createUserMessage,
662
+ fauxAssistantMessage2 as fauxAssistantMessage,
663
+ fauxText2 as fauxText,
664
+ fauxThinking,
665
+ getModelConfig,
666
+ registerMockProvider,
667
+ stream2 as stream
1005
668
  };
1006
669
  //# sourceMappingURL=index.mjs.map