1 Commits

2 changed files with 41 additions and 55 deletions

26
CHANGELOG.md Normal file
View File

@@ -0,0 +1,26 @@
# Changelog — Plugin Legal AI
## [2026-05-17] שיפורי תשתית: Hooks, Scheduled Jobs, Per-Agent Validation
### שינויים ב-plugin-legal-ai
#### Webhook Hook — עדכוני סטטוס תיק
- נוסף `onWebhook()` handler לאירוע `case-status` מ-legal-ai
- כל שינוי סטטוס תיק (מכל endpoint) → תגובה אוטומטית בעברית על ה-issue המקושר ב-Paperclip
- בשינוי סטטוס `qa_failed` — CEO מתעורר אוטומטית עם הנחיות לתיקון
- נוספה הגנת payload + לוג אזהרה על שדות חסרים
#### Scheduled Jobs — עבודות רקע
- **`stale-case-reminder`** (כל יום 08:00): מזהה תיקים שלא עודכנו 3+ ימים → מוסיף תגובה אזהרה על ה-issue
- **`weekly-feedback-analysis`** (כל ראשון 19:00): מסכם פידבק יו"ר מהשבוע → CEO מעדכן `decision-lessons.md`
- ביצועים: בנייה חד-פעמית של `Map<caseNumber, {issueId, companyId}>` — O(companies × issues) במקום O(cases × companies × issues)
#### manifest / plugin.json
- נוספה capability: `webhooks.receive`
- נוספה הצהרת webhook: `{ endpointKey: "case-status" }`
- נוספו הגדרות ל-2 jobs חדשים
#### תיקוני Quality
- החלפת `let` ב-`const` (biome)
- תיקון סדר imports (biome)
- תיקון פורמט manifest.ts (biome)

View File

@@ -713,15 +713,7 @@ const plugin = definePlugin({
const apiBase =
(config.legalApiBaseUrl as string) ?? "http://localhost:8085";
let resp: Awaited<ReturnType<typeof ctx.http.fetch>>;
try {
resp = await ctx.http.fetch(`${apiBase}/api/cases/stale?days=3`);
} catch (err) {
ctx.logger.error("stale-case-reminder: fetch failed", {
error: String(err),
});
return;
}
const resp = await ctx.http.fetch(`${apiBase}/api/cases/stale?days=3`);
if (!resp.ok) {
ctx.logger.error(`stale-case-reminder: API error ${resp.status}`);
return;
@@ -807,39 +799,26 @@ const plugin = definePlugin({
return;
}
// Pick the first company with a known CEO mapping — decision-lessons.md is
// shared between companies, so a single CEO invocation is sufficient.
const companies = await ctx.companies.list();
const mapped = companies
.map((c) => ({ company: c, ceoId: CEO_AGENT_IDS[c.id] }))
.filter((x): x is { company: (typeof companies)[0]; ceoId: string } =>
Boolean(x.ceoId),
);
const company = companies[0];
if (!company) return;
if (mapped.length === 0) {
const ceoId = CEO_AGENT_IDS[company.id];
if (!ceoId) {
ctx.logger.warn(
"weekly-feedback-analysis: no company has a mapped CEO agent — skipping",
`weekly-feedback-analysis: no CEO agent for company ${company.id}`,
);
return;
}
const { company, ceoId } = mapped[0];
await ctx.agents.invoke(ceoId, company.id, {
prompt: `ניתוח פידבק שבועי יו"ר (${data.entry_count} פריטים):\n\n${data.summary}\n\nהמשימה: עדכן את /home/chaim/legal-ai/docs/legal-decision-lessons.md עם הלקחים החדשים שעולים מהפידבק. הוסף רק לקחים חדשים שלא קיימים כבר. קבץ לפי נושא.`,
reason: "weekly-feedback-analysis scheduled job",
});
try {
await ctx.agents.invoke(ceoId, company.id, {
prompt: `ניתוח פידבק שבועי יו"ר (${data.entry_count} פריטים):\n\n${data.summary}\n\nהמשימה: עדכן את /home/chaim/legal-ai/docs/legal-decision-lessons.md עם הלקחים החדשים שעולים מהפידבק. הוסף רק לקחים חדשים שלא קיימים כבר. קבץ לפי נושא.`,
reason: "weekly-feedback-job",
});
ctx.logger.info(
`weekly-feedback-analysis: invoked CEO ${ceoId} (company ${company.id}) with ${data.entry_count} feedback entries`,
);
} catch (err) {
ctx.logger.error("weekly-feedback-analysis: failed to invoke CEO", {
error: String(err),
ceoId,
companyId: company.id,
});
}
ctx.logger.info(
`weekly-feedback-analysis: invoked CEO ${ceoId} with ${data.entry_count} feedback entries`,
);
});
ctx.logger.info("Legal AI plugin ready");
@@ -852,27 +831,8 @@ const plugin = definePlugin({
async onWebhook(input: PluginWebhookInput): Promise<void> {
if (!pluginCtx) return; // not yet initialized
// Idempotency guard: skip duplicate deliveries within 5 minutes
if (input.requestId) {
const idempKey = `webhook-idem-${input.requestId}`;
const seenAt = await pluginCtx.state.get({
scopeKind: "instance",
stateKey: idempKey,
});
if (seenAt && typeof seenAt === "string") {
const ageMs = Date.now() - new Date(seenAt).getTime();
if (ageMs < 5 * 60 * 1000) {
pluginCtx.logger.info(
`onWebhook: skipping duplicate requestId ${input.requestId} (age ${Math.round(ageMs / 1000)}s)`,
);
return;
}
}
await pluginCtx.state.set(
{ scopeKind: "instance", stateKey: idempKey },
new Date().toISOString(),
);
}
// TODO: add idempotency guard using input.requestId to prevent duplicate
// comments on rapid retries (store requestId in plugin state with ~60s TTL)
const { endpointKey, parsedBody } = input;