Skip to content

πŸ™Œ OpenHands: AI-Driven Development

test: strengthen skill-ready content formatting (#17353)

aivong-openhandsa53da17ec301
diff --git a/.pr/refactor-skill-ready-content-vitest.png b/.pr/refactor-skill-ready-content-vitest.png
new file mode 100644
index 0000000000..41abde257c
Binary files /dev/null and b/.pr/refactor-skill-ready-content-vitest.png differ
diff --git a/__tests__/components/conversation-events/chat/event-content-helpers/get-skill-ready-items.test.ts b/__tests__/components/conversation-events/chat/event-content-helpers/get-skill-ready-items.test.ts
index b322a13d5f..db30a8f07a 100644
--- a/__tests__/components/conversation-events/chat/event-content-helpers/get-skill-ready-items.test.ts
+++ b/__tests__/components/conversation-events/chat/event-content-helpers/get-skill-ready-items.test.ts
@@ -1,6 +1,9 @@
import { describe, expect, it } from "vitest";
-import { getSkillReadyItems } from "#/components/conversation-events/chat/event-content-helpers/get-skill-ready-content";
-import { TextContent } from "#/types/agent-server/core/base/common";
+import {
+ getSkillReadyContent,
+ getSkillReadyItems,
+} from "#/components/conversation-events/chat/event-content-helpers/get-skill-ready-content";
+import type { TextContent } from "#/types/agent-server/core/base/common";
const makeTextContent = (text: string): TextContent[] => [
{ type: "text", text },
@@ -72,3 +75,109 @@ describe("getSkillReadyItems", () => {
expect(items[0].content).toBe("trimmed content");
});
});
+
+describe("skill-ready markdown formatting", () => {
+ it("pairs skills with trimmed EXTRA_INFO blocks using exact markdown spacing", () => {
+ const skills = ["docker", "gitlab"];
+ const extended: TextContent[] = [
+ {
+ type: "text",
+ text: "prefix <extra_info> Docker guide",
+ },
+ {
+ type: "text",
+ text: "\nwith two lines </extra_info> ignored ",
+ },
+ {
+ type: "text",
+ text: "<EXTRA_INFO> GitLab guide </EXTRA_INFO> suffix",
+ },
+ ];
+
+ expect(getSkillReadyContent(skills, extended)).toBe(
+ [
+ "",
+ "",
+ "**Triggered Skill Knowledge:**",
+ "",
+ "- **docker**",
+ "",
+ "Docker guide\nwith two lines",
+ "",
+ "- **gitlab**",
+ "",
+ "GitLab guide",
+ ].join("\n"),
+ );
+ });
+
+ it("renders a skill without extra spacing when no matching block exists", () => {
+ const extended = makeTextContent(wrapExtraInfo("Docker guide"));
+
+ expect(getSkillReadyContent(["docker", "gitlab"], extended)).toBe(
+ [
+ "",
+ "",
+ "**Triggered Skill Knowledge:**",
+ "",
+ "- **docker**",
+ "",
+ "Docker guide",
+ "",
+ "- **gitlab**",
+ ].join("\n"),
+ );
+ });
+
+ it("ignores unpaired EXTRA_INFO blocks when skills are present", () => {
+ const extended = makeTextContent(
+ `${wrapExtraInfo("Docker guide")}${wrapExtraInfo("Unused guide")}`,
+ );
+
+ expect(getSkillReadyContent(["docker"], extended)).toBe(
+ "\n\n**Triggered Skill Knowledge:**\n\n- **docker**\n\nDocker guide",
+ );
+ });
+
+ it("formats extracted blocks as numbered-free extended content without skills", () => {
+ const extended = makeTextContent(
+ `${wrapExtraInfo(" First block ")}${wrapExtraInfo("Second block")}`,
+ );
+
+ expect(getSkillReadyContent([], extended)).toBe(
+ "\n\n**Extended Content:**\n\nFirst block\n\nSecond block",
+ );
+ });
+
+ it("returns empty content when no complete non-empty EXTRA_INFO block exists", () => {
+ const extended = makeTextContent(
+ `plain text ${wrapExtraInfo(" ")} <EXTRA_INFO>unclosed`,
+ );
+
+ expect(getSkillReadyContent([], extended)).toBe("");
+ });
+
+ it("ignores non-text runtime entries while joining surrounding text", () => {
+ const extended = [
+ { type: "text", text: "<EXTRA_INFO>joined" },
+ {
+ type: "image",
+ image_urls: ["data:image/png;base64,ignored"],
+ text: "<EXTRA_INFO>poisoned image text</EXTRA_INFO>",
+ },
+ { type: "text", text: " block</EXTRA_INFO>" },
+ ] as unknown as TextContent[];
+
+ expect(getSkillReadyContent([], extended)).toBe(
+ "\n\n**Extended Content:**\n\njoined block",
+ );
+ });
+
+ it("falls back safely when legacy runtime data omits both arrays", () => {
+ const missingSkills = undefined as unknown as string[];
+ const missingContent = undefined as unknown as TextContent[];
+
+ expect(getSkillReadyContent(missingSkills, missingContent)).toBe("");
+ expect(getSkillReadyItems(missingSkills, missingContent)).toEqual([]);
+ });
+});
diff --git a/src/components/conversation-events/chat/event-content-helpers/get-skill-ready-content.ts b/src/components/conversation-events/chat/event-content-helpers/get-skill-ready-content.ts
index 021eed2a85..f5666f971c 100644
--- a/src/components/conversation-events/chat/event-content-helpers/get-skill-ready-content.ts
+++ b/src/components/conversation-events/chat/event-content-helpers/get-skill-ready-content.ts
@@ -11,30 +11,22 @@ export interface SkillReadyItem {
/**
* Extracts all text content from an array of TextContent items.
*/
-const extractAllText = (extendedContent: TextContent[]): string =>
- extendedContent
+function extractAllText(extendedContent: TextContent[]): string {
+ return extendedContent
.filter((c) => c.type === "text")
.map((c) => c.text)
.join("");
+}
/**
* Extracts all <EXTRA_INFO> blocks from the given text.
* Returns an array of content strings (without the wrapper tags).
*/
const extractExtraInfoBlocks = (text: string): string[] => {
- const blocks: string[] = [];
const blockRegex = /<EXTRA_INFO>([\s\S]*?)<\/EXTRA_INFO>/gi;
- let match = blockRegex.exec(text);
-
- while (match !== null) {
- const blockContent = match[1].trim();
- if (blockContent.length > 0) {
- blocks.push(blockContent);
- }
- match = blockRegex.exec(text);
- }
-
- return blocks;
+ return Array.from(text.matchAll(blockRegex), (match) =>
+ match[1].trim(),
+ ).filter(Boolean);
};
/**
@@ -46,7 +38,7 @@ const formatSkillWithContent = (
): string => {
let formatted = `\n\n- **${skill}**`;
- if (contentBlock && contentBlock.trim().length > 0) {
+ if (contentBlock) {
formatted += `\n\n${contentBlock}`;
}
@@ -63,8 +55,7 @@ const formatSkillKnowledge = (
let content = `\n\n**Triggered Skill Knowledge:**`;
activatedSkills.forEach((skill, index) => {
- const contentBlock =
- index < extraInfoBlocks.length ? extraInfoBlocks[index] : undefined;
+ const contentBlock = extraInfoBlocks[index];
content += formatSkillWithContent(skill, contentBlock);
});
@@ -78,9 +69,7 @@ const formatExtendedContentOnly = (extraInfoBlocks: string[]): string => {
let content = `\n\n**Extended Content:**`;
extraInfoBlocks.forEach((block) => {
- if (block.trim().length > 0) {
- content += `\n\n${block}`;
- }
+ content += `\n\n${block}`;
});
return content;
@@ -89,8 +78,8 @@ const formatExtendedContentOnly = (extraInfoBlocks: string[]): string => {
/**
* Extracts EXTRA_INFO blocks from extended content.
*/
-const getExtraInfoBlocks = (extendedContent: TextContent[]): string[] => {
- if (!extendedContent || extendedContent.length === 0) return [];
+const getExtraInfoBlocks = (extendedContent?: TextContent[]): string[] => {
+ if (!extendedContent) return [];
return extractExtraInfoBlocks(extractAllText(extendedContent));
};
@@ -128,19 +117,12 @@ export const getSkillReadyItems = (
if (activatedSkills && activatedSkills.length > 0) {
return activatedSkills.map((skill, index) => ({
name: skill,
- content:
- index < extraInfoBlocks.length ? extraInfoBlocks[index].trim() : "",
+ content: extraInfoBlocks[index] ?? "",
}));
}
- if (extraInfoBlocks.length > 0) {
- return extraInfoBlocks
- .filter((block) => block.trim().length > 0)
- .map((block, index) => ({
- name: `Extended Content ${index + 1}`,
- content: block.trim(),
- }));
- }
-
- return [];
+ return extraInfoBlocks.map((block, index) => ({
+ name: `Extended Content ${index + 1}`,
+ content: block,
+ }));
};