@outputai/llm 0.8.2-next.edf06bb.0 → 0.9.1-dev.9f7b159.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@outputai/llm",
3
- "version": "0.8.2-next.edf06bb.0",
3
+ "version": "0.9.1-dev.9f7b159.0",
4
4
  "description": "Framework abstraction to interact with LLM models",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
@@ -13,7 +13,7 @@
13
13
  "gray-matter": "4.0.3",
14
14
  "liquidjs": "10.25.7",
15
15
  "undici": "8.5.0",
16
- "@outputai/core": "0.8.2-next.edf06bb.0"
16
+ "@outputai/core": "0.9.1-dev.9f7b159.0"
17
17
  },
18
18
  "devDependencies": {
19
19
  "@ai-sdk/amazon-bedrock": "4.0.111",
@@ -36,5 +36,8 @@
36
36
  "license": "Apache-2.0",
37
37
  "publishConfig": {
38
38
  "access": "public"
39
+ },
40
+ "scripts": {
41
+ "build": "pnpm exec tsc -p ./tsconfig.typecheck.json"
39
42
  }
40
43
  }
@@ -1,3 +1,5 @@
1
+ import { Logger } from '@outputai/core';
2
+
1
3
  const costTableUrl = 'https://models.dev/api.json';
2
4
  const cacheTTL = 1000 * 60 * 60 * 24; // 1 day
3
5
 
