-
Notifications
You must be signed in to change notification settings - Fork 233
chore(compass-e2e-tests): add assistant end to end tests COMPASS-9748 #7429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
gagik
wants to merge
15
commits into
main
Choose a base branch
from
gagik/e2e
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+828
−2
Open
Changes from 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a656e36
chore(compass-e2e-tests): add assistant end to end tests COMPASS-9384
gagik 4d1cddc
chore: add entry points
gagik bd6fc2f
chore: fixup
gagik dccb354
chore: cleanup and fix
gagik 1010161
chore: remove redundant comments
gagik 5f57459
chore: changes from feedback
gagik b0084fc
chore: fixups
gagik 0297c45
chore: fixups
gagik b995d47
chore: waituntil
gagik a9d07ed
chore: add some extra check
gagik a086d07
chore: add expected result option to sendMessage
gagik c81c712
chore: remove stop
gagik 65dd452
chore: fixup
gagik 2adb17f
chore: use set feature to support web
gagik 167f968
chore: avoid get on web
gagik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
248 changes: 248 additions & 0 deletions
248
packages/compass-e2e-tests/helpers/assistant-service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
import http from 'http'; | ||
import { once } from 'events'; | ||
import type { AddressInfo } from 'net'; | ||
|
||
export type MockAssistantResponse = { | ||
status: number; | ||
body: string; | ||
}; | ||
|
||
function sendStreamingResponse(res: http.ServerResponse, content: string) { | ||
// OpenAI Responses API streaming response format using Server-Sent Events | ||
res.writeHead(200, { | ||
'Content-Type': 'text/event-stream; charset=utf-8', | ||
'Cache-Control': 'no-cache', | ||
Connection: 'keep-alive', | ||
'Transfer-Encoding': 'chunked', | ||
}); | ||
|
||
const responseId = `resp_${Date.now()}`; | ||
const itemId = `item_${Date.now()}`; | ||
let sequenceNumber = 0; | ||
|
||
// Send response.created event | ||
res.write( | ||
`data: ${JSON.stringify({ | ||
type: 'response.created', | ||
response: { | ||
id: responseId, | ||
object: 'realtime.response', | ||
status: 'in_progress', | ||
output: [], | ||
usage: { | ||
input_tokens: 0, | ||
output_tokens: 0, | ||
total_tokens: 0, | ||
}, | ||
}, | ||
sequence_number: sequenceNumber++, | ||
})}\n\n` | ||
); | ||
|
||
// Send output_item.added event | ||
res.write( | ||
`data: ${JSON.stringify({ | ||
type: 'response.output_item.added', | ||
response_id: responseId, | ||
output_index: 0, | ||
item: { | ||
id: itemId, | ||
object: 'realtime.item', | ||
type: 'message', | ||
role: 'assistant', | ||
content: [], | ||
}, | ||
sequence_number: sequenceNumber++, | ||
})}\n\n` | ||
); | ||
|
||
// Send the content in chunks | ||
const words = content.split(' '); | ||
let index = 0; | ||
|
||
const sendChunk = () => { | ||
if (index < words.length) { | ||
const word = words[index] + (index < words.length - 1 ? ' ' : ''); | ||
// Send output_text.delta event | ||
res.write( | ||
`data: ${JSON.stringify({ | ||
type: 'response.output_text.delta', | ||
response_id: responseId, | ||
item_id: itemId, | ||
output_index: 0, | ||
delta: word, | ||
sequence_number: sequenceNumber++, | ||
})}\n\n` | ||
); | ||
index++; | ||
setTimeout(sendChunk, 10); | ||
} else { | ||
// Send output_item.done event | ||
res.write( | ||
`data: ${JSON.stringify({ | ||
type: 'response.output_item.done', | ||
response_id: responseId, | ||
output_index: 0, | ||
item: { | ||
id: itemId, | ||
object: 'realtime.item', | ||
type: 'message', | ||
role: 'assistant', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: content, | ||
}, | ||
], | ||
}, | ||
sequence_number: sequenceNumber++, | ||
})}\n\n` | ||
); | ||
|
||
// Send response.completed event | ||
const tokenCount = Math.ceil(content.split(' ').length * 1.3); | ||
res.write( | ||
`data: ${JSON.stringify({ | ||
type: 'response.completed', | ||
response: { | ||
id: responseId, | ||
object: 'realtime.response', | ||
status: 'completed', | ||
output: [ | ||
{ | ||
id: itemId, | ||
object: 'realtime.item', | ||
type: 'message', | ||
role: 'assistant', | ||
content: [ | ||
{ | ||
type: 'text', | ||
text: content, | ||
}, | ||
], | ||
}, | ||
], | ||
usage: { | ||
input_tokens: 10, | ||
output_tokens: tokenCount, | ||
total_tokens: 10 + tokenCount, | ||
}, | ||
}, | ||
sequence_number: sequenceNumber++, | ||
})}\n\n` | ||
); | ||
|
||
res.write('data: [DONE]\n\n'); | ||
res.end(); | ||
} | ||
}; | ||
|
||
sendChunk(); | ||
} | ||
|
||
export async function startMockAssistantServer( | ||
gagik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
response: _response, | ||
}: { | ||
response: MockAssistantResponse; | ||
} = { | ||
response: { | ||
status: 200, | ||
body: 'This is a test response from the AI assistant.', | ||
}, | ||
} | ||
): Promise<{ | ||
clearRequests: () => void; | ||
getResponse: () => MockAssistantResponse; | ||
setResponse: (response: MockAssistantResponse) => void; | ||
getRequests: () => { | ||
content: any; | ||
req: any; | ||
}[]; | ||
endpoint: string; | ||
server: http.Server; | ||
stop: () => Promise<void>; | ||
}> { | ||
let requests: { | ||
content: any; | ||
req: any; | ||
}[] = []; | ||
let response = _response; | ||
const server = http | ||
.createServer((req, res) => { | ||
// Only handle POST requests for chat completions | ||
if (req.method !== 'POST') { | ||
res.writeHead(404); | ||
return res.end('Not Found'); | ||
} | ||
|
||
let body = ''; | ||
req | ||
.setEncoding('utf8') | ||
.on('data', (chunk) => { | ||
body += chunk; | ||
}) | ||
.on('end', () => { | ||
let jsonObject; | ||
try { | ||
jsonObject = JSON.parse(body); | ||
} catch { | ||
res.writeHead(400); | ||
res.setHeader('Content-Type', 'application/json'); | ||
return res.end(JSON.stringify({ error: 'Invalid JSON' })); | ||
} | ||
|
||
requests.push({ | ||
req, | ||
content: jsonObject, | ||
}); | ||
|
||
if (response.status !== 200) { | ||
res.writeHead(response.status); | ||
res.setHeader('Content-Type', 'application/json'); | ||
return res.end(JSON.stringify({ error: response.body })); | ||
} | ||
|
||
// Send streaming response | ||
return sendStreamingResponse(res, response.body); | ||
}); | ||
}) | ||
.listen(0); | ||
await once(server, 'listening'); | ||
|
||
// address() returns either a string or AddressInfo. | ||
const address = server.address() as AddressInfo; | ||
|
||
const endpoint = `http://localhost:${address.port}`; | ||
|
||
async function stop() { | ||
server.close(); | ||
await once(server, 'close'); | ||
} | ||
|
||
function clearRequests() { | ||
requests = []; | ||
} | ||
|
||
function getRequests() { | ||
return requests; | ||
} | ||
|
||
function getResponse() { | ||
return response; | ||
} | ||
|
||
function setResponse(newResponse: MockAssistantResponse) { | ||
response = newResponse; | ||
} | ||
|
||
return { | ||
clearRequests, | ||
getRequests, | ||
endpoint, | ||
server, | ||
getResponse, | ||
setResponse, | ||
stop, | ||
}; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.