Files
mcp/cookbook/07-structured-schema-extraction.ts
zeekay 56a2b02dcf kai tools: next action, recovery and extraction without guesses; reads linked, not quoted
kai_next_action never returns a candidate whose preconditions are unmet,
returns a single remaining one as is, and picks among several by BM25
against the goal, abstaining on a tie or no shared word; no Kai call (a
Kai choice over candidates read below BM25 for tools). kai_recover maps
each kai_log_triage class to a step (permission asks the user) and keeps
the three-failure switch. kai_extract computes a yes/no field's confidence
from P(true), abstains in Kai's unknown band (.35 to .65), and returns a
score field's level rather than its mean.

Recipes 02, 06, 07, 11, 12 and 14 check what the tools return, including
abstention; the cookbook README and the tools' comments link the research
runs instead of quoting numbers. bench/tools/post.py files each tool's read
(benchmarks, one run an arm, items and cases as artifacts) to /v1/research.
2026-10-14 18:30:09 -07:00

61 lines
2.3 KiB
TypeScript

/**
* Recipe 07: Structured Value Extraction
*
* Objective: Extract discrete categorical classifications, risk ratings, and boolean flags
* from pull request descriptions or issue tickets in a single fast forward pass.
*/
import { createRecipeClient, logRecipe, RecipeResult } from "./helper.js";
export async function runRecipe07(): Promise<RecipeResult> {
const client = await createRecipeClient();
const prText = `
PR Title: Migrate payment gateway from Stripe v2 to v3 webhook endpoints
Description:
This change updates our payment webhook listener to handle asynchronous bank debits
and 3D-Secure 2.0 verification. It touches credit card transaction storage and
database billing columns. Comprehensive unit tests and integration tests have been added.
Database migration 0042_add_payment_intent.sql must be applied before deployment.
`;
const res = await client.callTool("kai_extract", {
text: prText,
schema: {
domain: {
type: "choice",
description: "Primary domain impacted by this pull request",
options: ["billing_and_payments", "auth_and_iam", "frontend_ui", "infra_devops"]
},
risk_score: {
type: "score",
description: "Production risk severity of this change",
levels: ["low_risk_cosmetic", "medium_risk_feature", "high_risk_financial_or_security"]
},
requires_db_migration: {
type: "choice",
description: "Does this change require running database migrations prior to release?",
options: ["requires_migration", "no_migration_needed"]
}
}
});
const result: RecipeResult = {
recipe: "07: Structured Value Extraction",
tool: "kai_extract",
// A field counts when it is right or abstains; every field carries its confidence.
success: !res.isError &&
[["domain", "billing_and_payments"], ["requires_db_migration", "requires_migration"]]
.every(([k, v]) => res.data.extracted[k].abstained || res.data.extracted[k].value === v),
latency_ms: res.latency_ms,
output: res.data,
explanation: "Three typed fields in one call, each with its confidence; a field near uniform abstains. Low confidence here means check before acting."
};
return result;
}
if (process.argv[1]?.endsWith("07-structured-schema-extraction.ts")) {
runRecipe07().then(logRecipe).catch(console.error);
}