Skip to content

Commit fd2dc8f

Browse files
authored
Merge pull request #1549 from trheyi/main
feat(chat): add last_workspace tracking and update session management
2 parents 5ec6add + cb14e58 commit fd2dc8f

9 files changed

Lines changed: 521 additions & 398 deletions

File tree

agent/assistant/chat.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,12 @@ func (ast *Assistant) FlushBuffer(ctx *agentcontext.Context, finalStatus string,
238238
if mode := ctx.Buffer.Mode(); mode != "" {
239239
updates["last_mode"] = mode
240240
}
241+
// Also update last_workspace if available in metadata
242+
if ctx.Metadata != nil {
243+
if ws, ok := ctx.Metadata["workspace_id"].(string); ok && ws != "" {
244+
updates["last_workspace"] = ws
245+
}
246+
}
241247
if updateErr := chatStore.UpdateChat(ctx.ChatID, updates); updateErr != nil {
242248
ctx.Logger.Debug("Failed to update chat: %v", updateErr)
243249
}
@@ -359,6 +365,13 @@ func (ast *Assistant) EnsureChat(ctx *agentcontext.Context) error {
359365
chat.LastConnector = ctx.Stack.Options.Connector
360366
}
361367

368+
// Set last_workspace from metadata
369+
if ctx.Metadata != nil {
370+
if ws, ok := ctx.Metadata["workspace_id"].(string); ok && ws != "" {
371+
chat.LastWorkspace = ws
372+
}
373+
}
374+
362375
// Set permission fields from authorized info
363376
if ctx.Authorized != nil {
364377
chat.CreatedBy = ctx.Authorized.UserID

agent/sandbox/v2/claude/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (r *Runner) buildCommand(ctx context.Context, req *types.StreamRequest, p p
7373

7474
var isContinuation bool
7575
if chatID != "" {
76-
storeKey := "claude-session:" + assistantID + ":" + chatID
76+
storeKey := "claude-session:" + assistantID + ":" + chatID + ":" + req.Config.WorkspaceID
7777
isContinuation = chatSessionExists(storeKey)
7878
} else {
7979
isContinuation = hasExistingSession(ctx, computer, p, assistantID)

agent/sandbox/v2/claude/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (r *Runner) Stream(ctx context.Context, req *types.StreamRequest, handler m
162162
// Claude CLI creates session files on disk at startup, so subsequent
163163
// requests must use --resume (not --session-id) even if this stream fails.
164164
if chatID != "" {
165-
storeKey := "claude-session:" + assistantID + ":" + chatID
165+
storeKey := "claude-session:" + assistantID + ":" + chatID + ":" + req.Config.WorkspaceID
166166
sessionUUID := chatIDToSessionUUID(assistantID, chatID)
167167
markChatSession(storeKey, sessionUUID, 90*24*time.Hour)
168168
}

agent/sandbox/v2/opencode/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (r *Runner) buildCommand(req *types.StreamRequest, p platform, attachmentPa
5959

6060
var isContinuation bool
6161
if chatID != "" {
62-
storeKey := "opencode-session:" + assistantID + ":" + chatID
62+
storeKey := "opencode-session:" + assistantID + ":" + chatID + ":" + req.Config.WorkspaceID
6363
isContinuation = chatSessionExists(storeKey)
6464
}
6565

agent/sandbox/v2/opencode/runner.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (r *Runner) Stream(ctx context.Context, req *types.StreamRequest, handler m
153153
// Write system prompt if this is the first turn.
154154
assistantID := req.AssistantID
155155
chatID := req.ChatID
156-
storeKey := "opencode-session:" + assistantID + ":" + chatID
156+
storeKey := "opencode-session:" + assistantID + ":" + chatID + ":" + req.Config.WorkspaceID
157157
isContinuation := chatID != "" && chatSessionExists(storeKey)
158158

159159
if !isContinuation && req.SystemPrompt != "" {

agent/store/types/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Chat struct {
3333
AssistantID string `json:"assistant_id"`
3434
LastConnector string `json:"last_connector,omitempty"` // Last used connector ID (updated on each message)
3535
LastMode string `json:"last_mode,omitempty"` // Last used chat mode (updated on each message)
36+
LastWorkspace string `json:"last_workspace,omitempty"` // Last used workspace ID (updated on each message)
3637
Status string `json:"status"` // "active" or "archived"
3738
Public bool `json:"public"` // Whether shared across all teams
3839
Share string `json:"share"` // "private" or "team"

agent/store/xun/chat.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ func (store *Xun) CreateChat(chat *types.Chat) error {
7676
if chat.LastMode != "" {
7777
data["last_mode"] = chat.LastMode
7878
}
79+
if chat.LastWorkspace != "" {
80+
data["last_workspace"] = chat.LastWorkspace
81+
}
7982
if chat.LastMessageAt != nil {
8083
data["last_message_at"] = *chat.LastMessageAt
8184
}
@@ -346,6 +349,7 @@ func (store *Xun) rowToChat(data map[string]interface{}) (*types.Chat, error) {
346349
AssistantID: getString(data, "assistant_id"),
347350
LastConnector: getString(data, "last_connector"),
348351
LastMode: getString(data, "last_mode"),
352+
LastWorkspace: getString(data, "last_workspace"),
349353
Status: getString(data, "status"),
350354
Public: getBool(data, "public"),
351355
Share: getString(data, "share"),

0 commit comments

Comments
 (0)