generated from LordPax/cli-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk) : add parameter sdk for text command
* create history file per sdk
- Loading branch information
Showing
11 changed files
with
193 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,123 @@ | ||
package sdk | ||
|
||
import ( | ||
"encoding/json" | ||
"os" | ||
"path" | ||
|
||
"github.com/LordPax/aicli/config" | ||
"github.com/LordPax/aicli/utils" | ||
) | ||
|
||
type ITextHistory interface { | ||
AppendHistory(role string, text ...string) Message | ||
SaveHistory() error | ||
LoadHistory() error | ||
GetHistory() []Message | ||
SetSelectedHistory(name string) | ||
GetSelectedHistory() string | ||
ClearHistory() | ||
} | ||
|
||
type TextHistory struct { | ||
History map[string][]Message | ||
SelectedHistory string | ||
HistoryFile string | ||
} | ||
|
||
func NewTextHistory(sdk string) (*TextHistory, error) { | ||
historyFile := path.Join(config.CONFIG_DIR, sdk+"-history.json") | ||
log, err := utils.GetLog() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if !utils.FileExist(historyFile) { | ||
if err := os.WriteFile(historyFile, []byte(config.HISTORY_CONTENT), 0644); err != nil { | ||
return nil, err | ||
} | ||
log.Logf("History file created at %s\n", historyFile) | ||
} | ||
|
||
history := &TextHistory{ | ||
History: make(map[string][]Message), | ||
SelectedHistory: "default", | ||
HistoryFile: historyFile, | ||
} | ||
|
||
return history, nil | ||
} | ||
|
||
func (t *TextHistory) SetSelectedHistory(name string) { | ||
t.SelectedHistory = name | ||
} | ||
|
||
func (t *TextHistory) GetSelectedHistory() string { | ||
return t.SelectedHistory | ||
} | ||
|
||
func (t *TextHistory) AppendHistory(role string, text ...string) Message { | ||
var content []Content | ||
|
||
for _, t := range text { | ||
content = append(content, Content{ | ||
Type: "text", | ||
Text: t, | ||
}) | ||
} | ||
|
||
name := t.SelectedHistory | ||
message := Message{ | ||
Role: role, | ||
Content: content, | ||
} | ||
t.History[name] = append(t.History[name], message) | ||
|
||
return message | ||
} | ||
|
||
func (t *TextHistory) SaveHistory() error { | ||
f, err := os.Create(t.HistoryFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
jsonHistory, err := json.Marshal(t.History) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := f.Write(jsonHistory); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *TextHistory) LoadHistory() error { | ||
f, err := os.ReadFile(t.HistoryFile) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if len(f) == 0 { | ||
return nil | ||
} | ||
|
||
if err := json.Unmarshal(f, &t.History); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (t *TextHistory) ClearHistory() { | ||
name := t.SelectedHistory | ||
t.History[name] = []Message{} | ||
} | ||
|
||
func (t *TextHistory) GetHistory() []Message { | ||
name := t.SelectedHistory | ||
return t.History[name] | ||
} |
This file contains 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
This file contains 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.