DN 流程,在笔记内让 ai 根据当前笔记内容进行回复,内部 ai 回复框

注意注意,别把这个语句放在有隐私的笔记里面!!!

原有功能与实现教程:
思源笔记页面内实现 ai 聊天与 ai 读取笔记内容进行回复(自动)! - 链滴

让 ai 根据当前笔记内容进行回复

代码功能:
在创建日记的时候进行回复

在每天写完日记以后,让 ai 根据日记内容进行回复

image.png

//!js // Function to get context from recently updated documents (Keep as is) async function getDocumentContext() { try { let currentDocId = Query.root_id(protyle); const sqlQuery = ` select * from blocks where root_id = '${currentDocId}' order by sort ASC limit 9999 `; let blocks = await Query.sql(sqlQuery); if (blocks && blocks.length > 0) { let contextEntries = blocks.map(block => { const content = block.markdown || block.content || '[No Content]'; return `Document Path: ${block.hpath || 'N/A'}\nContent:\n---\n${content}\n---`; }); return "以下是当前文档的内容,请参考:\n==========\n" + contextEntries.join('\n\n') + "\n==========\n"; } else { return "当前文档没有内容可供参考。\n"; } } catch (error) { console.error("查询文档时出错:", error); return "查询参考文档时出现错误。\n"; } } // Function to create the UI elements (Keep as is) const ui = () => { const textarea = document.createElement('textarea'); textarea.className = "fn__block b3-text-field"; textarea.rows = 3; textarea.placeholder = "继续提问..."; const buttonContainer = document.createElement('div'); buttonContainer.style.display = 'flex'; buttonContainer.style.justifyContent = 'flex-end'; buttonContainer.style.gap = '8px'; buttonContainer.style.marginTop = '8px'; const removeLastButton = document.createElement('button'); removeLastButton.className = "b3-button"; removeLastButton.textContent = "我不会和别人说的🌹"; buttonContainer.appendChild(removeLastButton); const sendButton = document.createElement('button'); sendButton.className = "b3-button"; sendButton.textContent = "是这样的😄"; buttonContainer.appendChild(sendButton); return { textarea, buttonContainer, sendButton, removeLastButton }; } // Main chat function (Modified to fix null querySelector error) const chat = async () => { let dv = Query.DataView(protyle, item, top); const messages = dv.useState('messages', []); const msgIds = dv.useState('msgIds', []); // --- Initial Rendering Structure --- dv.addmd(`#### 向我分享你的一天吧!`); // 1. Render existing messages from state const initialMsgIds = []; messages().forEach(msg => { try { let el = dv.addmd(`**${msg.role === 'user' ? 'You' : 'GPT'}**: ${msg.content}`); if (el && el.dataset && el.dataset.id) { initialMsgIds.push(el.dataset.id); } } catch (e) { console.error("Error rendering message:", e); } }); if (msgIds().length === 0 && initialMsgIds.length > 0) { msgIds(initialMsgIds); } // 2. Add Separator dv.addmd('---'); // 3. Create UI Elements but don't store unnecessary references const { textarea, buttonContainer, sendButton, removeLastButton } = ui(); // Just add them directly without storing the references dv.addele(textarea); dv.addele(buttonContainer); // --- Function to perform AI query (Modified with better error handling) --- const performAIQuery = async (prompt, isInitialQuery = false) => { try { sendButton.disabled = true; removeLastButton.disabled = true; // Add placeholder with better error handling let respondPlaceholder; let gptMsgId = null; try { respondPlaceholder = dv.addmd(`**GPT**: 正在检索参考资料并思考... 🤔`); if (respondPlaceholder && respondPlaceholder.dataset) { gptMsgId = respondPlaceholder.dataset.id; // Add placeholder ID to state only if it's valid if (gptMsgId) { msgIds([...msgIds(), gptMsgId]); } } } catch (e) { console.error("Error adding placeholder:", e); // If we can't add a placeholder, we'll just add a new message later } // Safe function to replace or add content const updateContent = (id, content) => { try { if (id) { dv.replaceView(id, dv.md(`**GPT**: ${content}`)); } else { // If no ID, add as new message let newEl = dv.addmd(`**GPT**: ${content}`); if (newEl && newEl.dataset && newEl.dataset.id) { msgIds([...msgIds(), newEl.dataset.id]); } } } catch (e) { console.error("Error updating content:", e); // Last resort fallback - just add a new message try { dv.addmd(`**GPT**: ${content}`); } catch (e) { console.error("Failed to add fallback message:", e); } } }; const docReference = await getDocumentContext(); const prefixText = `${docReference}\n这是我的提问背景信息:`; const suffixText = "\n请基于以上参考信息,回复我的问题。注意:多用表情,尽量简短且切中要点。"; const fullPrompt = `${prefixText}\n${prompt}\n${suffixText}`; const response = await Query.gpt(fullPrompt, { stream: true, streamInterval: 3, streamMsg: (content) => { // Safely update with streaming content updateContent(gptMsgId, content); } }); // Update state with final response const gptMessage = { role: 'assistant', content: response }; const currentMessages = messages(); if (isInitialQuery) { messages([gptMessage]); } else { messages([...currentMessages, gptMessage]); } // Final update with safe function updateContent(gptMsgId, response); } catch (error) { console.error("GPT 调用或处理失败:", error); const errorMessageContent = `抱歉,处理时遇到问题: ${error.message}`; try { if (gptMsgId) { dv.replaceView(gptMsgId, dv.md(`**GPT**: ${errorMessageContent}`)); } else { dv.addmd(`**GPT**: ${errorMessageContent}`); } const errorMessage = { role: 'assistant', content: errorMessageContent }; const currentMessages = messages(); if (isInitialQuery) { messages([errorMessage]); } else { messages([...currentMessages, errorMessage]); } } catch (e) { console.error("Error handling GPT failure:", e); // Try one more time with a simple message try { dv.addmd(`**Error**: ${error.message}`); } catch (finalError) { console.error("Failed all error handling attempts:", finalError); } } } finally { // Re-enable buttons regardless of outcome try { sendButton.disabled = false; removeLastButton.disabled = false; // Safely repaint dv.repaint(); } catch (e) { console.error("Error in finally block:", e); } } }; // --- Initial Query Logic --- if (messages().length === 0) { const predefinedPrompt = "今天真是美好的一天,请你基于参考内容,激励我度过美好的一天吧!请你用满满的正能量激励我,并且要加入很多的表情,让我的心情愉快!"; try { await performAIQuery(predefinedPrompt, true); } catch (e) { console.error("Initial query failed:", e); // Try to add a fallback message dv.addmd("**System**: 初始查询失败,但你仍然可以开始对话。"); } } // --- Button Handlers --- sendButton.onclick = async () => { const userInput = textarea.value.trim(); if (!userInput) return; // 1. Add user message to state and UI with better error handling const userMessage = { role: 'user', content: userInput }; messages([...messages(), userMessage]); let userEl; let userMsgId = null; try { userEl = dv.addmd(`**You**: ${userInput}`); if (userEl && userEl.dataset) { userMsgId = userEl.dataset.id; if (userMsgId) { msgIds([...msgIds(), userMsgId]); } } } catch (e) { console.error("Error adding user message:", e); } textarea.value = ''; // 2. Perform AI query with error handling try { await performAIQuery(userInput, false); } catch (e) { console.error("Query failed:", e); dv.addmd(`**System**: 查询失败,请重试。错误: ${e.message}`); } }; // Remove last button click handler (with better error handling) removeLastButton.onclick = () => { try { const currentMessages = messages(); let currentMsgIds = [...msgIds()]; if (currentMessages.length < 2 || currentMsgIds.length < 2) return; // Update messages state first (in case view operations fail) messages(currentMessages.slice(0, -2)); // Then try to remove the views try { const lastGptId = currentMsgIds.pop(); const lastUserId = currentMsgIds.pop(); if (lastGptId) dv.removeView(lastGptId); if (lastUserId) dv.removeView(lastUserId); // Update msgIds state only after successful removal msgIds(currentMsgIds); } catch (e) { console.error("Error removing views:", e); } // Always try to repaint dv.repaint(); } catch (e) { console.error("Error in removeLastButton handler:", e); } }; // --- Final Render --- try { dv.render(); } catch (e) { console.error("Error rendering DataView:", e); } } // Execute the chat function with error handling try { return chat(); } catch (e) { console.error("Fatal error in chat function:", e); return "Error initializing chat. Please check console for details."; }
  • 思源笔记

    思源笔记是一款隐私优先的个人知识管理系统,支持完全离线使用,同时也支持端到端加密同步。

    融合块、大纲和双向链接,重构你的思维。

    25730 引用 • 106473 回帖
1 操作
TangQi 在 2025-04-20 21:14:41 更新了该帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...

推荐标签 标签

  • Spark

    Spark 是 UC Berkeley AMP lab 所开源的类 Hadoop MapReduce 的通用并行框架。Spark 拥有 Hadoop MapReduce 所具有的优点;但不同于 MapReduce 的是 Job 中间输出结果可以保存在内存中,从而不再需要读写 HDFS,因此 Spark 能更好地适用于数据挖掘与机器学习等需要迭代的 MapReduce 的算法。

    74 引用 • 46 回帖 • 565 关注
  • 工具

    子曰:“工欲善其事,必先利其器。”

    298 引用 • 763 回帖
  • Q&A

    提问之前请先看《提问的智慧》,好的问题比好的答案更有价值。

    9858 引用 • 44812 回帖 • 78 关注
  • 游戏

    沉迷游戏伤身,强撸灰飞烟灭。

    180 引用 • 821 回帖
  • 开源

    Open Source, Open Mind, Open Sight, Open Future!

    411 引用 • 3588 回帖
  • RIP

    愿逝者安息!

    8 引用 • 92 回帖 • 403 关注
  • 阿里云

    阿里云是阿里巴巴集团旗下公司,是全球领先的云计算及人工智能科技公司。提供云服务器、云数据库、云安全等云计算服务,以及大数据、人工智能服务、精准定制基于场景的行业解决方案。

    84 引用 • 324 回帖
  • Solidity

    Solidity 是一种智能合约高级语言,运行在 [以太坊] 虚拟机(EVM)之上。它的语法接近于 JavaScript,是一种面向对象的语言。

    3 引用 • 18 回帖 • 433 关注
  • Vditor

    Vditor 是一款浏览器端的 Markdown 编辑器,支持所见即所得、即时渲染(类似 Typora)和分屏预览模式。它使用 TypeScript 实现,支持原生 JavaScript、Vue、React 和 Angular。

    371 引用 • 1854 回帖 • 2 关注
  • Laravel

    Laravel 是一套简洁、优雅的 PHP Web 开发框架。它采用 MVC 设计,是一款崇尚开发效率的全栈框架。

    19 引用 • 23 回帖 • 739 关注
  • PWL

    组织简介

    用爱发电 (Programming With Love) 是一个以开源精神为核心的民间开源爱好者技术组织,“用爱发电”象征开源与贡献精神,加入组织,代表你将遵守组织的“个人开源爱好者”的各项条款。申请加入:用爱发电组织邀请帖
    用爱发电组织官网:https://programmingwithlove.stackoverflow.wiki/

    用爱发电组织的核心驱动力:

    • 遵守开源守则,体现开源&贡献精神:以分享为目的,拒绝非法牟利。
    • 自我保护:使用适当的 License 保护自己的原创作品。
    • 尊重他人:不以各种理由、各种漏洞进行未经允许的抄袭、散播、洩露;以礼相待,尊重所有对社区做出贡献的开发者;通过他人的分享习得知识,要留下足迹,表示感谢。
    • 热爱编程、热爱学习:加入组织,热爱编程是首当其要的。我们欢迎热爱讨论、分享、提问的朋友,也同样欢迎默默成就的朋友。
    • 倾听:正确并恳切对待、处理问题与建议,及时修复开源项目的 Bug ,及时与反馈者沟通。不抬杠、不无视、不辱骂。
    • 平视:不诋毁、轻视、嘲讽其他开发者,主动提出建议、施以帮助,以和谐为本。只要他人肯努力,你也可能会被昔日小看的人所超越,所以请保持谦虚。
    • 乐观且活跃:你的努力决定了你的高度。不要放弃,多年后回头俯瞰,才会发现自己已经成就往日所仰望的水平。积极地将项目开源,帮助他人学习、改进,自己也会获得相应的提升、成就与成就感。
    1 引用 • 487 回帖 • 1 关注
  • FlowUs

    FlowUs.息流 个人及团队的新一代生产力工具。

    让复杂的信息管理更轻松、自由、充满创意。

    1 引用 • 2 关注
  • IDEA

    IDEA 全称 IntelliJ IDEA,是一款 Java 语言开发的集成环境,在业界被公认为最好的 Java 开发工具之一。IDEA 是 JetBrains 公司的产品,这家公司总部位于捷克共和国的首都布拉格,开发人员以严谨著称的东欧程序员为主。

    181 引用 • 400 回帖 • 2 关注
  • 30Seconds

    📙 前端知识精选集,包含 HTML、CSS、JavaScript、React、Node、安全等方面,每天仅需 30 秒。

    • 精选常见面试题,帮助您准备下一次面试
    • 精选常见交互,帮助您拥有简洁酷炫的站点
    • 精选有用的 React 片段,帮助你获取最佳实践
    • 精选常见代码集,帮助您提高打码效率
    • 整理前端界的最新资讯,邀您一同探索新世界
    488 引用 • 384 回帖 • 9 关注
  • JavaScript

    JavaScript 一种动态类型、弱类型、基于原型的直译式脚本语言,内置支持类型。它的解释器被称为 JavaScript 引擎,为浏览器的一部分,广泛用于客户端的脚本语言,最早是在 HTML 网页上使用,用来给 HTML 网页增加动态功能。

    730 引用 • 1281 回帖 • 1 关注
  • CodeMirror
    2 引用 • 17 回帖 • 164 关注
  • 星云链

    星云链是一个开源公链,业内简单的将其称为区块链上的谷歌。其实它不仅仅是区块链搜索引擎,一个公链的所有功能,它基本都有,比如你可以用它来开发部署你的去中心化的 APP,你可以在上面编写智能合约,发送交易等等。3 分钟快速接入星云链 (NAS) 测试网

    3 引用 • 16 回帖 • 2 关注
  • Windows

    Microsoft Windows 是美国微软公司研发的一套操作系统,它问世于 1985 年,起初仅仅是 Microsoft-DOS 模拟环境,后续的系统版本由于微软不断的更新升级,不但易用,也慢慢的成为家家户户人们最喜爱的操作系统。

    227 引用 • 476 回帖
  • Pipe

    Pipe 是一款小而美的开源博客平台。Pipe 有着非常活跃的社区,可将文章作为帖子推送到社区,来自社区的回帖将作为博客评论进行联动(具体细节请浏览 B3log 构思 - 分布式社区网络)。

    这是一种全新的网络社区体验,让热爱记录和分享的你不再感到孤单!

    134 引用 • 1127 回帖 • 108 关注
  • 京东

    京东是中国最大的自营式电商企业,2015 年第一季度在中国自营式 B2C 电商市场的占有率为 56.3%。2014 年 5 月,京东在美国纳斯达克证券交易所正式挂牌上市(股票代码:JD),是中国第一个成功赴美上市的大型综合型电商平台,与腾讯、百度等中国互联网巨头共同跻身全球前十大互联网公司排行榜。

    14 引用 • 102 回帖 • 314 关注
  • Postman

    Postman 是一款简单好用的 HTTP API 调试工具。

    4 引用 • 3 回帖 • 6 关注
  • JetBrains

    JetBrains 是一家捷克的软件开发公司,该公司位于捷克的布拉格,并在俄国的圣彼得堡及美国麻州波士顿都设有办公室,该公司最为人所熟知的产品是 Java 编程语言开发撰写时所用的集成开发环境:IntelliJ IDEA

    18 引用 • 54 回帖 • 1 关注
  • Telegram

    Telegram 是一个非盈利性、基于云端的即时消息服务。它提供了支持各大操作系统平台的开源的客户端,也提供了很多强大的 APIs 给开发者创建自己的客户端和机器人。

    5 引用 • 35 回帖 • 1 关注
  • Anytype
    3 引用 • 31 回帖 • 24 关注
  • GitLab

    GitLab 是利用 Ruby 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可通过 Web 界面操作公开或私有项目。

    46 引用 • 72 回帖
  • Word
    13 引用 • 41 回帖
  • Markdown

    Markdown 是一种轻量级标记语言,用户可使用纯文本编辑器来排版文档,最终通过 Markdown 引擎将文档转换为所需格式(比如 HTML、PDF 等)。

    171 引用 • 1537 回帖