@gammatech/aijsx 0.16.3-dev.2024-11-17 → 0.16.5-dev.2024-11-25

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
@@ -1,5 +1,5 @@
1
- import { L as LogChatCompletionRequest, A as AINode, C as ChatCompletionUsage, R as RenderContext, a as LogImplementation, S as SpanProcessor, b as ContextValues, c as Context, T as Tracer, d as ReadableSpan, e as SpanAttributes, f as SpanExporter, g as AIComponent, P as Prompt, h as ChatCompletionRequestPayloads, D as DebugMessage, E as EvaluatorResult, i as EvaluatorFn, j as LlmEvaluatorFn, k as PromptParsed, J as JSX } from './jsx-dev-runtime-RvJm3SlM.mjs';
2
- export { F as AIElement, m as AIFragment, B as BoundLogger, o as ChatMessage, p as ChatRole, w as CombinedLogger, v as ConsoleLogger, I as ImagePart, q as ImagePartProps, x as Literal, r as LogChatCompletionResponse, s as LogLevel, u as Logger, N as NoopLogImplementation, X as OutputParser, H as PropsOfAIComponent, y as RenderResult, G as Renderable, O as Span, K as SpanContext, Q as SpanEvent, M as SpanStatus, V as TracingContext, U as TracingContextKey, W as TracingContextManager, z as attachedContextSymbol, l as createAIElement, n as createContext, t as toDebugMessage } from './jsx-dev-runtime-RvJm3SlM.mjs';
1
+ import { L as LogChatCompletionRequest, A as AINode, C as ChatCompletionUsage, R as RenderContext, a as LogImplementation, S as SpanProcessor, b as ContextValues, c as Context, T as Tracer, d as ReadableSpan, e as SpanAttributes, f as SpanExporter, g as AIComponent, P as Prompt, h as ChatCompletionRequestPayloads, D as DebugMessage, E as EvaluatorResult, i as EvaluatorFn, j as LlmEvaluatorFn, k as PromptParsed, J as JSX } from './jsx-dev-runtime-CwVVc96-.mjs';
2
+ export { F as AIElement, m as AIFragment, B as BoundLogger, o as ChatMessage, p as ChatRole, w as CombinedLogger, v as ConsoleLogger, I as ImagePart, q as ImagePartProps, x as Literal, r as LogChatCompletionResponse, s as LogLevel, u as Logger, N as NoopLogImplementation, X as OutputParser, H as PropsOfAIComponent, y as RenderResult, G as Renderable, O as Span, K as SpanContext, Q as SpanEvent, M as SpanStatus, V as TracingContext, U as TracingContextKey, W as TracingContextManager, z as attachedContextSymbol, l as createAIElement, n as createContext, t as toDebugMessage } from './jsx-dev-runtime-CwVVc96-.mjs';
3
3
  import { ZodObject, ZodRawShape, ZodTypeAny, ZodString, z } from 'zod';
4
4
  import { OpenAI } from 'openai';
5
5
  export { OpenAI as OpenAIClient } from 'openai';
@@ -121,8 +121,10 @@ declare class SpanTreeNode {
121
121
  addChild(child: SpanTreeNode): void;
122
122
  descendents(fn: (node: SpanTreeNode) => void): void;
123
123
  everyDescendent(fn: (node: SpanTreeNode) => boolean): boolean;
124
+ getRoot(): SpanTreeNode;
124
125
  getRootByName(name: string): SpanTreeNode | null;
125
126
  getNearestAttribute<K>(key: string): K | null;
127
+ getRootSpanId(): string;
126
128
  getRootAttribute<K>(key: string): K | null;
127
129
  }
128
130
 
@@ -151,6 +153,7 @@ declare class AISpanProcessor implements SpanProcessor {
151
153
  shutdown(): Promise<void> | undefined;
152
154
  onRootPromptDone(rootSpan: ReadableSpan): Promise<void>;
153
155
  enrichSpan(span: ReadableSpan): Promise<ReadableSpan>;
156
+ protected addRootSpanId(span: ReadableSpan): Promise<ReadableSpan>;
154
157
  protected addPromptKeys(span: ReadableSpan): Promise<ReadableSpan>;
155
158
  protected evaluatePrompt(span: ReadableSpan): Promise<ReadableSpan>;
156
159
  }
