Skip to content

Commit e95fbc0

Browse files
committed
refactor: share agent harness loader helpers
1 parent cde6aff commit e95fbc0

4 files changed

Lines changed: 139 additions & 363 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { parse } from "yaml";
2+
import { type ExecutionEnv, type FileInfo, type Result, toError } from "./types.js";
3+
4+
export interface FileInfoDiagnostic {
5+
type: "warning";
6+
code: "file_info_failed";
7+
message: string;
8+
path: string;
9+
}
10+
11+
interface FileInfoDiagnostics {
12+
push(diagnostic: FileInfoDiagnostic): unknown;
13+
}
14+
15+
export function parseFrontmatter(
16+
content: string,
17+
): Result<{ frontmatter: Record<string, unknown>; body: string }, Error> {
18+
try {
19+
const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
20+
if (!normalized.startsWith("---")) {
21+
return { ok: true, value: { frontmatter: {}, body: normalized } };
22+
}
23+
const endIndex = normalized.indexOf("\n---", 3);
24+
if (endIndex === -1) {
25+
return { ok: true, value: { frontmatter: {}, body: normalized } };
26+
}
27+
const yamlString = normalized.slice(4, endIndex);
28+
const body = normalized.slice(endIndex + 4).trim();
29+
return {
30+
ok: true,
31+
value: { frontmatter: (parse(yamlString) ?? {}) as Record<string, unknown>, body },
32+
};
33+
} catch (error) {
34+
return { ok: false, error: toError(error) };
35+
}
36+
}
37+
38+
export async function resolveFileInfoKind(
39+
env: ExecutionEnv,
40+
info: FileInfo,
41+
diagnostics: FileInfoDiagnostics,
42+
): Promise<"file" | "directory" | undefined> {
43+
if (info.kind === "file" || info.kind === "directory") {
44+
return info.kind;
45+
}
46+
const canonicalPath = await env.canonicalPath(info.path);
47+
if (!canonicalPath.ok) {
48+
if (canonicalPath.error.code !== "not_found") {
49+
diagnostics.push({
50+
type: "warning",
51+
code: "file_info_failed",
52+
message: canonicalPath.error.message,
53+
path: info.path,
54+
});
55+
}
56+
return undefined;
57+
}
58+
const target = await env.fileInfo(canonicalPath.value);
59+
if (!target.ok) {
60+
if (target.error.code !== "not_found") {
61+
diagnostics.push({
62+
type: "warning",
63+
code: "file_info_failed",
64+
message: target.error.message,
65+
path: info.path,
66+
});
67+
}
68+
return undefined;
69+
}
70+
return target.value.kind === "file" || target.value.kind === "directory"
71+
? target.value.kind
72+
: undefined;
73+
}
74+
75+
export function joinEnvPath(base: string, child: string): string {
76+
return `${base.replace(/\/+$/, "")}/${child.replace(/^\/+/, "")}`;
77+
}
78+
79+
export function dirnameEnvPath(path: string): string {
80+
const normalized = path.replace(/\/+$/, "");
81+
const slashIndex = normalized.lastIndexOf("/");
82+
return slashIndex <= 0 ? "/" : normalized.slice(0, slashIndex);
83+
}
84+
85+
export function basenameEnvPath(path: string): string {
86+
const normalized = path.replace(/\/+$/, "");
87+
const slashIndex = normalized.lastIndexOf("/");
88+
return slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);
89+
}
90+
91+
export function relativeEnvPath(root: string, path: string): string {
92+
const normalizedRoot = root.replace(/\/+$/, "");
93+
const normalizedPath = path.replace(/\/+$/, "");
94+
if (normalizedPath === normalizedRoot) {
95+
return "";
96+
}
97+
return normalizedPath.startsWith(`${normalizedRoot}/`)
98+
? normalizedPath.slice(normalizedRoot.length + 1)
99+
: normalizedPath.replace(/^\/+/, "");
100+
}

‎packages/agent-core/src/harness/prompt-templates.ts‎

Lines changed: 5 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { parse } from "yaml";
21
import {
3-
type ExecutionEnv,
4-
type FileInfo,
5-
type PromptTemplate,
6-
type Result,
7-
toError,
8-
} from "./types.js";
2+
basenameEnvPath,
3+
parseFrontmatter,
4+
resolveFileInfoKind as resolveKind,
5+
} from "./file-loader-utils.js";
6+
import { type ExecutionEnv, type PromptTemplate, type Result } from "./types.js";
97

108
export type PromptTemplateDiagnosticCode =
119
| "file_info_failed"
@@ -190,72 +188,6 @@ async function loadTemplateFromFile(
190188
};
191189
}
192190

