-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2c87d0
commit ebd35ba
Showing
6 changed files
with
98 additions
and
5 deletions.
There are no files selected for viewing
17 changes: 17 additions & 0 deletions
17
packages/backend/server/migrations/20240321065017_ai_prompts/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
-- CreateEnum | ||
CREATE TYPE "AiPromptRole" AS ENUM ('system', 'assistant', 'user'); | ||
|
||
-- CreateTable | ||
CREATE TABLE "ai_prompts" ( | ||
"id" VARCHAR NOT NULL, | ||
"name" VARCHAR(20) NOT NULL, | ||
"idx" INTEGER NOT NULL, | ||
"role" "AiPromptRole" NOT NULL, | ||
"content" TEXT NOT NULL, | ||
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
|
||
CONSTRAINT "ai_prompts_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE UNIQUE INDEX "ai_prompts_name_key" ON "ai_prompts"("name"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { PrismaClient } from '@prisma/client'; | ||
|
||
import { ChatMessage } from './types'; | ||
|
||
@Injectable() | ||
export class CopilotPromptService { | ||
constructor(private readonly db: PrismaClient) {} | ||
|
||
// list prompt names | ||
async list() { | ||
return this.db.aiPrompt | ||
.findMany({ select: { name: true } }) | ||
.then(prompts => prompts.map(p => p.name)); | ||
} | ||
|
||
async get(name: string): Promise<ChatMessage[]> { | ||
return this.db.aiPrompt.findMany({ | ||
where: { | ||
name, | ||
}, | ||
select: { | ||
role: true, | ||
content: true, | ||
}, | ||
orderBy: { | ||
idx: 'asc', | ||
}, | ||
}); | ||
} | ||
|
||
async set(name: string, messages: ChatMessage[]) { | ||
return this.db.$transaction(async tx => { | ||
if (await tx.aiPrompt.findUnique({ where: { name } })) { | ||
return 0; | ||
} | ||
return tx.aiPrompt | ||
.createMany({ | ||
data: messages.map((m, idx) => ({ name, idx, ...m })), | ||
}) | ||
.then(ret => ret.count); | ||
}); | ||
} | ||
|
||
async delete(name: string) { | ||
return this.db.aiPrompt | ||
.deleteMany({ | ||
where: { name }, | ||
}) | ||
.then(ret => ret.count); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters