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
@@ -145,6 +148,33 @@ async function* pages(input: SourcePageInput): AsyncIterable<CopilotChatRow[]> {
145148 }
146149}
147150
151+ /** Tool payloads can contain private notes even when their mutation failed or targeted a shared note. */
152+ function projectExportMessage ( message : unknown ) : unknown {
153+ if ( ! isRecordLike ( message ) || ! Array . isArray ( message . contentBlocks ) ) return message
154+ return {
155+ ...message ,
156+ contentBlocks : message . contentBlocks . map ( ( block : unknown ) => {
157+ if ( ! isRecordLike ( block ) || ! isRecordLike ( block . toolCall ) ) return block
158+ if ( block . toolCall . name !== 'remember' ) {
159+ if ( ! isRecordLike ( block . toolCall . result ) || ! ( 'output' in block . toolCall . result ) )
160+ return block
161+ const parsed = persistedContentBlockSchema . safeParse ( block )
162+ if ( ! parsed . success ) return { ...block , toolCall : omit ( block . toolCall , [ 'result' ] ) }
163+ const projected = stripToolResultOutput ( { contentBlocks : [ parsed . data ] } ) . contentBlocks [ 0 ]
164+ return { ...block , toolCall : { ...block . toolCall , result : projected . toolCall ?. result } }
165+ }
166+ return {
167+ type : 'tool' ,
168+ toolCall : {
169+ name : 'remember' ,
170+ ...( typeof block . toolCall . id === 'string' ? { id : block . toolCall . id } : { } ) ,
171+ ...( typeof block . toolCall . state === 'string' ? { state : block . toolCall . state } : { } ) ,
172+ } ,
173+ }
174+ } ) ,
175+ }
176+ }
177+
148178export const copilotChatsSource : DrainSource < CopilotChatRow > = {
149179 type : 'copilot_chats' ,
150180 displayName : 'Chats' ,
@@ -157,7 +187,7 @@ export const copilotChatsSource: DrainSource<CopilotChatRow> = {
157187 workspaceId : row . workspaceId ,
158188 type : row . type ,
159189 title : row . title ,
160- messages : row . messages ,
190+ messages : row . messages . map ( projectExportMessage ) ,
161191 model : row . model ,
162192 conversationId : row . conversationId ,
163193 previewYaml : row . previewYaml ,
0 commit comments