Skip to content

Commit

Permalink
feat(constants): move core/constants to constants package (#726)
Browse files Browse the repository at this point in the history
We otherwise have circular dependencies between core and other packages
that make it hard to develop and test.
  • Loading branch information
geclos authored Dec 16, 2024
1 parent 12c3134 commit 6104b6e
Show file tree
Hide file tree
Showing 21 changed files with 380 additions and 336 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import { omit } from 'lodash-es'

import { Message } from '@latitude-data/compiler'
import {
ChainCallResponseDto,
ChainEventDto,
ChainEventTypes,
Commit,
DocumentVersion,
LatitudeEventData,
Project,
StreamEventTypes,
type ChainEvent,
type Workspace,
} from '@latitude-data/core/browser'
import { findFirstUserInWorkspace } from '@latitude-data/core/data-access'
Expand All @@ -23,6 +17,14 @@ import {
ProjectsRepository,
} from '@latitude-data/core/repositories'
import { Config } from '@latitude-data/core/services/ai/helpers'
import {
ChainCallResponseDto,
ChainEvent,
ChainEventDto,
ChainEventTypes,
LatitudeEventData,
StreamEventTypes,
} from '@latitude-data/constants'

export const getData = async ({
workspace,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { LatitudeError } from '@latitude-data/core/lib/errors'
import { Result, TypedResult } from '@latitude-data/core/lib/Result'
import { captureException } from '$/common/sentry'
import {
ChainStepObjectResponse,
ChainStepTextResponse,
RunSyncAPIResponse,
} from '@latitude-data/core/browser'
import { LatitudeError } from '@latitude-data/core/lib/errors'
import { Result, TypedResult } from '@latitude-data/core/lib/Result'
import { captureException } from '$/common/sentry'
} from '@latitude-data/constants'

type DocumentResponse = ChainStepObjectResponse | ChainStepTextResponse
export function documentRunPresenter(
Expand Down
9 changes: 8 additions & 1 deletion packages/constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@
"tc": "tsc --noEmit"
},
"exports": {
".": "./src/index.ts",
"./errors": "./src/errors.ts"
},
"devDependencies": {
"@latitude-data/eslint-config": "workspace:*",
"@latitude-data/typescript-config": "workspace:*"
"@latitude-data/typescript-config": "workspace:*",
"@types/json-schema": "^7.0.15"
},
"dependencies": {
"@latitude-data/compiler": "workspace:^",
"json-schema": "^0.4.0",
"zod": "^3.23.8"
}
}
186 changes: 186 additions & 0 deletions packages/constants/src/ai.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
import {
CoreTool,
LanguageModelUsage,
ObjectStreamPart,
TextStreamPart,
} from 'ai'
import { JSONSchema7 } from 'json-schema'
import { ToolCall, Message } from '@latitude-data/compiler'
import { z } from 'zod'

import { ProviderLog } from './models'

type GoogleConfig = z.infer<typeof googleConfig>

const googleCategorySettings = z.union([
z.literal('HARM_CATEGORY_HATE_SPEECH'),
z.literal('HARM_CATEGORY_DANGEROUS_CONTENT'),
z.literal('HARM_CATEGORY_HARASSMENT'),
z.literal('HARM_CATEGORY_SEXUALLY_EXPLICIT'),
])
const googleThresholdSettings = z.union([
z.literal('HARM_BLOCK_THRESHOLD_UNSPECIFIED'),
z.literal('BLOCK_LOW_AND_ABOVE'),
z.literal('BLOCK_MEDIUM_AND_ABOVE'),
z.literal('BLOCK_ONLY_HIGH'),
z.literal('BLOCK_NONE'),
])

export const googleConfig = z
.object({
structuredOutputs: z.boolean().optional(),
cachedContent: z.string().optional(),
safetySettings: z
.array(
z
.object({
category: googleCategorySettings.optional(),
threshold: googleThresholdSettings.optional(),
})
.optional(),
)
.optional(),
})
.optional()

export type PartialConfig = Omit<Config, 'provider'>

export type Config = {
provider: string
model: string
url?: string
cacheControl?: boolean
schema?: JSONSchema7
azure?: { resourceName: string }
google?: GoogleConfig
tools?: Record<
string,
{ description?: string; parameters: Record<string, any> }
>
}

export enum ChainEventTypes {
Error = 'chain-error',
Step = 'chain-step',
Complete = 'chain-complete',
StepComplete = 'chain-step-complete',
}

export type ProviderData =
| TextStreamPart<Record<string, CoreTool>>
| ObjectStreamPart<Record<string, CoreTool>>
| ObjectStreamPart<unknown>

export type ChainEventDto =
| ProviderData
| {
type: ChainEventTypes.Step
config: Config
isLastStep: boolean
messages: Message[]
uuid?: string
}
| {
type: ChainEventTypes.StepComplete
response: ChainEventDtoResponse
uuid?: string
}
| {
type: ChainEventTypes.Complete
config: Config
messages?: Message[]
object?: any
response: ChainEventDtoResponse
uuid?: string
}
| {
type: ChainEventTypes.Error
error: {
name: string
message: string
stack?: string
}
}

export type ChainCallResponseDto =
| Omit<ChainStepResponse<'object'>, 'documentLogUuid' | 'providerLog'>
| Omit<ChainStepResponse<'text'>, 'documentLogUuid' | 'providerLog'>

type ChainEventDtoResponse =
| Omit<ChainStepResponse<'object'>, 'providerLog'>
| Omit<ChainStepResponse<'text'>, 'providerLog'>

export type StreamType = 'object' | 'text'
type BaseResponse = {
text: string
usage: LanguageModelUsage
documentLogUuid?: string
providerLog?: ProviderLog
}

export type ChainStepTextResponse = BaseResponse & {
streamType: 'text'
toolCalls: ToolCall[]
}

export type ChainStepObjectResponse = BaseResponse & {
streamType: 'object'
object: any
}

export type ChainStepResponse<T extends StreamType> = T extends 'text'
? ChainStepTextResponse
: T extends 'object'
? ChainStepObjectResponse
: never

export enum StreamEventTypes {
Latitude = 'latitude-event',
Provider = 'provider-event',
}

export type ChainEvent =
| {
data: LatitudeEventData
event: StreamEventTypes.Latitude
}
| {
data: ProviderData
event: StreamEventTypes.Provider
}

export type LatitudeEventData =
| {
type: ChainEventTypes.Step
config: Config
isLastStep: boolean
messages: Message[]
documentLogUuid?: string
}
| {
type: ChainEventTypes.StepComplete
response: ChainStepResponse<StreamType>
documentLogUuid?: string
}
| {
type: ChainEventTypes.Complete
config: Config
messages?: Message[]
object?: any
response: ChainStepResponse<StreamType>
documentLogUuid?: string
}
| {
type: ChainEventTypes.Error
error: Error
}

// FIXME: Move to @latitude-data/constants
export type RunSyncAPIResponse = {
uuid: string
conversation: Message[]
response: ChainCallResponseDto
}

// FIXME: Move to @latitude-data/constants
export type ChatSyncAPIResponse = RunSyncAPIResponse
20 changes: 20 additions & 0 deletions packages/constants/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export enum LogSources {
API = 'api',
Playground = 'playground',
Evaluation = 'evaluation',
User = 'user',
SharedPrompt = 'shared_prompt',
}

export enum Providers {
OpenAI = 'openai',
Anthropic = 'anthropic',
Groq = 'groq',
Mistral = 'mistral',
Azure = 'azure',
Google = 'google',
Custom = 'custom',
}

export * from './models'
export * from './ai'
79 changes: 79 additions & 0 deletions packages/constants/src/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { LogSources } from '.'
import { Message, ToolCall } from '@latitude-data/compiler'
import { PartialConfig } from './ai'

export type DocumentLog = {
id: number
uuid: string
documentUuid: string
commitId: number
resolvedContent: string
contentHash: string
parameters: Record<string, unknown>
customIdentifier: string | null
duration: number | null
source: LogSources | null
createdAt: Date
updatedAt: Date
}

export enum EvaluationResultableType {
Boolean = 'evaluation_resultable_booleans',
Text = 'evaluation_resultable_texts',
Number = 'evaluation_resultable_numbers',
}

export type EvaluationResult = {
id: number
uuid: string
evaluationId: number
documentLogId: number
evaluatedProviderLogId: number | null
evaluationProviderLogId: number | null
resultableType: EvaluationResultableType | null
resultableId: number | null
source: LogSources | null
reason: string | null
createdAt: Date
updatedAt: Date
}

export type EvaluationResultDto = EvaluationResult & {
result: string | number | boolean | undefined
}

export type ProviderLog = {
id: number
uuid: string
documentLogUuid: string | null
providerId: number | null
model: string | null
finishReason: string | null
config: PartialConfig | null
messages: Message[]
responseObject: unknown | null
responseText: string | null
toolCalls: ToolCall[]
tokens: number | null
costInMillicents: number | null
duration: number | null
source: LogSources | null
apiKeyId: number | null
generatedAt: Date | null
updatedAt: Date
}

export type DocumentVersion = {
id: number
documentUuid: string
path: string
content: string
resolvedContent: string
contentHash: string
promptlVersion: number
commitId: number
datasetId: number | null
deletedAt: Date | null
createdAt: Date
updatedAt: Date
}
Loading

0 comments on commit 6104b6e

Please sign in to comment.