sefios 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. sefios-0.1.0/.gitignore +210 -0
  2. sefios-0.1.0/LICENSE +21 -0
  3. sefios-0.1.0/PKG-INFO +101 -0
  4. sefios-0.1.0/README.md +38 -0
  5. sefios-0.1.0/pyproject.toml +63 -0
  6. sefios-0.1.0/src/sefios/__init__.py +23 -0
  7. sefios-0.1.0/src/sefios/_scope.py +123 -0
  8. sefios-0.1.0/src/sefios/_session_state.py +147 -0
  9. sefios-0.1.0/src/sefios/_state_store.py +68 -0
  10. sefios-0.1.0/src/sefios/cli/__init__.py +43 -0
  11. sefios-0.1.0/src/sefios/cli/_app.py +266 -0
  12. sefios-0.1.0/src/sefios/exceptions.py +23 -0
  13. sefios-0.1.0/src/sefios/fastapi/__init__.py +37 -0
  14. sefios-0.1.0/src/sefios/fastapi/_app.py +193 -0
  15. sefios-0.1.0/src/sefios/handlers/__init__.py +9 -0
  16. sefios-0.1.0/src/sefios/handlers/_cost.py +29 -0
  17. sefios-0.1.0/src/sefios/history_storages/__init__.py +3 -0
  18. sefios-0.1.0/src/sefios/history_storages/_session.py +37 -0
  19. sefios-0.1.0/src/sefios/middleware/__init__.py +15 -0
  20. sefios-0.1.0/src/sefios/middleware/_compaction.py +47 -0
  21. sefios-0.1.0/src/sefios/middleware/_input.py +94 -0
  22. sefios-0.1.0/src/sefios/middleware/_max_steps.py +36 -0
  23. sefios-0.1.0/src/sefios/middleware/_retry.py +61 -0
  24. sefios-0.1.0/src/sefios/middleware/_stagnation.py +62 -0
  25. sefios-0.1.0/src/sefios/policies/__init__.py +5 -0
  26. sefios-0.1.0/src/sefios/policies/_default.py +23 -0
  27. sefios-0.1.0/src/sefios/py.typed +0 -0
  28. sefios-0.1.0/src/sefios/sessions/__init__.py +13 -0
  29. sefios-0.1.0/src/sefios/sessions/_manager.py +122 -0
  30. sefios-0.1.0/src/sefios/state.py +125 -0
  31. sefios-0.1.0/src/sefios/storage/__init__.py +5 -0
  32. sefios-0.1.0/src/sefios/storage/_base.py +27 -0
  33. sefios-0.1.0/src/sefios/storage/_file.py +67 -0
  34. sefios-0.1.0/src/sefios/storage/_memory.py +30 -0
  35. sefios-0.1.0/src/sefios/tools/__init__.py +12 -0
  36. sefios-0.1.0/src/sefios/tools/input.py +128 -0
  37. sefios-0.1.0/src/sefios/tools/output.py +82 -0
  38. sefios-0.1.0/src/sefios/tools/web.py +38 -0
  39. sefios-0.1.0/tests/conftest.py +34 -0
  40. sefios-0.1.0/tests/scenarios/test_file_backed_resume.py +94 -0
  41. sefios-0.1.0/tests/scenarios/test_history_compaction.py +131 -0
  42. sefios-0.1.0/tests/scenarios/test_output_tool.py +78 -0
  43. sefios-0.1.0/tests/scenarios/test_stagnation_policy.py +81 -0
  44. sefios-0.1.0/tests/scenarios/test_stateful_tools.py +222 -0
  45. sefios-0.1.0/tests/units/cli/test_cli_facade.py +105 -0
  46. sefios-0.1.0/tests/units/fastapi/test_http_facade.py +99 -0
  47. sefios-0.1.0/tests/units/handlers/test_cost_calculator.py +58 -0
  48. sefios-0.1.0/tests/units/middleware/test_compaction_middleware.py +73 -0
  49. sefios-0.1.0/tests/units/middleware/test_input_middleware.py +194 -0
  50. sefios-0.1.0/tests/units/middleware/test_max_steps_middleware.py +54 -0
  51. sefios-0.1.0/tests/units/middleware/test_retry_middleware.py +110 -0
  52. sefios-0.1.0/tests/units/middleware/test_stagnation_middleware.py +100 -0
  53. sefios-0.1.0/tests/units/policies/test_default_policy.py +16 -0
  54. sefios-0.1.0/tests/units/sessions/test_session_manager.py +119 -0
  55. sefios-0.1.0/tests/units/storage/test_session_storage.py +82 -0
  56. sefios-0.1.0/tests/units/test_optional_extras.py +79 -0
  57. sefios-0.1.0/tests/units/test_scope.py +21 -0
  58. sefios-0.1.0/tests/units/test_session_history_storage.py +76 -0
  59. sefios-0.1.0/tests/units/test_session_state.py +144 -0
  60. sefios-0.1.0/tests/units/test_state_container.py +99 -0
  61. sefios-0.1.0/tests/units/test_state_store.py +117 -0
  62. sefios-0.1.0/tests/units/tools/test_input_tool.py +33 -0
  63. sefios-0.1.0/tests/units/tools/test_output_tool.py +33 -0
