From 0dc97430017cac773fe229a57e2f3161618c82f4 Mon Sep 17 00:00:00 2001 From: Tom X Nguyen Date: Thu, 29 Aug 2024 17:42:31 +0700 Subject: [PATCH 1/4] feat: add templates feature --- devbox.json | 14 ++++++++ devbox.lock | 53 ++++++++++++++++++++++++++++ pkg/adapter/dify/dify.go | 8 +++-- pkg/adapter/dify/interface.go | 2 +- pkg/discord/command/sum/sum.go | 34 +++++++++++++++--- pkg/discord/service/sum/interface.go | 2 +- pkg/discord/service/sum/sum.go | 11 +++--- pkg/model/sum.go | 7 ++-- 8 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 devbox.json create mode 100644 devbox.lock diff --git a/devbox.json b/devbox.json new file mode 100644 index 0000000..d718e27 --- /dev/null +++ b/devbox.json @@ -0,0 +1,14 @@ +{ + "$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.12.0/.schema/devbox.schema.json", + "packages": ["go@1.23.0"], + "shell": { + "init_hook": [ + "echo 'Welcome to devbox!' > /dev/null" + ], + "scripts": { + "test": [ + "echo \"Error: no test specified\" && exit 1" + ] + } + } +} diff --git a/devbox.lock b/devbox.lock new file mode 100644 index 0000000..01e3e30 --- /dev/null +++ b/devbox.lock @@ -0,0 +1,53 @@ +{ + "lockfile_version": "1", + "packages": { + "go@1.23.0": { + "last_modified": "2024-08-14T11:41:26Z", + "resolved": "github:NixOS/nixpkgs/0cb2fd7c59fed0cd82ef858cbcbdb552b9a33465#go_1_23", + "source": "devbox-search", + "version": "1.23.0", + "systems": { + "aarch64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/x097y3rdlvw6ax1id52xx2didgzxjg34-go-1.23.0", + "default": true + } + ], + "store_path": "/nix/store/x097y3rdlvw6ax1id52xx2didgzxjg34-go-1.23.0" + }, + "aarch64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/znns5qk7fbhjb89bqhabxjxpgiq2ag1a-go-1.23.0", + "default": true + } + ], + "store_path": "/nix/store/znns5qk7fbhjb89bqhabxjxpgiq2ag1a-go-1.23.0" + }, + "x86_64-darwin": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/psym5ggdincwihwd81kssc4gbw1k3p3h-go-1.23.0", + "default": true + } + ], + "store_path": "/nix/store/psym5ggdincwihwd81kssc4gbw1k3p3h-go-1.23.0" + }, + "x86_64-linux": { + "outputs": [ + { + "name": "out", + "path": "/nix/store/0k144pnsvy0wa3dcl1y3df7d4zskylc4-go-1.23.0", + "default": true + } + ], + "store_path": "/nix/store/0k144pnsvy0wa3dcl1y3df7d4zskylc4-go-1.23.0" + } + } + } + } +} diff --git a/pkg/adapter/dify/dify.go b/pkg/adapter/dify/dify.go index 7ecc97f..fc13465 100644 --- a/pkg/adapter/dify/dify.go +++ b/pkg/adapter/dify/dify.go @@ -51,11 +51,13 @@ type AgentMessage struct { Answer string `json:"answer,omitempty"` } -func (d *Dify) SummarizeArticle(url string) (content string, err error) { +func (d *Dify) SummarizeArticle(template, url string) (content string, err error) { // Define the URL and request body requestBody, err := json.Marshal(map[string]interface{}{ - "inputs": map[string]interface{}{}, - "query": url, + "inputs": map[string]interface{}{ + "template": template, + }, + "query": fmt.Sprintf("%s %s", template, url), "response_mode": "streaming", "conversation_id": "", "user": "fortress", diff --git a/pkg/adapter/dify/interface.go b/pkg/adapter/dify/interface.go index 314f8d0..bc5f597 100644 --- a/pkg/adapter/dify/interface.go +++ b/pkg/adapter/dify/interface.go @@ -1,5 +1,5 @@ package dify type DifyAdapter interface { - SummarizeArticle(youtubeURL string) (content string, err error) + SummarizeArticle(template, url string) (content string, err error) } diff --git a/pkg/discord/command/sum/sum.go b/pkg/discord/command/sum/sum.go index 65f84bd..da8f865 100644 --- a/pkg/discord/command/sum/sum.go +++ b/pkg/discord/command/sum/sum.go @@ -1,6 +1,7 @@ package sum import ( + "fmt" "github.com/dwarvesf/fortress-discord/pkg/discord/service" "github.com/dwarvesf/fortress-discord/pkg/discord/view" "github.com/dwarvesf/fortress-discord/pkg/logger" @@ -22,14 +23,37 @@ func New(l logger.Logger, svc service.Servicer, view view.Viewer) SumCommander { } func (e *Sum) Sum(message *model.DiscordMessage) error { - url := message.ContentArgs[1] - // 1. get data from service - data, err := e.svc.Sum().SummarizeArticle(url) + var template, url string + var data *model.Sum + var err error + + switch len(message.ContentArgs) { + case 2: + // Original behavior: ?sum + url = message.ContentArgs[1] + data, err = e.svc.Sum().SummarizeArticle("", url) + case 3: + // New behavior: ?sum