From 60c896acd1e8acb6949565ac868c2b866eccf2fc Mon Sep 17 00:00:00 2001 From: femsci Date: Sat, 18 Nov 2023 04:50:31 +0100 Subject: [PATCH] current state of !stuff! --- .../Models/DocSerializable.cs | 6 +++ src/Nyanlabs.Umogen.Core/Models/IEntity.cs | 2 +- .../Models/LegalEntity.cs | 7 ++- .../Models/NLSerializable.cs | 2 +- src/Nyanlabs.Umogen.Core/Models/Person.cs | 7 ++- .../Models/UmoDocument.cs | 17 ++++++- .../Models/UmoDocumentResult.cs | 49 +++++++++++++++++++ src/Nyanlabs.Umogen.Core/Models/ValidTime.cs | 14 +++++- src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs | 6 +++ src/Nyanlabs.Umogen.Core/SseConsumer.cs | 4 -- src/Nyanlabs.Umogen.Core/Umogen.cs | 2 +- .../Shared/MainLayout.razor | 3 ++ .../Shared/MainLayout.razor.css | 19 ++++++- src/Nyanlabs.Umogen.Server/Shared/Navi.razor | 3 ++ .../Shared/Navi.razor.css | 3 ++ .../wwwroot/css/site.css | 19 +++++++ .../NLSerializationTests.cs | 14 +++--- .../GenerationTests.cs | 8 +-- 18 files changed, 156 insertions(+), 29 deletions(-) create mode 100644 src/Nyanlabs.Umogen.Core/Models/DocSerializable.cs create mode 100644 src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs create mode 100644 src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs create mode 100644 src/Nyanlabs.Umogen.Server/Shared/Navi.razor create mode 100644 src/Nyanlabs.Umogen.Server/Shared/Navi.razor.css diff --git a/src/Nyanlabs.Umogen.Core/Models/DocSerializable.cs b/src/Nyanlabs.Umogen.Core/Models/DocSerializable.cs new file mode 100644 index 0000000..0cf8b43 --- /dev/null +++ b/src/Nyanlabs.Umogen.Core/Models/DocSerializable.cs @@ -0,0 +1,6 @@ +namespace Nyanlabs.Umogen.Core.Models; + +public interface IDocSerializable +{ + public string SerializeToDocument(); +} diff --git a/src/Nyanlabs.Umogen.Core/Models/IEntity.cs b/src/Nyanlabs.Umogen.Core/Models/IEntity.cs index 0c11c94..c38bdcf 100644 --- a/src/Nyanlabs.Umogen.Core/Models/IEntity.cs +++ b/src/Nyanlabs.Umogen.Core/Models/IEntity.cs @@ -1,6 +1,6 @@ namespace Nyanlabs.Umogen.Core.Models; -public interface IEntity : INLSerializable +public interface IEntity : INLSerializable, IDocSerializable { public string GetName(); public string? IdCode(); diff --git a/src/Nyanlabs.Umogen.Core/Models/LegalEntity.cs b/src/Nyanlabs.Umogen.Core/Models/LegalEntity.cs index 027c4ea..10180f2 100644 --- a/src/Nyanlabs.Umogen.Core/Models/LegalEntity.cs +++ b/src/Nyanlabs.Umogen.Core/Models/LegalEntity.cs @@ -17,8 +17,13 @@ public class LegalEntity(string Name, string? Nip = null, string? Headquarters = return Nip; } - public string NLSerialize() + public string NLQuerySerialize() { return $"{Name}; with NIP {Nip ?? "unknown"}{(Headquarters != null ? $"; located in {Headquarters}" : "")}{(Representative != null ? $"; represented by {Representative.GetName()}" : "")}"; } + + public string SerializeToDocument() + { + return $"{Name}, reprezentowaną przez {Representative.GetName()}"; + } } diff --git a/src/Nyanlabs.Umogen.Core/Models/NLSerializable.cs b/src/Nyanlabs.Umogen.Core/Models/NLSerializable.cs index 92adccc..6967c40 100644 --- a/src/Nyanlabs.Umogen.Core/Models/NLSerializable.cs +++ b/src/Nyanlabs.Umogen.Core/Models/NLSerializable.cs @@ -2,5 +2,5 @@ namespace Nyanlabs.Umogen.Core.Models; public interface INLSerializable { - public string NLSerialize(); + public string NLQuerySerialize(); } diff --git a/src/Nyanlabs.Umogen.Core/Models/Person.cs b/src/Nyanlabs.Umogen.Core/Models/Person.cs index 343d09d..4804b1f 100644 --- a/src/Nyanlabs.Umogen.Core/Models/Person.cs +++ b/src/Nyanlabs.Umogen.Core/Models/Person.cs @@ -18,8 +18,13 @@ public record Person(string Name, string Surname, string? Id = null, string? Pes return IDCode!; } - public string NLSerialize() + public string NLQuerySerialize() { return $"{Name} {Surname}; {(Pesel != null ? $"PESEL {Pesel}" : "unknown PESEL")}; {(IDCode != null ? $"ID series {IDCode}" : "unknown ID series")}; {(DoB != null ? $"born {DoB.Value:dd MMM yyyy}" : "unknown date of birth")}"; } + + public string SerializeToDocument() + { + return $"{Name} {Surname}{(Pesel != null ? $" PESEL {Pesel}" : "")}{(IDCode != null ? $" legitymującą się dowodem {IDCode}" : "")}{(DoB != null ? $" ur. {DoB.Value:dd MMM yyyy}" : "")}"; + } } diff --git a/src/Nyanlabs.Umogen.Core/Models/UmoDocument.cs b/src/Nyanlabs.Umogen.Core/Models/UmoDocument.cs index 0ad4733..8ff2884 100644 --- a/src/Nyanlabs.Umogen.Core/Models/UmoDocument.cs +++ b/src/Nyanlabs.Umogen.Core/Models/UmoDocument.cs @@ -1,3 +1,5 @@ +using System.Data; + namespace Nyanlabs.Umogen.Core.Models; public enum UmoDoctype @@ -16,6 +18,17 @@ public static class UmoDoctypeBindings { UmoDoctype.CONTR_ASS, ("Contract for Assignment", "Umowa o Dzieło") } }; + public static string TemplateDoc(this UmoDoctype t) + { + return t switch + { + UmoDoctype.CONTR_EMPLOYMENT => "uop.fodt", + UmoDoctype.CONTR_SERVICE => "uz.fodt", + UmoDoctype.CONTR_ASS => "ud.fodt", + _ => throw new DataException("invalid"), + }; + } + public static (string, string) Name(this UmoDoctype t) { return s_umoass[t]; @@ -43,8 +56,8 @@ public class UmoDocument : INLSerializable public Person Employee { get; set; } //Null is 'unspecified time' public ValidTime? ValidTime { get; set; } - public string NLSerialize() + public string NLQuerySerialize() { - return $"{Doctype.Name().Item2} for {Employee.NLSerialize()} working for {Employer.NLSerialize()}; valid {ValidTime?.NLSerialize() ?? "for unspecified time"}."; + return $"{Doctype.Name().Item2} for {Employee.NLQuerySerialize()} working for {Employer.NLQuerySerialize()}; valid {ValidTime?.NLQuerySerialize() ?? "for unspecified time"}."; } } diff --git a/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs b/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs new file mode 100644 index 0000000..0cab7e1 --- /dev/null +++ b/src/Nyanlabs.Umogen.Core/Models/UmoDocumentResult.cs @@ -0,0 +1,49 @@ +using System.Data; +using System.Diagnostics; + +namespace Nyanlabs.Umogen.Core.Models; + +public class UmoDocumentResult(UmoDoctype type, Person employee, IEntity employer, ValidTime? validity, string? content) +{ + public UmoDoctype DocType { get; } = type; + public Person Employee { get; } = employee; + public IEntity Employer { get; } = employer; + public ValidTime? Validity { get; } = validity; + public string? Content { get; } = content; + + const string ISSUE_DATE = "$DATETIME_ISSUE", + ENACT_DATE = "$DATETIME", + EMPLOYER = "$EMPLOYER", + EMPLOYEE = "$EMPLOYEE", + VALID_TIME = "$VALIDITY_TIME", + WORK_TYPE = "$WORK_TYPE", + DYNAMIC_DATA = "$WORK_DYNAMIC_DATA", + START_DATE = "$WORK_START_DATE"; + + public async Task ProcessPdf() + { + string xml = await File.ReadAllTextAsync(DocType.TemplateDoc()); + xml = xml.Replace(EMPLOYER, Employer.SerializeToDocument()) + .Replace(EMPLOYEE, Employee.SerializeToDocument()) + .Replace(VALID_TIME, (Validity ?? ValidTime.Invalid).SerializeToDocument()) + .Replace(ENACT_DATE, DateTime.Now.ToString("dd.MM.yyyy")) + .Replace(ISSUE_DATE, DateTime.Now.ToString("dd.MM.yyyy")) + .Replace(DYNAMIC_DATA, Content); + string tmp = Path.GetTempFileName(); + await File.WriteAllTextAsync(tmp, xml); + + await Task.Run(async () => + { + var proc = Process.Start("soffice", $"--headless --convert-to pdf {tmp}"); + await proc.WaitForExitAsync(); + if (proc.ExitCode != 0) + { + throw new DataException("invaliddddd"); + } + }); + + byte[] data = await File.ReadAllBytesAsync(Path.ChangeExtension(tmp, "pdf")); + + return data; + } +} diff --git a/src/Nyanlabs.Umogen.Core/Models/ValidTime.cs b/src/Nyanlabs.Umogen.Core/Models/ValidTime.cs index b43974b..044b961 100644 --- a/src/Nyanlabs.Umogen.Core/Models/ValidTime.cs +++ b/src/Nyanlabs.Umogen.Core/Models/ValidTime.cs @@ -1,6 +1,6 @@ namespace Nyanlabs.Umogen.Core.Models; -public readonly struct ValidTime : INLSerializable +public readonly struct ValidTime : INLSerializable, IDocSerializable { public ValidTime(DateTime start, DateTime end) { @@ -14,9 +14,19 @@ public readonly struct ValidTime : INLSerializable End = start + duration; } + public static readonly ValidTime Invalid = new(DateTime.MaxValue, DateTime.MinValue); + public readonly DateTime Start { get; } public readonly DateTime End { get; } public readonly TimeSpan Duration => End - Start; - public string NLSerialize() => $"from {Start:dd MMM yyyy} until {End:dd MMM yyyy}"; + public string NLQuerySerialize() + { + return this.Start != DateTime.MaxValue ? $"from {Start:dd MMM yyyy} until {End:dd MMM yyyy}" : "for unspecified time"; + } + + public string SerializeToDocument() + { + return this.Start != DateTime.MaxValue ? $"na czas od {Start:dd MMM yyyy} do {End:dd MMM yyyy}" : "na czas nieokreślony"; + } } diff --git a/src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs b/src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs new file mode 100644 index 0000000..d4220ed --- /dev/null +++ b/src/Nyanlabs.Umogen.Core/Pdf/PdfRenderer.cs @@ -0,0 +1,6 @@ +namespace Nyanlabs.Umogen.Core.Pdf; + +public class PdfRenderer +{ + +} diff --git a/src/Nyanlabs.Umogen.Core/SseConsumer.cs b/src/Nyanlabs.Umogen.Core/SseConsumer.cs index 80c7558..a07ba05 100644 --- a/src/Nyanlabs.Umogen.Core/SseConsumer.cs +++ b/src/Nyanlabs.Umogen.Core/SseConsumer.cs @@ -1,9 +1,5 @@ -using System; -using System.Collections.Generic; using System.Data; -using System.Linq; using System.Text.Json; -using System.Threading.Tasks; namespace Nyanlabs.Umogen.Core; diff --git a/src/Nyanlabs.Umogen.Core/Umogen.cs b/src/Nyanlabs.Umogen.Core/Umogen.cs index 6331e4f..b2ec03f 100644 --- a/src/Nyanlabs.Umogen.Core/Umogen.cs +++ b/src/Nyanlabs.Umogen.Core/Umogen.cs @@ -5,7 +5,7 @@ namespace Nyanlabs.Umogen.Core; public class Umogen { public const string DEFAULT_API_KEY_FILE = "umogenkey.secret"; - public const string PROMPT = "You write Polish legal contracts in Polish language in markdown format. Fill unknown fields with signature placeholder (underscore) with enough space to sign it with pen."; + public const string PROMPT = "You analyze employment prompt requests and return document type (one of these 'Umowa o Pracę', 'Umowa Zlecenie', 'Umowa o Dzieło') and elaborate description of job details as a list separated by hyphens and newlines. You write them in format: '\n'"; public static readonly JsonSerializerOptions JSON_OPTS = new(JsonSerializerDefaults.Web) { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower diff --git a/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor b/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor index af0239c..0b0e4ab 100644 --- a/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor +++ b/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor @@ -2,6 +2,9 @@ umogen +
diff --git a/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor.css b/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor.css index f539b54..627a238 100644 --- a/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor.css +++ b/src/Nyanlabs.Umogen.Server/Shared/MainLayout.razor.css @@ -8,8 +8,23 @@ main { flex: 1; } -.sidebar { - background-image: linear-gradient(180deg, rgb(5, 39, 103) 0%, #3a0647 70%); +.nav-side { + color: #fefefe; + display: flex; + position: sticky; + top: 0; + left: 0; + margin: 0; + height: 100%; + width: 12rem; + background-image: linear-gradient( + 180deg, + rgb(10, 140, 173) 0%, + #198342 25%, + #fe00fe 50%, + #198342 75%, + rgb(10, 140, 173) 100% + ); } .top-row { diff --git a/src/Nyanlabs.Umogen.Server/Shared/Navi.razor b/src/Nyanlabs.Umogen.Server/Shared/Navi.razor new file mode 100644 index 0000000..ba94b6d --- /dev/null +++ b/src/Nyanlabs.Umogen.Server/Shared/Navi.razor @@ -0,0 +1,3 @@ +
+

umogen

+
diff --git a/src/Nyanlabs.Umogen.Server/Shared/Navi.razor.css b/src/Nyanlabs.Umogen.Server/Shared/Navi.razor.css new file mode 100644 index 0000000..9b01f19 --- /dev/null +++ b/src/Nyanlabs.Umogen.Server/Shared/Navi.razor.css @@ -0,0 +1,3 @@ +h2 { + text-align: center; +} diff --git a/src/Nyanlabs.Umogen.Server/wwwroot/css/site.css b/src/Nyanlabs.Umogen.Server/wwwroot/css/site.css index 78abcbd..5f9929a 100644 --- a/src/Nyanlabs.Umogen.Server/wwwroot/css/site.css +++ b/src/Nyanlabs.Umogen.Server/wwwroot/css/site.css @@ -1,6 +1,19 @@ html, body { font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; + position: relative; + width: 100%; + height: 100%; + margin: 0; +} + +footer, +header, +hgroup, +menu, +nav, +section { + display: block; } h1:focus { @@ -30,6 +43,12 @@ a, padding-top: 1.1rem; } +.container { + position: relative; + width: 100%; + height: 100%; +} + .valid.modified:not([type="checkbox"]) { outline: 1px solid #26b050; } diff --git a/test/Nyanlabs.Umogen.CoreTests/NLSerializationTests.cs b/test/Nyanlabs.Umogen.CoreTests/NLSerializationTests.cs index 46d23f8..43da2cd 100644 --- a/test/Nyanlabs.Umogen.CoreTests/NLSerializationTests.cs +++ b/test/Nyanlabs.Umogen.CoreTests/NLSerializationTests.cs @@ -15,10 +15,10 @@ public class NLSerializationTests UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee); // When - var query = doc.NLSerialize(); + var query = doc.NLQuerySerialize(); // Then - Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLSerialize()} working for {ModelEmployer.NLSerialize()}; valid for unspecified time.", query); + Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLQuerySerialize()} working for {ModelEmployer.NLQuerySerialize()}; valid for unspecified time.", query); } [Fact] @@ -28,10 +28,10 @@ public class NLSerializationTests UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee, new(new DateTime(2023, 11, 17), TimeSpan.FromDays(1))); // When - var query = doc.NLSerialize(); + var query = doc.NLQuerySerialize(); // Then - Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLSerialize()} working for {ModelEmployer.NLSerialize()}; valid from 17 Nov 2023 until 18 Nov 2023.", query); + Assert.Equal($"Umowa o Pracę for {ModelEmployee.NLQuerySerialize()} working for {ModelEmployer.NLQuerySerialize()}; valid from 17 Nov 2023 until 18 Nov 2023.", query); } [Fact] @@ -41,7 +41,7 @@ public class NLSerializationTests Person p = ModelEmployer; // When - var query = p.NLSerialize(); + var query = p.NLQuerySerialize(); // Then Assert.Equal("Kotek Miauczyński; PESEL 04281308999; ID series CBS4327563; born 13 Aug 2004", query); @@ -59,7 +59,7 @@ public class NLSerializationTests }; // When - var query = p.NLSerialize(); + var query = p.NLQuerySerialize(); // Then Assert.Equal("Kotek Miauczyński; unknown PESEL; unknown ID series; unknown date of birth", query); @@ -74,7 +74,7 @@ public class NLSerializationTests LegalEntity company = ModelCompany; // When - var query = company.NLSerialize(); + var query = company.NLQuerySerialize(); // Then Assert.Equal("Nyanbyte P.S.A.; with NIP 1313131313; located in Miaumiaśna 13, Miauczki; represented by Kotek Miauczyński", query); diff --git a/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs b/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs index 72fe9d8..3448585 100644 --- a/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs +++ b/test/Nyanlabs.Umogen.WebTests/GenerationTests.cs @@ -1,8 +1,3 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; - using Nyanlabs.Umogen.Core; using Nyanlabs.Umogen.Core.Models; @@ -26,12 +21,11 @@ public class GenerationTests UmoDocument doc = new(UmoDoctype.CONTR_EMPLOYMENT, ModelEmployer, ModelEmployee); UmoProcess proc = new(eng); - var enu = proc.Ask(doc.NLSerialize()); + var enu = proc.Ask("I would like to employ Jan Kowalski ID NO CBS3727348 for part time job as a shopkeeper."); await foreach (var str in enu) { Console.Write(str); - File.AppendAllText("/tmp/doc.md", str); } Console.WriteLine("~");