czat: graf przepisów i klauzul obok tekstu przepisu (ramię G, domyślnie wyłączone)
Przy przywołanym artykule czat może dołożyć odesłania z grafu Insidera i sąsiednie
artykuły tej samej jednostki aktu, a przy klauzulach umowy artykuły KC według typu
klauzuli i odwołań w jej treści. Włącza je tylko chatTurn({ prawo: true }); domyślny
czat bez zmian (bateria sucho 224/224 identycznie). Pomiar: testy/czat-prawo/PROJEKT.md.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016uwxqFLC3q51VqQzdfk12g
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
02afbae7e7
commit
a1bd4b4517
+97
-5
@@ -1851,6 +1851,7 @@ async function chatLaw(question, index) {
|
||||
const a = r.article && r.article.located ? r.article : null;
|
||||
if (a && docs[0]) {
|
||||
out.push({ slug: 'insider', kind: 'przepis', title: `${c}: ${docs[0].title}`, inner: insiderInner(docs[0], a), url: docs[0].official_url, key: 'insider:' + c,
|
||||
seed: { act: docs[0], num: a.label },
|
||||
text: `${docs[0].title} (${docs[0].signature}), status aktu: ${docs[0].status}. Tekst ${c}: ${chatClean(a.text).slice(0, 6000)}` });
|
||||
continue;
|
||||
}
|
||||
@@ -2195,6 +2196,7 @@ async function chatTopic(question, index, onStatus, model) {
|
||||
const art = await chatArticle(p.act, p.num, p.nth);
|
||||
const a = p.act;
|
||||
out.push({ slug: 'insider', kind: 'przepis', title: `art. ${p.shown} ${a.title.replace(/\s*\(tekst jednolity\)\s*$/, '')}`, inner: insiderInner(a, art && art.url), url: a.official_url, key: p.key,
|
||||
seed: p.whole ? { act: a, num: p.num, nth: p.nth } : null,
|
||||
text: `${a.title} (${a.signature}), status aktu: ${a.status}. ` + (art ? `Tekst art. ${p.shown}: ${art.text}`
|
||||
: p.whole ? `Tekst art. ${p.shown}: ${p.part}` : `Fragment art. ${p.shown} (urywek z wyszukiwarki, nie cały przepis): ${p.part}`) });
|
||||
}
|
||||
@@ -2213,6 +2215,88 @@ async function chatTopic(question, index, onStatus, model) {
|
||||
return { records: out };
|
||||
}
|
||||
|
||||
/* GraphRAG prawa (testy/czat-prawo/PROJEKT.md, ramię G, domyślnie wyłączone): obok przepisu, który czat już
|
||||
pobrał, jego sąsiedztwo w grafie, a przy klauzulach umowy przepisy, do których klauzula prowadzi. Krawędzie:
|
||||
odesłania z grafu Insidera (graph/references, ze zdaniem cytującym), struktura aktu (sąsiednie artykuły tej
|
||||
samej jednostki: Kodeks cywilny odsyła numerami rzadko, 111 krawędzi na 1297 artykułów, więc o obniżeniu kary
|
||||
z art. 483 mówi art. 484 obok, nie odesłanie) i klauzula → artykuł (typ klauzuli z biblioteki Compassu albo
|
||||
odwołanie w jej tekście). Kod łączy, model czyta. */
|
||||
const CHAT_UNIT_HEAD = /\n[\s#]*(?:KSIĘGA|TYTUŁ|DZIAŁ|Rozdział|Oddział)\b/;
|
||||
// A clause type → the Civil Code's general articles on it. Types with no general Civil Code rule get no edge
|
||||
// rather than a guessed one: poufność, RODO, spory, prawa autorskie, and termination (art. 365¹ binds only
|
||||
// open-ended obligations, so on a fixed-term contract it would mislead).
|
||||
const CHAT_CLAUSE_KC = { kary_umowne: ['kary umowne', ['483', '484']], odpowiedzialnosc: ['odpowiedzialność', ['471', '473']],
|
||||
platnosci: ['płatności', ['481']], rekojmia_gwarancja: ['rękojmia i gwarancja', ['556', '558', '577']] };
|
||||
const CHAT_REF_KIND = { ODSYLA_DO: 'odesłanie', STOSUJE_ODPOWIEDNIO: 'stosuje się odpowiednio', Z_ZASTRZEZENIEM: 'z zastrzeżeniem' };
|
||||
const chatCaret = (n) => String(n).replace(/[⁰¹²³⁴⁵⁶⁷⁸⁹]+/, (x) => '^' + [...x].map((c) => '⁰¹²³⁴⁵⁶⁷⁸⁹'.indexOf(c)).join(''));
|
||||
// The last article with that number: a t.j. opens with the promulgating notice's own Art. 1-4.
|
||||
const chatArtAt = (arts, num, nth) => { const same = arts.filter((a) => a.num === num); return nth == null ? same.at(-1) : same.find((a) => a.nth === nth); };
|
||||
const chatArtText = (a, n) => `Tekst art. ${chatSup(a.num)}: ${chatClean(a.text).replace(/\n\s*(?:KSIĘGA|TYTUŁ|DZIAŁ|Rozdział|Oddział)\b[\s\S]*$/, '').replace(/\s+/g, ' ').slice(0, n)}`;
|
||||
async function chatLawAround(seed, index, shown) {
|
||||
const doc = seed.act;
|
||||
const arts = await chatActArticles(doc, index);
|
||||
const num = chatCaret(seed.num);
|
||||
const at = arts.indexOf(chatArtAt(arts, num, seed.nth));
|
||||
if (at < 0) return null;
|
||||
// Two each way inside the unit: a unit heading ends the text of the article before it.
|
||||
const near = [];
|
||||
for (let j = at - 1; j >= Math.max(0, at - 2) && !CHAT_UNIT_HEAD.test(arts[j].text); j--) near.unshift(arts[j]);
|
||||
for (let j = at + 1; j <= Math.min(arts.length - 1, at + 2) && !CHAT_UNIT_HEAD.test(arts[j - 1].text); j++) near.push(arts[j]);
|
||||
const g = await askerGet('insider', `graph/references?act=${encodeURIComponent(chatBare(doc))}&article=${encodeURIComponent(chatSup(num))}&direction=out&depth=1`)
|
||||
.catch((ex) => { if (ex.name === 'AbortError') throw ex; return null; });
|
||||
const refs = ((g && g.out) || []).filter((e) => e.resolved && e.to && e.to.eli === chatBare(doc)).slice(0, 3);
|
||||
const title = doc.title.replace(/\s*\(tekst jednolity\)\s*$/, '');
|
||||
const own = chatSup(num);
|
||||
const refLines = refs.map((e) => {
|
||||
const t = chatArtAt(arts, chatCaret(e.to.num));
|
||||
const text = t && !shown.has(t.num) ? ' ' + chatArtText(t, 1500) : '';
|
||||
if (t) shown.add(t.num);
|
||||
return `Art. ${own} odsyła do art. ${e.to.num} (${CHAT_REF_KIND[e.kind] || e.kind}; zdanie z art. ${own}: „${String(e.context || '').replace(/\s+/g, ' ').trim()}”).` + text;
|
||||
});
|
||||
const rest = near.filter((a) => !shown.has(a.num));
|
||||
rest.forEach((a) => shown.add(a.num));
|
||||
return { slug: 'insider', kind: 'przepis', key: `insider:graf:${doc.id}#${num}`, title: `Przepisy powiązane z art. ${own} ${title} (graf przepisów)`,
|
||||
inner: insiderInner(doc), url: doc.official_url,
|
||||
text: `Przepisy powiązane z art. ${own} ${title} według grafu przepisów w Insiderze (tekst samego art. ${own} jest w osobnym źródle). ` +
|
||||
(refLines.length ? refLines.join(' ') : `Art. ${own} nie odsyła numerem do innych przepisów.`) +
|
||||
(rest.length ? ` Sąsiednie przepisy z tej samej części aktu: ${rest.map((a) => chatArtText(a, 1500)).join(' ')}` : '') };
|
||||
}
|
||||
async function chatClauseLaw(rec, index, shown) {
|
||||
const lines = [];
|
||||
const want = [];
|
||||
for (const k of rec.picked || []) {
|
||||
const typed = (k.types || []).filter((t) => CHAT_CLAUSE_KC[t]);
|
||||
// Only the Civil Code's articles: another act's citation is chatReview's (is it current), not this graph's.
|
||||
const cited = chatCites(k.text).filter((c) => /\bk\.?\s?c\b|cywiln/i.test(c)).map((c) => chatCaret((c.match(/\d+[⁰¹²³⁴⁵⁶⁷⁸⁹]*/) || [''])[0])).filter(Boolean);
|
||||
const nums = [...new Set([...typed.flatMap((t) => CHAT_CLAUSE_KC[t][1]), ...cited])];
|
||||
if (!nums.length) continue;
|
||||
want.push(...nums);
|
||||
lines.push(`${k.label}${typed.length ? ` (${typed.map((t) => CHAT_CLAUSE_KC[t][0]).join(', ')})` : ''} → ${nums.map((n) => 'art. ' + chatSup(n)).join(', ')}` +
|
||||
(cited.length ? ` (klauzula sama powołuje ${cited.map((n) => 'art. ' + chatSup(n)).join(', ')})` : ''));
|
||||
}
|
||||
if (!lines.length) return null;
|
||||
const doc = await chatActDoc('Kodeks cywilny');
|
||||
if (!doc) return null;
|
||||
const arts = await chatActArticles(doc, index);
|
||||
const texts = [...new Set(want)].filter((n) => !shown.has(n)).slice(0, 5).map((n) => chatArtAt(arts, n)).filter(Boolean);
|
||||
texts.forEach((a) => shown.add(a.num));
|
||||
return { slug: 'insider', kind: 'przepis', key: 'insider:graf:' + rec.key, title: rec.title.replace(/^Klauzule/, 'Przepisy Kodeksu cywilnego do klauzul'),
|
||||
inner: insiderInner(doc), url: doc.official_url, members: rec.members,
|
||||
text: `Połączenia klauzul (${rec.title}) z Kodeksem cywilnym według typu klauzuli w bibliotece Compassu albo odwołania w jej treści; to przepisy ogólne, umowa może się od nich różnić w granicach ustawy: ` +
|
||||
lines.join('; ') + '. ' + texts.map((a) => chatArtText(a, 1200)).join(' ') };
|
||||
}
|
||||
async function chatLawGraph({ law, topic, clauses }, index) {
|
||||
if (!index.law) return { near: [], clauses: [] };
|
||||
const seeds = [...law.filter((r) => r.seed).slice(0, 2), ...topic.filter((r) => r.seed).slice(0, 1)];
|
||||
// Articles the model already reads in full are not repeated.
|
||||
const shown = new Set(seeds.map((r) => chatCaret(r.seed.num)));
|
||||
const near = [];
|
||||
for (const r of seeds) near.push(await chatLawAround(r.seed, index, shown));
|
||||
const linked = [];
|
||||
for (const c of clauses) linked.push(await chatClauseLaw(c, index, shown));
|
||||
return { near: near.filter(Boolean), clauses: linked.filter(Boolean) };
|
||||
}
|
||||
|
||||
async function chatIndex({ rejestr = chat.rejestr ?? false } = {}) {
|
||||
const today = new Date().toLocaleDateString('sv');
|
||||
const names = Object.fromEntries(((await askerGet('sygna', 'users')) || []).map((u) => [u.id, u.full_name]));
|
||||
@@ -3819,7 +3903,7 @@ async function chatClauses(rec, risk, per, topics = []) {
|
||||
const today = new Date().toLocaleDateString('sv');
|
||||
const lines = [...new Set(picked)].slice(0, per).map((k) => `${k.label}: ${String(k.text).replace(/\s+/g, ' ').replace(/^\d+\.\s*|^§\s*\d+\s*/, '').trim().slice(0, risk ? 260 : 320)}` +
|
||||
chatNoticeBy(k.text, c.ends_on, today));
|
||||
return { ...base, text: lines.length ? `${head}, wybrane do pytania:\n` + lines.join('\n')
|
||||
return { ...base, picked: [...new Set(picked)].slice(0, per), text: lines.length ? `${head}, wybrane do pytania:\n` + lines.join('\n')
|
||||
: `${head}: w treści umowy nie znalazłem postanowień ${topics.length ? 'na temat z pytania' : 'o okresie obowiązywania, przedłużeniu, wypowiedzeniu ani karach umownych'}.` };
|
||||
}
|
||||
// A recommendation question gets told the shape of a useful answer; a lookup question keeps its plain one.
|
||||
@@ -3834,7 +3918,9 @@ async function chatTurn({ model, question, turns, index, answer = { role: 'assis
|
||||
// Default 'osoba' after round 2 (testy/czat-graf/RUNDA2.md): the team member's card helped both Qwen and Bielik;
|
||||
// the full map (true) cost Qwen its "nie ma" answers twice. false switches the map off entirely.
|
||||
// ja: the asker's own card for "co mam na głowie" (testy/czat-graf/RUNDA4.md: passed on Qwen and Bielik); false switches it off.
|
||||
graf = chat.graf ?? 'osoba', ja = chat.ja ?? true }) {
|
||||
graf = chat.graf ?? 'osoba', ja = chat.ja ?? true,
|
||||
// prawo: the law graph beside the cited article and the contract's clauses (testy/czat-prawo/PROJEKT.md), off until measured.
|
||||
prawo = chat.prawo ?? false }) {
|
||||
const done = (content, extra = {}) => Object.assign(answer, { content, status: '', ...extra });
|
||||
try {
|
||||
// "Umowa o dzieło" in a question about the law is not a question about Compass's contracts.
|
||||
@@ -3947,6 +4033,12 @@ async function chatTurn({ model, question, turns, index, answer = { role: 'assis
|
||||
// One contract: its whole text for a risk question, the clauses on the asked subject or its end clauses.
|
||||
const clauses = route === 'data' && cands.length && (risk || topics.length || CHAT_ACT.test(question))
|
||||
? await Promise.all(top.map((r) => chatClauses(r, risk && top.length === 1, top.length > 2 ? 2 : top.length > 1 ? 8 : risk ? 30 : 10, topics))) : [];
|
||||
// A fault in the law graph leaves the turn as without it.
|
||||
let lawGraph = { near: [], clauses: [] };
|
||||
if (prawo) {
|
||||
try { lawGraph = await chatLawGraph({ law, topic, clauses }, index); } catch (ex) { if (ex.name === 'AbortError') throw ex; console.warn('law graph off:', ex); }
|
||||
answer.prawo = [...lawGraph.near, ...lawGraph.clauses].map((r) => r.key);
|
||||
}
|
||||
// A question about the law gets no firm records: a dozen contracts beside three articles are noise.
|
||||
let found = topic.length ? [] : chatSearch(index.records, question + (ana.demonstrative && prevQ ? ' ' + prevQ : ''));
|
||||
// "z nich": the rest of the slots stay with that kind and the totals, not with whatever shares a word.
|
||||
@@ -3964,11 +4056,11 @@ async function chatTurn({ model, question, turns, index, answer = { role: 'assis
|
||||
const pick = review.length || law.length || topic.length || named.length || resolved.length || back.length || card ? null : chatPick(question, index);
|
||||
if (pick && !pick.table.rows.length) return done(pick.empty, { coded: 'pick', refs: [], verify: [] });
|
||||
// The entity's card right after law and topic; a one-record link card behind its record, so [1] stays the record.
|
||||
const sources = [...(pick ? [pick.rec] : []), ...review, ...law, ...topic,
|
||||
const sources = [...(pick ? [pick.rec] : []), ...review, ...law, ...topic, ...lawGraph.near,
|
||||
...(card && card.rec.kind === 'karta' ? [card.rec] : []), ...named, ...resolved,
|
||||
...(card && card.rec.kind !== 'karta' ? [card.rec] : []), ...backSum, ...back, ...clauses, ...chatCarry(prev, index),
|
||||
...(card && card.rec.kind !== 'karta' ? [card.rec] : []), ...backSum, ...back, ...clauses, ...lawGraph.clauses, ...chatCarry(prev, index),
|
||||
...(card ? card.lead : []), ...found]
|
||||
.filter((r) => !seen.has(r.key) && seen.add(r.key)).slice(0, CHAT_TOP + law.length + review.length + topic.length + clauses.length);
|
||||
.filter((r) => !seen.has(r.key) && seen.add(r.key)).slice(0, CHAT_TOP + law.length + review.length + topic.length + clauses.length + lawGraph.near.length + lawGraph.clauses.length);
|
||||
const name = (slug) => (chatModules().find((m) => m.slug === slug) || { name: slug }).name;
|
||||
const toRef = (r, i) => ({ n: i + 1, key: r.key, kind: r.kind, slug: r.slug, module: name(r.slug), title: r.title, text: r.text,
|
||||
data: !['przepis', 'akt', 'orzeczenie'].includes(r.kind), inner: r.inner, url: r.url });
|
||||
|
||||
Reference in New Issue
Block a user