//native
type Gitbook = {
token: string;
};
/**
* Ask a question in a site
* The response is streamed.
*/
export async function main(
auth: Gitbook,
organizationId: string,
siteId: string,
format: "document" | "markdown" | undefined,
body: {
question: string;
context?: { siteSpaceId?: string };
scope:
| { mode: "default"; includedSiteSpaces?: string[] }
| { mode: "specific"; siteSpaceIds: string[] }
| { mode: "all" };
},
) {
const url = new URL(`https://api.gitbook.com/v1/orgs/${organizationId}/sites/${siteId}/ask`);
for (const [k, v] of [["format", format]]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + auth.token,
},
body: JSON.stringify(body),
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.text();
}
Submitted by hugo697 235 days ago