Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(?sum): add templates feature #79

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions devbox.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"$schema": "https://raw.githubusercontent.com/jetify-com/devbox/0.12.0/.schema/devbox.schema.json",
"packages": ["[email protected]"],
"shell": {
"init_hook": [
"echo 'Welcome to devbox!' > /dev/null"
],
"scripts": {
"test": [
"echo \"Error: no test specified\" && exit 1"
]
}
}
}
53 changes: 53 additions & 0 deletions devbox.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"lockfile_version": "1",
"packages": {
"[email protected]": {
"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"
}
}
}
}
}
8 changes: 5 additions & 3 deletions pkg/adapter/dify/dify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion pkg/adapter/dify/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package dify

type DifyAdapter interface {
SummarizeArticle(youtubeURL string) (content string, err error)
SummarizeArticle(template, url string) (content string, err error)
}
34 changes: 29 additions & 5 deletions pkg/discord/command/sum/sum.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cái template mình có thể để cụ thể nó là: short/detail hay gì gì đó được khum?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah vậy thì enum luôn

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah. với làm nó không phụ thuộc vào vị trí của cái args mình điền vào thì hay hơn.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aight aight done ser 👍

case 2:
// Original behavior: ?sum <url>
url = message.ContentArgs[1]
data, err = e.svc.Sum().SummarizeArticle("", url)
case 3:
// New behavior: ?sum <template> <url>
template = message.ContentArgs[1]
url = message.ContentArgs[2]
data, err = e.svc.Sum().SummarizeArticle(template, url)
default:
errorSummary := &model.Sum{
Title: "Error",
Summary: "Invalid command format. Use: ?sum <url> or ?sum <template> <url>",
}
return e.view.Sum().Sum(message, errorSummary)
}

if err != nil {
e.L.Error(err, "failed to summarize the given article")
return err
errorSummary := &model.Sum{
Title: "Error",
Summary: fmt.Sprintf("Failed to summarize the article: %v", err),
}
return e.view.Sum().Sum(message, errorSummary)
}

// 2. render
// render
return e.view.Sum().Sum(message, data)
}
2 changes: 1 addition & 1 deletion pkg/discord/service/sum/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package sum
import "github.com/dwarvesf/fortress-discord/pkg/model"

type SumServicer interface {
SummarizeArticle(url string) (*model.Sum, error)
SummarizeArticle(template, url string) (*model.Sum, error)
}
11 changes: 6 additions & 5 deletions pkg/discord/service/sum/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ func New(adapter adapter.IAdapter, l logger.Logger) SumServicer {
}
}

func (e *Sum) SummarizeArticle(url string) (*model.Sum, error) {
data, err := e.adapter.Dify().SummarizeArticle(url)
func (e *Sum) SummarizeArticle(template, url string) (*model.Sum, error) {
data, err := e.adapter.Dify().SummarizeArticle(template, url)
if err != nil {
fmt.Printf("failed to summarize the given article. Error: %v\n", err)
return nil, err
}
title, summary := extractTitleAndSummary(data)

return &model.Sum{
URL: url,
Title: title,
Summary: summary,
URL: url,
Title: title,
Summary: summary,
Template: template,
}, nil
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/model/sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ type AdapterSum struct {

// Sum is in-app model, after process from adapters
type Sum struct {
URL string `json:"url"`
Title string `json:"title"`
Summary string `json:"summary"`
URL string `json:"url"`
Title string `json:"title"`
Summary string `json:"summary"`
Template string `json:"template"`
}