説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

session_routes.go 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package routes
  2. import (
  3. "context"
  4. "git.x2erp.com/qdy/go-base/ctx"
  5. "git.x2erp.com/qdy/go-base/model/response"
  6. "git.x2erp.com/qdy/go-base/webx/router"
  7. "git.x2erp.com/qdy/go-svc-code/internal/opencode"
  8. )
  9. // SessionCreateRequest 创建会话请求
  10. type SessionCreateRequest struct {
  11. Title string `json:"title" binding:"required"`
  12. }
  13. // SessionResponse 会话响应
  14. type SessionResponse struct {
  15. ID string `json:"id"`
  16. Title string `json:"title"`
  17. Port int `json:"port"`
  18. BaseURL string `json:"baseURL"`
  19. }
  20. // RegisterSessionRoutes 注册会话管理路由
  21. func RegisterSessionRoutes(ws *router.RouterService, client opencode.OpenCodeClient) {
  22. // 创建会话
  23. ws.POST("/api/session/create",
  24. func(req *SessionCreateRequest, ctx context.Context, reqCtx *ctx.RequestContext) (*response.QueryResult[SessionResponse], error) {
  25. session, err := client.CreateSession(ctx, req.Title)
  26. if err != nil {
  27. return &response.QueryResult[SessionResponse]{
  28. Success: false,
  29. Message: err.Error(),
  30. }, nil
  31. }
  32. return &response.QueryResult[SessionResponse]{
  33. Success: true,
  34. Data: SessionResponse{
  35. ID: session.ID,
  36. Title: session.Title,
  37. Port: client.GetPort(),
  38. BaseURL: client.GetBaseURL(),
  39. },
  40. }, nil
  41. },
  42. ).Desc("创建新的 opencode 会话").Register()
  43. // 获取会话列表
  44. ws.GET("/api/session/list",
  45. func(ctx context.Context, reqCtx *ctx.RequestContext) (*response.QueryResult[[]opencode.Session], error) {
  46. sessions, err := client.ListSessions(ctx)
  47. if err != nil {
  48. return &response.QueryResult[[]opencode.Session]{
  49. Success: false,
  50. Message: err.Error(),
  51. }, nil
  52. }
  53. return &response.QueryResult[[]opencode.Session]{
  54. Success: true,
  55. Data: sessions,
  56. }, nil
  57. },
  58. ).Desc("获取 opencode 会话列表").Register()
  59. // 获取单个会话(暂不实现,因为 opencode API 可能不支持)
  60. // ws.GET("/api/session/get",
  61. // func(ctx context.Context, reqCtx *ctx.RequestContext) (*response.QueryResult[opencode.Session], error) {
  62. // sessionID := reqCtx.GetQuery("id")
  63. // if sessionID == "" {
  64. // return &response.QueryResult[opencode.Session]{
  65. // Success: false,
  66. // Message: "参数 id 不能为空",
  67. // }, nil
  68. // }
  69. // session, err := client.GetSession(ctx, sessionID)
  70. // if err != nil {
  71. // return &response.QueryResult[opencode.Session]{
  72. // Success: false,
  73. // Message: err.Error(),
  74. // }, nil
  75. // }
  76. //
  77. // return &response.QueryResult[opencode.Session]{
  78. // Success: true,
  79. // Data: *session,
  80. // }, nil
  81. // },
  82. // ).Desc("获取 opencode 会话详情").Register()
  83. }