chore: format code
This commit is contained in:
@@ -123,15 +123,12 @@ export function fromAnthropicRequest(body: any): CommonRequest {
|
||||
if ((p as any).type === "tool_result") {
|
||||
const id = (p as any).tool_use_id
|
||||
const content =
|
||||
typeof (p as any).content === "string"
|
||||
? (p as any).content
|
||||
: JSON.stringify((p as any).content)
|
||||
typeof (p as any).content === "string" ? (p as any).content : JSON.stringify((p as any).content)
|
||||
msgs.push({ role: "tool", tool_call_id: id, content })
|
||||
}
|
||||
}
|
||||
if (partsOut.length > 0) {
|
||||
if (partsOut.length === 1 && partsOut[0].type === "text")
|
||||
msgs.push({ role: "user", content: partsOut[0].text })
|
||||
if (partsOut.length === 1 && partsOut[0].type === "text") msgs.push({ role: "user", content: partsOut[0].text })
|
||||
else msgs.push({ role: "user", content: partsOut })
|
||||
}
|
||||
continue
|
||||
@@ -143,8 +140,7 @@ export function fromAnthropicRequest(body: any): CommonRequest {
|
||||
const tcs: any[] = []
|
||||
for (const p of partsIn) {
|
||||
if (!p || !(p as any).type) continue
|
||||
if ((p as any).type === "text" && typeof (p as any).text === "string")
|
||||
texts.push((p as any).text)
|
||||
if ((p as any).type === "text" && typeof (p as any).text === "string") texts.push((p as any).text)
|
||||
if ((p as any).type === "tool_use") {
|
||||
const name = (p as any).name
|
||||
const id = (p as any).id
|
||||
@@ -214,9 +210,7 @@ export function fromAnthropicRequest(body: any): CommonRequest {
|
||||
export function toAnthropicRequest(body: CommonRequest) {
|
||||
if (!body || typeof body !== "object") return body
|
||||
|
||||
const sysIn = Array.isArray(body.messages)
|
||||
? body.messages.filter((m: any) => m && m.role === "system")
|
||||
: []
|
||||
const sysIn = Array.isArray(body.messages) ? body.messages.filter((m: any) => m && m.role === "system") : []
|
||||
let ccCount = 0
|
||||
const cc = () => {
|
||||
ccCount++
|
||||
@@ -367,9 +361,7 @@ export function fromAnthropicResponse(resp: any): CommonResponse {
|
||||
|
||||
const idIn = (resp as any).id
|
||||
const id =
|
||||
typeof idIn === "string"
|
||||
? idIn.replace(/^msg_/, "chatcmpl_")
|
||||
: `chatcmpl_${Math.random().toString(36).slice(2)}`
|
||||
typeof idIn === "string" ? idIn.replace(/^msg_/, "chatcmpl_") : `chatcmpl_${Math.random().toString(36).slice(2)}`
|
||||
const model = (resp as any).model
|
||||
|
||||
const blocks: any[] = Array.isArray((resp as any).content) ? (resp as any).content : []
|
||||
@@ -412,9 +404,7 @@ export function fromAnthropicResponse(resp: any): CommonResponse {
|
||||
const ct = typeof (u as any).output_tokens === "number" ? (u as any).output_tokens : undefined
|
||||
const total = pt != null && ct != null ? pt + ct : undefined
|
||||
const cached =
|
||||
typeof (u as any).cache_read_input_tokens === "number"
|
||||
? (u as any).cache_read_input_tokens
|
||||
: undefined
|
||||
typeof (u as any).cache_read_input_tokens === "number" ? (u as any).cache_read_input_tokens : undefined
|
||||
const details = cached != null ? { cached_tokens: cached } : undefined
|
||||
return {
|
||||
prompt_tokens: pt,
|
||||
@@ -591,9 +581,7 @@ export function fromAnthropicChunk(chunk: string): CommonChunk | string {
|
||||
prompt_tokens: u.input_tokens,
|
||||
completion_tokens: u.output_tokens,
|
||||
total_tokens: (u.input_tokens || 0) + (u.output_tokens || 0),
|
||||
...(u.cache_read_input_tokens
|
||||
? { prompt_tokens_details: { cached_tokens: u.cache_read_input_tokens } }
|
||||
: {}),
|
||||
...(u.cache_read_input_tokens ? { prompt_tokens_details: { cached_tokens: u.cache_read_input_tokens } } : {}),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,8 +57,7 @@ export const oaCompatHelper = {
|
||||
const inputTokens = usage.prompt_tokens ?? 0
|
||||
const outputTokens = usage.completion_tokens ?? 0
|
||||
const reasoningTokens = usage.completion_tokens_details?.reasoning_tokens ?? undefined
|
||||
const cacheReadTokens =
|
||||
usage.cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? undefined
|
||||
const cacheReadTokens = usage.cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? undefined
|
||||
return {
|
||||
inputTokens: inputTokens - (cacheReadTokens ?? 0),
|
||||
outputTokens,
|
||||
@@ -80,8 +79,7 @@ export function fromOaCompatibleRequest(body: any): CommonRequest {
|
||||
if (!m || !m.role) continue
|
||||
|
||||
if (m.role === "system") {
|
||||
if (typeof m.content === "string" && m.content.length > 0)
|
||||
msgsOut.push({ role: "system", content: m.content })
|
||||
if (typeof m.content === "string" && m.content.length > 0) msgsOut.push({ role: "system", content: m.content })
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -92,12 +90,10 @@ export function fromOaCompatibleRequest(body: any): CommonRequest {
|
||||
const parts: any[] = []
|
||||
for (const p of m.content) {
|
||||
if (!p || !p.type) continue
|
||||
if (p.type === "text" && typeof p.text === "string")
|
||||
parts.push({ type: "text", text: p.text })
|
||||
if (p.type === "text" && typeof p.text === "string") parts.push({ type: "text", text: p.text })
|
||||
if (p.type === "image_url") parts.push({ type: "image_url", image_url: p.image_url })
|
||||
}
|
||||
if (parts.length === 1 && parts[0].type === "text")
|
||||
msgsOut.push({ role: "user", content: parts[0].text })
|
||||
if (parts.length === 1 && parts[0].type === "text") msgsOut.push({ role: "user", content: parts[0].text })
|
||||
else if (parts.length > 0) msgsOut.push({ role: "user", content: parts })
|
||||
}
|
||||
continue
|
||||
@@ -141,8 +137,7 @@ export function toOaCompatibleRequest(body: CommonRequest) {
|
||||
if (p.type === "image_url" && p.image_url) return { type: "image_url", image_url: p.image_url }
|
||||
const s = (p as any).source
|
||||
if (!s || typeof s !== "object") return undefined
|
||||
if (s.type === "url" && typeof s.url === "string")
|
||||
return { type: "image_url", image_url: { url: s.url } }
|
||||
if (s.type === "url" && typeof s.url === "string") return { type: "image_url", image_url: { url: s.url } }
|
||||
if (s.type === "base64" && typeof s.media_type === "string" && typeof s.data === "string")
|
||||
return { type: "image_url", image_url: { url: `data:${s.media_type};base64,${s.data}` } }
|
||||
return undefined
|
||||
@@ -152,8 +147,7 @@ export function toOaCompatibleRequest(body: CommonRequest) {
|
||||
if (!m || !m.role) continue
|
||||
|
||||
if (m.role === "system") {
|
||||
if (typeof m.content === "string" && m.content.length > 0)
|
||||
msgsOut.push({ role: "system", content: m.content })
|
||||
if (typeof m.content === "string" && m.content.length > 0) msgsOut.push({ role: "system", content: m.content })
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -166,13 +160,11 @@ export function toOaCompatibleRequest(body: CommonRequest) {
|
||||
const parts: any[] = []
|
||||
for (const p of m.content) {
|
||||
if (!p || !p.type) continue
|
||||
if (p.type === "text" && typeof p.text === "string")
|
||||
parts.push({ type: "text", text: p.text })
|
||||
if (p.type === "text" && typeof p.text === "string") parts.push({ type: "text", text: p.text })
|
||||
const ip = toImg(p)
|
||||
if (ip) parts.push(ip)
|
||||
}
|
||||
if (parts.length === 1 && parts[0].type === "text")
|
||||
msgsOut.push({ role: "user", content: parts[0].text })
|
||||
if (parts.length === 1 && parts[0].type === "text") msgsOut.push({ role: "user", content: parts[0].text })
|
||||
else if (parts.length > 0) msgsOut.push({ role: "user", content: parts })
|
||||
}
|
||||
continue
|
||||
@@ -325,9 +317,7 @@ export function toOaCompatibleResponse(resp: CommonResponse) {
|
||||
|
||||
const idIn = (resp as any).id
|
||||
const id =
|
||||
typeof idIn === "string"
|
||||
? idIn.replace(/^msg_/, "chatcmpl_")
|
||||
: `chatcmpl_${Math.random().toString(36).slice(2)}`
|
||||
typeof idIn === "string" ? idIn.replace(/^msg_/, "chatcmpl_") : `chatcmpl_${Math.random().toString(36).slice(2)}`
|
||||
const model = (resp as any).model
|
||||
|
||||
const blocks: any[] = Array.isArray((resp as any).content) ? (resp as any).content : []
|
||||
@@ -369,8 +359,7 @@ export function toOaCompatibleResponse(resp: CommonResponse) {
|
||||
const pt = typeof u.input_tokens === "number" ? u.input_tokens : undefined
|
||||
const ct = typeof u.output_tokens === "number" ? u.output_tokens : undefined
|
||||
const total = pt != null && ct != null ? pt + ct : undefined
|
||||
const cached =
|
||||
typeof u.cache_read_input_tokens === "number" ? u.cache_read_input_tokens : undefined
|
||||
const cached = typeof u.cache_read_input_tokens === "number" ? u.cache_read_input_tokens : undefined
|
||||
const details = cached != null ? { cached_tokens: cached } : undefined
|
||||
return {
|
||||
prompt_tokens: pt,
|
||||
|
||||
@@ -86,11 +86,7 @@ export function fromOpenaiRequest(body: any): CommonRequest {
|
||||
|
||||
const msgs: any[] = []
|
||||
|
||||
const inMsgs = Array.isArray(body.input)
|
||||
? body.input
|
||||
: Array.isArray(body.messages)
|
||||
? body.messages
|
||||
: []
|
||||
const inMsgs = Array.isArray(body.input) ? body.input : Array.isArray(body.messages) ? body.messages : []
|
||||
|
||||
for (const m of inMsgs) {
|
||||
if (!m) continue
|
||||
@@ -103,9 +99,7 @@ export function fromOpenaiRequest(body: any): CommonRequest {
|
||||
const args = typeof a === "string" ? a : JSON.stringify(a ?? {})
|
||||
msgs.push({
|
||||
role: "assistant",
|
||||
tool_calls: [
|
||||
{ id: (m as any).id, type: "function", function: { name, arguments: args } },
|
||||
],
|
||||
tool_calls: [{ id: (m as any).id, type: "function", function: { name, arguments: args } }],
|
||||
})
|
||||
}
|
||||
if ((m as any).type === "function_call_output") {
|
||||
@@ -122,8 +116,7 @@ export function fromOpenaiRequest(body: any): CommonRequest {
|
||||
if (typeof c === "string" && c.length > 0) msgs.push({ role: "system", content: c })
|
||||
if (Array.isArray(c)) {
|
||||
const t = c.find((p: any) => p && typeof p.text === "string")
|
||||
if (t && typeof t.text === "string" && t.text.length > 0)
|
||||
msgs.push({ role: "system", content: t.text })
|
||||
if (t && typeof t.text === "string" && t.text.length > 0) msgs.push({ role: "system", content: t.text })
|
||||
}
|
||||
continue
|
||||
}
|
||||
@@ -136,24 +129,18 @@ export function fromOpenaiRequest(body: any): CommonRequest {
|
||||
const parts: any[] = []
|
||||
for (const p of c) {
|
||||
if (!p || !(p as any).type) continue
|
||||
if (
|
||||
((p as any).type === "text" || (p as any).type === "input_text") &&
|
||||
typeof (p as any).text === "string"
|
||||
)
|
||||
if (((p as any).type === "text" || (p as any).type === "input_text") && typeof (p as any).text === "string")
|
||||
parts.push({ type: "text", text: (p as any).text })
|
||||
const ip = toImg(p)
|
||||
if (ip) parts.push(ip)
|
||||
if ((p as any).type === "tool_result") {
|
||||
const id = (p as any).tool_call_id
|
||||
const content =
|
||||
typeof (p as any).content === "string"
|
||||
? (p as any).content
|
||||
: JSON.stringify((p as any).content)
|
||||
typeof (p as any).content === "string" ? (p as any).content : JSON.stringify((p as any).content)
|
||||
msgs.push({ role: "tool", tool_call_id: id, content })
|
||||
}
|
||||
}
|
||||
if (parts.length === 1 && parts[0].type === "text")
|
||||
msgs.push({ role: "user", content: parts[0].text })
|
||||
if (parts.length === 1 && parts[0].type === "text") msgs.push({ role: "user", content: parts[0].text })
|
||||
else if (parts.length > 0) msgs.push({ role: "user", content: parts })
|
||||
}
|
||||
continue
|
||||
@@ -280,10 +267,7 @@ export function toOpenaiRequest(body: CommonRequest) {
|
||||
}
|
||||
|
||||
if ((m as any).role === "tool") {
|
||||
const out =
|
||||
typeof (m as any).content === "string"
|
||||
? (m as any).content
|
||||
: JSON.stringify((m as any).content)
|
||||
const out = typeof (m as any).content === "string" ? (m as any).content : JSON.stringify((m as any).content)
|
||||
input.push({ type: "function_call_output", call_id: (m as any).tool_call_id, output: out })
|
||||
continue
|
||||
}
|
||||
@@ -351,9 +335,7 @@ export function fromOpenaiResponse(resp: any): CommonResponse {
|
||||
|
||||
const idIn = (r as any).id
|
||||
const id =
|
||||
typeof idIn === "string"
|
||||
? idIn.replace(/^resp_/, "chatcmpl_")
|
||||
: `chatcmpl_${Math.random().toString(36).slice(2)}`
|
||||
typeof idIn === "string" ? idIn.replace(/^resp_/, "chatcmpl_") : `chatcmpl_${Math.random().toString(36).slice(2)}`
|
||||
const model = (r as any).model ?? (resp as any).model
|
||||
|
||||
const out = Array.isArray((r as any).output) ? (r as any).output : []
|
||||
@@ -480,9 +462,7 @@ export function toOpenaiResponse(resp: CommonResponse) {
|
||||
})()
|
||||
|
||||
return {
|
||||
id:
|
||||
(resp as any).id?.replace(/^chatcmpl_/, "resp_") ??
|
||||
`resp_${Math.random().toString(36).slice(2)}`,
|
||||
id: (resp as any).id?.replace(/^chatcmpl_/, "resp_") ?? `resp_${Math.random().toString(36).slice(2)}`,
|
||||
object: "response",
|
||||
model: (resp as any).model,
|
||||
output: outputItems,
|
||||
|
||||
Reference in New Issue
Block a user