diff --git a/script-meta-v2.json b/script-meta-v2.json index c5d2a34..4a1998a 100644 --- a/script-meta-v2.json +++ b/script-meta-v2.json @@ -63,12 +63,12 @@ }, "commit.ts": { "script_name": "commit.ts", - "script_length": 2755, - "script_sha256": "ceb0915c81e38fdf02904a548ebf9cefe337bb7cf4d0a9510c95b83cd6d395fb", + "script_length": 4016, + "script_sha256": "db637841e28bdc0c9feb832d21371f0ef917c4f4a9e227060c69e7709aa6c9b7", "script_full_url": "https://git.hatter.ink/hatter/ts-scripts/raw/branch/main/single-scripts/commit.ts", "single_script_file": true, "publish_time": 1769318306585, - "update_time": 1770820023006 + "update_time": 1775314689271 }, "decode-uri-component.ts": { "script_name": "decode-uri-component.ts", diff --git a/single-scripts/commit.ts b/single-scripts/commit.ts index 75eca41..8e1e38a 100755 --- a/single-scripts/commit.ts +++ b/single-scripts/commit.ts @@ -1,8 +1,16 @@ -#!/usr/bin/env runts -- --allow-import --allow-run --allow-read +#!/usr/bin/env runts -- --allow-all import {createInterface} from "node:readline/promises"; import {stdin, stdout} from "node:process"; -import {execCommandShell, log, ProcessBar, term,} from "https://script.hatter.ink/@39/deno-commons-mod.ts"; +import { + execCommand, + execCommandShell, + fetchDataWithTimeout, + getSecretValue, + log, + ProcessBar, + term, +} from "https://script.hatter.ink/@61/deno-commons-mod.ts"; import {getGitLocalRev, getGitRemoteRev, getGitStatus,} from "https://script.hatter.ink/@2/deno-git-mod.ts"; async function checkRev(): Promise { @@ -19,6 +27,36 @@ async function checkRev(): Promise { } } +async function summarize(): Promise { + 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": "Bearer " + + await getSecretValue("ai-commit-summarize-token"), + }, + body: new URLSearchParams({ + "gitStatus": gitStatus, + "gitDiff": gitDiff, + }), + }, + ); + 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 main() { await checkRev(); // check local rev <--> remote rev equals const gitStatus = await getGitStatus(); @@ -28,6 +66,9 @@ async function main() { } log.info("Git status:", gitStatus); + const summary = await summarize(); + log.success(`Git commit message: ${summary}`); + let emptyCount = 0; let message = "empty message"; const readline = createInterface({ input: stdin, output: stdout }); @@ -37,7 +78,12 @@ async function main() { console.log("Too many empty messages, then exit"); return; } - message = await readline.question("Input your commit message > "); + message = await readline.question( + "Input your commit message (Enter for AI summary) > ", + ); + if (message.trim().length == 0) { + message = summary; + } } while (message.length == 0); readline.close(); @@ -76,5 +122,5 @@ main().catch((err) => { process.exit(0); }).then(() => process.exit(0)); -// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260211T222644+08:00.MEUCIQDeEy/R3ZVM/5AY9eD8 -// m1nb8pL2i3GKiqHDs0YQom0muQIgSqAkWhL2jO4tLvm928X7wNRCEC9gpbcSw9/IxdPtoY4= +// @SCRIPT-SIGNATURE-V1: yk-r1.ES256.20260404T225757+08:00.MEUCIQCp3myuHQTK17ZHS3zW +// Z7Wg/+Mgd0CasaV+FX34BHFB9gIgG8qRbLKxD1PD4jMrkGVucgT91xU1JLIwfVM6xwxh1Os=