Skip to content

Commit

Permalink
feature: do not fail to create documents if no provider found
Browse files Browse the repository at this point in the history
Just create the thing without the frontmatter
  • Loading branch information
geclos committed Sep 27, 2024
1 parent 62353a7 commit 2a0c7cd
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
8 changes: 3 additions & 5 deletions packages/core/src/services/documents/create.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { describe, expect, it } from 'vitest'

import { BadRequestError } from '../../lib'
import { DocumentVersionsRepository } from '../../repositories'
import { mergeCommit } from '../commits/merge'
import { createNewDocument } from './create'
Expand Down Expand Up @@ -95,7 +94,7 @@ model: gpt-4o-mini
)
})

it('fails when no provider is found', async (ctx) => {
it('creates the document without the frontmatter if no provider is found', async (ctx) => {
const { project, user } = await ctx.factories.createProject({
providers: [],
})
Expand All @@ -106,8 +105,7 @@ model: gpt-4o-mini
path: 'newdoc',
})

expect(result.error).toEqual(
new BadRequestError('No provider found when creating document'),
)
expect(result.ok).toBe(true)
expect(result.unwrap().content).toBe('')
})
})
13 changes: 4 additions & 9 deletions packages/core/src/services/documents/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,26 +47,21 @@ export async function createNewDocument(

const providerScope = new ProviderApiKeysRepository(workspace!.id, tx)
const provider = await providerScope.findFirst().then((r) => r.unwrap())

if (!provider) {
return Result.error(
new BadRequestError('No provider found when creating document'),
)
}

const newDoc = await tx
.insert(documentVersions)
.values({
commitId: commit.id,
path,
content:
content ??
`
(provider
? `
---
provider: ${provider.name}
model: ${findFirstModelForProvider(provider.provider)}
---
`.trim(),
`.trim()
: ''),
})
.returning()

Expand Down
8 changes: 3 additions & 5 deletions packages/core/src/services/evaluations/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
EvaluationMetadataType,
EvaluationResultableType,
} from '../../constants'
import { BadRequestError } from '../../lib'
import * as factories from '../../tests/factories'
import { createEvaluation } from './create'

Expand All @@ -21,7 +20,7 @@ describe('createEvaluation', () => {
})

describe('without existing provider', () => {
it('fails when there is no provider', async () => {
it('creates the evaluation without the frontmatter if no provider is found', async () => {
const name = 'Test Evaluation'
const description = 'Test Description'
const metadata = { prompt: 'Test prompt' }
Expand All @@ -36,9 +35,8 @@ describe('createEvaluation', () => {
metadata,
})

expect(result.error).toEqual(
new BadRequestError('No provider found when creating evaluation'),
)
expect(result.ok).toBe(true)
expect(result.unwrap().metadata.prompt).toBe(metadata.prompt)
})
})

Expand Down
10 changes: 3 additions & 7 deletions packages/core/src/services/evaluations/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ export async function createEvaluation(
const providerScope = new ProviderApiKeysRepository(workspace!.id, db)
const providerResult = await providerScope.findFirst()
const provider = providerResult.unwrap()

if (!provider) {
return Result.error(
new BadRequestError('No provider found when creating evaluation'),
)
}
const meta = metadata as { prompt: string; templateId?: number }
const promptWithProvider = `---
const promptWithProvider = provider
? `---
provider: ${provider.name}
model: ${findFirstModelForProvider(provider.provider)}
---
${meta.prompt}
`.trim()
: meta.prompt

return await Transaction.call(async (tx) => {
let metadataTable
Expand Down

0 comments on commit 2a0c7cd

Please sign in to comment.