@@ -25608,7 +25608,7 @@ else
2560825608 }
2560925609 let prompt = localsettings.inject_chatnames_instruct ? `Request for user input by ${action?.args?.whoToSendMessageAs}: ${action?.args?.message}` : `Request for user input: ${action?.args?.message}`
2561025610 addThought(createSysPrompt, prompt)
25611- return true
25611+ return currentOrderOfActionsOverall.length === 0
2561225612 }
2561325613 },
2561425614 {
@@ -25632,7 +25632,7 @@ else
2563225632 "name": "stop_thinking",
2563325633 "description": "Ends the current chain of thought. Can only be used after a \"send_message\" action.",
2563425634 "args": null,
25635- "enabled": true ,
25635+ "enabled": false ,
2563625636 "executor": (action) => {
2563725637 addThought(createSysPrompt, `Stop thinking action confirmed`)
2563825638 return true
@@ -25738,7 +25738,7 @@ else
2573825738 type: "array"
2573925739 }
2574025740 },
25741- "enabled": true ,
25741+ "enabled": documentdb_enabled ,
2574225742 "executor": (action) => {
2574325743 let textToAdd = action?.args?.text
2574425744 let keywords = action?.args?.keywords
@@ -25859,7 +25859,7 @@ else
2585925859 type: "array"
2586025860 }
2586125861 },
25862- "enabled": true ,
25862+ "enabled": false ,
2586325863 "executor": (action) => {
2586425864 let orderOfActions = action?.args?.orderOfActions
2586525865 if (!!orderOfActions && Array.isArray(orderOfActions)) {
@@ -26101,6 +26101,45 @@ else
2610126101 }
2610226102 return enabledCommands
2610326103 }
26104+
26105+ let getReasoningCommand = () => {
26106+ return [
26107+ {
26108+ "name": "plan_actions",
26109+ "description": "Defines a list of actions to respond to a user instruction",
26110+ "args": {
26111+ "orderOfActions": {
26112+ type: "array",
26113+ description: "<array of actions needed to complete the instruction and their objectives>",
26114+ format: {
26115+ type: "array",
26116+ items: {
26117+ type: "object",
26118+ properties: {
26119+ action: {
26120+ type: "string",
26121+ enum: getEnabledCommands().map(c => c.name)
26122+ },
26123+ objective: {
26124+ type: "string"
26125+ }
26126+ },
26127+ required: ["action", "objective"]
26128+ },
26129+ minItems: 1,
26130+ maxItems: Number(localsettings.agentCOTMax)
26131+ }
26132+ }
26133+ },
26134+ "enabled": true,
26135+ "executor": (action) => {
26136+ currentOrderOfActionsOverall = action?.args?.orderOfActions.map(act => act.action)
26137+ currentOrderOfActionDescriptionsOverall = action?.args?.orderOfActions.map(act => act.objective)
26138+ return false
26139+ }
26140+ }
26141+ ]
26142+ }
2610426143
2610526144 let toJsonSchema = (obj, currentRef = {}) => {
2610626145 switch (typeof obj) {
@@ -26158,20 +26197,39 @@ else
2615826197 {
2615926198 if (typeof command.args[arg] === "object" && !!command.args[arg]?.format)
2616026199 {
26161- let formatToUse = toJsonSchema(JSON.parse(command.args[arg]?.format))
26200+ let formatToUse = typeof command.args[arg]?.format === "string" ? toJsonSchema(JSON.parse(command.args[arg]?.format)) : command.args[arg]?.format
2616226201 args.properties[arg] = formatToUse
2616326202 }
2616426203 else if (typeof command.args[arg] === "object" && !!command.args[arg]?.type) {
2616526204 args.properties[arg] = {
2616626205 type: command.args[arg].type
2616726206 }
26207+
26208+ let propsToEdit = args.properties[arg]
26209+ if (command.args[arg].type === "array")
26210+ {
26211+ args.properties[arg].items = {
26212+ "type": "string"
26213+ }
26214+ propsToEdit = args.properties[arg].items
26215+
26216+ if (!!command.args[arg]?.itemType)
26217+ {
26218+ propsToEdit.type = command.args[arg]?.itemType
26219+ }
26220+ if (!!command.args[arg]?.uniqueItems)
26221+ {
26222+ args.properties[arg].pattern = command.args[arg]?.uniqueItems
26223+ }
26224+ }
26225+
2616826226 if (!!command.args[arg]?.enum)
2616926227 {
26170- args.properties[arg] .enum = command.args[arg]?.enum
26228+ propsToEdit .enum = command.args[arg]?.enum
2617126229 }
2617226230 if (!!command.args[arg]?.pattern)
2617326231 {
26174- args.properties[arg] .pattern = command.args[arg]?.pattern
26232+ propsToEdit .pattern = command.args[arg]?.pattern
2617526233 }
2617626234 } else {
2617726235 args.properties[arg] = {
@@ -26512,11 +26570,11 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2651226570 }
2651326571
2651426572 let getLastActions = (amountOfActions = 10) => {
26515- let exclusions = ["Chain of thought repetition detected - ending", "Chain of thought complete"] // "Action: {", "Action (words =", "Action taken: ", "Action taken (words ="
26573+ let exclusions = ["Chain of thought repetition detected - ending", "Chain of thought complete", "plan_actions", "Action: {", "Action (words =", "Action taken: ", "Action taken (words =" ] // "Action: {", "Action (words =", "Action taken: ", "Action taken (words ="
2651626574 return repack_instruct_turns(concat_gametext(true), `{{[INPUT]}}`, `{{[OUTPUT]}}`, `{{[SYSTEM]}}`, true, false).map(msg => {
2651726575 msg.msg = msg.msg.replaceAll("{{[SYSTEM_END]}}", "").replaceAll("{{[INPUT_END]}}", "").replaceAll("{{[OUTPUT_END]}}", "").trim();
2651826576 return msg
26519- }).filter(msg => !/^\n*$/.test(msg.msg) && !!msg.msg && !exclusions.find(exclusion => exclusion.indexOf( msg.msg) !== -1)).splice(-amountOfActions)
26577+ }).filter(msg => !/^\n*$/.test(msg.msg) && !!msg.msg && !exclusions.find(exclusion => msg.msg.indexOf(exclusion ) !== -1)).splice(-amountOfActions)
2652026578 }
2652126579
2652226580 let getInfoSnippet = (snippet) => {
@@ -26557,17 +26615,25 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2655726615 }
2655826616
2655926617 let getInitialAgentPrompt = (commands = getEnabledCommands(), max_mem_len) => {
26560- let prompt = createSysPrompt(`System prompt:\n\n${actionPrompt(commands)}`)
26618+ prompt = ""
2656126619 if (!!current_memory)
2656226620 {
2656326621 prompt += createSysPrompt(`Setting overview:\n\n${substring_to_boundary(current_memory, max_mem_len)}`)
2656426622 }
2656526623 return prompt
2656626624 }
2656726625
26568- let getFinalAgentPrompt = (currentOrderOfActions) => {
26626+ let getFinalAgentPrompt = (commands, currentOrderOfActions, objectiveForCurrentAction, initialPrompt ) => {
2656926627 let state = getDocumentFromTextDB('State')
2657026628 let prompt = []
26629+ if (!!initialPrompt)
26630+ {
26631+ prompt.push(`Last user instruction: ${initialPrompt}`)
26632+ }
26633+ if (!!objectiveForCurrentAction)
26634+ {
26635+ prompt.push(`Objective for current action: ${objectiveForCurrentAction}`)
26636+ }
2657126637 if (state != null)
2657226638 {
2657326639 prompt.push(`Current state: ${state}`)
@@ -26577,7 +26643,7 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2657726643 prompt.push(`Order of actions: ${currentOrderOfActions.join(" -> ")}`)
2657826644 }
2657926645 let basePrompt = prompt.join("\n\n")
26580- return basePrompt.length > 0 ? createSysPrompt(basePrompt) : ""
26646+ return createSysPrompt(`System prompt:\n\n${actionPrompt(!!commands.find(c => c.name === "plan_actions") ? getEnabledCommands() : commands)}`) + ( basePrompt.length > 0 ? createSysPrompt(basePrompt) : "")
2658126647 }
2658226648
2658326649 /**
@@ -26709,6 +26775,7 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2670926775 return actionAsText
2671026776 }
2671126777
26778+ let currentOrderOfActionsOverall = [], currentOrderOfActionDescriptionsOverall = []
2671226779 let recentActions = [], maxActionsInHistory = 20, currentAgentCycle = null, endCurrent = false
2671326780
2671426781 let runAgentCycle = async (initialPrompt = undefined) => {
@@ -26717,6 +26784,9 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2671726784 // render_gametext()
2671826785 currentChainOfThought = []
2671926786 recentActions = []
26787+ currentOrderOfActionsOverall = []
26788+ currentOrderOfActionDescriptionsOverall = []
26789+
2672026790 getLastActions(maxActionsInHistory).forEach(action => {
2672126791 switch(action.source)
2672226792 {
@@ -26756,33 +26826,55 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2675626826 let lastThoughtWasBlank = false
2675726827
2675826828 Array(...document.getElementsByClassName("stopThinking")).forEach(elem => elem.classList.remove("hidden"))
26759- for (let i = 0; i < localsettings.agentCOTMax && endCurrent === false; i++)
26829+
26830+ currentOrderOfActionsOverall = getDocumentFromTextDB('Order of actions')
26831+ currentOrderOfActionsOverall = !!currentOrderOfActionsOverall ? currentOrderOfActionsOverall.split(",").filter(act => !!act) : []
26832+ currentOrderOfActionDescriptions = []
26833+
26834+ for (let i = 0; i < Number(localsettings.agentCOTMax) + 1 && (currentOrderOfActionsOverall.length === 0 || i < currentOrderOfActionsOverall.length + 1) && endCurrent === false; i++)
2676026835 {
2676126836 clearSuggestions()
26762- // Ensure valid commands does not include stop thinking right away to ensure an action of some type is taken
26763- let validCommands = getEnabledCommands().map(command => command.name).filter(name => i != 0 || name != "stop_thinking")
2676426837
26765- let currentOrderOfActions = getDocumentFromTextDB('Order of actions')
26766- currentOrderOfActions = !!currentOrderOfActions ? currentOrderOfActions.split(",").filter(act => !!act) : []
26767- let nextAction = currentOrderOfActions.splice(i).filter(acts => acts.split("|").find(act => validCommands.includes(act)))
26768- nextAction = nextAction.length > 0 ? getCommands().filter(act => nextAction[0].split("|").includes(act.name)) : getEnabledCommands().filter(command => validCommands.includes(command.name))
26838+ let nextAction = []
26839+ let validCommands = getEnabledCommands().map(command => command.name).filter(name => i != 0 || name != "stop_thinking")
26840+ if (i == 0)
26841+ {
26842+ nextAction = getReasoningCommand()
26843+ }
26844+ else
26845+ {
26846+ // Ensure valid commands does not include stop thinking right away to ensure an action of some type is taken
26847+ nextAction = JSON.parse(JSON.stringify(currentOrderOfActionsOverall)).splice(i - 1).filter(acts => acts.split("|").find(act => validCommands.includes(act)))
26848+ nextAction = nextAction.length > 0 ? getCommands().filter(act => nextAction[0].split("|").includes(act.name)) : getEnabledCommands().filter(command => validCommands.includes(command.name))
2676926849
26770- // Find any actions which have occured more than the max repeats in settings and remove them from the options
26771- let actionsOverMaxRepeats = recentActions.map(elem => elem?.command?.name).reduce((o, c) => {
26772- let elem = o.find(e => e.name == c)
26773- if (elem === undefined) {
26774- o.push({name: c, repeats: 1})
26775- } else {
26776- elem.repeats++
26850+ // Find any actions which have occured more than the max repeats in settings and remove them from the options
26851+ if (currentOrderOfActionsOverall.length === 0)
26852+ {
26853+ let actionsOverMaxRepeats = recentActions.map(elem => elem?.command?.name).reduce((o, c) => {
26854+ let elem = o.find(e => e.name == c)
26855+ if (elem === undefined) {
26856+ o.push({name: c, repeats: 1})
26857+ } else {
26858+ elem.repeats++
26859+ }
26860+ return o
26861+ }, []).filter(o => o.repeats >= localsettings.agentCOTRepeatsMax).map(o => o.name)
26862+ nextAction = nextAction.filter(act => !actionsOverMaxRepeats.includes(act.name))
2677726863 }
26778- return o
26779- }, []).filter(o => o.repeats >= localsettings.agentCOTRepeatsMax).map(o => o.name)
26780- nextAction = nextAction.filter(act => !actionsOverMaxRepeats.includes(act.name))
26864+ }
26865+
2678126866
2678226867 // Find actions which are identical and have run twice - then remove them from the possible options to run again
2678326868 let duplicateActions = recentActions.filter((elem, pos, arr) => arr.findIndex((elem2) => JSON.stringify(elem) === JSON.stringify(elem2)) !== pos).map(elem => elem?.command?.name)
2678426869 nextAction = nextAction.filter(act => !duplicateActions.includes(act.name))
2678526870
26871+ // If no actions present, end cycle
26872+ if (nextAction.length === 0)
26873+ {
26874+ isCompleted = true
26875+ hasAttemptedToCompleteOnce = true
26876+ continue
26877+ }
2678626878 let jsonGrammar = await getCommandsGNBF(nextAction)
2678726879
2678826880 currentChainOfThought = currentChainOfThought.splice(-maxActionsInHistory)
@@ -26803,7 +26895,18 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2680326895 let history = getInitialAgentPrompt(nextAction, max_mem_len)
2680426896 let wiToInclude = createSysPrompt(substring_to_boundary(getWorldInfoForAgent(truncated_context, max_wi_len) + "\n\n" + textDBResults, max_wi_len))
2680526897 let anToInclude = !!current_anote ? createSysPrompt(substring_to_boundary(current_anotetemplate.replace("<|>", current_anote), max_anote_len)) : ""
26806- let finalAgentPrompt = getFinalAgentPrompt(currentOrderOfActions)
26898+
26899+ let promptOverview = currentOrderOfActionDescriptionsOverall.length > 0 ? currentOrderOfActionDescriptionsOverall[i - 1] : null
26900+ if (i === 0)
26901+ {
26902+ let planningPrompt = "The last action from the user is the instruction. If you need to ask the user for a response, the action ask_user must be used and be put as the final action in the order. Produces a list of actions to respond to this instruction."
26903+ // if (localsettings.inject_chatnames_instruct) {
26904+ // planningPrompt += ` You must respond as ${localsettings.chatopponent} when using the send_message or ask_user actions.`
26905+ // }
26906+ promptOverview = planningPrompt
26907+ }
26908+
26909+ let finalAgentPrompt = getFinalAgentPrompt(nextAction, currentOrderOfActionsOverall, promptOverview, initialPrompt)
2680726910
2680826911 let cotAsText = "", maxLengthOfCot = max_allowed_characters - history.length - wiToInclude.length - anToInclude.length - finalAgentPrompt.length
2680926912 for (let j = currentChainOfThought.length - 1; j >= 0; j--) {
@@ -26846,18 +26949,15 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2684626949 let json;
2684726950 if (resp.indexOf("stop_thinking") !== -1)
2684826951 {
26849- json = {
26850- command: {
26851- name: "stop_thinking",
26852- args: null
26853- }
26854- }
26952+ isCompleted = true
26953+ hasAttemptedToCompleteOnce = true
26954+ continue
2685526955 }
2685626956 else
2685726957 {
2685826958 json = JSON.parse(resp)
2685926959 }
26860- if (!!json?.command && !!json.command?.name && validCommands.includes(json.command.name))
26960+ if (!!json?.command && !!json.command?.name && (json.command.name === "plan_actions" || validCommands.includes(json.command.name) ))
2686126961 {
2686226962 // If message has been sent before, skip processing it again and let the agent try again
2686326963 if (recentActions.find((elem) => JSON.stringify(elem) === JSON.stringify(json)) !== undefined)
@@ -26878,7 +26978,7 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2687826978 }
2687926979
2688026980 let isCompleted = false;
26881- let command = getCommands().find(command => command.name === action.command.name)
26981+ let command = [...getReasoningCommand(), ... getCommands()] .find(command => command.name === action.command.name)
2688226982 if (!!command && command?.executor !== undefined) {
2688326983 let res = await command.executor(action.command)
2688426984 if (res === true) {
@@ -26925,6 +27025,7 @@ let checkFinalThoughtsPrompt = `Action: {"command":{"name":"thought","args":{"me
2692527025 }
2692627026 catch (e)
2692727027 {
27028+ console.log(i)
2692827029 addThought(createSysPrompt, `Chain of thought had an exception: ${e}`)
2692927030 }
2693027031 }
0 commit comments