Skip to content

Commit 813b752

Browse files
committed
remove old CodebuffMessage
1 parent a183f67 commit 813b752

36 files changed

+110
-377
lines changed

backend/src/__tests__/request-files-prompt.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import * as OriginalRequestFilesPromptModule from '../find-files/request-files-p
1212
import * as geminiWithFallbacksModule from '../llm-apis/gemini-with-fallbacks'
1313

1414
import type { CostMode } from '@codebuff/common/constants'
15-
import type { CodebuffMessage } from '@codebuff/common/types/message'
15+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
1616
import type { ProjectFileContext } from '@codebuff/common/util/file'
1717
import type { Mock } from 'bun:test'
1818

backend/src/__tests__/spawn-agents-message-history.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { TEST_USER_ID } from '@codebuff/common/constants'
2+
import { getInitialSessionState } from '@codebuff/common/types/session-state'
13
import {
24
describe,
35
expect,
@@ -7,16 +9,15 @@ import {
79
mock,
810
spyOn,
911
} from 'bun:test'
10-
import { handleSpawnAgents } from '../tools/handlers/tool/spawn-agents'
11-
import { TEST_USER_ID } from '@codebuff/common/constants'
12-
import { getInitialSessionState } from '@codebuff/common/types/session-state'
12+
1313
import { mockFileContext, MockWebSocket } from './test-utils'
14-
import * as loggerModule from '../util/logger'
1514
import * as runAgentStep from '../run-agent-step'
15+
import { handleSpawnAgents } from '../tools/handlers/tool/spawn-agents'
16+
import * as loggerModule from '../util/logger'
1617

17-
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
1818
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
19-
import type { CodebuffMessage } from '@codebuff/common/types/message'
19+
import type { AgentTemplate } from '@codebuff/common/types/agent-template'
20+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
2021
import type { WebSocket } from 'ws'
2122

2223
describe('Spawn Agents Message History', () => {

backend/src/admin/relabelRuns.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import type {
2727
GetRelevantFilesTrace,
2828
Relabel,
2929
} from '@codebuff/bigquery'
30-
import type { Message } from '@codebuff/common/types/message'
30+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
3131
import type { Request, Response } from 'express'
3232

3333
// --- GET Handler Logic ---
@@ -172,7 +172,7 @@ export async function relabelForUserHandler(req: Request, res: Response) {
172172

173173
output = await promptAiSdk({
174174
messages: transformMessages(
175-
messages as Message[],
175+
messages as CodebuffMessage[],
176176
system as System,
177177
),
178178
model: model,
@@ -397,7 +397,10 @@ export async function relabelWithClaudeWithFullFileContext(
397397
}
398398

399399
const output = await promptAiSdk({
400-
messages: transformMessages(trace.payload.messages as Message[], system),
400+
messages: transformMessages(
401+
trace.payload.messages as CodebuffMessage[],
402+
system,
403+
),
401404
model: model as any, // Model type is string here for flexibility
402405
clientSessionId: 'relabel-trace-api',
403406
fingerprintId: 'relabel-trace-api',

backend/src/fast-rewrite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { promptRelaceAI } from './llm-apis/relace-api'
1111
import { promptAiSdk } from './llm-apis/vercel-ai-sdk/ai-sdk'
1212
import { logger } from './util/logger'
1313

14-
import type { CodebuffMessage } from '@codebuff/common/types/message'
14+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
1515

1616
export async function fastRewrite(
1717
initialContent: string,

backend/src/find-files/request-files-prompt.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import type {
2929
GetExpandedFileContextForTrainingTrace,
3030
GetRelevantFilesTrace,
3131
} from '@codebuff/bigquery'
32-
import type { CodebuffMessage } from '@codebuff/common/types/message'
32+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
3333
import type { ProjectFileContext } from '@codebuff/common/util/file'
3434

3535
const NUMBER_OF_EXAMPLE_FILES = 100

backend/src/get-file-reading-updates.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { countTokensJson } from './util/token-counter'
1414
import { requestFiles } from './websockets/websocket-action'
1515

16-
import type { CodebuffMessage } from '@codebuff/common/types/message'
16+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
1717
import type { ProjectFileContext } from '@codebuff/common/util/file'
1818
import type { WebSocket } from 'ws'
1919

backend/src/llm-apis/gemini-with-fallbacks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import type {
88
FinetunedVertexModel,
99
GeminiModel,
1010
} from '@codebuff/common/constants'
11-
import type { CodebuffMessage } from '@codebuff/common/types/message'
11+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
1212

1313
/**
1414
* Prompts a Gemini model with fallback logic.

backend/src/llm-apis/message-cost-tracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { SWITCHBOARD } from '../websockets/server'
2222
import { sendAction } from '../websockets/websocket-action'
2323

2424
import type { ClientState } from '../websockets/switchboard'
25-
import type { CodebuffMessage } from '@codebuff/common/types/message'
25+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
2626

2727
export const PROFIT_MARGIN = 0.055
2828

backend/src/llm-apis/vercel-ai-sdk/ai-sdk.ts

Lines changed: 13 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ import type {
2929
Model,
3030
OpenAIModel,
3131
} from '@codebuff/common/constants'
32-
import type { CodebuffMessage, Message } from '@codebuff/common/types/message'
32+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
3333
import type {
3434
OpenRouterProviderOptions,
3535
OpenRouterUsageAccounting,
3636
} from '@openrouter/ai-sdk-provider'
37-
import type { AssistantModelMessage, UserModelMessage, LanguageModel } from 'ai'
37+
import type { LanguageModel } from 'ai'
3838
import type { z } from 'zod/v4'
3939

4040
// TODO: We'll want to add all our models here!
@@ -370,7 +370,7 @@ export const promptAiSdkStructured = async function <T>(options: {
370370
// TODO: temporary - ideally we move to using CodebuffMessage[] directly
371371
// and don't need this transform!!
372372
export function transformMessages(
373-
messages: (Message | CodebuffMessage)[],
373+
messages: CodebuffMessage[],
374374
system?: System,
375375
): CodebuffMessage[] {
376376
const codebuffMessages: CodebuffMessage[] = []
@@ -385,115 +385,14 @@ export function transformMessages(
385385
})
386386
}
387387

388-
for (const message of messages) {
389-
if (message.role === 'system') {
390-
if (typeof message.content === 'string') {
391-
codebuffMessages.push({ role: 'system', content: message.content })
392-
continue
393-
} else {
394-
throw new Error(
395-
'Multiple part system message - unsupported (TODO: fix if we hit this.)',
396-
)
397-
}
398-
}
399-
400-
if (message.role === 'user') {
401-
if (typeof message.content === 'string') {
402-
codebuffMessages.push({
403-
...message,
404-
role: 'user',
405-
content: message.content,
406-
})
407-
continue
408-
} else {
409-
const parts: UserModelMessage['content'] = []
410-
const modelMessage: UserModelMessage = { role: 'user', content: parts }
411-
for (const part of message.content) {
412-
// Add ephemeral if present
413-
if ('cache_control' in part) {
414-
modelMessage.providerOptions = {
415-
anthropic: { cacheControl: { type: 'ephemeral' } },
416-
openrouter: { cacheControl: { type: 'ephemeral' } },
417-
}
418-
}
419-
// Handle Message type image format
420-
if (part.type === 'image' && 'source' in part) {
421-
parts.push({
422-
type: 'image' as const,
423-
image: `data:${part.source.media_type};base64,${part.source.data}`,
424-
})
425-
continue
426-
}
427-
if (part.type === 'file') {
428-
throw new Error('File messages not supported')
429-
}
430-
if (part.type === 'text') {
431-
parts.push({
432-
type: 'text' as const,
433-
text: part.text,
434-
})
435-
continue
436-
}
437-
if (part.type === 'tool_use' || part.type === 'tool_result') {
438-
// Skip tool parts in user messages - they should be in assistant/tool messages
439-
continue
440-
}
441-
}
442-
codebuffMessages.push(modelMessage)
443-
continue
444-
}
445-
}
446-
447-
if (message.role === 'assistant') {
448-
if (message.content === undefined || message.content === null) {
449-
continue
450-
}
451-
if (typeof message.content === 'string') {
452-
codebuffMessages.push({
453-
...message,
454-
role: 'assistant',
455-
content: message.content,
456-
})
457-
continue
458-
} else {
459-
let messageContent: AssistantModelMessage['content'] = []
460-
const modelMessage: AssistantModelMessage = {
461-
...message,
462-
role: 'assistant',
463-
content: messageContent,
464-
}
465-
for (const part of message.content) {
466-
// Add ephemeral if present
467-
if ('cache_control' in part) {
468-
modelMessage.providerOptions = {
469-
anthropic: { cacheControl: { type: 'ephemeral' } },
470-
openrouter: { cacheControl: { type: 'ephemeral' } },
471-
}
472-
}
473-
if (part.type === 'text') {
474-
messageContent.push({ type: 'text', text: part.text })
475-
}
476-
if (part.type === 'tool_use') {
477-
messageContent.push({
478-
type: 'tool-call',
479-
toolCallId: part.id,
480-
toolName: part.name,
481-
input: part.input,
482-
})
483-
}
484-
}
485-
codebuffMessages.push(modelMessage)
486-
continue
487-
}
488-
}
489-
490-
if (message.role === 'tool') {
491-
codebuffMessages.push(message)
492-
continue
493-
}
494-
495-
throw new Error('Unknown message role received: ' + message)
496-
}
497-
498-
return codebuffMessages
388+
return buildArray<CodebuffMessage>([
389+
system && {
390+
role: 'system',
391+
content:
392+
typeof system === 'string'
393+
? system
394+
: system.map((block) => block.text).join('\n\n'),
395+
},
396+
messages,
397+
])
499398
}

backend/src/process-file-block.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { promptAiSdk } from './llm-apis/vercel-ai-sdk/ai-sdk'
1212
import { logger } from './util/logger'
1313
import { countTokens } from './util/token-counter'
1414

15-
import type { CodebuffMessage } from '@codebuff/common/types/message'
15+
import type { CodebuffMessage } from '@codebuff/common/types/messages/codebuff-message'
1616

1717
export async function processFileBlock(
1818
path: string,

0 commit comments

Comments
 (0)