@@ -0,0 +1,210 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ #uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ #poetry.lock
109
+ #poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ #pdm.lock
116
+ #pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ #pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # SageMath parsed files
135
+ *.sage.py
136
+
137
+ # Environments
138
+ .env
139
+ .envrc
140
+ .venv
141
+ env/
142
+ venv/
143
+ ENV/
144
+ env.bak/
145
+ venv.bak/
146
+
147
+ # Spyder project settings
148
+ .spyderproject
149
+ .spyproject
150
+
151
+ # Rope project settings
152
+ .ropeproject
153
+
154
+ # mkdocs documentation
155
+ /site
156
+
157
+ # mypy
158
+ .mypy_cache/
159
+ .dmypy.json
160
+ dmypy.json
161
+
162
+ # Pyre type checker
163
+ .pyre/
164
+
165
+ # pytype static type analyzer
166
+ .pytype/
167
+
168
+ # Cython debug symbols
169
+ cython_debug/
170
+
171
+ # PyCharm
172
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
173
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
174
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
175
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
176
+ #.idea/
177
+
178
+ # Abstra
179
+ # Abstra is an AI-powered process automation framework.
180
+ # Ignore directories containing user credentials, local state, and settings.
181
+ # Learn more at https://abstra.io/docs
182
+ .abstra/
183
+
184
+ # Visual Studio Code
185
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
186
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
187
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
188
+ # you could uncomment the following to ignore the entire vscode folder
189
+ # .vscode/
190
+
191
+ # Ruff stuff:
192
+ .ruff_cache/
193
+
194
+ # PyPI configuration file
195
+ .pypirc
196
+
197
+ # Cursor
198
+ # Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
199
+ # exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
200
+ # refer to https://docs.cursor.com/context/ignore-files
201
+ .cursorignore
202
+ .cursorindexingignore
203
+
204
+ # Marimo
205
+ marimo/_static/
206
+ marimo/_lsp/
207
+ __marimo__/
208
+
209
+ /out/
210
+ /.local/
sefios-0.1.0/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nueruyu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
sefios-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,101 @@
1
+ Metadata-Version: 2.4
2
+ Name: sefios
3
+ Version: 0.1.0
4
+ Summary: Opinionated Stack for building applications with the Sefia framework.
5
+ Project-URL: Homepage, https://github.com/nueruyu/sefia
6
+ Project-URL: Repository, https://github.com/nueruyu/sefia
7
+ Project-URL: Issues, https://github.com/nueruyu/sefia/issues
8
+ Project-URL: Documentation, https://github.com/nueruyu/sefia/tree/main/docs
9
+ Author: nueruyu
10
+ License: MIT License
11
+
12
+ Copyright (c) 2026 nueruyu
13
+
14
+ Permission is hereby granted, free of charge, to any person obtaining a copy
15
+ of this software and associated documentation files (the "Software"), to deal
16
+ in the Software without restriction, including without limitation the rights
17
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+ copies of the Software, and to permit persons to whom the Software is
19
+ furnished to do so, subject to the following conditions:
20
+
21
+ The above copyright notice and this permission notice shall be included in all
22
+ copies or substantial portions of the Software.
23
+
24
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
+ SOFTWARE.
31
+ License-File: LICENSE
32
+ Keywords: agents,durable-execution,human-in-the-loop,llm,sefia
33
+ Classifier: Development Status :: 3 - Alpha
34
+ Classifier: Framework :: AsyncIO
35
+ Classifier: Intended Audience :: Developers
36
+ Classifier: License :: OSI Approved :: MIT License
37
+ Classifier: Operating System :: OS Independent
38
+ Classifier: Programming Language :: Python :: 3
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Classifier: Programming Language :: Python :: 3.13
42
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
43
+ Classifier: Typing :: Typed
44
+ Requires-Python: >=3.11
45
+ Requires-Dist: glyff-file-store>=0.13.0
46
+ Requires-Dist: glyff-pydantic>=0.13.0
47
+ Requires-Dist: glyff>=0.13.0
48
+ Requires-Dist: sefia>=0.1.0
49
+ Provides-Extra: all
50
+ Requires-Dist: ddgs>=9.0; extra == 'all'
51
+ Requires-Dist: sefia-fastapi>=0.1.0; extra == 'all'
52
+ Requires-Dist: sefia-litellm>=0.1.0; extra == 'all'
53
+ Requires-Dist: sefia-typer>=0.1.0; extra == 'all'
54
+ Provides-Extra: cli
55
+ Requires-Dist: sefia-typer>=0.1.0; extra == 'cli'
56
+ Provides-Extra: fastapi
57
+ Requires-Dist: sefia-fastapi>=0.1.0; extra == 'fastapi'
58
+ Provides-Extra: litellm
59
+ Requires-Dist: sefia-litellm>=0.1.0; extra == 'litellm'
60
+ Provides-Extra: web
61
+ Requires-Dist: ddgs>=9.0; extra == 'web'
62
+ Description-Content-Type: text/markdown
63
+
64
+ # sefios
65
+
66
+ Opinionated Stack for building applications with the
67
+ [Sefia](https://pypi.org/project/sefia/) framework.
68
+
69
+ Where `sefia` is the core framework (decorators, session, tool system),
70
+ `sefios` layers the pieces an application actually ships with: session
71
+ scoping and persistence, state containers, built-in tools, and integration
72
+ wiring for CLI and HTTP front ends.
73
+
74
+ ## Install
75
+
76
+ ```bash
77
+ pip install 'sefios[litellm]'
78
+ ```
79
+
80
+ Extras:
81
+
82
+ | Extra | Pulls in | Purpose |
83
+ | ----------- | ----------------------------------- | -------------------------------- |
84
+ | `litellm` | [`sefia-litellm`](https://pypi.org/project/sefia-litellm/) | LLM providers via LiteLLM |
85
+ | `cli` | [`sefia-typer`](https://pypi.org/project/sefia-typer/) | Typer (CLI) integration |
86
+ | `fastapi` | [`sefia-fastapi`](https://pypi.org/project/sefia-fastapi/) | FastAPI (HTTP) integration |
87
+ | `web` | `ddgs` | Built-in web search tool |
88
+ | `all` | all of the above | |
89
+
90
+ ## Documentation
91
+
92
+ See the [repository](https://github.com/nueruyu/sefia) for the full README,
93
+ tutorial, and architecture docs.
94
+
95
+ ## Status
96
+
97
+ Early development. APIs may change before v1.0.
98
+
99
+ ## License
100
+
101
+ MIT
sefios-0.1.0/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # sefios
2
+
3
+ Opinionated Stack for building applications with the
4
+ [Sefia](https://pypi.org/project/sefia/) framework.
5
+
6
+ Where `sefia` is the core framework (decorators, session, tool system),
7
+ `sefios` layers the pieces an application actually ships with: session
8
+ scoping and persistence, state containers, built-in tools, and integration
9
+ wiring for CLI and HTTP front ends.
10
+
11
+ ## Install
12
+
13
+ ```bash
14
+ pip install 'sefios[litellm]'
15
+ ```
16
+
17
+ Extras:
18
+
19
+ | Extra | Pulls in | Purpose |
20
+ | ----------- | ----------------------------------- | -------------------------------- |
21
+ | `litellm` | [`sefia-litellm`](https://pypi.org/project/sefia-litellm/) | LLM providers via LiteLLM |
22
+ | `cli` | [`sefia-typer`](https://pypi.org/project/sefia-typer/) | Typer (CLI) integration |
23
+ | `fastapi` | [`sefia-fastapi`](https://pypi.org/project/sefia-fastapi/) | FastAPI (HTTP) integration |
24
+ | `web` | `ddgs` | Built-in web search tool |
25
+ | `all` | all of the above | |
26
+
27
+ ## Documentation
28
+
29
+ See the [repository](https://github.com/nueruyu/sefia) for the full README,
30
+ tutorial, and architecture docs.
31
+
32
+ ## Status
33
+
34
+ Early development. APIs may change before v1.0.
35
+
36
+ ## License
37
+
38
+ MIT
@@ -0,0 +1,63 @@
1
+ [project]
2
+ name = "sefios"
3
+ description = "Opinionated Stack for building applications with the Sefia framework."
4
+ readme = "README.md"
5
+ license = { file = "LICENSE" }
6
+ authors = [{ name = "nueruyu" }]
7
+ requires-python = ">=3.11"
8
+ dependencies = [
9
+ "sefia>=0.1.0",
10
+ "glyff>=0.13.0",
11
+ "glyff-file-store>=0.13.0",
12
+ "glyff-pydantic>=0.13.0",
13
+ ]
14
+ dynamic = ["version"]
15
+ keywords = [
16
+ "llm",
17
+ "agents",
18
+ "durable-execution",
19
+ "human-in-the-loop",
20
+ "sefia",
21
+ ]
22
+ classifiers = [
23
+ "Programming Language :: Python :: 3",
24
+ "Programming Language :: Python :: 3.11",
25
+ "Programming Language :: Python :: 3.12",
26
+ "Programming Language :: Python :: 3.13",
27
+ "License :: OSI Approved :: MIT License",
28
+ "Operating System :: OS Independent",
29
+ "Development Status :: 3 - Alpha",
30
+ "Framework :: AsyncIO",
31
+ "Intended Audience :: Developers",
32
+ "Topic :: Scientific/Engineering :: Artificial Intelligence",
33
+ "Typing :: Typed",
34
+ ]
35
+
36
+ [project.optional-dependencies]
37
+ litellm = ["sefia-litellm>=0.1.0"]
38
+ web = ["ddgs>=9.0"]
39
+ cli = ["sefia-typer>=0.1.0"]
40
+ fastapi = ["sefia-fastapi>=0.1.0"]
41
+ all = [
42
+ "sefios[litellm]",
43
+ "sefios[web]",
44
+ "sefios[cli]",
45
+ "sefios[fastapi]",
46
+ ]
47
+
48
+ [project.urls]
49
+ Homepage = "https://github.com/nueruyu/sefia"
50
+ Repository = "https://github.com/nueruyu/sefia"
51
+ Issues = "https://github.com/nueruyu/sefia/issues"
52
+ Documentation = "https://github.com/nueruyu/sefia/tree/main/docs"
53
+
54
+ [build-system]
55
+ requires = ["hatchling", "hatch-vcs"]
56
+ build-backend = "hatchling.build"
57
+
58
+ [tool.hatch.build.targets.wheel]
59
+ packages = ["src/sefios"]
60
+
61
+ [tool.hatch.version]
62
+ source = "vcs"
63
+ raw-options = { root = "../.." }
@@ -0,0 +1,23 @@
1
+ """Opinionated stack for building applications with the Sefia framework."""
2
+
3
+ from ._scope import SessionScope
4
+ from ._session_state import get_call_state_store, get_session_storage
5
+ from ._state_store import StateStore
6
+ from .exceptions import NeedsInput
7
+ from .state import StateContainer, StateRegistry, get_state, state
8
+ from .storage import FileSessionStorage, MemorySessionStorage, SessionStorage
9
+
10
+ __all__ = [
11
+ "SessionScope",
12
+ "NeedsInput",
13
+ "SessionStorage",
14
+ "StateStore",
15
+ "MemorySessionStorage",
16
+ "FileSessionStorage",
17
+ "get_call_state_store",
18
+ "get_session_storage",
19
+ "StateContainer",
20
+ "StateRegistry",
21
+ "get_state",
22
+ "state",
23
+ ]
@@ -0,0 +1,123 @@
1
+ from collections.abc import AsyncIterator, Callable
2
+ from contextlib import asynccontextmanager
3
+ from pathlib import Path
4
+
5
+ import glyff
6
+ import glyff_file_store
7
+ import sefia
8
+ from glyff_pydantic import PydanticArgsHasher, PydanticSerializer
9
+ from sefia import HistoryStorage, Profile, Policy
10
+ from sefia.llm import LLMClient
11
+
12
+ from ._session_state import bind_session_storage
13
+ from .policies import DefaultPolicy
14
+ from .storage import FileSessionStorage, SessionStorage
15
+
16
+
17
+ class SessionScope:
18
+ """
19
+ Manages shared configuration for Sefia sessions and provides helpers to run
20
+ code within a configured session context.
21
+
22
+ ``session_storage_factory`` is the seam for a custom session-state
23
+ persistence backend: it receives the session id and returns the
24
+ :class:`SessionStorage` to bind for that session. By default a
25
+ :class:`FileSessionStorage` under ``session_dir`` is used.
26
+
27
+ ``history_storage`` selects where run history is persisted; defaults to the
28
+ run's glyff metadata (:class:`~sefia.history_storages.GlyffHistoryStorage`).
29
+ """
30
+
31
+ def __init__(
32
+ self,
33
+ *,
34
+ session_dir: Path,
35
+ model: str | None = None,
36
+ llm_client: LLMClient | None = None,
37
+ policies: list[Policy] | None = None,
38
+ profiles: list[Profile] | None = None,
39
+ stream: bool = False,
40
+ max_steps: int | None = 25,
41
+ max_repair_attempts: int = 2,
42
+ session_storage_factory: Callable[[str], SessionStorage] | None = None,
43
+ history_storage: HistoryStorage | None = None,
44
+ ):
45
+ self.session_dir = session_dir
46
+ self.model = model
47
+ self.llm_client = llm_client
48
+ self.policies = list(policies or [])
49
+ self.profiles = list(profiles or [])
50
+ self.stream = stream
51
+ self.max_steps = max_steps
52
+ self.max_repair_attempts = max_repair_attempts
53
+ self.session_storage_factory = session_storage_factory
54
+ self.history_storage = history_storage
55
+
56
+ @asynccontextmanager
57
+ async def session(
58
+ self,
59
+ *,
60
+ session_id: str,
61
+ model: str | None = None,
62
+ stream: bool | None = None,
63
+ policies: list[Policy] | None = None,
64
+ profiles: list[Profile] | None = None,
65
+ ) -> AsyncIterator[sefia.Session]:
66
+ """Run code within a configured Sefia session context."""
67
+ llm_client = self.llm_client
68
+ resolved_model = model or self.model
69
+ resolved_stream = self.stream if stream is None else stream
70
+
71
+ if llm_client is None:
72
+ if resolved_model is None:
73
+ raise ValueError("Either llm_client or model must be provided.")
74
+ try:
75
+ import sefia_litellm
76
+ except ImportError as e:
77
+ raise ImportError(
78
+ "The 'litellm' extra is required to use the default session "
79
+ "setup. Please install it with: pip install 'sefios[litellm]'"
80
+ ) from e
81
+ llm_client = sefia_litellm.LiteLLMClient(model=resolved_model)
82
+
83
+ serializer = PydanticSerializer()
84
+
85
+ backend = glyff_file_store.JsonFileBackend(
86
+ base_dir=self.session_dir / "glyff_sessions",
87
+ session_id=session_id,
88
+ )
89
+ gs = glyff.Session(
90
+ id=session_id,
91
+ backend=backend,
92
+ serializer=serializer,
93
+ hasher=PydanticArgsHasher(),
94
+ )
95
+ if self.session_storage_factory is not None:
96
+ session_storage = self.session_storage_factory(session_id)
97
+ else:
98
+ session_storage = FileSessionStorage(
99
+ base_dir=self.session_dir / "sefia_metadata" / session_id,
100
+ serializer=serializer,
101
+ )
102
+
103
+ final_policies: list[Policy] = list(self.policies)
104
+ if policies is not None:
105
+ final_policies.extend(policies)
106
+ final_policies.append(DefaultPolicy(max_steps=self.max_steps))
107
+
108
+ final_profiles: list[Profile] = list(self.profiles)
109
+ if profiles is not None:
110
+ final_profiles.extend(profiles)
111
+
112
+ async with gs:
113
+ with bind_session_storage(session_storage):
114
+ async with sefia.Session(
115
+ llm_client=llm_client,
116
+ glyff_session=gs,
117
+ policies=final_policies,
118
+ profiles=final_profiles,
119
+ stream=resolved_stream,
120
+ history_storage=self.history_storage,
121
+ max_repair_attempts=self.max_repair_attempts,
122
+ ) as session:
123
+ yield session