70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import {
|
|
execCommand,
|
|
fetchDataWithTimeout,
|
|
getSecretValue,
|
|
term,
|
|
} from "https://script.hatter.ink/@67/deno-commons-mod.ts";
|
|
|
|
export async function howto(message: string): Promise<string> {
|
|
const response = await fetchDataWithTimeout(
|
|
"https://hatter.ink/ai/command-line-howto.json",
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Authorization": await getBearerToken(
|
|
"ai-command-line-howto-token",
|
|
),
|
|
},
|
|
body: new URLSearchParams({
|
|
"message": message,
|
|
}),
|
|
timeoutMillis: 30_000,
|
|
},
|
|
);
|
|
if (response.status != 200) {
|
|
console.log(
|
|
term.yellow(
|
|
`Howto command line failed, status: ${response.status}`,
|
|
),
|
|
);
|
|
return null;
|
|
}
|
|
return (await response.json())["data"]["summary"];
|
|
}
|
|
|
|
export async function summarizeGitStatusDiff(): Promise<string> {
|
|
const gitStatus = (await execCommand("git", ["status"]))
|
|
.assertSuccess().getStdoutAsStringThenTrim();
|
|
const gitDiff = (await execCommand("git", ["diff"]))
|
|
.assertSuccess().getStdoutAsStringThenTrim();
|
|
const response = await fetchDataWithTimeout(
|
|
"https://hatter.ink/ai/commit-summarize.json",
|
|
{
|
|
method: "POST",
|
|
headers: {
|
|
"Authorization": await getBearerToken(
|
|
"ai-commit-summarize-token",
|
|
),
|
|
},
|
|
body: new URLSearchParams({
|
|
"gitStatus": gitStatus,
|
|
"gitDiff": gitDiff,
|
|
}),
|
|
timeoutMillis: 30_000,
|
|
},
|
|
);
|
|
if (response.status != 200) {
|
|
console.log(
|
|
term.yellow(
|
|
`Summarize commit message failed, status: ${response.status}`,
|
|
),
|
|
);
|
|
return null;
|
|
}
|
|
return (await response.json())["data"]["summary"];
|
|
}
|
|
|
|
async function getBearerToken(token: string): Promise<string> {
|
|
return "Bearer " + await getSecretValue(token);
|
|
}
|