@@ -26,10 +28,10 @@ export const fetchModelsPricing = async () => {
26
28
  const res = await fetch( costTableUrl );
27
29
  if ( !res.ok ) {
28
30
  if ( cache.content ) {
29
- console.warn( `Error ${res.status} when fetching models pricing at ${costTableUrl}, falling back to stale cache` );
31
+ Logger.warn( `Error ${res.status} when fetching models pricing at ${costTableUrl}, falling back to stale cache`, { namespace: 'LLM' } );
30
32
  return cache.content;
31
33
  }
32
- console.error( `Error ${res.status} when fetching models pricing at ${costTableUrl}` );
34
+ Logger.error( `Error ${res.status} when fetching models pricing at ${costTableUrl}`, { namespace: 'LLM' } );
33
35
  return null;
34
36
  }
35
37
  cache.content = buildModelMap( await res.json() );
@@ -9,24 +9,29 @@ const fixturePath = join( __dirname, 'fixtures', 'models_api_light.json' );
9
9
  const fixture = JSON.parse( readFileSync( fixturePath, 'utf8' ) );
10
10
 
11
11
  const costTableUrl = 'https://models.dev/api.json';
12
- const errNoCache = status => `Error ${status} when fetching models pricing at ${costTableUrl}`;
13
- const warnStaleCache = status => `Error ${status} when fetching models pricing at ${costTableUrl}, falling back to stale cache`;
12
+ const okResponse = data => ( {
13
+ ok: true,
14
+ json: () => Promise.resolve( data )
15
+ } );
16
+ const stubFetch = response => {
17
+ const fetchMock = vi.fn().mockResolvedValue( response );
18
+ vi.stubGlobal( 'fetch', fetchMock );
19
+ return fetchMock;
20
+ };
14
21
 
15
22
  describe( 'fetchModelsPricing', () => {
16
23
  beforeEach( () => {
17
24
  cache.content = null;
18
25
  cache.expiresAt = 0;
19
- vi.restoreAllMocks();
26
+ vi.unstubAllGlobals();
20
27
  } );
21
28
 
22
29
  it( 'returns a Map of model costs when fetch succeeds', async () => {
23
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( {
24
- ok: true,
25
- json: () => Promise.resolve( fixture )
26
- } ) );
30
+ const fetchMock = stubFetch( okResponse( fixture ) );
27
31
 
28
32
  const result = await fetchModelsPricing();
29
33
 
34
+ expect( fetchMock ).toHaveBeenCalledWith( costTableUrl );
30
35
  expect( result ).toBeInstanceOf( Map );
31
36
  expect( result.size ).toBeGreaterThan( 0 );
32
37
  const firstModel = Object.values( fixture )[0];
@@ -37,10 +42,7 @@ describe( 'fetchModelsPricing', () => {
37
42
  } );
38
43
 
39
44
  it( 'includes main providers from fixture (openai, anthropic, google, nvidia, perplexity)', async () => {
40
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( {
41
- ok: true,
42
- json: () => Promise.resolve( fixture )
43
- } ) );
45
+ stubFetch( okResponse( fixture ) );
44
46
 
45
47
  const result = await fetchModelsPricing();
46
48
 
@@ -55,47 +57,35 @@ describe( 'fetchModelsPricing', () => {
55
57
 
56
58
  it( 'returns null when response is not ok and no cache', async () => {
57
59
  const status = 500;
58
- const err = vi.spyOn( console, 'error' ).mockImplementation( () => {} );
59
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( { ok: false, status } ) );
60
+ stubFetch( { ok: false, status } );
60
61
 
61
62
  const result = await fetchModelsPricing();
62
63
 
63
64
  expect( result ).toBeNull();
64
- expect( err ).toHaveBeenCalledWith( errNoCache( status ) );
65
- err.mockRestore();
66
65
  } );
67
66
 
68
67
  it( 'returns stale cache when response is not ok but cache exists', async () => {
69
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( {
70
- ok: true,
71
- json: () => Promise.resolve( fixture )
72
- } ) );
68
+ stubFetch( okResponse( fixture ) );
73
69
  await fetchModelsPricing();
74
70
  cache.expiresAt = 0; // force refetch so we hit the !res.ok path
75
71
 
76
72
  const status = 404;
77
- const warn = vi.spyOn( console, 'warn' ).mockImplementation( () => {} );
78
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( { ok: false, status } ) );
73
+ stubFetch( { ok: false, status } );
79
74
 
80
75
  const result = await fetchModelsPricing();
81
76
 
82
77
  expect( result ).toBeInstanceOf( Map );
83
78
  expect( result.size ).toBeGreaterThan( 0 );
84
- expect( warn ).toHaveBeenCalledWith( warnStaleCache( status ) );
85
- warn.mockRestore();
86
79
  } );
87
80
 
88
81
  it( 'returns cached Map when cache is still valid', async () => {
89
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( {
90
- ok: true,
91
- json: () => Promise.resolve( fixture )
92
- } ) );
82
+ const fetchMock = stubFetch( okResponse( fixture ) );
93
83
 
94
84
  const first = await fetchModelsPricing();
95
85
  const second = await fetchModelsPricing();
96
86
 
97
87
  expect( first ).toBe( second );
98
- expect( fetch ).toHaveBeenCalledTimes( 1 );
88
+ expect( fetchMock ).toHaveBeenCalledTimes( 1 );
99
89
  } );
100
90
 
101
91
  it( 'only stores models that have a cost object', async () => {
@@ -108,10 +98,7 @@ describe( 'fetchModelsPricing', () => {
108
98
  }
109
99
  }
110
100
  };
111
- vi.stubGlobal( 'fetch', vi.fn().mockResolvedValue( {
112
- ok: true,
113
- json: () => Promise.resolve( dataWithMissingCost )
114
- } ) );
101
+ stubFetch( okResponse( dataWithMissingCost ) );
115
102
 
116
103
  const result = await fetchModelsPricing();
117
104
 
package/src/cost/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { fetchModelsPricing } from './fetch_models_pricing.js';
2
2
  import { Tracing } from '@outputai/core/sdk/runtime';
3
+ import { Logger } from '@outputai/core';
3
4
 
4
5
  /**
5
6
  * Calculates the cost of an llm call based on the model and usage.
@@ -13,13 +14,13 @@ export const calculateLLMCallCost = async ( { modelId, usage } ) => {
13
14
  const models = await fetchModelsPricing();
14
15
 
15
16
  if ( !models ) {
16
- console.warn( 'Failed to fetch models pricing' );
17
+ Logger.warn( 'Failed to fetch models pricing', { namespace: 'LLM' } );
17
18
  return null;
18
19
  }
19
20
 
20
21
  const pricing = models.get( modelId );
21
22
  if ( !pricing ) {
22
- console.warn( 'Missing cost reference for model' );
23
+ Logger.warn( 'Missing cost reference for model', { namespace: 'LLM' } );
23
24
  return null;
24
25
  }
25
26
 
@@ -50,7 +51,7 @@ export const calculateLLMCallCost = async ( { modelId, usage } ) => {
50
51
 
51
52
  return llmUsage;
52
53
  } catch ( error ) {
53
- console.error( 'Error calculating LLM call costs', error );
54
+ Logger.error( 'Error calculating LLM call costs', { error: error.message, namespace: 'LLM' } );
54
55
  return null;
55
56
  }
56
57
  };
@@ -61,8 +61,6 @@ const expectLLMUsage = ( result, { modelId, usage, total, tokensUsed } ) => {
61
61
  describe( 'calculateLLMCallCost', () => {
62
62
  beforeEach( () => {
63
63
  vi.clearAllMocks();
64
- vi.spyOn( console, 'warn' ).mockImplementation( () => {} );
65
- vi.spyOn( console, 'error' ).mockImplementation( () => {} );
66
64
  } );
67
65
 
68
66
  afterEach( () => {
@@ -78,7 +76,6 @@ describe( 'calculateLLMCallCost', () => {
78
76
  } );
79
77
 
80
78
  expect( result ).toBeNull();
81
- expect( console.warn ).toHaveBeenCalledWith( 'Failed to fetch models pricing' );
82
79
  } );
83
80
 
84
81
  it( 'returns null when model is missing from cost table', async () => {
@@ -90,7 +87,6 @@ describe( 'calculateLLMCallCost', () => {
90
87
  } );
91
88
 
92
89
  expect( result ).toBeNull();
93
- expect( console.warn ).toHaveBeenCalledWith( 'Missing cost reference for model' );
94
90
  } );
95
91
 
96
92
  it( 'calculates input and output usage from model pricing', async () => {
@@ -283,6 +279,5 @@ describe( 'calculateLLMCallCost', () => {
283
279
  } );
284
280
 
285
281
  expect( result ).toBeNull();
286
- expect( console.error ).toHaveBeenCalledWith( 'Error calculating LLM call costs', error );
287
282
  } );
288
283
  } );
@@ -1,4 +1,4 @@
1
- import { ValidationError, z } from '@outputai/core';
1
+ import { Logger, ValidationError, z } from '@outputai/core';
2
2
  import { attributesSchema } from './block_options.js';
3
3
 
4
4
  const toolConfigSchema = z.record( z.string(), z.unknown() );
@@ -70,12 +70,12 @@ const SNAKE_CASE_WARNINGS = {
70
70
  function warnSnakeCaseFields( config ) {
71
71
  for ( const [ snake, camel ] of Object.entries( SNAKE_CASE_WARNINGS ) ) {
72
72
  if ( Object.hasOwn( config, snake ) ) {
73
- console.warn( `[output-llm] "${snake}" found in prompt config. Did you mean "${camel}"?` );
73
+ Logger.warn( `[output-llm] "${snake}" found in prompt config. Did you mean "${camel}"?`, { namespace: 'LLM' } );
74
74
  }
75
75
  }
76
76
  const thinking = config.providerOptions?.thinking;
77
77
  if ( thinking && Object.hasOwn( thinking, 'budget_tokens' ) ) {
78
- console.warn( '[output-llm] "budget_tokens" found in providerOptions.thinking. Did you mean "budgetTokens"?' );
78
+ Logger.warn( '[output-llm] "budget_tokens" found in providerOptions.thinking. Did you mean "budgetTokens"?', { namespace: 'LLM' } );
79
79
  }
80
80
  }
81
81
 
@@ -1,4 +1,4 @@
1
- import { describe, it, expect, vi } from 'vitest';
1
+ import { describe, it, expect } from 'vitest';
2
2
  import { ValidationError } from '@outputai/core';
3
3
  import { validatePrompt } from './validations.js';
4
4
 
@@ -548,8 +548,6 @@ describe( 'validatePrompt', () => {
548
548
  } );
549
549
 
550
550
  it( 'should pass through budget_tokens in thinking and warn about snake_case', () => {
551
- const warnSpy = vi.spyOn( console, 'warn' ).mockImplementation( () => {} );
552
-
553
551
  const promptWithBudgetTokensSnake = {
554
552
  name: 'thinking-budget-snake',
555
553
  config: {
@@ -571,11 +569,6 @@ describe( 'validatePrompt', () => {
571
569
  };
572
570
 
573
571
  expect( () => validatePrompt( promptWithBudgetTokensSnake ) ).not.toThrow();
574
- expect( warnSpy ).toHaveBeenCalledWith(
575
- '[output-llm] "budget_tokens" found in providerOptions.thinking. Did you mean "budgetTokens"?'
576
- );
577
-
578
- warnSpy.mockRestore();
579
572
  } );
580
573
 
581
574
  it( 'should allow snake_case fields in config via passthrough (no longer strict)', () => {