Files
FabledGrimoire/frontend/src/components/LessonSituationsPanel.vue
T
bvandeusenandClaude Opus 5.5 c22e8e1844
CI & Build / Plugin hooks (push) Successful in 13s
CI & Build / Python lint (push) Successful in 2s
CI & Build / TypeScript typecheck (push) Successful in 33s
CI & Build / Python tests (push) Failing after 1m41s
CI & Build / integration (push) Successful in 2m8s
CI & Build / Build & push image (push) Skipped
feat(7836): a lesson arrives at the situation confirmed for it, once per session
Each lesson can carry its situation: a moment of work (work.deliver,
work.finish, ...) or a typed trigger on the act (path, command, tool,
keyword, matched as rule triggers are). A pass proposes, a person judges:

- lesson_situations (migration 0137): one row per lesson, kind and value,
  suggested / confirmed / rejected; `none` records that no situation fits;
- MCP: lessons_to_situate, propose_lesson_situations,
  lesson_situation_proposals, judge_lesson_situations;
- Settings > Moments lists waiting proposals with Apply / Not this /
  Apply all; a lesson page lists its situations and lets a writer add one
  or take one off (kept as rejected, never proposed again).

Delivery: the moment doors (the plugin catch-all hook and MCP replies) add
the lessons whose confirmed moment the act reached or whose trigger it
matches. The plugin door shows each once per session, using the record
ledger, and records it as pushed (source lesson_situation); the tool filter
now includes the tools a lesson trigger listens on.

The edit door no longer finds lessons by meaning: lesson leaves the
work.change default kinds.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-25 06:10:23 -05:00

