chore: clean ups
This commit is contained in:
@@ -20,12 +20,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
// 1. Convert tools to OpenAI format
|
// 1. Convert tools to OpenAI format
|
||||||
const openaiTools = Object.entries(tools).map(([name, tool]) => zodToOpenAITool(name, tool))
|
const openaiTools = Object.entries(tools).map(([name, tool]) => zodToOpenAITool(name, tool))
|
||||||
|
|
||||||
// 2. Detect patch (auto-compatibility)
|
// 2. Call API
|
||||||
// TODO: Gemini also uses slightly different format than OpenAI
|
|
||||||
const isClaude = this.config.model.toLowerCase().startsWith('claude')
|
|
||||||
const isGrok = this.config.model.toLowerCase().startsWith('grok')
|
|
||||||
|
|
||||||
// 3. Call API
|
|
||||||
let response: Response
|
let response: Response
|
||||||
try {
|
try {
|
||||||
response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
||||||
@@ -59,9 +54,9 @@ export class OpenAIClient implements LLMClient {
|
|||||||
throw new InvokeError(InvokeErrorType.NETWORK_ERROR, 'Network request failed', error)
|
throw new InvokeError(InvokeErrorType.NETWORK_ERROR, 'Network request failed', error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Handle HTTP errors
|
// 3. Handle HTTP errors
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json().catch(() => ({}))
|
const errorData = await response.json().catch()
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
(errorData as { error?: { message?: string } }).error?.message || response.statusText
|
(errorData as { error?: { message?: string } }).error?.message || response.statusText
|
||||||
|
|
||||||
@@ -95,7 +90,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
|
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
|
|
||||||
// 5. Check finish_reason
|
// 4. Check finish_reason
|
||||||
const choice = data.choices?.[0]
|
const choice = data.choices?.[0]
|
||||||
if (!choice) {
|
if (!choice) {
|
||||||
throw new InvokeError(InvokeErrorType.UNKNOWN, 'No choices in response', data)
|
throw new InvokeError(InvokeErrorType.UNKNOWN, 'No choices in response', data)
|
||||||
@@ -130,7 +125,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 6. Parse tool call
|
// 5. Parse tool call
|
||||||
const toolCall = choice.message?.tool_calls?.[0]
|
const toolCall = choice.message?.tool_calls?.[0]
|
||||||
if (!toolCall) {
|
if (!toolCall) {
|
||||||
throw new InvokeError(InvokeErrorType.NO_TOOL_CALL, 'No tool call found in response', data)
|
throw new InvokeError(InvokeErrorType.NO_TOOL_CALL, 'No tool call found in response', data)
|
||||||
@@ -142,7 +137,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
throw new InvokeError(InvokeErrorType.UNKNOWN, `Tool ${toolName} not found`, data)
|
throw new InvokeError(InvokeErrorType.UNKNOWN, `Tool ${toolName} not found`, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. Parse and validate arguments
|
// 6. Parse and validate arguments
|
||||||
let toolArgs: unknown
|
let toolArgs: unknown
|
||||||
try {
|
try {
|
||||||
toolArgs = JSON.parse(toolCall.function.arguments)
|
toolArgs = JSON.parse(toolCall.function.arguments)
|
||||||
@@ -160,7 +155,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 8. Execute tool
|
// 7. Execute tool
|
||||||
let toolResult: unknown
|
let toolResult: unknown
|
||||||
try {
|
try {
|
||||||
toolResult = await tool.execute(validation.data)
|
toolResult = await tool.execute(validation.data)
|
||||||
@@ -172,7 +167,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. Return result (including cache tokens)
|
// 8. Return result (including cache tokens)
|
||||||
return {
|
return {
|
||||||
toolCall: {
|
toolCall: {
|
||||||
// id: toolCall.id,
|
// id: toolCall.id,
|
||||||
|
|||||||
@@ -22,11 +22,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
// 1. Convert tools to OpenAI format
|
// 1. Convert tools to OpenAI format
|
||||||
const openaiTools = Object.entries(tools).map(([name, tool]) => zodToOpenAITool(name, tool))
|
const openaiTools = Object.entries(tools).map(([name, tool]) => zodToOpenAITool(name, tool))
|
||||||
|
|
||||||
// 2. Detect if Claude (auto-compatibility)
|
// 2. Call API
|
||||||
// TODO: Gemini also uses slightly different format than OpenAI
|
|
||||||
const isClaude = this.config.model.toLowerCase().startsWith('claude')
|
|
||||||
|
|
||||||
// 3. Call API
|
|
||||||
let response: Response
|
let response: Response
|
||||||
try {
|
try {
|
||||||
response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
response = await fetch(`${this.config.baseURL}/chat/completions`, {
|
||||||
@@ -60,9 +56,9 @@ export class OpenAIClient implements LLMClient {
|
|||||||
throw new InvokeError(InvokeErrorType.NETWORK_ERROR, 'Network request failed', error)
|
throw new InvokeError(InvokeErrorType.NETWORK_ERROR, 'Network request failed', error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Handle HTTP errors
|
// 3. Handle HTTP errors
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
const errorData = await response.json().catch(() => ({}))
|
const errorData = await response.json().catch()
|
||||||
const errorMessage =
|
const errorMessage =
|
||||||
(errorData as { error?: { message?: string } }).error?.message || response.statusText
|
(errorData as { error?: { message?: string } }).error?.message || response.statusText
|
||||||
|
|
||||||
@@ -94,10 +90,10 @@ export class OpenAIClient implements LLMClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parse response
|
||||||
|
|
||||||
const data = await response.json()
|
const data = await response.json()
|
||||||
|
|
||||||
const tool = tools.AgentOutput
|
const tool = tools.AgentOutput
|
||||||
|
|
||||||
const macroToolInput = lenientParseMacroToolCall(data, tool.inputSchema as any)
|
const macroToolInput = lenientParseMacroToolCall(data, tool.inputSchema as any)
|
||||||
|
|
||||||
// Execute tool
|
// Execute tool
|
||||||
@@ -112,7 +108,7 @@ export class OpenAIClient implements LLMClient {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. Return result (including cache tokens)
|
// Return result (including cache tokens)
|
||||||
return {
|
return {
|
||||||
toolCall: {
|
toolCall: {
|
||||||
// id: toolCall.id,
|
// id: toolCall.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user