ask-agent 0.20.0 → 0.23.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/CHANGELOG.md +78 -0
- data/README.md +7 -0
- data/lib/ask/agent/chat.rb +30 -17
- data/lib/ask/agent/configuration.rb +4 -3
- data/lib/ask/agent/extensions/audit_log/active_record_writer.rb +70 -0
- data/lib/ask/agent/extensions/audit_log.rb +166 -19
- data/lib/ask/agent/loop.rb +7 -4
- data/lib/ask/agent/session.rb +12 -2
- data/lib/ask/agent/version.rb +1 -1
- data/lib/ask/agent.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: cf7561a6eebc134d4e84ba829e637638a783eb7dd425c1b1c24d2d879e5ff47e
|
|
4
|
+
data.tar.gz: 73cdf03eec1b27f3e50cbfa28399bdf17994ea1b5925be36ce463ebf48c6669d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 37b261bba11ac89e4051b2f631305ed6741ae8ba6a18f0973a2fa7a04416d3dd84d2ca9e4517d01f4776a21fbf143125a51bcceb74b67ce361d9c4ea35e15279
|
|
7
|
+
data.tar.gz: bc3051e56b9b68a003da46e1ee4c4ec10265239bfc2482c6cc335caf510f099256e1487526ead9277687004a0bb4c68926efb9f51db338b103f69188f21ed60a
|
data/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,81 @@
|
|
|
1
|
+
## [0.23.0] — 2026-07-30
|
|
2
|
+
|
|
3
|
+
### Added
|
|
4
|
+
|
|
5
|
+
- **`Ask::Agent::Configuration#default_provider` — global default provider**.
|
|
6
|
+
Pins which provider serves the default model when the model id is
|
|
7
|
+
registered under multiple providers (e.g. the same model on several
|
|
8
|
+
OpenAI-compatible endpoints). `Chat#build_provider` falls back to the
|
|
9
|
+
global default before the model's own catalog entry. A per-chat
|
|
10
|
+
`provider:` override or a Definition-level `provider` always wins.
|
|
11
|
+
|
|
12
|
+
## [0.22.0] — 2026-07-26
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- **`Ask::Agent::Extensions::AuditLog` — event-driven audit logging with pluggable adapters**.
|
|
17
|
+
Subscribes to all session events and writes them to a configurable adapter.
|
|
18
|
+
Ships with two built-in adapters:
|
|
19
|
+
|
|
20
|
+
- **`FileAdapter`** — appends JSON lines to a file (development/quick-start)
|
|
21
|
+
- **`ActiveRecordWriter`** — writes to an `ask_audit_logs` table, auto-creates it
|
|
22
|
+
on first write using `CREATE TABLE IF NOT EXISTS`. Works with or without Rails
|
|
23
|
+
migrations.
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
# Global config (all sessions)
|
|
27
|
+
Ask::Agent.configure { |c| c.audit_log = { adapter: :active_record } }
|
|
28
|
+
|
|
29
|
+
# Per-session
|
|
30
|
+
session = Ask::Agent::Session.new(model: "gpt-4o", audit_log: { adapter: :file })
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Events logged: `session_start`, `session_end`, `turn_end`, `tool_execution_start`,
|
|
34
|
+
`tool_execution_end`, `error`, `max_turns_exceeded`, `loop_detected`,
|
|
35
|
+
`compaction_end`, `evaluation_blocked`.
|
|
36
|
+
|
|
37
|
+
Sensitive arguments (password, token, api_key, sql, etc.) are redacted automatically.
|
|
38
|
+
|
|
39
|
+
- **12 tests** for AuditLog — adapter contract, event subscription, config integration,
|
|
40
|
+
sensitive arg redaction, legacy hook interface.
|
|
41
|
+
|
|
42
|
+
### Changed
|
|
43
|
+
|
|
44
|
+
- `Session#initialize` now accepts `audit_log:` parameter and falls back to
|
|
45
|
+
`Ask::Agent.configuration.audit_log`.
|
|
46
|
+
- `Ask::Agent::Configuration#audit_log` — new accessor for global audit log config.
|
|
47
|
+
|
|
48
|
+
## [0.21.0] — 2026-07-26
|
|
49
|
+
|
|
50
|
+
- Version bump only.
|
|
51
|
+
|
|
52
|
+
## [0.20.0] — 2026-07-26
|
|
53
|
+
|
|
54
|
+
### Added
|
|
55
|
+
|
|
56
|
+
- **`Events::ThinkingDelta`** — new event emitted when a chunk has thinking/reasoning
|
|
57
|
+
content. The loop already received chunks with `.thinking` data from providers like
|
|
58
|
+
DeepSeek and Claude, but it wasn't exposed as a dedicated event. Now it is.
|
|
59
|
+
- **`Ask::Agent::Streaming`** — framework-agnostic SSE streaming module. Returns a
|
|
60
|
+
Rack-compatible Enumerator that yields SSE-formatted strings as the agent runs.
|
|
61
|
+
Works with any Rack server without requiring Rails or ActionController::Live.
|
|
62
|
+
|
|
63
|
+
Two modes:
|
|
64
|
+
- **Enumerable mode** (no block) — for Rack/Roda/Sinatra:
|
|
65
|
+
```ruby
|
|
66
|
+
stream = Ask::Agent::Streaming.run(session, prompt)
|
|
67
|
+
[200, { "Content-Type" => "text/event-stream" }, stream]
|
|
68
|
+
```
|
|
69
|
+
- **Block mode** — for Rails `ActionController::Live::SSE`:
|
|
70
|
+
```ruby
|
|
71
|
+
Ask::Agent::Streaming.run(session, prompt) do |type, data|
|
|
72
|
+
sse.write(data, event: type)
|
|
73
|
+
end
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
- **19 tests** for Streaming + ThinkingDelta — Enumerator mode, block mode, custom
|
|
77
|
+
event maps, error handling, SSE line format, event structure.
|
|
78
|
+
|
|
1
79
|
## [0.19.0] — 2026-07-26
|
|
2
80
|
|
|
3
81
|
### Added
|
data/README.md
CHANGED
|
@@ -218,6 +218,7 @@ c.middleware.use :model_fallback,
|
|
|
218
218
|
```ruby
|
|
219
219
|
Ask::Agent.configure do |c|
|
|
220
220
|
c.default_model = "claude-sonnet-4"
|
|
221
|
+
c.default_provider = :anthropic
|
|
221
222
|
c.default_max_turns = 50
|
|
222
223
|
c.compactor_enabled = true
|
|
223
224
|
c.compactor_threshold = 0.8
|
|
@@ -226,6 +227,12 @@ Ask::Agent.configure do |c|
|
|
|
226
227
|
end
|
|
227
228
|
```
|
|
228
229
|
|
|
230
|
+
`default_provider` pins which provider serves the default model when the
|
|
231
|
+
model name doesn't uniquely identify one (for example, the same model id
|
|
232
|
+
registered under multiple OpenAI-compatible providers). A `provider:`
|
|
233
|
+
passed to `Session.new` or declared in an agent `Definition` always wins
|
|
234
|
+
over the global default.
|
|
235
|
+
|
|
229
236
|
## Persistence
|
|
230
237
|
|
|
231
238
|
```ruby
|
data/lib/ask/agent/chat.rb
CHANGED
|
@@ -19,7 +19,7 @@ module Ask
|
|
|
19
19
|
ToolCallInfo = Data.define(:id, :name, :arguments)
|
|
20
20
|
|
|
21
21
|
ChatChunk = Data.define(:content, :tool_calls, :thinking, :input_tokens, :output_tokens) do
|
|
22
|
-
def tool_call? = !tool_calls.empty?
|
|
22
|
+
def tool_call? = !tool_calls.to_a.empty?
|
|
23
23
|
end
|
|
24
24
|
|
|
25
25
|
class Chat
|
|
@@ -68,7 +68,8 @@ module Ask
|
|
|
68
68
|
metadata: {
|
|
69
69
|
input_tokens: response_msg.input_tokens,
|
|
70
70
|
output_tokens: response_msg.output_tokens,
|
|
71
|
-
cost: response_msg.cost
|
|
71
|
+
cost: response_msg.cost,
|
|
72
|
+
thinking: response_msg.thinking
|
|
72
73
|
}.compact
|
|
73
74
|
)
|
|
74
75
|
|
|
@@ -115,7 +116,7 @@ module Ask
|
|
|
115
116
|
end
|
|
116
117
|
|
|
117
118
|
def build_provider
|
|
118
|
-
slug = @provider_override&.to_s || @model_info.provider
|
|
119
|
+
slug = @provider_override&.to_s || Ask::Agent.configuration.default_provider&.to_s || @model_info.provider
|
|
119
120
|
klass = Ask::Provider.resolve(slug)
|
|
120
121
|
klass.new(provider_config(slug))
|
|
121
122
|
end
|
|
@@ -255,12 +256,17 @@ module Ask
|
|
|
255
256
|
def accumulate_tool_calls(raw_chunk, calls_acc)
|
|
256
257
|
return unless raw_chunk.tool_call?
|
|
257
258
|
|
|
258
|
-
raw_chunk.tool_calls
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
259
|
+
tool_calls = raw_chunk.tool_calls
|
|
260
|
+
return unless tool_calls.respond_to?(:each)
|
|
261
|
+
|
|
262
|
+
tool_calls.each do |tc|
|
|
263
|
+
next unless tc.respond_to?(:[])
|
|
264
|
+
idx = tc[:index] || tc["index"] || 0
|
|
265
|
+
calls_acc[idx] ||= { id: tc[:id] || tc["id"], name: tc[:name] || tc["name"], arguments: +"" }
|
|
266
|
+
calls_acc[idx][:id] ||= tc[:id] || tc["id"]
|
|
267
|
+
calls_acc[idx][:name] ||= tc[:name] || tc["name"]
|
|
268
|
+
arguments = tc[:arguments] || tc["arguments"]
|
|
269
|
+
calls_acc[idx][:arguments] << arguments.to_s if arguments
|
|
264
270
|
end
|
|
265
271
|
end
|
|
266
272
|
|
|
@@ -279,14 +285,21 @@ module Ask
|
|
|
279
285
|
|
|
280
286
|
def build_tool_call_hash(raw_calls)
|
|
281
287
|
hash = {}
|
|
288
|
+
return hash unless raw_calls.respond_to?(:each)
|
|
289
|
+
|
|
282
290
|
raw_calls.each do |tc|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
291
|
+
# Provider tool calls come as an Array of Hashes.
|
|
292
|
+
# A Hash argument (key-value pairs) means the caller passed
|
|
293
|
+
# a Hash instead of an Array — iterate values instead.
|
|
294
|
+
if tc.is_a?(Hash)
|
|
295
|
+
id = tc[:id] || tc["id"]
|
|
296
|
+
next unless id
|
|
297
|
+
hash[id] = ToolCallInfo.new(
|
|
298
|
+
id: id,
|
|
299
|
+
name: tc[:name] || tc["name"] || "",
|
|
300
|
+
arguments: tc[:arguments] || tc["arguments"] || ""
|
|
301
|
+
)
|
|
302
|
+
end
|
|
290
303
|
end
|
|
291
304
|
hash
|
|
292
305
|
end
|
|
@@ -298,7 +311,7 @@ module Ask
|
|
|
298
311
|
content: stream.accumulated_text,
|
|
299
312
|
tool_calls: build_current_tool_calls(calls_acc),
|
|
300
313
|
tool_results: {},
|
|
301
|
-
thinking: stream.chunks.filter_map(&:thinking).
|
|
314
|
+
thinking: stream.chunks.filter_map(&:thinking).join,
|
|
302
315
|
input_tokens: tokens[:input],
|
|
303
316
|
output_tokens: tokens[:output],
|
|
304
317
|
cost: cost
|
|
@@ -3,9 +3,10 @@
|
|
|
3
3
|
module Ask
|
|
4
4
|
module Agent
|
|
5
5
|
class Configuration
|
|
6
|
-
attr_accessor :default_model, :
|
|
7
|
-
:
|
|
8
|
-
:prompt_caching, :default_evaluator_model
|
|
6
|
+
attr_accessor :default_model, :default_provider, :default_max_turns,
|
|
7
|
+
:compactor_enabled, :compactor_threshold, :parallel_tool_execution,
|
|
8
|
+
:max_tool_retries, :prompt_caching, :default_evaluator_model,
|
|
9
|
+
:audit_log
|
|
9
10
|
|
|
10
11
|
# @return [Middleware::Pipeline] the middleware pipeline for provider calls
|
|
11
12
|
attr_reader :middleware
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Ask
|
|
4
|
+
module Agent
|
|
5
|
+
module Extensions
|
|
6
|
+
class AuditLog
|
|
7
|
+
# ActiveRecord adapter for the audit log.
|
|
8
|
+
# Auto-creates the +ask_audit_logs+ table on first write using
|
|
9
|
+
# CREATE TABLE IF NOT EXISTS, so it works with or without Rails
|
|
10
|
+
# migrations. Rails users can also run:
|
|
11
|
+
#
|
|
12
|
+
# rails generate ask_rails:install
|
|
13
|
+
#
|
|
14
|
+
# to get a proper migration file. The migration uses
|
|
15
|
+
# +if_not_exists: true+ so it won't conflict with auto-creation.
|
|
16
|
+
class ActiveRecordWriter < Adapter
|
|
17
|
+
TABLE_NAME = "ask_audit_logs"
|
|
18
|
+
|
|
19
|
+
def initialize
|
|
20
|
+
@table_checked = false
|
|
21
|
+
@mutex = Mutex.new
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def write(entry)
|
|
25
|
+
return unless defined?(ActiveRecord::Base)
|
|
26
|
+
|
|
27
|
+
ensure_table!
|
|
28
|
+
conn = ActiveRecord::Base.connection
|
|
29
|
+
conn.execute(
|
|
30
|
+
"INSERT INTO #{TABLE_NAME} (session_id, event_type, data, timestamp, created_at, updated_at) " \
|
|
31
|
+
"VALUES (#{quote(entry[:session_id])}, #{quote(entry[:event_type])}, " \
|
|
32
|
+
"#{quote(entry[:data].to_json)}, #{quote(entry[:timestamp])}, " \
|
|
33
|
+
"#{quote(Time.now.utc.iso8601(3))}, #{quote(Time.now.utc.iso8601(3))})"
|
|
34
|
+
)
|
|
35
|
+
rescue ActiveRecord::ActiveRecordError => e
|
|
36
|
+
warn "[ask-agent] AuditLog::ActiveRecordWriter write failed: #{e.message}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def ensure_table!
|
|
42
|
+
return if @table_checked
|
|
43
|
+
|
|
44
|
+
@mutex.synchronize do
|
|
45
|
+
return if @table_checked
|
|
46
|
+
conn = ActiveRecord::Base.connection
|
|
47
|
+
unless conn.table_exists?(TABLE_NAME)
|
|
48
|
+
conn.create_table(TABLE_NAME, if_not_exists: true) do |t|
|
|
49
|
+
t.string :session_id, null: false
|
|
50
|
+
t.string :event_type, null: false
|
|
51
|
+
t.jsonb :data, default: {}
|
|
52
|
+
t.datetime :timestamp, null: false
|
|
53
|
+
t.timestamps
|
|
54
|
+
|
|
55
|
+
t.index [:session_id, :event_type]
|
|
56
|
+
t.index :timestamp
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
@table_checked = true
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def quote(value)
|
|
64
|
+
ActiveRecord::Base.connection.quote(value)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -1,40 +1,187 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "json"
|
|
4
|
+
|
|
3
5
|
module Ask
|
|
4
6
|
module Agent
|
|
5
7
|
module Extensions
|
|
8
|
+
# Event-driven audit log for agent sessions.
|
|
9
|
+
#
|
|
10
|
+
# Subscribes to all session events and writes them to a configurable
|
|
11
|
+
# adapter. Ships with an ActiveRecord adapter; custom adapters can
|
|
12
|
+
# implement the simple {Adapter} interface.
|
|
13
|
+
#
|
|
14
|
+
# @example Enable globally (ActiveRecord)
|
|
15
|
+
# Ask::Agent.configure do |c|
|
|
16
|
+
# c.audit_log = { adapter: :active_record }
|
|
17
|
+
# end
|
|
18
|
+
#
|
|
19
|
+
# @example Per-session with custom adapter
|
|
20
|
+
# Session.new(model: "gpt-4o", audit_log: { adapter: MyWriter.new })
|
|
21
|
+
#
|
|
6
22
|
class AuditLog
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
23
|
+
# Pluggable adapter interface.
|
|
24
|
+
# Implement +write(entry)+ to persist a structured event hash.
|
|
25
|
+
class Adapter
|
|
26
|
+
def write(entry)
|
|
27
|
+
raise NotImplementedError
|
|
28
|
+
end
|
|
29
|
+
end
|
|
10
30
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
@
|
|
31
|
+
# Built-in adapter: appends JSON lines to a file.
|
|
32
|
+
class FileAdapter < Adapter
|
|
33
|
+
def initialize(path: "tmp/agent_audit.jsonl")
|
|
34
|
+
@path = path
|
|
35
|
+
@mutex = Mutex.new
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def write(entry)
|
|
39
|
+
@mutex.synchronize do
|
|
40
|
+
File.open(@path, "a") { |f| f.puts(JSON.generate(entry)) }
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Event types that get persisted (not every delta/stream event).
|
|
46
|
+
STORED_EVENTS = {
|
|
47
|
+
Events::SessionStart => "session_start",
|
|
48
|
+
Events::SessionEnd => "session_end",
|
|
49
|
+
Events::TurnEnd => "turn_end",
|
|
50
|
+
Events::ToolExecutionStart => "tool_execution_start",
|
|
51
|
+
Events::ToolExecutionEnd => "tool_execution_end",
|
|
52
|
+
Events::Error => "error",
|
|
53
|
+
Events::MaxTurnsExceeded => "max_turns_exceeded",
|
|
54
|
+
Events::LoopDetected => "loop_detected",
|
|
55
|
+
Events::CompactionEnd => "compaction_end",
|
|
56
|
+
Events::EvaluationBlocked => "evaluation_blocked"
|
|
57
|
+
}.freeze
|
|
58
|
+
|
|
59
|
+
def initialize(session, adapter: nil)
|
|
60
|
+
@session = session
|
|
61
|
+
@session_id = session.id
|
|
62
|
+
@adapter = resolve(adapter)
|
|
63
|
+
subscribe! if @adapter
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Subscribe to session events and log stored event types.
|
|
67
|
+
def subscribe!
|
|
68
|
+
@session.on_event do |event|
|
|
69
|
+
type = STORED_EVENTS[event.class]
|
|
70
|
+
next unless type
|
|
71
|
+
|
|
72
|
+
write_entry(type, extract(event))
|
|
16
73
|
end
|
|
17
74
|
end
|
|
18
75
|
|
|
76
|
+
# Legacy hook interface — kept for backward compatibility.
|
|
77
|
+
# Called by the hooks system after each tool execution.
|
|
19
78
|
def after_tool_call(tool_call, result, _context)
|
|
20
|
-
|
|
21
|
-
timestamp: Time.now.utc.iso8601(3),
|
|
79
|
+
write_entry("tool_call", {
|
|
22
80
|
tool_name: tool_call.name,
|
|
23
81
|
arguments: tool_call.arguments,
|
|
24
|
-
result: result,
|
|
25
|
-
|
|
26
|
-
}
|
|
82
|
+
result: result&.to_s&.to_s[0, 500],
|
|
83
|
+
duration_ms: result[:duration_ms]
|
|
84
|
+
})
|
|
85
|
+
end
|
|
27
86
|
|
|
28
|
-
|
|
29
|
-
@entries << entry
|
|
30
|
-
@io.puts entry.to_json
|
|
31
|
-
end
|
|
87
|
+
private
|
|
32
88
|
|
|
89
|
+
def resolve(adapter)
|
|
90
|
+
return nil if adapter.nil?
|
|
91
|
+
return adapter if adapter.is_a?(Adapter)
|
|
92
|
+
|
|
93
|
+
case adapter
|
|
94
|
+
when :active_record
|
|
95
|
+
require "ask/agent/extensions/audit_log/active_record_writer"
|
|
96
|
+
AuditLog::ActiveRecordWriter.new
|
|
97
|
+
when Hash
|
|
98
|
+
resolve(adapter[:adapter] || adapter[:writer])
|
|
99
|
+
when Symbol, String
|
|
100
|
+
# Try to load adapter by convention:
|
|
101
|
+
# :active_record → ask/agent/extensions/audit_log/active_record_writer
|
|
102
|
+
name = adapter.to_s
|
|
103
|
+
begin
|
|
104
|
+
require "ask/agent/extensions/audit_log/#{name}_writer"
|
|
105
|
+
klass_name = name.split("_").map(&:capitalize).join
|
|
106
|
+
klass = AuditLog.const_get(klass_name)
|
|
107
|
+
klass.new
|
|
108
|
+
rescue LoadError
|
|
109
|
+
warn "[ask-agent] AuditLog: adapter not found: #{name}"
|
|
110
|
+
nil
|
|
111
|
+
end
|
|
112
|
+
else
|
|
113
|
+
adapter
|
|
114
|
+
end
|
|
115
|
+
rescue LoadError
|
|
116
|
+
warn "[ask-agent] AuditLog: ActiveRecord adapter not available"
|
|
33
117
|
nil
|
|
34
118
|
end
|
|
35
119
|
|
|
36
|
-
def
|
|
37
|
-
|
|
120
|
+
def write_entry(type, data)
|
|
121
|
+
entry = {
|
|
122
|
+
session_id: @session_id,
|
|
123
|
+
event_type: type,
|
|
124
|
+
timestamp: Time.now.utc.iso8601(3),
|
|
125
|
+
data: data
|
|
126
|
+
}
|
|
127
|
+
@adapter.write(entry)
|
|
128
|
+
rescue => e
|
|
129
|
+
warn "[ask-agent] AuditLog write failed: #{e.message}"
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def extract(event)
|
|
133
|
+
case event
|
|
134
|
+
when Events::SessionStart
|
|
135
|
+
{}
|
|
136
|
+
when Events::SessionEnd
|
|
137
|
+
{
|
|
138
|
+
turn_count: event.turn_count,
|
|
139
|
+
tool_calls_made: event.tool_calls_made,
|
|
140
|
+
input_tokens: event.input_tokens,
|
|
141
|
+
output_tokens: event.output_tokens,
|
|
142
|
+
cost: event.cost
|
|
143
|
+
}
|
|
144
|
+
when Events::TurnEnd
|
|
145
|
+
{
|
|
146
|
+
turn_number: event.turn_number,
|
|
147
|
+
tool_results_count: event.tool_results&.length || 0,
|
|
148
|
+
input_tokens: event.input_tokens,
|
|
149
|
+
output_tokens: event.output_tokens,
|
|
150
|
+
cost: event.cost
|
|
151
|
+
}
|
|
152
|
+
when Events::ToolExecutionStart
|
|
153
|
+
{ name: event.name, id: event.id, args: safe_args(event.arguments) }
|
|
154
|
+
when Events::ToolExecutionEnd
|
|
155
|
+
{
|
|
156
|
+
name: event.name, id: event.id,
|
|
157
|
+
duration_ms: event.duration_ms,
|
|
158
|
+
is_error: event.is_error,
|
|
159
|
+
result: event.result&.to_s&.to_s[0, 500]
|
|
160
|
+
}
|
|
161
|
+
when Events::Error
|
|
162
|
+
{ message: event.error, recoverable: event.recoverable }
|
|
163
|
+
when Events::MaxTurnsExceeded
|
|
164
|
+
{ max_turns: event.max_turns }
|
|
165
|
+
when Events::LoopDetected
|
|
166
|
+
{ tool_name: event.tool_name, repeated_count: event.repeated_count }
|
|
167
|
+
when Events::CompactionEnd
|
|
168
|
+
{ tokens_before: event.tokens_before, tokens_after: event.tokens_after }
|
|
169
|
+
when Events::EvaluationBlocked
|
|
170
|
+
{ feedback: event.feedback, scores: event.scores }
|
|
171
|
+
else
|
|
172
|
+
{}
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def safe_args(args)
|
|
177
|
+
return {} unless args.is_a?(Hash)
|
|
178
|
+
|
|
179
|
+
safe = args.dup
|
|
180
|
+
%w[password secret token api_key key auth_token access_token sql command].each do |sensitive|
|
|
181
|
+
safe[sensitive] = "[REDACTED]" if safe.key?(sensitive)
|
|
182
|
+
safe[sensitive.to_sym] = "[REDACTED]" if safe.key?(sensitive.to_sym)
|
|
183
|
+
end
|
|
184
|
+
safe
|
|
38
185
|
end
|
|
39
186
|
end
|
|
40
187
|
end
|
data/lib/ask/agent/loop.rb
CHANGED
|
@@ -32,10 +32,13 @@ module Ask
|
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
if chunk.tool_call?
|
|
35
|
-
chunk.tool_calls
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
35
|
+
calls = chunk.tool_calls
|
|
36
|
+
if calls.respond_to?(:each)
|
|
37
|
+
calls.each do |id, tc|
|
|
38
|
+
event_emitter.emit(Events::ToolCallDelta.new(
|
|
39
|
+
name: tc.name, arguments: tc.arguments, id: tc.id
|
|
40
|
+
))
|
|
41
|
+
end
|
|
39
42
|
end
|
|
40
43
|
end
|
|
41
44
|
end
|
data/lib/ask/agent/session.rb
CHANGED
|
@@ -21,7 +21,7 @@ module Ask
|
|
|
21
21
|
compactor: nil, hooks: {}, state: nil, persistence: nil,
|
|
22
22
|
id: nil, system_prompt: nil, parallel_tools: true,
|
|
23
23
|
reflector: nil, telemetry: true, meta_agent: nil,
|
|
24
|
-
agent_dir: nil, evaluator: nil, **chat_options)
|
|
24
|
+
agent_dir: nil, evaluator: nil, audit_log: nil, **chat_options)
|
|
25
25
|
@id = id || SecureRandom.uuid
|
|
26
26
|
@agent_dir = agent_dir
|
|
27
27
|
@max_turns = max_turns
|
|
@@ -47,6 +47,7 @@ module Ask
|
|
|
47
47
|
@tool_executor = ToolExecutor.new(max_retries: max_tool_retries, parallel: parallel_tools)
|
|
48
48
|
@compactor = compactor ? build_compactor(compactor) : nil
|
|
49
49
|
@hooks = Hooks.new(hooks)
|
|
50
|
+
@audit_log = build_audit_log(audit_log)
|
|
50
51
|
|
|
51
52
|
@system_context = build_system_context(system_prompt)
|
|
52
53
|
apply_system_context
|
|
@@ -229,6 +230,10 @@ module Ask
|
|
|
229
230
|
try_auto_meta_agent
|
|
230
231
|
end
|
|
231
232
|
|
|
233
|
+
# Capture messages before emitting SessionEnd so event handlers
|
|
234
|
+
# can access agent.messages during the callback
|
|
235
|
+
@messages = @chat.messages.dup
|
|
236
|
+
|
|
232
237
|
emit(Events::SessionEnd.new(
|
|
233
238
|
result: response,
|
|
234
239
|
turn_count: @turn_count,
|
|
@@ -237,7 +242,6 @@ module Ask
|
|
|
237
242
|
output_tokens: @total_output_tokens,
|
|
238
243
|
cost: @total_cost
|
|
239
244
|
))
|
|
240
|
-
@messages = @chat.messages.dup
|
|
241
245
|
|
|
242
246
|
response
|
|
243
247
|
end
|
|
@@ -331,6 +335,12 @@ module Ask
|
|
|
331
335
|
|
|
332
336
|
private
|
|
333
337
|
|
|
338
|
+
def build_audit_log(config)
|
|
339
|
+
config ||= Ask::Agent.configuration.audit_log
|
|
340
|
+
return nil unless config
|
|
341
|
+
Ask::Agent::Extensions::AuditLog.new(self, adapter: config)
|
|
342
|
+
end
|
|
343
|
+
|
|
334
344
|
def build_chat(model, system_prompt, tools, **chat_options)
|
|
335
345
|
if model.respond_to?(:ask)
|
|
336
346
|
model
|
data/lib/ask/agent/version.rb
CHANGED
data/lib/ask/agent.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: ask-agent
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.23.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Kaka Ruto
|
|
@@ -174,6 +174,7 @@ files:
|
|
|
174
174
|
- lib/ask/agent/evaluator.rb
|
|
175
175
|
- lib/ask/agent/events.rb
|
|
176
176
|
- lib/ask/agent/extensions/audit_log.rb
|
|
177
|
+
- lib/ask/agent/extensions/audit_log/active_record_writer.rb
|
|
177
178
|
- lib/ask/agent/extensions/permissions.rb
|
|
178
179
|
- lib/ask/agent/extensions/rate_limiter.rb
|
|
179
180
|
- lib/ask/agent/hooks.rb
|