Skip to content

Comments

feat: auto save sessions before next user input#94

Merged
lifeizhou-ap merged 4 commits intomainfrom
lifei/auto-save-sessions
Sep 26, 2024
Merged

feat: auto save sessions before next user input#94
lifeizhou-ap merged 4 commits intomainfrom
lifei/auto-save-sessions

Conversation

@lifeizhou-ap
Copy link
Collaborator

@lifeizhou-ap lifeizhou-ap commented Sep 25, 2024

Why
Currently we only save the session file when user exits the goose successfully. When user exits goose unexpectedly, the session file is not saved.

What

  • Save the session file every time before prompting for user input.
  • Add the optional argument name for session start. If the name is not specified, use an auto-generated name as session name

Note
Added the log level option to session resume, and session planned. This is not relevant to this feature. It is a followup of #80

self.exchange = build_exchange(profile=load_profile(profile), notifier=self.notifier)
setup_logging(log_file_directory=LOG_PATH, log_level=log_level)

if name is not None and self.session_file_path.exists():
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extracted to _get_initial_messages function

@lifeizhou-ap lifeizhou-ap marked this pull request as ready for review September 25, 2024 12:15
@codefromthecrypt
Copy link
Collaborator

so is this a fix to #89?

@lamchau
Copy link
Contributor

lamchau commented Sep 25, 2024

could we merge #76 before we land this change (i don't have the ability to merge to main)? it'll add autocomplete to session selection :)

@lifeizhou-ap
Copy link
Collaborator Author

could we merge #76 before we land this change (i don't have the ability to merge to main)? it'll add autocomplete to session selection :)

Merged PR. Thanks for the heads-up!

* main:
  feat: add shell-completions subcommand (#76)
  chore: update readme! (#96)
  chore: update docs again (#77)
  fix: remove overly general match for long running commands (#87)
# rewind to right before the last user message
self.exchange.rewind()
print(traceback.format_exc())
print(traceback.lformat_exc())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting this! 🙇



def save_latest_session(file_path: Path, messages: List[Message]) -> None:
with tempfile.NamedTemporaryFile("w", delete=False) as temp_file:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: curious why going through the tempfile here? could do file_path.write_text

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using tempfile is to ensure atomic operation while writing file. It prevents from incomplete writes, losing original file dataduring the unexpected interruption or concurrency update

I guess the chance of unexpected scenario is small. However, the file could contain 100000 tokens (around 400KB). It would be safe to use tempfile to prevent from the unexpected behaviours

user_input = self.prompt_session.get_user_input()
message = Message.user(text=user_input.text) if user_input.to_continue() else None

self.save_session()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im not totally sure this is better, but if the session has been saving as current_session, this is maybe a good time to change it's name using the old dialog? otherwise i think we'll be constantly overwriting sessions

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just saw the existing discussion in #89 - even better to generate a name at the start and save to that (but avoid current session which will cause overwrites)

@lifeizhou-ap
Copy link
Collaborator Author

lifeizhou-ap commented Sep 26, 2024

so is this a fix to #89?

Yes, it is related to. This PR contains 2 parts:

  1. auto-save sessions

  2. allow user to specify session name so that we can create/read the file with the session name at the beginning of the session. More info in the comments here

I was about to mention this issue in the PR, but forgot! Thanks for the reminder!

@codefromthecrypt
Copy link
Collaborator

@lifeizhou-ap so if I specify the session name as an arg, and I "exit" the prompt, no more duplicate prompting after this change? or do we still have that.

@lifeizhou-ap lifeizhou-ap merged commit 6065125 into main Sep 26, 2024
@lifeizhou-ap
Copy link
Collaborator Author

lifeizhou-ap commented Sep 26, 2024

@lifeizhou-ap so if I specify the session name as an arg, and I "exit" the prompt, no more duplicate prompting after this change? or do we still have that.

There will be no more prompting at the end of the session. At the beginning of the session, we show a message about the location of the session file.

Screenshot 2024-09-26 at 12 03 47 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants