Skip to content

Commit

Permalink
feat: add Dust piece
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamSelene committed Feb 15, 2024
1 parent 032d19a commit 29dc36d
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 0 deletions.
37 changes: 37 additions & 0 deletions packages/pieces/community/dust/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"extends": [
"../../../../.eslintrc.json"
],
"ignorePatterns": [
"!**/*"
],
"overrides": [
{
"files": [
"*.ts",
"*.tsx",
"*.js",
"*.jsx"
],
"rules": {},
"extends": [
"plugin:prettier/recommended"
],
"plugins": ["prettier"]
},
{
"files": [
"*.ts",
"*.tsx"
],
"rules": {}
},
{
"files": [
"*.js",
"*.jsx"
],
"rules": {}
}
]
}
7 changes: 7 additions & 0 deletions packages/pieces/community/dust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# pieces-dust

This library was generated with [Nx](https://nx.dev).

## Building

Run `nx build pieces-dust` to build the library.
4 changes: 4 additions & 0 deletions packages/pieces/community/dust/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@activepieces/piece-dust",
"version": "0.0.1"
}
43 changes: 43 additions & 0 deletions packages/pieces/community/dust/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "pieces-dust",
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
"sourceRoot": "packages/pieces/community/dust/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": [
"{options.outputPath}"
],
"options": {
"outputPath": "dist/packages/pieces/community/dust",
"tsConfig": "packages/pieces/community/dust/tsconfig.lib.json",
"packageJson": "packages/pieces/community/dust/package.json",
"main": "packages/pieces/community/dust/src/index.ts",
"assets": [
"packages/pieces/community/dust/*.md"
],
"buildableProjectDepsInPackageJsonType": "dependencies",
"updateBuildableProjectDepsInPackageJson": true
}
},
"publish": {
"command": "node tools/scripts/publish.mjs pieces-dust {args.ver} {args.tag}",
"dependsOn": [
"build"
]
},
"lint": {
"executor": "@nx/eslint:lint",
"outputs": [
"{options.outputFile}"
],
"options": {
"lintFilePatterns": [
"packages/pieces/community/dust/**/*.ts"
]
}
}
},
"tags": []
}
32 changes: 32 additions & 0 deletions packages/pieces/community/dust/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {
createPiece,
PieceAuth,
Property,
} from '@activepieces/pieces-framework';
import { createConversation } from './lib/actions/create-conversation';

export const dustAuth = PieceAuth.CustomAuth({
description: 'Dust authentication requires an API key.',
required: true,
props: {
apiKey: PieceAuth.SecretText({
displayName: 'API key',
required: true,
}),
workspaceId: Property.ShortText({
displayName: 'Dust workspace ID',
required: true,
description: "Can be found in any of the workspace's URL",
}),
},
});

export const dust = createPiece({
displayName: 'Dust',
auth: dustAuth,
minimumSupportedRelease: '0.9.0',
logoUrl: 'https://cdn.activepieces.com/pieces/dust.png',
authors: ['AdamSelene'],
actions: [createConversation],
triggers: [],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { createAction, Property } from '@activepieces/pieces-framework';
import { dustAuth } from '../..';
import {
httpClient,
HttpMethod,
HttpRequest,
} from '@activepieces/pieces-common';

const DUST_BASE_URL = 'https://dust.tt/api/v1/w';
export const createConversation = createAction({
// auth: check https://www.activepieces.com/docs/developers/piece-reference/authentication,
name: 'createConversation',
displayName: 'Create conversation',
description: '',
auth: dustAuth,
props: {
query: Property.LongText({ displayName: 'Query', required: true }),
assistant: Property.ShortText({ displayName: 'Assistant', required: true }),
timeZone: Property.ShortText({
displayName: 'Time zone',
required: true,
defaultValue: 'Europe/Paris',
}),
username: Property.ShortText({
displayName: 'Username',
required: true,
}),
fullName: Property.ShortText({
displayName: 'Full name',
required: false,
}),
profilePictureUrl: Property.ShortText({
displayName: 'Profile picture URL',
required: false,
}),
},
async run({ auth, propsValue }) {
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: 'Europe/Paris',
username: propsValue.username,
email: null,
fullName: propsValue.fullName,
profilePictureUrl: propsValue.profilePictureUrl,
},
},
},
(key, value) => (typeof value === 'undefined' ? null : value)
),
};
const body = (await httpClient.sendRequest(request)).body;
const conversationId = body['conversation']['sId'];
const getConversation = async (conversationId: string) => {
return httpClient.sendRequest({
method: HttpMethod.GET,
url: `${DUST_BASE_URL}/${auth.workspaceId}/assistant/conversations/${conversationId}`,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${auth.apiKey}`,
},
});
};

let conversation = await getConversation(conversationId);

let retries = 0;
while (
!['succeeded', 'errored'].includes(
conversation.body['conversation']['content']?.at(-1)?.at(0)?.status
) &&
retries < 12 // 2mn
) {
await new Promise((f) => setTimeout(f, 10000));

conversation = await getConversation(conversationId);
retries += 1;
}

return conversation.body;
},
});
19 changes: 19 additions & 0 deletions packages/pieces/community/dust/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../../../tsconfig.base.json",
"compilerOptions": {
"module": "commonjs",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
},
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
}
]
}
11 changes: 11 additions & 0 deletions packages/pieces/community/dust/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "../../../../dist/out-tsc",
"declaration": true,
"types": ["node"]
},
"exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"],
"include": ["src/**/*.ts"]
}

0 comments on commit 29dc36d

Please sign in to comment.