Skip to content

Commit

Permalink
Rename persisted threads
Browse files Browse the repository at this point in the history
Currently threads are persisted by generating a filename based on the
CRC of the name of the thread. However, if there are different threads
with the same name this will cause a collision. This commit appends
the creation timestamp of a thread in order to avoid
collisions. Additionally, it will rename any files using the old
naming scheme to the new one.
  • Loading branch information
mikeb26 committed Oct 30, 2024
1 parent f89d38b commit 780e937
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions cmd/gptcli/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2023 Mike Brown. All Rights Reserved.
/* Copyright © 2023-2024 Mike Brown. All Rights Reserved.
*
* See LICENSE file at the root of this package for license terms
*/
Expand Down Expand Up @@ -172,11 +172,18 @@ func (gptCliCtx *GptCliContext) loadThreads() error {
if err != nil {
return fmt.Errorf("Failed to parse %v: %w", fullpath, err)
}
fileName := fmt.Sprintf("%v.json",
strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(thread.Name))), 16))
fileName := genUniqFileName(thread.Name, thread.CreateTime)
thread.filePath = filepath.Join(gptCliCtx.threadsDir, fileName)
thread.archiveFilePath = filepath.Join(gptCliCtx.archiveDir, fileName)

if fileName != dEnt.Name() {
oldPath := filepath.Join(gptCliCtx.threadsDir, dEnt.Name())
fmt.Fprintf(os.Stderr, "Renaming thread %v/%v to %v/%v\n",
gptCliCtx.threadsDir, dEnt.Name(), gptCliCtx.threadsDir, fileName)
_ = os.Remove(oldPath)
_ = thread.save()
}

gptCliCtx.threads = append(gptCliCtx.threads, &thread)
gptCliCtx.totThreads++
}
Expand Down Expand Up @@ -605,6 +612,12 @@ func printStringViaPager(content string) error {
return nil
}

func genUniqFileName(name string, cTime time.Time) string {
return fmt.Sprintf("%v_%v.json",
strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(name))), 16),
cTime.Unix())
}

func newThreadMain(ctx context.Context, gptCliCtx *GptCliContext,
args []string) error {

Expand All @@ -618,8 +631,8 @@ func newThreadMain(ctx context.Context, gptCliCtx *GptCliContext,
return err
}
name = strings.TrimSpace(name)
fileName := fmt.Sprintf("%v.json",
strconv.FormatUint(uint64(crc32.ChecksumIEEE([]byte(name))), 16))
cTime := time.Now()
fileName := genUniqFileName(name, cTime)
filePath := filepath.Join(gptCliCtx.threadsDir, fileName)
archiveFilePath := filepath.Join(gptCliCtx.archiveDir, fileName)

Expand All @@ -629,9 +642,9 @@ func newThreadMain(ctx context.Context, gptCliCtx *GptCliContext,

curThread := &GptCliThread{
Name: name,
CreateTime: time.Now(),
AccessTime: time.Now(),
ModTime: time.Now(),
CreateTime: cTime,
AccessTime: cTime,
ModTime: cTime,
Dialogue: dialogue,
SummaryDialogue: make([]openai.ChatCompletionMessage, 0),
filePath: filePath,
Expand Down

0 comments on commit 780e937

Please sign in to comment.