<script>
// ★ここだけあなたの環境に合わせて変更
const DIFY_BASE_URL = "http://localhost"; // ← 必ず「Difyが動いてる実URL」に
// ログインユーザー取得(ダッシュボードでは $p.loginUser が取れないことがあるので userId() 優先)
const pleId =
(window.$p && typeof $p.userId === 'function') ? String($p.userId()) :
(($p.loginUser && $p.loginUser.Id) ? String($p.loginUser.Id) : 'guest');
const pleName =
(window.$p && typeof $p.userName === 'function') ? String($p.userName()) :
(($p.loginUser && $p.loginUser.Name) ? String($p.loginUser.Name) : 'guest');
// 履歴分離+リセット用:ユーザー×セッションで user_id を変える
const SESSION_KEY = 'dify_user_session_' + pleId;
function makeShortId() {
return (crypto.randomUUID ? crypto.randomUUID() : String(Math.random()).slice(2))
.replace(/-/g,'').slice(0,12);
}
let sessionId = localStorage.getItem(SESSION_KEY);
if (!sessionId) { sessionId = makeShortId(); localStorage.setItem(SESSION_KEY, sessionId); }
const difyUserId = 'pleasanter_' + pleId + '_' + sessionId;
// デバッグ表示
//const debugEl = document.getElementById('difyDebug');
// if (debugEl) {
//debugEl.textContent = `user_id=${difyUserId} / baseUrl=${DIFY_BASE_URL}` + (pleId === 'guest' ? ' ← ⚠ userId取得失敗' : '');
//}
// Dify設定:embed.min.js より先に必ず定義
window.difyChatbotConfig = {
token: 'xxxxx',
baseUrl: DIFY_BASE_URL.replace(/\/+$/, ''), // 末尾スラッシュ除去
inputs: {},
systemVariables: {
user_id: difyUserId
// conversation_id は入れない(Conversation Not Exists を避ける)
},
userVariables: {
name: pleName
}
};
// リセット
window.resetDifyChat = function () {
localStorage.removeItem(SESSION_KEY);
location.reload();
};
// embed.min.js を「確実に」読み込む(onload / onerror で原因がわかる)
const scriptId = 'dify-embed-script';
if (!document.getElementById(scriptId)) {
const s = document.createElement('script');
s.id = scriptId;
s.defer = true;
s.src = window.difyChatbotConfig.baseUrl + '/embed.min.js';
s.onload = () => console.log('[Dify] embed.min.js loaded:', s.src);
s.onerror = (e) => console.error('[Dify] embed.min.js load failed:', s.src, e);
document.body.appendChild(s);
console.log('[Dify] loading embed.min.js...', s.src);
}
</script>
<style>
#dify-chatbot-bubble-button { background-color: #1C64F2 !important; }
#dify-chatbot-bubble-window { width: 24rem !important; height: 40rem !important; }
</style>
これでプリザンターのログインユーザー毎に、チャット履歴がお互いの見えないようになる。