Skip to content

Commit

Permalink
feat: fixes message hook trigger and SSE parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Jun 17, 2024
1 parent 29f8665 commit 96ed6ac
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/answerSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,16 +151,16 @@ export class AnswerSession {

messageQueue.push(parsedMessage.message)

if (parsedMessage.endOfBlock) {
while (messageQueue.length > 0) {
lastMessage.content += messageQueue.shift()
// Process the message queue immediately, regardless of endOfBlock
while (messageQueue.length > 0) {
lastMessage.content += messageQueue.shift()

if (this.events?.onMessageChange) {
this.events.onMessageChange(this.messages)
}

yield lastMessage.content
if (this.events?.onMessageChange) {
console.log('onMessageChange is defined')
this.events.onMessageChange(this.messages)
}

yield lastMessage.content
}
} catch (e) {
console.error('Error parsing SSE event:', e)
Expand Down
18 changes: 18 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,20 @@
export * from './client.js'
export * from './proxy.js'

import { OramaClient } from './client.js'
import { parseSSE } from './utils.js'

const client = new OramaClient({
endpoint: 'https://cloud.orama.run/v1/indexes/nodejs-org-prod-vo71di',
api_key: '9A3alsqP5owMUvA4zeHdWrz93yxYZXra'
})

const answerSession = client.createAnswerSession({
events: {
onMessageChange: console.log
}
})

await answerSession.ask({
term: 'Release schedule'
})
16 changes: 7 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,12 @@ export function debounce(func: (...args: any[]) => any, delay: number) {
}
}

export function parseSSE(data: string): SSEPayload {
const lines = data.split('\n')
const obj: Record<string, string> = {}
for (const line of lines) {
const [key, value] = line.split(': ')
if (key) {
obj[key] = value
}
export function parseSSE(input: string): SSEPayload {
const [event, ...data] = input.split('\n')
const eventData = data.join('\n').replace('data: ', '')

return {
event: event.replace('event: ', ''),
data: eventData
}
return obj as SSEPayload
}
1 change: 1 addition & 0 deletions tests/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ await t.test('answer session', async t => {

await t.test('can create an answer session', async t => {
const session = client.createAnswerSession()

const answer = await session.ask({
term: 'german'
})
Expand Down

0 comments on commit 96ed6ac

Please sign in to comment.