@yourgpt/llm-sdk 2.1.8 → 2.1.10-alpha.0

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.
Files changed (51) hide show
  1. package/dist/adapters/index.d.mts +38 -4
  2. package/dist/adapters/index.d.ts +38 -4
  3. package/dist/adapters/index.js +318 -8
  4. package/dist/adapters/index.mjs +318 -8
  5. package/dist/{base-iGi9Va6Z.d.ts → base-DN1EfKnE.d.mts} +2 -1
  6. package/dist/{base-D-U61JaB.d.mts → base-DuUNxtVg.d.ts} +2 -1
  7. package/dist/fallback/index.d.mts +4 -4
  8. package/dist/fallback/index.d.ts +4 -4
  9. package/dist/index.d.mts +7 -7
  10. package/dist/index.d.ts +7 -7
  11. package/dist/index.js +43 -23
  12. package/dist/index.mjs +43 -23
  13. package/dist/providers/anthropic/index.d.mts +3 -3
  14. package/dist/providers/anthropic/index.d.ts +3 -3
  15. package/dist/providers/anthropic/index.js +17 -0
  16. package/dist/providers/anthropic/index.mjs +17 -0
  17. package/dist/providers/azure/index.d.mts +3 -3
  18. package/dist/providers/azure/index.d.ts +3 -3
  19. package/dist/providers/fireworks/index.d.mts +1 -1
  20. package/dist/providers/fireworks/index.d.ts +1 -1
  21. package/dist/providers/google/index.d.mts +3 -3
  22. package/dist/providers/google/index.d.ts +3 -3
  23. package/dist/providers/google/index.js +311 -8
  24. package/dist/providers/google/index.mjs +311 -8
  25. package/dist/providers/ollama/index.d.mts +4 -4
  26. package/dist/providers/ollama/index.d.ts +4 -4
  27. package/dist/providers/openai/index.d.mts +3 -3
  28. package/dist/providers/openai/index.d.ts +3 -3
  29. package/dist/providers/openai/index.js +321 -8
  30. package/dist/providers/openai/index.mjs +321 -8
  31. package/dist/providers/openrouter/index.d.mts +7 -3
  32. package/dist/providers/openrouter/index.d.ts +7 -3
  33. package/dist/providers/openrouter/index.js +601 -11
  34. package/dist/providers/openrouter/index.mjs +601 -11
  35. package/dist/providers/togetherai/index.d.mts +61 -2
  36. package/dist/providers/togetherai/index.d.ts +61 -2
  37. package/dist/providers/togetherai/index.js +1030 -2
  38. package/dist/providers/togetherai/index.mjs +1029 -2
  39. package/dist/providers/xai/index.d.mts +3 -3
  40. package/dist/providers/xai/index.d.ts +3 -3
  41. package/dist/providers/xai/index.js +311 -8
  42. package/dist/providers/xai/index.mjs +311 -8
  43. package/dist/{types-D4YfrQJR.d.mts → types-BNCmlJMs.d.mts} +1 -1
  44. package/dist/{types-DRqxMIjF.d.mts → types-CMMQ8s2O.d.mts} +1 -1
  45. package/dist/{types-CR8mi9I0.d.ts → types-CMvvDo-E.d.mts} +12 -1
  46. package/dist/{types-CR8mi9I0.d.mts → types-CMvvDo-E.d.ts} +12 -1
  47. package/dist/{types-BctsnC3g.d.ts → types-DhktekQ3.d.ts} +1 -1
  48. package/dist/{types-38yolWJn.d.ts → types-Pj-vpmoT.d.ts} +1 -1
  49. package/dist/yourgpt/index.d.mts +1 -1
  50. package/dist/yourgpt/index.d.ts +1 -1
  51. package/package.json +1 -1
@@ -21,7 +21,12 @@ function togetherai(modelId, options = {}) {
21
21
  supportsThinking: false,
22
22
  supportsPDF: false,
23
23
  maxTokens: 131072,
24
- supportedImageTypes: ["image/png", "image/jpeg", "image/gif", "image/webp"]
24
+ supportedImageTypes: [
25
+ "image/png",
26
+ "image/jpeg",
27
+ "image/gif",
28
+ "image/webp"
29
+ ]
25
30
  },
26
31
  async doGenerate(params) {
27
32
  const client2 = await getClient();
@@ -197,6 +202,1028 @@ function formatMessages(messages) {
197
202
  });
198
203
  }
199
204
 
