-
Notifications
You must be signed in to change notification settings - Fork 513
Expand file tree
/
Copy pathqwenpaw.go
More file actions
346 lines (327 loc) · 9.86 KB
/
Copy pathqwenpaw.go
File metadata and controls
346 lines (327 loc) · 9.86 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// ABOUTME: Parses QwenPaw sessions/<name>.json files into structured session data.
// ABOUTME: Handles Anthropic-style content blocks (text/thinking/tool_use/
// ABOUTME: tool_result) with system-role carriers for tool results.
package parser
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/tidwall/gjson"
)
// IsValidQwenPawIDPart accepts workspace names and session file
// stems. QwenPaw emits channel-scoped filenames containing dots,
// at-signs, and double dashes (e.g. "<userId>@im.wechat_wechat--..."),
// so the check is permissive — but it still rejects path traversal
// components (".", "..") since those never appear in a real session
// stem and would let a crafted raw ID escape the QwenPaw root.
//
// It also rejects characters that are structurally significant once a
// part is joined into a session ID:
//
// - ":" joins ID parts in qwenpawSessionID. A stem "foo:bar" would
// produce qwenpaw:<workspace>:foo:bar, which source lookup reparses
// as the sessions/foo/bar.json subdir layout.
// - "~" is the remote-host separator (see StripHostPrefix). A part
// containing it would be split off as a bogus host prefix.
// - "?", "#", and "%" are URL delimiters. Session IDs are
// interpolated into API path segments that are not all percent-
// encoded (e.g. the export and watch routes), so these would
// truncate or corrupt the path.
func IsValidQwenPawIDPart(s string) bool {
if s == "" || s == "." || s == ".." {
return false
}
if strings.ContainsAny(s, "/\\:~?#%") {
return false
}
if strings.ContainsRune(s, 0) {
return false
}
return true
}
// qwenpawSessionID builds the canonical session ID for a file at
// `path`. Layout determines the namespace:
//
// - <workspace>/sessions/<stem>.json -> qwenpaw:<workspace>:<stem>
// - <workspace>/sessions/<subdir>/<stem>.json -> qwenpaw:<workspace>:<subdir>:<stem>
//
// The subdir segment prevents collisions when both
// sessions/foo.json and sessions/console/foo.json exist in the same
// workspace (otherwise both would become qwenpaw:<workspace>:foo
// and one would overwrite the other during sync).
//
// Every component is validated with IsValidQwenPawIDPart before
// joining: a ":" (the part separator) or path separator in any of
// workspace/subdir/stem would make the ID ambiguous, so such a file
// is rejected rather than silently colliding with another session.
func qwenpawSessionID(path, project, stem string) (string, error) {
if !IsValidQwenPawIDPart(project) {
return "", fmt.Errorf(
"qwenpaw: invalid workspace %q for %s", project, path,
)
}
if !IsValidQwenPawIDPart(stem) {
return "", fmt.Errorf(
"qwenpaw: invalid session stem %q for %s", stem, path,
)
}
parent := filepath.Base(filepath.Dir(path))
if parent == "sessions" {
return "qwenpaw:" + project + ":" + stem, nil
}
if !IsValidQwenPawIDPart(parent) {
return "", fmt.Errorf(
"qwenpaw: invalid subdir %q for %s", parent, path,
)
}
return "qwenpaw:" + project + ":" + parent + ":" + stem, nil
}
// parseSession parses a QwenPaw sessions/<name>.json file.
//
// The on-disk shape is:
//
// {
// "agent": {
// "memory": {
// "content": [[message, []], [message, []], ...]
// }
// }
// }
//
// Each message has fields:
//
// - id: message identifier (string)
// - name: sender name ("user", "<AgentName>", or "system")
// - role: "user" | "assistant" | "system"
// - content: array of content blocks (text/thinking/tool_use/tool_result)
// - metadata: opaque object
// - timestamp: "YYYY-MM-DD HH:MM:SS.fff" (local time, milliseconds)
//
// System-role messages carry tool_result blocks (QwenPaw's equivalent
// of Anthropic's user-side tool_result). They map to RoleUser +
// IsSystem so they remain distinguishable from real user turns
// without inflating UserMessageCount.
func parseQwenPawSession(
path, project, machine string,
) (*ParsedSession, []ParsedMessage, error) {
raw, err := os.ReadFile(path)
if err != nil {
return nil, nil, fmt.Errorf("read %s: %w", path, err)
}
info, err := os.Stat(path)
if err != nil {
return nil, nil, fmt.Errorf("stat %s: %w", path, err)
}
// gjson.GetBytes silently returns "not found" on malformed JSON,
// which would otherwise surface as "agent.memory.content missing"
// — masking the real cause. Validate up front so syntax errors
// are reported as such.
if !gjson.ValidBytes(raw) {
return nil, nil, fmt.Errorf(
"qwenpaw: malformed JSON in %s", path,
)
}
stem := strings.TrimSuffix(filepath.Base(path), ".json")
id, err := qwenpawSessionID(path, project, stem)
if err != nil {
return nil, nil, err
}
sess := &ParsedSession{
ID: id,
Project: project,
Machine: machine,
Agent: AgentQwenPaw,
}
contentArr := gjson.GetBytes(raw, "agent.memory.content")
if !contentArr.Exists() {
return nil, nil, fmt.Errorf(
"qwenpaw: agent.memory.content missing in %s", path,
)
}
if !contentArr.IsArray() {
return nil, nil, fmt.Errorf(
"qwenpaw: agent.memory.content not an array in %s", path,
)
}
var messages []ParsedMessage
var malformed int
ordinal := 0
contentArr.ForEach(func(_, entry gjson.Result) bool {
if !entry.IsArray() {
malformed++
return true
}
items := entry.Array()
if len(items) == 0 {
malformed++
return true
}
msgJSON := items[0]
if !msgJSON.IsObject() {
malformed++
return true
}
pm, ok := parseQwenPawMessage(msgJSON, ordinal)
if !ok {
malformed++
return true
}
ordinal++
messages = append(messages, pm)
return true
})
sess.MalformedLines = malformed
sess.MessageCount = len(messages)
for _, m := range messages {
if m.Role == RoleUser && !m.IsSystem {
sess.UserMessageCount++
}
}
if len(messages) > 0 {
sess.StartedAt = messages[0].Timestamp
sess.EndedAt = messages[len(messages)-1].Timestamp
}
for _, m := range messages {
if m.Role == RoleUser && !m.IsSystem && strings.TrimSpace(m.Content) != "" {
sess.FirstMessage = truncateFirstMessage(m.Content)
break
}
}
populateQwenPawFileFields(sess, info, path)
if messages == nil {
return sess, nil, nil
}
return sess, messages, nil
}
// parseQwenPawMessage turns one gjson message object into a
// ParsedMessage. Returns ok=false when the role is missing or
// unrecognized.
func parseQwenPawMessage(
msg gjson.Result, ordinal int,
) (ParsedMessage, bool) {
roleStr := msg.Get("role").Str
role, isSystem, ok := qwenpawRole(roleStr)
if !ok {
return ParsedMessage{}, false
}
pm := ParsedMessage{
Ordinal: ordinal,
Role: role,
IsSystem: isSystem,
Timestamp: parseQwenPawTimestamp(msg.Get("timestamp").Str),
}
var textParts []string
content := msg.Get("content")
if content.IsArray() {
content.ForEach(func(_, block gjson.Result) bool {
switch block.Get("type").Str {
case "text":
if t := block.Get("text").Str; t != "" {
textParts = append(textParts, t)
}
case "thinking":
if th := block.Get("thinking").Str; th != "" {
pm.HasThinking = true
if pm.ThinkingText != "" {
pm.ThinkingText += "\n"
}
pm.ThinkingText += th
}
case "tool_use":
tc := ParsedToolCall{
ToolUseID: block.Get("id").Str,
ToolName: block.Get("name").Str,
Category: NormalizeToolCategory(block.Get("name").Str),
}
tc.InputJSON = qwenpawToolInputJSON(block)
pm.ToolCalls = append(pm.ToolCalls, tc)
pm.HasToolUse = true
case "tool_result":
output := block.Get("output")
tr := ParsedToolResult{
ToolUseID: block.Get("id").Str,
ContentLength: toolResultContentLength(output),
ContentRaw: output.Raw,
}
pm.ToolResults = append(pm.ToolResults, tr)
}
return true
})
}
pm.Content = strings.Join(textParts, "\n")
pm.ContentLength = len(pm.Content)
return pm, true
}
// qwenpawRole maps a QwenPaw role string to (ParsedMessage role,
// IsSystem, ok). System messages map to RoleUser + IsSystem so tool
// results remain queryable as user-side content while staying
// distinguishable from real user turns.
func qwenpawRole(role string) (RoleType, bool, bool) {
switch role {
case "user":
return RoleUser, false, true
case "assistant":
return RoleAssistant, false, true
case "system":
return RoleUser, true, true
}
return "", false, false
}
// qwenpawToolInputJSON selects the canonical tool input payload.
// QwenPaw echoes the parser input both as a structured "input"
// object and as a JSON-string "raw_input"; prefer "raw_input"
// when present because it is the verbatim original, falling back
// to the raw "input" object as emitted by gjson.
func qwenpawToolInputJSON(block gjson.Result) string {
if raw := block.Get("raw_input"); raw.Exists() {
if s := strings.TrimSpace(raw.Str); s != "" {
return s
}
}
if input := block.Get("input"); input.Exists() {
return input.Raw
}
return "{}"
}
// parseQwenPawTimestamp parses QwenPaw's "YYYY-MM-DD HH:MM:SS.fff"
// format. Timestamps are recorded as local wall-clock time without a
// timezone offset, so naive values are interpreted as time.Local
// (mirroring the Hermes parser). Empty or unparseable inputs return
// the zero time.
func parseQwenPawTimestamp(s string) time.Time {
if s == "" {
return time.Time{}
}
for _, layout := range []string{
"2006-01-02 15:04:05.999",
"2006-01-02 15:04:05",
} {
if t, err := time.ParseInLocation(layout, s, time.Local); err == nil {
return t
}
}
return time.Time{}
}
// truncateFirstMessage caps FirstMessage length to keep list views
// readable; the constant matches the truncation length other parsers
// use for the same field.
func truncateFirstMessage(s string) string {
const max = 300
r := []rune(s)
if len(r) <= max {
return s
}
return string(r[:max])
}
// populateQwenPawFileFields fills the File metadata on the session.
func populateQwenPawFileFields(
sess *ParsedSession, info os.FileInfo, path string,
) {
sess.File = FileInfo{
Path: path,
Size: info.Size(),
Mtime: info.ModTime().UnixNano(),
}
}