| 898 | } |
| 899 | |
| 900 | func generateStructuredTurnStatusLabel( |
| 901 | ctx context.Context, |
| 902 | model fantasy.LanguageModel, |
| 903 | systemPrompt string, |
| 904 | userInput string, |
| 905 | ) (string, error) { |
| 906 | userInput = strings.TrimSpace(userInput) |
| 907 | if userInput == "" { |
| 908 | return "", xerrors.New("turn status label input was empty") |
| 909 | } |
| 910 | |
| 911 | prompt := fantasy.Prompt{ |
| 912 | { |
| 913 | Role: fantasy.MessageRoleSystem, |
| 914 | Content: []fantasy.MessagePart{ |
| 915 | fantasy.TextPart{Text: systemPrompt}, |
| 916 | }, |
| 917 | }, |
| 918 | { |
| 919 | Role: fantasy.MessageRoleUser, |
| 920 | Content: []fantasy.MessagePart{ |
| 921 | fantasy.TextPart{Text: userInput}, |
| 922 | }, |
| 923 | }, |
| 924 | } |
| 925 | |
| 926 | var maxOutputTokens int64 = 64 |
| 927 | var result *fantasy.ObjectResult[generatedTurnStatusLabel] |
| 928 | err := chatretry.Retry(ctx, func(retryCtx context.Context) error { |
| 929 | var genErr error |
| 930 | result, genErr = object.Generate[generatedTurnStatusLabel](retryCtx, model, fantasy.ObjectCall{ |
| 931 | Prompt: prompt, |
| 932 | SchemaName: "propose_turn_status_label", |
| 933 | SchemaDescription: "Propose a compact chat status label.", |
| 934 | MaxOutputTokens: &maxOutputTokens, |
| 935 | }) |
| 936 | return genErr |
| 937 | }, nil) |
| 938 | if err != nil { |
| 939 | return "", xerrors.Errorf("generate structured turn status label: %w", err) |
| 940 | } |
| 941 | |
| 942 | label, ok := normalizeTurnStatusLabel(result.Object.Label) |
| 943 | if !ok { |
| 944 | return "", xerrors.New("generated turn status label was invalid") |
| 945 | } |
| 946 | return label, nil |
| 947 | } |
| 948 | |
| 949 | func turnStatusLabelStateContext(status database.ChatStatus) string { |
| 950 | switch status { |