Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
isahers1 committed Dec 12, 2024
1 parent 1d7de25 commit a41799b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
42 changes: 33 additions & 9 deletions js/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import {
UpdateExamplesResponse,
RawExample,
AttachmentInfo,
AttachmentDescription,
AttachmentData,
} from "./schemas.js";
import {
convertLangChainMessageToExample,
Expand Down Expand Up @@ -1128,9 +1130,17 @@ export class Client implements LangSmithTracingClientInterface {
const attachments = allAttachments[payload.id];
if (attachments) {
delete allAttachments[payload.id];
for (const [name, [contentType, content]] of Object.entries(
attachments
)) {
for (const [name, attachment] of Object.entries(attachments)) {
let contentType: string;
let content: AttachmentData;

if (Array.isArray(attachment)) {
[contentType, content] = attachment;
} else {
contentType = attachment.mimeType;
content = attachment.data;
}

// Validate that the attachment name doesn't contain a '.'
if (name.includes(".")) {
console.warn(
Expand Down Expand Up @@ -3947,9 +3957,16 @@ export class Client implements LangSmithTracingClientInterface {

// Add attachments if present
if (example.attachments) {
for (const [name, [mimeType, data]] of Object.entries(
example.attachments
)) {
for (const [name, attachment] of Object.entries(example.attachments)) {
let mimeType: string;
let data: AttachmentData;

if (Array.isArray(attachment)) {
[mimeType, data] = attachment;
} else {
mimeType = attachment.mimeType;
data = attachment.data;
}
const attachmentBlob = new Blob([data], {
type: `${mimeType}; length=${data.byteLength}`,
});
Expand Down Expand Up @@ -4038,9 +4055,16 @@ export class Client implements LangSmithTracingClientInterface {

// Add attachments if present
if (example.attachments) {
for (const [name, [mimeType, data]] of Object.entries(
example.attachments
)) {
for (const [name, attachment] of Object.entries(example.attachments)) {
let mimeType: string;
let data: AttachmentData;

if (Array.isArray(attachment)) {
[mimeType, data] = attachment;
} else {
mimeType = attachment.mimeType;
data = attachment.data;
}
const attachmentBlob = new Blob([data], {
type: `${mimeType}; length=${data.byteLength}`,
});
Expand Down
10 changes: 9 additions & 1 deletion js/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,15 @@ export interface AttachmentInfo {
}

export type AttachmentData = Uint8Array | ArrayBuffer;
export type Attachments = Record<string, [string, AttachmentData]>;

export type AttachmentDescription = {
mimeType: string;
data: AttachmentData;
};
export type Attachments = Record<
string,
[string, AttachmentData] | AttachmentDescription
>;

/**
* A run can represent either a trace (root run)
Expand Down
5 changes: 4 additions & 1 deletion js/src/tests/client.int.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,10 @@ test("upload examples multipart", async () => {
inputs: { text: "hello world" },
// check that passing no outputs works fine
attachments: {
test_file: ["image/png", fs.readFileSync(pathname)],
test_file: {
mimeType: "image/png",
data: fs.readFileSync(pathname),
},
},
};

Expand Down

0 comments on commit a41799b

Please sign in to comment.