193-
async function resolveKind(
194-
env: ExecutionEnv,
195-
info: FileInfo,
196-
diagnostics: PromptTemplateDiagnostic[],
197-
): Promise<"file" | "directory" | undefined> {
198-
if (info.kind === "file" || info.kind === "directory") {
199-
return info.kind;
200-
}
201-
const canonicalPath = await env.canonicalPath(info.path);
202-
if (!canonicalPath.ok) {
203-
if (canonicalPath.error.code !== "not_found") {
204-
diagnostics.push({
205-
type: "warning",
206-
code: "file_info_failed",
207-
message: canonicalPath.error.message,
208-
path: info.path,
209-
});
210-
}
211-
return undefined;
212-
}
213-
const target = await env.fileInfo(canonicalPath.value);
214-
if (!target.ok) {
215-
if (target.error.code !== "not_found") {
216-
diagnostics.push({
217-
type: "warning",
218-
code: "file_info_failed",
219-
message: target.error.message,
220-
path: info.path,
221-
});
222-
}
223-
return undefined;
224-
}
225-
return target.value.kind === "file" || target.value.kind === "directory"
226-
? target.value.kind
227-
: undefined;
228-
}
229-
230-
function parseFrontmatter(
231-
content: string,
232-
): Result<{ frontmatter: Record<string, unknown>; body: string }, Error> {
233-
try {
234-
const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
235-
if (!normalized.startsWith("---")) {
236-
return { ok: true, value: { frontmatter: {}, body: normalized } };
237-
}
238-
const endIndex = normalized.indexOf("\n---", 3);
239-
if (endIndex === -1) {
240-
return { ok: true, value: { frontmatter: {}, body: normalized } };
241-
}
242-
const yamlString = normalized.slice(4, endIndex);
243-
const body = normalized.slice(endIndex + 4).trim();
244-
return {
245-
ok: true,
246-
value: { frontmatter: (parse(yamlString) ?? {}) as Record<string, unknown>, body },
247-
};
248-
} catch (error) {
249-
return { ok: false, error: toError(error) };
250-
}
251-
}
252-
253-
function basenameEnvPath(path: string): string {
254-
const normalized = path.replace(/\/+$/, "");
255-
const slashIndex = normalized.lastIndexOf("/");
256-
return slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);
257-
}
258-
259191
/** Parse an argument string using simple shell-style single and double quotes. */
260192
export function parseCommandArgs(argsString: string): string[] {
261193
const args: string[] = [];

‎packages/agent-core/src/harness/skills.ts‎

Lines changed: 9 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import ignore from "ignore";
2-
import { parse } from "yaml";
3-
import { type ExecutionEnv, type FileInfo, type Result, type Skill, toError } from "./types.js";
2+
import {
3+
basenameEnvPath,
4+
dirnameEnvPath,
5+
joinEnvPath,
6+
parseFrontmatter,
7+
relativeEnvPath,
8+
resolveFileInfoKind as resolveKind,
9+
} from "./file-loader-utils.js";
10+
import { type ExecutionEnv, type Result, type Skill } from "./types.js";
411

512
const MAX_NAME_LENGTH = 64;
613
const MAX_DESCRIPTION_LENGTH = 1024;
@@ -374,90 +381,3 @@ function validateDescription(description: string | undefined): string[] {
374381
}
375382
return errors;
376383
}
377-
378-
function parseFrontmatter(
379-
content: string,
380-
): Result<{ frontmatter: Record<string, unknown>; body: string }, Error> {
381-
try {
382-
const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
383-
if (!normalized.startsWith("---")) {
384-
return { ok: true, value: { frontmatter: {}, body: normalized } };
385-
}
386-
const endIndex = normalized.indexOf("\n---", 3);
387-
if (endIndex === -1) {
388-
return { ok: true, value: { frontmatter: {}, body: normalized } };
389-
}
390-
const yamlString = normalized.slice(4, endIndex);
391-
const body = normalized.slice(endIndex + 4).trim();
392-
return {
393-
ok: true,
394-
value: { frontmatter: (parse(yamlString) ?? {}) as Record<string, unknown>, body },
395-
};
396-
} catch (error) {
397-
return { ok: false, error: toError(error) };
398-
}
399-
}
400-
401-
async function resolveKind(
402-
env: ExecutionEnv,
403-
info: FileInfo,
404-
diagnostics: SkillDiagnostic[],
405-
): Promise<"file" | "directory" | undefined> {
406-
if (info.kind === "file" || info.kind === "directory") {
407-
return info.kind;
408-
}
409-
const canonicalPath = await env.canonicalPath(info.path);
410-
if (!canonicalPath.ok) {
411-
if (canonicalPath.error.code !== "not_found") {
412-
diagnostics.push({
413-
type: "warning",
414-
code: "file_info_failed",
415-
message: canonicalPath.error.message,
416-
path: info.path,
417-
});
418-
}
419-
return undefined;
420-
}
421-
const target = await env.fileInfo(canonicalPath.value);
422-
if (!target.ok) {
423-
if (target.error.code !== "not_found") {
424-
diagnostics.push({
425-
type: "warning",
426-
code: "file_info_failed",
427-
message: target.error.message,
428-
path: info.path,
429-
});
430-
}
431-
return undefined;
432-
}
433-
return target.value.kind === "file" || target.value.kind === "directory"
434-
? target.value.kind
435-
: undefined;
436-
}
437-
438-
function joinEnvPath(base: string, child: string): string {
439-
return `${base.replace(/\/+$/, "")}/${child.replace(/^\/+/, "")}`;
440-
}
441-
442-
function dirnameEnvPath(path: string): string {
443-
const normalized = path.replace(/\/+$/, "");
444-
const slashIndex = normalized.lastIndexOf("/");
445-
return slashIndex <= 0 ? "/" : normalized.slice(0, slashIndex);
446-
}
447-
448-
function basenameEnvPath(path: string): string {
449-
const normalized = path.replace(/\/+$/, "");
450-
const slashIndex = normalized.lastIndexOf("/");
451-
return slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);
452-
}
453-
454-
function relativeEnvPath(root: string, path: string): string {
455-
const normalizedRoot = root.replace(/\/+$/, "");
456-
const normalizedPath = path.replace(/\/+$/, "");
457-
if (normalizedPath === normalizedRoot) {
458-
return "";
459-
}
460-
return normalizedPath.startsWith(`${normalizedRoot}/`)
461-
? normalizedPath.slice(normalizedRoot.length + 1)
462-
: normalizedPath.replace(/^\/+/, "");
463-
}

0 commit comments

Comments
 (0)