@yourgpt/llm-sdk 2.6.0-beta.0 → 2.6.0-beta.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.
Files changed (55) hide show
  1. package/README.md +77 -0
  2. package/dist/adapters/index.d.mts +11 -13
  3. package/dist/adapters/index.d.ts +11 -13
  4. package/dist/adapters/index.js +255 -164
  5. package/dist/adapters/index.mjs +255 -164
  6. package/dist/{base-C58Dsr9p.d.ts → base-BdTpUAWI.d.ts} +2 -1
  7. package/dist/{base-tNgbBaSo.d.mts → base-CkeT7u-O.d.mts} +2 -1
  8. package/dist/fallback/index.d.mts +4 -4
  9. package/dist/fallback/index.d.ts +4 -4
  10. package/dist/fallback/index.js +75 -39
  11. package/dist/fallback/index.mjs +75 -39
  12. package/dist/index.d.mts +8 -7
  13. package/dist/index.d.ts +8 -7
  14. package/dist/index.js +75 -28
  15. package/dist/index.mjs +75 -28
  16. package/dist/providers/anthropic/index.d.mts +3 -3
  17. package/dist/providers/anthropic/index.d.ts +3 -3
  18. package/dist/providers/azure/index.d.mts +3 -3
  19. package/dist/providers/azure/index.d.ts +3 -3
  20. package/dist/providers/fireworks/index.d.mts +3 -3
  21. package/dist/providers/fireworks/index.d.ts +3 -3
  22. package/dist/providers/fireworks/index.js +255 -164
  23. package/dist/providers/fireworks/index.mjs +255 -164
  24. package/dist/providers/google/index.d.mts +3 -3
  25. package/dist/providers/google/index.d.ts +3 -3
  26. package/dist/providers/google/index.js +255 -164
  27. package/dist/providers/google/index.mjs +255 -164
  28. package/dist/providers/ollama/index.d.mts +4 -4
  29. package/dist/providers/ollama/index.d.ts +4 -4
  30. package/dist/providers/openai/index.d.mts +6 -23
  31. package/dist/providers/openai/index.d.ts +6 -23
  32. package/dist/providers/openai/index.js +656 -450
  33. package/dist/providers/openai/index.mjs +656 -450
  34. package/dist/providers/openrouter/index.d.mts +3 -3
  35. package/dist/providers/openrouter/index.d.ts +3 -3
  36. package/dist/providers/openrouter/index.js +255 -164
  37. package/dist/providers/openrouter/index.mjs +255 -164
  38. package/dist/providers/togetherai/index.d.mts +3 -3
  39. package/dist/providers/togetherai/index.d.ts +3 -3
  40. package/dist/providers/togetherai/index.js +255 -164
  41. package/dist/providers/togetherai/index.mjs +255 -164
  42. package/dist/providers/xai/index.d.mts +3 -3
  43. package/dist/providers/xai/index.d.ts +3 -3
  44. package/dist/providers/xai/index.js +255 -164
  45. package/dist/providers/xai/index.mjs +255 -164
  46. package/dist/{types-BkQCSiIt.d.mts → types-BW3csYo5.d.mts} +22 -1
  47. package/dist/{types-BkQCSiIt.d.ts → types-BW3csYo5.d.ts} +22 -1
  48. package/dist/{types-B6dhnguR.d.mts → types-BWRYIvl_.d.mts} +11 -4
  49. package/dist/{types-CCxPmkmK.d.ts → types-CpXcT-tY.d.ts} +11 -4
  50. package/dist/{types-BQ31QIsA.d.ts → types-DA54bSmX.d.ts} +3 -11
  51. package/dist/{types-BSSiJW2o.d.mts → types-NOswlhm9.d.mts} +3 -11
  52. package/dist/yourgpt/index.d.mts +1 -1
  53. package/dist/yourgpt/index.d.ts +1 -1
  54. package/package.json +115 -48
  55. package/LICENSE +0 -21
package/dist/index.js CHANGED
@@ -132,6 +132,14 @@ function formatToolsForGoogle(tools) {
132
132
  ];
133
133
  }
134
134
 
