Skip to content

feat: improve embedding sys.user_id and conversion id info usage #18035

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

Merged

Conversation

ModyQyW
Copy link
Contributor

@ModyQyW ModyQyW commented Apr 14, 2025

Summary

This PR improves embedding sys.user_id and conversation id info usage. Remove getPublicToken method as it is not used.

Close #17870.

Screenshots

Before After
image image image image

Checklist

Important

Please review the checklist below before submitting your pull request.

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 💪 enhancement New feature or request labels Apr 14, 2025
@asrguru20
Copy link

Summary

This PR improves embedding sys.user_id and conversation id info usage. Remove getPublicToken method as it is not used. Please review carefully, thank you 🙏

Close #17870.

Screenshots

Before After
image image image image

Checklist

Important

Please review the checklist below before submitting your pull request.

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.
  • I ran dev/reformat(backend) and cd web && npx lint-staged(frontend) to appease the lint gods

very useful fix then we can create chatbot that aware if WordPress user is logged in or not

@asrguru20
Copy link

Please approve this pull because I need this issue to fixed so my chatbot iframe can pass logged in user information from my WordPress site

@crazywoola crazywoola requested review from Copilot and JzoNgKVO April 15, 2025 06:59
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

@crazywoola crazywoola self-requested a review April 15, 2025 07:01
@asrguru20
Copy link

will this pull be merged ? is there anyone answer me please

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Apr 16, 2025
@crazywoola crazywoola merged commit 44cdb3d into langgenius:main Apr 16, 2025
5 checks passed
@ModyQyW ModyQyW deleted the Ray/feat/improve-embedding-user-id-usage branch April 20, 2025 08:41
@vivianxe-ifast
Copy link

Hi @asrguru20 , would like to check with you, do we have documentation on how to implement in the difyChatbotConfig so that user_id is passed dynamically according to the logged in user? Thanks in advanced.

@ModyQyW
Copy link
Contributor Author

ModyQyW commented Apr 25, 2025

So far, I have initiated two PRs to handle the custom sys.user_id issue, one landed in v1.2 #16062, and the other landed in v1.3 #18035.

The following are my personal suggestions based on v1.3:

If you do not need to customize sys.user_id, or you do not need to dynamically handle custom sys.user_id, just follow the steps described in your dify app Publish / Embed Into Site to embed the iframe yourself or use embed.min.js to create a chatbot.

If you need to dynamically handle sys.user_id, please handle it according to the situation:

For the case of directly loading the iframe, please try to destroy the iframe and then create a new iframe using the new URL;

For the case of using embed.min.js, please set window.difyChatbotConfig.dynamicScript to true, set window.systemVariables.user_id to the corresponding user id, and reload the page to destroy the iframe and then create a new iframe using the new URL.

If there are any problems, please open a new issue and use a new app to specifically describe the reproduction steps.

@vivianxe-ifast
Copy link

vivianxe-ifast commented Apr 25, 2025

For the case of using embed.min.js, please set window.difyChatbotConfig.dynamicScript to true, set window.systemVariables.user_id to the corresponding user id, and reload the page to destroy the iframe and then create a new iframe using the new URL.

Hi @ModyQyW , the URL you meant is the public URL (as attached)?

Screenshot 2025-04-25 at 10 17 22 AM

1. As in, for every change of user_id, the public URL also need to regenerate?

2. Based on your enhancement, are we able to achieve something like this:-

a) user A login

window.difyChatbotConfig = {
token: 'N8oUKetDAUmhAUhU',
baseUrl: 'http://xxxxx',
systemVariables: {
user_id: 'userA',
},
}

b) user B login

window.difyChatbotConfig = {
token: 'N8oUKetDAUmhAUhU',
baseUrl: 'http://xxxxx',
systemVariables: {
user_id: 'userB',
},
}

c) when user A login again, still able to view own previous conversation
window.difyChatbotConfig = {
token: 'N8oUKetDAUmhAUhU',
baseUrl: 'http://xxxxx',
systemVariables: {
user_id: 'userA',
},
}

@jason-lee-x
Copy link

So far, I have initiated two PRs to handle the custom sys.user_id issue, one landed in v1.2 #16062, and the other landed in v1.3 #18035.

The following are my personal suggestions based on v1.3:

