Merge feature/custom-formula-templates into dev
This commit is contained in:
@@ -374,6 +374,10 @@ public class ApplicationDbContext : IdentityDbContext<ApplicationUser>, IDataPro
|
||||
/// <summary>Customer self-service intake sessions (walk-in tablet or remote email link); tenant-filtered with soft delete.</summary>
|
||||
public DbSet<KioskSession> KioskSessions { get; set; }
|
||||
|
||||
// Custom Formula Templates
|
||||
/// <summary>Per-company reusable NCalc pricing formula templates; tenant-filtered with soft delete.</summary>
|
||||
public DbSet<CustomItemTemplate> CustomItemTemplates { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Platform-wide audit log capturing who changed what and when, across all tenants.
|
||||
/// No global query filter — SuperAdmin controllers query this directly.
|
||||
|
||||
Generated
+10780
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,197 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
#nullable disable
|
||||
|
||||
namespace PowderCoating.Infrastructure.Migrations
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public partial class AddCustomItemTemplates : Migration
|
||||
{
|
||||
/// <inheritdoc />
|
||||
protected override void Up(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "CustomItemTemplateId",
|
||||
table: "QuoteItems",
|
||||
type: "int",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "FormulaFieldValuesJson",
|
||||
table: "QuoteItems",
|
||||
type: "nvarchar(max)",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "IsCustomFormulaItem",
|
||||
table: "QuoteItems",
|
||||
type: "bit",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
|
||||
migrationBuilder.AddColumn<int>(
|
||||
name: "CustomItemTemplateId",
|
||||
table: "JobItems",
|
||||
type: "int",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<string>(
|
||||
name: "FormulaFieldValuesJson",
|
||||
table: "JobItems",
|
||||
type: "nvarchar(max)",
|
||||
nullable: true);
|
||||
|
||||
migrationBuilder.AddColumn<bool>(
|
||||
name: "IsCustomFormulaItem",
|
||||
table: "JobItems",
|
||||
type: "bit",
|
||||
nullable: false,
|
||||
defaultValue: false);
|
||||
|
||||
migrationBuilder.CreateTable(
|
||||
name: "CustomItemTemplates",
|
||||
columns: table => new
|
||||
{
|
||||
Id = table.Column<int>(type: "int", nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1"),
|
||||
Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Description = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
OutputMode = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
FieldsJson = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
Formula = table.Column<string>(type: "nvarchar(max)", nullable: false),
|
||||
DefaultRate = table.Column<decimal>(type: "decimal(18,2)", nullable: true),
|
||||
RateLabel = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
Notes = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
DisplayOrder = table.Column<int>(type: "int", nullable: false),
|
||||
IsActive = table.Column<bool>(type: "bit", nullable: false),
|
||||
DiagramImagePath = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
CompanyId = table.Column<int>(type: "int", nullable: false),
|
||||
CreatedAt = table.Column<DateTime>(type: "datetime2", nullable: false),
|
||||
UpdatedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||
CreatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
UpdatedBy = table.Column<string>(type: "nvarchar(max)", nullable: true),
|
||||
IsDeleted = table.Column<bool>(type: "bit", nullable: false),
|
||||
DeletedAt = table.Column<DateTime>(type: "datetime2", nullable: true),
|
||||
DeletedBy = table.Column<string>(type: "nvarchar(max)", nullable: true)
|
||||
},
|
||||
constraints: table =>
|
||||
{
|
||||
table.PrimaryKey("PK_CustomItemTemplates", x => x.Id);
|
||||
});
|
||||
|
||||
migrationBuilder.UpdateData(
|
||||
table: "PricingTiers",
|
||||
keyColumn: "Id",
|
||||
keyValue: 1,
|
||||
column: "CreatedAt",
|
||||
value: new DateTime(2026, 5, 23, 15, 30, 51, 760, DateTimeKind.Utc).AddTicks(9869));
|
||||
|
||||
migrationBuilder.UpdateData(
|
||||
table: "PricingTiers",
|
||||
keyColumn: "Id",
|
||||
keyValue: 2,
|
||||
column: "CreatedAt",
|
||||
value: new DateTime(2026, 5, 23, 15, 30, 51, 760, DateTimeKind.Utc).AddTicks(9876));
|
||||
|
||||
migrationBuilder.UpdateData(
|
||||
table: "PricingTiers",
|
||||
keyColumn: "Id",
|
||||
keyValue: 3,
|
||||
column: "CreatedAt",
|
||||
value: new DateTime(2026, 5, 23, 15, 30, 51, 760, DateTimeKind.Utc).AddTicks(9878));
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_QuoteItems_CustomItemTemplateId",
|
||||
table: "QuoteItems",
|
||||
column: "CustomItemTemplateId");
|
||||
|
||||
migrationBuilder.CreateIndex(
|
||||
name: "IX_JobItems_CustomItemTemplateId",
|
||||
table: "JobItems",
|
||||
column: "CustomItemTemplateId");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_JobItems_CustomItemTemplates_CustomItemTemplateId",
|
||||
table: "JobItems",
|
||||
column: "CustomItemTemplateId",
|
||||
principalTable: "CustomItemTemplates",
|
||||
principalColumn: "Id");
|
||||
|
||||
migrationBuilder.AddForeignKey(
|
||||
name: "FK_QuoteItems_CustomItemTemplates_CustomItemTemplateId",
|
||||
table: "QuoteItems",
|
||||
column: "CustomItemTemplateId",
|
||||
principalTable: "CustomItemTemplates",
|
||||
principalColumn: "Id");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void Down(MigrationBuilder migrationBuilder)
|
||||
{
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_JobItems_CustomItemTemplates_CustomItemTemplateId",
|
||||
table: "JobItems");
|
||||
|
||||
migrationBuilder.DropForeignKey(
|
||||
name: "FK_QuoteItems_CustomItemTemplates_CustomItemTemplateId",
|
||||
table: "QuoteItems");
|
||||
|
||||
migrationBuilder.DropTable(
|
||||
name: "CustomItemTemplates");
|
||||
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_QuoteItems_CustomItemTemplateId",
|
||||
table: "QuoteItems");
|
||||
|
||||
migrationBuilder.DropIndex(
|
||||
name: "IX_JobItems_CustomItemTemplateId",
|
||||
table: "JobItems");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "CustomItemTemplateId",
|
||||
table: "QuoteItems");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "FormulaFieldValuesJson",
|
||||
table: "QuoteItems");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "IsCustomFormulaItem",
|
||||
table: "QuoteItems");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "CustomItemTemplateId",
|
||||
table: "JobItems");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "FormulaFieldValuesJson",
|
||||
table: "JobItems");
|
||||
|
||||
migrationBuilder.DropColumn(
|
||||
name: "IsCustomFormulaItem",
|
||||
table: "JobItems");
|
||||
|
||||
migrationBuilder.UpdateData(
|
||||
table: "PricingTiers",
|
||||
keyColumn: "Id",
|
||||
keyValue: 1,
|
||||
column: "CreatedAt",
|
||||
value: new DateTime(2026, 5, 23, 13, 51, 6, 293, DateTimeKind.Utc).AddTicks(4300));
|
||||
|
||||
migrationBuilder.UpdateData(
|
||||
table: "PricingTiers",
|
||||
keyColumn: "Id",
|
||||
keyValue: 2,
|
||||
column: "CreatedAt",
|
||||
value: new DateTime(2026, 5, 23, 13, 51, 6, 293, DateTimeKind.Utc).AddTicks(4313));
|
||||
|
||||
migrationBuilder.UpdateData(
|
||||
table: "PricingTiers",
|
||||
keyColumn: "Id",
|
||||
keyValue: 3,
|
||||
column: "CreatedAt",
|
||||
value: new DateTime(2026, 5, 23, 13, 51, 6, 293, DateTimeKind.Utc).AddTicks(4315));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2650,6 +2650,80 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
b.ToTable("CreditMemoApplications");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PowderCoating.Core.Entities.CustomItemTemplate", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
.ValueGeneratedOnAdd()
|
||||
.HasColumnType("int");
|
||||
|
||||
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));
|
||||
|
||||
b.Property<int>("CompanyId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime>("CreatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("CreatedBy")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<decimal?>("DefaultRate")
|
||||
.HasColumnType("decimal(18,2)");
|
||||
|
||||
b.Property<DateTime?>("DeletedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("DeletedBy")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Description")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("DiagramImagePath")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int>("DisplayOrder")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("FieldsJson")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Formula")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<bool>("IsActive")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<string>("Name")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("Notes")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("OutputMode")
|
||||
.IsRequired()
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("RateLabel")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<DateTime?>("UpdatedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
b.Property<string>("UpdatedBy")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.HasKey("Id");
|
||||
|
||||
b.ToTable("CustomItemTemplates");
|
||||
});
|
||||
|
||||
modelBuilder.Entity("PowderCoating.Core.Entities.Customer", b =>
|
||||
{
|
||||
b.Property<int>("Id")
|
||||
@@ -4473,6 +4547,9 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
b.Property<string>("CreatedBy")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int?>("CustomItemTemplateId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime?>("DeletedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
@@ -4489,12 +4566,18 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
b.Property<string>("Finish")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<string>("FormulaFieldValuesJson")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<bool>("IncludePrepCost")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsAiItem")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsCustomFormulaItem")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
@@ -4558,6 +4641,8 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
|
||||
b.HasIndex("CatalogItemId");
|
||||
|
||||
b.HasIndex("CustomItemTemplateId");
|
||||
|
||||
b.HasIndex("JobId")
|
||||
.HasDatabaseName("IX_JobItems_JobId");
|
||||
|
||||
@@ -7260,6 +7345,9 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
b.Property<string>("CreatedBy")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<int?>("CustomItemTemplateId")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<DateTime?>("DeletedAt")
|
||||
.HasColumnType("datetime2");
|
||||
|
||||
@@ -7273,12 +7361,18 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
b.Property<int>("EstimatedMinutes")
|
||||
.HasColumnType("int");
|
||||
|
||||
b.Property<string>("FormulaFieldValuesJson")
|
||||
.HasColumnType("nvarchar(max)");
|
||||
|
||||
b.Property<bool>("IncludePrepCost")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsAiItem")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsCustomFormulaItem")
|
||||
.HasColumnType("bit");
|
||||
|
||||
b.Property<bool>("IsDeleted")
|
||||
.HasColumnType("bit");
|
||||
|
||||
@@ -7348,6 +7442,8 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
|
||||
b.HasIndex("CatalogItemId");
|
||||
|
||||
b.HasIndex("CustomItemTemplateId");
|
||||
|
||||
b.HasIndex("QuoteId")
|
||||
.HasDatabaseName("IX_QuoteItems_QuoteId");
|
||||
|
||||
@@ -9512,6 +9608,10 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
.HasForeignKey("CatalogItemId")
|
||||
.OnDelete(DeleteBehavior.SetNull);
|
||||
|
||||
b.HasOne("PowderCoating.Core.Entities.CustomItemTemplate", "CustomItemTemplate")
|
||||
.WithMany()
|
||||
.HasForeignKey("CustomItemTemplateId");
|
||||
|
||||
b.HasOne("PowderCoating.Core.Entities.Job", "Job")
|
||||
.WithMany("JobItems")
|
||||
.HasForeignKey("JobId")
|
||||
@@ -9522,6 +9622,8 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
|
||||
b.Navigation("CatalogItem");
|
||||
|
||||
b.Navigation("CustomItemTemplate");
|
||||
|
||||
b.Navigation("Job");
|
||||
});
|
||||
|
||||
@@ -10131,6 +10233,10 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
.WithMany()
|
||||
.HasForeignKey("CatalogItemId");
|
||||
|
||||
b.HasOne("PowderCoating.Core.Entities.CustomItemTemplate", "CustomItemTemplate")
|
||||
.WithMany()
|
||||
.HasForeignKey("CustomItemTemplateId");
|
||||
|
||||
b.HasOne("PowderCoating.Core.Entities.Quote", "Quote")
|
||||
.WithMany("QuoteItems")
|
||||
.HasForeignKey("QuoteId")
|
||||
@@ -10141,6 +10247,8 @@ namespace PowderCoating.Infrastructure.Migrations
|
||||
|
||||
b.Navigation("CatalogItem");
|
||||
|
||||
b.Navigation("CustomItemTemplate");
|
||||
|
||||
b.Navigation("Quote");
|
||||
});
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="NCalc2" Version="2.1.0" />
|
||||
<PackageReference Include="SendGrid" Version="9.29.3" />
|
||||
<PackageReference Include="SixLabors.ImageSharp" Version="3.1.12" />
|
||||
<PackageReference Include="Stripe.net" Version="50.4.1" />
|
||||
|
||||
@@ -123,6 +123,9 @@ public class UnitOfWork : IUnitOfWork
|
||||
// Customer Intake Kiosk
|
||||
private IRepository<KioskSession>? _kioskSessions;
|
||||
|
||||
// Custom Formula Templates
|
||||
private IRepository<CustomItemTemplate>? _customItemTemplates;
|
||||
|
||||
// Purchase Orders
|
||||
private IPurchaseOrderRepository? _purchaseOrders;
|
||||
private IRepository<PurchaseOrderItem>? _purchaseOrderItems;
|
||||
@@ -457,6 +460,10 @@ public class UnitOfWork : IUnitOfWork
|
||||
public IRepository<KioskSession> KioskSessions =>
|
||||
_kioskSessions ??= new Repository<KioskSession>(_context);
|
||||
|
||||
/// <summary>Repository for <see cref="CustomItemTemplate"/> per-company reusable NCalc pricing formula templates; tenant-filtered with soft delete.</summary>
|
||||
public IRepository<CustomItemTemplate> CustomItemTemplates =>
|
||||
_customItemTemplates ??= new Repository<CustomItemTemplate>(_context);
|
||||
|
||||
// Job Templates
|
||||
/// <summary>Repository for <see cref="JobTemplate"/> reusable job blueprints; tenant-filtered with soft delete.</summary>
|
||||
public IJobTemplateRepository JobTemplates =>
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using NCalc2;
|
||||
using Anthropic.SDK;
|
||||
using Anthropic.SDK.Messaging;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PowderCoating.Application.DTOs.Company;
|
||||
using PowderCoating.Application.Interfaces;
|
||||
|
||||
namespace PowderCoating.Infrastructure.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Generates NCalc pricing formula templates from natural-language descriptions using
|
||||
/// Claude Sonnet. Accepts an optional diagram image so the model can see the physical
|
||||
/// shape being estimated. The model returns a structured JSON object containing the
|
||||
/// field list, NCalc expression, output mode, and verification inputs; the service
|
||||
/// parses and returns it as a <see cref="GenerateFormulaFromAiResponse"/>.
|
||||
/// </summary>
|
||||
public class CustomFormulaAiService : ICustomFormulaAiService
|
||||
{
|
||||
private readonly IConfiguration _config;
|
||||
private readonly ILogger<CustomFormulaAiService> _logger;
|
||||
|
||||
private const string SystemPrompt = @"You are an expert pricing formula engineer for a powder coating business.
|
||||
Your job is to generate NCalc expressions that calculate either a fixed price or a surface area
|
||||
from user-supplied field values. NCalc supports standard math operators (+, -, *, /, %, Pow()),
|
||||
comparison operators, and the Abs(), Round(), Max(), Min() built-in functions.
|
||||
|
||||
The user will describe a custom fabricated item (e.g., 'Roof curb', 'Electrical enclosure',
|
||||
'Tubular frame') and you must produce a pricing formula template.
|
||||
|
||||
Respond ONLY with a valid JSON object matching this exact schema — no markdown, no explanation:
|
||||
|
||||
{
|
||||
""name"": ""string — short template name (e.g. 'Roof Curb', 'Electrical Enclosure')"",
|
||||
""outputMode"": ""FixedRate"" | ""SurfaceAreaSqFt"",
|
||||
""fields"": [
|
||||
{
|
||||
""name"": ""snake_case_variable_name"",
|
||||
""label"": ""Human-readable label"",
|
||||
""unit"": ""in / ft / mm / cm / qty / lbs — or empty string"",
|
||||
""defaultValue"": number
|
||||
}
|
||||
],
|
||||
""formula"": ""NCalc expression using field name variables and optionally 'rate'"",
|
||||
""defaultRate"": number or null,
|
||||
""rateLabel"": ""string label for the rate field, e.g. '$/sq ft' — null if no rate"",
|
||||
""reasoning"": ""1-2 sentences explaining how the formula was derived"",
|
||||
""verificationInputs"": { ""variable_name"": number },
|
||||
""verificationResult"": number
|
||||
}
|
||||
|
||||
Rules:
|
||||
- Use FixedRate when the formula directly calculates a dollar amount (e.g. surface area × rate per sqft)
|
||||
- Use SurfaceAreaSqFt when the formula calculates square footage and the standard pricing engine handles the rest
|
||||
- Always include a 'rate' variable when outputMode is FixedRate and the price scales with dimensions
|
||||
- Field names must be valid NCalc identifiers (letters, digits, underscores; no spaces or hyphens)
|
||||
- verificationInputs and verificationResult must use the exact field names and formula you generated
|
||||
- Surface area formulas for box shapes: 2*(L*W + L*H + W*H) where L/W/H are in the same unit; convert to sqft if needed
|
||||
- For inch inputs convert to sqft: divide by 144 (sqin→sqft) or use /12 per side before multiplying
|
||||
";
|
||||
|
||||
public CustomFormulaAiService(IConfiguration config, ILogger<CustomFormulaAiService> logger)
|
||||
{
|
||||
_config = config;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task<GenerateFormulaFromAiResponse> GenerateFormulaAsync(
|
||||
GenerateFormulaFromAiRequest request,
|
||||
byte[]? imageBytes = null,
|
||||
string? imageContentType = null)
|
||||
{
|
||||
var apiKey = _config["AI:Anthropic:ApiKey"];
|
||||
if (string.IsNullOrWhiteSpace(apiKey) || apiKey.StartsWith("your-"))
|
||||
{
|
||||
return new GenerateFormulaFromAiResponse
|
||||
{
|
||||
Success = false,
|
||||
Error = "Anthropic API key is not configured. Add AI:Anthropic:ApiKey to appsettings.json."
|
||||
};
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var client = new AnthropicClient(apiKey);
|
||||
|
||||
var userContent = new List<ContentBase>();
|
||||
|
||||
if (imageBytes is { Length: > 0 } && !string.IsNullOrWhiteSpace(imageContentType))
|
||||
{
|
||||
userContent.Add(new ImageContent
|
||||
{
|
||||
Source = new ImageSource
|
||||
{
|
||||
MediaType = imageContentType,
|
||||
Data = Convert.ToBase64String(imageBytes)
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
userContent.Add(new TextContent { Text = request.Description });
|
||||
|
||||
var messages = new List<Message>
|
||||
{
|
||||
new() { Role = RoleType.User, Content = userContent }
|
||||
};
|
||||
|
||||
var response = await client.Messages.GetClaudeMessageAsync(new MessageParameters
|
||||
{
|
||||
Model = "claude-sonnet-4-6",
|
||||
MaxTokens = 1024,
|
||||
SystemMessage = SystemPrompt,
|
||||
Messages = messages
|
||||
});
|
||||
|
||||
var rawJson = response.Message.ToString().Trim();
|
||||
|
||||
// Strip markdown code fences if the model adds them
|
||||
if (rawJson.StartsWith("```"))
|
||||
{
|
||||
var start = rawJson.IndexOf('\n') + 1;
|
||||
var end = rawJson.LastIndexOf("```");
|
||||
if (end > start) rawJson = rawJson[start..end].Trim();
|
||||
}
|
||||
|
||||
using var doc = JsonDocument.Parse(rawJson);
|
||||
var root = doc.RootElement;
|
||||
|
||||
var fieldsJson = root.TryGetProperty("fields", out var fieldsEl)
|
||||
? fieldsEl.GetRawText()
|
||||
: "[]";
|
||||
|
||||
decimal? defaultRate = null;
|
||||
if (root.TryGetProperty("defaultRate", out var rateEl) && rateEl.ValueKind == JsonValueKind.Number)
|
||||
defaultRate = rateEl.GetDecimal();
|
||||
|
||||
decimal? verificationResult = null;
|
||||
if (root.TryGetProperty("verificationResult", out var vrEl) && vrEl.ValueKind == JsonValueKind.Number)
|
||||
verificationResult = vrEl.GetDecimal();
|
||||
|
||||
string? verificationInputs = null;
|
||||
if (root.TryGetProperty("verificationInputs", out var viEl))
|
||||
verificationInputs = viEl.GetRawText();
|
||||
|
||||
return new GenerateFormulaFromAiResponse
|
||||
{
|
||||
Success = true,
|
||||
Name = root.TryGetProperty("name", out var nameEl) ? nameEl.GetString() : null,
|
||||
OutputMode = root.TryGetProperty("outputMode", out var omEl) ? omEl.GetString() : "FixedRate",
|
||||
FieldsJson = fieldsJson,
|
||||
Formula = root.TryGetProperty("formula", out var fEl) ? fEl.GetString() : null,
|
||||
DefaultRate = defaultRate,
|
||||
RateLabel = root.TryGetProperty("rateLabel", out var rlEl) ? rlEl.GetString() : null,
|
||||
Reasoning = root.TryGetProperty("reasoning", out var reEl) ? reEl.GetString() : null,
|
||||
VerificationResult = verificationResult,
|
||||
VerificationInputs = verificationInputs
|
||||
};
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "CustomFormulaAiService.GenerateFormulaAsync failed");
|
||||
return new GenerateFormulaFromAiResponse { Success = false, Error = ex.Message };
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public EvaluateFormulaResponse EvaluateFormula(EvaluateFormulaRequest request)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(request?.Formula))
|
||||
return new EvaluateFormulaResponse { Success = false, Error = "Formula is required." };
|
||||
|
||||
try
|
||||
{
|
||||
var variables = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(
|
||||
request.VariablesJson ?? "{}") ?? new();
|
||||
|
||||
var expr = new Expression(request.Formula);
|
||||
foreach (var kv in variables)
|
||||
{
|
||||
expr.Parameters[kv.Key] = kv.Value.ValueKind == JsonValueKind.Number
|
||||
? (object)kv.Value.GetDecimal()
|
||||
: (object)(kv.Value.GetString() ?? "");
|
||||
}
|
||||
|
||||
var result = expr.Evaluate();
|
||||
var decResult = Convert.ToDecimal(result);
|
||||
return new EvaluateFormulaResponse { Success = true, Result = Math.Round(decResult, 4) };
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new EvaluateFormulaResponse { Success = false, Error = ex.Message };
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user