Skip to content

Commit

Permalink
feature: default frontmatter for new documents and changed name of de…
Browse files Browse the repository at this point in the history
…fault provider to Latitude
  • Loading branch information
geclos committed Sep 18, 2024
1 parent d05d404 commit 0825ece
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion apps/web/src/services/user/setupService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function setupService({
{
workspace,
provider: Providers.OpenAI,
name: 'OpenAI',
name: env.DEFAULT_PROVIDER_ID,
token: env.DEFAULT_PROVIDER_API_KEY,
authorId: user.id,
},
Expand Down
32 changes: 32 additions & 0 deletions packages/core/src/services/documents/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from '@latitude-data/env'
import { describe, expect, it } from 'vitest'

import { DocumentVersionsRepository } from '../../repositories'
Expand Down Expand Up @@ -81,4 +82,35 @@ describe('createNewDocument', () => {
expect(result.ok).toBe(false)
expect(result.error!.message).toBe('Cannot modify a merged commit')
})

it('creates a new document with default content when no content is provided', async (ctx) => {
const { project, user } = await ctx.factories.createProject()
const { commit } = await ctx.factories.createDraft({ project, user })

const documentResult = await createNewDocument({
commit,
path: 'newdoc',
})

const document = documentResult.unwrap()
expect(document.path).toBe('newdoc')

const scope = new DocumentVersionsRepository(project.workspaceId)
const commitChanges = await scope.listCommitChanges(commit)
expect(commitChanges.value.length).toBe(1)

const createdDocument = commitChanges.value[0]!
expect(createdDocument.documentUuid).toBe(document.documentUuid)
expect(createdDocument.path).toBe(document.path)

// Check for default content
expect(createdDocument.content).toBe(
`
---
provider: ${env.DEFAULT_PROVIDER_ID}
model: gpt-4o-mini
---
`.trim(),
)
})
})
10 changes: 9 additions & 1 deletion packages/core/src/services/documents/create.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { env } from '@latitude-data/env'
import { eq } from 'drizzle-orm'

import type { Commit, DocumentVersion } from '../../browser'
Expand Down Expand Up @@ -42,7 +43,14 @@ export async function createNewDocument(
.values({
commitId: commit.id,
path,
content: content ?? '',
content:
content ??
`
---
provider: ${env.DEFAULT_PROVIDER_ID}
model: gpt-4o
---
`.trim(),
})
.returning()

Expand Down
2 changes: 2 additions & 0 deletions packages/env/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ export const env = createEnv({
SENTRY_DSN: z.string().optional(),
SENTRY_ORG: z.string().optional(),
SENTRY_PROJECT: z.string().optional(),
DEFAULT_PROVIDER_ID: z.string(),
},
runtimeEnv: {
...process.env,
FILE_PUBLIC_PATH: process.env.FILE_PUBLIC_PATH ?? FILE_PUBLIC_PATH,
DRIVE_DISK: process.env.DRIVE_DISK ?? 'local',
QUEUE_PORT: process.env.QUEUE_PORT ?? '6379',
CACHE_PORT: process.env.CACHE_PORT ?? '6379',
DEFAULT_PROVIDER_ID: 'Latitude',
},
})

0 comments on commit 0825ece

Please sign in to comment.