Stammdaten: Leistungen mit Ordnern und Vorkalkulation, GAEB- und DATANORM-Import, Material mit Rabatten, Angebot mit Reitern Leistungen/Material und KI-Vorschlag (0.18.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01AmyPfuNc4sCRfaF3oURXZz
This commit is contained in:
@@ -18,6 +18,7 @@ import m013 from "../src/server/migrations/013_kolonnen_subunternehmer.sql";
|
||||
import m014 from "../src/server/migrations/014_angebote.sql";
|
||||
import m015 from "../src/server/migrations/015_mengenblatt.sql";
|
||||
import m016 from "../src/server/migrations/016_fahrt.sql";
|
||||
import m017 from "../src/server/migrations/017_stammdaten.sql";
|
||||
|
||||
const MIGRATIONEN: [string, string][] = [
|
||||
["001_grundlage.sql", m001], ["002_v70_uebernahme.sql", m002], ["003_vorgangsnummer.sql", m003], ["004_besetzung.sql", m004],
|
||||
@@ -31,6 +32,7 @@ const MIGRATIONEN: [string, string][] = [
|
||||
["014_angebote.sql", m014],
|
||||
["015_mengenblatt.sql", m015],
|
||||
["016_fahrt.sql", m016],
|
||||
["017_stammdaten.sql", m017],
|
||||
];
|
||||
|
||||
export async function starte(db: PGlite, v70?: unknown) {
|
||||
|
||||
35
demo/main.ts
35
demo/main.ts
@@ -4,11 +4,14 @@ import { PGlite } from "@electric-sql/pglite";
|
||||
import { starte } from "./kern.js";
|
||||
import { PARSER } from "./pool.js";
|
||||
import { NUR_SICHERUNG, TABELLEN } from "../src/server/tabellen.js";
|
||||
import { kiAnfrage, kiVorschlaege } from "../src/domain/ki.js";
|
||||
|
||||
type Doc = { id: string; exists: boolean; data(): Record<string, unknown> | undefined };
|
||||
type Query = { where(f: string, op: string, v: unknown): Query; orderBy(f: string): Query; limit(n: number): Query; get(): Promise<{ docs: Doc[] }> };
|
||||
type Speicher = { collection(p: string): Query; doc(p: string): { get(): Promise<Doc>; set(d: Record<string, unknown>): Promise<void>; delete(): Promise<void> } };
|
||||
type Claude = { use(name: string): Promise<any> };
|
||||
// Tabelle, deren Zeilen gebündelt gespeichert werden (Speicher: ein Dokument je Zeile wäre bei 20 000 Artikeln zu viel)
|
||||
const PAKETE = "artikel";
|
||||
const claude = (globalThis as { claude?: Claude }).claude;
|
||||
|
||||
const leiste = document.getElementById("test-leiste")!;
|
||||
@@ -56,13 +59,17 @@ async function nochmal<T>(fn: () => Promise<T>): Promise<T> {
|
||||
async function main() {
|
||||
melde("Programm wird geladen …");
|
||||
const [db, speicher, laden] = await Promise.all([pglite(), claude?.use("db") as Promise<Speicher | null> ?? null, claude?.use("downloads") ?? null]);
|
||||
// KI-Vorschlag im Angebot: fragt Claude über das Konto dessen, der die Testversion offen hat (sample-Fähigkeit)
|
||||
const sample = claude?.use("sample").catch(() => null) ?? Promise.resolve(null);
|
||||
const app = await starte(db);
|
||||
|
||||
// gespeicherten Stand holen und einspielen
|
||||
if (speicher) {
|
||||
melde("Deine Daten werden geladen …");
|
||||
const daten: Record<string, Record<string, unknown>[]> = {};
|
||||
for (const pfad of [...TABELLEN, ...NUR_SICHERUNG].map((t) => t.pfad).concat("benutzer", "ansichten")) daten[pfad] = await nochmal(() => alle(speicher, pfad));
|
||||
for (const pfad of [...TABELLEN, ...NUR_SICHERUNG].map((t) => t.pfad).concat("benutzer", "ansichten")) {
|
||||
daten[pfad] = pfad === PAKETE ? (await nochmal(() => alle(speicher, `${PAKETE}-paket`))).flatMap((p) => p.zeilen as Record<string, unknown>[]) : await nochmal(() => alle(speicher, pfad));
|
||||
}
|
||||
const e = await nochmal(() => speicher.doc("meta/einstellungen").get());
|
||||
if (Object.values(daten).some((z) => z.length) || e.exists) {
|
||||
const r = await app.verarbeite("POST", "/api/import/stand", { programm: "betrieb", einstellungen: e.data()?.daten, daten });
|
||||
@@ -75,7 +82,13 @@ async function main() {
|
||||
const stand = new Map<string, string>();
|
||||
const spiegel = (x: Awaited<ReturnType<typeof exportiere>>) => {
|
||||
const m = new Map<string, string>();
|
||||
for (const [pfad, zeilen] of Object.entries(x.daten)) for (const z of zeilen) m.set(`${pfad}/${z.id}`, JSON.stringify(z));
|
||||
for (const [pfad, zeilen] of Object.entries(x.daten)) {
|
||||
if (pfad !== PAKETE) { for (const z of zeilen) m.set(`${pfad}/${z.id}`, JSON.stringify(z)); continue; }
|
||||
// Artikel kommen zu Tausenden (DATANORM): je Paket ein Dokument, nach den ersten zwei Zeichen der id
|
||||
const pakete = new Map<string, { id: string }[]>();
|
||||
for (const z of zeilen) { const k = z.id.slice(0, 2); if (!pakete.has(k)) pakete.set(k, []); pakete.get(k)!.push(z); }
|
||||
for (const [k, liste] of pakete) m.set(`${PAKETE}-paket/${k}`, JSON.stringify({ id: k, zeilen: liste }));
|
||||
}
|
||||
m.set("meta/einstellungen", JSON.stringify({ daten: x.einstellungen }));
|
||||
return m;
|
||||
};
|
||||
@@ -109,12 +122,30 @@ async function main() {
|
||||
if (!adresse.startsWith("/api/")) return original(eingabe, init);
|
||||
const methode = (init?.method ?? "GET").toUpperCase();
|
||||
const body = typeof init?.body === "string" ? JSON.parse(init.body) : undefined;
|
||||
if (adresse === "/api/ki" && methode === "POST") return ki(body);
|
||||
// die Oberfläche schickt mit, als wer Henning gerade arbeitet (x-test-benutzer)
|
||||
const antwort = await app.verarbeite(methode, adresse, body, Object.fromEntries(new Headers(init?.headers).entries()));
|
||||
if (methode !== "GET" && antwort.ok) await speichern();
|
||||
return antwort;
|
||||
};
|
||||
|
||||
const ki = async (body: { text?: string }): Promise<Response> => {
|
||||
const antwort = (status: number, x: unknown) => new Response(JSON.stringify(x), { status, headers: { "content-type": "application/json" } });
|
||||
const s = await sample;
|
||||
if (!s) return antwort(409, { fehler: "Claude ist in dieser Ansicht nicht erreichbar. Die Testversion im Claude-Fenster öffnen." });
|
||||
const text = String(body?.text ?? "").trim();
|
||||
if (text.length < 5) return antwort(400, { fehler: "Bitte kurz beschreiben, was gemacht werden soll." });
|
||||
const leistungen = (await (await app.verarbeite("GET", "/api/leistungen")).json()) as { id: string; kurztext: string; einheit: string | null; gruppe: string; nummer: string | null }[];
|
||||
if (!leistungen.length) return antwort(409, { fehler: "Es gibt noch keine Leistungen in den Stammdaten." });
|
||||
try {
|
||||
const r = await s(kiAnfrage(text, leistungen.slice(0, 800).map((l) => ({ id: l.id, kurztext: l.kurztext, einheit: l.einheit, ordner: l.gruppe, nummer: l.nummer }))), { modelTier: "default" });
|
||||
return antwort(200, { vorschlaege: kiVorschlaege(r.text, new Set(leistungen.map((l) => l.id))) });
|
||||
} catch (e) {
|
||||
const code = (e as { code?: string }).code;
|
||||
return antwort(409, { fehler: code === "not_granted" ? "Claude wurde nicht erlaubt. Beim nächsten Versuch die Anfrage zulassen." : code === "rate_limited" ? "Gerade zu viele Anfragen. Bitte gleich noch einmal." : `Claude hat nicht geantwortet: ${(e as Error).message ?? code}` });
|
||||
}
|
||||
};
|
||||
|
||||
const datei = async (filename: string, data: string) => {
|
||||
if (!laden) throw new Error("Speichern als Datei geht in dieser Ansicht nicht.");
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user