135
+ // src/core/provider-error.ts
136
+ function throwIfAborted(signal) {
137
+ if (!signal?.aborted) return;
138
+ const error = new Error("The operation was aborted");
139
+ error.name = "AbortError";
140
+ throw error;
141
+ }
142
+
135
143
  // src/core/generate-text.ts
136
144
  async function generateText(params) {
137
145
  const { model, tools, maxSteps = 1, signal } = params;
@@ -145,22 +153,23 @@ async function generateText(params) {
145
153
  const allToolCalls = [];
146
154
  const allToolResults = [];
147
155
  for (let step = 0; step < maxSteps; step++) {
148
- if (signal?.aborted) {
149
- throw new Error("Generation aborted");
150
- }
156
+ throwIfAborted(signal);
151
157
  const formattedTools = tools ? formatToolsForProvider(tools, model.provider) : void 0;
152
158
  const result = await model.doGenerate({
153
159
  messages,
154
160
  tools: formattedTools,
155
161
  temperature: params.temperature,
162
+ reasoningEffort: params.reasoningEffort,
156
163
  maxTokens: params.maxTokens,
157
164
  responseFormat: params.responseFormat,
158
165
  signal
159
166
  });
167
+ throwIfAborted(signal);
160
168
  const stepToolResults = [];
161
169
  if (result.toolCalls && result.toolCalls.length > 0 && tools) {
162
170
  allToolCalls.push(...result.toolCalls);
163
171
  for (const call of result.toolCalls) {
172
+ throwIfAborted(signal);
164
173
  const toolDef = tools[call.name];
165
174
  if (!toolDef) {
166
175
  const errorResult = {
@@ -178,6 +187,7 @@ async function generateText(params) {
178
187
  abortSignal: signal,
179
188
  messages
180
189
  });
190
+ throwIfAborted(signal);
181
191
  const result2 = {
182
192
  toolCallId: call.id,
183
193
  result: toolResult
@@ -185,6 +195,7 @@ async function generateText(params) {
185
195
  stepToolResults.push(result2);
186
196
  allToolResults.push(result2);
187
197
  } catch (error) {
198
+ throwIfAborted(signal);
188
199
  const errorResult = {
189
200
  toolCallId: call.id,
190
201
  result: {
@@ -301,10 +312,12 @@ async function streamText(params) {
301
312
  messages,
302
313
  tools: formattedTools,
303
314
  temperature: params.temperature,
315
+ reasoningEffort: params.reasoningEffort,
304
316
  maxTokens: params.maxTokens,
305
317
  responseFormat: params.responseFormat,
306
318
  signal
307
319
  })) {
320
+ throwIfAborted(signal);
308
321
  switch (chunk.type) {
309
322
  case "text-delta":
310
323
  stepText += chunk.text;
@@ -352,6 +365,7 @@ async function streamText(params) {
352
365
  };
353
366
  messages = [...messages, assistantMessage];
354
367
  for (const call of toolCalls) {
368
+ throwIfAborted(signal);
355
369
  const toolDef = tools[call.name];
356
370
  if (!toolDef) {
357
371
  const errorResult = { error: `Tool not found: ${call.name}` };
@@ -377,6 +391,7 @@ async function streamText(params) {
377
391
  abortSignal: signal,
378
392
  messages
379
393
  });
394
+ throwIfAborted(signal);
380
395
  yield { type: "tool-result", toolCallId: call.id, result };
381
396
  messages = [
382
397
  ...messages,
@@ -387,6 +402,7 @@ async function streamText(params) {
387
402
  }
388
403
  ];
389
404
  } catch (error) {
405
+ throwIfAborted(signal);
390
406
  const errorResult = {
391
407
  error: error instanceof Error ? error.message : "Tool execution failed"
392
408
  };
@@ -412,40 +428,63 @@ async function streamText(params) {
412
428
  usage: finalUsage
413
429
  };
414
430
  }
415
- async function* createTextStream() {
416
- for await (const part of createFullStream()) {
417
- if (part.type === "text-delta") {
418
- yield part.text;
419
- }
431
+ const source = createFullStream();
432
+ const parts = [];
433
+ let finished = false;
434
+ let failure;
435
+ let pending;
436
+ async function advance() {
437
+ if (!pending) {
438
+ pending = (async () => {
439
+ try {
440
+ const next = await source.next();
441
+ if (next.done) finished = true;
442
+ else parts.push(next.value);
443
+ } catch (error) {
444
+ failure = error;
445
+ finished = true;
446
+ }
447
+ })();
448
+ }
449
+ const current = pending;
450
+ await current;
451
+ if (pending === current) pending = void 0;
452
+ }
453
+ async function* sharedStream() {
454
+ let index = 0;
455
+ while (true) {
456
+ if (index < parts.length) yield parts[index++];
457
+ else if (finished) {
458
+ if (failure) throw failure;
459
+ return;
460
+ } else await advance();
420
461
  }
421
462
  }
422
- let textPromise;
423
- let usagePromise;
424
- let finishReasonPromise;
425
- async function consumeStream() {
426
- for await (const _ of createFullStream()) {
463
+ async function* createTextStream() {
464
+ for await (const part of sharedStream()) {
465
+ if (part.type === "error") throw part.error;
466
+ if (part.type === "text-delta") yield part.text;
427
467
  }
428
468
  }
469
+ let completion;
470
+ function consumeStream() {
471
+ return completion ?? (completion = (async () => {
472
+ for await (const part of sharedStream()) {
473
+ if (part.type === "error") throw part.error;
474
+ }
475
+ })());
476
+ }
429
477
  return {
430
478
  textStream: createTextStream(),
431
- fullStream: createFullStream(),
479
+ fullStream: sharedStream(),
432
480
  get text() {
433
- if (!textPromise) {
434
- textPromise = consumeStream().then(() => fullText);
435
- }
436
- return textPromise;
481
+ return consumeStream().then(() => fullText);
437
482
  },
438
483
  get usage() {
439
- if (!usagePromise) {
440
- usagePromise = consumeStream().then(() => finalUsage);
441
- }
442
- return usagePromise;
484
+ return consumeStream().then(() => finalUsage);
443
485
  },
444
486
  get finishReason() {
445
- if (!finishReasonPromise) {
446
- finishReasonPromise = consumeStream().then(() => finalFinishReason);
447
- }
448
- return finishReasonPromise;
487
+ return consumeStream().then(() => finalFinishReason);
449
488
  },
450
489
  toTextStreamResponse(options) {
451
490
  const stream = createTextStreamReadable(createTextStream());
@@ -460,7 +499,7 @@ async function streamText(params) {
460
499
  });
461
500
  },
462
501
  toDataStreamResponse(options) {
463
- const stream = createDataStreamReadable(createFullStream());
502
+ const stream = createDataStreamReadable(sharedStream());
464
503
  return new Response(stream, {
465
504
  status: options?.status ?? 200,
466
505
  headers: {
@@ -1840,14 +1879,21 @@ var Runtime = class {
1840
1879
  );
1841
1880
  }
1842
1881
  }
1882
+ const resolvedToolIds = new Set(
1883
+ (messages ?? []).filter((message) => message.role === "tool" && message.tool_call_id).map((message) => message.tool_call_id)
1884
+ );
1885
+ const responseToolCalls = requiresAction ? toolCalls.filter((call) => !resolvedToolIds.has(call.id)) : toolCalls;
1843
1886
  const response = {
1844
1887
  success: !error,
1845
1888
  content,
1846
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
1889
+ toolCalls: responseToolCalls.length > 0 ? responseToolCalls : void 0,
1847
1890
  toolResults: toolResults.length > 0 ? toolResults : void 0,
1848
1891
  messages,
1849
1892
  requiresAction,
1850
1893
  error,
1894
+ // Copilot's HTTP transport reads a top-level message for HTTP errors.
1895
+ // Retain the structured error as well for existing API consumers.
1896
+ ...error ? { message: error.message } : {},
1851
1897
  // Include raw events for debugging
1852
1898
  _events: this.config.debug ? events : void 0
1853
1899
  };
@@ -1871,6 +1917,7 @@ var Runtime = class {
1871
1917
  return new Response(
1872
1918
  JSON.stringify({
1873
1919
  success: false,
1920
+ message: err instanceof Error ? err.message : "Unknown error",
1874
1921
  error: {
1875
1922
  message: err instanceof Error ? err.message : "Unknown error"
1876
1923
  }
package/dist/index.mjs CHANGED
@@ -130,6 +130,14 @@ function formatToolsForGoogle(tools) {
130
130
  ];
131
131
  }
132
132
 
133
+ // src/core/provider-error.ts
134
+ function throwIfAborted(signal) {
135
+ if (!signal?.aborted) return;
136
+ const error = new Error("The operation was aborted");
137
+ error.name = "AbortError";
138
+ throw error;
139
+ }
140
+
133
141
  // src/core/generate-text.ts
134
142
  async function generateText(params) {
135
143
  const { model, tools, maxSteps = 1, signal } = params;
@@ -143,22 +151,23 @@ async function generateText(params) {
143
151
  const allToolCalls = [];
144
152
  const allToolResults = [];
145
153
  for (let step = 0; step < maxSteps; step++) {
146
- if (signal?.aborted) {
147
- throw new Error("Generation aborted");
148
- }
154
+ throwIfAborted(signal);
149
155
  const formattedTools = tools ? formatToolsForProvider(tools, model.provider) : void 0;
150
156
  const result = await model.doGenerate({
151
157
  messages,
152
158
  tools: formattedTools,
153
159
  temperature: params.temperature,
160
+ reasoningEffort: params.reasoningEffort,
154
161
  maxTokens: params.maxTokens,
155
162
  responseFormat: params.responseFormat,
156
163
  signal
157
164
  });
165
+ throwIfAborted(signal);
158
166
  const stepToolResults = [];
159
167
  if (result.toolCalls && result.toolCalls.length > 0 && tools) {
160
168
  allToolCalls.push(...result.toolCalls);
161
169
  for (const call of result.toolCalls) {
170
+ throwIfAborted(signal);
162
171
  const toolDef = tools[call.name];
163
172
  if (!toolDef) {
164
173
  const errorResult = {
@@ -176,6 +185,7 @@ async function generateText(params) {
176
185
  abortSignal: signal,
177
186
  messages
178
187
  });
188
+ throwIfAborted(signal);
179
189
  const result2 = {
180
190
  toolCallId: call.id,
181
191
  result: toolResult
@@ -183,6 +193,7 @@ async function generateText(params) {
183
193
  stepToolResults.push(result2);
184
194
  allToolResults.push(result2);
185
195
  } catch (error) {
196
+ throwIfAborted(signal);
186
197
  const errorResult = {
187
198
  toolCallId: call.id,
188
199
  result: {
@@ -299,10 +310,12 @@ async function streamText(params) {
299
310
  messages,
300
311
  tools: formattedTools,
301
312
  temperature: params.temperature,
313
+ reasoningEffort: params.reasoningEffort,
302
314
  maxTokens: params.maxTokens,
303
315
  responseFormat: params.responseFormat,
304
316
  signal
305
317
  })) {
318
+ throwIfAborted(signal);
306
319
  switch (chunk.type) {
307
320
  case "text-delta":
308
321
  stepText += chunk.text;
@@ -350,6 +363,7 @@ async function streamText(params) {
350
363
  };
351
364
  messages = [...messages, assistantMessage];
352
365
  for (const call of toolCalls) {
366
+ throwIfAborted(signal);
353
367
  const toolDef = tools[call.name];
354
368
  if (!toolDef) {
355
369
  const errorResult = { error: `Tool not found: ${call.name}` };
@@ -375,6 +389,7 @@ async function streamText(params) {
375
389
  abortSignal: signal,
376
390
  messages
377
391
  });
392
+ throwIfAborted(signal);
378
393
  yield { type: "tool-result", toolCallId: call.id, result };
379
394
  messages = [
380
395
  ...messages,
@@ -385,6 +400,7 @@ async function streamText(params) {
385
400
  }
386
401
  ];
387
402
  } catch (error) {
403
+ throwIfAborted(signal);
388
404
  const errorResult = {
389
405
  error: error instanceof Error ? error.message : "Tool execution failed"
390
406
  };
@@ -410,40 +426,63 @@ async function streamText(params) {
410
426
  usage: finalUsage
411
427
  };
412
428
  }
413
- async function* createTextStream() {
414
- for await (const part of createFullStream()) {
415
- if (part.type === "text-delta") {
416
- yield part.text;
417
- }
429
+ const source = createFullStream();
430
+ const parts = [];
431
+ let finished = false;
432
+ let failure;
433
+ let pending;
434
+ async function advance() {
435
+ if (!pending) {
436
+ pending = (async () => {
437
+ try {
438
+ const next = await source.next();
439
+ if (next.done) finished = true;
440
+ else parts.push(next.value);
441
+ } catch (error) {
442
+ failure = error;
443
+ finished = true;
444
+ }
445
+ })();
446
+ }
447
+ const current = pending;
448
+ await current;
449
+ if (pending === current) pending = void 0;
450
+ }
451
+ async function* sharedStream() {
452
+ let index = 0;
453
+ while (true) {
454
+ if (index < parts.length) yield parts[index++];
455
+ else if (finished) {
456
+ if (failure) throw failure;
457
+ return;
458
+ } else await advance();
418
459
  }
419
460
  }
420
- let textPromise;
421
- let usagePromise;
422
- let finishReasonPromise;
423
- async function consumeStream() {
424
- for await (const _ of createFullStream()) {
461
+ async function* createTextStream() {
462
+ for await (const part of sharedStream()) {
463
+ if (part.type === "error") throw part.error;
464
+ if (part.type === "text-delta") yield part.text;
425
465
  }
426
466
  }
467
+ let completion;
468
+ function consumeStream() {
469
+ return completion ?? (completion = (async () => {
470
+ for await (const part of sharedStream()) {
471
+ if (part.type === "error") throw part.error;
472
+ }
473
+ })());
474
+ }
427
475
  return {
428
476
  textStream: createTextStream(),
429
- fullStream: createFullStream(),
477
+ fullStream: sharedStream(),
430
478
  get text() {
431
- if (!textPromise) {
432
- textPromise = consumeStream().then(() => fullText);
433
- }
434
- return textPromise;
479
+ return consumeStream().then(() => fullText);
435
480
  },
436
481
  get usage() {
437
- if (!usagePromise) {
438
- usagePromise = consumeStream().then(() => finalUsage);
439
- }
440
- return usagePromise;
482
+ return consumeStream().then(() => finalUsage);
441
483
  },
442
484
  get finishReason() {
443
- if (!finishReasonPromise) {
444
- finishReasonPromise = consumeStream().then(() => finalFinishReason);
445
- }
446
- return finishReasonPromise;
485
+ return consumeStream().then(() => finalFinishReason);
447
486
  },
448
487
  toTextStreamResponse(options) {
449
488
  const stream = createTextStreamReadable(createTextStream());
@@ -458,7 +497,7 @@ async function streamText(params) {
458
497
  });
459
498
  },
460
499
  toDataStreamResponse(options) {
461
- const stream = createDataStreamReadable(createFullStream());
500
+ const stream = createDataStreamReadable(sharedStream());
462
501
  return new Response(stream, {
463
502
  status: options?.status ?? 200,
464
503
  headers: {
@@ -1838,14 +1877,21 @@ var Runtime = class {
1838
1877
  );
1839
1878
  }
1840
1879
  }
1880
+ const resolvedToolIds = new Set(
1881
+ (messages ?? []).filter((message) => message.role === "tool" && message.tool_call_id).map((message) => message.tool_call_id)
1882
+ );
1883
+ const responseToolCalls = requiresAction ? toolCalls.filter((call) => !resolvedToolIds.has(call.id)) : toolCalls;
1841
1884
  const response = {
1842
1885
  success: !error,
1843
1886
  content,
1844
- toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
1887
+ toolCalls: responseToolCalls.length > 0 ? responseToolCalls : void 0,
1845
1888
  toolResults: toolResults.length > 0 ? toolResults : void 0,
1846
1889
  messages,
1847
1890
  requiresAction,
1848
1891
  error,
1892
+ // Copilot's HTTP transport reads a top-level message for HTTP errors.
1893
+ // Retain the structured error as well for existing API consumers.
1894
+ ...error ? { message: error.message } : {},
1849
1895
  // Include raw events for debugging
1850
1896
  _events: this.config.debug ? events : void 0
1851
1897
  };
@@ -1869,6 +1915,7 @@ var Runtime = class {
1869
1915
  return new Response(
1870
1916
  JSON.stringify({
1871
1917
  success: false,
1918
+ message: err instanceof Error ? err.message : "Unknown error",
1872
1919
  error: {
1873
1920
  message: err instanceof Error ? err.message : "Unknown error"
1874
1921
  }
@@ -1,7 +1,7 @@
1
- import { L as LanguageModel } from '../../types-BkQCSiIt.mjs';
2
- import { a as AnthropicProviderConfig, A as AIProvider } from '../../types-BSSiJW2o.mjs';
1
+ import { L as LanguageModel } from '../../types-BW3csYo5.mjs';
2
+ import { a as AnthropicProviderConfig, A as AIProvider } from '../../types-NOswlhm9.mjs';
3
3
  import 'zod';
4
- import '../../base-tNgbBaSo.mjs';
4
+ import '../../base-CkeT7u-O.mjs';
5
5
 
6
6
  /**
7
7
  * Anthropic Provider - Modern Pattern
@@ -1,7 +1,7 @@
1
- import { L as LanguageModel } from '../../types-BkQCSiIt.js';
2
- import { a as AnthropicProviderConfig, A as AIProvider } from '../../types-BQ31QIsA.js';
1
+ import { L as LanguageModel } from '../../types-BW3csYo5.js';
2
+ import { a as AnthropicProviderConfig, A as AIProvider } from '../../types-DA54bSmX.js';
3
3
  import 'zod';
4
- import '../../base-C58Dsr9p.js';
4
+ import '../../base-BdTpUAWI.js';
5
5
 
6
6
  /**
7
7
  * Anthropic Provider - Modern Pattern
@@ -1,7 +1,7 @@
1
- import { b as AzureProviderConfig, A as AIProvider } from '../../types-BSSiJW2o.mjs';
2
- import '../../types-BkQCSiIt.mjs';
1
+ import { b as AzureProviderConfig, A as AIProvider } from '../../types-NOswlhm9.mjs';
2
+ import '../../types-BW3csYo5.mjs';
3
3
  import 'zod';
4
- import '../../base-tNgbBaSo.mjs';
4
+ import '../../base-CkeT7u-O.mjs';
5
5
 
6
6
  /**
7
7
  * Azure OpenAI Provider
@@ -1,7 +1,7 @@
1
- import { b as AzureProviderConfig, A as AIProvider } from '../../types-BQ31QIsA.js';
2
- import '../../types-BkQCSiIt.js';
1
+ import { b as AzureProviderConfig, A as AIProvider } from '../../types-DA54bSmX.js';
2
+ import '../../types-BW3csYo5.js';
3
3
  import 'zod';
4
- import '../../base-C58Dsr9p.js';
4
+ import '../../base-BdTpUAWI.js';
5
5
 
6
6
  /**
7
7
  * Azure OpenAI Provider
@@ -1,7 +1,7 @@
1
- import { L as LanguageModel } from '../../types-BkQCSiIt.mjs';
2
- import { A as AIProvider } from '../../types-BSSiJW2o.mjs';
1
+ import { L as LanguageModel } from '../../types-BW3csYo5.mjs';
2
+ import { A as AIProvider } from '../../types-NOswlhm9.mjs';
3
3
  import 'zod';
4
- import '../../base-tNgbBaSo.mjs';
4
+ import '../../base-CkeT7u-O.mjs';
5
5
 
6
6
  /**
7
7
  * Fireworks Provider
@@ -1,7 +1,7 @@
1
- import { L as LanguageModel } from '../../types-BkQCSiIt.js';
2
- import { A as AIProvider } from '../../types-BQ31QIsA.js';
1
+ import { L as LanguageModel } from '../../types-BW3csYo5.js';
2
+ import { A as AIProvider } from '../../types-DA54bSmX.js';
3
3
  import 'zod';
4
- import '../../base-C58Dsr9p.js';
4
+ import '../../base-BdTpUAWI.js';
5
5
 
6
6
  /**
7
7
  * Fireworks Provider