@@ -195,10 +198,12 @@ declare namespace ProcessedAISpanAttributes {
195
198
  totalCost?: Cost;
196
199
  parentPromptKey?: string;
197
200
  rootPromptKey?: string;
201
+ rootSpanId: string;
198
202
  };
199
203
  type ChatCompletion<K extends keyof ChatCompletionRequestPayloads = keyof ChatCompletionRequestPayloads> = AISpanAttributes.ChatCompletion<K> & {
200
204
  parentPromptKey?: string;
201
205
  rootPromptKey?: string;
206
+ rootSpanId: string;
202
207
  };
203
208
  }
204
209
 
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { L as LogChatCompletionRequest, A as AINode, C as ChatCompletionUsage, R as RenderContext, a as LogImplementation, S as SpanProcessor, b as ContextValues, c as Context, T as Tracer, d as ReadableSpan, e as SpanAttributes, f as SpanExporter, g as AIComponent, P as Prompt, h as ChatCompletionRequestPayloads, D as DebugMessage, E as EvaluatorResult, i as EvaluatorFn, j as LlmEvaluatorFn, k as PromptParsed, J as JSX } from './jsx-dev-runtime-RvJm3SlM.js';
2
- export { F as AIElement, m as AIFragment, B as BoundLogger, o as ChatMessage, p as ChatRole, w as CombinedLogger, v as ConsoleLogger, I as ImagePart, q as ImagePartProps, x as Literal, r as LogChatCompletionResponse, s as LogLevel, u as Logger, N as NoopLogImplementation, X as OutputParser, H as PropsOfAIComponent, y as RenderResult, G as Renderable, O as Span, K as SpanContext, Q as SpanEvent, M as SpanStatus, V as TracingContext, U as TracingContextKey, W as TracingContextManager, z as attachedContextSymbol, l as createAIElement, n as createContext, t as toDebugMessage } from './jsx-dev-runtime-RvJm3SlM.js';
1
+ import { L as LogChatCompletionRequest, A as AINode, C as ChatCompletionUsage, R as RenderContext, a as LogImplementation, S as SpanProcessor, b as ContextValues, c as Context, T as Tracer, d as ReadableSpan, e as SpanAttributes, f as SpanExporter, g as AIComponent, P as Prompt, h as ChatCompletionRequestPayloads, D as DebugMessage, E as EvaluatorResult, i as EvaluatorFn, j as LlmEvaluatorFn, k as PromptParsed, J as JSX } from './jsx-dev-runtime-CwVVc96-.js';
2
+ export { F as AIElement, m as AIFragment, B as BoundLogger, o as ChatMessage, p as ChatRole, w as CombinedLogger, v as ConsoleLogger, I as ImagePart, q as ImagePartProps, x as Literal, r as LogChatCompletionResponse, s as LogLevel, u as Logger, N as NoopLogImplementation, X as OutputParser, H as PropsOfAIComponent, y as RenderResult, G as Renderable, O as Span, K as SpanContext, Q as SpanEvent, M as SpanStatus, V as TracingContext, U as TracingContextKey, W as TracingContextManager, z as attachedContextSymbol, l as createAIElement, n as createContext, t as toDebugMessage } from './jsx-dev-runtime-CwVVc96-.js';
3
3
  import { ZodObject, ZodRawShape, ZodTypeAny, ZodString, z } from 'zod';
4
4
  import { OpenAI } from 'openai';
5
5
  export { OpenAI as OpenAIClient } from 'openai';
