-
-
Notifications
You must be signed in to change notification settings - Fork 603
Expand file tree
/
Copy pathcreate-responses.ts
More file actions
75 lines (61 loc) · 2.03 KB
/
create-responses.ts
File metadata and controls
75 lines (61 loc) · 2.03 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
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 interface ResponsesPayload {
model: string
input: string | Array<ResponseInputItem>
instructions?: string | null
stream?: boolean | null
temperature?: number | null
top_p?: number | null
max_output_tokens?: number | null
[key: string]: unknown
}
type ResponseInputItem = {
type?: string
role?: string
content?: string | Array<{ type: string; [key: string]: unknown }>
[key: string]: unknown
}
export const createResponses = async (payload: ResponsesPayload) => {
if (!state.copilotToken) throw new Error("Copilot token not found")
const enableVision = hasVisionContent(payload)
const isAgentCall = hasAgentMessages(payload)
const headers: Record<string, string> = {
...copilotHeaders(state, enableVision),
"X-Initiator": isAgentCall ? "agent" : "user",
}
const response = await fetch(`${copilotBaseUrl(state)}/responses`, {
method: "POST",
headers,
body: JSON.stringify(payload),
})
if (!response.ok) {
consola.error("Failed to create responses", response)
throw new HTTPError("Failed to create responses", response)
}
if (payload.stream) {
return events(response)
}
return (await response.json()) as Record<string, unknown>
}
function hasVisionContent(payload: ResponsesPayload): boolean {
if (typeof payload.input === "string") return false
return payload.input.some((item) => {
if (Array.isArray(item.content)) {
return item.content.some((part) => part.type === "input_image")
}
return false
})
}
function hasAgentMessages(payload: ResponsesPayload): boolean {
if (typeof payload.input === "string") return false
return payload.input.some((item) => {
if (item.role === "assistant") return true
if (item.type === "function_call_output") return true
if (item.type === "function_call") return true
return false
})
}