assistant 2.2.0a1__tar.gz → 2.2.0a2__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 (110) hide show
  1. {assistant-2.2.0a1/assistant.egg-info → assistant-2.2.0a2}/PKG-INFO +1 -1
  2. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/__init__.py +1 -1
  3. assistant-2.2.0a2/assistant/traces/__init__.py +10 -0
  4. assistant-2.2.0a2/assistant/traces/cmd.py +473 -0
  5. assistant-2.2.0a2/assistant/traces/export.py +232 -0
  6. assistant-2.2.0a2/assistant/traces/schema.py +137 -0
  7. assistant-2.2.0a2/assistant/traces/store.py +646 -0
  8. {assistant-2.2.0a1 → assistant-2.2.0a2/assistant.egg-info}/PKG-INFO +1 -1
  9. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant.egg-info/SOURCES.txt +5 -0
  10. {assistant-2.2.0a1 → assistant-2.2.0a2}/setup.py +1 -0
  11. {assistant-2.2.0a1 → assistant-2.2.0a2}/LICENSE +0 -0
  12. {assistant-2.2.0a1 → assistant-2.2.0a2}/MANIFEST.in +0 -0
  13. {assistant-2.2.0a1 → assistant-2.2.0a2}/README.md +0 -0
  14. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/__main__.py +0 -0
  15. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/agent.py +0 -0
  16. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/as_client.py +0 -0
  17. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/as_service.py +0 -0
  18. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/builtin_cmds.py +0 -0
  19. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/codecache.py +0 -0
  20. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/compress.py +0 -0
  21. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/conditions.py +0 -0
  22. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/config.py +0 -0
  23. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/entry_points/__init__.py +0 -0
  24. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/entry_points/run_assistant.py +0 -0
  25. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/envindex.py +0 -0
  26. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/execer.py +0 -0
  27. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/history.py +0 -0
  28. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/listen/__init__.py +0 -0
  29. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/listen/__main__.py +0 -0
  30. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/listen/interface.py +0 -0
  31. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/listen/main.py +0 -0
  32. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/main.py +0 -0
  33. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/memory.py +0 -0
  34. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/__init__.py +0 -0
  35. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/clear.py +0 -0
  36. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/exit.py +0 -0
  37. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/http_client.py +0 -0
  38. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/interface.py +0 -0
  39. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/message.py +0 -0
  40. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/__init__.py +0 -0
  41. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/google.py +0 -0
  42. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/llama_cpp.py +0 -0
  43. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/nim.py +0 -0
  44. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/ollama.py +0 -0
  45. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/openai.py +0 -0
  46. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/openrouter.py +0 -0
  47. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/providers/vllm.py +0 -0
  48. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/think.py +0 -0
  49. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/nlp/xonsh.py +0 -0
  50. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/parser.py +0 -0
  51. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/projectfile.py +0 -0
  52. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/__init__.py +0 -0
  53. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/bindings.py +0 -0
  54. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/completer.py +0 -0
  55. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/dialogs.py +0 -0
  56. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/floats.py +0 -0
  57. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/layout.py +0 -0
  58. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/operators.py +0 -0
  59. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/shell.py +0 -0
  60. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/strformat_utils.py +0 -0
  61. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/styles.py +0 -0
  62. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/ptk_shell/windows.py +0 -0
  63. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/say/__init__.py +0 -0
  64. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/say/interface.py +0 -0
  65. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/setup_wizard.py +0 -0
  66. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/time.py +0 -0
  67. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/tools.py +0 -0
  68. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/tools_worker.py +0 -0
  69. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/utils.py +0 -0
  70. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/version.py +0 -0
  71. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant/vocabulary.py +0 -0
  72. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant.egg-info/dependency_links.txt +0 -0
  73. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant.egg-info/entry_points.txt +0 -0
  74. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant.egg-info/requires.txt +0 -0
  75. {assistant-2.2.0a1 → assistant-2.2.0a2}/assistant.egg-info/top_level.txt +0 -0
  76. {assistant-2.2.0a1 → assistant-2.2.0a2}/pyproject.toml +0 -0
  77. {assistant-2.2.0a1 → assistant-2.2.0a2}/setup.cfg +0 -0
  78. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_bindings.py +0 -0
  79. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_builtin_cmds.py +0 -0
  80. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_compaction.py +0 -0
  81. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_compress.py +0 -0
  82. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_config.py +0 -0
  83. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_envindex.py +0 -0
  84. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_envindex_suggestions.py +0 -0
  85. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_git_hook.py +0 -0
  86. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_history_bridge.py +0 -0
  87. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_http_client.py +0 -0
  88. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_init.py +0 -0
  89. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_interrupt.py +0 -0
  90. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_interrupt_real.py +0 -0
  91. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_listen_interrupt.py +0 -0
  92. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_memory.py +0 -0
  93. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_memory_sqlite.py +0 -0
  94. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_model_compare.py +0 -0
  95. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_projectfile.py +0 -0
  96. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_setup_wizard.py +0 -0
  97. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_startup_performance.py +0 -0
  98. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_tmux_interactive.py +0 -0
  99. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_tool_handling.py +0 -0
  100. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_tools.py +0 -0
  101. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_traces_agent.py +0 -0
  102. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_traces_cmd.py +0 -0
  103. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_traces_export.py +0 -0
  104. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_traces_schema.py +0 -0
  105. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_traces_store.py +0 -0
  106. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_tts_streaming.py +0 -0
  107. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_utils.py +0 -0
  108. {assistant-2.2.0a1 → assistant-2.2.0a2}/tests/test_xontrib.py +0 -0
  109. {assistant-2.2.0a1 → assistant-2.2.0a2}/xontrib/__init__.py +0 -0
  110. {assistant-2.2.0a1 → assistant-2.2.0a2}/xontrib/assistant.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: assistant
