Skip to content

Commit

Permalink
Adds support for run attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Oct 20, 2024
1 parent 30bed06 commit 6552a3e
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,14 +1013,22 @@ export class Client {
return;
}
// transform and convert to dicts
let preparedCreateParams =
runCreates?.map((create) =>
this.prepareRunCreateOrUpdateInputs(create)
) ?? [];
let preparedUpdateParams =
runUpdates?.map((update) =>
this.prepareRunCreateOrUpdateInputs(update)
) ?? [];
const allAttachments: Record<
string,
Record<string, [string, Uint8Array]>
> = {};
let preparedCreateParams = [];
for (const create of runCreates ?? []) {
preparedCreateParams.push(this.prepareRunCreateOrUpdateInputs(create));
if (create.id !== undefined && create.attachments !== undefined) {
allAttachments[create.id] = create.attachments;
}
delete create.attachments;
}
let preparedUpdateParams = [];
for (const update of runUpdates ?? []) {
preparedUpdateParams.push(this.prepareRunCreateOrUpdateInputs(update));
}

// require trace_id and dotted_order
const invalidRunCreate = preparedCreateParams.find((runCreate) => {
Expand Down Expand Up @@ -1107,7 +1115,23 @@ export class Client {
}),
});
}

// encode the attachments
if (payload.id !== undefined) {
const attachments = allAttachments[payload.id];
if (attachments) {
delete allAttachments[payload.id];
for (const [name, [contentType, content]] of Object.entries(
attachments
)) {
accumulatedParts.push({
name: `attachment.${payload.id}.${name}`,
payload: new Blob([content], {
type: `${contentType}; length=${content.length}`,
}),
});
}
}
}
// compute context
accumulatedContext.push(`trace=${payload.trace_id},id=${payload.id}`);
}
Expand Down Expand Up @@ -1139,6 +1163,7 @@ export class Client {
);
} catch (e) {
let errorMessage = "Failed to multipart ingest runs";
// eslint-disable-next-line no-instanceof/no-instanceof
if (e instanceof Error) {
errorMessage += `: ${e.stack || e.message}`;
} else {
Expand Down

0 comments on commit 6552a3e

Please sign in to comment.