zames_pro 1.1.0 → 1.2.0

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.
@@ -0,0 +1,69 @@
1
+ function unescapeXml(s) {
2
+ const A = String.fromCharCode(38) // ampersand
3
+ return String(s)
4
+ .split(A + "lt;").join(String.fromCharCode(60))
5
+ .split(A + "gt;").join(String.fromCharCode(62))
6
+ .split(A + "quot;").join(String.fromCharCode(34))
7
+ .split(A + "apos;").join(String.fromCharCode(39))
8
+ .split(A + "amp;").join(A)
9
+ }
10
+
11
+ function readAttr(attrs, name) {
12
+ const Q = String.fromCharCode(34)
13
+ const re = new RegExp(name + "[ ]*=[ ]*([" + Q + "]([^" + Q + "]*)[" + Q + "])")
14
+ const m = attrs.match(re)
15
+ return m ? m[2] : null
16
+ }
17
+
18
+ export function parseXmlToolCalls(text) {
19
+ if (!text || typeof text !== "string") return null
20
+ if (!/<[^>]*invoke/i.test(text)) return null
21
+
22
+ const invokeRe = /<[^>]*?invoke([^>]*)>/gi
23
+ const calls = []
24
+ let m
25
+ while ((m = invokeRe.exec(text)) !== null) {
26
+ const attrs = m[1] || ""
27
+ const name = readAttr(attrs, "name")
28
+ if (!name) continue
29
+
30
+ const rest = text.slice(invokeRe.lastIndex)
31
+ const closeRe = /<\/[^>]*?invoke[^>]*>/i
32
+ const closeMatch = closeRe.exec(rest)
33
+ const body = closeMatch ? rest.slice(0, closeMatch.index) : rest
34
+
35
+ const args = {}
36
+ const paramRe = /<[^>]*?parameter([^>]*)>([\s\S]*?)<\/[^>]*?parameter[^>]*>/gi
37
+ let p
38
+ while ((p = paramRe.exec(body)) !== null) {
39
+ const pname = readAttr(p[1] || "", "name")
40
+ if (!pname) continue
41
+ const isString = /string[ ]*=[ ]*"?true"?/i.test(p[1] || "")
42
+ let value = unescapeXml(p[2])
43
+ if (!isString) {
44
+ try { value = JSON.parse(value) } catch (e) {}
45
+ }
46
+ args[pname] = value
47
+ }
48
+
49
+ // Некоторые модели кладут весь JSON-объект аргументов в один
50
+ // parameter с именем args. Разворачиваем, чтобы не было
51
+ // {args: {args: {...}}}.
52
+ let finalArgs = args
53
+ const keys = Object.keys(args)
54
+ if (
55
+ keys.length === 1 &&
56
+ keys[0] === 'args' &&
57
+ args.args &&
58
+ typeof args.args === 'object' &&
59
+ !Array.isArray(args.args)
60
+ ) {
61
+ finalArgs = args.args
62
+ }
63
+
64
+ calls.push({ tool: name, args: finalArgs })
65
+ }
66
+
67
+ if (!calls.length) return null
68
+ return calls.length === 1 ? calls[0] : calls
69
+ }