Skip to content

Commit

Permalink
feat(dust): add support for fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSelene committed Mar 21, 2024
1 parent 362638b commit 12a6cfd
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/pieces/community/dust/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "@activepieces/piece-dust",
"version": "0.0.4"
}
"version": "0.1.0"
}
8 changes: 7 additions & 1 deletion packages/pieces/community/dust/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
import { createConversation } from './lib/actions/create-conversation';
import { replyToConversation } from './lib/actions/reply-to-conversation';
import { upsertDocument } from './lib/actions/upsert-document';
import { addFragmentToConversation } from './lib/actions/add-fragment-to-conversation';

export const dustAuth = PieceAuth.CustomAuth({
description: 'Dust authentication requires an API key.',
Expand Down Expand Up @@ -35,6 +36,11 @@ export const dust = createPiece({
minimumSupportedRelease: '0.9.0',
logoUrl: 'https://cdn.activepieces.com/pieces/dust.png',
authors: ['AdamSelene', 'abuaboud'],
actions: [createConversation, replyToConversation, upsertDocument],
actions: [
createConversation,
replyToConversation,
addFragmentToConversation,
upsertDocument,
],
triggers: [],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { dustAuth } from '../..';
import { DUST_BASE_URL, getConversationContent } from '../common';
import {
httpClient,
HttpMethod,
HttpRequest,
} from '@activepieces/pieces-common';

export const addFragmentToConversation = createAction({
// auth: check https://www.activepieces.com/docs/developers/piece-reference/authentication,
name: 'addFragmentToConversation',
displayName: 'Add fragment to conversation',
description:
'Create a new content fragment in a conversation. Content fragments are pieces of information that can be inserted in conversations and are passed as context to assistants to when they generate an answer.',
auth: dustAuth,
props: {
conversationId: Property.ShortText({
displayName: 'Conversation ID',
required: true,
}),
fragment: Property.File({ displayName: 'Fragment', required: true }),
fragmentName: Property.ShortText({
displayName: 'Fragment name',
required: false,
}),
},
async run({ auth, propsValue }) {
const request: HttpRequest = {
method: HttpMethod.POST,
url: `${DUST_BASE_URL}/${auth.workspaceId}/assistant/conversations/${propsValue.conversationId}/content_fragments`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${auth.apiKey}`,
},
body: JSON.stringify(
{
content: propsValue.fragment.data.toString('utf-8'),
title: propsValue.fragmentName || propsValue.fragment.filename,
contentType: 'file_attachment',
context: null,
url: null,
},
(key, value) => (typeof value === 'undefined' ? null : value)
),
};
return (await httpClient.sendRequest(request)).body;
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,32 +24,47 @@ export const createConversation = createAction({
username: usernameProp,
timezone: timezoneProp,
query: Property.LongText({ displayName: 'Query', required: true }),
fragment: Property.File({ displayName: 'Fragment', required: false }),
fragmentName: Property.ShortText({
displayName: 'Fragment name',
required: false,
}),
},
async run({ auth, propsValue }) {
const payload: Record<string, any> = {
visibility: 'unlisted',
title: null,
message: {
content: propsValue.query,
mentions: [{ configurationId: propsValue.assistant }],
context: {
timezone: propsValue.timezone,
username: propsValue.username,
email: null,
fullName: null,
profilePictureUrl: null,
},
},
};
if (propsValue.fragment) {
payload['contentFragment'] = {
title: propsValue.fragmentName || propsValue.fragment.filename,
content: propsValue.fragment.data.toString('utf-8'),
contentType: 'file_attachment',
context: null,
url: null,
};
}

const request: HttpRequest = {
method: HttpMethod.POST,
url: `${DUST_BASE_URL}/${auth.workspaceId}/assistant/conversations`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${auth.apiKey}`,
},
body: JSON.stringify(
{
visibility: 'unlisted',
title: null,
message: {
content: propsValue.query,
mentions: [{ configurationId: propsValue.assistant }],
context: {
timezone: propsValue.timezone,
username: propsValue.username,
email: null,
fullName: null,
profilePictureUrl: null,
},
},
},
(key, value) => (typeof value === 'undefined' ? null : value)
body: JSON.stringify(payload, (key, value) =>
typeof value === 'undefined' ? null : value
),
};
const body = (await httpClient.sendRequest(request)).body;
Expand Down
1 change: 1 addition & 0 deletions packages/pieces/community/dust/src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export async function getConversationContent(
await new Promise((f) => setTimeout(f, 10000));

conversation = await getConversation(conversationId);
console.log('STATUS', conversation.body);
retries += 1;
}

Expand Down

0 comments on commit 12a6cfd

Please sign in to comment.