praxo-core 0.1.0 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 58af042d8ca9a28e2090a34faf13dbee0de142463fcb4ad98b16b697baac8554
4
- data.tar.gz: 87919c6781c1dac6fd8f731289b02cf563e25d4a4fada8ad52321137236e97c6
3
+ metadata.gz: 6b5277a84676a585ae272a89166d4cf957c2d295ae65ce17e469f8b406c1c9b6
4
+ data.tar.gz: 196c814209b547fdc344f692f2b8eb721c7c934f60a8f213ad846d22feca3242
5
5
  SHA512:
6
- metadata.gz: a26816e4f1f02666c156f3607b4139b1bf3b28a1069e1be4b6853f1854fcbb63dc22217de302a7553713ee9c13c88e9a43a55cc3b689e3743bbc176f09d2c239
7
- data.tar.gz: 7348cb7bde0fa6f0c47526b15a5b0c5a1f0b003ff06344205d12d770dfbbee8c2da6a421ee14c2b5f4e84173728e2a5c0857d7ff7d9ec3d2300709e9d42268b4
6
+ metadata.gz: 5c3d00bbb7ebe5e62fbce729af6fb9fb756ef7cac79d6a3625bfd18edbd368e5990cdd213828cd07a42ac5da98da9430f0f536470cf8c377c47bf4a7233b2113
7
+ data.tar.gz: a3facc141b9639e06bc0ecca22f672496c56def5834dd52a98b00c928bf9e8c2a2e30d2255302ce6be3bdb616820e24df4499e8f1e46919b20d4ac0c53041c6b
data/README.md CHANGED
@@ -9,10 +9,17 @@ The normative contract is [`spec/`](../../spec/README.md); Go remains the
9
9
  reference implementation.
10
10
 
11
11
  - `SPEC_VERSION`: `0.2.0`
12
- - Core version: `0.1.0`
12
+ - Core version: `0.3.0`
13
13
  - Tier: core
14
14
  - License: MIT
15
15
  - Dependencies: Ruby standard library only
16
+ - The latest RubyGems package is `praxo-core@0.1.0`; `0.3.0` is a source release
17
+
18
+ ## Install
19
+
20
+ ```bash
21
+ gem install praxo-core
22
+ ```
16
23
 
17
24
  ## Scope
18
25
 
@@ -54,6 +61,24 @@ options.tools = registry
54
61
  options.max_steps = 8
