feat: Integrate ButlerAiProvider for AI summarization

Added ButlerAiProvider to handle AI summarization functionality instead of directly using Summarizer. This change improves modularity and separates the AI functionality into a dedicated provider. Updated the Summarizer class to utilize the new AIProvider interface for handling AI evaluations. This enhances code readability and maintainability.
This commit is contained in:
Caleb Owens 2024-03-05 22:05:29 +00:00
parent 34c13cc254
commit 9f8afcfd1d
3 changed files with 29 additions and 16 deletions

View File

@ -34,9 +34,27 @@ Here is my diff:
%{diff}
`
export class Summarizer {
interface AIProvider {
evaluate(prompt: string): Promise<string>
}
export class ButlerAIProvider {
constructor(private cloud: ReturnType<typeof getCloudApiClient>, private user: User) {}
async evaluate(prompt: string) {
const messages: PromptMessage[] = [
{ role: MessageRole.User, content: prompt }
]
const response = await this.cloud.summarize.evaluatePrompt(this.user.access_token, { messages })
return response.message
}
}
export class Summarizer {
constructor(private aiProvider: AIProvider) {}
async commit(diff: string, useEmojiStyle: boolean, useBreifStyle: boolean) {
const briefStyle = "The commit message must be only one sentence and as short as possible."
const emojiStyle = "Make use of GitMoji in the title prefix."
@ -53,12 +71,7 @@ export class Summarizer {
}
prompt.replaceAll("%{breif_style}", "")
const messages: PromptMessage[] = [
{ role: MessageRole.User, content: prompt }
]
const response = await this.cloud.summarize.evaluatePrompt(this.user.access_token, { messages })
let message = response.message
let message = await this.aiProvider.evaluate(prompt)
if (useBreifStyle) {
message = message.split("\n")[0]
@ -75,12 +88,8 @@ export class Summarizer {
async branch(diff: string) {
const prompt = branchTemplate.replaceAll("%{diff}", diff.slice(0, 20000))
const messages: PromptMessage[] = [
{ role: MessageRole.User, content: prompt }
]
let message = await this.aiProvider.evaluate(prompt)
const response = await this.cloud.summarize.evaluatePrompt(this.user.access_token, { messages })
let message = response.message
message = message.replaceAll(" ", "-")
message = message.replaceAll("\n", "-")
return message

View File

@ -42,7 +42,7 @@
RemoteBranchData,
RemoteCommit
} from '$lib/vbranches/types';
import { Summarizer } from '$lib/backend/summarizing';
import { ButlerAIProvider, Summarizer } from '$lib/backend/summarizing';
export let branch: Branch;
export let isUnapplied = false;
@ -100,7 +100,9 @@
.slice(0, 5000);
if (user && aiGenEnabled) {
new Summarizer(cloud, user).branch(diff).then((message) => {
const aiProvider = new ButlerAIProvider(cloud, user)
new Summarizer(aiProvider).branch(diff).then((message) => {
if (message !== branch.name) {
branch.name = message;
branchController.updateBranchName(branch.id, branch.name);

View File

@ -26,7 +26,7 @@
import type { Ownership } from '$lib/vbranches/ownership';
import type { Branch, LocalFile } from '$lib/vbranches/types';
import type { Writable } from 'svelte/store';
import { Summarizer } from '$lib/backend/summarizing';
import { ButlerAIProvider, Summarizer } from '$lib/backend/summarizing';
const dispatch = createEventDispatcher<{
action: 'generate-branch-name';
@ -76,7 +76,9 @@
let summarizer: Summarizer | undefined
$: if (user) {
summarizer = new Summarizer(cloud, user)
const aiProvider = new ButlerAIProvider(cloud, user)
summarizer = new Summarizer(aiProvider)
}
let isGeneratingCommitMessage = false;