Spaces:
Running
Running
File size: 4,283 Bytes
9e27976 | 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 | import consola from "consola"
import { events } from "fetch-event-stream"
import { copilotHeaders, copilotBaseUrl } from "~/lib/api-config"
import { HTTPError } from "~/lib/error"
import { state } from "~/lib/state"
export const createChatCompletions = async (
payload: ChatCompletionsPayload,
) => {
if (!state.copilotToken) throw new Error("Copilot token not found")
const enableVision = payload.messages.some(
(x) =>
typeof x.content !== "string"
&& x.content?.some((x) => x.type === "image_url"),
)
// Agent/user check for X-Initiator header
// Determine if any message is from an agent ("assistant" or "tool")
const isAgentCall = payload.messages.some((msg) =>
["assistant", "tool"].includes(msg.role),
)
// Build headers and add X-Initiator
const headers: Record<string, string> = {
...copilotHeaders(state, enableVision),
"X-Initiator": isAgentCall ? "agent" : "user",
}
const response = await fetch(`${copilotBaseUrl(state)}/chat/completions`, {
method: "POST",
headers,
body: JSON.stringify(payload),
})
if (!response.ok) {
consola.error("Failed to create chat completions", response)
throw new HTTPError("Failed to create chat completions", response)
}
if (payload.stream) {
return events(response)
}
return (await response.json()) as ChatCompletionResponse
}
// Streaming types
export interface ChatCompletionChunk {
id: string
object: "chat.completion.chunk"
created: number
model: string
choices: Array<Choice>
system_fingerprint?: string
usage?: {
prompt_tokens: number
completion_tokens: number
total_tokens: number
prompt_tokens_details?: {
cached_tokens: number
}
completion_tokens_details?: {
accepted_prediction_tokens: number
rejected_prediction_tokens: number
}
}
}
interface Delta {
content?: string | null
role?: "user" | "assistant" | "system" | "tool"
tool_calls?: Array<{
index: number
id?: string
type?: "function"
function?: {
name?: string
arguments?: string
}
}>
}
interface Choice {
index: number
delta: Delta
finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | null
logprobs: object | null
}
// Non-streaming types
export interface ChatCompletionResponse {
id: string
object: "chat.completion"
created: number
model: string
choices: Array<ChoiceNonStreaming>
system_fingerprint?: string
usage?: {
prompt_tokens: number
completion_tokens: number
total_tokens: number
prompt_tokens_details?: {
cached_tokens: number
}
}
}
interface ResponseMessage {
role: "assistant"
content: string | null
tool_calls?: Array<ToolCall>
}
interface ChoiceNonStreaming {
index: number
message: ResponseMessage
logprobs: object | null
finish_reason: "stop" | "length" | "tool_calls" | "content_filter"
}
// Payload types
export interface ChatCompletionsPayload {
messages: Array<Message>
model: string
temperature?: number | null
top_p?: number | null
max_tokens?: number | null
stop?: string | Array<string> | null
n?: number | null
stream?: boolean | null
frequency_penalty?: number | null
presence_penalty?: number | null
logit_bias?: Record<string, number> | null
logprobs?: boolean | null
response_format?: { type: "json_object" } | null
seed?: number | null
tools?: Array<Tool> | null
tool_choice?:
| "none"
| "auto"
| "required"
| { type: "function"; function: { name: string } }
| null
user?: string | null
}
export interface Tool {
type: "function"
function: {
name: string
description?: string
parameters: Record<string, unknown>
}
}
export interface Message {
role: "user" | "assistant" | "system" | "tool" | "developer"
content: string | Array<ContentPart> | null
name?: string
tool_calls?: Array<ToolCall>
tool_call_id?: string
}
export interface ToolCall {
id: string
type: "function"
function: {
name: string
arguments: string
}
}
export type ContentPart = TextPart | ImagePart
export interface TextPart {
type: "text"
text: string
}
export interface ImagePart {
type: "image_url"
image_url: {
url: string
detail?: "low" | "high" | "auto"
}
}
|