Skip to content

Commit

Permalink
echo chat
Browse files Browse the repository at this point in the history
  • Loading branch information
pwh-pwh committed Jul 25, 2024
1 parent 23e52ec commit f732e49
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 14 deletions.
10 changes: 9 additions & 1 deletion app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ package main
import (
"context"
"fmt"
"wailsdemo/service"
"wailsdemo/types"
)

// App struct
type App struct {
ctx context.Context
ctx context.Context
chat service.Chat
}

// NewApp creates a new App application struct
Expand All @@ -19,10 +22,15 @@ func NewApp() *App {
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
a.chat = &service.EchoChat{}
InitWithApp()
}

// Greet returns a greeting for the given name
func (a *App) Greet(name string) string {
return fmt.Sprintf("Hello %s, It's show time!", name)
}

func (a *App) DoChat(msg []types.Message) string {
return a.chat.Dochat(msg)
}
29 changes: 22 additions & 7 deletions frontend/src/components/Chat.vue
Original file line number Diff line number Diff line change
@@ -1,32 +1,47 @@
<script setup lang="ts">
import MsgItem from "./MsgItem.vue";
import {nextTick, onMounted, reactive, ref, watch} from "vue";
import {Message} from "../types/Message";
import {nextTick, onMounted, reactive, ref, toRaw, watch} from "vue";
import ScrollPanel from "primevue/scrollpanel";
import {BrowserOpenURL, Quit} from "../../wailsjs/runtime";
import {is} from "@babel/types";
import {DoChat} from "../../wailsjs/go/main/App";
import {types} from "../../wailsjs/go/models";
import Message = types.Message;
const msgList = reactive<Message[]>([{content: 'hello', userType: 'user', id: '1'},
{content: 'how are you', userType: 'assistant', id: '2'}])
const inputMsg = ref('')
const isShrink = ref(false)
const helpMsg = ref('/help: show this message\n/clear: clear messages\n')
const doChat = () => {
if (!dispatchMsg(inputMsg.value)) {
inputMsg.value = ''
return
}
//生成id
const id = Date.now().toString(16)
msgList.push({content: inputMsg.value, userType: 'user', id: id})
const cList = msgList.filter(msg => msg.id !== 'tool')
msgList.push({content: '正在思考...', userType: 'assistant', id: id})
DoChat(cList).then(res => {
msgList[msgList.length - 1].content = res
})
inputMsg.value = ''
}
const dispatchMsg = (msg: string):boolean => {
if (msg === '/clear') {
msgList.length = 0
return false
switch (msg) {
case '/clear':
msgList.length = 0
return false
case '/help':
msgList.push({content: inputMsg.value, userType: 'user', id: 'tool'})
msgList.push({content: helpMsg.value, userType: 'assistant', id: 'tool'})
return false
default:
return true
}
return true
}
const scrollPanelRef = ref()
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/MsgItem.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<script setup lang="ts">
import {computed} from "vue";
import {Message} from "../types/Message";
import Avatar from 'primevue/avatar';
import {types} from "../../wailsjs/go/models";
import Message = types.Message;
const props = defineProps<{ message: Message }>()
Expand Down
5 changes: 0 additions & 5 deletions frontend/src/types/Message.ts

This file was deleted.

3 changes: 3 additions & 0 deletions frontend/wailsjs/go/main/App.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT
import {types} from '../models';

export function DoChat(arg1:Array<types.Message>):Promise<string>;

export function Greet(arg1:string):Promise<string>;
4 changes: 4 additions & 0 deletions frontend/wailsjs/go/main/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// Cynhyrchwyd y ffeil hon yn awtomatig. PEIDIWCH Â MODIWL
// This file is automatically generated. DO NOT EDIT

export function DoChat(arg1) {
return window['go']['main']['App']['DoChat'](arg1);
}

export function Greet(arg1) {
return window['go']['main']['App']['Greet'](arg1);
}
21 changes: 21 additions & 0 deletions frontend/wailsjs/go/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export namespace types {

export class Message {
id: string;
content: string;
userType: string;

static createFrom(source: any = {}) {
return new Message(source);
}

constructor(source: any = {}) {
if ('string' === typeof source) source = JSON.parse(source);
this.id = source["id"];
this.content = source["content"];
this.userType = source["userType"];
}
}

}

7 changes: 7 additions & 0 deletions service/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package service

import "wailsdemo/types"

type Chat interface {
Dochat([]types.Message) string
}
10 changes: 10 additions & 0 deletions service/echochat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package service

import "wailsdemo/types"

type EchoChat struct {
}

func (e *EchoChat) Dochat(messages []types.Message) string {
return messages[len(messages)-1].Content
}
7 changes: 7 additions & 0 deletions types/msg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package types

type Message struct {
Id string `json:"id"`
Content string `json:"content"`
UserType string `json:"userType"`
}

0 comments on commit f732e49

Please sign in to comment.