-
Notifications
You must be signed in to change notification settings - Fork 5.8k
feat(dify): add new actions for listing conversations, knowledge base, querying knowledge base, running workflows, and sending chat messages #21864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Priyadharshan-Pdm
wants to merge
7
commits into
master
Choose a base branch
from
dharshan/issue-21662-dify
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
749120a
feat(dify): add new actions for listing conversations, knowledge base…
Priyadharshan-Pdm f22ffe4
feat(dify): enhance descriptions and validation for knowledge base ac…
Priyadharshan-Pdm 207765c
feat(dify): improve descriptions for knowledge base actions and input…
Priyadharshan-Pdm ebeb5f3
Add Dify Get App Parameters and List Messages actions
Priyadharshan-Pdm fcc305b
Optimize Dify actions and clarify user field requirements
Priyadharshan-Pdm 72905c3
Merge branch 'master' of https://github.com/PipedreamHQ/pipedream int…
Priyadharshan-Pdm fd80905
Remove Send Chat Message action and update documentation
Priyadharshan-Pdm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
components/dify/actions/get-app-parameters/get-app-parameters.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import dify from "../../dify.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "dify-get-app-parameters", | ||
| name: "Get App Parameters", | ||
| description: "Return the connected Dify app's configuration: its `user_input_form` (the exact input variable names, types, and which are required), file-upload limits, opening statement, and suggested questions. Call this before **Run Workflow** to know what to pass in its `Inputs` parameter, instead of guessing variable names. [See the documentation](https://docs.dify.ai/en/api-reference/applications/get-app-parameters)", | ||
| version: "0.0.1", | ||
| ai: "optimized", | ||
| type: "action", | ||
| annotations: { | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| }, | ||
| props: { | ||
| dify, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.dify.getAppParameters({ | ||
| $, | ||
| }); | ||
|
|
||
| const inputCount = response.user_input_form?.length ?? 0; | ||
| $.export("$summary", `Retrieved app parameters (${inputCount} input variable(s))`); | ||
| return response; | ||
| }, | ||
| }; |
65 changes: 65 additions & 0 deletions
65
components/dify/actions/list-conversations/list-conversations.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import dify from "../../dify.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "dify-list-conversations", | ||
| name: "List Conversations", | ||
| description: "List a Dify Chatflow, Chatbot, Agent, or Legacy Agent app's conversations, most recently active first. Conversations are scoped by `User`, so pass the same value used to create those conversations to see that end user's threads. [See the documentation](https://docs.dify.ai/en/api-reference/conversations/list-conversations)", | ||
| version: "0.0.1", | ||
| ai: "optimized", | ||
| type: "action", | ||
| annotations: { | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| }, | ||
| props: { | ||
| dify, | ||
| user: { | ||
| propDefinition: [ | ||
| dify, | ||
| "user", | ||
| ], | ||
| description: "A unique identifier for the end user whose conversations to list. This must match the `User` value used when those conversations were created — Dify silently returns an empty page instead of an error when `User` is omitted.", | ||
| }, | ||
| lastId: { | ||
| type: "string", | ||
| label: "Last Conversation ID", | ||
| description: "Pagination cursor: the `id` of the last conversation from the previous page's results. Omit to fetch the first page.", | ||
| optional: true, | ||
| }, | ||
| limit: { | ||
| type: "integer", | ||
| label: "Limit", | ||
| description: "Number of conversations to return, between `1` and `100`. Defaults to `20`.", | ||
| min: 1, | ||
| max: 100, | ||
| optional: true, | ||
| }, | ||
| sortBy: { | ||
| type: "string", | ||
| label: "Sort By", | ||
| description: "Field to sort results by. Defaults to `-updated_at` (most recently updated first).", | ||
| options: [ | ||
| "created_at", | ||
| "-created_at", | ||
| "updated_at", | ||
| "-updated_at", | ||
| ], | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.dify.listConversations({ | ||
| $, | ||
| params: { | ||
| user: this.user, | ||
| last_id: this.lastId, | ||
| limit: this.limit, | ||
| sort_by: this.sortBy, | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", `Found ${response.data.length} conversation(s)`); | ||
| return response; | ||
| }, | ||
| }; | ||
49 changes: 49 additions & 0 deletions
49
components/dify/actions/list-knowledge-bases/list-knowledge-bases.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import dify from "../../dify.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "dify-list-knowledge-bases", | ||
| name: "List Knowledge Bases", | ||
| description: "List the knowledge bases (datasets) visible to your Dify account, optionally filtered by name. Use this to find the `Knowledge Base ID` needed by **Query Knowledge Base**. This requires a Dify connection authenticated with a knowledge base API key (issued from a knowledge base's own **API Access** page), not an app API key — those authenticate **Run Workflow** instead. A `401 unauthorized` error here usually means the connected account is using an app key; reconnect with a knowledge base key instead. [See the documentation](https://docs.dify.ai/en/api-reference/knowledge-bases/list-knowledge-bases)", | ||
| version: "0.0.1", | ||
| ai: "optimized", | ||
| type: "action", | ||
| annotations: { | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| }, | ||
| props: { | ||
| dify, | ||
| keyword: { | ||
| type: "string", | ||
| label: "Keyword", | ||
| description: "Filter knowledge bases by name, e.g. `Product Documentation`.", | ||
| optional: true, | ||
| }, | ||
| page: { | ||
| type: "integer", | ||
| label: "Page", | ||
| description: "Page number of results to return. Defaults to `1`.", | ||
| optional: true, | ||
| }, | ||
| limit: { | ||
| type: "integer", | ||
| label: "Limit", | ||
| description: "Number of knowledge bases to return per page. Defaults to `20`.", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.dify.listDatasets({ | ||
| $, | ||
| params: { | ||
| keyword: this.keyword, | ||
| page: this.page, | ||
| limit: this.limit, | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", `Found ${response.data.length} knowledge base(s)`); | ||
| return response; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import dify from "../../dify.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "dify-list-messages", | ||
| name: "List Messages", | ||
| description: "Return a Dify Chatflow, Chatbot, Agent, or Legacy Agent conversation's message history, newest first. Use **List Conversations** to find a `Conversation ID`. Each message includes the `query`/`answer` pair, so this is how an agent reconstructs prior turns of a conversation instead of relying on its own memory. [See the documentation](https://docs.dify.ai/en/api-reference/conversations/list-conversation-messages)", | ||
| version: "0.0.1", | ||
| ai: "optimized", | ||
| type: "action", | ||
| annotations: { | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| }, | ||
| props: { | ||
| dify, | ||
| conversationId: { | ||
| type: "string", | ||
| label: "Conversation ID", | ||
| description: "The ID of the conversation to read. Use **List Conversations** to find valid IDs.", | ||
| }, | ||
| user: { | ||
| propDefinition: [ | ||
| dify, | ||
| "user", | ||
| ], | ||
| description: "A unique identifier for the end user who owns this conversation. This must match the `User` value used when the conversation was created — Dify silently returns an empty page instead of an error when `User` is omitted.", | ||
| }, | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| firstId: { | ||
| type: "string", | ||
| label: "First Message ID", | ||
| description: "Pagination cursor: the `id` of the first (oldest) message on the current page. Pass it to fetch the previous, older page. Omit to fetch the most recent messages.", | ||
| optional: true, | ||
| }, | ||
| limit: { | ||
| type: "integer", | ||
| label: "Limit", | ||
| description: "Number of messages to return, between `1` and `100`. Defaults to `20`.", | ||
| min: 1, | ||
| max: 100, | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.dify.listMessages({ | ||
| $, | ||
| params: { | ||
| conversation_id: this.conversationId, | ||
| user: this.user, | ||
| first_id: this.firstId, | ||
| limit: this.limit, | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", `Found ${response.data.length} message(s) in conversation ${this.conversationId}`); | ||
| return response; | ||
| }, | ||
| }; | ||
91 changes: 91 additions & 0 deletions
91
components/dify/actions/query-knowledge-base/query-knowledge-base.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| import { ConfigurationError } from "@pipedream/platform"; | ||
| import dify from "../../dify.app.mjs"; | ||
|
|
||
| const SEARCH_METHODS = [ | ||
| "keyword_search", | ||
| "semantic_search", | ||
| "full_text_search", | ||
| "hybrid_search", | ||
| ]; | ||
|
|
||
| export default { | ||
| key: "dify-query-knowledge-base", | ||
| name: "Query Knowledge Base", | ||
| description: "Search a Dify knowledge base (dataset) and return the chunks most relevant to a query. Use **List Knowledge Bases** to find the `Knowledge Base ID`. This requires a Dify connection authenticated with a knowledge base API key (issued from a knowledge base's own **API Access** page), not an app API key — those authenticate **Run Workflow** instead. A `401 unauthorized` error here usually means the connected account is using an app key; reconnect with a knowledge base key instead. [See the documentation](https://docs.dify.ai/en/api-reference/knowledge-bases/retrieve-chunks-from-a-knowledge-base-test-retrieval)", | ||
| version: "0.0.1", | ||
| ai: "optimized", | ||
| type: "action", | ||
| annotations: { | ||
| readOnlyHint: true, | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| }, | ||
| props: { | ||
| dify, | ||
| datasetId: { | ||
| type: "string", | ||
| label: "Knowledge Base ID", | ||
| description: "The UUID of the knowledge base to search, e.g. `c42e2a6e-40b3-4330-96f8-f1e4d768e8c9`. Use **List Knowledge Bases** to find valid IDs.", | ||
| }, | ||
| query: { | ||
| type: "string", | ||
| label: "Query", | ||
| description: "The search text, up to 250 characters, e.g. `What is Dify?`", | ||
| }, | ||
| searchMethod: { | ||
| type: "string", | ||
| label: "Search Method", | ||
| description: "The retrieval method to use. Setting this, `Top K`, or `Score Threshold` builds a full retrieval configuration for this request (with reranking disabled) instead of using the knowledge base's own configured defaults. Leave all three unset to use the knowledge base's defaults.", | ||
| options: SEARCH_METHODS, | ||
| optional: true, | ||
| }, | ||
| topK: { | ||
| type: "integer", | ||
| label: "Top K", | ||
| description: "The maximum number of matching chunks to return. Defaults to `3` if any of `Search Method`, `Top K`, or `Score Threshold` is set.", | ||
| optional: true, | ||
| }, | ||
| scoreThreshold: { | ||
| type: "string", | ||
| label: "Score Threshold", | ||
| description: "The minimum similarity score (between `0` and `1`) a chunk must have to be included in the results, e.g. `0.5`. Setting this enables score threshold filtering for this request.", | ||
| optional: true, | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| if (this.query.length > 250) { | ||
| throw new ConfigurationError("Query must be 250 characters or fewer."); | ||
| } | ||
|
|
||
| let scoreThreshold; | ||
| if (this.scoreThreshold !== undefined) { | ||
| scoreThreshold = Number(this.scoreThreshold); | ||
| if (!Number.isFinite(scoreThreshold) || scoreThreshold < 0 || scoreThreshold > 1) { | ||
| throw new ConfigurationError("Score Threshold must be a number between 0 and 1."); | ||
| } | ||
| } | ||
|
|
||
| const useCustomRetrieval = this.searchMethod | ||
| || this.topK !== undefined | ||
| || scoreThreshold !== undefined; | ||
| const retrievalModel = useCustomRetrieval && { | ||
| search_method: this.searchMethod || "hybrid_search", | ||
| reranking_enable: false, | ||
| top_k: this.topK ?? 3, | ||
| score_threshold_enabled: scoreThreshold !== undefined, | ||
| score_threshold: scoreThreshold, | ||
| }; | ||
|
|
||
| const response = await this.dify.retrieveFromDataset({ | ||
| $, | ||
| datasetId: this.datasetId, | ||
| data: { | ||
| query: this.query, | ||
| retrieval_model: retrievalModel || undefined, | ||
| }, | ||
| }); | ||
|
|
||
| $.export("$summary", `Found ${response.records.length} matching chunk(s) for "${this.query}"`); | ||
| return response; | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import dify from "../../dify.app.mjs"; | ||
|
|
||
| export default { | ||
| key: "dify-run-workflow", | ||
| name: "Run Workflow", | ||
| description: "Run a Dify Workflow app's published workflow and return its outputs. Requires the workflow to be published — draft-only workflows return a `bad_request` error. This action uses `blocking` response mode, which waits for the run to finish before returning; long-running workflows on Dify Cloud risk being cut off by the platform's 100-second edge proxy timeout, in which case the run may still complete server-side but this action will not see the result. [See the documentation](https://docs.dify.ai/en/api-reference/workflow-runs/run-workflow)", | ||
| version: "0.0.1", | ||
| ai: "optimized", | ||
| type: "action", | ||
| annotations: { | ||
| readOnlyHint: false, | ||
| destructiveHint: false, | ||
| openWorldHint: true, | ||
| }, | ||
| props: { | ||
| dify, | ||
| inputs: { | ||
| propDefinition: [ | ||
| dify, | ||
| "inputs", | ||
| ], | ||
| }, | ||
| user: { | ||
| propDefinition: [ | ||
| dify, | ||
| "user", | ||
| ], | ||
| }, | ||
| }, | ||
| async run({ $ }) { | ||
| const response = await this.dify.runWorkflow({ | ||
| $, | ||
| data: { | ||
| inputs: this.inputs ?? {}, | ||
| user: this.user, | ||
| response_mode: "blocking", | ||
| }, | ||
| }); | ||
|
|
||
| const { data } = response; | ||
| $.export("$summary", `Workflow run ${data.id} finished with status "${data.status}"`); | ||
| return response; | ||
| }, | ||
| }; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Keep shared
usermetadata incomponents/dify/dify.app.mjs.Both actions locally redefine the shared
userprop description. This can cause descriptions and examples to drift between actions.components/dify/actions/list-conversations/list-conversations.mjs#L22-L22: remove the local description and use the shareduserpropDefinition; place the identifier guidance and example incomponents/dify/dify.app.mjs.components/dify/actions/list-messages/list-messages.mjs#L27-L27: remove the local description and use the same shareduserpropDefinition; keep the message-history guidance incomponents/dify/dify.app.mjs.As per path instructions, shared props must use the app file's
propDefinitions, and component files must not duplicate prop descriptions.📍 Affects 2 files
components/dify/actions/list-conversations/list-conversations.mjs#L22-L22(this comment)components/dify/actions/list-messages/list-messages.mjs#L27-L27🤖 Prompt for AI Agents
Source: Path instructions