Skip to content

fix: auto-detect Vercel for chat history path#436

Merged
CJackHwang merged 1 commit intoCJackHwang:devfrom
waiwaic:main
May 6, 2026
Merged

fix: auto-detect Vercel for chat history path#436
CJackHwang merged 1 commit intoCJackHwang:devfrom
waiwaic:main

Conversation

@waiwaic
Copy link
Copy Markdown
Contributor

@waiwaic waiwaic commented May 6, 2026

Summary

On Vercel, /var/task is read-only at runtime. The chat history module previously tried to create directories under /var/task/data/, which fails with:

create chat history dir: mkdir /var/task/data: read-only file system

Fix

Modified ChatHistoryPath() in internal/config/paths.go to auto-detect Vercel via the existing IsVercel() function. When running on Vercel and no explicit DS2API_CHAT_HISTORY_PATH env var is set, it now defaults to /tmp/chat_history.json (the only writable directory on Vercel Serverless).

If the user explicitly sets DS2API_CHAT_HISTORY_PATH, that value is still respected as an override.

Changes

  • internal/config/paths.go: +5 lines in ChatHistoryPath()

Verification

  • go build -o /dev/null ./cmd/ds2api/ passes
  • Backward compatible: non-Vercel environments still use the original default data/chat_history.json

On Vercel, /var/task is read-only at runtime. ChatHistoryPath() now
auto-detects Vercel via IsVercel() and defaults to /tmp/chat_history.json
when no explicit DS2API_CHAT_HISTORY_PATH is set. Manual env var still
works as explicit override.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 6, 2026

@waiwaic is attempting to deploy a commit to the cjack's projects Team on Vercel.

A member of the Team first needs to authorize it.

@waiwaic
Copy link
Copy Markdown
Contributor Author

waiwaic commented May 6, 2026

Hi @CJackHwang 👋

首先非常感谢你创建并维护 ds2api 这个项目,它帮了大忙! 🙏

我最近在 Vercel 上部署时遇到了 chat history 写入失败的问题(read-only file system),所以提交了这个简单的修复:自动检测 Vercel 环境并在未显式设置 DS2API_CHAT_HISTORY_PATH 时默认使用 /tmp/chat_history.json

不过有个小顾虑想跟你讨论一下——有朋友提醒我,Vercel Serverless 环境下的 /tmp每个容器独立的,由于负载均衡和容器回收机制,不同请求可能命中不同的执行环境,各自拥有独立的 /tmp。这意味着:

  • 并发请求可能会把 chat history 写到不同的容器里,导致数据分裂
  • 容器空闲回收后,之前写入的 /tmp 数据会永久丢失
  • 冷启动的请求完全看不到之前的任何历史记录

所以当前这个修复虽然解决了 read-only 崩溃的问题,但在持久化可靠性上还有提升空间。

不知道你怎么看这个方向?如果你觉得需要更完善的持久化方案(比如集成 Vercel KV、Vercel Blob Storage,或者提供文档说明让用户在 Vercel 上使用外部存储),我也很乐意进一步贡献 PR 😊

期待你的反馈,再次感谢你的出色工作!

@CJackHwang CJackHwang changed the base branch from main to dev May 6, 2026 04:48
@CJackHwang
Copy link
Copy Markdown
Owner

CJackHwang commented May 6, 2026

Hi @CJackHwang 👋

首先非常感谢你创建并维护 ds2api 这个项目,它帮了大忙! 🙏

我最近在 Vercel 上部署时遇到了 chat history 写入失败的问题(read-only file system),所以提交了这个简单的修复:自动检测 Vercel 环境并在未显式设置 DS2API_CHAT_HISTORY_PATH 时默认使用 /tmp/chat_history.json

不过有个小顾虑想跟你讨论一下——有朋友提醒我,Vercel Serverless 环境下的 /tmp每个容器独立的,由于负载均衡和容器回收机制,不同请求可能命中不同的执行环境,各自拥有独立的 /tmp。这意味着:

  • 并发请求可能会把 chat history 写到不同的容器里,导致数据分裂
  • 容器空闲回收后,之前写入的 /tmp 数据会永久丢失
  • 冷启动的请求完全看不到之前的任何历史记录

所以当前这个修复虽然解决了 read-only 崩溃的问题,但在持久化可靠性上还有提升空间。

不知道你怎么看这个方向?如果你觉得需要更完善的持久化方案(比如集成 Vercel KV、Vercel Blob Storage,或者提供文档说明让用户在 Vercel 上使用外部存储),我也很乐意进一步贡献 PR 😊

期待你的反馈,再次感谢你的出色工作!

说来挺有趣的,我用Vercel这么多年但从来没有用过任何外部存储的功能,在本项目里我一直都默认Vercel部署是属于“功能限制版”的状态,没有做太多深入的维护,主打的一个兼容和基础场景使用,毕竟免费版的active cpu time 只有4h,大量使用好像也不太现实,而维护Vercel的持久化似乎很麻烦。如果Vercel部署能使用外部存储那么本项目甚至都可以完全抛弃环境变量的配置方式了,毕竟当初就是为了方便在这个平台上使用才做的
如果可以的话我确实更愿意使用外部持久化,这样config文件也能直接热更新,再也不用担心环境变量配置后异步的更新机制带来各种不方便了

@vercel
Copy link
Copy Markdown

vercel Bot commented May 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
ds2api Ready Ready Preview, Comment May 6, 2026 4:59am

@CJackHwang
Copy link
Copy Markdown
Owner

@waiwaic 很感谢你的贡献,现在看起来功能似乎完全正常了

@CJackHwang CJackHwang merged commit 79ae9c8 into CJackHwang:dev May 6, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] 在vercel里部署,web页面的‘响应记录’里不显示记录。

2 participants