feat(agent): migrate to pi-agent-core harness and restore robust frontmatter parsing

This commit is contained in:
yiling
2026-05-21 13:14:55 +08:00
parent 2e11748beb
commit 94cea7b713
5 changed files with 94 additions and 80 deletions
+3 -11
View File
@@ -4,9 +4,9 @@ import { s3Service } from '@/services/s3/s3'
import AdmZip from 'adm-zip'
import * as crypto from 'crypto'
import { ulid } from 'ulid'
import { parse } from 'yaml'
import { parseFrontmatter } from '@/utils/frontmatter'
interface SkillFrontmatter {
interface SkillFrontmatter extends Record<string, unknown> {
name?: string
description?: string
}
@@ -258,15 +258,7 @@ export class SkillService {
}
private parseSkillMd(content: string): { name: string; description: string } {
const match = content.match(/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n([\s\S]*))?$/)
let frontmatter: SkillFrontmatter = {}
if (match) {
try {
frontmatter = parse(match[1])
} catch (e) {
console.error('Failed to parse skill frontmatter', e)
}
}
const { frontmatter } = parseFrontmatter<SkillFrontmatter>(content)
const name = frontmatter.name || 'Unnamed Skill'
const description = frontmatter.description || ''
+40
View File
@@ -0,0 +1,40 @@
import { describe, expect, it } from 'vitest'
import { parseFrontmatter } from './frontmatter'
describe('frontmatter parser', () => {
it('should parse content with frontmatter', () => {
const content = `---
name: test-skill
description: simple skill
---
# Body
Hello world`
const { frontmatter, body } = parseFrontmatter<{ name: string; description: string }>(content)
expect(frontmatter.name).toBe('test-skill')
expect(frontmatter.description).toBe('simple skill')
expect(body).toBe('# Body\nHello world')
})
it('should handle content without frontmatter', () => {
const content = '# No Frontmatter\nJust text'
const { frontmatter, body } = parseFrontmatter(content)
expect(frontmatter).toEqual({})
expect(body).toBe(content)
})
it('should handle invalid frontmatter delimiters', () => {
const content = `---
no-end-delimiter
# Body`
const { frontmatter, body } = parseFrontmatter(content)
expect(frontmatter).toEqual({})
expect(body).toBe(content)
})
it('should normalize newlines', () => {
const content = '---\r\nname: win\r\n---\r\n# Body'
const { frontmatter, body } = parseFrontmatter<{ name: string }>(content)
expect(frontmatter.name).toBe('win')
expect(body).toBe('# Body')
})
})
+40
View File
@@ -0,0 +1,40 @@
import { parse } from 'yaml'
export type ParsedFrontmatter<T extends Record<string, unknown>> = {
frontmatter: T
body: string
}
const normalizeNewlines = (value: string): string =>
value.replace(/\r\n/g, '\n').replace(/\r/g, '\n')
const extractFrontmatter = (content: string): { yamlString: string | null; body: string } => {
const normalized = normalizeNewlines(content)
if (!normalized.startsWith('---')) {
return { yamlString: null, body: normalized }
}
const endIndex = normalized.indexOf('\n---', 3)
if (endIndex === -1) {
return { yamlString: null, body: normalized }
}
return {
yamlString: normalized.slice(4, endIndex),
body: normalized.slice(endIndex + 4).trim(),
}
}
export const parseFrontmatter = <T extends Record<string, unknown> = Record<string, unknown>>(
content: string,
): ParsedFrontmatter<T> => {
const { yamlString, body } = extractFrontmatter(content)
if (!yamlString) {
return { frontmatter: {} as T, body }
}
const parsed = parse(yamlString)
return { frontmatter: (parsed ?? {}) as T, body }
}
export const stripFrontmatter = (content: string): string => parseFrontmatter(content).body