diff --git a/packages/pieces/community/dust/.eslintrc.json b/packages/pieces/community/dust/.eslintrc.json new file mode 100644 index 00000000000..d7449bb0383 --- /dev/null +++ b/packages/pieces/community/dust/.eslintrc.json @@ -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": {} + } + ] +} diff --git a/packages/pieces/community/dust/README.md b/packages/pieces/community/dust/README.md new file mode 100644 index 00000000000..70fd912f9d1 --- /dev/null +++ b/packages/pieces/community/dust/README.md @@ -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. diff --git a/packages/pieces/community/dust/package.json b/packages/pieces/community/dust/package.json new file mode 100644 index 00000000000..1317d7a8e77 --- /dev/null +++ b/packages/pieces/community/dust/package.json @@ -0,0 +1,4 @@ +{ + "name": "@activepieces/piece-dust", + "version": "0.0.1" +} diff --git a/packages/pieces/community/dust/project.json b/packages/pieces/community/dust/project.json new file mode 100644 index 00000000000..60cb49f5fc4 --- /dev/null +++ b/packages/pieces/community/dust/project.json @@ -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": [] +} \ No newline at end of file diff --git a/packages/pieces/community/dust/src/index.ts b/packages/pieces/community/dust/src/index.ts new file mode 100644 index 00000000000..e3be22403c7 --- /dev/null +++ b/packages/pieces/community/dust/src/index.ts @@ -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: [], +}); diff --git a/packages/pieces/community/dust/src/lib/actions/create-conversation.ts b/packages/pieces/community/dust/src/lib/actions/create-conversation.ts new file mode 100644 index 00000000000..189ae287d03 --- /dev/null +++ b/packages/pieces/community/dust/src/lib/actions/create-conversation.ts @@ -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; + }, +}); diff --git a/packages/pieces/community/dust/tsconfig.json b/packages/pieces/community/dust/tsconfig.json new file mode 100644 index 00000000000..059cd816618 --- /dev/null +++ b/packages/pieces/community/dust/tsconfig.json @@ -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" + } + ] +} diff --git a/packages/pieces/community/dust/tsconfig.lib.json b/packages/pieces/community/dust/tsconfig.lib.json new file mode 100644 index 00000000000..28369ef7622 --- /dev/null +++ b/packages/pieces/community/dust/tsconfig.lib.json @@ -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"] +}