11import { copilotChats , copilotMessages } from '@sim/db/schema'
2+ import { isRecordLike , omit } from '@sim/utils/object'
23import { and , asc , eq , inArray , isNull , or , sql } from 'drizzle-orm'
4+ import { persistedContentBlockSchema } from '@/lib/api/contracts/copilot-messages'
35import { assertKnownSizeWithinLimit } from '@/lib/core/utils/stream-limits'
46import { DATA_DRAIN_LIMITS } from '@/lib/data-drains/limits'
57import {
@@ -11,6 +13,7 @@ import {
1113} from '@/lib/data-drains/sources/cursor'
1214import { readBoundedSourcePage , workspaceInOrganization } from '@/lib/data-drains/sources/helpers'
1315import type { Cursor , DrainSource , SourcePageInput } from '@/lib/data-drains/types'
16+ import { stripToolResultOutput } from '@/lib/mothership/chat/persisted-message'
1417
1518/**
1619 * The transcript no longer lives on `copilot_chats.messages` — it is assembled
@@ -141,6 +144,33 @@ async function* pages(input: SourcePageInput): AsyncIterable<CopilotChatRow[]> {
141144 }
142145}
143146
147+ /** Tool payloads can contain private notes even when their mutation failed or targeted a shared note. */
148+ function projectExportMessage ( message : unknown ) : unknown {
149+ if ( ! isRecordLike ( message ) || ! Array . isArray ( message . contentBlocks ) ) return message
150+ return {
151+ ...message ,
152+ contentBlocks : message . contentBlocks . map ( ( block : unknown ) => {
153+ if ( ! isRecordLike ( block ) || ! isRecordLike ( block . toolCall ) ) return block
154+ if ( block . toolCall . name !== 'remember' ) {
155+ if ( ! isRecordLike ( block . toolCall . result ) || ! ( 'output' in block . toolCall . result ) )
156+ return block
157+ const parsed = persistedContentBlockSchema . safeParse ( block )
158+ if ( ! parsed . success ) return { ...block , toolCall : omit ( block . toolCall , [ 'result' ] ) }
159+ const projected = stripToolResultOutput ( { contentBlocks : [ parsed . data ] } ) . contentBlocks [ 0 ]
160+ return { ...block , toolCall : { ...block . toolCall , result : projected . toolCall ?. result } }
161+ }
162+ return {
163+ type : 'tool' ,
164+ toolCall : {
165+ name : 'remember' ,
166+ ...( typeof block . toolCall . id === 'string' ? { id : block . toolCall . id } : { } ) ,
167+ ...( typeof block . toolCall . state === 'string' ? { state : block . toolCall . state } : { } ) ,
168+ } ,
169+ }
170+ } ) ,
171+ }
172+ }
173+
144174export const copilotChatsSource : DrainSource < CopilotChatRow > = {
145175 type : 'copilot_chats' ,
146176 displayName : 'Chats' ,
@@ -153,7 +183,7 @@ export const copilotChatsSource: DrainSource<CopilotChatRow> = {
153183 workspaceId : row . workspaceId ,
154184 type : row . type ,
155185 title : row . title ,
156- messages : row . messages ,
186+ messages : row . messages . map ( projectExportMessage ) ,
157187 model : row . model ,
158188 conversationId : row . conversationId ,
159189 previewYaml : row . previewYaml ,
0 commit comments