@outputai/core 0.15.1-next.69acd83.0 → 0.15.1-next.755b932.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 +1 -1
- package/src/llm/agent.js +6 -0
- package/src/llm/generate.js +13 -3
- package/src/llm/utils/cost.js +7 -6
- package/src/llm/utils/metering.js +10 -2
- package/src/llm/utils/models_pricing.js +5 -2
- package/src/llm/utils/models_pricing_snapshot.json +850 -425
- package/src/llm/utils/tools.js +14 -0
- package/src/llm/utils/usage.js +11 -12
- package/src/llm/utils/web_search.js +155 -0
- package/src/llm/utils/wrap.js +6 -4
- package/src/llm/utils/grounding.js +0 -49
package/package.json
CHANGED
package/src/llm/agent.js
CHANGED
|
@@ -5,6 +5,7 @@ import { Role } from './consts.js';
|
|
|
5
5
|
import { drainStream } from './utils/stream.js';
|
|
6
6
|
import { loadPrompt } from './prompt/loader.js';
|
|
7
7
|
import { loadSkills } from './utils/skills.js';
|
|
8
|
+
import { extractProviderToolIds } from './utils/tools.js';
|
|
8
9
|
import * as Validator from './validations.js';
|
|
9
10
|
import { Logger } from '#runtime_logger';
|
|
10
11
|
|
|
@@ -13,6 +14,7 @@ export class Agent {
|
|
|
13
14
|
#prompt;
|
|
14
15
|
#initialMessages;
|
|
15
16
|
#store;
|
|
17
|
+
#toolIdsByName;
|
|
16
18
|
|
|
17
19
|
constructor( args ) {
|
|
18
20
|
const { promptFile, promptObject, promptDir, variables, tools, output, stopWhen, messageStore } = Validator.parseAgentArgs( args );
|
|
@@ -32,6 +34,7 @@ export class Agent {
|
|
|
32
34
|
// seed only <user> turns into each generate()/stream() call.
|
|
33
35
|
this.#initialMessages = messages.filter( m => m.role === Role.USER );
|
|
34
36
|
this.#store = messageStore ?? null;
|
|
37
|
+
this.#toolIdsByName = extractProviderToolIds( aiOptions.tools );
|
|
35
38
|
}
|
|
36
39
|
|
|
37
40
|
async #combineWithPreviousMessages( messages ) {
|
|
@@ -51,6 +54,7 @@ export class Agent {
|
|
|
51
54
|
return wrapTextGeneration( {
|
|
52
55
|
name: 'Agent.generate',
|
|
53
56
|
prompt: this.#prompt,
|
|
57
|
+
toolIdsByName: this.#toolIdsByName,
|
|
54
58
|
fn: async ( { onStepEndHook } ) => {
|
|
55
59
|
const response = await this.#agent.generate( {
|
|
56
60
|
messages: combinedMessages,
|
|
@@ -77,6 +81,7 @@ export class Agent {
|
|
|
77
81
|
return wrapTextGeneration( {
|
|
78
82
|
name: 'Agent.generateWithStreaming',
|
|
79
83
|
prompt: this.#prompt,
|
|
84
|
+
toolIdsByName: this.#toolIdsByName,
|
|
80
85
|
fn: async ( { onStepEndHook } ) => {
|
|
81
86
|
const state = { response: null };
|
|
82
87
|
const stream = await this.#agent.stream( {
|
|
@@ -115,6 +120,7 @@ export class Agent {
|
|
|
115
120
|
return wrapStream( {
|
|
116
121
|
name: 'Agent.stream',
|
|
117
122
|
prompt: this.#prompt,
|
|
123
|
+
toolIdsByName: this.#toolIdsByName,
|
|
118
124
|
abortSignal,
|
|
119
125
|
fn: ( { onEndHook, onErrorHook, onStepEndHook, onAbortHook } ) => this.#agent.stream( {
|
|
120
126
|
messages: combinedMessages,
|
package/src/llm/generate.js
CHANGED
|
@@ -4,18 +4,22 @@ import { wrapImageGeneration, wrapStream, wrapTextGeneration } from './utils/wra
|
|
|
4
4
|
import { loadAiSdkTextOptions, loadAiSdkImageOptions } from './ai_sdk_options.js';
|
|
5
5
|
import { drainStream } from './utils/stream.js';
|
|
6
6
|
import { loadSkills } from './utils/skills.js';
|
|
7
|
+
import { extractProviderToolIds } from './utils/tools.js';
|
|
7
8
|
import * as Validator from './validations.js';
|
|
8
9
|
|
|
9
10
|
export const generateText = async args => {
|
|
10
11
|
const { promptFile, promptObject, variables, promptDir, ...aiOptions } = Validator.parseGenerateTextArgs( args );
|
|
11
12
|
const prompt = promptObject ?? loadPrompt( promptFile, variables, promptDir );
|
|
12
13
|
const skills = loadSkills( prompt );
|
|
14
|
+
const options = loadAiSdkTextOptions( { prompt, skills, ...aiOptions } );
|
|
15
|
+
const toolIdsByName = extractProviderToolIds( options.tools );
|
|
13
16
|
|
|
14
17
|
return wrapTextGeneration( {
|
|
15
18
|
name: 'generateText',
|
|
16
19
|
prompt,
|
|
20
|
+
toolIdsByName,
|
|
17
21
|
fn: ( { onStepEndHook } ) => AI.generateText( {
|
|
18
|
-
...
|
|
22
|
+
...options,
|
|
19
23
|
onStepEnd: onStepEndHook
|
|
20
24
|
} )
|
|
21
25
|
} );
|
|
@@ -25,13 +29,16 @@ export const streamText = args => {
|
|
|
25
29
|
const { promptFile, promptObject, variables, promptDir, onEnd, onError, onChunk, ...aiOptions } = Validator.parseStreamTextArgs( args );
|
|
26
30
|
const prompt = promptObject ?? loadPrompt( promptFile, variables, promptDir );
|
|
27
31
|
const skills = loadSkills( prompt );
|
|
32
|
+
const options = loadAiSdkTextOptions( { prompt, skills, ...aiOptions } );
|
|
33
|
+
const toolIdsByName = extractProviderToolIds( options.tools );
|
|
28
34
|
|
|
29
35
|
return wrapStream( {
|
|
30
36
|
name: 'streamText',
|
|
31
37
|
prompt,
|
|
38
|
+
toolIdsByName,
|
|
32
39
|
abortSignal: aiOptions.abortSignal,
|
|
33
40
|
fn: ( { onEndHook, onErrorHook, onStepEndHook, onAbortHook } ) => AI.streamText( {
|
|
34
|
-
...
|
|
41
|
+
...options,
|
|
35
42
|
...( onChunk && { onChunk } ),
|
|
36
43
|
onStepEnd: onStepEndHook,
|
|
37
44
|
onAbort: onAbortHook,
|
|
@@ -48,14 +55,17 @@ export const generateTextWithStreaming = async args => {
|
|
|
48
55
|
const { promptFile, promptObject, variables, promptDir, onChunk, ...aiOptions } = Validator.parseGenerateTextWithStreamingArgs( args );
|
|
49
56
|
const prompt = promptObject ?? loadPrompt( promptFile, variables, promptDir );
|
|
50
57
|
const skills = loadSkills( prompt );
|
|
58
|
+
const options = loadAiSdkTextOptions( { prompt, skills, ...aiOptions } );
|
|
59
|
+
const toolIdsByName = extractProviderToolIds( options.tools );
|
|
51
60
|
|
|
52
61
|
return wrapTextGeneration( {
|
|
53
62
|
name: 'generateTextWithStreaming',
|
|
54
63
|
prompt,
|
|
64
|
+
toolIdsByName,
|
|
55
65
|
fn: async ( { onStepEndHook } ) => {
|
|
56
66
|
const state = { response: null };
|
|
57
67
|
const stream = AI.streamText( {
|
|
58
|
-
...
|
|
68
|
+
...options,
|
|
59
69
|
...( onChunk && { onChunk } ),
|
|
60
70
|
onStepEnd: onStepEndHook,
|
|
61
71
|
onEnd: res => {
|
package/src/llm/utils/cost.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { fetchModelsPricing, Freshness } from './models_pricing.js';
|
|
2
2
|
import { BaseAttribute } from '#trace_attribute';
|
|
3
3
|
import { LLMGenerationUsage, LLMGenerationUsageItem } from './usage.js';
|
|
4
|
-
import {
|
|
4
|
+
import { resolveWebSearchPpm } from './web_search.js';
|
|
5
5
|
import { Logger } from '#runtime_logger';
|
|
6
6
|
import Decimal from 'decimal.js';
|
|
7
7
|
|
|
@@ -88,10 +88,11 @@ const resolveValue = values => {
|
|
|
88
88
|
return { ppm: null, status: LLMGenerationCostItem.Status.MISSING };
|
|
89
89
|
};
|
|
90
90
|
|
|
91
|
-
const resolvePrice = ( { group, label,
|
|
91
|
+
const resolvePrice = ( { providerId, group, label, modelPricing } ) => {
|
|
92
|
+
const pricing = modelPricing?.cost;
|
|
92
93
|
// Tool charges are never token-priced: models.dev has no rate for them.
|
|
93
94
|
if ( group === LLMGenerationUsageItem.Group.TOOLS ) {
|
|
94
|
-
return resolveValue( [
|
|
95
|
+
return resolveValue( [ resolveWebSearchPpm( providerId, label, modelPricing ) ] );
|
|
95
96
|
}
|
|
96
97
|
if ( !pricing ) {
|
|
97
98
|
return resolveValue( [] );
|
|
@@ -129,13 +130,13 @@ export const calculateCosts = async usage => {
|
|
|
129
130
|
|
|
130
131
|
const { providerId, modelId } = usage;
|
|
131
132
|
|
|
132
|
-
const
|
|
133
|
-
if ( !
|
|
133
|
+
const modelPricing = models.get( `${providerId}/${modelId}` );
|
|
134
|
+
if ( !modelPricing ) {
|
|
134
135
|
Logger.warn( 'Missing pricing reference for model', { namespace: 'LLM', modelId, providerId } );
|
|
135
136
|
}
|
|
136
137
|
|
|
137
138
|
const items = usage.items.map( ( { group, label, amount } ) => {
|
|
138
|
-
const { ppm, status } = resolvePrice( {
|
|
139
|
+
const { ppm, status } = resolvePrice( { providerId, modelPricing, group, label } );
|
|
139
140
|
const total = exists( ppm ) ? Decimal( amount ).div( 1_000_000 ).mul( ppm ).toNumber() : null;
|
|
140
141
|
return new LLMGenerationCostItem( group, label, amount, ppm, total, status );
|
|
141
142
|
} );
|
|
@@ -18,6 +18,7 @@ export class Metering {
|
|
|
18
18
|
#billed = false;
|
|
19
19
|
#prompt;
|
|
20
20
|
#traceId;
|
|
21
|
+
#toolIdsByName;
|
|
21
22
|
#attributes = {
|
|
22
23
|
usage: null,
|
|
23
24
|
cost: null,
|
|
@@ -25,7 +26,12 @@ export class Metering {
|
|
|
25
26
|
};
|
|
26
27
|
|
|
27
28
|
async #createAttributes( { usage, steps } ) {
|
|
28
|
-
this.#attributes.usage = parseLLMUsage( {
|
|
29
|
+
this.#attributes.usage = parseLLMUsage( {
|
|
30
|
+
prompt: this.#prompt,
|
|
31
|
+
usage,
|
|
32
|
+
steps,
|
|
33
|
+
toolIdsByName: this.#toolIdsByName
|
|
34
|
+
} );
|
|
29
35
|
if ( this.#attributes.usage ) {
|
|
30
36
|
this.#attributes.cost = await calculateCosts( this.#attributes.usage );
|
|
31
37
|
if ( this.#attributes.cost ) {
|
|
@@ -60,10 +66,12 @@ export class Metering {
|
|
|
60
66
|
* @param {object} args
|
|
61
67
|
* @param {string} args.traceId - Trace event the usage and cost attributes are attached to
|
|
62
68
|
* @param {object} args.prompt - Loaded prompt (`config.provider` / `config.model` used for cost)
|
|
69
|
+
* @param {Record<string, string>} [args.toolIdsByName] - Provider tool ids keyed by configured name
|
|
63
70
|
*/
|
|
64
|
-
constructor( { traceId, prompt } ) {
|
|
71
|
+
constructor( { traceId, prompt, toolIdsByName } ) {
|
|
65
72
|
this.#traceId = traceId;
|
|
66
73
|
this.#prompt = prompt;
|
|
74
|
+
this.#toolIdsByName = toolIdsByName;
|
|
67
75
|
}
|
|
68
76
|
|
|
69
77
|
/**
|
|
@@ -33,9 +33,12 @@ const parseData = data => {
|
|
|
33
33
|
if ( providerId === '_meta' ) {
|
|
34
34
|
continue;
|
|
35
35
|
}
|
|
36
|
-
for ( const [ modelName, { cost } ] of Object.entries( provider.models ?? {} ) ) {
|
|
36
|
+
for ( const [ modelName, { cost, reasoning } ] of Object.entries( provider.models ?? {} ) ) {
|
|
37
37
|
if ( cost ) { // some models don't have cost
|
|
38
|
-
map.set( `${providerId}/${modelName}`,
|
|
38
|
+
map.set( `${providerId}/${modelName}`, {
|
|
39
|
+
cost,
|
|
40
|
+
reasoning: reasoning === true
|
|
41
|
+
} );
|
|
39
42
|
}
|
|
40
43
|
}
|
|
41
44
|
}
|