87 lines
2.7 KiB
Go
87 lines
2.7 KiB
Go
package domain
|
|
|
|
import "strings"
|
|
|
|
// TaskDecomposition is the small, typed intermediate representation used
|
|
// before a capability is called. It separates the user's goal from the
|
|
// object, source and target instead of treating the whole sentence as a
|
|
// command string.
|
|
type TaskDecomposition struct {
|
|
Intent string
|
|
Object string
|
|
Source string
|
|
Target string
|
|
Qualifiers []string
|
|
Missing []string
|
|
NeedsResearch bool
|
|
}
|
|
|
|
const (
|
|
TaskIntentSearch = "SEARCH"
|
|
TaskIntentCreate = "CREATE"
|
|
TaskObjectBrand = "BRAND"
|
|
TaskObjectImage = "IMAGE"
|
|
TaskSourceWB = "WILDBERRIES"
|
|
)
|
|
|
|
// DecomposeTask performs a cheap semantic pass. It is deliberately a
|
|
// vocabulary-assisted parser, not a complete language grammar: the result is
|
|
// an execution plan seed for the appropriate agent, not the final answer.
|
|
func DecomposeTask(message string) TaskDecomposition {
|
|
lower := strings.ToLower(strings.TrimSpace(message))
|
|
tokens := semanticTokens(message)
|
|
result := TaskDecomposition{}
|
|
|
|
if containsStemInTokens(tokens, "найд", "поищ", "подбер", "проверь") {
|
|
result.Intent = TaskIntentSearch
|
|
}
|
|
if containsStemInTokens(tokens, "нарис", "рису", "изобраз", "создай") {
|
|
result.Intent = TaskIntentCreate
|
|
}
|
|
if containsStemInTokens(tokens, "бренд") {
|
|
result.Object = TaskObjectBrand
|
|
}
|
|
if containsStemInTokens(tokens, "медвед", "мишк", "картин") {
|
|
result.Object = TaskObjectImage
|
|
}
|
|
if containsToken(tokens, "wb") || containsStemInTokens(tokens, "wildberr", "маркетплейс") {
|
|
result.Source = TaskSourceWB
|
|
result.NeedsResearch = result.Intent == TaskIntentSearch
|
|
}
|
|
if result.Object == TaskObjectBrand {
|
|
result.Target = valueAfterCue(lower, "бренд")
|
|
}
|
|
if result.Object == TaskObjectImage && result.Intent == TaskIntentCreate {
|
|
result.Target = valueAfterAnyCue(lower, "нарисуй", "нарисовать", "изобрази", "создай")
|
|
}
|
|
if result.Intent == TaskIntentSearch && result.Source == "" {
|
|
result.Missing = append(result.Missing, "SOURCE_OR_SEARCH_SCOPE")
|
|
}
|
|
if result.Intent == TaskIntentSearch && result.Target == "" {
|
|
result.Missing = append(result.Missing, "TARGET")
|
|
}
|
|
return result
|
|
}
|
|
|
|
func valueAfterAnyCue(message string, cues ...string) string {
|
|
for _, cue := range cues {
|
|
if value := valueAfterCue(message, cue); value != "" {
|
|
return value
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
func valueAfterCue(message, cue string) string {
|
|
index := strings.Index(message, cue)
|
|
if index < 0 {
|
|
return ""
|
|
}
|
|
value := strings.TrimSpace(message[index+len(cue):])
|
|
value = strings.Trim(value, " \t:,-")
|
|
if end := strings.IndexAny(value, "?!,.;"); end >= 0 {
|
|
value = value[:end]
|
|
}
|
|
return strings.TrimSpace(value)
|
|
}
|