Skip to content

Commit

Permalink
connectors: slack bot use file API for attachments (#9015)
Browse files Browse the repository at this point in the history
* connectors: slack bot use file API for attachments

* move to text/plain

* front

* simplify File

* not supported folder

* fix sdk/js headers

* fix

* useCaseMetadata when the conversation exists
  • Loading branch information
spolu authored Nov 29, 2024
1 parent bd980c0 commit bd97432
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 33 deletions.
55 changes: 39 additions & 16 deletions connectors/src/connectors/slack/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,23 +339,10 @@ async function answerMessage(
userType: slackUserInfo.is_bot ? "bot" : "user",
});

const buildContentFragmentRes = await makeContentFragment(
slackClient,
slackChannel,
slackThreadTs || slackMessageTs,
lastSlackChatBotMessage?.messageTs || slackThreadTs || slackMessageTs,
connector
);

if (!DUST_FRONT_API) {
throw new Error("DUST_FRONT_API environment variable is not defined");
}

if (slackUserInfo.is_bot) {
const botName = slackUserInfo.real_name;
requestedGroups = await slackConfig.getBotGroupIds(botName);
}

const userEmailHeader =
slackChatBotMessage.slackEmail !== "unknown"
? slackChatBotMessage.slackEmail
Expand All @@ -376,6 +363,21 @@ async function answerMessage(
}
);

const buildContentFragmentRes = await makeContentFragment(
slackClient,
dustAPI,
slackChannel,
slackThreadTs || slackMessageTs,
lastSlackChatBotMessage?.messageTs || slackThreadTs || slackMessageTs,
connector,
lastSlackChatBotMessage?.conversationId || null
);

if (slackUserInfo.is_bot) {
const botName = slackUserInfo.real_name;
requestedGroups = await slackConfig.getBotGroupIds(botName);
}

const agentConfigurationsRes = await dustAPI.getAgentConfigurations();
if (agentConfigurationsRes.isErr()) {
return new Err(new Error(agentConfigurationsRes.error.message));
Expand Down Expand Up @@ -690,10 +692,12 @@ export async function getBotEnabled(

async function makeContentFragment(
slackClient: WebClient,
dustAPI: DustAPI,
channelId: string,
threadTs: string,
startingAtTs: string | null,
connector: ConnectorResource
connector: ConnectorResource,
conversationId: string | null
): Promise<Result<PublicPostContentFragmentRequestBody | null, Error>> {
let allMessages: MessageElement[] = [];

Expand Down Expand Up @@ -770,11 +774,30 @@ async function makeContentFragment(
// Prepend $url to the content to make it available to the model.
const section = `$url: ${url}\n${sectionFullText(content)}`;

const contentType = "dust-application/slack";
const fileName = `slack_thread-${channel.channel.name}.txt`;

const fileRes = await dustAPI.uploadFile({
contentType,
fileName,
fileSize: section.length,
useCase: "conversation",
useCaseMetadata: conversationId
? {
conversationId,
}
: undefined,
fileObject: new File([section], fileName, { type: contentType }),
});

if (fileRes.isErr()) {
return new Err(new Error(fileRes.error.message));
}

return new Ok({
title: `Thread content from #${channel.channel.name}`,
content: section,
url: url,
contentType: "dust-application/slack",
fileId: fileRes.value.id,
context: null,
});
}
7 changes: 7 additions & 0 deletions front/lib/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,13 @@ const processingPerContentType: ProcessingPerContentType = {
avatar: notSupportedError,
tool_output: notSupportedError,
},
"dust-application/slack": {
conversation: storeRawText,
folder_document: notSupportedError,
folder_table: notSupportedError,
avatar: notSupportedError,
tool_output: notSupportedError,
},
};

const maybeApplyProcessing: ProcessingFunction = async (
Expand Down
7 changes: 7 additions & 0 deletions front/lib/api/files/upsert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ const processingPerContentType: ProcessingPerContentType = {
avatar: notSupportedError,
tool_output: notSupportedError,
},
"dust-application/slack": {
conversation: upsertDocumentToDatasource,
folder_document: notSupportedError,
folder_table: notSupportedError,
avatar: notSupportedError,
tool_output: notSupportedError,
},
};

const maybeApplyProcessing: ProcessingFunction = async ({
Expand Down
37 changes: 21 additions & 16 deletions sdks/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,20 +171,8 @@ export class DustAPI {
return new Ok(r.value.user);
}

async request(args: RequestArgsType) {
// Conveniently clean path from any leading "/" just in case
args.path = args.path.replace(/^\/+/, "");

let url = `${this.apiUrl()}/api/v1/w/${
args.overrideWorkspaceId ?? this.workspaceId()
}/${args.path}`;

if (args.query) {
url += `?${args.query.toString()}`;
}

async baseHeaders() {
const headers: RequestInit["headers"] = {
"Content-Type": "application/json",
Authorization: `Bearer ${await this.getApiKey()}`,
};
if (this._credentials.groupIds) {
Expand All @@ -196,6 +184,23 @@ export class DustAPI {
if (this._credentials.extraHeaders) {
Object.assign(headers, this._credentials.extraHeaders);
}
return headers;
}

async request(args: RequestArgsType) {
// Conveniently clean path from any leading "/" just in case
args.path = args.path.replace(/^\/+/, "");

let url = `${this.apiUrl()}/api/v1/w/${
args.overrideWorkspaceId ?? this.workspaceId()
}/${args.path}`;

if (args.query) {
url += `?${args.query.toString()}`;
}

const headers = await this.baseHeaders();
headers["Content-Type"] = "application/json";

const res = await this._fetchWithError(url, {
method: args.method,
Expand Down Expand Up @@ -801,6 +806,7 @@ export class DustAPI {
fileName,
fileSize,
useCase,
useCaseMetadata,
fileObject,
}: FileUploadUrlRequestType & { fileObject: File }) {
FileUploadUrlRequestSchema;
Expand All @@ -812,6 +818,7 @@ export class DustAPI {
fileName,
fileSize,
useCase,
useCaseMetadata,
},
});

Expand All @@ -834,9 +841,7 @@ export class DustAPI {
try {
uploadResult = await fetch(file.uploadUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${await this.getApiKey()}`,
},
headers: await this.baseHeaders(),
body: formData,
});
} catch (err) {
Expand Down
1 change: 0 additions & 1 deletion types/src/front/content_fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export type ContentFragmentContextType = {

export const supportedContentFragmentType = [
...supportedUploadableContentType,
"dust-application/slack",
] as const;

export type SupportedContentFragmentType =
Expand Down
1 change: 1 addition & 0 deletions types/src/front/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const supportedPlainText = {
"application/pdf": [".pdf"],
"text/markdown": [".md", ".markdown"],
"text/plain": [".txt"],
"dust-application/slack": [".txt"],
} as const;

// Supported content types for images.
Expand Down

0 comments on commit bd97432

Please sign in to comment.