diff --git a/Makefile b/Makefile index 9f880a2..e74ffd5 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,9 @@ setup: go run . setup + +# Example: go run -mod=mod entgo.io/ent/cmd/ent new User ent: - # go run -mod=mod entgo.io/ent/cmd/ent new User go generate ./ent grpc-web: grpcwebproxy --backend_addr=localhost:8081 --run_http_server --run_tls_server=false --server_http_debug_port 18081 --allow_all_origins --server_bind_address 127.0.0.1 diff --git a/api/rag/document.pb.go b/api/rag/document.pb.go index fc0d877..40613f1 100644 --- a/api/rag/document.pb.go +++ b/api/rag/document.pb.go @@ -132,6 +132,100 @@ func (x *CreateDocumentResponse) GetDocument() *Document { return nil } +type ListDocumentRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + LibraryId string `protobuf:"bytes,1,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` +} + +func (x *ListDocumentRequest) Reset() { + *x = ListDocumentRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_api_rag_document_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListDocumentRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListDocumentRequest) ProtoMessage() {} + +func (x *ListDocumentRequest) ProtoReflect() protoreflect.Message { + mi := &file_api_rag_document_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ListDocumentRequest.ProtoReflect.Descriptor instead. +func (*ListDocumentRequest) Descriptor() ([]byte, []int) { + return file_api_rag_document_proto_rawDescGZIP(), []int{2} +} + +func (x *ListDocumentRequest) GetLibraryId() string { + if x != nil { + return x.LibraryId + } + return "" +} + +type ListDocumentResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Documents []*Document `protobuf:"bytes,1,rep,name=documents,proto3" json:"documents,omitempty"` +} + +func (x *ListDocumentResponse) Reset() { + *x = ListDocumentResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_api_rag_document_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *ListDocumentResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ListDocumentResponse) ProtoMessage() {} + +func (x *ListDocumentResponse) ProtoReflect() protoreflect.Message { + mi := &file_api_rag_document_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ListDocumentResponse.ProtoReflect.Descriptor instead. +func (*ListDocumentResponse) Descriptor() ([]byte, []int) { + return file_api_rag_document_proto_rawDescGZIP(), []int{3} +} + +func (x *ListDocumentResponse) GetDocuments() []*Document { + if x != nil { + return x.Documents + } + return nil +} + type Document struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -141,14 +235,15 @@ type Document struct { Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` Content string `protobuf:"bytes,3,opt,name=content,proto3" json:"content,omitempty"` LibraryId string `protobuf:"bytes,4,opt,name=library_id,json=libraryId,proto3" json:"library_id,omitempty"` - CreatedAt string `protobuf:"bytes,5,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` - UpdatedAt string `protobuf:"bytes,6,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` + UserId string `protobuf:"bytes,5,opt,name=user_id,json=userId,proto3" json:"user_id,omitempty"` + CreatedAt string `protobuf:"bytes,6,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` + UpdatedAt string `protobuf:"bytes,7,opt,name=updated_at,json=updatedAt,proto3" json:"updated_at,omitempty"` } func (x *Document) Reset() { *x = Document{} if protoimpl.UnsafeEnabled { - mi := &file_api_rag_document_proto_msgTypes[2] + mi := &file_api_rag_document_proto_msgTypes[4] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -161,7 +256,7 @@ func (x *Document) String() string { func (*Document) ProtoMessage() {} func (x *Document) ProtoReflect() protoreflect.Message { - mi := &file_api_rag_document_proto_msgTypes[2] + mi := &file_api_rag_document_proto_msgTypes[4] if protoimpl.UnsafeEnabled && x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -174,7 +269,7 @@ func (x *Document) ProtoReflect() protoreflect.Message { // Deprecated: Use Document.ProtoReflect.Descriptor instead. func (*Document) Descriptor() ([]byte, []int) { - return file_api_rag_document_proto_rawDescGZIP(), []int{2} + return file_api_rag_document_proto_rawDescGZIP(), []int{4} } func (x *Document) GetId() int64 { @@ -205,6 +300,13 @@ func (x *Document) GetLibraryId() string { return "" } +func (x *Document) GetUserId() string { + if x != nil { + return x.UserId + } + return "" +} + func (x *Document) GetCreatedAt() string { if x != nil { return x.CreatedAt @@ -237,17 +339,26 @@ var file_api_rag_document_proto_rawDesc = []byte{ 0x65, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x25, 0x0a, 0x08, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x08, - 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0xa5, 0x01, 0x0a, 0x08, 0x44, 0x6f, 0x63, - 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, - 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, - 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, - 0x74, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, - 0x65, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, - 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, + 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x22, 0x34, 0x0a, 0x13, 0x4c, 0x69, 0x73, 0x74, + 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, + 0x1d, 0x0a, 0x0a, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x09, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x22, 0x3f, + 0x0a, 0x14, 0x4c, 0x69, 0x73, 0x74, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x27, 0x0a, 0x09, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, + 0x6e, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x09, 0x2e, 0x44, 0x6f, 0x63, 0x75, + 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x09, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x22, + 0xbe, 0x01, 0x0a, 0x08, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x0e, 0x0a, 0x02, + 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, + 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, + 0x12, 0x18, 0x0a, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x07, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x6c, 0x69, + 0x62, 0x72, 0x61, 0x72, 0x79, 0x5f, 0x69, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, + 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x49, 0x64, 0x12, 0x17, 0x0a, 0x07, 0x75, 0x73, 0x65, + 0x72, 0x5f, 0x69, 0x64, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x75, 0x73, 0x65, 0x72, 0x49, 0x64, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, - 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, + 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x1d, 0x0a, 0x0a, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, - 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, + 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x42, 0x1e, 0x5a, 0x1c, 0x6c, 0x65, 0x61, 0x66, 0x64, 0x65, 0x76, 0x2e, 0x74, 0x6f, 0x70, 0x2f, 0x6c, 0x65, 0x61, 0x66, 0x2f, 0x72, 0x61, 0x67, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x72, 0x61, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, @@ -265,19 +376,22 @@ func file_api_rag_document_proto_rawDescGZIP() []byte { return file_api_rag_document_proto_rawDescData } -var file_api_rag_document_proto_msgTypes = make([]protoimpl.MessageInfo, 3) +var file_api_rag_document_proto_msgTypes = make([]protoimpl.MessageInfo, 5) var file_api_rag_document_proto_goTypes = []interface{}{ (*CreateDocumentRequest)(nil), // 0: CreateDocumentRequest (*CreateDocumentResponse)(nil), // 1: CreateDocumentResponse - (*Document)(nil), // 2: Document + (*ListDocumentRequest)(nil), // 2: ListDocumentRequest + (*ListDocumentResponse)(nil), // 3: ListDocumentResponse + (*Document)(nil), // 4: Document } var file_api_rag_document_proto_depIdxs = []int32{ - 2, // 0: CreateDocumentResponse.document:type_name -> Document - 1, // [1:1] is the sub-list for method output_type - 1, // [1:1] is the sub-list for method input_type - 1, // [1:1] is the sub-list for extension type_name - 1, // [1:1] is the sub-list for extension extendee - 0, // [0:1] is the sub-list for field type_name + 4, // 0: CreateDocumentResponse.document:type_name -> Document + 4, // 1: ListDocumentResponse.documents:type_name -> Document + 2, // [2:2] is the sub-list for method output_type + 2, // [2:2] is the sub-list for method input_type + 2, // [2:2] is the sub-list for extension type_name + 2, // [2:2] is the sub-list for extension extendee + 0, // [0:2] is the sub-list for field type_name } func init() { file_api_rag_document_proto_init() } @@ -311,6 +425,30 @@ func file_api_rag_document_proto_init() { } } file_api_rag_document_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListDocumentRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_rag_document_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*ListDocumentResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_api_rag_document_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*Document); i { case 0: return &v.state @@ -329,7 +467,7 @@ func file_api_rag_document_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: file_api_rag_document_proto_rawDesc, NumEnums: 0, - NumMessages: 3, + NumMessages: 5, NumExtensions: 0, NumServices: 0, }, diff --git a/api/rag/document.proto b/api/rag/document.proto index d1c075a..0f31deb 100644 --- a/api/rag/document.proto +++ b/api/rag/document.proto @@ -6,7 +6,6 @@ import "google/api/httpbody.proto"; option go_package = "leafdev.top/leaf/rag/api/rag"; - message CreateDocumentRequest { string name = 1; string content = 2; @@ -17,11 +16,20 @@ message CreateDocumentResponse { Document document = 1; } +message ListDocumentRequest { + string library_id = 1; +} + +message ListDocumentResponse { + repeated Document documents = 1; +} + message Document { int64 id = 1; string name = 2; string content = 3; string library_id = 4; - string created_at = 5; - string updated_at = 6; + string user_id = 5; + string created_at = 6; + string updated_at = 7; } \ No newline at end of file diff --git a/api/rag/rag.pb.go b/api/rag/rag.pb.go index a93811f..f0e1d7f 100644 --- a/api/rag/rag.pb.go +++ b/api/rag/rag.pb.go @@ -32,46 +32,56 @@ var file_api_rag_rag_proto_rawDesc = []byte{ 0x64, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x16, 0x61, 0x70, 0x69, 0x2f, 0x72, 0x61, 0x67, 0x2f, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x15, 0x61, 0x70, 0x69, 0x2f, 0x72, 0x61, 0x67, 0x2f, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, - 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0x9a, 0x02, 0x0a, 0x0a, 0x52, 0x41, 0x47, 0x53, - 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x5c, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, + 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x32, 0xf2, 0x02, 0x0a, 0x0a, 0x52, 0x41, 0x47, 0x53, + 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x53, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x4c, 0x69, + 0x62, 0x72, 0x61, 0x72, 0x79, 0x12, 0x13, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4c, 0x69, 0x62, 0x72, + 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x4c, 0x69, 0x73, + 0x74, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x22, 0x19, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, + 0x31, 0x2f, 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x69, 0x65, 0x73, 0x12, 0x59, 0x0a, 0x0d, 0x43, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x12, 0x15, 0x2e, 0x43, + 0x72, 0x65, 0x61, 0x74, 0x65, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4c, 0x69, 0x62, 0x72, + 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x19, 0x82, 0xd3, 0xe4, + 0x93, 0x02, 0x13, 0x22, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x6c, 0x69, 0x62, + 0x72, 0x61, 0x72, 0x69, 0x65, 0x73, 0x12, 0x5c, 0x0a, 0x0e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x16, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x19, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x22, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x64, 0x6f, 0x63, 0x75, 0x6d, - 0x65, 0x6e, 0x74, 0x73, 0x12, 0x53, 0x0a, 0x0b, 0x4c, 0x69, 0x73, 0x74, 0x4c, 0x69, 0x62, 0x72, - 0x61, 0x72, 0x79, 0x12, 0x13, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, - 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x14, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x4c, - 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x19, - 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, - 0x6c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x69, 0x65, 0x73, 0x12, 0x59, 0x0a, 0x0d, 0x43, 0x72, 0x65, - 0x61, 0x74, 0x65, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x12, 0x15, 0x2e, 0x43, 0x72, 0x65, - 0x61, 0x74, 0x65, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x16, 0x2e, 0x43, 0x72, 0x65, 0x61, 0x74, 0x65, 0x4c, 0x69, 0x62, 0x72, 0x61, 0x72, - 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x19, 0x82, 0xd3, 0xe4, 0x93, 0x02, - 0x13, 0x22, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, 0x2f, 0x6c, 0x69, 0x62, 0x72, 0x61, - 0x72, 0x69, 0x65, 0x73, 0x42, 0x1e, 0x5a, 0x1c, 0x6c, 0x65, 0x61, 0x66, 0x64, 0x65, 0x76, 0x2e, - 0x74, 0x6f, 0x70, 0x2f, 0x6c, 0x65, 0x61, 0x66, 0x2f, 0x72, 0x61, 0x67, 0x2f, 0x61, 0x70, 0x69, - 0x2f, 0x72, 0x61, 0x67, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x65, 0x6e, 0x74, 0x73, 0x12, 0x56, 0x0a, 0x0c, 0x4c, 0x69, 0x73, 0x74, 0x44, 0x6f, 0x63, 0x75, + 0x6d, 0x65, 0x6e, 0x74, 0x12, 0x14, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x44, 0x6f, 0x63, 0x75, 0x6d, + 0x65, 0x6e, 0x74, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x4c, 0x69, 0x73, + 0x74, 0x44, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x22, 0x19, 0x82, 0xd3, 0xe4, 0x93, 0x02, 0x13, 0x12, 0x11, 0x2f, 0x61, 0x70, 0x69, 0x2f, + 0x76, 0x31, 0x2f, 0x64, 0x6f, 0x63, 0x75, 0x6d, 0x65, 0x6e, 0x74, 0x73, 0x42, 0x1e, 0x5a, 0x1c, + 0x6c, 0x65, 0x61, 0x66, 0x64, 0x65, 0x76, 0x2e, 0x74, 0x6f, 0x70, 0x2f, 0x6c, 0x65, 0x61, 0x66, + 0x2f, 0x72, 0x61, 0x67, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x72, 0x61, 0x67, 0x62, 0x06, 0x70, 0x72, + 0x6f, 0x74, 0x6f, 0x33, } var file_api_rag_rag_proto_goTypes = []interface{}{ - (*CreateDocumentRequest)(nil), // 0: CreateDocumentRequest - (*ListLibraryRequest)(nil), // 1: ListLibraryRequest - (*CreateLibraryRequest)(nil), // 2: CreateLibraryRequest - (*CreateDocumentResponse)(nil), // 3: CreateDocumentResponse + (*ListLibraryRequest)(nil), // 0: ListLibraryRequest + (*CreateLibraryRequest)(nil), // 1: CreateLibraryRequest + (*CreateDocumentRequest)(nil), // 2: CreateDocumentRequest + (*ListDocumentRequest)(nil), // 3: ListDocumentRequest (*ListLibraryResponse)(nil), // 4: ListLibraryResponse (*CreateLibraryResponse)(nil), // 5: CreateLibraryResponse + (*CreateDocumentResponse)(nil), // 6: CreateDocumentResponse + (*ListDocumentResponse)(nil), // 7: ListDocumentResponse } var file_api_rag_rag_proto_depIdxs = []int32{ - 0, // 0: RagService.RAGService.CreateDocument:input_type -> CreateDocumentRequest - 1, // 1: RagService.RAGService.ListLibrary:input_type -> ListLibraryRequest - 2, // 2: RagService.RAGService.CreateLibrary:input_type -> CreateLibraryRequest - 3, // 3: RagService.RAGService.CreateDocument:output_type -> CreateDocumentResponse + 0, // 0: RagService.RAGService.ListLibrary:input_type -> ListLibraryRequest + 1, // 1: RagService.RAGService.CreateLibrary:input_type -> CreateLibraryRequest + 2, // 2: RagService.RAGService.CreateDocument:input_type -> CreateDocumentRequest + 3, // 3: RagService.RAGService.ListDocument:input_type -> ListDocumentRequest 4, // 4: RagService.RAGService.ListLibrary:output_type -> ListLibraryResponse 5, // 5: RagService.RAGService.CreateLibrary:output_type -> CreateLibraryResponse - 3, // [3:6] is the sub-list for method output_type - 0, // [0:3] is the sub-list for method input_type + 6, // 6: RagService.RAGService.CreateDocument:output_type -> CreateDocumentResponse + 7, // 7: RagService.RAGService.ListDocument:output_type -> ListDocumentResponse + 4, // [4:8] is the sub-list for method output_type + 0, // [0:4] is the sub-list for method input_type 0, // [0:0] is the sub-list for extension type_name 0, // [0:0] is the sub-list for extension extendee 0, // [0:0] is the sub-list for field type_name diff --git a/api/rag/rag.pb.gw.go b/api/rag/rag.pb.gw.go index 0b46c5d..c1a0c0f 100644 --- a/api/rag/rag.pb.gw.go +++ b/api/rag/rag.pb.gw.go @@ -31,42 +31,6 @@ var _ = runtime.String var _ = utilities.NewDoubleArray var _ = metadata.Join -var ( - filter_RAGService_CreateDocument_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} -) - -func request_RAGService_CreateDocument_0(ctx context.Context, marshaler runtime.Marshaler, client RAGServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq CreateDocumentRequest - var metadata runtime.ServerMetadata - - if err := req.ParseForm(); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_RAGService_CreateDocument_0); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - - msg, err := client.CreateDocument(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) - return msg, metadata, err - -} - -func local_request_RAGService_CreateDocument_0(ctx context.Context, marshaler runtime.Marshaler, server RAGServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { - var protoReq CreateDocumentRequest - var metadata runtime.ServerMetadata - - if err := req.ParseForm(); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_RAGService_CreateDocument_0); err != nil { - return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) - } - - msg, err := server.CreateDocument(ctx, &protoReq) - return msg, metadata, err - -} - var ( filter_RAGService_ListLibrary_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} ) @@ -139,37 +103,84 @@ func local_request_RAGService_CreateLibrary_0(ctx context.Context, marshaler run } +var ( + filter_RAGService_CreateDocument_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + +func request_RAGService_CreateDocument_0(ctx context.Context, marshaler runtime.Marshaler, client RAGServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq CreateDocumentRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_RAGService_CreateDocument_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.CreateDocument(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_RAGService_CreateDocument_0(ctx context.Context, marshaler runtime.Marshaler, server RAGServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq CreateDocumentRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_RAGService_CreateDocument_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.CreateDocument(ctx, &protoReq) + return msg, metadata, err + +} + +var ( + filter_RAGService_ListDocument_0 = &utilities.DoubleArray{Encoding: map[string]int{}, Base: []int(nil), Check: []int(nil)} +) + +func request_RAGService_ListDocument_0(ctx context.Context, marshaler runtime.Marshaler, client RAGServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq ListDocumentRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_RAGService_ListDocument_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := client.ListDocument(ctx, &protoReq, grpc.Header(&metadata.HeaderMD), grpc.Trailer(&metadata.TrailerMD)) + return msg, metadata, err + +} + +func local_request_RAGService_ListDocument_0(ctx context.Context, marshaler runtime.Marshaler, server RAGServiceServer, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { + var protoReq ListDocumentRequest + var metadata runtime.ServerMetadata + + if err := req.ParseForm(); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + if err := runtime.PopulateQueryParameters(&protoReq, req.Form, filter_RAGService_ListDocument_0); err != nil { + return nil, metadata, status.Errorf(codes.InvalidArgument, "%v", err) + } + + msg, err := server.ListDocument(ctx, &protoReq) + return msg, metadata, err + +} + // RegisterRAGServiceHandlerServer registers the http handlers for service RAGService to "mux". // UnaryRPC :call RAGServiceServer directly. // StreamingRPC :currently unsupported pending https://github.com/grpc/grpc-go/issues/906. // Note that using this registration option will cause many gRPC library features to stop working. Consider using RegisterRAGServiceHandlerFromEndpoint instead. func RegisterRAGServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux, server RAGServiceServer) error { - mux.Handle("POST", pattern_RAGService_CreateDocument_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - var stream runtime.ServerTransportStream - ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/RagService.RAGService/CreateDocument", runtime.WithHTTPPathPattern("/api/v1/documents")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := local_request_RAGService_CreateDocument_0(annotatedContext, inboundMarshaler, server, req, pathParams) - md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_RAGService_CreateDocument_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - mux.Handle("GET", pattern_RAGService_ListLibrary_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() @@ -220,6 +231,56 @@ func RegisterRAGServiceHandlerServer(ctx context.Context, mux *runtime.ServeMux, }) + mux.Handle("POST", pattern_RAGService_CreateDocument_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + var stream runtime.ServerTransportStream + ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + var err error + var annotatedContext context.Context + annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/RagService.RAGService/CreateDocument", runtime.WithHTTPPathPattern("/api/v1/documents")) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_RAGService_CreateDocument_0(annotatedContext, inboundMarshaler, server, req, pathParams) + md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) + annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) + if err != nil { + runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) + return + } + + forward_RAGService_CreateDocument_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_RAGService_ListDocument_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + var stream runtime.ServerTransportStream + ctx = grpc.NewContextWithServerTransportStream(ctx, &stream) + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + var err error + var annotatedContext context.Context + annotatedContext, err = runtime.AnnotateIncomingContext(ctx, mux, req, "/RagService.RAGService/ListDocument", runtime.WithHTTPPathPattern("/api/v1/documents")) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := local_request_RAGService_ListDocument_0(annotatedContext, inboundMarshaler, server, req, pathParams) + md.HeaderMD, md.TrailerMD = metadata.Join(md.HeaderMD, stream.Header()), metadata.Join(md.TrailerMD, stream.Trailer()) + annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) + if err != nil { + runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) + return + } + + forward_RAGService_ListDocument_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } @@ -261,28 +322,6 @@ func RegisterRAGServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn // "RAGServiceClient" to call the correct interceptors. func RegisterRAGServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, client RAGServiceClient) error { - mux.Handle("POST", pattern_RAGService_CreateDocument_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/RagService.RAGService/CreateDocument", runtime.WithHTTPPathPattern("/api/v1/documents")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_RAGService_CreateDocument_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_RAGService_CreateDocument_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - mux.Handle("GET", pattern_RAGService_ListLibrary_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { ctx, cancel := context.WithCancel(req.Context()) defer cancel() @@ -327,21 +366,69 @@ func RegisterRAGServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, }) + mux.Handle("POST", pattern_RAGService_CreateDocument_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + var err error + var annotatedContext context.Context + annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/RagService.RAGService/CreateDocument", runtime.WithHTTPPathPattern("/api/v1/documents")) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_RAGService_CreateDocument_0(annotatedContext, inboundMarshaler, client, req, pathParams) + annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) + if err != nil { + runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) + return + } + + forward_RAGService_CreateDocument_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + + mux.Handle("GET", pattern_RAGService_ListDocument_0, func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { + ctx, cancel := context.WithCancel(req.Context()) + defer cancel() + inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) + var err error + var annotatedContext context.Context + annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/RagService.RAGService/ListDocument", runtime.WithHTTPPathPattern("/api/v1/documents")) + if err != nil { + runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) + return + } + resp, md, err := request_RAGService_ListDocument_0(annotatedContext, inboundMarshaler, client, req, pathParams) + annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) + if err != nil { + runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) + return + } + + forward_RAGService_ListDocument_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) + + }) + return nil } var ( - pattern_RAGService_CreateDocument_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "documents"}, "")) - pattern_RAGService_ListLibrary_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "libraries"}, "")) pattern_RAGService_CreateLibrary_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "libraries"}, "")) + + pattern_RAGService_CreateDocument_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "documents"}, "")) + + pattern_RAGService_ListDocument_0 = runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 2, 2}, []string{"api", "v1", "documents"}, "")) ) var ( - forward_RAGService_CreateDocument_0 = runtime.ForwardResponseMessage - forward_RAGService_ListLibrary_0 = runtime.ForwardResponseMessage forward_RAGService_CreateLibrary_0 = runtime.ForwardResponseMessage + + forward_RAGService_CreateDocument_0 = runtime.ForwardResponseMessage + + forward_RAGService_ListDocument_0 = runtime.ForwardResponseMessage ) diff --git a/api/rag/rag.proto b/api/rag/rag.proto index 452d3e6..5baee60 100644 --- a/api/rag/rag.proto +++ b/api/rag/rag.proto @@ -12,12 +12,6 @@ import "api/rag/library.proto"; service RAGService { - rpc CreateDocument(CreateDocumentRequest) returns (CreateDocumentResponse) { - option (google.api.http) = { - post: "/api/v1/documents" - }; - } - rpc ListLibrary(ListLibraryRequest) returns (ListLibraryResponse) { option (google.api.http) = { get: "/api/v1/libraries" @@ -29,5 +23,19 @@ service RAGService { post: "/api/v1/libraries" }; } + +// DocumentService + rpc CreateDocument(CreateDocumentRequest) returns (CreateDocumentResponse) { + option (google.api.http) = { + post: "/api/v1/documents" + }; + } + + rpc ListDocument(ListDocumentRequest) returns (ListDocumentResponse) { + option (google.api.http) = { + get: "/api/v1/documents" + }; + } + } diff --git a/api/rag/rag.swagger.json b/api/rag/rag.swagger.json index 7151b64..dbf24a3 100644 --- a/api/rag/rag.swagger.json +++ b/api/rag/rag.swagger.json @@ -17,7 +17,36 @@ ], "paths": { "/api/v1/documents": { + "get": { + "operationId": "RAGService_ListDocument", + "responses": { + "200": { + "description": "A successful response.", + "schema": { + "$ref": "#/definitions/ListDocumentResponse" + } + }, + "default": { + "description": "An unexpected error response.", + "schema": { + "$ref": "#/definitions/rpcStatus" + } + } + }, + "parameters": [ + { + "name": "libraryId", + "in": "query", + "required": false, + "type": "string" + } + ], + "tags": [ + "RAGService" + ] + }, "post": { + "summary": "DocumentService", "operationId": "RAGService_CreateDocument", "responses": { "200": { @@ -151,6 +180,9 @@ "libraryId": { "type": "string" }, + "userId": { + "type": "string" + }, "createdAt": { "type": "string" }, @@ -181,6 +213,18 @@ }, "title": "import \"google.golang.org/grpc/health/grpc_health_v1\";" }, + "ListDocumentResponse": { + "type": "object", + "properties": { + "documents": { + "type": "array", + "items": { + "type": "object", + "$ref": "#/definitions/Document" + } + } + } + }, "ListLibraryResponse": { "type": "object", "properties": { diff --git a/api/rag/rag_grpc.pb.go b/api/rag/rag_grpc.pb.go index f6a1e3b..c2362bd 100644 --- a/api/rag/rag_grpc.pb.go +++ b/api/rag/rag_grpc.pb.go @@ -22,9 +22,11 @@ const _ = grpc.SupportPackageIsVersion7 // // For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream. type RAGServiceClient interface { - CreateDocument(ctx context.Context, in *CreateDocumentRequest, opts ...grpc.CallOption) (*CreateDocumentResponse, error) ListLibrary(ctx context.Context, in *ListLibraryRequest, opts ...grpc.CallOption) (*ListLibraryResponse, error) CreateLibrary(ctx context.Context, in *CreateLibraryRequest, opts ...grpc.CallOption) (*CreateLibraryResponse, error) + // DocumentService + CreateDocument(ctx context.Context, in *CreateDocumentRequest, opts ...grpc.CallOption) (*CreateDocumentResponse, error) + ListDocument(ctx context.Context, in *ListDocumentRequest, opts ...grpc.CallOption) (*ListDocumentResponse, error) } type rAGServiceClient struct { @@ -35,15 +37,6 @@ func NewRAGServiceClient(cc grpc.ClientConnInterface) RAGServiceClient { return &rAGServiceClient{cc} } -func (c *rAGServiceClient) CreateDocument(ctx context.Context, in *CreateDocumentRequest, opts ...grpc.CallOption) (*CreateDocumentResponse, error) { - out := new(CreateDocumentResponse) - err := c.cc.Invoke(ctx, "/RagService.RAGService/CreateDocument", in, out, opts...) - if err != nil { - return nil, err - } - return out, nil -} - func (c *rAGServiceClient) ListLibrary(ctx context.Context, in *ListLibraryRequest, opts ...grpc.CallOption) (*ListLibraryResponse, error) { out := new(ListLibraryResponse) err := c.cc.Invoke(ctx, "/RagService.RAGService/ListLibrary", in, out, opts...) @@ -62,13 +55,33 @@ func (c *rAGServiceClient) CreateLibrary(ctx context.Context, in *CreateLibraryR return out, nil } +func (c *rAGServiceClient) CreateDocument(ctx context.Context, in *CreateDocumentRequest, opts ...grpc.CallOption) (*CreateDocumentResponse, error) { + out := new(CreateDocumentResponse) + err := c.cc.Invoke(ctx, "/RagService.RAGService/CreateDocument", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *rAGServiceClient) ListDocument(ctx context.Context, in *ListDocumentRequest, opts ...grpc.CallOption) (*ListDocumentResponse, error) { + out := new(ListDocumentResponse) + err := c.cc.Invoke(ctx, "/RagService.RAGService/ListDocument", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + // RAGServiceServer is the server API for RAGService service. // All implementations must embed UnimplementedRAGServiceServer // for forward compatibility type RAGServiceServer interface { - CreateDocument(context.Context, *CreateDocumentRequest) (*CreateDocumentResponse, error) ListLibrary(context.Context, *ListLibraryRequest) (*ListLibraryResponse, error) CreateLibrary(context.Context, *CreateLibraryRequest) (*CreateLibraryResponse, error) + // DocumentService + CreateDocument(context.Context, *CreateDocumentRequest) (*CreateDocumentResponse, error) + ListDocument(context.Context, *ListDocumentRequest) (*ListDocumentResponse, error) mustEmbedUnimplementedRAGServiceServer() } @@ -76,15 +89,18 @@ type RAGServiceServer interface { type UnimplementedRAGServiceServer struct { } -func (UnimplementedRAGServiceServer) CreateDocument(context.Context, *CreateDocumentRequest) (*CreateDocumentResponse, error) { - return nil, status.Errorf(codes.Unimplemented, "method CreateDocument not implemented") -} func (UnimplementedRAGServiceServer) ListLibrary(context.Context, *ListLibraryRequest) (*ListLibraryResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method ListLibrary not implemented") } func (UnimplementedRAGServiceServer) CreateLibrary(context.Context, *CreateLibraryRequest) (*CreateLibraryResponse, error) { return nil, status.Errorf(codes.Unimplemented, "method CreateLibrary not implemented") } +func (UnimplementedRAGServiceServer) CreateDocument(context.Context, *CreateDocumentRequest) (*CreateDocumentResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method CreateDocument not implemented") +} +func (UnimplementedRAGServiceServer) ListDocument(context.Context, *ListDocumentRequest) (*ListDocumentResponse, error) { + return nil, status.Errorf(codes.Unimplemented, "method ListDocument not implemented") +} func (UnimplementedRAGServiceServer) mustEmbedUnimplementedRAGServiceServer() {} // UnsafeRAGServiceServer may be embedded to opt out of forward compatibility for this service. @@ -98,24 +114,6 @@ func RegisterRAGServiceServer(s grpc.ServiceRegistrar, srv RAGServiceServer) { s.RegisterService(&RAGService_ServiceDesc, srv) } -func _RAGService_CreateDocument_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { - in := new(CreateDocumentRequest) - if err := dec(in); err != nil { - return nil, err - } - if interceptor == nil { - return srv.(RAGServiceServer).CreateDocument(ctx, in) - } - info := &grpc.UnaryServerInfo{ - Server: srv, - FullMethod: "/RagService.RAGService/CreateDocument", - } - handler := func(ctx context.Context, req interface{}) (interface{}, error) { - return srv.(RAGServiceServer).CreateDocument(ctx, req.(*CreateDocumentRequest)) - } - return interceptor(ctx, in, info, handler) -} - func _RAGService_ListLibrary_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(ListLibraryRequest) if err := dec(in); err != nil { @@ -152,6 +150,42 @@ func _RAGService_CreateLibrary_Handler(srv interface{}, ctx context.Context, dec return interceptor(ctx, in, info, handler) } +func _RAGService_CreateDocument_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(CreateDocumentRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(RAGServiceServer).CreateDocument(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/RagService.RAGService/CreateDocument", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(RAGServiceServer).CreateDocument(ctx, req.(*CreateDocumentRequest)) + } + return interceptor(ctx, in, info, handler) +} + +func _RAGService_ListDocument_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(ListDocumentRequest) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(RAGServiceServer).ListDocument(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/RagService.RAGService/ListDocument", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(RAGServiceServer).ListDocument(ctx, req.(*ListDocumentRequest)) + } + return interceptor(ctx, in, info, handler) +} + // RAGService_ServiceDesc is the grpc.ServiceDesc for RAGService service. // It's only intended for direct use with grpc.RegisterService, // and not to be introspected or modified (even as a copy) @@ -159,10 +193,6 @@ var RAGService_ServiceDesc = grpc.ServiceDesc{ ServiceName: "RagService.RAGService", HandlerType: (*RAGServiceServer)(nil), Methods: []grpc.MethodDesc{ - { - MethodName: "CreateDocument", - Handler: _RAGService_CreateDocument_Handler, - }, { MethodName: "ListLibrary", Handler: _RAGService_ListLibrary_Handler, @@ -171,6 +201,14 @@ var RAGService_ServiceDesc = grpc.ServiceDesc{ MethodName: "CreateLibrary", Handler: _RAGService_CreateLibrary_Handler, }, + { + MethodName: "CreateDocument", + Handler: _RAGService_CreateDocument_Handler, + }, + { + MethodName: "ListDocument", + Handler: _RAGService_ListDocument_Handler, + }, }, Streams: []grpc.StreamDesc{}, Metadata: "api/rag/rag.proto", diff --git a/buf.lock b/buf.lock index 6a67b8d..225fe46 100644 --- a/buf.lock +++ b/buf.lock @@ -4,5 +4,5 @@ deps: - remote: buf.build owner: googleapis repository: googleapis - commit: f0e53af8f2fc4556b94f482688b57223 - digest: shake256:de26a277fc28b8b411ecf58729d78d32fcf15090ffd998a4469225b17889bfb51442eaab04bb7a8d88d203ecdf0a9febd4ffd52c18ed1c2229160c7bd353ca95 + commit: 2bbd25900cb34c79bae97d85c948d3cf + digest: shake256:a6446e23f4408160217c22738a0c8c370ff3ff966f601d678960b803829cf53b5cf5998d20fcb3f9c9be944b24337843e7beb6e681ddbf9d036fb6491c0e47e7 diff --git a/ent/client.go b/ent/client.go index 43f8d60..8031dd8 100644 --- a/ent/client.go +++ b/ent/client.go @@ -14,6 +14,9 @@ import ( "entgo.io/ent" "entgo.io/ent/dialect" "entgo.io/ent/dialect/sql" + "entgo.io/ent/dialect/sql/sqlgraph" + "leafdev.top/leaf/rag/ent/document" + "leafdev.top/leaf/rag/ent/documentblock" "leafdev.top/leaf/rag/ent/library" ) @@ -22,6 +25,10 @@ type Client struct { config // Schema is the client for creating, migrating and dropping schema. Schema *migrate.Schema + // Document is the client for interacting with the Document builders. + Document *DocumentClient + // DocumentBlock is the client for interacting with the DocumentBlock builders. + DocumentBlock *DocumentBlockClient // Library is the client for interacting with the Library builders. Library *LibraryClient } @@ -35,6 +42,8 @@ func NewClient(opts ...Option) *Client { func (c *Client) init() { c.Schema = migrate.NewSchema(c.driver) + c.Document = NewDocumentClient(c.config) + c.DocumentBlock = NewDocumentBlockClient(c.config) c.Library = NewLibraryClient(c.config) } @@ -126,9 +135,11 @@ func (c *Client) Tx(ctx context.Context) (*Tx, error) { cfg := c.config cfg.driver = tx return &Tx{ - ctx: ctx, - config: cfg, - Library: NewLibraryClient(cfg), + ctx: ctx, + config: cfg, + Document: NewDocumentClient(cfg), + DocumentBlock: NewDocumentBlockClient(cfg), + Library: NewLibraryClient(cfg), }, nil } @@ -146,16 +157,18 @@ func (c *Client) BeginTx(ctx context.Context, opts *sql.TxOptions) (*Tx, error) cfg := c.config cfg.driver = &txDriver{tx: tx, drv: c.driver} return &Tx{ - ctx: ctx, - config: cfg, - Library: NewLibraryClient(cfg), + ctx: ctx, + config: cfg, + Document: NewDocumentClient(cfg), + DocumentBlock: NewDocumentBlockClient(cfg), + Library: NewLibraryClient(cfg), }, nil } // Debug returns a new debug-client. It's used to get verbose logging on specific operations. // // client.Debug(). -// Library. +// Document. // Query(). // Count(ctx) func (c *Client) Debug() *Client { @@ -177,18 +190,26 @@ func (c *Client) Close() error { // Use adds the mutation hooks to all the entity clients. // In order to add hooks to a specific client, call: `client.Node.Use(...)`. func (c *Client) Use(hooks ...Hook) { + c.Document.Use(hooks...) + c.DocumentBlock.Use(hooks...) c.Library.Use(hooks...) } // Intercept adds the query interceptors to all the entity clients. // In order to add interceptors to a specific client, call: `client.Node.Intercept(...)`. func (c *Client) Intercept(interceptors ...Interceptor) { + c.Document.Intercept(interceptors...) + c.DocumentBlock.Intercept(interceptors...) c.Library.Intercept(interceptors...) } // Mutate implements the ent.Mutator interface. func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { switch m := m.(type) { + case *DocumentMutation: + return c.Document.mutate(ctx, m) + case *DocumentBlockMutation: + return c.DocumentBlock.mutate(ctx, m) case *LibraryMutation: return c.Library.mutate(ctx, m) default: @@ -196,6 +217,288 @@ func (c *Client) Mutate(ctx context.Context, m Mutation) (Value, error) { } } +// DocumentClient is a client for the Document schema. +type DocumentClient struct { + config +} + +// NewDocumentClient returns a client for the Document from the given config. +func NewDocumentClient(c config) *DocumentClient { + return &DocumentClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `document.Hooks(f(g(h())))`. +func (c *DocumentClient) Use(hooks ...Hook) { + c.hooks.Document = append(c.hooks.Document, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `document.Intercept(f(g(h())))`. +func (c *DocumentClient) Intercept(interceptors ...Interceptor) { + c.inters.Document = append(c.inters.Document, interceptors...) +} + +// Create returns a builder for creating a Document entity. +func (c *DocumentClient) Create() *DocumentCreate { + mutation := newDocumentMutation(c.config, OpCreate) + return &DocumentCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of Document entities. +func (c *DocumentClient) CreateBulk(builders ...*DocumentCreate) *DocumentCreateBulk { + return &DocumentCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *DocumentClient) MapCreateBulk(slice any, setFunc func(*DocumentCreate, int)) *DocumentCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &DocumentCreateBulk{err: fmt.Errorf("calling to DocumentClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*DocumentCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &DocumentCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for Document. +func (c *DocumentClient) Update() *DocumentUpdate { + mutation := newDocumentMutation(c.config, OpUpdate) + return &DocumentUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *DocumentClient) UpdateOne(d *Document) *DocumentUpdateOne { + mutation := newDocumentMutation(c.config, OpUpdateOne, withDocument(d)) + return &DocumentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *DocumentClient) UpdateOneID(id int) *DocumentUpdateOne { + mutation := newDocumentMutation(c.config, OpUpdateOne, withDocumentID(id)) + return &DocumentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for Document. +func (c *DocumentClient) Delete() *DocumentDelete { + mutation := newDocumentMutation(c.config, OpDelete) + return &DocumentDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *DocumentClient) DeleteOne(d *Document) *DocumentDeleteOne { + return c.DeleteOneID(d.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *DocumentClient) DeleteOneID(id int) *DocumentDeleteOne { + builder := c.Delete().Where(document.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &DocumentDeleteOne{builder} +} + +// Query returns a query builder for Document. +func (c *DocumentClient) Query() *DocumentQuery { + return &DocumentQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeDocument}, + inters: c.Interceptors(), + } +} + +// Get returns a Document entity by its id. +func (c *DocumentClient) Get(ctx context.Context, id int) (*Document, error) { + return c.Query().Where(document.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *DocumentClient) GetX(ctx context.Context, id int) *Document { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// Hooks returns the client hooks. +func (c *DocumentClient) Hooks() []Hook { + return c.hooks.Document +} + +// Interceptors returns the client interceptors. +func (c *DocumentClient) Interceptors() []Interceptor { + return c.inters.Document +} + +func (c *DocumentClient) mutate(ctx context.Context, m *DocumentMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&DocumentCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&DocumentUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&DocumentUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&DocumentDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown Document mutation op: %q", m.Op()) + } +} + +// DocumentBlockClient is a client for the DocumentBlock schema. +type DocumentBlockClient struct { + config +} + +// NewDocumentBlockClient returns a client for the DocumentBlock from the given config. +func NewDocumentBlockClient(c config) *DocumentBlockClient { + return &DocumentBlockClient{config: c} +} + +// Use adds a list of mutation hooks to the hooks stack. +// A call to `Use(f, g, h)` equals to `documentblock.Hooks(f(g(h())))`. +func (c *DocumentBlockClient) Use(hooks ...Hook) { + c.hooks.DocumentBlock = append(c.hooks.DocumentBlock, hooks...) +} + +// Intercept adds a list of query interceptors to the interceptors stack. +// A call to `Intercept(f, g, h)` equals to `documentblock.Intercept(f(g(h())))`. +func (c *DocumentBlockClient) Intercept(interceptors ...Interceptor) { + c.inters.DocumentBlock = append(c.inters.DocumentBlock, interceptors...) +} + +// Create returns a builder for creating a DocumentBlock entity. +func (c *DocumentBlockClient) Create() *DocumentBlockCreate { + mutation := newDocumentBlockMutation(c.config, OpCreate) + return &DocumentBlockCreate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// CreateBulk returns a builder for creating a bulk of DocumentBlock entities. +func (c *DocumentBlockClient) CreateBulk(builders ...*DocumentBlockCreate) *DocumentBlockCreateBulk { + return &DocumentBlockCreateBulk{config: c.config, builders: builders} +} + +// MapCreateBulk creates a bulk creation builder from the given slice. For each item in the slice, the function creates +// a builder and applies setFunc on it. +func (c *DocumentBlockClient) MapCreateBulk(slice any, setFunc func(*DocumentBlockCreate, int)) *DocumentBlockCreateBulk { + rv := reflect.ValueOf(slice) + if rv.Kind() != reflect.Slice { + return &DocumentBlockCreateBulk{err: fmt.Errorf("calling to DocumentBlockClient.MapCreateBulk with wrong type %T, need slice", slice)} + } + builders := make([]*DocumentBlockCreate, rv.Len()) + for i := 0; i < rv.Len(); i++ { + builders[i] = c.Create() + setFunc(builders[i], i) + } + return &DocumentBlockCreateBulk{config: c.config, builders: builders} +} + +// Update returns an update builder for DocumentBlock. +func (c *DocumentBlockClient) Update() *DocumentBlockUpdate { + mutation := newDocumentBlockMutation(c.config, OpUpdate) + return &DocumentBlockUpdate{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOne returns an update builder for the given entity. +func (c *DocumentBlockClient) UpdateOne(db *DocumentBlock) *DocumentBlockUpdateOne { + mutation := newDocumentBlockMutation(c.config, OpUpdateOne, withDocumentBlock(db)) + return &DocumentBlockUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// UpdateOneID returns an update builder for the given id. +func (c *DocumentBlockClient) UpdateOneID(id int) *DocumentBlockUpdateOne { + mutation := newDocumentBlockMutation(c.config, OpUpdateOne, withDocumentBlockID(id)) + return &DocumentBlockUpdateOne{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// Delete returns a delete builder for DocumentBlock. +func (c *DocumentBlockClient) Delete() *DocumentBlockDelete { + mutation := newDocumentBlockMutation(c.config, OpDelete) + return &DocumentBlockDelete{config: c.config, hooks: c.Hooks(), mutation: mutation} +} + +// DeleteOne returns a builder for deleting the given entity. +func (c *DocumentBlockClient) DeleteOne(db *DocumentBlock) *DocumentBlockDeleteOne { + return c.DeleteOneID(db.ID) +} + +// DeleteOneID returns a builder for deleting the given entity by its id. +func (c *DocumentBlockClient) DeleteOneID(id int) *DocumentBlockDeleteOne { + builder := c.Delete().Where(documentblock.ID(id)) + builder.mutation.id = &id + builder.mutation.op = OpDeleteOne + return &DocumentBlockDeleteOne{builder} +} + +// Query returns a query builder for DocumentBlock. +func (c *DocumentBlockClient) Query() *DocumentBlockQuery { + return &DocumentBlockQuery{ + config: c.config, + ctx: &QueryContext{Type: TypeDocumentBlock}, + inters: c.Interceptors(), + } +} + +// Get returns a DocumentBlock entity by its id. +func (c *DocumentBlockClient) Get(ctx context.Context, id int) (*DocumentBlock, error) { + return c.Query().Where(documentblock.ID(id)).Only(ctx) +} + +// GetX is like Get, but panics if an error occurs. +func (c *DocumentBlockClient) GetX(ctx context.Context, id int) *DocumentBlock { + obj, err := c.Get(ctx, id) + if err != nil { + panic(err) + } + return obj +} + +// QueryDocument queries the document edge of a DocumentBlock. +func (c *DocumentBlockClient) QueryDocument(db *DocumentBlock) *DocumentQuery { + query := (&DocumentClient{config: c.config}).Query() + query.path = func(context.Context) (fromV *sql.Selector, _ error) { + id := db.ID + step := sqlgraph.NewStep( + sqlgraph.From(documentblock.Table, documentblock.FieldID, id), + sqlgraph.To(document.Table, document.FieldID), + sqlgraph.Edge(sqlgraph.O2M, false, documentblock.DocumentTable, documentblock.DocumentColumn), + ) + fromV = sqlgraph.Neighbors(db.driver.Dialect(), step) + return fromV, nil + } + return query +} + +// Hooks returns the client hooks. +func (c *DocumentBlockClient) Hooks() []Hook { + return c.hooks.DocumentBlock +} + +// Interceptors returns the client interceptors. +func (c *DocumentBlockClient) Interceptors() []Interceptor { + return c.inters.DocumentBlock +} + +func (c *DocumentBlockClient) mutate(ctx context.Context, m *DocumentBlockMutation) (Value, error) { + switch m.Op() { + case OpCreate: + return (&DocumentBlockCreate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdate: + return (&DocumentBlockUpdate{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpUpdateOne: + return (&DocumentBlockUpdateOne{config: c.config, hooks: c.Hooks(), mutation: m}).Save(ctx) + case OpDelete, OpDeleteOne: + return (&DocumentBlockDelete{config: c.config, hooks: c.Hooks(), mutation: m}).Exec(ctx) + default: + return nil, fmt.Errorf("ent: unknown DocumentBlock mutation op: %q", m.Op()) + } +} + // LibraryClient is a client for the Library schema. type LibraryClient struct { config @@ -332,9 +635,9 @@ func (c *LibraryClient) mutate(ctx context.Context, m *LibraryMutation) (Value, // hooks and interceptors per client, for fast access. type ( hooks struct { - Library []ent.Hook + Document, DocumentBlock, Library []ent.Hook } inters struct { - Library []ent.Interceptor + Document, DocumentBlock, Library []ent.Interceptor } ) diff --git a/ent/ent.go b/ent/ent.go index 5dce65a..7ae71eb 100644 --- a/ent/ent.go +++ b/ent/ent.go @@ -12,6 +12,8 @@ import ( "entgo.io/ent" "entgo.io/ent/dialect/sql" "entgo.io/ent/dialect/sql/sqlgraph" + "leafdev.top/leaf/rag/ent/document" + "leafdev.top/leaf/rag/ent/documentblock" "leafdev.top/leaf/rag/ent/library" ) @@ -73,7 +75,9 @@ var ( func checkColumn(table, column string) error { initCheck.Do(func() { columnCheck = sql.NewColumnCheck(map[string]func(string) bool{ - library.Table: library.ValidColumn, + document.Table: document.ValidColumn, + documentblock.Table: documentblock.ValidColumn, + library.Table: library.ValidColumn, }) }) return columnCheck(table, column) diff --git a/ent/hook/hook.go b/ent/hook/hook.go index 6a32124..9e2514b 100644 --- a/ent/hook/hook.go +++ b/ent/hook/hook.go @@ -9,6 +9,30 @@ import ( "leafdev.top/leaf/rag/ent" ) +// The DocumentFunc type is an adapter to allow the use of ordinary +// function as Document mutator. +type DocumentFunc func(context.Context, *ent.DocumentMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f DocumentFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.DocumentMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.DocumentMutation", m) +} + +// The DocumentBlockFunc type is an adapter to allow the use of ordinary +// function as DocumentBlock mutator. +type DocumentBlockFunc func(context.Context, *ent.DocumentBlockMutation) (ent.Value, error) + +// Mutate calls f(ctx, m). +func (f DocumentBlockFunc) Mutate(ctx context.Context, m ent.Mutation) (ent.Value, error) { + if mv, ok := m.(*ent.DocumentBlockMutation); ok { + return f(ctx, mv) + } + return nil, fmt.Errorf("unexpected mutation type %T. expect *ent.DocumentBlockMutation", m) +} + // The LibraryFunc type is an adapter to allow the use of ordinary // function as Library mutator. type LibraryFunc func(context.Context, *ent.LibraryMutation) (ent.Value, error) diff --git a/ent/migrate/schema.go b/ent/migrate/schema.go index 8fda654..375f26a 100644 --- a/ent/migrate/schema.go +++ b/ent/migrate/schema.go @@ -8,6 +8,60 @@ import ( ) var ( + // DocumentsColumns holds the columns for the "documents" table. + DocumentsColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "name", Type: field.TypeString}, + {Name: "user_id", Type: field.TypeString}, + {Name: "created_at", Type: field.TypeTime}, + {Name: "updated_at", Type: field.TypeTime}, + {Name: "document_block_document", Type: field.TypeInt, Nullable: true}, + } + // DocumentsTable holds the schema information for the "documents" table. + DocumentsTable = &schema.Table{ + Name: "documents", + Columns: DocumentsColumns, + PrimaryKey: []*schema.Column{DocumentsColumns[0]}, + ForeignKeys: []*schema.ForeignKey{ + { + Symbol: "documents_document_blocks_document", + Columns: []*schema.Column{DocumentsColumns[5]}, + RefColumns: []*schema.Column{DocumentBlocksColumns[0]}, + OnDelete: schema.SetNull, + }, + }, + Indexes: []*schema.Index{ + { + Name: "document_name_user_id", + Unique: false, + Columns: []*schema.Column{DocumentsColumns[1], DocumentsColumns[2]}, + }, + }, + } + // DocumentBlocksColumns holds the columns for the "document_blocks" table. + DocumentBlocksColumns = []*schema.Column{ + {Name: "id", Type: field.TypeInt, Increment: true}, + {Name: "name", Type: field.TypeString}, + {Name: "content", Type: field.TypeString}, + {Name: "order", Type: field.TypeInt}, + {Name: "document_id", Type: field.TypeInt}, + {Name: "user_id", Type: field.TypeString}, + {Name: "created_at", Type: field.TypeTime}, + {Name: "updated_at", Type: field.TypeTime}, + } + // DocumentBlocksTable holds the schema information for the "document_blocks" table. + DocumentBlocksTable = &schema.Table{ + Name: "document_blocks", + Columns: DocumentBlocksColumns, + PrimaryKey: []*schema.Column{DocumentBlocksColumns[0]}, + Indexes: []*schema.Index{ + { + Name: "documentblock_name_user_id_order", + Unique: false, + Columns: []*schema.Column{DocumentBlocksColumns[1], DocumentBlocksColumns[5], DocumentBlocksColumns[3]}, + }, + }, + } // LibrariesColumns holds the columns for the "libraries" table. LibrariesColumns = []*schema.Column{ {Name: "id", Type: field.TypeInt, Increment: true}, @@ -31,9 +85,12 @@ var ( } // Tables holds all the tables in the schema. Tables = []*schema.Table{ + DocumentsTable, + DocumentBlocksTable, LibrariesTable, } ) func init() { + DocumentsTable.ForeignKeys[0].RefTable = DocumentBlocksTable } diff --git a/ent/mutation.go b/ent/mutation.go index f0382c8..244a4d1 100644 --- a/ent/mutation.go +++ b/ent/mutation.go @@ -11,6 +11,8 @@ import ( "entgo.io/ent" "entgo.io/ent/dialect/sql" + "leafdev.top/leaf/rag/ent/document" + "leafdev.top/leaf/rag/ent/documentblock" "leafdev.top/leaf/rag/ent/library" "leafdev.top/leaf/rag/ent/predicate" ) @@ -24,9 +26,1311 @@ const ( OpUpdateOne = ent.OpUpdateOne // Node types. - TypeLibrary = "Library" + TypeDocument = "Document" + TypeDocumentBlock = "DocumentBlock" + TypeLibrary = "Library" ) +// DocumentMutation represents an operation that mutates the Document nodes in the graph. +type DocumentMutation struct { + config + op Op + typ string + id *int + name *string + user_id *string + created_at *time.Time + updated_at *time.Time + clearedFields map[string]struct{} + done bool + oldValue func(context.Context) (*Document, error) + predicates []predicate.Document +} + +var _ ent.Mutation = (*DocumentMutation)(nil) + +// documentOption allows management of the mutation configuration using functional options. +type documentOption func(*DocumentMutation) + +// newDocumentMutation creates new mutation for the Document entity. +func newDocumentMutation(c config, op Op, opts ...documentOption) *DocumentMutation { + m := &DocumentMutation{ + config: c, + op: op, + typ: TypeDocument, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withDocumentID sets the ID field of the mutation. +func withDocumentID(id int) documentOption { + return func(m *DocumentMutation) { + var ( + err error + once sync.Once + value *Document + ) + m.oldValue = func(ctx context.Context) (*Document, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().Document.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withDocument sets the old Document of the mutation. +func withDocument(node *Document) documentOption { + return func(m *DocumentMutation) { + m.oldValue = func(context.Context) (*Document, error) { + return node, nil + } + m.id = &node.ID + } +} + +// Client returns a new `ent.Client` from the mutation. If the mutation was +// executed in a transaction (ent.Tx), a transactional client is returned. +func (m DocumentMutation) Client() *Client { + client := &Client{config: m.config} + client.init() + return client +} + +// Tx returns an `ent.Tx` for mutations that were executed in transactions; +// it returns an error otherwise. +func (m DocumentMutation) Tx() (*Tx, error) { + if _, ok := m.driver.(*txDriver); !ok { + return nil, errors.New("ent: mutation is not running in a transaction") + } + tx := &Tx{config: m.config} + tx.init() + return tx, nil +} + +// ID returns the ID value in the mutation. Note that the ID is only available +// if it was provided to the builder or after it was returned from the database. +func (m *DocumentMutation) ID() (id int, exists bool) { + if m.id == nil { + return + } + return *m.id, true +} + +// IDs queries the database and returns the entity ids that match the mutation's predicate. +// That means, if the mutation is applied within a transaction with an isolation level such +// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated +// or updated by the mutation. +func (m *DocumentMutation) IDs(ctx context.Context) ([]int, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []int{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().Document.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetName sets the "name" field. +func (m *DocumentMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *DocumentMutation) Name() (r string, exists bool) { + v := m.name + if v == nil { + return + } + return *v, true +} + +// OldName returns the old "name" field's value of the Document entity. +// If the Document object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentMutation) OldName(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldName is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldName requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldName: %w", err) + } + return oldValue.Name, nil +} + +// ResetName resets all changes to the "name" field. +func (m *DocumentMutation) ResetName() { + m.name = nil +} + +// SetUserID sets the "user_id" field. +func (m *DocumentMutation) SetUserID(s string) { + m.user_id = &s +} + +// UserID returns the value of the "user_id" field in the mutation. +func (m *DocumentMutation) UserID() (r string, exists bool) { + v := m.user_id + if v == nil { + return + } + return *v, true +} + +// OldUserID returns the old "user_id" field's value of the Document entity. +// If the Document object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentMutation) OldUserID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUserID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUserID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUserID: %w", err) + } + return oldValue.UserID, nil +} + +// ResetUserID resets all changes to the "user_id" field. +func (m *DocumentMutation) ResetUserID() { + m.user_id = nil +} + +// SetCreatedAt sets the "created_at" field. +func (m *DocumentMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *DocumentMutation) CreatedAt() (r time.Time, exists bool) { + v := m.created_at + if v == nil { + return + } + return *v, true +} + +// OldCreatedAt returns the old "created_at" field's value of the Document entity. +// If the Document object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldCreatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err) + } + return oldValue.CreatedAt, nil +} + +// ResetCreatedAt resets all changes to the "created_at" field. +func (m *DocumentMutation) ResetCreatedAt() { + m.created_at = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *DocumentMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *DocumentMutation) UpdatedAt() (r time.Time, exists bool) { + v := m.updated_at + if v == nil { + return + } + return *v, true +} + +// OldUpdatedAt returns the old "updated_at" field's value of the Document entity. +// If the Document object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUpdatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUpdatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err) + } + return oldValue.UpdatedAt, nil +} + +// ResetUpdatedAt resets all changes to the "updated_at" field. +func (m *DocumentMutation) ResetUpdatedAt() { + m.updated_at = nil +} + +// Where appends a list predicates to the DocumentMutation builder. +func (m *DocumentMutation) Where(ps ...predicate.Document) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the DocumentMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *DocumentMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.Document, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *DocumentMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *DocumentMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (Document). +func (m *DocumentMutation) Type() string { + return m.typ +} + +// Fields returns all fields that were changed during this mutation. Note that in +// order to get all numeric fields that were incremented/decremented, call +// AddedFields(). +func (m *DocumentMutation) Fields() []string { + fields := make([]string, 0, 4) + if m.name != nil { + fields = append(fields, document.FieldName) + } + if m.user_id != nil { + fields = append(fields, document.FieldUserID) + } + if m.created_at != nil { + fields = append(fields, document.FieldCreatedAt) + } + if m.updated_at != nil { + fields = append(fields, document.FieldUpdatedAt) + } + return fields +} + +// Field returns the value of a field with the given name. The second boolean +// return value indicates that this field was not set, or was not defined in the +// schema. +func (m *DocumentMutation) Field(name string) (ent.Value, bool) { + switch name { + case document.FieldName: + return m.Name() + case document.FieldUserID: + return m.UserID() + case document.FieldCreatedAt: + return m.CreatedAt() + case document.FieldUpdatedAt: + return m.UpdatedAt() + } + return nil, false +} + +// OldField returns the old value of the field from the database. An error is +// returned if the mutation operation is not UpdateOne, or the query to the +// database failed. +func (m *DocumentMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case document.FieldName: + return m.OldName(ctx) + case document.FieldUserID: + return m.OldUserID(ctx) + case document.FieldCreatedAt: + return m.OldCreatedAt(ctx) + case document.FieldUpdatedAt: + return m.OldUpdatedAt(ctx) + } + return nil, fmt.Errorf("unknown Document field %s", name) +} + +// SetField sets the value of a field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *DocumentMutation) SetField(name string, value ent.Value) error { + switch name { + case document.FieldName: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetName(v) + return nil + case document.FieldUserID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUserID(v) + return nil + case document.FieldCreatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetCreatedAt(v) + return nil + case document.FieldUpdatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUpdatedAt(v) + return nil + } + return fmt.Errorf("unknown Document field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *DocumentMutation) AddedFields() []string { + return nil +} + +// AddedField returns the numeric value that was incremented/decremented on a field +// with the given name. The second boolean return value indicates that this field +// was not set, or was not defined in the schema. +func (m *DocumentMutation) AddedField(name string) (ent.Value, bool) { + return nil, false +} + +// AddField adds the value to the field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *DocumentMutation) AddField(name string, value ent.Value) error { + switch name { + } + return fmt.Errorf("unknown Document numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *DocumentMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *DocumentMutation) FieldCleared(name string) bool { + _, ok := m.clearedFields[name] + return ok +} + +// ClearField clears the value of the field with the given name. It returns an +// error if the field is not defined in the schema. +func (m *DocumentMutation) ClearField(name string) error { + return fmt.Errorf("unknown Document nullable field %s", name) +} + +// ResetField resets all changes in the mutation for the field with the given name. +// It returns an error if the field is not defined in the schema. +func (m *DocumentMutation) ResetField(name string) error { + switch name { + case document.FieldName: + m.ResetName() + return nil + case document.FieldUserID: + m.ResetUserID() + return nil + case document.FieldCreatedAt: + m.ResetCreatedAt() + return nil + case document.FieldUpdatedAt: + m.ResetUpdatedAt() + return nil + } + return fmt.Errorf("unknown Document field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *DocumentMutation) AddedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *DocumentMutation) AddedIDs(name string) []ent.Value { + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *DocumentMutation) RemovedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *DocumentMutation) RemovedIDs(name string) []ent.Value { + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *DocumentMutation) ClearedEdges() []string { + edges := make([]string, 0, 0) + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *DocumentMutation) EdgeCleared(name string) bool { + return false +} + +// ClearEdge clears the value of the edge with the given name. It returns an error +// if that edge is not defined in the schema. +func (m *DocumentMutation) ClearEdge(name string) error { + return fmt.Errorf("unknown Document unique edge %s", name) +} + +// ResetEdge resets all changes to the edge with the given name in this mutation. +// It returns an error if the edge is not defined in the schema. +func (m *DocumentMutation) ResetEdge(name string) error { + return fmt.Errorf("unknown Document edge %s", name) +} + +// DocumentBlockMutation represents an operation that mutates the DocumentBlock nodes in the graph. +type DocumentBlockMutation struct { + config + op Op + typ string + id *int + name *string + content *string + _order *int + add_order *int + document_id *int + adddocument_id *int + user_id *string + created_at *time.Time + updated_at *time.Time + clearedFields map[string]struct{} + document map[int]struct{} + removeddocument map[int]struct{} + cleareddocument bool + done bool + oldValue func(context.Context) (*DocumentBlock, error) + predicates []predicate.DocumentBlock +} + +var _ ent.Mutation = (*DocumentBlockMutation)(nil) + +// documentblockOption allows management of the mutation configuration using functional options. +type documentblockOption func(*DocumentBlockMutation) + +// newDocumentBlockMutation creates new mutation for the DocumentBlock entity. +func newDocumentBlockMutation(c config, op Op, opts ...documentblockOption) *DocumentBlockMutation { + m := &DocumentBlockMutation{ + config: c, + op: op, + typ: TypeDocumentBlock, + clearedFields: make(map[string]struct{}), + } + for _, opt := range opts { + opt(m) + } + return m +} + +// withDocumentBlockID sets the ID field of the mutation. +func withDocumentBlockID(id int) documentblockOption { + return func(m *DocumentBlockMutation) { + var ( + err error + once sync.Once + value *DocumentBlock + ) + m.oldValue = func(ctx context.Context) (*DocumentBlock, error) { + once.Do(func() { + if m.done { + err = errors.New("querying old values post mutation is not allowed") + } else { + value, err = m.Client().DocumentBlock.Get(ctx, id) + } + }) + return value, err + } + m.id = &id + } +} + +// withDocumentBlock sets the old DocumentBlock of the mutation. +func withDocumentBlock(node *DocumentBlock) documentblockOption { + return func(m *DocumentBlockMutation) { + m.oldValue = func(context.Context) (*DocumentBlock, error) { + return node, nil + } + m.id = &node.ID + } +} + +// Client returns a new `ent.Client` from the mutation. If the mutation was +// executed in a transaction (ent.Tx), a transactional client is returned. +func (m DocumentBlockMutation) Client() *Client { + client := &Client{config: m.config} + client.init() + return client +} + +// Tx returns an `ent.Tx` for mutations that were executed in transactions; +// it returns an error otherwise. +func (m DocumentBlockMutation) Tx() (*Tx, error) { + if _, ok := m.driver.(*txDriver); !ok { + return nil, errors.New("ent: mutation is not running in a transaction") + } + tx := &Tx{config: m.config} + tx.init() + return tx, nil +} + +// ID returns the ID value in the mutation. Note that the ID is only available +// if it was provided to the builder or after it was returned from the database. +func (m *DocumentBlockMutation) ID() (id int, exists bool) { + if m.id == nil { + return + } + return *m.id, true +} + +// IDs queries the database and returns the entity ids that match the mutation's predicate. +// That means, if the mutation is applied within a transaction with an isolation level such +// as sql.LevelSerializable, the returned ids match the ids of the rows that will be updated +// or updated by the mutation. +func (m *DocumentBlockMutation) IDs(ctx context.Context) ([]int, error) { + switch { + case m.op.Is(OpUpdateOne | OpDeleteOne): + id, exists := m.ID() + if exists { + return []int{id}, nil + } + fallthrough + case m.op.Is(OpUpdate | OpDelete): + return m.Client().DocumentBlock.Query().Where(m.predicates...).IDs(ctx) + default: + return nil, fmt.Errorf("IDs is not allowed on %s operations", m.op) + } +} + +// SetName sets the "name" field. +func (m *DocumentBlockMutation) SetName(s string) { + m.name = &s +} + +// Name returns the value of the "name" field in the mutation. +func (m *DocumentBlockMutation) Name() (r string, exists bool) { + v := m.name + if v == nil { + return + } + return *v, true +} + +// OldName returns the old "name" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldName(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldName is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldName requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldName: %w", err) + } + return oldValue.Name, nil +} + +// ResetName resets all changes to the "name" field. +func (m *DocumentBlockMutation) ResetName() { + m.name = nil +} + +// SetContent sets the "content" field. +func (m *DocumentBlockMutation) SetContent(s string) { + m.content = &s +} + +// Content returns the value of the "content" field in the mutation. +func (m *DocumentBlockMutation) Content() (r string, exists bool) { + v := m.content + if v == nil { + return + } + return *v, true +} + +// OldContent returns the old "content" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldContent(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldContent is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldContent requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldContent: %w", err) + } + return oldValue.Content, nil +} + +// ResetContent resets all changes to the "content" field. +func (m *DocumentBlockMutation) ResetContent() { + m.content = nil +} + +// SetOrder sets the "order" field. +func (m *DocumentBlockMutation) SetOrder(i int) { + m._order = &i + m.add_order = nil +} + +// Order returns the value of the "order" field in the mutation. +func (m *DocumentBlockMutation) Order() (r int, exists bool) { + v := m._order + if v == nil { + return + } + return *v, true +} + +// OldOrder returns the old "order" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldOrder(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldOrder is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldOrder requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldOrder: %w", err) + } + return oldValue.Order, nil +} + +// AddOrder adds i to the "order" field. +func (m *DocumentBlockMutation) AddOrder(i int) { + if m.add_order != nil { + *m.add_order += i + } else { + m.add_order = &i + } +} + +// AddedOrder returns the value that was added to the "order" field in this mutation. +func (m *DocumentBlockMutation) AddedOrder() (r int, exists bool) { + v := m.add_order + if v == nil { + return + } + return *v, true +} + +// ResetOrder resets all changes to the "order" field. +func (m *DocumentBlockMutation) ResetOrder() { + m._order = nil + m.add_order = nil +} + +// SetDocumentID sets the "document_id" field. +func (m *DocumentBlockMutation) SetDocumentID(i int) { + m.document_id = &i + m.adddocument_id = nil +} + +// DocumentID returns the value of the "document_id" field in the mutation. +func (m *DocumentBlockMutation) DocumentID() (r int, exists bool) { + v := m.document_id + if v == nil { + return + } + return *v, true +} + +// OldDocumentID returns the old "document_id" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldDocumentID(ctx context.Context) (v int, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldDocumentID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldDocumentID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldDocumentID: %w", err) + } + return oldValue.DocumentID, nil +} + +// AddDocumentID adds i to the "document_id" field. +func (m *DocumentBlockMutation) AddDocumentID(i int) { + if m.adddocument_id != nil { + *m.adddocument_id += i + } else { + m.adddocument_id = &i + } +} + +// AddedDocumentID returns the value that was added to the "document_id" field in this mutation. +func (m *DocumentBlockMutation) AddedDocumentID() (r int, exists bool) { + v := m.adddocument_id + if v == nil { + return + } + return *v, true +} + +// ResetDocumentID resets all changes to the "document_id" field. +func (m *DocumentBlockMutation) ResetDocumentID() { + m.document_id = nil + m.adddocument_id = nil +} + +// SetUserID sets the "user_id" field. +func (m *DocumentBlockMutation) SetUserID(s string) { + m.user_id = &s +} + +// UserID returns the value of the "user_id" field in the mutation. +func (m *DocumentBlockMutation) UserID() (r string, exists bool) { + v := m.user_id + if v == nil { + return + } + return *v, true +} + +// OldUserID returns the old "user_id" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldUserID(ctx context.Context) (v string, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUserID is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUserID requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUserID: %w", err) + } + return oldValue.UserID, nil +} + +// ResetUserID resets all changes to the "user_id" field. +func (m *DocumentBlockMutation) ResetUserID() { + m.user_id = nil +} + +// SetCreatedAt sets the "created_at" field. +func (m *DocumentBlockMutation) SetCreatedAt(t time.Time) { + m.created_at = &t +} + +// CreatedAt returns the value of the "created_at" field in the mutation. +func (m *DocumentBlockMutation) CreatedAt() (r time.Time, exists bool) { + v := m.created_at + if v == nil { + return + } + return *v, true +} + +// OldCreatedAt returns the old "created_at" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldCreatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldCreatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldCreatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldCreatedAt: %w", err) + } + return oldValue.CreatedAt, nil +} + +// ResetCreatedAt resets all changes to the "created_at" field. +func (m *DocumentBlockMutation) ResetCreatedAt() { + m.created_at = nil +} + +// SetUpdatedAt sets the "updated_at" field. +func (m *DocumentBlockMutation) SetUpdatedAt(t time.Time) { + m.updated_at = &t +} + +// UpdatedAt returns the value of the "updated_at" field in the mutation. +func (m *DocumentBlockMutation) UpdatedAt() (r time.Time, exists bool) { + v := m.updated_at + if v == nil { + return + } + return *v, true +} + +// OldUpdatedAt returns the old "updated_at" field's value of the DocumentBlock entity. +// If the DocumentBlock object wasn't provided to the builder, the object is fetched from the database. +// An error is returned if the mutation operation is not UpdateOne, or the database query fails. +func (m *DocumentBlockMutation) OldUpdatedAt(ctx context.Context) (v time.Time, err error) { + if !m.op.Is(OpUpdateOne) { + return v, errors.New("OldUpdatedAt is only allowed on UpdateOne operations") + } + if m.id == nil || m.oldValue == nil { + return v, errors.New("OldUpdatedAt requires an ID field in the mutation") + } + oldValue, err := m.oldValue(ctx) + if err != nil { + return v, fmt.Errorf("querying old value for OldUpdatedAt: %w", err) + } + return oldValue.UpdatedAt, nil +} + +// ResetUpdatedAt resets all changes to the "updated_at" field. +func (m *DocumentBlockMutation) ResetUpdatedAt() { + m.updated_at = nil +} + +// AddDocumentIDs adds the "document" edge to the Document entity by ids. +func (m *DocumentBlockMutation) AddDocumentIDs(ids ...int) { + if m.document == nil { + m.document = make(map[int]struct{}) + } + for i := range ids { + m.document[ids[i]] = struct{}{} + } +} + +// ClearDocument clears the "document" edge to the Document entity. +func (m *DocumentBlockMutation) ClearDocument() { + m.cleareddocument = true +} + +// DocumentCleared reports if the "document" edge to the Document entity was cleared. +func (m *DocumentBlockMutation) DocumentCleared() bool { + return m.cleareddocument +} + +// RemoveDocumentIDs removes the "document" edge to the Document entity by IDs. +func (m *DocumentBlockMutation) RemoveDocumentIDs(ids ...int) { + if m.removeddocument == nil { + m.removeddocument = make(map[int]struct{}) + } + for i := range ids { + delete(m.document, ids[i]) + m.removeddocument[ids[i]] = struct{}{} + } +} + +// RemovedDocument returns the removed IDs of the "document" edge to the Document entity. +func (m *DocumentBlockMutation) RemovedDocumentIDs() (ids []int) { + for id := range m.removeddocument { + ids = append(ids, id) + } + return +} + +// DocumentIDs returns the "document" edge IDs in the mutation. +func (m *DocumentBlockMutation) DocumentIDs() (ids []int) { + for id := range m.document { + ids = append(ids, id) + } + return +} + +// ResetDocument resets all changes to the "document" edge. +func (m *DocumentBlockMutation) ResetDocument() { + m.document = nil + m.cleareddocument = false + m.removeddocument = nil +} + +// Where appends a list predicates to the DocumentBlockMutation builder. +func (m *DocumentBlockMutation) Where(ps ...predicate.DocumentBlock) { + m.predicates = append(m.predicates, ps...) +} + +// WhereP appends storage-level predicates to the DocumentBlockMutation builder. Using this method, +// users can use type-assertion to append predicates that do not depend on any generated package. +func (m *DocumentBlockMutation) WhereP(ps ...func(*sql.Selector)) { + p := make([]predicate.DocumentBlock, len(ps)) + for i := range ps { + p[i] = ps[i] + } + m.Where(p...) +} + +// Op returns the operation name. +func (m *DocumentBlockMutation) Op() Op { + return m.op +} + +// SetOp allows setting the mutation operation. +func (m *DocumentBlockMutation) SetOp(op Op) { + m.op = op +} + +// Type returns the node type of this mutation (DocumentBlock). +func (m *DocumentBlockMutation) Type() string { + return m.typ +} + +// Fields returns all fields that were changed during this mutation. Note that in +// order to get all numeric fields that were incremented/decremented, call +// AddedFields(). +func (m *DocumentBlockMutation) Fields() []string { + fields := make([]string, 0, 7) + if m.name != nil { + fields = append(fields, documentblock.FieldName) + } + if m.content != nil { + fields = append(fields, documentblock.FieldContent) + } + if m._order != nil { + fields = append(fields, documentblock.FieldOrder) + } + if m.document_id != nil { + fields = append(fields, documentblock.FieldDocumentID) + } + if m.user_id != nil { + fields = append(fields, documentblock.FieldUserID) + } + if m.created_at != nil { + fields = append(fields, documentblock.FieldCreatedAt) + } + if m.updated_at != nil { + fields = append(fields, documentblock.FieldUpdatedAt) + } + return fields +} + +// Field returns the value of a field with the given name. The second boolean +// return value indicates that this field was not set, or was not defined in the +// schema. +func (m *DocumentBlockMutation) Field(name string) (ent.Value, bool) { + switch name { + case documentblock.FieldName: + return m.Name() + case documentblock.FieldContent: + return m.Content() + case documentblock.FieldOrder: + return m.Order() + case documentblock.FieldDocumentID: + return m.DocumentID() + case documentblock.FieldUserID: + return m.UserID() + case documentblock.FieldCreatedAt: + return m.CreatedAt() + case documentblock.FieldUpdatedAt: + return m.UpdatedAt() + } + return nil, false +} + +// OldField returns the old value of the field from the database. An error is +// returned if the mutation operation is not UpdateOne, or the query to the +// database failed. +func (m *DocumentBlockMutation) OldField(ctx context.Context, name string) (ent.Value, error) { + switch name { + case documentblock.FieldName: + return m.OldName(ctx) + case documentblock.FieldContent: + return m.OldContent(ctx) + case documentblock.FieldOrder: + return m.OldOrder(ctx) + case documentblock.FieldDocumentID: + return m.OldDocumentID(ctx) + case documentblock.FieldUserID: + return m.OldUserID(ctx) + case documentblock.FieldCreatedAt: + return m.OldCreatedAt(ctx) + case documentblock.FieldUpdatedAt: + return m.OldUpdatedAt(ctx) + } + return nil, fmt.Errorf("unknown DocumentBlock field %s", name) +} + +// SetField sets the value of a field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *DocumentBlockMutation) SetField(name string, value ent.Value) error { + switch name { + case documentblock.FieldName: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetName(v) + return nil + case documentblock.FieldContent: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetContent(v) + return nil + case documentblock.FieldOrder: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetOrder(v) + return nil + case documentblock.FieldDocumentID: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetDocumentID(v) + return nil + case documentblock.FieldUserID: + v, ok := value.(string) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUserID(v) + return nil + case documentblock.FieldCreatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetCreatedAt(v) + return nil + case documentblock.FieldUpdatedAt: + v, ok := value.(time.Time) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.SetUpdatedAt(v) + return nil + } + return fmt.Errorf("unknown DocumentBlock field %s", name) +} + +// AddedFields returns all numeric fields that were incremented/decremented during +// this mutation. +func (m *DocumentBlockMutation) AddedFields() []string { + var fields []string + if m.add_order != nil { + fields = append(fields, documentblock.FieldOrder) + } + if m.adddocument_id != nil { + fields = append(fields, documentblock.FieldDocumentID) + } + return fields +} + +// AddedField returns the numeric value that was incremented/decremented on a field +// with the given name. The second boolean return value indicates that this field +// was not set, or was not defined in the schema. +func (m *DocumentBlockMutation) AddedField(name string) (ent.Value, bool) { + switch name { + case documentblock.FieldOrder: + return m.AddedOrder() + case documentblock.FieldDocumentID: + return m.AddedDocumentID() + } + return nil, false +} + +// AddField adds the value to the field with the given name. It returns an error if +// the field is not defined in the schema, or if the type mismatched the field +// type. +func (m *DocumentBlockMutation) AddField(name string, value ent.Value) error { + switch name { + case documentblock.FieldOrder: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddOrder(v) + return nil + case documentblock.FieldDocumentID: + v, ok := value.(int) + if !ok { + return fmt.Errorf("unexpected type %T for field %s", value, name) + } + m.AddDocumentID(v) + return nil + } + return fmt.Errorf("unknown DocumentBlock numeric field %s", name) +} + +// ClearedFields returns all nullable fields that were cleared during this +// mutation. +func (m *DocumentBlockMutation) ClearedFields() []string { + return nil +} + +// FieldCleared returns a boolean indicating if a field with the given name was +// cleared in this mutation. +func (m *DocumentBlockMutation) FieldCleared(name string) bool { + _, ok := m.clearedFields[name] + return ok +} + +// ClearField clears the value of the field with the given name. It returns an +// error if the field is not defined in the schema. +func (m *DocumentBlockMutation) ClearField(name string) error { + return fmt.Errorf("unknown DocumentBlock nullable field %s", name) +} + +// ResetField resets all changes in the mutation for the field with the given name. +// It returns an error if the field is not defined in the schema. +func (m *DocumentBlockMutation) ResetField(name string) error { + switch name { + case documentblock.FieldName: + m.ResetName() + return nil + case documentblock.FieldContent: + m.ResetContent() + return nil + case documentblock.FieldOrder: + m.ResetOrder() + return nil + case documentblock.FieldDocumentID: + m.ResetDocumentID() + return nil + case documentblock.FieldUserID: + m.ResetUserID() + return nil + case documentblock.FieldCreatedAt: + m.ResetCreatedAt() + return nil + case documentblock.FieldUpdatedAt: + m.ResetUpdatedAt() + return nil + } + return fmt.Errorf("unknown DocumentBlock field %s", name) +} + +// AddedEdges returns all edge names that were set/added in this mutation. +func (m *DocumentBlockMutation) AddedEdges() []string { + edges := make([]string, 0, 1) + if m.document != nil { + edges = append(edges, documentblock.EdgeDocument) + } + return edges +} + +// AddedIDs returns all IDs (to other nodes) that were added for the given edge +// name in this mutation. +func (m *DocumentBlockMutation) AddedIDs(name string) []ent.Value { + switch name { + case documentblock.EdgeDocument: + ids := make([]ent.Value, 0, len(m.document)) + for id := range m.document { + ids = append(ids, id) + } + return ids + } + return nil +} + +// RemovedEdges returns all edge names that were removed in this mutation. +func (m *DocumentBlockMutation) RemovedEdges() []string { + edges := make([]string, 0, 1) + if m.removeddocument != nil { + edges = append(edges, documentblock.EdgeDocument) + } + return edges +} + +// RemovedIDs returns all IDs (to other nodes) that were removed for the edge with +// the given name in this mutation. +func (m *DocumentBlockMutation) RemovedIDs(name string) []ent.Value { + switch name { + case documentblock.EdgeDocument: + ids := make([]ent.Value, 0, len(m.removeddocument)) + for id := range m.removeddocument { + ids = append(ids, id) + } + return ids + } + return nil +} + +// ClearedEdges returns all edge names that were cleared in this mutation. +func (m *DocumentBlockMutation) ClearedEdges() []string { + edges := make([]string, 0, 1) + if m.cleareddocument { + edges = append(edges, documentblock.EdgeDocument) + } + return edges +} + +// EdgeCleared returns a boolean which indicates if the edge with the given name +// was cleared in this mutation. +func (m *DocumentBlockMutation) EdgeCleared(name string) bool { + switch name { + case documentblock.EdgeDocument: + return m.cleareddocument + } + return false +} + +// ClearEdge clears the value of the edge with the given name. It returns an error +// if that edge is not defined in the schema. +func (m *DocumentBlockMutation) ClearEdge(name string) error { + switch name { + } + return fmt.Errorf("unknown DocumentBlock unique edge %s", name) +} + +// ResetEdge resets all changes to the edge with the given name in this mutation. +// It returns an error if the edge is not defined in the schema. +func (m *DocumentBlockMutation) ResetEdge(name string) error { + switch name { + case documentblock.EdgeDocument: + m.ResetDocument() + return nil + } + return fmt.Errorf("unknown DocumentBlock edge %s", name) +} + // LibraryMutation represents an operation that mutates the Library nodes in the graph. type LibraryMutation struct { config diff --git a/ent/predicate/predicate.go b/ent/predicate/predicate.go index 7946102..221e7ac 100644 --- a/ent/predicate/predicate.go +++ b/ent/predicate/predicate.go @@ -6,5 +6,11 @@ import ( "entgo.io/ent/dialect/sql" ) +// Document is the predicate function for document builders. +type Document func(*sql.Selector) + +// DocumentBlock is the predicate function for documentblock builders. +type DocumentBlock func(*sql.Selector) + // Library is the predicate function for library builders. type Library func(*sql.Selector) diff --git a/ent/runtime.go b/ent/runtime.go index 2239764..bf839d1 100644 --- a/ent/runtime.go +++ b/ent/runtime.go @@ -5,6 +5,8 @@ package ent import ( "time" + "leafdev.top/leaf/rag/ent/document" + "leafdev.top/leaf/rag/ent/documentblock" "leafdev.top/leaf/rag/ent/library" "leafdev.top/leaf/rag/ent/schema" ) @@ -13,6 +15,26 @@ import ( // (default values, validators, hooks and policies) and stitches it // to their package variables. func init() { + documentFields := schema.Document{}.Fields() + _ = documentFields + // documentDescCreatedAt is the schema descriptor for created_at field. + documentDescCreatedAt := documentFields[2].Descriptor() + // document.DefaultCreatedAt holds the default value on creation for the created_at field. + document.DefaultCreatedAt = documentDescCreatedAt.Default.(func() time.Time) + // documentDescUpdatedAt is the schema descriptor for updated_at field. + documentDescUpdatedAt := documentFields[3].Descriptor() + // document.DefaultUpdatedAt holds the default value on creation for the updated_at field. + document.DefaultUpdatedAt = documentDescUpdatedAt.Default.(func() time.Time) + documentblockFields := schema.DocumentBlock{}.Fields() + _ = documentblockFields + // documentblockDescCreatedAt is the schema descriptor for created_at field. + documentblockDescCreatedAt := documentblockFields[5].Descriptor() + // documentblock.DefaultCreatedAt holds the default value on creation for the created_at field. + documentblock.DefaultCreatedAt = documentblockDescCreatedAt.Default.(func() time.Time) + // documentblockDescUpdatedAt is the schema descriptor for updated_at field. + documentblockDescUpdatedAt := documentblockFields[6].Descriptor() + // documentblock.DefaultUpdatedAt holds the default value on creation for the updated_at field. + documentblock.DefaultUpdatedAt = documentblockDescUpdatedAt.Default.(func() time.Time) libraryFields := schema.Library{}.Fields() _ = libraryFields // libraryDescCreatedAt is the schema descriptor for created_at field. diff --git a/ent/tx.go b/ent/tx.go index df5f384..a8b8483 100644 --- a/ent/tx.go +++ b/ent/tx.go @@ -12,6 +12,10 @@ import ( // Tx is a transactional client that is created by calling Client.Tx(). type Tx struct { config + // Document is the client for interacting with the Document builders. + Document *DocumentClient + // DocumentBlock is the client for interacting with the DocumentBlock builders. + DocumentBlock *DocumentBlockClient // Library is the client for interacting with the Library builders. Library *LibraryClient @@ -145,6 +149,8 @@ func (tx *Tx) Client() *Client { } func (tx *Tx) init() { + tx.Document = NewDocumentClient(tx.config) + tx.DocumentBlock = NewDocumentBlockClient(tx.config) tx.Library = NewLibraryClient(tx.config) } @@ -155,7 +161,7 @@ func (tx *Tx) init() { // of them in order to commit or rollback the transaction. // // If a closed transaction is embedded in one of the generated entities, and the entity -// applies a query, for example: Library.QueryXXX(), the query will be executed +// applies a query, for example: Document.QueryXXX(), the query will be executed // through the driver which created this transaction. // // Note that txDriver is not goroutine safe. diff --git a/internal/logic/base.go b/internal/logic/base.go index 4c79103..4aeef5f 100644 --- a/internal/logic/base.go +++ b/internal/logic/base.go @@ -1 +1,8 @@ package logic + +import ( + "leafdev.top/leaf/rag/ent" + "leafdev.top/leaf/rag/internal/providers" +) + +var orm = providers.MustGet[ent.Client]() diff --git a/internal/logic/document.go b/internal/logic/document.go new file mode 100644 index 0000000..4559beb --- /dev/null +++ b/internal/logic/document.go @@ -0,0 +1,25 @@ +package logic + +import ( + "context" + "leafdev.top/leaf/rag/ent" + "leafdev.top/leaf/rag/ent/document" +) + +type DocumentLogic struct { +} + +func NewDocumentLogic() *DocumentLogic { + return &DocumentLogic{} +} + +func (DocumentLogic) ListDocument(ctx context.Context) ([]*ent.Document, error) { + return orm.Document.Query().Where(document.UserIDEQ(GetUserId(ctx))).All(ctx) +} + +func (DocumentLogic) CreateDocument(ctx context.Context, name string) (*ent.Document, error) { + return orm.Document.Create(). + SetName(name). + SetUserID(GetUserId(ctx)). + Save(ctx) +} diff --git a/internal/logic/library.go b/internal/logic/library.go index 4ca297f..dd82153 100644 --- a/internal/logic/library.go +++ b/internal/logic/library.go @@ -5,20 +5,17 @@ import ( "errors" "leafdev.top/leaf/rag/ent" "leafdev.top/leaf/rag/ent/library" - "leafdev.top/leaf/rag/internal/providers" ) type LibraryLogic struct { } -var client = providers.MustGet[ent.Client]() - func NewLibraryLogic() *LibraryLogic { return &LibraryLogic{} } func (l *LibraryLogic) ListLibrary(ctx context.Context) ([]*ent.Library, error) { - return client.Library.Query().Where(library.UserID(GetUserId(ctx))).All(ctx) + return orm.Library.Query().Where(library.UserID(GetUserId(ctx))).All(ctx) } func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Library, error) { @@ -27,7 +24,7 @@ func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Lib return nil, errors.New("名称不能为空") } - return client.Library.Create(). + return orm.Library.Create(). SetName(name). SetUserID(GetUserId(ctx)). Save(ctx) diff --git a/internal/middleware/grpc/auth.go b/internal/middleware/grpc/auth.go index 0c85176..3a07ecc 100644 --- a/internal/middleware/grpc/auth.go +++ b/internal/middleware/grpc/auth.go @@ -56,3 +56,11 @@ func JwtAuth(ctx context.Context) (context.Context, error) { return context.WithValue(ctx, consts.AuthMiddlewareKey, &jwtIdToken), nil } + +func Gate(ctx context.Context) (context.Context, error) { + // 检查调用的方法 + + // + + return ctx, nil +} diff --git a/internal/migrations/atlas.sum b/internal/migrations/atlas.sum index d4f4cd3..3410112 100644 --- a/internal/migrations/atlas.sum +++ b/internal/migrations/atlas.sum @@ -1,3 +1,5 @@ -h1:gAvB+J97CWicumiwquwzeenMmWbnkb2SkIa7TZ9+r2w= -20240714155720_create_library.sql h1:JzdeQPcsm2609MV/6DbYu5N7tg583qpxoaXgFtazOzE= -20240714164619_update_user_id.sql h1:YiqNbf8FcKJCH9cj3PIFqOtr/pqzLKJgXza8nw8iO/w= +h1:sLEQ6Lxd6zOalx8i43YLFGrjyKJrnXXnj8KDE/eqF3A= +20240714155720_create_library.sql h1:nfzvzMxwqJRhmvUQSnXcK/LZDyAfy/vnjx7ZD2vY3AU= +20240714164619_update_user_id.sql h1:AUCFOY9OVkR4W/sFc7Ipb5Cz3xR4gY61jDuTN4MN7w8= +20240720092815_add_documents_tables.sql h1:S0OeUzCLWqXtA1Wi2Wvmlj/2eg2Kl0NkPNRy7Mu+2MY= +20240720093157_add_order_to_document_blocks.sql h1:6CHgxARw5yXmXfHTzFFPxqZZ8bWl3g76KHXlB3XYeLQ= diff --git a/internal/services/rag/documentService.go b/internal/services/rag/documentService.go index e686537..003f213 100644 --- a/internal/services/rag/documentService.go +++ b/internal/services/rag/documentService.go @@ -1,4 +1,47 @@ package rag -type DocumentService struct { +import ( + "context" + "github.com/mitchellh/mapstructure" + ragApi "leafdev.top/leaf/rag/api/rag" +) + +func (Service) ListDocument(ctx context.Context, req *ragApi.ListDocumentRequest) (*ragApi.ListDocumentResponse, error) { + document, err := documentLogic.ListDocument(ctx) + if err != nil { + return nil, err + } + + var documentApiResponse ragApi.ListDocumentResponse + for _, document := range document { + var documentApiResponseItem = ragApi.Document{ + Id: int64(document.ID), + Name: document.Name, + UserId: document.UserID, + CreatedAt: document.CreatedAt.String(), + UpdatedAt: document.UpdatedAt.String(), + } + documentApiResponse.Documents = append(documentApiResponse.Documents, &documentApiResponseItem) + } + + return &documentApiResponse, nil } + +func (Service) CreateDocument(ctx context.Context, req *ragApi.CreateDocumentRequest) (*ragApi.CreateDocumentResponse, error) { + document, err := documentLogic.CreateDocument(ctx, req.Name) + if err != nil { + return nil, err + } + + var documentApiResponse ragApi.CreateDocumentResponse + err = mapstructure.Decode(document, &documentApiResponse) + if err != nil { + return nil, err + } + + return &documentApiResponse, nil +} + +//func (Service) UpdateDocument(ctx context.Context, _ *ragApi.UpdateDocumentRequest) (*ragApi.UpdateDocumentResponse, error) { +// panic("implement me") +//} diff --git a/internal/services/rag/libraryservice.go b/internal/services/rag/libraryservice.go index 3ca1b1f..6760918 100644 --- a/internal/services/rag/libraryservice.go +++ b/internal/services/rag/libraryservice.go @@ -4,11 +4,8 @@ import ( "context" "github.com/mitchellh/mapstructure" ragApi "leafdev.top/leaf/rag/api/rag" - "leafdev.top/leaf/rag/internal/logic" ) -var libraryLogic = logic.NewLibraryLogic() - func (Service) ListLibrary(ctx context.Context, _ *ragApi.ListLibraryRequest) (*ragApi.ListLibraryResponse, error) { libraries, err := libraryLogic.ListLibrary(ctx) if err != nil { diff --git a/internal/services/rag/rag.go b/internal/services/rag/rag.go index 1473bc7..476c557 100644 --- a/internal/services/rag/rag.go +++ b/internal/services/rag/rag.go @@ -1,6 +1,12 @@ package rag -import ragApi "leafdev.top/leaf/rag/api/rag" +import ( + ragApi "leafdev.top/leaf/rag/api/rag" + "leafdev.top/leaf/rag/internal/logic" +) + +var libraryLogic = logic.NewLibraryLogic() +var documentLogic = logic.NewDocumentLogic() type Service struct { ragApi.UnimplementedRAGServiceServer