Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 64 additions & 9 deletions server/shiso/conversationTurn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ const SYSTEM_PROMPT = [
" it, and — when the user asked you to FIX or APPLY — apply the supported corrections",
" and recalculate. Do it all in this conversation.",
"- When the user explicitly asks to RESOLVE, FINISH or COMPLETE the missing/unresolved",
" nutrition (for example 'resolve the last two ingredients', 'finish the nutrition',",
" 'estimate whatever is missing'), call complete_nutrition IMMEDIATELY. That single",
" capability runs the bounded source-first pipeline and prepares the review; do not",
" merely suggest searches or tell the user to do it manually.",
" nutrition and MORE THAN ONE line is unresolved, first call",
" investigate_unresolved_ingredients ONCE. Act on its select_source next_actions,",
" ask one consolidated question for genuine choices, then call complete_nutrition",
" for what remains. For one simple unresolved line, complete_nutrition directly.",
" Do not merely suggest searches or tell the user to do it manually.",
"- When the user asks you to FIX, CORRECT, CHANGE or UPDATE the recipe or its nutrition",
" ('fix this recipe', 'apply all supported fixes', 'change the chicken to turkey and",
" update the nutrition'), their instruction IS the authorization. Execute the supported",
Expand All @@ -95,6 +96,42 @@ const SYSTEM_PROMPT = [
" never about whether you are allowed to look something up or change what they asked",
" you to change.",
"",
"AGENT EXECUTION — UNDERSTAND, PLAN, ACT, OBSERVE, VERIFY",
"- For a multi-step goal, identify the requested outcome, inspect the current recipe/review,",
" choose the existing capabilities that can do the work, and execute a bounded plan.",
" Research is not completion. A search result is not a fix. Continue to the authorized",
" next action when the tool observation gives you one; stop only for a genuine user",
" decision, missing evidence, missing permission, or a failed operation.",
"- VERIFY every write from the tool observation. Never claim a source was selected,",
" nutrition applied, a recipe changed, or a save occurred unless the corresponding tool",
" returned success. If a step failed, say what failed and what remains.",
"- Do not repeat an identical read, search, inspection, or failed correction. Use the",
" evidence already returned. If a tool has a next_action, use it or explain why it is",
" blocked. Do not exhaust the reasoning budget on rediscovery.",
"- Full recipe: get_current_recipe is paged. Its result includes instructions_total,",
" instructions_offset, instructions_complete and next_instruction_offset. If it is not",
" complete, request the next page before judging the cooking method. Never say the",
" instructions were cut off without making that bounded pagination attempt.",
"- If get_vault_recipe reports instructions_truncated, select that recipe and use",
" get_current_recipe pagination before making a method judgement.",
"- Culinary investigation: for unresolved nutrition with several ingredients, call",
" investigate_unresolved_ingredients ONCE. It returns a culinary projection per line",
" (identity, quantity, preparation, alternatives, flexible mixture, measurement note),",
" compatible candidates, next_actions and pending_user_choices. Use the culinary",
" evidence, not raw token overlap.",
"- For a next_action with kind='select_source', call select_usda_nutrition_source with",
" its line_ref/fdc_id/portion_index. A successful search or inspection alone is not a",
" resolution. For equal authored alternatives, flexible mixtures, or vague measures,",
" ask the user; do not silently choose.",
"- If pending_user_choices is not empty, ask ONE consolidated question covering those",
" choices (use consolidated_question when present). Do not ask the same thing six times.",
" After the user answers, continue the unfinished task and reuse still-valid evidence;",
" revalidate if the recipe, review, Vault or catalogue identity changed.",
"- Chef analysis: read the complete instructions first, distinguish verified source",
" facts from your own culinary judgement, and give recommendations tied to the actual",
" steps/ingredients. You may propose/perform supported ingredient edits when authorized.",
" Never claim an instruction step was edited unless a tool actually changed it.",
"",
"CONFIRMATIONS — A SHORT \"YES\" IS THE GO-AHEAD",
"- If YOUR previous message asked whether to make a supported change (for example",
" \"Shall I rename that ingredient to jalapeño?\" or \"Want me to correct the nutrition?\")",
Expand Down Expand Up @@ -213,6 +250,10 @@ const SYSTEM_PROMPT = [
" not stall on a list of possibilities when the application can complete the work.",
"- Proposals remain the right answer when the user should CHOOSE between options: they",
" appear in the chat for review, and the user confirms them explicitly.",
"- When a culinary block is present, it overrides raw token-overlap candidates. Never",
" propose a different food head or a composite dish it marked incompatible. If",
" pending_user_choices is non-empty, ask the consolidated question rather than six",
" separate clarifications.",
"",
"WHAT YOU CANNOT DO",
"- You cannot generate, edit, or retrieve recipe images. If asked for an image, say",
Expand Down Expand Up @@ -242,7 +283,7 @@ const SYSTEM_PROMPT = [
"",
"TOOLS",
...[
"get_current_recipe read the recipe the user is looking at (each ingredient carries its line_ref handle)",
"get_current_recipe read the recipe the user is looking at; instructions are paged (line_ref handles included)",
"get_vault_status read the authoritative active Vault recipe count and source",
"search_vault_recipes search the active Vault by title, ingredient, category, or metadata",
"get_vault_recipe retrieve a bounded recipe detail from the active Vault",
Expand All @@ -254,7 +295,7 @@ const SYSTEM_PROMPT = [
"inspect_usda_food read one authenticated record and its portions",
"select_usda_nutrition_source place one authenticated food + portion into the working review",
"investigate_ingredient_amount diagnose ONE unresolved ingredient line",
"investigate_unresolved_ingredients diagnose EVERY unresolved line in one step (use this for 'all'/'investigate these')",
"investigate_unresolved_ingredients diagnose every unresolved line in one step; returns culinary identity, alternatives, next_actions and consolidated choices (use this for 'all'/'these')",
"run_nutrition_analysis prepare source-backed and validated AI-estimated nutrition in the unsaved review",
"complete_nutrition COMPLETE the missing nutrition: generic USDA first, then USDA Branded manufacturer evidence, then bounded external research, then validated AI estimates for what remains (unsaved review only)",
"get_capability_status say what is missing and the one action that fixes it",
Expand All @@ -266,8 +307,9 @@ const SYSTEM_PROMPT = [
"",
"TOOL ARGUMENTS — EXACT JSON KEYS (never invent key names)",
"- A tool call is {\"kind\":\"tool_request\",\"tool\":\"<name>\",\"args\":{...}}.",
"- No arguments: get_current_recipe, get_nutrition_review, get_branded_evidence, investigate_unresolved_ingredients,",
"- No arguments: get_nutrition_review, get_branded_evidence, investigate_unresolved_ingredients,",
" get_vault_status, load_usda_catalog, run_nutrition_analysis, complete_nutrition, get_capability_status, apply_nutrition_review, save_working_recipe.",
"- {\"tool\":\"get_current_recipe\",\"instruction_offset\":0,\"instruction_limit\":20} (use next_instruction_offset until complete)",
"- {\"tool\":\"investigate_ingredient_amount\",\"line_ref\":\"<from get_current_recipe>\"}",
"- {\"tool\":\"search_usda_foods\",\"query\":\"...\"}",
"- {\"tool\":\"inspect_usda_food\",\"fdc_id\":123}",
Expand Down Expand Up @@ -505,8 +547,21 @@ export function parseShisoDecision(raw: unknown): ShisoModelDecision | null {
function readToolArgs(tool: ShisoToolName, raw: unknown): ShisoToolArgs | null {
const rec = (raw === null || typeof raw !== "object" || Array.isArray(raw) ? {} : raw) as Record<string, unknown>;
switch (tool) {
case 'get_current_recipe':
return { tool };
case 'get_current_recipe': {
const offsetRaw = rec['instruction_offset'];
const limitRaw = rec['instruction_limit'];
const instruction_offset = typeof offsetRaw === 'number' && Number.isFinite(offsetRaw) && offsetRaw >= 0
? Math.min(1000, Math.floor(offsetRaw))
: undefined;
const instruction_limit = typeof limitRaw === 'number' && Number.isFinite(limitRaw) && limitRaw > 0
? Math.max(1, Math.min(SHISO_LIMITS.maxInstructionPage, Math.floor(limitRaw)))
: undefined;
return {
tool,
...(instruction_offset !== undefined ? { instruction_offset } : {}),
...(instruction_limit !== undefined ? { instruction_limit } : {}),
};
}
case 'get_vault_status':
return { tool };
case 'search_vault_recipes': {
Expand Down
49 changes: 47 additions & 2 deletions src/application-ui/useShisoRecipeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,49 @@ import { parseIngredient } from '../core/nutritionV2/matching/parse';

export interface ShisoRecipeView {
readonly title: string;
readonly description?: string;
readonly servings: number | null;
readonly category?: string;
readonly cuisine?: string;
readonly difficulty?: string;
readonly tags?: readonly string[];
readonly prepTime?: string;
readonly cookTime?: string;
readonly totalTime?: string;
readonly ingredients: ReadonlyArray<{
readonly text: string;
readonly name: string;
/** Canonical authored quantity, retained for nutrition actions. */
readonly amount?: number;
/** Canonical authored unit, retained for nutrition actions. */
readonly unit?: string;
/** Authored preparation after the first comma, when present. */
readonly preparation?: string;
/** H-2 stable edit handle; null when the recipe cannot be adapted. */
readonly line_ref?: string | null;
}>;
readonly instructions: ReadonlyArray<{ readonly text: string }>;
readonly instructions: ReadonlyArray<{
readonly text: string;
/** Stable, non-secret step handle used by instruction reads. */
readonly step_ref: string;
}>;
}

export interface ShisoRecipeLike {
readonly title: string;
readonly description?: string;
readonly servings?: number | string;
readonly category?: string;
readonly cuisine?: string;
readonly difficulty?: string;
readonly tags?: readonly string[];
readonly prepTime?: string;
readonly cookTime?: string;
readonly totalTime?: string;
readonly ingredients: ReadonlyArray<{
readonly name: string;
readonly original?: string;
readonly note?: string;
readonly quantity?: number;
readonly amount?: number | null;
readonly unit?: string;
Expand Down Expand Up @@ -112,14 +135,36 @@ export function toShisoRecipeView(recipe: ShisoRecipeLike | null): ShisoRecipeVi
name,
...(amount !== undefined ? { amount } : {}),
...(typeof unit === 'string' && unit.length > 0 ? { unit } : {}),
...(typeof i.note === 'string' && i.note.trim().length > 0 ? { preparation: i.note.trim().slice(0, 240) } : {}),
line_ref: typeof lineRefs[index] === 'string' ? lineRefs[index] : null,
};
});
return {
title: String(recipe.title ?? '').slice(0, 200),
...(typeof recipe.description === 'string' && recipe.description.trim().length > 0
? { description: recipe.description.trim().slice(0, 1200) }
: {}),
servings: parseServings(recipe.servings),
...(typeof recipe.category === 'string' && recipe.category.trim().length > 0
? { category: recipe.category.trim().slice(0, 120) }
: {}),
...(typeof recipe.cuisine === 'string' && recipe.cuisine.trim().length > 0
? { cuisine: recipe.cuisine.trim().slice(0, 120) }
: {}),
...(typeof recipe.difficulty === 'string' && recipe.difficulty.trim().length > 0
? { difficulty: recipe.difficulty.trim().slice(0, 40) }
: {}),
...(Array.isArray(recipe.tags) ? { tags: recipe.tags.slice(0, 30).map((tag) => String(tag).slice(0, 60)) } : {}),
...(typeof recipe.prepTime === 'string' && recipe.prepTime.trim().length > 0 ? { prepTime: recipe.prepTime.trim().slice(0, 40) } : {}),
...(typeof recipe.cookTime === 'string' && recipe.cookTime.trim().length > 0 ? { cookTime: recipe.cookTime.trim().slice(0, 40) } : {}),
...(typeof recipe.totalTime === 'string' && recipe.totalTime.trim().length > 0 ? { totalTime: recipe.totalTime.trim().slice(0, 40) } : {}),
ingredients,
instructions: rawInstructions.slice(0, 60).map((s) => ({ text: String(s.text ?? '').slice(0, 400) })),
// Instructions stay available for bounded retrieval. They are NEVER sent in
// the conversation context automatically; the read capability pages them.
instructions: rawInstructions.slice(0, 200).map((step, index) => ({
text: String(step.text ?? '').slice(0, 4000),
step_ref: `step:${index}`,
})),
};
}

Expand Down
Loading