@learning-commons/evaluators 0.2.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,4 +1,6 @@
1
1
  import { z } from 'zod';
2
+ import { B as BaseEvaluator, a as BaseEvaluatorConfig } from './base-Ced9oKKa.cjs';
3
+ export { E as EvaluatorMetadata, b as LogContext, c as LogLevel, L as Logger, T as TelemetryOptions } from './base-Ced9oKKa.cjs';
2
4
 
3
5
  /**
4
6
  * Shared complexity levels used across all text complexity evaluators
@@ -202,85 +204,6 @@ declare class TimeoutError extends APIError {
202
204
  constructor(message?: string);
203
205
  }
204
206
 
205
- /**
206
- * Logging interface for the Evaluators SDK
207
- *
208
- * Provides structured logging with verbosity levels.
209
- * Users can inject custom loggers or use the default console logger.
210
- */
211
- /**
212
- * Log levels in order of verbosity
213
- */
214
- declare enum LogLevel {
215
- /** Debug messages - very verbose, for development */
216
- DEBUG = 0,
217
- /** Informational messages - normal operations */
218
- INFO = 1,
219
- /** Warning messages - potentially problematic situations */
220
- WARN = 2,
221
- /** Error messages - errors that need attention */
222
- ERROR = 3,
223
- /** Silent - no logging */
224
- SILENT = 4
225
- }
226
- /**
227
- * Context object for structured logging
228
- */
229
- interface LogContext {
230
- /** Evaluator type (vocabulary, sentence-structure, etc.) */
231
- evaluator?: string;
232
- /** Current operation or stage */
233
- operation?: string;
234
- /** Error object if applicable */
235
- error?: Error;
236
- /** Additional metadata */
237
- [key: string]: unknown;
238
- }
239
- /**
240
- * Logger interface
241
- *
242
- * Implement this interface to provide custom logging behavior.
243
- *
244
- * @example
245
- * ```typescript
246
- * const customLogger: Logger = {
247
- * debug: (msg, ctx) => myLogger.debug(msg, ctx),
248
- * info: (msg, ctx) => myLogger.info(msg, ctx),
249
- * warn: (msg, ctx) => myLogger.warn(msg, ctx),
250
- * error: (msg, ctx) => myLogger.error(msg, ctx),
251
- * };
252
- *
253
- * const evaluator = new VocabularyEvaluator({
254
- * googleApiKey: '...',
255
- * openaiApiKey: '...',
256
- * logger: customLogger,
257
- * logLevel: LogLevel.INFO,
258
- * });
259
- * ```
260
- */
261
- interface Logger {
262
- /**
263
- * Log debug message
264
- * Used for detailed debugging information
265
- */
266
- debug(message: string, context?: LogContext): void;
267
- /**
268
- * Log informational message
269
- * Used for normal operations
270
- */
271
- info(message: string, context?: LogContext): void;
272
- /**
273
- * Log warning message
274
- * Used for potentially problematic situations
275
- */
276
- warn(message: string, context?: LogContext): void;
277
- /**
278
- * Log error message
279
- * Used for errors that need attention
280
- */
281
- error(message: string, context?: LogContext): void;
282
- }
283
-
284
207
  /**
285
208
  * Message format for LLM conversations
286
209
  */
