@m6d/cortex 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/README.md CHANGED
@@ -89,7 +89,11 @@ export const datasets = defineDatasetHost({
89
89
  app.route("/datasets", datasets);
90
90
  ```
91
91
 
92
- Register the host on the console's **Datasets** page with that URL. Every query arrives validated against the manifest; the host runs it under the user `resolveUser` returns.
92
+ Register the host on the console's **Datasets** page with that URL. The host checks every query against its own manifest, as Cortex does, and runs it under the user `resolveUser` returns. A body past 256 KiB is refused before it is read, one nested past 32 levels as malformed, and a drizzle dataset refuses a query whose SQL would be written out past 50,000 parts before it reaches the database.
93
+
94
+ `values: { status: ["pending", "shipped", "cancelled"] }` lists every value a string column can hold: the model reads them, and a query comparing the column to any other value is refused. A column drizzle knows as an enum (a `pgEnum`, or `text({ enum })`) lists its own values.
95
+
96
+ A query can carry the IANA time zone of the user it runs for, and `run` gets it as `context.timeZone`. A drizzle dataset reads its timestamps on that zone's clock, at the zone's offset now. A bare date in a filter is the user's midnight. A month or weekday is the user's. A timestamp answers with the offset, as in `2024-03-31T14:00:00-10:00`. A calendar date, a `datetimeoffset`, a timestamp read as text (`mode: "string"`) and a date kept as text stay as stored.
93
97
 
94
98
  ## Errors
95
99
 
@@ -136,7 +140,7 @@ export default defineManifest({
136
140
  {
137
141
  slug: "support",
138
142
  name: "Support",
139
- server: "Orders backend", // registered in the console
143
+ server: "Orders backend", // registered by `cortex setup` or in the console
140
144
  prompt: "You help customers with their orders.",
141
145
  auth: { kind: "server" },
142
146
  },
package/dist/drizzle.d.ts CHANGED
@@ -22,6 +22,7 @@ declare const datasetDefinitionSchema: z.ZodObject<{
22
22
  date: "date";
23
23
  }>;
24
24
  description: z.ZodDefault<z.ZodString>;
25
+ values: z.ZodOptional<z.ZodArray<z.ZodString>>;
25
26
  }, z.core.$strip>>;
26
27
  relations: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodObject<{
27
28
  dataset: z.ZodString;
@@ -134,7 +135,8 @@ export type DatasetCondition = {
134
135
  * A condition, a group of filters that must all hold (`all`) or of which one
135
136
  * must hold (`any`), or the negation of one (`not`, with SQL's null
136
137
  * semantics: a row whose comparison is unknown is left out either way). A
137
- * filter list is an `all`.
138
+ * filter list is an `all`. An object carrying `col` is a condition, whatever
139
+ * else it carries; any other is an `any`, else an `all`, else a `not` group.
138
140
  */
139
141
  export type DatasetFilter = DatasetCondition | {
140
142
  any: DatasetFilter[];
@@ -578,21 +580,470 @@ declare const datasetQuerySchema: z.ZodPipe<z.ZodCustom<{
578
580
  desc: "desc";
579
581
  }>;
580
582
  }, z.core.$strip>>>;
581
- limit: z.ZodOptional<z.ZodNumber>;
582
- offset: z.ZodOptional<z.ZodNumber>;
583
+ limit: z.ZodOptional<z.ZodInt32>;
584
+ offset: z.ZodOptional<z.ZodInt32>;
583
585
  }, z.core.$strip>>;
584
586
  export type DatasetQuery = z.infer<typeof datasetQuerySchema>;
587
+ declare const datasetRequestSchema: z.ZodObject<{
588
+ query: z.ZodPipe<z.ZodCustom<{
589
+ from: string;
590
+ join?: string[] | undefined;
591
+ select?: (string | {
592
+ expr: DatasetOperand;
593
+ as: string;
594
+ })[] | undefined;
595
+ filter?: DatasetFilter[] | undefined;
596
+ groupBy?: (string | {
597
+ expr: DatasetOperand;
598
+ as: string;
599
+ })[] | undefined;
600
+ aggregate?: {
601
+ fn: "count" | "countDistinct" | "sum" | "avg" | "min" | "max";
602
+ as: string;
603
+ col?: string | {
604
+ value: string | boolean;
605
+ } | {
606
+ op: DatasetArithmeticOp;
607
+ left: DatasetOperand;
608
+ right: DatasetOperand;
609
+ } | {
610
+ fn: "dateTrunc";
611
+ unit: DatasetBucket;
612
+ of: DatasetOperand;
613
+ } | {
614
+ fn: "datePart";
615
+ unit: DatasetDatePart;
616
+ of: DatasetOperand;
617
+ } | {
618
+ fn: DatasetFunction;
619
+ args: DatasetOperand[];
620
+ } | {
621
+ case: {
622
+ when: DatasetFilter;
623
+ then: DatasetOperand;
624
+ }[];
625
+ else?: DatasetOperand;
626
+ } | undefined;
627
+ filter?: DatasetFilter[] | undefined;
628
+ }[] | undefined;
629
+ window?: {
630
+ fn: "count" | "countDistinct" | "sum" | "avg" | "min" | "max";
631
+ as: string;
632
+ col?: string | {
633
+ value: string | boolean;
634
+ } | {
635
+ op: DatasetArithmeticOp;
636
+ left: DatasetOperand;
637
+ right: DatasetOperand;
638
+ } | {
639
+ fn: "dateTrunc";
640
+ unit: DatasetBucket;
641
+ of: DatasetOperand;
642
+ } | {
643
+ fn: "datePart";
644
+ unit: DatasetDatePart;
645
+ of: DatasetOperand;
646
+ } | {
647
+ fn: DatasetFunction;
648
+ args: DatasetOperand[];
649
+ } | {
650
+ case: {
651
+ when: DatasetFilter;
652
+ then: DatasetOperand;
653
+ }[];
654
+ else?: DatasetOperand;
655
+ } | undefined;
656
+ filter?: DatasetFilter[] | undefined;
657
+ partitionBy?: string[] | undefined;
658
+ }[] | undefined;
659
+ compute?: {
660
+ expr: DatasetOperand;
661
+ as: string;
662
+ }[] | undefined;
663
+ having?: DatasetFilter[] | undefined;
664
+ qualify?: DatasetFilter[] | undefined;
665
+ rank?: {
666
+ partitionBy: string[];
667
+ sort: {
668
+ col: string;
669
+ dir: "asc" | "desc";
670
+ }[];
671
+ limit: number;
672
+ } | undefined;
673
+ sort?: {
674
+ col: string;
675
+ dir: "asc" | "desc";
676
+ }[] | undefined;
677
+ limit?: number | undefined;
678
+ offset?: number | undefined;
679
+ }, {
680
+ from: string;
681
+ join?: string[] | undefined;
682
+ select?: (string | {
683
+ expr: DatasetOperand;
684
+ as: string;
685
+ })[] | undefined;
686
+ filter?: DatasetFilter[] | undefined;
687
+ groupBy?: (string | {
688
+ expr: DatasetOperand;
689
+ as: string;
690
+ })[] | undefined;
691
+ aggregate?: {
692
+ fn: "count" | "countDistinct" | "sum" | "avg" | "min" | "max";
693
+ as: string;
694
+ col?: string | {
695
+ value: string | boolean;
696
+ } | {
697
+ op: DatasetArithmeticOp;
698
+ left: DatasetOperand;
699
+ right: DatasetOperand;
700
+ } | {
701
+ fn: "dateTrunc";
702
+ unit: DatasetBucket;
703
+ of: DatasetOperand;
704
+ } | {
705
+ fn: "datePart";
706
+ unit: DatasetDatePart;
707
+ of: DatasetOperand;
708
+ } | {
709
+ fn: DatasetFunction;
710
+ args: DatasetOperand[];
711
+ } | {
712
+ case: {
713
+ when: DatasetFilter;
714
+ then: DatasetOperand;
715
+ }[];
716
+ else?: DatasetOperand;
717
+ } | undefined;
718
+ filter?: DatasetFilter[] | undefined;
719
+ }[] | undefined;
720
+ window?: {
721
+ fn: "count" | "countDistinct" | "sum" | "avg" | "min" | "max";
722
+ as: string;
723
+ col?: string | {
724
+ value: string | boolean;
725
+ } | {
726
+ op: DatasetArithmeticOp;
727
+ left: DatasetOperand;
728
+ right: DatasetOperand;
729
+ } | {
730
+ fn: "dateTrunc";
731
+ unit: DatasetBucket;
732
+ of: DatasetOperand;
733
+ } | {
734
+ fn: "datePart";
735
+ unit: DatasetDatePart;
736
+ of: DatasetOperand;
737
+ } | {
738
+ fn: DatasetFunction;
739
+ args: DatasetOperand[];
740
+ } | {
741
+ case: {
742
+ when: DatasetFilter;
743
+ then: DatasetOperand;
744
+ }[];
745
+ else?: DatasetOperand;
746
+ } | undefined;
747
+ filter?: DatasetFilter[] | undefined;
748
+ partitionBy?: string[] | undefined;
749
+ }[] | undefined;
750
+ compute?: {
751
+ expr: DatasetOperand;
752
+ as: string;
753
+ }[] | undefined;
754
+ having?: DatasetFilter[] | undefined;
755
+ qualify?: DatasetFilter[] | undefined;
756
+ rank?: {
757
+ partitionBy: string[];
758
+ sort: {
759
+ col: string;
760
+ dir: "asc" | "desc";
761
+ }[];
762
+ limit: number;
763
+ } | undefined;
764
+ sort?: {
765
+ col: string;
766
+ dir: "asc" | "desc";
767
+ }[] | undefined;
768
+ limit?: number | undefined;
769
+ offset?: number | undefined;
770
+ }>, z.ZodObject<{
771
+ from: z.ZodString;
772
+ join: z.ZodDefault<z.ZodArray<z.ZodString>>;
773
+ select: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [
774
+ z.ZodString,
775
+ z.ZodObject<{
776
+ expr: z.ZodType<DatasetOperand, DatasetOperand, z.core.$ZodTypeInternals<DatasetOperand, DatasetOperand>>;
777
+ as: z.ZodString;
778
+ }, z.core.$strip>
779
+ ]>>>;
780
+ filter: z.ZodDefault<z.ZodArray<z.ZodType<DatasetFilter, DatasetFilter, z.core.$ZodTypeInternals<DatasetFilter, DatasetFilter>>>>;
781
+ groupBy: z.ZodDefault<z.ZodArray<z.ZodUnion<readonly [
782
+ z.ZodString,
783
+ z.ZodObject<{
784
+ expr: z.ZodType<DatasetOperand, DatasetOperand, z.core.$ZodTypeInternals<DatasetOperand, DatasetOperand>>;
785
+ as: z.ZodString;
786
+ }, z.core.$strip>
787
+ ]>>>;
788
+ aggregate: z.ZodDefault<z.ZodArray<z.ZodObject<{
789
+ fn: z.ZodEnum<{
790
+ count: "count";
791
+ countDistinct: "countDistinct";
792
+ sum: "sum";
793
+ avg: "avg";
794
+ min: "min";
795
+ max: "max";
796
+ }>;
797
+ col: z.ZodOptional<z.ZodType<string | {
798
+ value: string | boolean;
799
+ } | {
800
+ op: DatasetArithmeticOp;
801
+ left: DatasetOperand;
802
+ right: DatasetOperand;
803
+ } | {
804
+ fn: "dateTrunc";
805
+ unit: DatasetBucket;
806
+ of: DatasetOperand;
807
+ } | {
808
+ fn: "datePart";
809
+ unit: DatasetDatePart;
810
+ of: DatasetOperand;
811
+ } | {
812
+ fn: DatasetFunction;
813
+ args: DatasetOperand[];
814
+ } | {
815
+ case: {
816
+ when: DatasetFilter;
817
+ then: DatasetOperand;
818
+ }[];
819
+ else?: DatasetOperand;
820
+ }, string | {
821
+ value: string | boolean;
822
+ } | {
823
+ op: DatasetArithmeticOp;
824
+ left: DatasetOperand;
825
+ right: DatasetOperand;
826
+ } | {
827
+ fn: "dateTrunc";
828
+ unit: DatasetBucket;
829
+ of: DatasetOperand;
830
+ } | {
831
+ fn: "datePart";
832
+ unit: DatasetDatePart;
833
+ of: DatasetOperand;
834
+ } | {
835
+ fn: DatasetFunction;
836
+ args: DatasetOperand[];
837
+ } | {
838
+ case: {
839
+ when: DatasetFilter;
840
+ then: DatasetOperand;
841
+ }[];
842
+ else?: DatasetOperand;
843
+ }, z.core.$ZodTypeInternals<string | {
844
+ value: string | boolean;
845
+ } | {
846
+ op: DatasetArithmeticOp;
847
+ left: DatasetOperand;
848
+ right: DatasetOperand;
849
+ } | {
850
+ fn: "dateTrunc";
851
+ unit: DatasetBucket;
852
+ of: DatasetOperand;
853
+ } | {
854
+ fn: "datePart";
855
+ unit: DatasetDatePart;
856
+ of: DatasetOperand;
857
+ } | {
858
+ fn: DatasetFunction;
859
+ args: DatasetOperand[];
860
+ } | {
861
+ case: {
862
+ when: DatasetFilter;
863
+ then: DatasetOperand;
864
+ }[];
865
+ else?: DatasetOperand;
866
+ }, string | {
867
+ value: string | boolean;
868
+ } | {
869
+ op: DatasetArithmeticOp;
870
+ left: DatasetOperand;
871
+ right: DatasetOperand;
872
+ } | {
873
+ fn: "dateTrunc";
874
+ unit: DatasetBucket;
875
+ of: DatasetOperand;
876
+ } | {
877
+ fn: "datePart";
878
+ unit: DatasetDatePart;
879
+ of: DatasetOperand;
880
+ } | {
881
+ fn: DatasetFunction;
882
+ args: DatasetOperand[];
883
+ } | {
884
+ case: {
885
+ when: DatasetFilter;
886
+ then: DatasetOperand;
887
+ }[];
888
+ else?: DatasetOperand;
889
+ }>>>;
890
+ as: z.ZodString;
891
+ filter: z.ZodDefault<z.ZodArray<z.ZodType<DatasetFilter, DatasetFilter, z.core.$ZodTypeInternals<DatasetFilter, DatasetFilter>>>>;
892
+ }, z.core.$strip>>>;
893
+ window: z.ZodDefault<z.ZodArray<z.ZodObject<{
894
+ fn: z.ZodEnum<{
895
+ count: "count";
896
+ countDistinct: "countDistinct";
897
+ sum: "sum";
898
+ avg: "avg";
899
+ min: "min";
900
+ max: "max";
901
+ }>;
902
+ col: z.ZodOptional<z.ZodType<string | {
903
+ value: string | boolean;
904
+ } | {
905
+ op: DatasetArithmeticOp;
906
+ left: DatasetOperand;
907
+ right: DatasetOperand;
908
+ } | {
909
+ fn: "dateTrunc";
910
+ unit: DatasetBucket;
911
+ of: DatasetOperand;
912
+ } | {
913
+ fn: "datePart";
914
+ unit: DatasetDatePart;
915
+ of: DatasetOperand;
916
+ } | {
917
+ fn: DatasetFunction;
918
+ args: DatasetOperand[];
919
+ } | {
920
+ case: {
921
+ when: DatasetFilter;
922
+ then: DatasetOperand;
923
+ }[];
924
+ else?: DatasetOperand;
925
+ }, string | {
926
+ value: string | boolean;
927
+ } | {
928
+ op: DatasetArithmeticOp;
929
+ left: DatasetOperand;
930
+ right: DatasetOperand;
931
+ } | {
932
+ fn: "dateTrunc";
933
+ unit: DatasetBucket;
934
+ of: DatasetOperand;
935
+ } | {
936
+ fn: "datePart";
937
+ unit: DatasetDatePart;
938
+ of: DatasetOperand;
939
+ } | {
940
+ fn: DatasetFunction;
941
+ args: DatasetOperand[];
942
+ } | {
943
+ case: {
944
+ when: DatasetFilter;
945
+ then: DatasetOperand;
946
+ }[];
947
+ else?: DatasetOperand;
948
+ }, z.core.$ZodTypeInternals<string | {
949
+ value: string | boolean;
950
+ } | {
951
+ op: DatasetArithmeticOp;
952
+ left: DatasetOperand;
953
+ right: DatasetOperand;
954
+ } | {
955
+ fn: "dateTrunc";
956
+ unit: DatasetBucket;
957
+ of: DatasetOperand;
958
+ } | {
959
+ fn: "datePart";
960
+ unit: DatasetDatePart;
961
+ of: DatasetOperand;
962
+ } | {
963
+ fn: DatasetFunction;
964
+ args: DatasetOperand[];
965
+ } | {
966
+ case: {
967
+ when: DatasetFilter;
968
+ then: DatasetOperand;
969
+ }[];
970
+ else?: DatasetOperand;
971
+ }, string | {
972
+ value: string | boolean;
973
+ } | {
974
+ op: DatasetArithmeticOp;
975
+ left: DatasetOperand;
976
+ right: DatasetOperand;
977
+ } | {
978
+ fn: "dateTrunc";
979
+ unit: DatasetBucket;
980
+ of: DatasetOperand;
981
+ } | {
982
+ fn: "datePart";
983
+ unit: DatasetDatePart;
984
+ of: DatasetOperand;
985
+ } | {
986
+ fn: DatasetFunction;
987
+ args: DatasetOperand[];
988
+ } | {
989
+ case: {
990
+ when: DatasetFilter;
991
+ then: DatasetOperand;
992
+ }[];
993
+ else?: DatasetOperand;
994
+ }>>>;
995
+ as: z.ZodString;
996
+ filter: z.ZodDefault<z.ZodArray<z.ZodType<DatasetFilter, DatasetFilter, z.core.$ZodTypeInternals<DatasetFilter, DatasetFilter>>>>;
997
+ partitionBy: z.ZodDefault<z.ZodArray<z.ZodString>>;
998
+ }, z.core.$strip>>>;
999
+ compute: z.ZodDefault<z.ZodArray<z.ZodObject<{
1000
+ expr: z.ZodType<DatasetOperand, DatasetOperand, z.core.$ZodTypeInternals<DatasetOperand, DatasetOperand>>;
1001
+ as: z.ZodString;
1002
+ }, z.core.$strip>>>;
1003
+ having: z.ZodDefault<z.ZodArray<z.ZodType<DatasetFilter, DatasetFilter, z.core.$ZodTypeInternals<DatasetFilter, DatasetFilter>>>>;
1004
+ qualify: z.ZodDefault<z.ZodArray<z.ZodType<DatasetFilter, DatasetFilter, z.core.$ZodTypeInternals<DatasetFilter, DatasetFilter>>>>;
1005
+ rank: z.ZodOptional<z.ZodObject<{
1006
+ partitionBy: z.ZodArray<z.ZodString>;
1007
+ sort: z.ZodArray<z.ZodObject<{
1008
+ col: z.ZodString;
1009
+ dir: z.ZodEnum<{
1010
+ asc: "asc";
1011
+ desc: "desc";
1012
+ }>;
1013
+ }, z.core.$strip>>;
1014
+ limit: z.ZodNumber;
1015
+ }, z.core.$strip>>;
1016
+ sort: z.ZodDefault<z.ZodArray<z.ZodObject<{
1017
+ col: z.ZodString;
1018
+ dir: z.ZodEnum<{
1019
+ asc: "asc";
1020
+ desc: "desc";
1021
+ }>;
1022
+ }, z.core.$strip>>>;
1023
+ limit: z.ZodOptional<z.ZodInt32>;
1024
+ offset: z.ZodOptional<z.ZodInt32>;
1025
+ }, z.core.$strip>>;
1026
+ context: z.ZodObject<{
1027
+ threadId: z.ZodString;
1028
+ requestId: z.ZodOptional<z.ZodString>;
1029
+ timeZone: z.ZodOptional<z.ZodString>;
1030
+ }, z.core.$strip>;
1031
+ }, z.core.$strip>;
1032
+ export type DatasetRequest = z.infer<typeof datasetRequestSchema>;
585
1033
  /**
586
1034
  * One dataset the host exposes. `run` receives a query already validated
587
1035
  * against the host's own manifest, with `limit` set to one more than the
588
- * rows to return, so a full page means more matched.
1036
+ * rows to return, so a full page means more matched, and the request's
1037
+ * context: the thread and request it came from, and `timeZone`, the IANA
1038
+ * zone of the user the query runs for. A drizzle dataset reads its instants
1039
+ * on that zone's clock; a custom source may ignore it.
589
1040
  */
590
1041
  export type DatasetSource<TUser> = {
591
1042
  /** As authored: `description` and `maxRows` may be left out and take the contract's defaults. */
592
1043
  definition: DatasetDefinitionInput;
593
1044
  run(query: DatasetQuery & {
594
1045
  limit: number;
595
- }, user: TUser, signal: AbortSignal): Promise<ReadonlyArray<Record<string, unknown>>>;
1046
+ }, user: TUser, signal: AbortSignal, context: DatasetRequest["context"]): Promise<ReadonlyArray<Record<string, unknown>>>;
596
1047
  };
597
1048
  export type DrizzleDialect = "postgres" | "mssql" | "sqlite";
598
1049
  /**
@@ -648,6 +1099,11 @@ export type DrizzleDatasetOptions<TUser> = {
648
1099
  exclude?: readonly string[];
649
1100
  };
650
1101
  descriptions?: Record<string, string>;
1102
+ /**
1103
+ * Every value a string column can hold, which the agent reads and a query
1104
+ * may only compare it to; a column drizzle knows as an enum lists its own.
1105
+ */
1106
+ values?: Record<string, readonly string[]>;
651
1107
  /** The dataset type of a column the table cannot describe, such as a date kept as ISO text. */
652
1108
  types?: Record<string, DatasetColumnType>;
653
1109
  /** Read late, so datasets may refer to each other and to themselves. */