spl-agent-engine 0.3.0__tar.gz → 0.4.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 (20) hide show
  1. {spl_agent_engine-0.3.0/spl_agent_engine.egg-info → spl_agent_engine-0.4.0}/PKG-INFO +1 -1
  2. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/pyproject.toml +1 -1
  3. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/__init__.py +3 -2
  4. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/core.py +108 -0
  5. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0/spl_agent_engine.egg-info}/PKG-INFO +1 -1
  6. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/LICENSE +0 -0
  7. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/README.md +0 -0
  8. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/setup.cfg +0 -0
  9. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/__init__.py +0 -0
  10. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/bias.py +0 -0
  11. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/goal.py +0 -0
  12. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/identity.py +0 -0
  13. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/language_style.py +0 -0
  14. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/value.py +0 -0
  15. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/feature/world.py +0 -0
  16. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine/py.typed +0 -0
  17. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine.egg-info/SOURCES.txt +0 -0
  18. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine.egg-info/dependency_links.txt +0 -0
  19. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine.egg-info/requires.txt +0 -0
  20. {spl_agent_engine-0.3.0 → spl_agent_engine-0.4.0}/spl_agent_engine.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: spl-agent-engine
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Anthropomorphic psychology engine built on SPL Pure Core V8.0 — deterministic emotion, trauma, memory, trust, and defense mechanisms.
5
5
  Author-email: NOHN-AI <ai@nohnlins.com>
6
6
  Maintainer-email: NOHN-AI <ai@nohnlins.com>
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "spl-agent-engine"
7
- version = "0.3.0"
7
+ version = "0.4.0"
8
8
  description = "Anthropomorphic psychology engine built on SPL Pure Core V8.0 — deterministic emotion, trauma, memory, trust, and defense mechanisms."
9
9
  readme = { file = "README.md", content-type = "text/markdown" }
10
10
  requires-python = ">=3.8"
@@ -24,6 +24,7 @@ from .core import (
24
24
  SPLPureCoreV7_3,
25
25
  NarrativeMapper,
26
26
  TokenUsage,
27
+ TokenStats,
27
28
  LLMAdapter,
28
29
  OpenAIAdapter,
29
30
  ClaudeAdapter,
@@ -34,10 +35,10 @@ from .core import (
34
35
  # Canonical name for V8.0 (class name kept for backward compatibility)
35
36
  SPLPureCore = SPLPureCoreV7_3
36
37
 
37
- __version__ = "0.3.0"
38
+ __version__ = "0.4.0"
38
39
  __all__ = [
39
40
  "SPLPureCore", "SPLPureCoreV7_3", "NarrativeMapper", "AuditLogger",
40
- "TokenUsage",
41
+ "TokenUsage", "TokenStats",
41
42
  "LLMAdapter", "OpenAIAdapter", "ClaudeAdapter", "ChainAdapter",
42
43
  "generate_line_with_llm",
43
44
  ]
@@ -123,6 +123,114 @@ class TokenUsage:
123
123
  model: str = ""
124
124
 
125
125
 
126
+ # ================================================================
127
+ # Token 累计统计器 — 跨调用汇总 token 用量
128
+ # ================================================================
129
+ class TokenStats:
130
+ """跨多次 LLM 调用累计统计 token 用量。
131
+
132
+ 功能:
133
+ - 累计 prompt / completion / total tokens
134
+ - 按模型分别统计
135
+ - 记录每次调用明细
136
+ - 导出 JSON 汇总报告
137
+
138
+ 用法:
139
+ stats = TokenStats()
140
+ stats.record(usage) # 传入 TokenUsage
141
+ stats.record(usage2)
142
+ stats.summary() # 汇总 dict
143
+ stats.export_json("stats.json") # 导出文件
144
+ """
145
+
146
+ def __init__(self):
147
+ self._total_prompt: int = 0
148
+ self._total_completion: int = 0
149
+ self._total_tokens: int = 0
150
+ self._call_count: int = 0
151
+ self._by_model: Dict[str, Dict[str, int]] = {}
152
+ self._history: List[Dict[str, Any]] = []
153
+
154
+ def record(self, usage: TokenUsage) -> None:
155
+ """记录一次 LLM 调用的 token 用量。"""
156
+ if usage is None:
157
+ return
158
+ self._total_prompt += usage.prompt_tokens
159
+ self._total_completion += usage.completion_tokens
160
+ self._total_tokens += usage.total_tokens
161
+ self._call_count += 1
162
+
163
+ model = usage.model or "unknown"
164
+ if model not in self._by_model:
165
+ self._by_model[model] = {
166
+ "prompt_tokens": 0,
167
+ "completion_tokens": 0,
168
+ "total_tokens": 0,
169
+ "call_count": 0,
170
+ }
171
+ m = self._by_model[model]
172
+ m["prompt_tokens"] += usage.prompt_tokens
173
+ m["completion_tokens"] += usage.completion_tokens
174
+ m["total_tokens"] += usage.total_tokens
175
+ m["call_count"] += 1
176
+
177
+ self._history.append({
178
+ "seq": self._call_count,
179
+ "model": model,
180
+ "prompt_tokens": usage.prompt_tokens,
181
+ "completion_tokens": usage.completion_tokens,
182
+ "total_tokens": usage.total_tokens,
183
+ })
184
+
185
+ def summary(self) -> Dict[str, Any]:
186
+ """返回累计统计汇总。"""
187
+ return {
188
+ "call_count": self._call_count,
189
+ "total_prompt_tokens": self._total_prompt,
190
+ "total_completion_tokens": self._total_completion,
191
+ "total_tokens": self._total_tokens,
192
+ "by_model": dict(self._by_model),
193
+ }
194
+
195
+ def history(self) -> List[Dict[str, Any]]:
196
+ """返回每次调用的明细记录。"""
197
+ return list(self._history)
198
+
199
+ def reset(self) -> None:
200
+ """清空所有统计。"""
201
+ self._total_prompt = 0
202
+ self._total_completion = 0
203
+ self._total_tokens = 0
204
+ self._call_count = 0
205
+ self._by_model.clear()
206
+ self._history.clear()
207
+
208
+ def export_json(self, path: str) -> None:
209
+ """将统计汇总 + 明细导出为 JSON 文件。"""
210
+ data = {
211
+ "summary": self.summary(),
212
+ "history": self.history(),
213
+ }
214
+ with open(path, "w", encoding="utf-8") as f:
215
+ json.dump(data, f, ensure_ascii=False, indent=2)
216
+
217
+ @property
218
+ def total_tokens(self) -> int:
219
+ return self._total_tokens
220
+
221
+ @property
222
+ def total_prompt_tokens(self) -> int:
223
+ return self._total_prompt
224
+
225
+ @property
226
+ def total_completion_tokens(self) -> int:
227
+ return self._total_completion
228
+
229
+ @property
230
+ def call_count(self) -> int:
231
+ return self._call_count
232
+
233
+
126
234
  # ================================================================
127
235
  # 1. 外部叙事映射器("自设:偏见/世界观/价值观"——纯外部策略)
128
236
  # 用户可随意替换,无需修改核心代码。
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: spl-agent-engine
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Summary: Anthropomorphic psychology engine built on SPL Pure Core V8.0 — deterministic emotion, trauma, memory, trust, and defense mechanisms.
5
5
  Author-email: NOHN-AI <ai@nohnlins.com>
6
6
  Maintainer-email: NOHN-AI <ai@nohnlins.com>