@@ -581,255 +504,28 @@ declare const SmkOutputSchema: z.ZodObject<{
581
504
  type SmkInternal = z.infer<typeof SmkOutputSchema>;
582
505
 
583
506
  /**
584
- * Evaluation status
507
+ * Conventionality evaluation output schema
585
508
  */
586
- type EvaluationStatus = 'success' | 'error';
587
- /**
588
- * Token usage metrics from LLM providers
589
- */
590
- interface TokenUsage {
591
- input_tokens: number;
592
- output_tokens: number;
593
- }
594
- /**
595
- * Per-stage details for multi-stage evaluations
596
- */
597
- interface StageDetail {
598
- /** Stage name (e.g., "background_knowledge", "complexity_evaluation") */
599
- stage: string;
600
- /** Provider used for this stage (e.g., "openai:gpt-4o") */
601
- provider: string;
602
- /** Total latency including all retries (ms) */
603
- latency_ms: number;
604
- /** Token usage aggregated across all attempts */
605
- token_usage?: TokenUsage;
606
- /**
607
- * Whether schema validation failed (indicates prompt needs clearer instructions)
608
- *
609
- * TODO: Not currently tracked. Vercel AI SDK abstracts validation away.
610
- * To implement: Add custom retry wrapper that catches validation errors.
611
- */
612
- schema_validation_failed?: boolean;
613
- }
614
- /**
615
- * Extensible metadata for telemetry events
616
- */
617
- interface TelemetryMetadata {
618
- /** Detailed breakdown by stage (for multi-stage evaluations) */
619
- stage_details?: StageDetail[];
620
- }
621
- /**
622
- * Telemetry event payload
623
- */
624
- interface TelemetryEvent {
625
- timestamp: string;
626
- sdk_version: string;
627
- evaluator_type: string;
628
- grade?: string;
629
- status: EvaluationStatus;
630
- error_code?: string;
631
- latency_ms: number;
632
- text_length_chars: number;
633
- provider: string;
634
- token_usage?: TokenUsage;
635
- metadata?: TelemetryMetadata;
636
- input_text?: string;
637
- }
638
- /**
639
- * Configuration for telemetry client
640
- */
641
- interface TelemetryConfig {
642
- /** Analytics service endpoint URL */
643
- endpoint: string;
644
- /** Learning Commons partner key (optional, sent as X-API-Key header) */
645
- partnerKey?: string;
646
- /** Client ID for anonymous tracking (persistent UUID from ~/.config/learning-commons/config.json) */
647
- clientId: string;
648
- /** Enable telemetry (default: true) */
649
- enabled: boolean;
650
- /** Logger instance (respects the SDK's configured log level and custom logger) */
651
- logger: Logger;
652
- }
653
-
654
- /**
655
- * Telemetry client for sending analytics events
656
- *
657
- * Fire-and-forget implementation that never blocks SDK operations.
658
- * Errors are logged but don't fail evaluations.
659
- */
660
- declare class TelemetryClient {
661
- private config;
662
- private logger;
663
- constructor(config: TelemetryConfig);
664
- /**
665
- * Send telemetry event to analytics service
666
- *
667
- * Fire-and-forget: Errors are logged but don't throw.
668
- */
669
- send(event: TelemetryEvent): Promise<void>;
670
- }
671
-
672
- /**
673
- * Granular telemetry configuration options
674
- */
675
- interface TelemetryOptions {
676
- /** Enable telemetry (default: true) */
677
- enabled?: boolean;
678
- /** Record input text in telemetry (default: false) */
679
- recordInputs?: boolean;
680
- }
681
- /**
682
- * Base configuration for all evaluators
683
- */
684
- interface BaseEvaluatorConfig {
685
- /** Google API key (for evaluators using Gemini) */
686
- googleApiKey?: string;
687
- /** OpenAI API key (for evaluators using GPT) */
688
- openaiApiKey?: string;
689
- /** Learning Commons partner key for authenticated telemetry (optional) */
690
- partnerKey?: string;
691
- /**
692
- * Maximum number of retries for failed API calls (default: 2)
693
- * Set to 0 to disable retries.
694
- *
695
- * Note: With maxRetries=2, a failed call will be attempted up to 3 times total
696
- * (1 initial attempt + 2 retries)
697
- */
698
- maxRetries?: number;
699
- /**
700
- * Telemetry configuration (default: all enabled)
701
- *
702
- * Can be:
703
- * - `true`: Enable with defaults (recordInputs: false)
704
- * - `false`: Disable completely
705
- * - `TelemetryOptions`: Granular control
706
- */
707
- telemetry?: boolean | TelemetryOptions;
708
- /**
709
- * Custom logger implementation (optional)
710
- * If not provided, uses console logger with specified logLevel
711
- */
712
- logger?: Logger;
713
- /**
714
- * Log level for default console logger (default: WARN)
715
- * Only used if custom logger is not provided
716
- *
717
- * - DEBUG: Very verbose, shows all operations
718
- * - INFO: Normal operations
719
- * - WARN: Warnings only (default)
720
- * - ERROR: Errors only
721
- * - SILENT: No logging
722
- */
723
- logLevel?: LogLevel;
724
- }
725
- /**
726
- * Evaluator metadata interface
727
- * Each evaluator must provide this metadata as static properties
728
- */
729
- interface EvaluatorMetadata {
730
- /** Unique identifier for the evaluator (e.g., 'vocabulary', 'sentence-structure') */
731
- readonly id: string;
732
- /** Human-readable name (e.g., 'Vocabulary', 'Sentence Structure') */
733
- readonly name: string;
734
- /** Brief description of what the evaluator does */
735
- readonly description: string;
736
- /** Supported grade levels (e.g., ['3', '4', '5', ...]) */
737
- readonly supportedGrades: readonly string[];
738
- /** Whether this evaluator requires a Google API key */
739
- readonly requiresGoogleKey: boolean;
740
- /** Whether this evaluator requires an OpenAI API key */
741
- readonly requiresOpenAIKey: boolean;
742
- }
743
- /**
744
- * Abstract base class for all evaluators
745
- *
746
- * Provides common functionality:
747
- * - Telemetry setup and event sending
748
- * - Text validation
749
- * - Grade validation (with overridable default)
750
- * - Metadata creation
751
- *
752
- * Concrete evaluators must implement:
753
- * - static metadata: Provide evaluator metadata (see EvaluatorMetadata interface)
754
- */
755
- declare abstract class BaseEvaluator {
756
- protected telemetryClient?: TelemetryClient;
757
- protected logger: Logger;
758
- protected config: Required<Pick<BaseEvaluatorConfig, 'maxRetries'>> & {
759
- telemetry: Required<TelemetryOptions>;
760
- };
761
- /**
762
- * Static metadata for the evaluator
763
- *
764
- * Concrete evaluators MUST define this property.
765
- *
766
- * @example
767
- * ```typescript
768
- * class MyEvaluator extends BaseEvaluator {
769
- * static readonly metadata = {
770
- * id: 'my-evaluator',
771
- * name: 'My Evaluator',
772
- * description: 'Does something useful',
773
- * supportedGrades: ['3', '4', '5'],
774
- * requiresGoogleKey: true,
775
- * requiresOpenAIKey: false,
776
- * };
777
- * }
778
- * ```
779
- */
780
- static readonly metadata: EvaluatorMetadata;
781
- constructor(config: BaseEvaluatorConfig);
782
- /**
783
- * Get metadata for this evaluator instance
784
- * @throws {ConfigurationError} If the subclass has not defined static metadata
785
- */
786
- protected get metadata(): EvaluatorMetadata;
787
- /**
788
- * Validate that required API keys are provided based on metadata
789
- * @throws {ConfigurationError} If required API keys are missing
790
- */
791
- private validateApiKeys;
792
- /**
793
- * Normalize telemetry config to standard format
794
- */
795
- private normalizeTelemetryConfig;
796
- /**
797
- * Get the evaluator type identifier from metadata
798
- * @returns The evaluator type ID (e.g., "vocabulary", "sentence-structure")
799
- */
800
- protected getEvaluatorType(): string;
801
- /**
802
- * Validate text meets requirements
803
- * Default implementation - can be overridden by concrete evaluators
804
- *
805
- * @throws {ValidationError} If text is invalid
806
- */
807
- protected validateText(text: string): void;
808
- /**
809
- * Validate grade is in supported range
810
- * Default implementation - can be overridden by concrete evaluators
811
- *
812
- * @param grade - Grade level to validate
813
- * @param validGrades - Set of valid grades for this evaluator
814
- * @throws {ValidationError} If grade is invalid
815
- */
816
- protected validateGrade(grade: string, validGrades: Set<string>): void;
817
- /**
818
- * Send telemetry event to analytics service
819
- * Common helper for all evaluators
820
- */
821
- protected sendTelemetry(params: {
822
- status: 'success' | 'error';
823
- latencyMs: number;
824
- textLength: number;
825
- grade?: string;
826
- provider: string;
827
- errorCode?: string;
828
- tokenUsage?: TokenUsage;
829
- metadata?: TelemetryMetadata;
830
- inputText?: string;
831
- }): Promise<void>;
832
- }
509
+ declare const ConventionalityOutputSchema: z.ZodObject<{
510
+ conventionality_features: z.ZodArray<z.ZodString, "many">;
511
+ grade_context: z.ZodString;
512
+ instructional_insights: z.ZodString;
513
+ complexity_score: z.ZodEnum<["Slightly complex", "Moderately complex", "Very complex", "Exceedingly complex"]>;
514
+ reasoning: z.ZodString;
515
+ }, "strip", z.ZodTypeAny, {
516
+ reasoning: string;
517
+ complexity_score: "Slightly complex" | "Moderately complex" | "Very complex" | "Exceedingly complex";
518
+ conventionality_features: string[];
519
+ grade_context: string;
520
+ instructional_insights: string;
521
+ }, {
522
+ reasoning: string;
523
+ complexity_score: "Slightly complex" | "Moderately complex" | "Very complex" | "Exceedingly complex";
524
+ conventionality_features: string[];
525
+ grade_context: string;
526
+ instructional_insights: string;
527
+ }>;
528
+ type ConventionalityInternal = z.infer<typeof ConventionalityOutputSchema>;
833
529
 
834
530
  /**
835
531
  * Vocabulary Evaluator
@@ -1114,6 +810,69 @@ declare class SmkEvaluator extends BaseEvaluator {
1114
810
  */
1115
811
  declare function evaluateSmk(text: string, grade: string, config: BaseEvaluatorConfig): Promise<EvaluationResult<TextComplexityLevel, SmkInternal>>;
1116
812
 
813
+ /**
814
+ * Conventionality Evaluator
815
+ *
816
+ * Evaluates how explicit, literal, and straightforward a text's meaning is versus
817
+ * how abstract, ironic, figurative, or archaic it is for the target grade level.
818
+ *
819
+ * Based on the Common Core Qualitative Text Complexity Rubric with 4 levels:
820
+ * - Slightly complex
821
+ * - Moderately complex
822
+ * - Very complex
823
+ * - Exceedingly complex
824
+ *
825
+ * @example
826
+ * ```typescript
827
+ * const evaluator = new ConventionalityEvaluator({
828
+ * googleApiKey: process.env.GOOGLE_API_KEY
829
+ * });
830
+ *
831
+ * const result = await evaluator.evaluate(text, "6");
832
+ * console.log(result.score); // "Moderately complex"
833
+ * console.log(result.reasoning);
834
+ * ```
835
+ */
836
+ declare class ConventionalityEvaluator extends BaseEvaluator {
837
+ static readonly metadata: {
838
+ id: string;
839
+ name: string;
840
+ description: string;
841
+ supportedGrades: readonly ["3", "4", "5", "6", "7", "8", "9", "10", "11", "12"];
842
+ requiresGoogleKey: boolean;
843
+ requiresOpenAIKey: boolean;
844
+ };
845
+ private provider;
846
+ constructor(config: BaseEvaluatorConfig);
847
+ /**
848
+ * Evaluate conventionality complexity for a given text and grade level
849
+ *
850
+ * @param text - The text to evaluate
851
+ * @param grade - The target grade level (3-12)
852
+ * @returns Evaluation result with complexity score and detailed analysis
853
+ * @throws {ValidationError} If text is empty, too short/long, or grade is invalid
854
+ * @throws {APIError} If LLM API calls fail (includes AuthenticationError, RateLimitError, NetworkError, TimeoutError)
855
+ */
856
+ evaluate(text: string, grade: string): Promise<EvaluationResult<TextComplexityLevel, ConventionalityInternal>>;
857
+ /**
858
+ * Run the Conventionality evaluation LLM call
859
+ */
860
+ private evaluateConventionality;
861
+ }
862
+ /**
863
+ * Functional API for Conventionality evaluation
864
+ *
865
+ * @example
866
+ * ```typescript
867
+ * const result = await evaluateConventionality(
868
+ * "The author uses sustained irony to critique societal norms.",
869
+ * "10",
870
+ * { googleApiKey: process.env.GOOGLE_API_KEY }
871
+ * );
872
+ * ```
873
+ */
874
+ declare function evaluateConventionality(text: string, grade: string, config: BaseEvaluatorConfig): Promise<EvaluationResult<TextComplexityLevel, ConventionalityInternal>>;
875
+
1117
876
  /**
1118
877
  * Result map returned by TextComplexityEvaluator.
1119
878
  * Each key holds the full evaluation result from its sub-evaluator, or an error if it failed.
@@ -1128,17 +887,21 @@ interface TextComplexityResult {
1128
887
  subjectMatterKnowledge: EvaluationResult<TextComplexityLevel, SmkInternal> | {
1129
888
  error: Error;
1130
889
  };
890
+ conventionality: EvaluationResult<TextComplexityLevel, ConventionalityInternal> | {
891
+ error: Error;
892
+ };
1131
893
  }
1132
894
  /**
1133
895
  * Text Complexity Evaluator
1134
896
  *
1135
- * Composite evaluator that analyzes vocabulary, sentence structure, and subject matter knowledge.
897
+ * Composite evaluator that analyzes vocabulary, sentence structure, subject matter knowledge, and conventionality.
1136
898
  * Runs all evaluations in parallel with concurrency control to avoid rate limiting.
1137
899
  *
1138
900
  * Uses:
1139
901
  * - VocabularyEvaluator (Google Gemini 2.5 Pro + OpenAI GPT-4o)
1140
902
  * - SentenceStructureEvaluator (OpenAI GPT-4o)
1141
903
  * - SmkEvaluator (Google Gemini 3 Flash Preview)
904
+ * - ConventionalityEvaluator (Google Gemini 3 Flash Preview)
1142
905
  *
1143
906
  * @example
1144
907
  * ```typescript
@@ -1165,6 +928,7 @@ declare class TextComplexityEvaluator extends BaseEvaluator {
1165
928
  private vocabularyEvaluator;
1166
929
  private sentenceStructureEvaluator;
1167
930
  private smkEvaluator;
931
+ private conventionalityEvaluator;
1168
932
  private limit;
1169
933
  constructor(config: BaseEvaluatorConfig);
1170
934
  /**
@@ -1234,4 +998,4 @@ declare function addEngineeredFeatures(analysis: SentenceAnalysis): SentenceFeat
1234
998
  */
1235
999
  declare function featuresToJSON(features: SentenceFeatures, decimals?: number, castToInt?: boolean): string;
1236
1000
 
1237
- export { APIError, AuthenticationError, type BaseEvaluatorConfig, type ComplexityClassification, ComplexityClassificationSchema, ConfigurationError, type EvaluationError, type EvaluationMetadata, type EvaluationResult, EvaluatorError, type EvaluatorMetadata, GradeBand, GradeLevelAppropriatenessEvaluator, type GradeLevelAppropriatenessInternal, GradeLevelAppropriatenessSchema, type LLMProvider, type LLMRequest, type LLMResponse, type LogContext, LogLevel, type Logger, type Message, NetworkError, type ProviderConfig, RateLimitError, type ReadabilityMetrics, type SentenceAnalysis, SentenceAnalysisSchema, type SentenceFeatures, SentenceStructureEvaluator, type SentenceStructureInternal, SmkEvaluator, type SmkInternal, type TelemetryOptions, TextComplexityEvaluator, TextComplexityLevel, type TextComplexityResult, type TextGenerationResponse, TimeoutError, ValidationError, VocabularyEvaluator, type VocabularyInternal, addEngineeredFeatures, calculateFleschKincaidGrade, calculateReadabilityMetrics, evaluateGradeLevelAppropriateness, evaluateSentenceStructure, evaluateSmk, evaluateTextComplexity, evaluateVocabulary, featuresToJSON };
1001
+ export { APIError, AuthenticationError, BaseEvaluatorConfig, type ComplexityClassification, ComplexityClassificationSchema, ConfigurationError, ConventionalityEvaluator, type ConventionalityInternal, type EvaluationError, type EvaluationMetadata, type EvaluationResult, EvaluatorError, GradeBand, GradeLevelAppropriatenessEvaluator, type GradeLevelAppropriatenessInternal, GradeLevelAppropriatenessSchema, type LLMProvider, type LLMRequest, type LLMResponse, type Message, NetworkError, type ProviderConfig, RateLimitError, type ReadabilityMetrics, type SentenceAnalysis, SentenceAnalysisSchema, type SentenceFeatures, SentenceStructureEvaluator, type SentenceStructureInternal, SmkEvaluator, type SmkInternal, TextComplexityEvaluator, TextComplexityLevel, type TextComplexityResult, type TextGenerationResponse, TimeoutError, ValidationError, VocabularyEvaluator, type VocabularyInternal, addEngineeredFeatures, calculateFleschKincaidGrade, calculateReadabilityMetrics, evaluateConventionality, evaluateGradeLevelAppropriateness, evaluateSentenceStructure, evaluateSmk, evaluateTextComplexity, evaluateVocabulary, featuresToJSON };