-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit implements the ability to upload user generated logs to a project. It adds a new route for uploading logs and a new page for the user to upload logs. Logs are stored in the same way as regular document logs but they lack some optional fields such as duration, cost, token count or finish reason. The only required field is the messages array.
- Loading branch information
Showing
49 changed files
with
12,651 additions
and
224 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...teway/src/routes/api/v2/projects/[projectId]/versions/[versionUuid]/logs/handlers/post.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { zValidator } from '@hono/zod-validator' | ||
import { LogSources, messagesSchema } from '@latitude-data/core/browser' | ||
import { generateUUIDIdentifier } from '@latitude-data/core/lib/generateUUID' | ||
import { createDocumentLog } from '@latitude-data/core/services/documentLogs/create' | ||
import { getData } from '$/routes/api/v1/projects/[projectId]/versions/[versionUuid]/documents/handlers/_shared' | ||
import { Factory } from 'hono/factory' | ||
import { z } from 'zod' | ||
|
||
const factory = new Factory() | ||
|
||
const schema = z.object({ | ||
path: z.string(), | ||
messages: messagesSchema, | ||
response: z.string().optional(), | ||
}) | ||
|
||
export const postHandler = factory.createHandlers( | ||
zValidator('json', schema), | ||
async (c) => { | ||
const { projectId, versionUuid } = c.req.param() | ||
const { path, messages, response } = c.req.valid('json') | ||
const workspace = c.get('workspace') | ||
const { document, commit } = await getData({ | ||
workspace, | ||
projectId: Number(projectId!), | ||
commitUuid: versionUuid!, | ||
documentPath: path!, | ||
}).then((r) => r.unwrap()) | ||
|
||
const documentLog = await createDocumentLog({ | ||
data: { | ||
uuid: generateUUIDIdentifier(), | ||
documentUuid: document.documentUuid, | ||
resolvedContent: document.content, | ||
source: LogSources.API, | ||
parameters: {}, | ||
providerLog: { | ||
messages, | ||
responseText: | ||
response ?? | ||
(messages[messages.length - 1].content?.text || | ||
messages[messages.length - 1].content), | ||
}, | ||
}, | ||
commit, | ||
}).then((r) => r.unwrap()) | ||
|
||
return c.json(documentLog) | ||
}, | ||
) |
10 changes: 10 additions & 0 deletions
10
apps/gateway/src/routes/api/v2/projects/[projectId]/versions/[versionUuid]/logs/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import ROUTES from '$/common/routes' | ||
import { Hono } from 'hono' | ||
|
||
import { postHandler } from './handlers/post' | ||
|
||
const router = new Hono() | ||
|
||
router.post(ROUTES.Api.V2.Logs.Base, ...postHandler) | ||
|
||
export { router as documentsRouter } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
'use server' | ||
|
||
import { | ||
DELIMITERS_KEYS, | ||
MAX_SIZE, | ||
MAX_UPLOAD_SIZE_IN_MB, | ||
} from '@latitude-data/core/browser' | ||
import { CommitsRepository } from '@latitude-data/core/repositories' | ||
import { bulkUploadDocumentLogs } from '@latitude-data/core/services/documentLogs/bulkUpload' | ||
import { z } from 'zod' | ||
|
||
import { withDocument } from '../procedures' | ||
|
||
export const uploadDocumentLogsAction = withDocument | ||
.createServerAction() | ||
.input( | ||
z.object({ | ||
csvDelimiter: z.enum(DELIMITERS_KEYS, { | ||
message: 'Choose a valid delimiter option', | ||
}), | ||
logsFile: z | ||
.instanceof(File) | ||
.refine((file) => { | ||
return !file || file.size <= MAX_UPLOAD_SIZE_IN_MB | ||
}, `Your file must be less than ${MAX_SIZE}MB in size`) | ||
.refine( | ||
(file) => file.type === 'text/csv', | ||
'Your file must be a CSV file', | ||
), | ||
}), | ||
) | ||
.handler(async ({ ctx, input }) => { | ||
const commitsScope = new CommitsRepository(ctx.workspace.id) | ||
const commit = await commitsScope | ||
.getCommitByUuid({ | ||
projectId: ctx.project.id, | ||
uuid: ctx.currentCommitUuid, | ||
}) | ||
.then((r) => r.unwrap()) | ||
|
||
await bulkUploadDocumentLogs({ | ||
workspace: ctx.workspace, | ||
document: ctx.document, | ||
commit, | ||
csvDelimiter: input.csvDelimiter, | ||
logsFile: input.logsFile, | ||
}) | ||
|
||
return { success: true } | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.