Skip to content

Commit

Permalink
feat: wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon committed Oct 21, 2024
1 parent a2cd5fb commit 1d69e31
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -1,55 +1,90 @@
import { describe, expect, it } from 'vitest'
import { beforeEach, describe, expect, it } from 'vitest'

import { Commit, DocumentLog, DocumentVersion, Project } from '../../browser'
import { mergeCommit } from '../../services/commits'
import { updateDocument } from '../../services/documents'
import * as factories from '../../tests/factories'
import { computeDocumentLogsWithMetadataQuery } from './computeDocumentLogsWithMetadata'

describe('getDocumentLogsWithMetadata', () => {
it('return all logs from merged commits', async () => {
const { project, user, workspace, providers } =
await factories.createProject()
const { commit: commit1 } = await factories.createDraft({ project, user })
const { documentVersion: doc } = await factories.createDocumentVersion({
workspace,
user,
commit: commit1,
path: 'folder1/doc1',
content: factories.helpers.createPrompt({
provider: providers[0]!,
content: 'VERSION_1',
}),
})
await mergeCommit(commit1).then((r) => r.unwrap())
describe('logs from merged commits', () => {
let doc: DocumentVersion
let log1: DocumentLog
let log2: DocumentLog
let project: Project
let draft: Commit

const { commit: commit2 } = await factories.createDraft({ project, user })
await updateDocument({
commit: commit2,
document: doc,
content: factories.helpers.createPrompt({
provider: providers[0]!,
content: 'VERSION_2',
}),
})
await mergeCommit(commit2).then((r) => r.unwrap())
beforeEach(async () => {
const setup = await factories.createProject()
project = setup.project
const provider = setup.providers[0]!
const { commit: commit1 } = await factories.createDraft({
project,
user: setup.user,
})
const { documentVersion: docVersion } =
await factories.createDocumentVersion({
workspace: setup.workspace,
user: setup.user,
commit: commit1,
path: 'folder1/doc1',
content: factories.helpers.createPrompt({
provider,
content: 'VERSION_1',
}),
})
doc = docVersion
await mergeCommit(commit1).then((r) => r.unwrap())

const { documentLog: log1 } = await factories.createDocumentLog({
document: doc,
commit: commit1,
})
const { documentLog: log2 } = await factories.createDocumentLog({
document: doc,
commit: commit2,
const { commit: commit2 } = await factories.createDraft({
project,
user: setup.user,
})
draft = commit2
await updateDocument({
commit: commit2,
document: doc,
content: factories.helpers.createPrompt({
provider,
content: 'VERSION_2',
}),
})
await mergeCommit(commit2).then((r) => r.unwrap())

const { documentLog: docLog1 } = await factories.createDocumentLog({
document: doc,
commit: commit1,
})
log1 = docLog1
const { documentLog: docLog2 } = await factories.createDocumentLog({
document: doc,
commit: commit2,
})
log2 = docLog2
})

const result = await computeDocumentLogsWithMetadataQuery({
workspaceId: project.workspaceId,
documentUuid: doc.documentUuid,
draft: commit2,
it('return all logs from merged commits', async () => {
const result = await computeDocumentLogsWithMetadataQuery({
workspaceId: project.workspaceId,
documentUuid: doc.documentUuid,
draft,
})

expect(result.find((l) => l.uuid === log1.uuid)).toBeDefined()
expect(result.find((l) => l.uuid === log2.uuid)).toBeDefined()
})

expect(result.find((l) => l.uuid === log1.uuid)).toBeDefined()
expect(result.find((l) => l.uuid === log2.uuid)).toBeDefined()
it('paginate logs', async () => {
const result = await computeDocumentLogsWithMetadataQuery({
workspaceId: project.workspaceId,
documentUuid: doc.documentUuid,
draft,
page: '1',
pageSize: '1',
})

expect(result.length).toBe(1)
})
})

it('includes logs from specified draft', async () => {
Expand Down Expand Up @@ -207,4 +242,35 @@ describe('getDocumentLogsWithMetadata', () => {
result.find((l) => l.uuid === log.uuid)?.costInMillicents,
).toBeTypeOf('number')
})

it('returns logs without provider logs', async () => {
const { project, user, workspace, providers } =
await factories.createProject()
const { commit: commit1 } = await factories.createDraft({ project, user })
const { documentVersion: doc } = await factories.createDocumentVersion({
workspace,
user,
commit: commit1,
path: 'folder1/doc1',
content: factories.helpers.createPrompt({
provider: providers[0]!,
content: 'VERSION_1',
}),
})
await mergeCommit(commit1).then((r) => r.unwrap())

const { documentLog: log1 } = await factories.createDocumentLog({
document: doc,
commit: commit1,
skipProviderLogs: true,
})

const result = await computeDocumentLogsWithMetadataQuery({
workspaceId: project.workspaceId,
documentUuid: doc.documentUuid,
draft: commit1,
})

expect(result.find((l) => l.uuid === log1.uuid)).toBeDefined()
})
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { and, desc, eq, sum } from 'drizzle-orm'
import { and, desc, eq, getTableColumns, sum } from 'drizzle-orm'

import { Commit } from '../../browser'
import { database } from '../../client'
Expand All @@ -10,11 +10,9 @@ import { commits, providerLogs } from '../../schema'
import { getCommitFilter } from './_createDocumentLogQuery'

function getRepositoryScopes(workspaceId: number, db = database) {
const documentLogsScope = new DocumentLogsWithErrorsRepository(
workspaceId,
db,
).scope
return { documentLogsScope }
const scope = new DocumentLogsWithErrorsRepository(workspaceId, db).scope

return { scope }
}

function getCommonQueryConditions(
Expand All @@ -25,7 +23,6 @@ function getCommonQueryConditions(
return and(eq(scope.documentUuid, documentUuid), getCommitFilter(draft))
}

// TODO: Add test document log without provider should appear here
export function computeDocumentLogsWithMetadataQuery(
{
workspaceId,
Expand All @@ -42,58 +39,50 @@ export function computeDocumentLogsWithMetadataQuery(
},
db = database,
) {
const { documentLogsScope } = getRepositoryScopes(workspaceId, db)
const { scope } = getRepositoryScopes(workspaceId, db)
const offset = (parseInt(page) - 1) * parseInt(pageSize)
const filteredSubQuery = db
.select({
id: documentLogsScope.id,
tokens: sum(providerLogs.tokens).mapWith(Number).as('tokens'),
duration: sum(providerLogs.duration).mapWith(Number).as('duration_in_ms'),
costInMillicents: sum(providerLogs.costInMillicents)
.mapWith(Number)
.as('cost_in_millicents'),
id: scope.id,
error: scope.error,
})
.from(documentLogsScope)
.leftJoin(
providerLogs,
eq(providerLogs.documentLogUuid, documentLogsScope.uuid),
)
.where(getCommonQueryConditions(documentLogsScope, documentUuid, draft))
.orderBy(desc(documentLogsScope.createdAt))
.from(scope)
.innerJoin(commits, eq(commits.id, scope.commitId))
.leftJoin(providerLogs, eq(providerLogs.documentLogUuid, scope.uuid))
.where(getCommonQueryConditions(scope, documentUuid, draft))
.orderBy(desc(scope.createdAt))
.limit(parseInt(pageSize))
.offset(offset)
.as('filteredDocumentLogsSubQuery')

const aggregatedFieldsSubQuery = db
.select({
id: documentLogsScope.id,
id: scope.id,
tokens: sum(providerLogs.tokens).mapWith(Number).as('tokens'),
duration: sum(providerLogs.duration).mapWith(Number).as('duration_in_ms'),
costInMillicents: sum(providerLogs.costInMillicents)
.mapWith(Number)
.as('cost_in_millicents'),
})
.from(documentLogsScope)
.innerJoin(filteredSubQuery, eq(filteredSubQuery.id, documentLogsScope.id))
.leftJoin(
providerLogs,
eq(providerLogs.documentLogUuid, documentLogsScope.uuid),
)
.groupBy(documentLogsScope.id)
.from(scope)
.innerJoin(filteredSubQuery, eq(filteredSubQuery.id, scope.id))
.leftJoin(providerLogs, eq(providerLogs.documentLogUuid, scope.uuid))
.groupBy(scope.id)
.as('aggregatedFieldsSubQuery')

return db
.select({
...documentLogsScope._.selectedFields,
commit: commits,
...scope._.selectedFields,
commit: getTableColumns(commits),
tokens: aggregatedFieldsSubQuery.tokens,
duration: aggregatedFieldsSubQuery.duration,
costInMillicents: aggregatedFieldsSubQuery.costInMillicents,
})
.from(documentLogsScope)
.innerJoin(commits, eq(commits.id, documentLogsScope.commitId))
.from(scope)
.innerJoin(commits, eq(commits.id, scope.commitId))
.innerJoin(
aggregatedFieldsSubQuery,
eq(aggregatedFieldsSubQuery.id, documentLogsScope.id),
eq(aggregatedFieldsSubQuery.id, scope.id),
)
.orderBy(desc(scope.createdAt))
}
67 changes: 49 additions & 18 deletions packages/core/src/tests/factories/documentLogs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ContentType, createChain } from '@latitude-data/compiler'
import { v4 as uuid } from 'uuid'

import { Commit, DocumentVersion, LogSources, ProviderLog } from '../../browser'
import {
Commit,
DocumentVersion,
LogSources,
ProviderLog,
Workspace,
} from '../../browser'
import { findWorkspaceFromCommit } from '../../data-access'
import { ProviderApiKeysRepository } from '../../repositories'
import { Config } from '../../services/ai'
Expand All @@ -16,32 +22,28 @@ export type IDocumentLogData = {
parameters?: Record<string, unknown>
customIdentifier?: string
createdAt?: Date
skipProviderLogs?: boolean
}

export async function createDocumentLog({
document,
commit,
async function generateProviderLogs({
workspace,
parameters,
customIdentifier,
createdAt,
}: IDocumentLogData) {
const workspace = (await findWorkspaceFromCommit(commit))!
const providerScope = new ProviderApiKeysRepository(workspace.id)

const documentContent = await getResolvedContent({
workspaceId: workspace.id,
document,
commit,
}).then((r) => r.unwrap())

documentContent,
documentLogUuid,
}: {
workspace: Workspace
parameters?: Record<string, unknown>
documentContent: string
documentLogUuid: string
}) {
const providerLogs: ProviderLog[] = []
const chain = createChain({
prompt: documentContent,
parameters: parameters ?? {},
})
let mockedResponse = undefined
const providerScope = new ProviderApiKeysRepository(workspace.id)

const documentLogUuid = uuid()
const providerLogs: ProviderLog[] = []
while (true) {
const { completed, conversation } = await chain.step(mockedResponse)

Expand Down Expand Up @@ -86,6 +88,35 @@ export async function createDocumentLog({
if (completed) break
}

return providerLogs
}

export async function createDocumentLog({
document,
commit,
parameters,
customIdentifier,
createdAt,
skipProviderLogs,
}: IDocumentLogData) {
const workspace = (await findWorkspaceFromCommit(commit))!
const documentContent = await getResolvedContent({
workspaceId: workspace.id,
document,
commit,
}).then((r) => r.unwrap())
const documentLogUuid = uuid()
let providerLogs: ProviderLog[] = []

if (!skipProviderLogs) {
providerLogs = await generateProviderLogs({
workspace,
parameters,
documentContent,
documentLogUuid,
})
}

const duration =
Math.floor(Math.random() * 100) +
providerLogs.reduce((acc, log) => acc + log.duration, 0)
Expand Down

0 comments on commit 1d69e31

Please sign in to comment.