From 48fe81d3c4be7039a7364014425a414a3d0366dd Mon Sep 17 00:00:00 2001 From: juzeon Date: Sat, 20 Jan 2024 13:26:54 +0800 Subject: [PATCH] add ShareGPT --- app.go | 56 +++++++++++++++++++ .../src/components/index/Conversation.vue | 2 + .../src/components/index/WorkspaceNav.vue | 11 +++- frontend/wailsjs/go/main/App.d.ts | 2 + frontend/wailsjs/go/main/App.js | 4 ++ 5 files changed, 73 insertions(+), 2 deletions(-) diff --git a/app.go b/app.go index 3e1fa0e..843e73f 100644 --- a/app.go +++ b/app.go @@ -364,3 +364,59 @@ func (a *App) ExportWorkspace(id int) error { } return os.WriteFile(filePath, out.Bytes(), 0644) } + +type ShareGPTRequest struct { + Title string `json:"title"` + Items []ShareGPTItem `json:"items"` +} +type ShareGPTItem struct { + From string `json:"from"` + Value string `json:"value"` +} +type ShareGPTResponse struct { + Message string `json:"message"` // on error + ID string `json:"id"` // on success +} + +func (a *App) ShareWorkspace(id int) error { + workspace, ok := lo.Find(a.settings.config.Workspaces, func(item Workspace) bool { + return item.ID == id + }) + if !ok { + return errors.New("workspace not exist by id: " + strconv.Itoa(id)) + } + hClient, err := util.MakeHTTPClient(a.settings.config.Proxy, 0) + if err != nil { + return err + } + client := resty.New().SetTimeout(5 * time.Second).SetTransport(hClient.Transport) + resp, err := client.R().SetBody(ShareGPTRequest{ + Title: workspace.Title, + Items: lo.Map(util.GetChatMessage(workspace.Context), func(item util.ChatMessage, index int) ShareGPTItem { + from := "gpt" + if item.Role == "user" { + from = "human" + } + return ShareGPTItem{ + From: from, + Value: fmt.Sprintf("[%s](#%s)\n%s", item.Role, item.Type, item.Content), + } + }), + }).Post("https://sharegpt.com/api/conversations") + if err != nil { + return err + } + if resp.IsError() { + return errors.New("error status code: " + resp.Status()) + } + var response ShareGPTResponse + err = json.Unmarshal(resp.Body(), &response) + if err != nil { + return err + } + if response.Message != "" { + return errors.New("error posting conversation: " + response.Message) + } + err = util.OpenURL("https://sharegpt.com/c/" + response.ID) + return nil +} diff --git a/frontend/src/components/index/Conversation.vue b/frontend/src/components/index/Conversation.vue index bc6962d..f67e791 100644 --- a/frontend/src/components/index/Conversation.vue +++ b/frontend/src/components/index/Conversation.vue @@ -15,6 +15,7 @@ let emit = defineEmits<{ (e: 'edit'): void, (e: 'click'): void, (e: 'export'): void + (e: 'share'): void }>() @@ -31,6 +32,7 @@ let emit = defineEmits<{ + { + swal.error(err) + }) +}
diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 1f7a4e0..8b86e64 100644 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -23,6 +23,8 @@ export function GetUser():Promise; export function SaveRemoteJPEGImage(arg1:string):Promise; +export function ShareWorkspace(arg1:number):Promise; + export function UploadDocument():Promise; export function UploadSydneyImage():Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index 03bc33f..1018a70 100644 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -42,6 +42,10 @@ export function SaveRemoteJPEGImage(arg1) { return window['go']['main']['App']['SaveRemoteJPEGImage'](arg1); } +export function ShareWorkspace(arg1) { + return window['go']['main']['App']['ShareWorkspace'](arg1); +} + export function UploadDocument() { return window['go']['main']['App']['UploadDocument'](); }