Skip to content

Commit 88cba00

Browse files
authored
Synthesize Tool Update (#219)
1 parent c6118b6 commit 88cba00

File tree

1 file changed

+43
-11
lines changed

1 file changed

+43
-11
lines changed

pkg/externalfunctions/ansysmeshpilot.go

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ func ParseHistoryToHistoricMessages(historyJson string) (history []sharedtypes.H
17161716
// - slashCmd: the slash command if found, otherwise an empty string
17171717
// - targetCmd: the target command if found, otherwise an empty string
17181718
// - hasCmd: boolean indicating if a slash command or target command was found
1719-
func ParseSlashCommand(userInput string) (slashCmd, targetCmd string, hasCmd bool) {
1719+
func ParseSlashCommand(userInput string) (slashCmd, targetCmd string, hasCmd bool, hasContext bool) {
17201720

17211721
targetRe := regexp.MustCompile(`@[A-Za-z][\w]*`)
17221722
slashRe := regexp.MustCompile(`/[A-Za-z][\w]*`)
@@ -1740,9 +1740,12 @@ func ParseSlashCommand(userInput string) (slashCmd, targetCmd string, hasCmd boo
17401740
hasCmd = false
17411741
}
17421742

1743-
logging.Log.Debugf(&logging.ContextMap{}, "User Command: Slash: %s, Target: %s, Has Command: %t", slash, target, hasCmd)
1743+
remaining := strings.TrimSpace(strings.ReplaceAll(strings.ReplaceAll(userInput, "/"+slash, ""), "@"+target, ""))
1744+
hasContext = remaining != ""
17441745

1745-
return slash, target, hasCmd
1746+
logging.Log.Debugf(&logging.ContextMap{}, "User Command: Slash: %s, Target: %s, Has Command: %t, Has Context: %t, Remaining: %s", slash, target, hasCmd, hasContext, remaining)
1747+
1748+
return slash, target, hasCmd, hasContext
17461749
}
17471750

17481751
// SynthesizeSlashCommand synthesize actions based on user instruction
@@ -1756,7 +1759,7 @@ func ParseSlashCommand(userInput string) (slashCmd, targetCmd string, hasCmd boo
17561759
//
17571760
// Returns:
17581761
// - result: the synthesized string
1759-
func SynthesizeSlashCommand(slashCmd, targetCmd string) (result string) {
1762+
func SynthesizeSlashCommand(slashCmd, targetCmd, finalizeResult string) (result string) {
17601763
ctx := &logging.ContextMap{}
17611764

17621765
message, exists := config.GlobalConfig.WORKFLOW_CONFIG_VARIABLES["APP_ACTION_TOOL_17_SUCCESS_MESSAGE"]
@@ -1787,12 +1790,41 @@ func SynthesizeSlashCommand(slashCmd, targetCmd string) (result string) {
17871790
panic(errorMessage)
17881791
}
17891792

1790-
actions := []map[string]string{
1791-
{
1792-
actionKey1: targetCmd,
1793-
actionKey2: actionValue2,
1794-
"Argument": slashCmd,
1795-
},
1793+
var actions []map[string]string
1794+
1795+
if finalizeResult != "" {
1796+
var parsedResult map[string]interface{}
1797+
err := json.Unmarshal([]byte(finalizeResult), &parsedResult)
1798+
if err != nil {
1799+
errorMessage := fmt.Sprintf("failed to unmarshal finalizeResult: %v", err)
1800+
logging.Log.Error(ctx, errorMessage)
1801+
panic(errorMessage)
1802+
}
1803+
1804+
if parsedActions, ok := parsedResult["Actions"].([]interface{}); ok {
1805+
for _, action := range parsedActions {
1806+
if actionMap, ok := action.(map[string]interface{}); ok {
1807+
updatedAction := map[string]string{}
1808+
for k, v := range actionMap {
1809+
if strVal, ok := v.(string); ok {
1810+
updatedAction[k] = strVal
1811+
}
1812+
}
1813+
updatedAction[actionKey1] = targetCmd
1814+
updatedAction[actionKey2] = actionValue2
1815+
updatedAction["Argument"] = slashCmd
1816+
actions = append(actions, updatedAction)
1817+
}
1818+
}
1819+
}
1820+
} else {
1821+
actions = []map[string]string{
1822+
{
1823+
actionKey1: targetCmd,
1824+
actionKey2: actionValue2,
1825+
"Argument": slashCmd,
1826+
},
1827+
}
17961828
}
17971829

17981830
finalMessage := map[string]interface{}{
@@ -1802,7 +1834,7 @@ func SynthesizeSlashCommand(slashCmd, targetCmd string) (result string) {
18021834

18031835
resultStream, err := json.Marshal(finalMessage)
18041836
if err != nil {
1805-
errorMessage := fmt.Sprintf("failed to marshal final message for tool 17: %v", err)
1837+
errorMessage := fmt.Sprintf("failed to marshal final message: %v", err)
18061838
logging.Log.Error(ctx, errorMessage)
18071839
panic(errorMessage)
18081840
}

0 commit comments

Comments
 (0)