55
62
  ```
56
63
 
64
+ ## Agent options
65
+
66
+ `AgentOptions` supports cancellation, stream callbacks, bounded parallel tool
67
+ execution, and run-scoped call limits:
68
+
69
+ - `cancellation` accepts a proc or an object responding to `cancelled?`. The
70
+ runner checks it before model work and before and after each tool.
71
+ - `on_stream` receives `Models::StreamPart` values for `values` and `messages`.
72
+ A callback error aborts the run and runs after-run cleanup.
73
+ - `max_parallel_tools` starts tool invocations in threads within each batch while
74
+ preserving result order. Tool implementations remain responsible for their own
75
+ thread safety.
76
+ - `Middleware::CallLimit` keeps counters per run ID, so independent runs do not
77
+ share limits.
78
+
79
+ `Praxo::SSE.parse` parses OpenAI-compatible server-sent-event text and raises
80
+ `MODEL_STREAM` for truncated streams or malformed half packets.
81
+
57
82
  ## Layout
58
83
 
59
84
  - `lib/praxo/` package source.
@@ -63,4 +88,5 @@ options.max_steps = 8
63
88
 
64
89
  ## Release
65
90
 
66
- Tag shape: `ruby/v0.1.0`. The tag targets `SPEC_VERSION` `0.2.0`.
91
+ Tag shape: `ruby/v0.3.0`. The tag targets `SPEC_VERSION` `0.2.0`. See
92
+ [CHANGELOG.md](CHANGELOG.md) for per-release changes and compatibility notes.
data/api/0.2.0.txt ADDED
@@ -0,0 +1,112 @@
1
+ # PRAXO-CORE Ruby API 0.2.0
2
+ # SPEC_VERSION 0.2.0
3
+
4
+ class AgentOptions
5
+ class CallLimit
6
+ class CheckpointAllowlist
7
+ class ContextBudget
8
+ class Error
9
+ class FilterSink
10
+ class FuncTool
11
+ class MemoryStore
12
+ class MultiSink
13
+ class PathChecker
14
+ class PiiRedactor
15
+ class Runner
16
+ class StaticModel
17
+ class StructuredOutput
18
+ class ToolRegistry
19
+ class ToolSelection
20
+ def action
21
+ def after_run
22
+ def allowed?
23
+ def allows?
24
+ def apply
25
+ def array
26
+ def as_int
27
+ def as_object
28
+ def as_string
29
+ def authorize
30
+ def before_model
31
+ def before_run
32
+ def before_tool
33
+ def bool
34
+ def check
35
+ def check_enum
36
+ def check_keys
37
+ def compact
38
+ def content_block
39
+ def decision
40
+ def emit
41
+ def equals
42
+ def error
43
+ def error_code
44
+ def estimate_tokens
45
+ def event
46
+ def extract_json
47
+ def fallback_model
48
+ def filter
49
+ def generate
50
+ def get
51
+ def has?
52
+ def initialize
53
+ def int
54
+ def invoke
55
+ def invoke_tool
56
+ def marshal
57
+ def memory_record
58
+ def message
59
+ def message_assistant
60
+ def message_text
61
+ def message_tool
62
+ def message_wire
63
+ def parse
64
+ def parse_input_json
65
+ def parse_json_call
66
+ def parse_manifest
67
+ def parse_plain
68
+ def parse_tag
69
+ def parse_toolcall
70
+ def parse_xml
71
+ def plus
72
+ def put
73
+ def redact
74
+ def redact_value
75
+ def register
76
+ def reject
77
+ def request
78
+ def resolve_path
79
+ def response
80
+ def run
81
+ def run_agent
82
+ def run_result
83
+ def search
84
+ def session
85
+ def specs
86
+ def string
87
+ def stringify
88
+ def strip_markup
89
+ def structured_apply
90
+ def text_present
91
+ def tool_call
92
+ def tool_spec
93
+ def truncate_to_tokens
94
+ def unmarshal
95
+ def usage
96
+ def validate
97
+ def visit
98
+ def visit_array
99
+ def visit_boolean
100
+ def visit_number
101
+ def visit_object
102
+ def visit_string
103
+ def within?
104
+ module Agent
105
+ module Json
106
+ module JsonSchema
107
+ module Middleware
108
+ module Models
109
+ module Plugin
110
+ module Praxo
111
+ module Runtime
112
+ module Toolcall
data/api/0.3.0.txt ADDED
@@ -0,0 +1,119 @@
1
+ # PRAXO-CORE Ruby API 0.3.0
2
+ # SPEC_VERSION 0.2.0
3
+
4
+ class AgentOptions
5
+ class CallLimit
6
+ class CheckpointAllowlist
7
+ class ContextBudget
8
+ class Error
9
+ class FilterSink
10
+ class FuncTool
11
+ class MemoryStore
12
+ class MultiSink
13
+ class PathChecker
14
+ class PiiRedactor
15
+ class Runner
16
+ class StaticModel
17
+ class StructuredOutput
18
+ class ToolRegistry
19
+ class ToolSelection
20
+ def action
21
+ def after_run
22
+ def allowed?
23
+ def allows?
24
+ def apply
25
+ def array
26
+ def as_int
27
+ def as_object
28
+ def as_string
29
+ def authorize
30
+ def before_model
31
+ def before_run
32
+ def before_tool
33
+ def bool
34
+ def check
35
+ def check_cancelled
36
+ def check_enum
37
+ def check_keys
38
+ def compact
39
+ def content_block
40
+ def decision
41
+ def emit
42
+ def emit_stream
43
+ def equals
44
+ def error
45
+ def error_code
46
+ def estimate_tokens
47
+ def event
48
+ def execute_tool
49
+ def extract_json
50
+ def fallback_model
51
+ def filter
52
+ def generate
53
+ def get
54
+ def has?
55
+ def initialize
56
+ def int
57
+ def invoke
58
+ def invoke_tool
59
+ def invoke_tools
60
+ def marshal
61
+ def memory_record
62
+ def message
63
+ def message_assistant
64
+ def message_text
65
+ def message_tool
66
+ def message_wire
67
+ def parse
68
+ def parse_input_json
69
+ def parse_json_call
70
+ def parse_manifest
71
+ def parse_plain
72
+ def parse_tag
73
+ def parse_toolcall
74
+ def parse_xml
75
+ def plus
76
+ def prepare_tool
77
+ def put
78
+ def redact
79
+ def redact_value
80
+ def register
81
+ def reject
82
+ def request
83
+ def resolve_path
84
+ def response
85
+ def run
86
+ def run_agent
87
+ def run_result
88
+ def search
89
+ def session
90
+ def specs
91
+ def stream_part
92
+ def string
93
+ def stringify
94
+ def strip_markup
95
+ def structured_apply
96
+ def text_present
97
+ def tool_call
98
+ def tool_spec
99
+ def truncate_to_tokens
100
+ def unmarshal
101
+ def usage
102
+ def validate
103
+ def visit
104
+ def visit_array
105
+ def visit_boolean
106
+ def visit_number
107
+ def visit_object
108
+ def visit_string
109
+ def within?
110
+ module Agent
111
+ module Json
112
+ module JsonSchema
113
+ module Middleware
114
+ module Models
115
+ module Plugin
116
+ module Praxo
117
+ module Runtime
118
+ module SSE
119
+ module Toolcall
data/lib/praxo/agent.rb CHANGED
@@ -1,162 +1,292 @@
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, :cancellation, :on_stream,
58
+ :max_parallel_tools
59
+
60
+ def initialize(model)
61
+ @model = model
62
+ @tools = nil
63
+ @events = nil
64
+ @policy = nil
65
+ @system_prompt = ''
66
+ @context = nil
67
+ @max_steps = 8
68
+ @max_tokens = 0
69
+ @tool_choice = nil
70
+ @middleware = []
71
+ @allowed_tools = []
72
+ @cancellation = nil
73
+ @on_stream = nil
74
+ @max_parallel_tools = 1
75
+ end
76
+ end
77
+
78
+ class Runner
79
+ def initialize(options)
80
+ @options = options
81
+ end
82
+
83
+ def run(session, prompt)
84
+ options = @options
85
+ max_steps = options.max_steps.to_i.positive? ? options.max_steps : 8
86
+ run_id = "run-#{(Time.now.to_f * 1000).to_i}"
87
+ messages = session.messages.dup
88
+ unless options.system_prompt.to_s.empty?
89
+ messages.unshift(Models.message_text(Models::SYSTEM, options.system_prompt))
90
+ end
91
+ messages << Models.message_text(Models::USER, prompt.to_s)
92
+ usage = Models.usage
93
+ entered = []
94
+ begin
95
+ before_run(options, entered, run_id, prompt)
96
+ rescue StandardError => e
97
+ after_run(options, entered, nil, e)
98
+ raise
99
+ end
100
+ emit(options, 'run.started', session.id, run_id)
101
+ emit_stream(options, Models.stream_part(
102
+ run_id: run_id, session_id: session.id, kind: 'values',
103
+ step: 0, data: prompt.to_s
104
+ ))
105
+ after_run_invoked = false
106
+ begin
107
+ (1..max_steps).each do |step|
108
+ check_cancelled(options)
109
+ authorize(options, Models.action(kind: 'model.generate', session_id: session.id))
110
+ if options.context
111
+ compacted, ok = options.context.compact(messages)
112
+ Praxo.error('AGENT_CONTEXT_BUDGET', 'context budget') unless ok
113
+ messages = compacted
114
+ end
115
+ request = Models.request(
116
+ messages: messages,
117
+ tools: options.tools ? options.tools.specs : [],
118
+ tool_choice: options.tool_choice
119
+ )
120
+ response = options.model.generate(request)
121
+ check_cancelled(options)
122
+ usage = usage.plus(response.usage || Models.usage)
123
+ total = usage.input_tokens.to_i + usage.output_tokens.to_i
124
+ if options.max_tokens.to_i.positive? && total > options.max_tokens
125
+ Praxo.error('AGENT_MAX_TOKENS', 'token budget')
126
+ end
127
+ message = response.message
128
+ messages << message
129
+ calls = message.tool_calls || []
130
+ if calls.empty?
131
+ result = Models.run_result(
132
+ run_id: run_id, output: message.content.to_s, steps: step,
133
+ usage: usage
134
+ )
135
+ emit_stream(options, Models.stream_part(
136
+ run_id: run_id, session_id: session.id, kind: 'messages',
137
+ step: step, data: message.content.to_s
138
+ ))
139
+ after_run_invoked = true
140
+ cleanup_error = after_run(options, entered, result, nil)
141
+ raise cleanup_error if cleanup_error
142
+ emit(options, 'run.completed', session.id, run_id)
143
+ return result
144
+ end
145
+ messages.concat(invoke_tools(options, calls, run_id, session.id))
146
+ end
147
+ Praxo.error('AGENT_MAX_STEPS', 'maximum steps')
148
+ rescue StandardError => e
149
+ after_run(options, entered, nil, e) unless after_run_invoked
150
+ emit(options, 'run.failed', session.id, run_id, { 'error' => e.message })
151
+ raise
152
+ end
153
+ end
154
+
155
+ private
156
+
157
+ def invoke_tools(options, calls, run_id, session_id)
158
+ limit = [options.max_parallel_tools.to_i, 1].max
159
+ results = []
160
+ calls.each_slice(limit) do |batch|
161
+ check_cancelled(options)
162
+ prepared = batch.map do |call|
163
+ prepare_tool(options, call, run_id, session_id)
164
+ end
165
+ if limit <= 1
166
+ prepared.each do |tool, call|
167
+ results << execute_tool(tool, call)
168
+ check_cancelled(options)
169
+ end
170
+ else
171
+ outcomes = prepared.map do |tool, call|
172
+ Thread.new do
173
+ begin
174
+ [:ok, execute_tool(tool, call)]
175
+ rescue StandardError => caught
176
+ [:error, caught]
177
+ end
178
+ end
179
+ end.map(&:value)
180
+ outcomes.each do |status, payload|
181
+ raise payload if status == :error
182
+ results << payload
183
+ end
184
+ check_cancelled(options)
185
+ end
186
+ end
187
+ results
188
+ end
189
+
190
+ def invoke_tool(options, call, run_id, session_id)
191
+ tool, prepared = prepare_tool(options, call, run_id, session_id)
192
+ execute_tool(tool, prepared)
193
+ end
194
+
195
+ def prepare_tool(options, call, run_id, session_id)
196
+ original_name = call.name
197
+ original_arguments = call.arguments
198
+ unless allowed?(options, original_name)
199
+ Praxo.error('AGENT_TOOL_NOT_ALLOWED', "tool #{original_name} is not allowed")
200
+ end
201
+ authorize(options, Models.action(
202
+ kind: 'tool.invoke', session_id: session_id,
203
+ tool_name: original_name, arguments: original_arguments
204
+ ))
205
+ options.middleware.each do |middleware|
206
+ next unless middleware.respond_to?(:before_tool)
207
+ call = middleware.before_tool(call) || call
208
+ end
209
+ unless allowed?(options, call.name)
210
+ Praxo.error('AGENT_TOOL_NOT_ALLOWED', "tool #{call.name} is not allowed")
211
+ end
212
+ if call.name != original_name || call.arguments != original_arguments
213
+ authorize(options, Models.action(
214
+ kind: 'tool.invoke', session_id: session_id,
215
+ tool_name: call.name, arguments: call.arguments
216
+ ))
217
+ end
218
+ Praxo.error('TOOL_NOT_FOUND', 'tool is not registered') unless options.tools
219
+ tool = options.tools.get(call.name)
220
+ emit(options, 'tool.call', session_id, run_id,
221
+ { 'name' => call.name, 'arguments' => call.arguments || {} })
222
+ [tool, call]
223
+ end
224
+
225
+ def execute_tool(tool, call)
226
+ value = tool.invoke(call.arguments || {})
227
+ Models.message_tool(Json.stringify(value), call.id.to_s)
228
+ end
229
+
230
+ def check_cancelled(options)
231
+ return unless options.cancellation
232
+ cancelled = if options.cancellation.respond_to?(:cancelled?)
233
+ options.cancellation.cancelled?
234
+ elsif options.cancellation.respond_to?(:call)
235
+ options.cancellation.call
236
+ else
237
+ options.cancellation
238
+ end
239
+ Praxo.error('AGENT_CANCELLED', 'run cancelled') if cancelled
240
+ end
241
+
242
+ def emit_stream(options, part)
243
+ return unless options.on_stream
244
+ options.on_stream.call(part)
245
+ end
246
+
247
+ def allowed?(options, name)
248
+ options.allowed_tools.empty? || options.allowed_tools.include?(name)
249
+ end
250
+
251
+ def before_run(options, entered, run_id, prompt)
252
+ state = { run_id: run_id, prompt: prompt }
253
+ options.middleware.each do |middleware|
254
+ entered << middleware
255
+ middleware.before_run(state) if middleware.respond_to?(:before_run)
256
+ end
257
+ end
258
+
259
+ def after_run(options, entered, result, error)
260
+ first_error = nil
261
+ entered.each do |middleware|
262
+ next unless middleware.respond_to?(:after_run)
263
+ begin
264
+ middleware.after_run(result, error)
265
+ rescue StandardError => caught
266
+ first_error ||= caught
267
+ end
268
+ end
269
+ first_error
270
+ end
271
+
272
+ def authorize(options, action)
273
+ return unless options.policy
274
+ decision = options.policy.call(action)
275
+ Praxo.error('AGENT_POLICY_DENIED', 'policy denied the action') unless decision&.allowed
276
+ end
277
+
278
+ def emit(options, type, session_id, run_id, data = nil)
279
+ return unless options.events
280
+ options.events.emit(Models.event(
281
+ type: type, session_id: session_id, run_id: run_id, data: data
282
+ ))
283
+ end
284
+ end
285
+
286
+ module_function
287
+
288
+ def run_agent(options, session, prompt)
289
+ Runner.new(options).run(session, prompt)
290
+ end
291
+ end
292
+ end
@@ -58,20 +58,25 @@ module Praxo
58
58
  def initialize(model_limit:, tool_limit:)
59
59
  @model_limit = model_limit
60
60
  @tool_limit = tool_limit
61
- @models = 0
62
- @tools = 0
61
+ @runs = Hash.new { |hash, key| hash[key] = { models: 0, tools: 0 } }
63
62
  end
64
63
 
65
- def before_model
66
- @models += 1
67
- if @model_limit.positive? && @models > @model_limit
64
+ def before_run(state)
65
+ @runs[state[:run_id]] = { models: 0, tools: 0 }
66
+ end
67
+
68
+ def before_model(run_id = :default)
69
+ counts = @runs[run_id]
70
+ counts[:models] += 1
71
+ if @model_limit.positive? && counts[:models] > @model_limit
68
72
  Praxo.error('AGENT_MODEL_CALL_LIMIT', 'model call limit')
69
73
  end
70
74
  end
71
75
 
72
- def before_tool
73
- @tools += 1
74
- if @tool_limit.positive? && @tools > @tool_limit
76
+ def before_tool(run_id = :default)
77
+ counts = @runs[run_id]
78
+ counts[:tools] += 1
79
+ if @tool_limit.positive? && counts[:tools] > @tool_limit
75
80
  Praxo.error('AGENT_TOOL_CALL_LIMIT', 'tool call limit')
76
81
  end
77
82
  end
data/lib/praxo/models.rb CHANGED
@@ -29,6 +29,7 @@ module Praxo
29
29
  Action = Struct.new(:kind, :session_id, :tool_name, :arguments, keyword_init: true)
30
30
  Decision = Struct.new(:allowed, :reason, keyword_init: true)
31
31
  Event = Struct.new(:type, :session_id, :run_id, :data, keyword_init: true)
32
+ StreamPart = Struct.new(:run_id, :session_id, :kind, :step, :data, keyword_init: true)
32
33
 
33
34
  module_function
34
35
 
@@ -121,6 +122,16 @@ module Praxo
121
122
  Event.new(type: type, session_id: session_id, run_id: run_id, data: data)
122
123
  end
123
124
 
125
+ def stream_part(run_id:, session_id:, kind:, step: 0, data: nil)
126
+ StreamPart.new(
127
+ run_id: run_id,
128
+ session_id: session_id,
129
+ kind: kind,
130
+ step: step,
131
+ data: data
132
+ )
133
+ end
134
+
124
135
  def action(kind:, session_id:, tool_name: nil, arguments: nil)
125
136
  Action.new(kind: kind, session_id: session_id, tool_name: tool_name,
126
137
  arguments: arguments)
data/lib/praxo/sse.rb ADDED
@@ -0,0 +1,37 @@
1
+ require_relative 'errors'
2
+ require_relative 'json'
3
+
4
+ module Praxo
5
+ module SSE
6
+ module_function
7
+
8
+ def parse(text)
9
+ content = +''
10
+ done = false
11
+ text.to_s.each_line do |raw|
12
+ line = raw.strip
13
+ next unless line.start_with?('data:')
14
+ chunk = line[5..].to_s.strip
15
+ next if chunk.empty?
16
+ if chunk == '[DONE]'
17
+ done = true
18
+ break
19
+ end
20
+ begin
21
+ event = Json.parse(chunk)
22
+ rescue StandardError
23
+ Praxo.error('MODEL_STREAM', 'invalid SSE packet')
24
+ end
25
+ next unless event.is_a?(Hash)
26
+ (event['choices'] || []).each do |choice|
27
+ delta = choice['delta'] || {}
28
+ content << delta['content'].to_s if delta['content']
29
+ finish = choice['finish_reason']
30
+ done = true if choice.key?('finish_reason') && !finish.nil?
31
+ end
32
+ end
33
+ Praxo.error('MODEL_STREAM', 'stream ended before completion') unless done
34
+ content
35
+ end
36
+ end
37
+ end
data/lib/praxo.rb CHANGED
@@ -5,5 +5,6 @@ require_relative 'praxo/toolcall'
5
5
  require_relative 'praxo/jsonschema'
6
6
  require_relative 'praxo/plugin'
7
7
  require_relative 'praxo/runtime'
8
+ require_relative 'praxo/sse'
8
9
  require_relative 'praxo/middleware'
9
10
  require_relative 'praxo/agent'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: praxo-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - AxisVela
@@ -20,6 +20,8 @@ files:
20
20
  - LICENSE
21
21
  - README.md
22
22
  - api/0.1.0.txt
23
+ - api/0.2.0.txt
24
+ - api/0.3.0.txt
23
25
  - lib/praxo.rb
24
26
  - lib/praxo/agent.rb
25
27
  - lib/praxo/errors.rb
@@ -29,6 +31,7 @@ files:
29
31
  - lib/praxo/models.rb
30
32
  - lib/praxo/plugin.rb
31
33
  - lib/praxo/runtime.rb
34
+ - lib/praxo/sse.rb
32
35
  - lib/praxo/toolcall.rb
33
36
  homepage: https://axisvela.github.io/praxo-core/
34
37
  licenses: