my-neuro插件开发指南:从基础到高级功能扩展实战

发布时间:2026/7/16 21:24:34
my-neuro插件开发指南:从基础到高级功能扩展实战 my-neuro插件开发指南从基础到高级功能扩展实战【免费下载链接】my-neuroThis project lets you create your own AI desktop companion with customizable characters and voice conversations that respond in just 1 second. Features include long-term memory, visual recognition, voice cloning and LLM training. Compatible with various Live2D customizations.项目地址: https://gitcode.com/gh_mirrors/my/my-neuromy-neuro是一款功能强大的AI桌面伴侣项目允许用户创建自定义角色和语音对话响应速度仅需1秒。本指南将带你从基础开始逐步掌握my-neuro插件开发的核心技能实现从简单功能到高级扩展的全流程实战。插件开发基础了解my-neuro插件架构在开始开发之前首先需要了解my-neuro的插件架构。my-neuro采用模块化设计插件系统是其核心组成部分允许开发者扩展各种功能如自动对话、记忆管理、语音交互等。插件目录结构my-neuro的插件主要存放在live-2d/plugins目录下分为内置插件和社区插件内置插件live-2d/plugins/built-in/包含官方开发的核心功能插件社区插件live-2d/plugins/community/由社区开发者贡献的扩展功能每个插件都有自己的独立目录包含至少两个核心文件index.js插件主入口文件metadata.json插件元数据配置文件metadata.json详解metadata.json是插件的身份标识包含插件的基本信息。以下是一个典型的metadata.json示例{ name: ai-log, displayName: 思痕之册, version: 1.0.0, author: official, description: 每一天的对话都值得铭记 —— 夜幕降临时自动凝结当日思绪为观察日志汇聚月光编织月度回忆录让 AI 的记忆跨越每一次重启, framework_version: 1.0.0, main: index.js, lang: js }关键字段说明name插件唯一标识符用于文件系统和代码引用displayName用户界面显示的插件名称version插件版本号遵循语义化版本规范framework_version插件兼容的my-neuro框架版本main插件入口文件lang开发语言目前支持JavaScript快速入门开发你的第一个插件环境准备克隆my-neuro仓库git clone https://gitcode.com/gh_mirrors/my/my-neuro进入项目目录cd my-neuro创建插件目录在live-2d/plugins/community/目录下创建你的插件文件夹例如hello-world/mkdir -p live-2d/plugins/community/hello-world创建metadata.json在插件目录中创建metadata.json文件{ name: hello-world, displayName: Hello World 插件, version: 1.0.0, author: 你的名字, description: 我的第一个my-neuro插件, framework_version: 1.0.0, main: index.js, lang: js }编写插件主文件创建index.js文件实现一个简单的插件// 导入插件基础类 const PluginBase require(../../../core/plugin-base); // 继承PluginBase类 class HelloWorldPlugin extends PluginBase { constructor(context) { super(context); this.name hello-world; } // 插件初始化方法 async initialize() { this.logger.info(Hello World 插件初始化成功); // 注册一个简单的命令 this.registerCommand(hello, () { return Hello from my first plugin!; }); // 监听应用启动事件 this.eventBus.on(app.initialized, () { this.showNotification(Hello World 插件已就绪); }); } // 插件销毁方法 async destroy() { this.logger.info(Hello World 插件已卸载); } } // 导出插件 module.exports HelloWorldPlugin;启用插件编辑live-2d/plugins/enabled_plugins.json文件添加你的插件{ enabled: [ hello-world, // 其他已启用插件... ] }核心功能开发事件系统与API调用my-neuro插件系统提供了丰富的事件和API让插件能够与核心系统深度集成。事件系统事件系统是插件与主程序通信的主要方式。你可以监听系统事件也可以触发自定义事件。常用系统事件app.initialized应用初始化完成conversation.started对话开始conversation.ended对话结束tts.speakingTTS开始说话tts.finishedTTS说话结束监听事件示例// 监听对话开始事件 this.eventBus.on(conversation.started, (data) { this.logger.info(对话开始: ${data.text}); // 在这里可以实现自定义逻辑如记录对话内容 });触发自定义事件// 触发自定义事件 this.eventBus.emit(hello-world.greet, { message: Hello from plugin, timestamp: new Date() });API调用插件可以通过pluginContext访问系统API实现各种功能。常用API示例// 显示通知 this.pluginContext.ui.showNotification(这是一条通知); // 调用AI对话 const response await this.pluginContext.ai.chat(你好今天天气怎么样); // 播放声音 this.pluginContext.audio.playSound(path/to/sound.wav); // 保存数据 await this.pluginContext.storage.set(user_preference, { theme: dark }); // 读取数据 const preference await this.pluginContext.storage.get(user_preference);高级功能UI集成与配置界面对于需要用户交互的插件你可以创建自定义UI界面和配置面板。创建配置界面在插件目录中创建config.html文件div classplugin-config h3Hello World 插件设置/h3 div classform-group label forgreeting-text问候语/label input typetext idgreeting-text classform-control /div div classform-group label input typecheckbox idshow-notification 启动时显示通知 /label /div button idsave-config classbtn btn-primary保存设置/button /div在插件中加载配置界面async initialize() { // 注册配置页面 this.registerConfigPage({ title: Hello World 设置, html: await this.loadAsset(config.html), onLoad: (container) { // 加载配置 const config this.getConfig() || { greetingText: Hello World, showNotification: true }; // 填充表单 container.querySelector(#greeting-text).value config.greetingText; container.querySelector(#show-notification).checked config.showNotification; // 保存按钮事件 container.querySelector(#save-config).addEventListener(click, () { const newConfig { greetingText: container.querySelector(#greeting-text).value, showNotification: container.querySelector(#show-notification).checked }; // 保存配置 this.setConfig(newConfig); this.showNotification(设置已保存); }); } }); }插件发布与分享完成插件开发后你可以将其分享给其他用户。打包插件创建plugin.json文件描述插件的详细信息{ name: hello-world, version: 1.0.0, author: 你的名字, description: 我的第一个my-neuro插件, homepage: , repository: , dependencies: {}, files: [ index.js, metadata.json, config.html ] }将插件目录压缩为ZIP文件即可分享给其他用户。提交到社区插件库如果你希望将插件贡献给社区可以提交到live-2d/plugins/plugin-house/plugin_hub.json文件添加你的插件信息。实战案例开发一个情绪日志插件下面我们通过一个实际案例开发一个记录AI情绪变化的插件。插件功能描述该插件将记录AI在对话中的情绪变化生成情绪日志并在每天结束时总结情绪变化趋势。创建插件结构live-2d/plugins/community/emotion-logger/ ├── index.js ├── metadata.json ├── config.html └── templates/ ├── daily-report.html └── weekly-report.html实现核心功能const PluginBase require(../../../core/plugin-base); const fs require(fs); const path require(path); class EmotionLoggerPlugin extends PluginBase { constructor(context) { super(context); this.name emotion-logger; this.emotionLog []; this.logDir path.join(this.pluginContext.appDataDir, emotion-logs); // 确保日志目录存在 if (!fs.existsSync(this.logDir)) { fs.mkdirSync(this.logDir, { recursive: true }); } } async initialize() { this.logger.info(情绪日志插件初始化成功); // 监听对话事件 this.eventBus.on(conversation.ended, this.handleConversationEnded.bind(this)); // 注册生成报告命令 this.registerCommand(generate-emotion-report, (params) { return this.generateReport(params.period || daily); }); // 设置每日报告定时任务 this.pluginContext.scheduler.scheduleDaily(23:59, () { this.generateReport(daily); }); // 注册配置页面 this.registerConfigPage({ title: 情绪日志设置, html: await this.loadAsset(config.html), onLoad: this.setupConfigUI.bind(this) }); } handleConversationEnded(data) { // 记录情绪信息 const emotionEntry { timestamp: new Date().toISOString(), text: data.text, emotion: data.emotion, confidence: data.emotionConfidence }; this.emotionLog.push(emotionEntry); // 保存到文件 this.saveLog(); } saveLog() { const date new Date().toISOString().split(T)[0]; const logFile path.join(this.logDir, ${date}.json); fs.writeFileSync(logFile, JSON.stringify(this.emotionLog, null, 2), utf8); } async generateReport(period) { // 实现报告生成逻辑 // ... const report 情绪报告(${period})\n${summary}; this.showNotification(情绪${period}报告已生成); return report; } setupConfigUI(container) { // 设置配置界面逻辑 // ... } async destroy() { this.logger.info(情绪日志插件已卸载); // 保存未保存的日志 this.saveLog(); } } module.exports EmotionLoggerPlugin;插件开发最佳实践代码组织保持代码模块化将不同功能拆分为多个文件使用ES6特性如async/await、箭头函数等遵循JavaScript标准风格指南性能优化避免阻塞主线程耗时操作使用异步处理合理使用缓存减少重复计算事件监听在不需要时及时移除错误处理使用try/catch捕获异常提供有意义的错误信息实现优雅降级机制安全性验证所有用户输入限制文件系统访问范围避免使用eval等危险函数常用插件API参考核心APIAPI描述this.logger日志记录器this.eventBus事件总线this.pluginContext插件上下文this.registerCommand(name, handler)注册命令this.registerConfigPage(config)注册配置页面存储API// 保存数据 await this.pluginContext.storage.set(key, value); // 获取数据 const value await this.pluginContext.storage.get(key); // 删除数据 await this.pluginContext.storage.delete(key);UI API// 显示通知 this.pluginContext.ui.showNotification(message); // 显示对话框 const result await this.pluginContext.ui.showDialog(title, message, buttons); // 打开URL this.pluginContext.ui.openUrl(url);总结与进阶通过本指南你已经掌握了my-neuro插件开发的基础知识和实战技能。从简单的Hello World插件到功能完善的情绪日志插件你可以看到my-neuro插件系统的强大和灵活。要进一步提升插件开发技能可以研究内置插件源码live-2d/plugins/built-in/探索高级API如语音处理、图像识别等参与社区讨论分享你的插件和经验my-neuro的插件生态正在不断发展期待你的贡献让这个AI桌面伴侣变得更加强大和多样化【免费下载链接】my-neuroThis project lets you create your own AI desktop companion with customizable characters and voice conversations that respond in just 1 second. Features include long-term memory, visual recognition, voice cloning and LLM training. Compatible with various Live2D customizations.项目地址: https://gitcode.com/gh_mirrors/my/my-neuro创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考