SET ANSI_NULLS ON; GO SET QUOTED_IDENTIFIER ON; GO IF COL_LENGTH(N'dbo.StoryIntelligenceRuns', N'KnownCharactersJson') IS NULL BEGIN ALTER TABLE dbo.StoryIntelligenceRuns ADD KnownCharactersJson nvarchar(max) NULL; END; GO CREATE OR ALTER PROCEDURE dbo.StoryIntelligenceRun_QueueAdminText @UserID int, @ProjectID int = NULL, @BookID int = NULL, @ChapterID int = NULL, @ChapterNumber decimal(9, 2) = NULL, @SourceType nvarchar(50), @SourceLabel nvarchar(300), @SourceText nvarchar(max), @SourceFileName nvarchar(260) = NULL, @SourceFileSizeBytes bigint = NULL, @SourceWordCount int = NULL, @SourceCharacterCount int = NULL, @SourceParagraphCount int = NULL, @SourceChapterCount int = NULL, @Model nvarchar(100), @PromptVersionsSummary nvarchar(500), @KnownCharactersJson nvarchar(max) = NULL AS BEGIN SET NOCOUNT ON; INSERT dbo.StoryIntelligenceRuns ( UserID, ProjectID, BookID, ChapterID, ChapterNumber, Status, SourceType, SourceLabel, SourceText, SourceFileName, SourceFileSizeBytes, SourceWordCount, SourceCharacterCount, SourceParagraphCount, SourceChapterCount, Model, PromptVersion, PromptVersionsSummary, KnownCharactersJson, StartedUtc, CurrentStage, CurrentMessage, CompletedScenes, FailedScenes ) VALUES ( @UserID, @ProjectID, @BookID, @ChapterID, @ChapterNumber, N'Pending', @SourceType, @SourceLabel, @SourceText, @SourceFileName, @SourceFileSizeBytes, @SourceWordCount, @SourceCharacterCount, @SourceParagraphCount, @SourceChapterCount, @Model, @PromptVersionsSummary, @PromptVersionsSummary, @KnownCharactersJson, SYSUTCDATETIME(), N'Pending', N'Queued for Story Intelligence processing.', 0, 0 ); SELECT CAST(SCOPE_IDENTITY() AS int) AS StoryIntelligenceRunID; END; GO CREATE OR ALTER PROCEDURE dbo.StoryIntelligenceRun_ClaimNextPending AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; DECLARE @StoryIntelligenceRunID int; SELECT TOP (1) @StoryIntelligenceRunID = StoryIntelligenceRunID FROM dbo.StoryIntelligenceRuns WITH (UPDLOCK, READPAST) WHERE Status = N'Pending' ORDER BY CreatedUtc; IF @StoryIntelligenceRunID IS NULL BEGIN SELECT TOP (0) StoryIntelligenceRunID, UserID, ProjectID, BookID, ChapterID, ChapterNumber, Status, SourceType, SourceLabel, SourceText, SourceWordCount, SourceCharacterCount, SourceParagraphCount, SourceChapterCount, PromptVersion, PromptVersionsSummary, KnownCharactersJson, Model, StartedUtc, CompletedUtc, FailureStage, TotalInputTokens, TotalOutputTokens, TotalTokens, TotalDurationMs, EstimatedCostGBP, EstimatedCostUSD, ErrorMessage, ErrorDetail, CurrentStage, CurrentMessage, TotalDetectedScenes, CompletedScenes, FailedScenes, CancellationRequestedUtc, CancelledUtc, CreatedUtc, UpdatedUtc FROM dbo.StoryIntelligenceRuns; RETURN; END; UPDATE dbo.StoryIntelligenceRuns SET Status = N'Running', StartedUtc = SYSUTCDATETIME(), CurrentStage = N'ChapterStructure', CurrentMessage = N'Running Chapter Structure analysis.', UpdatedUtc = SYSUTCDATETIME() WHERE StoryIntelligenceRunID = @StoryIntelligenceRunID AND Status = N'Pending'; SELECT StoryIntelligenceRunID, UserID, ProjectID, BookID, ChapterID, ChapterNumber, Status, SourceType, SourceLabel, SourceText, SourceWordCount, SourceCharacterCount, SourceParagraphCount, SourceChapterCount, PromptVersion, PromptVersionsSummary, KnownCharactersJson, Model, StartedUtc, CompletedUtc, FailureStage, TotalInputTokens, TotalOutputTokens, TotalTokens, TotalDurationMs, EstimatedCostGBP, EstimatedCostUSD, ErrorMessage, ErrorDetail, CurrentStage, CurrentMessage, TotalDetectedScenes, CompletedScenes, FailedScenes, CancellationRequestedUtc, CancelledUtc, CreatedUtc, UpdatedUtc FROM dbo.StoryIntelligenceRuns WHERE StoryIntelligenceRunID = @StoryIntelligenceRunID; END; GO