-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat(attachments): create context-aware helper for attachments #44
Merged
Dam-Buty
merged 6 commits into
main
from
damien/eng-1590-ts-sdk-inconsistencies-on-uploadfile
Jul 25, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,96 @@ | ||
import 'dotenv/config'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you so much for splitting tests in their own file! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you're welcome, i'm unable to focus on a 700 line test file anyway 😅 |
||
import { createReadStream, readFileSync } from 'fs'; | ||
|
||
import { Attachment, LiteralClient, Maybe } from '../src'; | ||
|
||
const url = process.env.LITERAL_API_URL; | ||
const apiKey = process.env.LITERAL_API_KEY; | ||
if (!url || !apiKey) { | ||
throw new Error('Missing environment variables'); | ||
} | ||
const client = new LiteralClient(apiKey, url); | ||
|
||
const filePath = './tests/chainlit-logo.png'; | ||
const mime = 'image/png'; | ||
|
||
function removeVariableParts(url: string) { | ||
return url.split('X-Amz-Date')[0].split('X-Goog-Date')[0]; | ||
} | ||
|
||
describe('Attachments', () => { | ||
describe('Uploading a file', () => { | ||
const stream = createReadStream(filePath); | ||
const buffer = readFileSync(filePath); | ||
const arrayBuffer = buffer.buffer; | ||
const blob = new Blob([buffer]); | ||
// We wrap the blob in a blob and simulate the structure of a File | ||
const file = new Blob([blob], { type: 'image/png' }); | ||
|
||
it.each([ | ||
{ type: 'Stream', content: stream! }, | ||
{ type: 'Buffer', content: buffer! }, | ||
{ type: 'ArrayBuffer', content: arrayBuffer! }, | ||
{ type: 'Blob', content: blob! }, | ||
{ type: 'File', content: file! } | ||
])('handles $type objects', async function ({ type, content }) { | ||
const attachment = await client.api.createAttachment({ | ||
content, | ||
mime, | ||
name: `Attachment ${type}`, | ||
metadata: { type } | ||
}); | ||
|
||
const step = await client | ||
.run({ | ||
name: `Test ${type}`, | ||
attachments: [attachment] | ||
}) | ||
.send(); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
const fetchedStep = await client.api.getStep(step.id!); | ||
|
||
const urlWithoutVariables = removeVariableParts(attachment.url!); | ||
const fetchedUrlWithoutVariables = removeVariableParts( | ||
fetchedStep?.attachments![0].url as string | ||
); | ||
|
||
expect(fetchedStep?.attachments?.length).toBe(1); | ||
expect(fetchedStep?.attachments![0].objectKey).toEqual( | ||
attachment.objectKey | ||
); | ||
expect(fetchedStep?.attachments![0].name).toEqual(attachment.name); | ||
expect(fetchedStep?.attachments![0].metadata).toEqual( | ||
attachment.metadata | ||
); | ||
expect(urlWithoutVariables).toEqual(fetchedUrlWithoutVariables); | ||
}); | ||
}); | ||
|
||
describe('Handling context', () => { | ||
it('attaches the attachment to the step in the context', async () => { | ||
const stream = createReadStream(filePath); | ||
|
||
let stepId: Maybe<string>; | ||
let attachment: Maybe<Attachment>; | ||
|
||
await client.run({ name: 'Attachment test ' }).wrap(async () => { | ||
stepId = client.getCurrentStep().id!; | ||
attachment = await client.api.createAttachment({ | ||
content: stream!, | ||
mime, | ||
name: 'Attachment', | ||
metadata: { type: 'Stream' } | ||
}); | ||
}); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
const fetchedStep = await client.api.getStep(stepId!); | ||
|
||
expect(fetchedStep?.attachments?.length).toBe(1); | ||
expect(fetchedStep?.attachments![0].id).toEqual(attachment!.id); | ||
}); | ||
}); | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know we could declare prototypes for doc/autocompletion purposes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just realized you mentioned "Fix typing to enforce either content or path at compile time" -> very clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes overloading function types is a great way to handle this kind of either/or parameters. It's a bit verbose but the end result is satisfying from the POV of the dev using the function.