Skip to content

Commit

Permalink
Merge pull request #44 from oramasearch/feat/complete-interaction-event
Browse files Browse the repository at this point in the history
Feat: introduces a new event to handle response completion
  • Loading branch information
matijagaspar authored Nov 21, 2024
2 parents e361da1 + c605227 commit 97b1959
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/client/src/answerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type AnswerParams<M extends boolean, UserContext = unknown> = {
onRelatedQueries?: (relatedQueries: string[]) => void
onNewInteractionStarted?: (interactionId: string) => void
onStateChange?: (state: Interaction[]) => void
onInteractionDone?: (interaction: Interaction) => void
}
systemPrompts?: string[]
}
Expand All @@ -38,6 +39,8 @@ export type Interaction<T = AnyDocument> = {
relatedQueries: Nullable<string[]>
sources: Nullable<Results<T>>
translatedQuery: Nullable<SearchParams<AnyOrama>>
segment: Nullable<string>
trigger: Nullable<string>
aborted: boolean
loading: boolean
error: boolean
Expand Down Expand Up @@ -166,6 +169,8 @@ export class AnswerSession<M extends boolean> {
relatedQueries: null,
sources: null,
translatedQuery: null,
segment: null,
trigger: null,
aborted: false,
loading: true,
error: false,
Expand Down Expand Up @@ -282,6 +287,21 @@ export class AnswerSession<M extends boolean> {
this.events.onStateChange(this.state)
}

// MANAGE INCOMING METADATA
} else if (parsedMessage.type === 'conversation-metadata') {
const { segment, trigger } = parsedMessage.message
if (segment) {
this.state[currentStateIndex].segment = segment
}

if (trigger) {
this.state[currentStateIndex].trigger = trigger
}

if (this.events?.onStateChange) {
this.events.onStateChange(this.state)
}

// MANAGE INCOMING RELATED QUERIES
} else if (parsedMessage.type === 'related-queries') {
this.state[currentStateIndex].relatedQueries = parsedMessage.message
Expand Down Expand Up @@ -344,6 +364,10 @@ export class AnswerSession<M extends boolean> {
this.events.onStateChange(this.state)
}

if (this.events?.onInteractionDone) {
this.events.onInteractionDone(this.state[currentStateIndex])
}

if (this.events?.onMessageLoading) {
this.events.onMessageLoading(false)
}
Expand Down
7 changes: 6 additions & 1 deletion packages/react-client/src/useAnswerSession.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnyDocument, AnyOrama, Nullable, Results, SearchParams } from '@orama/orama'
import type { AnswerSessionParams, Message } from '@oramacloud/client'
import type { AnswerSessionParams, Message, Interaction } from '@oramacloud/client'

import { AnswerSession, OramaClient } from '@oramacloud/client'
import { useCallback, useEffect, useRef, useState } from 'react'
Expand All @@ -14,6 +14,7 @@ export function useAnswerSession<Document = AnyDocument>(params: AnswerSessionHo
const [error, setError] = useState<Nullable<Error>>(null)
const [aborted, setAborted] = useState<boolean>(false)
const [sources, setSources] = useState<Nullable<Results<Document>>>(null)
const [interaction, setInteraction] = useState<Nullable<Interaction>>(null)
const [relatedQueries, setRelatedQueries] = useState<Nullable<string[]>>(null)
//no support for multi index answer session yet
const sessionRef = useRef<Nullable<AnswerSession<true>>>(null)
Expand Down Expand Up @@ -47,6 +48,9 @@ export function useAnswerSession<Document = AnyDocument>(params: AnswerSessionHo
},
onRelatedQueries: (relatedQueries) => {
setRelatedQueries(relatedQueries)
},
onInteractionDone: (interaction) => {
setInteraction(interaction)
}
}
})
Expand Down Expand Up @@ -83,6 +87,7 @@ export function useAnswerSession<Document = AnyDocument>(params: AnswerSessionHo
error,
sources,
relatedQueries,
interaction,
ask,
clearSession
}
Expand Down

0 comments on commit 97b1959

Please sign in to comment.