diff --git a/.omo/evidence/pr1-prompts-core/loader.green.log b/.omo/evidence/pr1-prompts-core/loader.green.log new file mode 100644 index 000000000..ea4122b83 --- /dev/null +++ b/.omo/evidence/pr1-prompts-core/loader.green.log @@ -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] diff --git a/.omo/evidence/pr1-prompts-core/s10.green.log b/.omo/evidence/pr1-prompts-core/s10.green.log new file mode 100644 index 000000000..ea4122b83 --- /dev/null +++ b/.omo/evidence/pr1-prompts-core/s10.green.log @@ -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] diff --git a/.omo/evidence/pr1-prompts-core/s11.green.log b/.omo/evidence/pr1-prompts-core/s11.green.log new file mode 100644 index 000000000..ea4122b83 --- /dev/null +++ b/.omo/evidence/pr1-prompts-core/s11.green.log @@ -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] diff --git a/.omo/evidence/pr1-prompts-core/s8.green.log b/.omo/evidence/pr1-prompts-core/s8.green.log new file mode 100644 index 000000000..ea4122b83 --- /dev/null +++ b/.omo/evidence/pr1-prompts-core/s8.green.log @@ -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] diff --git a/.omo/evidence/pr1-prompts-core/s9.green.log b/.omo/evidence/pr1-prompts-core/s9.green.log new file mode 100644 index 000000000..ea4122b83 --- /dev/null +++ b/.omo/evidence/pr1-prompts-core/s9.green.log @@ -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] diff --git a/packages/prompts-core/src/loader.ts b/packages/prompts-core/src/loader.ts index 789900696..87085ae3d 100644 --- a/packages/prompts-core/src/loader.ts +++ b/packages/prompts-core/src/loader.ts @@ -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 { + 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 { return { - body: "__red__", - frontmatter: {}, - resolvedPath: "__red__", + body, + frontmatter: parsed.data, + resolvedPath, } }