forked from ericc-ch/copilot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-responses.ts
More file actions
90 lines (73 loc) · 2.79 KB
/
create-responses.ts
File metadata and controls
90 lines (73 loc) · 2.79 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import type { ServerSentEventMessage } from "fetch-event-stream"
import consola from "consola"
import { events } from "fetch-event-stream"
import type {
ResponsesContentPart,
ResponsesInputItem,
ResponsesPayload,
ResponsesResponse,
} from "~/routes/responses/types"
import { copilotHeaders, copilotBaseUrl } from "~/lib/api-config"
import { HTTPError } from "~/lib/error"
import { state } from "~/lib/state"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
/**
* Returns true if any input item contains an `input_image` content part.
* Handles both a top-level string input and an array of input items.
*/
export function inputHasImages(payload: ResponsesPayload): boolean {
if (typeof payload.input === "string") return false
return payload.input.some((item) => {
if (item.type !== "message") return false
if (typeof item.content === "string") return false
return item.content.some(
(part: ResponsesContentPart) => part.type === "input_image",
)
})
}
/**
* Returns true if this looks like an agent/multi-turn call:
* - any input item has role "assistant", OR
* - any item has type "function_call_output", "function_call", or "reasoning"
* (reasoning items only appear when echoing back prior agentic turn context)
*/
export function isAgentCall(payload: ResponsesPayload): boolean {
if (typeof payload.input === "string") return false
return payload.input.some(
(item: ResponsesInputItem) =>
("role" in item && item.role === "assistant")
|| item.type === "function_call_output"
|| item.type === "function_call"
|| item.type === "reasoning",
)
}
// ---------------------------------------------------------------------------
// Service client
// ---------------------------------------------------------------------------
export const createResponses = async (
payload: ResponsesPayload,
): Promise<ResponsesResponse | AsyncIterable<ServerSentEventMessage>> => {
if (!state.copilotToken) throw new Error("Copilot token not found")
const enableVision = inputHasImages(payload)
const initiator = isAgentCall(payload) ? "agent" : "user"
// TODO(#11): add Copilot-Vision-Request header when vision detected
const headers: Record<string, string> = {
...copilotHeaders(state, enableVision),
"X-Initiator": initiator,
}
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 ResponsesResponse
}