Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions apps/gateway/src/common/documents/getData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,7 @@ export async function publishDocumentRunRequestedEvent({
const user = await findFirstUserInWorkspace(workspace)

const commitsScope = new CommitsRepository(workspace.id)
const headCommit = await commitsScope
.getHeadCommit(project.id)
.then((r) => r.unwrap())
const headCommit = await commitsScope.getHeadCommit(project.id)

if (user) {
publisher.publishLater({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { BadRequestError } from '@latitude-data/constants/errors'
import { AppRouteHandler } from '$/openApi/types'
import { getVersionRoute } from './getCommit.route'

// @ts-expect-error: broken types
// @ts-expect-error: Types are not working as expected
export const getVersionHandler: AppRouteHandler<
typeof getVersionRoute
> = async (c: Context) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ async function resolveWorkspaceAndCommit(
return Result.ok({ workspace, commit })
}

const headCommitResult = await commitsRepo.getHeadCommit(rawTrigger.projectId)
const commit = headCommitResult.unwrap()
const commit = await commitsRepo.getHeadCommit(rawTrigger.projectId)
if (!commit) {
return Result.error(
new NotFoundError(
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/actions/sdk/runDocumentAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export async function runDocumentAction({
const { workspace, user } = await getCurrentUserOrRedirect()

const commitsScope = new CommitsRepository(workspace.id)
const headCommit = await commitsScope
.getHeadCommit(projectId)
.then((r) => r.unwrap())
const headCommit = await commitsScope.getHeadCommit(projectId)

publisher.publishLater({
type: 'documentRunRequested',
Expand Down
3 changes: 1 addition & 2 deletions apps/web/src/app/(private)/_data-access/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ export const getHeadCommitCached = cache(
projectId: number
}) => {
const commitsScope = new CommitsRepository(workspace.id)
const headCommitResult = await commitsScope.getHeadCommit(projectId)
return headCommitResult.value
return await commitsScope.getHeadCommit(projectId)
},
)

Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/app/api/documents/[documentUuid]/run/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export const POST = errorHandler(
).then((r) => r.unwrap())

const commitsScope = new CommitsRepository(workspace.id)
const headCommit = await commitsScope
.getHeadCommit(projectId)
.then((r) => r.unwrap())
const headCommit = await commitsScope.getHeadCommit(projectId)

// Publish document run event
publisher.publishLater({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/lib/analytics/AnalyticsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class AnalyticsClient {

const repo = new WorkspacesRepository(user.id)

// TODO: remove, it's very expensive to have a read operation on each analytics event
// TODO: remove, it's very expensive to have a read operation on each analytics event
const result = await repo.find(this.workspaceId)

if (result.error) return undefined
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/repositories/commitsRepository/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ export class CommitsRepository extends RepositoryLegacy<
return Result.error(new NotFoundError('Project ID is required'))
}

const headCommit = await this.getHeadCommit(projectId).then((r) =>
r.unwrap(),
)
if (!headCommit) {
const headCommit = await this.getHeadCommit(projectId)
if (!headCommit)
return Result.error(new NotFoundError('Head commit not found'))
}

return Result.ok(headCommit)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { and, desc, eq, isNotNull } from 'drizzle-orm'

import { database } from '../../../client'
import { InferedReturnType } from '../../../lib/commonTypes'
import { Result } from '../../../lib/Result'
import { buildCommitsScope } from './buildCommitsScope'
import { Commit } from '../../../browser'

export async function getHeadCommitForProject(
{
Expand All @@ -27,5 +27,5 @@ export async function getHeadCommitForProject(
.orderBy(desc(commitsScope.mergedAt))
.limit(1)

return Result.ok(result[0])
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not return result objects in read methods

return result[0] as Commit | undefined
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can clearly be undefined yet types always asserted commit existed, for some reason

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just don't do the as no? It will give drizzle schema or undefined

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason it doesn't, not sure what is happening here

}
6 changes: 1 addition & 5 deletions packages/core/src/services/actions/cloneAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ async function getSampleAgent(
}

const commitsRepository = new CommitsRepository(workspace.id, db)
const gettingco = await commitsRepository.getHeadCommit(project.id)
if (gettingco.error) {
return Result.error(gettingco.error)
}
const commit = gettingco.unwrap()
const commit = await commitsRepository.getHeadCommit(project.id)
if (!commit) {
return Result.error(
new UnprocessableEntityError('Sample Agents commit not found'),
Expand Down
4 changes: 1 addition & 3 deletions packages/core/src/services/copilot/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ export async function getCopilot({ path }: { path: string }, db = database) {
}

const commitsRepository = new CommitsRepository(workspace.id, db)
const commit = await commitsRepository
.getHeadCommit(env.COPILOT_PROJECT_ID)
.then((r) => r.unwrap())
const commit = await commitsRepository.getHeadCommit(env.COPILOT_PROJECT_ID)
if (!commit) {
return Result.error(new Error('Copilot commit not found'))
}
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/services/copilot/latte/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,7 @@ async function getCommitResult({
})
}

const headCommitResult = await commitScope.getHeadCommit(projectId)
if (!Result.isOk(headCommitResult)) return headCommitResult
const headCommit = headCommitResult.unwrap()

const headCommit = await commitScope.getHeadCommit(projectId)
if (!headCommit) {
return Result.error(
new NotFoundError('Live commit not found in Latte project'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ const validateDocumentReadyForCreatingTrigger = async ({
promptUuid: string
}): PromisedResult<boolean> => {
const commitsScope = new CommitsRepository(workspaceId)
const headCommit = await commitsScope
.getHeadCommit(projectId)
.then((r) => r.unwrap())

const headCommit = await commitsScope.getHeadCommit(projectId)
if (
headCommit !== undefined &&
(versionUuid === headCommit.uuid || versionUuid === HEAD_COMMIT)
Expand All @@ -111,11 +108,10 @@ const validateDocumentReadyForCreatingTrigger = async ({
.then((r) => r.unwrap())

const document = documents.find((doc) => doc.documentUuid === promptUuid)

if (!document) {
return Result.error(
new NotFoundError(
`Document with UUID ${promptUuid} not found in commit ${headCommit.uuid}.`,
`Document with UUID ${promptUuid} not found in commit ${commit?.uuid ?? headCommit?.uuid}.`,
),
)
}
Expand Down
7 changes: 2 additions & 5 deletions packages/core/src/services/documentTriggers/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,8 @@ async function getLiveDocumentTrigger<T extends DocumentTriggerType>(
): PromisedResult<DocumentTrigger<T> | undefined> {
return transaction.call(async (tx) => {
const commitsScope = new CommitsRepository(workspace.id, tx)
const liveCommitResult = await commitsScope.getHeadCommit(projectId)
if (!Result.isOk(liveCommitResult) || !liveCommitResult.value) {
return Result.ok(undefined)
}
const liveCommit = liveCommitResult.unwrap()
const liveCommit = await commitsScope.getHeadCommit(projectId)
if (!liveCommit) return Result.ok(undefined)

const triggersScope = new DocumentTriggersRepository(workspace.id, tx)
const liveTriggerResult = await triggersScope.getTriggerByUuid<T>({
Expand Down
5 changes: 1 addition & 4 deletions packages/core/src/services/documentTriggers/enable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export async function setDocumentTriggerEnabled<T extends DocumentTriggerType>(
): PromisedResult<DocumentTrigger<T>> {
return await transaction.call(async (tx) => {
const commitsScope = new CommitsRepository(workspace.id, tx)
const liveCommitResult = await commitsScope.getHeadCommit(commit.projectId)
if (!Result.isOk(liveCommitResult)) return liveCommitResult
const liveCommit = liveCommitResult.unwrap()

const liveCommit = await commitsScope.getHeadCommit(commit.projectId)
if (commit.uuid !== liveCommit?.uuid) {
return Result.error(
new BadRequestError(
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/services/documentTriggers/handleMerge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ export async function handleTriggerMerge(
const triggersScope = new DocumentTriggersRepository(workspace.id, tx)
const commitsScope = new CommitsRepository(workspace.id, tx)

const liveCommitResult = await commitsScope.getHeadCommit(draft.projectId)
if (!Result.isOk(liveCommitResult)) return liveCommitResult

const liveCommit = liveCommitResult.unwrap()

const liveCommit = await commitsScope.getHeadCommit(draft.projectId)
const triggerUpdatesResult =
await triggersScope.getTriggerUpdatesInDraft(draft)
if (!Result.isOk(triggerUpdatesResult)) return triggerUpdatesResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,7 @@ export async function registerEmailTriggerEvent(
const uploadedFiles = uploadResult.unwrap()

const commitsScope = new CommitsRepository(workspace.id, db)
const headCommitResult = await commitsScope.getHeadCommit(project.id)
if (headCommitResult.error) return Result.nil()
const headCommit = headCommitResult.unwrap()
const headCommit = await commitsScope.getHeadCommit(project.id)

// TODO: Search the Live and all draft versions for each trigger, not just the Live one
if (!headCommit) return Result.nil()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@ async function registerSingleScheduledTriggerEvent(
// However, the trigger's associated commit may not be the Live commit,
// but rather just the last merged commit where it has been updated.
// Thus, we need to find the Live commit
const liveCommitResult = await commitsScope.getHeadCommit(
const liveCommit = await commitsScope.getHeadCommit(
documentTrigger.projectId,
)
if (!Result.isOk(liveCommitResult)) return liveCommitResult
const liveCommit = liveCommitResult.unwrap()
eventCommit = liveCommit!
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,9 @@ export async function getCommitFromTrigger(
}

// Commit is merged. This means that the latest commit active for this trigger is Live
const liveCommitResult = await commitsScope.getHeadCommit(
const liveCommit = await commitsScope.getHeadCommit(
documentTrigger.projectId,
)
if (!Result.isOk(liveCommitResult)) return liveCommitResult
const liveCommit = liveCommitResult.unwrap()

if (!liveCommit) {
return Result.error(
new NotFoundError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function getDocumentsAtCommit(
const headCommit = await getHeadCommitForProject(
{ projectId: projectResult.value.id, commitsScope },
tx,
).then((r) => r.unwrap())
)

const docsScope = new DocumentVersionsRepository(workspaceId, tx)
const headDocumentsResult = await docsScope.getDocumentsAtCommit(headCommit)
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/services/documents/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '../../repositories'
import { recomputeChanges } from './recomputeChanges'
import { updateDocument } from './update'
import { NotFoundError } from '@latitude-data/constants/errors'

describe('updateDocument', () => {
it('modifies a document that was created in a previous commit', async (ctx) => {
Expand Down Expand Up @@ -295,9 +296,8 @@ describe('updateDocument', () => {
})
const commitsScope = new CommitsRepository(project.workspaceId)

const commit = await commitsScope
.getHeadCommit(project.id)
.then((r) => r.unwrap())
const commit = await commitsScope.getHeadCommit(project.id)
if (!commit) throw new NotFoundError('Head commit not found')
const fooDoc = documents.find((d) => d.path === 'foo')!

const result = await updateDocument({
Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/services/history/resetDocumentToVersion.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NotFoundError } from '@latitude-data/constants/errors'
import { Commit, DraftChange, Project, User, Workspace } from '../../browser'
import { Result } from '../../lib/Result'
import { PromisedResult } from '../../lib/Transaction'
Expand Down Expand Up @@ -33,14 +34,14 @@ async function fetchDocumentVersionDetails({
.then((r) => r.value)

const commitScope = new CommitsRepository(workspace.id)
const headCommit = await commitScope
.getHeadCommit(project.id)
.then((r) => r.unwrap()!)
const headCommit = await commitScope.getHeadCommit(project.id)
if (!headCommit)
return Result.error(new NotFoundError('Head commit not found'))

const targetCommit = targetDraftUuid
? await commitScope
.getCommitByUuid({ uuid: targetDraftUuid, projectId: project.id })
.then((r) => r.unwrap())
.getCommitByUuid({ uuid: targetDraftUuid, projectId: project.id })
.then((r) => r.unwrap())
: headCommit

const targetDocument = await docsScope
Expand Down Expand Up @@ -166,13 +167,13 @@ export async function resetDocumentToVersion({
const targetDraftResult = targetDraftUuid
? Result.ok(targetCommit)
: await createCommit({
project: project,
user: user,
data: {
title: `Reset "${oldDocumentPath}"`,
description: `Reset document "${oldDocumentPath}" to version "${targetCommit.title}"`,
},
})
project: project,
user: user,
data: {
title: `Reset "${oldDocumentPath}"`,
description: `Reset document "${oldDocumentPath}" to version "${targetCommit.title}"`,
},
})

const targetDraft = targetDraftResult.unwrap()

Expand Down
25 changes: 13 additions & 12 deletions packages/core/src/services/history/resetProjectToCommit.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { NotFoundError } from '@latitude-data/constants/errors'
import { Commit, DraftChange, Project, User, Workspace } from '../../browser'
import { database } from '../../client'
import { Result } from '../../lib/Result'
Expand All @@ -24,14 +25,14 @@ async function fetchCommitDetails({
try {
const commitScope = new CommitsRepository(workspace.id)

const headCommit = await commitScope
.getHeadCommit(project.id)
.then((r) => r.unwrap()!)
const headCommit = await commitScope.getHeadCommit(project.id)
if (!headCommit)
return Result.error(new NotFoundError('Head commit not found'))

const targetCommit = targetDraftUuid
? await commitScope
.getCommitByUuid({ uuid: targetDraftUuid, projectId: project.id })
.then((r) => r.unwrap())
.getCommitByUuid({ uuid: targetDraftUuid, projectId: project.id })
.then((r) => r.unwrap())
: headCommit

const originalCommit = await commitScope
Expand Down Expand Up @@ -150,13 +151,13 @@ export async function resetProjectToCommit(
const targetDraft = targetDraftUuid
? Result.ok(targetCommit)
: await createCommit({
project: project,
user: user,
data: {
title: `Reset project to v${originalCommit.version} "${originalCommit.title}"`,
description: `Resetted the project to the state of commit v${originalCommit.version} "${originalCommit.title}"`,
},
})
project: project,
user: user,
data: {
title: `Reset project to v${originalCommit.version} "${originalCommit.title}"`,
description: `Resetted the project to the state of commit v${originalCommit.version} "${originalCommit.title}"`,
},
})

if (targetDraft.error) {
return Result.error(targetDraft.error)
Expand Down
Loading