@gammatech/aijsx 0.10.1-dev.2024-06-07 → 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
@@ -6,8 +6,8 @@ export { OpenAI as OpenAIClient } from 'openai';
6
6
  import { ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam, ChatCompletionAssistantMessageParam, ChatCompletionCreateParams } from 'openai/resources';
7
7
  import AnthropicClient from '@anthropic-ai/sdk';
8
8
  export { default as AnthropicClient } from '@anthropic-ai/sdk';
9
- import { PromptFeedback, GenerateContentRequest, VertexAI } from '@google-cloud/vertexai';
10
- export { VertexAI } from '@google-cloud/vertexai';
9
+ import { PromptFeedback, GenerateContentRequest, VertexAI, HarmCategory, HarmBlockThreshold } from '@google-cloud/vertexai';
10
+ export { HarmBlockThreshold as GoogleHarmBlockThreshold, HarmCategory as GoogleHarmCategory, VertexAI } from '@google-cloud/vertexai';
11
11
 
12
12
  declare class ChatCompletionError extends Error {
13
13
  readonly chatCompletionRequest: LogChatCompletionRequest;
@@ -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;
@@ -299,7 +313,11 @@ type GoogleChatCompletionProps = {
299
313
  stop?: string | string[];
300
314
  maxRetries?: number;
301
315
  children: AINode;
316
+ safetySettings?: {
317
+ category: HarmCategory;
318
+ threshold: HarmBlockThreshold;
319
+ }[];
302
320
  };
303
321
  declare function GoogleChatCompletion(props: GoogleChatCompletionProps, ctx: RenderContext): JSX.Element;
304
322
 
305
- 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
@@ -6,8 +6,8 @@ export { OpenAI as OpenAIClient } from 'openai';
6
6
  import { ChatCompletionSystemMessageParam, ChatCompletionUserMessageParam, ChatCompletionAssistantMessageParam, ChatCompletionCreateParams } from 'openai/resources';
7
7
  import AnthropicClient from '@anthropic-ai/sdk';
8
8
  export { default as AnthropicClient } from '@anthropic-ai/sdk';
9
- import { PromptFeedback, GenerateContentRequest, VertexAI } from '@google-cloud/vertexai';
10
- export { VertexAI } from '@google-cloud/vertexai';
9
+ import { PromptFeedback, GenerateContentRequest, VertexAI, HarmCategory, HarmBlockThreshold } from '@google-cloud/vertexai';
10
+ export { HarmBlockThreshold as GoogleHarmBlockThreshold, HarmCategory as GoogleHarmCategory, VertexAI } from '@google-cloud/vertexai';
11
11
 
12
12
  declare class ChatCompletionError extends Error {
13
13
  readonly chatCompletionRequest: LogChatCompletionRequest;
@@ -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;
@@ -299,7 +313,11 @@ type GoogleChatCompletionProps = {
299
313
  stop?: string | string[];
300
314
  maxRetries?: number;
301
315
  children: AINode;
316
+ safetySettings?: {
317
+ category: HarmCategory;
318
+ threshold: HarmBlockThreshold;
319
+ }[];
302
320
  };
303
321
  declare function GoogleChatCompletion(props: GoogleChatCompletionProps, ctx: RenderContext): JSX.Element;
304
322
 
305
- 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
@@ -44,6 +44,8 @@ __export(src_exports, {
44
44
  Fallback: () => Fallback,
45
45
  GoogleChatCompletion: () => GoogleChatCompletion,
46
46
  GoogleClientContext: () => GoogleClientContext,
47
+ GoogleHarmBlockThreshold: () => import_vertexai2.HarmBlockThreshold,
48
+ GoogleHarmCategory: () => import_vertexai2.HarmCategory,
47
49
  ImagePart: () => ImagePart,
48
50
  LogImplementation: () => LogImplementation,
49
51
  NoopLogImplementation: () => NoopLogImplementation,
@@ -54,6 +56,7 @@ __export(src_exports, {
54
56
  PromptInvalidOutputError: () => PromptInvalidOutputError,
55
57
  Retry: () => Retry,
56
58
  RetryCountContext: () => RetryCountContext,
59
+ RetryLastErrorContext: () => RetryLastErrorContext,
57
60
  SystemMessage: () => SystemMessage,
58
61
  Trace: () => Trace,
59
62
  UserMessage: () => UserMessage,
@@ -1536,26 +1539,41 @@ function renderCloseTag(element) {
1536
1539
  }
1537
1540
 
1538
1541
  // src/retry.tsx
1539
- var RetryCountContext = createContext(0);
1542
+ var RetryCountContext = createContext({
1543
+ retryCount: 0,
1544
+ lastError: null
1545
+ });
1546
+ var RetryLastErrorContext = createContext(null);
1540
1547
  var DefaultMaxRetriesContext = createContext(0);
1541
- async function* Retry({ shouldRetry: shouldRetry4, retries = 0, maxRetries = 3, children }, ctx) {
1548
+ async function* Retry({ shouldRetry: shouldRetry4, retries = 0, maxRetries = 3, lastError, children }, ctx) {
1542
1549
  const { render } = ctx;
1543
1550
  let hasYieldedData = false;
1544
1551
  try {
1552
+ const ctxValue = retries === 0 ? { retryCount: 0, lastError: null } : {
1553
+ retryCount: retries,
1554
+ lastError
1555
+ };
1545
1556
  const result = render(
1546
- /* @__PURE__ */ jsx(RetryCountContext.Provider, { value: retries, children })
1557
+ /* @__PURE__ */ jsx(RetryCountContext.Provider, { value: ctxValue, children })
1547
1558
  );
1548
1559
  for await (const value of result) {
1549
1560
  hasYieldedData = true;
1550
1561
  yield value;
1551
1562
  }
1552
1563
  } catch (e) {
1553
- if (hasYieldedData || retries >= maxRetries || !shouldRetry4(e)) {
1564
+ const err = castToError(e);
1565
+ if (hasYieldedData || retries >= maxRetries || !shouldRetry4(err)) {
1554
1566
  throw e;
1555
1567
  }
1556
1568
  await backoff(retries);
1557
1569
  yield* Retry(
1558
- { shouldRetry: shouldRetry4, retries: retries + 1, maxRetries, children },
1570
+ {
1571
+ shouldRetry: shouldRetry4,
1572
+ retries: retries + 1,
1573
+ maxRetries,
1574
+ lastError: err,
1575
+ children
1576
+ },
1559
1577
  ctx
1560
1578
  );
1561
1579
  }
@@ -1565,6 +1583,15 @@ var backoff = (retries) => {
1565
1583
  const waitTime = BASE_BACKOFF * Math.pow(4, retries);
1566
1584
  return new Promise((resolve) => setTimeout(resolve, waitTime));
1567
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
+ };
1568
1595
 
1569
1596
  // src/fallback.tsx
1570
1597
  async function* Fallback({ shouldFallback = () => true, fallback, children }, ctx) {
@@ -2049,7 +2076,7 @@ function getEnvVar(name, shouldThrow = true) {
2049
2076
 
2050
2077
  // src/lib/openai/OpenAI.tsx
2051
2078
  var defaultClient = null;
2052
- var OpenAIClientContext = createContext(() => {
2079
+ var OpenAIClientContext = createContext(async () => {
2053
2080
  if (defaultClient) {
2054
2081
  return defaultClient;
2055
2082
  }
@@ -2121,9 +2148,16 @@ function OpenAIChatCompletion(props, ctx) {
2121
2148
  async function* OpenAIChatCompletionInner(props, ctx) {
2122
2149
  const startTime = performance.now();
2123
2150
  const { logger, tracer, getContext } = ctx;
2124
- const retryCount = getContext(RetryCountContext);
2151
+ const { retryCount, lastError } = getContext(RetryCountContext);
2125
2152
  const span = tracer.getActiveSpan();
2126
- 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
+ );
2127
2161
  if (!client) {
2128
2162
  throw new Error("[OpenAI] must supply OpenAI model via context");
2129
2163
  }
@@ -2293,7 +2327,7 @@ var anthropicTokenizer = (message) => {
2293
2327
 
2294
2328
  // src/lib/anthropic/Anthropic.tsx
2295
2329
  var defaultClient2 = null;
2296
- var AnthropicClientContext = createContext(() => {
2330
+ var AnthropicClientContext = createContext(async () => {
2297
2331
  if (defaultClient2) {
2298
2332
  return defaultClient2;
2299
2333
  }
@@ -2411,11 +2445,16 @@ function AnthropicChatCompletion(props, ctx) {
2411
2445
  async function* AnthropicChatCompletionInner(props, ctx) {
2412
2446
  const startTime = performance.now();
2413
2447
  const { logger, tracer, getContext } = ctx;
2414
- const retryCount = getContext(RetryCountContext);
2448
+ const { retryCount, lastError } = getContext(RetryCountContext);
2415
2449
  const span = tracer.getActiveSpan();
2416
- const { client, provider, providerRegion, costFn } = getContext(
2417
- AnthropicClientContext
2418
- )();
2450
+ const getClientFn = getContext(AnthropicClientContext);
2451
+ const { client, provider, providerRegion, costFn } = await getClientFn(
2452
+ props.model,
2453
+ {
2454
+ retryCount,
2455
+ lastError
2456
+ }
2457
+ );
2419
2458
  if (!client) {
2420
2459
  throw new Error(
2421
2460
  "[AnthropicChatCompletion] must supply AnthropicClient via context"
@@ -2600,7 +2639,7 @@ var errorToChatCompletionError = (error, requestData) => {
2600
2639
  };
2601
2640
 
2602
2641
  // src/lib/google/Google.tsx
2603
- var SAFETY_SETTINGS = [
2642
+ var DEFAULT_SAFETY_SETTINGS = [
2604
2643
  {
2605
2644
  category: import_vertexai.HarmCategory.HARM_CATEGORY_HATE_SPEECH,
2606
2645
  threshold: import_vertexai.HarmBlockThreshold.BLOCK_ONLY_HIGH
@@ -2622,7 +2661,7 @@ var SAFETY_SETTINGS = [
2622
2661
  threshold: import_vertexai.HarmBlockThreshold.BLOCK_ONLY_HIGH
2623
2662
  }
2624
2663
  ];
2625
- var GoogleClientContext = createContext(() => {
2664
+ var GoogleClientContext = createContext(async () => {
2626
2665
  if (defaultClient3) {
2627
2666
  return defaultClient3;
2628
2667
  }
@@ -2703,9 +2742,16 @@ function GoogleChatCompletion(props, ctx) {
2703
2742
  async function* GoogleChatCompletionInner(props, ctx) {
2704
2743
  const startTime = performance.now();
2705
2744
  const { logger, tracer, getContext } = ctx;
2706
- const retryCount = getContext(RetryCountContext);
2745
+ const { retryCount, lastError } = getContext(RetryCountContext);
2707
2746
  const span = tracer.getActiveSpan();
2708
- 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
+ );
2709
2755
  if (!client) {
2710
2756
  throw new Error(
2711
2757
  "[GoogleChatCompletion] must supply GoogleClient via context"
@@ -2754,7 +2800,7 @@ async function* GoogleChatCompletionInner(props, ctx) {
2754
2800
  });
2755
2801
  const model = client.getGenerativeModel({
2756
2802
  model: props.model,
2757
- safetySettings: SAFETY_SETTINGS
2803
+ safetySettings: props.safetySettings || DEFAULT_SAFETY_SETTINGS
2758
2804
  });
2759
2805
  let response;
2760
2806
  try {
@@ -2882,6 +2928,8 @@ var import_vertexai2 = require("@google-cloud/vertexai");
2882
2928
  Fallback,
2883
2929
  GoogleChatCompletion,
2884
2930
  GoogleClientContext,
2931
+ GoogleHarmBlockThreshold,
2932
+ GoogleHarmCategory,
2885
2933
  ImagePart,
2886
2934
  LogImplementation,
2887
2935
  NoopLogImplementation,
@@ -2892,6 +2940,7 @@ var import_vertexai2 = require("@google-cloud/vertexai");
2892
2940
  PromptInvalidOutputError,
2893
2941
  Retry,
2894
2942
  RetryCountContext,
2943
+ RetryLastErrorContext,
2895
2944
  SystemMessage,
2896
2945
  Trace,
2897
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"
@@ -2473,9 +2509,9 @@ import AnthropicClient2 from "@anthropic-ai/sdk";
2473
2509
 
2474
2510
  // src/lib/google/Google.tsx
2475
2511
  import {
2476
- VertexAI,
2512
+ HarmBlockThreshold,
2477
2513
  HarmCategory,
2478
- HarmBlockThreshold
2514
+ VertexAI
2479
2515
  } from "@google-cloud/vertexai";
2480
2516
 
2481
2517
  // src/lib/google/errors.ts
@@ -2504,7 +2540,7 @@ var errorToChatCompletionError = (error, requestData) => {
2504
2540
  };
2505
2541
 
2506
2542
  // src/lib/google/Google.tsx
2507
- var SAFETY_SETTINGS = [
2543
+ var DEFAULT_SAFETY_SETTINGS = [
2508
2544
  {
2509
2545
  category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
2510
2546
  threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH
@@ -2526,7 +2562,7 @@ var 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"
@@ -2658,7 +2701,7 @@ async function* GoogleChatCompletionInner(props, ctx) {
2658
2701
  });
2659
2702
  const model = client.getGenerativeModel({
2660
2703
  model: props.model,
2661
- safetySettings: SAFETY_SETTINGS
2704
+ safetySettings: props.safetySettings || DEFAULT_SAFETY_SETTINGS
2662
2705
  });
2663
2706
  let response;
2664
2707
  try {
@@ -2768,7 +2811,11 @@ function cleanChatCompletionRequest3(chatCompletionRequest) {
2768
2811
  }
2769
2812
 
2770
2813
  // src/lib/google/index.ts
2771
- import { VertexAI as VertexAI2 } from "@google-cloud/vertexai";
2814
+ import {
2815
+ VertexAI as VertexAI2,
2816
+ HarmBlockThreshold as HarmBlockThreshold2,
2817
+ HarmCategory as HarmCategory2
2818
+ } from "@google-cloud/vertexai";
2772
2819
  export {
2773
2820
  AIFragment,
2774
2821
  AISpanProcessor,
@@ -2785,6 +2832,8 @@ export {
2785
2832
  Fallback,
2786
2833
  GoogleChatCompletion,
2787
2834
  GoogleClientContext,
2835
+ HarmBlockThreshold2 as GoogleHarmBlockThreshold,
2836
+ HarmCategory2 as GoogleHarmCategory,
2788
2837
  ImagePart,
2789
2838
  LogImplementation,
2790
2839
  NoopLogImplementation,
@@ -2795,6 +2844,7 @@ export {
2795
2844
  PromptInvalidOutputError,
2796
2845
  Retry,
2797
2846
  RetryCountContext,
2847
+ RetryLastErrorContext,
2798
2848
  SystemMessage,
2799
2849
  Trace,
2800
2850
  UserMessage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gammatech/aijsx",
3
- "version": "0.10.1-dev.2024-06-07",
3
+ "version": "0.11.0-dev.2024-06-17",
4
4
  "description": "Rewrite of aijsx",
5
5
  "author": "Jordan Garcia",
6
6
  "license": "MIT",