If you do not need to customize sys.user_id, or you do not need to dynamically handle custom sys.user_id, just follow the steps described in your dify app Publish / Embed Into Site to embed the iframe yourself or use embed.min.js to create a chatbot.

If you need to dynamically handle sys.user_id, please handle it according to the situation:

For the case of directly loading the iframe, please try to destroy the iframe and then create a new iframe using the new URL;

For the case of using embed.min.js, please set window.difyChatbotConfig.dynamicScript to true, set window.systemVariables.user_id to the corresponding user id, and reload the page to destroy the iframe and then create a new iframe using the new URL.

If there are any problems, please open a new issue and use a new app to specifically describe the reproduction steps.

It's better to have a prefix on the local storage that the embed iframes produced,so that we can remove them easily instead destroying iframe and reload the page.

@ModyQyW
Copy link
Contributor Author

ModyQyW commented Apr 25, 2025

For the case of using embed.min.js, please set window.difyChatbotConfig.dynamicScript to true, set window.systemVariables.user_id to the corresponding user id, and reload the page to destroy the iframe and then create a new iframe using the new URL.

Hi @ModyQyW , the URL you meant is the public URL (as attached)?

Screenshot 2025-04-25 at 10 17 22 AM **1. As in, for every change of user_id, the public URL also need to regenerate?**

2. Based on your enhancement, are we able to achieve something like this:-

a) user A login

window.difyChatbotConfig = { token: 'N8oUKetDAUmhAUhU', baseUrl: 'http://xxxxx', systemVariables: { user_id: 'userA', }, }

b) user B login

window.difyChatbotConfig = { token: 'N8oUKetDAUmhAUhU', baseUrl: 'http://xxxxx', systemVariables: { user_id: 'userB', }, }

c) when user A login again, still able to view own previous conversation window.difyChatbotConfig = { token: 'N8oUKetDAUmhAUhU', baseUrl: 'http://xxxxx', systemVariables: { user_id: 'userA', }, }

👋 Hi @vivianxe-ifast

  1. I'm not referring to regenerating the Public URL, but specifically the URL Search Params part.

User A with userA as user_id should have https://udify.app/chat/N8oUKetDAUmhAUhU?sys.user_id=H4sIAAAAAAAAAystTi1yBABp9FTcBQAAAA== (visit https://dify-ce-dd.modyqyw.top/ce?value=userA to get the final value), and User B with userB as user_id should have https://udify.app/chat/N8oUKetDAUmhAUhU?sys.user_id=H4sIAAAAAAAAAystTi1yAgDTpV1FBQAAAA== (visit https://dify-ce-dd.modyqyw.top/ce?value=userB to get the final value).

The core difference can be seen in the part after the ?.

If you update window.difyChatbotConfig after creating an iframe, embed.js or your own managed iframe will not update the already created iframe. That's why I said you need to destroy the original iframe first and then create a new iframe, just to ensure that the URL Search Params used by the iframe are correct.

  1. Yes! The last conversation id is attached to both app id and user id now.

My English is a bit poor, I hope I can explain everything clearly to you. Thanks, hope this helps! 🙏

@ModyQyW
Copy link
Contributor Author

ModyQyW commented Apr 25, 2025

So far, I have initiated two PRs to handle the custom sys.user_id issue, one landed in v1.2 #16062, and the other landed in v1.3 #18035.
The following are my personal suggestions based on v1.3:
If you do not need to customize sys.user_id, or you do not need to dynamically handle custom sys.user_id, just follow the steps described in your dify app Publish / Embed Into Site to embed the iframe yourself or use embed.min.js to create a chatbot.
If you need to dynamically handle sys.user_id, please handle it according to the situation:
For the case of directly loading the iframe, please try to destroy the iframe and then create a new iframe using the new URL;
For the case of using embed.min.js, please set window.difyChatbotConfig.dynamicScript to true, set window.systemVariables.user_id to the corresponding user id, and reload the page to destroy the iframe and then create a new iframe using the new URL.
If there are any problems, please open a new issue and use a new app to specifically describe the reproduction steps.

It's better to have a prefix on the local storage that the embed iframes produced,so that we can remove them easily instead destroying iframe and reload the page.

Sorry but I don't get your point. Could you provide an example to further illustrate please? Thank you! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💪 enhancement New feature or request lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request] Improve embedding sys.user_id usage
5 participants