feat(prompts-core): load markdown prompts with injection
Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
bun test v1.3.12 (700fc117)
|
||||
|
||||
packages/prompts-core/src/loader.test.ts:
|
||||
S8 actual: {"body":"Hello {PLACEHOLDER}, this is a test.\n","frontmatter":{"description":"test fixture"},"resolvedPath":"/Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/demo/default.md"}
|
||||
S9 actual: Prompt not found at /Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/missing/default.md
|
||||
S10 actual: Hello Y, this is a test.
|
||||
|
||||
S11 actual: HeLLo Y, this is a demo.
|
||||
|
||||
|
||||
4 pass
|
||||
0 fail
|
||||
5 expect() calls
|
||||
Ran 4 tests across 1 file. [43.00ms]
|
||||
@@ -0,0 +1,14 @@
|
||||
bun test v1.3.12 (700fc117)
|
||||
|
||||
packages/prompts-core/src/loader.test.ts:
|
||||
S8 actual: {"body":"Hello {PLACEHOLDER}, this is a test.\n","frontmatter":{"description":"test fixture"},"resolvedPath":"/Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/demo/default.md"}
|
||||
S9 actual: Prompt not found at /Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/missing/default.md
|
||||
S10 actual: Hello Y, this is a test.
|
||||
|
||||
S11 actual: HeLLo Y, this is a demo.
|
||||
|
||||
|
||||
4 pass
|
||||
0 fail
|
||||
5 expect() calls
|
||||
Ran 4 tests across 1 file. [43.00ms]
|
||||
@@ -0,0 +1,14 @@
|
||||
bun test v1.3.12 (700fc117)
|
||||
|
||||
packages/prompts-core/src/loader.test.ts:
|
||||
S8 actual: {"body":"Hello {PLACEHOLDER}, this is a test.\n","frontmatter":{"description":"test fixture"},"resolvedPath":"/Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/demo/default.md"}
|
||||
S9 actual: Prompt not found at /Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/missing/default.md
|
||||
S10 actual: Hello Y, this is a test.
|
||||
|
||||
S11 actual: HeLLo Y, this is a demo.
|
||||
|
||||
|
||||
4 pass
|
||||
0 fail
|
||||
5 expect() calls
|
||||
Ran 4 tests across 1 file. [43.00ms]
|
||||
@@ -0,0 +1,14 @@
|
||||
bun test v1.3.12 (700fc117)
|
||||
|
||||
packages/prompts-core/src/loader.test.ts:
|
||||
S8 actual: {"body":"Hello {PLACEHOLDER}, this is a test.\n","frontmatter":{"description":"test fixture"},"resolvedPath":"/Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/demo/default.md"}
|
||||
S9 actual: Prompt not found at /Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/missing/default.md
|
||||
S10 actual: Hello Y, this is a test.
|
||||
|
||||
S11 actual: HeLLo Y, this is a demo.
|
||||
|
||||
|
||||
4 pass
|
||||
0 fail
|
||||
5 expect() calls
|
||||
Ran 4 tests across 1 file. [43.00ms]
|
||||
@@ -0,0 +1,14 @@
|
||||
bun test v1.3.12 (700fc117)
|
||||
|
||||
packages/prompts-core/src/loader.test.ts:
|
||||
S8 actual: {"body":"Hello {PLACEHOLDER}, this is a test.\n","frontmatter":{"description":"test fixture"},"resolvedPath":"/Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/demo/default.md"}
|
||||
S9 actual: Prompt not found at /Users/yeongyu/local-workspaces/omo-wt/feat/prompts-core-foundation/packages/prompts-core/src/__fixtures__/missing/default.md
|
||||
S10 actual: Hello Y, this is a test.
|
||||
|
||||
S11 actual: HeLLo Y, this is a demo.
|
||||
|
||||
|
||||
4 pass
|
||||
0 fail
|
||||
5 expect() calls
|
||||
Ran 4 tests across 1 file. [43.00ms]
|
||||
@@ -1,9 +1,38 @@
|
||||
import type { LoadedPrompt, LoadPromptInput } from "./types"
|
||||
import { parseFrontmatter } from "@oh-my-opencode/utils"
|
||||
import { readFile } from "node:fs/promises"
|
||||
import { resolve } from "node:path"
|
||||
|
||||
import { PromptNotFoundError, type LoadedPrompt, type LoadPromptInput } from "./types"
|
||||
|
||||
function hasErrorCode(error: unknown, code: string): boolean {
|
||||
if (!(error instanceof Error)) return false
|
||||
if (!("code" in error)) return false
|
||||
return error.code === code
|
||||
}
|
||||
|
||||
export async function loadPrompt(input: LoadPromptInput): Promise<LoadedPrompt> {
|
||||
const resolvedPath = resolve(input.source, input.name, `${input.variant}.md`)
|
||||
let content: string
|
||||
|
||||
try {
|
||||
content = await readFile(resolvedPath, "utf8")
|
||||
} catch (error) {
|
||||
if (hasErrorCode(error, "ENOENT")) {
|
||||
throw new PromptNotFoundError(`Prompt not found at ${resolvedPath}`)
|
||||
}
|
||||
throw error
|
||||
}
|
||||
|
||||
const parsed = parseFrontmatter(content)
|
||||
let body = parsed.body
|
||||
|
||||
for (const injection of input.inject ?? []) {
|
||||
body = body.replaceAll(injection.placeholder, injection.resolver())
|
||||
}
|
||||
|
||||
export async function loadPrompt(_input: LoadPromptInput): Promise<LoadedPrompt> {
|
||||
return {
|
||||
body: "__red__",
|
||||
frontmatter: {},
|
||||
resolvedPath: "__red__",
|
||||
body,
|
||||
frontmatter: parsed.data,
|
||||
resolvedPath,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user