-
Notifications
You must be signed in to change notification settings - Fork 9.5k
feat[Go]: implement agent session/download/logs/get-agent APIs #15661
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hunnyboy1217
wants to merge
1
commit into
infiniflow:main
Choose a base branch
from
hunnyboy1217:feat/go-agent-session-download-logs-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // | ||
| // Copyright 2026 The InfiniFlow Authors. All Rights Reserved. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| package dao | ||
|
|
||
| import ( | ||
| "ragflow/internal/entity" | ||
| ) | ||
|
|
||
| // AgentSessionDAO data access for agent sessions (conversation table, dialog_id = canvas id). | ||
| type AgentSessionDAO struct{} | ||
|
|
||
| // NewAgentSessionDAO creates an AgentSessionDAO. | ||
| func NewAgentSessionDAO() *AgentSessionDAO { | ||
| return &AgentSessionDAO{} | ||
| } | ||
|
|
||
| // GetByID returns a single agent session by conversation ID. | ||
| func (d *AgentSessionDAO) GetByID(id string) (*entity.AgentSession, error) { | ||
| var s entity.AgentSession | ||
| err := DB.Where("id = ?", id).First(&s).Error | ||
| return &s, err | ||
| } | ||
|
|
||
| // ListByAgentID returns all sessions for a given agent (canvas) ID, ordered newest first. | ||
| func (d *AgentSessionDAO) ListByAgentID(agentID string) ([]*entity.AgentSession, error) { | ||
| var sessions []*entity.AgentSession | ||
| err := DB.Where("dialog_id = ?", agentID). | ||
| Order("create_time DESC"). | ||
| Find(&sessions).Error | ||
| return sessions, err | ||
| } | ||
|
|
||
| // ListByAgentIDPaged returns paginated sessions for a given agent ID. | ||
| func (d *AgentSessionDAO) ListByAgentIDPaged( | ||
| agentID string, | ||
| page, pageSize int, | ||
| orderby string, | ||
| desc bool, | ||
| sessionID, userID, keywords string, | ||
| ) ([]*entity.AgentSession, int64, error) { | ||
| base := DB.Model(&entity.AgentSession{}).Where("dialog_id = ?", agentID) | ||
|
|
||
| if sessionID != "" { | ||
| base = base.Where("id = ?", sessionID) | ||
| } | ||
| if userID != "" { | ||
| base = base.Where("user_id = ?", userID) | ||
| } | ||
| if keywords != "" { | ||
| base = base.Where("name LIKE ?", "%"+keywords+"%") | ||
| } | ||
|
|
||
| var total int64 | ||
| if err := base.Count(&total).Error; err != nil { | ||
| return nil, 0, err | ||
| } | ||
|
|
||
| order := orderby | ||
| if order == "" { | ||
| order = "update_time" | ||
| } | ||
| if desc { | ||
| order += " DESC" | ||
| } else { | ||
| order += " ASC" | ||
| } | ||
|
|
||
| query := base.Order(order) | ||
| if page > 0 && pageSize > 0 { | ||
| query = query.Offset((page - 1) * pageSize).Limit(pageSize) | ||
| } | ||
|
|
||
| var sessions []*entity.AgentSession | ||
| err := query.Find(&sessions).Error | ||
| return sessions, total, err | ||
| } | ||
|
|
||
| // DeleteByID hard-deletes a session by ID. | ||
| func (d *AgentSessionDAO) DeleteByID(id string) error { | ||
| return DB.Unscoped().Where("id = ?", id).Delete(&entity.AgentSession{}).Error | ||
| } | ||
|
|
||
| // GetByIDAndAgentID returns a session only if it belongs to the given agent. | ||
| func (d *AgentSessionDAO) GetByIDAndAgentID(sessionID, agentID string) (*entity.AgentSession, error) { | ||
| var s entity.AgentSession | ||
| err := DB.Where("id = ? AND dialog_id = ?", sessionID, agentID).First(&s).Error | ||
| return &s, err | ||
| } | ||
|
|
||
| // BelongsToAgent checks whether a session belongs to a specific agent (canvas owner). | ||
| func (d *AgentSessionDAO) BelongsToAgent(sessionID, agentID string) (bool, error) { | ||
| var count int64 | ||
| err := DB.Model(&entity.AgentSession{}). | ||
| Where("id = ? AND dialog_id = ?", sessionID, agentID). | ||
| Count(&count).Error | ||
| return count > 0, err | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🌐 Web query:
In GORM v1.25.x, does db.Order(string) treat input as raw SQL, requiring allowlisting/safe column construction for user-provided sort fields?💡 Result:
Yes, in GORM v1.25.x, the db.Order(string) method treats its input as raw SQL and is not automatically sanitized against SQL injection [1][2]. Because SQL standards do not allow column names or directions to be parameterized in an ORDER BY clause, GORM does not provide built-in escaping for this method [3][4]. To securely handle user-provided sort fields, you must use an allowlist approach [1][4]. Never pass raw user input directly into db.Order [4]. Instead, map the user input to a predefined set of safe, hardcoded column names [4]. Example of secure pattern: // SECURE: Use a map to validate allowed columns allowedOrders:= map[string]string{ "id": "id", "name": "name", "price": "price", } if col, ok:= allowedOrders[userInput]; ok { db.Order(col + " asc").Find(&products) } else { // Default fallback db.Order("id asc").Find(&products) } The official GORM security documentation explicitly lists db.Order among the methods that are not escaped and requires developer caution to avoid SQL injection [1].
Citations:
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 2190
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 16829
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 9598
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 8416
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 9737
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 119
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 428
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 4439
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 3139
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 4482
🏁 Script executed:
Repository: infiniflow/ragflow
Length of output: 87
Prevent SQL injection from user-controlled
orderbyinAgentSessionDAO.ListByAgentIDPaged.internal/dao/agent_session.gobuildsorder := orderby(only defaulting when empty) and then callsquery := base.Order(order), so theorderbyquery param reaches GORM as rawORDER BYSQL (not identifier-escaped) viainternal/handler/agent.go→ service → DAO.Fix: allowlist
orderbyto a fixed set of permitted column names (e.g.,id,name,create_time,update_time, etc.) and constructORDER BY <mapped_column> <ASC|DESC>using only the mapped value and thedescboolean.🤖 Prompt for AI Agents