@@ -121,8 +121,10 @@ declare class SpanTreeNode {
121
121
  addChild(child: SpanTreeNode): void;
122
122
  descendents(fn: (node: SpanTreeNode) => void): void;
123
123
  everyDescendent(fn: (node: SpanTreeNode) => boolean): boolean;
124
+ getRoot(): SpanTreeNode;
124
125
  getRootByName(name: string): SpanTreeNode | null;
125
126
  getNearestAttribute<K>(key: string): K | null;
127
+ getRootSpanId(): string;
126
128
  getRootAttribute<K>(key: string): K | null;
127
129
  }
128
130
 
@@ -151,6 +153,7 @@ declare class AISpanProcessor implements SpanProcessor {
151
153
  shutdown(): Promise<void> | undefined;
152
154
  onRootPromptDone(rootSpan: ReadableSpan): Promise<void>;
153
155
  enrichSpan(span: ReadableSpan): Promise<ReadableSpan>;
156
+ protected addRootSpanId(span: ReadableSpan): Promise<ReadableSpan>;
154
157
  protected addPromptKeys(span: ReadableSpan): Promise<ReadableSpan>;
155
158
  protected evaluatePrompt(span: ReadableSpan): Promise<ReadableSpan>;
156
159
  }
@@ -195,10 +198,12 @@ declare namespace ProcessedAISpanAttributes {
195
198
  totalCost?: Cost;
196
199
  parentPromptKey?: string;
197
200
  rootPromptKey?: string;
201
+ rootSpanId: string;
198
202
  };
199
203
  type ChatCompletion<K extends keyof ChatCompletionRequestPayloads = keyof ChatCompletionRequestPayloads> = AISpanAttributes.ChatCompletion<K> & {
200
204
  parentPromptKey?: string;
201
205
  rootPromptKey?: string;
206
+ rootSpanId: string;
202
207
  };
203
208
  }
204
209
 
package/dist/index.js CHANGED
@@ -109,7 +109,12 @@ var ImagePart = (_props) => {
109
109
  return null;
110
110
  };
111
111
  async function fetchImageAndConvertToBase64(url) {
112
- const response = await fetch(url);
112
+ let response;
113
+ try {
114
+ response = await fetch(url);
115
+ } catch (error) {
116
+ throw new Error(`Failed to fetch image URL ${url}: ${error.message}`);
117
+ }
113
118
  const contentType = response.headers.get("content-type");
114
119
  if (!contentType || !RE_ALLOWED_MEDIA_TYPES.test(contentType)) {
115
120
  throw new Error(`Unsupported media type: ${contentType}`);
@@ -1767,6 +1772,15 @@ var SpanTreeNode = class {
1767
1772
  });
1768
1773
  return result;
1769
1774
  }
