Skip to content

Commit

Permalink
feature: Import logs modal
Browse files Browse the repository at this point in the history
Modal to import logs while testing the evaluation in the editor
  • Loading branch information
geclos committed Sep 5, 2024
1 parent fac3b74 commit 1db6b6d
Show file tree
Hide file tree
Showing 51 changed files with 2,664 additions and 189 deletions.
2 changes: 1 addition & 1 deletion .tmuxinator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
name: latitude-llm
windows:
- web: cd apps/web
- apps: pnpm dev --filter='./apps/*'
- apps: pnpm dev --filter='./apps/*' --filter='./packages/**'
- docker: docker compose up
- studio: cd packages/core && pnpm db:studio
28 changes: 2 additions & 26 deletions apps/gateway/src/routes/api/v1/chats/handlers/addMessage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { testConsumeStream } from 'test/helpers'
import { beforeEach, describe, expect, it, vi } from 'vitest'

const mocks = vi.hoisted(() => ({
addMessages: vi.fn(async ({ providerLogHandler }) => {
providerLogHandler({ uuid: 'fake-provider-log-uuid' })
addMessages: vi.fn(async () => {
const stream = new ReadableStream({
start(controller) {
controller.enqueue({
Expand All @@ -43,9 +42,7 @@ const mocks = vi.hoisted(() => ({
}),
queues: {
defaultQueue: {
jobs: {
enqueueCreateProviderLogJob: vi.fn(),
},
jobs: {},
},
},
}))
Expand Down Expand Up @@ -149,26 +146,5 @@ describe('POST /add-message', () => {
providerLogHandler: expect.any(Function),
})
})

it('enqueue the provider log of the new message', async () => {
const res = await app.request(route, {
method: 'POST',
body,
headers,
})

expect(mocks.queues)
expect(res.status).toBe(200)
expect(res.body).toBeInstanceOf(ReadableStream)
await testConsumeStream(res.body as ReadableStream)

expect(
mocks.queues.defaultQueue.jobs.enqueueCreateProviderLogJob,
).toHaveBeenCalledWith({
uuid: 'fake-provider-log-uuid',
source: LogSources.Playground,
apiKeyId: apiKey.id,
})
})
})
})
9 changes: 4 additions & 5 deletions apps/gateway/src/routes/api/v1/chats/handlers/addMessage.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { zValidator } from '@hono/zod-validator'
import { LogSources } from '@latitude-data/core/browser'
import { addMessages } from '@latitude-data/core/services/documentLogs/index'
import { createProviderLog } from '@latitude-data/core/services/providerLogs/create'
import { messageSchema } from '$/common/messageSchema'
import { pipeToStream } from '$/common/pipeToStream'
import { queues } from '$/jobs'
import { Factory } from 'hono/factory'
import { streamSSE } from 'hono/streaming'
import { z } from 'zod'
Expand All @@ -28,13 +28,12 @@ export const addMessageHandler = factory.createHandlers(
workspace,
documentLogUuid,
messages,
providerLogHandler: (log) => {
// TODO: Review why this is possibly undefined now
queues.defaultQueue.jobs.enqueueCreateProviderLogJob!({
providerLogHandler: async (log) => {
await createProviderLog({
...log,
source,
apiKeyId: apiKey.id,
})
}).then((r) => r.unwrap())
},
}).then((r) => r.unwrap())

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { zValidator } from '@hono/zod-validator'
import { LogSources } from '@latitude-data/core/browser'
import { runDocumentAtCommit } from '@latitude-data/core/services/commits/runDocumentAtCommit'
import { createProviderLog } from '@latitude-data/core/services/providerLogs/create'
import { pipeToStream } from '$/common/pipeToStream'
import { queues } from '$/jobs'
import { Factory } from 'hono/factory'
Expand Down Expand Up @@ -42,16 +43,17 @@ export const runHandler = factory.createHandlers(
commit,
parameters,
providerLogHandler: (log) => {
queues.defaultQueue.jobs.enqueueCreateProviderLogJob({
createProviderLog({
...log,
source,
apiKeyId: apiKey.id,
})
}).then((r) => r.unwrap())
},
}).then((r) => r.unwrap())

await pipeToStream(stream, result.stream)

// TODO: review if this is needed and why it's not in addMessages handler?
queues.defaultQueue.jobs.enqueueCreateDocumentLogJob({
commit,
data: {
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/actions/documents/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ export const createDocumentVersionAction = withProject
.input(
z.object({
path: z.string(),
commitId: z.number(),
commitUuid: z.string(),
}),
{ type: 'json' },
)
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.getCommitByUuid({ uuid: input.commitUuid, project: ctx.project })
.then((r) => r.unwrap())

const result = await createNewDocument({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('destroyDocumentAction', async () => {
it('fails when the user is not authenticated', async () => {
const [_, error] = await destroyDocumentAction({
projectId: project.id,
commitId: draft.id,
commitUuid: draft.uuid,
documentUuid: document.documentUuid,
})

Expand All @@ -72,7 +72,7 @@ describe('destroyDocumentAction', async () => {
const otherWorkspaceDocument = allDocs[0]!
const [_, error] = await destroyDocumentAction({
projectId: otherWorkspaceProject.id,
commitId: otherCommit.id,
commitUuid: otherCommit.uuid,
documentUuid: otherWorkspaceDocument.documentUuid,
})
expect(error?.name).toEqual('NotFoundError')
Expand All @@ -81,7 +81,7 @@ describe('destroyDocumentAction', async () => {
it('fails when trying to remove a document from a merged commit', async () => {
const [_, error] = await destroyDocumentAction({
projectId: project.id,
commitId: merged.id,
commitUuid: merged.uuid,
documentUuid: document.documentUuid,
})
expect(error?.name).toEqual('BadRequestError')
Expand All @@ -90,7 +90,7 @@ describe('destroyDocumentAction', async () => {
it('creates a soft deleted documents in draft document', async () => {
await destroyDocumentAction({
projectId: project.id,
commitId: draft.id,
commitUuid: draft.uuid,
documentUuid: document.documentUuid,
})
// TODO: move to core
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/actions/documents/destroyDocumentAction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { z } from 'zod'

export const destroyDocumentAction = withProject
.createServerAction()
.input(z.object({ documentUuid: z.string(), commitId: z.number() }), {
.input(z.object({ documentUuid: z.string(), commitUuid: z.string() }), {
type: 'json',
})
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.getCommitByUuid({ uuid: input.commitUuid, project: ctx.project })
.then((r) => r.unwrap())
const docsScope = new DocumentVersionsRepository(ctx.project.workspaceId)
const document = await docsScope
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/actions/documents/destroyFolderAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import { withProject } from '../procedures'

export const destroyFolderAction = withProject
.createServerAction()
.input(z.object({ path: z.string(), commitId: z.number() }), {
.input(z.object({ path: z.string(), commitUuid: z.string() }), {
type: 'json',
})
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.getCommitByUuid({ uuid: input.commitUuid, project: ctx.project })
.then((r) => r.unwrap())
const result = await destroyFolder({
path: input.path,
Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/actions/documents/getDocumentsAtCommitAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { withProject } from '../procedures'

export const getDocumentsAtCommitAction = withProject
.createServerAction()
.input(z.object({ commitId: z.number() }))
.input(z.object({ commitUuid: z.string() }))
.handler(async ({ input, ctx }) => {
const commit = await new CommitsRepository(ctx.project.workspaceId)
.getCommitById(input.commitId)
.getCommitByUuid({ uuid: input.commitUuid, project: ctx.project })
.then((r) => r.unwrap())
const docsScope = new DocumentVersionsRepository(ctx.project.workspaceId)
const result = await docsScope.getDocumentsAtCommit(commit)
Expand Down
10 changes: 7 additions & 3 deletions apps/web/src/actions/documents/updateContent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DocumentVersion, Project, SafeUser } from '@latitude-data/core/browser'
import * as factories from '@latitude-data/core/factories'
import { updateDocument } from '@latitude-data/core/services/documents/update'
import { findCommitById } from 'node_modules/@latitude-data/core/src/data-access/commits'
import { beforeEach, describe, expect, it, vi } from 'vitest'

import { updateDocumentContentAction } from './updateContent'
Expand Down Expand Up @@ -32,10 +33,13 @@ describe('updateDocumentAction', async () => {
})

it('errors when the user is not authenticated', async () => {
const commit = await findCommitById({ id: doc1.commitId }).then((r) =>
r.unwrap(),
)
const [_, error] = await updateDocumentContentAction({
projectId,
documentUuid: doc1.documentUuid,
commitId: doc1.commitId,
commitUuid: commit.uuid,
content: 'foo2',
})

Expand Down Expand Up @@ -78,7 +82,7 @@ describe('updateDocumentAction', async () => {
const [data, error] = await updateDocumentContentAction({
projectId: project.id,
documentUuid: doc1.documentUuid,
commitId: draft.id,
commitUuid: draft.uuid,
content: 'foo3',
})

Expand All @@ -96,7 +100,7 @@ describe('updateDocumentAction', async () => {
const [data, error] = await updateDocumentContentAction({
projectId: project.id,
documentUuid: doc1.documentUuid,
commitId: draft.id,
commitUuid: draft.uuid,
content: 'foo2',
})

Expand Down
4 changes: 2 additions & 2 deletions apps/web/src/actions/documents/updateContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ export const updateDocumentContentAction = withProject
.input(
z.object({
documentUuid: z.string(),
commitId: z.number(),
commitUuid: z.string(),
content: z.string(),
}),
{ type: 'json' },
)
.handler(async ({ input, ctx }) => {
const commitsScope = new CommitsRepository(ctx.project.workspaceId)
const commit = await commitsScope
.getCommitById(input.commitId)
.getCommitByUuid({ uuid: input.commitUuid, project: ctx.project })
.then((r) => r.unwrap())
const docsScope = new DocumentVersionsRepository(ctx.project.workspaceId)
const document = await docsScope
Expand Down
34 changes: 34 additions & 0 deletions apps/web/src/actions/providerLogs/fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use server'

import { ProviderLogsRepository } from '@latitude-data/core/repositories'
import { z } from 'zod'

import { authProcedure } from '../procedures'

export const getProviderLogsAction = authProcedure
.createServerAction()
.input(
z.object({
documentUuid: z.string().optional(),
documentLogUuid: z.string().optional(),
}),
)
.handler(async ({ input, ctx }) => {
const { documentUuid, documentLogUuid } = input
const scope = new ProviderLogsRepository(ctx.workspace.id)

let result
if (documentLogUuid) {
result = await scope
.findByDocumentLogUuid(documentLogUuid, { limit: 1000 })
.then((r) => r.unwrap())
} else if (documentUuid) {
result = await scope
.findByDocumentUuid(documentUuid)
.then((r) => r.unwrap())
} else {
result = await scope.findAll({ limit: 1000 }).then((r) => r.unwrap())
}

return result
})

This file was deleted.

2 changes: 1 addition & 1 deletion apps/web/src/actions/sdk/addMessagesAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export async function addMessagesAction({
const sdk = await createSdk().then((r) => r.unwrap())
const stream = createStreamableValue<ChainEvent, Error>()

const response = sdk.addMessges({
const response = sdk.addMessages({
params: { documentLogUuid, messages, source: LogSources.Playground },
onMessage: (chainEvent) => {
stream.update(chainEvent)
Expand Down
7 changes: 7 additions & 0 deletions apps/web/src/app/(private)/_data-access/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
DocumentVersionsRepository,
EvaluationsRepository,
ProjectsRepository,
ProviderLogsRepository,
} from '@latitude-data/core/repositories/index'
import { getCurrentUser } from '$/services/auth/getCurrentUser'
import { notFound } from 'next/navigation'
Expand Down Expand Up @@ -163,3 +164,9 @@ export const getEvaluationByUuidCached = cache(async (uuid: string) => {

return evaluation
})

export const getProviderLogCached = cache(async (uuid: string) => {
const { workspace } = await getCurrentUser()
const scope = new ProviderLogsRepository(workspace.id)
return await scope.findByUuid(uuid).then((r) => r.unwrap())
})
Loading

0 comments on commit 1db6b6d

Please sign in to comment.