Skip to content

fix(book-flight-ai-agent): 前端 / 路由接入 ChatHandler.Index 修复 Set-Cookie 不下发问题 (#1086)#1100

Merged
Alanxtl merged 1 commit into
apache:mainfrom
Harry33t:fix/1086-book-flight-session-cookie
May 10, 2026
Merged

fix(book-flight-ai-agent): 前端 / 路由接入 ChatHandler.Index 修复 Set-Cookie 不下发问题 (#1086)#1100
Alanxtl merged 1 commit into
apache:mainfrom
Harry33t:fix/1086-book-flight-session-cookie

Conversation

@Harry33t

@Harry33t Harry33t commented May 4, 2026

Copy link
Copy Markdown
Contributor

问题

go-client/frontend/main.go/ 路由注册成一个匿名函数,它只渲染模板、根本没碰 session:

r.GET("/", func(c *gin.Context) {
    c.HTML(http.StatusOK, "index.html", gin.H{
        "TimeoutSecond": cfgEnv.TimeOut,
        "OllamaModel":   cfgEnv.Model,
    })
})

handlers/chat.go真正做 session 处理ChatHandler.Index 方法从未被注册到任何路由,是死代码。

后果:

  • GET / 响应没有 Set-Cookie 头
  • 每次请求 session 里都拿不到 current_context,handler 在 Chat / ListContexts 等地方继续 CreateContext()ContextManager.Contexts
    无上限增长
  • 浏览器刷新一次就丢一次会话历史

修复

  • main.go:把 / 路由改成 r.GET("/", h.Index),删掉重复的匿名函数和不再使用的 net/http import
  • handlers/chat.go::Index:把 TimeoutSecond / OllamaModel 模板变量补回来(保持页面行为不变),同时给 session.Save()
    加上错误日志(之前是静默忽略)

验证

本地启动 server + frontend 后实测:

$ curl -s -i http://127.0.0.1:8080/ | grep Set-Cookie
Set-Cookie: llm_session=MTc3Nzg5OTc1Nnx...; Path=/; Max-Age=2592000

# 用同一份 cookie 连续请求 3 次
$ curl -s -b cookies.txt http://127.0.0.1:8080/api/context/list
{"contexts":["0"],"current":"0"}

修复前同样的 3 次请求会创建 3 个 context;修复后始终只有 1 个,会话语义正确。

Closes #1086


@Alanxtl

Alanxtl commented May 5, 2026

Copy link
Copy Markdown
Member

merge to main to fix ci fail

…ndex

The "/" route was registered with an inline anonymous function that
just rendered the template without touching the session, while
ChatHandler.Index — the method that actually reads/creates the session
context and calls session.Save() — was never wired up. As a result:

- The first GET / never returned a Set-Cookie header.
- Every subsequent request still found no `current_context` in the
  session and created a brand-new context, growing
  ContextManager.Contexts unbounded and discarding conversation
  history on every page reload.

Fix:

- Use h.Index for the GET / route in main.go and drop the now-unused
  net/http import.
- Move the template variables (TimeoutSecond, OllamaModel) into
  ChatHandler.Index so the page still renders with the same data.
- Check the error from session.Save() and log it; previously it was
  silently dropped.

Verified manually:

  $ curl -s -i http://127.0.0.1:8080/ | grep Set-Cookie
  Set-Cookie: llm_session=...; Path=/; Max-Age=2592000

  # 3 sequential requests reusing the same cookie
  $ curl -s -b cookies.txt http://127.0.0.1:8080/api/context/list
  {"contexts":["0"],"current":"0"}

Closes apache#1086
@Harry33t Harry33t force-pushed the fix/1086-book-flight-session-cookie branch from 7a51dcc to ac9a696 Compare May 9, 2026 14:24
@Alanxtl Alanxtl merged commit 7e1979b into apache:main May 10, 2026
2 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.

[book-flight-ai-agent] 前端未返回 Set-Cookie,可能导致 context 无法跨请求保持

2 participants