Skip to content

Commit

Permalink
add ShareGPT
Browse files Browse the repository at this point in the history
  • Loading branch information
juzeon committed Jan 20, 2024
1 parent 0df42e9 commit 48fe81d
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
56 changes: 56 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions frontend/src/components/index/Conversation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let emit = defineEmits<{
(e: 'edit'): void,
(e: 'click'): void,
(e: 'export'): void
(e: 'share'): void
}>()
</script>

Expand All @@ -31,6 +32,7 @@ let emit = defineEmits<{
</v-card-text>
<v-card-actions>
<v-spacer></v-spacer>
<v-btn @click="emit('share')" color="primary" density="comfortable" icon="mdi-share-variant"></v-btn>
<v-btn @click="emit('export')" color="primary" density="comfortable" icon="mdi-export"></v-btn>
<v-btn @click="emit('edit')" color="primary" density="compact" icon="mdi-pencil"></v-btn>
<v-btn @click="emit('delete')" color="primary" :disabled="disabled ?? false" density="compact"
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/components/index/WorkspaceNav.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {main, sydney} from "../../../wailsjs/go/models"
import {generateRandomName, swal} from "../../helper"
import dayjs from "dayjs"
import {computed, ref} from "vue"
import {ExportWorkspace} from "../../../wailsjs/go/main/App"
import {ExportWorkspace, ShareWorkspace} from "../../../wailsjs/go/main/App"
import Workspace = main.Workspace
import Preset = main.Preset
import GenerateImageResult = sydney.GenerateImageResult
Expand Down Expand Up @@ -102,6 +102,12 @@ function exportWorkspace(workspace: Workspace) {
swal.error(err)
})
}
function shareWorkspace(workspace: Workspace) {
ShareWorkspace(workspace.id).catch(err => {
swal.error(err)
})
}
</script>

<template>
Expand All @@ -114,7 +120,8 @@ function exportWorkspace(workspace: Workspace) {
<conversation :title="workspace.title" :created-at="workspace.created_at"
:active="workspace.id===currentWorkspace.id" :disabled="isAsking"
@delete="onDeleteWorkspace(workspace)" @edit="onEditWorkspace(workspace)"
@click="switchWorkspace(workspace)" @export="exportWorkspace(workspace)"></conversation>
@click="switchWorkspace(workspace)" @export="exportWorkspace(workspace)"
@share="shareWorkspace(workspace)"></conversation>
</template>
</v-virtual-scroll>
<div class="d-flex ma-3">
Expand Down
2 changes: 2 additions & 0 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export function GetUser():Promise<string>;

export function SaveRemoteJPEGImage(arg1:string):Promise<void>;

export function ShareWorkspace(arg1:number):Promise<void>;

export function UploadDocument():Promise<main.UploadSydneyDocumentResult>;

export function UploadSydneyImage():Promise<main.UploadSydneyImageResult>;
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']();
}
Expand Down

0 comments on commit 48fe81d

Please sign in to comment.