cognitive-modules-cli 1.4.1 → 2.2.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/modules/runner.d.ts +2 -44
- package/dist/modules/runner.js +2 -604
- package/dist/providers/base.d.ts +1 -45
- package/dist/providers/base.js +0 -67
- package/dist/providers/openai.d.ts +3 -27
- package/dist/providers/openai.js +3 -175
- package/dist/types.d.ts +1 -308
- package/dist/types.js +1 -120
- package/package.json +1 -1
- package/src/modules/runner.ts +3 -797
- package/src/providers/base.ts +1 -86
- package/src/providers/openai.ts +4 -226
- package/src/types.ts +1 -454
package/src/types.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Cognitive Runtime - Core Types
|
|
3
|
-
* Version 2.
|
|
3
|
+
* Version 2.2 - With Control/Data plane separation, tier, overflow, extensible enums
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
// =============================================================================
|
|
@@ -493,456 +493,3 @@ export function shouldEscalate<T>(
|
|
|
493
493
|
|
|
494
494
|
return false;
|
|
495
495
|
}
|
|
496
|
-
|
|
497
|
-
// =============================================================================
|
|
498
|
-
// v2.5 Streaming Types
|
|
499
|
-
// =============================================================================
|
|
500
|
-
|
|
501
|
-
/** Response mode configuration */
|
|
502
|
-
export type ResponseMode = 'sync' | 'streaming' | 'both';
|
|
503
|
-
|
|
504
|
-
/** Chunk type for streaming */
|
|
505
|
-
export type ChunkType = 'delta' | 'snapshot';
|
|
506
|
-
|
|
507
|
-
/** Response configuration in module.yaml */
|
|
508
|
-
export interface ResponseConfig {
|
|
509
|
-
mode: ResponseMode;
|
|
510
|
-
chunk_type?: ChunkType;
|
|
511
|
-
buffer_size?: number;
|
|
512
|
-
heartbeat_interval_ms?: number;
|
|
513
|
-
max_duration_ms?: number;
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
/** Meta chunk - initial streaming response */
|
|
517
|
-
export interface MetaChunk {
|
|
518
|
-
ok: true;
|
|
519
|
-
streaming: true;
|
|
520
|
-
session_id: string;
|
|
521
|
-
resumed?: boolean;
|
|
522
|
-
resume_from_seq?: number;
|
|
523
|
-
meta: Partial<EnvelopeMeta>;
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
/** Delta chunk - incremental content */
|
|
527
|
-
export interface DeltaChunk {
|
|
528
|
-
chunk: {
|
|
529
|
-
seq: number;
|
|
530
|
-
type: 'delta';
|
|
531
|
-
field?: string;
|
|
532
|
-
delta: string;
|
|
533
|
-
checkpoint?: Checkpoint;
|
|
534
|
-
};
|
|
535
|
-
}
|
|
536
|
-
|
|
537
|
-
/** Snapshot chunk - full state replacement */
|
|
538
|
-
export interface SnapshotChunk {
|
|
539
|
-
chunk: {
|
|
540
|
-
seq: number;
|
|
541
|
-
type: 'snapshot';
|
|
542
|
-
field?: string;
|
|
543
|
-
data: unknown;
|
|
544
|
-
};
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
/** Progress chunk - progress update */
|
|
548
|
-
export interface ProgressChunk {
|
|
549
|
-
progress: {
|
|
550
|
-
percent: number;
|
|
551
|
-
stage?: string;
|
|
552
|
-
message?: string;
|
|
553
|
-
};
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
/** Final chunk - completion signal */
|
|
557
|
-
export interface FinalChunk {
|
|
558
|
-
final: true;
|
|
559
|
-
meta: EnvelopeMeta;
|
|
560
|
-
data: ModuleResultData;
|
|
561
|
-
usage?: {
|
|
562
|
-
input_tokens: number;
|
|
563
|
-
output_tokens: number;
|
|
564
|
-
total_tokens: number;
|
|
565
|
-
};
|
|
566
|
-
}
|
|
567
|
-
|
|
568
|
-
/** Recovery checkpoint for stream resume */
|
|
569
|
-
export interface Checkpoint {
|
|
570
|
-
offset: number;
|
|
571
|
-
hash: string; // First 6 chars of SHA256
|
|
572
|
-
}
|
|
573
|
-
|
|
574
|
-
/** Recovery information in error */
|
|
575
|
-
export interface RecoveryInfo {
|
|
576
|
-
last_seq: number;
|
|
577
|
-
last_checkpoint?: Checkpoint;
|
|
578
|
-
retry_after_ms?: number;
|
|
579
|
-
max_retries?: number;
|
|
580
|
-
}
|
|
581
|
-
|
|
582
|
-
/** Error with optional recovery information */
|
|
583
|
-
export interface ErrorWithRecovery {
|
|
584
|
-
code: string;
|
|
585
|
-
message: string;
|
|
586
|
-
recoverable?: boolean;
|
|
587
|
-
recovery?: RecoveryInfo;
|
|
588
|
-
details?: Record<string, unknown>;
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
/** Error chunk during streaming */
|
|
592
|
-
export interface ErrorChunk {
|
|
593
|
-
ok: false;
|
|
594
|
-
streaming: true;
|
|
595
|
-
session_id?: string;
|
|
596
|
-
error: ErrorWithRecovery;
|
|
597
|
-
partial_data?: unknown;
|
|
598
|
-
}
|
|
599
|
-
|
|
600
|
-
/** Union of all streaming chunk types */
|
|
601
|
-
export type StreamingChunk =
|
|
602
|
-
| MetaChunk
|
|
603
|
-
| DeltaChunk
|
|
604
|
-
| SnapshotChunk
|
|
605
|
-
| ProgressChunk
|
|
606
|
-
| FinalChunk
|
|
607
|
-
| ErrorChunk;
|
|
608
|
-
|
|
609
|
-
/** Streaming session state */
|
|
610
|
-
export interface StreamingSession {
|
|
611
|
-
session_id: string;
|
|
612
|
-
module_name: string;
|
|
613
|
-
started_at: number;
|
|
614
|
-
chunks_sent: number;
|
|
615
|
-
accumulated_data: Record<string, unknown>;
|
|
616
|
-
accumulated_text: Record<string, string>;
|
|
617
|
-
last_checkpoint?: Checkpoint;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
// =============================================================================
|
|
621
|
-
// v2.5 Response Mode Negotiation
|
|
622
|
-
// =============================================================================
|
|
623
|
-
|
|
624
|
-
/** Request options for response mode negotiation */
|
|
625
|
-
export interface RequestOptions {
|
|
626
|
-
response_mode?: ResponseMode;
|
|
627
|
-
chunk_type?: ChunkType;
|
|
628
|
-
}
|
|
629
|
-
|
|
630
|
-
/** Recovery context for stream retry */
|
|
631
|
-
export interface RecoveryContext {
|
|
632
|
-
session_id: string;
|
|
633
|
-
last_seq: number;
|
|
634
|
-
last_checkpoint?: Checkpoint;
|
|
635
|
-
}
|
|
636
|
-
|
|
637
|
-
/** Warning in response (for fallback scenarios) */
|
|
638
|
-
export interface ResponseWarning {
|
|
639
|
-
code: string;
|
|
640
|
-
message: string;
|
|
641
|
-
fallback_used?: string;
|
|
642
|
-
}
|
|
643
|
-
|
|
644
|
-
/** Execute options with negotiation support */
|
|
645
|
-
export interface ExecuteOptionsV25 {
|
|
646
|
-
input: Record<string, unknown>;
|
|
647
|
-
_options?: RequestOptions;
|
|
648
|
-
_recovery?: RecoveryContext;
|
|
649
|
-
}
|
|
650
|
-
|
|
651
|
-
/** Negotiation result */
|
|
652
|
-
export interface NegotiationResult {
|
|
653
|
-
mode: ResponseMode;
|
|
654
|
-
reason: 'header' | 'body_option' | 'query_param' | 'accept_header' | 'module_default';
|
|
655
|
-
warnings?: ResponseWarning[];
|
|
656
|
-
}
|
|
657
|
-
|
|
658
|
-
// =============================================================================
|
|
659
|
-
// v2.5 Multimodal Types
|
|
660
|
-
// =============================================================================
|
|
661
|
-
|
|
662
|
-
/** Supported modality types */
|
|
663
|
-
export type ModalityType = 'text' | 'image' | 'audio' | 'video' | 'document';
|
|
664
|
-
|
|
665
|
-
/** Modalities configuration in module.yaml */
|
|
666
|
-
export interface ModalitiesConfig {
|
|
667
|
-
input: ModalityType[];
|
|
668
|
-
output: ModalityType[];
|
|
669
|
-
constraints?: MediaConstraints;
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
/** Media size/duration constraints */
|
|
673
|
-
export interface MediaConstraints {
|
|
674
|
-
max_image_size_mb?: number;
|
|
675
|
-
max_audio_size_mb?: number;
|
|
676
|
-
max_video_size_mb?: number;
|
|
677
|
-
max_audio_duration_s?: number;
|
|
678
|
-
max_video_duration_s?: number;
|
|
679
|
-
allowed_image_types?: string[];
|
|
680
|
-
allowed_audio_types?: string[];
|
|
681
|
-
allowed_video_types?: string[];
|
|
682
|
-
}
|
|
683
|
-
|
|
684
|
-
/** Media input - URL reference */
|
|
685
|
-
export interface UrlMediaInput {
|
|
686
|
-
type: 'url';
|
|
687
|
-
url: string;
|
|
688
|
-
media_type?: string;
|
|
689
|
-
}
|
|
690
|
-
|
|
691
|
-
/** Media input - Base64 inline */
|
|
692
|
-
export interface Base64MediaInput {
|
|
693
|
-
type: 'base64';
|
|
694
|
-
media_type: string;
|
|
695
|
-
data: string;
|
|
696
|
-
}
|
|
697
|
-
|
|
698
|
-
/** Media input - File path */
|
|
699
|
-
export interface FileMediaInput {
|
|
700
|
-
type: 'file';
|
|
701
|
-
path: string;
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
/** Media input - Upload reference (for pre-uploaded files) */
|
|
705
|
-
export interface UploadRefMediaInput {
|
|
706
|
-
type: 'upload_ref';
|
|
707
|
-
upload_id: string;
|
|
708
|
-
media_type?: string;
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
/** Union of media input types */
|
|
712
|
-
export type MediaInput = UrlMediaInput | Base64MediaInput | FileMediaInput | UploadRefMediaInput;
|
|
713
|
-
|
|
714
|
-
/** Checksum for media integrity */
|
|
715
|
-
export interface MediaChecksum {
|
|
716
|
-
algorithm: 'sha256' | 'md5' | 'crc32';
|
|
717
|
-
value: string;
|
|
718
|
-
}
|
|
719
|
-
|
|
720
|
-
/** Media validation result */
|
|
721
|
-
export interface MediaValidationResult {
|
|
722
|
-
index: number;
|
|
723
|
-
media_type: string;
|
|
724
|
-
size_bytes: number;
|
|
725
|
-
dimensions?: {
|
|
726
|
-
width: number;
|
|
727
|
-
height: number;
|
|
728
|
-
};
|
|
729
|
-
duration_ms?: number;
|
|
730
|
-
valid: boolean;
|
|
731
|
-
errors?: string[];
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
/** Media validation summary in meta */
|
|
735
|
-
export interface MediaValidationSummary {
|
|
736
|
-
input_count: number;
|
|
737
|
-
validated: MediaValidationResult[];
|
|
738
|
-
}
|
|
739
|
-
|
|
740
|
-
/** Magic bytes for media type detection */
|
|
741
|
-
export const MEDIA_MAGIC_BYTES: Record<string, string[]> = {
|
|
742
|
-
'image/jpeg': ['ffd8ff'],
|
|
743
|
-
'image/png': ['89504e470d0a1a0a'],
|
|
744
|
-
'image/gif': ['47494638'],
|
|
745
|
-
'image/webp': ['52494646'],
|
|
746
|
-
'audio/mpeg': ['fffb', 'fffa', '494433'],
|
|
747
|
-
'audio/wav': ['52494646'],
|
|
748
|
-
'audio/ogg': ['4f676753'],
|
|
749
|
-
'video/mp4': ['0000001866747970', '0000002066747970'],
|
|
750
|
-
'video/webm': ['1a45dfa3'],
|
|
751
|
-
'application/pdf': ['25504446'],
|
|
752
|
-
};
|
|
753
|
-
|
|
754
|
-
/** Media size limits in bytes */
|
|
755
|
-
export const MEDIA_SIZE_LIMITS: Record<string, number> = {
|
|
756
|
-
'image': 20 * 1024 * 1024, // 20MB
|
|
757
|
-
'audio': 25 * 1024 * 1024, // 25MB
|
|
758
|
-
'video': 100 * 1024 * 1024, // 100MB
|
|
759
|
-
'document': 50 * 1024 * 1024, // 50MB
|
|
760
|
-
};
|
|
761
|
-
|
|
762
|
-
/** Media dimension limits */
|
|
763
|
-
export const MEDIA_DIMENSION_LIMITS = {
|
|
764
|
-
max_width: 8192,
|
|
765
|
-
max_height: 8192,
|
|
766
|
-
min_width: 10,
|
|
767
|
-
min_height: 10,
|
|
768
|
-
max_pixels: 67108864, // 8192 x 8192
|
|
769
|
-
};
|
|
770
|
-
|
|
771
|
-
/** Media output with metadata */
|
|
772
|
-
export interface MediaOutput {
|
|
773
|
-
type: 'url' | 'base64' | 'file';
|
|
774
|
-
media_type: string;
|
|
775
|
-
url?: string;
|
|
776
|
-
data?: string;
|
|
777
|
-
path?: string;
|
|
778
|
-
width?: number;
|
|
779
|
-
height?: number;
|
|
780
|
-
duration_ms?: number;
|
|
781
|
-
expires_at?: string;
|
|
782
|
-
generation_params?: Record<string, unknown>;
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
/** Supported image MIME types */
|
|
786
|
-
export const SUPPORTED_IMAGE_TYPES = [
|
|
787
|
-
'image/jpeg',
|
|
788
|
-
'image/png',
|
|
789
|
-
'image/webp',
|
|
790
|
-
'image/gif'
|
|
791
|
-
] as const;
|
|
792
|
-
|
|
793
|
-
/** Supported audio MIME types */
|
|
794
|
-
export const SUPPORTED_AUDIO_TYPES = [
|
|
795
|
-
'audio/mpeg',
|
|
796
|
-
'audio/wav',
|
|
797
|
-
'audio/ogg',
|
|
798
|
-
'audio/webm'
|
|
799
|
-
] as const;
|
|
800
|
-
|
|
801
|
-
/** Supported video MIME types */
|
|
802
|
-
export const SUPPORTED_VIDEO_TYPES = [
|
|
803
|
-
'video/mp4',
|
|
804
|
-
'video/webm',
|
|
805
|
-
'video/quicktime'
|
|
806
|
-
] as const;
|
|
807
|
-
|
|
808
|
-
// =============================================================================
|
|
809
|
-
// v2.5 Error Codes
|
|
810
|
-
// =============================================================================
|
|
811
|
-
|
|
812
|
-
/** v2.5 Error codes for streaming and multimodal */
|
|
813
|
-
export const ErrorCodesV25 = {
|
|
814
|
-
// Media errors (E1xxx)
|
|
815
|
-
UNSUPPORTED_MEDIA_TYPE: 'E1010',
|
|
816
|
-
MEDIA_TOO_LARGE: 'E1011',
|
|
817
|
-
MEDIA_FETCH_FAILED: 'E1012',
|
|
818
|
-
MEDIA_DECODE_FAILED: 'E1013',
|
|
819
|
-
MEDIA_TYPE_MISMATCH: 'E1014',
|
|
820
|
-
MEDIA_DIMENSION_EXCEEDED: 'E1015',
|
|
821
|
-
MEDIA_DIMENSION_TOO_SMALL: 'E1016',
|
|
822
|
-
MEDIA_PIXEL_LIMIT: 'E1017',
|
|
823
|
-
UPLOAD_EXPIRED: 'E1018',
|
|
824
|
-
UPLOAD_NOT_FOUND: 'E1019',
|
|
825
|
-
CHECKSUM_MISMATCH: 'E1020',
|
|
826
|
-
|
|
827
|
-
// Streaming errors (E2xxx)
|
|
828
|
-
STREAM_INTERRUPTED: 'E2010',
|
|
829
|
-
STREAM_TIMEOUT: 'E2011',
|
|
830
|
-
|
|
831
|
-
// Capability errors (E4xxx)
|
|
832
|
-
STREAMING_NOT_SUPPORTED: 'E4010',
|
|
833
|
-
MULTIMODAL_NOT_SUPPORTED: 'E4011',
|
|
834
|
-
RECOVERY_NOT_SUPPORTED: 'E4012',
|
|
835
|
-
SESSION_EXPIRED: 'E4013',
|
|
836
|
-
CHECKPOINT_INVALID: 'E4014',
|
|
837
|
-
} as const;
|
|
838
|
-
|
|
839
|
-
export type ErrorCodeV25 = typeof ErrorCodesV25[keyof typeof ErrorCodesV25];
|
|
840
|
-
|
|
841
|
-
// =============================================================================
|
|
842
|
-
// v2.5 Runtime Capabilities
|
|
843
|
-
// =============================================================================
|
|
844
|
-
|
|
845
|
-
/** Runtime capability declaration */
|
|
846
|
-
export interface RuntimeCapabilities {
|
|
847
|
-
streaming: boolean;
|
|
848
|
-
multimodal: {
|
|
849
|
-
input: ModalityType[];
|
|
850
|
-
output: ModalityType[];
|
|
851
|
-
};
|
|
852
|
-
max_media_size_mb: number;
|
|
853
|
-
supported_transports: ('sse' | 'websocket' | 'ndjson')[];
|
|
854
|
-
}
|
|
855
|
-
|
|
856
|
-
/** Default runtime capabilities */
|
|
857
|
-
export const DEFAULT_RUNTIME_CAPABILITIES: RuntimeCapabilities = {
|
|
858
|
-
streaming: true,
|
|
859
|
-
multimodal: {
|
|
860
|
-
input: ['text', 'image'],
|
|
861
|
-
output: ['text']
|
|
862
|
-
},
|
|
863
|
-
max_media_size_mb: 20,
|
|
864
|
-
supported_transports: ['sse', 'ndjson']
|
|
865
|
-
};
|
|
866
|
-
|
|
867
|
-
// =============================================================================
|
|
868
|
-
// v2.5 Extended Provider Interface
|
|
869
|
-
// =============================================================================
|
|
870
|
-
|
|
871
|
-
/** Extended invoke params with streaming support */
|
|
872
|
-
export interface InvokeParamsV25 extends InvokeParams {
|
|
873
|
-
stream?: boolean;
|
|
874
|
-
images?: MediaInput[];
|
|
875
|
-
audio?: MediaInput[];
|
|
876
|
-
video?: MediaInput[];
|
|
877
|
-
}
|
|
878
|
-
|
|
879
|
-
/** Streaming invoke result */
|
|
880
|
-
export interface StreamingInvokeResult {
|
|
881
|
-
stream: AsyncIterable<string>;
|
|
882
|
-
usage?: {
|
|
883
|
-
promptTokens: number;
|
|
884
|
-
completionTokens: number;
|
|
885
|
-
totalTokens: number;
|
|
886
|
-
};
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
/** Extended provider interface for v2.5 */
|
|
890
|
-
export interface ProviderV25 extends Provider {
|
|
891
|
-
/** Check if provider supports streaming */
|
|
892
|
-
supportsStreaming?(): boolean;
|
|
893
|
-
|
|
894
|
-
/** Check if provider supports multimodal input */
|
|
895
|
-
supportsMultimodal?(): { input: ModalityType[]; output: ModalityType[] };
|
|
896
|
-
|
|
897
|
-
/** Invoke with streaming */
|
|
898
|
-
invokeStream?(params: InvokeParamsV25): Promise<StreamingInvokeResult>;
|
|
899
|
-
}
|
|
900
|
-
|
|
901
|
-
/** Type guard for v2.5 provider */
|
|
902
|
-
export function isProviderV25(provider: Provider): provider is ProviderV25 {
|
|
903
|
-
return 'invokeStream' in provider || 'supportsStreaming' in provider;
|
|
904
|
-
}
|
|
905
|
-
|
|
906
|
-
// =============================================================================
|
|
907
|
-
// v2.5 Module Configuration Extensions
|
|
908
|
-
// =============================================================================
|
|
909
|
-
|
|
910
|
-
/** Extended module interface for v2.5 */
|
|
911
|
-
export interface CognitiveModuleV25 extends CognitiveModule {
|
|
912
|
-
/** v2.5: Response configuration */
|
|
913
|
-
response?: ResponseConfig;
|
|
914
|
-
|
|
915
|
-
/** v2.5: Modalities configuration */
|
|
916
|
-
modalities?: ModalitiesConfig;
|
|
917
|
-
}
|
|
918
|
-
|
|
919
|
-
/** Type guard for v2.5 module */
|
|
920
|
-
export function isModuleV25(module: CognitiveModule): module is CognitiveModuleV25 {
|
|
921
|
-
return 'response' in module || 'modalities' in module;
|
|
922
|
-
}
|
|
923
|
-
|
|
924
|
-
/** Check if module supports streaming */
|
|
925
|
-
export function moduleSupportsStreaming(module: CognitiveModule): boolean {
|
|
926
|
-
if (!isModuleV25(module)) return false;
|
|
927
|
-
const mode = module.response?.mode;
|
|
928
|
-
return mode === 'streaming' || mode === 'both';
|
|
929
|
-
}
|
|
930
|
-
|
|
931
|
-
/** Check if module supports multimodal input */
|
|
932
|
-
export function moduleSupportsMultimodal(module: CognitiveModule): boolean {
|
|
933
|
-
if (!isModuleV25(module)) return false;
|
|
934
|
-
const modalities = module.modalities?.input ?? ['text'];
|
|
935
|
-
return modalities.some(m => m !== 'text');
|
|
936
|
-
}
|
|
937
|
-
|
|
938
|
-
/** Get supported input modalities for module */
|
|
939
|
-
export function getModuleInputModalities(module: CognitiveModule): ModalityType[] {
|
|
940
|
-
if (!isModuleV25(module)) return ['text'];
|
|
941
|
-
return module.modalities?.input ?? ['text'];
|
|
942
|
-
}
|
|
943
|
-
|
|
944
|
-
/** Get supported output modalities for module */
|
|
945
|
-
export function getModuleOutputModalities(module: CognitiveModule): ModalityType[] {
|
|
946
|
-
if (!isModuleV25(module)) return ['text'];
|
|
947
|
-
return module.modalities?.output ?? ['text'];
|
|
948
|
-
}
|