| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package routes
-
- import (
- "context"
-
- "git.x2erp.com/qdy/go-base/ctx"
- "git.x2erp.com/qdy/go-base/model/response"
- "git.x2erp.com/qdy/go-base/webx/router"
- "git.x2erp.com/qdy/go-svc-code/internal/opencode"
- )
-
- // SessionCreateRequest 创建会话请求
- type SessionCreateRequest struct {
- Title string `json:"title" binding:"required"`
- }
-
- // SessionResponse 会话响应
- type SessionResponse struct {
- ID string `json:"id"`
- Title string `json:"title"`
- Port int `json:"port"`
- BaseURL string `json:"baseURL"`
- }
-
- // RegisterSessionRoutes 注册会话管理路由
- func RegisterSessionRoutes(ws *router.RouterService, client opencode.OpenCodeClient) {
- // 创建会话
- ws.POST("/api/session/create",
- func(req *SessionCreateRequest, ctx context.Context, reqCtx *ctx.RequestContext) (*response.QueryResult[SessionResponse], error) {
- session, err := client.CreateSession(ctx, req.Title)
- if err != nil {
- return &response.QueryResult[SessionResponse]{
- Success: false,
- Message: err.Error(),
- }, nil
- }
-
- return &response.QueryResult[SessionResponse]{
- Success: true,
- Data: SessionResponse{
- ID: session.ID,
- Title: session.Title,
- Port: client.GetPort(),
- BaseURL: client.GetBaseURL(),
- },
- }, nil
- },
- ).Desc("创建新的 opencode 会话").Register()
-
- // 获取会话列表
- ws.GET("/api/session/list",
- func(ctx context.Context, reqCtx *ctx.RequestContext) (*response.QueryResult[[]opencode.Session], error) {
- sessions, err := client.ListSessions(ctx)
- if err != nil {
- return &response.QueryResult[[]opencode.Session]{
- Success: false,
- Message: err.Error(),
- }, nil
- }
-
- return &response.QueryResult[[]opencode.Session]{
- Success: true,
- Data: sessions,
- }, nil
- },
- ).Desc("获取 opencode 会话列表").Register()
-
- // 获取单个会话(暂不实现,因为 opencode API 可能不支持)
- // ws.GET("/api/session/get",
- // func(ctx context.Context, reqCtx *ctx.RequestContext) (*response.QueryResult[opencode.Session], error) {
- // sessionID := reqCtx.GetQuery("id")
- // if sessionID == "" {
- // return &response.QueryResult[opencode.Session]{
- // Success: false,
- // Message: "参数 id 不能为空",
- // }, nil
- // }
- // session, err := client.GetSession(ctx, sessionID)
- // if err != nil {
- // return &response.QueryResult[opencode.Session]{
- // Success: false,
- // Message: err.Error(),
- // }, nil
- // }
- //
- // return &response.QueryResult[opencode.Session]{
- // Success: true,
- // Data: *session,
- // }, nil
- // },
- // ).Desc("获取 opencode 会话详情").Register()
- }
|