Skip to content

Commit

Permalink
client server additions
Browse files Browse the repository at this point in the history
  • Loading branch information
sal committed Jan 18, 2025
1 parent d326e5a commit 5a65e80
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 8 deletions.
20 changes: 18 additions & 2 deletions commune/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def module(cls,
except Exception as e:
if trials == 0:
raise ValueError(f'Error in module {og_path} {e}')
return c.module(path,cache=cache, tree=c.tree(max_age=10), trials=trials-1)
return c.module(path,cache=cache, tree=c.tree(max_age=100), trials=trials-1)
module = module if cls.is_module(module) else cls.convert_module(module)
if cache:
c.module_cache[path] = module
Expand Down Expand Up @@ -330,10 +330,14 @@ def get_key(cls,key:str = None , **kwargs) -> None:

@classmethod
def files(cls,
path='./',
path=None,
search:str = None,
avoid_terms = ['__pycache__', '.git', '.ipynb_checkpoints', 'node_modules', 'artifacts', 'egg-info'],
**kwargs) -> List[str]:
if cls.module_name == 'module':
path = path or './'
else:
path = path or cls.storage_dir()
files =c.glob(path, **kwargs)
files = [f for f in files if not any([at in f for at in avoid_terms])]
if search != None:
Expand Down Expand Up @@ -1592,6 +1596,7 @@ def get_tree(cls, path, depth = 10, max_age=60, update=False, **kwargs):
def tree(cls, search=None, max_age=60,update=False, **kwargs):
local_tree = c.local_tree(update=update, max_age=max_age)
lib_tree = c.lib_tree(update=update, max_age=max_age)
# overlap the local tree over the lib tree
tree = {**lib_tree, **local_tree}
if search != None:
tree = {k:v for k,v in tree.items() if search in k}
Expand Down Expand Up @@ -1744,6 +1749,17 @@ def file2hash(self, path='./'):



def __repr__(self):
name = self.__class__.__name__
if 'c' == name:
name = 'module'
name = name.capitalize()

return f'{name}()'

def __str__(self):
return self.__repr__()

@classmethod
def help(cls, *text, module=None, **kwargs):
text = ' '.join(map(str, text))
Expand Down
18 changes: 18 additions & 0 deletions commune/modules/agent/memory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import commune as c

reducer = c.module('reduce')
class Memory:

def __init__(self, size=10000):
self.size = size
self.data = {}

def add(self, key, value):
self.data[key] = value

def search(self, query=None):
keys = list(self.data.keys())
reducer.forward(keys, query=query)



3 changes: 3 additions & 0 deletions commune/server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def call(cls,
if '/' in str(fn):
module = '.'.join(fn.split('/')[:-1])
fn = fn.split('/')[-1]
else:
module = fn
fn = 'info'
kwargs = (params or kwargs) or {}
kwargs = {**kwargs, **extra_kwargs}
return cls(module=module, network=network).forward(fn=fn,
Expand Down
26 changes: 20 additions & 6 deletions commune/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def __init__(
crypto_type = 'sr25519', # the crypto type of the key
users_path: Optional[str] = None, # the path to the user data
serializer: str = 'serializer', # the serializer used for the data
run_server = True, # if the server should be run
) -> 'Server':
module = module or 'module'
kwargs = kwargs or {}
Expand All @@ -71,7 +72,8 @@ def __init__(
self.set_functions(functions=functions, fn2cost=fn2cost)
self.set_user_path(users_path)
self.serializer = c.module(serializer)()
self.start_server()
if run_server:
self.run_server()

def set_functions(self, functions:Optional[List[str]] , fn2cost=None):
if self.free:
Expand Down Expand Up @@ -132,7 +134,7 @@ def set_key(self, key, crypto_type):
module.crypto_type = module.key.crypto_type
return {'success':True, 'message':f'Set key to {module.key.ss58_address}'}

def start_server(self,
def run_server(self,
max_bytes = 10 * 1024 * 1024 , # max bytes within the request (bytes)
allow_origins = ["*"], # allowed origins
allow_credentials =True, # allow credentials
Expand Down Expand Up @@ -591,6 +593,14 @@ def user2count(self):

def history(self, user):
return self.user_data(user)

@classmethod
def all_history(cls, module=None):
self = cls(module=module, run_server=False)
all_history = {}
for user in self.users():
all_history[user] = self.history(user)
return all_history

def user2fn2count(self):
user2fn2count = {}
Expand Down Expand Up @@ -624,7 +634,10 @@ def user_call_count(self, user):
return len(self.user_call_paths(user))

def users(self):
return os.listdir(self.users_path)
try:
return os.listdir(self.users_path)
except:
return []

def user_path2time(self, address):
user_paths = self.user_call_paths(address)
Expand All @@ -645,11 +658,12 @@ def check_user_data(self, address):
if os.path.exists(path):
os.remove(path)

def resolve_path(self, path):
return c.resolve_path(path, storage_dir=self.storage_dir())
@classmethod
def resolve_path(cls, path):
return c.resolve_path(path, storage_dir=cls.storage_dir())

def set_user_path(self, users_path):
self.users_path = users_path or self.resolve_path(f'users/{self.module.name}')
self.users_path = users_path or self.resolve_path(f'history/{self.module.name}')


def add_endpoint(self, name, fn):
Expand Down

0 comments on commit 5a65e80

Please sign in to comment.