@eminent337/aery-core 0.67.120 → 0.67.121
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/dist/agent-loop.d.ts.map +1 -1
- package/dist/agent-loop.js +31 -1
- package/dist/agent-loop.js.map +1 -1
- package/dist/agent.d.ts +4 -3
- package/dist/agent.d.ts.map +1 -1
- package/dist/agent.js +7 -3
- package/dist/agent.js.map +1 -1
- package/dist/harness/agent-harness.d.ts +103 -0
- package/dist/harness/agent-harness.d.ts.map +1 -0
- package/dist/harness/agent-harness.js +788 -0
- package/dist/harness/agent-harness.js.map +1 -0
- package/dist/harness/compaction/branch-summarization.d.ts +88 -0
- package/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
- package/dist/harness/compaction/branch-summarization.js +243 -0
- package/dist/harness/compaction/branch-summarization.js.map +1 -0
- package/dist/harness/compaction/compaction.d.ts +122 -0
- package/dist/harness/compaction/compaction.d.ts.map +1 -0
- package/dist/harness/compaction/compaction.js +631 -0
- package/dist/harness/compaction/compaction.js.map +1 -0
- package/dist/harness/compaction/utils.d.ts +38 -0
- package/dist/harness/compaction/utils.d.ts.map +1 -0
- package/dist/harness/compaction/utils.js +153 -0
- package/dist/harness/compaction/utils.js.map +1 -0
- package/dist/harness/env/nodejs.d.ts +50 -0
- package/dist/harness/env/nodejs.d.ts.map +1 -0
- package/dist/harness/env/nodejs.js +487 -0
- package/dist/harness/env/nodejs.js.map +1 -0
- package/dist/harness/execution-env.d.ts +4 -0
- package/dist/harness/execution-env.d.ts.map +1 -0
- package/dist/harness/execution-env.js +3 -0
- package/dist/harness/execution-env.js.map +1 -0
- package/dist/harness/factory.d.ts +6 -0
- package/dist/harness/factory.d.ts.map +1 -0
- package/dist/harness/factory.js +9 -0
- package/dist/harness/factory.js.map +1 -0
- package/dist/harness/messages.d.ts +51 -0
- package/dist/harness/messages.d.ts.map +1 -0
- package/dist/harness/messages.js +102 -0
- package/dist/harness/messages.js.map +1 -0
- package/dist/harness/prompt-templates.d.ts +47 -0
- package/dist/harness/prompt-templates.d.ts.map +1 -0
- package/dist/harness/prompt-templates.js +201 -0
- package/dist/harness/prompt-templates.js.map +1 -0
- package/dist/harness/session/jsonl-repo.d.ts +26 -0
- package/dist/harness/session/jsonl-repo.d.ts.map +1 -0
- package/dist/harness/session/jsonl-repo.js +97 -0
- package/dist/harness/session/jsonl-repo.js.map +1 -0
- package/dist/harness/session/jsonl-storage.d.ts +33 -0
- package/dist/harness/session/jsonl-storage.d.ts.map +1 -0
- package/dist/harness/session/jsonl-storage.js +159 -0
- package/dist/harness/session/jsonl-storage.js.map +1 -0
- package/dist/harness/session/memory-repo.d.ts +18 -0
- package/dist/harness/session/memory-repo.d.ts.map +1 -0
- package/dist/harness/session/memory-repo.js +42 -0
- package/dist/harness/session/memory-repo.js.map +1 -0
- package/dist/harness/session/memory-storage.d.ts +26 -0
- package/dist/harness/session/memory-storage.d.ts.map +1 -0
- package/dist/harness/session/memory-storage.js +89 -0
- package/dist/harness/session/memory-storage.js.map +1 -0
- package/dist/harness/session/repo/jsonl.d.ts +20 -0
- package/dist/harness/session/repo/jsonl.d.ts.map +1 -0
- package/dist/harness/session/repo/jsonl.js +92 -0
- package/dist/harness/session/repo/jsonl.js.map +1 -0
- package/dist/harness/session/repo/memory.d.ts +18 -0
- package/dist/harness/session/repo/memory.d.ts.map +1 -0
- package/dist/harness/session/repo/memory.js +42 -0
- package/dist/harness/session/repo/memory.js.map +1 -0
- package/dist/harness/session/repo/shared.d.ts +10 -0
- package/dist/harness/session/repo/shared.d.ts.map +1 -0
- package/dist/harness/session/repo/shared.js +31 -0
- package/dist/harness/session/repo/shared.js.map +1 -0
- package/dist/harness/session/repo-utils.d.ts +10 -0
- package/dist/harness/session/repo-utils.d.ts.map +1 -0
- package/dist/harness/session/repo-utils.js +31 -0
- package/dist/harness/session/repo-utils.js.map +1 -0
- package/dist/harness/session/session.d.ts +32 -0
- package/dist/harness/session/session.d.ts.map +1 -0
- package/dist/harness/session/session.js +196 -0
- package/dist/harness/session/session.js.map +1 -0
- package/dist/harness/session/storage/jsonl.d.ts +30 -0
- package/dist/harness/session/storage/jsonl.d.ts.map +1 -0
- package/dist/harness/session/storage/jsonl.js +170 -0
- package/dist/harness/session/storage/jsonl.js.map +1 -0
- package/dist/harness/session/storage/memory.d.ts +26 -0
- package/dist/harness/session/storage/memory.d.ts.map +1 -0
- package/dist/harness/session/storage/memory.js +90 -0
- package/dist/harness/session/storage/memory.js.map +1 -0
- package/dist/harness/session/uuid.d.ts +2 -0
- package/dist/harness/session/uuid.d.ts.map +1 -0
- package/dist/harness/session/uuid.js +50 -0
- package/dist/harness/session/uuid.js.map +1 -0
- package/dist/harness/skills.d.ts +43 -0
- package/dist/harness/skills.d.ts.map +1 -0
- package/dist/harness/skills.js +255 -0
- package/dist/harness/skills.js.map +1 -0
- package/dist/harness/system-prompt.d.ts +3 -0
- package/dist/harness/system-prompt.d.ts.map +1 -0
- package/dist/harness/system-prompt.js +30 -0
- package/dist/harness/system-prompt.js.map +1 -0
- package/dist/harness/types.d.ts +578 -0
- package/dist/harness/types.d.ts.map +1 -0
- package/dist/harness/types.js +56 -0
- package/dist/harness/types.js.map +1 -0
- package/dist/harness/utils/shell-output.d.ts +14 -0
- package/dist/harness/utils/shell-output.d.ts.map +1 -0
- package/dist/harness/utils/shell-output.js +125 -0
- package/dist/harness/utils/shell-output.js.map +1 -0
- package/dist/harness/utils/truncate.d.ts +70 -0
- package/dist/harness/utils/truncate.d.ts.map +1 -0
- package/dist/harness/utils/truncate.js +288 -0
- package/dist/harness/utils/truncate.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -1
- package/dist/node.d.ts +3 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +3 -0
- package/dist/node.js.map +1 -0
- package/dist/proxy.d.ts.map +1 -1
- package/dist/proxy.js +5 -2
- package/dist/proxy.js.map +1 -1
- package/dist/types.d.ts +50 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +19 -2
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.d.ts","sourceRoot":"","sources":["../../src/harness/messages.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAC9E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,eAAO,MAAM,yBAAyB,wGAGrC,CAAC;AAEF,eAAO,MAAM,yBAAyB,iBAC3B,CAAC;AAEZ,eAAO,MAAM,qBAAqB,iGAGjC,CAAC;AAEF,eAAO,MAAM,qBAAqB,eAAe,CAAC;AAElD,MAAM,WAAW,oBAAoB;IACpC,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,GAAG,SAAS,CAAC;IAC7B,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,SAAS,EAAE,MAAM,CAAC;IAClB,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,aAAa,CAAC,CAAC,GAAG,OAAO;IACzC,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,GAAG,CAAC,WAAW,GAAG,YAAY,CAAC,EAAE,CAAC;IACjD,OAAO,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,CAAC,CAAC;IACZ,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,oBAAoB;IACpC,IAAI,EAAE,eAAe,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,wBAAwB;IACxC,IAAI,EAAE,mBAAmB,CAAC;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CAClB;AAED,OAAO,QAAQ,aAAa,CAAC,CAAC;IAC7B,UAAU,mBAAmB;QAC5B,aAAa,EAAE,oBAAoB,CAAC;QACpC,MAAM,EAAE,aAAa,CAAC;QACtB,aAAa,EAAE,oBAAoB,CAAC;QACpC,iBAAiB,EAAE,wBAAwB,CAAC;KAC5C;CACD;AAED,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,oBAAoB,GAAG,MAAM,CAgBrE;AAED,wBAAgB,0BAA0B,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,oBAAoB,CAOnH;AAED,wBAAgB,8BAA8B,CAC7C,OAAO,EAAE,MAAM,EACf,YAAY,EAAE,MAAM,EACpB,SAAS,EAAE,MAAM,GACf,wBAAwB,CAO1B;AAED,wBAAgB,mBAAmB,CAClC,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,GAAG,CAAC,WAAW,GAAG,YAAY,CAAC,EAAE,EAChD,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,OAAO,GAAG,SAAS,EAC5B,SAAS,EAAE,MAAM,GACf,aAAa,CASf;AAED,wBAAgB,YAAY,CAAC,QAAQ,EAAE,YAAY,EAAE,GAAG,OAAO,EAAE,CA4ChE","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@eminent337/aery-ai\";\nimport type { AgentMessage } from \"../types.js\";\n\nexport const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:\n\n<summary>\n`;\n\nexport const COMPACTION_SUMMARY_SUFFIX = `\n</summary>`;\n\nexport const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:\n\n<summary>\n`;\n\nexport const BRANCH_SUMMARY_SUFFIX = `</summary>`;\n\nexport interface BashExecutionMessage {\n\trole: \"bashExecution\";\n\tcommand: string;\n\toutput: string;\n\texitCode: number | undefined;\n\tcancelled: boolean;\n\ttruncated: boolean;\n\tfullOutputPath?: string;\n\ttimestamp: number;\n\texcludeFromContext?: boolean;\n}\n\nexport interface CustomMessage<T = unknown> {\n\trole: \"custom\";\n\tcustomType: string;\n\tcontent: string | (TextContent | ImageContent)[];\n\tdisplay: boolean;\n\tdetails?: T;\n\ttimestamp: number;\n}\n\nexport interface BranchSummaryMessage {\n\trole: \"branchSummary\";\n\tsummary: string;\n\tfromId: string;\n\ttimestamp: number;\n}\n\nexport interface CompactionSummaryMessage {\n\trole: \"compactionSummary\";\n\tsummary: string;\n\ttokensBefore: number;\n\ttimestamp: number;\n}\n\ndeclare module \"../types.js\" {\n\tinterface CustomAgentMessages {\n\t\tbashExecution: BashExecutionMessage;\n\t\tcustom: CustomMessage;\n\t\tbranchSummary: BranchSummaryMessage;\n\t\tcompactionSummary: CompactionSummaryMessage;\n\t}\n}\n\nexport function bashExecutionToText(msg: BashExecutionMessage): string {\n\tlet text = `Ran \\`${msg.command}\\`\\n`;\n\tif (msg.output) {\n\t\ttext += `\\`\\`\\`\\n${msg.output}\\n\\`\\`\\``;\n\t} else {\n\t\ttext += \"(no output)\";\n\t}\n\tif (msg.cancelled) {\n\t\ttext += \"\\n\\n(command cancelled)\";\n\t} else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {\n\t\ttext += `\\n\\nCommand exited with code ${msg.exitCode}`;\n\t}\n\tif (msg.truncated && msg.fullOutputPath) {\n\t\ttext += `\\n\\n[Output truncated. Full output: ${msg.fullOutputPath}]`;\n\t}\n\treturn text;\n}\n\nexport function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage {\n\treturn {\n\t\trole: \"branchSummary\",\n\t\tsummary,\n\t\tfromId,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function createCompactionSummaryMessage(\n\tsummary: string,\n\ttokensBefore: number,\n\ttimestamp: string,\n): CompactionSummaryMessage {\n\treturn {\n\t\trole: \"compactionSummary\",\n\t\tsummary,\n\t\ttokensBefore,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function createCustomMessage(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: unknown | undefined,\n\ttimestamp: string,\n): CustomMessage {\n\treturn {\n\t\trole: \"custom\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function convertToLlm(messages: AgentMessage[]): Message[] {\n\treturn messages\n\t\t.map((m): Message | undefined => {\n\t\t\tswitch (m.role) {\n\t\t\t\tcase \"bashExecution\":\n\t\t\t\t\tif (m.excludeFromContext) {\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: bashExecutionToText(m) }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\tcase \"custom\": {\n\t\t\t\t\tconst content = typeof m.content === \"string\" ? [{ type: \"text\" as const, text: m.content }] : m.content;\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tcase \"branchSummary\":\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\tcase \"compactionSummary\":\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{ type: \"text\" as const, text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },\n\t\t\t\t\t\t],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\tcase \"user\":\n\t\t\t\tcase \"assistant\":\n\t\t\t\tcase \"toolResult\":\n\t\t\t\t\treturn m;\n\t\t\t\tdefault:\n\t\t\t\t\treturn undefined;\n\t\t\t}\n\t\t})\n\t\t.filter((m): m is Message => m !== undefined);\n}\n"]}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
export const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:
|
|
2
|
+
|
|
3
|
+
<summary>
|
|
4
|
+
`;
|
|
5
|
+
export const COMPACTION_SUMMARY_SUFFIX = `
|
|
6
|
+
</summary>`;
|
|
7
|
+
export const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:
|
|
8
|
+
|
|
9
|
+
<summary>
|
|
10
|
+
`;
|
|
11
|
+
export const BRANCH_SUMMARY_SUFFIX = `</summary>`;
|
|
12
|
+
export function bashExecutionToText(msg) {
|
|
13
|
+
let text = `Ran \`${msg.command}\`\n`;
|
|
14
|
+
if (msg.output) {
|
|
15
|
+
text += `\`\`\`\n${msg.output}\n\`\`\``;
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
text += "(no output)";
|
|
19
|
+
}
|
|
20
|
+
if (msg.cancelled) {
|
|
21
|
+
text += "\n\n(command cancelled)";
|
|
22
|
+
}
|
|
23
|
+
else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {
|
|
24
|
+
text += `\n\nCommand exited with code ${msg.exitCode}`;
|
|
25
|
+
}
|
|
26
|
+
if (msg.truncated && msg.fullOutputPath) {
|
|
27
|
+
text += `\n\n[Output truncated. Full output: ${msg.fullOutputPath}]`;
|
|
28
|
+
}
|
|
29
|
+
return text;
|
|
30
|
+
}
|
|
31
|
+
export function createBranchSummaryMessage(summary, fromId, timestamp) {
|
|
32
|
+
return {
|
|
33
|
+
role: "branchSummary",
|
|
34
|
+
summary,
|
|
35
|
+
fromId,
|
|
36
|
+
timestamp: new Date(timestamp).getTime(),
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
export function createCompactionSummaryMessage(summary, tokensBefore, timestamp) {
|
|
40
|
+
return {
|
|
41
|
+
role: "compactionSummary",
|
|
42
|
+
summary,
|
|
43
|
+
tokensBefore,
|
|
44
|
+
timestamp: new Date(timestamp).getTime(),
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
export function createCustomMessage(customType, content, display, details, timestamp) {
|
|
48
|
+
return {
|
|
49
|
+
role: "custom",
|
|
50
|
+
customType,
|
|
51
|
+
content,
|
|
52
|
+
display,
|
|
53
|
+
details,
|
|
54
|
+
timestamp: new Date(timestamp).getTime(),
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
export function convertToLlm(messages) {
|
|
58
|
+
return messages
|
|
59
|
+
.map((m) => {
|
|
60
|
+
switch (m.role) {
|
|
61
|
+
case "bashExecution":
|
|
62
|
+
if (m.excludeFromContext) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
role: "user",
|
|
67
|
+
content: [{ type: "text", text: bashExecutionToText(m) }],
|
|
68
|
+
timestamp: m.timestamp,
|
|
69
|
+
};
|
|
70
|
+
case "custom": {
|
|
71
|
+
const content = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content;
|
|
72
|
+
return {
|
|
73
|
+
role: "user",
|
|
74
|
+
content,
|
|
75
|
+
timestamp: m.timestamp,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
case "branchSummary":
|
|
79
|
+
return {
|
|
80
|
+
role: "user",
|
|
81
|
+
content: [{ type: "text", text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],
|
|
82
|
+
timestamp: m.timestamp,
|
|
83
|
+
};
|
|
84
|
+
case "compactionSummary":
|
|
85
|
+
return {
|
|
86
|
+
role: "user",
|
|
87
|
+
content: [
|
|
88
|
+
{ type: "text", text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },
|
|
89
|
+
],
|
|
90
|
+
timestamp: m.timestamp,
|
|
91
|
+
};
|
|
92
|
+
case "user":
|
|
93
|
+
case "assistant":
|
|
94
|
+
case "toolResult":
|
|
95
|
+
return m;
|
|
96
|
+
default:
|
|
97
|
+
return undefined;
|
|
98
|
+
}
|
|
99
|
+
})
|
|
100
|
+
.filter((m) => m !== undefined);
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=messages.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"messages.js","sourceRoot":"","sources":["../../src/harness/messages.ts"],"names":[],"mappings":"AAGA,MAAM,CAAC,MAAM,yBAAyB,GAAG;;;CAGxC,CAAC;AAEF,MAAM,CAAC,MAAM,yBAAyB,GAAG;WAC9B,CAAC;AAEZ,MAAM,CAAC,MAAM,qBAAqB,GAAG;;;CAGpC,CAAC;AAEF,MAAM,CAAC,MAAM,qBAAqB,GAAG,YAAY,CAAC;AA8ClD,MAAM,UAAU,mBAAmB,CAAC,GAAyB,EAAU;IACtE,IAAI,IAAI,GAAG,SAAS,GAAG,CAAC,OAAO,MAAM,CAAC;IACtC,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;QAChB,IAAI,IAAI,WAAW,GAAG,CAAC,MAAM,UAAU,CAAC;IACzC,CAAC;SAAM,CAAC;QACP,IAAI,IAAI,aAAa,CAAC;IACvB,CAAC;IACD,IAAI,GAAG,CAAC,SAAS,EAAE,CAAC;QACnB,IAAI,IAAI,yBAAyB,CAAC;IACnC,CAAC;SAAM,IAAI,GAAG,CAAC,QAAQ,KAAK,IAAI,IAAI,GAAG,CAAC,QA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,SAAS,CAAC,CAAC;AAAA,CAC/C","sourcesContent":["import type { ImageContent, Message, TextContent } from \"@eminent337/aery-ai\";\nimport type { AgentMessage } from \"../types.js\";\n\nexport const COMPACTION_SUMMARY_PREFIX = `The conversation history before this point was compacted into the following summary:\n\n<summary>\n`;\n\nexport const COMPACTION_SUMMARY_SUFFIX = `\n</summary>`;\n\nexport const BRANCH_SUMMARY_PREFIX = `The following is a summary of a branch that this conversation came back from:\n\n<summary>\n`;\n\nexport const BRANCH_SUMMARY_SUFFIX = `</summary>`;\n\nexport interface BashExecutionMessage {\n\trole: \"bashExecution\";\n\tcommand: string;\n\toutput: string;\n\texitCode: number | undefined;\n\tcancelled: boolean;\n\ttruncated: boolean;\n\tfullOutputPath?: string;\n\ttimestamp: number;\n\texcludeFromContext?: boolean;\n}\n\nexport interface CustomMessage<T = unknown> {\n\trole: \"custom\";\n\tcustomType: string;\n\tcontent: string | (TextContent | ImageContent)[];\n\tdisplay: boolean;\n\tdetails?: T;\n\ttimestamp: number;\n}\n\nexport interface BranchSummaryMessage {\n\trole: \"branchSummary\";\n\tsummary: string;\n\tfromId: string;\n\ttimestamp: number;\n}\n\nexport interface CompactionSummaryMessage {\n\trole: \"compactionSummary\";\n\tsummary: string;\n\ttokensBefore: number;\n\ttimestamp: number;\n}\n\ndeclare module \"../types.js\" {\n\tinterface CustomAgentMessages {\n\t\tbashExecution: BashExecutionMessage;\n\t\tcustom: CustomMessage;\n\t\tbranchSummary: BranchSummaryMessage;\n\t\tcompactionSummary: CompactionSummaryMessage;\n\t}\n}\n\nexport function bashExecutionToText(msg: BashExecutionMessage): string {\n\tlet text = `Ran \\`${msg.command}\\`\\n`;\n\tif (msg.output) {\n\t\ttext += `\\`\\`\\`\\n${msg.output}\\n\\`\\`\\``;\n\t} else {\n\t\ttext += \"(no output)\";\n\t}\n\tif (msg.cancelled) {\n\t\ttext += \"\\n\\n(command cancelled)\";\n\t} else if (msg.exitCode !== null && msg.exitCode !== undefined && msg.exitCode !== 0) {\n\t\ttext += `\\n\\nCommand exited with code ${msg.exitCode}`;\n\t}\n\tif (msg.truncated && msg.fullOutputPath) {\n\t\ttext += `\\n\\n[Output truncated. Full output: ${msg.fullOutputPath}]`;\n\t}\n\treturn text;\n}\n\nexport function createBranchSummaryMessage(summary: string, fromId: string, timestamp: string): BranchSummaryMessage {\n\treturn {\n\t\trole: \"branchSummary\",\n\t\tsummary,\n\t\tfromId,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function createCompactionSummaryMessage(\n\tsummary: string,\n\ttokensBefore: number,\n\ttimestamp: string,\n): CompactionSummaryMessage {\n\treturn {\n\t\trole: \"compactionSummary\",\n\t\tsummary,\n\t\ttokensBefore,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function createCustomMessage(\n\tcustomType: string,\n\tcontent: string | (TextContent | ImageContent)[],\n\tdisplay: boolean,\n\tdetails: unknown | undefined,\n\ttimestamp: string,\n): CustomMessage {\n\treturn {\n\t\trole: \"custom\",\n\t\tcustomType,\n\t\tcontent,\n\t\tdisplay,\n\t\tdetails,\n\t\ttimestamp: new Date(timestamp).getTime(),\n\t};\n}\n\nexport function convertToLlm(messages: AgentMessage[]): Message[] {\n\treturn messages\n\t\t.map((m): Message | undefined => {\n\t\t\tswitch (m.role) {\n\t\t\t\tcase \"bashExecution\":\n\t\t\t\t\tif (m.excludeFromContext) {\n\t\t\t\t\t\treturn undefined;\n\t\t\t\t\t}\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\", text: bashExecutionToText(m) }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\tcase \"custom\": {\n\t\t\t\t\tconst content = typeof m.content === \"string\" ? [{ type: \"text\" as const, text: m.content }] : m.content;\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent,\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tcase \"branchSummary\":\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [{ type: \"text\" as const, text: BRANCH_SUMMARY_PREFIX + m.summary + BRANCH_SUMMARY_SUFFIX }],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\tcase \"compactionSummary\":\n\t\t\t\t\treturn {\n\t\t\t\t\t\trole: \"user\",\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{ type: \"text\" as const, text: COMPACTION_SUMMARY_PREFIX + m.summary + COMPACTION_SUMMARY_SUFFIX },\n\t\t\t\t\t\t],\n\t\t\t\t\t\ttimestamp: m.timestamp,\n\t\t\t\t\t};\n\t\t\t\tcase \"user\":\n\t\t\t\tcase \"assistant\":\n\t\t\t\tcase \"toolResult\":\n\t\t\t\t\treturn m;\n\t\t\t\tdefault:\n\t\t\t\t\treturn undefined;\n\t\t\t}\n\t\t})\n\t\t.filter((m): m is Message => m !== undefined);\n}\n"]}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { type ExecutionEnv, type PromptTemplate } from "./types.js";
|
|
2
|
+
/** Warning produced while loading prompt templates. */
|
|
3
|
+
export interface PromptTemplateDiagnostic {
|
|
4
|
+
/** Diagnostic severity. Currently only warnings are emitted. */
|
|
5
|
+
type: "warning";
|
|
6
|
+
/** Human-readable diagnostic message. */
|
|
7
|
+
message: string;
|
|
8
|
+
/** Path associated with the diagnostic. */
|
|
9
|
+
path: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Load prompt templates from one or more paths.
|
|
13
|
+
*
|
|
14
|
+
* Directory inputs load direct `.md` children non-recursively. File inputs load explicit `.md` files. Missing paths and
|
|
15
|
+
* non-markdown files are skipped. Read and parse failures are returned as diagnostics.
|
|
16
|
+
*/
|
|
17
|
+
export declare function loadPromptTemplates(env: ExecutionEnv, paths: string | string[]): Promise<{
|
|
18
|
+
promptTemplates: PromptTemplate[];
|
|
19
|
+
diagnostics: PromptTemplateDiagnostic[];
|
|
20
|
+
}>;
|
|
21
|
+
/**
|
|
22
|
+
* Load prompt templates from source-tagged paths.
|
|
23
|
+
*
|
|
24
|
+
* Source values are preserved exactly and attached to every loaded prompt template and diagnostic. The agent package does
|
|
25
|
+
* not interpret source values; applications define their own provenance shape.
|
|
26
|
+
*/
|
|
27
|
+
export declare function loadSourcedPromptTemplates<TSource, TPromptTemplate extends PromptTemplate = PromptTemplate>(env: ExecutionEnv, inputs: Array<{
|
|
28
|
+
path: string;
|
|
29
|
+
source: TSource;
|
|
30
|
+
}>, mapPromptTemplate?: (promptTemplate: PromptTemplate, source: TSource) => TPromptTemplate): Promise<{
|
|
31
|
+
promptTemplates: Array<{
|
|
32
|
+
promptTemplate: TPromptTemplate;
|
|
33
|
+
source: TSource;
|
|
34
|
+
}>;
|
|
35
|
+
diagnostics: Array<PromptTemplateDiagnostic & {
|
|
36
|
+
source: TSource;
|
|
37
|
+
}>;
|
|
38
|
+
}>;
|
|
39
|
+
/** Parse an argument string using simple shell-style single and double quotes. */
|
|
40
|
+
export declare function parseCommandArgs(argsString: string): string[];
|
|
41
|
+
/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */
|
|
42
|
+
export declare function substituteArgs(content: string, args: string[]): string;
|
|
43
|
+
/** Format a prompt template invocation with positional arguments. */
|
|
44
|
+
export declare function formatPromptTemplateInvocation(template: PromptTemplate, args?: string[]): string;
|
|
45
|
+
/** Compatibility alias for older harness consumers. */
|
|
46
|
+
export declare const expandPromptTemplate: typeof formatPromptTemplateInvocation;
|
|
47
|
+
//# sourceMappingURL=prompt-templates.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-templates.d.ts","sourceRoot":"","sources":["../../src/harness/prompt-templates.ts"],"names":[],"mappings":"AACA,OAAO,E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,QAAQ,EAAE,cAAc,EAAE,IAAI,GAAE,MAAM,EAAO,GAAG,MAAM,CAEpG;AAED,uDAAuD;AACvD,eAAO,MAAM,oBAAoB,uCAAiC,CAAC","sourcesContent":["import { parse } from \"yaml\";\nimport { type ExecutionEnv, type FileInfo, getOrUndefined, type PromptTemplate, type Result } from \"./types.js\";\n\n/** Warning produced while loading prompt templates. */\nexport interface PromptTemplateDiagnostic {\n\t/** Diagnostic severity. Currently only warnings are emitted. */\n\ttype: \"warning\";\n\t/** Human-readable diagnostic message. */\n\tmessage: string;\n\t/** Path associated with the diagnostic. */\n\tpath: string;\n}\n\ninterface PromptTemplateFrontmatter {\n\tdescription?: string;\n\t\"argument-hint\"?: string;\n\t[key: string]: unknown;\n}\n\n/**\n * Load prompt templates from one or more paths.\n *\n * Directory inputs load direct `.md` children non-recursively. File inputs load explicit `.md` files. Missing paths and\n * non-markdown files are skipped. Read and parse failures are returned as diagnostics.\n */\nexport async function loadPromptTemplates(\n\tenv: ExecutionEnv,\n\tpaths: string | string[],\n): Promise<{ promptTemplates: PromptTemplate[]; diagnostics: PromptTemplateDiagnostic[] }> {\n\tconst promptTemplates: PromptTemplate[] = [];\n\tconst diagnostics: PromptTemplateDiagnostic[] = [];\n\tfor (const path of Array.isArray(paths) ? paths : [paths]) {\n\t\tconst info = getOrUndefined(await env.fileInfo(path));\n\t\tif (!info) continue;\n\t\tconst kind = await resolveKind(env, info);\n\t\tif (kind === \"directory\") {\n\t\t\tconst result = await loadTemplatesFromDir(env, info.path);\n\t\t\tpromptTemplates.push(...result.promptTemplates);\n\t\t\tdiagnostics.push(...result.diagnostics);\n\t\t} else if (kind === \"file\" && info.name.endsWith(\".md\")) {\n\t\t\tconst result = await loadTemplateFromFile(env, info.path);\n\t\t\tif (result.promptTemplate) promptTemplates.push(result.promptTemplate);\n\t\t\tdiagnostics.push(...result.diagnostics);\n\t\t}\n\t}\n\treturn { promptTemplates, diagnostics };\n}\n\n/**\n * Load prompt templates from source-tagged paths.\n *\n * Source values are preserved exactly and attached to every loaded prompt template and diagnostic. The agent package does\n * not interpret source values; applications define their own provenance shape.\n */\nexport async function loadSourcedPromptTemplates<TSource, TPromptTemplate extends PromptTemplate = PromptTemplate>(\n\tenv: ExecutionEnv,\n\tinputs: Array<{ path: string; source: TSource }>,\n\tmapPromptTemplate?: (promptTemplate: PromptTemplate, source: TSource) => TPromptTemplate,\n): Promise<{\n\tpromptTemplates: Array<{ promptTemplate: TPromptTemplate; source: TSource }>;\n\tdiagnostics: Array<PromptTemplateDiagnostic & { source: TSource }>;\n}> {\n\tconst promptTemplates: Array<{ promptTemplate: TPromptTemplate; source: TSource }> = [];\n\tconst diagnostics: Array<PromptTemplateDiagnostic & { source: TSource }> = [];\n\tfor (const input of inputs) {\n\t\tconst result = await loadPromptTemplates(env, input.path);\n\t\tfor (const promptTemplate of result.promptTemplates) {\n\t\t\tpromptTemplates.push({\n\t\t\t\tpromptTemplate: mapPromptTemplate\n\t\t\t\t\t? mapPromptTemplate(promptTemplate, input.source)\n\t\t\t\t\t: (promptTemplate as TPromptTemplate),\n\t\t\t\tsource: input.source,\n\t\t\t});\n\t\t}\n\t\tfor (const diagnostic of result.diagnostics) diagnostics.push({ ...diagnostic, source: input.source });\n\t}\n\treturn { promptTemplates, diagnostics };\n}\n\nasync function loadTemplatesFromDir(\n\tenv: ExecutionEnv,\n\tdir: string,\n): Promise<{ promptTemplates: PromptTemplate[]; diagnostics: PromptTemplateDiagnostic[] }> {\n\tconst promptTemplates: PromptTemplate[] = [];\n\tconst diagnostics: PromptTemplateDiagnostic[] = [];\n\tconst entriesResult = await env.listDir(dir);\n\tif (!entriesResult.ok) {\n\t\tdiagnostics.push({\n\t\t\ttype: \"warning\",\n\t\t\tmessage: entriesResult.error.message,\n\t\t\tpath: dir,\n\t\t});\n\t\treturn { promptTemplates, diagnostics };\n\t}\n\tconst entries = entriesResult.value;\n\n\tfor (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {\n\t\tconst kind = await resolveKind(env, entry);\n\t\tif (kind !== \"file\" || !entry.name.endsWith(\".md\")) continue;\n\t\tconst result = await loadTemplateFromFile(env, entry.path);\n\t\tif (result.promptTemplate) promptTemplates.push(result.promptTemplate);\n\t\tdiagnostics.push(...result.diagnostics);\n\t}\n\treturn { promptTemplates, diagnostics };\n}\n\nasync function loadTemplateFromFile(\n\tenv: ExecutionEnv,\n\tfilePath: string,\n): Promise<{ promptTemplate: PromptTemplate | null; diagnostics: PromptTemplateDiagnostic[] }> {\n\tconst diagnostics: PromptTemplateDiagnostic[] = [];\n\tconst rawContent = await env.readTextFile(filePath);\n\tif (!rawContent.ok) {\n\t\tdiagnostics.push({\n\t\t\ttype: \"warning\",\n\t\t\tmessage: rawContent.error.message,\n\t\t\tpath: filePath,\n\t\t});\n\t\treturn { promptTemplate: null, diagnostics };\n\t}\n\n\tconst parsed = parseFrontmatter<PromptTemplateFrontmatter>(rawContent.value);\n\tif (!parsed.ok) {\n\t\tdiagnostics.push({\n\t\t\ttype: \"warning\",\n\t\t\tmessage: parsed.error.message,\n\t\t\tpath: filePath,\n\t\t});\n\t\treturn { promptTemplate: null, diagnostics };\n\t}\n\n\tconst { frontmatter, body } = parsed.value;\n\tconst firstLine = body.split(\"\\n\").find((line) => line.trim());\n\tlet description = typeof frontmatter.description === \"string\" ? frontmatter.description : \"\";\n\tif (!description && firstLine) {\n\t\tdescription = firstLine.slice(0, 60);\n\t\tif (firstLine.length > 60) description += \"...\";\n\t}\n\treturn {\n\t\tpromptTemplate: {\n\t\t\tname: basenameEnvPath(filePath).replace(/\\.md$/i, \"\"),\n\t\t\tdescription,\n\t\t\tcontent: body,\n\t\t},\n\t\tdiagnostics,\n\t};\n}\n\nasync function resolveKind(env: ExecutionEnv, info: FileInfo): Promise<\"file\" | \"directory\" | undefined> {\n\tif (info.kind === \"file\" || info.kind === \"directory\") return info.kind;\n\tconst canonicalPath = await env.canonicalPath(info.path);\n\tif (!canonicalPath.ok) return undefined;\n\tconst target = getOrUndefined(await env.fileInfo(canonicalPath.value));\n\tif (!target) return undefined;\n\treturn target.kind === \"file\" || target.kind === \"directory\" ? target.kind : undefined;\n}\n\nfunction parseFrontmatter<T extends Record<string, unknown>>(\n\tcontent: string,\n): Result<{ frontmatter: T; body: string }, Error> {\n\ttry {\n\t\tconst normalized = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\");\n\t\tif (!normalized.startsWith(\"---\")) return { ok: true, value: { frontmatter: {} as T, body: normalized } };\n\t\tconst endIndex = normalized.indexOf(\"\\n---\", 3);\n\t\tif (endIndex === -1) return { ok: true, value: { frontmatter: {} as T, body: normalized } };\n\t\tconst yamlString = normalized.slice(4, endIndex);\n\t\tconst body = normalized.slice(endIndex + 4).trim();\n\t\treturn { ok: true, value: { frontmatter: (parse(yamlString) ?? {}) as T, body } };\n\t} catch (error) {\n\t\treturn { ok: false, error: error instanceof Error ? error : new Error(String(error)) };\n\t}\n}\n\nfunction basenameEnvPath(path: string): string {\n\tconst normalized = path.replace(/\\/+$/, \"\");\n\tconst slashIndex = normalized.lastIndexOf(\"/\");\n\treturn slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);\n}\n\n/** Parse an argument string using simple shell-style single and double quotes. */\nexport function parseCommandArgs(argsString: string): string[] {\n\tconst args: string[] = [];\n\tlet current = \"\";\n\tlet inQuote: string | null = null;\n\n\tfor (let i = 0; i < argsString.length; i++) {\n\t\tconst char = argsString[i]!;\n\t\tif (inQuote) {\n\t\t\tif (char === inQuote) inQuote = null;\n\t\t\telse current += char;\n\t\t} else if (char === '\"' || char === \"'\") {\n\t\t\tinQuote = char;\n\t\t} else if (char === \" \" || char === \"\\t\") {\n\t\t\tif (current) {\n\t\t\t\targs.push(current);\n\t\t\t\tcurrent = \"\";\n\t\t\t}\n\t\t} else {\n\t\t\tcurrent += char;\n\t\t}\n\t}\n\tif (current) args.push(current);\n\treturn args;\n}\n\n/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */\nexport function substituteArgs(content: string, args: string[]): string {\n\tlet result = content;\n\tresult = result.replace(/\\$(\\d+)/g, (_, num: string) => args[parseInt(num, 10) - 1] ?? \"\");\n\tresult = result.replace(/\\$\\{@:(\\d+)(?::(\\d+))?\\}/g, (_, startStr: string, lengthStr?: string) => {\n\t\tlet start = parseInt(startStr, 10) - 1;\n\t\tif (start < 0) start = 0;\n\t\tif (lengthStr) return args.slice(start, start + parseInt(lengthStr, 10)).join(\" \");\n\t\treturn args.slice(start).join(\" \");\n\t});\n\tconst allArgs = args.join(\" \");\n\tresult = result.replace(/\\$ARGUMENTS/g, allArgs);\n\tresult = result.replace(/\\$@/g, allArgs);\n\treturn result;\n}\n\n/** Format a prompt template invocation with positional arguments. */\nexport function formatPromptTemplateInvocation(template: PromptTemplate, args: string[] = []): string {\n\treturn substituteArgs(template.content, args);\n}\n\n/** Compatibility alias for older harness consumers. */\nexport const expandPromptTemplate = formatPromptTemplateInvocation;\n"]}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { parse } from "yaml";
|
|
2
|
+
import { getOrUndefined } from "./types.js";
|
|
3
|
+
/**
|
|
4
|
+
* Load prompt templates from one or more paths.
|
|
5
|
+
*
|
|
6
|
+
* Directory inputs load direct `.md` children non-recursively. File inputs load explicit `.md` files. Missing paths and
|
|
7
|
+
* non-markdown files are skipped. Read and parse failures are returned as diagnostics.
|
|
8
|
+
*/
|
|
9
|
+
export async function loadPromptTemplates(env, paths) {
|
|
10
|
+
const promptTemplates = [];
|
|
11
|
+
const diagnostics = [];
|
|
12
|
+
for (const path of Array.isArray(paths) ? paths : [paths]) {
|
|
13
|
+
const info = getOrUndefined(await env.fileInfo(path));
|
|
14
|
+
if (!info)
|
|
15
|
+
continue;
|
|
16
|
+
const kind = await resolveKind(env, info);
|
|
17
|
+
if (kind === "directory") {
|
|
18
|
+
const result = await loadTemplatesFromDir(env, info.path);
|
|
19
|
+
promptTemplates.push(...result.promptTemplates);
|
|
20
|
+
diagnostics.push(...result.diagnostics);
|
|
21
|
+
}
|
|
22
|
+
else if (kind === "file" && info.name.endsWith(".md")) {
|
|
23
|
+
const result = await loadTemplateFromFile(env, info.path);
|
|
24
|
+
if (result.promptTemplate)
|
|
25
|
+
promptTemplates.push(result.promptTemplate);
|
|
26
|
+
diagnostics.push(...result.diagnostics);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return { promptTemplates, diagnostics };
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Load prompt templates from source-tagged paths.
|
|
33
|
+
*
|
|
34
|
+
* Source values are preserved exactly and attached to every loaded prompt template and diagnostic. The agent package does
|
|
35
|
+
* not interpret source values; applications define their own provenance shape.
|
|
36
|
+
*/
|
|
37
|
+
export async function loadSourcedPromptTemplates(env, inputs, mapPromptTemplate) {
|
|
38
|
+
const promptTemplates = [];
|
|
39
|
+
const diagnostics = [];
|
|
40
|
+
for (const input of inputs) {
|
|
41
|
+
const result = await loadPromptTemplates(env, input.path);
|
|
42
|
+
for (const promptTemplate of result.promptTemplates) {
|
|
43
|
+
promptTemplates.push({
|
|
44
|
+
promptTemplate: mapPromptTemplate
|
|
45
|
+
? mapPromptTemplate(promptTemplate, input.source)
|
|
46
|
+
: promptTemplate,
|
|
47
|
+
source: input.source,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
for (const diagnostic of result.diagnostics)
|
|
51
|
+
diagnostics.push({ ...diagnostic, source: input.source });
|
|
52
|
+
}
|
|
53
|
+
return { promptTemplates, diagnostics };
|
|
54
|
+
}
|
|
55
|
+
async function loadTemplatesFromDir(env, dir) {
|
|
56
|
+
const promptTemplates = [];
|
|
57
|
+
const diagnostics = [];
|
|
58
|
+
const entriesResult = await env.listDir(dir);
|
|
59
|
+
if (!entriesResult.ok) {
|
|
60
|
+
diagnostics.push({
|
|
61
|
+
type: "warning",
|
|
62
|
+
message: entriesResult.error.message,
|
|
63
|
+
path: dir,
|
|
64
|
+
});
|
|
65
|
+
return { promptTemplates, diagnostics };
|
|
66
|
+
}
|
|
67
|
+
const entries = entriesResult.value;
|
|
68
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
69
|
+
const kind = await resolveKind(env, entry);
|
|
70
|
+
if (kind !== "file" || !entry.name.endsWith(".md"))
|
|
71
|
+
continue;
|
|
72
|
+
const result = await loadTemplateFromFile(env, entry.path);
|
|
73
|
+
if (result.promptTemplate)
|
|
74
|
+
promptTemplates.push(result.promptTemplate);
|
|
75
|
+
diagnostics.push(...result.diagnostics);
|
|
76
|
+
}
|
|
77
|
+
return { promptTemplates, diagnostics };
|
|
78
|
+
}
|
|
79
|
+
async function loadTemplateFromFile(env, filePath) {
|
|
80
|
+
const diagnostics = [];
|
|
81
|
+
const rawContent = await env.readTextFile(filePath);
|
|
82
|
+
if (!rawContent.ok) {
|
|
83
|
+
diagnostics.push({
|
|
84
|
+
type: "warning",
|
|
85
|
+
message: rawContent.error.message,
|
|
86
|
+
path: filePath,
|
|
87
|
+
});
|
|
88
|
+
return { promptTemplate: null, diagnostics };
|
|
89
|
+
}
|
|
90
|
+
const parsed = parseFrontmatter(rawContent.value);
|
|
91
|
+
if (!parsed.ok) {
|
|
92
|
+
diagnostics.push({
|
|
93
|
+
type: "warning",
|
|
94
|
+
message: parsed.error.message,
|
|
95
|
+
path: filePath,
|
|
96
|
+
});
|
|
97
|
+
return { promptTemplate: null, diagnostics };
|
|
98
|
+
}
|
|
99
|
+
const { frontmatter, body } = parsed.value;
|
|
100
|
+
const firstLine = body.split("\n").find((line) => line.trim());
|
|
101
|
+
let description = typeof frontmatter.description === "string" ? frontmatter.description : "";
|
|
102
|
+
if (!description && firstLine) {
|
|
103
|
+
description = firstLine.slice(0, 60);
|
|
104
|
+
if (firstLine.length > 60)
|
|
105
|
+
description += "...";
|
|
106
|
+
}
|
|
107
|
+
return {
|
|
108
|
+
promptTemplate: {
|
|
109
|
+
name: basenameEnvPath(filePath).replace(/\.md$/i, ""),
|
|
110
|
+
description,
|
|
111
|
+
content: body,
|
|
112
|
+
},
|
|
113
|
+
diagnostics,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
async function resolveKind(env, info) {
|
|
117
|
+
if (info.kind === "file" || info.kind === "directory")
|
|
118
|
+
return info.kind;
|
|
119
|
+
const canonicalPath = await env.canonicalPath(info.path);
|
|
120
|
+
if (!canonicalPath.ok)
|
|
121
|
+
return undefined;
|
|
122
|
+
const target = getOrUndefined(await env.fileInfo(canonicalPath.value));
|
|
123
|
+
if (!target)
|
|
124
|
+
return undefined;
|
|
125
|
+
return target.kind === "file" || target.kind === "directory" ? target.kind : undefined;
|
|
126
|
+
}
|
|
127
|
+
function parseFrontmatter(content) {
|
|
128
|
+
try {
|
|
129
|
+
const normalized = content.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
130
|
+
if (!normalized.startsWith("---"))
|
|
131
|
+
return { ok: true, value: { frontmatter: {}, body: normalized } };
|
|
132
|
+
const endIndex = normalized.indexOf("\n---", 3);
|
|
133
|
+
if (endIndex === -1)
|
|
134
|
+
return { ok: true, value: { frontmatter: {}, body: normalized } };
|
|
135
|
+
const yamlString = normalized.slice(4, endIndex);
|
|
136
|
+
const body = normalized.slice(endIndex + 4).trim();
|
|
137
|
+
return { ok: true, value: { frontmatter: (parse(yamlString) ?? {}), body } };
|
|
138
|
+
}
|
|
139
|
+
catch (error) {
|
|
140
|
+
return { ok: false, error: error instanceof Error ? error : new Error(String(error)) };
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function basenameEnvPath(path) {
|
|
144
|
+
const normalized = path.replace(/\/+$/, "");
|
|
145
|
+
const slashIndex = normalized.lastIndexOf("/");
|
|
146
|
+
return slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);
|
|
147
|
+
}
|
|
148
|
+
/** Parse an argument string using simple shell-style single and double quotes. */
|
|
149
|
+
export function parseCommandArgs(argsString) {
|
|
150
|
+
const args = [];
|
|
151
|
+
let current = "";
|
|
152
|
+
let inQuote = null;
|
|
153
|
+
for (let i = 0; i < argsString.length; i++) {
|
|
154
|
+
const char = argsString[i];
|
|
155
|
+
if (inQuote) {
|
|
156
|
+
if (char === inQuote)
|
|
157
|
+
inQuote = null;
|
|
158
|
+
else
|
|
159
|
+
current += char;
|
|
160
|
+
}
|
|
161
|
+
else if (char === '"' || char === "'") {
|
|
162
|
+
inQuote = char;
|
|
163
|
+
}
|
|
164
|
+
else if (char === " " || char === "\t") {
|
|
165
|
+
if (current) {
|
|
166
|
+
args.push(current);
|
|
167
|
+
current = "";
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
current += char;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (current)
|
|
175
|
+
args.push(current);
|
|
176
|
+
return args;
|
|
177
|
+
}
|
|
178
|
+
/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */
|
|
179
|
+
export function substituteArgs(content, args) {
|
|
180
|
+
let result = content;
|
|
181
|
+
result = result.replace(/\$(\d+)/g, (_, num) => args[parseInt(num, 10) - 1] ?? "");
|
|
182
|
+
result = result.replace(/\$\{@:(\d+)(?::(\d+))?\}/g, (_, startStr, lengthStr) => {
|
|
183
|
+
let start = parseInt(startStr, 10) - 1;
|
|
184
|
+
if (start < 0)
|
|
185
|
+
start = 0;
|
|
186
|
+
if (lengthStr)
|
|
187
|
+
return args.slice(start, start + parseInt(lengthStr, 10)).join(" ");
|
|
188
|
+
return args.slice(start).join(" ");
|
|
189
|
+
});
|
|
190
|
+
const allArgs = args.join(" ");
|
|
191
|
+
result = result.replace(/\$ARGUMENTS/g, allArgs);
|
|
192
|
+
result = result.replace(/\$@/g, allArgs);
|
|
193
|
+
return result;
|
|
194
|
+
}
|
|
195
|
+
/** Format a prompt template invocation with positional arguments. */
|
|
196
|
+
export function formatPromptTemplateInvocation(template, args = []) {
|
|
197
|
+
return substituteArgs(template.content, args);
|
|
198
|
+
}
|
|
199
|
+
/** Compatibility alias for older harness consumers. */
|
|
200
|
+
export const expandPromptTemplate = formatPromptTemplateInvocation;
|
|
201
|
+
//# sourceMappingURL=prompt-templates.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prompt-templates.js","sourceRoot":"","sources":["../../src/harness/prompt-templates.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7B,OAAO,EAAoC,cAAc,EAAoC,MAAM,YAAY,CAAC;AAkBhH;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,mBAAmB,CACxC,GAAiB,EACjB,KAAwB,EACkE;IAC1F,MAAM,eAAe,GAAqB,EAAE,CAAC;IAC7C,MAAM,WAAW,GAA+B,EAAE,CAAC;IACnD,KAAK,MAAM,IAAI,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,EAAE,CAAC;QAC3D,MAAM,IAAI,GAAG,cAAc,CAAC,MAAM,GAAG,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC;QACtD,IAAI,CAAC,IAAI;YAAE,SAAS;QACpB,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;QAC1C,IAAI,IAAI,KAAK,WAAW,EAAE,CAAC;YAC1B,MAAM,MAAM,GAAG,MAAM,oBAAoB,CAAC,GAAG,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;YAC1D,eAAe,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,eAAe,CAAC,CAAC;YAChD,WAAW,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC;QACzC,CAAC;aAAM,IAAI,IAAI,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;YACzD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G,CAAC,CAAC;QACzB,IAAI,SAAS;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,QAAQ,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnF,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAAA,CACnC,CAAC,CAAC;IACH,MAAM,OAAO,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;IAC/B,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE,OAAO,CAAC,CAAC;IACjD,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IACzC,OAAO,MAAM,CAAC;AAAA,CACd;AAED,qEAAqE;AACrE,MAAM,UAAU,8BAA8B,CAAC,QAAwB,EAAE,IAAI,GAAa,EAAE,EAAU;IACrG,OAAO,cAAc,CAAC,QAAQ,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;AAAA,CAC9C;AAED,uDAAuD;AACvD,MAAM,CAAC,MAAM,oBAAoB,GAAG,8BAA8B,CAAC","sourcesContent":["import { parse } from \"yaml\";\nimport { type ExecutionEnv, type FileInfo, getOrUndefined, type PromptTemplate, type Result } from \"./types.js\";\n\n/** Warning produced while loading prompt templates. */\nexport interface PromptTemplateDiagnostic {\n\t/** Diagnostic severity. Currently only warnings are emitted. */\n\ttype: \"warning\";\n\t/** Human-readable diagnostic message. */\n\tmessage: string;\n\t/** Path associated with the diagnostic. */\n\tpath: string;\n}\n\ninterface PromptTemplateFrontmatter {\n\tdescription?: string;\n\t\"argument-hint\"?: string;\n\t[key: string]: unknown;\n}\n\n/**\n * Load prompt templates from one or more paths.\n *\n * Directory inputs load direct `.md` children non-recursively. File inputs load explicit `.md` files. Missing paths and\n * non-markdown files are skipped. Read and parse failures are returned as diagnostics.\n */\nexport async function loadPromptTemplates(\n\tenv: ExecutionEnv,\n\tpaths: string | string[],\n): Promise<{ promptTemplates: PromptTemplate[]; diagnostics: PromptTemplateDiagnostic[] }> {\n\tconst promptTemplates: PromptTemplate[] = [];\n\tconst diagnostics: PromptTemplateDiagnostic[] = [];\n\tfor (const path of Array.isArray(paths) ? paths : [paths]) {\n\t\tconst info = getOrUndefined(await env.fileInfo(path));\n\t\tif (!info) continue;\n\t\tconst kind = await resolveKind(env, info);\n\t\tif (kind === \"directory\") {\n\t\t\tconst result = await loadTemplatesFromDir(env, info.path);\n\t\t\tpromptTemplates.push(...result.promptTemplates);\n\t\t\tdiagnostics.push(...result.diagnostics);\n\t\t} else if (kind === \"file\" && info.name.endsWith(\".md\")) {\n\t\t\tconst result = await loadTemplateFromFile(env, info.path);\n\t\t\tif (result.promptTemplate) promptTemplates.push(result.promptTemplate);\n\t\t\tdiagnostics.push(...result.diagnostics);\n\t\t}\n\t}\n\treturn { promptTemplates, diagnostics };\n}\n\n/**\n * Load prompt templates from source-tagged paths.\n *\n * Source values are preserved exactly and attached to every loaded prompt template and diagnostic. The agent package does\n * not interpret source values; applications define their own provenance shape.\n */\nexport async function loadSourcedPromptTemplates<TSource, TPromptTemplate extends PromptTemplate = PromptTemplate>(\n\tenv: ExecutionEnv,\n\tinputs: Array<{ path: string; source: TSource }>,\n\tmapPromptTemplate?: (promptTemplate: PromptTemplate, source: TSource) => TPromptTemplate,\n): Promise<{\n\tpromptTemplates: Array<{ promptTemplate: TPromptTemplate; source: TSource }>;\n\tdiagnostics: Array<PromptTemplateDiagnostic & { source: TSource }>;\n}> {\n\tconst promptTemplates: Array<{ promptTemplate: TPromptTemplate; source: TSource }> = [];\n\tconst diagnostics: Array<PromptTemplateDiagnostic & { source: TSource }> = [];\n\tfor (const input of inputs) {\n\t\tconst result = await loadPromptTemplates(env, input.path);\n\t\tfor (const promptTemplate of result.promptTemplates) {\n\t\t\tpromptTemplates.push({\n\t\t\t\tpromptTemplate: mapPromptTemplate\n\t\t\t\t\t? mapPromptTemplate(promptTemplate, input.source)\n\t\t\t\t\t: (promptTemplate as TPromptTemplate),\n\t\t\t\tsource: input.source,\n\t\t\t});\n\t\t}\n\t\tfor (const diagnostic of result.diagnostics) diagnostics.push({ ...diagnostic, source: input.source });\n\t}\n\treturn { promptTemplates, diagnostics };\n}\n\nasync function loadTemplatesFromDir(\n\tenv: ExecutionEnv,\n\tdir: string,\n): Promise<{ promptTemplates: PromptTemplate[]; diagnostics: PromptTemplateDiagnostic[] }> {\n\tconst promptTemplates: PromptTemplate[] = [];\n\tconst diagnostics: PromptTemplateDiagnostic[] = [];\n\tconst entriesResult = await env.listDir(dir);\n\tif (!entriesResult.ok) {\n\t\tdiagnostics.push({\n\t\t\ttype: \"warning\",\n\t\t\tmessage: entriesResult.error.message,\n\t\t\tpath: dir,\n\t\t});\n\t\treturn { promptTemplates, diagnostics };\n\t}\n\tconst entries = entriesResult.value;\n\n\tfor (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {\n\t\tconst kind = await resolveKind(env, entry);\n\t\tif (kind !== \"file\" || !entry.name.endsWith(\".md\")) continue;\n\t\tconst result = await loadTemplateFromFile(env, entry.path);\n\t\tif (result.promptTemplate) promptTemplates.push(result.promptTemplate);\n\t\tdiagnostics.push(...result.diagnostics);\n\t}\n\treturn { promptTemplates, diagnostics };\n}\n\nasync function loadTemplateFromFile(\n\tenv: ExecutionEnv,\n\tfilePath: string,\n): Promise<{ promptTemplate: PromptTemplate | null; diagnostics: PromptTemplateDiagnostic[] }> {\n\tconst diagnostics: PromptTemplateDiagnostic[] = [];\n\tconst rawContent = await env.readTextFile(filePath);\n\tif (!rawContent.ok) {\n\t\tdiagnostics.push({\n\t\t\ttype: \"warning\",\n\t\t\tmessage: rawContent.error.message,\n\t\t\tpath: filePath,\n\t\t});\n\t\treturn { promptTemplate: null, diagnostics };\n\t}\n\n\tconst parsed = parseFrontmatter<PromptTemplateFrontmatter>(rawContent.value);\n\tif (!parsed.ok) {\n\t\tdiagnostics.push({\n\t\t\ttype: \"warning\",\n\t\t\tmessage: parsed.error.message,\n\t\t\tpath: filePath,\n\t\t});\n\t\treturn { promptTemplate: null, diagnostics };\n\t}\n\n\tconst { frontmatter, body } = parsed.value;\n\tconst firstLine = body.split(\"\\n\").find((line) => line.trim());\n\tlet description = typeof frontmatter.description === \"string\" ? frontmatter.description : \"\";\n\tif (!description && firstLine) {\n\t\tdescription = firstLine.slice(0, 60);\n\t\tif (firstLine.length > 60) description += \"...\";\n\t}\n\treturn {\n\t\tpromptTemplate: {\n\t\t\tname: basenameEnvPath(filePath).replace(/\\.md$/i, \"\"),\n\t\t\tdescription,\n\t\t\tcontent: body,\n\t\t},\n\t\tdiagnostics,\n\t};\n}\n\nasync function resolveKind(env: ExecutionEnv, info: FileInfo): Promise<\"file\" | \"directory\" | undefined> {\n\tif (info.kind === \"file\" || info.kind === \"directory\") return info.kind;\n\tconst canonicalPath = await env.canonicalPath(info.path);\n\tif (!canonicalPath.ok) return undefined;\n\tconst target = getOrUndefined(await env.fileInfo(canonicalPath.value));\n\tif (!target) return undefined;\n\treturn target.kind === \"file\" || target.kind === \"directory\" ? target.kind : undefined;\n}\n\nfunction parseFrontmatter<T extends Record<string, unknown>>(\n\tcontent: string,\n): Result<{ frontmatter: T; body: string }, Error> {\n\ttry {\n\t\tconst normalized = content.replace(/\\r\\n/g, \"\\n\").replace(/\\r/g, \"\\n\");\n\t\tif (!normalized.startsWith(\"---\")) return { ok: true, value: { frontmatter: {} as T, body: normalized } };\n\t\tconst endIndex = normalized.indexOf(\"\\n---\", 3);\n\t\tif (endIndex === -1) return { ok: true, value: { frontmatter: {} as T, body: normalized } };\n\t\tconst yamlString = normalized.slice(4, endIndex);\n\t\tconst body = normalized.slice(endIndex + 4).trim();\n\t\treturn { ok: true, value: { frontmatter: (parse(yamlString) ?? {}) as T, body } };\n\t} catch (error) {\n\t\treturn { ok: false, error: error instanceof Error ? error : new Error(String(error)) };\n\t}\n}\n\nfunction basenameEnvPath(path: string): string {\n\tconst normalized = path.replace(/\\/+$/, \"\");\n\tconst slashIndex = normalized.lastIndexOf(\"/\");\n\treturn slashIndex === -1 ? normalized : normalized.slice(slashIndex + 1);\n}\n\n/** Parse an argument string using simple shell-style single and double quotes. */\nexport function parseCommandArgs(argsString: string): string[] {\n\tconst args: string[] = [];\n\tlet current = \"\";\n\tlet inQuote: string | null = null;\n\n\tfor (let i = 0; i < argsString.length; i++) {\n\t\tconst char = argsString[i]!;\n\t\tif (inQuote) {\n\t\t\tif (char === inQuote) inQuote = null;\n\t\t\telse current += char;\n\t\t} else if (char === '\"' || char === \"'\") {\n\t\t\tinQuote = char;\n\t\t} else if (char === \" \" || char === \"\\t\") {\n\t\t\tif (current) {\n\t\t\t\targs.push(current);\n\t\t\t\tcurrent = \"\";\n\t\t\t}\n\t\t} else {\n\t\t\tcurrent += char;\n\t\t}\n\t}\n\tif (current) args.push(current);\n\treturn args;\n}\n\n/** Substitute prompt template placeholders (`$1`, `$@`, `$ARGUMENTS`, `${@:N}`, `${@:N:L}`) with command arguments. */\nexport function substituteArgs(content: string, args: string[]): string {\n\tlet result = content;\n\tresult = result.replace(/\\$(\\d+)/g, (_, num: string) => args[parseInt(num, 10) - 1] ?? \"\");\n\tresult = result.replace(/\\$\\{@:(\\d+)(?::(\\d+))?\\}/g, (_, startStr: string, lengthStr?: string) => {\n\t\tlet start = parseInt(startStr, 10) - 1;\n\t\tif (start < 0) start = 0;\n\t\tif (lengthStr) return args.slice(start, start + parseInt(lengthStr, 10)).join(\" \");\n\t\treturn args.slice(start).join(\" \");\n\t});\n\tconst allArgs = args.join(\" \");\n\tresult = result.replace(/\\$ARGUMENTS/g, allArgs);\n\tresult = result.replace(/\\$@/g, allArgs);\n\treturn result;\n}\n\n/** Format a prompt template invocation with positional arguments. */\nexport function formatPromptTemplateInvocation(template: PromptTemplate, args: string[] = []): string {\n\treturn substituteArgs(template.content, args);\n}\n\n/** Compatibility alias for older harness consumers. */\nexport const expandPromptTemplate = formatPromptTemplateInvocation;\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { FileSystem, JsonlSessionCreateOptions, JsonlSessionListOptions, JsonlSessionMetadata, JsonlSessionRepoApi, Session } from "../types.js";
|
|
2
|
+
type JsonlSessionRepoFileSystem = Pick<FileSystem, "cwd" | "absolutePath" | "joinPath" | "readTextFile" | "writeFile" | "appendFile" | "listDir" | "exists" | "createDir" | "remove">;
|
|
3
|
+
export declare class JsonlSessionRepo implements JsonlSessionRepoApi {
|
|
4
|
+
private readonly fs;
|
|
5
|
+
private readonly sessionsRootInput;
|
|
6
|
+
private sessionsRoot;
|
|
7
|
+
constructor(options: {
|
|
8
|
+
fs: JsonlSessionRepoFileSystem;
|
|
9
|
+
sessionsRoot: string;
|
|
10
|
+
});
|
|
11
|
+
private getSessionsRoot;
|
|
12
|
+
private getSessionDir;
|
|
13
|
+
private createSessionFilePath;
|
|
14
|
+
create(options: JsonlSessionCreateOptions): Promise<Session<JsonlSessionMetadata>>;
|
|
15
|
+
open(metadata: JsonlSessionMetadata): Promise<Session<JsonlSessionMetadata>>;
|
|
16
|
+
list(options?: JsonlSessionListOptions): Promise<JsonlSessionMetadata[]>;
|
|
17
|
+
delete(metadata: JsonlSessionMetadata): Promise<void>;
|
|
18
|
+
fork(sourceMetadata: JsonlSessionMetadata, options: JsonlSessionCreateOptions & {
|
|
19
|
+
entryId?: string;
|
|
20
|
+
position?: "before" | "at";
|
|
21
|
+
id?: string;
|
|
22
|
+
}): Promise<Session<JsonlSessionMetadata>>;
|
|
23
|
+
private listSessionDirs;
|
|
24
|
+
}
|
|
25
|
+
export {};
|
|
26
|
+
//# sourceMappingURL=jsonl-repo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonl-repo.d.ts","sourceRoot":"","sources":["../../../src/harness/session/jsonl-repo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACX,UAAU,EACV,yBAAyB,EACzB,uBAAuB,EACvB,o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onlSessionMetadata,\n\tJsonlSessionRepoApi,\n\tSession,\n} from \"../types.js\";\nimport { getOrThrow } from \"../types.js\";\nimport { JsonlSessionStorage, loadJsonlSessionMetadata } from \"./jsonl-storage.js\";\nimport { createSessionId, createTimestamp, getEntriesToFork, toSession } from \"./repo-utils.js\";\n\ntype JsonlSessionRepoFileSystem = Pick<\n\tFileSystem,\n\t| \"cwd\"\n\t| \"absolutePath\"\n\t| \"joinPath\"\n\t| \"readTextFile\"\n\t| \"writeFile\"\n\t| \"appendFile\"\n\t| \"listDir\"\n\t| \"exists\"\n\t| \"createDir\"\n\t| \"remove\"\n>;\n\nfunction encodeCwd(cwd: string): string {\n\treturn `--${cwd.replace(/^[/\\\\]/, \"\").replace(/[/\\\\:]/g, \"-\")}--`;\n}\n\nexport class JsonlSessionRepo implements JsonlSessionRepoApi {\n\tprivate readonly fs: JsonlSessionRepoFileSystem;\n\tprivate readonly sessionsRootInput: string;\n\tprivate sessionsRoot: string | undefined;\n\n\tconstructor(options: { fs: JsonlSessionRepoFileSystem; sessionsRoot: string }) {\n\t\tthis.fs = options.fs;\n\t\tthis.sessionsRootInput = options.sessionsRoot;\n\t}\n\n\tprivate async getSessionsRoot(): Promise<string> {\n\t\tif (!this.sessionsRoot) {\n\t\t\tthis.sessionsRoot = getOrThrow(await this.fs.absolutePath(this.sessionsRootInput));\n\t\t}\n\t\treturn this.sessionsRoot;\n\t}\n\n\tprivate async getSessionDir(cwd: string): Promise<string> {\n\t\treturn getOrThrow(await this.fs.joinPath([await this.getSessionsRoot(), encodeCwd(cwd)]));\n\t}\n\n\tprivate async createSessionFilePath(cwd: string, sessionId: string, timestamp: string): Promise<string> {\n\t\treturn getOrThrow(\n\t\t\tawait this.fs.joinPath([\n\t\t\t\tawait this.getSessionDir(cwd),\n\t\t\t\t`${timestamp.replace(/[:.]/g, \"-\")}_${sessionId}.jsonl`,\n\t\t\t]),\n\t\t);\n\t}\n\n\tasync create(options: JsonlSessionCreateOptions): Promise<Session<JsonlSessionMetadata>> {\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst sessionDir = await this.getSessionDir(options.cwd);\n\t\tgetOrThrow(await this.fs.createDir(sessionDir, { recursive: true }));\n\t\tconst filePath = await this.createSessionFilePath(options.cwd, id, createdAt);\n\t\tconst storage = await JsonlSessionStorage.create(this.fs, filePath, {\n\t\t\tcwd: options.cwd,\n\t\t\tsessionId: id,\n\t\t\tparentSessionPath: options.parentSessionPath,\n\t\t});\n\t\treturn toSession(storage);\n\t}\n\n\tasync open(metadata: JsonlSessionMetadata): Promise<Session<JsonlSessionMetadata>> {\n\t\tif (!getOrThrow(await this.fs.exists(metadata.path))) {\n\t\t\tthrow new Error(`Session not found: ${metadata.path}`);\n\t\t}\n\t\tconst storage = await JsonlSessionStorage.open(this.fs, metadata.path);\n\t\treturn toSession(storage);\n\t}\n\n\tasync list(options: JsonlSessionListOptions = {}): Promise<JsonlSessionMetadata[]> {\n\t\tconst dirs = options.cwd ? [await this.getSessionDir(options.cwd)] : await this.listSessionDirs();\n\t\tconst sessions: JsonlSessionMetadata[] = [];\n\t\tfor (const dir of dirs) {\n\t\t\tif (!getOrThrow(await this.fs.exists(dir))) continue;\n\t\t\tconst files = getOrThrow(await this.fs.listDir(dir)).filter(\n\t\t\t\t(file) => file.kind !== \"directory\" && file.name.endsWith(\".jsonl\"),\n\t\t\t);\n\t\t\tfor (const file of files) {\n\t\t\t\ttry {\n\t\t\t\t\tsessions.push(await loadJsonlSessionMetadata(this.fs, file.path));\n\t\t\t\t} catch {\n\t\t\t\t\t// Ignore invalid session files when listing a directory.\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsessions.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());\n\t\treturn sessions;\n\t}\n\n\tasync delete(metadata: JsonlSessionMetadata): Promise<void> {\n\t\tgetOrThrow(await this.fs.remove(metadata.path, { force: true }));\n\t}\n\n\tasync fork(\n\t\tsourceMetadata: JsonlSessionMetadata,\n\t\toptions: JsonlSessionCreateOptions & { entryId?: string; position?: \"before\" | \"at\"; id?: string },\n\t): Promise<Session<JsonlSessionMetadata>> {\n\t\tconst source = await this.open(sourceMetadata);\n\t\tconst forkedEntries = await getEntriesToFork(source.getStorage(), options);\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst sessionDir = await this.getSessionDir(options.cwd);\n\t\tgetOrThrow(await this.fs.createDir(sessionDir, { recursive: true }));\n\t\tconst storage = await JsonlSessionStorage.create(\n\t\t\tthis.fs,\n\t\t\tawait this.createSessionFilePath(options.cwd, id, createdAt),\n\t\t\t{\n\t\t\t\tcwd: options.cwd,\n\t\t\t\tsessionId: id,\n\t\t\t\tparentSessionPath: options.parentSessionPath ?? sourceMetadata.path,\n\t\t\t},\n\t\t);\n\t\tfor (const entry of forkedEntries) {\n\t\t\tawait storage.appendEntry(entry);\n\t\t}\n\t\treturn toSession(storage);\n\t}\n\n\tprivate async listSessionDirs(): Promise<string[]> {\n\t\tconst sessionsRoot = await this.getSessionsRoot();\n\t\tif (!getOrThrow(await this.fs.exists(sessionsRoot))) return [];\n\t\tconst entries = getOrThrow(await this.fs.listDir(sessionsRoot));\n\t\treturn entries.filter((entry) => entry.kind === \"directory\").map((entry) => entry.path);\n\t}\n}\n"]}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { getOrThrow } from "../types.js";
|
|
2
|
+
import { JsonlSessionStorage, loadJsonlSessionMetadata } from "./jsonl-storage.js";
|
|
3
|
+
import { createSessionId, createTimestamp, getEntriesToFork, toSession } from "./repo-utils.js";
|
|
4
|
+
function encodeCwd(cwd) {
|
|
5
|
+
return `--${cwd.replace(/^[/\\]/, "").replace(/[/\\:]/g, "-")}--`;
|
|
6
|
+
}
|
|
7
|
+
export class JsonlSessionRepo {
|
|
8
|
+
fs;
|
|
9
|
+
sessionsRootInput;
|
|
10
|
+
sessionsRoot;
|
|
11
|
+
constructor(options) {
|
|
12
|
+
this.fs = options.fs;
|
|
13
|
+
this.sessionsRootInput = options.sessionsRoot;
|
|
14
|
+
}
|
|
15
|
+
async getSessionsRoot() {
|
|
16
|
+
if (!this.sessionsRoot) {
|
|
17
|
+
this.sessionsRoot = getOrThrow(await this.fs.absolutePath(this.sessionsRootInput));
|
|
18
|
+
}
|
|
19
|
+
return this.sessionsRoot;
|
|
20
|
+
}
|
|
21
|
+
async getSessionDir(cwd) {
|
|
22
|
+
return getOrThrow(await this.fs.joinPath([await this.getSessionsRoot(), encodeCwd(cwd)]));
|
|
23
|
+
}
|
|
24
|
+
async createSessionFilePath(cwd, sessionId, timestamp) {
|
|
25
|
+
return getOrThrow(await this.fs.joinPath([
|
|
26
|
+
await this.getSessionDir(cwd),
|
|
27
|
+
`${timestamp.replace(/[:.]/g, "-")}_${sessionId}.jsonl`,
|
|
28
|
+
]));
|
|
29
|
+
}
|
|
30
|
+
async create(options) {
|
|
31
|
+
const id = options.id ?? createSessionId();
|
|
32
|
+
const createdAt = createTimestamp();
|
|
33
|
+
const sessionDir = await this.getSessionDir(options.cwd);
|
|
34
|
+
getOrThrow(await this.fs.createDir(sessionDir, { recursive: true }));
|
|
35
|
+
const filePath = await this.createSessionFilePath(options.cwd, id, createdAt);
|
|
36
|
+
const storage = await JsonlSessionStorage.create(this.fs, filePath, {
|
|
37
|
+
cwd: options.cwd,
|
|
38
|
+
sessionId: id,
|
|
39
|
+
parentSessionPath: options.parentSessionPath,
|
|
40
|
+
});
|
|
41
|
+
return toSession(storage);
|
|
42
|
+
}
|
|
43
|
+
async open(metadata) {
|
|
44
|
+
if (!getOrThrow(await this.fs.exists(metadata.path))) {
|
|
45
|
+
throw new Error(`Session not found: ${metadata.path}`);
|
|
46
|
+
}
|
|
47
|
+
const storage = await JsonlSessionStorage.open(this.fs, metadata.path);
|
|
48
|
+
return toSession(storage);
|
|
49
|
+
}
|
|
50
|
+
async list(options = {}) {
|
|
51
|
+
const dirs = options.cwd ? [await this.getSessionDir(options.cwd)] : await this.listSessionDirs();
|
|
52
|
+
const sessions = [];
|
|
53
|
+
for (const dir of dirs) {
|
|
54
|
+
if (!getOrThrow(await this.fs.exists(dir)))
|
|
55
|
+
continue;
|
|
56
|
+
const files = getOrThrow(await this.fs.listDir(dir)).filter((file) => file.kind !== "directory" && file.name.endsWith(".jsonl"));
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
try {
|
|
59
|
+
sessions.push(await loadJsonlSessionMetadata(this.fs, file.path));
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
// Ignore invalid session files when listing a directory.
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
sessions.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());
|
|
67
|
+
return sessions;
|
|
68
|
+
}
|
|
69
|
+
async delete(metadata) {
|
|
70
|
+
getOrThrow(await this.fs.remove(metadata.path, { force: true }));
|
|
71
|
+
}
|
|
72
|
+
async fork(sourceMetadata, options) {
|
|
73
|
+
const source = await this.open(sourceMetadata);
|
|
74
|
+
const forkedEntries = await getEntriesToFork(source.getStorage(), options);
|
|
75
|
+
const id = options.id ?? createSessionId();
|
|
76
|
+
const createdAt = createTimestamp();
|
|
77
|
+
const sessionDir = await this.getSessionDir(options.cwd);
|
|
78
|
+
getOrThrow(await this.fs.createDir(sessionDir, { recursive: true }));
|
|
79
|
+
const storage = await JsonlSessionStorage.create(this.fs, await this.createSessionFilePath(options.cwd, id, createdAt), {
|
|
80
|
+
cwd: options.cwd,
|
|
81
|
+
sessionId: id,
|
|
82
|
+
parentSessionPath: options.parentSessionPath ?? sourceMetadata.path,
|
|
83
|
+
});
|
|
84
|
+
for (const entry of forkedEntries) {
|
|
85
|
+
await storage.appendEntry(entry);
|
|
86
|
+
}
|
|
87
|
+
return toSession(storage);
|
|
88
|
+
}
|
|
89
|
+
async listSessionDirs() {
|
|
90
|
+
const sessionsRoot = await this.getSessionsRoot();
|
|
91
|
+
if (!getOrThrow(await this.fs.exists(sessionsRoot)))
|
|
92
|
+
return [];
|
|
93
|
+
const entries = getOrThrow(await this.fs.listDir(sessionsRoot));
|
|
94
|
+
return entries.filter((entry) => entry.kind === "directory").map((entry) => entry.path);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
//# sourceMappingURL=jsonl-repo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"jsonl-repo.js","sourceRoot":"","sources":["../../../src/harness/session/jsonl-repo.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,mBAAmB,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC;AACnF,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAgBhG,SAAS,SAAS,CAAC,GAAW,EAAU;IACvC,OAAO,KAAK,GAAG,CAAC,OAAO,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,OAAO,CAAC,SAAS,EAAE,GAAG,CAAC,IAAI,CAAC;AAAA,CAClE;AAED,MAAM,OAAO,gBAAgB;IACX,EAAE,CAA6B;IAC/B,iBAAiB,CAAS;IACnC,YAAY,CAAqB;IAEzC,YAAY,OAAiE,EAAE;QAC9E,IAAI,CAAC,EAAE,GAAG,OAAO,CAAC,EAAE,CAAC;QACrB,IAAI,CAAC,iBAAiB,GAAG,OAAO,CAAC,YAAY,CAAC;IAAA,CAC9C;IAEO,KAAK,CAAC,eAAe,GAAoB;QAChD,IAAI,CAAC,IAAI,CAAC,YAAY,EAAE,CAAC;YACxB,IAAI,CAAC,YAAY,GAAG,UAAU,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,YAAY,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC;QACpF,CAAC;QACD,OAAO,IAAI,CAAC,YAAY,CAAC;IAAA,CACzB;IAEO,KAAK,CAAC,aAAa,CAAC,GAAW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CAAC,EAC5D;YACC,GAAG,EAAE,OAAO,CAAC,GAAG;YAChB,SAAS,EAAE,EAAE;YACb,iBAAiB,EAAE,OAAO,CAAC,iBAAiB,IAAI,cAAc,CAAC,IAAI;SACnE,CACD,CAAC;QACF,KAAK,MAAM,KAAK,IAAI,aAAa,EAAE,CAAC;YACnC,MAAM,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,SAAS,CAAC,OAAO,CAAC,CAAC;IAAA,CAC1B;IAEO,KAAK,CAAC,eAAe,GAAsB;QAClD,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;QAClD,IAAI,CAAC,UAAU,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;YAAE,OAAO,EAAE,CAAC;QAC/D,MAAM,OAAO,GAAG,UAAU,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC,OAAO,CAAC,YAAY,CAAC,CAAC,CAAC;QAChE,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,WAAW,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAAA,CACxF;CACD","sourcesContent":["import type {\n\tFileSystem,\n\tJsonlSessionCreateOptions,\n\tJsonlSessionListOptions,\n\tJsonlSessionMetadata,\n\tJsonlSessionRepoApi,\n\tSession,\n} from \"../types.js\";\nimport { getOrThrow } from \"../types.js\";\nimport { JsonlSessionStorage, loadJsonlSessionMetadata } from \"./jsonl-storage.js\";\nimport { createSessionId, createTimestamp, getEntriesToFork, toSession } from \"./repo-utils.js\";\n\ntype JsonlSessionRepoFileSystem = Pick<\n\tFileSystem,\n\t| \"cwd\"\n\t| \"absolutePath\"\n\t| \"joinPath\"\n\t| \"readTextFile\"\n\t| \"writeFile\"\n\t| \"appendFile\"\n\t| \"listDir\"\n\t| \"exists\"\n\t| \"createDir\"\n\t| \"remove\"\n>;\n\nfunction encodeCwd(cwd: string): string {\n\treturn `--${cwd.replace(/^[/\\\\]/, \"\").replace(/[/\\\\:]/g, \"-\")}--`;\n}\n\nexport class JsonlSessionRepo implements JsonlSessionRepoApi {\n\tprivate readonly fs: JsonlSessionRepoFileSystem;\n\tprivate readonly sessionsRootInput: string;\n\tprivate sessionsRoot: string | undefined;\n\n\tconstructor(options: { fs: JsonlSessionRepoFileSystem; sessionsRoot: string }) {\n\t\tthis.fs = options.fs;\n\t\tthis.sessionsRootInput = options.sessionsRoot;\n\t}\n\n\tprivate async getSessionsRoot(): Promise<string> {\n\t\tif (!this.sessionsRoot) {\n\t\t\tthis.sessionsRoot = getOrThrow(await this.fs.absolutePath(this.sessionsRootInput));\n\t\t}\n\t\treturn this.sessionsRoot;\n\t}\n\n\tprivate async getSessionDir(cwd: string): Promise<string> {\n\t\treturn getOrThrow(await this.fs.joinPath([await this.getSessionsRoot(), encodeCwd(cwd)]));\n\t}\n\n\tprivate async createSessionFilePath(cwd: string, sessionId: string, timestamp: string): Promise<string> {\n\t\treturn getOrThrow(\n\t\t\tawait this.fs.joinPath([\n\t\t\t\tawait this.getSessionDir(cwd),\n\t\t\t\t`${timestamp.replace(/[:.]/g, \"-\")}_${sessionId}.jsonl`,\n\t\t\t]),\n\t\t);\n\t}\n\n\tasync create(options: JsonlSessionCreateOptions): Promise<Session<JsonlSessionMetadata>> {\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst sessionDir = await this.getSessionDir(options.cwd);\n\t\tgetOrThrow(await this.fs.createDir(sessionDir, { recursive: true }));\n\t\tconst filePath = await this.createSessionFilePath(options.cwd, id, createdAt);\n\t\tconst storage = await JsonlSessionStorage.create(this.fs, filePath, {\n\t\t\tcwd: options.cwd,\n\t\t\tsessionId: id,\n\t\t\tparentSessionPath: options.parentSessionPath,\n\t\t});\n\t\treturn toSession(storage);\n\t}\n\n\tasync open(metadata: JsonlSessionMetadata): Promise<Session<JsonlSessionMetadata>> {\n\t\tif (!getOrThrow(await this.fs.exists(metadata.path))) {\n\t\t\tthrow new Error(`Session not found: ${metadata.path}`);\n\t\t}\n\t\tconst storage = await JsonlSessionStorage.open(this.fs, metadata.path);\n\t\treturn toSession(storage);\n\t}\n\n\tasync list(options: JsonlSessionListOptions = {}): Promise<JsonlSessionMetadata[]> {\n\t\tconst dirs = options.cwd ? [await this.getSessionDir(options.cwd)] : await this.listSessionDirs();\n\t\tconst sessions: JsonlSessionMetadata[] = [];\n\t\tfor (const dir of dirs) {\n\t\t\tif (!getOrThrow(await this.fs.exists(dir))) continue;\n\t\t\tconst files = getOrThrow(await this.fs.listDir(dir)).filter(\n\t\t\t\t(file) => file.kind !== \"directory\" && file.name.endsWith(\".jsonl\"),\n\t\t\t);\n\t\t\tfor (const file of files) {\n\t\t\t\ttry {\n\t\t\t\t\tsessions.push(await loadJsonlSessionMetadata(this.fs, file.path));\n\t\t\t\t} catch {\n\t\t\t\t\t// Ignore invalid session files when listing a directory.\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tsessions.sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime());\n\t\treturn sessions;\n\t}\n\n\tasync delete(metadata: JsonlSessionMetadata): Promise<void> {\n\t\tgetOrThrow(await this.fs.remove(metadata.path, { force: true }));\n\t}\n\n\tasync fork(\n\t\tsourceMetadata: JsonlSessionMetadata,\n\t\toptions: JsonlSessionCreateOptions & { entryId?: string; position?: \"before\" | \"at\"; id?: string },\n\t): Promise<Session<JsonlSessionMetadata>> {\n\t\tconst source = await this.open(sourceMetadata);\n\t\tconst forkedEntries = await getEntriesToFork(source.getStorage(), options);\n\t\tconst id = options.id ?? createSessionId();\n\t\tconst createdAt = createTimestamp();\n\t\tconst sessionDir = await this.getSessionDir(options.cwd);\n\t\tgetOrThrow(await this.fs.createDir(sessionDir, { recursive: true }));\n\t\tconst storage = await JsonlSessionStorage.create(\n\t\t\tthis.fs,\n\t\t\tawait this.createSessionFilePath(options.cwd, id, createdAt),\n\t\t\t{\n\t\t\t\tcwd: options.cwd,\n\t\t\t\tsessionId: id,\n\t\t\t\tparentSessionPath: options.parentSessionPath ?? sourceMetadata.path,\n\t\t\t},\n\t\t);\n\t\tfor (const entry of forkedEntries) {\n\t\t\tawait storage.appendEntry(entry);\n\t\t}\n\t\treturn toSession(storage);\n\t}\n\n\tprivate async listSessionDirs(): Promise<string[]> {\n\t\tconst sessionsRoot = await this.getSessionsRoot();\n\t\tif (!getOrThrow(await this.fs.exists(sessionsRoot))) return [];\n\t\tconst entries = getOrThrow(await this.fs.listDir(sessionsRoot));\n\t\treturn entries.filter((entry) => entry.kind === \"directory\").map((entry) => entry.path);\n\t}\n}\n"]}
|