async function responses({
url = "https://api.openai.com/v1/responses",
model = "o4-mini",
input,
background,
include,
instructions,
max_output_tokens,
metadata,
parallel_tool_calls,
previous_response_id,
reasoning,
service_tier,
store,
temperature,
text,
tool_choice,
tools,
top_p,
truncation,
user
} = {}) {
debugger;
if (typeof input === "string") {
input = [
{
role: "user",
content: input
}
];
}
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${viewof OPENAI_API_KEY.value}`,
"Content-type": "application/json"
},
method: "POST",
body: JSON.stringify({
model,
background,
input: await deepResolve(input),
include,
instructions: await (typeof instructions == "function"
? instructions()
: instructions),
max_output_tokens,
metadata,
parallel_tool_calls,
previous_response_id,
reasoning,
service_tier,
store,
temperature,
text,
tool_choice,
tools,
top_p,
truncation,
user
})
});
if (response.status == 403 || response.status == 401)
throw "Authentication error: update OPENAI_API_KEY";
const responseJson = {
...arguments[0],
input,
...(await response.json()),
tools
};
console.log(arguments[0], responseJson);
responseJson.output &&
(await Promise.all(
responseJson.output
.filter((o) => o.type == "image_generation_call")
.map(async (call) => {
call.blob = await fetch(
`data:image/${call.format};base64,${call.result}`
).then((r) => r.blob());
})
));
responseJson.output &&
(await Promise.all(
responseJson.output
.filter((o) => o.type == "function_call")
.map(async (call) => {
call.arguments =
typeof call.arguments == "string"
? JSON.parse(call.arguments)
: call.arguments;
})
));
return responseJson;
}