Saoud Rizwan 1 year ago
parent
commit
da505170d4
3 changed files with 41 additions and 22 deletions
  1. 12 2
      src/api/providers/openrouter.ts
  2. 7 0
      src/core/Cline.ts
  3. 22 20
      src/core/prompts/responses.ts

+ 12 - 2
src/api/providers/openrouter.ts

@@ -107,6 +107,13 @@ export class OpenRouterHandler implements ApiHandler {
 					text: delta.content,
 				}
 			}
+			if (chunk.usage) {
+				yield {
+					type: "usage",
+					inputTokens: chunk.usage.prompt_tokens || 0,
+					outputTokens: chunk.usage.completion_tokens || 0,
+				}
+			}
 		}
 
 		try {
@@ -121,10 +128,13 @@ export class OpenRouterHandler implements ApiHandler {
 			console.log("OpenRouter generation details:", response.data)
 			yield {
 				type: "usage",
-				inputTokens: generation?.native_tokens_prompt || 0,
-				outputTokens: generation?.native_tokens_completion || 0,
 				// cacheWriteTokens: 0,
 				// cacheReadTokens: 0,
+				// openrouter generation endpoint fails often, so we'll report tokens from stream as normal
+				// inputTokens: generation?.native_tokens_prompt || 0,
+				// outputTokens: generation?.native_tokens_completion || 0,
+				inputTokens: 0,
+				outputTokens: 0,
 				totalCost: generation?.total_cost || 0,
 			}
 		} catch (error) {

+ 7 - 0
src/core/Cline.ts

@@ -973,6 +973,13 @@ export class Cline {
 							newContent = newContent.split("\n").slice(0, -1).join("\n").trim()
 						}
 
+						if (
+							this.api.getModel().id.includes("llama") &&
+							(newContent.includes(">") || newContent.includes("<"))
+						) {
+							newContent = newContent.replace(/&gt;/g, ">").replace(/&lt;/g, "<")
+						}
+
 						const sharedMessageProps: ClineSayTool = {
 							tool: fileExists ? "editedExistingFile" : "newFileCreated",
 							path: getReadablePath(cwd, removeClosingTag("path", relPath)),

+ 22 - 20
src/core/prompts/responses.ts

@@ -13,25 +13,7 @@ export const formatResponse = {
 	noToolsUsed: () =>
 		`[ERROR] You did not use a tool in your previous response! Please retry with a tool use.
 
-# Instructions for Tool Use
-
-Tool uses are formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure:
-
-<tool_name>
-<parameter1_name>value1</parameter1_name>
-<parameter2_name>value2</parameter2_name>
-...
-</tool_name>
-
-For example:
-
-<attempt_completion>
-<result>
-I have completed the task...
-</result>
-</attempt_completion>
-
-Always adhere to this format for all tool uses to ensure proper parsing and execution.
+${toolUseInstructionsReminder}
 
 # Next Steps
 
@@ -44,7 +26,7 @@ Otherwise, if you have not completed the task and do not need additional informa
 		`You seem to be having trouble proceeding. The user has provided the following feedback to help guide you:\n<feedback>\n${feedback}\n</feedback>`,
 
 	missingToolParameterError: (paramName: string) =>
-		`Missing value for required parameter '${paramName}'. Please retry with complete response.`,
+		`Missing value for required parameter '${paramName}'. Please retry with complete response.\n\n${toolUseInstructionsReminder}`,
 
 	toolResult: (
 		text: string,
@@ -126,3 +108,23 @@ const formatImagesIntoBlocks = (images?: string[]): Anthropic.ImageBlockParam[]
 		  })
 		: []
 }
+
+const toolUseInstructionsReminder = `# Reminder: Instructions for Tool Use
+
+Tool uses are formatted using XML-style tags. The tool name is enclosed in opening and closing tags, and each parameter is similarly enclosed within its own set of tags. Here's the structure:
+
+<tool_name>
+<parameter1_name>value1</parameter1_name>
+<parameter2_name>value2</parameter2_name>
+...
+</tool_name>
+
+For example:
+
+<attempt_completion>
+<result>
+I have completed the task...
+</result>
+</attempt_completion>
+
+Always adhere to this format for all tool uses to ensure proper parsing and execution.`