@@ -15,19 +15,15 @@ import {
1515import type { AgentRunContext , AiSdkContentPart } from '../agent-types' ;
1616import { getPrimaryModel , getSummarizerModel } from './model-config' ;
1717
18- async function hydrateConversationHistoryBlobParts (
18+ export async function hydrateConversationHistoryBlobParts (
1919 messages : MessageSelect [ ] ,
2020 hydrate : HydrateBlobToDataUrl
2121) : Promise < { hydrated : MessageSelect [ ] ; nonHydrated : MessageSelect [ ] } > {
22- const hydrated : MessageSelect [ ] = [ ] ;
23- const nonHydrated : MessageSelect [ ] = [ ] ;
24-
25- await Promise . all (
26- messages . map ( async ( msg ) => {
22+ const hydratedResults = await Promise . all (
23+ messages . map ( async ( msg ) : Promise < { message : MessageSelect ; hydrated : boolean } > => {
2724 const content = msg . content ;
2825 if ( ! content ?. parts ?. length ) {
29- nonHydrated . push ( msg ) ;
30- return ;
26+ return { message : msg , hydrated : false } ;
3127 }
3228
3329 let hasHydratedParts = false ;
@@ -48,14 +44,17 @@ async function hydrateConversationHistoryBlobParts(
4844 ) ;
4945
5046 const processedMsg = { ...msg , content : { ...content , parts } } ;
51- if ( hasHydratedParts ) {
52- hydrated . push ( processedMsg ) ;
53- } else {
54- nonHydrated . push ( processedMsg ) ;
55- }
47+ return { message : processedMsg , hydrated : hasHydratedParts } ;
5648 } )
5749 ) ;
5850
51+ const hydrated = hydratedResults
52+ . filter ( ( result ) => result . hydrated )
53+ . map ( ( result ) => result . message ) ;
54+ const nonHydrated = hydratedResults
55+ . filter ( ( result ) => ! result . hydrated )
56+ . map ( ( result ) => result . message ) ;
57+
5958 return { hydrated, nonHydrated } ;
6059}
6160
@@ -132,11 +131,22 @@ export async function buildConversationHistory({
132131 conversationHistory = nonHydrated ;
133132 }
134133
135- console . log ( 'conversationHistoryWithFileData' , conversationHistoryWithFileData ) ;
136-
137134 const conversationHistoryString = formatMessagesAsConversationHistory ( conversationHistory ) ;
138135
139- const conversationHistoryTokens = estimateTokens ( conversationHistoryString ) ;
136+ const hydratedHistoryWithFileData = conversationHistoryWithFileData
137+ . flatMap ( ( msg ) => msg . content ?. parts ?? [ ] )
138+ . map ( ( part ) => {
139+ if ( part . kind === 'text' && typeof part . text === 'string' ) {
140+ return part . text ;
141+ }
142+ if ( part . kind === 'file' && typeof part . data === 'string' ) {
143+ return part . data ;
144+ }
145+ return '' ;
146+ } )
147+ . join ( '\n' ) ;
148+ const conversationHistoryTokens =
149+ estimateTokens ( conversationHistoryString ) + estimateTokens ( hydratedHistoryWithFileData ) ;
140150 const updatedContextBreakdown : ContextBreakdown = {
141151 components : {
142152 ...initialContextBreakdown . components ,
@@ -176,18 +186,20 @@ export function buildInitialMessages({
176186
177187 if ( conversationHistoryWithFileData ?. length ) {
178188 conversationHistoryWithFileData . forEach ( ( msg ) => {
179- const content = ( msg . content ?. parts ?? [ ] ) . flatMap ( ( part ) => {
180- if ( part . kind !== 'file' || typeof part . data !== 'string' ) {
181- return [ ] ;
189+ const content = ( msg . content ?. parts ?? [ ] ) . reduce < AiSdkContentPart [ ] > ( ( acc , part ) => {
190+ if ( part . kind === 'text' && typeof part . text === 'string' ) {
191+ acc . push ( { type : 'text' , text : part . text } ) ;
192+ return acc ;
182193 }
183194
184- return [
185- {
186- type : 'image' as const ,
187- image : part . data ,
188- } ,
189- ] ;
190- } ) ;
195+ if ( part . kind === 'file' && typeof part . data === 'string' ) {
196+ // Temporarily hard code to image. Must update when we add other file types.
197+ acc . push ( { type : 'image' , image : part . data } ) ;
198+ return acc ;
199+ }
200+
201+ return acc ;
202+ } , [ ] ) ;
191203
192204 if ( content ?. length && content . length > 0 ) {
193205 messages . push ( { role : 'user' , content } ) ;
0 commit comments