Repository navigation
Expand file tree
/
Copy pathexport_transcript.py
More file actions
219 lines (185 loc) · 7.77 KB
/
Copy pathexport_transcript.py
File metadata and controls
219 lines (185 loc) · 7.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
"""Export a DigitalOcean agent session's conversation to a Markdown file.
Run it inside the session you are moving away from:
doctl harness-runtime exec OLD_SESSION_ID -- python3 -c "$(cat export_transcript.py)"
It writes /workspace/.migrated-history/conversation.md, owned by the workspace
user. Download the workspace, upload it into a new session, and ask the new
agent to read that file. It only reads the agent's files; it changes nothing
except that one output file. Supports claude-code, codex, opencode and
openhands; the agent is detected from the session, or pass its name as the
only argument.
Prints one JSON line: {"adapter", "source", "messages", "path"}.
"""
import glob
import json
import os
import re
import shutil
import subprocess
import sys
WORKSPACE = "/workspace"
OUT_DIR = os.path.join(WORKSPACE, ".migrated-history")
OUT = os.path.join(OUT_DIR, "conversation.md")
HOME_ROOTS = ["/var/lib/plano-agent", os.path.join(WORKSPACE, ".agent-home"), "/root", "/home", "/tmp"]
RECORD_DIRS = ["/var/lib/plano-agent", os.path.join(WORKSPACE, ".agent-home")]
OHR_CONFIGS = ["/etc/plano/config.override.yaml", "/etc/plano/config.yaml"]
RUNTIME_LINE = re.compile(r"^[\s-]*runtime:\s*[\"']?([A-Za-z0-9_-]+)[\"']?\s*(?:#.*)?$", re.M)
def fail(msg):
print(json.dumps({"error": msg}))
sys.exit(1)
def record(name):
for d in RECORD_DIRS:
try:
with open(os.path.join(d, name)) as f:
return json.load(f)
except (OSError, ValueError):
continue
return {}
def newest(pattern, prefer=""):
paths = []
for root in HOME_ROOTS:
paths.extend(glob.glob(os.path.join(root, "**", pattern), recursive=True))
paths = [p for p in paths if os.path.isfile(p) and os.path.realpath(p) == p]
hits = [p for p in paths if prefer and p.endswith(prefer)]
pool = hits or paths
return max(pool, key=os.path.getmtime) if pool else None
def jsonl(path):
with open(path) as f:
for line in f:
try:
yield json.loads(line)
except ValueError:
continue
def tool(name):
return f"_[used tool: {name}]_"
def claude():
sid = record("claude-code-resume.json").get("claude_session_id") or ""
path = newest(".claude/projects/*/*.jsonl", f"/{sid}.jsonl" if sid else "")
if not path:
fail("no claude transcript found")
out = []
for d in jsonl(path):
if d.get("type") not in ("user", "assistant") or d.get("isMeta"):
continue
m = d.get("message") or {}
content = m.get("content")
parts = []
if isinstance(content, str):
parts.append(content)
for c in content if isinstance(content, list) else []:
if c.get("type") == "text":
parts.append(c.get("text", ""))
elif c.get("type") == "tool_use":
parts.append(tool(c.get("name", "?")))
text = "\n".join(p for p in parts if p.strip())
if text:
out.append((m.get("role") or d["type"], text))
return out, path
def codex():
tid = record("codex-resume.json").get("thread_id") or ""
path = newest("sessions/*/*/*/rollout-*.jsonl", f"-{tid}.jsonl" if tid else "")
if not path:
fail("no codex rollout found")
out = []
for d in jsonl(path):
if d.get("type") != "response_item":
continue
p = d.get("payload") or {}
if p.get("type") == "function_call":
out.append(("assistant", tool(p.get("name", "?"))))
if p.get("type") != "message" or p.get("role") not in ("user", "assistant"):
continue
text = "\n".join(c.get("text", "") for c in p.get("content") or [] if c.get("text"))
# The harness's own context blocks (<environment_context>, ...) are not the user's words.
if text.strip() and not text.lstrip().startswith("<"):
out.append((p["role"], text))
return out, path
def opencode():
rec = record("opencode-resume.json")
sid, home = rec.get("opencode_session_id"), rec.get("home")
if not (sid and home):
fail("no opencode resume record; cannot tell which session to export")
st = os.stat(WORKSPACE)
def demote():
os.setgroups([])
os.setgid(st.st_gid)
os.setuid(st.st_uid)
binary = shutil.which("opencode") or "/usr/local/bin/opencode"
env = {"HOME": home, "PATH": "/usr/local/bin:/usr/bin:/bin"}
res = subprocess.run([binary, "export", sid], cwd=WORKSPACE, env=env, preexec_fn=demote,
capture_output=True, timeout=120)
body = res.stdout
start = body.find(b"{")
if res.returncode != 0 or start < 0:
fail("opencode export failed: " + res.stderr.decode(errors="replace")[-400:])
data = json.loads(body[start:])
out = []
for m in data.get("messages") or []:
role = (m.get("info") or {}).get("role")
parts = []
for p in m.get("parts") or []:
if p.get("type") == "text" and not p.get("synthetic"):
parts.append(p.get("text", ""))
elif p.get("type") == "tool":
parts.append(tool(p.get("tool", "?")))
text = "\n".join(t for t in parts if t.strip())
if role in ("user", "assistant") and text:
out.append((role, text))
return out, f"opencode session {sid}"
def openhands():
convs = glob.glob(os.path.join(WORKSPACE, "workspace", "conversations", "*", "events"))
if not convs:
fail("no openhands conversation found in the workspace")
events = max(convs, key=os.path.getmtime)
out = []
for path in sorted(glob.glob(os.path.join(events, "event-*.json"))):
try:
with open(path) as f:
d = json.load(f)
except (OSError, ValueError):
continue
if d.get("kind") == "ActionEvent":
out.append(("assistant", tool((d.get("tool_name") or "?"))))
if d.get("kind") != "MessageEvent":
continue
lm = d.get("llm_message") or {}
text = "\n".join(c.get("text", "") for c in lm.get("content") or [] if c.get("type") == "text")
if text.strip():
out.append(("user" if d.get("source") == "user" else "assistant", text))
return out, events
EXPORTERS = {"claude-code": claude, "codex": codex, "opencode": opencode, "openhands": openhands}
def detect():
for path in OHR_CONFIGS:
try:
with open(path) as f:
names = set(RUNTIME_LINE.findall(f.read()))
except OSError:
continue
if len(names) == 1 and next(iter(names)) in EXPORTERS:
return names.pop()
fail(f"{path} names {sorted(names) or 'no runtime'}; pass one of {', '.join(EXPORTERS)}")
fail("no OHR config found; pass one of " + ", ".join(EXPORTERS))
def main():
if len(sys.argv) > 2 or (len(sys.argv) == 2 and sys.argv[1] not in EXPORTERS):
fail(f"usage: export_transcript.py [{'|'.join(EXPORTERS)}]")
adapter = sys.argv[1] if len(sys.argv) == 2 else detect()
messages, source = EXPORTERS[adapter]()
if not messages:
fail(f"the {adapter} conversation in {source} has no messages")
lines = [
"# Previous conversation",
"",
f"Exported from an earlier {adapter} session. Messages are in order; tool calls are",
"listed by name only, without their inputs or results.",
"",
]
for role, text in messages:
lines += [f"## {'User' if role == 'user' else 'Assistant'}", "", text.strip(), ""]
os.makedirs(OUT_DIR, exist_ok=True)
with open(OUT, "w") as f:
f.write("\n".join(lines))
st = os.stat(WORKSPACE)
for p in (OUT_DIR, OUT):
os.chown(p, st.st_uid, st.st_gid)
print(json.dumps({"adapter": adapter, "source": source, "messages": len(messages), "path": OUT}))
if __name__ == "__main__":
main()