feat: add suggestions to issue output
This commit is contained in:
parent
93ef37bba6
commit
65f9f2ef15
1
go.mod
1
go.mod
@ -38,6 +38,7 @@ require (
|
||||
github.com/tidwall/match v1.1.1 // indirect
|
||||
github.com/tidwall/pretty v1.2.1 // indirect
|
||||
github.com/tidwall/sjson v1.2.5 // indirect
|
||||
github.com/yuin/goldmark v1.7.8 // indirect
|
||||
go.opentelemetry.io/otel v1.29.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.29.0 // indirect
|
||||
golang.org/x/crypto v0.33.0 // indirect
|
||||
|
2
go.sum
2
go.sum
@ -72,6 +72,8 @@ github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhso
|
||||
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
|
||||
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
|
||||
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
go.opentelemetry.io/otel v1.29.0 h1:PdomN/Al4q/lN6iBJEN3AwPvUiHPMlt93c8bqTG5Llw=
|
||||
go.opentelemetry.io/otel v1.29.0/go.mod h1:N/WtXPs1CNCUEx+Agz5uouwCba+i+bJGFicT8SR4NP8=
|
||||
go.opentelemetry.io/otel/trace v1.29.0 h1:J/8ZNK4XgR7a21DZUAsbF8pZ5Jcw1VhACmnYt39JTi4=
|
||||
|
@ -80,17 +80,17 @@ func (m *IssueManager) GetUserProjects(ctx context.Context, user *model.User) ([
|
||||
return projects, nil
|
||||
}
|
||||
|
||||
func (m *IssueManager) GenerateIssue(ctx context.Context, user *model.User, projectID string, issueSummary string) (string, string, error) {
|
||||
func (m *IssueManager) GenerateIssue(ctx context.Context, user *model.User, projectID string, issueSummary string) (string, string, string, error) {
|
||||
systemPrompt, err := m.getIssueSystemPrompt(ctx, user, projectID)
|
||||
if err != nil {
|
||||
return "", "", errors.WithStack(err)
|
||||
return "", "", "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
slog.DebugContext(ctx, "using system prompt", slog.String("systemPrompt", systemPrompt))
|
||||
|
||||
userPrompt, err := m.getIssueUserPrompt(ctx, user, projectID, issueSummary)
|
||||
if err != nil {
|
||||
return "", "", errors.WithStack(err)
|
||||
return "", "", "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
slog.DebugContext(ctx, "using user prompt", slog.String("userPrompt", userPrompt))
|
||||
@ -102,7 +102,7 @@ func (m *IssueManager) GenerateIssue(ctx context.Context, user *model.User, proj
|
||||
|
||||
res, err := m.llmClient.ChatCompletion(ctx, llm.WithMessages(messages...))
|
||||
if err != nil {
|
||||
return "", "", errors.WithStack(err)
|
||||
return "", "", "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
body := res.Message().Content()
|
||||
@ -112,12 +112,22 @@ func (m *IssueManager) GenerateIssue(ctx context.Context, user *model.User, proj
|
||||
|
||||
res, err = m.llmClient.ChatCompletion(ctx, llm.WithMessages(messages...))
|
||||
if err != nil {
|
||||
return "", "", errors.WithStack(err)
|
||||
return "", "", "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
title := toTitle(res.Message().Content())
|
||||
|
||||
return title, body, nil
|
||||
messages = append(messages, res.Message())
|
||||
messages = append(messages, llm.NewMessage(llm.RoleUser, "Give me a list of questions as Markdown that could help clarify the request. Only write the list without additional headings."))
|
||||
|
||||
res, err = m.llmClient.ChatCompletion(ctx, llm.WithMessages(messages...))
|
||||
if err != nil {
|
||||
return "", "", "", errors.WithStack(err)
|
||||
}
|
||||
|
||||
tips := res.Message().Content()
|
||||
|
||||
return title, body, tips, nil
|
||||
}
|
||||
|
||||
func (m *IssueManager) getIssueSystemPrompt(ctx context.Context, user *model.User, projectID string) (string, error) {
|
||||
|
@ -1,15 +1,21 @@
|
||||
package component
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"forge.cadoles.com/wpetit/clearcase/internal/core/model"
|
||||
"forge.cadoles.com/wpetit/clearcase/internal/http/form"
|
||||
common "forge.cadoles.com/wpetit/clearcase/internal/http/handler/webui/common/component"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/yuin/goldmark"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type IssuePageVModel struct {
|
||||
IssueURL string
|
||||
SummaryForm *form.Form
|
||||
IssueForm *form.Form
|
||||
IssueTips string
|
||||
Projects []*model.Project
|
||||
SelectedProjectID string
|
||||
}
|
||||
@ -128,6 +134,22 @@ templ IssuePage(vmodel IssuePageVModel) {
|
||||
</div>
|
||||
</div>
|
||||
<progress id="generation-progress" class="htmx-indicator progress"></progress>
|
||||
if vmodel.IssueTips != "" {
|
||||
{{ html := markdownToHTML(ctx, vmodel.IssueTips) }}
|
||||
if html != "" {
|
||||
<article class="message is-info mt-5">
|
||||
<div class="message-header">
|
||||
<p><span class="icon"><i class="fa fa-lightbulb"></i></span>Suggestions</p>
|
||||
<button class="delete" aria-label="delete" hx-on:click="onCloseMessage(this)"></button>
|
||||
</div>
|
||||
<div class="message-body">
|
||||
<div class="content">
|
||||
@templ.Raw(html)
|
||||
</div>
|
||||
</div>
|
||||
</article>
|
||||
}
|
||||
}
|
||||
</section>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
@ -183,3 +205,13 @@ func projectsToOptions(projects []*model.Project) []string {
|
||||
}
|
||||
return options
|
||||
}
|
||||
|
||||
func markdownToHTML(ctx context.Context, text string) string {
|
||||
var buff bytes.Buffer
|
||||
if err := goldmark.Convert([]byte(text), &buff); err != nil {
|
||||
slog.ErrorContext(ctx, "could not convert markdown to html", slog.Any("error", errors.WithStack(err)))
|
||||
return ""
|
||||
}
|
||||
|
||||
return buff.String()
|
||||
}
|
||||
|
@ -9,15 +9,21 @@ import "github.com/a-h/templ"
|
||||
import templruntime "github.com/a-h/templ/runtime"
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"forge.cadoles.com/wpetit/clearcase/internal/core/model"
|
||||
"forge.cadoles.com/wpetit/clearcase/internal/http/form"
|
||||
common "forge.cadoles.com/wpetit/clearcase/internal/http/handler/webui/common/component"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/yuin/goldmark"
|
||||
"log/slog"
|
||||
)
|
||||
|
||||
type IssuePageVModel struct {
|
||||
IssueURL string
|
||||
SummaryForm *form.Form
|
||||
IssueForm *form.Form
|
||||
IssueTips string
|
||||
Projects []*model.Project
|
||||
SelectedProjectID string
|
||||
}
|
||||
@ -124,7 +130,7 @@ func IssuePage(vmodel IssuePageVModel) templ.Component {
|
||||
var templ_7745c5c3_Var5 string
|
||||
templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(vmodel.IssueURL)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/http/handler/webui/issue/component/issue_page.templ`, Line: 71, Col: 89}
|
||||
return templ.Error{Err: templ_7745c5c3_Err, FileName: `internal/http/handler/webui/issue/component/issue_page.templ`, Line: 77, Col: 89}
|
||||
}
|
||||
_, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5))
|
||||
if templ_7745c5c3_Err != nil {
|
||||
@ -202,7 +208,28 @@ func IssuePage(vmodel IssuePageVModel) templ.Component {
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "<div class=\"buttons is-right\"><button type=\"submit\" class=\"button is-primary is-large\"><span class=\"icon\"><i class=\"fa fa-rocket\"></i></span> <span>Créer le ticket</span></button></div></form></div></div><progress id=\"generation-progress\" class=\"htmx-indicator progress\"></progress></section></div><script type=\"text/javascript\">\n\t\tfunction onCloseMessage(closeElement) {\n\t\t\tcloseElement.closest('.message').style.display = 'none';\n\t\t}\n\n\t\tfunction onSummaryChange(evt) {\n\t\t\tconst summary = evt.currentTarget.value;\n\t\t\tconst projectId = document.getElementById(\"issue-project\").value;\n\t\t\tsessionStorage.setItem(`summary-${projectId}`, summary);\n\t\t}\n\n\t\tfunction savePreferredProject() {\n\t\t\tconst projectId = document.getElementById(\"issue-project\").value;\n\t\t\tlocalStorage.setItem(`preferred-project`, projectId);\n\t\t}\n\n\t\tfunction restorePreferredProject() {\n\t\t\tconst preferredProject = localStorage.getItem(`preferred-project`);\n\t\t\tif (!preferredProject) return;\n\t\t\tconst projectElement = document.getElementById(\"issue-project\");\n\t\t\tif (!projectElement) return;\n\t\t\tif (preferredProject === projectElement.value) return;\n\t\t\tprojectElement.value = preferredProject;\n\t\t\tprojectElement.dispatchEvent(new Event('change'));\n\t\t}\n\n\t\tfunction restoreLastSummary() {\n\t\t\tconst summaryTextarea = document.getElementById(\"issue-summary\");\n\t\t\tif (!summaryTextarea) return;\n const summary = summaryTextarea.value;\n\t\t\tif (summary !== \"\") return;\n\t\t\tconst projectId = document.getElementById(\"issue-project\").value;\n\t\t\tif (!projectId) return;\n\t\t\tconst savedSummary = sessionStorage.getItem(`summary-${projectId}`);\n\t\t\tif (!savedSummary) return;\n\t\t\tsummaryTextarea.value = savedSummary;\n\t\t}\n\n\t\thtmx.onLoad(function(){\n\t\t\trestoreLastSummary();\n\t\t\trestorePreferredProject();\n })\n\t\t</script>")
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 11, "<div class=\"buttons is-right\"><button type=\"submit\" class=\"button is-primary is-large\"><span class=\"icon\"><i class=\"fa fa-rocket\"></i></span> <span>Créer le ticket</span></button></div></form></div></div><progress id=\"generation-progress\" class=\"htmx-indicator progress\"></progress> ")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
if vmodel.IssueTips != "" {
|
||||
html := markdownToHTML(ctx, vmodel.IssueTips)
|
||||
if html != "" {
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 12, "<article class=\"message is-info mt-5\"><div class=\"message-header\"><p><span class=\"icon\"><i class=\"fa fa-lightbulb\"></i></span>Suggestions</p><button class=\"delete\" aria-label=\"delete\" hx-on:click=\"onCloseMessage(this)\"></button></div><div class=\"message-body\"><div class=\"content\">")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templ.Raw(html).Render(ctx, templ_7745c5c3_Buffer)
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 13, "</div></div></article>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
}
|
||||
}
|
||||
templ_7745c5c3_Err = templruntime.WriteString(templ_7745c5c3_Buffer, 14, "</section></div><script type=\"text/javascript\">\n\t\tfunction onCloseMessage(closeElement) {\n\t\t\tcloseElement.closest('.message').style.display = 'none';\n\t\t}\n\n\t\tfunction onSummaryChange(evt) {\n\t\t\tconst summary = evt.currentTarget.value;\n\t\t\tconst projectId = document.getElementById(\"issue-project\").value;\n\t\t\tsessionStorage.setItem(`summary-${projectId}`, summary);\n\t\t}\n\n\t\tfunction savePreferredProject() {\n\t\t\tconst projectId = document.getElementById(\"issue-project\").value;\n\t\t\tlocalStorage.setItem(`preferred-project`, projectId);\n\t\t}\n\n\t\tfunction restorePreferredProject() {\n\t\t\tconst preferredProject = localStorage.getItem(`preferred-project`);\n\t\t\tif (!preferredProject) return;\n\t\t\tconst projectElement = document.getElementById(\"issue-project\");\n\t\t\tif (!projectElement) return;\n\t\t\tif (preferredProject === projectElement.value) return;\n\t\t\tprojectElement.value = preferredProject;\n\t\t\tprojectElement.dispatchEvent(new Event('change'));\n\t\t}\n\n\t\tfunction restoreLastSummary() {\n\t\t\tconst summaryTextarea = document.getElementById(\"issue-summary\");\n\t\t\tif (!summaryTextarea) return;\n const summary = summaryTextarea.value;\n\t\t\tif (summary !== \"\") return;\n\t\t\tconst projectId = document.getElementById(\"issue-project\").value;\n\t\t\tif (!projectId) return;\n\t\t\tconst savedSummary = sessionStorage.getItem(`summary-${projectId}`);\n\t\t\tif (!savedSummary) return;\n\t\t\tsummaryTextarea.value = savedSummary;\n\t\t}\n\n\t\thtmx.onLoad(function(){\n\t\t\trestoreLastSummary();\n\t\t\trestorePreferredProject();\n })\n\t\t</script>")
|
||||
if templ_7745c5c3_Err != nil {
|
||||
return templ_7745c5c3_Err
|
||||
}
|
||||
@ -224,4 +251,14 @@ func projectsToOptions(projects []*model.Project) []string {
|
||||
return options
|
||||
}
|
||||
|
||||
func markdownToHTML(ctx context.Context, text string) string {
|
||||
var buff bytes.Buffer
|
||||
if err := goldmark.Convert([]byte(text), &buff); err != nil {
|
||||
slog.ErrorContext(ctx, "could not convert markdown to html", slog.Any("error", errors.WithStack(err)))
|
||||
return ""
|
||||
}
|
||||
|
||||
return buff.String()
|
||||
}
|
||||
|
||||
var _ = templruntime.GeneratedTemplate
|
||||
|
@ -105,12 +105,13 @@ func (h *Handler) handleIssueSummaryForm(w http.ResponseWriter, r *http.Request)
|
||||
return
|
||||
}
|
||||
|
||||
issueTitle, issueBody, err := h.issueManager.GenerateIssue(ctx, httpCtx.User(ctx), projectID, summary)
|
||||
issueTitle, issueBody, issueTips, err := h.issueManager.GenerateIssue(ctx, httpCtx.User(ctx), projectID, summary)
|
||||
if err != nil {
|
||||
h.handleError(w, r, errors.WithStack(err))
|
||||
return
|
||||
}
|
||||
|
||||
vmodel.IssueTips = issueTips
|
||||
vmodel.IssueForm.Field("title").Set("value", issueTitle)
|
||||
vmodel.IssueForm.Field("body").Set("value", issueBody)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user