Repository: GoogleCloudPlatform/kubectl-ai
File: pkg/agent/conversation.go (Go)

Code before:

		// Add ```text so markdown doesn't wreck the format
		availableSessions := "```text"
		availableSessions += "Available sessions:\n\n"
		availableSessions += "ID\t\t\tCreated\t\t\tLast Accessed\t\tModel\t\tProvider\n"
		availableSessions += "--\t\t\t-------\t\t\t-------------\t\t-----\t\t--------\n"

		for _, session := range sessionList {
			metadata, err := session.LoadMetadata()
			if err != nil {
				availableSessions += fmt.Sprintf("%s\t\t<error loading metadata>\n", session.ID)
				continue
			}

			availableSessions += fmt.Sprintf("%s\t%s\t%s\t%s\t%s\n",
				session.ID,
				metadata.CreatedAt.Format("2006-01-02 15:04"),
				metadata.LastAccessed.Format("2006-01-02 15:04"),
				metadata.ModelID,
				metadata.ProviderID)
		}
		// close the ```text box
		availableSessions += "```"
		return availableSessions, true, nil
	}

	return "", false, nil
}

func (c *Agent) SaveSession() (string, error) {
	c.sessionMu.Lock()
	defer c.sessionMu.Unlock()

	manager, err := sessions.NewSessionManager()
	if err != nil {
		return "", fmt.Errorf("failed to create session manager: %w", err)
	}
	foundSession, _ := manager.FindSessionByID(c.session.ID)
	if foundSession != nil {
		return "", fmt.Errorf("session with ID %s already exists", foundSession.ID)
	}
	metadata := sessions.Metadata{
		CreatedAt:    time.Now(),
		LastAccessed: time.Now(),
		ModelID:      c.Model,
		ProviderID:   c.Provider,
	}
	newSession, err := manager.NewSession(metadata)
	if err != nil {
		return "", fmt.Errorf("failed to create new session: %w", err)
	}

Review comment (suggestion):
if user is already in a session that being persisted and they say "save-session" just to ensure that session is being saved, will that result in error ? (probably, we should respond with a more friendly message saying.... session is already being saved or something....)

Code after:
		}
		// close the ```text box
		availableSessions += "```"
		return availableSessions, true, nil
	}

	if strings.HasPrefix(query, "resume-session") {
		parts := strings.Split(query, " ")
		if len(parts) != 2 {
			return "Invalid command. Usage: resume-session <session_id>", true, nil
		}
		sessionID := parts[1]
		if err := c.loadSession(sessionID); err != nil {
			return "", false, err
		}
		return fmt.Sprintf("Resumed session %s.", sessionID), true, nil
	}

	return "", false, nil
}

func (c *Agent) SaveSession() (string, error) {
	c.sessionMu.Lock()
	defer c.sessionMu.Unlock()

	manager, err := sessions.NewSessionManager()
	if err != nil {
		return "", fmt.Errorf("failed to create session manager: %w", err)
	}
	foundSession, _ := manager.FindSessionByID(c.session.ID)
	if foundSession != nil {
		return foundSession.ID, nil
	}
	metadata := sessions.Metadata{
		CreatedAt:    c.session.CreatedAt,
		LastAccessed: time.Now(),
		ModelID:      c.Model,
		ProviderID:   c.Provider,
	}
	newSession, err := manager.NewSession(metadata)
	if err != nil {
		return "", fmt.Errorf("failed to create new session: %w", err)
	}

	messages := c.ChatMessageStore.ChatMessages()
	if err := newSession.SetChatMessages(messages); err != nil {
		return "", fmt.Errorf("failed to save chat messages to new session: %w", err)
	}

	c.ChatMessageStore = newSession
	c.session.ChatMessageStore = newSession