diff --git a/cmd/core-runtime/household_scenario_test.go b/cmd/core-runtime/household_scenario_test.go index 109683d..d8cae95 100644 --- a/cmd/core-runtime/household_scenario_test.go +++ b/cmd/core-runtime/household_scenario_test.go @@ -38,6 +38,10 @@ func TestHouseholdCreativeRouteDoesNotUseResearch(t *testing.T) { func TestHouseholdMarketplaceBrandSearchUsesResearchRoute(t *testing.T) { candidate := interactiondomain.BuildDeterministicCandidate("найди на WB бренд marka-wear", "", time.Unix(10, 0).UnixMilli()) + decomposition := interactiondomain.DecomposeTask("найди на WB бренд marka-wear") + if decomposition.Intent != interactiondomain.TaskIntentSearch || decomposition.Object != interactiondomain.TaskObjectBrand || decomposition.Source != interactiondomain.TaskSourceWB || decomposition.Target != "marka-wear" || len(decomposition.Missing) != 0 { + t.Fatalf("marketplace request was not split into executable atoms: %+v", decomposition) + } routes := buildChatRoutePlan(interactiondomain.SemanticResult{NeedsCognition: candidate.NeedsCognition, CapabilityHints: candidate.CapabilityHints}) if !hasChatRoute(routes, chatRouteResearch) { t.Fatalf("marketplace brand request did not select research route: candidate=%+v routes=%+v", candidate, routes) diff --git a/modules/interaction/domain/chat_test.go b/modules/interaction/domain/chat_test.go index 642abad..6a11770 100644 --- a/modules/interaction/domain/chat_test.go +++ b/modules/interaction/domain/chat_test.go @@ -170,6 +170,23 @@ func TestSourceDependentQuestionToleratesTyposAndPlaceSearch(t *testing.T) { } } +func TestDecomposeMarketplaceBrandRequestIntoExecutionAtoms(t *testing.T) { + got := DecomposeTask("найди на WB бренд marka-wear") + if got.Intent != TaskIntentSearch || got.Object != TaskObjectBrand || got.Source != TaskSourceWB || got.Target != "marka-wear" || !got.NeedsResearch { + t.Fatalf("unexpected marketplace decomposition: %+v", got) + } + if len(got.Missing) != 0 { + t.Fatalf("complete marketplace request marked incomplete: %+v", got) + } +} + +func TestDecomposeCreativeRequestDoesNotInventResearch(t *testing.T) { + got := DecomposeTask("нарисуй бурого медведя") + if got.Intent != TaskIntentCreate || got.Object != TaskObjectImage || got.Target != "бурого медведя" || got.NeedsResearch { + t.Fatalf("unexpected creative decomposition: %+v", got) + } +} + func TestRequestToClarifyCurrentFactRequestsWebResearch(t *testing.T) { candidate := BuildDeterministicCandidate("уточни погоду в питере", "", time.Unix(10, 0).UnixMilli()) for _, hint := range candidate.CapabilityHints { diff --git a/modules/interaction/domain/task_decomposition.go b/modules/interaction/domain/task_decomposition.go new file mode 100644 index 0000000..c0f7962 --- /dev/null +++ b/modules/interaction/domain/task_decomposition.go @@ -0,0 +1,86 @@ +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) +}