Extract shared MeeBot config and unify OpenAI/RPC handling across API endpoints#76
Extract shared MeeBot config and unify OpenAI/RPC handling across API endpoints#76MEECHAIN1 wants to merge 1 commit into
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
🚅 Deployed to the MeeChain-Connect-pr-76 environment in meechain 5 services not affected by this PR
|
📝 WalkthroughCode Review AnalysisWalkthroughEnglish: ไทย: ChangesShared Configuration Extraction
Estimated Code Review EffortEnglish: Consistent refactoring pattern applied across multiple files: import shared helpers and replace inline configuration with function calls. Low logic density, straightforward extraction, and homogeneous repetitive changes across endpoints. ไทย: รูปแบบ refactoring ที่สอดคล้องกันถูกนำไปใช้ในหลายไฟล์: นำเข้า shared helpers และแทนที่การตั้งค่าแบบ inline ด้วยการเรียกฟังก์ชัน ความหนาแน่นของลอจิกต่ำ การแยกไฟล์ตรงไปตรงมา และการเปลี่ยนแปลงแบบทำซ้ำที่เป็นเนื้อเดียวกันในทั่วทั้ง endpoints Possibly Related PRs
PoemEnglish:
ไทย:
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
⚔️ Resolve merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 0/1 reviews remaining, refill in 60 minutes.Comment |
Deploying with
|
| Status | Name | Latest Commit | Updated (UTC) |
|---|---|---|---|
| ❌ Deployment failed View logs |
meechain-connect | d40b804 | May 04 2026, 04:14 PM |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (4)
cf-deploy/functions/api/health.js (1)
1-1: ⚡ Quick win🔧
modelยังคง hardcode ไว้ — ควรใช้MEEBOT_MODELจาก shared moduleEnglish: The PR centralises
MEEBOT_MODELin the shared module, but line 12 still holds a raw string'gpt-5-mini'. If the model is bumped inmeebot.js, this response field will silently drift out of sync. Import and reuse the constant.ภาษาไทย: PR นี้มีเป้าหมายรวมชื่อ model ไว้ที่เดียว แต่บรรทัด 12 ยังใช้ string ตรงๆ
'gpt-5-mini'หาก model ถูกเปลี่ยนในmeebot.jsค่า response ของ health endpoint จะเก่าโดยไม่มีการเตือน ควร import และใช้ constant แทน🛠️ Proposed fix
-import { getDefaultRpcUrl } from './_shared/meebot.js'; +import { getDefaultRpcUrl, MEEBOT_MODEL } from './_shared/meebot.js'; // ... const data = { status: 'ok', - model: 'gpt-5-mini', + model: MEEBOT_MODEL,Also applies to: 12-12
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cf-deploy/functions/api/health.js` at line 1, The health endpoint currently hardcodes the model string instead of using the shared constant; import MEEBOT_MODEL from the same module where getDefaultRpcUrl is imported (./_shared/meebot.js) and replace the raw 'gpt-5-mini' value in the health response with MEEBOT_MODEL so the endpoint stays in sync with the centralized model constant.cf-deploy/functions/api/_shared/meebot.js (1)
8-8: 💤 Low value💡 (Optional) RPC URL ใน system prompt ไม่ตรงกับ runtime config ที่อาจเปลี่ยนได้
English: The RPC URL
https://rpc.meechain.liveis hardcoded inside the prompt string itself (line 8), whilegetDefaultRpcUrl(env)allows it to be overridden viaDRPC_RPC_URL. If the runtime RPC differs from the one in the prompt, MeeBot's self-described knowledge will be stale. Consider injecting the live URL at build/render time, or note in comments that this string must be kept in sync manually.ภาษาไทย: URL ของ RPC ในบรรทัดที่ 8 ถูก hardcode ไว้ใน string ของ prompt โดยตรง ในขณะที่
getDefaultRpcUrl(env)ช่วยให้เปลี่ยนได้ผ่าน env varDRPC_RPC_URLหาก RPC จริงในตอน runtime ต่างจากที่อยู่ใน prompt MeeBot จะให้ข้อมูลที่ล้าสมัย ควรพิจารณาใส่ค่านี้แบบ dynamic หรืออย่างน้อยเพิ่ม comment เตือนให้ sync ด้วยตัวเอง🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cf-deploy/functions/api/_shared/meebot.js` at line 8, The system prompt in meebot.js hardcodes "https://rpc.meechain.live" which can diverge from the runtime value returned by getDefaultRpcUrl(env); update the prompt construction to use the live RPC URL from getDefaultRpcUrl(env) (or DRPC_RPC_URL) instead of the hardcoded string so the prompt reflects the actual runtime RPC, e.g., replace the static RPC text in the prompt string with the variable returned by getDefaultRpcUrl(env) at build/render time; if you can't make it dynamic, add a clear comment next to the prompt string reminding maintainers to keep it synced with getDefaultRpcUrl(env)/DRPC_RPC_URL.cf-deploy/functions/api/chat/stream.js (1)
88-107: ⚡ Quick win💡 (Pre-existing)
done: trueถูกส่งซ้ำสองครั้งต่อ streamEnglish: This is pre-existing behaviour, not introduced by this PR, but worth noting: when the upstream sends
[DONE], line 91 writes{ done: true }and thencontinues the loop. Thefinallyblock at line 104 unconditionally writes another{ done: true }. Every stream ends with two consecutive done events. Clients that guard against duplicatedonesignals are unaffected, but this could cause a double-close attempt or unexpected state in more strict consumers.ภาษาไทย: นี่เป็น behavior ที่มีอยู่เดิมก่อน PR นี้ แต่ควรทราบไว้: เมื่อ upstream ส่ง
[DONE]บรรทัดที่ 91 จะส่ง{ done: true }และcontinueต่อไป จากนั้นfinallyblock ที่บรรทัด 104 จะส่ง{ done: true }อีกครั้งโดยไม่มีเงื่อนไข ทุก stream จึงจบด้วย done event สองครั้งติดกัน🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cf-deploy/functions/api/chat/stream.js` around lines 88 - 107, The stream currently emits { done: true } twice: once when seeing raw === '[DONE]' inside the data parsing loop and again unconditionally in the finally block; fix this by ensuring only one done is sent—either remove the write when handling raw === '[DONE]' and break/return to let the finally block emit the single done, or add a boolean flag (e.g., sentDone) checked/updated in the data handling code and in the finally block so the writer.write(encoder.encode(JSON.stringify({ done: true }))) runs only once; update the code paths around the data parsing loop and the finally block (references: the data parsing loop that checks line.startsWith('data: ') and raw === '[DONE]', the writer and encoder usages, and the finally block that writes done and closes the writer).functions/api/_shared/meebot.js (1)
1-34: ⚖️ Poor tradeoff🔧 ไฟล์นี้ซ้ำกับ
cf-deploy/functions/api/_shared/meebot.jsแต่ขาดgetDefaultRpcUrlEnglish:
functions/api/_shared/meebot.jsandcf-deploy/functions/api/_shared/meebot.jsare nearly identical, but this file omitsgetDefaultRpcUrl. While neither handler underfunctions/api/currently needs it, the two files will silently diverge as the codebase evolves. Consider whether both deployment targets (functions/andcf-deploy/) can share a single source file (e.g., via a symlink or a workspace-level module), or at minimum add a comment noting that both files must be kept in sync.ภาษาไทย: ไฟล์
functions/api/_shared/meebot.jsและcf-deploy/functions/api/_shared/meebot.jsมีเนื้อหาเกือบเหมือนกัน แต่ไฟล์นี้ไม่มีgetDefaultRpcUrlแม้ handler ปัจจุบันภายใต้functions/api/จะยังไม่ต้องการฟังก์ชันนี้ แต่ทั้งสองไฟล์จะค่อยๆ แตกต่างกันโดยไม่ได้ตั้งใจ ควรพิจารณาใช้ไฟล์ source เดียวกัน (เช่น symlink หรือ workspace module) หรืออย่างน้อยเพิ่ม comment เตือนให้ sync ทั้งสองไฟล์เสมอ🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@functions/api/_shared/meebot.js` around lines 1 - 34, This file duplicates cf-deploy/functions/api/_shared/meebot.js but omits getDefaultRpcUrl, which will cause silent drift; either (A) restore/add the missing getDefaultRpcUrl function to this file (matching the implementation in cf-deploy), or (B) consolidate both locations into a single shared module (or symlink) so MEEBOT_SYSTEM_PROMPT, MEEBOT_MODEL, getOpenAIBaseUrl, buildChatMessages and the missing getDefaultRpcUrl live in one source; if you choose to keep two copies, add a clear top-of-file comment referencing the sibling file and reminding maintainers to keep them in sync.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@cf-deploy/functions/api/network.js`:
- Around line 11-17: The rpcUrls can collapse to a single entry because
fallbackRpc is computed as env.VITE_RPC_URL || primaryRpc; update the fallback
computation in getDefaultRpcUrl usage so it prefers a distinct configured
fallback (e.g. env.DRPC_RPC_URL) before falling back to primary: set fallbackRpc
= env.VITE_RPC_URL || env.DRPC_RPC_URL || primaryRpc, then build rpcUrls from
[primaryRpc, fallbackRpc] and if they are identical, ensure you still supply two
entries (for example by using the configured fallback if present or appending a
no-op query param to the second URL) so wallet_addEthereumChain consumers always
receive two URLs; adjust the code around primaryRpc, fallbackRpc and rpcUrls
accordingly.
In `@functions/api/chat.js`:
- Line 3: Update the stale header comment that hardcodes "powered by OpenAI
GPT-5-mini": change the banner in functions/api/chat.js to not hardcode the
model name and instead state that the model is configurable/comes from
MEEBOT_MODEL (shared module) or leave a neutral phrase like "model determined by
MEEBOT_MODEL"; search for the header comment text "// ║ MeeBot AI — powered by
OpenAI GPT-5-mini ║" and replace it with a generic, accurate line that
references MEEBOT_MODEL or "environment-configured model" so the comment remains
correct when the environment variable changes.
---
Nitpick comments:
In `@cf-deploy/functions/api/_shared/meebot.js`:
- Line 8: The system prompt in meebot.js hardcodes "https://rpc.meechain.live"
which can diverge from the runtime value returned by getDefaultRpcUrl(env);
update the prompt construction to use the live RPC URL from
getDefaultRpcUrl(env) (or DRPC_RPC_URL) instead of the hardcoded string so the
prompt reflects the actual runtime RPC, e.g., replace the static RPC text in the
prompt string with the variable returned by getDefaultRpcUrl(env) at
build/render time; if you can't make it dynamic, add a clear comment next to the
prompt string reminding maintainers to keep it synced with
getDefaultRpcUrl(env)/DRPC_RPC_URL.
In `@cf-deploy/functions/api/chat/stream.js`:
- Around line 88-107: The stream currently emits { done: true } twice: once when
seeing raw === '[DONE]' inside the data parsing loop and again unconditionally
in the finally block; fix this by ensuring only one done is sent—either remove
the write when handling raw === '[DONE]' and break/return to let the finally
block emit the single done, or add a boolean flag (e.g., sentDone)
checked/updated in the data handling code and in the finally block so the
writer.write(encoder.encode(JSON.stringify({ done: true }))) runs only once;
update the code paths around the data parsing loop and the finally block
(references: the data parsing loop that checks line.startsWith('data: ') and raw
=== '[DONE]', the writer and encoder usages, and the finally block that writes
done and closes the writer).
In `@cf-deploy/functions/api/health.js`:
- Line 1: The health endpoint currently hardcodes the model string instead of
using the shared constant; import MEEBOT_MODEL from the same module where
getDefaultRpcUrl is imported (./_shared/meebot.js) and replace the raw
'gpt-5-mini' value in the health response with MEEBOT_MODEL so the endpoint
stays in sync with the centralized model constant.
In `@functions/api/_shared/meebot.js`:
- Around line 1-34: This file duplicates
cf-deploy/functions/api/_shared/meebot.js but omits getDefaultRpcUrl, which will
cause silent drift; either (A) restore/add the missing getDefaultRpcUrl function
to this file (matching the implementation in cf-deploy), or (B) consolidate both
locations into a single shared module (or symlink) so MEEBOT_SYSTEM_PROMPT,
MEEBOT_MODEL, getOpenAIBaseUrl, buildChatMessages and the missing
getDefaultRpcUrl live in one source; if you choose to keep two copies, add a
clear top-of-file comment referencing the sibling file and reminding maintainers
to keep them in sync.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 8b192753-646b-4ac2-a6eb-4dfe1152abd5
📒 Files selected for processing (10)
cf-deploy/functions/api/_shared/meebot.jscf-deploy/functions/api/chat.jscf-deploy/functions/api/chat/stream.jscf-deploy/functions/api/health.jscf-deploy/functions/api/network.jscf-deploy/functions/api/nodecloud/stats.jscf-deploy/functions/api/web3/status.jsfunctions/api/_shared/meebot.jsfunctions/api/chat.jsfunctions/api/chat/stream.js
| const primaryRpc = getDefaultRpcUrl(env); | ||
| const fallbackRpc = env.VITE_RPC_URL || primaryRpc; | ||
|
|
||
| const data = { | ||
| chainId: `0x${chainId.toString(16)}`, // 0x344e | ||
| chainName: 'MeeChain Ritual Chain', | ||
| rpcUrls: [ | ||
| env.DRPC_RPC_URL || 'http://rpc.meechain.run.place', | ||
| env.VITE_RPC_URL || 'https://ritual-chain--pouaun2499.replit.app', | ||
| ], | ||
| nativeCurrency: { name: 'MEE Token', symbol: 'MEE', decimals: 18 }, | ||
| blockExplorerUrls:['http://explorer.meechain.run.place'], | ||
| chainId: `0x${chainId.toString(16)}`, // 0x344e | ||
| chainName: 'MeeChain Ritual Chain', | ||
| rpcUrls: Array.from(new Set([primaryRpc, fallbackRpc])), |
There was a problem hiding this comment.
rpcUrls อาจมีแค่รายการเดียวเมื่อ VITE_RPC_URL ไม่ได้ถูกกำหนด
English: fallbackRpc is derived as env.VITE_RPC_URL || primaryRpc. When only DRPC_RPC_URL is set (the typical production configuration per server.js), env.VITE_RPC_URL is undefined, so fallbackRpc === primaryRpc. The Set deduplication then produces a single-entry rpcUrls array instead of two. This is a behaviour regression — clients relying on this endpoint for wallet_addEthereumChain (MetaMask) or multi-RPC fallover lose their backup endpoint.
ภาษาไทย: fallbackRpc ถูกคำนวณจาก env.VITE_RPC_URL || primaryRpc เมื่อตั้งค่าแค่ DRPC_RPC_URL (กรณี production ทั่วไปตาม server.js) ตัวแปร env.VITE_RPC_URL จะเป็น undefined ทำให้ fallbackRpc === primaryRpc และหลังจาก dedup ด้วย Set แล้ว rpcUrls จะมีแค่รายการเดียว ส่งผลให้ client ที่ใช้ endpoint นี้สำหรับ wallet_addEthereumChain (MetaMask) หรือ multi-RPC fallover สูญเสีย backup endpoint ไป
ตัวอย่างการแก้ไขที่ให้ fallback URL ที่แตกต่างเสมอ:
🛠️ Proposed fix
const primaryRpc = getDefaultRpcUrl(env);
- const fallbackRpc = env.VITE_RPC_URL || primaryRpc;
+ // Use a dedicated fallback that is distinct from primaryRpc; only deduplicate
+ // if both happen to resolve to the same string.
+ const fallbackRpc = env.VITE_RPC_URL
+ || env.DRPC_RPC_URL
+ || 'https://rpc.meechain.run.place';🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@cf-deploy/functions/api/network.js` around lines 11 - 17, The rpcUrls can
collapse to a single entry because fallbackRpc is computed as env.VITE_RPC_URL
|| primaryRpc; update the fallback computation in getDefaultRpcUrl usage so it
prefers a distinct configured fallback (e.g. env.DRPC_RPC_URL) before falling
back to primary: set fallbackRpc = env.VITE_RPC_URL || env.DRPC_RPC_URL ||
primaryRpc, then build rpcUrls from [primaryRpc, fallbackRpc] and if they are
identical, ensure you still supply two entries (for example by using the
configured fallback if present or appending a no-op query param to the second
URL) so wallet_addEthereumChain consumers always receive two URLs; adjust the
code around primaryRpc, fallbackRpc and rpcUrls accordingly.
| @@ -3,56 +3,22 @@ | |||
| // ║ MeeBot AI — powered by OpenAI GPT-5-mini ║ | |||
There was a problem hiding this comment.
แก้ไข comment ส่วนหัวที่ล้าสมัย / Fix stale banner comment
ภาษาไทย: บรรทัดที่ 3 ยังระบุ "powered by OpenAI GPT-5-mini" แบบ hardcode ในขณะที่โมเดลที่ใช้จริงตอนนี้มาจาก MEEBOT_MODEL ใน shared module ซึ่งสามารถเปลี่ยนแปลงได้ผ่าน environment โดยที่ comment นี้จะยังคงแสดงชื่อโมเดลผิดอยู่
English: Line 3 still hardcodes "GPT-5-mini" in the banner, but the actual model is now driven by MEEBOT_MODEL from the shared module, which is configurable. This comment will mislead anyone reading the file header after an environment-driven model change.
✏️ Proposed fix
-// ║ MeeBot AI — powered by OpenAI GPT-5-mini ║
+// ║ MeeBot AI — model configured via MEEBOT_MODEL ║📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // ║ MeeBot AI — powered by OpenAI GPT-5-mini ║ | |
| // ║ MeeBot AI — model configured via MEEBOT_MODEL ║ |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@functions/api/chat.js` at line 3, Update the stale header comment that
hardcodes "powered by OpenAI GPT-5-mini": change the banner in
functions/api/chat.js to not hardcode the model name and instead state that the
model is configurable/comes from MEEBOT_MODEL (shared module) or leave a neutral
phrase like "model determined by MEEBOT_MODEL"; search for the header comment
text "// ║ MeeBot AI — powered by OpenAI GPT-5-mini ║" and replace it
with a generic, accurate line that references MEEBOT_MODEL or
"environment-configured model" so the comment remains correct when the
environment variable changes.
| @@ -0,0 +1,33 @@ | |||
| export const MEEBOT_SYSTEM_PROMPT = `คุณคือ "MeeBot" — AI Assistant ผู้ช่วยอัจฉริยะของแพลตฟอร์ม MeeChain | |||
| const fallbackRpc = env.VITE_RPC_URL || primaryRpc; | ||
|
|
||
| const data = { | ||
| chainId: `0x${chainId.toString(16)}`, // 0x344e | ||
| chainName: 'MeeChain Ritual Chain', | ||
| rpcUrls: [ | ||
| env.DRPC_RPC_URL || 'http://rpc.meechain.run.place', | ||
| env.VITE_RPC_URL || 'https://ritual-chain--pouaun2499.replit.app', | ||
| ], | ||
| nativeCurrency: { name: 'MEE Token', symbol: 'MEE', decimals: 18 }, | ||
| blockExplorerUrls:['http://explorer.meechain.run.place'], | ||
| chainId: `0x${chainId.toString(16)}`, // 0x344e | ||
| chainName: 'MeeChain Ritual Chain', | ||
| rpcUrls: Array.from(new Set([primaryRpc, fallbackRpc])), |
There was a problem hiding this comment.
| const fallbackRpc = env.VITE_RPC_URL || primaryRpc; | |
| const data = { | |
| chainId: `0x${chainId.toString(16)}`, // 0x344e | |
| chainName: 'MeeChain Ritual Chain', | |
| rpcUrls: [ | |
| env.DRPC_RPC_URL || 'http://rpc.meechain.run.place', | |
| env.VITE_RPC_URL || 'https://ritual-chain--pouaun2499.replit.app', | |
| ], | |
| nativeCurrency: { name: 'MEE Token', symbol: 'MEE', decimals: 18 }, | |
| blockExplorerUrls:['http://explorer.meechain.run.place'], | |
| chainId: `0x${chainId.toString(16)}`, // 0x344e | |
| chainName: 'MeeChain Ritual Chain', | |
| rpcUrls: Array.from(new Set([primaryRpc, fallbackRpc])), | |
| // Ensure fallback is different from primary to maintain redundancy | |
| // Priority: DRPC_RPC_URL → VITE_RPC_URL → default fallback | |
| let fallbackRpc; | |
| if (env.DRPC_RPC_URL && env.VITE_RPC_URL && env.DRPC_RPC_URL !== env.VITE_RPC_URL) { | |
| // Both are set and different, use the one not in primaryRpc | |
| fallbackRpc = primaryRpc === env.DRPC_RPC_URL ? env.VITE_RPC_URL : env.DRPC_RPC_URL; | |
| } else if (env.VITE_RPC_URL && primaryRpc !== env.VITE_RPC_URL) { | |
| // Use VITE_RPC_URL if it's different from primary | |
| fallbackRpc = env.VITE_RPC_URL; | |
| } else if (primaryRpc !== 'https://rpc.meechain.live') { | |
| // Default fallback is different from primary | |
| fallbackRpc = 'https://rpc.meechain.live'; | |
| } else { | |
| // Fallback to historical URL when primary is the default | |
| fallbackRpc = 'https://ritual-chain--pouaun2499.replit.app'; | |
| } | |
| const data = { | |
| chainId: `0x${chainId.toString(16)}`, // 0x344e | |
| chainName: 'MeeChain Ritual Chain', | |
| rpcUrls: [primaryRpc, fallbackRpc], |
Refactored rpcUrls array logic can result in duplicate URLs, reducing failover capability to a single RPC endpoint
Motivation
Description
*_shared/meebot.jswithMEEBOT_SYSTEM_PROMPT,MEEBOT_MODEL,getOpenAIBaseUrl,getDefaultRpcUrl, andbuildChatMessagesto encapsulate bot config and helpers./api/chat,/api/chat/stream) to import and useMEEBOT_MODEL,buildChatMessagesandgetOpenAIBaseUrlinstead of inline prompt/model/base URL, and removed the unusedsessionIdparameter.health.js,network.js,nodecloud/stats.js, andweb3/status.jswithgetDefaultRpcUrl(env)and deduplicatedrpcUrlsinnetwork.jsusingArray.from(new Set(...)).Testing
Codex Task
Summary by CodeRabbit
บันทึกประจำรุ่น