@gammatech/aijsx 0.10.2-dev.2024-06-11 → 0.11.0-dev.2024-06-17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.mts CHANGED
@@ -43,6 +43,10 @@ type ChatCompletionClientAndProvider<K> = {
43
43
  completion: number;
44
44
  }) => number;
45
45
  };
46
+ type GetChatCompletionClientAndProvider<Model, Client> = (model: Model, args: {
47
+ retryCount: number;
48
+ lastError?: Error | null;
49
+ }) => Promise<ChatCompletionClientAndProvider<Client>>;
46
50
 
47
51
  type CreateRenderContextOptions = {
48
52
  logger?: LogImplementation;
@@ -52,15 +56,25 @@ type CreateRenderContextOptions = {
52
56
  };
53
57
  declare function createRenderContext({ logger, traceId, processor, contextValues, }?: CreateRenderContextOptions): RenderContext;
54
58
 
55
- declare const RetryCountContext: Context<number>;
59
+ type ExcludeNumber<E extends number, T = number> = T extends E ? never : T;
60
+ type RetryCountContextValue = {
61
+ retryCount: 0;
62
+ lastError: null;
63
+ } | {
64
+ retryCount: ExcludeNumber<0>;
65
+ lastError: Error;
66
+ };
67
+ declare const RetryCountContext: Context<RetryCountContextValue>;
68
+ declare const RetryLastErrorContext: Context<Error | null>;
56
69
  declare const DefaultMaxRetriesContext: Context<number>;
57
70
  type RetryProps = {
58
71
  shouldRetry: (error: Error) => boolean;
59
72
  retries?: number;
73
+ lastError?: Error;
60
74
  maxRetries?: number;
61
75
  children: AINode;
62
76
  };
63
- declare function Retry({ shouldRetry, retries, maxRetries, children }: RetryProps, ctx: RenderContext): AsyncGenerator<string, void, unknown>;
77
+ declare function Retry({ shouldRetry, retries, maxRetries, lastError, children }: RetryProps, ctx: RenderContext): AsyncGenerator<string, void, unknown>;
64
78
 
65
79
  type FallbackProps = {
66
80
  fallback: AINode;
@@ -241,7 +255,7 @@ declare module '@gammatech/aijsx' {
241
255
  }
242
256
  type ValidOpenAIVisionModel = 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo' | 'gpt-4-vision-preview';
243
257
  type ValidOpenAIChatModel = ValidOpenAIVisionModel | 'gpt-4' | 'gpt-4-0314' | 'gpt-4-0613' | 'gpt-4-32k' | 'gpt-4-32k-0314' | 'gpt-4-32k-0613' | 'gpt-4-1106-preview' | 'gpt-4-0125-preview' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-0301' | 'gpt-3.5-turbo-0613' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-16k-0613' | 'gpt-3.5-turbo-1106' | 'gpt-3.5-turbo-0125';
244
- declare const OpenAIClientContext: Context<() => ChatCompletionClientAndProvider<OpenAI>>;
258
+ declare const OpenAIClientContext: Context<GetChatCompletionClientAndProvider<ValidOpenAIChatModel, OpenAI>>;
245
259
  type OpenAIChatCompletionProps = {
246
260
  model: ValidOpenAIChatModel;
247
261
  maxTokens?: number;
@@ -266,7 +280,7 @@ declare module '@gammatech/aijsx' {
266
280
  * @see https://docs.anthropic.com/claude/reference/selecting-a-model
267
281
  */
268
282
  type ValidAnthropicChatModel = 'claude-instant-1.2' | 'claude-2.1' | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307';
269
- declare const AnthropicClientContext: Context<() => ChatCompletionClientAndProvider<AnthropicClient>>;
283
+ declare const AnthropicClientContext: Context<GetChatCompletionClientAndProvider<ValidAnthropicChatModel, AnthropicClient>>;
270
284
  type AnthropicChatCompletionProps = {
271
285
  model: ValidAnthropicChatModel;
272
286
  maxTokens?: number;
@@ -291,7 +305,7 @@ declare module '@gammatech/aijsx' {
291
305
  }
292
306
  }
293
307
  type ValidGoogleChatModel = 'gemini-1.5-pro' | 'gemini-1.5-flash';
294
- declare const GoogleClientContext: Context<() => ChatCompletionClientAndProvider<VertexAI>>;
308
+ declare const GoogleClientContext: Context<GetChatCompletionClientAndProvider<ValidGoogleChatModel, VertexAI>>;
295
309
  type GoogleChatCompletionProps = {
296
310
  model: ValidGoogleChatModel;
297
311
  maxTokens?: number;
@@ -306,4 +320,4 @@ type GoogleChatCompletionProps = {
306
320
  };
307
321
  declare function GoogleChatCompletion(props: GoogleChatCompletionProps, ctx: RenderContext): JSX.Element;
308
322
 
309
- export { AIComponent, AINode, AISpanAttributes, AISpanProcessor, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext, AssistantMessage, type ChatCompletionClientAndProvider, ChatCompletionError, type ChatCompletionRequestPayloads, ChatMessage, Context, type CostFn, DebugMessage, DefaultMaxRetriesContext, EnrichingSpanProcessor, EvaluatorFn, EvaluatorResult, Fallback, GoogleChatCompletion, type GoogleChatCompletionRequest, GoogleClientContext, LogChatCompletionRequest, LogImplementation, OpenAIChatCompletion, type OpenAIChatCompletionRequest, type OpenAIChatMessage, OpenAIClientContext, ParseVariablesError, ProcessedAISpanAttributes, type Prompt, PromptInvalidOutputError, PromptParsed, ReadableSpan, RenderContext, Retry, RetryCountContext, SpanAttributes, SpanExporter, SpanProcessor, SystemMessage, type TokenizerFn, Trace, Tracer, UserMessage, type ValidAnthropicChatModel, type ValidGoogleChatModel, type ValidOpenAIChatModel, type ValidOpenAIVisionModel, anthropicTokenizer, computeUsage, createPrompt, createRenderContext, evaluatePrompt, isPromptParsed, openaiTokenizer, tracing };
323
+ export { AIComponent, AINode, AISpanAttributes, AISpanProcessor, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext, AssistantMessage, type ChatCompletionClientAndProvider, ChatCompletionError, type ChatCompletionRequestPayloads, ChatMessage, Context, type CostFn, DebugMessage, DefaultMaxRetriesContext, EnrichingSpanProcessor, EvaluatorFn, EvaluatorResult, Fallback, type GetChatCompletionClientAndProvider, GoogleChatCompletion, type GoogleChatCompletionRequest, GoogleClientContext, LogChatCompletionRequest, LogImplementation, OpenAIChatCompletion, type OpenAIChatCompletionRequest, type OpenAIChatMessage, OpenAIClientContext, ParseVariablesError, ProcessedAISpanAttributes, type Prompt, PromptInvalidOutputError, PromptParsed, ReadableSpan, RenderContext, Retry, RetryCountContext, RetryLastErrorContext, SpanAttributes, SpanExporter, SpanProcessor, SystemMessage, type TokenizerFn, Trace, Tracer, UserMessage, type ValidAnthropicChatModel, type ValidGoogleChatModel, type ValidOpenAIChatModel, type ValidOpenAIVisionModel, anthropicTokenizer, computeUsage, createPrompt, createRenderContext, evaluatePrompt, isPromptParsed, openaiTokenizer, tracing };
package/dist/index.d.ts CHANGED
@@ -43,6 +43,10 @@ type ChatCompletionClientAndProvider<K> = {
43
43
  completion: number;
44
44
  }) => number;
45
45
  };
46
+ type GetChatCompletionClientAndProvider<Model, Client> = (model: Model, args: {
47
+ retryCount: number;
48
+ lastError?: Error | null;
49
+ }) => Promise<ChatCompletionClientAndProvider<Client>>;
46
50
 
47
51
  type CreateRenderContextOptions = {
48
52
  logger?: LogImplementation;
@@ -52,15 +56,25 @@ type CreateRenderContextOptions = {
52
56
  };
53
57
  declare function createRenderContext({ logger, traceId, processor, contextValues, }?: CreateRenderContextOptions): RenderContext;
54
58
 
55
- declare const RetryCountContext: Context<number>;
59
+ type ExcludeNumber<E extends number, T = number> = T extends E ? never : T;
60
+ type RetryCountContextValue = {
61
+ retryCount: 0;
62
+ lastError: null;
63
+ } | {
64
+ retryCount: ExcludeNumber<0>;
65
+ lastError: Error;
66
+ };
67
+ declare const RetryCountContext: Context<RetryCountContextValue>;
68
+ declare const RetryLastErrorContext: Context<Error | null>;
56
69
  declare const DefaultMaxRetriesContext: Context<number>;
57
70
  type RetryProps = {
58
71
  shouldRetry: (error: Error) => boolean;
59
72
  retries?: number;
73
+ lastError?: Error;
60
74
  maxRetries?: number;
61
75
  children: AINode;
62
76
  };
63
- declare function Retry({ shouldRetry, retries, maxRetries, children }: RetryProps, ctx: RenderContext): AsyncGenerator<string, void, unknown>;
77
+ declare function Retry({ shouldRetry, retries, maxRetries, lastError, children }: RetryProps, ctx: RenderContext): AsyncGenerator<string, void, unknown>;
64
78
 
65
79
  type FallbackProps = {
66
80
  fallback: AINode;
@@ -241,7 +255,7 @@ declare module '@gammatech/aijsx' {
241
255
  }
242
256
  type ValidOpenAIVisionModel = 'gpt-4o' | 'gpt-4o-2024-05-13' | 'gpt-4-turbo-2024-04-09' | 'gpt-4-turbo' | 'gpt-4-vision-preview';
243
257
  type ValidOpenAIChatModel = ValidOpenAIVisionModel | 'gpt-4' | 'gpt-4-0314' | 'gpt-4-0613' | 'gpt-4-32k' | 'gpt-4-32k-0314' | 'gpt-4-32k-0613' | 'gpt-4-1106-preview' | 'gpt-4-0125-preview' | 'gpt-3.5-turbo' | 'gpt-3.5-turbo-0301' | 'gpt-3.5-turbo-0613' | 'gpt-3.5-turbo-16k' | 'gpt-3.5-turbo-16k-0613' | 'gpt-3.5-turbo-1106' | 'gpt-3.5-turbo-0125';
244
- declare const OpenAIClientContext: Context<() => ChatCompletionClientAndProvider<OpenAI>>;
258
+ declare const OpenAIClientContext: Context<GetChatCompletionClientAndProvider<ValidOpenAIChatModel, OpenAI>>;
245
259
  type OpenAIChatCompletionProps = {
246
260
  model: ValidOpenAIChatModel;
247
261
  maxTokens?: number;
@@ -266,7 +280,7 @@ declare module '@gammatech/aijsx' {
266
280
  * @see https://docs.anthropic.com/claude/reference/selecting-a-model
267
281
  */
268
282
  type ValidAnthropicChatModel = 'claude-instant-1.2' | 'claude-2.1' | 'claude-3-opus-20240229' | 'claude-3-sonnet-20240229' | 'claude-3-haiku-20240307';
269
- declare const AnthropicClientContext: Context<() => ChatCompletionClientAndProvider<AnthropicClient>>;
283
+ declare const AnthropicClientContext: Context<GetChatCompletionClientAndProvider<ValidAnthropicChatModel, AnthropicClient>>;
270
284
  type AnthropicChatCompletionProps = {
271
285
  model: ValidAnthropicChatModel;
272
286
  maxTokens?: number;
@@ -291,7 +305,7 @@ declare module '@gammatech/aijsx' {
291
305
  }
292
306
  }
293
307
  type ValidGoogleChatModel = 'gemini-1.5-pro' | 'gemini-1.5-flash';
294
- declare const GoogleClientContext: Context<() => ChatCompletionClientAndProvider<VertexAI>>;
308
+ declare const GoogleClientContext: Context<GetChatCompletionClientAndProvider<ValidGoogleChatModel, VertexAI>>;
295
309
  type GoogleChatCompletionProps = {
296
310
  model: ValidGoogleChatModel;
297
311
  maxTokens?: number;
@@ -306,4 +320,4 @@ type GoogleChatCompletionProps = {
306
320
  };
307
321
  declare function GoogleChatCompletion(props: GoogleChatCompletionProps, ctx: RenderContext): JSX.Element;
308
322
 
309
- export { AIComponent, AINode, AISpanAttributes, AISpanProcessor, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext, AssistantMessage, type ChatCompletionClientAndProvider, ChatCompletionError, type ChatCompletionRequestPayloads, ChatMessage, Context, type CostFn, DebugMessage, DefaultMaxRetriesContext, EnrichingSpanProcessor, EvaluatorFn, EvaluatorResult, Fallback, GoogleChatCompletion, type GoogleChatCompletionRequest, GoogleClientContext, LogChatCompletionRequest, LogImplementation, OpenAIChatCompletion, type OpenAIChatCompletionRequest, type OpenAIChatMessage, OpenAIClientContext, ParseVariablesError, ProcessedAISpanAttributes, type Prompt, PromptInvalidOutputError, PromptParsed, ReadableSpan, RenderContext, Retry, RetryCountContext, SpanAttributes, SpanExporter, SpanProcessor, SystemMessage, type TokenizerFn, Trace, Tracer, UserMessage, type ValidAnthropicChatModel, type ValidGoogleChatModel, type ValidOpenAIChatModel, type ValidOpenAIVisionModel, anthropicTokenizer, computeUsage, createPrompt, createRenderContext, evaluatePrompt, isPromptParsed, openaiTokenizer, tracing };
323
+ export { AIComponent, AINode, AISpanAttributes, AISpanProcessor, AnthropicChatCompletion, type AnthropicChatCompletionRequest, AnthropicClientContext, AssistantMessage, type ChatCompletionClientAndProvider, ChatCompletionError, type ChatCompletionRequestPayloads, ChatMessage, Context, type CostFn, DebugMessage, DefaultMaxRetriesContext, EnrichingSpanProcessor, EvaluatorFn, EvaluatorResult, Fallback, type GetChatCompletionClientAndProvider, GoogleChatCompletion, type GoogleChatCompletionRequest, GoogleClientContext, LogChatCompletionRequest, LogImplementation, OpenAIChatCompletion, type OpenAIChatCompletionRequest, type OpenAIChatMessage, OpenAIClientContext, ParseVariablesError, ProcessedAISpanAttributes, type Prompt, PromptInvalidOutputError, PromptParsed, ReadableSpan, RenderContext, Retry, RetryCountContext, RetryLastErrorContext, SpanAttributes, SpanExporter, SpanProcessor, SystemMessage, type TokenizerFn, Trace, Tracer, UserMessage, type ValidAnthropicChatModel, type ValidGoogleChatModel, type ValidOpenAIChatModel, type ValidOpenAIVisionModel, anthropicTokenizer, computeUsage, createPrompt, createRenderContext, evaluatePrompt, isPromptParsed, openaiTokenizer, tracing };
package/dist/index.js CHANGED
@@ -56,6 +56,7 @@ __export(src_exports, {
56
56
  PromptInvalidOutputError: () => PromptInvalidOutputError,
57
57
  Retry: () => Retry,
58
58
  RetryCountContext: () => RetryCountContext,
59
+ RetryLastErrorContext: () => RetryLastErrorContext,
59
60
  SystemMessage: () => SystemMessage,
60
61
  Trace: () => Trace,
61
62
  UserMessage: () => UserMessage,
@@ -1538,26 +1539,41 @@ function renderCloseTag(element) {
1538
1539
  }
1539
1540
 
1540
1541
  // src/retry.tsx
1541
- var RetryCountContext = createContext(0);
1542
+ var RetryCountContext = createContext({
1543
+ retryCount: 0,
1544
+ lastError: null
1545
+ });
1546
+ var RetryLastErrorContext = createContext(null);
1542
1547
  var DefaultMaxRetriesContext = createContext(0);
1543
- async function* Retry({ shouldRetry: shouldRetry4, retries = 0, maxRetries = 3, children }, ctx) {
1548
+ async function* Retry({ shouldRetry: shouldRetry4, retries = 0, maxRetries = 3, lastError, children }, ctx) {
1544
1549
  const { render } = ctx;
1545
1550
  let hasYieldedData = false;
1546
1551
  try {
1552
+ const ctxValue = retries === 0 ? { retryCount: 0, lastError: null } : {
1553
+ retryCount: retries,
1554
+ lastError
1555
+ };
1547
1556
  const result = render(
1548
- /* @__PURE__ */ jsx(RetryCountContext.Provider, { value: retries, children })
1557
+ /* @__PURE__ */ jsx(RetryCountContext.Provider, { value: ctxValue, children })
1549
1558
  );
1550
1559
  for await (const value of result) {
1551
1560
  hasYieldedData = true;
1552
1561
  yield value;
1553
1562
  }
1554
1563
  } catch (e) {
1555
- if (hasYieldedData || retries >= maxRetries || !shouldRetry4(e)) {
1564
+ const err = castToError(e);
1565
+ if (hasYieldedData || retries >= maxRetries || !shouldRetry4(err)) {
1556
1566
  throw e;
1557
1567
  }
1558
1568
  await backoff(retries);
1559
1569
  yield* Retry(
1560
- { shouldRetry: shouldRetry4, retries: retries + 1, maxRetries, children },
1570
+ {
1571
+ shouldRetry: shouldRetry4,
1572
+ retries: retries + 1,
1573
+ maxRetries,
1574
+ lastError: err,
1575
+ children
1576
+ },
1561
1577
  ctx
1562
1578
  );
1563
1579
  }
@@ -1567,6 +1583,15 @@ var backoff = (retries) => {
1567
1583
  const waitTime = BASE_BACKOFF * Math.pow(4, retries);
1568
1584
  return new Promise((resolve) => setTimeout(resolve, waitTime));
1569
1585
  };
1586
+ var castToError = (e) => {
1587
+ if (e instanceof Error) {
1588
+ return e;
1589
+ }
1590
+ if (typeof e === "string") {
1591
+ return new Error(e);
1592
+ }
1593
+ return new Error("Unknown error");
1594
+ };
1570
1595
 
1571
1596
  // src/fallback.tsx
1572
1597
  async function* Fallback({ shouldFallback = () => true, fallback, children }, ctx) {
@@ -2051,7 +2076,7 @@ function getEnvVar(name, shouldThrow = true) {
2051
2076
 
2052
2077
  // src/lib/openai/OpenAI.tsx
2053
2078
  var defaultClient = null;
2054
- var OpenAIClientContext = createContext(() => {
2079
+ var OpenAIClientContext = createContext(async () => {
2055
2080
  if (defaultClient) {
2056
2081
  return defaultClient;
2057
2082
  }
@@ -2123,9 +2148,16 @@ function OpenAIChatCompletion(props, ctx) {
2123
2148
  async function* OpenAIChatCompletionInner(props, ctx) {
2124
2149
  const startTime = performance.now();
2125
2150
  const { logger, tracer, getContext } = ctx;
2126
- const retryCount = getContext(RetryCountContext);
2151
+ const { retryCount, lastError } = getContext(RetryCountContext);
2127
2152
  const span = tracer.getActiveSpan();
2128
- const { client, provider, providerRegion, costFn } = getContext(OpenAIClientContext)();
2153
+ const getClientFn = getContext(OpenAIClientContext);
2154
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2155
+ props.model,
2156
+ {
2157
+ retryCount,
2158
+ lastError
2159
+ }
2160
+ );
2129
2161
  if (!client) {
2130
2162
  throw new Error("[OpenAI] must supply OpenAI model via context");
2131
2163
  }
@@ -2295,7 +2327,7 @@ var anthropicTokenizer = (message) => {
2295
2327
 
2296
2328
  // src/lib/anthropic/Anthropic.tsx
2297
2329
  var defaultClient2 = null;
2298
- var AnthropicClientContext = createContext(() => {
2330
+ var AnthropicClientContext = createContext(async () => {
2299
2331
  if (defaultClient2) {
2300
2332
  return defaultClient2;
2301
2333
  }
@@ -2413,11 +2445,16 @@ function AnthropicChatCompletion(props, ctx) {
2413
2445
  async function* AnthropicChatCompletionInner(props, ctx) {
2414
2446
  const startTime = performance.now();
2415
2447
  const { logger, tracer, getContext } = ctx;
2416
- const retryCount = getContext(RetryCountContext);
2448
+ const { retryCount, lastError } = getContext(RetryCountContext);
2417
2449
  const span = tracer.getActiveSpan();
2418
- const { client, provider, providerRegion, costFn } = getContext(
2419
- AnthropicClientContext
2420
- )();
2450
+ const getClientFn = getContext(AnthropicClientContext);
2451
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2452
+ props.model,
2453
+ {
2454
+ retryCount,
2455
+ lastError
2456
+ }
2457
+ );
2421
2458
  if (!client) {
2422
2459
  throw new Error(
2423
2460
  "[AnthropicChatCompletion] must supply AnthropicClient via context"
@@ -2624,7 +2661,7 @@ var DEFAULT_SAFETY_SETTINGS = [
2624
2661
  threshold: import_vertexai.HarmBlockThreshold.BLOCK_ONLY_HIGH
2625
2662
  }
2626
2663
  ];
2627
- var GoogleClientContext = createContext(() => {
2664
+ var GoogleClientContext = createContext(async () => {
2628
2665
  if (defaultClient3) {
2629
2666
  return defaultClient3;
2630
2667
  }
@@ -2705,9 +2742,16 @@ function GoogleChatCompletion(props, ctx) {
2705
2742
  async function* GoogleChatCompletionInner(props, ctx) {
2706
2743
  const startTime = performance.now();
2707
2744
  const { logger, tracer, getContext } = ctx;
2708
- const retryCount = getContext(RetryCountContext);
2745
+ const { retryCount, lastError } = getContext(RetryCountContext);
2709
2746
  const span = tracer.getActiveSpan();
2710
- const { client, provider, providerRegion, costFn } = getContext(GoogleClientContext)();
2747
+ const getClientFn = getContext(GoogleClientContext);
2748
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2749
+ props.model,
2750
+ {
2751
+ retryCount,
2752
+ lastError
2753
+ }
2754
+ );
2711
2755
  if (!client) {
2712
2756
  throw new Error(
2713
2757
  "[GoogleChatCompletion] must supply GoogleClient via context"
@@ -2896,6 +2940,7 @@ var import_vertexai2 = require("@google-cloud/vertexai");
2896
2940
  PromptInvalidOutputError,
2897
2941
  Retry,
2898
2942
  RetryCountContext,
2943
+ RetryLastErrorContext,
2899
2944
  SystemMessage,
2900
2945
  Trace,
2901
2946
  UserMessage,
package/dist/index.mjs CHANGED
@@ -1436,26 +1436,41 @@ function renderCloseTag(element) {
1436
1436
  }
1437
1437
 
1438
1438
  // src/retry.tsx
1439
- var RetryCountContext = createContext(0);
1439
+ var RetryCountContext = createContext({
1440
+ retryCount: 0,
1441
+ lastError: null
1442
+ });
1443
+ var RetryLastErrorContext = createContext(null);
1440
1444
  var DefaultMaxRetriesContext = createContext(0);
1441
- async function* Retry({ shouldRetry: shouldRetry4, retries = 0, maxRetries = 3, children }, ctx) {
1445
+ async function* Retry({ shouldRetry: shouldRetry4, retries = 0, maxRetries = 3, lastError, children }, ctx) {
1442
1446
  const { render } = ctx;
1443
1447
  let hasYieldedData = false;
1444
1448
  try {
1449
+ const ctxValue = retries === 0 ? { retryCount: 0, lastError: null } : {
1450
+ retryCount: retries,
1451
+ lastError
1452
+ };
1445
1453
  const result = render(
1446
- /* @__PURE__ */ jsx(RetryCountContext.Provider, { value: retries, children })
1454
+ /* @__PURE__ */ jsx(RetryCountContext.Provider, { value: ctxValue, children })
1447
1455
  );
1448
1456
  for await (const value of result) {
1449
1457
  hasYieldedData = true;
1450
1458
  yield value;
1451
1459
  }
1452
1460
  } catch (e) {
1453
- if (hasYieldedData || retries >= maxRetries || !shouldRetry4(e)) {
1461
+ const err = castToError(e);
1462
+ if (hasYieldedData || retries >= maxRetries || !shouldRetry4(err)) {
1454
1463
  throw e;
1455
1464
  }
1456
1465
  await backoff(retries);
1457
1466
  yield* Retry(
1458
- { shouldRetry: shouldRetry4, retries: retries + 1, maxRetries, children },
1467
+ {
1468
+ shouldRetry: shouldRetry4,
1469
+ retries: retries + 1,
1470
+ maxRetries,
1471
+ lastError: err,
1472
+ children
1473
+ },
1459
1474
  ctx
1460
1475
  );
1461
1476
  }
@@ -1465,6 +1480,15 @@ var backoff = (retries) => {
1465
1480
  const waitTime = BASE_BACKOFF * Math.pow(4, retries);
1466
1481
  return new Promise((resolve) => setTimeout(resolve, waitTime));
1467
1482
  };
1483
+ var castToError = (e) => {
1484
+ if (e instanceof Error) {
1485
+ return e;
1486
+ }
1487
+ if (typeof e === "string") {
1488
+ return new Error(e);
1489
+ }
1490
+ return new Error("Unknown error");
1491
+ };
1468
1492
 
1469
1493
  // src/fallback.tsx
1470
1494
  async function* Fallback({ shouldFallback = () => true, fallback, children }, ctx) {
@@ -1949,7 +1973,7 @@ function getEnvVar(name, shouldThrow = true) {
1949
1973
 
1950
1974
  // src/lib/openai/OpenAI.tsx
1951
1975
  var defaultClient = null;
1952
- var OpenAIClientContext = createContext(() => {
1976
+ var OpenAIClientContext = createContext(async () => {
1953
1977
  if (defaultClient) {
1954
1978
  return defaultClient;
1955
1979
  }
@@ -2021,9 +2045,16 @@ function OpenAIChatCompletion(props, ctx) {
2021
2045
  async function* OpenAIChatCompletionInner(props, ctx) {
2022
2046
  const startTime = performance.now();
2023
2047
  const { logger, tracer, getContext } = ctx;
2024
- const retryCount = getContext(RetryCountContext);
2048
+ const { retryCount, lastError } = getContext(RetryCountContext);
2025
2049
  const span = tracer.getActiveSpan();
2026
- const { client, provider, providerRegion, costFn } = getContext(OpenAIClientContext)();
2050
+ const getClientFn = getContext(OpenAIClientContext);
2051
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2052
+ props.model,
2053
+ {
2054
+ retryCount,
2055
+ lastError
2056
+ }
2057
+ );
2027
2058
  if (!client) {
2028
2059
  throw new Error("[OpenAI] must supply OpenAI model via context");
2029
2060
  }
@@ -2193,7 +2224,7 @@ var anthropicTokenizer = (message) => {
2193
2224
 
2194
2225
  // src/lib/anthropic/Anthropic.tsx
2195
2226
  var defaultClient2 = null;
2196
- var AnthropicClientContext = createContext(() => {
2227
+ var AnthropicClientContext = createContext(async () => {
2197
2228
  if (defaultClient2) {
2198
2229
  return defaultClient2;
2199
2230
  }
@@ -2311,11 +2342,16 @@ function AnthropicChatCompletion(props, ctx) {
2311
2342
  async function* AnthropicChatCompletionInner(props, ctx) {
2312
2343
  const startTime = performance.now();
2313
2344
  const { logger, tracer, getContext } = ctx;
2314
- const retryCount = getContext(RetryCountContext);
2345
+ const { retryCount, lastError } = getContext(RetryCountContext);
2315
2346
  const span = tracer.getActiveSpan();
2316
- const { client, provider, providerRegion, costFn } = getContext(
2317
- AnthropicClientContext
2318
- )();
2347
+ const getClientFn = getContext(AnthropicClientContext);
2348
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2349
+ props.model,
2350
+ {
2351
+ retryCount,
2352
+ lastError
2353
+ }
2354
+ );
2319
2355
  if (!client) {
2320
2356
  throw new Error(
2321
2357
  "[AnthropicChatCompletion] must supply AnthropicClient via context"
@@ -2526,7 +2562,7 @@ var DEFAULT_SAFETY_SETTINGS = [
2526
2562
  threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
2527
2563
  }
2528
2564
  ];
2529
- var GoogleClientContext = createContext(() => {
2565
+ var GoogleClientContext = createContext(async () => {
2530
2566
  if (defaultClient3) {
2531
2567
  return defaultClient3;
2532
2568
  }
@@ -2607,9 +2643,16 @@ function GoogleChatCompletion(props, ctx) {
2607
2643
  async function* GoogleChatCompletionInner(props, ctx) {
2608
2644
  const startTime = performance.now();
2609
2645
  const { logger, tracer, getContext } = ctx;
2610
- const retryCount = getContext(RetryCountContext);
2646
+ const { retryCount, lastError } = getContext(RetryCountContext);
2611
2647
  const span = tracer.getActiveSpan();
2612
- const { client, provider, providerRegion, costFn } = getContext(GoogleClientContext)();
2648
+ const getClientFn = getContext(GoogleClientContext);
2649
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2650
+ props.model,
2651
+ {
2652
+ retryCount,
2653
+ lastError
2654
+ }
2655
+ );
2613
2656
  if (!client) {
2614
2657
  throw new Error(
2615
2658
  "[GoogleChatCompletion] must supply GoogleClient via context"
@@ -2801,6 +2844,7 @@ export {
2801
2844
  PromptInvalidOutputError,
2802
2845
  Retry,
2803
2846
  RetryCountContext,
2847
+ RetryLastErrorContext,
2804
2848
  SystemMessage,
2805
2849
  Trace,
2806
2850
  UserMessage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gammatech/aijsx",
3
- "version": "0.10.2-dev.2024-06-11",
3
+ "version": "0.11.0-dev.2024-06-17",
4
4
  "description": "Rewrite of aijsx",
5
5
  "author": "Jordan Garcia",
6
6
  "license": "MIT",