3
- Version: 2.2.0a1
3
+ Version: 2.2.0a2
4
4
  Summary: Your very own Assistant. Because you deserve it.
5
5
  Home-page: https://gitlab.com/waser-technologies/technologies/assistant
6
6
  Author: Danny Waser
@@ -5,7 +5,7 @@ import warnings
5
5
  warnings.filterwarnings("ignore", category=DeprecationWarning, module="webrtcvad")
6
6
  warnings.filterwarnings("ignore", category=UserWarning, module="webrtcvad")
7
7
 
8
- __version__ = "2.2.0a1"
8
+ __version__ = "2.2.0a2"
9
9
 
10
10
  USERNAME = os.environ.get("USER", "root")
11
11
  HOME = os.environ.get("HOME", f"/home/{USERNAME}" if USERNAME != "root" else "/root")
@@ -0,0 +1,10 @@
1
+ """Trace storage submodule (Phase 1 + Phase 2 exports).
2
+
3
+ Public surface: the SQLite ``store``, the ``schema`` dataclasses/classifier,
4
+ the ``export`` OneOS/SFT/Arrow exporters and the ``cmd`` implementation of the
5
+ ``traces`` built-in.
6
+ """
7
+
8
+ from assistant.traces import cmd, export, schema, store
9
+
10
+ __all__ = ["store", "schema", "export", "cmd"]
@@ -0,0 +1,473 @@
1
+ """The ``traces`` built-in command (Phase 1: list/show/regen/help).
2
+
3
+ Registered in ``assistant.builtin_cmds.BUILTIN_COMMANDS`` and dispatched by
4
+ ``_try_builtin`` (``shell.py``) as ``traces_fn(rest.split())``. Rendering
5
+ follows the builtin convention: ``shell = XSH.shell.shell`` then
6
+ ``_log_md(shell, content)`` for Rich Markdown output.
7
+ """
8
+
9
+ import datetime
10
+ import json
11
+ import os
12
+ from typing import Any, Dict, List, Optional
13
+
14
+ from assistant.traces.export import export_arrow, export_oneos, export_sft
15
+ from assistant.traces.schema import Execution, Step
16
+ from assistant.traces.store import store
17
+
18
+ TRACES_HELP = """Usage: traces <subcommand> [options]
19
+
20
+ Subcommands:
21
+ list [--status X] [--model Y] [--limit N] List past conversations (default 20)
22
+ show <id> [--step N] Show conversation steps (one turn per section)
23
+ regen <id> <step> [--model <id>] Regenerate a step with the LLM
24
+ export <id|--all> [--format oneos|sft|arrow] [--output PATH|--output-dir DIR]
25
+ Export steps as OneOS/SFT/Arrow files
26
+ help Show this help"""
27
+
28
+ _VALUE_FLAGS = (
29
+ "--status",
30
+ "--model",
31
+ "--limit",
32
+ "--step",
33
+ "--format",
34
+ "--output",
35
+ "--output-dir",
36
+ )
37
+
38
+
39
+ def _log_md(shell, content):
40
+ """Helper to lazily load and log Markdown content."""
41
+ from rich.markdown import Markdown
42
+
43
+ shell.log_response(Markdown(content))
44
+
45
+
46
+ def _parse_opts(args: List[str]) -> Dict[str, Any]:
47
+ """Minimal option parser for the traces subcommands.
48
+
49
+ ``--flag value`` pairs land under the flag name (without dashes); positional
50
+ arguments land under ``_positional``; ``-h``/``--help`` sets ``help``.
51
+ """
52
+ opts: Dict[str, Any] = {"_positional": []}
53
+ i = 0
54
+ while i < len(args):
55
+ a = args[i]
56
+ if a in ("-h", "--help"):
57
+ opts["help"] = True
58
+ i += 1
59
+ elif a in _VALUE_FLAGS:
60
+ if i + 1 < len(args):
61
+ opts[a[2:]] = args[i + 1]
62
+ i += 2
63
+ else:
64
+ i += 1
65
+ elif a.startswith("--") and "=" in a:
66
+ key, value = a[2:].split("=", 1)
67
+ opts[key] = value
68
+ i += 1
69
+ else:
70
+ opts["_positional"].append(a)
71
+ i += 1
72
+ return opts
73
+
74
+
75
+ def _render_step(step: Step) -> str:
76
+ """Render one Step payload as a Markdown section."""
77
+ parts = [
78
+ f"## Step {step.step} - {step.execution} ({step.status})",
79
+ f"- model: `{step.model or '-'}`",
80
+ f"- date: {step.created_at}",
81
+ ]
82
+ if step.tokens:
83
+ parts.append(f"- tokens: {step.tokens}")
84
+ if step.edits:
85
+ parts.append(
86
+ f"- regenerated from step {step.edits[-1].get('step', '?')} "
87
+ f"({len(step.edits)} edit(s))"
88
+ )
89
+ if step.user_msg and step.user_msg.get("content"):
90
+ parts.append(f"\n**User:**\n\n{step.user_msg['content']}")
91
+ assistant = step.assistant or {}
92
+ content = assistant.get("content") or ""
93
+ if content:
94
+ parts.append(f"\n**Assistant:**\n\n{content}")
95
+ reasoning = assistant.get("reasoning") or ""
96
+ if reasoning:
97
+ parts.append(
98
+ f"\n<details><summary>Reasoning</summary>\n\n{reasoning}\n\n</details>"
99
+ )
100
+ tool_calls = assistant.get("tool_calls") or []
101
+ if tool_calls:
102
+ parts.append("\n**Tool calls:**")
103
+ for t in tool_calls:
104
+ fn = t.get("function", {})
105
+ parts.append(f"- `{fn.get('name', '?')}` {fn.get('arguments', '')}")
106
+ for tr in step.tool_results:
107
+ name = tr.get("name", "?")
108
+ content_tr = (tr.get("content") or "")[:500]
109
+ parts.append(f"\n**Tool result ({name}):**\n\n```\n{content_tr}\n```")
110
+ return "\n".join(parts)
111
+
112
+
113
+ def _traces_list(shell, args: List[str]) -> int:
114
+ opts = _parse_opts(args)
115
+ if opts.get("help"):
116
+ _log_md(shell, TRACES_HELP)
117
+ return 0
118
+ try:
119
+ limit = int(opts.get("limit", 20))
120
+ except (TypeError, ValueError):
121
+ limit = 20
122
+ conversations = store.list_conversations(
123
+ limit=limit,
124
+ status=opts.get("status"),
125
+ model=opts.get("model"),
126
+ )
127
+ if not conversations:
128
+ _log_md(shell, "No conversations recorded yet.")
129
+ return 0
130
+ lines = [
131
+ "| id | date | model | status | steps | subject |",
132
+ "|---|---|---|---|---|---|",
133
+ ]
134
+ for c in conversations:
135
+ subject = (c.subject or "")[:40] or "-"
136
+ lines.append(
137
+ f"| {c.id} | {c.created_at} | {c.model or '-'} | {c.status} | "
138
+ f"{c.n_steps} | {subject} |"
139
+ )
140
+ _log_md(shell, "\n".join(lines))
141
+ return 0
142
+
143
+
144
+ def _traces_show(shell, args: List[str]) -> int:
145
+ opts = _parse_opts(args)
146
+ if opts.get("help"):
147
+ _log_md(shell, TRACES_HELP)
148
+ return 0
149
+ positional = opts.get("_positional", [])
150
+ if not positional:
151
+ _log_md(shell, "Usage: traces show <id> [--step N]")
152
+ return 1
153
+ conv_id = positional[0]
154
+ conversation = store.get_conversation(conv_id)
155
+ if conversation is None:
156
+ _log_md(shell, f"Conversation {conv_id} not found.")
157
+ return 1
158
+ steps = store.list_steps(conv_id)
159
+ if not steps:
160
+ _log_md(shell, f"Conversation {conv_id} has no recorded steps.")
161
+ return 0
162
+ step_filter = opts.get("step")
163
+ if step_filter is not None:
164
+ steps = [s for s in steps if str(s.step) == str(step_filter)]
165
+ if not steps:
166
+ _log_md(shell, f"No step {step_filter} in conversation {conv_id}.")
167
+ return 1
168
+ header = [
169
+ f"# Conversation {conv_id}",
170
+ f"- created: {conversation.created_at}",
171
+ f"- model: {conversation.model or '-'}",
172
+ f"- status: {conversation.status}",
173
+ f"- steps: {conversation.n_steps}",
174
+ ]
175
+ sections = ["\n".join(header)] + [_render_step(s) for s in steps]
176
+ _log_md(shell, "\n\n".join(sections))
177
+ return 0
178
+
179
+
180
+ def _traces_regen(shell, args: List[str]) -> int:
181
+ opts = _parse_opts(args)
182
+ if opts.get("help"):
183
+ _log_md(shell, TRACES_HELP)
184
+ return 0
185
+ positional = opts.get("_positional", [])
186
+ if len(positional) < 2:
187
+ _log_md(shell, "Usage: traces regen <id> <step> [--model <id>]")
188
+ return 1
189
+ conv_id = positional[0]
190
+ try:
191
+ step_idx = int(positional[1])
192
+ except (TypeError, ValueError):
193
+ _log_md(shell, f"Invalid step number: {positional[1]}")
194
+ return 1
195
+ old_step = store.get_step(conv_id, step_idx)
196
+ if old_step is None:
197
+ _log_md(shell, f"Step {step_idx} not found in conversation {conv_id}.")
198
+ return 1
199
+ try:
200
+ new_step = regenerate_step(conv_id, step_idx, model_id=opts.get("model"))
201
+ except ValueError as e:
202
+ _log_md(shell, f"traces regen error: {e}")
203
+ return 1
204
+ lines = [
205
+ "# Regenerated step",
206
+ f"- conversation: `{conv_id}`",
207
+ f"- old step: {step_idx} -> new step: {new_step.step}",
208
+ "",
209
+ "## Previous",
210
+ "",
211
+ _render_step(old_step),
212
+ "",
213
+ "## New",
214
+ "",
215
+ _render_step(new_step),
216
+ ]
217
+ _log_md(shell, "\n\n".join(lines))
218
+ return 0
219
+
220
+
221
+ def _default_export_dir() -> str:
222
+ """Legacy OneOS directory: ``<ASSISTANT_PATH>/data/conversations``."""
223
+ from assistant import ASSISTANT_PATH
224
+
225
+ return os.path.join(ASSISTANT_PATH, "data", "conversations")
226
+
227
+
228
+ def _write_jsonl(path: str, records: List[Dict[str, Any]]) -> None:
229
+ os.makedirs(os.path.dirname(os.path.abspath(path)), exist_ok=True)
230
+ with open(path, "w", encoding="utf-8") as f:
231
+ for record in records:
232
+ f.write(json.dumps(record, ensure_ascii=False) + "\n")
233
+
234
+
235
+ def _export_one_conversation(
236
+ store, conv_id: str, fmt: str, output_dir: str, output: Optional[str], cfg=None
237
+ ) -> str:
238
+ """Export a single conversation and return the written path."""
239
+ if fmt == "oneos":
240
+ path = output or os.path.join(output_dir, f"{conv_id}.jsonl")
241
+ _write_jsonl(path, export_oneos(store, conv_id, cfg=cfg))
242
+ return path
243
+ if fmt == "sft":
244
+ path = output or os.path.join(output_dir, f"{conv_id}_sft.jsonl")
245
+ _write_jsonl(path, export_sft(store, conv_id))
246
+ return path
247
+ path = output or os.path.join(output_dir, f"{conv_id}.parquet")
248
+ return export_arrow(store, [conv_id], path, cfg=cfg)
249
+
250
+
251
+ def _traces_export(shell, args: List[str]) -> int:
252
+ from assistant.config import get_config
253
+
254
+ opts = _parse_opts(args)
255
+ if opts.get("help"):
256
+ _log_md(shell, TRACES_HELP)
257
+ return 0
258
+ positional = opts.get("_positional", [])
259
+ fmt = (opts.get("format") or "oneos").lower()
260
+ if fmt not in ("oneos", "sft", "arrow"):
261
+ _log_md(shell, f"traces export error: unknown format {fmt!r} (oneos|sft|arrow)")
262
+ return 1
263
+ output_dir = opts.get("output-dir") or _default_export_dir()
264
+
265
+ if "--all" in positional:
266
+ if opts.get("output"):
267
+ _log_md(
268
+ shell,
269
+ "traces export error: --output cannot be used with --all "
270
+ "(use --output-dir)",
271
+ )
272
+ return 1
273
+ # Conversations without steps (empty sessions) have nothing to export.
274
+ conversations = [
275
+ c for c in store.list_conversations(limit=1000) if c.n_steps > 0
276
+ ]
277
+ if not conversations:
278
+ _log_md(shell, "traces export error: no conversations with steps to export")
279
+ return 1
280
+ try:
281
+ if fmt == "arrow":
282
+ path = opts.get("output") or os.path.join(
283
+ output_dir, "traces_export.parquet"
284
+ )
285
+ export_arrow(
286
+ store, [c.id for c in conversations], path, cfg=get_config()
287
+ )
288
+ _log_md(
289
+ shell, f"Exported {len(conversations)} conversation(s) to `{path}`"
290
+ )
291
+ return 0
292
+ paths = [
293
+ _export_one_conversation(
294
+ store, c.id, fmt, output_dir, None, cfg=get_config()
295
+ )
296
+ for c in conversations
297
+ ]
298
+ except ValueError as e:
299
+ _log_md(shell, f"traces export error: {e}")
300
+ return 1
301
+ _log_md(shell, "Exported:\n" + "\n".join(f"- {p}" for p in paths))
302
+ return 0
303
+
304
+ if not positional:
305
+ _log_md(
306
+ shell,
307
+ "Usage: traces export <id|--all> "
308
+ "[--format oneos|sft|arrow] [--output PATH|--output-dir DIR]",
309
+ )
310
+ return 1
311
+ conv_id = positional[0]
312
+ if store.get_conversation(conv_id) is None:
313
+ _log_md(shell, f"traces export error: conversation {conv_id} not found")
314
+ return 1
315
+ try:
316
+ path = _export_one_conversation(
317
+ store, conv_id, fmt, output_dir, opts.get("output"), cfg=get_config()
318
+ )
319
+ except ValueError as e:
320
+ _log_md(shell, f"traces export error: {e}")
321
+ return 1
322
+ _log_md(shell, f"Exported to `{path}`")
323
+ return 0
324
+
325
+
326
+ def traces_fn(args: Optional[List[str]], stdin=None) -> int:
327
+ """Inspect, regenerate and export conversation traces (list/show/regen/export)."""
328
+ from xonsh.built_ins import XSH
329
+
330
+ shell = XSH.shell.shell
331
+ if args is None:
332
+ args = []
333
+ if not args or args[0] in ("-h", "--help", "help"):
334
+ _log_md(shell, TRACES_HELP)
335
+ return 0
336
+ subcmd = args[0]
337
+ try:
338
+ if subcmd == "list":
339
+ return _traces_list(shell, args[1:])
340
+ if subcmd == "show":
341
+ return _traces_show(shell, args[1:])
342
+ if subcmd == "regen":
343
+ return _traces_regen(shell, args[1:])
344
+ if subcmd == "export":
345
+ return _traces_export(shell, args[1:])
346
+ _log_md(shell, f"Unknown subcommand: {subcmd}\n\n{TRACES_HELP}")
347
+ return 1
348
+ except Exception as e:
349
+ _log_md(shell, f"traces {subcmd} error: {e}")
350
+ return 1
351
+
352
+
353
+ def _resolve_regen_model(model_id: Optional[str], cfg: Any, step_model: str) -> dict:
354
+ """Resolve the model for a regen call (Phase 7).
355
+
356
+ Cascade: an explicit ``model_id`` wins, otherwise the configured traces
357
+ role model (``traces.model``), otherwise the step's original model. An
358
+ explicit or configured-but-unknown model id raises ``ValueError`` instead
359
+ of silently falling back.
360
+ """
361
+ if model_id:
362
+ model_cfg = cfg.get_model_config(model_id)
363
+ if "id" not in model_cfg:
364
+ raise ValueError(
365
+ f"Unknown model: {model_id}. Use a model id from DEFAULT_MODELS_BY_PROVIDER "
366
+ "or the config model.<id> section."
367
+ )
368
+ elif cfg.traces_model is not None:
369
+ traces_model = cfg.traces_model
370
+ model_cfg = cfg.get_model_config(traces_model)
371
+ if "id" not in model_cfg:
372
+ raise ValueError(
373
+ f"Unknown model: {traces_model}. Use a model id from DEFAULT_MODELS_BY_PROVIDER "
374
+ "or the config model.<id> section."
375
+ )
376
+ else:
377
+ model_cfg = cfg.get_model_config(step_model)
378
+ model_cfg = dict(model_cfg)
379
+ model_cfg.setdefault("id", step_model)
380
+ model_cfg.setdefault("provider", "ollama")
381
+ return model_cfg
382
+
383
+
384
+ def regenerate_step(
385
+ conv_id: str,
386
+ step_idx: int,
387
+ *,
388
+ model_id: Optional[str] = None,
389
+ cfg: Any = None,
390
+ ) -> Step:
391
+ """Regenerate a stored step with a fresh non-streaming LLM call.
392
+
393
+ Replays the step's stored ``messages`` (run_messages) through the resolved
394
+ provider/client. The previous version is kept in the new step's ``edits``
395
+ (regen never mutates existing rows or the live conversation context).
396
+
397
+ Model resolution cascade (Phase 7): an explicit ``model_id`` wins,
398
+ otherwise the configured ``traces.model`` role model, otherwise the
399
+ step's original model.
400
+ """
401
+ from assistant.config import get_config
402
+ from assistant.nlp.providers import get_provider
403
+
404
+ if cfg is None:
405
+ cfg = get_config()
406
+ old_step = store.get_step(conv_id, step_idx)
407
+ if old_step is None:
408
+ raise ValueError(f"Step {step_idx} not found in conversation {conv_id}")
409
+ messages = old_step.messages or []
410
+ if not messages:
411
+ raise ValueError(f"Step {step_idx} has no stored messages to regenerate from")
412
+
413
+ model_cfg = _resolve_regen_model(model_id, cfg, old_step.model)
414
+
415
+ provider = get_provider(model_cfg["provider"])
416
+ provider_cfg = cfg.get_provider_config(model_cfg["provider"])
417
+ client = provider.create_client(
418
+ provider_cfg.get("endpoint", "http://localhost:11434/v1"),
419
+ provider_cfg.get("credential", "ollama"),
420
+ )
421
+ kwargs = provider.build_chat_kwargs(
422
+ model=model_cfg["id"],
423
+ messages=messages,
424
+ temperature=getattr(cfg, "temperature", 0.1),
425
+ think=bool(model_cfg.get("think", False)),
426
+ tools=old_step.tools,
427
+ )
428
+ kwargs["stream"] = False
429
+ response = client.chat.completions.create(**kwargs)
430
+
431
+ content = ""
432
+ reasoning = ""
433
+ if response.choices:
434
+ choice = response.choices[0]
435
+ msg = getattr(choice, "message", None)
436
+ if msg is not None:
437
+ content = msg.content or ""
438
+ try:
439
+ reasoning = provider.extract_thinking(response) or ""
440
+ except Exception:
441
+ reasoning = ""
442
+ usage = getattr(response, "usage", None)
443
+ tokens = None
444
+ if usage is not None:
445
+ tokens = {
446
+ "prompt_tokens": getattr(usage, "prompt_tokens", None),
447
+ "completion_tokens": getattr(usage, "completion_tokens", None),
448
+ "total_tokens": getattr(usage, "total_tokens", None),
449
+ }
450
+
451
+ steps = store.list_steps(conv_id)
452
+ next_step = max((s.step for s in steps), default=0) + 1
453
+ new_step = Step(
454
+ conversation_id=conv_id,
455
+ step=next_step,
456
+ created_at=datetime.datetime.now().isoformat(timespec="seconds"),
457
+ model=model_cfg["id"],
458
+ execution=Execution.TOOL.value,
459
+ status="ok",
460
+ user_msg=old_step.user_msg,
461
+ assistant={"content": content, "reasoning": reasoning, "tool_calls": []},
462
+ tool_results=[],
463
+ tokens=tokens,
464
+ edits=[old_step.to_dict()],
465
+ messages=messages,
466
+ tools=old_step.tools,
467
+ response_meta={
468
+ "model": model_cfg["id"],
469
+ "temperature": getattr(cfg, "temperature", 0.1),
470
+ },
471
+ )
472
+ store.record_step(new_step)
473
+ return new_step