231 lines
8.0 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, onMounted, ref, watch } from "vue";
import {
addLessonSituation, getLessonSituations, judgeLessonSituations, removeLessonSituation,
type LessonSituation, type LessonSituationsPayload,
} from "@/api/lessons";
import type { RuleTrigger } from "@/api/rulebooks";
import { apiErrorMessage } from "@/api/client";
import { useToastStore } from "@/stores/toast";
/**
* When a lesson arrives. A situation is a moment of work (delivering,
* finishing, …) or a trigger on an act (a path, a command, a tool, a word);
* the lesson is shown to a session when its work reaches one, once per
* session. Proposals from a pass wait here and in Settings › Moments until
* applied or declined. A writer can add a situation directly or take one off;
* one taken off is not proposed again.
*/
const props = defineProps<{ lessonId: number; canWrite: boolean }>();
const toast = useToastStore();
const data = ref<LessonSituationsPayload | null>(null);
const failed = ref(false);
const busy = ref(false);
// The add form: what kind of situation, then a moment or a pattern.
const what = ref<"moment" | RuleTrigger["kind"]>("moment");
const moment = ref("");
const pattern = ref("");
// Placeholders are generic shapes, never anyone's own paths or commands.
const PLACEHOLDER: Record<string, string> = {
path: "e.g. migrations/**/*.py",
command: "e.g. git push",
tool: "e.g. edit",
keyword: "e.g. release",
};
const live = computed(() => (data.value?.situations ?? []).filter((s) => s.state === "confirmed"));
const waiting = computed(() => (data.value?.situations ?? []).filter((s) => s.state === "suggested"));
const canAdd = computed(() =>
what.value === "moment" ? !!moment.value : pattern.value.trim().length > 0,
);
async function load() {
try {
data.value = await getLessonSituations(props.lessonId);
failed.value = false;
} catch {
failed.value = true;
}
}
async function run(action: () => Promise<unknown>, done: string, failure: string) {
busy.value = true;
try {
await action();
toast.show(done, "success");
await load();
} catch (e) {
toast.show(apiErrorMessage(e, failure), "error");
} finally {
busy.value = false;
}
}
function add() {
const body = what.value === "moment"
? { moment: moment.value }
: { trigger: { kind: what.value, pattern: pattern.value.trim() } };
return run(async () => {
await addLessonSituation(props.lessonId, body);
moment.value = "";
pattern.value = "";
}, "The lesson now arrives there", "Could not add that situation");
}
function remove(s: LessonSituation) {
return run(() => removeLessonSituation(s.id), "Taken off", "Could not take that off");
}
function judge(s: LessonSituation, verdict: "confirm" | "reject") {
return run(
() => judgeLessonSituations([{
id: s.id, verdict, note: verdict === "confirm" ? "Applied on the lesson." : "Declined on the lesson.",
}]),
verdict === "confirm" ? "Applied" : "Declined",
"Could not record that",
);
}
onMounted(load);
watch(() => props.lessonId, load);
</script>
<template>
<section class="ls-panel" aria-labelledby="ls-title">
<h2 id="ls-title" class="ls-label">When it arrives</h2>
<p v-if="failed" class="ls-muted">
The situations could not be loaded.
<button type="button" class="ls-text-btn" @click="load">Try again</button>
</p>
<template v-else-if="data">
<ul v-if="live.length" class="ls-list">
<li v-for="s in live" :key="s.id" class="ls-item">
<code class="ls-value">{{ s.describes }}</code>
<button
v-if="canWrite" type="button" class="ls-text-btn"
:disabled="busy" :aria-label="`Take off ${s.describes}`"
@click="remove(s)"
>Take off</button>
</li>
</ul>
<p v-else class="ls-muted">
No situation yet — the lesson is found only when a message resembles it.
</p>
<template v-if="waiting.length">
<h3 class="ls-sub">Proposed</h3>
<ul class="ls-list">
<li v-for="s in waiting" :key="s.id" class="ls-item">
<code class="ls-value">{{ s.describes }}</code>
<span v-if="s.note" class="ls-note">{{ s.note }}</span>
<span v-if="canWrite" class="ls-actions">
<button type="button" class="ls-btn" :disabled="busy" @click="judge(s, 'confirm')">Apply</button>
<button type="button" class="ls-text-btn" :disabled="busy" @click="judge(s, 'reject')">Not this</button>
</span>
</li>
</ul>
</template>
<form v-if="canWrite" class="ls-add" @submit.prevent="add">
<label class="ls-field">
<span class="ls-field-label">Arrive at</span>
<select v-model="what" class="ls-input" :disabled="busy">
<option value="moment">a moment of work</option>
<option v-for="k in data.trigger_kinds" :key="k" :value="k">a {{ k }}</option>
</select>
</label>
<label v-if="what === 'moment'" class="ls-field ls-grow">
<span class="ls-field-label">Moment</span>
<select v-model="moment" class="ls-input" :disabled="busy">
<option value="" disabled>Choose a moment</option>
<option v-for="m in data.catalog" :key="m.name" :value="m.name">
{{ m.name }} — {{ m.means }}
</option>
</select>
</label>
<label v-else class="ls-field ls-grow">
<span class="ls-field-label">Pattern</span>
<input
v-model="pattern" class="ls-input" type="text" maxlength="200"
:placeholder="PLACEHOLDER[what]" :disabled="busy"
>
</label>
<button type="submit" class="ls-btn" :disabled="busy || !canAdd">Add</button>
</form>
</template>
</section>
</template>
<style scoped>
.ls-panel {
margin-top: 1.75rem;
padding-top: 1rem;
border-top: 1px solid var(--fs-border-color);
}
.ls-label {
margin: 0 0 0.5rem;
font-size: 0.72rem;
font-weight: 500;
text-transform: uppercase;
letter-spacing: 0.04em;
color: var(--fs-text-tertiary);
}
.ls-sub {
margin: var(--fs-space-3) 0 var(--fs-space-1);
font-size: var(--fs-size-label);
font-weight: var(--fs-weight-medium);
color: var(--fs-text-tertiary);
}
.ls-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--fs-space-1); }
.ls-item { display: flex; flex-wrap: wrap; align-items: baseline; gap: var(--fs-space-2); font-size: 0.9rem; }
.ls-value { font-size: var(--fs-size-label); color: var(--fs-text-primary); }
.ls-note { flex: 1 1 14rem; color: var(--fs-text-secondary); font-size: var(--fs-size-label); }
.ls-actions { display: inline-flex; gap: var(--fs-space-2); margin-left: auto; }
.ls-muted { margin: 0; color: var(--fs-text-secondary); font-size: 0.88rem; }
.ls-add {
display: flex; flex-wrap: wrap; align-items: flex-end; gap: var(--fs-space-2);
margin-top: var(--fs-space-3);
}
.ls-field { display: flex; flex-direction: column; gap: var(--fs-space-1); }
.ls-grow { flex: 1 1 14rem; }
.ls-field-label { font-size: var(--fs-size-label); color: var(--fs-text-tertiary); }
.ls-input {
padding: 0.4rem 0.6rem;
border: 1px solid var(--fs-border-color);
border-radius: var(--fs-radius-sm);
background: var(--fs-surface-raised);
color: var(--fs-text-primary);
font: inherit;
font-size: 0.88rem;
}
.ls-btn {
padding: 0.4rem 0.9rem;
border: 1px solid var(--fs-border-color);
border-radius: var(--fs-radius-sm);
background: transparent;
color: var(--fs-text-primary);
font: inherit;
font-size: 0.88rem;
cursor: pointer;
}
.ls-text-btn {
padding: 0;
border: none;
background: none;
color: var(--fs-text-secondary);
font: inherit;
font-size: var(--fs-size-label);
text-decoration: underline;
cursor: pointer;
}
.ls-input:focus-visible,
.ls-btn:focus-visible,
.ls-text-btn:focus-visible { outline: none; box-shadow: var(--fs-focus-ring); }
.ls-btn:disabled,
.ls-text-btn:disabled,
.ls-input:disabled { opacity: var(--fs-disabled-opacity); cursor: default; }
</style>