1- import { type Message } from '@latitude-data/constants/legacyCompiler'
2-
31import { NotFoundError } from '@latitude-data/constants/errors'
42import { LatitudePromptConfig } from '@latitude-data/constants/latitudePromptSchema'
5- import { buildConversation } from '../../../helpers '
3+ import { type Message } from '@latitude-data/constants/legacyCompiler '
64import { LogSources } from '../../../constants'
7- import { type Workspace } from '../../../schema/models/types/Workspace'
85import { unsafelyFindProviderApiKey } from '../../../data-access/providerApiKeys'
6+ import { publisher } from '../../../events/publisher'
7+ import { buildConversation } from '../../../helpers'
98import { Result } from '../../../lib/Result'
109import { ToolHandler } from '../../../lib/streamManager/clientTools/handlers'
1110import { DefaultStreamManager } from '../../../lib/streamManager/defaultStreamManager'
@@ -15,9 +14,11 @@ import {
1514 DocumentVersionsRepository ,
1615 ProviderLogsRepository ,
1716} from '../../../repositories'
17+ import { type Workspace } from '../../../schema/models/types/Workspace'
1818import { BACKGROUND , telemetry , TelemetryContext } from '../../../telemetry'
1919import { getInputSchema , getOutputType } from '../../chains/ChainValidator'
2020import { scanDocumentContent } from '../../documents'
21+ import { isErrorRetryable } from '../../evaluationsV2/run'
2122import serializeProviderLog from '../../providerLogs/serialize'
2223
2324type AddMessagesArgs = {
@@ -48,7 +49,8 @@ export async function addMessages({
4849 documentLogUuid,
4950 } )
5051 if ( dataResult . error ) return dataResult
51- const { document, commit, providerLog, globalConfig } = dataResult . unwrap ( )
52+ const { document, commit, documentLog, providerLog, globalConfig } =
53+ dataResult . unwrap ( )
5254
5355 const $prompt = telemetry . prompt ( context , {
5456 documentLogUuid,
@@ -105,9 +107,24 @@ export async function addMessages({
105107
106108 start ( )
107109
108- streamResult . error . then ( ( error ) =>
109- error ? $prompt . fail ( error ) : $prompt . end ( ) ,
110- )
110+ streamResult . response . then ( async ( response ) => {
111+ const error = await streamResult . error
112+ if ( error ) {
113+ $prompt . fail ( error )
114+
115+ if ( isErrorRetryable ( error ) ) return response
116+ } else {
117+ $prompt . end ( )
118+ }
119+
120+ await publisher . publishLater ( {
121+ type : 'documentLogInteracted' ,
122+ data : {
123+ id : documentLog . id ,
124+ workspaceId : workspace . id ,
125+ } ,
126+ } )
127+ } )
111128
112129 return Result . ok ( streamResult )
113130}
@@ -150,5 +167,5 @@ async function retrieveData({
150167 if ( metadataResult . error ) return metadataResult
151168 const globalConfig = metadataResult . value . config as LatitudePromptConfig
152169
153- return Result . ok ( { commit, document, providerLog, globalConfig } )
170+ return Result . ok ( { commit, document, documentLog , providerLog, globalConfig } )
154171}
0 commit comments