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.
data/lib/praxo/agent.rb CHANGED
@@ -1,162 +1,221 @@
1
- require_relative 'errors'
2
- require_relative 'json'
3
- require_relative 'models'
4
- require_relative 'runtime'
5
-
6
- module Praxo
7
- module Agent
8
- class FuncTool
9
- attr_reader :name, :description
10
-
11
- def initialize(name, description, function)
12
- @name = name
13
- @description = description
14
- @function = function
15
- end
16
-
17
- def invoke(arguments)
18
- @function.call(arguments)
19
- end
20
- end
21
-
22
- class ToolRegistry
23
- def initialize
24
- @tools = {}
25
- end
26
-
27
- def register(tool)
28
- @tools[tool.name] = tool
29
- tool
30
- end
31
-
32
- def get(name)
33
- Praxo.error('TOOL_NOT_FOUND', "tool not found: #{name}") unless @tools.key?(name)
34
- @tools[name]
35
- end
36
-
37
- def specs
38
- @tools.values.map do |tool|
39
- Models.tool_spec(name: tool.name, description: tool.description)
40
- end.sort_by(&:name)
41
- end
42
- end
43
-
44
- class StaticModel
45
- def initialize(content)
46
- @content = content.to_s
47
- end
48
-
49
- def generate(_request)
50
- Models.response(message: Models.message_assistant(@content))
51
- end
52
- end
53
-
54
- class AgentOptions
55
- attr_accessor :model, :tools, :events, :policy, :system_prompt, :context,
56
- :max_steps, :max_tokens, :tool_choice
57
-
58
- def initialize(model)
59
- @model = model
60
- @tools = nil
61
- @events = nil
62
- @policy = nil
63
- @system_prompt = ''
64
- @context = nil
65
- @max_steps = 8
66
- @max_tokens = 0
67
- @tool_choice = nil
68
- end
69
- end
70
-
71
- class Runner
72
- def initialize(options)
73
- @options = options
74
- end
75
-
76
- def run(session, prompt)
77
- options = @options
78
- max_steps = options.max_steps.to_i.positive? ? options.max_steps : 8
79
- run_id = "run-#{(Time.now.to_f * 1000).to_i}"
80
- messages = session.messages.dup
81
- unless options.system_prompt.to_s.empty?
82
- messages.unshift(Models.message_text(Models::SYSTEM, options.system_prompt))
83
- end
84
- messages << Models.message_text(Models::USER, prompt.to_s)
85
- usage = Models.usage
86
- emit(options, 'run.started', session.id, run_id)
87
- begin
88
- (1..max_steps).each do |step|
89
- authorize(options, Models.action(kind: 'model.generate', session_id: session.id))
90
- if options.context
91
- compacted, ok = options.context.compact(messages)
92
- Praxo.error('AGENT_CONTEXT_BUDGET', 'context budget') unless ok
93
- messages = compacted
94
- end
95
- request = Models.request(
96
- messages: messages,
97
- tools: options.tools ? options.tools.specs : [],
98
- tool_choice: options.tool_choice
99
- )
100
- response = options.model.generate(request)
101
- usage = usage.plus(response.usage || Models.usage)
102
- total = usage.input_tokens.to_i + usage.output_tokens.to_i
103
- if options.max_tokens.to_i.positive? && total > options.max_tokens
104
- Praxo.error('AGENT_MAX_TOKENS', 'token budget')
105
- end
106
- message = response.message
107
- messages << message
108
- calls = message.tool_calls || []
109
- if calls.empty?
110
- emit(options, 'run.completed', session.id, run_id)
111
- return Models.run_result(
112
- run_id: run_id, output: message.content.to_s, steps: step,
113
- usage: usage
114
- )
115
- end
116
- calls.each do |call|
117
- messages << invoke_tool(options, call, run_id, session.id)
118
- end
119
- end
120
- Praxo.error('AGENT_MAX_STEPS', 'maximum steps')
121
- rescue StandardError => e
122
- emit(options, 'run.failed', session.id, run_id, { 'error' => e.message })
123
- raise
124
- end
125
- end
126
-
127
- private
128
-
129
- def invoke_tool(options, call, run_id, session_id)
130
- authorize(options, Models.action(
131
- kind: 'tool.invoke', session_id: session_id,
132
- tool_name: call.name, arguments: call.arguments
133
- ))
134
- Praxo.error('TOOL_NOT_FOUND', 'tool is not registered') unless options.tools
135
- tool = options.tools.get(call.name)
136
- emit(options, 'tool.call', session_id, run_id,
137
- { 'name' => call.name, 'arguments' => call.arguments || {} })
138
- value = tool.invoke(call.arguments || {})
139
- Models.message_tool(Json.stringify(value), call.id.to_s)
140
- end
141
-
142
- def authorize(options, action)
143
- return unless options.policy
144
- decision = options.policy.call(action)
145
- Praxo.error('POLICY_ERROR', 'policy denied the action') unless decision&.allowed
146
- end
147
-
148
- def emit(options, type, session_id, run_id, data = nil)
149
- return unless options.events
150
- options.events.emit(Models.event(
151
- type: type, session_id: session_id, run_id: run_id, data: data
152
- ))
153
- end
154
- end
155
-
156
- module_function
157
-
158
- def run_agent(options, session, prompt)
159
- Runner.new(options).run(session, prompt)
160
- end
161
- end
162
- end
1
+ require_relative 'errors'
2
+ require_relative 'json'
3
+ require_relative 'models'
4
+ require_relative 'runtime'
5
+
6
+ module Praxo
7
+ module Agent
8
+ class FuncTool
9
+ attr_reader :name, :description
10
+
11
+ def initialize(name, description, function)
12
+ @name = name
13
+ @description = description
14
+ @function = function
15
+ end
16
+
17
+ def invoke(arguments)
18
+ @function.call(arguments)
19
+ end
20
+ end
21
+
22
+ class ToolRegistry
23
+ def initialize
24
+ @tools = {}
25
+ end
26
+
27
+ def register(tool)
28
+ @tools[tool.name] = tool
29
+ tool
30
+ end
31
+
32
+ def get(name)
33
+ Praxo.error('TOOL_NOT_FOUND', "tool not found: #{name}") unless @tools.key?(name)
34
+ @tools[name]
35
+ end
36
+
37
+ def specs
38
+ @tools.values.map do |tool|
39
+ Models.tool_spec(name: tool.name, description: tool.description)
40
+ end.sort_by(&:name)
41
+ end
42
+ end
43
+
44
+ class StaticModel
45
+ def initialize(content)
46
+ @content = content.to_s
47
+ end
48
+
49
+ def generate(_request)
50
+ Models.response(message: Models.message_assistant(@content))
51
+ end
52
+ end
53
+
54
+ class AgentOptions
55
+ attr_accessor :model, :tools, :events, :policy, :system_prompt, :context,
56
+ :max_steps, :max_tokens, :tool_choice, :middleware,
57
+ :allowed_tools
58
+
59
+ def initialize(model)
60
+ @model = model
61
+ @tools = nil
62
+ @events = nil
63
+ @policy = nil
64
+ @system_prompt = ''
65
+ @context = nil
66
+ @max_steps = 8
67
+ @max_tokens = 0
68
+ @tool_choice = nil
69
+ @middleware = []
70
+ @allowed_tools = []
71
+ end
72
+ end
73
+
74
+ class Runner
75
+ def initialize(options)
76
+ @options = options
77
+ end
78
+
79
+ def run(session, prompt)
80
+ options = @options
81
+ max_steps = options.max_steps.to_i.positive? ? options.max_steps : 8
82
+ run_id = "run-#{(Time.now.to_f * 1000).to_i}"
83
+ messages = session.messages.dup
84
+ unless options.system_prompt.to_s.empty?
85
+ messages.unshift(Models.message_text(Models::SYSTEM, options.system_prompt))
86
+ end
87
+ messages << Models.message_text(Models::USER, prompt.to_s)
88
+ usage = Models.usage
89
+ entered = []
90
+ begin
91
+ before_run(options, entered, run_id, prompt)
92
+ rescue StandardError => e
93
+ after_run(options, entered, nil, e)
94
+ raise
95
+ end
96
+ emit(options, 'run.started', session.id, run_id)
97
+ after_run_invoked = false
98
+ begin
99
+ (1..max_steps).each do |step|
100
+ authorize(options, Models.action(kind: 'model.generate', session_id: session.id))
101
+ if options.context
102
+ compacted, ok = options.context.compact(messages)
103
+ Praxo.error('AGENT_CONTEXT_BUDGET', 'context budget') unless ok
104
+ messages = compacted
105
+ end
106
+ request = Models.request(
107
+ messages: messages,
108
+ tools: options.tools ? options.tools.specs : [],
109
+ tool_choice: options.tool_choice
110
+ )
111
+ response = options.model.generate(request)
112
+ usage = usage.plus(response.usage || Models.usage)
113
+ total = usage.input_tokens.to_i + usage.output_tokens.to_i
114
+ if options.max_tokens.to_i.positive? && total > options.max_tokens
115
+ Praxo.error('AGENT_MAX_TOKENS', 'token budget')
116
+ end
117
+ message = response.message
118
+ messages << message
119
+ calls = message.tool_calls || []
120
+ if calls.empty?
121
+ result = Models.run_result(
122
+ run_id: run_id, output: message.content.to_s, steps: step,
123
+ usage: usage
124
+ )
125
+ after_run_invoked = true
126
+ cleanup_error = after_run(options, entered, result, nil)
127
+ raise cleanup_error if cleanup_error
128
+ emit(options, 'run.completed', session.id, run_id)
129
+ return result
130
+ end
131
+ calls.each do |call|
132
+ messages << invoke_tool(options, call, run_id, session.id)
133
+ end
134
+ end
135
+ Praxo.error('AGENT_MAX_STEPS', 'maximum steps')
136
+ rescue StandardError => e
137
+ after_run(options, entered, nil, e) unless after_run_invoked
138
+ emit(options, 'run.failed', session.id, run_id, { 'error' => e.message })
139
+ raise
140
+ end
141
+ end
142
+
143
+ private
144
+
145
+ def invoke_tool(options, call, run_id, session_id)
146
+ original_name = call.name
147
+ original_arguments = call.arguments
148
+ unless allowed?(options, original_name)
149
+ Praxo.error('AGENT_TOOL_NOT_ALLOWED', "tool #{original_name} is not allowed")
150
+ end
151
+ authorize(options, Models.action(
152
+ kind: 'tool.invoke', session_id: session_id,
153
+ tool_name: original_name, arguments: original_arguments
154
+ ))
155
+ options.middleware.each do |middleware|
156
+ next unless middleware.respond_to?(:before_tool)
157
+ call = middleware.before_tool(call) || call
158
+ end
159
+ unless allowed?(options, call.name)
160
+ Praxo.error('AGENT_TOOL_NOT_ALLOWED', "tool #{call.name} is not allowed")
161
+ end
162
+ if call.name != original_name || call.arguments != original_arguments
163
+ authorize(options, Models.action(
164
+ kind: 'tool.invoke', session_id: session_id,
165
+ tool_name: call.name, arguments: call.arguments
166
+ ))
167
+ end
168
+ Praxo.error('TOOL_NOT_FOUND', 'tool is not registered') unless options.tools
169
+ tool = options.tools.get(call.name)
170
+ emit(options, 'tool.call', session_id, run_id,
171
+ { 'name' => call.name, 'arguments' => call.arguments || {} })
172
+ value = tool.invoke(call.arguments || {})
173
+ Models.message_tool(Json.stringify(value), call.id.to_s)
174
+ end
175
+
176
+ def allowed?(options, name)
177
+ options.allowed_tools.empty? || options.allowed_tools.include?(name)
178
+ end
179
+
180
+ def before_run(options, entered, run_id, prompt)
181
+ state = { run_id: run_id, prompt: prompt }
182
+ options.middleware.each do |middleware|
183
+ entered << middleware
184
+ middleware.before_run(state) if middleware.respond_to?(:before_run)
185
+ end
186
+ end
187
+
188
+ def after_run(options, entered, result, error)
189
+ first_error = nil
190
+ entered.each do |middleware|
191
+ next unless middleware.respond_to?(:after_run)
192
+ begin
193
+ middleware.after_run(result, error)
194
+ rescue StandardError => caught
195
+ first_error ||= caught
196
+ end
197
+ end
198
+ first_error
199
+ end
200
+
201
+ def authorize(options, action)
202
+ return unless options.policy
203
+ decision = options.policy.call(action)
204
+ Praxo.error('AGENT_POLICY_DENIED', 'policy denied the action') unless decision&.allowed
205
+ end
206
+
207
+ def emit(options, type, session_id, run_id, data = nil)
208
+ return unless options.events
209
+ options.events.emit(Models.event(
210
+ type: type, session_id: session_id, run_id: run_id, data: data
211
+ ))
212
+ end
213
+ end
214
+
215
+ module_function
216
+
217
+ def run_agent(options, session, prompt)
218
+ Runner.new(options).run(session, prompt)
219
+ end
220
+ end
221
+ end
data/lib/praxo/errors.rb CHANGED
@@ -1,18 +1,18 @@
1
- module Praxo
2
- class Error < StandardError
3
- attr_reader :code
4
-
5
- def initialize(code, message)
6
- @code = code
7
- super(message)
8
- end
9
- end
10
-
11
- def self.error(code, message)
12
- raise Error.new(code, message)
13
- end
14
-
15
- def self.error_code(error)
16
- error.is_a?(Error) ? error.code : ''
17
- end
18
- end
1
+ module Praxo
2
+ class Error < StandardError
3
+ attr_reader :code
4
+
5
+ def initialize(code, message)
6
+ @code = code
7
+ super(message)
8
+ end
9
+ end
10
+
11
+ def self.error(code, message)
12
+ raise Error.new(code, message)
13
+ end
14
+
15
+ def self.error_code(error)
16
+ error.is_a?(Error) ? error.code : ''
17
+ end
18
+ end
data/lib/praxo/json.rb CHANGED
@@ -1,47 +1,47 @@
1
- require 'json'
2
-
3
- module Praxo
4
- module Json
5
- module_function
6
-
7
- def parse(text)
8
- JSON.parse(text)
9
- end
10
-
11
- def stringify(value)
12
- JSON.generate(value)
13
- end
14
-
15
- def get(node, key)
16
- node.is_a?(Hash) ? node[key] : nil
17
- end
18
-
19
- def has?(node, key)
20
- node.is_a?(Hash) && node.key?(key)
21
- end
22
-
23
- def string(node, key)
24
- value = get(node, key)
25
- value.is_a?(String) ? value : nil
26
- end
27
-
28
- def bool(node, key, fallback = false)
29
- value = get(node, key)
30
- value == true || value == false ? value : fallback
31
- end
32
-
33
- def int(node, key, fallback = 0)
34
- value = get(node, key)
35
- value.is_a?(Numeric) ? value.to_i : fallback
36
- end
37
-
38
- def array(node, key)
39
- value = get(node, key)
40
- value.is_a?(Array) ? value : []
41
- end
42
-
43
- def equals(left, right)
44
- left == right
45
- end
46
- end
47
- end
1
+ require 'json'
2
+
3
+ module Praxo
4
+ module Json
5
+ module_function
6
+
7
+ def parse(text)
8
+ JSON.parse(text)
9
+ end
10
+
11
+ def stringify(value)
12
+ JSON.generate(value)
13
+ end
14
+
15
+ def get(node, key)
16
+ node.is_a?(Hash) ? node[key] : nil
17
+ end
18
+
19
+ def has?(node, key)
20
+ node.is_a?(Hash) && node.key?(key)
21
+ end
22
+
23
+ def string(node, key)
24
+ value = get(node, key)
25
+ value.is_a?(String) ? value : nil
26
+ end
27
+
28
+ def bool(node, key, fallback = false)
29
+ value = get(node, key)
30
+ value == true || value == false ? value : fallback
31
+ end
32
+
33
+ def int(node, key, fallback = 0)
34
+ value = get(node, key)
35
+ value.is_a?(Numeric) ? value.to_i : fallback
36
+ end
37
+
38
+ def array(node, key)
39
+ value = get(node, key)
40
+ value.is_a?(Array) ? value : []
41
+ end
42
+
43
+ def equals(left, right)
44
+ left == right
45
+ end
46
+ end
47
+ end