Реверснутый zcode с своими фишками и плагинами · VibeGit
Реверснутый zcode с своими фишками и плагинами Реверснутая ade zcode без z ai, с массовой загрузкой ключей, умным апи роутингом, плагином для командной разработки
reversnutyy-zcode-s-svoimi-fishkami-i-plaginami / tools / test-history-sanitizer.mjs 215 строк · изменён 13 нояб., 21:19
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
import assert from 'node:assert/strict' ;
import { createRequire } from 'node:module' ;
import path from 'node:path' ;
const require = createRequire ( import . meta . url ) ;
const { sanitizePayload, STUB } = require ( path. resolve ( 'fork/resources/glm/history-sanitizer.cjs' ) ) ;
let n = 0 ;
const test = ( name, fn
n
name
p
assert p
p
r p
assert r
assert r
assert r
assert p
p
r p
roles r m m m
assert roles
assert r
assert r
p
r p
assert r
assert r
assert r m
assert r
p
assert p
p
r p
u r
assert u
assert u
assert u
assert u
p
r p
assert r
assert r
assert r
p
r p
assert r m
p
r p
assert r
p
r p
assert r x x x
assert
assert
fs
os
tmp fs path os
process tmp
process
v2 path tmp
fs v2
fs
path v2
engine path
sent
f engine
sent init
assert sent
r
rt fs path v2
assert rt e e
n
fs tmp
)
=>
{
fn
(
)
;
++
;
console
.
log
(
'ok'
,
)
;
}
;
test
(
'chat: valid history is untouched'
,
(
)
=>
{
const
=
{
model
:
'm'
,
messages
:
[
{
role
:
'system'
,
content
:
'sys'
}
,
{
role
:
'user'
,
content
:
'hi'
}
,
{
role
:
'assistant'
,
content
:
null
,
tool_calls
:
[
{
id
:
'a'
,
type
:
'function'
,
function
:
{
name
:
'Read'
,
arguments
:
'{}'
}
}
]
}
,
{
role
:
'tool'
,
tool_call_id
:
'a'
,
content
:
'file'
}
,
{
role
:
'assistant'
,
content
:
'done'
}
,
]
,
}
;
.
equal
(
sanitizePayload
(
)
,
null
)
;
}
)
;
test
(
'chat: dangling tool_call at the end gets a stub'
,
(
)
=>
{
const
=
{
model
:
'm'
,
messages
:
[
{
role
:
'user'
,
content
:
'read'
}
,
{
role
:
'assistant'
,
content
:
''
,
tool_calls
:
[
{
id
:
'a'
,
type
:
'function'
,
function
:
{
name
:
'Read'
,
arguments
:
'{}'
}
}
]
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
.
equal
(
.
stats
.
stubbed
,
1
)
;
.
deepEqual
(
.
payload
.
messages
.
at
(
-
1
)
,
{
role
:
'tool'
,
tool_call_id
:
'a'
,
content
:
STUB
}
)
;
.
equal
(
.
payload
.
messages
[
1
]
.
content
,
null
)
;
.
equal
(
.
messages
.
length
,
2
,
'original not mutated'
)
;
}
)
;
test
(
'chat: dangling call in the middle, user typed next, empty assistant dropped'
,
(
)
=>
{
const
=
{
model
:
'm'
,
messages
:
[
{
role
:
'user'
,
content
:
'go'
}
,
{
role
:
'assistant'
,
content
:
null
,
tool_calls
:
[
{
id
:
'a'
}
,
{
id
:
'b'
}
]
}
,
{
role
:
'tool'
,
tool_call_id
:
'a'
,
content
:
'ok'
}
,
{
role
:
'assistant'
,
content
:
' '
}
,
{
role
:
'user'
,
content
:
'continue'
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
const
=
.
payload
.
messages
.
map
(
(
m
)
=>
.
role
+
(
.
tool_call_id
?
':'
+
.
tool_call_id
:
''
)
)
;
.
deepEqual
(
,
[
'user'
,
'assistant'
,
'tool:a'
,
'tool:b'
,
'user'
]
)
;
.
equal
(
.
stats
.
stubbed
,
1
)
;
.
equal
(
.
stats
.
emptyMessages
,
1
)
;
}
)
;
test
(
'chat: orphan tool result and empty tool output'
,
(
)
=>
{
const
=
{
model
:
'm'
,
messages
:
[
{
role
:
'user'
,
content
:
'x'
}
,
{
role
:
'tool'
,
tool_call_id
:
'ghost'
,
content
:
'zzz'
}
,
{
role
:
'assistant'
,
content
:
null
,
tool_calls
:
[
{
id
:
'a'
}
]
}
,
{
role
:
'tool'
,
tool_call_id
:
'a'
,
content
:
''
}
,
{
role
:
'tool'
,
tool_call_id
:
'a'
,
content
:
'dup'
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
.
equal
(
.
stats
.
orphanResults
,
2
)
;
.
equal
(
.
stats
.
emptyResults
,
1
)
;
.
deepEqual
(
.
payload
.
messages
.
map
(
(
m
)
=>
.
role
)
,
[
'user'
,
'assistant'
,
'tool'
]
)
;
.
equal
(
.
payload
.
messages
[
2
]
.
content
,
'(no output)'
)
;
}
)
;
test
(
'anthropic: valid history is untouched'
,
(
)
=>
{
const
=
{
model
:
'c'
,
system
:
's'
,
messages
:
[
{
role
:
'user'
,
content
:
'hi'
}
,
{
role
:
'assistant'
,
content
:
[
{
type
:
'text'
,
text
:
'reading'
}
,
{
type
:
'tool_use'
,
id
:
't1'
,
name
:
'Read'
,
input
:
{
}
}
]
}
,
{
role
:
'user'
,
content
:
[
{
type
:
'tool_result'
,
tool_use_id
:
't1'
,
content
:
'data'
}
]
}
,
]
,
}
;
.
equal
(
sanitizePayload
(
)
,
null
)
;
}
)
;
test
(
'anthropic: tool_use without result, next user is text'
,
(
)
=>
{
const
=
{
model
:
'c'
,
system
:
's'
,
messages
:
[
{
role
:
'user'
,
content
:
'hi'
}
,
{
role
:
'assistant'
,
content
:
[
{
type
:
'tool_use'
,
id
:
't1'
,
name
:
'Bash'
,
input
:
{
}
}
]
}
,
{
role
:
'user'
,
content
:
[
{
type
:
'text'
,
text
:
'what happened?'
}
]
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
const
=
.
payload
.
messages
[
2
]
.
content
;
.
equal
(
[
0
]
.
type
,
'tool_result'
)
;
.
equal
(
[
0
]
.
tool_use_id
,
't1'
)
;
.
equal
(
[
0
]
.
is_error
,
true
)
;
.
equal
(
[
1
]
.
text
,
'what happened?'
)
;
}
)
;
test
(
'anthropic: tool_use is last message'
,
(
)
=>
{
const
=
{
model
:
'c'
,
messages
:
[
{
role
:
'user'
,
content
:
'hi'
}
,
{
role
:
'assistant'
,
content
:
[
{
type
:
'text'
,
text
:
''
}
,
{
type
:
'tool_use'
,
id
:
't1'
,
name
:
'Bash'
,
input
:
{
}
}
]
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
.
equal
(
.
payload
.
messages
.
length
,
3
)
;
.
equal
(
.
payload
.
messages
[
1
]
.
content
.
length
,
1
,
'empty text block removed'
)
;
.
equal
(
.
payload
.
messages
[
2
]
.
content
[
0
]
.
tool_use_id
,
't1'
)
;
}
)
;
test
(
'anthropic: assistant followed by assistant (stub user inserted), empty message dropped'
,
(
)
=>
{
const
=
{
model
:
'c'
,
system
:
's'
,
messages
:
[
{
role
:
'user'
,
content
:
'hi'
}
,
{
role
:
'assistant'
,
content
:
[
{
type
:
'tool_use'
,
id
:
't1'
,
name
:
'Bash'
,
input
:
{
}
}
]
}
,
{
role
:
'assistant'
,
content
:
[
]
}
,
{
role
:
'user'
,
content
:
''
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
.
deepEqual
(
.
payload
.
messages
.
map
(
(
m
)
=>
.
role
)
,
[
'user'
,
'assistant'
,
'user'
]
)
;
}
)
;
test
(
'anthropic: orphan tool_result removed'
,
(
)
=>
{
const
=
{
model
:
'c'
,
system
:
's'
,
messages
:
[
{
role
:
'user'
,
content
:
[
{
type
:
'tool_result'
,
tool_use_id
:
'ghost'
,
content
:
'x'
}
,
{
type
:
'text'
,
text
:
'hi'
}
]
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
.
deepEqual
(
.
payload
.
messages
[
0
]
.
content
,
[
{
type
:
'text'
,
text
:
'hi'
}
]
)
;
}
)
;
test
(
'responses: function_call without output gets stub, orphan output dropped'
,
(
)
=>
{
const
=
{
model
:
'r'
,
input
:
[
{
type
:
'message'
,
role
:
'user'
,
content
:
[
{
type
:
'input_text'
,
text
:
'go'
}
]
}
,
{
type
:
'function_call'
,
call_id
:
'c1'
,
name
:
'Read'
,
arguments
:
'{}'
}
,
{
type
:
'function_call_output'
,
call_id
:
'ghost'
,
output
:
'x'
}
,
{
type
:
'message'
,
role
:
'user'
,
content
:
[
{
type
:
'input_text'
,
text
:
' '
}
]
}
,
]
,
}
;
const
=
sanitizePayload
(
)
;
.
deepEqual
(
.
payload
.
input
.
map
(
(
x
)
=>
.
type
+
(
.
call_id
?
':'
+
.
call_id
:
''
)
)
,
[
'message'
,
'function_call:c1'
,
'function_call_output:c1'
]
)
;
}
)
;
test
(
'non-chat payloads are ignored'
,
(
)
=>
{
.
equal
(
sanitizePayload
(
{
model
:
'x'
,
prompt
:
'p'
}
)
,
null
)
;
.
equal
(
sanitizePayload
(
null
)
,
null
)
;
}
)
;
{
const
=
await
import
(
'node:fs'
)
;
const
=
await
import
(
'node:os'
)
;
const
=
.
mkdtempSync
(
.
join
(
.
tmpdir
(
)
,
'hs-'
)
)
;
.
env
.
ZCODE_DATA_BASE_DIR
=
;
delete
.
env
.
ZCODE_PERSONAL_PROVIDER_CONFIG_FILE
;
const
=
.
join
(
,
'.zcode'
,
'v2'
)
;
.
mkdirSync
(
,
{
recursive
:
true
}
)
;
.
writeFileSync
(
.
join
(
,
'provider_config.json'
)
,
JSON
.
stringify
(
{
config
:
{
providerConfigRules
:
{
providerRules
:
[
{
providerId
:
'a'
,
config
:
{
access
:
{
type
:
'api-key'
,
apiKey
:
'k'
}
,
api
:
{
type
:
'openai-chat-completions'
,
baseUrl
:
'https://a.test/v1'
}
,
modelOrder
:
[
'm'
]
}
}
]
,
}
,
}
,
}
)
,
)
;
const
=
require
(
.
resolve
(
'fork/resources/glm/key-pool-engine.cjs'
)
)
;
let
=
null
;
const
=
.
wrap
(
'a'
,
async
(
url, init
)
=>
{
=
JSON
.
parse
(
.
body
)
;
return
new
Response
(
'{}'
,
{
status
:
200
}
)
;
}
)
;
await
f
(
'https://a.test/v1/chat/completions'
,
{
method
:
'POST'
,
headers
:
{
'content-type'
:
'application/json'
,
'content-length'
:
'999'
}
,
body
:
JSON
.
stringify
(
{
model
:
'm'
,
messages
:
[
{
role
:
'user'
,
content
:
'x'
}
,
{
role
:
'assistant'
,
content
:
''
,
tool_calls
:
[
{
id
:
'a'
}
]
}
]
}
)
,
}
)
;
.
equal
(
.
messages
.
at
(
-
1
)
.
tool_call_id
,
'a'
)
;
await
new
Promise
(
(
r
)
=>
setTimeout
(
,
1500
)
)
;
const
=
JSON
.
parse
(
.
readFileSync
(
.
join
(
,
'key_pool_runtime.json'
)
,
'utf8'
)
)
;
.
ok
(
.
events
.
some
(
(
e
)
=>
.
kind
===
'history-fix'
&&
.
stubbed
===
1
)
,
'journal event written'
)
;
++
;
console
.
log
(
'ok engine: request body repaired and journaled'
)
;
.
rmSync
(
,
{
recursive
:
true
,
force
:
true
}
)
;
}
console
.
log
(
` \n ${ n} passed `
)
;