Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: public endpoint #6445

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/actions/deploy/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const {
R2_SECRET_ACCESS_KEY,
CAPTCHA_TURNSTILE_SECRET,
COPILOT_OPENAI_API_KEY,
COPILOT_FAL_API_KEY,
MAILER_SENDER,
MAILER_USER,
MAILER_PASSWORD,
Expand Down Expand Up @@ -101,6 +102,7 @@ const createHelmCommand = ({ isDryRun }) => {
`--set-string graphql.app.captcha.turnstile.secret="${CAPTCHA_TURNSTILE_SECRET}"`,
`--set graphql.app.copilot.enabled=true`,
`--set-string graphql.app.copilot.openai.key="${COPILOT_OPENAI_API_KEY}"`,
`--set-string graphql.app.copilot.fal.key="${COPILOT_FAL_API_KEY}"`,
`--set graphql.app.objectStorage.r2.enabled=true`,
`--set-string graphql.app.objectStorage.r2.accountId="${R2_ACCOUNT_ID}"`,
`--set-string graphql.app.objectStorage.r2.accessKeyId="${R2_ACCESS_KEY_ID}"`,
Expand All @@ -116,6 +118,7 @@ const createHelmCommand = ({ isDryRun }) => {
`--set graphql.app.experimental.enableJwstCodec=${namespace === 'dev'}`,
`--set graphql.app.features.earlyAccessPreview=false`,
`--set graphql.app.features.syncClientVersionCheck=true`,
`--set graphql.app.features.copilotAuthorization=true`,
`--set sync.replicaCount=${syncReplicaCount}`,
`--set-string sync.image.tag="${imageTag}"`,
...serviceAnnotations,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ metadata:
type: Opaque
data:
openaiSecret: {{ .Values.app.copilot.openai.key | b64enc }}
falSecret: {{ .Values.app.copilot.fal.key | b64enc }}
{{- end }}
7 changes: 7 additions & 0 deletions .github/helm/affine/charts/graphql/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ spec:
value: "{{ .Values.app.features.earlyAccessPreview }}"
- name: FEATURES_SYNC_CLIENT_VERSION_CHECK
value: "{{ .Values.app.features.syncClientVersionCheck }}"
- name: FEATURES_COPILOT_SKIP_AUTHORIZATION
value: "{{ .Values.app.features.copilotAuthorization }}"
- name: MAILER_HOST
valueFrom:
secretKeyRef:
Expand Down Expand Up @@ -154,6 +156,11 @@ spec:
secretKeyRef:
name: "{{ .Values.app.copilot.secretName }}"
key: openaiSecret
- name: COPILOT_FAL_API_KEY
valueFrom:
secretKeyRef:
name: "{{ .Values.app.copilot.secretName }}"
key: falSecret
{{ end }}
{{ if .Values.app.oauth.google.enabled }}
- name: OAUTH_GOOGLE_ENABLED
Expand Down
1 change: 1 addition & 0 deletions .github/helm/affine/charts/graphql/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ app:
features:
earlyAccessPreview: false
syncClientVersionCheck: false
copilotAuthorization: false

serviceAccount:
create: true
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ jobs:
R2_SECRET_ACCESS_KEY: ${{ secrets.R2_SECRET_ACCESS_KEY }}
CAPTCHA_TURNSTILE_SECRET: ${{ secrets.CAPTCHA_TURNSTILE_SECRET }}
COPILOT_OPENAI_API_KEY: ${{ secrets.COPILOT_OPENAI_API_KEY }}
COPILOT_FAL_API_KEY: ${{ secrets.COPILOT_FAL_API_KEY }}
MAILER_SENDER: ${{ secrets.OAUTH_EMAIL_SENDER }}
MAILER_USER: ${{ secrets.OAUTH_EMAIL_LOGIN }}
MAILER_PASSWORD: ${{ secrets.OAUTH_EMAIL_PASSWORD }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ CREATE TABLE "ai_prompts_messages" (
"idx" INTEGER NOT NULL,
"role" "AiPromptRole" NOT NULL,
"content" TEXT NOT NULL,
"attachments" JSON,
"params" JSON,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
);
Expand All @@ -47,6 +48,8 @@ CREATE TABLE "ai_sessions_messages" (
"session_id" VARCHAR(36) NOT NULL,
"role" "AiPromptRole" NOT NULL,
"content" TEXT NOT NULL,
"attachments" JSON,
"params" JSON,
"created_at" TIMESTAMPTZ(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updated_at" TIMESTAMPTZ(6) NOT NULL,

Expand Down
27 changes: 15 additions & 12 deletions packages/backend/server/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,16 @@ enum AiPromptRole {
}

model AiPromptMessage {
promptId Int @map("prompt_id") @db.Integer
promptId Int @map("prompt_id") @db.Integer
// if a group of prompts contains multiple sentences, idx specifies the order of each sentence
idx Int @db.Integer
idx Int @db.Integer
// system/assistant/user
role AiPromptRole
role AiPromptRole
// prompt content
content String @db.Text
params Json? @db.Json
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
content String @db.Text
attachments Json? @db.Json
params Json? @db.Json
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)

prompt AiPrompt @relation(fields: [promptId], references: [id], onDelete: Cascade)

Expand All @@ -462,12 +463,14 @@ model AiPrompt {
}

model AiSessionMessage {
id String @id @default(uuid()) @db.VarChar(36)
sessionId String @map("session_id") @db.VarChar(36)
role AiPromptRole
content String @db.Text
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz(6)
id String @id @default(uuid()) @db.VarChar(36)
sessionId String @map("session_id") @db.VarChar(36)
role AiPromptRole
content String @db.Text
attachments Json? @db.Json
params Json? @db.Json
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(6)
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz(6)

session AiSession @relation(fields: [sessionId], references: [id], onDelete: Cascade)

Expand Down
5 changes: 5 additions & 0 deletions packages/backend/server/src/config/affine.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ AFFiNE.ENV_MAP = {
THROTTLE_TTL: ['rateLimiter.ttl', 'int'],
THROTTLE_LIMIT: ['rateLimiter.limit', 'int'],
COPILOT_OPENAI_API_KEY: 'plugins.copilot.openai.apiKey',
COPILOT_FAL_API_KEY: 'plugins.copilot.fal.apiKey',
REDIS_SERVER_HOST: 'plugins.redis.host',
REDIS_SERVER_PORT: ['plugins.redis.port', 'int'],
REDIS_SERVER_USER: 'plugins.redis.username',
Expand All @@ -37,5 +38,9 @@ AFFiNE.ENV_MAP = {
'featureFlags.syncClientVersionCheck',
'boolean',
],
FEATURES_COPILOT_SKIP_AUTHORIZATION: [
'featureFlags.copilotAuthorization',
'boolean',
],
TELEMETRY_ENABLE: ['telemetry.enabled', 'boolean'],
};
48 changes: 30 additions & 18 deletions packages/backend/server/src/data/migrations/utils/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,22 @@ export const prompts: Prompt[] = [
model: 'gpt-4-vision-preview',
messages: [],
},
{
name: 'debug:action:dalle3',
action: 'image',
model: 'dall-e-3',
messages: [],
},
{
name: 'debug:action:fal-sd15',
action: 'image',
model: '110602490-lcm-sd15-i2i',
messages: [],
},
{
name: 'Summary',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -46,7 +58,7 @@ export const prompts: Prompt[] = [
{
name: 'Summary the webpage',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -58,7 +70,7 @@ export const prompts: Prompt[] = [
{
name: 'Explain this image',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-vision-preview',
messages: [
{
role: 'assistant',
Expand All @@ -70,7 +82,7 @@ export const prompts: Prompt[] = [
{
name: 'Explain this code',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -82,7 +94,7 @@ export const prompts: Prompt[] = [
{
name: 'Translate to',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -108,7 +120,7 @@ export const prompts: Prompt[] = [
{
name: 'Write an article about this',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -119,7 +131,7 @@ export const prompts: Prompt[] = [
{
name: 'Write a twitter about this',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -130,7 +142,7 @@ export const prompts: Prompt[] = [
{
name: 'Write a poem about this',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -141,7 +153,7 @@ export const prompts: Prompt[] = [
{
name: 'Write a blog post about this',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -152,7 +164,7 @@ export const prompts: Prompt[] = [
{
name: 'Change tone to',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -165,7 +177,7 @@ export const prompts: Prompt[] = [
{
name: 'Brainstorm ideas about this',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -177,7 +189,7 @@ export const prompts: Prompt[] = [
{
name: 'Improve writing for it',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -189,7 +201,7 @@ export const prompts: Prompt[] = [
{
name: 'Improve grammar for it',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -201,7 +213,7 @@ export const prompts: Prompt[] = [
{
name: 'Fix spelling for it',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -227,7 +239,7 @@ export const prompts: Prompt[] = [
{
name: 'Find action items from it',
action: 'todo-list',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -239,7 +251,7 @@ export const prompts: Prompt[] = [
{
name: 'Check code error',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -251,7 +263,7 @@ export const prompts: Prompt[] = [
{
name: 'Create a presentation',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand All @@ -263,7 +275,7 @@ export const prompts: Prompt[] = [
{
name: 'Create headings',
action: 'text',
model: 'gpt-3.5-turbo',
model: 'gpt-4-turbo-preview',
messages: [
{
role: 'assistant',
Expand Down
1 change: 1 addition & 0 deletions packages/backend/server/src/fundamentals/config/def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export interface AFFiNEConfig {
featureFlags: {
earlyAccessPreview: boolean;
syncClientVersionCheck: boolean;
copilotAuthorization: boolean;
};

/**
Expand Down
1 change: 1 addition & 0 deletions packages/backend/server/src/fundamentals/config/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const getDefaultAFFiNEConfig: () => AFFiNEConfig = () => {
featureFlags: {
earlyAccessPreview: false,
syncClientVersionCheck: false,
copilotAuthorization: false,
},
https: false,
host: 'localhost',
Expand Down
10 changes: 10 additions & 0 deletions packages/backend/server/src/fundamentals/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ export type GraphqlContext = {
GraphQLModule.forRootAsync<ApolloDriverConfig>({
driver: ApolloDriver,
useFactory: (config: Config) => {
const copilotAuthorization = config.featureFlags.copilotAuthorization;
const cors = {
cors: {
origin: [
'https://try-blocksuite.vercel.app/',
'http://localhost:5173/',
],
},
};
return {
...config.graphql,
path: `${config.path}/graphql`,
Expand Down Expand Up @@ -78,6 +87,7 @@ export type GraphqlContext = {

return formattedError;
},
...(copilotAuthorization ? cors : {}),
};
},
inject: [Config],
Expand Down