insika 0.2.0 → 0.7.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 +325 -17
- data/README.md +52 -11
- data/bin/insika +895 -8
- data/bin/insika-router +87 -0
- data/docs/AGENTS.md +229 -163
- data/docs/API.md +56 -0
- data/docs/ARCHITECTURE.md +3 -2
- data/docs/ARTIFACTS.md +95 -0
- data/docs/BENCHMARK.md +2 -2
- data/docs/CHANNELS.md +111 -21
- data/docs/CONTEXT.md +132 -19
- data/docs/DEMO.md +80 -0
- data/docs/DEPLOY.md +75 -4
- data/docs/EMBEDDING.md +1 -1
- data/docs/EVALS.md +146 -2
- data/docs/FACTS.md +135 -0
- data/docs/HARVEST.md +116 -0
- data/docs/KNOWLEDGE.md +290 -0
- data/docs/LOADTEST.md +16 -10
- data/docs/MEDIA.md +128 -0
- data/docs/OBSERVABILITY.md +65 -6
- data/docs/OUTCOMES.md +137 -0
- data/docs/PLUGINS.md +51 -6
- data/docs/POLICY.md +216 -0
- data/docs/REFINEMENT.md +17 -12
- data/docs/RELEASING.md +27 -0
- data/docs/ROUTER.md +213 -0
- data/docs/RUNNING-LOCAL.md +6 -6
- data/docs/SCHEDULING.md +121 -0
- data/docs/SECURITY.md +79 -2
- data/docs/SKILLS.md +13 -4
- data/docs/SOAK.md +127 -0
- data/docs/TEMPLATES.md +134 -0
- data/docs/TOOLS.md +220 -27
- data/docs/WHY.md +1 -1
- data/docs/WORKFLOWS.md +2 -2
- data/docs/_includes/head_custom.html +5 -0
- data/docs/_includes/title.html +13 -0
- data/docs/_sass/color_schemes/insika.scss +32 -0
- data/docs/_sass/custom/custom.scss +199 -0
- data/docs/_sass/custom/setup.scss +26 -0
- data/docs/assets/img/favicon.svg +7 -0
- data/docs/assets/img/insika-mark.svg +7 -0
- data/docs/core-concepts.md +21 -0
- data/docs/domain.md +115 -0
- data/docs/improve.md +20 -0
- data/docs/index.md +9 -6
- data/docs/integrate.md +20 -0
- data/docs/operate.md +13 -6
- data/docs/prompts/ADD-TOOL.md +118 -0
- data/docs/prompts/DIAGNOSE-TURN.md +65 -0
- data/docs/prompts/GO-LIVE.md +138 -0
- data/docs/prompts/RUN-EXAMPLES.md +70 -0
- data/docs/reference.md +19 -0
- data/docs/ship.md +10 -2
- data/docs/start-here.md +18 -0
- data/lib/insika/agent_profile.rb +202 -10
- data/lib/insika/artifact_signing.rb +82 -0
- data/lib/insika/artifact_store.rb +160 -0
- data/lib/insika/balloon_splitter.rb +102 -0
- data/lib/insika/budget_ledger.rb +34 -3
- data/lib/insika/cache_series_store.rb +49 -0
- data/lib/insika/channel_delivery.rb +119 -14
- data/lib/insika/channels/relay.rb +77 -3
- data/lib/insika/channels/web.rb +2 -2
- data/lib/insika/chat_builder.rb +105 -2
- data/lib/insika/checkpoint_store.rb +16 -0
- data/lib/insika/commands/agent_payload.rb +3 -3
- data/lib/insika/commands/backfill_knowledge.rb +145 -0
- data/lib/insika/commands/cancel_followup.rb +49 -0
- data/lib/insika/commands/delete_artifact.rb +35 -0
- data/lib/insika/commands/delete_concept.rb +34 -0
- data/lib/insika/commands/delete_mcp.rb +6 -2
- data/lib/insika/commands/delete_tenant_data.rb +107 -0
- data/lib/insika/commands/export_customer_memory.rb +48 -0
- data/lib/insika/commands/forget_customer.rb +117 -0
- data/lib/insika/commands/freeze_funnel_baseline.rb +113 -0
- data/lib/insika/commands/gate_harvest.rb +138 -0
- data/lib/insika/commands/gate_refinement.rb +1 -1
- data/lib/insika/commands/judge_shadow_pairs.rb +124 -0
- data/lib/insika/commands/memory_forget_fact.rb +20 -4
- data/lib/insika/commands/memory_put_fact.rb +23 -4
- data/lib/insika/commands/promote_harvest.rb +130 -0
- data/lib/insika/commands/record_outcome.rb +46 -0
- data/lib/insika/commands/record_shadow_reply.rb +68 -0
- data/lib/insika/commands/refresh_mcp_tools.rb +47 -0
- data/lib/insika/commands/reject_harvest.rb +38 -0
- data/lib/insika/commands/resolve_proposal.rb +108 -0
- data/lib/insika/commands/restore_concept.rb +34 -0
- data/lib/insika/commands/revoke_contact.rb +49 -0
- data/lib/insika/commands/rollback_harvest.rb +86 -0
- data/lib/insika/commands/run_distillation.rb +186 -0
- data/lib/insika/commands/run_harvest.rb +393 -0
- data/lib/insika/commands/seed_demo_data.rb +31 -0
- data/lib/insika/commands/send_message.rb +103 -6
- data/lib/insika/commands/session_purge.rb +67 -0
- data/lib/insika/commands/upsert_mcp.rb +6 -3
- data/lib/insika/commands/write_concept.rb +57 -0
- data/lib/insika/contact_store.rb +183 -0
- data/lib/insika/context/builder.rb +21 -3
- data/lib/insika/context/fragment.rb +7 -3
- data/lib/insika/context/priority.rb +5 -0
- data/lib/insika/context/provider.rb +17 -3
- data/lib/insika/context/providers/briefing.rb +96 -0
- data/lib/insika/context/providers/knowledge.rb +108 -0
- data/lib/insika/context/providers/memory.rb +16 -7
- data/lib/insika/context/providers/prompt.rb +57 -23
- data/lib/insika/context/providers/skill.rb +2 -0
- data/lib/insika/context/providers/tool_search.rb +2 -0
- data/lib/insika/context_trace_store.rb +38 -2
- data/lib/insika/cron.rb +189 -0
- data/lib/insika/demo/agent_attrs.rb +43 -0
- data/lib/insika/demo/golden_cases.rb +81 -0
- data/lib/insika/demo/seeder.rb +336 -0
- data/lib/insika/distill.rb +224 -0
- data/lib/insika/distill_engine.rb +169 -0
- data/lib/insika/doctor.rb +889 -11
- data/lib/insika/dsl/definition.rb +3 -2
- data/lib/insika/dsl/runtime.rb +69 -82
- data/lib/insika/dsl/server_boot.rb +92 -1
- data/lib/insika/dsl/system.rb +10 -2
- data/lib/insika/dsl.rb +186 -5
- data/lib/insika/edge_limiter.rb +28 -7
- data/lib/insika/env_schema.rb +25 -3
- data/lib/insika/errors.rb +11 -0
- data/lib/insika/evals/assertions.rb +3 -2
- data/lib/insika/evals/golden.rb +41 -4
- data/lib/insika/evals/judge.rb +47 -2
- data/lib/insika/evals/pairwise.rb +29 -6
- data/lib/insika/evals/persona.rb +98 -0
- data/lib/insika/evals/runner.rb +9 -0
- data/lib/insika/evals/simulator.rb +225 -0
- data/lib/insika/evals/transport.rb +83 -1
- data/lib/insika/event_stream.rb +10 -0
- data/lib/insika/evidence.rb +183 -0
- data/lib/insika/executor.rb +869 -76
- data/lib/insika/followup_engine.rb +207 -0
- data/lib/insika/followup_policy.rb +198 -0
- data/lib/insika/followup_store.rb +306 -0
- data/lib/insika/funnel_declaration.rb +106 -0
- data/lib/insika/funnel_fold.rb +179 -0
- data/lib/insika/funnel_store.rb +163 -0
- data/lib/insika/golden_store.rb +17 -2
- data/lib/insika/grounding/matcher.rb +69 -0
- data/lib/insika/grounding.rb +44 -0
- data/lib/insika/harvest/conversion_gate.rb +159 -0
- data/lib/insika/harvest/criterion.rb +98 -0
- data/lib/insika/harvest/gate.rb +194 -0
- data/lib/insika/harvest/negative_list.rb +199 -0
- data/lib/insika/harvest.rb +241 -0
- data/lib/insika/harvest_engine.rb +193 -0
- data/lib/insika/harvest_store.rb +548 -0
- data/lib/insika/knowledge.rb +680 -0
- data/lib/insika/knowledge_store.rb +140 -0
- data/lib/insika/mcp_client.rb +94 -0
- data/lib/insika/mcp_json.rb +74 -0
- data/lib/insika/mcp_live_tool.rb +43 -0
- data/lib/insika/mcp_store.rb +98 -26
- data/lib/insika/mcp_tool_ingestor.rb +30 -8
- data/lib/insika/mcp_tool_registry.rb +100 -0
- data/lib/insika/media.rb +382 -0
- data/lib/insika/memory_audit_store.rb +85 -0
- data/lib/insika/memory_store.rb +264 -23
- data/lib/insika/message_origin.rb +7 -2
- data/lib/insika/middleware.rb +9 -0
- data/lib/insika/model_visible.rb +87 -0
- data/lib/insika/model_visible_trace_store.rb +66 -0
- data/lib/insika/onboarding.rb +22 -1
- data/lib/insika/outbox_store.rb +42 -4
- data/lib/insika/outcome_store.rb +147 -0
- data/lib/insika/overlay_tool_registry.rb +37 -17
- data/lib/insika/packaging.rb +163 -0
- data/lib/insika/parity/criterion.rb +79 -0
- data/lib/insika/parity/verdict.rb +318 -0
- data/lib/insika/prefix_fingerprint.rb +58 -0
- data/lib/insika/profile_source.rb +29 -1
- data/lib/insika/prompt_catalog.rb +10 -0
- data/lib/insika/proposal_store.rb +271 -0
- data/lib/insika/queue_policy.rb +4 -1
- data/lib/insika/refinement/proposer.rb +1 -1
- data/lib/insika/reliability.rb +32 -6
- data/lib/insika/retention.rb +316 -0
- data/lib/insika/router/app.rb +157 -0
- data/lib/insika/router/backend_pool.rb +98 -0
- data/lib/insika/router/hash_ring.rb +55 -0
- data/lib/insika/router/proxy_body.rb +34 -0
- data/lib/insika/router/session_key.rb +54 -0
- data/lib/insika/router.rb +18 -0
- data/lib/insika/routing.rb +101 -0
- data/lib/insika/safety/config.rb +44 -4
- data/lib/insika/safety/corpus.rb +255 -0
- data/lib/insika/safety/detectors.rb +32 -113
- data/lib/insika/safety/factory.rb +15 -2
- data/lib/insika/safety/grounding_enforcer.rb +59 -0
- data/lib/insika/safety/grounding_validator.rb +49 -0
- data/lib/insika/safety/input_guardrail.rb +1 -1
- data/lib/insika/safety/moderator.rb +1 -1
- data/lib/insika/safety/output_filter.rb +9 -5
- data/lib/insika/safety/output_validator.rb +11 -5
- data/lib/insika/schedule.rb +177 -0
- data/lib/insika/schedule_engine.rb +314 -0
- data/lib/insika/schedule_store.rb +208 -0
- data/lib/insika/schema_guard.rb +35 -0
- data/lib/insika/server/app.rb +247 -17
- data/lib/insika/server/rack_app.rb +21 -1
- data/lib/insika/server/responses.rb +40 -1
- data/lib/insika/session_actor.rb +8 -4
- data/lib/insika/session_store.rb +65 -2
- data/lib/insika/settings_store.rb +10 -0
- data/lib/insika/shadow_pair_store.rb +258 -0
- data/lib/insika/skill_catalog.rb +16 -0
- data/lib/insika/soak/envelope.rb +140 -0
- data/lib/insika/soak/report.rb +392 -0
- data/lib/insika/soak/runner.rb +554 -0
- data/lib/insika/steer_injector.rb +21 -10
- data/lib/insika/store.rb +10 -1
- data/lib/insika/stores/memory.rb +6 -0
- data/lib/insika/stores/sqlite.rb +8 -0
- data/lib/insika/studio/app.rb +1437 -54
- data/lib/insika/studio/assets/dist/application.css +1 -1
- data/lib/insika/studio/assets/dist/application.js +23 -23
- data/lib/insika/studio/assets/dist/favicon.svg +6 -0
- data/lib/insika/studio/forms.rb +308 -10
- data/lib/insika/studio/nav_icons.rb +19 -1
- data/lib/insika/studio/views/_agent_tab_cache.erb +25 -0
- data/lib/insika/studio/views/_agent_tab_config.erb +514 -0
- data/lib/insika/studio/views/_agent_tab_history.erb +24 -0
- data/lib/insika/studio/views/_agent_tab_loops.erb +54 -0
- data/lib/insika/studio/views/_agent_tab_memory.erb +51 -0
- data/lib/insika/studio/views/_agent_tab_outcomes.erb +31 -0
- data/lib/insika/studio/views/_agent_tab_prompts.erb +108 -0
- data/lib/insika/studio/views/_agent_tab_skills.erb +38 -0
- data/lib/insika/studio/views/_agents_master.erb +44 -0
- data/lib/insika/studio/views/_message.erb +49 -32
- data/lib/insika/studio/views/agent_detail.erb +62 -278
- data/lib/insika/studio/views/agents.erb +71 -54
- data/lib/insika/studio/views/approvals.erb +4 -1
- data/lib/insika/studio/views/artifact.erb +23 -0
- data/lib/insika/studio/views/artifacts.erb +59 -0
- data/lib/insika/studio/views/chats.erb +4 -1
- data/lib/insika/studio/views/customer.erb +94 -0
- data/lib/insika/studio/views/customers.erb +32 -0
- data/lib/insika/studio/views/evals.erb +6 -3
- data/lib/insika/studio/views/facts.erb +133 -0
- data/lib/insika/studio/views/followups.erb +125 -0
- data/lib/insika/studio/views/funnel.erb +106 -0
- data/lib/insika/studio/views/harvest.erb +234 -0
- data/lib/insika/studio/views/home.erb +106 -66
- data/lib/insika/studio/views/knowledge.erb +123 -0
- data/lib/insika/studio/views/layout.erb +15 -11
- data/lib/insika/studio/views/mcp.erb +174 -80
- data/lib/insika/studio/views/parity.erb +147 -0
- data/lib/insika/studio/views/playground.erb +7 -1
- data/lib/insika/studio/views/session.erb +233 -124
- data/lib/insika/studio/views/settings.erb +41 -1
- data/lib/insika/studio/views/skills.erb +1 -2
- data/lib/insika/studio/views/system_files.erb +1 -1
- data/lib/insika/studio/views/task.erb +13 -0
- data/lib/insika/studio/views/tasks.erb +4 -1
- data/lib/insika/studio/views/tools.erb +24 -10
- data/lib/insika/task_store.rb +21 -1
- data/lib/insika/templates/browser-agent/README.md +36 -0
- data/lib/insika/templates/browser-agent/agent.rb +49 -0
- data/lib/insika/templates/daily-digest/README.md +38 -0
- data/lib/insika/templates/daily-digest/agent.rb +77 -0
- data/lib/insika/templates/repo-explorer/README.md +36 -0
- data/lib/insika/templates/repo-explorer/agent.rb +45 -0
- data/lib/insika/templates/research-analyst/README.md +26 -0
- data/lib/insika/templates/research-analyst/agent.rb +58 -0
- data/lib/insika/templates/review-panel/README.md +20 -0
- data/lib/insika/templates/review-panel/agent.rb +50 -0
- data/lib/insika/templates/travel-planner/README.md +35 -0
- data/lib/insika/templates/travel-planner/agent.rb +87 -0
- data/lib/insika/templates.rb +112 -0
- data/lib/insika/testing/store_contract.rb +27 -6
- data/lib/insika/tick.rb +47 -11
- data/lib/insika/timezone.rb +45 -0
- data/lib/insika/tool_definition.rb +17 -7
- data/lib/insika/tool_envelope.rb +69 -0
- data/lib/insika/tool_manifest.rb +5 -1
- data/lib/insika/tools/data_defined_tool.rb +10 -0
- data/lib/insika/tools/generate_image.rb +89 -0
- data/lib/insika/tools/load_knowledge.rb +74 -0
- data/lib/insika/tools/run_persona_eval.rb +328 -0
- data/lib/insika/tools/save_artifact.rb +95 -0
- data/lib/insika/tools/schedule_followup.rb +164 -0
- data/lib/insika/tools/tts.rb +47 -0
- data/lib/insika/tools/update_briefing.rb +126 -0
- data/lib/insika/turn_output.rb +1 -1
- data/lib/insika/turn_state.rb +49 -1
- data/lib/insika/turn_timing.rb +22 -2
- data/lib/insika/version.rb +1 -1
- data/lib/insika/vitals.rb +84 -0
- data/lib/insika/wiring/graph.rb +472 -13
- data/lib/insika/wiring/graph_chat.rb +102 -0
- data/lib/insika.rb +149 -3
- metadata +181 -5
- data/docs/build.md +0 -14
- data/docs/understand.md +0 -10
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ruby_llm"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Insika
|
|
7
|
+
module Tools
|
|
8
|
+
# Level 2 of knowledge's progressive disclosure: loads a learned
|
|
9
|
+
# concept's full body on demand, by the name the `<knowledge>` block
|
|
10
|
+
# listed. Same shape as `LoadSkill` — a system default (outside the
|
|
11
|
+
# allowlist), wired only when the profile opted in
|
|
12
|
+
# (`knowledge.retrieve`), never through `tools_allow`.
|
|
13
|
+
#
|
|
14
|
+
# `require "ruby_llm"` stays in THIS file (it inherits from
|
|
15
|
+
# RubyLLM::Tool), so it does NOT enter lib/insika.rb — the Executor
|
|
16
|
+
# loads it lazily inside create_chat, same as load_skill.
|
|
17
|
+
class LoadKnowledge < RubyLLM::Tool
|
|
18
|
+
description "Loads the complete content of a learned concept by name"
|
|
19
|
+
param :name, desc: "Exact concept name, as listed in <knowledge>"
|
|
20
|
+
|
|
21
|
+
# RubyLLM::Tool#name derives from self.class.name — for a nested class it
|
|
22
|
+
# produces "insika--tools--load_knowledge", not "load_knowledge" (which
|
|
23
|
+
# wire_callbacks/:knowledge_retrieved assume). Explicit override, same
|
|
24
|
+
# reason LoadSkill has one.
|
|
25
|
+
def name = "load_knowledge"
|
|
26
|
+
|
|
27
|
+
# trace_recorder/state are OPTIONAL (nil = no trace, parity): this tool
|
|
28
|
+
# is deliberately NOT enveloped (ToolAssembly#wrap_tools), so without
|
|
29
|
+
# recording HERE the call is missing from the Studio's trace — same
|
|
30
|
+
# shape as LoadSkill.
|
|
31
|
+
def initialize(store, agent_id, tenant: nil, trace_recorder: nil, state: nil)
|
|
32
|
+
@store = store
|
|
33
|
+
@agent_id = agent_id
|
|
34
|
+
@tenant = tenant
|
|
35
|
+
@trace_recorder = trace_recorder
|
|
36
|
+
@state = state
|
|
37
|
+
super()
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def execute(name:)
|
|
41
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
42
|
+
result = load(name)
|
|
43
|
+
trace(name, result, started)
|
|
44
|
+
result
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def load(name)
|
|
50
|
+
content = @store.get(@agent_id, name.to_s, tenant: @tenant)
|
|
51
|
+
return { error: "concept '#{name}' not found" } unless content
|
|
52
|
+
|
|
53
|
+
content
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Mirrors LoadSkill#trace (same entry shape, so the Studio renders it
|
|
57
|
+
# like any other call). NEVER breaks the turn — the trace is
|
|
58
|
+
# observability, not the answer.
|
|
59
|
+
def trace(name, result, started)
|
|
60
|
+
return unless @trace_recorder && @state&.task&.session_id
|
|
61
|
+
|
|
62
|
+
@trace_recorder.record(
|
|
63
|
+
session_id: @state.task.session_id,
|
|
64
|
+
entry: { "turn" => @state.turn, "tool" => "load_knowledge", "call_id" => "",
|
|
65
|
+
"args" => { "name" => name.to_s }, "result" => result,
|
|
66
|
+
"ms" => ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round,
|
|
67
|
+
"at" => Time.now.utc.iso8601 }
|
|
68
|
+
)
|
|
69
|
+
rescue StandardError
|
|
70
|
+
nil
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "securerandom"
|
|
4
|
+
require "ruby_llm"
|
|
5
|
+
require_relative "agent_enum"
|
|
6
|
+
|
|
7
|
+
module Insika
|
|
8
|
+
module Tools
|
|
9
|
+
# `run_persona_eval` — a QA agent's own probe: pick an authored SIMULATED
|
|
10
|
+
# persona case (Evals::GoldenStore) and run it, in-process, against the
|
|
11
|
+
# case's declared target agent — the same Simulator + Judge machinery
|
|
12
|
+
# `insika evals:simulate` drives over HTTP, minus the CLI and the network
|
|
13
|
+
# hop.
|
|
14
|
+
#
|
|
15
|
+
# SAFETY is DERIVED, never a flag: the target's reachable side-effect tools
|
|
16
|
+
# are computed from the live registry (Evals::EvalProfile), the same way
|
|
17
|
+
# the CLI derives them. A read-only target needs no swap:
|
|
18
|
+
# Simulator::Safety's own "side_effect_tools.empty?" branch allows it
|
|
19
|
+
# directly. A target that DOES reach a side-effect tool gets the REAL
|
|
20
|
+
# swap (Evals::EvalProfile.registry — own overlay, wired here):
|
|
21
|
+
# every one of those tools resolves to a Simulator::DryRunTool for the
|
|
22
|
+
# duration of this ONE simulated conversation, run through a THROWAWAY
|
|
23
|
+
# Executor+Bus built fresh per call (`shadow_runtime`) — sharing every
|
|
24
|
+
# OTHER collaborator of the real graph (guardrails, policy, context
|
|
25
|
+
# assembly, skills/prompts, the real session/task/checkpoint stores), so
|
|
26
|
+
# the target is tested as faithfully as `--staging` ever was, minus the
|
|
27
|
+
# one write. Needs the real `graph:` (see `initialize`) — a caller that
|
|
28
|
+
# does not have one (an old-style double) falls back to refusing outright.
|
|
29
|
+
#
|
|
30
|
+
# BUDGET: the persona model + judge model calls are the cost of running the
|
|
31
|
+
# eval, charged to the CALLING agent's turn (never the target's — the
|
|
32
|
+
# target's own turns are billed normally, through the ordinary edge
|
|
33
|
+
# limiter, exactly as if a customer had sent those messages). A hard cap on
|
|
34
|
+
# the calling agent skips the run — visibly, in the tool result — before a
|
|
35
|
+
# cent is spent.
|
|
36
|
+
#
|
|
37
|
+
# TENANT ISOLATION: a persona case belongs to a tenant (Golden#tenant,
|
|
38
|
+
# "platform" by default); this tool only ever lists/runs cases in the
|
|
39
|
+
# CALLING agent's own tenant (calling_tenant, read off turn_context, never
|
|
40
|
+
# the model). One QA agent per store (the C3.2 plan) is what makes this
|
|
41
|
+
# meaningful -- without it, "qa-store-a" could enumerate and run
|
|
42
|
+
# "qa-store-b"'s persona and read its `knows` in the transcript.
|
|
43
|
+
class RunPersonaEval < RubyLLM::Tool
|
|
44
|
+
description "Run an authored simulated-customer persona case, in-process, against " \
|
|
45
|
+
"its target agent and score the whole conversation with the configured " \
|
|
46
|
+
"judge panel. Refuses if the target exposes a tool that could write for " \
|
|
47
|
+
"real."
|
|
48
|
+
param :case_id, desc: "Id of the persona case to run"
|
|
49
|
+
|
|
50
|
+
def name = "run_persona_eval"
|
|
51
|
+
|
|
52
|
+
# golden_store: Evals::GoldenStore — where authored persona cases live,
|
|
53
|
+
# scoped to the CALLING agent's own tenant (never the
|
|
54
|
+
# model's — see `calling_tenant`). A case authored for
|
|
55
|
+
# another tenant is invisible here, not merely undocumented.
|
|
56
|
+
# profiles: ProfileSource — resolves both the target agent (the
|
|
57
|
+
# case's `agent:`) and the CALLING agent (turn_context, for
|
|
58
|
+
# the budget check).
|
|
59
|
+
# tool_registry: the deployment's EFFECTIVE registry — what
|
|
60
|
+
# Evals::EvalProfile derives the target's side-effect tools
|
|
61
|
+
# from (the same registry a real turn resolves tools on).
|
|
62
|
+
# runtime: anything answering `#chat(message, session_id:, agent:)`
|
|
63
|
+
# (raises Insika::Error on failure) — Evals::GraphTransport's
|
|
64
|
+
# contract. Used AS-IS for a read-only target; a target
|
|
65
|
+
# with a reachable side-effect tool needs `graph:` instead
|
|
66
|
+
# (this alone cannot swap anything).
|
|
67
|
+
# graph: the real Wiring::Graph::Result — ONLY consulted to build
|
|
68
|
+
# the throwaway swapped-registry Executor+Bus
|
|
69
|
+
# (`shadow_runtime`) when the target has a reachable
|
|
70
|
+
# side-effect tool. nil (an old-style double) = that case
|
|
71
|
+
# refuses outright, same as before this existed.
|
|
72
|
+
# settings_store: where the platform `utility_model` (persona) and the
|
|
73
|
+
# judge panel (`evals.judges`) are configured.
|
|
74
|
+
# budget_ledger: WS2 counters — read before the run (skip on a hard cap),
|
|
75
|
+
# written after (persona + judge spend only).
|
|
76
|
+
# llm: this graph's own RubyLLM::Context, if it has one (a
|
|
77
|
+
# DSL-built graph's own credentials) — the persona/judge
|
|
78
|
+
# calls' preferred source, ahead of `runtime.llm`
|
|
79
|
+
# (kept for the existing double-based specs) and the
|
|
80
|
+
# process-wide RubyLLM constant.
|
|
81
|
+
def initialize(golden_store:, profiles:, tool_registry:, runtime:, settings_store:,
|
|
82
|
+
graph: nil, budget_ledger: nil, event_stream: nil, llm: nil)
|
|
83
|
+
@golden_store = golden_store
|
|
84
|
+
@profiles = profiles
|
|
85
|
+
@tool_registry = tool_registry
|
|
86
|
+
@runtime = runtime
|
|
87
|
+
@graph = graph
|
|
88
|
+
@settings_store = settings_store
|
|
89
|
+
@budget_ledger = budget_ledger
|
|
90
|
+
@event_stream = event_stream
|
|
91
|
+
@llm = llm
|
|
92
|
+
super()
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
# Per-turn bindings, deposited by the Executor (ToolAssembly's
|
|
96
|
+
# `turn_context=` seam — same as save_artifact/data-tools). Only the
|
|
97
|
+
# CALLING agent's id + declared tenant are used here (the budget check);
|
|
98
|
+
# never a tenant/agent the model types.
|
|
99
|
+
attr_reader :turn_context
|
|
100
|
+
|
|
101
|
+
def turn_context=(ctx)
|
|
102
|
+
@turn_context = (ctx || {}).each_with_object({}) { |(k, v), acc| acc[k.to_sym] = v }
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# The runnable case ids, named so the model cannot guess one that does
|
|
106
|
+
# not exist (the Subagent tool's lesson — see AgentEnum).
|
|
107
|
+
def description
|
|
108
|
+
ids = case_ids
|
|
109
|
+
return super if ids.empty?
|
|
110
|
+
|
|
111
|
+
"#{super} Cases you may run: #{ids.join(', ')}."
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def params_schema
|
|
115
|
+
Insika::Tools::AgentEnum.inject(super, case_ids, path: %i[case_id])
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def execute(case_id:)
|
|
119
|
+
golden = @golden_store.find(case_id.to_s)
|
|
120
|
+
# The SAME error, whether the case does not exist or exists under another
|
|
121
|
+
# tenant — a QA agent must not be able to tell the two apart (that
|
|
122
|
+
# distinction is itself a leak: "case exists, just not yours").
|
|
123
|
+
unless golden&.simulated? && golden.tenant == calling_tenant
|
|
124
|
+
return { error: "unknown or invalid persona case '#{case_id}'" }
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
target = @profiles[golden.agent]
|
|
128
|
+
return { error: "persona case '#{case_id}' targets unknown agent '#{golden.agent}'" } unless target
|
|
129
|
+
|
|
130
|
+
derived = Insika::Evals::EvalProfile.side_effect_tools(target, @tool_registry)
|
|
131
|
+
return refuse_side_effects(golden.agent, derived) if !derived.empty? && @graph.nil?
|
|
132
|
+
|
|
133
|
+
skip = budget_skip
|
|
134
|
+
return skip if skip
|
|
135
|
+
|
|
136
|
+
meter = []
|
|
137
|
+
judge = build_judge(meter)
|
|
138
|
+
return { error: "no judge configured for this case — Studio -> Settings -> Evals" } unless judge
|
|
139
|
+
|
|
140
|
+
persona_ask = build_persona_ask(meter)
|
|
141
|
+
return persona_ask if persona_ask.is_a?(Hash) # {error:}
|
|
142
|
+
|
|
143
|
+
run_and_score(golden, derived, persona_ask, judge, meter)
|
|
144
|
+
rescue Insika::Evals::Simulator::UnsafeTarget => e
|
|
145
|
+
{ error: e.message }
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
private
|
|
149
|
+
|
|
150
|
+
def refuse_side_effects(agent, derived)
|
|
151
|
+
{ error: "target agent '#{agent}' exposes side-effect tool(s) (#{derived.join(', ')}) — " \
|
|
152
|
+
"run_persona_eval needs the real graph (no swap available for this caller)" }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def run_and_score(golden, derived, persona_ask, judge, meter)
|
|
156
|
+
runtime = derived.empty? ? @runtime : shadow_runtime(derived)
|
|
157
|
+
transport = Insika::Evals::GraphTransport.new(runtime: runtime, event_stream: @event_stream)
|
|
158
|
+
safety = Insika::Evals::Simulator::Safety.new(
|
|
159
|
+
side_effect_tools: derived, eval_profile: !derived.empty?, swapped_tools: derived
|
|
160
|
+
)
|
|
161
|
+
simulator = Insika::Evals::Simulator.new(transport: transport, ask: persona_ask, safety: safety)
|
|
162
|
+
|
|
163
|
+
conv = "eval-#{golden.id}-#{SecureRandom.hex(4)}" # a fresh session every run (never reused)
|
|
164
|
+
run = simulator.run(persona: golden.persona, agent: golden.agent, conv: conv)
|
|
165
|
+
verdict = judge.score_conversation(
|
|
166
|
+
rubric: golden.rubric, transcript: run.transcript, policy: golden.policy,
|
|
167
|
+
min_score: golden.min_score || Insika::Evals::Judge::DEFAULT_MIN_SCORE
|
|
168
|
+
)
|
|
169
|
+
account_spend(meter)
|
|
170
|
+
|
|
171
|
+
{ case: golden.id, agent: golden.agent, stop: run.stop.to_s, turns: run.turns,
|
|
172
|
+
score: verdict&.score, pass: verdict&.pass, reason: verdict&.reason,
|
|
173
|
+
transcript: run.transcript.map { |m| { role: m[:role], text: m[:text] } } }
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# A THROWAWAY Executor+Bus, built fresh for THIS call, over the SAME
|
|
177
|
+
# session/task/checkpoint stores, guardrails, policy engine, context
|
|
178
|
+
# assembly, skills/prompts and capabilities as the real graph — the
|
|
179
|
+
# ONLY thing different is the tool registry, which resolves every name
|
|
180
|
+
# in `derived` to a Simulator::DryRunTool (Evals::EvalProfile.registry)
|
|
181
|
+
# and everything else exactly as the real one does. Never persisted
|
|
182
|
+
# anywhere NEW: the simulated turn's session/task rows land in the
|
|
183
|
+
# SAME stores a `--staging` run's already do (RunPersonaEval's own
|
|
184
|
+
# never-reused `conv` id is what keeps it from colliding with a real
|
|
185
|
+
# customer session, same as before this existed).
|
|
186
|
+
def shadow_runtime(derived)
|
|
187
|
+
overlay = Insika::Evals::EvalProfile.registry(@tool_registry, side_effect_tools: derived)
|
|
188
|
+
executor = Insika::Executor.new(
|
|
189
|
+
context_builder: @graph.context_builder, policy_engine: @graph.policy_engine,
|
|
190
|
+
middleware: @graph.middleware, hooks: @graph.hooks,
|
|
191
|
+
tool_registry: overlay, skill_catalog: @graph.skill_catalog, profiles: @graph.profiles,
|
|
192
|
+
session_store: @graph.session_store, task_store: @graph.task_store,
|
|
193
|
+
checkpoint_store: @graph.checkpoint_store, event_stream: @graph.event_stream,
|
|
194
|
+
workflow_registry: @graph.workflow_registry, pending_action_store: @graph.pending_action_store,
|
|
195
|
+
capability_registry: @graph.capability_registry,
|
|
196
|
+
tool_catalog: Insika::ToolCatalog.new(tool_registry: overlay),
|
|
197
|
+
memory_store: @graph.memory_store, settings_store: @settings_store,
|
|
198
|
+
delegation_store: @graph.delegation_store, channel_delivery: @graph.channel_delivery,
|
|
199
|
+
llm: llm_context
|
|
200
|
+
)
|
|
201
|
+
bus = Insika::CommandBus.new
|
|
202
|
+
bus.register(:send_message, Insika::Commands::SendMessage.new(
|
|
203
|
+
profiles: @graph.profiles, session_store: @graph.session_store,
|
|
204
|
+
task_store: @graph.task_store, executor: executor,
|
|
205
|
+
inbound_log: @graph.inbound_log, contact_store: @graph.contact_store,
|
|
206
|
+
followup_store: @graph.followup_store, store: @graph.backend
|
|
207
|
+
))
|
|
208
|
+
shadow = @graph.dup
|
|
209
|
+
shadow.bus = bus
|
|
210
|
+
shadow.executor = executor
|
|
211
|
+
Insika::Wiring::GraphChat.new(graph: shadow)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# -> [String] every case this tool can run: a valid, SIMULATED (persona:)
|
|
215
|
+
# golden belonging to the CALLING agent's own tenant. A scripted (turns:)
|
|
216
|
+
# case has nothing to simulate — the CLI skips it too — and a case outside
|
|
217
|
+
# `calling_tenant` is not merely un-runnable, it never appears at all (the
|
|
218
|
+
# model cannot even learn another tenant's case ids from the enum).
|
|
219
|
+
def case_ids
|
|
220
|
+
@golden_store.for_tenant(calling_tenant).select(&:simulated?).map(&:id)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
# The CALLING agent's tenant — never the model's, never the target's. The
|
|
224
|
+
# same "declared command tenant, 'platform' is the single-tenant default"
|
|
225
|
+
# rule `save_artifact`'s `binding_tenant` uses. Every persona-case
|
|
226
|
+
# read/list/run in this tool is scoped to it.
|
|
227
|
+
def calling_tenant
|
|
228
|
+
Insika::Coercion.presence(turn_context&.dig(:command_tenant)) || "platform"
|
|
229
|
+
end
|
|
230
|
+
|
|
231
|
+
# settings["evals"] -> the configured judge panel, scoped to this
|
|
232
|
+
# graph's own RubyLLM credentials (never the process-wide default —
|
|
233
|
+
# a graph's judge spends the graph's own key) and METERED (every judge
|
|
234
|
+
# call's usage lands in `meter`). nil = nobody configured (the CLI's own
|
|
235
|
+
# rule: never guess a judge to spend money on).
|
|
236
|
+
def build_judge(meter)
|
|
237
|
+
judge, = Insika::Evals::JudgePanel.build(@settings_store.get["evals"] || {}, chat_factory: metered_factory(meter))
|
|
238
|
+
judge
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# The persona model — the platform `utility_model` (never a caller-chosen
|
|
242
|
+
# model: the model does not get to pick what it costs to test itself),
|
|
243
|
+
# metered the same way. -> callable | {error:}.
|
|
244
|
+
def build_persona_ask(meter)
|
|
245
|
+
model = Insika::Coercion.presence(@settings_store.get["utility_model"])
|
|
246
|
+
return { error: "no persona model — set the platform utility_model (Studio -> Settings)" } if model.nil?
|
|
247
|
+
|
|
248
|
+
metered_factory(meter).call(model, nil)
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
# The explicit `llm:` wins (the real wiring passes it — see
|
|
252
|
+
# `Wiring::Graph.register_persona_eval_tool`); `@runtime.llm` is the
|
|
253
|
+
# fallback the existing double-based specs rely on (a fake `runtime`
|
|
254
|
+
# answering `#llm` with no `graph:` at all). nil = the process-wide
|
|
255
|
+
# RubyLLM constant.
|
|
256
|
+
def llm_context
|
|
257
|
+
@llm || (@runtime.respond_to?(:llm) ? @runtime.llm : nil)
|
|
258
|
+
end
|
|
259
|
+
|
|
260
|
+
# ->(model, provider) { ask } — a RubyLLM chat (this graph's own
|
|
261
|
+
# credentials, temperature 0) whose every `#ask` lands its Message in
|
|
262
|
+
# `sink` before handing back the text. Same shape as
|
|
263
|
+
# JudgePanel.ruby_llm_ask, except it does not throw the usage away.
|
|
264
|
+
# `assume_model_exists` is passed ONLY alongside an explicit `provider`
|
|
265
|
+
# (the judge panel's own shape — `settings["evals"]["judges"]` always
|
|
266
|
+
# carries one) — RubyLLM raises ArgumentError on `assume_model_exists:
|
|
267
|
+
# true` with no provider. The persona's own model (the platform
|
|
268
|
+
# `utility_model`, a bare ref with no companion provider setting
|
|
269
|
+
# anywhere in the schema) needs the OPPOSITE: no provider, no
|
|
270
|
+
# assume_model_exists, so RubyLLM resolves it from its own registry —
|
|
271
|
+
# exactly how a bare model ref already works everywhere else this
|
|
272
|
+
# codebase reaches for `utility_model`.
|
|
273
|
+
def metered_factory(sink)
|
|
274
|
+
lambda do |model, provider|
|
|
275
|
+
kwargs = { model: model }
|
|
276
|
+
if provider
|
|
277
|
+
kwargs[:provider] = provider
|
|
278
|
+
kwargs[:assume_model_exists] = true
|
|
279
|
+
end
|
|
280
|
+
chat = (llm_context || RubyLLM).chat(**kwargs).with_temperature(0)
|
|
281
|
+
lambda do |prompt|
|
|
282
|
+
msg = chat.ask(prompt)
|
|
283
|
+
sink << msg
|
|
284
|
+
msg.content
|
|
285
|
+
end
|
|
286
|
+
end
|
|
287
|
+
end
|
|
288
|
+
|
|
289
|
+
# -> truthy (a visible skip result) when the CALLING agent's own hard
|
|
290
|
+
# budget is already at/over a window cap; nil otherwise. Mirrors
|
|
291
|
+
# ScheduleEngine#budget_exhausted? — a HARD cap skips rather than spends
|
|
292
|
+
# (a soft one just runs; the ledger's own alert already warns).
|
|
293
|
+
def budget_skip
|
|
294
|
+
return nil unless @budget_ledger
|
|
295
|
+
|
|
296
|
+
agent_id = turn_context&.dig(:agent_id)
|
|
297
|
+
profile = agent_id && @profiles[agent_id]
|
|
298
|
+
budget = profile&.respond_to?(:budget) ? profile.budget : nil
|
|
299
|
+
return nil if budget.nil? || budget["soft"] == true
|
|
300
|
+
|
|
301
|
+
tenant = turn_context&.dig(:command_tenant)
|
|
302
|
+
window = %w[daily monthly].find do |w|
|
|
303
|
+
cap = budget[w].to_i
|
|
304
|
+
cap.positive? && @budget_ledger.current(tenant: tenant, agent: agent_id)[w.to_sym] >= cap
|
|
305
|
+
end
|
|
306
|
+
{ skipped: true, reason: "budget", window: window } if window
|
|
307
|
+
end
|
|
308
|
+
|
|
309
|
+
# The turn's real billed spend for the persona + judge calls (the A4
|
|
310
|
+
# rule: total + cached + cache_creation), charged to the CALLING agent —
|
|
311
|
+
# never the target, whose own turns are billed normally by the edge
|
|
312
|
+
# limiter, exactly like a real customer's would be.
|
|
313
|
+
def account_spend(meter)
|
|
314
|
+
return if @budget_ledger.nil? || meter.empty?
|
|
315
|
+
|
|
316
|
+
tokens = meter.sum do |m|
|
|
317
|
+
m.input_tokens.to_i + m.output_tokens.to_i +
|
|
318
|
+
(m.respond_to?(:cached_tokens) ? m.cached_tokens.to_i : 0) +
|
|
319
|
+
(m.respond_to?(:cache_creation_tokens) ? m.cache_creation_tokens.to_i : 0)
|
|
320
|
+
end
|
|
321
|
+
return if tokens.zero?
|
|
322
|
+
|
|
323
|
+
@budget_ledger.add(tenant: turn_context&.dig(:command_tenant),
|
|
324
|
+
agent: turn_context&.dig(:agent_id), by: tokens)
|
|
325
|
+
end
|
|
326
|
+
end
|
|
327
|
+
end
|
|
328
|
+
end
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ruby_llm"
|
|
4
|
+
|
|
5
|
+
module Insika
|
|
6
|
+
module Tools
|
|
7
|
+
# `save_artifact` — the agent delivers a page (a report, a digest) to a
|
|
8
|
+
# destination that survives the channel: title + content in, a URL out,
|
|
9
|
+
# which it can include in a channel message ("today's report: <url>").
|
|
10
|
+
#
|
|
11
|
+
# A REGISTRY tool (not a ChatBuilder system tool): the per-agent
|
|
12
|
+
# `tools_allow` is the switch — registered `optional: true`, so an agent
|
|
13
|
+
# that did not name it cannot call it, and nothing global enables it.
|
|
14
|
+
# The bindings (tenant, agent, task) are the tool INSTANCE's, deposited
|
|
15
|
+
# by the Executor into `turn_context` like a data-tool's — never
|
|
16
|
+
# parameters the model types.
|
|
17
|
+
class SaveArtifact < RubyLLM::Tool
|
|
18
|
+
description "Save an HTML (or markdown / SVG) page — a report, a digest, " \
|
|
19
|
+
"a summary — and get back a URL to share. Use when the answer " \
|
|
20
|
+
"is a document the channel message cannot carry."
|
|
21
|
+
param :title, desc: "Short human-readable title (<= 200 chars)"
|
|
22
|
+
param :content, desc: "The page content: HTML with inline SVG for charts, " \
|
|
23
|
+
"markdown, or SVG. No scripts, no external resources."
|
|
24
|
+
param :mime, desc: "text/html (default), text/markdown, or image/svg+xml",
|
|
25
|
+
required: false
|
|
26
|
+
|
|
27
|
+
def name = "save_artifact"
|
|
28
|
+
|
|
29
|
+
def initialize(artifact_store:, base_url: nil, signing_key: nil, signing_ttl: nil,
|
|
30
|
+
max_bytes: nil, event_stream: nil)
|
|
31
|
+
@artifact_store = artifact_store
|
|
32
|
+
@base_url = base_url.to_s.sub(%r{/\z}, "")
|
|
33
|
+
@signing_key = signing_key
|
|
34
|
+
@signing_ttl = signing_ttl
|
|
35
|
+
@max_bytes = max_bytes
|
|
36
|
+
@event_stream = event_stream
|
|
37
|
+
super()
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Per-turn bindings, deposited by the Executor (ToolAssembly's
|
|
41
|
+
# `turn_context=` seam — the same one data-tools use). From the TURN,
|
|
42
|
+
# never from the model (the tenant-lesson: an id the model types is
|
|
43
|
+
# inventable, and the turn looks right anyway).
|
|
44
|
+
attr_reader :turn_context
|
|
45
|
+
|
|
46
|
+
def turn_context=(ctx)
|
|
47
|
+
@turn_context = (ctx || {}).each_with_object({}) { |(k, v), acc| acc[k.to_sym] = v }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def execute(title:, content:, mime: "text/html")
|
|
51
|
+
# the deployment's size cap rides the tool instance (INSIKA_ARTIFACT_
|
|
52
|
+
# MAX_BYTES at wiring); absent -> the store's default.
|
|
53
|
+
attrs = { tenant: binding_tenant, agent: @turn_context&.dig(:agent_id),
|
|
54
|
+
task_id: @turn_context&.dig(:task_id), title: title, mime: mime,
|
|
55
|
+
content: content }
|
|
56
|
+
attrs[:max_bytes] = @max_bytes if @max_bytes
|
|
57
|
+
record = @artifact_store.create(**attrs)
|
|
58
|
+
# the authenticated Studio URL is the always-on answer; the signed
|
|
59
|
+
# link exists ONLY when a key was configured (it shares OUTSIDE the
|
|
60
|
+
# Studio and expires).
|
|
61
|
+
url = Insika::ArtifactSigning.url_for(id: record.id, base: @base_url)
|
|
62
|
+
result = { id: record.id, url: url }
|
|
63
|
+
signed = Insika::ArtifactSigning.url_for(
|
|
64
|
+
id: record.id, base: @base_url, key: @signing_key, ttl: @signing_ttl
|
|
65
|
+
)
|
|
66
|
+
result[:signed_url] = signed if signed != url
|
|
67
|
+
emit(record)
|
|
68
|
+
result
|
|
69
|
+
rescue Insika::ValidationError => e
|
|
70
|
+
{ error: e.message }
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
private
|
|
74
|
+
|
|
75
|
+
# The tenant is the AGENT's tenant: the declared command tenant, or the
|
|
76
|
+
# deployment's "platform" in single-tenant — never the chat id (a report
|
|
77
|
+
# belongs to the agent, not to whichever chat ran it), never a parameter
|
|
78
|
+
# the model types.
|
|
79
|
+
def binding_tenant
|
|
80
|
+
declared = @turn_context&.dig(:command_tenant).to_s
|
|
81
|
+
declared.empty? ? "platform" : declared
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# :artifact_saved carries ids + title — NEVER the content (a report is
|
|
85
|
+
# customer data that rides the store, not the events).
|
|
86
|
+
def emit(record)
|
|
87
|
+
@event_stream&.emit(Insika::Event.new(
|
|
88
|
+
type: :artifact_saved,
|
|
89
|
+
data: { id: record.id, title: record.title },
|
|
90
|
+
meta: { task_id: @turn_context&.dig(:task_id) }
|
|
91
|
+
))
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
end
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "ruby_llm"
|
|
4
|
+
require "time"
|
|
5
|
+
|
|
6
|
+
module Insika
|
|
7
|
+
module Tools
|
|
8
|
+
# `schedule` — the agent books a follow-up with a customer at
|
|
9
|
+
# a future time. The tool ONLY validates shape + the dedup rule (D6/D7):
|
|
10
|
+
# everything that can block (contact state, frequency, quiet hours, a
|
|
11
|
+
# malformed policy) is decided at FIRE time by the policy in force then —
|
|
12
|
+
# the schedule is a promise made in-conversation. The consent record IS
|
|
13
|
+
# the tool call itself: the customer agreeing in-conversation writes
|
|
14
|
+
# :granted.
|
|
15
|
+
#
|
|
16
|
+
# System builtin (like remember): `require "ruby_llm"` stays in THIS file,
|
|
17
|
+
# loaded lazily by the Executor in create_chat. Never enveloped.
|
|
18
|
+
class ScheduleFollowup < RubyLLM::Tool
|
|
19
|
+
description "Schedule a follow-up with this customer at a future time. Use " \
|
|
20
|
+
"when the customer agrees to be contacted again (a product, a " \
|
|
21
|
+
"cart, a pending payment). The cancellation policy is permanent: " \
|
|
22
|
+
"a customer who opted out can never be rescheduled."
|
|
23
|
+
param :at, desc: "ISO 8601 (absolute) or relative '+6h' / '+2d'"
|
|
24
|
+
param :reason, desc: "Short machine-readable reason, e.g. 'pix pending, " \
|
|
25
|
+
"customer said she would pay tonight'"
|
|
26
|
+
|
|
27
|
+
def name = "schedule"
|
|
28
|
+
|
|
29
|
+
def initialize(contact_store:, followup_store:, state:, event_stream: nil, **)
|
|
30
|
+
@contact_store = contact_store
|
|
31
|
+
@followup_store = followup_store
|
|
32
|
+
@state = state
|
|
33
|
+
@event_stream = event_stream
|
|
34
|
+
super()
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def execute(at:, reason:)
|
|
38
|
+
followup = @state.profile.followup
|
|
39
|
+
# D9: a malformed policy is a tool error naming the rule (the
|
|
40
|
+
# ValidationError rescue below).
|
|
41
|
+
Insika::FollowupPolicy.parse!(followup)
|
|
42
|
+
|
|
43
|
+
resolved = resolve_at(at)
|
|
44
|
+
return { error: resolved } if resolved.is_a?(String)
|
|
45
|
+
|
|
46
|
+
reason = reason.to_s.strip
|
|
47
|
+
return { error: "reason is required (1..200 chars)" } if reason.empty? || reason.length > 200
|
|
48
|
+
|
|
49
|
+
at_time = resolved
|
|
50
|
+
return { error: "the follow-up must be at least 5 minutes in the future" } if at_time < Time.now.utc + 300
|
|
51
|
+
|
|
52
|
+
tenant = @state.respond_to?(:tenant) ? @state.tenant : nil
|
|
53
|
+
# D7: the tool call IS the consent — recorded WITHOUT un-silencing a
|
|
54
|
+
# silent customer (only a customer message reopens, D2). A revoked
|
|
55
|
+
# customer raises (the error names the opt-out).
|
|
56
|
+
@contact_store.consent(tenant: tenant, customer: customer)
|
|
57
|
+
record = @followup_store.create(
|
|
58
|
+
tenant: tenant, agent: @state.profile.id, customer: customer,
|
|
59
|
+
session_id: @state.task&.session_id, at: at_time, reason: reason,
|
|
60
|
+
arm: arm(followup), transport: transport
|
|
61
|
+
)
|
|
62
|
+
emit(record.id, record.at)
|
|
63
|
+
{ scheduled: record.id, at: record.at, reason: record.reason }
|
|
64
|
+
rescue Insika::ValidationError => e
|
|
65
|
+
{ error: e.message }
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
# The customer is the SAME string the message contract carries — from
|
|
71
|
+
# the running task's command, never from the model's arguments.
|
|
72
|
+
def customer
|
|
73
|
+
command = @state.task&.command
|
|
74
|
+
return nil unless command.is_a?(Hash)
|
|
75
|
+
|
|
76
|
+
payload = command["payload"] || command[:payload] || {}
|
|
77
|
+
payload["customer"] || payload[:customer]
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def arm(followup)
|
|
81
|
+
return Insika::FollowupPolicy::DEFAULT_ARM unless followup.is_a?(Hash)
|
|
82
|
+
|
|
83
|
+
arm = followup["arm"].to_s
|
|
84
|
+
arm.empty? ? Insika::FollowupPolicy::DEFAULT_ARM : arm
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The transport is provenance captured at schedule time (D5): the fired
|
|
88
|
+
# turn's reply travels out of band through the SAME channel.
|
|
89
|
+
def transport
|
|
90
|
+
command = @state.task&.command
|
|
91
|
+
return nil unless command.is_a?(Hash)
|
|
92
|
+
|
|
93
|
+
meta = command["meta"] || command[:meta] || {}
|
|
94
|
+
meta["transport"] || meta[:transport]
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# ISO-8601 UTC or `+(\d+)([mhd])+` relative forms; anything else is a
|
|
98
|
+
# tool error. -> Time | String (the error).
|
|
99
|
+
def resolve_at(raw)
|
|
100
|
+
text = raw.to_s.strip
|
|
101
|
+
if (m = text.match(/\A\+(\d+)([mhd])\z/))
|
|
102
|
+
seconds = m[1].to_i * { "m" => 60, "h" => 3600, "d" => 86_400 }.fetch(m[2])
|
|
103
|
+
return Time.now.utc + seconds
|
|
104
|
+
end
|
|
105
|
+
begin
|
|
106
|
+
Time.iso8601(text).utc
|
|
107
|
+
rescue ArgumentError
|
|
108
|
+
return "`at` must be ISO 8601 (absolute) or relative '+6h' / '+2d'"
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# :followup_scheduled carries ids + at, NEVER the reason text — a reason
|
|
113
|
+
# is contact data that rides the record and the Studio, not the events.
|
|
114
|
+
def emit(id, at)
|
|
115
|
+
@event_stream&.emit(Insika::Event.new(
|
|
116
|
+
type: :followup_scheduled,
|
|
117
|
+
data: { id: id, at: at },
|
|
118
|
+
meta: { task_id: @state.task&.id, session_id: @state.task&.session_id }
|
|
119
|
+
))
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# `cancel_followup` — the sibling of `schedule`. Refuses a
|
|
124
|
+
# record that is already fired (":fired — it is in the air; it fires
|
|
125
|
+
# once"), refuses a record of another tenant (WS1), and is an idempotent
|
|
126
|
+
# no-op for an already-cancelled one.
|
|
127
|
+
class CancelFollowup < RubyLLM::Tool
|
|
128
|
+
description "Cancel a previously scheduled follow-up by its id. A follow-up " \
|
|
129
|
+
"that already fired cannot be cancelled."
|
|
130
|
+
param :id, desc: "The id returned by the schedule tool"
|
|
131
|
+
|
|
132
|
+
def name = "cancel_followup"
|
|
133
|
+
|
|
134
|
+
def initialize(followup_store:, state:, **)
|
|
135
|
+
@followup_store = followup_store
|
|
136
|
+
@state = state
|
|
137
|
+
super()
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def execute(id:)
|
|
141
|
+
record = @followup_store.find(id.to_s)
|
|
142
|
+
return { error: "no follow-up with id #{id}" } if record.nil?
|
|
143
|
+
if record.status == "fired"
|
|
144
|
+
return { error: "follow-up #{id} is already fired — it is in the air; it fires once" }
|
|
145
|
+
end
|
|
146
|
+
if record.status == "blocked"
|
|
147
|
+
return { error: "follow-up #{id} is already blocked (#{record.blocked_reason})" }
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
tenant = (@state.respond_to?(:tenant) ? @state.tenant : nil).to_s
|
|
151
|
+
unless record.tenant == (tenant.empty? ? "platform" : tenant)
|
|
152
|
+
return { error: "follow-up #{id} belongs to another tenant" }
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
return { cancelled: record.id } if record.status == "cancelled" # idempotent
|
|
156
|
+
|
|
157
|
+
@followup_store.cancel(id: record.id)
|
|
158
|
+
{ cancelled: record.id }
|
|
159
|
+
rescue Insika::ValidationError => e
|
|
160
|
+
{ error: e.message }
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|