praxo-core 0.1.0 → 0.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.
- checksums.yaml +4 -4
- data/LICENSE +21 -21
- data/README.md +74 -66
- data/api/0.2.0.txt +112 -0
- data/lib/praxo/agent.rb +221 -162
- data/lib/praxo/errors.rb +18 -18
- data/lib/praxo/json.rb +47 -47
- data/lib/praxo/jsonschema.rb +122 -122
- data/lib/praxo/middleware.rb +133 -133
- data/lib/praxo/models.rb +154 -154
- data/lib/praxo/plugin.rb +147 -147
- data/lib/praxo/runtime.rb +224 -224
- data/lib/praxo/toolcall.rb +96 -96
- data/lib/praxo.rb +9 -9
- metadata +2 -1
data/lib/praxo/runtime.rb
CHANGED
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
require_relative 'errors'
|
|
2
|
-
require_relative 'json'
|
|
3
|
-
require_relative 'models'
|
|
4
|
-
|
|
5
|
-
module Praxo
|
|
6
|
-
module Runtime
|
|
7
|
-
class MultiSink
|
|
8
|
-
def initialize(sinks)
|
|
9
|
-
@sinks = sinks
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
def emit(event)
|
|
13
|
-
@sinks.each { |sink| sink.emit(event) }
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
class FilterSink
|
|
18
|
-
def initialize(sink, predicate)
|
|
19
|
-
@sink = sink
|
|
20
|
-
@predicate = predicate
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
def emit(event)
|
|
24
|
-
@sink.emit(event) if @predicate.call(event)
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
class MemoryStore
|
|
29
|
-
def initialize
|
|
30
|
-
@records = Hash.new { |hash, key| hash[key] = [] }
|
|
31
|
-
@sequence = 0
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def put(session_id, record)
|
|
35
|
-
if record.id.nil? || record.id.empty?
|
|
36
|
-
record.id = "mem-#{(Time.now.to_f * 1000).to_i}-#{@sequence}"
|
|
37
|
-
@sequence += 1
|
|
38
|
-
end
|
|
39
|
-
if record.created_at.nil?
|
|
40
|
-
record.created_at = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
41
|
-
end
|
|
42
|
-
@records[session_id] << record
|
|
43
|
-
record
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def search(session_id, query, limit)
|
|
47
|
-
needle = query.to_s.strip.downcase
|
|
48
|
-
matches = @records[session_id].select do |record|
|
|
49
|
-
needle.empty? || record.content.to_s.downcase.include?(needle)
|
|
50
|
-
end
|
|
51
|
-
matches.sort_by! { |record| record.created_at.to_s }.reverse!
|
|
52
|
-
limit.positive? ? matches.first(limit) : matches
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
class CheckpointAllowlist
|
|
57
|
-
def initialize
|
|
58
|
-
@registered = {}
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
def register(type)
|
|
62
|
-
Praxo.error('CHECKPOINT_ERROR', 'checkpoint: type is required') if type.nil? || type.empty?
|
|
63
|
-
@registered[type] = true
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
def marshal(type, value)
|
|
67
|
-
unless @registered[type]
|
|
68
|
-
Praxo.error('CHECKPOINT_TYPE_NOT_ALLOWED',
|
|
69
|
-
"checkpoint: payload type not allowed: #{type}")
|
|
70
|
-
end
|
|
71
|
-
Json.stringify({ 'type' => type, 'data' => value })
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def unmarshal(data)
|
|
75
|
-
envelope = Json.parse(data)
|
|
76
|
-
Praxo.error('CHECKPOINT_ERROR', 'checkpoint: envelope must be an object') unless envelope.is_a?(Hash)
|
|
77
|
-
type = envelope['type'].is_a?(String) ? envelope['type'] : ''
|
|
78
|
-
unless @registered[type]
|
|
79
|
-
Praxo.error('CHECKPOINT_TYPE_NOT_ALLOWED',
|
|
80
|
-
"checkpoint: payload type not allowed: #{type}")
|
|
81
|
-
end
|
|
82
|
-
envelope['data']
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
class PathChecker
|
|
87
|
-
def initialize(grants)
|
|
88
|
-
@grants = grants.map do |grant|
|
|
89
|
-
Models::PathGrant.new(root: resolve_path(grant.root), access: grant.access || 'read')
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
def check(target, need)
|
|
94
|
-
unless %w[read write readwrite].include?(need)
|
|
95
|
-
Praxo.error('POLICY_ERROR', 'policy: invalid access')
|
|
96
|
-
end
|
|
97
|
-
return Models::Decision.new(allowed: false, reason: 'no allowed roots') if @grants.empty?
|
|
98
|
-
resolved = resolve_path(target)
|
|
99
|
-
matched = nil
|
|
100
|
-
@grants.each do |grant|
|
|
101
|
-
next unless within?(grant.root, resolved)
|
|
102
|
-
matched = grant.root
|
|
103
|
-
return Models::Decision.new(allowed: true, reason: "path within #{grant.root}") if allows?(grant.access, need)
|
|
104
|
-
end
|
|
105
|
-
Models::Decision.new(allowed: false, reason: matched ? 'insufficient grant' : 'outside roots')
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def allowed?(target, need)
|
|
109
|
-
check(target, need).allowed
|
|
110
|
-
rescue Error
|
|
111
|
-
false
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
private
|
|
115
|
-
|
|
116
|
-
def resolve_path(value)
|
|
117
|
-
absolute = File.expand_path(value.to_s)
|
|
118
|
-
return File.realpath(absolute) if File.exist?(absolute)
|
|
119
|
-
parent = File.dirname(absolute)
|
|
120
|
-
return absolute if parent == absolute
|
|
121
|
-
File.join(resolve_path(parent), File.basename(absolute))
|
|
122
|
-
rescue StandardError
|
|
123
|
-
absolute
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
def within?(root, target)
|
|
127
|
-
root = root.tr('\\', '/').sub(%r{/+\z}, '')
|
|
128
|
-
target = target.tr('\\', '/').sub(%r{/+\z}, '')
|
|
129
|
-
if RbConfig::CONFIG['host_os'].match?(/mswin|mingw|cygwin/)
|
|
130
|
-
root = root.downcase
|
|
131
|
-
target = target.downcase
|
|
132
|
-
end
|
|
133
|
-
target == root || target.start_with?("#{root}/")
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
def allows?(grant, need)
|
|
137
|
-
return true if need == 'read' && %w[read readwrite].include?(grant)
|
|
138
|
-
return true if need == 'write' && %w[write readwrite].include?(grant)
|
|
139
|
-
return true if need == 'readwrite' && grant == 'readwrite'
|
|
140
|
-
false
|
|
141
|
-
end
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
class ContextBudget
|
|
145
|
-
attr_reader :max_tokens, :keep_recent
|
|
146
|
-
|
|
147
|
-
def initialize(max_tokens: 0, keep_recent: 0)
|
|
148
|
-
@max_tokens = max_tokens
|
|
149
|
-
@keep_recent = keep_recent
|
|
150
|
-
end
|
|
151
|
-
|
|
152
|
-
def compact(messages)
|
|
153
|
-
max = @max_tokens
|
|
154
|
-
return [messages, true] if max <= 0 || Runtime.estimate_tokens(messages) <= max
|
|
155
|
-
system = []
|
|
156
|
-
rest = []
|
|
157
|
-
messages.each do |message|
|
|
158
|
-
if message.role == Models::SYSTEM
|
|
159
|
-
next if message.content.to_s.start_with?('[context compacted:')
|
|
160
|
-
system << message
|
|
161
|
-
else
|
|
162
|
-
rest << message
|
|
163
|
-
end
|
|
164
|
-
end
|
|
165
|
-
if Runtime.estimate_tokens(system) > max
|
|
166
|
-
return [[], false] if system.empty?
|
|
167
|
-
first = system[0].dup
|
|
168
|
-
first.content = Runtime.truncate_to_tokens(system[0].content.to_s, max - 4)
|
|
169
|
-
system = [first]
|
|
170
|
-
return [[], false] if Runtime.estimate_tokens(system) > max
|
|
171
|
-
end
|
|
172
|
-
keep = @keep_recent.positive? ? @keep_recent : 4
|
|
173
|
-
start = [rest.length - keep, 0].max
|
|
174
|
-
start -= 1 while start.positive? && rest[start].role == Models::TOOL
|
|
175
|
-
marker = Models.message_text(
|
|
176
|
-
Models::SYSTEM,
|
|
177
|
-
"[context compacted: #{start} older messages omitted]"
|
|
178
|
-
)
|
|
179
|
-
kept = rest[start..] || []
|
|
180
|
-
build = lambda do
|
|
181
|
-
output = system.dup
|
|
182
|
-
output << marker if start.positive?
|
|
183
|
-
output.concat(kept)
|
|
184
|
-
output
|
|
185
|
-
end
|
|
186
|
-
output = build.call
|
|
187
|
-
while Runtime.estimate_tokens(output) > max && kept.length > 1
|
|
188
|
-
break if kept[1].role == Models::TOOL
|
|
189
|
-
kept.shift
|
|
190
|
-
output = build.call
|
|
191
|
-
end
|
|
192
|
-
[output, Runtime.estimate_tokens(output) <= max]
|
|
193
|
-
end
|
|
194
|
-
end
|
|
195
|
-
|
|
196
|
-
module_function
|
|
197
|
-
|
|
198
|
-
def estimate_tokens(messages)
|
|
199
|
-
total = 0
|
|
200
|
-
messages.each do |message|
|
|
201
|
-
total += 4 + message.content.to_s.length / 4
|
|
202
|
-
total += message.tool_id.to_s.length / 4
|
|
203
|
-
(message.content_blocks || []).each do |block|
|
|
204
|
-
total += 4 + block.text.to_s.length / 4
|
|
205
|
-
total += block.image_url.to_s.length / 4
|
|
206
|
-
total += block.data.to_s.length / 4
|
|
207
|
-
end
|
|
208
|
-
(message.tool_calls || []).each do |call|
|
|
209
|
-
total += 4 + call.name.to_s.length / 4
|
|
210
|
-
total += Json.stringify(call.arguments || {}).length / 4
|
|
211
|
-
end
|
|
212
|
-
end
|
|
213
|
-
total
|
|
214
|
-
end
|
|
215
|
-
|
|
216
|
-
def truncate_to_tokens(value, tokens)
|
|
217
|
-
return '' if tokens <= 0
|
|
218
|
-
limit = tokens * 4
|
|
219
|
-
return value if value.length <= limit
|
|
220
|
-
return value[0, limit] if limit <= 16
|
|
221
|
-
"#{value[0, limit - 16]}\n[truncated]"
|
|
222
|
-
end
|
|
223
|
-
end
|
|
224
|
-
end
|
|
1
|
+
require_relative 'errors'
|
|
2
|
+
require_relative 'json'
|
|
3
|
+
require_relative 'models'
|
|
4
|
+
|
|
5
|
+
module Praxo
|
|
6
|
+
module Runtime
|
|
7
|
+
class MultiSink
|
|
8
|
+
def initialize(sinks)
|
|
9
|
+
@sinks = sinks
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def emit(event)
|
|
13
|
+
@sinks.each { |sink| sink.emit(event) }
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
class FilterSink
|
|
18
|
+
def initialize(sink, predicate)
|
|
19
|
+
@sink = sink
|
|
20
|
+
@predicate = predicate
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def emit(event)
|
|
24
|
+
@sink.emit(event) if @predicate.call(event)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class MemoryStore
|
|
29
|
+
def initialize
|
|
30
|
+
@records = Hash.new { |hash, key| hash[key] = [] }
|
|
31
|
+
@sequence = 0
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def put(session_id, record)
|
|
35
|
+
if record.id.nil? || record.id.empty?
|
|
36
|
+
record.id = "mem-#{(Time.now.to_f * 1000).to_i}-#{@sequence}"
|
|
37
|
+
@sequence += 1
|
|
38
|
+
end
|
|
39
|
+
if record.created_at.nil?
|
|
40
|
+
record.created_at = Time.now.utc.strftime('%Y-%m-%dT%H:%M:%SZ')
|
|
41
|
+
end
|
|
42
|
+
@records[session_id] << record
|
|
43
|
+
record
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def search(session_id, query, limit)
|
|
47
|
+
needle = query.to_s.strip.downcase
|
|
48
|
+
matches = @records[session_id].select do |record|
|
|
49
|
+
needle.empty? || record.content.to_s.downcase.include?(needle)
|
|
50
|
+
end
|
|
51
|
+
matches.sort_by! { |record| record.created_at.to_s }.reverse!
|
|
52
|
+
limit.positive? ? matches.first(limit) : matches
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
class CheckpointAllowlist
|
|
57
|
+
def initialize
|
|
58
|
+
@registered = {}
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def register(type)
|
|
62
|
+
Praxo.error('CHECKPOINT_ERROR', 'checkpoint: type is required') if type.nil? || type.empty?
|
|
63
|
+
@registered[type] = true
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def marshal(type, value)
|
|
67
|
+
unless @registered[type]
|
|
68
|
+
Praxo.error('CHECKPOINT_TYPE_NOT_ALLOWED',
|
|
69
|
+
"checkpoint: payload type not allowed: #{type}")
|
|
70
|
+
end
|
|
71
|
+
Json.stringify({ 'type' => type, 'data' => value })
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def unmarshal(data)
|
|
75
|
+
envelope = Json.parse(data)
|
|
76
|
+
Praxo.error('CHECKPOINT_ERROR', 'checkpoint: envelope must be an object') unless envelope.is_a?(Hash)
|
|
77
|
+
type = envelope['type'].is_a?(String) ? envelope['type'] : ''
|
|
78
|
+
unless @registered[type]
|
|
79
|
+
Praxo.error('CHECKPOINT_TYPE_NOT_ALLOWED',
|
|
80
|
+
"checkpoint: payload type not allowed: #{type}")
|
|
81
|
+
end
|
|
82
|
+
envelope['data']
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
class PathChecker
|
|
87
|
+
def initialize(grants)
|
|
88
|
+
@grants = grants.map do |grant|
|
|
89
|
+
Models::PathGrant.new(root: resolve_path(grant.root), access: grant.access || 'read')
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def check(target, need)
|
|
94
|
+
unless %w[read write readwrite].include?(need)
|
|
95
|
+
Praxo.error('POLICY_ERROR', 'policy: invalid access')
|
|
96
|
+
end
|
|
97
|
+
return Models::Decision.new(allowed: false, reason: 'no allowed roots') if @grants.empty?
|
|
98
|
+
resolved = resolve_path(target)
|
|
99
|
+
matched = nil
|
|
100
|
+
@grants.each do |grant|
|
|
101
|
+
next unless within?(grant.root, resolved)
|
|
102
|
+
matched = grant.root
|
|
103
|
+
return Models::Decision.new(allowed: true, reason: "path within #{grant.root}") if allows?(grant.access, need)
|
|
104
|
+
end
|
|
105
|
+
Models::Decision.new(allowed: false, reason: matched ? 'insufficient grant' : 'outside roots')
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def allowed?(target, need)
|
|
109
|
+
check(target, need).allowed
|
|
110
|
+
rescue Error
|
|
111
|
+
false
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
private
|
|
115
|
+
|
|
116
|
+
def resolve_path(value)
|
|
117
|
+
absolute = File.expand_path(value.to_s)
|
|
118
|
+
return File.realpath(absolute) if File.exist?(absolute)
|
|
119
|
+
parent = File.dirname(absolute)
|
|
120
|
+
return absolute if parent == absolute
|
|
121
|
+
File.join(resolve_path(parent), File.basename(absolute))
|
|
122
|
+
rescue StandardError
|
|
123
|
+
absolute
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def within?(root, target)
|
|
127
|
+
root = root.tr('\\', '/').sub(%r{/+\z}, '')
|
|
128
|
+
target = target.tr('\\', '/').sub(%r{/+\z}, '')
|
|
129
|
+
if RbConfig::CONFIG['host_os'].match?(/mswin|mingw|cygwin/)
|
|
130
|
+
root = root.downcase
|
|
131
|
+
target = target.downcase
|
|
132
|
+
end
|
|
133
|
+
target == root || target.start_with?("#{root}/")
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def allows?(grant, need)
|
|
137
|
+
return true if need == 'read' && %w[read readwrite].include?(grant)
|
|
138
|
+
return true if need == 'write' && %w[write readwrite].include?(grant)
|
|
139
|
+
return true if need == 'readwrite' && grant == 'readwrite'
|
|
140
|
+
false
|
|
141
|
+
end
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
class ContextBudget
|
|
145
|
+
attr_reader :max_tokens, :keep_recent
|
|
146
|
+
|
|
147
|
+
def initialize(max_tokens: 0, keep_recent: 0)
|
|
148
|
+
@max_tokens = max_tokens
|
|
149
|
+
@keep_recent = keep_recent
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
def compact(messages)
|
|
153
|
+
max = @max_tokens
|
|
154
|
+
return [messages, true] if max <= 0 || Runtime.estimate_tokens(messages) <= max
|
|
155
|
+
system = []
|
|
156
|
+
rest = []
|
|
157
|
+
messages.each do |message|
|
|
158
|
+
if message.role == Models::SYSTEM
|
|
159
|
+
next if message.content.to_s.start_with?('[context compacted:')
|
|
160
|
+
system << message
|
|
161
|
+
else
|
|
162
|
+
rest << message
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
if Runtime.estimate_tokens(system) > max
|
|
166
|
+
return [[], false] if system.empty?
|
|
167
|
+
first = system[0].dup
|
|
168
|
+
first.content = Runtime.truncate_to_tokens(system[0].content.to_s, max - 4)
|
|
169
|
+
system = [first]
|
|
170
|
+
return [[], false] if Runtime.estimate_tokens(system) > max
|
|
171
|
+
end
|
|
172
|
+
keep = @keep_recent.positive? ? @keep_recent : 4
|
|
173
|
+
start = [rest.length - keep, 0].max
|
|
174
|
+
start -= 1 while start.positive? && rest[start].role == Models::TOOL
|
|
175
|
+
marker = Models.message_text(
|
|
176
|
+
Models::SYSTEM,
|
|
177
|
+
"[context compacted: #{start} older messages omitted]"
|
|
178
|
+
)
|
|
179
|
+
kept = rest[start..] || []
|
|
180
|
+
build = lambda do
|
|
181
|
+
output = system.dup
|
|
182
|
+
output << marker if start.positive?
|
|
183
|
+
output.concat(kept)
|
|
184
|
+
output
|
|
185
|
+
end
|
|
186
|
+
output = build.call
|
|
187
|
+
while Runtime.estimate_tokens(output) > max && kept.length > 1
|
|
188
|
+
break if kept[1].role == Models::TOOL
|
|
189
|
+
kept.shift
|
|
190
|
+
output = build.call
|
|
191
|
+
end
|
|
192
|
+
[output, Runtime.estimate_tokens(output) <= max]
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
|
|
196
|
+
module_function
|
|
197
|
+
|
|
198
|
+
def estimate_tokens(messages)
|
|
199
|
+
total = 0
|
|
200
|
+
messages.each do |message|
|
|
201
|
+
total += 4 + message.content.to_s.length / 4
|
|
202
|
+
total += message.tool_id.to_s.length / 4
|
|
203
|
+
(message.content_blocks || []).each do |block|
|
|
204
|
+
total += 4 + block.text.to_s.length / 4
|
|
205
|
+
total += block.image_url.to_s.length / 4
|
|
206
|
+
total += block.data.to_s.length / 4
|
|
207
|
+
end
|
|
208
|
+
(message.tool_calls || []).each do |call|
|
|
209
|
+
total += 4 + call.name.to_s.length / 4
|
|
210
|
+
total += Json.stringify(call.arguments || {}).length / 4
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
total
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def truncate_to_tokens(value, tokens)
|
|
217
|
+
return '' if tokens <= 0
|
|
218
|
+
limit = tokens * 4
|
|
219
|
+
return value if value.length <= limit
|
|
220
|
+
return value[0, limit] if limit <= 16
|
|
221
|
+
"#{value[0, limit - 16]}\n[truncated]"
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
end
|
data/lib/praxo/toolcall.rb
CHANGED
|
@@ -1,96 +1,96 @@
|
|
|
1
|
-
require_relative 'json'
|
|
2
|
-
|
|
3
|
-
module Praxo
|
|
4
|
-
module Toolcall
|
|
5
|
-
ParsedToolCall = Struct.new(
|
|
6
|
-
:call_id, :name, :input, :operation, :resource, :risk, :reason,
|
|
7
|
-
:idempotency_key, keyword_init: true
|
|
8
|
-
)
|
|
9
|
-
|
|
10
|
-
module_function
|
|
11
|
-
|
|
12
|
-
def parse_toolcall(content)
|
|
13
|
-
call = parse_json_call(content) || parse_xml(content) || parse_tag(content) ||
|
|
14
|
-
parse_plain(content)
|
|
15
|
-
return nil unless call
|
|
16
|
-
call.call_id = "tc-#{(Time.now.to_f * 1000).to_i}"
|
|
17
|
-
call
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def strip_markup(content)
|
|
21
|
-
output = content.dup
|
|
22
|
-
output.gsub!(%r{<tool_call\s+name="[^"]+"\s*>.*?(?:</tool_call>|$)}m, '')
|
|
23
|
-
output.gsub!(%r{<tool_call_invoke>.*?(?:</tool_call_invoke>|$)}m, '')
|
|
24
|
-
output.gsub!(%r{<[a-zA-Z][\w-]*(?:\.[\w-]+)+>\s*\{.*?(?:</[a-zA-Z][\w.-]*>|$)}m, '')
|
|
25
|
-
output.gsub!(/\n{3,}/, "\n\n")
|
|
26
|
-
output.strip
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def parse_input_json(raw)
|
|
30
|
-
begin
|
|
31
|
-
value = JSON.parse(raw.strip)
|
|
32
|
-
return value if value.is_a?(Hash)
|
|
33
|
-
rescue JSON::ParserError
|
|
34
|
-
nil
|
|
35
|
-
end
|
|
36
|
-
escaped = raw.gsub('\\', '\\\\\\\\')
|
|
37
|
-
begin
|
|
38
|
-
value = JSON.parse(escaped.strip)
|
|
39
|
-
return value if value.is_a?(Hash)
|
|
40
|
-
rescue JSON::ParserError
|
|
41
|
-
return nil
|
|
42
|
-
end
|
|
43
|
-
nil
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def parse_json_call(content)
|
|
47
|
-
value = JSON.parse(content)
|
|
48
|
-
return nil unless value.is_a?(Hash) && value['name'].is_a?(String) &&
|
|
49
|
-
!value['name'].empty?
|
|
50
|
-
ParsedToolCall.new(
|
|
51
|
-
name: value['name'],
|
|
52
|
-
input: value['input'].is_a?(Hash) ? value['input'] : {},
|
|
53
|
-
operation: value['operation'] || '',
|
|
54
|
-
resource: value['resource'] || '',
|
|
55
|
-
risk: value['risk'] || '',
|
|
56
|
-
reason: value['reason'] || '',
|
|
57
|
-
idempotency_key: value['idempotency_key'] || ''
|
|
58
|
-
)
|
|
59
|
-
rescue JSON::ParserError
|
|
60
|
-
nil
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def parse_xml(content)
|
|
64
|
-
match = nil
|
|
65
|
-
content.scan(%r{<tool_call\s+name="([^"]+)"\s*>\s*(.*?)\s*</tool_call>}m) do |name, raw|
|
|
66
|
-
match = [name, raw]
|
|
67
|
-
end
|
|
68
|
-
return nil unless match
|
|
69
|
-
input = parse_input_json(match[1])
|
|
70
|
-
return nil unless input
|
|
71
|
-
ParsedToolCall.new(name: match[0], input: input)
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
def parse_tag(content)
|
|
75
|
-
match = nil
|
|
76
|
-
content.scan(%r{<([a-zA-Z][\w-]*(?:\.[\w-]+)+)>\s*(\{.*?)\s*(?:</[a-zA-Z][\w.-]*>|$)}m) do |name, raw|
|
|
77
|
-
match = [name, raw]
|
|
78
|
-
end
|
|
79
|
-
return nil unless match
|
|
80
|
-
input = parse_input_json(match[1])
|
|
81
|
-
return nil unless input
|
|
82
|
-
ParsedToolCall.new(name: match[0], input: input)
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def parse_plain(content)
|
|
86
|
-
match = nil
|
|
87
|
-
content.scan(/(?:^|\n)\s*([a-zA-Z][\w.-]*)\s+(\{.*\})\s*(?:\n|$)/m) do |name, raw|
|
|
88
|
-
match = [name, raw]
|
|
89
|
-
end
|
|
90
|
-
return nil unless match && match[0].include?('.')
|
|
91
|
-
input = parse_input_json(match[1])
|
|
92
|
-
return nil unless input
|
|
93
|
-
ParsedToolCall.new(name: match[0], input: input)
|
|
94
|
-
end
|
|
95
|
-
end
|
|
96
|
-
end
|
|
1
|
+
require_relative 'json'
|
|
2
|
+
|
|
3
|
+
module Praxo
|
|
4
|
+
module Toolcall
|
|
5
|
+
ParsedToolCall = Struct.new(
|
|
6
|
+
:call_id, :name, :input, :operation, :resource, :risk, :reason,
|
|
7
|
+
:idempotency_key, keyword_init: true
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def parse_toolcall(content)
|
|
13
|
+
call = parse_json_call(content) || parse_xml(content) || parse_tag(content) ||
|
|
14
|
+
parse_plain(content)
|
|
15
|
+
return nil unless call
|
|
16
|
+
call.call_id = "tc-#{(Time.now.to_f * 1000).to_i}"
|
|
17
|
+
call
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def strip_markup(content)
|
|
21
|
+
output = content.dup
|
|
22
|
+
output.gsub!(%r{<tool_call\s+name="[^"]+"\s*>.*?(?:</tool_call>|$)}m, '')
|
|
23
|
+
output.gsub!(%r{<tool_call_invoke>.*?(?:</tool_call_invoke>|$)}m, '')
|
|
24
|
+
output.gsub!(%r{<[a-zA-Z][\w-]*(?:\.[\w-]+)+>\s*\{.*?(?:</[a-zA-Z][\w.-]*>|$)}m, '')
|
|
25
|
+
output.gsub!(/\n{3,}/, "\n\n")
|
|
26
|
+
output.strip
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def parse_input_json(raw)
|
|
30
|
+
begin
|
|
31
|
+
value = JSON.parse(raw.strip)
|
|
32
|
+
return value if value.is_a?(Hash)
|
|
33
|
+
rescue JSON::ParserError
|
|
34
|
+
nil
|
|
35
|
+
end
|
|
36
|
+
escaped = raw.gsub('\\', '\\\\\\\\')
|
|
37
|
+
begin
|
|
38
|
+
value = JSON.parse(escaped.strip)
|
|
39
|
+
return value if value.is_a?(Hash)
|
|
40
|
+
rescue JSON::ParserError
|
|
41
|
+
return nil
|
|
42
|
+
end
|
|
43
|
+
nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def parse_json_call(content)
|
|
47
|
+
value = JSON.parse(content)
|
|
48
|
+
return nil unless value.is_a?(Hash) && value['name'].is_a?(String) &&
|
|
49
|
+
!value['name'].empty?
|
|
50
|
+
ParsedToolCall.new(
|
|
51
|
+
name: value['name'],
|
|
52
|
+
input: value['input'].is_a?(Hash) ? value['input'] : {},
|
|
53
|
+
operation: value['operation'] || '',
|
|
54
|
+
resource: value['resource'] || '',
|
|
55
|
+
risk: value['risk'] || '',
|
|
56
|
+
reason: value['reason'] || '',
|
|
57
|
+
idempotency_key: value['idempotency_key'] || ''
|
|
58
|
+
)
|
|
59
|
+
rescue JSON::ParserError
|
|
60
|
+
nil
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def parse_xml(content)
|
|
64
|
+
match = nil
|
|
65
|
+
content.scan(%r{<tool_call\s+name="([^"]+)"\s*>\s*(.*?)\s*</tool_call>}m) do |name, raw|
|
|
66
|
+
match = [name, raw]
|
|
67
|
+
end
|
|
68
|
+
return nil unless match
|
|
69
|
+
input = parse_input_json(match[1])
|
|
70
|
+
return nil unless input
|
|
71
|
+
ParsedToolCall.new(name: match[0], input: input)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def parse_tag(content)
|
|
75
|
+
match = nil
|
|
76
|
+
content.scan(%r{<([a-zA-Z][\w-]*(?:\.[\w-]+)+)>\s*(\{.*?)\s*(?:</[a-zA-Z][\w.-]*>|$)}m) do |name, raw|
|
|
77
|
+
match = [name, raw]
|
|
78
|
+
end
|
|
79
|
+
return nil unless match
|
|
80
|
+
input = parse_input_json(match[1])
|
|
81
|
+
return nil unless input
|
|
82
|
+
ParsedToolCall.new(name: match[0], input: input)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def parse_plain(content)
|
|
86
|
+
match = nil
|
|
87
|
+
content.scan(/(?:^|\n)\s*([a-zA-Z][\w.-]*)\s+(\{.*\})\s*(?:\n|$)/m) do |name, raw|
|
|
88
|
+
match = [name, raw]
|
|
89
|
+
end
|
|
90
|
+
return nil unless match && match[0].include?('.')
|
|
91
|
+
input = parse_input_json(match[1])
|
|
92
|
+
return nil unless input
|
|
93
|
+
ParsedToolCall.new(name: match[0], input: input)
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
end
|
data/lib/praxo.rb
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
require_relative 'praxo/errors'
|
|
2
|
-
require_relative 'praxo/json'
|
|
3
|
-
require_relative 'praxo/models'
|
|
4
|
-
require_relative 'praxo/toolcall'
|
|
5
|
-
require_relative 'praxo/jsonschema'
|
|
6
|
-
require_relative 'praxo/plugin'
|
|
7
|
-
require_relative 'praxo/runtime'
|
|
8
|
-
require_relative 'praxo/middleware'
|
|
9
|
-
require_relative 'praxo/agent'
|
|
1
|
+
require_relative 'praxo/errors'
|
|
2
|
+
require_relative 'praxo/json'
|
|
3
|
+
require_relative 'praxo/models'
|
|
4
|
+
require_relative 'praxo/toolcall'
|
|
5
|
+
require_relative 'praxo/jsonschema'
|
|
6
|
+
require_relative 'praxo/plugin'
|
|
7
|
+
require_relative 'praxo/runtime'
|
|
8
|
+
require_relative 'praxo/middleware'
|
|
9
|
+
require_relative 'praxo/agent'
|