200
- export { togetherai as createTogetherAI, togetherai };
205
+ // src/core/utils.ts
206
+ function generateId(prefix = "id") {
207
+ return `${prefix}_${Date.now()}_${Math.random().toString(36).substring(2, 9)}`;
208
+ }
209
+ function generateMessageId() {
210
+ return generateId("msg");
211
+ }
212
+ function generateToolCallId() {
213
+ return generateId("call");
214
+ }
215
+
216
+ // src/adapters/base.ts
217
+ function stringifyForDebug(value) {
218
+ return JSON.stringify(
219
+ value,
220
+ (_key, currentValue) => {
221
+ if (typeof currentValue === "bigint") {
222
+ return currentValue.toString();
223
+ }
224
+ if (currentValue instanceof Error) {
225
+ return {
226
+ name: currentValue.name,
227
+ message: currentValue.message,
228
+ stack: currentValue.stack
229
+ };
230
+ }
231
+ return currentValue;
232
+ },
233
+ 2
234
+ );
235
+ }
236
+ function logProviderPayload(provider, label, payload, enabled) {
237
+ if (!enabled) {
238
+ return;
239
+ }
240
+ if (label.toLowerCase().includes("stream ")) {
241
+ return;
242
+ }
243
+ try {
244
+ console.log(
245
+ `[llm-sdk:${provider}] ${label}
246
+ ${stringifyForDebug(payload)}`
247
+ );
248
+ } catch (error) {
249
+ console.log(
250
+ `[llm-sdk:${provider}] ${label} (failed to stringify payload)`,
251
+ error
252
+ );
253
+ }
254
+ }
255
+ function parameterToJsonSchema(param) {
256
+ const schema = {
257
+ type: param.type
258
+ };
259
+ if (param.description) {
260
+ schema.description = param.description;
261
+ }
262
+ if (param.enum) {
263
+ schema.enum = param.enum;
264
+ }
265
+ if (param.type === "array" && param.items) {
266
+ schema.items = parameterToJsonSchema(
267
+ param.items
268
+ );
269
+ }
270
+ if (param.type === "object" && param.properties) {
271
+ schema.properties = Object.fromEntries(
272
+ Object.entries(param.properties).map(([key, prop]) => [
273
+ key,
274
+ parameterToJsonSchema(
275
+ prop
276
+ )
277
+ ])
278
+ );
279
+ schema.additionalProperties = false;
280
+ }
281
+ return schema;
282
+ }
283
+ function normalizeObjectJsonSchema(schema) {
284
+ if (!schema || typeof schema !== "object") {
285
+ return {
286
+ type: "object",
287
+ properties: {},
288
+ required: [],
289
+ additionalProperties: false
290
+ };
291
+ }
292
+ const normalized = { ...schema };
293
+ const type = normalized.type;
294
+ if (type === "object") {
295
+ const properties = normalized.properties && typeof normalized.properties === "object" && !Array.isArray(normalized.properties) ? normalized.properties : {};
296
+ normalized.properties = Object.fromEntries(
297
+ Object.entries(properties).map(([key, value]) => [
298
+ key,
299
+ normalizeObjectJsonSchema(value)
300
+ ])
301
+ );
302
+ const propertyKeys = Object.keys(properties);
303
+ const required = Array.isArray(normalized.required) ? normalized.required.filter(
304
+ (value) => typeof value === "string"
305
+ ) : [];
306
+ normalized.required = Array.from(/* @__PURE__ */ new Set([...required, ...propertyKeys]));
307
+ if (normalized.additionalProperties === void 0) {
308
+ normalized.additionalProperties = false;
309
+ }
310
+ } else if (type === "array" && normalized.items && typeof normalized.items === "object") {
311
+ normalized.items = normalizeObjectJsonSchema(
312
+ normalized.items
313
+ );
314
+ }
315
+ return normalized;
316
+ }
317
+ function formatTools(actions) {
318
+ return actions.map((action) => ({
319
+ type: "function",
320
+ function: {
321
+ name: action.name,
322
+ description: action.description,
323
+ parameters: {
324
+ type: "object",
325
+ properties: action.parameters ? Object.fromEntries(
326
+ Object.entries(action.parameters).map(([key, param]) => [
327
+ key,
328
+ parameterToJsonSchema(param)
329
+ ])
330
+ ) : {},
331
+ required: action.parameters ? Object.entries(action.parameters).filter(([, param]) => param.required).map(([key]) => key) : [],
332
+ additionalProperties: false
333
+ }
334
+ }
335
+ }));
336
+ }
337
+ function hasImageAttachments(message) {
338
+ const attachments = message.metadata?.attachments;
339
+ return attachments?.some((a) => a.type === "image") ?? false;
340
+ }
341
+ function attachmentToOpenAIImage(attachment) {
342
+ if (attachment.type !== "image") return null;
343
+ let imageUrl;
344
+ if (attachment.url) {
345
+ imageUrl = attachment.url;
346
+ } else if (attachment.data) {
347
+ imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
348
+ } else {
349
+ return null;
350
+ }
351
+ return {
352
+ type: "image_url",
353
+ image_url: {
354
+ url: imageUrl,
355
+ detail: "auto"
356
+ }
357
+ };
358
+ }
359
+ function messageToOpenAIContent(message) {
360
+ const attachments = message.metadata?.attachments;
361
+ const content = message.content ?? "";
362
+ if (!hasImageAttachments(message)) {
363
+ return content;
364
+ }
365
+ const blocks = [];
366
+ if (content) {
367
+ blocks.push({ type: "text", text: content });
368
+ }
369
+ if (attachments) {
370
+ for (const attachment of attachments) {
371
+ const imageBlock = attachmentToOpenAIImage(attachment);
372
+ if (imageBlock) {
373
+ blocks.push(imageBlock);
374
+ }
375
+ }
376
+ }
377
+ return blocks;
378
+ }
379
+ function formatMessagesForOpenAI(messages, systemPrompt) {
380
+ const formatted = [];
381
+ if (systemPrompt) {
382
+ formatted.push({ role: "system", content: systemPrompt });
383
+ }
384
+ for (const msg of messages) {
385
+ if (msg.role === "system") {
386
+ formatted.push({ role: "system", content: msg.content ?? "" });
387
+ } else if (msg.role === "user") {
388
+ formatted.push({
389
+ role: "user",
390
+ content: messageToOpenAIContent(msg)
391
+ });
392
+ } else if (msg.role === "assistant") {
393
+ const hasToolCalls = msg.tool_calls && msg.tool_calls.length > 0;
394
+ const assistantMsg = {
395
+ role: "assistant",
396
+ // Gemini/xAI (OpenAI-compatible) reject content: "" on assistant messages with tool_calls
397
+ content: hasToolCalls ? msg.content || null : msg.content
398
+ };
399
+ if (hasToolCalls) {
400
+ assistantMsg.tool_calls = msg.tool_calls;
401
+ }
402
+ formatted.push(assistantMsg);
403
+ } else if (msg.role === "tool" && msg.tool_call_id) {
404
+ formatted.push({
405
+ role: "tool",
406
+ content: msg.content ?? "",
407
+ tool_call_id: msg.tool_call_id
408
+ });
409
+ }
410
+ }
411
+ return formatted;
412
+ }
413
+
414
+ // src/adapters/openai.ts
415
+ var OpenAIAdapter = class _OpenAIAdapter {
416
+ constructor(config) {
417
+ this.config = config;
418
+ this.model = config.model || "gpt-4o";
419
+ this.provider = _OpenAIAdapter.resolveProviderName(config.baseUrl);
420
+ }
421
+ static resolveProviderName(baseUrl) {
422
+ if (!baseUrl) return "openai";
423
+ if (baseUrl.includes("generativelanguage.googleapis.com")) return "google";
424
+ if (baseUrl.includes("x.ai")) return "xai";
425
+ if (baseUrl.includes("azure")) return "azure";
426
+ if (baseUrl.includes("openrouter.ai")) return "openrouter";
427
+ return "openai";
428
+ }
429
+ async getClient() {
430
+ if (!this.client) {
431
+ const { default: OpenAI } = await import('openai');
432
+ this.client = new OpenAI({
433
+ apiKey: this.config.apiKey,
434
+ baseURL: this.config.baseUrl
435
+ });
436
+ }
437
+ return this.client;
438
+ }
439
+ shouldUseResponsesApi(request) {
440
+ return request.providerToolOptions?.openai?.nativeToolSearch?.enabled === true && request.providerToolOptions.openai.nativeToolSearch.useResponsesApi !== false && Array.isArray(request.toolDefinitions) && request.toolDefinitions.length > 0;
441
+ }
442
+ buildResponsesInput(request) {
443
+ const sourceMessages = request.rawMessages && request.rawMessages.length > 0 ? request.rawMessages : formatMessagesForOpenAI(request.messages, void 0);
444
+ const input = [];
445
+ for (const message of sourceMessages) {
446
+ if (message.role === "system") {
447
+ continue;
448
+ }
449
+ if (message.role === "assistant") {
450
+ const content = typeof message.content === "string" ? message.content : Array.isArray(message.content) ? message.content : message.content ? JSON.stringify(message.content) : "";
451
+ if (content) {
452
+ input.push({
453
+ type: "message",
454
+ role: "assistant",
455
+ content
456
+ });
457
+ }
458
+ const toolCalls = Array.isArray(message.tool_calls) ? message.tool_calls : [];
459
+ for (const toolCall of toolCalls) {
460
+ input.push({
461
+ type: "function_call",
462
+ call_id: toolCall.id,
463
+ name: toolCall.function?.name,
464
+ arguments: toolCall.function?.arguments ?? "{}"
465
+ });
466
+ }
467
+ continue;
468
+ }
469
+ if (message.role === "tool") {
470
+ input.push({
471
+ type: "function_call_output",
472
+ call_id: message.tool_call_id,
473
+ output: typeof message.content === "string" ? message.content : JSON.stringify(message.content ?? null)
474
+ });
475
+ continue;
476
+ }
477
+ input.push({
478
+ type: "message",
479
+ role: message.role === "developer" ? "developer" : "user",
480
+ content: typeof message.content === "string" ? message.content : Array.isArray(message.content) ? message.content : JSON.stringify(message.content ?? "")
481
+ });
482
+ }
483
+ return input;
484
+ }
485
+ buildResponsesTools(tools) {
486
+ const nativeTools = tools.filter((tool) => tool.available !== false).map((tool) => ({
487
+ type: "function",
488
+ name: tool.name,
489
+ description: tool.description,
490
+ parameters: normalizeObjectJsonSchema(
491
+ tool.inputSchema ?? {
492
+ type: "object",
493
+ properties: {},
494
+ required: []
495
+ }
496
+ ),
497
+ strict: true,
498
+ defer_loading: tool.deferLoading === true
499
+ }));
500
+ return [{ type: "tool_search" }, ...nativeTools];
501
+ }
502
+ parseResponsesResult(response) {
503
+ const content = typeof response?.output_text === "string" ? response.output_text : "";
504
+ const toolCalls = Array.isArray(response?.output) ? response.output.filter((item) => item?.type === "function_call").map((item) => ({
505
+ id: item.call_id ?? item.id ?? generateToolCallId(),
506
+ name: item.name,
507
+ args: (() => {
508
+ try {
509
+ return JSON.parse(item.arguments ?? "{}");
510
+ } catch {
511
+ return {};
512
+ }
513
+ })()
514
+ })) : [];
515
+ return {
516
+ content,
517
+ toolCalls,
518
+ usage: response?.usage ? {
519
+ promptTokens: response.usage.input_tokens ?? 0,
520
+ completionTokens: response.usage.output_tokens ?? 0,
521
+ totalTokens: response.usage.total_tokens ?? (response.usage.input_tokens ?? 0) + (response.usage.output_tokens ?? 0)
522
+ } : void 0,
523
+ rawResponse: response
524
+ };
525
+ }
526
+ /**
527
+ * OpenAI reasoning models on OpenRouter (o1/o3/o4/gpt-5 family) hide their
528
+ * reasoning content on the chat-completions endpoint. To surface reasoning
529
+ * SUMMARIES (not raw CoT, which OpenAI never exposes) we have to use the
530
+ * Responses API, which streams `response.reasoning_summary_text.delta` events.
531
+ *
532
+ * Match by prefix on the OpenRouter model id. Excludes openai/gpt-4o,
533
+ * openai/gpt-4.1, openai/chatgpt-* — those continue on chat-completions.
534
+ */
535
+ isOpenAIReasoningModelOnOpenRouter(activeModel) {
536
+ if (this.provider !== "openrouter") return false;
537
+ return activeModel.startsWith("openai/o1") || activeModel.startsWith("openai/o3") || activeModel.startsWith("openai/o4") || activeModel.startsWith("openai/gpt-5");
538
+ }
539
+ /**
540
+ * Convert ActionDefinition[] (the chat-completions tool shape used by the
541
+ * adapter) to the Responses API tool shape.
542
+ */
543
+ buildResponsesToolsFromActions(actions) {
544
+ if (!actions || actions.length === 0) return void 0;
545
+ const formatted = formatTools(actions);
546
+ return formatted.map((t) => ({
547
+ type: "function",
548
+ name: t.function.name,
549
+ description: t.function.description,
550
+ parameters: t.function.parameters
551
+ }));
552
+ }
553
+ /**
554
+ * Streaming Responses API path for OpenAI reasoning models on OpenRouter.
555
+ *
556
+ * Maps Responses API SSE events back to the same StreamEvent shapes the
557
+ * chat-completions path emits, so downstream consumers (processChunk.ts,
558
+ * frontend tool handlers, plan approval, specialist delegations) see
559
+ * identical events regardless of which path produced them.
560
+ *
561
+ * response.reasoning_summary_text.delta → thinking:start (once) + thinking:delta
562
+ * response.output_text.delta → message:delta
563
+ * response.output_item.added (function_call) → action:start (queued buffer)
564
+ * response.function_call_arguments.delta → action:args (progressive)
565
+ * response.output_item.done (function_call) → final action:args + action:end
566
+ * response.completed → message:end + done(usage)
567
+ * response.error → error
568
+ */
569
+ async *streamWithResponsesAPI(request, activeModel, messageId) {
570
+ const client = await this.getClient();
571
+ const maxTokensValue = request.config?.maxTokens ?? this.config.maxTokens;
572
+ const payload = {
573
+ model: activeModel,
574
+ input: this.buildResponsesInput(request),
575
+ stream: true,
576
+ reasoning: {
577
+ effort: request.config?.reasoningEffort ?? "medium",
578
+ summary: "auto"
579
+ }
580
+ };
581
+ if (request.systemPrompt) payload.instructions = request.systemPrompt;
582
+ if (typeof maxTokensValue === "number")
583
+ payload.max_output_tokens = maxTokensValue;
584
+ const tools = this.buildResponsesToolsFromActions(request.actions);
585
+ if (tools && tools.length > 0) payload.tools = tools;
586
+ logProviderPayload(
587
+ "openai",
588
+ "responses-api request payload",
589
+ payload,
590
+ request.debug
591
+ );
592
+ let stream;
593
+ try {
594
+ stream = await client.responses.create(payload);
595
+ } catch (error) {
596
+ yield {
597
+ type: "error",
598
+ message: error instanceof Error ? error.message : "Unknown error",
599
+ code: "OPENAI_RESPONSES_ERROR"
600
+ };
601
+ return;
602
+ }
603
+ const toolBuffers = /* @__PURE__ */ new Map();
604
+ const itemIdToCallId = /* @__PURE__ */ new Map();
605
+ let usage;
606
+ let reasoningStarted = false;
607
+ let textStarted = false;
608
+ let finishEmitted = false;
609
+ const resolveCallId = (evt) => {
610
+ if (evt?.call_id) return evt.call_id;
611
+ if (evt?.item_id) return itemIdToCallId.get(evt.item_id) ?? evt.item_id;
612
+ if (evt?.item?.call_id) return evt.item.call_id;
613
+ if (evt?.item?.id) return evt.item.id;
614
+ return "";
615
+ };
616
+ try {
617
+ for await (const evt of stream) {
618
+ logProviderPayload(
619
+ "openai",
620
+ "responses-api stream chunk",
621
+ evt,
622
+ request.debug
623
+ );
624
+ if (request.signal?.aborted) break;
625
+ const t = evt?.type ?? "";
626
+ if (t === "response.reasoning_summary_text.delta") {
627
+ const delta = evt.delta ?? "";
628
+ if (!delta) continue;
629
+ if (!reasoningStarted) {
630
+ yield { type: "thinking:start" };
631
+ reasoningStarted = true;
632
+ }
633
+ yield { type: "thinking:delta", content: delta };
634
+ continue;
635
+ }
636
+ if (t === "response.reasoning_summary_text.done" || t === "response.reasoning.done") {
637
+ continue;
638
+ }
639
+ if (t === "response.output_text.delta") {
640
+ const text = evt.delta ?? "";
641
+ if (!text) continue;
642
+ if (reasoningStarted && !textStarted) {
643
+ yield { type: "thinking:end" };
644
+ textStarted = true;
645
+ }
646
+ yield { type: "message:delta", content: text };
647
+ continue;
648
+ }
649
+ if (t === "response.output_item.added") {
650
+ const item = evt.item;
651
+ if (item?.type === "function_call") {
652
+ const callId = item.call_id ?? item.id ?? "";
653
+ const itemId = item.id ?? callId;
654
+ if (callId) {
655
+ if (itemId && itemId !== callId) {
656
+ itemIdToCallId.set(itemId, callId);
657
+ }
658
+ if (!toolBuffers.has(callId)) {
659
+ toolBuffers.set(callId, {
660
+ id: callId,
661
+ name: item.name ?? "",
662
+ arguments: item.arguments ?? "",
663
+ emittedStart: false
664
+ });
665
+ }
666
+ const buf = toolBuffers.get(callId);
667
+ if (buf.name && !buf.emittedStart) {
668
+ yield { type: "action:start", id: buf.id, name: buf.name };
669
+ buf.emittedStart = true;
670
+ }
671
+ }
672
+ }
673
+ continue;
674
+ }
675
+ if (t === "response.function_call_arguments.delta") {
676
+ const callId = resolveCallId(evt);
677
+ const delta = evt.delta ?? "";
678
+ if (!callId || !delta) continue;
679
+ let buf = toolBuffers.get(callId);
680
+ if (!buf) {
681
+ buf = { id: callId, name: "", arguments: "", emittedStart: false };
682
+ toolBuffers.set(callId, buf);
683
+ }
684
+ buf.arguments += delta;
685
+ if (buf.emittedStart) {
686
+ yield {
687
+ type: "action:args",
688
+ id: buf.id,
689
+ args: buf.arguments
690
+ };
691
+ }
692
+ continue;
693
+ }
694
+ if (t === "response.output_item.done") {
695
+ const item = evt.item;
696
+ if (item?.type === "function_call") {
697
+ const callId = item.call_id ?? item.id ?? "";
698
+ const buf = toolBuffers.get(callId);
699
+ const name = buf?.name || item.name || "";
700
+ const argsStr = buf?.arguments || item.arguments || "{}";
701
+ if (callId && name) {
702
+ if (!buf?.emittedStart) {
703
+ yield { type: "action:start", id: callId, name };
704
+ }
705
+ yield {
706
+ type: "action:args",
707
+ id: callId,
708
+ args: argsStr
709
+ };
710
+ yield {
711
+ type: "action:end",
712
+ id: callId,
713
+ name
714
+ };
715
+ }
716
+ toolBuffers.delete(callId);
717
+ }
718
+ continue;
719
+ }
720
+ if (t === "response.completed") {
721
+ const u = evt.response?.usage;
722
+ if (u) {
723
+ usage = {
724
+ prompt_tokens: u.input_tokens ?? 0,
725
+ completion_tokens: u.output_tokens ?? 0,
726
+ total_tokens: u.total_tokens ?? (u.input_tokens ?? 0) + (u.output_tokens ?? 0)
727
+ };
728
+ }
729
+ for (const buf of toolBuffers.values()) {
730
+ if (!buf.id || !buf.name) continue;
731
+ if (!buf.emittedStart) {
732
+ yield { type: "action:start", id: buf.id, name: buf.name };
733
+ }
734
+ yield {
735
+ type: "action:args",
736
+ id: buf.id,
737
+ args: buf.arguments || "{}"
738
+ };
739
+ yield { type: "action:end", id: buf.id, name: buf.name };
740
+ }
741
+ toolBuffers.clear();
742
+ if (reasoningStarted && !textStarted) {
743
+ yield { type: "thinking:end" };
744
+ }
745
+ yield { type: "message:end" };
746
+ yield { type: "done", usage };
747
+ finishEmitted = true;
748
+ continue;
749
+ }
750
+ if (t === "response.error" || t === "error") {
751
+ const msg = evt.error?.message || evt.message || "Responses API error";
752
+ yield {
753
+ type: "error",
754
+ message: msg,
755
+ code: "OPENAI_RESPONSES_ERROR"
756
+ };
757
+ return;
758
+ }
759
+ }
760
+ } catch (error) {
761
+ yield {
762
+ type: "error",
763
+ message: error instanceof Error ? error.message : "Unknown error",
764
+ code: "OPENAI_RESPONSES_ERROR"
765
+ };
766
+ return;
767
+ }
768
+ if (!finishEmitted) {
769
+ if (reasoningStarted && !textStarted) {
770
+ yield { type: "thinking:end" };
771
+ }
772
+ yield { type: "message:end" };
773
+ yield { type: "done", usage };
774
+ }
775
+ }
776
+ async completeWithResponses(request) {
777
+ const client = await this.getClient();
778
+ const openaiToolOptions = request.providerToolOptions?.openai;
779
+ const payload = {
780
+ model: request.config?.model || this.model,
781
+ instructions: request.systemPrompt,
782
+ input: this.buildResponsesInput(request),
783
+ tools: this.buildResponsesTools(request.toolDefinitions ?? []),
784
+ tool_choice: openaiToolOptions?.toolChoice === "required" ? "required" : openaiToolOptions?.toolChoice === "auto" ? "auto" : void 0,
785
+ parallel_tool_calls: openaiToolOptions?.parallelToolCalls,
786
+ temperature: request.config?.temperature ?? this.config.temperature,
787
+ max_output_tokens: request.config?.maxTokens ?? this.config.maxTokens,
788
+ stream: false
789
+ };
790
+ logProviderPayload("openai", "request payload", payload, request.debug);
791
+ const response = await client.responses.create(payload);
792
+ logProviderPayload("openai", "response payload", response, request.debug);
793
+ return this.parseResponsesResult(response);
794
+ }
795
+ async *stream(request) {
796
+ if (this.shouldUseResponsesApi(request)) {
797
+ const messageId2 = generateMessageId();
798
+ yield { type: "message:start", id: messageId2 };
799
+ try {
800
+ const result = await this.completeWithResponses(request);
801
+ if (result.content) {
802
+ yield { type: "message:delta", content: result.content };
803
+ }
804
+ for (const toolCall of result.toolCalls) {
805
+ yield {
806
+ type: "action:start",
807
+ id: toolCall.id,
808
+ name: toolCall.name
809
+ };
810
+ yield {
811
+ type: "action:args",
812
+ id: toolCall.id,
813
+ args: JSON.stringify(toolCall.args)
814
+ };
815
+ }
816
+ yield { type: "message:end" };
817
+ yield {
818
+ type: "done",
819
+ usage: result.usage ? {
820
+ prompt_tokens: result.usage.promptTokens,
821
+ completion_tokens: result.usage.completionTokens,
822
+ total_tokens: result.usage.totalTokens
823
+ } : void 0
824
+ };
825
+ return;
826
+ } catch (error) {
827
+ yield {
828
+ type: "error",
829
+ message: error instanceof Error ? error.message : "Unknown error",
830
+ code: "OPENAI_RESPONSES_ERROR"
831
+ };
832
+ return;
833
+ }
834
+ }
835
+ const client = await this.getClient();
836
+ let messages;
837
+ if (request.rawMessages && request.rawMessages.length > 0) {
838
+ const processedMessages = request.rawMessages.map((msg) => {
839
+ if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0 && msg.content === "") {
840
+ return { ...msg, content: null };
841
+ }
842
+ const hasAttachments = msg.attachments && Array.isArray(msg.attachments) && msg.attachments.length > 0;
843
+ if (hasAttachments) {
844
+ const content = [];
845
+ if (msg.content) {
846
+ content.push({ type: "text", text: msg.content });
847
+ }
848
+ for (const attachment of msg.attachments) {
849
+ if (attachment.type === "image") {
850
+ let imageUrl;
851
+ if (attachment.url) {
852
+ imageUrl = attachment.url;
853
+ } else if (attachment.data) {
854
+ imageUrl = attachment.data.startsWith("data:") ? attachment.data : `data:${attachment.mimeType || "image/png"};base64,${attachment.data}`;
855
+ } else {
856
+ continue;
857
+ }
858
+ content.push({
859
+ type: "image_url",
860
+ image_url: { url: imageUrl, detail: "auto" }
861
+ });
862
+ }
863
+ }
864
+ return { ...msg, content, attachments: void 0 };
865
+ }
866
+ return msg;
867
+ });
868
+ if (request.systemPrompt) {
869
+ const hasSystem = processedMessages.some((m) => m.role === "system");
870
+ if (!hasSystem) {
871
+ messages = [
872
+ { role: "system", content: request.systemPrompt },
873
+ ...processedMessages
874
+ ];
875
+ } else {
876
+ messages = processedMessages;
877
+ }
878
+ } else {
879
+ messages = processedMessages;
880
+ }
881
+ } else {
882
+ messages = formatMessagesForOpenAI(
883
+ request.messages,
884
+ request.systemPrompt
885
+ );
886
+ }
887
+ const tools = request.actions?.length ? formatTools(request.actions) : [];
888
+ const webSearchConfig = request.webSearch ?? this.config.webSearch;
889
+ if (webSearchConfig) {
890
+ const webSearchTool = {
891
+ type: "web_search_preview"
892
+ };
893
+ const wsConfig = typeof webSearchConfig === "object" ? webSearchConfig : {};
894
+ if (wsConfig.userLocation) {
895
+ webSearchTool.search_context_size = "medium";
896
+ }
897
+ tools.push(webSearchTool);
898
+ }
899
+ const messageId = generateMessageId();
900
+ yield { type: "message:start", id: messageId };
901
+ try {
902
+ const openaiToolOptions = request.providerToolOptions?.openai;
903
+ const toolChoice = openaiToolOptions?.toolChoice && typeof openaiToolOptions.toolChoice === "object" ? {
904
+ type: "function",
905
+ function: {
906
+ name: openaiToolOptions.toolChoice.name
907
+ }
908
+ } : openaiToolOptions?.toolChoice;
909
+ const isOpenRouter = this.provider === "openrouter";
910
+ const activeModel = request.config?.model || this.model;
911
+ const modelSlug = activeModel.replace("openai/", "");
912
+ const isOSeries = /^o[1-9]/.test(modelSlug);
913
+ const isOpenAIOnOpenRouter = isOpenRouter && activeModel.startsWith("openai/");
914
+ if (!this.config.disableThinking && this.isOpenAIReasoningModelOnOpenRouter(activeModel)) {
915
+ yield* this.streamWithResponsesAPI(request, activeModel, messageId);
916
+ return;
917
+ }
918
+ const maxTokensValue = request.config?.maxTokens ?? this.config.maxTokens;
919
+ const payload = {
920
+ model: activeModel,
921
+ messages,
922
+ tools: tools.length > 0 ? tools : void 0,
923
+ tool_choice: tools.length > 0 ? toolChoice : void 0,
924
+ parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
925
+ stream: true,
926
+ stream_options: { include_usage: true },
927
+ // o-series: use max_completion_tokens + reasoning_effort, no temperature
928
+ // regular models: use max_tokens + temperature
929
+ ...isOSeries ? {
930
+ max_completion_tokens: maxTokensValue,
931
+ reasoning_effort: request.config?.reasoningEffort ?? "medium"
932
+ } : {
933
+ temperature: request.config?.temperature ?? this.config.temperature,
934
+ max_tokens: maxTokensValue
935
+ },
936
+ // Non-OpenAI OpenRouter models support OR's reasoning/include_reasoning params.
937
+ // When disableThinking=true we must explicitly send include_reasoning:false because
938
+ // models like Qwen3 and DeepSeek-R1 reason by default even without the reasoning param.
939
+ ...isOpenRouter && !isOpenAIOnOpenRouter ? this.config.disableThinking ? { include_reasoning: false } : { reasoning: { max_tokens: 8e3 }, include_reasoning: true } : {}
940
+ };
941
+ logProviderPayload("openai", "request payload", payload, request.debug);
942
+ const stream = await client.chat.completions.create(payload);
943
+ let currentToolCall = null;
944
+ const collectedCitations = [];
945
+ let citationIndex = 0;
946
+ let usage;
947
+ let adapterReasoningStarted = false;
948
+ for await (const chunk of stream) {
949
+ logProviderPayload("openai", "stream chunk", chunk, request.debug);
950
+ if (request.signal?.aborted) {
951
+ break;
952
+ }
953
+ const delta = chunk.choices[0]?.delta;
954
+ const choice = chunk.choices[0];
955
+ if (delta?.content) {
956
+ yield { type: "message:delta", content: delta.content };
957
+ }
958
+ if (isOpenRouter) {
959
+ const rc = delta?.reasoning_content ?? delta?.reasoning ?? null;
960
+ if (rc) {
961
+ const rcText = typeof rc === "string" ? rc : Array.isArray(rc) && rc[0]?.text ? rc[0].text : "";
962
+ if (rcText) {
963
+ if (!adapterReasoningStarted) {
964
+ yield { type: "thinking:start" };
965
+ adapterReasoningStarted = true;
966
+ }
967
+ yield { type: "thinking:delta", content: rcText };
968
+ }
969
+ } else if (adapterReasoningStarted && (delta?.content || choice?.finish_reason)) {
970
+ yield { type: "thinking:end" };
971
+ adapterReasoningStarted = false;
972
+ }
973
+ }
974
+ const annotations = delta?.annotations;
975
+ if (annotations && annotations.length > 0) {
976
+ for (const annotation of annotations) {
977
+ if (annotation.type === "url_citation" && annotation.url_citation?.url) {
978
+ citationIndex++;
979
+ const url = annotation.url_citation.url;
980
+ const domain = extractDomain(url);
981
+ collectedCitations.push({
982
+ index: citationIndex,
983
+ url,
984
+ title: annotation.url_citation.title || domain,
985
+ domain,
986
+ favicon: domain ? `https://www.google.com/s2/favicons?domain=${domain}&sz=32` : void 0
987
+ });
988
+ }
989
+ }
990
+ }
991
+ if (delta?.tool_calls) {
992
+ for (const toolCall of delta.tool_calls) {
993
+ if (toolCall.id) {
994
+ if (currentToolCall) {
995
+ yield {
996
+ type: "action:args",
997
+ id: currentToolCall.id,
998
+ args: currentToolCall.arguments
999
+ };
1000
+ yield {
1001
+ type: "action:end",
1002
+ id: currentToolCall.id,
1003
+ name: currentToolCall.name
1004
+ };
1005
+ }
1006
+ const tcExtraContent = toolCall.extra_content;
1007
+ currentToolCall = {
1008
+ id: toolCall.id,
1009
+ name: toolCall.function?.name || "",
1010
+ arguments: toolCall.function?.arguments || "",
1011
+ ...tcExtraContent ? { extra_content: tcExtraContent } : {}
1012
+ };
1013
+ yield {
1014
+ type: "action:start",
1015
+ id: currentToolCall.id,
1016
+ name: currentToolCall.name,
1017
+ ...currentToolCall.extra_content ? { extra_content: currentToolCall.extra_content } : {}
1018
+ };
1019
+ } else if (currentToolCall && toolCall.function?.arguments) {
1020
+ currentToolCall.arguments += toolCall.function.arguments;
1021
+ yield {
1022
+ type: "action:args",
1023
+ id: currentToolCall.id,
1024
+ args: currentToolCall.arguments
1025
+ };
1026
+ }
1027
+ }
1028
+ }
1029
+ if (chunk.usage) {
1030
+ usage = {
1031
+ prompt_tokens: chunk.usage.prompt_tokens,
1032
+ completion_tokens: chunk.usage.completion_tokens,
1033
+ total_tokens: chunk.usage.total_tokens
1034
+ };
1035
+ }
1036
+ if (choice?.finish_reason) {
1037
+ if (currentToolCall) {
1038
+ yield {
1039
+ type: "action:args",
1040
+ id: currentToolCall.id,
1041
+ args: currentToolCall.arguments
1042
+ };
1043
+ yield {
1044
+ type: "action:end",
1045
+ id: currentToolCall.id,
1046
+ name: currentToolCall.name
1047
+ };
1048
+ currentToolCall = null;
1049
+ }
1050
+ }
1051
+ }
1052
+ if (collectedCitations.length > 0) {
1053
+ const uniqueCitations = deduplicateCitations(collectedCitations);
1054
+ yield { type: "citation", citations: uniqueCitations };
1055
+ }
1056
+ yield { type: "message:end" };
1057
+ yield { type: "done", usage };
1058
+ } catch (error) {
1059
+ yield {
1060
+ type: "error",
1061
+ message: error instanceof Error ? error.message : "Unknown error",
1062
+ code: `${this.provider.toUpperCase()}_ERROR`
1063
+ };
1064
+ }
1065
+ }
1066
+ async complete(request) {
1067
+ if (this.shouldUseResponsesApi(request)) {
1068
+ return this.completeWithResponses(request);
1069
+ }
1070
+ const client = await this.getClient();
1071
+ let messages;
1072
+ if (request.rawMessages && request.rawMessages.length > 0) {
1073
+ const sanitized = request.rawMessages.map((msg) => {
1074
+ if (msg.role === "assistant" && Array.isArray(msg.tool_calls) && msg.tool_calls.length > 0 && msg.content === "") {
1075
+ return { ...msg, content: null };
1076
+ }
1077
+ return msg;
1078
+ });
1079
+ if (request.systemPrompt && !sanitized.some((message2) => message2.role === "system")) {
1080
+ messages = [
1081
+ { role: "system", content: request.systemPrompt },
1082
+ ...sanitized
1083
+ ];
1084
+ } else {
1085
+ messages = sanitized;
1086
+ }
1087
+ } else {
1088
+ messages = formatMessagesForOpenAI(
1089
+ request.messages,
1090
+ request.systemPrompt
1091
+ );
1092
+ }
1093
+ const tools = request.actions?.length ? formatTools(request.actions) : [];
1094
+ const openaiToolOptions = request.providerToolOptions?.openai;
1095
+ const toolChoice = openaiToolOptions?.toolChoice && typeof openaiToolOptions.toolChoice === "object" ? {
1096
+ type: "function",
1097
+ function: {
1098
+ name: openaiToolOptions.toolChoice.name
1099
+ }
1100
+ } : openaiToolOptions?.toolChoice;
1101
+ const activeModel2 = request.config?.model || this.model;
1102
+ const modelSlug2 = activeModel2.replace("openai/", "");
1103
+ const isOSeries2 = /^o[1-9]/.test(modelSlug2);
1104
+ const maxTokensValue2 = request.config?.maxTokens ?? this.config.maxTokens;
1105
+ const payload = {
1106
+ model: activeModel2,
1107
+ messages,
1108
+ tools: tools.length > 0 ? tools : void 0,
1109
+ tool_choice: tools.length > 0 ? toolChoice : void 0,
1110
+ parallel_tool_calls: tools.length > 0 ? openaiToolOptions?.parallelToolCalls : void 0,
1111
+ stream: false,
1112
+ ...isOSeries2 ? {
1113
+ max_completion_tokens: maxTokensValue2,
1114
+ reasoning_effort: request.config?.reasoningEffort ?? "medium"
1115
+ } : {
1116
+ temperature: request.config?.temperature ?? this.config.temperature,
1117
+ max_tokens: maxTokensValue2
1118
+ }
1119
+ };
1120
+ logProviderPayload("openai", "request payload", payload, request.debug);
1121
+ const response = await client.chat.completions.create(payload);
1122
+ logProviderPayload("openai", "response payload", response, request.debug);
1123
+ const choice = response.choices?.[0];
1124
+ const message = choice?.message;
1125
+ return {
1126
+ content: message?.content ?? "",
1127
+ toolCalls: message?.tool_calls?.map((toolCall) => ({
1128
+ id: toolCall.id ?? generateToolCallId(),
1129
+ name: toolCall.function?.name ?? "",
1130
+ args: (() => {
1131
+ try {
1132
+ return JSON.parse(toolCall.function?.arguments ?? "{}");
1133
+ } catch {
1134
+ return {};
1135
+ }
1136
+ })(),
1137
+ ...toolCall.extra_content ? { extra_content: toolCall.extra_content } : {}
1138
+ })) ?? [],
1139
+ usage: response.usage ? {
1140
+ promptTokens: response.usage.prompt_tokens,
1141
+ completionTokens: response.usage.completion_tokens,
1142
+ totalTokens: response.usage.total_tokens
1143
+ } : void 0,
1144
+ rawResponse: response
1145
+ };
1146
+ }
1147
+ };
1148
+ function extractDomain(url) {
1149
+ try {
1150
+ const parsed = new URL(url);
1151
+ return parsed.hostname;
1152
+ } catch {
1153
+ return "";
1154
+ }
1155
+ }
1156
+ function deduplicateCitations(citations) {
1157
+ const seen = /* @__PURE__ */ new Map();
1158
+ let index = 0;
1159
+ for (const citation of citations) {
1160
+ if (!seen.has(citation.url)) {
1161
+ index++;
1162
+ seen.set(citation.url, { ...citation, index });
1163
+ }
1164
+ }
1165
+ return Array.from(seen.values());
1166
+ }
1167
+ function createOpenAIAdapter(config) {
1168
+ return new OpenAIAdapter(config);
1169
+ }
1170
+
1171
+ // src/providers/types.ts
1172
+ function createCallableProvider(providerFn, properties) {
1173
+ Object.defineProperty(providerFn, "name", {
1174
+ value: properties.name,
1175
+ writable: false,
1176
+ configurable: true
1177
+ });
1178
+ Object.assign(providerFn, {
1179
+ supportedModels: properties.supportedModels,
1180
+ languageModel: providerFn,
1181
+ getCapabilities: properties.getCapabilities,
1182
+ embeddingModel: properties.embeddingModel
1183
+ });
1184
+ return providerFn;
1185
+ }
1186
+
1187
+ // src/providers/togetherai/index.ts
1188
+ var DEFAULT_CAPABILITIES = {
1189
+ vision: true,
1190
+ tools: true,
1191
+ jsonMode: true,
1192
+ maxTokens: 131072
1193
+ };
1194
+ function createTogetherAI(config = {}) {
1195
+ const apiKey = config.apiKey ?? process.env.TOGETHER_API_KEY ?? "";
1196
+ const baseUrl = config.baseUrl ?? "https://api.together.xyz/v1";
1197
+ const providerFn = (modelId) => {
1198
+ return createOpenAIAdapter({
1199
+ apiKey,
1200
+ model: modelId,
1201
+ baseUrl
1202
+ });
1203
+ };
1204
+ const getCapabilities = (_modelId) => {
1205
+ return {
1206
+ supportsVision: DEFAULT_CAPABILITIES.vision,
1207
+ supportsTools: DEFAULT_CAPABILITIES.tools,
1208
+ supportsThinking: false,
1209
+ supportsStreaming: true,
1210
+ supportsPDF: false,
1211
+ supportsAudio: false,
1212
+ supportsVideo: false,
1213
+ maxTokens: DEFAULT_CAPABILITIES.maxTokens,
1214
+ supportedImageTypes: ["image/png", "image/jpeg", "image/gif", "image/webp"] ,
1215
+ supportsJsonMode: DEFAULT_CAPABILITIES.jsonMode,
1216
+ supportsSystemMessages: true
1217
+ };
1218
+ };
1219
+ return createCallableProvider(providerFn, {
1220
+ name: "togetherai",
1221
+ supportedModels: [],
1222
+ getCapabilities
1223
+ });
1224
+ }
1225
+ var createTogetherAIProvider = createTogetherAI;
1226
+
1227
+ export { createTogetherAI, createTogetherAIProvider, togetherai };
201
1228
  //# sourceMappingURL=index.mjs.map
202
1229
  //# sourceMappingURL=index.mjs.map