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 all commits
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"
}
}
}
}
}
4 changes: 2 additions & 2 deletions pkg/adapter/dify/dify.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ 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,
"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)
}
49 changes: 44 additions & 5 deletions pkg/discord/command/sum/sum.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
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"
"github.com/dwarvesf/fortress-discord/pkg/model"
"net/url"
)

type Sum struct {
Expand All @@ -22,14 +24,51 @@ 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 string
var articleURL string
var data *model.Sum
var err error

// Parse arguments
for _, arg := range message.ContentArgs[1:] {
if isValidURL(arg) {
articleURL = arg
} else if model.IsValidTemplateType(arg) {
template = arg
}
}

// Validate parsed arguments
if articleURL == "" {
errorSummary := &model.Sum{
Title: "Error",
Summary: "No valid URL provided. Use: ?sum <url> or ?sum <template> <url> or ?sum <url> <template>",
}
return e.view.Sum().Sum(message, errorSummary)
}

// If no template is provided, use the default summary template
if template == "" {
template = string(model.TemplateSummary)
}

// Summarize the article
data, err = e.svc.Sum().SummarizeArticle(template, articleURL)
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 the summary
return e.view.Sum().Sum(message, data)
}

// isValidURL checks if a string is a valid URL
func isValidURL(str string) bool {
u, err := url.Parse(str)
return err == nil && u.Scheme != "" && u.Host != ""
}
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"`
}
19 changes: 19 additions & 0 deletions pkg/model/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package model

type TemplateType string

const (
TemplateSummary TemplateType = "summary"
TemplateKeyPoints TemplateType = "key_points"
TemplateNuggets TemplateType = "nuggets"
TemplateFacts TemplateType = "facts"
)

func IsValidTemplateType(t string) bool {
switch TemplateType(t) {
case TemplateSummary, TemplateKeyPoints, TemplateNuggets, TemplateFacts:
return true
default:
return false
}
}