Skip to content

Commit

Permalink
Do not use compiler enums in core. This breaks things (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutgon authored Nov 6, 2024
1 parent 3ccd959 commit 88639e9
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
4 changes: 1 addition & 3 deletions packages/core/src/services/ai/providers/rules/anthropic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { MessageRole } from '@latitude-data/compiler'

import { AppliedRules, ProviderRules } from '.'
import { enforceAllSystemMessagesFirst } from './helpers/enforceAllSystemMessagesFirst'

Expand All @@ -11,7 +9,7 @@ export function applyAnthropicRules(appliedRule: AppliedRules): AppliedRules {
})

const roles = rule.messages.map((m) => m.role)
const onlySystemMessages = roles.every((r) => r === MessageRole.system)
const onlySystemMessages = roles.every((r) => r === 'system')
if (!onlySystemMessages) return rule

const rules = [
Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/services/ai/providers/rules/google.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, MessageRole } from '@latitude-data/compiler'
import type { Message } from '@latitude-data/compiler'
import { beforeAll, describe, expect, it } from 'vitest'

import { applyCustomRules, ProviderRules } from '.'
Expand Down Expand Up @@ -59,22 +59,22 @@ describe('applyGoogleRules', () => {
expect(appliedMessages[1]).toEqual(messages[1])
expect(appliedMessages[2]).toEqual(messages[2])
expect(appliedMessages[4]).toEqual({
role: MessageRole.assistant,
role: 'assistant',
content: [{ type: 'text', text: messages[4]!.content }],
})

expect(appliedMessages[3]).toEqual({
role: MessageRole.user,
role: 'user',
content: [{ type: 'text', text: messages[3]!.content }],
})

expect(appliedMessages[3]).toEqual({
role: MessageRole.user,
role: 'user',
content: [{ type: 'text', text: messages[3]!.content }],
})

expect(appliedMessages[5]).toEqual({
role: MessageRole.user,
role: 'user',
content: [
{ type: 'text', text: 'Use a short response' },
{ type: 'text', text: 'Second a short response' },
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ContentType, MessageRole } from '@latitude-data/compiler'
import type { ContentType, Message, MessageRole } from '@latitude-data/compiler'

import { AppliedRules, ProviderRules } from '../index'

const system = MessageRole.system
const SYSTEM_ROLE = 'system'

export function enforceAllSystemMessagesFirst(
appliedRule: AppliedRules,
Expand All @@ -13,7 +13,7 @@ export function enforceAllSystemMessagesFirst(
): AppliedRules {
const messages = appliedRule.messages
const firstNonSystemMessageIndex = messages.findIndex(
(m) => m.role !== system,
(m) => m.role !== SYSTEM_ROLE,
)

if (firstNonSystemMessageIndex === -1) {
Expand All @@ -23,7 +23,7 @@ export function enforceAllSystemMessagesFirst(
const messagesAfterFirstNonSystemMessage = messages.slice(
firstNonSystemMessageIndex,
)
if (!messagesAfterFirstNonSystemMessage.some((m) => m.role === system)) {
if (!messagesAfterFirstNonSystemMessage.some((m) => m.role === SYSTEM_ROLE)) {
return appliedRule
}

Expand All @@ -39,15 +39,15 @@ export function enforceAllSystemMessagesFirst(
rules,
messages: messages.map((m, i) => {
if (i < firstNonSystemMessageIndex) return m
if (m.role !== system) return m
if (m.role !== SYSTEM_ROLE) return m

return {
...m,
role: MessageRole.user,
role: 'user' as MessageRole,
content: Array.isArray(m.content)
? m.content
: [{ type: ContentType.text, text: m.content }],
}
: [{ type: 'text' as ContentType, text: m.content }],
} as Message
}),
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message, MessageRole } from '@latitude-data/compiler'
import type { Message } from '@latitude-data/compiler'

import { Providers } from '../../models'

Expand Down Expand Up @@ -121,7 +121,7 @@ export function extractMessageMetadata({

if (Object.keys(rest).length === 0) return common

if (role === MessageRole.user && Object.hasOwnProperty.call(rest, 'name')) {
if (role === 'user' && Object.hasOwnProperty.call(rest, 'name')) {
// @ts-ignore
const name = rest.name
common = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Message } from '@latitude-data/compiler'
import type { Message } from '@latitude-data/compiler'
import { describe, expect, it } from 'vitest'

import { PartialConfig } from '../../helpers'
Expand Down
7 changes: 3 additions & 4 deletions packages/core/src/services/ai/providers/rules/vercel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
import type {
ContentType,
Message,
MessageRole,
SystemMessage,
TextContent,
} from '@latitude-data/compiler'
Expand Down Expand Up @@ -72,7 +71,7 @@ function groupContentMetadata({
const key = getProviderMetadataKey(provider)

if (typeof content === 'string') {
const baseMsg = { type: ContentType.text, text: content }
const baseMsg = { type: 'text' as ContentType, text: content }
if (!messageMetadata) return [baseMsg]

return [
Expand Down Expand Up @@ -108,7 +107,7 @@ export function vercelSdkRules(
provider: Providers,
): AppliedRules {
const messages = rules.messages.flatMap((message) => {
if (message.role === MessageRole.system) {
if (message.role === 'system') {
return flattenSystemMessage({ message, provider })
}

Expand Down

0 comments on commit 88639e9

Please sign in to comment.