slight building refactoring, and removed any user state changes from prompt builder
This commit is contained in:
@@ -475,15 +475,17 @@ func Parse(raw string) ([]Action, error) { //nolint:funlen
|
||||
if start != -1 && end != -1 && end > start {
|
||||
raw = raw[start : end+1]
|
||||
}
|
||||
|
||||
if start == -1 || end == -1 || end < start {
|
||||
return nil, errors.New("parse: action array not found in output")
|
||||
}
|
||||
|
||||
raw = strings.TrimSpace(raw)
|
||||
|
||||
if !gjson.Valid(raw) {
|
||||
return nil, errors.New("parse: response is not valid JSON")
|
||||
}
|
||||
result := gjson.Parse(raw)
|
||||
if !result.IsArray() {
|
||||
return nil, errors.New("parse: response must be an array of actions")
|
||||
}
|
||||
|
||||
var actions []Action
|
||||
for i, item := range result.Array() {
|
||||
|
||||
@@ -1556,7 +1556,13 @@ func TestParse(t *testing.T) {
|
||||
name: "not an array",
|
||||
raw: `{"type":"wait"}`,
|
||||
wantErr: true,
|
||||
errMsg: "must be an array",
|
||||
errMsg: "action array not found in output",
|
||||
},
|
||||
{
|
||||
name: "invalid JSON",
|
||||
raw: `[aboba]`,
|
||||
wantErr: true,
|
||||
errMsg: "response is not valid JSON",
|
||||
},
|
||||
{
|
||||
name: "unknown action type",
|
||||
|
||||
+33
-50
@@ -126,7 +126,7 @@ YOUR GOAL IS TO SEND USER CONTANT A MESSAGE USING message COMMAND
|
||||
NOTIFICATION MESSAGE SHOULD BE BASED ON NOTIFICAION CONTENT IN COONTEXT
|
||||
IF NOTIFICAION IS "CREATE CONTACT NOTIFICAION" - YOU SHOULD TAKE CONTACT NAME FROM MESSAGES, UUID FROM NOTIFICAION AND EXECUTE add_contact FOR USER, AFTEER THAT - INFORM USER WITH MESSAGE
|
||||
`
|
||||
repeatedOn = `
|
||||
repeatedOnTmpl = `
|
||||
YOU SHOULD CREATE NEXT NOTIFICAION, USING PROVIDED TIME CONTEXT
|
||||
NEW NOTIFICAION SHOULD BE POSSIBLY KEPT AS IS, WITH ONLY TIME CHANGED
|
||||
`
|
||||
@@ -152,7 +152,7 @@ const (
|
||||
AgeOld = "AGE OLD"
|
||||
)
|
||||
|
||||
var behaviourMapping = [6][3]string{ //nolint:gochecknoglobals // here not to use in-function mapping
|
||||
var behaviourMapping = [6][3]string{ //nolint:gochecknoglobals // simple behavioral mapping
|
||||
{GenderMale, AgeYoung, behaviourManYoung},
|
||||
{GenderMale, AgeMiddle, behaviourManMiddle},
|
||||
{GenderMale, AgeOld, behaviourManOld},
|
||||
@@ -161,43 +161,7 @@ var behaviourMapping = [6][3]string{ //nolint:gochecknoglobals // here not to us
|
||||
{GenderFemale, AgeOld, behaviourWomanOld},
|
||||
}
|
||||
|
||||
type buildParams struct {
|
||||
repeatOn string
|
||||
errors []string
|
||||
}
|
||||
|
||||
func extractParams(params []any) buildParams {
|
||||
var p buildParams
|
||||
for _, param := range params {
|
||||
switch v := param.(type) {
|
||||
case RepeatOn:
|
||||
p.repeatOn = string(v)
|
||||
case error:
|
||||
if msg := errorMessage(v); msg != "" {
|
||||
p.errors = append(p.errors, msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
func errorMessage(err error) string {
|
||||
switch err.Error() {
|
||||
case "parse: response is not valid JSON":
|
||||
return strings.Join([]string{
|
||||
"engine was unable to parse your previous output",
|
||||
"your output MUST contain valid JSON with array of actions",
|
||||
"JSON is considered started on first [ and ended on first ]",
|
||||
"previous response was broken, need valid JSON with actions",
|
||||
}, "\n")
|
||||
default:
|
||||
return "unexpected error: " + err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
func Build(user *user.User, source, content string, params ...any) string {
|
||||
p := extractParams(params)
|
||||
|
||||
var b strings.Builder
|
||||
|
||||
b.WriteString(base)
|
||||
@@ -219,11 +183,13 @@ func Build(user *user.User, source, content string, params ...any) string {
|
||||
b.WriteString(notification)
|
||||
}
|
||||
|
||||
if p.repeatOn != "" {
|
||||
b.WriteString(repeatedOn)
|
||||
repeatedOn := ejectRepeatedOn(params...)
|
||||
if repeatedOn != "" {
|
||||
b.WriteString(repeatedOnTmpl)
|
||||
}
|
||||
|
||||
if len(p.errors) > 0 {
|
||||
errCtx := buildErrorContext(params...)
|
||||
if errCtx != "" {
|
||||
b.WriteString(errs)
|
||||
}
|
||||
|
||||
@@ -232,14 +198,12 @@ func Build(user *user.User, source, content string, params ...any) string {
|
||||
b.WriteString(buildUserContext(user))
|
||||
b.WriteString(buildTimeContext(user.Timezone))
|
||||
|
||||
if len(p.errors) > 0 {
|
||||
b.WriteString(buildErrorContext(p.errors))
|
||||
}
|
||||
b.WriteString(errCtx)
|
||||
|
||||
if source != database.DatabaseSource {
|
||||
b.WriteString(buildMessage(source, content))
|
||||
} else {
|
||||
b.WriteString(buildNotification(content, p.repeatOn))
|
||||
b.WriteString(buildNotification(content, repeatedOn))
|
||||
}
|
||||
|
||||
return b.String()
|
||||
@@ -300,7 +264,7 @@ func buildUserContext(user *user.User) string {
|
||||
}
|
||||
|
||||
if len(user.OutgoingNotifications) > 0 {
|
||||
b.WriteString("OUTGOING NOTIFICAIONS (CREATED BY USER FOR OTHERS):\n")
|
||||
b.WriteString("OUTGOING NOTIFICAIONS (FOR OTHER USERS):\n")
|
||||
for _, n := range user.OutgoingNotifications {
|
||||
localTime := timeconv.ToLocal(n.ScheduledAt, user.Timezone)
|
||||
fmt.Fprintf(&b, " - [%s][%s] %s (%s)\n", n.ID.String(), localTime, n.Content, n.RepeatOn)
|
||||
@@ -315,11 +279,12 @@ func buildUserContext(user *user.User) string {
|
||||
}
|
||||
}
|
||||
|
||||
var tz = user.Timezone
|
||||
if user.Timezone == "" {
|
||||
user.Timezone = "(CRITICAL! ASK USER ABOUT HIS LOCATION/TIMEZONE, REQUIRED FOR NORMAL WORK FOR NOTIFICAIONS, WHEN USING SET - USE LINUX COMPATIBLE COMMAND)"
|
||||
tz = "(CRITICAL! ASK USER ABOUT HIS LOCATION/TIMEZONE, REQUIRED FOR NORMAL WORK FOR NOTIFICAIONS, WHEN USING SET - USE LINUX COMPATIBLE COMMAND)"
|
||||
}
|
||||
|
||||
fmt.Fprintf(&b, "USER TIMEZONE: %s\n", user.Timezone)
|
||||
fmt.Fprintf(&b, "USER TIMEZONE: %s\n", tz)
|
||||
fmt.Fprintf(&b, "LANGUAGE: %s\n", user.Language)
|
||||
fmt.Fprintf(&b, "SELECTED CHAT: %s\n", user.PreferredChat)
|
||||
fmt.Fprintf(&b, "BIND CODE: %s\n", user.BindCode.String())
|
||||
@@ -328,8 +293,26 @@ func buildUserContext(user *user.User) string {
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func buildErrorContext(errors []string) string {
|
||||
return "=== ERROR CONTEXT ===\n" + strings.Join(errors, "\n")
|
||||
func buildErrorContext(params ...any) string {
|
||||
var b strings.Builder
|
||||
for _, p := range params {
|
||||
if err, ok := p.(error); ok {
|
||||
if b.Len() == 0 {
|
||||
b.WriteString("=== ERROR CONTEXT ===\n")
|
||||
}
|
||||
fmt.Fprintf(&b, "- %s\n", strings.ToUpper(err.Error()))
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func ejectRepeatedOn(params ...any) string {
|
||||
for _, p := range params {
|
||||
if rpon, ok := p.(RepeatOn); ok {
|
||||
return string(rpon)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func buildTimeContext(timezone string) string {
|
||||
|
||||
Reference in New Issue
Block a user