@@ -8,11 +8,13 @@ import { calculateApiCostOpenAI } from "../../shared/cost"
88
99import { convertToOpenAiMessages } from "../transform/openai-format"
1010import { ApiStream , ApiStreamUsageChunk } from "../transform/stream"
11+ import { getModelParams } from "../transform/model-params"
12+ import { AnthropicReasoningParams } from "../transform/reasoning"
1113
1214import { DEFAULT_HEADERS } from "./constants"
1315import { getModels } from "./fetchers/modelCache"
1416import { BaseProvider } from "./base-provider"
15- import type { SingleCompletionHandler , ApiHandlerCreateMessageMetadata } from "../"
17+ import type { SingleCompletionHandler , ApiHandlerCreateMessageMetadata } from "../index "
1618
1719// Requesty usage includes an extra field for Anthropic use cases.
1820// Safely cast the prompt token details section to the appropriate structure.
@@ -31,10 +33,7 @@ type RequestyChatCompletionParams = OpenAI.Chat.ChatCompletionCreateParams & {
3133 mode ?: string
3234 }
3335 }
34- thinking ?: {
35- type : string
36- budget_tokens ?: number
37- }
36+ thinking ?: AnthropicReasoningParams
3837}
3938
4039export class RequestyHandler extends BaseProvider implements SingleCompletionHandler {
@@ -44,25 +43,33 @@ export class RequestyHandler extends BaseProvider implements SingleCompletionHan
4443
4544 constructor ( options : ApiHandlerOptions ) {
4645 super ( )
47- this . options = options
48-
49- const apiKey = this . options . requestyApiKey ?? "not-provided"
50- const baseURL = "https://router.requesty.ai/v1"
5146
52- const defaultHeaders = DEFAULT_HEADERS
47+ this . options = options
5348
54- this . client = new OpenAI ( { baseURL, apiKey, defaultHeaders } )
49+ this . client = new OpenAI ( {
50+ baseURL : "https://router.requesty.ai/v1" ,
51+ apiKey : this . options . requestyApiKey ?? "not-provided" ,
52+ defaultHeaders : DEFAULT_HEADERS ,
53+ } )
5554 }
5655
5756 public async fetchModel ( ) {
5857 this . models = await getModels ( { provider : "requesty" } )
5958 return this . getModel ( )
6059 }
6160
62- override getModel ( ) : { id : string ; info : ModelInfo } {
61+ override getModel ( ) {
6362 const id = this . options . requestyModelId ?? requestyDefaultModelId
6463 const info = this . models [ id ] ?? requestyDefaultModelInfo
65- return { id, info }
64+
65+ const params = getModelParams ( {
66+ format : "anthropic" ,
67+ modelId : id ,
68+ model : info ,
69+ settings : this . options ,
70+ } )
71+
72+ return { id, info, ...params }
6673 }
6774
6875 protected processUsageMetrics ( usage : any , modelInfo ?: ModelInfo ) : ApiStreamUsageChunk {
@@ -90,70 +97,44 @@ export class RequestyHandler extends BaseProvider implements SingleCompletionHan
9097 messages : Anthropic . Messages . MessageParam [ ] ,
9198 metadata ?: ApiHandlerCreateMessageMetadata ,
9299 ) : ApiStream {
93- const model = await this . fetchModel ( )
94-
95- let openAiMessages : OpenAI . Chat . ChatCompletionMessageParam [ ] = [
100+ const {
101+ id : model ,
102+ info,
103+ maxTokens : max_tokens ,
104+ temperature,
105+ reasoningEffort : reasoning_effort ,
106+ reasoning : thinking ,
107+ } = await this . fetchModel ( )
108+
109+ const openAiMessages : OpenAI . Chat . ChatCompletionMessageParam [ ] = [
96110 { role : "system" , content : systemPrompt } ,
97111 ...convertToOpenAiMessages ( messages ) ,
98112 ]
99113
100- let maxTokens = undefined
101- if ( this . options . modelMaxTokens ) {
102- maxTokens = this . options . modelMaxTokens
103- } else if ( this . options . includeMaxTokens ) {
104- maxTokens = model . info . maxTokens
105- }
106-
107- let reasoningEffort = undefined
108- if ( this . options . reasoningEffort ) {
109- reasoningEffort = this . options . reasoningEffort
110- }
111-
112- let thinking = undefined
113- if ( this . options . modelMaxThinkingTokens ) {
114- thinking = {
115- type : "enabled" ,
116- budget_tokens : this . options . modelMaxThinkingTokens ,
117- }
118- }
119-
120- const temperature = this . options . modelTemperature
121-
122114 const completionParams : RequestyChatCompletionParams = {
123- model : model . id ,
124- max_tokens : maxTokens ,
125115 messages : openAiMessages ,
126- temperature : temperature ,
116+ model,
117+ max_tokens,
118+ temperature,
119+ reasoning_effort,
120+ thinking,
127121 stream : true ,
128122 stream_options : { include_usage : true } ,
129- reasoning_effort : reasoningEffort ,
130- thinking : thinking ,
131- requesty : {
132- trace_id : metadata ?. taskId ,
133- extra : {
134- mode : metadata ?. mode ,
135- } ,
136- } ,
123+ requesty : { trace_id : metadata ?. taskId , extra : { mode : metadata ?. mode } } ,
137124 }
138125
139126 const stream = await this . client . chat . completions . create ( completionParams )
140-
141127 let lastUsage : any = undefined
142128
143129 for await ( const chunk of stream ) {
144130 const delta = chunk . choices [ 0 ] ?. delta
131+
145132 if ( delta ?. content ) {
146- yield {
147- type : "text" ,
148- text : delta . content ,
149- }
133+ yield { type : "text" , text : delta . content }
150134 }
151135
152136 if ( delta && "reasoning_content" in delta && delta . reasoning_content ) {
153- yield {
154- type : "reasoning" ,
155- text : ( delta . reasoning_content as string | undefined ) || "" ,
156- }
137+ yield { type : "reasoning" , text : ( delta . reasoning_content as string | undefined ) || "" }
157138 }
158139
159140 if ( chunk . usage ) {
@@ -162,7 +143,7 @@ export class RequestyHandler extends BaseProvider implements SingleCompletionHan
162143 }
163144
164145 if ( lastUsage ) {
165- yield this . processUsageMetrics ( lastUsage , model . info )
146+ yield this . processUsageMetrics ( lastUsage , info )
166147 }
167148 }
168149
0 commit comments