Skip to content
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
25 changes: 24 additions & 1 deletion app_llm.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,22 @@ func (app *App) getSuggestedCreatedDate(ctx context.Context, content string, log
result := stripReasoning(completion.Choices[0].Content)
return strings.TrimSpace(strings.Trim(result, "\"")), nil
}
var xmlAttrEscaper = strings.NewReplacer(
"&", "&",
`"`, """,
"'", "'",
"<", "&lt;",
">", "&gt;",
)

var xmlTextEscaper = strings.NewReplacer(
"&", "&amp;",
"<", "&lt;",
">", "&gt;",
)

func escapeXMLAttr(s string) string { return xmlAttrEscaper.Replace(s) }
func escapeXMLText(s string) string { return xmlTextEscaper.Replace(s) }
// getSuggestedCustomFields generates suggested custom fields for a document using the LLM
func (app *App) getSuggestedCustomFields(ctx context.Context, doc Document, selectedFieldIDs []int, logger *logrus.Entry) ([]CustomFieldSuggestion, error) {
// Fetch all available custom fields
Expand Down Expand Up @@ -401,7 +416,15 @@ func (app *App) getSuggestedCustomFields(ctx context.Context, doc Document, sele
var xmlBuilder strings.Builder
xmlBuilder.WriteString("<custom_fields>\n")
for _, field := range selectedCustomFields {
xmlBuilder.WriteString(fmt.Sprintf(" <field name=\"%s\" type=\"%s\"></field>\n", field.Name, field.DataType))
if field.DataType == "select" && field.ExtraData != nil && len(field.ExtraData.SelectOptions) > 0 {
xmlBuilder.WriteString(fmt.Sprintf(" <field name=\"%s\" type=\"%s\">\n", escapeXMLAttr(field.Name), escapeXMLAttr(field.DataType)))
for _, opt := range field.ExtraData.SelectOptions {
xmlBuilder.WriteString(fmt.Sprintf(" <option id=\"%s\">%s</option>\n", escapeXMLAttr(opt.ID), escapeXMLText(opt.Label)))
}
xmlBuilder.WriteString(" </field>\n")
} else {
xmlBuilder.WriteString(fmt.Sprintf(" <field name=\"%s\" type=\"%s\"></field>\n", escapeXMLAttr(field.Name), escapeXMLAttr(field.DataType)))
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
xmlBuilder.WriteString("</custom_fields>")
customFieldsXML := xmlBuilder.String()
Expand Down
16 changes: 13 additions & 3 deletions paperless.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,20 @@ type PaperlessClient struct {
}

// CustomField represents a custom field from the Paperless-ngx API
type SelectOption struct {
ID string `json:"id"`
Label string `json:"label"`
}

type CustomFieldExtraData struct {
SelectOptions []SelectOption `json:"select_options"`
}

type CustomField struct {
ID int `json:"id"`
Name string `json:"name"`
DataType string `json:"data_type"`
ID int `json:"id"`
Name string `json:"name"`
DataType string `json:"data_type"`
ExtraData *CustomFieldExtraData `json:"extra_data"`
}

// DocumentType represents a document type from the Paperless-ngx API
Expand Down