922a57fe34
Self-hosted 開源:WASM 零件 + recipe + cypher-executor,跑在你自己的 Cloudflare。 此為重建的乾淨歷史起點(移除曾誤 commit 的 GCP SA 金鑰,舊歷史保留在 richblack/arcrun 與本地 backup 分支)。含: - acr init --self-hosted installer(建 KV/R2 + codeload 拉預編譯 wasm + wrangler deploy + seed recipe) - recipe push 把關(資料外流提醒 + 打通檢查) - 19 個正當零件預編譯 wasm(claude_api/km_writer/kbdb_upsert_block 排除:違反 DECISIONS §1) - CLI / cypher-executor / registry / 完整 SDD Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
/**
|
||
* Prompt recipe loader:從 RECIPES KV 抓 prompt_recipe 定義並驗證
|
||
* SDD: matrix/arcrun/.agents/specs/recipe-system/design.md Phase 1.3
|
||
*
|
||
* KV key 格式:prompt_recipe:{name}
|
||
* KV value:JSON 字串(不用 YAML,避免引入 yaml parser 進 worker)
|
||
*/
|
||
|
||
import { PromptRecipeSchema, type PromptRecipe } from './prompt-recipe-schema';
|
||
|
||
type KvBinding = { get: (key: string) => Promise<string | null> };
|
||
|
||
export class RecipeLoadError extends Error {
|
||
constructor(message: string, public readonly recipe: string) {
|
||
super(message);
|
||
}
|
||
}
|
||
|
||
/** 從 RECIPES KV 抓 + parse + validate */
|
||
export async function loadPromptRecipe(
|
||
recipeRef: string, // 完整 key 如 "prompt_recipe:wiki_synthesis",或裸名 "wiki_synthesis"
|
||
recipesKv: KvBinding,
|
||
): Promise<PromptRecipe> {
|
||
const key = recipeRef.startsWith('prompt_recipe:')
|
||
? recipeRef
|
||
: `prompt_recipe:${recipeRef}`;
|
||
|
||
const raw = await recipesKv.get(key);
|
||
if (!raw) {
|
||
throw new RecipeLoadError(`找不到 recipe: ${key}`, key);
|
||
}
|
||
|
||
let parsed: unknown;
|
||
try {
|
||
parsed = JSON.parse(raw);
|
||
} catch (e) {
|
||
throw new RecipeLoadError(
|
||
`recipe ${key} 不是合法 JSON: ${e instanceof Error ? e.message : String(e)}`,
|
||
key,
|
||
);
|
||
}
|
||
|
||
const result = PromptRecipeSchema.safeParse(parsed);
|
||
if (!result.success) {
|
||
const issues = result.error.issues.map((i) => `${i.path.join('.')}: ${i.message}`).join('; ');
|
||
throw new RecipeLoadError(`recipe ${key} schema 驗證失敗: ${issues}`, key);
|
||
}
|
||
|
||
return result.data;
|
||
}
|