-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Expand file tree
/
Copy pathindex.ts
More file actions
227 lines (194 loc) · 7.25 KB
/
Copy pathindex.ts
File metadata and controls
227 lines (194 loc) · 7.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { getApiKeyWithBYOK } from '@/lib/api-key/byok'
import type { StreamingExecution } from '@/executor/types'
import {
applyModelCostPolicy,
applySegmentCostPolicy,
calculateBillableModelCost,
installStreamingCostPolicy,
type ModelCostPolicy,
resolveModelCostPolicy,
withoutToolCost,
} from '@/providers/cost-policy'
import {
attachLargeFileRemoteUrls,
uploadLargeFilesToProvider,
} from '@/providers/file-attachments.server'
import { getProviderExecutor } from '@/providers/registry'
import type { ProviderId, ProviderRequest, ProviderResponse } from '@/providers/types'
import {
generateStructuredOutputInstructions,
sumToolCosts,
supportsPromptCaching,
supportsReasoningEffort,
supportsTemperature,
supportsThinking,
supportsVerbosity,
} from '@/providers/utils'
const logger = createLogger('Providers')
/**
* Maximum number of iterations for tool call loops to prevent infinite loops.
* Used across all providers that support tool/function calling.
*/
export const MAX_TOOL_ITERATIONS = 20
function sanitizeRequest(request: ProviderRequest): ProviderRequest {
const sanitizedRequest = { ...request }
const model = sanitizedRequest.model
if (model && !supportsTemperature(model)) {
sanitizedRequest.temperature = undefined
}
if (model && !supportsReasoningEffort(model)) {
sanitizedRequest.reasoningEffort = undefined
}
if (model && !supportsVerbosity(model)) {
sanitizedRequest.verbosity = undefined
}
if (model && !supportsThinking(model)) {
sanitizedRequest.thinkingLevel = undefined
}
if (model && !supportsPromptCaching(model)) {
sanitizedRequest.promptCaching = undefined
}
return sanitizedRequest
}
function isStreamingExecution(response: any): response is StreamingExecution {
return response && typeof response === 'object' && 'stream' in response && 'execution' in response
}
function isReadableStream(response: any): response is ReadableStream {
return response instanceof ReadableStream
}
/**
* Applies the shared model-cost policy to a streaming response.
*
* The streaming and non-streaming paths must charge identically for the same
* model and tokens, but streaming providers write their cost from inside the
* stream drain — long after this function returns — so the policy is installed
* on the live output object rather than applied to a value.
*/
function applyStreamingCostPolicy(response: StreamingExecution, policy: ModelCostPolicy): void {
const output = response.execution?.output
if (!output || typeof output !== 'object') {
logger.warn('Streaming output unavailable at intercept time; cost policy not applied')
return
}
installStreamingCostPolicy(output, policy)
const segments = output.providerTiming?.timeSegments
if (Array.isArray(segments)) {
applySegmentCostPolicy(segments, policy)
}
}
export async function executeProviderRequest(
providerId: string,
request: ProviderRequest
): Promise<ProviderResponse | ReadableStream | StreamingExecution> {
const provider = await getProviderExecutor(providerId as ProviderId)
if (!provider) {
throw new Error(`Provider not found: ${providerId}`)
}
if (!provider.executeRequest) {
throw new Error(`Provider ${providerId} does not implement executeRequest`)
}
let resolvedRequest = sanitizeRequest(request)
let isBYOK = false
if (request.workspaceId) {
try {
const result = await getApiKeyWithBYOK(
providerId,
request.model,
request.workspaceId,
request.apiKey
)
resolvedRequest = { ...resolvedRequest, apiKey: result.apiKey }
isBYOK = result.isBYOK
logger.info('API key resolved', {
provider: providerId,
model: request.model,
workspaceId: request.workspaceId,
isBYOK,
})
} catch (error) {
logger.error('Failed to resolve API key:', {
provider: providerId,
model: request.model,
error: toError(error).message,
})
throw error
}
}
resolvedRequest.isBYOK = isBYOK
const sanitizedRequest = resolvedRequest
if (sanitizedRequest.responseFormat) {
if (
typeof sanitizedRequest.responseFormat === 'string' &&
sanitizedRequest.responseFormat === ''
) {
logger.info('Empty response format provided, ignoring it')
sanitizedRequest.responseFormat = undefined
} else {
const structuredOutputInstructions = generateStructuredOutputInstructions(
sanitizedRequest.responseFormat
)
if (structuredOutputInstructions.trim()) {
const originalPrompt = sanitizedRequest.systemPrompt || ''
sanitizedRequest.systemPrompt =
`${originalPrompt}\n\n${structuredOutputInstructions}`.trim()
logger.info('Added structured output instructions to system prompt')
}
}
}
await attachLargeFileRemoteUrls(sanitizedRequest, providerId)
await uploadLargeFilesToProvider(sanitizedRequest, providerId)
const response = await provider.executeRequest(sanitizedRequest)
if (isStreamingExecution(response)) {
logger.info('Provider returned StreamingExecution', { isBYOK })
applyStreamingCostPolicy(response, resolveModelCostPolicy(sanitizedRequest.model, isBYOK))
return response
}
if (isReadableStream(response)) {
logger.info('Provider returned ReadableStream')
return response
}
const costPolicy = resolveModelCostPolicy(response.model, isBYOK)
if (response.tokens) {
const { input: promptTokens = 0, output: completionTokens = 0 } = response.tokens
/**
* Any provider that reports cache buckets also prices itself, because only
* it knows the tiers involved — Anthropic's 5m vs 1h writes cannot be
* reconstructed from a single `cacheWrite` count. Its cost is therefore
* authoritative and only the policy is applied on top. The fallback prices
* providers that report no cache usage at all.
*
* Tool cost is stripped either way: it is re-derived from `toolResults`
* below and must not be counted twice.
*/
response.cost = response.cost
? (applyModelCostPolicy(withoutToolCost(response.cost), costPolicy) as typeof response.cost)
: calculateBillableModelCost(response.model, promptTokens, completionTokens, { isBYOK })
if (!costPolicy.billable) {
logger.info(
isBYOK
? `Not billing model usage for ${response.model} - workspace BYOK key used`
: `Not billing model usage for ${response.model} - user provided API key or not hosted model`
)
}
}
// Per-segment model costs are written by trace enrichers regardless of key
// provenance. Align them with the block-level decision so the displayed
// breakdown does not contradict the authoritative block cost.
if (response.timing?.timeSegments) {
applySegmentCostPolicy(response.timing.timeSegments, costPolicy)
}
const toolCost = sumToolCosts(response.toolResults)
if (toolCost > 0 && response.cost) {
// Replaced rather than mutated: a provider-supplied cost can be the same
// object it also handed to a time segment, and tool cost belongs only to
// the block total.
response.cost = {
...response.cost,
toolCost,
total: response.cost.total + toolCost,
}
}
return response
}