1775
+ getRoot() {
1776
+ let node = this;
1777
+ let found = this;
1778
+ while (node) {
1779
+ found = node;
1780
+ node = node.parent;
1781
+ }
1782
+ return found;
1783
+ }
1770
1784
  getRootByName(name) {
1771
1785
  let node = this;
1772
1786
  let found = null;
@@ -1781,6 +1795,9 @@ var SpanTreeNode = class {
1781
1795
  getNearestAttribute(key) {
1782
1796
  return getNearestAttribute(this.parent, key);
1783
1797
  }
1798
+ getRootSpanId() {
1799
+ return this.getRoot().span.spanContext.spanId;
1800
+ }
1784
1801
  getRootAttribute(key) {
1785
1802
  return getFarthestAttribute(this, key);
1786
1803
  }
@@ -1885,7 +1902,22 @@ var AISpanProcessor = class {
1885
1902
  async enrichSpan(span) {
1886
1903
  const result1 = await this.evaluatePrompt(span);
1887
1904
  const result2 = await this.addPromptKeys(result1);
1888
- return result2;
1905
+ const result3 = await this.addRootSpanId(result2);
1906
+ return result3;
1907
+ }
1908
+ async addRootSpanId(span) {
1909
+ const node = this.spanTree.findNode(span.spanContext.spanId);
1910
+ if (!node) {
1911
+ return span;
1912
+ }
1913
+ const final = {
1914
+ ...span,
1915
+ attributes: {
1916
+ ...span.attributes,
1917
+ rootSpanId: node.getRootSpanId()
1918
+ }
1919
+ };
1920
+ return final;
1889
1921
  }
1890
1922
  async addPromptKeys(span) {
1891
1923
  if (!isChatCompletionSpan(span) && !isPromptSpan(span)) {
@@ -1897,7 +1929,8 @@ var AISpanProcessor = class {
1897
1929
  }
1898
1930
  const extraAttributes = {
1899
1931
  parentPromptKey: node.getNearestAttribute("promptKey"),
1900
- rootPromptKey: node.getRootAttribute("promptKey")
1932
+ rootPromptKey: node.getRootAttribute("promptKey"),
1933
+ rootSpanId: node.getRootSpanId()
1901
1934
  };
1902
1935
  const final = {
1903
1936
  ...span,
@@ -2663,7 +2696,6 @@ async function* AnthropicChatCompletionInner(props, ctx) {
2663
2696
  const cacheCreated = inputUsage.cache_creation_input_tokens || 0;
2664
2697
  const cacheRead = inputUsage.cache_read_input_tokens || 0;
2665
2698
  const promptTokens = inputUsage.input_tokens + cacheCreated + cacheRead;
2666
- logger.info(`inputUsage: ${JSON.stringify(inputUsage, null, 2)}`);
2667
2699
  tokensUsed.prompt = promptTokens;
2668
2700
  tokensUsed.cachedPromptTokensCreated = cacheCreated;
2669
2701
  tokensUsed.cachedPromptTokensRead = cacheRead;
package/dist/index.mjs CHANGED
@@ -41,7 +41,12 @@ var ImagePart = (_props) => {
41
41
  return null;
42
42
  };
43
43
  async function fetchImageAndConvertToBase64(url) {
44
- const response = await fetch(url);
44
+ let response;
45
+ try {
46
+ response = await fetch(url);
47
+ } catch (error) {
48
+ throw new Error(`Failed to fetch image URL ${url}: ${error.message}`);
49
+ }
45
50
  const contentType = response.headers.get("content-type");
46
51
  if (!contentType || !RE_ALLOWED_MEDIA_TYPES.test(contentType)) {
47
52
  throw new Error(`Unsupported media type: ${contentType}`);
@@ -1666,6 +1671,15 @@ var SpanTreeNode = class {
1666
1671
  });
1667
1672
  return result;
1668
1673
  }
1674
+ getRoot() {
1675
+ let node = this;
1676
+ let found = this;
1677
+ while (node) {
1678
+ found = node;
1679
+ node = node.parent;
1680
+ }
1681
+ return found;
1682
+ }
1669
1683
  getRootByName(name) {
1670
1684
  let node = this;
1671
1685
  let found = null;
@@ -1680,6 +1694,9 @@ var SpanTreeNode = class {
1680
1694
  getNearestAttribute(key) {
1681
1695
  return getNearestAttribute(this.parent, key);
1682
1696
  }
1697
+ getRootSpanId() {
1698
+ return this.getRoot().span.spanContext.spanId;
1699
+ }
1683
1700
  getRootAttribute(key) {
1684
1701
  return getFarthestAttribute(this, key);
1685
1702
  }
@@ -1784,7 +1801,22 @@ var AISpanProcessor = class {
1784
1801
  async enrichSpan(span) {
1785
1802
  const result1 = await this.evaluatePrompt(span);
1786
1803
  const result2 = await this.addPromptKeys(result1);
1787
- return result2;
1804
+ const result3 = await this.addRootSpanId(result2);
1805
+ return result3;
1806
+ }
1807
+ async addRootSpanId(span) {
1808
+ const node = this.spanTree.findNode(span.spanContext.spanId);
1809
+ if (!node) {
1810
+ return span;
1811
+ }
1812
+ const final = {
1813
+ ...span,
1814
+ attributes: {
1815
+ ...span.attributes,
1816
+ rootSpanId: node.getRootSpanId()
1817
+ }
1818
+ };
1819
+ return final;
1788
1820
  }
1789
1821
  async addPromptKeys(span) {
1790
1822
  if (!isChatCompletionSpan(span) && !isPromptSpan(span)) {
@@ -1796,7 +1828,8 @@ var AISpanProcessor = class {
1796
1828
  }
1797
1829
  const extraAttributes = {
1798
1830
  parentPromptKey: node.getNearestAttribute("promptKey"),
1799
- rootPromptKey: node.getRootAttribute("promptKey")
1831
+ rootPromptKey: node.getRootAttribute("promptKey"),
1832
+ rootSpanId: node.getRootSpanId()
1800
1833
  };
1801
1834
  const final = {
1802
1835
  ...span,
@@ -2562,7 +2595,6 @@ async function* AnthropicChatCompletionInner(props, ctx) {
2562
2595
  const cacheCreated = inputUsage.cache_creation_input_tokens || 0;
2563
2596
  const cacheRead = inputUsage.cache_read_input_tokens || 0;
2564
2597
  const promptTokens = inputUsage.input_tokens + cacheCreated + cacheRead;
2565
- logger.info(`inputUsage: ${JSON.stringify(inputUsage, null, 2)}`);
2566
2598
  tokensUsed.prompt = promptTokens;
2567
2599
  tokensUsed.cachedPromptTokensCreated = cacheCreated;
2568
2600
  tokensUsed.cachedPromptTokensRead = cacheRead;
@@ -107,10 +107,10 @@ type EvaluatorResult = {
107
107
  debug?: string;
108
108
  };
109
109
  type EvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (variables: V, result: O) => Promise<EvaluatorResult | EvaluatorResult[]>;
110
- type LlmEvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (ctx: RenderContext, options: {
110
+ type LlmEvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any, E = O> = (ctx: RenderContext, options: {
111
111
  variables: V;
112
112
  output: O;
113
- expected?: O;
113
+ expected?: E;
114
114
  }) => Promise<EvaluatorResult | EvaluatorResult[]>;
115
115
  type OutputParser<O> = {
116
116
  schema: ZodTypeAny;
@@ -107,10 +107,10 @@ type EvaluatorResult = {
107
107
  debug?: string;
108
108
  };
109
109
  type EvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (variables: V, result: O) => Promise<EvaluatorResult | EvaluatorResult[]>;
110
- type LlmEvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any> = (ctx: RenderContext, options: {
110
+ type LlmEvaluatorFn<V extends Record<string, any> = Record<string, any>, O = any, E = O> = (ctx: RenderContext, options: {
111
111
  variables: V;
112
112
  output: O;
113
- expected?: O;
113
+ expected?: E;
114
114
  }) => Promise<EvaluatorResult | EvaluatorResult[]>;
115
115
  type OutputParser<O> = {
116
116
  schema: ZodTypeAny;
@@ -1,2 +1,2 @@
1
- export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-RvJm3SlM.mjs';
1
+ export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-CwVVc96-.mjs';
2
2
  import 'zod';
@@ -1,2 +1,2 @@
1
- export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-RvJm3SlM.js';
1
+ export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-CwVVc96-.js';
2
2
  import 'zod';
@@ -1,2 +1,2 @@
1
- export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-RvJm3SlM.mjs';
1
+ export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-CwVVc96-.mjs';
2
2
  import 'zod';
@@ -1,2 +1,2 @@
1
- export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-RvJm3SlM.js';
1
+ export { $ as Fragment, J as JSX, Y as jsx, Z as jsxDEV, _ as jsxs } from './jsx-dev-runtime-CwVVc96-.js';
2
2
  import 'zod';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gammatech/aijsx",
3
- "version": "0.16.3-dev.2024-11-17",
3
+ "version": "0.16.5-dev.2024-11-25",
4
4
  "description": "Rewrite of aijsx",
5
5
  "author": "Jordan Garcia",
6
6
  "license": "MIT",